2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token
=
56 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant
{
62 /* floating-point literal */
66 /* %<__FUNCTION__%> */
68 /* %<__PRETTY_FUNCTION__%> */
76 /* %<typeid%> operator */
78 /* non-constant compound literals */
86 /* an array reference */
92 /* the address of a label */
106 /* calls to overloaded operators */
110 /* a comma operator */
112 /* a call to a constructor */
114 /* a transaction expression */
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error
{
124 /* is not a class or namespace */
126 /* is not a class, namespace, or enumeration */
130 /* The various kinds of required token */
131 enum required_token
{
133 RT_SEMICOLON
, /* ';' */
134 RT_OPEN_PAREN
, /* '(' */
135 RT_CLOSE_BRACE
, /* '}' */
136 RT_OPEN_BRACE
, /* '{' */
137 RT_CLOSE_SQUARE
, /* ']' */
138 RT_OPEN_SQUARE
, /* '[' */
142 RT_GREATER
, /* '>' */
144 RT_ELLIPSIS
, /* '...' */
148 RT_COLON_SCOPE
, /* ':' or '::' */
149 RT_CLOSE_PAREN
, /* ')' */
150 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
151 RT_PRAGMA_EOL
, /* end of line */
152 RT_NAME
, /* identifier */
154 /* The type is CPP_KEYWORD */
156 RT_DELETE
, /* delete */
157 RT_RETURN
, /* return */
158 RT_WHILE
, /* while */
159 RT_EXTERN
, /* extern */
160 RT_STATIC_ASSERT
, /* static_assert */
161 RT_DECLTYPE
, /* decltype */
162 RT_OPERATOR
, /* operator */
163 RT_CLASS
, /* class */
164 RT_TEMPLATE
, /* template */
165 RT_NAMESPACE
, /* namespace */
166 RT_USING
, /* using */
169 RT_CATCH
, /* catch */
170 RT_THROW
, /* throw */
171 RT_LABEL
, /* __label__ */
172 RT_AT_TRY
, /* @try */
173 RT_AT_SYNCHRONIZED
, /* @synchronized */
174 RT_AT_THROW
, /* @throw */
176 RT_SELECT
, /* selection-statement */
177 RT_ITERATION
, /* iteration-statement */
178 RT_JUMP
, /* jump-statement */
179 RT_CLASS_KEY
, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
194 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
196 saved (parser
->in_type_id_in_expr_p
)
197 { parser
->in_type_id_in_expr_p
= set
; }
198 ~type_id_in_expr_sentinel ()
199 { parser
->in_type_id_in_expr_p
= saved
; }
204 static cp_lexer
*cp_lexer_new_main
206 static cp_lexer
*cp_lexer_new_from_tokens
207 (cp_token_cache
*tokens
);
208 static void cp_lexer_destroy
210 static int cp_lexer_saving_tokens
212 static cp_token
*cp_lexer_token_at
213 (cp_lexer
*, cp_token_position
);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer
*, cp_token
*);
216 static inline cp_token
*cp_lexer_peek_token
218 static cp_token
*cp_lexer_peek_nth_token
219 (cp_lexer
*, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer
*, enum cpp_ttype
);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer
*, enum cpp_ttype
);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer
*, enum rid
);
226 static cp_token
*cp_lexer_consume_token
228 static void cp_lexer_purge_token
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer
*, cp_token_position
);
232 static void cp_lexer_save_tokens
234 static void cp_lexer_commit_tokens
236 static void cp_lexer_rollback_tokens
238 static void cp_lexer_print_token
239 (FILE *, cp_token
*);
240 static inline bool cp_lexer_debugging_p
242 static void cp_lexer_start_debugging
243 (cp_lexer
*) ATTRIBUTE_UNUSED
;
244 static void cp_lexer_stop_debugging
245 (cp_lexer
*) ATTRIBUTE_UNUSED
;
247 static cp_token_cache
*cp_token_cache_new
248 (cp_token
*, cp_token
*);
250 static void cp_parser_initial_pragma
253 static bool cp_parser_omp_declare_reduction_exprs
255 static void cp_finalize_oacc_routine
256 (cp_parser
*, tree
, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream
;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand
;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
278 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
279 cp_token
*start_token
, unsigned num
,
280 cp_token
*curr_token
)
282 unsigned i
, nprinted
;
286 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
292 num
= buffer
->length ();
294 if (start_token
== NULL
)
295 start_token
= buffer
->address ();
297 if (start_token
> buffer
->address ())
299 cp_lexer_print_token (file
, &(*buffer
)[0]);
300 fprintf (file
, " ... ");
305 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
307 if (token
== start_token
)
314 if (token
== curr_token
)
315 fprintf (file
, "[[");
317 cp_lexer_print_token (file
, token
);
319 if (token
== curr_token
)
320 fprintf (file
, "]]");
326 case CPP_CLOSE_BRACE
:
336 if (i
== num
&& i
< buffer
->length ())
338 fprintf (file
, " ... ");
339 cp_lexer_print_token (file
, &buffer
->last ());
342 fprintf (file
, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
349 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
351 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
355 debug (vec
<cp_token
, va_gc
> &ref
)
357 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
361 debug (vec
<cp_token
, va_gc
> *ptr
)
366 fprintf (stderr
, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
374 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
378 fprintf (file
, "%s: ", desc
);
379 print_node_brief (file
, "", t
, 0);
384 /* Dump parser context C to FILE. */
387 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
389 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
391 print_node_brief (file
, "", c
->object_type
, 0);
392 fprintf (file
, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
399 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
402 cp_parser_context
*c
;
404 fprintf (file
, "Parsing context stack:\n");
405 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
407 fprintf (file
, "\t#%u: ", i
);
408 cp_debug_print_context (file
, c
);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
416 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
419 fprintf (file
, "%s: true\n", desc
);
423 /* Print an unparsed function entry UF to FILE. */
426 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
429 cp_default_arg_entry
*default_arg_fn
;
432 fprintf (file
, "\tFunctions with default args:\n");
434 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
437 fprintf (file
, "\t\tClass type: ");
438 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
439 fprintf (file
, "\t\tDeclaration: ");
440 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
441 fprintf (file
, "\n");
444 fprintf (file
, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
448 print_node_brief (file
, "", fn
, 0);
451 fprintf (file
, "\n");
453 fprintf (file
, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
457 print_node_brief (file
, "", fn
, 0);
460 fprintf (file
, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
467 cp_debug_print_unparsed_queues (FILE *file
,
468 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
471 cp_unparsed_functions_entry
*uf
;
473 fprintf (file
, "Unparsed functions\n");
474 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
476 fprintf (file
, "#%u:\n", i
);
477 cp_debug_print_unparsed_function (file
, uf
);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
486 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
488 cp_token
*next_token
, *first_token
, *start_token
;
493 next_token
= parser
->lexer
->next_token
;
494 first_token
= parser
->lexer
->buffer
->address ();
495 start_token
= (next_token
> first_token
+ window_size
/ 2)
496 ? next_token
- window_size
/ 2
498 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
507 cp_debug_parser (FILE *file
, cp_parser
*parser
)
509 const size_t window_size
= 20;
511 expanded_location eloc
;
516 fprintf (file
, "Parser state\n\n");
517 fprintf (file
, "Number of tokens: %u\n",
518 vec_safe_length (parser
->lexer
->buffer
));
519 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
520 cp_debug_print_tree_if_set (file
, "Object scope",
521 parser
->object_scope
);
522 cp_debug_print_tree_if_set (file
, "Qualifying scope",
523 parser
->qualifying_scope
);
524 cp_debug_print_context_stack (file
, parser
->context
);
525 cp_debug_print_flag (file
, "Allow GNU extensions",
526 parser
->allow_gnu_extensions_p
);
527 cp_debug_print_flag (file
, "'>' token is greater-than",
528 parser
->greater_than_is_operator_p
);
529 cp_debug_print_flag (file
, "Default args allowed in current "
530 "parameter list", parser
->default_arg_ok_p
);
531 cp_debug_print_flag (file
, "Parsing integral constant-expression",
532 parser
->integral_constant_expression_p
);
533 cp_debug_print_flag (file
, "Allow non-constant expression in current "
534 "constant-expression",
535 parser
->allow_non_integral_constant_expression_p
);
536 cp_debug_print_flag (file
, "Seen non-constant expression",
537 parser
->non_integral_constant_expression_p
);
538 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
540 parser
->local_variables_forbidden_p
);
541 cp_debug_print_flag (file
, "In unbraced linkage specification",
542 parser
->in_unbraced_linkage_specification_p
);
543 cp_debug_print_flag (file
, "Parsing a declarator",
544 parser
->in_declarator_p
);
545 cp_debug_print_flag (file
, "In template argument list",
546 parser
->in_template_argument_list_p
);
547 cp_debug_print_flag (file
, "Parsing an iteration statement",
548 parser
->in_statement
& IN_ITERATION_STMT
);
549 cp_debug_print_flag (file
, "Parsing a switch statement",
550 parser
->in_statement
& IN_SWITCH_STMT
);
551 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
552 parser
->in_statement
& IN_OMP_BLOCK
);
553 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
554 parser
->in_statement
& IN_OMP_FOR
);
555 cp_debug_print_flag (file
, "Parsing an if statement",
556 parser
->in_statement
& IN_IF_STMT
);
557 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
558 "context", parser
->in_type_id_in_expr_p
);
559 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
560 parser
->implicit_extern_c
);
561 cp_debug_print_flag (file
, "String expressions should be translated "
562 "to execution character set",
563 parser
->translate_strings_p
);
564 cp_debug_print_flag (file
, "Parsing function body outside of a "
565 "local class", parser
->in_function_body
);
566 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
567 parser
->colon_corrects_to_scope_p
);
568 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
569 parser
->colon_doesnt_start_class_def_p
);
570 if (parser
->type_definition_forbidden_message
)
571 fprintf (file
, "Error message for forbidden type definitions: %s\n",
572 parser
->type_definition_forbidden_message
);
573 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
574 fprintf (file
, "Number of class definitions in progress: %u\n",
575 parser
->num_classes_being_defined
);
576 fprintf (file
, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser
->num_template_parameter_lists
);
578 cp_debug_parser_tokens (file
, parser
, window_size
);
579 token
= parser
->lexer
->next_token
;
580 fprintf (file
, "Next token to parse:\n");
581 fprintf (file
, "\tToken: ");
582 cp_lexer_print_token (file
, token
);
583 eloc
= expand_location (token
->location
);
584 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
585 fprintf (file
, "\tLine: %d\n", eloc
.line
);
586 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
590 debug (cp_parser
&ref
)
592 cp_debug_parser (stderr
, &ref
);
596 debug (cp_parser
*ptr
)
601 fprintf (stderr
, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
607 cp_lexer_alloc (void)
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
616 /* Initially we are not debugging. */
617 lexer
->debugging_p
= false;
619 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
621 /* Create the buffer. */
622 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
632 cp_lexer_new_main (void)
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token
);
642 lexer
= cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer
->buffer
->quick_push (token
);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token
.type
!= CPP_EOF
)
650 cp_lexer_get_preprocessor_token (lexer
, &token
);
651 vec_safe_push (lexer
->buffer
, token
);
654 lexer
->last_token
= lexer
->buffer
->address ()
655 + lexer
->buffer
->length ()
657 lexer
->next_token
= lexer
->buffer
->length ()
658 ? lexer
->buffer
->address ()
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
665 gcc_assert (!lexer
->next_token
->purged_p
);
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
673 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
675 cp_token
*first
= cache
->first
;
676 cp_token
*last
= cache
->last
;
677 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
679 /* We do not own the buffer. */
680 lexer
->buffer
= NULL
;
681 lexer
->next_token
= first
== last
? &eof_token
: first
;
682 lexer
->last_token
= last
;
684 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
686 /* Initially we are not debugging. */
687 lexer
->debugging_p
= false;
689 gcc_assert (!lexer
->next_token
->purged_p
);
693 /* Frees all resources associated with LEXER. */
696 cp_lexer_destroy (cp_lexer
*lexer
)
698 vec_free (lexer
->buffer
);
699 lexer
->saved_tokens
.release ();
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
713 cp_lexer_debugging_p (cp_lexer
*lexer
)
715 if (!LEXER_DEBUGGING_ENABLED_P
)
718 return lexer
->debugging_p
;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
725 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
727 return lexer
->next_token
- previous_p
;
730 static inline cp_token
*
731 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
737 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
739 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer
*lexer
)
745 if (lexer
->next_token
== &eof_token
)
746 return lexer
->last_token
- 1;
748 return cp_lexer_token_position (lexer
, true);
751 static inline cp_token
*
752 cp_lexer_previous_token (cp_lexer
*lexer
)
754 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
756 /* Skip past purged tokens. */
759 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
763 return cp_lexer_token_at (lexer
, tp
);
766 /* nonzero if we are presently saving tokens. */
769 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
771 return lexer
->saved_tokens
.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
780 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
782 static int is_extern_c
= 0;
784 /* Get a new token from the preprocessor. */
786 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
787 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
788 token
->keyword
= RID_MAX
;
789 token
->purged_p
= false;
790 token
->error_reported
= false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c
+= pending_lang_change
;
796 pending_lang_change
= 0;
797 token
->implicit_extern_c
= is_extern_c
> 0;
799 /* Check to see if this token is a keyword. */
800 if (token
->type
== CPP_NAME
)
802 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
804 /* Mark this token as a keyword. */
805 token
->type
= CPP_KEYWORD
;
806 /* Record which keyword. */
807 token
->keyword
= C_RID_CODE (token
->u
.value
);
811 if (warn_cxx11_compat
812 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
813 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
815 /* Warn about the C++0x keyword (but still treat it as
817 warning (OPT_Wc__11_compat
,
818 "identifier %qE is a keyword in C++11",
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
826 token
->keyword
= RID_MAX
;
829 else if (token
->type
== CPP_AT_NAME
)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token
->type
= CPP_KEYWORD
;
833 switch (C_RID_CODE (token
->u
.value
))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
843 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
844 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
845 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
846 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
847 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
848 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
849 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
850 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
851 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
856 /* Update the globals input_location and the input file stack from TOKEN. */
858 cp_lexer_set_source_position_from_token (cp_token
*token
)
860 if (token
->type
!= CPP_EOF
)
862 input_location
= token
->location
;
866 /* Update the globals input_location and the input file stack from LEXER. */
868 cp_lexer_set_source_position (cp_lexer
*lexer
)
870 cp_token
*token
= cp_lexer_peek_token (lexer
);
871 cp_lexer_set_source_position_from_token (token
);
874 /* Return a pointer to the next token in the token stream, but do not
877 static inline cp_token
*
878 cp_lexer_peek_token (cp_lexer
*lexer
)
880 if (cp_lexer_debugging_p (lexer
))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
883 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
884 putc ('\n', cp_lexer_debug_stream
);
886 return lexer
->next_token
;
889 /* Return true if the next token has the indicated TYPE. */
892 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
894 return cp_lexer_peek_token (lexer
)->type
== type
;
897 /* Return true if the next token does not have the indicated TYPE. */
900 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
902 return !cp_lexer_next_token_is (lexer
, type
);
905 /* Return true if the next token is the indicated KEYWORD. */
908 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
910 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
914 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
916 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
920 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
922 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
925 /* Return true if the next token is not the indicated KEYWORD. */
928 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
930 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
933 /* Return true if KEYWORD can start a decl-specifier. */
936 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
943 /* Storage classes. */
949 /* Elaborated type specifiers. */
955 /* Simple type specifiers. */
969 /* GNU extensions. */
972 /* C++0x extensions. */
974 case RID_UNDERLYING_TYPE
:
979 if (keyword
>= RID_FIRST_INT_N
980 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
981 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
987 /* Return true if the next token is a keyword for a decl-specifier. */
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
994 token
= cp_lexer_peek_token (lexer
);
995 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
998 /* Returns TRUE iff the token T begins a decltype type. */
1001 token_is_decltype (cp_token
*t
)
1003 return (t
->keyword
== RID_DECLTYPE
1004 || t
->type
== CPP_DECLTYPE
);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1010 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1012 cp_token
*t
= cp_lexer_peek_token (lexer
);
1013 return token_is_decltype (t
);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1020 saved_checks_value (struct tree_check
*check_value
)
1022 /* Perform any access checks that were deferred. */
1023 vec
<deferred_access_check
, va_gc
> *checks
;
1024 deferred_access_check
*chk
;
1025 checks
= check_value
->checks
;
1029 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1030 perform_or_defer_access_check (chk
->binfo
,
1032 chk
->diag_decl
, tf_warning_or_error
);
1034 /* Return the stored value. */
1035 return check_value
->value
;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1045 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1049 /* N is 1-based, not zero-based. */
1052 if (cp_lexer_debugging_p (lexer
))
1053 fprintf (cp_lexer_debug_stream
,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1057 token
= lexer
->next_token
;
1058 gcc_assert (!n
|| token
!= &eof_token
);
1062 if (token
== lexer
->last_token
)
1068 if (!token
->purged_p
)
1072 if (cp_lexer_debugging_p (lexer
))
1074 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1075 putc ('\n', cp_lexer_debug_stream
);
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1085 cp_lexer_consume_token (cp_lexer
* lexer
)
1087 cp_token
*token
= lexer
->next_token
;
1089 gcc_assert (token
!= &eof_token
);
1090 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1094 lexer
->next_token
++;
1095 if (lexer
->next_token
== lexer
->last_token
)
1097 lexer
->next_token
= &eof_token
;
1102 while (lexer
->next_token
->purged_p
);
1104 cp_lexer_set_source_position_from_token (token
);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer
))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1110 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1111 putc ('\n', cp_lexer_debug_stream
);
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1122 cp_lexer_purge_token (cp_lexer
*lexer
)
1124 cp_token
*tok
= lexer
->next_token
;
1126 gcc_assert (tok
!= &eof_token
);
1127 tok
->purged_p
= true;
1128 tok
->location
= UNKNOWN_LOCATION
;
1129 tok
->u
.value
= NULL_TREE
;
1130 tok
->keyword
= RID_MAX
;
1135 if (tok
== lexer
->last_token
)
1141 while (tok
->purged_p
);
1142 lexer
->next_token
= tok
;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1150 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1152 cp_token
*peek
= lexer
->next_token
;
1154 if (peek
== &eof_token
)
1155 peek
= lexer
->last_token
;
1157 gcc_assert (tok
< peek
);
1159 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1161 tok
->purged_p
= true;
1162 tok
->location
= UNKNOWN_LOCATION
;
1163 tok
->u
.value
= NULL_TREE
;
1164 tok
->keyword
= RID_MAX
;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1172 cp_lexer_save_tokens (cp_lexer
* lexer
)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer
))
1176 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1178 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1181 /* Commit to the portion of the token stream most recently saved. */
1184 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer
))
1188 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1190 lexer
->saved_tokens
.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1197 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer
))
1201 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1203 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1211 struct saved_token_sentinel
1216 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1218 len
= lexer
->saved_tokens
.length ();
1219 cp_lexer_save_tokens (lexer
);
1223 cp_lexer_rollback_tokens (lexer
);
1226 ~saved_token_sentinel()
1229 cp_lexer_commit_tokens (lexer
);
1230 gcc_assert (lexer
->saved_tokens
.length () == len
);
1234 /* Print a representation of the TOKEN on the STREAM. */
1237 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names
[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1248 /* C++ parser token types - see "Manifest constants", above. */
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token
->type
)
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token
->u
.value
))
1264 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1271 case CPP_UTF8STRING
:
1272 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1276 print_generic_expr (stream
, token
->u
.value
);
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token
->type
< ARRAY_SIZE(token_names
))
1283 fputs (token_names
[token
->type
], stream
);
1285 fprintf (stream
, "[%d]", token
->type
);
1291 debug (cp_token
&ref
)
1293 cp_lexer_print_token (stderr
, &ref
);
1294 fprintf (stderr
, "\n");
1298 debug (cp_token
*ptr
)
1303 fprintf (stderr
, "<nil>\n");
1307 /* Start emitting debugging information. */
1310 cp_lexer_start_debugging (cp_lexer
* lexer
)
1312 if (!LEXER_DEBUGGING_ENABLED_P
)
1313 fatal_error (input_location
,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer
->debugging_p
= true;
1317 cp_lexer_debug_stream
= stderr
;
1320 /* Stop emitting debugging information. */
1323 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1325 if (!LEXER_DEBUGGING_ENABLED_P
)
1326 fatal_error (input_location
,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer
->debugging_p
= false;
1330 cp_lexer_debug_stream
= NULL
;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache
*
1336 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1338 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1339 cache
->first
= first
;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1348 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1350 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser
->omp_declare_simd
= NULL
;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1362 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1364 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1366 if (fndecl
== error_mark_node
)
1368 parser
->omp_declare_simd
= NULL
;
1371 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1373 cp_ensure_no_omp_declare_simd (parser
);
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1383 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1385 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1387 error_at (parser
->oacc_routine
->loc
,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser
->oacc_routine
= NULL
;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1399 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1401 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator
*make_call_declarator
1412 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1413 static cp_declarator
*make_array_declarator
1414 (cp_declarator
*, tree
);
1415 static cp_declarator
*make_pointer_declarator
1416 (cp_cv_quals
, cp_declarator
*, tree
);
1417 static cp_declarator
*make_reference_declarator
1418 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1419 static cp_declarator
*make_ptrmem_declarator
1420 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1422 /* An erroneous declarator. */
1423 static cp_declarator
*cp_error_declarator
;
1425 /* The obstack on which declarators and related data structures are
1427 static struct obstack declarator_obstack
;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes
)
1434 return obstack_alloc (&declarator_obstack
, bytes
);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator
*
1441 make_declarator (cp_declarator_kind kind
)
1443 cp_declarator
*declarator
;
1445 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1446 declarator
->kind
= kind
;
1447 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1448 declarator
->attributes
= NULL_TREE
;
1449 declarator
->std_attributes
= NULL_TREE
;
1450 declarator
->declarator
= NULL
;
1451 declarator
->parameter_pack_p
= false;
1452 declarator
->id_loc
= UNKNOWN_LOCATION
;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1463 static cp_declarator
*
1464 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1465 special_function_kind sfk
)
1467 cp_declarator
*declarator
;
1469 /* It is valid to write:
1471 class C { void f(); };
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1479 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1480 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1482 gcc_assert (identifier_p (unqualified_name
)
1483 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1486 declarator
= make_declarator (cdk_id
);
1487 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1488 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1489 declarator
->u
.id
.sfk
= sfk
;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1503 cp_declarator
*declarator
;
1505 declarator
= make_declarator (cdk_pointer
);
1506 declarator
->declarator
= target
;
1507 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1508 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1511 declarator
->id_loc
= target
->id_loc
;
1512 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1513 target
->parameter_pack_p
= false;
1516 declarator
->parameter_pack_p
= false;
1518 declarator
->std_attributes
= attributes
;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1528 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1529 bool rvalue_ref
, tree attributes
)
1531 cp_declarator
*declarator
;
1533 declarator
= make_declarator (cdk_reference
);
1534 declarator
->declarator
= target
;
1535 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1536 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1539 declarator
->id_loc
= target
->id_loc
;
1540 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1541 target
->parameter_pack_p
= false;
1544 declarator
->parameter_pack_p
= false;
1546 declarator
->std_attributes
= attributes
;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1557 cp_declarator
*pointee
,
1560 cp_declarator
*declarator
;
1562 declarator
= make_declarator (cdk_ptrmem
);
1563 declarator
->declarator
= pointee
;
1564 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1565 declarator
->u
.pointer
.class_type
= class_type
;
1569 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1570 pointee
->parameter_pack_p
= false;
1573 declarator
->parameter_pack_p
= false;
1575 declarator
->std_attributes
= attributes
;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1586 make_call_declarator (cp_declarator
*target
,
1588 cp_cv_quals cv_qualifiers
,
1589 cp_virt_specifiers virt_specifiers
,
1590 cp_ref_qualifier ref_qualifier
,
1592 tree exception_specification
,
1593 tree late_return_type
,
1594 tree requires_clause
)
1596 cp_declarator
*declarator
;
1598 declarator
= make_declarator (cdk_function
);
1599 declarator
->declarator
= target
;
1600 declarator
->u
.function
.parameters
= parms
;
1601 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1602 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1603 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1604 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1605 declarator
->u
.function
.exception_specification
= exception_specification
;
1606 declarator
->u
.function
.late_return_type
= late_return_type
;
1607 declarator
->u
.function
.requires_clause
= requires_clause
;
1610 declarator
->id_loc
= target
->id_loc
;
1611 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1612 target
->parameter_pack_p
= false;
1615 declarator
->parameter_pack_p
= false;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1624 make_array_declarator (cp_declarator
*element
, tree bounds
)
1626 cp_declarator
*declarator
;
1628 declarator
= make_declarator (cdk_array
);
1629 declarator
->declarator
= element
;
1630 declarator
->u
.array
.bounds
= bounds
;
1633 declarator
->id_loc
= element
->id_loc
;
1634 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1635 element
->parameter_pack_p
= false;
1638 declarator
->parameter_pack_p
= false;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1646 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1648 if (declarator
&& declarator
->parameter_pack_p
)
1649 /* We already saw an ellipsis. */
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1657 while (declarator
&& !found
)
1659 switch ((int)declarator
->kind
)
1671 declarator
= declarator
->declarator
;
1679 cp_parameter_declarator
*no_parameters
;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator
*
1685 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1686 cp_declarator
*declarator
,
1687 tree default_argument
,
1689 bool template_parameter_pack_p
= false)
1691 cp_parameter_declarator
*parameter
;
1693 parameter
= ((cp_parameter_declarator
*)
1694 alloc_declarator (sizeof (cp_parameter_declarator
)));
1695 parameter
->next
= NULL
;
1696 if (decl_specifiers
)
1697 parameter
->decl_specifiers
= *decl_specifiers
;
1699 clear_decl_specs (¶meter
->decl_specifiers
);
1700 parameter
->declarator
= declarator
;
1701 parameter
->default_argument
= default_argument
;
1702 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1703 parameter
->loc
= loc
;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1711 function_declarator_p (const cp_declarator
*declarator
)
1715 if (declarator
->kind
== cdk_function
1716 && declarator
->declarator
->kind
== cdk_id
)
1718 if (declarator
->kind
== cdk_id
1719 || declarator
->kind
== cdk_decomp
1720 || declarator
->kind
== cdk_error
)
1722 declarator
= declarator
->declarator
;
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1788 CP_PARSER_FLAGS_NONE
= 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags
;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT
,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED
,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1828 PREC_LOGICAL_OR_EXPRESSION
,
1829 PREC_LOGICAL_AND_EXPRESSION
,
1830 PREC_INCLUSIVE_OR_EXPRESSION
,
1831 PREC_EXCLUSIVE_OR_EXPRESSION
,
1832 PREC_AND_EXPRESSION
,
1833 PREC_EQUALITY_EXPRESSION
,
1834 PREC_RELATIONAL_EXPRESSION
,
1835 PREC_SHIFT_EXPRESSION
,
1836 PREC_ADDITIVE_EXPRESSION
,
1837 PREC_MULTIPLICATIVE_EXPRESSION
,
1839 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type
;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type
;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec
;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type
;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type
;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec
;
1867 /* Location of the binary operation we are parsing. */
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1879 /* Constructors and destructors. */
1881 static cp_parser_context
*cp_parser_context_new
1882 (cp_parser_context
*);
1884 /* Class variables. */
1886 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1892 static const cp_parser_binary_operations_map_node binops
[] = {
1893 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1894 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1896 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1897 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1898 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1900 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1901 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1903 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1904 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1906 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1907 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1908 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1909 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1911 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1912 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1914 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1916 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1918 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1920 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1922 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1928 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context
*
1936 cp_parser_context_new (cp_parser_context
* next
)
1938 cp_parser_context
*context
;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list
!= NULL
)
1943 /* Pull the first entry from the free list. */
1944 context
= cp_parser_context_free_list
;
1945 cp_parser_context_free_list
= context
->next
;
1946 memset (context
, 0, sizeof (*context
));
1949 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1951 /* No errors have occurred yet in this context. */
1952 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context
->object_type
= next
->object_type
;
1960 /* Thread the stack. */
1961 context
->next
= next
;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1979 push_unparsed_function_queues (cp_parser
*parser
)
1981 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1982 vec_safe_push (parser
->unparsed_queues
, e
);
1986 pop_unparsed_function_queues (cp_parser
*parser
)
1988 release_tree_vector (unparsed_funs_with_definitions
);
1989 parser
->unparsed_queues
->pop ();
1994 /* Constructors and destructors. */
1996 static cp_parser
*cp_parser_new
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser
*, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2019 static tree cp_parser_userdef_string_literal
2021 static cp_expr cp_parser_userdef_numeric_literal
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser
*, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser
*, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser
*, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser
*, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser
*, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser
*, tree
, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2049 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2050 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser
*, tree
, tree
*, tree
*);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2060 static tree cp_parser_new_expression
2062 static vec
<tree
, va_gc
> *cp_parser_new_placement
2064 static tree cp_parser_new_type_id
2065 (cp_parser
*, tree
*);
2066 static cp_declarator
*cp_parser_new_declarator_opt
2068 static cp_declarator
*cp_parser_direct_new_declarator
2070 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2072 static tree cp_parser_delete_expression
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser
*, cp_expr
);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2084 static cp_expr cp_parser_expression
2085 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2090 static cp_expr cp_parser_lambda_expression
2092 static void cp_parser_lambda_introducer
2093 (cp_parser
*, tree
);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser
*, tree
);
2096 static void cp_parser_lambda_body
2097 (cp_parser
*, tree
);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser
*, tree
);
2105 static tree cp_parser_expression_statement
2106 (cp_parser
*, tree
);
2107 static tree cp_parser_compound_statement
2108 (cp_parser
*, tree
, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser
*, tree
);
2111 static tree cp_parser_selection_statement
2112 (cp_parser
*, bool *, vec
<tree
> *);
2113 static tree cp_parser_condition
2115 static tree cp_parser_iteration_statement
2116 (cp_parser
*, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser
*, tree
*decl
);
2119 static tree cp_parser_for
2120 (cp_parser
*, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser
*, tree
, tree
, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree
, tree
*, tree
*);
2129 static tree cp_parser_range_for_member_function
2131 static tree cp_parser_jump_statement
2133 static void cp_parser_declaration_statement
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser
*, bool *, const token_indent_info
&);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2145 static void cp_parser_declaration
2147 static void cp_parser_block_declaration
2148 (cp_parser
*, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser
*, bool, tree
*);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser
*, cp_decl_specifier_seq
*);
2157 static tree cp_parser_type_specifier
2158 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2162 static tree cp_parser_type_name
2163 (cp_parser
*, bool);
2164 static tree cp_parser_type_name
2166 static tree cp_parser_nonclass_name
2167 (cp_parser
* parser
);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser
*, bool, bool);
2170 static tree cp_parser_enum_specifier
2172 static void cp_parser_enumerator_list
2173 (cp_parser
*, tree
);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser
*, tree
);
2176 static tree cp_parser_namespace_name
2178 static void cp_parser_namespace_definition
2180 static void cp_parser_namespace_body
2182 static tree cp_parser_qualified_namespace_specifier
2184 static void cp_parser_namespace_alias_definition
2186 static bool cp_parser_using_declaration
2187 (cp_parser
*, bool);
2188 static void cp_parser_using_directive
2190 static tree cp_parser_alias_declaration
2192 static void cp_parser_asm_definition
2194 static void cp_parser_linkage_specification
2196 static void cp_parser_static_assert
2197 (cp_parser
*, bool);
2198 static tree cp_parser_decltype
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2207 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2208 static cp_declarator
*cp_parser_declarator
2209 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2210 static cp_declarator
*cp_parser_direct_declarator
2211 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2220 static tree cp_parser_tx_qualifier_opt
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2224 static tree cp_parser_declarator_id
2225 (cp_parser
*, bool);
2226 static tree cp_parser_type_id
2228 static tree cp_parser_template_type_arg
2230 static tree
cp_parser_trailing_type_id (cp_parser
*);
2231 static tree cp_parser_type_id_1
2232 (cp_parser
*, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2235 static tree cp_parser_parameter_declaration_clause
2237 static tree cp_parser_parameter_declaration_list
2239 static cp_parameter_declarator
*cp_parser_parameter_declaration
2240 (cp_parser
*, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser
*, bool);
2243 static void cp_parser_function_body
2244 (cp_parser
*, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser
*, bool *, bool *, bool = false);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser
*, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser
*, bool*);
2251 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2252 (cp_parser
*, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser
*, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser
*, tree
);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser
*, tree
);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser
*, tree
);
2265 static tree finish_fully_implicit_template
2266 (cp_parser
*, tree
);
2267 static void abort_fully_implicit_template
2270 /* Classes [gram.class] */
2272 static tree cp_parser_class_name
2273 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2276 static tree cp_parser_class_head
2277 (cp_parser
*, bool *);
2278 static enum tag_types cp_parser_class_key
2280 static void cp_parser_type_parameter_key
2281 (cp_parser
* parser
);
2282 static void cp_parser_member_specification_opt
2284 static void cp_parser_member_declaration
2286 static tree cp_parser_pure_specifier
2288 static tree cp_parser_constant_initializer
2291 /* Derived classes [gram.class.derived] */
2293 static tree cp_parser_base_clause
2295 static tree cp_parser_base_specifier
2298 /* Special member functions [gram.special] */
2300 static tree cp_parser_conversion_function_id
2302 static tree cp_parser_conversion_type_id
2304 static cp_declarator
*cp_parser_conversion_declarator_opt
2306 static void cp_parser_ctor_initializer_opt
2308 static void cp_parser_mem_initializer_list
2310 static tree cp_parser_mem_initializer
2312 static tree cp_parser_mem_initializer_id
2315 /* Overloading [gram.over] */
2317 static cp_expr cp_parser_operator_function_id
2319 static cp_expr cp_parser_operator
2322 /* Templates [gram.temp] */
2324 static void cp_parser_template_declaration
2325 (cp_parser
*, bool);
2326 static tree cp_parser_template_parameter_list
2328 static tree cp_parser_template_parameter
2329 (cp_parser
*, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser
*, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser
*, bool, bool, enum tag_types
, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2336 static tree cp_parser_template_argument_list
2338 static tree cp_parser_template_argument
2340 static void cp_parser_explicit_instantiation
2342 static void cp_parser_explicit_specialization
2345 /* Exception handling [gram.exception] */
2347 static tree cp_parser_try_block
2349 static void cp_parser_function_try_block
2351 static void cp_parser_handler_seq
2353 static void cp_parser_handler
2355 static tree cp_parser_exception_declaration
2357 static tree cp_parser_throw_expression
2359 static tree cp_parser_exception_specification_opt
2361 static tree cp_parser_type_id_list
2364 /* GNU Extensions */
2366 static tree cp_parser_asm_specification_opt
2368 static tree cp_parser_asm_operand_list
2370 static tree cp_parser_asm_clobber_list
2372 static tree cp_parser_asm_label_list
2374 static bool cp_next_tokens_can_be_attribute_p
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2378 static bool cp_next_tokens_can_be_std_attribute_p
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser
*, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser
*, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser
*, size_t);
2386 static tree cp_parser_attributes_opt
2388 static tree cp_parser_gnu_attributes_opt
2390 static tree cp_parser_gnu_attribute_list
2392 static tree cp_parser_std_attribute
2393 (cp_parser
*, tree
);
2394 static tree cp_parser_std_attribute_spec
2396 static tree cp_parser_std_attribute_spec_seq
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser
*, size_t);
2400 static bool cp_parser_extension_opt
2401 (cp_parser
*, int *);
2402 static void cp_parser_label_declaration
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2409 static tree cp_parser_requires_clause_opt
2411 static tree cp_parser_requires_expression
2413 static tree cp_parser_requirement_parameter_list
2415 static tree cp_parser_requirement_body
2417 static tree cp_parser_requirement_list
2419 static tree cp_parser_requirement
2421 static tree cp_parser_simple_requirement
2423 static tree cp_parser_compound_requirement
2425 static tree cp_parser_type_requirement
2427 static tree cp_parser_nested_requirement
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser
*, cp_token
*);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser
*, enum rid
);
2436 static void cp_parser_function_transaction
2437 (cp_parser
*, enum rid
);
2438 static tree cp_parser_transaction_cancel
2441 enum pragma_context
{
2448 static bool cp_parser_pragma
2449 (cp_parser
*, enum pragma_context
, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2455 static tree cp_parser_objc_message_args
2457 static tree cp_parser_objc_message_expression
2459 static cp_expr cp_parser_objc_encode_expression
2461 static tree cp_parser_objc_defs_expression
2463 static tree cp_parser_objc_protocol_expression
2465 static tree cp_parser_objc_selector_expression
2467 static cp_expr cp_parser_objc_expression
2469 static bool cp_parser_objc_selector_p
2471 static tree cp_parser_objc_selector
2473 static tree cp_parser_objc_protocol_refs_opt
2475 static void cp_parser_objc_declaration
2476 (cp_parser
*, tree
);
2477 static tree cp_parser_objc_statement
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser
*, tree
*);
2481 static void cp_parser_objc_at_property_declaration
2483 static void cp_parser_objc_at_synthesize_declaration
2485 static void cp_parser_objc_at_dynamic_declaration
2487 static tree cp_parser_objc_struct_declaration
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser
*, tree
, location_t
);
2496 static tree cp_parser_maybe_treat_template_as_class
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser
*, cp_declarator
*, location_t
);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2502 static cp_expr cp_parser_simple_cast_expression
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser
*, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser
*, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser
*, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser
*, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec
<deferred_access_check
, va_gc
> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser
*, tree
);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2522 static tree cp_parser_save_nsdmi
2524 static tree cp_parser_enclosed_template_argument_list
2526 static void cp_parser_save_default_args
2527 (cp_parser
*, tree
);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser
*, tree
);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser
*, tree
, tree
, tree
);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser
*, tree
);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser
*, tree
);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser
*, enum rid
);
2538 static cp_expr cp_parser_trait_expr
2539 (cp_parser
*, enum rid
);
2540 static bool cp_parser_declares_only_class_p
2542 static void cp_parser_set_storage_class
2543 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq
*decl_specs
,
2548 cp_decl_spec ds
, cp_token
*);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq
*);
2551 static void cp_parser_required_error
2552 (cp_parser
*, required_token
, bool, location_t
);
2553 static cp_token
*cp_parser_require
2554 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2555 static cp_token
*cp_parser_require_keyword
2556 (cp_parser
*, enum rid
, required_token
);
2557 static bool cp_parser_token_starts_function_definition_p
2559 static bool cp_parser_next_token_starts_class_definition_p
2561 static bool cp_parser_next_token_ends_template_argument_p
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser
*, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2569 static void cp_parser_check_class_key
2570 (enum tag_types
, tree type
);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type
, location_t location
);
2573 static bool cp_parser_optional_template_keyword
2575 static void cp_parser_pre_parsed_nested_name_specifier
2577 static bool cp_parser_cache_group
2578 (cp_parser
*, enum cpp_ttype
, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser
*parser
, bool nsdmi
);
2581 static void cp_parser_parse_tentatively
2583 static void cp_parser_commit_to_tentative_parse
2585 static void cp_parser_commit_to_topmost_tentative_parse
2587 static void cp_parser_abort_tentative_parse
2589 static bool cp_parser_parse_definitely
2591 static inline bool cp_parser_parsing_tentatively
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2595 static void cp_parser_error
2596 (cp_parser
*, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2599 static bool cp_parser_simulate_error
2601 static bool cp_parser_check_type_definition
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator
*, tree
, location_t type_location
);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser
*, non_integral_constant
);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser
*, tree
, location_t
);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser
*, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2619 static void cp_parser_skip_to_end_of_block_or_statement
2621 static bool cp_parser_skip_to_closing_brace
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser
*, cp_token
*);
2627 static bool cp_parser_error_occurred
2629 static bool cp_parser_allow_gnu_extensions_p
2631 static bool cp_parser_is_pure_string_literal
2633 static bool cp_parser_is_string_literal
2635 static bool cp_parser_is_keyword
2636 (cp_token
*, enum rid
);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser
*, tree
, location_t location
);
2639 static cp_declarator
* cp_parser_make_indirect_declarator
2640 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2641 static bool cp_parser_compound_literal_p
2643 static bool cp_parser_array_designator_p
2645 static bool cp_parser_init_statement_p
2647 static bool cp_parser_skip_to_closing_square_bracket
2650 /* Concept-related syntactic transformations */
2652 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2653 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2661 ++cp_unevaluated_operand
;
2662 ++c_inhibit_evaluation_warnings
;
2665 cp_unevaluated::~cp_unevaluated ()
2667 --c_inhibit_evaluation_warnings
;
2668 --cp_unevaluated_operand
;
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2674 /* Returns nonzero if we are parsing tentatively. */
2677 cp_parser_parsing_tentatively (cp_parser
* parser
)
2679 return parser
->context
->next
!= NULL
;
2682 /* Returns nonzero if TOKEN is a string literal. */
2685 cp_parser_is_pure_string_literal (cp_token
* token
)
2687 return (token
->type
== CPP_STRING
||
2688 token
->type
== CPP_STRING16
||
2689 token
->type
== CPP_STRING32
||
2690 token
->type
== CPP_WSTRING
||
2691 token
->type
== CPP_UTF8STRING
);
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2698 cp_parser_is_string_literal (cp_token
* token
)
2700 return (cp_parser_is_pure_string_literal (token
) ||
2701 token
->type
== CPP_STRING_USERDEF
||
2702 token
->type
== CPP_STRING16_USERDEF
||
2703 token
->type
== CPP_STRING32_USERDEF
||
2704 token
->type
== CPP_WSTRING_USERDEF
||
2705 token
->type
== CPP_UTF8STRING_USERDEF
);
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2711 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2713 return token
->keyword
== keyword
;
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token
*token
)
2722 if (token
->type
!= CPP_PRAGMA
)
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2740 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2741 location_t
*out_loc
)
2743 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2744 if (token2
->type
!= tok1_kind
)
2746 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2747 if (token3
->type
!= tok1_kind
)
2749 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2750 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2753 /* It must be at the start of the line. */
2754 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2755 if (LOCATION_COLUMN (start_loc
) != 1)
2758 /* We have a conflict marker. Construct a location of the form:
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc
= get_finish (token4
->location
);
2763 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2772 get_matching_symbol (required_token token_desc
)
2779 case RT_CLOSE_BRACE
:
2781 case RT_CLOSE_PAREN
:
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc
)
2795 return CPP_SEMICOLON
;
2797 return CPP_OPEN_PAREN
;
2798 case RT_CLOSE_BRACE
:
2799 return CPP_CLOSE_BRACE
;
2801 return CPP_OPEN_BRACE
;
2802 case RT_CLOSE_SQUARE
:
2803 return CPP_CLOSE_SQUARE
;
2804 case RT_OPEN_SQUARE
:
2805 return CPP_OPEN_SQUARE
;
2810 case RT_CLOSE_PAREN
:
2811 return CPP_CLOSE_PAREN
;
2814 /* Use CPP_EOF as a "no completions possible" code. */
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2839 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2840 required_token missing_token_desc
,
2841 location_t matching_location
)
2843 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token
);
2848 if (token
->type
== CPP_PRAGMA
)
2850 error_at (token
->location
,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser
, token
);
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token
->type
== CPP_LSHIFT
2858 || token
->type
== CPP_RSHIFT
2859 || token
->type
== CPP_EQ_EQ
)
2862 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2864 error_at (loc
, "version control conflict marker in file");
2865 expanded_location token_exploc
= expand_location (token
->location
);
2866 /* Consume tokens until the end of the source line. */
2869 cp_lexer_consume_token (parser
->lexer
);
2870 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2873 expanded_location next_exploc
= expand_location (next
->location
);
2874 if (next_exploc
.file
!= token_exploc
.file
)
2876 if (next_exploc
.line
!= token_exploc
.line
)
2883 gcc_rich_location
richloc (input_location
);
2885 bool added_matching_location
= false;
2887 if (missing_token_desc
!= RT_NONE
)
2889 /* Potentially supply a fix-it hint, suggesting to add the
2890 missing token immediately after the *previous* token.
2891 This may move the primary location within richloc. */
2892 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2893 location_t prev_token_loc
2894 = cp_lexer_previous_token (parser
->lexer
)->location
;
2895 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2897 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2898 Attempt to consolidate diagnostics by printing it as a
2899 secondary range within the main diagnostic. */
2900 if (matching_location
!= UNKNOWN_LOCATION
)
2901 added_matching_location
2902 = richloc
.add_location_if_nearby (matching_location
);
2905 /* Actually emit the error. */
2906 c_parse_error (gmsgid
,
2907 /* Because c_parser_error does not understand
2908 CPP_KEYWORD, keywords are treated like
2910 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2911 token
->u
.value
, token
->flags
, &richloc
);
2913 if (missing_token_desc
!= RT_NONE
)
2915 /* If we weren't able to consolidate matching_location, then
2916 print it as a secondary diagnostic. */
2917 if (matching_location
!= UNKNOWN_LOCATION
2918 && !added_matching_location
)
2919 inform (matching_location
, "to match this %qs",
2920 get_matching_symbol (missing_token_desc
));
2924 /* If not parsing tentatively, issue a diagnostic of the form
2925 FILE:LINE: MESSAGE before TOKEN
2926 where TOKEN is the next token in the input stream. MESSAGE
2927 (specified by the caller) is usually of the form "expected
2931 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2933 if (!cp_parser_simulate_error (parser
))
2934 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2937 /* Issue an error about name-lookup failing. NAME is the
2938 IDENTIFIER_NODE DECL is the result of
2939 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2940 the thing that we hoped to find. */
2943 cp_parser_name_lookup_error (cp_parser
* parser
,
2946 name_lookup_error desired
,
2947 location_t location
)
2949 /* If name lookup completely failed, tell the user that NAME was not
2951 if (decl
== error_mark_node
)
2953 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2954 error_at (location
, "%<%E::%E%> has not been declared",
2955 parser
->scope
, name
);
2956 else if (parser
->scope
== global_namespace
)
2957 error_at (location
, "%<::%E%> has not been declared", name
);
2958 else if (parser
->object_scope
2959 && !CLASS_TYPE_P (parser
->object_scope
))
2960 error_at (location
, "request for member %qE in non-class type %qT",
2961 name
, parser
->object_scope
);
2962 else if (parser
->object_scope
)
2963 error_at (location
, "%<%T::%E%> has not been declared",
2964 parser
->object_scope
, name
);
2966 error_at (location
, "%qE has not been declared", name
);
2968 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2973 error_at (location
, "%<%E::%E%> is not a type",
2974 parser
->scope
, name
);
2977 error_at (location
, "%<%E::%E%> is not a class or namespace",
2978 parser
->scope
, name
);
2982 "%<%E::%E%> is not a class, namespace, or enumeration",
2983 parser
->scope
, name
);
2990 else if (parser
->scope
== global_namespace
)
2995 error_at (location
, "%<::%E%> is not a type", name
);
2998 error_at (location
, "%<::%E%> is not a class or namespace", name
);
3002 "%<::%E%> is not a class, namespace, or enumeration",
3014 error_at (location
, "%qE is not a type", name
);
3017 error_at (location
, "%qE is not a class or namespace", name
);
3021 "%qE is not a class, namespace, or enumeration", name
);
3029 /* If we are parsing tentatively, remember that an error has occurred
3030 during this tentative parse. Returns true if the error was
3031 simulated; false if a message should be issued by the caller. */
3034 cp_parser_simulate_error (cp_parser
* parser
)
3036 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3038 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3044 /* This function is called when a type is defined. If type
3045 definitions are forbidden at this point, an error message is
3049 cp_parser_check_type_definition (cp_parser
* parser
)
3051 /* If types are forbidden here, issue a message. */
3052 if (parser
->type_definition_forbidden_message
)
3054 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3055 in the message need to be interpreted. */
3056 error (parser
->type_definition_forbidden_message
);
3062 /* This function is called when the DECLARATOR is processed. The TYPE
3063 was a type defined in the decl-specifiers. If it is invalid to
3064 define a type in the decl-specifiers for DECLARATOR, an error is
3065 issued. TYPE_LOCATION is the location of TYPE and is used
3066 for error reporting. */
3069 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3070 tree type
, location_t type_location
)
3072 /* [dcl.fct] forbids type definitions in return types.
3073 Unfortunately, it's not easy to know whether or not we are
3074 processing a return type until after the fact. */
3076 && (declarator
->kind
== cdk_pointer
3077 || declarator
->kind
== cdk_reference
3078 || declarator
->kind
== cdk_ptrmem
))
3079 declarator
= declarator
->declarator
;
3081 && declarator
->kind
== cdk_function
)
3083 error_at (type_location
,
3084 "new types may not be defined in a return type");
3085 inform (type_location
,
3086 "(perhaps a semicolon is missing after the definition of %qT)",
3091 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3092 "<" in any valid C++ program. If the next token is indeed "<",
3093 issue a message warning the user about what appears to be an
3094 invalid attempt to form a template-id. LOCATION is the location
3095 of the type-specifier (TYPE) */
3098 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3100 enum tag_types tag_type
,
3101 location_t location
)
3103 cp_token_position start
= 0;
3105 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3107 if (TREE_CODE (type
) == TYPE_DECL
)
3108 type
= TREE_TYPE (type
);
3109 if (TYPE_P (type
) && !template_placeholder_p (type
))
3110 error_at (location
, "%qT is not a template", type
);
3111 else if (identifier_p (type
))
3113 if (tag_type
!= none_type
)
3114 error_at (location
, "%qE is not a class template", type
);
3116 error_at (location
, "%qE is not a template", type
);
3119 error_at (location
, "invalid template-id");
3120 /* Remember the location of the invalid "<". */
3121 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3122 start
= cp_lexer_token_position (parser
->lexer
, true);
3123 /* Consume the "<". */
3124 cp_lexer_consume_token (parser
->lexer
);
3125 /* Parse the template arguments. */
3126 cp_parser_enclosed_template_argument_list (parser
);
3127 /* Permanently remove the invalid template arguments so that
3128 this error message is not issued again. */
3130 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3134 /* If parsing an integral constant-expression, issue an error message
3135 about the fact that THING appeared and return true. Otherwise,
3136 return false. In either case, set
3137 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3140 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3141 non_integral_constant thing
)
3143 parser
->non_integral_constant_expression_p
= true;
3144 if (parser
->integral_constant_expression_p
)
3146 if (!parser
->allow_non_integral_constant_expression_p
)
3148 const char *msg
= NULL
;
3152 pedwarn (input_location
, OPT_Wpedantic
,
3153 "ISO C++ forbids using a floating-point literal "
3154 "in a constant-expression");
3157 error ("a cast to a type other than an integral or "
3158 "enumeration type cannot appear in a "
3159 "constant-expression");
3162 error ("%<typeid%> operator "
3163 "cannot appear in a constant-expression");
3166 error ("non-constant compound literals "
3167 "cannot appear in a constant-expression");
3170 error ("a function call "
3171 "cannot appear in a constant-expression");
3174 error ("an increment "
3175 "cannot appear in a constant-expression");
3178 error ("an decrement "
3179 "cannot appear in a constant-expression");
3182 error ("an array reference "
3183 "cannot appear in a constant-expression");
3185 case NIC_ADDR_LABEL
:
3186 error ("the address of a label "
3187 "cannot appear in a constant-expression");
3189 case NIC_OVERLOADED
:
3190 error ("calls to overloaded operators "
3191 "cannot appear in a constant-expression");
3193 case NIC_ASSIGNMENT
:
3194 error ("an assignment cannot appear in a constant-expression");
3197 error ("a comma operator "
3198 "cannot appear in a constant-expression");
3200 case NIC_CONSTRUCTOR
:
3201 error ("a call to a constructor "
3202 "cannot appear in a constant-expression");
3204 case NIC_TRANSACTION
:
3205 error ("a transaction expression "
3206 "cannot appear in a constant-expression");
3212 msg
= "__FUNCTION__";
3214 case NIC_PRETTY_FUNC
:
3215 msg
= "__PRETTY_FUNCTION__";
3235 case NIC_PREINCREMENT
:
3238 case NIC_PREDECREMENT
:
3251 error ("%qs cannot appear in a constant-expression", msg
);
3258 /* Emit a diagnostic for an invalid type name. This function commits
3259 to the current active tentative parse, if any. (Otherwise, the
3260 problematic construct might be encountered again later, resulting
3261 in duplicate error messages.) LOCATION is the location of ID. */
3264 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3265 location_t location
)
3267 tree decl
, ambiguous_decls
;
3268 cp_parser_commit_to_tentative_parse (parser
);
3269 /* Try to lookup the identifier. */
3270 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3271 /*is_template=*/false,
3272 /*is_namespace=*/false,
3273 /*check_dependency=*/true,
3274 &ambiguous_decls
, location
);
3275 if (ambiguous_decls
)
3276 /* If the lookup was ambiguous, an error will already have
3279 /* If the lookup found a template-name, it means that the user forgot
3280 to specify an argument list. Emit a useful error message. */
3281 if (DECL_TYPE_TEMPLATE_P (decl
))
3283 auto_diagnostic_group d
;
3285 "invalid use of template-name %qE without an argument list",
3287 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3288 inform (location
, "class template argument deduction is only available "
3289 "with -std=c++17 or -std=gnu++17");
3290 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3292 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3293 error_at (location
, "invalid use of destructor %qD as a type", id
);
3294 else if (TREE_CODE (decl
) == TYPE_DECL
)
3295 /* Something like 'unsigned A a;' */
3296 error_at (location
, "invalid combination of multiple type-specifiers");
3297 else if (!parser
->scope
)
3299 /* Issue an error message. */
3300 auto_diagnostic_group d
;
3302 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3303 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3306 gcc_rich_location
richloc (location
);
3307 richloc
.add_fixit_replace (hint
.suggestion ());
3309 "%qE does not name a type; did you mean %qs?",
3310 id
, hint
.suggestion ());
3313 error_at (location
, "%qE does not name a type", id
);
3314 /* If we're in a template class, it's possible that the user was
3315 referring to a type from a base class. For example:
3317 template <typename T> struct A { typedef T X; };
3318 template <typename T> struct B : public A<T> { X x; };
3320 The user should have said "typename A<T>::X". */
3321 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3322 inform (location
, "C++11 %<constexpr%> only available with "
3323 "-std=c++11 or -std=gnu++11");
3324 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3325 inform (location
, "C++11 %<noexcept%> only available with "
3326 "-std=c++11 or -std=gnu++11");
3327 else if (cxx_dialect
< cxx11
3328 && TREE_CODE (id
) == IDENTIFIER_NODE
3329 && id_equal (id
, "thread_local"))
3330 inform (location
, "C++11 %<thread_local%> only available with "
3331 "-std=c++11 or -std=gnu++11");
3332 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3333 inform (location
, "%<concept%> only available with -fconcepts");
3334 else if (processing_template_decl
&& current_class_type
3335 && TYPE_BINFO (current_class_type
))
3339 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3343 tree base_type
= BINFO_TYPE (b
);
3344 if (CLASS_TYPE_P (base_type
)
3345 && dependent_type_p (base_type
))
3348 /* Go from a particular instantiation of the
3349 template (which will have an empty TYPE_FIELDs),
3350 to the main version. */
3351 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3352 for (field
= TYPE_FIELDS (base_type
);
3354 field
= DECL_CHAIN (field
))
3355 if (TREE_CODE (field
) == TYPE_DECL
3356 && DECL_NAME (field
) == id
)
3359 "(perhaps %<typename %T::%E%> was intended)",
3360 BINFO_TYPE (b
), id
);
3369 /* Here we diagnose qualified-ids where the scope is actually correct,
3370 but the identifier does not resolve to a valid type name. */
3371 else if (parser
->scope
!= error_mark_node
)
3373 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3375 auto_diagnostic_group d
;
3376 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3377 error_at (location_of (id
),
3378 "%qE in namespace %qE does not name a template type",
3380 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3381 error_at (location_of (id
),
3382 "%qE in namespace %qE does not name a template type",
3383 TREE_OPERAND (id
, 0), parser
->scope
);
3385 error_at (location_of (id
),
3386 "%qE in namespace %qE does not name a type",
3389 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3390 else if (decl
== error_mark_node
)
3391 suggest_alternative_in_explicit_scope (location
, id
,
3394 else if (CLASS_TYPE_P (parser
->scope
)
3395 && constructor_name_p (id
, parser
->scope
))
3398 auto_diagnostic_group d
;
3399 error_at (location
, "%<%T::%E%> names the constructor, not"
3400 " the type", parser
->scope
, id
);
3401 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3402 error_at (location
, "and %qT has no template constructors",
3405 else if (TYPE_P (parser
->scope
)
3406 && dependent_scope_p (parser
->scope
))
3408 gcc_rich_location
richloc (location
);
3409 richloc
.add_fixit_insert_before ("typename ");
3410 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3412 "need %<typename%> before %<%T::%D::%E%> because "
3413 "%<%T::%D%> is a dependent scope",
3414 TYPE_CONTEXT (parser
->scope
),
3415 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3417 TYPE_CONTEXT (parser
->scope
),
3418 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3420 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3421 "%qT is a dependent scope",
3422 parser
->scope
, id
, parser
->scope
);
3424 else if (TYPE_P (parser
->scope
))
3426 auto_diagnostic_group d
;
3427 if (!COMPLETE_TYPE_P (parser
->scope
))
3428 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3430 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3431 error_at (location_of (id
),
3432 "%qE in %q#T does not name a template type",
3434 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3435 error_at (location_of (id
),
3436 "%qE in %q#T does not name a template type",
3437 TREE_OPERAND (id
, 0), parser
->scope
);
3439 error_at (location_of (id
),
3440 "%qE in %q#T does not name a type",
3443 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3450 /* Check for a common situation where a type-name should be present,
3451 but is not, and issue a sensible error message. Returns true if an
3452 invalid type-name was detected.
3454 The situation handled by this function are variable declarations of the
3455 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3456 Usually, `ID' should name a type, but if we got here it means that it
3457 does not. We try to emit the best possible error message depending on
3458 how exactly the id-expression looks like. */
3461 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3464 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3466 /* Avoid duplicate error about ambiguous lookup. */
3467 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3469 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3470 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3474 cp_parser_parse_tentatively (parser
);
3475 id
= cp_parser_id_expression (parser
,
3476 /*template_keyword_p=*/false,
3477 /*check_dependency_p=*/true,
3478 /*template_p=*/NULL
,
3479 /*declarator_p=*/false,
3480 /*optional_p=*/false);
3481 /* If the next token is a (, this is a function with no explicit return
3482 type, i.e. constructor, destructor or conversion op. */
3483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3484 || TREE_CODE (id
) == TYPE_DECL
)
3486 cp_parser_abort_tentative_parse (parser
);
3489 if (!cp_parser_parse_definitely (parser
))
3492 /* Emit a diagnostic for the invalid type. */
3493 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3495 /* If we aren't in the middle of a declarator (i.e. in a
3496 parameter-declaration-clause), skip to the end of the declaration;
3497 there's no point in trying to process it. */
3498 if (!parser
->in_declarator_p
)
3499 cp_parser_skip_to_end_of_block_or_statement (parser
);
3503 /* Consume tokens up to, and including, the next non-nested closing `)'.
3504 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3505 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3506 found an unnested token of that type. */
3509 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3514 unsigned paren_depth
= 0;
3515 unsigned brace_depth
= 0;
3516 unsigned square_depth
= 0;
3517 unsigned condop_depth
= 0;
3519 if (recovering
&& or_ttype
== CPP_EOF
3520 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3525 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3527 /* Have we found what we're looking for before the closing paren? */
3528 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3529 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3532 switch (token
->type
)
3535 case CPP_PRAGMA_EOL
:
3536 /* If we've run out of tokens, then there is no closing `)'. */
3539 /* This is good for lambda expression capture-lists. */
3540 case CPP_OPEN_SQUARE
:
3543 case CPP_CLOSE_SQUARE
:
3544 if (!square_depth
--)
3549 /* This matches the processing in skip_to_end_of_statement. */
3554 case CPP_OPEN_BRACE
:
3557 case CPP_CLOSE_BRACE
:
3562 case CPP_OPEN_PAREN
:
3567 case CPP_CLOSE_PAREN
:
3568 if (!brace_depth
&& !paren_depth
--)
3571 cp_lexer_consume_token (parser
->lexer
);
3577 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3582 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3590 /* Consume the token. */
3591 cp_lexer_consume_token (parser
->lexer
);
3595 /* Consume tokens up to, and including, the next non-nested closing `)'.
3596 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3597 are doing error recovery. Returns -1 if OR_COMMA is true and we
3598 found an unnested token of that type. */
3601 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3606 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3607 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3608 ttype
, consume_paren
);
3611 /* Consume tokens until we reach the end of the current statement.
3612 Normally, that will be just before consuming a `;'. However, if a
3613 non-nested `}' comes first, then we stop before consuming that. */
3616 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3618 unsigned nesting_depth
= 0;
3620 /* Unwind generic function template scope if necessary. */
3621 if (parser
->fully_implicit_function_template_p
)
3622 abort_fully_implicit_template (parser
);
3626 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3628 switch (token
->type
)
3631 case CPP_PRAGMA_EOL
:
3632 /* If we've run out of tokens, stop. */
3636 /* If the next token is a `;', we have reached the end of the
3642 case CPP_CLOSE_BRACE
:
3643 /* If this is a non-nested '}', stop before consuming it.
3644 That way, when confronted with something like:
3648 we stop before consuming the closing '}', even though we
3649 have not yet reached a `;'. */
3650 if (nesting_depth
== 0)
3653 /* If it is the closing '}' for a block that we have
3654 scanned, stop -- but only after consuming the token.
3660 we will stop after the body of the erroneously declared
3661 function, but before consuming the following `typedef'
3663 if (--nesting_depth
== 0)
3665 cp_lexer_consume_token (parser
->lexer
);
3670 case CPP_OPEN_BRACE
:
3678 /* Consume the token. */
3679 cp_lexer_consume_token (parser
->lexer
);
3683 /* This function is called at the end of a statement or declaration.
3684 If the next token is a semicolon, it is consumed; otherwise, error
3685 recovery is attempted. */
3688 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3690 /* Look for the trailing `;'. */
3691 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3693 /* If there is additional (erroneous) input, skip to the end of
3695 cp_parser_skip_to_end_of_statement (parser
);
3696 /* If the next token is now a `;', consume it. */
3697 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3698 cp_lexer_consume_token (parser
->lexer
);
3702 /* Skip tokens until we have consumed an entire block, or until we
3703 have consumed a non-nested `;'. */
3706 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3708 int nesting_depth
= 0;
3710 /* Unwind generic function template scope if necessary. */
3711 if (parser
->fully_implicit_function_template_p
)
3712 abort_fully_implicit_template (parser
);
3714 while (nesting_depth
>= 0)
3716 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3718 switch (token
->type
)
3721 case CPP_PRAGMA_EOL
:
3722 /* If we've run out of tokens, stop. */
3726 /* Stop if this is an unnested ';'. */
3731 case CPP_CLOSE_BRACE
:
3732 /* Stop if this is an unnested '}', or closes the outermost
3735 if (nesting_depth
< 0)
3741 case CPP_OPEN_BRACE
:
3750 /* Consume the token. */
3751 cp_lexer_consume_token (parser
->lexer
);
3755 /* Skip tokens until a non-nested closing curly brace is the next
3756 token, or there are no more tokens. Return true in the first case,
3760 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3762 unsigned nesting_depth
= 0;
3766 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3768 switch (token
->type
)
3771 case CPP_PRAGMA_EOL
:
3772 /* If we've run out of tokens, stop. */
3775 case CPP_CLOSE_BRACE
:
3776 /* If the next token is a non-nested `}', then we have reached
3777 the end of the current block. */
3778 if (nesting_depth
-- == 0)
3782 case CPP_OPEN_BRACE
:
3783 /* If it the next token is a `{', then we are entering a new
3784 block. Consume the entire block. */
3792 /* Consume the token. */
3793 cp_lexer_consume_token (parser
->lexer
);
3797 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3798 parameter is the PRAGMA token, allowing us to purge the entire pragma
3802 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3806 parser
->lexer
->in_pragma
= false;
3809 token
= cp_lexer_consume_token (parser
->lexer
);
3810 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3812 /* Ensure that the pragma is not parsed again. */
3813 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3816 /* Require pragma end of line, resyncing with it as necessary. The
3817 arguments are as for cp_parser_skip_to_pragma_eol. */
3820 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3822 parser
->lexer
->in_pragma
= false;
3823 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3824 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3827 /* This is a simple wrapper around make_typename_type. When the id is
3828 an unresolved identifier node, we can provide a superior diagnostic
3829 using cp_parser_diagnose_invalid_type_name. */
3832 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3833 location_t id_location
)
3836 if (identifier_p (id
))
3838 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3839 /*complain=*/tf_none
);
3840 if (result
== error_mark_node
)
3841 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3844 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3847 /* This is a wrapper around the
3848 make_{pointer,ptrmem,reference}_declarator functions that decides
3849 which one to call based on the CODE and CLASS_TYPE arguments. The
3850 CODE argument should be one of the values returned by
3851 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3852 appertain to the pointer or reference. */
3854 static cp_declarator
*
3855 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3856 cp_cv_quals cv_qualifiers
,
3857 cp_declarator
*target
,
3860 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3861 return cp_error_declarator
;
3863 if (code
== INDIRECT_REF
)
3864 if (class_type
== NULL_TREE
)
3865 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3867 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3868 target
, attributes
);
3869 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3870 return make_reference_declarator (cv_qualifiers
, target
,
3872 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3873 return make_reference_declarator (cv_qualifiers
, target
,
3878 /* Create a new C++ parser. */
3881 cp_parser_new (void)
3887 /* cp_lexer_new_main is called before doing GC allocation because
3888 cp_lexer_new_main might load a PCH file. */
3889 lexer
= cp_lexer_new_main ();
3891 /* Initialize the binops_by_token so that we can get the tree
3892 directly from the token. */
3893 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3894 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3896 parser
= ggc_cleared_alloc
<cp_parser
> ();
3897 parser
->lexer
= lexer
;
3898 parser
->context
= cp_parser_context_new (NULL
);
3900 /* For now, we always accept GNU extensions. */
3901 parser
->allow_gnu_extensions_p
= 1;
3903 /* The `>' token is a greater-than operator, not the end of a
3905 parser
->greater_than_is_operator_p
= true;
3907 parser
->default_arg_ok_p
= true;
3909 /* We are not parsing a constant-expression. */
3910 parser
->integral_constant_expression_p
= false;
3911 parser
->allow_non_integral_constant_expression_p
= false;
3912 parser
->non_integral_constant_expression_p
= false;
3914 /* Local variable names are not forbidden. */
3915 parser
->local_variables_forbidden_p
= false;
3917 /* We are not processing an `extern "C"' declaration. */
3918 parser
->in_unbraced_linkage_specification_p
= false;
3920 /* We are not processing a declarator. */
3921 parser
->in_declarator_p
= false;
3923 /* We are not processing a template-argument-list. */
3924 parser
->in_template_argument_list_p
= false;
3926 /* We are not in an iteration statement. */
3927 parser
->in_statement
= 0;
3929 /* We are not in a switch statement. */
3930 parser
->in_switch_statement_p
= false;
3932 /* We are not parsing a type-id inside an expression. */
3933 parser
->in_type_id_in_expr_p
= false;
3935 /* Declarations aren't implicitly extern "C". */
3936 parser
->implicit_extern_c
= false;
3938 /* String literals should be translated to the execution character set. */
3939 parser
->translate_strings_p
= true;
3941 /* We are not parsing a function body. */
3942 parser
->in_function_body
= false;
3944 /* We can correct until told otherwise. */
3945 parser
->colon_corrects_to_scope_p
= true;
3947 /* The unparsed function queue is empty. */
3948 push_unparsed_function_queues (parser
);
3950 /* There are no classes being defined. */
3951 parser
->num_classes_being_defined
= 0;
3953 /* No template parameters apply. */
3954 parser
->num_template_parameter_lists
= 0;
3956 /* Special parsing data structures. */
3957 parser
->omp_declare_simd
= NULL
;
3958 parser
->oacc_routine
= NULL
;
3960 /* Not declaring an implicit function template. */
3961 parser
->auto_is_implicit_function_template_parm_p
= false;
3962 parser
->fully_implicit_function_template_p
= false;
3963 parser
->implicit_template_parms
= 0;
3964 parser
->implicit_template_scope
= 0;
3966 /* Allow constrained-type-specifiers. */
3967 parser
->prevent_constrained_type_specifiers
= 0;
3969 /* We haven't yet seen an 'extern "C"'. */
3970 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3975 /* Create a cp_lexer structure which will emit the tokens in CACHE
3976 and push it onto the parser's lexer stack. This is used for delayed
3977 parsing of in-class method bodies and default arguments, and should
3978 not be confused with tentative parsing. */
3980 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3982 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3983 lexer
->next
= parser
->lexer
;
3984 parser
->lexer
= lexer
;
3986 /* Move the current source position to that of the first token in the
3988 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3991 /* Pop the top lexer off the parser stack. This is never used for the
3992 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3994 cp_parser_pop_lexer (cp_parser
*parser
)
3996 cp_lexer
*lexer
= parser
->lexer
;
3997 parser
->lexer
= lexer
->next
;
3998 cp_lexer_destroy (lexer
);
4000 /* Put the current source position back where it was before this
4001 lexer was pushed. */
4002 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4005 /* Lexical conventions [gram.lex] */
4007 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4011 cp_parser_identifier (cp_parser
* parser
)
4015 /* Look for the identifier. */
4016 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4017 /* Return the value. */
4019 return cp_expr (token
->u
.value
, token
->location
);
4021 return error_mark_node
;
4024 /* Parse a sequence of adjacent string constants. Returns a
4025 TREE_STRING representing the combined, nul-terminated string
4026 constant. If TRANSLATE is true, translate the string to the
4027 execution character set. If WIDE_OK is true, a wide string is
4030 C++98 [lex.string] says that if a narrow string literal token is
4031 adjacent to a wide string literal token, the behavior is undefined.
4032 However, C99 6.4.5p4 says that this results in a wide string literal.
4033 We follow C99 here, for consistency with the C front end.
4035 This code is largely lifted from lex_string() in c-lex.c.
4037 FUTURE: ObjC++ will need to handle @-strings here. */
4039 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4040 bool lookup_udlit
= true)
4044 struct obstack str_ob
;
4045 cpp_string str
, istr
, *strs
;
4047 enum cpp_ttype type
, curr_type
;
4048 int have_suffix_p
= 0;
4050 tree suffix_id
= NULL_TREE
;
4051 bool curr_tok_is_userdef_p
= false;
4053 tok
= cp_lexer_peek_token (parser
->lexer
);
4054 if (!cp_parser_is_string_literal (tok
))
4056 cp_parser_error (parser
, "expected string-literal");
4057 return error_mark_node
;
4060 location_t loc
= tok
->location
;
4062 if (cpp_userdef_string_p (tok
->type
))
4064 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4065 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4066 curr_tok_is_userdef_p
= true;
4070 string_tree
= tok
->u
.value
;
4071 curr_type
= tok
->type
;
4075 /* Try to avoid the overhead of creating and destroying an obstack
4076 for the common case of just one string. */
4077 if (!cp_parser_is_string_literal
4078 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4080 cp_lexer_consume_token (parser
->lexer
);
4082 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4083 str
.len
= TREE_STRING_LENGTH (string_tree
);
4086 if (curr_tok_is_userdef_p
)
4088 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4090 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4093 curr_type
= tok
->type
;
4099 location_t last_tok_loc
= tok
->location
;
4100 gcc_obstack_init (&str_ob
);
4105 cp_lexer_consume_token (parser
->lexer
);
4107 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4108 str
.len
= TREE_STRING_LENGTH (string_tree
);
4110 if (curr_tok_is_userdef_p
)
4112 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4113 if (have_suffix_p
== 0)
4115 suffix_id
= curr_suffix_id
;
4118 else if (have_suffix_p
== 1
4119 && curr_suffix_id
!= suffix_id
)
4121 error ("inconsistent user-defined literal suffixes"
4122 " %qD and %qD in string literal",
4123 suffix_id
, curr_suffix_id
);
4126 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4129 curr_type
= tok
->type
;
4131 if (type
!= curr_type
)
4133 if (type
== CPP_STRING
)
4135 else if (curr_type
!= CPP_STRING
)
4137 rich_location
rich_loc (line_table
, tok
->location
);
4138 rich_loc
.add_range (last_tok_loc
);
4139 error_at (&rich_loc
,
4140 "unsupported non-standard concatenation "
4141 "of string literals");
4145 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4147 last_tok_loc
= tok
->location
;
4149 tok
= cp_lexer_peek_token (parser
->lexer
);
4150 if (cpp_userdef_string_p (tok
->type
))
4152 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4153 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4154 curr_tok_is_userdef_p
= true;
4158 string_tree
= tok
->u
.value
;
4159 curr_type
= tok
->type
;
4160 curr_tok_is_userdef_p
= false;
4163 while (cp_parser_is_string_literal (tok
));
4165 /* A string literal built by concatenation has its caret=start at
4166 the start of the initial string, and its finish at the finish of
4167 the final string literal. */
4168 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4170 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4173 if (type
!= CPP_STRING
&& !wide_ok
)
4175 cp_parser_error (parser
, "a wide string is invalid in this context");
4179 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4180 (parse_in
, strs
, count
, &istr
, type
))
4182 value
= build_string (istr
.len
, (const char *)istr
.text
);
4183 free (CONST_CAST (unsigned char *, istr
.text
));
4189 case CPP_UTF8STRING
:
4190 TREE_TYPE (value
) = char_array_type_node
;
4193 TREE_TYPE (value
) = char16_array_type_node
;
4196 TREE_TYPE (value
) = char32_array_type_node
;
4199 TREE_TYPE (value
) = wchar_array_type_node
;
4203 value
= fix_string_type (value
);
4207 tree literal
= build_userdef_literal (suffix_id
, value
,
4208 OT_NONE
, NULL_TREE
);
4210 value
= cp_parser_userdef_string_literal (literal
);
4216 /* cpp_interpret_string has issued an error. */
4217 value
= error_mark_node
;
4220 obstack_free (&str_ob
, 0);
4222 return cp_expr (value
, loc
);
4225 /* Look up a literal operator with the name and the exact arguments. */
4228 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4231 decl
= lookup_name (name
);
4232 if (!decl
|| !is_overloaded_fn (decl
))
4233 return error_mark_node
;
4235 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4240 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4241 if (parmtypes
!= NULL_TREE
)
4243 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4244 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4246 tree tparm
= TREE_VALUE (parmtypes
);
4247 tree targ
= TREE_TYPE ((*args
)[ix
]);
4248 bool ptr
= TYPE_PTR_P (tparm
);
4249 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4250 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4252 || !same_type_p (TREE_TYPE (tparm
),
4257 && ix
== vec_safe_length (args
)
4258 /* May be this should be sufficient_parms_p instead,
4259 depending on how exactly should user-defined literals
4260 work in presence of default arguments on the literal
4261 operator parameters. */
4262 && parmtypes
== void_list_node
)
4267 return error_mark_node
;
4270 /* Parse a user-defined char constant. Returns a call to a user-defined
4271 literal operator taking the character as an argument. */
4274 cp_parser_userdef_char_literal (cp_parser
*parser
)
4276 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4277 tree literal
= token
->u
.value
;
4278 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4279 tree value
= USERDEF_LITERAL_VALUE (literal
);
4280 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4283 /* Build up a call to the user-defined operator */
4284 /* Lookup the name we got back from the id-expression. */
4285 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4286 vec_safe_push (args
, value
);
4287 decl
= lookup_literal_operator (name
, args
);
4288 if (!decl
|| decl
== error_mark_node
)
4290 error ("unable to find character literal operator %qD with %qT argument",
4291 name
, TREE_TYPE (value
));
4292 release_tree_vector (args
);
4293 return error_mark_node
;
4295 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4296 release_tree_vector (args
);
4300 /* A subroutine of cp_parser_userdef_numeric_literal to
4301 create a char... template parameter pack from a string node. */
4304 make_char_string_pack (tree value
)
4307 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4308 const char *str
= TREE_STRING_POINTER (value
);
4309 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4310 tree argvec
= make_tree_vec (1);
4312 /* Fill in CHARVEC with all of the parameters. */
4313 charvec
= make_tree_vec (len
);
4314 for (i
= 0; i
< len
; ++i
)
4316 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4317 cpp_string in
= { 3, s
};
4318 cpp_string out
= { 0, 0 };
4319 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4321 gcc_assert (out
.len
== 2);
4322 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4326 /* Build the argument packs. */
4327 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4329 TREE_VEC_ELT (argvec
, 0) = argpack
;
4334 /* A subroutine of cp_parser_userdef_numeric_literal to
4335 create a char... template parameter pack from a string node. */
4338 make_string_pack (tree value
)
4341 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4342 const unsigned char *str
4343 = (const unsigned char *) TREE_STRING_POINTER (value
);
4344 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4345 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4346 tree argvec
= make_tree_vec (2);
4348 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4349 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4351 /* First template parm is character type. */
4352 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4354 /* Fill in CHARVEC with all of the parameters. */
4355 charvec
= make_tree_vec (len
);
4356 for (int i
= 0; i
< len
; ++i
)
4357 TREE_VEC_ELT (charvec
, i
)
4358 = double_int_to_tree (str_char_type_node
,
4359 double_int::from_buffer (str
+ i
* sz
, sz
));
4361 /* Build the argument packs. */
4362 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4364 TREE_VEC_ELT (argvec
, 1) = argpack
;
4369 /* Parse a user-defined numeric constant. returns a call to a user-defined
4370 literal operator. */
4373 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4375 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4376 tree literal
= token
->u
.value
;
4377 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4378 tree value
= USERDEF_LITERAL_VALUE (literal
);
4379 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4380 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4381 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4383 vec
<tree
, va_gc
> *args
;
4385 /* Look for a literal operator taking the exact type of numeric argument
4386 as the literal value. */
4387 args
= make_tree_vector ();
4388 vec_safe_push (args
, value
);
4389 decl
= lookup_literal_operator (name
, args
);
4390 if (decl
&& decl
!= error_mark_node
)
4392 result
= finish_call_expr (decl
, &args
, false, true,
4393 tf_warning_or_error
);
4395 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4397 warning_at (token
->location
, OPT_Woverflow
,
4398 "integer literal exceeds range of %qT type",
4399 long_long_unsigned_type_node
);
4404 warning_at (token
->location
, OPT_Woverflow
,
4405 "floating literal exceeds range of %qT type",
4406 long_double_type_node
);
4407 else if (overflow
< 0)
4408 warning_at (token
->location
, OPT_Woverflow
,
4409 "floating literal truncated to zero");
4412 release_tree_vector (args
);
4415 release_tree_vector (args
);
4417 /* If the numeric argument didn't work, look for a raw literal
4418 operator taking a const char* argument consisting of the number
4419 in string format. */
4420 args
= make_tree_vector ();
4421 vec_safe_push (args
, num_string
);
4422 decl
= lookup_literal_operator (name
, args
);
4423 if (decl
&& decl
!= error_mark_node
)
4425 result
= finish_call_expr (decl
, &args
, false, true,
4426 tf_warning_or_error
);
4427 release_tree_vector (args
);
4430 release_tree_vector (args
);
4432 /* If the raw literal didn't work, look for a non-type template
4433 function with parameter pack char.... Call the function with
4434 template parameter characters representing the number. */
4435 args
= make_tree_vector ();
4436 decl
= lookup_literal_operator (name
, args
);
4437 if (decl
&& decl
!= error_mark_node
)
4439 tree tmpl_args
= make_char_string_pack (num_string
);
4440 if (tmpl_args
== NULL_TREE
)
4442 error ("failed to translate literal to execution character set %qT",
4444 return error_mark_node
;
4446 decl
= lookup_template_function (decl
, tmpl_args
);
4447 result
= finish_call_expr (decl
, &args
, false, true,
4448 tf_warning_or_error
);
4449 release_tree_vector (args
);
4453 release_tree_vector (args
);
4455 /* In C++14 the standard library defines complex number suffixes that
4456 conflict with GNU extensions. Prefer them if <complex> is #included. */
4457 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4458 bool i14
= (cxx_dialect
> cxx11
4459 && (id_equal (suffix_id
, "i")
4460 || id_equal (suffix_id
, "if")
4461 || id_equal (suffix_id
, "il")));
4462 diagnostic_t kind
= DK_ERROR
;
4467 tree cxlit
= lookup_qualified_name (std_node
,
4468 get_identifier ("complex_literals"),
4470 if (cxlit
== error_mark_node
)
4472 /* No <complex>, so pedwarn and use GNU semantics. */
4474 opt
= OPT_Wpedantic
;
4479 = emit_diagnostic (kind
, input_location
, opt
,
4480 "unable to find numeric literal operator %qD", name
);
4483 /* Don't inform either. */;
4486 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4487 "(from <complex>) to enable the C++14 user-defined literal "
4490 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4491 "GNU built-in suffix");
4494 inform (token
->location
, "use -fext-numeric-literals "
4495 "to enable more built-in suffixes");
4497 if (kind
== DK_ERROR
)
4498 value
= error_mark_node
;
4501 /* Use the built-in semantics. */
4503 if (id_equal (suffix_id
, "i"))
4505 if (TREE_CODE (value
) == INTEGER_CST
)
4506 type
= integer_type_node
;
4508 type
= double_type_node
;
4510 else if (id_equal (suffix_id
, "if"))
4511 type
= float_type_node
;
4512 else /* if (id_equal (suffix_id, "il")) */
4513 type
= long_double_type_node
;
4515 value
= build_complex (build_complex_type (type
),
4516 fold_convert (type
, integer_zero_node
),
4517 fold_convert (type
, value
));
4520 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4521 /* Avoid repeated diagnostics. */
4522 token
->u
.value
= value
;
4526 /* Parse a user-defined string constant. Returns a call to a user-defined
4527 literal operator taking a character pointer and the length of the string
4531 cp_parser_userdef_string_literal (tree literal
)
4533 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4534 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4535 tree value
= USERDEF_LITERAL_VALUE (literal
);
4536 int len
= TREE_STRING_LENGTH (value
)
4537 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4539 vec
<tree
, va_gc
> *args
;
4541 /* Build up a call to the user-defined operator. */
4542 /* Lookup the name we got back from the id-expression. */
4543 args
= make_tree_vector ();
4544 vec_safe_push (args
, value
);
4545 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4546 decl
= lookup_literal_operator (name
, args
);
4548 if (decl
&& decl
!= error_mark_node
)
4550 result
= finish_call_expr (decl
, &args
, false, true,
4551 tf_warning_or_error
);
4552 release_tree_vector (args
);
4555 release_tree_vector (args
);
4557 /* Look for a template function with typename parameter CharT
4558 and parameter pack CharT... Call the function with
4559 template parameter characters representing the string. */
4560 args
= make_tree_vector ();
4561 decl
= lookup_literal_operator (name
, args
);
4562 if (decl
&& decl
!= error_mark_node
)
4564 tree tmpl_args
= make_string_pack (value
);
4565 decl
= lookup_template_function (decl
, tmpl_args
);
4566 result
= finish_call_expr (decl
, &args
, false, true,
4567 tf_warning_or_error
);
4568 release_tree_vector (args
);
4571 release_tree_vector (args
);
4573 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4574 name
, TREE_TYPE (value
), size_type_node
);
4575 return error_mark_node
;
4579 /* Basic concepts [gram.basic] */
4581 /* Parse a translation-unit.
4584 declaration-seq [opt]
4586 Returns TRUE if all went well. */
4589 cp_parser_translation_unit (cp_parser
* parser
)
4591 /* The address of the first non-permanent object on the declarator
4593 static void *declarator_obstack_base
;
4597 /* Create the declarator obstack, if necessary. */
4598 if (!cp_error_declarator
)
4600 gcc_obstack_init (&declarator_obstack
);
4601 /* Create the error declarator. */
4602 cp_error_declarator
= make_declarator (cdk_error
);
4603 /* Create the empty parameter list. */
4604 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4606 /* Remember where the base of the declarator obstack lies. */
4607 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4610 cp_parser_declaration_seq_opt (parser
);
4612 /* If there are no tokens left then all went well. */
4613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4615 /* Get rid of the token array; we don't need it any more. */
4616 cp_lexer_destroy (parser
->lexer
);
4617 parser
->lexer
= NULL
;
4619 /* This file might have been a context that's implicitly extern
4620 "C". If so, pop the lang context. (Only relevant for PCH.) */
4621 if (parser
->implicit_extern_c
)
4623 pop_lang_context ();
4624 parser
->implicit_extern_c
= false;
4628 finish_translation_unit ();
4634 cp_parser_error (parser
, "expected declaration");
4638 /* Make sure the declarator obstack was fully cleaned up. */
4639 gcc_assert (obstack_next_free (&declarator_obstack
)
4640 == declarator_obstack_base
);
4642 /* All went well. */
4646 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4647 decltype context. */
4649 static inline tsubst_flags_t
4650 complain_flags (bool decltype_p
)
4652 tsubst_flags_t complain
= tf_warning_or_error
;
4654 complain
|= tf_decltype
;
4658 /* We're about to parse a collection of statements. If we're currently
4659 parsing tentatively, set up a firewall so that any nested
4660 cp_parser_commit_to_tentative_parse won't affect the current context. */
4662 static cp_token_position
4663 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4665 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4668 cp_parser_parse_tentatively (parser
);
4669 cp_parser_commit_to_topmost_tentative_parse (parser
);
4670 return cp_lexer_token_position (parser
->lexer
, false);
4673 /* We've finished parsing the collection of statements. Wrap up the
4674 firewall and replace the relevant tokens with the parsed form. */
4677 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4683 /* Finish the firewall level. */
4684 cp_parser_parse_definitely (parser
);
4685 /* And remember the result of the parse for when we try again. */
4686 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4687 token
->type
= CPP_PREPARSED_EXPR
;
4688 token
->u
.value
= expr
;
4689 token
->keyword
= RID_MAX
;
4690 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4693 /* Like the above functions, but let the user modify the tokens. Used by
4694 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4695 later parses, so it makes sense to localize the effects of
4696 cp_parser_commit_to_tentative_parse. */
4698 struct tentative_firewall
4703 tentative_firewall (cp_parser
*p
): parser(p
)
4705 /* If we're currently parsing tentatively, start a committed level as a
4706 firewall and then an inner tentative parse. */
4707 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4709 cp_parser_parse_tentatively (parser
);
4710 cp_parser_commit_to_topmost_tentative_parse (parser
);
4711 cp_parser_parse_tentatively (parser
);
4715 ~tentative_firewall()
4719 /* Finish the inner tentative parse and the firewall, propagating any
4720 uncommitted error state to the outer tentative parse. */
4721 bool err
= cp_parser_error_occurred (parser
);
4722 cp_parser_parse_definitely (parser
);
4723 cp_parser_parse_definitely (parser
);
4725 cp_parser_simulate_error (parser
);
4730 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4731 This class is for tracking such a matching pair of symbols.
4732 In particular, it tracks the location of the first token,
4733 so that if the second token is missing, we can highlight the
4734 location of the first token when notifying the user about the
4737 template <typename traits_t
>
4741 /* token_pair's ctor. */
4742 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4744 /* If the next token is the opening symbol for this pair, consume it and
4746 Otherwise, issue an error and return false.
4747 In either case, record the location of the opening token. */
4749 bool require_open (cp_parser
*parser
)
4751 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4752 return cp_parser_require (parser
, traits_t::open_token_type
,
4753 traits_t::required_token_open
);
4756 /* Consume the next token from PARSER, recording its location as
4757 that of the opening token within the pair. */
4759 cp_token
* consume_open (cp_parser
*parser
)
4761 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4762 gcc_assert (tok
->type
== traits_t::open_token_type
);
4763 m_open_loc
= tok
->location
;
4767 /* If the next token is the closing symbol for this pair, consume it
4769 Otherwise, issue an error, highlighting the location of the
4770 corresponding opening token, and return NULL. */
4772 cp_token
*require_close (cp_parser
*parser
) const
4774 return cp_parser_require (parser
, traits_t::close_token_type
,
4775 traits_t::required_token_close
,
4780 location_t m_open_loc
;
4783 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4785 struct matching_paren_traits
4787 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4788 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4789 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4790 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4793 /* "matching_parens" is a token_pair<T> class for tracking matching
4794 pairs of parentheses. */
4796 typedef token_pair
<matching_paren_traits
> matching_parens
;
4798 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4800 struct matching_brace_traits
4802 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4803 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4804 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4805 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4808 /* "matching_braces" is a token_pair<T> class for tracking matching
4811 typedef token_pair
<matching_brace_traits
> matching_braces
;
4814 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4815 enclosing parentheses. */
4818 cp_parser_statement_expr (cp_parser
*parser
)
4820 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4822 /* Consume the '('. */
4823 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4824 matching_parens parens
;
4825 parens
.consume_open (parser
);
4826 /* Start the statement-expression. */
4827 tree expr
= begin_stmt_expr ();
4828 /* Parse the compound-statement. */
4829 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4831 expr
= finish_stmt_expr (expr
, false);
4832 /* Consume the ')'. */
4833 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4834 if (!parens
.require_close (parser
))
4835 cp_parser_skip_to_end_of_statement (parser
);
4837 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4838 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4839 return cp_expr (expr
, combined_loc
);
4842 /* Expressions [gram.expr] */
4844 /* Parse a fold-operator.
4847 - * / % ^ & | = < > << >>
4848 = -= *= /= %= ^= &= |= <<= >>=
4849 == != <= >= && || , .* ->*
4851 This returns the tree code corresponding to the matched operator
4852 as an int. When the current token matches a compound assignment
4853 opertor, the resulting tree code is the negative value of the
4854 non-assignment operator. */
4857 cp_parser_fold_operator (cp_token
*token
)
4859 switch (token
->type
)
4861 case CPP_PLUS
: return PLUS_EXPR
;
4862 case CPP_MINUS
: return MINUS_EXPR
;
4863 case CPP_MULT
: return MULT_EXPR
;
4864 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4865 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4866 case CPP_XOR
: return BIT_XOR_EXPR
;
4867 case CPP_AND
: return BIT_AND_EXPR
;
4868 case CPP_OR
: return BIT_IOR_EXPR
;
4869 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4870 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4872 case CPP_EQ
: return -NOP_EXPR
;
4873 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4874 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4875 case CPP_MULT_EQ
: return -MULT_EXPR
;
4876 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4877 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4878 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4879 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4880 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4881 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4882 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4884 case CPP_EQ_EQ
: return EQ_EXPR
;
4885 case CPP_NOT_EQ
: return NE_EXPR
;
4886 case CPP_LESS
: return LT_EXPR
;
4887 case CPP_GREATER
: return GT_EXPR
;
4888 case CPP_LESS_EQ
: return LE_EXPR
;
4889 case CPP_GREATER_EQ
: return GE_EXPR
;
4891 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4892 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4894 case CPP_COMMA
: return COMPOUND_EXPR
;
4896 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4897 case CPP_DEREF_STAR
: return MEMBER_REF
;
4899 default: return ERROR_MARK
;
4903 /* Returns true if CODE indicates a binary expression, which is not allowed in
4904 the LHS of a fold-expression. More codes will need to be added to use this
4905 function in other contexts. */
4908 is_binary_op (tree_code code
)
4913 case POINTER_PLUS_EXPR
:
4916 case TRUNC_DIV_EXPR
:
4917 case TRUNC_MOD_EXPR
:
4933 case TRUTH_ANDIF_EXPR
:
4934 case TRUTH_ORIF_EXPR
:
4947 /* If the next token is a suitable fold operator, consume it and return as
4948 the function above. */
4951 cp_parser_fold_operator (cp_parser
*parser
)
4953 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4954 int code
= cp_parser_fold_operator (token
);
4955 if (code
!= ERROR_MARK
)
4956 cp_lexer_consume_token (parser
->lexer
);
4960 /* Parse a fold-expression.
4963 ( ... folding-operator cast-expression)
4964 ( cast-expression folding-operator ... )
4965 ( cast-expression folding operator ... folding-operator cast-expression)
4967 Note that the '(' and ')' are matched in primary expression. */
4970 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4977 cp_lexer_consume_token (parser
->lexer
);
4978 int op
= cp_parser_fold_operator (parser
);
4979 if (op
== ERROR_MARK
)
4981 cp_parser_error (parser
, "expected binary operator");
4982 return error_mark_node
;
4985 tree expr
= cp_parser_cast_expression (parser
, false, false,
4987 if (expr
== error_mark_node
)
4988 return error_mark_node
;
4989 return finish_left_unary_fold_expr (expr
, op
);
4992 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4993 int op
= cp_parser_fold_operator (parser
);
4994 if (op
== ERROR_MARK
)
4996 cp_parser_error (parser
, "expected binary operator");
4997 return error_mark_node
;
5000 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5002 cp_parser_error (parser
, "expected ...");
5003 return error_mark_node
;
5005 cp_lexer_consume_token (parser
->lexer
);
5007 /* The operands of a fold-expression are cast-expressions, so binary or
5008 conditional expressions are not allowed. We check this here to avoid
5009 tentative parsing. */
5010 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5011 /* OK, the expression was parenthesized. */;
5012 else if (is_binary_op (TREE_CODE (expr1
)))
5013 error_at (location_of (expr1
),
5014 "binary expression in operand of fold-expression");
5015 else if (TREE_CODE (expr1
) == COND_EXPR
5016 || (REFERENCE_REF_P (expr1
)
5017 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5018 error_at (location_of (expr1
),
5019 "conditional expression in operand of fold-expression");
5022 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5023 return finish_right_unary_fold_expr (expr1
, op
);
5025 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5027 cp_parser_error (parser
, "mismatched operator in fold-expression");
5028 return error_mark_node
;
5030 cp_lexer_consume_token (parser
->lexer
);
5032 // Binary left or right fold.
5033 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5034 if (expr2
== error_mark_node
)
5035 return error_mark_node
;
5036 return finish_binary_fold_expr (expr1
, expr2
, op
);
5039 /* Parse a primary-expression.
5046 lambda-expression (C++11)
5051 ( compound-statement )
5052 __builtin_va_arg ( assignment-expression , type-id )
5053 __builtin_offsetof ( type-id , offsetof-expression )
5056 __has_nothrow_assign ( type-id )
5057 __has_nothrow_constructor ( type-id )
5058 __has_nothrow_copy ( type-id )
5059 __has_trivial_assign ( type-id )
5060 __has_trivial_constructor ( type-id )
5061 __has_trivial_copy ( type-id )
5062 __has_trivial_destructor ( type-id )
5063 __has_virtual_destructor ( type-id )
5064 __is_abstract ( type-id )
5065 __is_base_of ( type-id , type-id )
5066 __is_class ( type-id )
5067 __is_empty ( type-id )
5068 __is_enum ( type-id )
5069 __is_final ( type-id )
5070 __is_literal_type ( type-id )
5071 __is_pod ( type-id )
5072 __is_polymorphic ( type-id )
5073 __is_std_layout ( type-id )
5074 __is_trivial ( type-id )
5075 __is_union ( type-id )
5077 Objective-C++ Extension:
5085 ADDRESS_P is true iff this expression was immediately preceded by
5086 "&" and therefore might denote a pointer-to-member. CAST_P is true
5087 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5088 true iff this expression is a template argument.
5090 Returns a representation of the expression. Upon return, *IDK
5091 indicates what kind of id-expression (if any) was present. */
5094 cp_parser_primary_expression (cp_parser
*parser
,
5097 bool template_arg_p
,
5101 cp_token
*token
= NULL
;
5103 /* Assume the primary expression is not an id-expression. */
5104 *idk
= CP_ID_KIND_NONE
;
5106 /* Peek at the next token. */
5107 token
= cp_lexer_peek_token (parser
->lexer
);
5108 switch ((int) token
->type
)
5117 user-defined-literal */
5124 case CPP_PREPARSED_EXPR
:
5125 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5126 return cp_parser_userdef_numeric_literal (parser
);
5127 token
= cp_lexer_consume_token (parser
->lexer
);
5128 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5130 error_at (token
->location
,
5131 "fixed-point types not supported in C++");
5132 return error_mark_node
;
5134 /* Floating-point literals are only allowed in an integral
5135 constant expression if they are cast to an integral or
5136 enumeration type. */
5137 if (TREE_CODE (token
->u
.value
) == REAL_CST
5138 && parser
->integral_constant_expression_p
5141 /* CAST_P will be set even in invalid code like "int(2.7 +
5142 ...)". Therefore, we have to check that the next token
5143 is sure to end the cast. */
5146 cp_token
*next_token
;
5148 next_token
= cp_lexer_peek_token (parser
->lexer
);
5149 if (/* The comma at the end of an
5150 enumerator-definition. */
5151 next_token
->type
!= CPP_COMMA
5152 /* The curly brace at the end of an enum-specifier. */
5153 && next_token
->type
!= CPP_CLOSE_BRACE
5154 /* The end of a statement. */
5155 && next_token
->type
!= CPP_SEMICOLON
5156 /* The end of the cast-expression. */
5157 && next_token
->type
!= CPP_CLOSE_PAREN
5158 /* The end of an array bound. */
5159 && next_token
->type
!= CPP_CLOSE_SQUARE
5160 /* The closing ">" in a template-argument-list. */
5161 && (next_token
->type
!= CPP_GREATER
5162 || parser
->greater_than_is_operator_p
)
5163 /* C++0x only: A ">>" treated like two ">" tokens,
5164 in a template-argument-list. */
5165 && (next_token
->type
!= CPP_RSHIFT
5166 || (cxx_dialect
== cxx98
)
5167 || parser
->greater_than_is_operator_p
))
5171 /* If we are within a cast, then the constraint that the
5172 cast is to an integral or enumeration type will be
5173 checked at that point. If we are not within a cast, then
5174 this code is invalid. */
5176 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5178 return cp_expr (token
->u
.value
, token
->location
);
5180 case CPP_CHAR_USERDEF
:
5181 case CPP_CHAR16_USERDEF
:
5182 case CPP_CHAR32_USERDEF
:
5183 case CPP_WCHAR_USERDEF
:
5184 case CPP_UTF8CHAR_USERDEF
:
5185 return cp_parser_userdef_char_literal (parser
);
5191 case CPP_UTF8STRING
:
5192 case CPP_STRING_USERDEF
:
5193 case CPP_STRING16_USERDEF
:
5194 case CPP_STRING32_USERDEF
:
5195 case CPP_WSTRING_USERDEF
:
5196 case CPP_UTF8STRING_USERDEF
:
5197 /* ??? Should wide strings be allowed when parser->translate_strings_p
5198 is false (i.e. in attributes)? If not, we can kill the third
5199 argument to cp_parser_string_literal. */
5200 return cp_parser_string_literal (parser
,
5201 parser
->translate_strings_p
,
5204 case CPP_OPEN_PAREN
:
5205 /* If we see `( { ' then we are looking at the beginning of
5206 a GNU statement-expression. */
5207 if (cp_parser_allow_gnu_extensions_p (parser
)
5208 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5210 /* Statement-expressions are not allowed by the standard. */
5211 pedwarn (token
->location
, OPT_Wpedantic
,
5212 "ISO C++ forbids braced-groups within expressions");
5214 /* And they're not allowed outside of a function-body; you
5215 cannot, for example, write:
5217 int i = ({ int j = 3; j + 1; });
5219 at class or namespace scope. */
5220 if (!parser
->in_function_body
5221 || parser
->in_template_argument_list_p
)
5223 error_at (token
->location
,
5224 "statement-expressions are not allowed outside "
5225 "functions nor in template-argument lists");
5226 cp_parser_skip_to_end_of_block_or_statement (parser
);
5227 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5228 cp_lexer_consume_token (parser
->lexer
);
5229 return error_mark_node
;
5232 return cp_parser_statement_expr (parser
);
5234 /* Otherwise it's a normal parenthesized expression. */
5237 bool saved_greater_than_is_operator_p
;
5239 location_t open_paren_loc
= token
->location
;
5241 /* Consume the `('. */
5242 matching_parens parens
;
5243 parens
.consume_open (parser
);
5244 /* Within a parenthesized expression, a `>' token is always
5245 the greater-than operator. */
5246 saved_greater_than_is_operator_p
5247 = parser
->greater_than_is_operator_p
;
5248 parser
->greater_than_is_operator_p
= true;
5250 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5251 /* Left fold expression. */
5254 /* Parse the parenthesized expression. */
5255 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5257 token
= cp_lexer_peek_token (parser
->lexer
);
5258 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5260 expr
= cp_parser_fold_expression (parser
, expr
);
5261 if (expr
!= error_mark_node
5262 && cxx_dialect
< cxx17
5263 && !in_system_header_at (input_location
))
5264 pedwarn (input_location
, 0, "fold-expressions only available "
5265 "with -std=c++17 or -std=gnu++17");
5268 /* Let the front end know that this expression was
5269 enclosed in parentheses. This matters in case, for
5270 example, the expression is of the form `A::B', since
5271 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5273 expr
= finish_parenthesized_expr (expr
);
5275 /* DR 705: Wrapping an unqualified name in parentheses
5276 suppresses arg-dependent lookup. We want to pass back
5277 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5278 (c++/37862), but none of the others. */
5279 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5280 *idk
= CP_ID_KIND_NONE
;
5282 /* The `>' token might be the end of a template-id or
5283 template-parameter-list now. */
5284 parser
->greater_than_is_operator_p
5285 = saved_greater_than_is_operator_p
;
5287 /* Consume the `)'. */
5288 token
= cp_lexer_peek_token (parser
->lexer
);
5289 location_t close_paren_loc
= token
->location
;
5290 expr
.set_range (open_paren_loc
, close_paren_loc
);
5291 if (!parens
.require_close (parser
)
5292 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5293 cp_parser_skip_to_end_of_statement (parser
);
5298 case CPP_OPEN_SQUARE
:
5300 if (c_dialect_objc ())
5302 /* We might have an Objective-C++ message. */
5303 cp_parser_parse_tentatively (parser
);
5304 tree msg
= cp_parser_objc_message_expression (parser
);
5305 /* If that works out, we're done ... */
5306 if (cp_parser_parse_definitely (parser
))
5308 /* ... else, fall though to see if it's a lambda. */
5310 cp_expr lam
= cp_parser_lambda_expression (parser
);
5311 /* Don't warn about a failed tentative parse. */
5312 if (cp_parser_error_occurred (parser
))
5313 return error_mark_node
;
5314 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5318 case CPP_OBJC_STRING
:
5319 if (c_dialect_objc ())
5320 /* We have an Objective-C++ string literal. */
5321 return cp_parser_objc_expression (parser
);
5322 cp_parser_error (parser
, "expected primary-expression");
5323 return error_mark_node
;
5326 switch (token
->keyword
)
5328 /* These two are the boolean literals. */
5330 cp_lexer_consume_token (parser
->lexer
);
5331 return cp_expr (boolean_true_node
, token
->location
);
5333 cp_lexer_consume_token (parser
->lexer
);
5334 return cp_expr (boolean_false_node
, token
->location
);
5336 /* The `__null' literal. */
5338 cp_lexer_consume_token (parser
->lexer
);
5339 return cp_expr (null_node
, token
->location
);
5341 /* The `nullptr' literal. */
5343 cp_lexer_consume_token (parser
->lexer
);
5344 return cp_expr (nullptr_node
, token
->location
);
5346 /* Recognize the `this' keyword. */
5348 cp_lexer_consume_token (parser
->lexer
);
5349 if (parser
->local_variables_forbidden_p
)
5351 error_at (token
->location
,
5352 "%<this%> may not be used in this context");
5353 return error_mark_node
;
5355 /* Pointers cannot appear in constant-expressions. */
5356 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5357 return error_mark_node
;
5358 return cp_expr (finish_this_expr (), token
->location
);
5360 /* The `operator' keyword can be the beginning of an
5365 case RID_FUNCTION_NAME
:
5366 case RID_PRETTY_FUNCTION_NAME
:
5367 case RID_C99_FUNCTION_NAME
:
5369 non_integral_constant name
;
5371 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5372 __func__ are the names of variables -- but they are
5373 treated specially. Therefore, they are handled here,
5374 rather than relying on the generic id-expression logic
5375 below. Grammatically, these names are id-expressions.
5377 Consume the token. */
5378 token
= cp_lexer_consume_token (parser
->lexer
);
5380 switch (token
->keyword
)
5382 case RID_FUNCTION_NAME
:
5383 name
= NIC_FUNC_NAME
;
5385 case RID_PRETTY_FUNCTION_NAME
:
5386 name
= NIC_PRETTY_FUNC
;
5388 case RID_C99_FUNCTION_NAME
:
5389 name
= NIC_C99_FUNC
;
5395 if (cp_parser_non_integral_constant_expression (parser
, name
))
5396 return error_mark_node
;
5398 /* Look up the name. */
5399 return finish_fname (token
->u
.value
);
5406 source_location type_location
;
5407 location_t start_loc
5408 = cp_lexer_peek_token (parser
->lexer
)->location
;
5409 /* The `__builtin_va_arg' construct is used to handle
5410 `va_arg'. Consume the `__builtin_va_arg' token. */
5411 cp_lexer_consume_token (parser
->lexer
);
5412 /* Look for the opening `('. */
5413 matching_parens parens
;
5414 parens
.require_open (parser
);
5415 /* Now, parse the assignment-expression. */
5416 expression
= cp_parser_assignment_expression (parser
);
5417 /* Look for the `,'. */
5418 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5419 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5420 /* Parse the type-id. */
5422 type_id_in_expr_sentinel
s (parser
);
5423 type
= cp_parser_type_id (parser
);
5425 /* Look for the closing `)'. */
5426 location_t finish_loc
5427 = cp_lexer_peek_token (parser
->lexer
)->location
;
5428 parens
.require_close (parser
);
5429 /* Using `va_arg' in a constant-expression is not
5431 if (cp_parser_non_integral_constant_expression (parser
,
5433 return error_mark_node
;
5434 /* Construct a location of the form:
5435 __builtin_va_arg (v, int)
5436 ~~~~~~~~~~~~~~~~~~~~~^~~~
5437 with the caret at the type, ranging from the start of the
5438 "__builtin_va_arg" token to the close paren. */
5439 location_t combined_loc
5440 = make_location (type_location
, start_loc
, finish_loc
);
5441 return build_x_va_arg (combined_loc
, expression
, type
);
5445 return cp_parser_builtin_offsetof (parser
);
5447 case RID_HAS_NOTHROW_ASSIGN
:
5448 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5449 case RID_HAS_NOTHROW_COPY
:
5450 case RID_HAS_TRIVIAL_ASSIGN
:
5451 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5452 case RID_HAS_TRIVIAL_COPY
:
5453 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5454 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5455 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5456 case RID_IS_ABSTRACT
:
5457 case RID_IS_AGGREGATE
:
5458 case RID_IS_BASE_OF
:
5463 case RID_IS_LITERAL_TYPE
:
5465 case RID_IS_POLYMORPHIC
:
5466 case RID_IS_SAME_AS
:
5467 case RID_IS_STD_LAYOUT
:
5468 case RID_IS_TRIVIAL
:
5469 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5470 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5471 case RID_IS_TRIVIALLY_COPYABLE
:
5473 case RID_IS_ASSIGNABLE
:
5474 case RID_IS_CONSTRUCTIBLE
:
5475 return cp_parser_trait_expr (parser
, token
->keyword
);
5479 return cp_parser_requires_expression (parser
);
5481 /* Objective-C++ expressions. */
5483 case RID_AT_PROTOCOL
:
5484 case RID_AT_SELECTOR
:
5485 return cp_parser_objc_expression (parser
);
5488 if (parser
->in_function_body
5489 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5492 error_at (token
->location
,
5493 "a template declaration cannot appear at block scope");
5494 cp_parser_skip_to_end_of_block_or_statement (parser
);
5495 return error_mark_node
;
5499 cp_parser_error (parser
, "expected primary-expression");
5500 return error_mark_node
;
5503 /* An id-expression can start with either an identifier, a
5504 `::' as the beginning of a qualified-id, or the "operator"
5508 case CPP_TEMPLATE_ID
:
5509 case CPP_NESTED_NAME_SPECIFIER
:
5512 cp_expr id_expression
;
5514 const char *error_msg
;
5517 cp_token
*id_expr_token
;
5519 /* Parse the id-expression. */
5521 = cp_parser_id_expression (parser
,
5522 /*template_keyword_p=*/false,
5523 /*check_dependency_p=*/true,
5525 /*declarator_p=*/false,
5526 /*optional_p=*/false);
5527 if (id_expression
== error_mark_node
)
5528 return error_mark_node
;
5529 id_expr_token
= token
;
5530 token
= cp_lexer_peek_token (parser
->lexer
);
5531 done
= (token
->type
!= CPP_OPEN_SQUARE
5532 && token
->type
!= CPP_OPEN_PAREN
5533 && token
->type
!= CPP_DOT
5534 && token
->type
!= CPP_DEREF
5535 && token
->type
!= CPP_PLUS_PLUS
5536 && token
->type
!= CPP_MINUS_MINUS
);
5537 /* If we have a template-id, then no further lookup is
5538 required. If the template-id was for a template-class, we
5539 will sometimes have a TYPE_DECL at this point. */
5540 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5541 || TREE_CODE (id_expression
) == TYPE_DECL
)
5542 decl
= id_expression
;
5543 /* Look up the name. */
5546 tree ambiguous_decls
;
5548 /* If we already know that this lookup is ambiguous, then
5549 we've already issued an error message; there's no reason
5551 if (id_expr_token
->type
== CPP_NAME
5552 && id_expr_token
->error_reported
)
5554 cp_parser_simulate_error (parser
);
5555 return error_mark_node
;
5558 decl
= cp_parser_lookup_name (parser
, id_expression
,
5561 /*is_namespace=*/false,
5562 /*check_dependency=*/true,
5564 id_expr_token
->location
);
5565 /* If the lookup was ambiguous, an error will already have
5567 if (ambiguous_decls
)
5568 return error_mark_node
;
5570 /* In Objective-C++, we may have an Objective-C 2.0
5571 dot-syntax for classes here. */
5572 if (c_dialect_objc ()
5573 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5574 && TREE_CODE (decl
) == TYPE_DECL
5575 && objc_is_class_name (decl
))
5578 cp_lexer_consume_token (parser
->lexer
);
5579 component
= cp_parser_identifier (parser
);
5580 if (component
== error_mark_node
)
5581 return error_mark_node
;
5583 tree result
= objc_build_class_component_ref (id_expression
,
5585 /* Build a location of the form:
5588 with caret at the start of the component name (at
5589 input_location), ranging from the start of the id_expression
5590 to the end of the component name. */
5591 location_t combined_loc
5592 = make_location (input_location
, id_expression
.get_start (),
5593 get_finish (input_location
));
5594 protected_set_expr_location (result
, combined_loc
);
5598 /* In Objective-C++, an instance variable (ivar) may be preferred
5599 to whatever cp_parser_lookup_name() found.
5600 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5601 rest of c-family, we have to do a little extra work to preserve
5602 any location information in cp_expr "decl". Given that
5603 objc_lookup_ivar is implemented in "c-family" and "objc", we
5604 have a trip through the pure "tree" type, rather than cp_expr.
5605 Naively copying it back to "decl" would implicitly give the
5606 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5607 store an EXPR_LOCATION. Hence we only update "decl" (and
5608 hence its location_t) if we get back a different tree node. */
5609 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5611 if (decl_tree
!= decl
.get_value ())
5612 decl
= cp_expr (decl_tree
);
5614 /* If name lookup gives us a SCOPE_REF, then the
5615 qualifying scope was dependent. */
5616 if (TREE_CODE (decl
) == SCOPE_REF
)
5618 /* At this point, we do not know if DECL is a valid
5619 integral constant expression. We assume that it is
5620 in fact such an expression, so that code like:
5622 template <int N> struct A {
5626 is accepted. At template-instantiation time, we
5627 will check that B<N>::i is actually a constant. */
5630 /* Check to see if DECL is a local variable in a context
5631 where that is forbidden. */
5632 if (parser
->local_variables_forbidden_p
5633 && local_variable_p (decl
))
5635 error_at (id_expr_token
->location
,
5636 "local variable %qD may not appear in this context",
5638 return error_mark_node
;
5642 if (processing_template_decl
)
5643 if (tree fns
= maybe_get_fns (decl
))
5644 /* It's too difficult to mark ths in all the places where
5645 we know for sure we need to keep the lookup, so do it
5646 now. The cost is extra GC to recycle the lookups
5647 resolved at parse time. */
5650 decl
= (finish_id_expression
5651 (id_expression
, decl
, parser
->scope
,
5653 parser
->integral_constant_expression_p
,
5654 parser
->allow_non_integral_constant_expression_p
,
5655 &parser
->non_integral_constant_expression_p
,
5656 template_p
, done
, address_p
,
5659 id_expression
.get_location ()));
5661 cp_parser_error (parser
, error_msg
);
5662 decl
.set_location (id_expr_token
->location
);
5666 /* Anything else is an error. */
5668 cp_parser_error (parser
, "expected primary-expression");
5669 return error_mark_node
;
5673 static inline cp_expr
5674 cp_parser_primary_expression (cp_parser
*parser
,
5677 bool template_arg_p
,
5680 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5681 /*decltype*/false, idk
);
5684 /* Parse an id-expression.
5691 :: [opt] nested-name-specifier template [opt] unqualified-id
5693 :: operator-function-id
5696 Return a representation of the unqualified portion of the
5697 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5698 a `::' or nested-name-specifier.
5700 Often, if the id-expression was a qualified-id, the caller will
5701 want to make a SCOPE_REF to represent the qualified-id. This
5702 function does not do this in order to avoid wastefully creating
5703 SCOPE_REFs when they are not required.
5705 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5708 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5709 uninstantiated templates.
5711 If *TEMPLATE_P is non-NULL, it is set to true iff the
5712 `template' keyword is used to explicitly indicate that the entity
5713 named is a template.
5715 If DECLARATOR_P is true, the id-expression is appearing as part of
5716 a declarator, rather than as part of an expression. */
5719 cp_parser_id_expression (cp_parser
*parser
,
5720 bool template_keyword_p
,
5721 bool check_dependency_p
,
5726 bool global_scope_p
;
5727 bool nested_name_specifier_p
;
5729 /* Assume the `template' keyword was not used. */
5731 *template_p
= template_keyword_p
;
5733 /* Look for the optional `::' operator. */
5735 = (!template_keyword_p
5736 && (cp_parser_global_scope_opt (parser
,
5737 /*current_scope_valid_p=*/false)
5740 /* Look for the optional nested-name-specifier. */
5741 nested_name_specifier_p
5742 = (cp_parser_nested_name_specifier_opt (parser
,
5743 /*typename_keyword_p=*/false,
5750 /* If there is a nested-name-specifier, then we are looking at
5751 the first qualified-id production. */
5752 if (nested_name_specifier_p
)
5755 tree saved_object_scope
;
5756 tree saved_qualifying_scope
;
5757 cp_expr unqualified_id
;
5760 /* See if the next token is the `template' keyword. */
5762 template_p
= &is_template
;
5763 *template_p
= cp_parser_optional_template_keyword (parser
);
5764 /* Name lookup we do during the processing of the
5765 unqualified-id might obliterate SCOPE. */
5766 saved_scope
= parser
->scope
;
5767 saved_object_scope
= parser
->object_scope
;
5768 saved_qualifying_scope
= parser
->qualifying_scope
;
5769 /* Process the final unqualified-id. */
5770 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5773 /*optional_p=*/false);
5774 /* Restore the SAVED_SCOPE for our caller. */
5775 parser
->scope
= saved_scope
;
5776 parser
->object_scope
= saved_object_scope
;
5777 parser
->qualifying_scope
= saved_qualifying_scope
;
5779 return unqualified_id
;
5781 /* Otherwise, if we are in global scope, then we are looking at one
5782 of the other qualified-id productions. */
5783 else if (global_scope_p
)
5788 /* Peek at the next token. */
5789 token
= cp_lexer_peek_token (parser
->lexer
);
5791 /* If it's an identifier, and the next token is not a "<", then
5792 we can avoid the template-id case. This is an optimization
5793 for this common case. */
5794 if (token
->type
== CPP_NAME
5795 && !cp_parser_nth_token_starts_template_argument_list_p
5797 return cp_parser_identifier (parser
);
5799 cp_parser_parse_tentatively (parser
);
5800 /* Try a template-id. */
5801 id
= cp_parser_template_id (parser
,
5802 /*template_keyword_p=*/false,
5803 /*check_dependency_p=*/true,
5806 /* If that worked, we're done. */
5807 if (cp_parser_parse_definitely (parser
))
5810 /* Peek at the next token. (Changes in the token buffer may
5811 have invalidated the pointer obtained above.) */
5812 token
= cp_lexer_peek_token (parser
->lexer
);
5814 switch (token
->type
)
5817 return cp_parser_identifier (parser
);
5820 if (token
->keyword
== RID_OPERATOR
)
5821 return cp_parser_operator_function_id (parser
);
5825 cp_parser_error (parser
, "expected id-expression");
5826 return error_mark_node
;
5830 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5831 /*check_dependency_p=*/true,
5836 /* Parse an unqualified-id.
5840 operator-function-id
5841 conversion-function-id
5845 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5846 keyword, in a construct like `A::template ...'.
5848 Returns a representation of unqualified-id. For the `identifier'
5849 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5850 production a BIT_NOT_EXPR is returned; the operand of the
5851 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5852 other productions, see the documentation accompanying the
5853 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5854 names are looked up in uninstantiated templates. If DECLARATOR_P
5855 is true, the unqualified-id is appearing as part of a declarator,
5856 rather than as part of an expression. */
5859 cp_parser_unqualified_id (cp_parser
* parser
,
5860 bool template_keyword_p
,
5861 bool check_dependency_p
,
5867 /* Peek at the next token. */
5868 token
= cp_lexer_peek_token (parser
->lexer
);
5870 switch ((int) token
->type
)
5876 /* We don't know yet whether or not this will be a
5878 cp_parser_parse_tentatively (parser
);
5879 /* Try a template-id. */
5880 id
= cp_parser_template_id (parser
, template_keyword_p
,
5884 /* If it worked, we're done. */
5885 if (cp_parser_parse_definitely (parser
))
5887 /* Otherwise, it's an ordinary identifier. */
5888 return cp_parser_identifier (parser
);
5891 case CPP_TEMPLATE_ID
:
5892 return cp_parser_template_id (parser
, template_keyword_p
,
5900 tree qualifying_scope
;
5905 /* Consume the `~' token. */
5906 cp_lexer_consume_token (parser
->lexer
);
5907 /* Parse the class-name. The standard, as written, seems to
5910 template <typename T> struct S { ~S (); };
5911 template <typename T> S<T>::~S() {}
5913 is invalid, since `~' must be followed by a class-name, but
5914 `S<T>' is dependent, and so not known to be a class.
5915 That's not right; we need to look in uninstantiated
5916 templates. A further complication arises from:
5918 template <typename T> void f(T t) {
5922 Here, it is not possible to look up `T' in the scope of `T'
5923 itself. We must look in both the current scope, and the
5924 scope of the containing complete expression.
5926 Yet another issue is:
5935 The standard does not seem to say that the `S' in `~S'
5936 should refer to the type `S' and not the data member
5939 /* DR 244 says that we look up the name after the "~" in the
5940 same scope as we looked up the qualifying name. That idea
5941 isn't fully worked out; it's more complicated than that. */
5942 scope
= parser
->scope
;
5943 object_scope
= parser
->object_scope
;
5944 qualifying_scope
= parser
->qualifying_scope
;
5946 /* Check for invalid scopes. */
5947 if (scope
== error_mark_node
)
5949 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5950 cp_lexer_consume_token (parser
->lexer
);
5951 return error_mark_node
;
5953 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5955 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5956 error_at (token
->location
,
5957 "scope %qT before %<~%> is not a class-name",
5959 cp_parser_simulate_error (parser
);
5960 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5961 cp_lexer_consume_token (parser
->lexer
);
5962 return error_mark_node
;
5964 gcc_assert (!scope
|| TYPE_P (scope
));
5966 /* If the name is of the form "X::~X" it's OK even if X is a
5968 token
= cp_lexer_peek_token (parser
->lexer
);
5970 && token
->type
== CPP_NAME
5971 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5973 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5974 || (CLASS_TYPE_P (scope
)
5975 && constructor_name_p (token
->u
.value
, scope
))))
5977 cp_lexer_consume_token (parser
->lexer
);
5978 return build_nt (BIT_NOT_EXPR
, scope
);
5981 /* ~auto means the destructor of whatever the object is. */
5982 if (cp_parser_is_keyword (token
, RID_AUTO
))
5984 if (cxx_dialect
< cxx14
)
5985 pedwarn (input_location
, 0,
5986 "%<~auto%> only available with "
5987 "-std=c++14 or -std=gnu++14");
5988 cp_lexer_consume_token (parser
->lexer
);
5989 return build_nt (BIT_NOT_EXPR
, make_auto ());
5992 /* If there was an explicit qualification (S::~T), first look
5993 in the scope given by the qualification (i.e., S).
5995 Note: in the calls to cp_parser_class_name below we pass
5996 typename_type so that lookup finds the injected-class-name
5997 rather than the constructor. */
5999 type_decl
= NULL_TREE
;
6002 cp_parser_parse_tentatively (parser
);
6003 type_decl
= cp_parser_class_name (parser
,
6004 /*typename_keyword_p=*/false,
6005 /*template_keyword_p=*/false,
6007 /*check_dependency=*/false,
6008 /*class_head_p=*/false,
6010 if (cp_parser_parse_definitely (parser
))
6013 /* In "N::S::~S", look in "N" as well. */
6014 if (!done
&& scope
&& qualifying_scope
)
6016 cp_parser_parse_tentatively (parser
);
6017 parser
->scope
= qualifying_scope
;
6018 parser
->object_scope
= NULL_TREE
;
6019 parser
->qualifying_scope
= NULL_TREE
;
6021 = cp_parser_class_name (parser
,
6022 /*typename_keyword_p=*/false,
6023 /*template_keyword_p=*/false,
6025 /*check_dependency=*/false,
6026 /*class_head_p=*/false,
6028 if (cp_parser_parse_definitely (parser
))
6031 /* In "p->S::~T", look in the scope given by "*p" as well. */
6032 else if (!done
&& object_scope
)
6034 cp_parser_parse_tentatively (parser
);
6035 parser
->scope
= object_scope
;
6036 parser
->object_scope
= NULL_TREE
;
6037 parser
->qualifying_scope
= NULL_TREE
;
6039 = cp_parser_class_name (parser
,
6040 /*typename_keyword_p=*/false,
6041 /*template_keyword_p=*/false,
6043 /*check_dependency=*/false,
6044 /*class_head_p=*/false,
6046 if (cp_parser_parse_definitely (parser
))
6049 /* Look in the surrounding context. */
6052 parser
->scope
= NULL_TREE
;
6053 parser
->object_scope
= NULL_TREE
;
6054 parser
->qualifying_scope
= NULL_TREE
;
6055 if (processing_template_decl
)
6056 cp_parser_parse_tentatively (parser
);
6058 = cp_parser_class_name (parser
,
6059 /*typename_keyword_p=*/false,
6060 /*template_keyword_p=*/false,
6062 /*check_dependency=*/false,
6063 /*class_head_p=*/false,
6065 if (processing_template_decl
6066 && ! cp_parser_parse_definitely (parser
))
6068 /* We couldn't find a type with this name. If we're parsing
6069 tentatively, fail and try something else. */
6070 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6072 cp_parser_simulate_error (parser
);
6073 return error_mark_node
;
6075 /* Otherwise, accept it and check for a match at instantiation
6077 type_decl
= cp_parser_identifier (parser
);
6078 if (type_decl
!= error_mark_node
)
6079 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6083 /* If an error occurred, assume that the name of the
6084 destructor is the same as the name of the qualifying
6085 class. That allows us to keep parsing after running
6086 into ill-formed destructor names. */
6087 if (type_decl
== error_mark_node
&& scope
)
6088 return build_nt (BIT_NOT_EXPR
, scope
);
6089 else if (type_decl
== error_mark_node
)
6090 return error_mark_node
;
6092 /* Check that destructor name and scope match. */
6093 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6095 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6096 error_at (token
->location
,
6097 "declaration of %<~%T%> as member of %qT",
6099 cp_parser_simulate_error (parser
);
6100 return error_mark_node
;
6105 A typedef-name that names a class shall not be used as the
6106 identifier in the declarator for a destructor declaration. */
6108 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6109 && !DECL_SELF_REFERENCE_P (type_decl
)
6110 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6111 error_at (token
->location
,
6112 "typedef-name %qD used as destructor declarator",
6115 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6119 if (token
->keyword
== RID_OPERATOR
)
6123 /* This could be a template-id, so we try that first. */
6124 cp_parser_parse_tentatively (parser
);
6125 /* Try a template-id. */
6126 id
= cp_parser_template_id (parser
, template_keyword_p
,
6127 /*check_dependency_p=*/true,
6130 /* If that worked, we're done. */
6131 if (cp_parser_parse_definitely (parser
))
6133 /* We still don't know whether we're looking at an
6134 operator-function-id or a conversion-function-id. */
6135 cp_parser_parse_tentatively (parser
);
6136 /* Try an operator-function-id. */
6137 id
= cp_parser_operator_function_id (parser
);
6138 /* If that didn't work, try a conversion-function-id. */
6139 if (!cp_parser_parse_definitely (parser
))
6140 id
= cp_parser_conversion_function_id (parser
);
6149 cp_parser_error (parser
, "expected unqualified-id");
6150 return error_mark_node
;
6154 /* Parse an (optional) nested-name-specifier.
6156 nested-name-specifier: [C++98]
6157 class-or-namespace-name :: nested-name-specifier [opt]
6158 class-or-namespace-name :: template nested-name-specifier [opt]
6160 nested-name-specifier: [C++0x]
6163 nested-name-specifier identifier ::
6164 nested-name-specifier template [opt] simple-template-id ::
6166 PARSER->SCOPE should be set appropriately before this function is
6167 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6168 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6171 Sets PARSER->SCOPE to the class (TYPE) or namespace
6172 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6173 it unchanged if there is no nested-name-specifier. Returns the new
6174 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6176 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6177 part of a declaration and/or decl-specifier. */
6180 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6181 bool typename_keyword_p
,
6182 bool check_dependency_p
,
6184 bool is_declaration
,
6185 bool template_keyword_p
/* = false */)
6187 bool success
= false;
6188 cp_token_position start
= 0;
6191 /* Remember where the nested-name-specifier starts. */
6192 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6194 start
= cp_lexer_token_position (parser
->lexer
, false);
6195 push_deferring_access_checks (dk_deferred
);
6202 tree saved_qualifying_scope
;
6204 /* Spot cases that cannot be the beginning of a
6205 nested-name-specifier. */
6206 token
= cp_lexer_peek_token (parser
->lexer
);
6208 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6209 the already parsed nested-name-specifier. */
6210 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6212 /* Grab the nested-name-specifier and continue the loop. */
6213 cp_parser_pre_parsed_nested_name_specifier (parser
);
6214 /* If we originally encountered this nested-name-specifier
6215 with IS_DECLARATION set to false, we will not have
6216 resolved TYPENAME_TYPEs, so we must do so here. */
6218 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6220 new_scope
= resolve_typename_type (parser
->scope
,
6221 /*only_current_p=*/false);
6222 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6223 parser
->scope
= new_scope
;
6229 /* Spot cases that cannot be the beginning of a
6230 nested-name-specifier. On the second and subsequent times
6231 through the loop, we look for the `template' keyword. */
6232 if (success
&& token
->keyword
== RID_TEMPLATE
)
6234 /* A template-id can start a nested-name-specifier. */
6235 else if (token
->type
== CPP_TEMPLATE_ID
)
6237 /* DR 743: decltype can be used in a nested-name-specifier. */
6238 else if (token_is_decltype (token
))
6242 /* If the next token is not an identifier, then it is
6243 definitely not a type-name or namespace-name. */
6244 if (token
->type
!= CPP_NAME
)
6246 /* If the following token is neither a `<' (to begin a
6247 template-id), nor a `::', then we are not looking at a
6248 nested-name-specifier. */
6249 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6251 if (token
->type
== CPP_COLON
6252 && parser
->colon_corrects_to_scope_p
6253 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6255 gcc_rich_location
richloc (token
->location
);
6256 richloc
.add_fixit_replace ("::");
6258 "found %<:%> in nested-name-specifier, "
6260 token
->type
= CPP_SCOPE
;
6263 if (token
->type
!= CPP_SCOPE
6264 && !cp_parser_nth_token_starts_template_argument_list_p
6269 /* The nested-name-specifier is optional, so we parse
6271 cp_parser_parse_tentatively (parser
);
6273 /* Look for the optional `template' keyword, if this isn't the
6274 first time through the loop. */
6276 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6278 /* Save the old scope since the name lookup we are about to do
6279 might destroy it. */
6280 old_scope
= parser
->scope
;
6281 saved_qualifying_scope
= parser
->qualifying_scope
;
6282 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6283 look up names in "X<T>::I" in order to determine that "Y" is
6284 a template. So, if we have a typename at this point, we make
6285 an effort to look through it. */
6287 && !typename_keyword_p
6289 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6290 parser
->scope
= resolve_typename_type (parser
->scope
,
6291 /*only_current_p=*/false);
6292 /* Parse the qualifying entity. */
6294 = cp_parser_qualifying_entity (parser
,
6300 /* Look for the `::' token. */
6301 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6303 /* If we found what we wanted, we keep going; otherwise, we're
6305 if (!cp_parser_parse_definitely (parser
))
6307 bool error_p
= false;
6309 /* Restore the OLD_SCOPE since it was valid before the
6310 failed attempt at finding the last
6311 class-or-namespace-name. */
6312 parser
->scope
= old_scope
;
6313 parser
->qualifying_scope
= saved_qualifying_scope
;
6315 /* If the next token is a decltype, and the one after that is a
6316 `::', then the decltype has failed to resolve to a class or
6317 enumeration type. Give this error even when parsing
6318 tentatively since it can't possibly be valid--and we're going
6319 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6320 won't get another chance.*/
6321 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6322 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6325 token
= cp_lexer_consume_token (parser
->lexer
);
6326 error_at (token
->location
, "decltype evaluates to %qT, "
6327 "which is not a class or enumeration type",
6328 token
->u
.tree_check_value
->value
);
6329 parser
->scope
= error_mark_node
;
6333 cp_lexer_consume_token (parser
->lexer
);
6336 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6337 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6339 /* If we have a non-type template-id followed by ::, it can't
6340 possibly be valid. */
6341 token
= cp_lexer_peek_token (parser
->lexer
);
6342 tree tid
= token
->u
.tree_check_value
->value
;
6343 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6344 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6346 tree tmpl
= NULL_TREE
;
6347 if (is_overloaded_fn (tid
))
6349 tree fns
= get_fns (tid
);
6350 if (OVL_SINGLE_P (fns
))
6351 tmpl
= OVL_FIRST (fns
);
6352 error_at (token
->location
, "function template-id %qD "
6353 "in nested-name-specifier", tid
);
6357 /* Variable template. */
6358 tmpl
= TREE_OPERAND (tid
, 0);
6359 gcc_assert (variable_template_p (tmpl
));
6360 error_at (token
->location
, "variable template-id %qD "
6361 "in nested-name-specifier", tid
);
6364 inform (DECL_SOURCE_LOCATION (tmpl
),
6365 "%qD declared here", tmpl
);
6367 parser
->scope
= error_mark_node
;
6371 cp_lexer_consume_token (parser
->lexer
);
6372 cp_lexer_consume_token (parser
->lexer
);
6376 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6378 /* If the next token is an identifier, and the one after
6379 that is a `::', then any valid interpretation would have
6380 found a class-or-namespace-name. */
6381 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6382 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6384 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6387 token
= cp_lexer_consume_token (parser
->lexer
);
6390 if (!token
->error_reported
)
6393 tree ambiguous_decls
;
6395 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6397 /*is_template=*/false,
6398 /*is_namespace=*/false,
6399 /*check_dependency=*/true,
6402 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6403 error_at (token
->location
,
6404 "%qD used without template arguments",
6406 else if (ambiguous_decls
)
6408 // cp_parser_lookup_name has the same diagnostic,
6409 // thus make sure to emit it at most once.
6410 if (cp_parser_uncommitted_to_tentative_parse_p
6413 error_at (token
->location
,
6414 "reference to %qD is ambiguous",
6416 print_candidates (ambiguous_decls
);
6418 decl
= error_mark_node
;
6422 if (cxx_dialect
!= cxx98
)
6423 cp_parser_name_lookup_error
6424 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6427 cp_parser_name_lookup_error
6428 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6432 parser
->scope
= error_mark_node
;
6434 /* Treat this as a successful nested-name-specifier
6439 If the name found is not a class-name (clause
6440 _class_) or namespace-name (_namespace.def_), the
6441 program is ill-formed. */
6444 cp_lexer_consume_token (parser
->lexer
);
6448 /* We've found one valid nested-name-specifier. */
6450 /* Name lookup always gives us a DECL. */
6451 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6452 new_scope
= TREE_TYPE (new_scope
);
6453 /* Uses of "template" must be followed by actual templates. */
6454 if (template_keyword_p
6455 && !(CLASS_TYPE_P (new_scope
)
6456 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6457 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6458 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6459 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6460 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6461 == TEMPLATE_ID_EXPR
)))
6462 permerror (input_location
, TYPE_P (new_scope
)
6463 ? G_("%qT is not a template")
6464 : G_("%qD is not a template"),
6466 /* If it is a class scope, try to complete it; we are about to
6467 be looking up names inside the class. */
6468 if (TYPE_P (new_scope
)
6469 /* Since checking types for dependency can be expensive,
6470 avoid doing it if the type is already complete. */
6471 && !COMPLETE_TYPE_P (new_scope
)
6472 /* Do not try to complete dependent types. */
6473 && !dependent_type_p (new_scope
))
6475 new_scope
= complete_type (new_scope
);
6476 /* If it is a typedef to current class, use the current
6477 class instead, as the typedef won't have any names inside
6479 if (!COMPLETE_TYPE_P (new_scope
)
6480 && currently_open_class (new_scope
))
6481 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6483 /* Make sure we look in the right scope the next time through
6485 parser
->scope
= new_scope
;
6488 /* If parsing tentatively, replace the sequence of tokens that makes
6489 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6490 token. That way, should we re-parse the token stream, we will
6491 not have to repeat the effort required to do the parse, nor will
6492 we issue duplicate error messages. */
6493 if (success
&& start
)
6497 token
= cp_lexer_token_at (parser
->lexer
, start
);
6498 /* Reset the contents of the START token. */
6499 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6500 /* Retrieve any deferred checks. Do not pop this access checks yet
6501 so the memory will not be reclaimed during token replacing below. */
6502 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6503 token
->u
.tree_check_value
->value
= parser
->scope
;
6504 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6505 token
->u
.tree_check_value
->qualifying_scope
=
6506 parser
->qualifying_scope
;
6507 token
->keyword
= RID_MAX
;
6509 /* Purge all subsequent tokens. */
6510 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6514 pop_to_parent_deferring_access_checks ();
6516 return success
? parser
->scope
: NULL_TREE
;
6519 /* Parse a nested-name-specifier. See
6520 cp_parser_nested_name_specifier_opt for details. This function
6521 behaves identically, except that it will an issue an error if no
6522 nested-name-specifier is present. */
6525 cp_parser_nested_name_specifier (cp_parser
*parser
,
6526 bool typename_keyword_p
,
6527 bool check_dependency_p
,
6529 bool is_declaration
)
6533 /* Look for the nested-name-specifier. */
6534 scope
= cp_parser_nested_name_specifier_opt (parser
,
6539 /* If it was not present, issue an error message. */
6542 cp_parser_error (parser
, "expected nested-name-specifier");
6543 parser
->scope
= NULL_TREE
;
6549 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6550 this is either a class-name or a namespace-name (which corresponds
6551 to the class-or-namespace-name production in the grammar). For
6552 C++0x, it can also be a type-name that refers to an enumeration
6553 type or a simple-template-id.
6555 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6556 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6557 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6558 TYPE_P is TRUE iff the next name should be taken as a class-name,
6559 even the same name is declared to be another entity in the same
6562 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6563 specified by the class-or-namespace-name. If neither is found the
6564 ERROR_MARK_NODE is returned. */
6567 cp_parser_qualifying_entity (cp_parser
*parser
,
6568 bool typename_keyword_p
,
6569 bool template_keyword_p
,
6570 bool check_dependency_p
,
6572 bool is_declaration
)
6575 tree saved_qualifying_scope
;
6576 tree saved_object_scope
;
6579 bool successful_parse_p
;
6581 /* DR 743: decltype can appear in a nested-name-specifier. */
6582 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6584 scope
= cp_parser_decltype (parser
);
6585 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6586 && !MAYBE_CLASS_TYPE_P (scope
))
6588 cp_parser_simulate_error (parser
);
6589 return error_mark_node
;
6591 if (TYPE_NAME (scope
))
6592 scope
= TYPE_NAME (scope
);
6596 /* Before we try to parse the class-name, we must save away the
6597 current PARSER->SCOPE since cp_parser_class_name will destroy
6599 saved_scope
= parser
->scope
;
6600 saved_qualifying_scope
= parser
->qualifying_scope
;
6601 saved_object_scope
= parser
->object_scope
;
6602 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6603 there is no need to look for a namespace-name. */
6604 only_class_p
= template_keyword_p
6605 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6607 cp_parser_parse_tentatively (parser
);
6608 scope
= cp_parser_class_name (parser
,
6611 type_p
? class_type
: none_type
,
6613 /*class_head_p=*/false,
6615 /*enum_ok=*/cxx_dialect
> cxx98
);
6616 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6617 /* If that didn't work, try for a namespace-name. */
6618 if (!only_class_p
&& !successful_parse_p
)
6620 /* Restore the saved scope. */
6621 parser
->scope
= saved_scope
;
6622 parser
->qualifying_scope
= saved_qualifying_scope
;
6623 parser
->object_scope
= saved_object_scope
;
6624 /* If we are not looking at an identifier followed by the scope
6625 resolution operator, then this is not part of a
6626 nested-name-specifier. (Note that this function is only used
6627 to parse the components of a nested-name-specifier.) */
6628 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6629 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6630 return error_mark_node
;
6631 scope
= cp_parser_namespace_name (parser
);
6637 /* Return true if we are looking at a compound-literal, false otherwise. */
6640 cp_parser_compound_literal_p (cp_parser
*parser
)
6642 cp_lexer_save_tokens (parser
->lexer
);
6644 /* Skip tokens until the next token is a closing parenthesis.
6645 If we find the closing `)', and the next token is a `{', then
6646 we are looking at a compound-literal. */
6647 bool compound_literal_p
6648 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6649 /*consume_paren=*/true)
6650 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6652 /* Roll back the tokens we skipped. */
6653 cp_lexer_rollback_tokens (parser
->lexer
);
6655 return compound_literal_p
;
6658 /* Return true if EXPR is the integer constant zero or a complex constant
6659 of zero, without any folding, but ignoring location wrappers. */
6662 literal_integer_zerop (const_tree expr
)
6664 return (location_wrapper_p (expr
)
6665 && integer_zerop (TREE_OPERAND (expr
, 0)));
6668 /* Parse a postfix-expression.
6672 postfix-expression [ expression ]
6673 postfix-expression ( expression-list [opt] )
6674 simple-type-specifier ( expression-list [opt] )
6675 typename :: [opt] nested-name-specifier identifier
6676 ( expression-list [opt] )
6677 typename :: [opt] nested-name-specifier template [opt] template-id
6678 ( expression-list [opt] )
6679 postfix-expression . template [opt] id-expression
6680 postfix-expression -> template [opt] id-expression
6681 postfix-expression . pseudo-destructor-name
6682 postfix-expression -> pseudo-destructor-name
6683 postfix-expression ++
6684 postfix-expression --
6685 dynamic_cast < type-id > ( expression )
6686 static_cast < type-id > ( expression )
6687 reinterpret_cast < type-id > ( expression )
6688 const_cast < type-id > ( expression )
6689 typeid ( expression )
6695 ( type-id ) { initializer-list , [opt] }
6697 This extension is a GNU version of the C99 compound-literal
6698 construct. (The C99 grammar uses `type-name' instead of `type-id',
6699 but they are essentially the same concept.)
6701 If ADDRESS_P is true, the postfix expression is the operand of the
6702 `&' operator. CAST_P is true if this expression is the target of a
6705 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6706 class member access expressions [expr.ref].
6708 Returns a representation of the expression. */
6711 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6712 bool member_access_only_p
, bool decltype_p
,
6713 cp_id_kind
* pidk_return
)
6718 cp_id_kind idk
= CP_ID_KIND_NONE
;
6719 cp_expr postfix_expression
= NULL_TREE
;
6720 bool is_member_access
= false;
6722 /* Peek at the next token. */
6723 token
= cp_lexer_peek_token (parser
->lexer
);
6724 loc
= token
->location
;
6725 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6727 /* Some of the productions are determined by keywords. */
6728 keyword
= token
->keyword
;
6738 const char *saved_message
;
6739 bool saved_in_type_id_in_expr_p
;
6741 /* All of these can be handled in the same way from the point
6742 of view of parsing. Begin by consuming the token
6743 identifying the cast. */
6744 cp_lexer_consume_token (parser
->lexer
);
6746 /* New types cannot be defined in the cast. */
6747 saved_message
= parser
->type_definition_forbidden_message
;
6748 parser
->type_definition_forbidden_message
6749 = G_("types may not be defined in casts");
6751 /* Look for the opening `<'. */
6752 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6753 /* Parse the type to which we are casting. */
6754 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6755 parser
->in_type_id_in_expr_p
= true;
6756 type
= cp_parser_type_id (parser
);
6757 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6758 /* Look for the closing `>'. */
6759 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6760 /* Restore the old message. */
6761 parser
->type_definition_forbidden_message
= saved_message
;
6763 bool saved_greater_than_is_operator_p
6764 = parser
->greater_than_is_operator_p
;
6765 parser
->greater_than_is_operator_p
= true;
6767 /* And the expression which is being cast. */
6768 matching_parens parens
;
6769 parens
.require_open (parser
);
6770 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6771 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6773 location_t end_loc
= close_paren
?
6774 close_paren
->location
: UNKNOWN_LOCATION
;
6776 parser
->greater_than_is_operator_p
6777 = saved_greater_than_is_operator_p
;
6779 /* Only type conversions to integral or enumeration types
6780 can be used in constant-expressions. */
6781 if (!cast_valid_in_integral_constant_expression_p (type
)
6782 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6784 postfix_expression
= error_mark_node
;
6792 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6796 = build_static_cast (type
, expression
, tf_warning_or_error
);
6800 = build_reinterpret_cast (type
, expression
,
6801 tf_warning_or_error
);
6805 = build_const_cast (type
, expression
, tf_warning_or_error
);
6811 /* Construct a location e.g. :
6812 reinterpret_cast <int *> (expr)
6813 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6814 ranging from the start of the "*_cast" token to the final closing
6815 paren, with the caret at the start. */
6816 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6817 postfix_expression
.set_location (cp_cast_loc
);
6824 const char *saved_message
;
6825 bool saved_in_type_id_in_expr_p
;
6827 /* Consume the `typeid' token. */
6828 cp_lexer_consume_token (parser
->lexer
);
6829 /* Look for the `(' token. */
6830 matching_parens parens
;
6831 parens
.require_open (parser
);
6832 /* Types cannot be defined in a `typeid' expression. */
6833 saved_message
= parser
->type_definition_forbidden_message
;
6834 parser
->type_definition_forbidden_message
6835 = G_("types may not be defined in a %<typeid%> expression");
6836 /* We can't be sure yet whether we're looking at a type-id or an
6838 cp_parser_parse_tentatively (parser
);
6839 /* Try a type-id first. */
6840 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6841 parser
->in_type_id_in_expr_p
= true;
6842 type
= cp_parser_type_id (parser
);
6843 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6844 /* Look for the `)' token. Otherwise, we can't be sure that
6845 we're not looking at an expression: consider `typeid (int
6846 (3))', for example. */
6847 cp_token
*close_paren
= parens
.require_close (parser
);
6848 /* If all went well, simply lookup the type-id. */
6849 if (cp_parser_parse_definitely (parser
))
6850 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6851 /* Otherwise, fall back to the expression variant. */
6856 /* Look for an expression. */
6857 expression
= cp_parser_expression (parser
, & idk
);
6858 /* Compute its typeid. */
6859 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6860 /* Look for the `)' token. */
6861 close_paren
= parens
.require_close (parser
);
6863 /* Restore the saved message. */
6864 parser
->type_definition_forbidden_message
= saved_message
;
6865 /* `typeid' may not appear in an integral constant expression. */
6866 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6867 postfix_expression
= error_mark_node
;
6869 /* Construct a location e.g. :
6872 ranging from the start of the "typeid" token to the final closing
6873 paren, with the caret at the start. */
6876 location_t typeid_loc
6877 = make_location (start_loc
, start_loc
, close_paren
->location
);
6878 postfix_expression
.set_location (typeid_loc
);
6879 postfix_expression
.maybe_add_location_wrapper ();
6887 /* The syntax permitted here is the same permitted for an
6888 elaborated-type-specifier. */
6889 ++parser
->prevent_constrained_type_specifiers
;
6890 type
= cp_parser_elaborated_type_specifier (parser
,
6891 /*is_friend=*/false,
6892 /*is_declaration=*/false);
6893 --parser
->prevent_constrained_type_specifiers
;
6894 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6899 case RID_BUILTIN_SHUFFLE
:
6900 case RID_BUILTIN_LAUNDER
:
6902 vec
<tree
, va_gc
> *vec
;
6906 cp_lexer_consume_token (parser
->lexer
);
6907 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6908 /*cast_p=*/false, /*allow_expansion_p=*/true,
6909 /*non_constant_p=*/NULL
);
6912 postfix_expression
= error_mark_node
;
6916 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6922 if (vec
->length () == 1)
6924 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6927 error_at (loc
, "wrong number of arguments to "
6928 "%<__builtin_addressof%>");
6929 postfix_expression
= error_mark_node
;
6933 case RID_BUILTIN_LAUNDER
:
6934 if (vec
->length () == 1)
6935 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6936 tf_warning_or_error
);
6939 error_at (loc
, "wrong number of arguments to "
6940 "%<__builtin_launder%>");
6941 postfix_expression
= error_mark_node
;
6945 case RID_BUILTIN_SHUFFLE
:
6946 if (vec
->length () == 2)
6948 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6949 (*vec
)[1], tf_warning_or_error
);
6950 else if (vec
->length () == 3)
6952 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6953 (*vec
)[2], tf_warning_or_error
);
6956 error_at (loc
, "wrong number of arguments to "
6957 "%<__builtin_shuffle%>");
6958 postfix_expression
= error_mark_node
;
6972 /* If the next thing is a simple-type-specifier, we may be
6973 looking at a functional cast. We could also be looking at
6974 an id-expression. So, we try the functional cast, and if
6975 that doesn't work we fall back to the primary-expression. */
6976 cp_parser_parse_tentatively (parser
);
6977 /* Look for the simple-type-specifier. */
6978 ++parser
->prevent_constrained_type_specifiers
;
6979 type
= cp_parser_simple_type_specifier (parser
,
6980 /*decl_specs=*/NULL
,
6981 CP_PARSER_FLAGS_NONE
);
6982 --parser
->prevent_constrained_type_specifiers
;
6983 /* Parse the cast itself. */
6984 if (!cp_parser_error_occurred (parser
))
6986 = cp_parser_functional_cast (parser
, type
);
6987 /* If that worked, we're done. */
6988 if (cp_parser_parse_definitely (parser
))
6991 /* If the functional-cast didn't work out, try a
6992 compound-literal. */
6993 if (cp_parser_allow_gnu_extensions_p (parser
)
6994 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6996 cp_expr initializer
= NULL_TREE
;
6998 cp_parser_parse_tentatively (parser
);
7000 matching_parens parens
;
7001 parens
.consume_open (parser
);
7003 /* Avoid calling cp_parser_type_id pointlessly, see comment
7004 in cp_parser_cast_expression about c++/29234. */
7005 if (!cp_parser_compound_literal_p (parser
))
7006 cp_parser_simulate_error (parser
);
7009 /* Parse the type. */
7010 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7011 parser
->in_type_id_in_expr_p
= true;
7012 type
= cp_parser_type_id (parser
);
7013 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7014 parens
.require_close (parser
);
7017 /* If things aren't going well, there's no need to
7019 if (!cp_parser_error_occurred (parser
))
7021 bool non_constant_p
;
7022 /* Parse the brace-enclosed initializer list. */
7023 initializer
= cp_parser_braced_list (parser
,
7026 /* If that worked, we're definitely looking at a
7027 compound-literal expression. */
7028 if (cp_parser_parse_definitely (parser
))
7030 /* Warn the user that a compound literal is not
7031 allowed in standard C++. */
7032 pedwarn (input_location
, OPT_Wpedantic
,
7033 "ISO C++ forbids compound-literals");
7034 /* For simplicity, we disallow compound literals in
7035 constant-expressions. We could
7036 allow compound literals of integer type, whose
7037 initializer was a constant, in constant
7038 expressions. Permitting that usage, as a further
7039 extension, would not change the meaning of any
7040 currently accepted programs. (Of course, as
7041 compound literals are not part of ISO C++, the
7042 standard has nothing to say.) */
7043 if (cp_parser_non_integral_constant_expression (parser
,
7046 postfix_expression
= error_mark_node
;
7049 /* Form the representation of the compound-literal. */
7051 = finish_compound_literal (type
, initializer
,
7052 tf_warning_or_error
, fcl_c99
);
7053 postfix_expression
.set_location (initializer
.get_location ());
7058 /* It must be a primary-expression. */
7060 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7061 /*template_arg_p=*/false,
7068 /* Note that we don't need to worry about calling build_cplus_new on a
7069 class-valued CALL_EXPR in decltype when it isn't the end of the
7070 postfix-expression; unary_complex_lvalue will take care of that for
7073 /* Keep looping until the postfix-expression is complete. */
7076 if (idk
== CP_ID_KIND_UNQUALIFIED
7077 && identifier_p (postfix_expression
)
7078 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7079 /* It is not a Koenig lookup function call. */
7081 = unqualified_name_lookup_error (postfix_expression
);
7083 /* Peek at the next token. */
7084 token
= cp_lexer_peek_token (parser
->lexer
);
7086 switch (token
->type
)
7088 case CPP_OPEN_SQUARE
:
7089 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7091 cp_parser_error (parser
,
7092 "two consecutive %<[%> shall "
7093 "only introduce an attribute");
7094 return error_mark_node
;
7097 = cp_parser_postfix_open_square_expression (parser
,
7101 postfix_expression
.set_range (start_loc
,
7102 postfix_expression
.get_location ());
7104 idk
= CP_ID_KIND_NONE
;
7105 is_member_access
= false;
7108 case CPP_OPEN_PAREN
:
7109 /* postfix-expression ( expression-list [opt] ) */
7112 bool is_builtin_constant_p
;
7113 bool saved_integral_constant_expression_p
= false;
7114 bool saved_non_integral_constant_expression_p
= false;
7115 tsubst_flags_t complain
= complain_flags (decltype_p
);
7116 vec
<tree
, va_gc
> *args
;
7117 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7119 is_member_access
= false;
7121 is_builtin_constant_p
7122 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7123 if (is_builtin_constant_p
)
7125 /* The whole point of __builtin_constant_p is to allow
7126 non-constant expressions to appear as arguments. */
7127 saved_integral_constant_expression_p
7128 = parser
->integral_constant_expression_p
;
7129 saved_non_integral_constant_expression_p
7130 = parser
->non_integral_constant_expression_p
;
7131 parser
->integral_constant_expression_p
= false;
7133 args
= (cp_parser_parenthesized_expression_list
7135 /*cast_p=*/false, /*allow_expansion_p=*/true,
7136 /*non_constant_p=*/NULL
,
7137 /*close_paren_loc=*/&close_paren_loc
,
7138 /*wrap_locations_p=*/true));
7139 if (is_builtin_constant_p
)
7141 parser
->integral_constant_expression_p
7142 = saved_integral_constant_expression_p
;
7143 parser
->non_integral_constant_expression_p
7144 = saved_non_integral_constant_expression_p
;
7149 postfix_expression
= error_mark_node
;
7153 /* Function calls are not permitted in
7154 constant-expressions. */
7155 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7156 && cp_parser_non_integral_constant_expression (parser
,
7159 postfix_expression
= error_mark_node
;
7160 release_tree_vector (args
);
7165 if (idk
== CP_ID_KIND_UNQUALIFIED
7166 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7168 if (identifier_p (postfix_expression
))
7170 if (!args
->is_empty ())
7173 if (!any_type_dependent_arguments_p (args
))
7175 = perform_koenig_lookup (postfix_expression
, args
,
7180 = unqualified_fn_lookup_error (postfix_expression
);
7182 /* We do not perform argument-dependent lookup if
7183 normal lookup finds a non-function, in accordance
7184 with the expected resolution of DR 218. */
7185 else if (!args
->is_empty ()
7186 && is_overloaded_fn (postfix_expression
))
7188 tree fn
= get_first_fn (postfix_expression
);
7189 fn
= STRIP_TEMPLATE (fn
);
7191 /* Do not do argument dependent lookup if regular
7192 lookup finds a member function or a block-scope
7193 function declaration. [basic.lookup.argdep]/3 */
7194 if (!DECL_FUNCTION_MEMBER_P (fn
)
7195 && !DECL_LOCAL_FUNCTION_P (fn
))
7198 if (!any_type_dependent_arguments_p (args
))
7200 = perform_koenig_lookup (postfix_expression
, args
,
7206 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7208 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7209 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7211 if (processing_template_decl
7212 && (type_dependent_object_expression_p (instance
)
7213 || (!BASELINK_P (fn
)
7214 && TREE_CODE (fn
) != FIELD_DECL
)
7215 || type_dependent_expression_p (fn
)
7216 || any_type_dependent_arguments_p (args
)))
7218 maybe_generic_this_capture (instance
, fn
);
7220 = build_min_nt_call_vec (postfix_expression
, args
);
7221 release_tree_vector (args
);
7225 if (BASELINK_P (fn
))
7228 = (build_new_method_call
7229 (instance
, fn
, &args
, NULL_TREE
,
7230 (idk
== CP_ID_KIND_QUALIFIED
7231 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7238 = finish_call_expr (postfix_expression
, &args
,
7239 /*disallow_virtual=*/false,
7243 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7244 || TREE_CODE (postfix_expression
) == MEMBER_REF
7245 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7246 postfix_expression
= (build_offset_ref_call_from_tree
7247 (postfix_expression
, &args
,
7249 else if (idk
== CP_ID_KIND_QUALIFIED
)
7250 /* A call to a static class member, or a namespace-scope
7253 = finish_call_expr (postfix_expression
, &args
,
7254 /*disallow_virtual=*/true,
7258 /* All other function calls. */
7260 = finish_call_expr (postfix_expression
, &args
,
7261 /*disallow_virtual=*/false,
7265 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7267 location_t combined_loc
= make_location (token
->location
,
7270 postfix_expression
.set_location (combined_loc
);
7273 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7274 idk
= CP_ID_KIND_NONE
;
7276 release_tree_vector (args
);
7282 /* postfix-expression . template [opt] id-expression
7283 postfix-expression . pseudo-destructor-name
7284 postfix-expression -> template [opt] id-expression
7285 postfix-expression -> pseudo-destructor-name */
7287 /* Consume the `.' or `->' operator. */
7288 cp_lexer_consume_token (parser
->lexer
);
7291 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7295 is_member_access
= true;
7299 /* postfix-expression ++ */
7300 /* Consume the `++' token. */
7301 cp_lexer_consume_token (parser
->lexer
);
7302 /* Generate a representation for the complete expression. */
7304 = finish_increment_expr (postfix_expression
,
7305 POSTINCREMENT_EXPR
);
7306 /* Increments may not appear in constant-expressions. */
7307 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7308 postfix_expression
= error_mark_node
;
7309 idk
= CP_ID_KIND_NONE
;
7310 is_member_access
= false;
7313 case CPP_MINUS_MINUS
:
7314 /* postfix-expression -- */
7315 /* Consume the `--' token. */
7316 cp_lexer_consume_token (parser
->lexer
);
7317 /* Generate a representation for the complete expression. */
7319 = finish_increment_expr (postfix_expression
,
7320 POSTDECREMENT_EXPR
);
7321 /* Decrements may not appear in constant-expressions. */
7322 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7323 postfix_expression
= error_mark_node
;
7324 idk
= CP_ID_KIND_NONE
;
7325 is_member_access
= false;
7329 if (pidk_return
!= NULL
)
7330 * pidk_return
= idk
;
7331 if (member_access_only_p
)
7332 return is_member_access
7333 ? postfix_expression
7334 : cp_expr (error_mark_node
);
7336 return postfix_expression
;
7340 /* We should never get here. */
7342 return error_mark_node
;
7345 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7346 by cp_parser_builtin_offsetof. We're looking for
7348 postfix-expression [ expression ]
7349 postfix-expression [ braced-init-list ] (C++11)
7351 FOR_OFFSETOF is set if we're being called in that context, which
7352 changes how we deal with integer constant expressions. */
7355 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7356 tree postfix_expression
,
7360 tree index
= NULL_TREE
;
7361 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7362 bool saved_greater_than_is_operator_p
;
7364 /* Consume the `[' token. */
7365 cp_lexer_consume_token (parser
->lexer
);
7367 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7368 parser
->greater_than_is_operator_p
= true;
7370 /* Parse the index expression. */
7371 /* ??? For offsetof, there is a question of what to allow here. If
7372 offsetof is not being used in an integral constant expression context,
7373 then we *could* get the right answer by computing the value at runtime.
7374 If we are in an integral constant expression context, then we might
7375 could accept any constant expression; hard to say without analysis.
7376 Rather than open the barn door too wide right away, allow only integer
7377 constant expressions here. */
7379 index
= cp_parser_constant_expression (parser
);
7382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7384 bool expr_nonconst_p
;
7385 cp_lexer_set_source_position (parser
->lexer
);
7386 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7387 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7390 index
= cp_parser_expression (parser
);
7393 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7395 /* Look for the closing `]'. */
7396 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7398 /* Build the ARRAY_REF. */
7399 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7402 /* When not doing offsetof, array references are not permitted in
7403 constant-expressions. */
7405 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7406 postfix_expression
= error_mark_node
;
7408 return postfix_expression
;
7411 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7412 dereference of incomplete type, returns true if error_mark_node should
7413 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7414 and *DEPENDENT_P. */
7417 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7420 /* In a template, be permissive by treating an object expression
7421 of incomplete type as dependent (after a pedwarn). */
7422 diagnostic_t kind
= (processing_template_decl
7423 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7425 switch (TREE_CODE (*postfix_expression
))
7428 case REINTERPRET_CAST_EXPR
:
7429 case CONST_CAST_EXPR
:
7430 case STATIC_CAST_EXPR
:
7431 case DYNAMIC_CAST_EXPR
:
7432 case IMPLICIT_CONV_EXPR
:
7433 case VIEW_CONVERT_EXPR
:
7434 case NON_LVALUE_EXPR
:
7438 /* Don't emit any diagnostic for OVERLOADs. */
7442 /* Avoid clobbering e.g. DECLs. */
7443 if (!EXPR_P (*postfix_expression
))
7448 if (kind
== DK_IGNORED
)
7451 location_t exploc
= location_of (*postfix_expression
);
7452 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7453 if (!MAYBE_CLASS_TYPE_P (*scope
))
7455 if (kind
== DK_ERROR
)
7456 *scope
= *postfix_expression
= error_mark_node
;
7457 else if (processing_template_decl
)
7459 *dependent_p
= true;
7460 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7465 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7466 by cp_parser_builtin_offsetof. We're looking for
7468 postfix-expression . template [opt] id-expression
7469 postfix-expression . pseudo-destructor-name
7470 postfix-expression -> template [opt] id-expression
7471 postfix-expression -> pseudo-destructor-name
7473 FOR_OFFSETOF is set if we're being called in that context. That sorta
7474 limits what of the above we'll actually accept, but nevermind.
7475 TOKEN_TYPE is the "." or "->" token, which will already have been
7476 removed from the stream. */
7479 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7480 enum cpp_ttype token_type
,
7481 cp_expr postfix_expression
,
7482 bool for_offsetof
, cp_id_kind
*idk
,
7483 location_t location
)
7487 bool pseudo_destructor_p
;
7488 tree scope
= NULL_TREE
;
7489 location_t start_loc
= postfix_expression
.get_start ();
7491 /* If this is a `->' operator, dereference the pointer. */
7492 if (token_type
== CPP_DEREF
)
7493 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7494 tf_warning_or_error
);
7495 /* Check to see whether or not the expression is type-dependent and
7496 not the current instantiation. */
7497 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7498 /* The identifier following the `->' or `.' is not qualified. */
7499 parser
->scope
= NULL_TREE
;
7500 parser
->qualifying_scope
= NULL_TREE
;
7501 parser
->object_scope
= NULL_TREE
;
7502 *idk
= CP_ID_KIND_NONE
;
7504 /* Enter the scope corresponding to the type of the object
7505 given by the POSTFIX_EXPRESSION. */
7508 scope
= TREE_TYPE (postfix_expression
);
7509 /* According to the standard, no expression should ever have
7510 reference type. Unfortunately, we do not currently match
7511 the standard in this respect in that our internal representation
7512 of an expression may have reference type even when the standard
7513 says it does not. Therefore, we have to manually obtain the
7514 underlying type here. */
7515 scope
= non_reference (scope
);
7516 /* The type of the POSTFIX_EXPRESSION must be complete. */
7517 /* Unlike the object expression in other contexts, *this is not
7518 required to be of complete type for purposes of class member
7519 access (5.2.5) outside the member function body. */
7520 if (postfix_expression
!= current_class_ref
7521 && scope
!= error_mark_node
7522 && !currently_open_class (scope
))
7524 scope
= complete_type (scope
);
7525 if (!COMPLETE_TYPE_P (scope
)
7526 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7528 return error_mark_node
;
7533 /* Let the name lookup machinery know that we are processing a
7534 class member access expression. */
7535 parser
->context
->object_type
= scope
;
7536 /* If something went wrong, we want to be able to discern that case,
7537 as opposed to the case where there was no SCOPE due to the type
7538 of expression being dependent. */
7540 scope
= error_mark_node
;
7541 /* If the SCOPE was erroneous, make the various semantic analysis
7542 functions exit quickly -- and without issuing additional error
7544 if (scope
== error_mark_node
)
7545 postfix_expression
= error_mark_node
;
7550 /* Tell cp_parser_lookup_name that there was an object, even though it's
7552 parser
->context
->object_type
= unknown_type_node
;
7554 /* Assume this expression is not a pseudo-destructor access. */
7555 pseudo_destructor_p
= false;
7557 /* If the SCOPE is a scalar type, then, if this is a valid program,
7558 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7559 is type dependent, it can be pseudo-destructor-name or something else.
7560 Try to parse it as pseudo-destructor-name first. */
7561 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7566 cp_parser_parse_tentatively (parser
);
7567 /* Parse the pseudo-destructor-name. */
7569 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7572 && (cp_parser_error_occurred (parser
)
7573 || !SCALAR_TYPE_P (type
)))
7574 cp_parser_abort_tentative_parse (parser
);
7575 else if (cp_parser_parse_definitely (parser
))
7577 pseudo_destructor_p
= true;
7579 = finish_pseudo_destructor_expr (postfix_expression
,
7584 if (!pseudo_destructor_p
)
7586 /* If the SCOPE is not a scalar type, we are looking at an
7587 ordinary class member access expression, rather than a
7588 pseudo-destructor-name. */
7590 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7591 /* Parse the id-expression. */
7592 name
= (cp_parser_id_expression
7594 cp_parser_optional_template_keyword (parser
),
7595 /*check_dependency_p=*/true,
7597 /*declarator_p=*/false,
7598 /*optional_p=*/false));
7599 /* In general, build a SCOPE_REF if the member name is qualified.
7600 However, if the name was not dependent and has already been
7601 resolved; there is no need to build the SCOPE_REF. For example;
7603 struct X { void f(); };
7604 template <typename T> void f(T* t) { t->X::f(); }
7606 Even though "t" is dependent, "X::f" is not and has been resolved
7607 to a BASELINK; there is no need to include scope information. */
7609 /* But we do need to remember that there was an explicit scope for
7610 virtual function calls. */
7612 *idk
= CP_ID_KIND_QUALIFIED
;
7614 /* If the name is a template-id that names a type, we will get a
7615 TYPE_DECL here. That is invalid code. */
7616 if (TREE_CODE (name
) == TYPE_DECL
)
7618 error_at (token
->location
, "invalid use of %qD", name
);
7619 postfix_expression
= error_mark_node
;
7623 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7625 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7627 error_at (token
->location
, "%<%D::%D%> is not a class member",
7628 parser
->scope
, name
);
7629 postfix_expression
= error_mark_node
;
7632 name
= build_qualified_name (/*type=*/NULL_TREE
,
7636 parser
->scope
= NULL_TREE
;
7637 parser
->qualifying_scope
= NULL_TREE
;
7638 parser
->object_scope
= NULL_TREE
;
7640 if (parser
->scope
&& name
&& BASELINK_P (name
))
7641 adjust_result_of_qualified_name_lookup
7642 (name
, parser
->scope
, scope
);
7644 = finish_class_member_access_expr (postfix_expression
, name
,
7646 tf_warning_or_error
);
7647 /* Build a location e.g.:
7650 where the caret is at the deref token, ranging from
7651 the start of postfix_expression to the end of the access expr. */
7653 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7654 location_t combined_loc
7655 = make_location (input_location
, start_loc
, end_loc
);
7656 protected_set_expr_location (postfix_expression
, combined_loc
);
7660 /* We no longer need to look up names in the scope of the object on
7661 the left-hand side of the `.' or `->' operator. */
7662 parser
->context
->object_type
= NULL_TREE
;
7664 /* Outside of offsetof, these operators may not appear in
7665 constant-expressions. */
7667 && (cp_parser_non_integral_constant_expression
7668 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7669 postfix_expression
= error_mark_node
;
7671 return postfix_expression
;
7674 /* Parse a parenthesized expression-list.
7677 assignment-expression
7678 expression-list, assignment-expression
7683 identifier, expression-list
7685 CAST_P is true if this expression is the target of a cast.
7687 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7690 WRAP_LOCATIONS_P is true if expressions within this list for which
7691 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7692 their source locations.
7694 Returns a vector of trees. Each element is a representation of an
7695 assignment-expression. NULL is returned if the ( and or ) are
7696 missing. An empty, but allocated, vector is returned on no
7697 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7698 if we are parsing an attribute list for an attribute that wants a
7699 plain identifier argument, normal_attr for an attribute that wants
7700 an expression, or non_attr if we aren't parsing an attribute list. If
7701 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7702 not all of the expressions in the list were constant.
7703 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7704 will be written to with the location of the closing parenthesis. If
7705 an error occurs, it may or may not be written to. */
7707 static vec
<tree
, va_gc
> *
7708 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7709 int is_attribute_list
,
7711 bool allow_expansion_p
,
7712 bool *non_constant_p
,
7713 location_t
*close_paren_loc
,
7714 bool wrap_locations_p
)
7716 vec
<tree
, va_gc
> *expression_list
;
7717 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7718 tree identifier
= NULL_TREE
;
7719 bool saved_greater_than_is_operator_p
;
7721 /* Assume all the expressions will be constant. */
7723 *non_constant_p
= false;
7725 matching_parens parens
;
7726 if (!parens
.require_open (parser
))
7729 expression_list
= make_tree_vector ();
7731 /* Within a parenthesized expression, a `>' token is always
7732 the greater-than operator. */
7733 saved_greater_than_is_operator_p
7734 = parser
->greater_than_is_operator_p
;
7735 parser
->greater_than_is_operator_p
= true;
7737 cp_expr
expr (NULL_TREE
);
7739 /* Consume expressions until there are no more. */
7740 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7743 /* At the beginning of attribute lists, check to see if the
7744 next token is an identifier. */
7745 if (is_attribute_list
== id_attr
7746 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7750 /* Consume the identifier. */
7751 token
= cp_lexer_consume_token (parser
->lexer
);
7752 /* Save the identifier. */
7753 identifier
= token
->u
.value
;
7757 bool expr_non_constant_p
;
7759 /* Parse the next assignment-expression. */
7760 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7762 /* A braced-init-list. */
7763 cp_lexer_set_source_position (parser
->lexer
);
7764 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7765 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7766 if (non_constant_p
&& expr_non_constant_p
)
7767 *non_constant_p
= true;
7769 else if (non_constant_p
)
7771 expr
= (cp_parser_constant_expression
7772 (parser
, /*allow_non_constant_p=*/true,
7773 &expr_non_constant_p
));
7774 if (expr_non_constant_p
)
7775 *non_constant_p
= true;
7778 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7782 expr
= instantiate_non_dependent_expr (expr
);
7784 /* If we have an ellipsis, then this is an expression
7786 if (allow_expansion_p
7787 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7789 /* Consume the `...'. */
7790 cp_lexer_consume_token (parser
->lexer
);
7792 /* Build the argument pack. */
7793 expr
= make_pack_expansion (expr
);
7796 if (wrap_locations_p
)
7797 expr
.maybe_add_location_wrapper ();
7799 /* Add it to the list. We add error_mark_node
7800 expressions to the list, so that we can still tell if
7801 the correct form for a parenthesized expression-list
7802 is found. That gives better errors. */
7803 vec_safe_push (expression_list
, expr
.get_value ());
7805 if (expr
== error_mark_node
)
7809 /* After the first item, attribute lists look the same as
7810 expression lists. */
7811 is_attribute_list
= non_attr
;
7814 /* If the next token isn't a `,', then we are done. */
7815 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7818 /* Otherwise, consume the `,' and keep going. */
7819 cp_lexer_consume_token (parser
->lexer
);
7822 if (close_paren_loc
)
7823 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7825 if (!parens
.require_close (parser
))
7830 /* We try and resync to an unnested comma, as that will give the
7831 user better diagnostics. */
7832 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7833 /*recovering=*/true,
7835 /*consume_paren=*/true);
7840 parser
->greater_than_is_operator_p
7841 = saved_greater_than_is_operator_p
;
7846 parser
->greater_than_is_operator_p
7847 = saved_greater_than_is_operator_p
;
7850 vec_safe_insert (expression_list
, 0, identifier
);
7852 return expression_list
;
7855 /* Parse a pseudo-destructor-name.
7857 pseudo-destructor-name:
7858 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7859 :: [opt] nested-name-specifier template template-id :: ~ type-name
7860 :: [opt] nested-name-specifier [opt] ~ type-name
7862 If either of the first two productions is used, sets *SCOPE to the
7863 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7864 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7865 or ERROR_MARK_NODE if the parse fails. */
7868 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7873 bool nested_name_specifier_p
;
7876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7877 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7878 && !type_dependent_expression_p (object
))
7880 if (cxx_dialect
< cxx14
)
7881 pedwarn (input_location
, 0,
7882 "%<~auto%> only available with "
7883 "-std=c++14 or -std=gnu++14");
7884 cp_lexer_consume_token (parser
->lexer
);
7885 cp_lexer_consume_token (parser
->lexer
);
7887 *type
= TREE_TYPE (object
);
7891 /* Assume that things will not work out. */
7892 *type
= error_mark_node
;
7894 /* Look for the optional `::' operator. */
7895 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7896 /* Look for the optional nested-name-specifier. */
7897 nested_name_specifier_p
7898 = (cp_parser_nested_name_specifier_opt (parser
,
7899 /*typename_keyword_p=*/false,
7900 /*check_dependency_p=*/true,
7902 /*is_declaration=*/false)
7904 /* Now, if we saw a nested-name-specifier, we might be doing the
7905 second production. */
7906 if (nested_name_specifier_p
7907 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7909 /* Consume the `template' keyword. */
7910 cp_lexer_consume_token (parser
->lexer
);
7911 /* Parse the template-id. */
7912 cp_parser_template_id (parser
,
7913 /*template_keyword_p=*/true,
7914 /*check_dependency_p=*/false,
7916 /*is_declaration=*/true);
7917 /* Look for the `::' token. */
7918 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7920 /* If the next token is not a `~', then there might be some
7921 additional qualification. */
7922 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7924 /* At this point, we're looking for "type-name :: ~". The type-name
7925 must not be a class-name, since this is a pseudo-destructor. So,
7926 it must be either an enum-name, or a typedef-name -- both of which
7927 are just identifiers. So, we peek ahead to check that the "::"
7928 and "~" tokens are present; if they are not, then we can avoid
7929 calling type_name. */
7930 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7931 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7932 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7934 cp_parser_error (parser
, "non-scalar type");
7938 /* Look for the type-name. */
7939 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7940 if (*scope
== error_mark_node
)
7943 /* Look for the `::' token. */
7944 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7949 /* Look for the `~'. */
7950 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7952 /* Once we see the ~, this has to be a pseudo-destructor. */
7953 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7954 cp_parser_commit_to_topmost_tentative_parse (parser
);
7956 /* Look for the type-name again. We are not responsible for
7957 checking that it matches the first type-name. */
7958 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7961 /* Parse a unary-expression.
7967 unary-operator cast-expression
7968 sizeof unary-expression
7970 alignof ( type-id ) [C++0x]
7977 __extension__ cast-expression
7978 __alignof__ unary-expression
7979 __alignof__ ( type-id )
7980 alignof unary-expression [C++0x]
7981 __real__ cast-expression
7982 __imag__ cast-expression
7984 sizeof ( type-id ) { initializer-list , [opt] }
7985 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7986 __alignof__ ( type-id ) { initializer-list , [opt] }
7988 ADDRESS_P is true iff the unary-expression is appearing as the
7989 operand of the `&' operator. CAST_P is true if this expression is
7990 the target of a cast.
7992 Returns a representation of the expression. */
7995 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7996 bool address_p
, bool cast_p
, bool decltype_p
)
7999 enum tree_code unary_operator
;
8001 /* Peek at the next token. */
8002 token
= cp_lexer_peek_token (parser
->lexer
);
8003 /* Some keywords give away the kind of expression. */
8004 if (token
->type
== CPP_KEYWORD
)
8006 enum rid keyword
= token
->keyword
;
8015 location_t start_loc
= token
->location
;
8017 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8018 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8020 /* Consume the token. */
8021 cp_lexer_consume_token (parser
->lexer
);
8022 /* Parse the operand. */
8023 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8025 if (TYPE_P (operand
))
8026 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8030 /* ISO C++ defines alignof only with types, not with
8031 expressions. So pedwarn if alignof is used with a non-
8032 type expression. However, __alignof__ is ok. */
8034 pedwarn (token
->location
, OPT_Wpedantic
,
8035 "ISO C++ does not allow %<alignof%> "
8038 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8040 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8041 SIZEOF_EXPR with the original operand. */
8042 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8044 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8046 if (!processing_template_decl
&& TYPE_P (operand
))
8048 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8049 build1 (NOP_EXPR
, operand
,
8051 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8054 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8055 TREE_SIDE_EFFECTS (ret
) = 0;
8056 TREE_READONLY (ret
) = 1;
8060 /* Construct a location e.g. :
8063 with start == caret at the start of the "alignof"/"sizeof"
8064 token, with the endpoint at the final closing paren. */
8065 location_t finish_loc
8066 = cp_lexer_previous_token (parser
->lexer
)->location
;
8067 location_t compound_loc
8068 = make_location (start_loc
, start_loc
, finish_loc
);
8070 cp_expr
ret_expr (ret
);
8071 ret_expr
.set_location (compound_loc
);
8072 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8077 return cp_parser_new_expression (parser
);
8080 return cp_parser_delete_expression (parser
);
8084 /* The saved value of the PEDANTIC flag. */
8088 /* Save away the PEDANTIC flag. */
8089 cp_parser_extension_opt (parser
, &saved_pedantic
);
8090 /* Parse the cast-expression. */
8091 expr
= cp_parser_simple_cast_expression (parser
);
8092 /* Restore the PEDANTIC flag. */
8093 pedantic
= saved_pedantic
;
8103 /* Consume the `__real__' or `__imag__' token. */
8104 cp_lexer_consume_token (parser
->lexer
);
8105 /* Parse the cast-expression. */
8106 expression
= cp_parser_simple_cast_expression (parser
);
8107 /* Create the complete representation. */
8108 return build_x_unary_op (token
->location
,
8109 (keyword
== RID_REALPART
8110 ? REALPART_EXPR
: IMAGPART_EXPR
),
8112 tf_warning_or_error
);
8116 case RID_TRANSACTION_ATOMIC
:
8117 case RID_TRANSACTION_RELAXED
:
8118 return cp_parser_transaction_expression (parser
, keyword
);
8123 const char *saved_message
;
8124 bool saved_integral_constant_expression_p
;
8125 bool saved_non_integral_constant_expression_p
;
8126 bool saved_greater_than_is_operator_p
;
8128 location_t start_loc
= token
->location
;
8130 cp_lexer_consume_token (parser
->lexer
);
8131 matching_parens parens
;
8132 parens
.require_open (parser
);
8134 saved_message
= parser
->type_definition_forbidden_message
;
8135 parser
->type_definition_forbidden_message
8136 = G_("types may not be defined in %<noexcept%> expressions");
8138 saved_integral_constant_expression_p
8139 = parser
->integral_constant_expression_p
;
8140 saved_non_integral_constant_expression_p
8141 = parser
->non_integral_constant_expression_p
;
8142 parser
->integral_constant_expression_p
= false;
8144 saved_greater_than_is_operator_p
8145 = parser
->greater_than_is_operator_p
;
8146 parser
->greater_than_is_operator_p
= true;
8148 ++cp_unevaluated_operand
;
8149 ++c_inhibit_evaluation_warnings
;
8150 ++cp_noexcept_operand
;
8151 expr
= cp_parser_expression (parser
);
8152 --cp_noexcept_operand
;
8153 --c_inhibit_evaluation_warnings
;
8154 --cp_unevaluated_operand
;
8156 parser
->greater_than_is_operator_p
8157 = saved_greater_than_is_operator_p
;
8159 parser
->integral_constant_expression_p
8160 = saved_integral_constant_expression_p
;
8161 parser
->non_integral_constant_expression_p
8162 = saved_non_integral_constant_expression_p
;
8164 parser
->type_definition_forbidden_message
= saved_message
;
8166 location_t finish_loc
8167 = cp_lexer_peek_token (parser
->lexer
)->location
;
8168 parens
.require_close (parser
);
8170 /* Construct a location of the form:
8173 with start == caret, finishing at the close-paren. */
8174 location_t noexcept_loc
8175 = make_location (start_loc
, start_loc
, finish_loc
);
8177 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8186 /* Look for the `:: new' and `:: delete', which also signal the
8187 beginning of a new-expression, or delete-expression,
8188 respectively. If the next token is `::', then it might be one of
8190 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8194 /* See if the token after the `::' is one of the keywords in
8195 which we're interested. */
8196 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8197 /* If it's `new', we have a new-expression. */
8198 if (keyword
== RID_NEW
)
8199 return cp_parser_new_expression (parser
);
8200 /* Similarly, for `delete'. */
8201 else if (keyword
== RID_DELETE
)
8202 return cp_parser_delete_expression (parser
);
8205 /* Look for a unary operator. */
8206 unary_operator
= cp_parser_unary_operator (token
);
8207 /* The `++' and `--' operators can be handled similarly, even though
8208 they are not technically unary-operators in the grammar. */
8209 if (unary_operator
== ERROR_MARK
)
8211 if (token
->type
== CPP_PLUS_PLUS
)
8212 unary_operator
= PREINCREMENT_EXPR
;
8213 else if (token
->type
== CPP_MINUS_MINUS
)
8214 unary_operator
= PREDECREMENT_EXPR
;
8215 /* Handle the GNU address-of-label extension. */
8216 else if (cp_parser_allow_gnu_extensions_p (parser
)
8217 && token
->type
== CPP_AND_AND
)
8221 location_t start_loc
= token
->location
;
8223 /* Consume the '&&' token. */
8224 cp_lexer_consume_token (parser
->lexer
);
8225 /* Look for the identifier. */
8226 location_t finish_loc
8227 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8228 identifier
= cp_parser_identifier (parser
);
8229 /* Construct a location of the form:
8232 with caret==start at the "&&", finish at the end of the label. */
8233 location_t combined_loc
8234 = make_location (start_loc
, start_loc
, finish_loc
);
8235 /* Create an expression representing the address. */
8236 expression
= finish_label_address_expr (identifier
, combined_loc
);
8237 if (cp_parser_non_integral_constant_expression (parser
,
8239 expression
= error_mark_node
;
8243 if (unary_operator
!= ERROR_MARK
)
8245 cp_expr cast_expression
;
8246 cp_expr expression
= error_mark_node
;
8247 non_integral_constant non_constant_p
= NIC_NONE
;
8248 location_t loc
= token
->location
;
8249 tsubst_flags_t complain
= complain_flags (decltype_p
);
8251 /* Consume the operator token. */
8252 token
= cp_lexer_consume_token (parser
->lexer
);
8253 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8255 /* Parse the cast-expression. */
8257 = cp_parser_cast_expression (parser
,
8258 unary_operator
== ADDR_EXPR
,
8264 OP_TOKEN CAST_EXPRESSION
8265 ^~~~~~~~~~~~~~~~~~~~~~~~~
8266 with start==caret at the operator token, and
8267 extending to the end of the cast_expression. */
8268 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8270 /* Now, build an appropriate representation. */
8271 switch (unary_operator
)
8274 non_constant_p
= NIC_STAR
;
8275 expression
= build_x_indirect_ref (loc
, cast_expression
,
8278 /* TODO: build_x_indirect_ref does not always honor the
8279 location, so ensure it is set. */
8280 expression
.set_location (loc
);
8284 non_constant_p
= NIC_ADDR
;
8287 expression
= build_x_unary_op (loc
, unary_operator
,
8290 /* TODO: build_x_unary_op does not always honor the location,
8291 so ensure it is set. */
8292 expression
.set_location (loc
);
8295 case PREINCREMENT_EXPR
:
8296 case PREDECREMENT_EXPR
:
8297 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8298 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8301 /* Immediately fold negation of a constant, unless the constant is 0
8302 (since -0 == 0) or it would overflow. */
8303 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8304 && CONSTANT_CLASS_P (cast_expression
)
8305 && !integer_zerop (cast_expression
)
8306 && !TREE_OVERFLOW (cast_expression
))
8308 tree folded
= fold_build1 (unary_operator
,
8309 TREE_TYPE (cast_expression
),
8311 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8313 expression
= cp_expr (folded
, loc
);
8318 case UNARY_PLUS_EXPR
:
8319 case TRUTH_NOT_EXPR
:
8320 expression
= finish_unary_op_expr (loc
, unary_operator
,
8321 cast_expression
, complain
);
8328 if (non_constant_p
!= NIC_NONE
8329 && cp_parser_non_integral_constant_expression (parser
,
8331 expression
= error_mark_node
;
8336 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8337 /*member_access_only_p=*/false,
8342 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8343 unary-operator, the corresponding tree code is returned. */
8345 static enum tree_code
8346 cp_parser_unary_operator (cp_token
* token
)
8348 switch (token
->type
)
8351 return INDIRECT_REF
;
8357 return UNARY_PLUS_EXPR
;
8363 return TRUTH_NOT_EXPR
;
8366 return BIT_NOT_EXPR
;
8373 /* Parse a new-expression.
8376 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8377 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8379 Returns a representation of the expression. */
8382 cp_parser_new_expression (cp_parser
* parser
)
8384 bool global_scope_p
;
8385 vec
<tree
, va_gc
> *placement
;
8387 vec
<tree
, va_gc
> *initializer
;
8388 tree nelts
= NULL_TREE
;
8391 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8393 /* Look for the optional `::' operator. */
8395 = (cp_parser_global_scope_opt (parser
,
8396 /*current_scope_valid_p=*/false)
8398 /* Look for the `new' operator. */
8399 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8400 /* There's no easy way to tell a new-placement from the
8401 `( type-id )' construct. */
8402 cp_parser_parse_tentatively (parser
);
8403 /* Look for a new-placement. */
8404 placement
= cp_parser_new_placement (parser
);
8405 /* If that didn't work out, there's no new-placement. */
8406 if (!cp_parser_parse_definitely (parser
))
8408 if (placement
!= NULL
)
8409 release_tree_vector (placement
);
8413 /* If the next token is a `(', then we have a parenthesized
8415 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8418 const char *saved_message
= parser
->type_definition_forbidden_message
;
8420 /* Consume the `('. */
8421 matching_parens parens
;
8422 parens
.consume_open (parser
);
8424 /* Parse the type-id. */
8425 parser
->type_definition_forbidden_message
8426 = G_("types may not be defined in a new-expression");
8428 type_id_in_expr_sentinel
s (parser
);
8429 type
= cp_parser_type_id (parser
);
8431 parser
->type_definition_forbidden_message
= saved_message
;
8433 /* Look for the closing `)'. */
8434 parens
.require_close (parser
);
8435 token
= cp_lexer_peek_token (parser
->lexer
);
8436 /* There should not be a direct-new-declarator in this production,
8437 but GCC used to allowed this, so we check and emit a sensible error
8438 message for this case. */
8439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8441 error_at (token
->location
,
8442 "array bound forbidden after parenthesized type-id");
8443 inform (token
->location
,
8444 "try removing the parentheses around the type-id");
8445 cp_parser_direct_new_declarator (parser
);
8448 /* Otherwise, there must be a new-type-id. */
8450 type
= cp_parser_new_type_id (parser
, &nelts
);
8452 /* If the next token is a `(' or '{', then we have a new-initializer. */
8453 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8454 if (token
->type
== CPP_OPEN_PAREN
8455 || token
->type
== CPP_OPEN_BRACE
)
8456 initializer
= cp_parser_new_initializer (parser
);
8460 /* A new-expression may not appear in an integral constant
8462 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8463 ret
= error_mark_node
;
8464 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8465 of a new-type-id or type-id of a new-expression, the new-expression shall
8466 contain a new-initializer of the form ( assignment-expression )".
8467 Additionally, consistently with the spirit of DR 1467, we want to accept
8468 'new auto { 2 }' too. */
8469 else if ((ret
= type_uses_auto (type
))
8470 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8471 && (vec_safe_length (initializer
) != 1
8472 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8473 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8475 error_at (token
->location
,
8476 "initialization of new-expression for type %<auto%> "
8477 "requires exactly one element");
8478 ret
= error_mark_node
;
8482 /* Construct a location e.g.:
8485 with caret == start at the start of the "new" token, and the end
8486 at the end of the final token we consumed. */
8487 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8488 location_t end_loc
= get_finish (end_tok
->location
);
8489 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8491 /* Create a representation of the new-expression. */
8492 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8493 tf_warning_or_error
);
8494 protected_set_expr_location (ret
, combined_loc
);
8497 if (placement
!= NULL
)
8498 release_tree_vector (placement
);
8499 if (initializer
!= NULL
)
8500 release_tree_vector (initializer
);
8505 /* Parse a new-placement.
8510 Returns the same representation as for an expression-list. */
8512 static vec
<tree
, va_gc
> *
8513 cp_parser_new_placement (cp_parser
* parser
)
8515 vec
<tree
, va_gc
> *expression_list
;
8517 /* Parse the expression-list. */
8518 expression_list
= (cp_parser_parenthesized_expression_list
8519 (parser
, non_attr
, /*cast_p=*/false,
8520 /*allow_expansion_p=*/true,
8521 /*non_constant_p=*/NULL
));
8523 if (expression_list
&& expression_list
->is_empty ())
8524 error ("expected expression-list or type-id");
8526 return expression_list
;
8529 /* Parse a new-type-id.
8532 type-specifier-seq new-declarator [opt]
8534 Returns the TYPE allocated. If the new-type-id indicates an array
8535 type, *NELTS is set to the number of elements in the last array
8536 bound; the TYPE will not include the last array bound. */
8539 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8541 cp_decl_specifier_seq type_specifier_seq
;
8542 cp_declarator
*new_declarator
;
8543 cp_declarator
*declarator
;
8544 cp_declarator
*outer_declarator
;
8545 const char *saved_message
;
8547 /* The type-specifier sequence must not contain type definitions.
8548 (It cannot contain declarations of new types either, but if they
8549 are not definitions we will catch that because they are not
8551 saved_message
= parser
->type_definition_forbidden_message
;
8552 parser
->type_definition_forbidden_message
8553 = G_("types may not be defined in a new-type-id");
8554 /* Parse the type-specifier-seq. */
8555 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8556 /*is_trailing_return=*/false,
8557 &type_specifier_seq
);
8558 /* Restore the old message. */
8559 parser
->type_definition_forbidden_message
= saved_message
;
8561 if (type_specifier_seq
.type
== error_mark_node
)
8562 return error_mark_node
;
8564 /* Parse the new-declarator. */
8565 new_declarator
= cp_parser_new_declarator_opt (parser
);
8567 /* Determine the number of elements in the last array dimension, if
8570 /* Skip down to the last array dimension. */
8571 declarator
= new_declarator
;
8572 outer_declarator
= NULL
;
8573 while (declarator
&& (declarator
->kind
== cdk_pointer
8574 || declarator
->kind
== cdk_ptrmem
))
8576 outer_declarator
= declarator
;
8577 declarator
= declarator
->declarator
;
8580 && declarator
->kind
== cdk_array
8581 && declarator
->declarator
8582 && declarator
->declarator
->kind
== cdk_array
)
8584 outer_declarator
= declarator
;
8585 declarator
= declarator
->declarator
;
8588 if (declarator
&& declarator
->kind
== cdk_array
)
8590 *nelts
= declarator
->u
.array
.bounds
;
8591 if (*nelts
== error_mark_node
)
8592 *nelts
= integer_one_node
;
8594 if (outer_declarator
)
8595 outer_declarator
->declarator
= declarator
->declarator
;
8597 new_declarator
= NULL
;
8600 return groktypename (&type_specifier_seq
, new_declarator
, false);
8603 /* Parse an (optional) new-declarator.
8606 ptr-operator new-declarator [opt]
8607 direct-new-declarator
8609 Returns the declarator. */
8611 static cp_declarator
*
8612 cp_parser_new_declarator_opt (cp_parser
* parser
)
8614 enum tree_code code
;
8615 tree type
, std_attributes
= NULL_TREE
;
8616 cp_cv_quals cv_quals
;
8618 /* We don't know if there's a ptr-operator next, or not. */
8619 cp_parser_parse_tentatively (parser
);
8620 /* Look for a ptr-operator. */
8621 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8622 /* If that worked, look for more new-declarators. */
8623 if (cp_parser_parse_definitely (parser
))
8625 cp_declarator
*declarator
;
8627 /* Parse another optional declarator. */
8628 declarator
= cp_parser_new_declarator_opt (parser
);
8630 declarator
= cp_parser_make_indirect_declarator
8631 (code
, type
, cv_quals
, declarator
, std_attributes
);
8636 /* If the next token is a `[', there is a direct-new-declarator. */
8637 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8638 return cp_parser_direct_new_declarator (parser
);
8643 /* Parse a direct-new-declarator.
8645 direct-new-declarator:
8647 direct-new-declarator [constant-expression]
8651 static cp_declarator
*
8652 cp_parser_direct_new_declarator (cp_parser
* parser
)
8654 cp_declarator
*declarator
= NULL
;
8661 /* Look for the opening `['. */
8662 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8664 token
= cp_lexer_peek_token (parser
->lexer
);
8665 expression
= cp_parser_expression (parser
);
8666 /* The standard requires that the expression have integral
8667 type. DR 74 adds enumeration types. We believe that the
8668 real intent is that these expressions be handled like the
8669 expression in a `switch' condition, which also allows
8670 classes with a single conversion to integral or
8671 enumeration type. */
8672 if (!processing_template_decl
)
8675 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8680 error_at (token
->location
,
8681 "expression in new-declarator must have integral "
8682 "or enumeration type");
8683 expression
= error_mark_node
;
8687 /* Look for the closing `]'. */
8688 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8690 /* Add this bound to the declarator. */
8691 declarator
= make_array_declarator (declarator
, expression
);
8693 /* If the next token is not a `[', then there are no more
8695 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8702 /* Parse a new-initializer.
8705 ( expression-list [opt] )
8708 Returns a representation of the expression-list. */
8710 static vec
<tree
, va_gc
> *
8711 cp_parser_new_initializer (cp_parser
* parser
)
8713 vec
<tree
, va_gc
> *expression_list
;
8715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8718 bool expr_non_constant_p
;
8719 cp_lexer_set_source_position (parser
->lexer
);
8720 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8721 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8722 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8723 expression_list
= make_tree_vector_single (t
);
8726 expression_list
= (cp_parser_parenthesized_expression_list
8727 (parser
, non_attr
, /*cast_p=*/false,
8728 /*allow_expansion_p=*/true,
8729 /*non_constant_p=*/NULL
));
8731 return expression_list
;
8734 /* Parse a delete-expression.
8737 :: [opt] delete cast-expression
8738 :: [opt] delete [ ] cast-expression
8740 Returns a representation of the expression. */
8743 cp_parser_delete_expression (cp_parser
* parser
)
8745 bool global_scope_p
;
8749 /* Look for the optional `::' operator. */
8751 = (cp_parser_global_scope_opt (parser
,
8752 /*current_scope_valid_p=*/false)
8754 /* Look for the `delete' keyword. */
8755 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8756 /* See if the array syntax is in use. */
8757 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8759 /* Consume the `[' token. */
8760 cp_lexer_consume_token (parser
->lexer
);
8761 /* Look for the `]' token. */
8762 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8763 /* Remember that this is the `[]' construct. */
8769 /* Parse the cast-expression. */
8770 expression
= cp_parser_simple_cast_expression (parser
);
8772 /* A delete-expression may not appear in an integral constant
8774 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8775 return error_mark_node
;
8777 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8778 tf_warning_or_error
);
8781 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8782 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8786 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8788 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8789 switch (token
->type
)
8795 case CPP_CLOSE_SQUARE
:
8796 case CPP_CLOSE_PAREN
:
8797 case CPP_CLOSE_BRACE
:
8798 case CPP_OPEN_BRACE
:
8802 case CPP_DEREF_STAR
:
8810 case CPP_GREATER_EQ
:
8831 case CPP_OPEN_PAREN
:
8832 /* In ((type ()) () the last () isn't a valid cast-expression,
8833 so the whole must be parsed as postfix-expression. */
8834 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8837 case CPP_OPEN_SQUARE
:
8838 /* '[' may start a primary-expression in obj-c++ and in C++11,
8839 as a lambda-expression, eg, '(void)[]{}'. */
8840 if (cxx_dialect
>= cxx11
)
8842 return c_dialect_objc ();
8845 case CPP_MINUS_MINUS
:
8846 /* '++' and '--' may or may not start a cast-expression:
8848 struct T { void operator++(int); };
8849 void f() { (T())++; }
8862 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8863 in the order: const_cast, static_cast, reinterpret_cast.
8865 Don't suggest dynamic_cast.
8867 Return the first legal cast kind found, or NULL otherwise. */
8870 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8874 /* Reuse the parser logic by attempting to build the various kinds of
8875 cast, with "complain" disabled.
8876 Identify the first such cast that is valid. */
8878 /* Don't attempt to run such logic within template processing. */
8879 if (processing_template_decl
)
8882 /* First try const_cast. */
8883 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8884 if (trial
!= error_mark_node
)
8885 return "const_cast";
8887 /* If that fails, try static_cast. */
8888 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8889 if (trial
!= error_mark_node
)
8890 return "static_cast";
8892 /* Finally, try reinterpret_cast. */
8893 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8894 if (trial
!= error_mark_node
)
8895 return "reinterpret_cast";
8897 /* No such cast possible. */
8901 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8902 suggesting how to convert a C-style cast of the form:
8906 to a C++-style cast.
8908 The primary range of RICHLOC is asssumed to be that of the original
8909 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8910 of the parens in the C-style cast. */
8913 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8914 location_t close_paren_loc
, tree orig_expr
,
8917 /* This function is non-trivial, so bail out now if the warning isn't
8918 going to be emitted. */
8919 if (!warn_old_style_cast
)
8922 /* Try to find a legal C++ cast, trying them in order:
8923 const_cast, static_cast, reinterpret_cast. */
8924 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8925 if (!cast_suggestion
)
8928 /* Replace the open paren with "CAST_SUGGESTION<". */
8930 pp_printf (&pp
, "%s<", cast_suggestion
);
8931 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8933 /* Replace the close paren with "> (". */
8934 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8936 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8937 rich_loc
->add_fixit_insert_after (")");
8941 /* Parse a cast-expression.
8945 ( type-id ) cast-expression
8947 ADDRESS_P is true iff the unary-expression is appearing as the
8948 operand of the `&' operator. CAST_P is true if this expression is
8949 the target of a cast.
8951 Returns a representation of the expression. */
8954 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8955 bool decltype_p
, cp_id_kind
* pidk
)
8957 /* If it's a `(', then we might be looking at a cast. */
8958 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8960 tree type
= NULL_TREE
;
8961 cp_expr
expr (NULL_TREE
);
8962 int cast_expression
= 0;
8963 const char *saved_message
;
8965 /* There's no way to know yet whether or not this is a cast.
8966 For example, `(int (3))' is a unary-expression, while `(int)
8967 3' is a cast. So, we resort to parsing tentatively. */
8968 cp_parser_parse_tentatively (parser
);
8969 /* Types may not be defined in a cast. */
8970 saved_message
= parser
->type_definition_forbidden_message
;
8971 parser
->type_definition_forbidden_message
8972 = G_("types may not be defined in casts");
8973 /* Consume the `('. */
8974 matching_parens parens
;
8975 cp_token
*open_paren
= parens
.consume_open (parser
);
8976 location_t open_paren_loc
= open_paren
->location
;
8977 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8979 /* A very tricky bit is that `(struct S) { 3 }' is a
8980 compound-literal (which we permit in C++ as an extension).
8981 But, that construct is not a cast-expression -- it is a
8982 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8983 is legal; if the compound-literal were a cast-expression,
8984 you'd need an extra set of parentheses.) But, if we parse
8985 the type-id, and it happens to be a class-specifier, then we
8986 will commit to the parse at that point, because we cannot
8987 undo the action that is done when creating a new class. So,
8988 then we cannot back up and do a postfix-expression.
8990 Another tricky case is the following (c++/29234):
8992 struct S { void operator () (); };
8999 As a type-id we parse the parenthesized S()() as a function
9000 returning a function, groktypename complains and we cannot
9001 back up in this case either.
9003 Therefore, we scan ahead to the closing `)', and check to see
9004 if the tokens after the `)' can start a cast-expression. Otherwise
9005 we are dealing with an unary-expression, a postfix-expression
9008 Yet another tricky case, in C++11, is the following (c++/54891):
9012 The issue is that usually, besides the case of lambda-expressions,
9013 the parenthesized type-id cannot be followed by '[', and, eg, we
9014 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9015 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9016 we don't commit, we try a cast-expression, then an unary-expression.
9018 Save tokens so that we can put them back. */
9019 cp_lexer_save_tokens (parser
->lexer
);
9021 /* We may be looking at a cast-expression. */
9022 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9023 /*consume_paren=*/true))
9025 = cp_parser_tokens_start_cast_expression (parser
);
9027 /* Roll back the tokens we skipped. */
9028 cp_lexer_rollback_tokens (parser
->lexer
);
9029 /* If we aren't looking at a cast-expression, simulate an error so
9030 that the call to cp_parser_error_occurred below returns true. */
9031 if (!cast_expression
)
9032 cp_parser_simulate_error (parser
);
9035 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9036 parser
->in_type_id_in_expr_p
= true;
9037 /* Look for the type-id. */
9038 type
= cp_parser_type_id (parser
);
9039 /* Look for the closing `)'. */
9040 cp_token
*close_paren
= parens
.require_close (parser
);
9042 close_paren_loc
= close_paren
->location
;
9043 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9046 /* Restore the saved message. */
9047 parser
->type_definition_forbidden_message
= saved_message
;
9049 /* At this point this can only be either a cast or a
9050 parenthesized ctor such as `(T ())' that looks like a cast to
9051 function returning T. */
9052 if (!cp_parser_error_occurred (parser
))
9054 /* Only commit if the cast-expression doesn't start with
9055 '++', '--', or '[' in C++11. */
9056 if (cast_expression
> 0)
9057 cp_parser_commit_to_topmost_tentative_parse (parser
);
9059 expr
= cp_parser_cast_expression (parser
,
9060 /*address_p=*/false,
9062 /*decltype_p=*/false,
9065 if (cp_parser_parse_definitely (parser
))
9067 /* Warn about old-style casts, if so requested. */
9068 if (warn_old_style_cast
9069 && !in_system_header_at (input_location
)
9070 && !VOID_TYPE_P (type
)
9071 && current_lang_name
!= lang_name_c
)
9073 gcc_rich_location
rich_loc (input_location
);
9074 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9076 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9077 "use of old-style cast to %q#T", type
);
9080 /* Only type conversions to integral or enumeration types
9081 can be used in constant-expressions. */
9082 if (!cast_valid_in_integral_constant_expression_p (type
)
9083 && cp_parser_non_integral_constant_expression (parser
,
9085 return error_mark_node
;
9087 /* Perform the cast. */
9091 with start==caret at the open paren, extending to the
9093 location_t cast_loc
= make_location (open_paren_loc
,
9095 expr
.get_finish ());
9096 expr
= build_c_cast (cast_loc
, type
, expr
);
9101 cp_parser_abort_tentative_parse (parser
);
9104 /* If we get here, then it's not a cast, so it must be a
9105 unary-expression. */
9106 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9107 cast_p
, decltype_p
);
9110 /* Parse a binary expression of the general form:
9114 pm-expression .* cast-expression
9115 pm-expression ->* cast-expression
9117 multiplicative-expression:
9119 multiplicative-expression * pm-expression
9120 multiplicative-expression / pm-expression
9121 multiplicative-expression % pm-expression
9123 additive-expression:
9124 multiplicative-expression
9125 additive-expression + multiplicative-expression
9126 additive-expression - multiplicative-expression
9130 shift-expression << additive-expression
9131 shift-expression >> additive-expression
9133 relational-expression:
9135 relational-expression < shift-expression
9136 relational-expression > shift-expression
9137 relational-expression <= shift-expression
9138 relational-expression >= shift-expression
9142 relational-expression:
9143 relational-expression <? shift-expression
9144 relational-expression >? shift-expression
9146 equality-expression:
9147 relational-expression
9148 equality-expression == relational-expression
9149 equality-expression != relational-expression
9153 and-expression & equality-expression
9155 exclusive-or-expression:
9157 exclusive-or-expression ^ and-expression
9159 inclusive-or-expression:
9160 exclusive-or-expression
9161 inclusive-or-expression | exclusive-or-expression
9163 logical-and-expression:
9164 inclusive-or-expression
9165 logical-and-expression && inclusive-or-expression
9167 logical-or-expression:
9168 logical-and-expression
9169 logical-or-expression || logical-and-expression
9171 All these are implemented with a single function like:
9174 simple-cast-expression
9175 binary-expression <token> binary-expression
9177 CAST_P is true if this expression is the target of a cast.
9179 The binops_by_token map is used to get the tree codes for each <token> type.
9180 binary-expressions are associated according to a precedence table. */
9182 #define TOKEN_PRECEDENCE(token) \
9183 (((token->type == CPP_GREATER \
9184 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9185 && !parser->greater_than_is_operator_p) \
9186 ? PREC_NOT_OPERATOR \
9187 : binops_by_token[token->type].prec)
9190 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9191 bool no_toplevel_fold_p
,
9193 enum cp_parser_prec prec
,
9196 cp_parser_expression_stack stack
;
9197 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9198 cp_parser_expression_stack_entry current
;
9201 enum tree_code rhs_type
;
9202 enum cp_parser_prec new_prec
, lookahead_prec
;
9205 /* Parse the first expression. */
9206 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9207 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9208 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9209 cast_p
, decltype_p
, pidk
);
9210 current
.prec
= prec
;
9212 if (cp_parser_error_occurred (parser
))
9213 return error_mark_node
;
9217 /* Get an operator token. */
9218 token
= cp_lexer_peek_token (parser
->lexer
);
9220 if (warn_cxx11_compat
9221 && token
->type
== CPP_RSHIFT
9222 && !parser
->greater_than_is_operator_p
)
9224 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9225 "%<>>%> operator is treated"
9226 " as two right angle brackets in C++11"))
9227 inform (token
->location
,
9228 "suggest parentheses around %<>>%> expression");
9231 new_prec
= TOKEN_PRECEDENCE (token
);
9232 if (new_prec
!= PREC_NOT_OPERATOR
9233 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9234 /* This is a fold-expression; handle it later. */
9235 new_prec
= PREC_NOT_OPERATOR
;
9237 /* Popping an entry off the stack means we completed a subexpression:
9238 - either we found a token which is not an operator (`>' where it is not
9239 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9240 will happen repeatedly;
9241 - or, we found an operator which has lower priority. This is the case
9242 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9244 if (new_prec
<= current
.prec
)
9253 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9254 current
.loc
= token
->location
;
9256 /* We used the operator token. */
9257 cp_lexer_consume_token (parser
->lexer
);
9259 /* For "false && x" or "true || x", x will never be executed;
9260 disable warnings while evaluating it. */
9261 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9262 c_inhibit_evaluation_warnings
+=
9263 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9264 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9265 c_inhibit_evaluation_warnings
+=
9266 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9268 /* Extract another operand. It may be the RHS of this expression
9269 or the LHS of a new, higher priority expression. */
9270 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9271 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9272 rhs
= cp_parser_simple_cast_expression (parser
);
9274 /* Get another operator token. Look up its precedence to avoid
9275 building a useless (immediately popped) stack entry for common
9276 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9277 token
= cp_lexer_peek_token (parser
->lexer
);
9278 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9279 if (lookahead_prec
!= PREC_NOT_OPERATOR
9280 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9281 lookahead_prec
= PREC_NOT_OPERATOR
;
9282 if (lookahead_prec
> new_prec
)
9284 /* ... and prepare to parse the RHS of the new, higher priority
9285 expression. Since precedence levels on the stack are
9286 monotonically increasing, we do not have to care about
9291 current
.lhs_type
= rhs_type
;
9292 current
.prec
= new_prec
;
9293 new_prec
= lookahead_prec
;
9297 lookahead_prec
= new_prec
;
9298 /* If the stack is not empty, we have parsed into LHS the right side
9299 (`4' in the example above) of an expression we had suspended.
9300 We can use the information on the stack to recover the LHS (`3')
9301 from the stack together with the tree code (`MULT_EXPR'), and
9302 the precedence of the higher level subexpression
9303 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9304 which will be used to actually build the additive expression. */
9306 rhs_type
= current
.lhs_type
;
9311 /* Undo the disabling of warnings done above. */
9312 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9313 c_inhibit_evaluation_warnings
-=
9314 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9315 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9316 c_inhibit_evaluation_warnings
-=
9317 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9319 if (warn_logical_not_paren
9320 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9321 && current
.lhs_type
== TRUTH_NOT_EXPR
9322 /* Avoid warning for !!x == y. */
9323 && (TREE_CODE (current
.lhs
) != NE_EXPR
9324 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9325 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9326 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9327 /* Avoid warning for !b == y where b is boolean. */
9328 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9329 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9331 /* Avoid warning for !!b == y where b is boolean. */
9332 && (!DECL_P (current
.lhs
)
9333 || TREE_TYPE (current
.lhs
) == NULL_TREE
9334 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9335 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9336 current
.lhs
, maybe_constant_value (rhs
));
9340 location_t combined_loc
= make_location (current
.loc
,
9341 current
.lhs
.get_start (),
9344 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9345 ERROR_MARK for everything that is not a binary expression.
9346 This makes warn_about_parentheses miss some warnings that
9347 involve unary operators. For unary expressions we should
9348 pass the correct tree_code unless the unary expression was
9349 surrounded by parentheses.
9351 if (no_toplevel_fold_p
9352 && lookahead_prec
<= current
.prec
9355 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9356 current
.lhs
= error_mark_node
;
9360 = build_min (current
.tree_type
,
9361 TREE_CODE_CLASS (current
.tree_type
)
9363 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9364 current
.lhs
.get_value (), rhs
.get_value ());
9365 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9370 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9371 current
.lhs
, current
.lhs_type
,
9372 rhs
, rhs_type
, &overload
,
9373 complain_flags (decltype_p
));
9374 /* TODO: build_x_binary_op doesn't always honor the location. */
9375 current
.lhs
.set_location (combined_loc
);
9377 current
.lhs_type
= current
.tree_type
;
9379 /* If the binary operator required the use of an overloaded operator,
9380 then this expression cannot be an integral constant-expression.
9381 An overloaded operator can be used even if both operands are
9382 otherwise permissible in an integral constant-expression if at
9383 least one of the operands is of enumeration type. */
9386 && cp_parser_non_integral_constant_expression (parser
,
9388 return error_mark_node
;
9395 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9396 bool no_toplevel_fold_p
,
9397 enum cp_parser_prec prec
,
9400 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9401 /*decltype*/false, prec
, pidk
);
9404 /* Parse the `? expression : assignment-expression' part of a
9405 conditional-expression. The LOGICAL_OR_EXPR is the
9406 logical-or-expression that started the conditional-expression.
9407 Returns a representation of the entire conditional-expression.
9409 This routine is used by cp_parser_assignment_expression.
9411 ? expression : assignment-expression
9415 ? : assignment-expression */
9418 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9420 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9421 cp_expr assignment_expr
;
9422 struct cp_token
*token
;
9423 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9425 /* Consume the `?' token. */
9426 cp_lexer_consume_token (parser
->lexer
);
9427 token
= cp_lexer_peek_token (parser
->lexer
);
9428 if (cp_parser_allow_gnu_extensions_p (parser
)
9429 && token
->type
== CPP_COLON
)
9431 pedwarn (token
->location
, OPT_Wpedantic
,
9432 "ISO C++ does not allow ?: with omitted middle operand");
9433 /* Implicit true clause. */
9435 c_inhibit_evaluation_warnings
+=
9436 folded_logical_or_expr
== truthvalue_true_node
;
9437 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9441 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9442 parser
->colon_corrects_to_scope_p
= false;
9443 /* Parse the expression. */
9444 c_inhibit_evaluation_warnings
+=
9445 folded_logical_or_expr
== truthvalue_false_node
;
9446 expr
= cp_parser_expression (parser
);
9447 c_inhibit_evaluation_warnings
+=
9448 ((folded_logical_or_expr
== truthvalue_true_node
)
9449 - (folded_logical_or_expr
== truthvalue_false_node
));
9450 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9453 /* The next token should be a `:'. */
9454 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9455 /* Parse the assignment-expression. */
9456 assignment_expr
= cp_parser_assignment_expression (parser
);
9457 c_inhibit_evaluation_warnings
-=
9458 folded_logical_or_expr
== truthvalue_true_node
;
9461 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9462 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9463 with the caret at the "?", ranging from the start of
9464 the logical_or_expr to the end of the assignment_expr. */
9465 loc
= make_location (loc
,
9466 logical_or_expr
.get_start (),
9467 assignment_expr
.get_finish ());
9469 /* Build the conditional-expression. */
9470 return build_x_conditional_expr (loc
, logical_or_expr
,
9473 tf_warning_or_error
);
9476 /* Parse an assignment-expression.
9478 assignment-expression:
9479 conditional-expression
9480 logical-or-expression assignment-operator assignment_expression
9483 CAST_P is true if this expression is the target of a cast.
9484 DECLTYPE_P is true if this expression is the operand of decltype.
9486 Returns a representation for the expression. */
9489 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9490 bool cast_p
, bool decltype_p
)
9494 /* If the next token is the `throw' keyword, then we're looking at
9495 a throw-expression. */
9496 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9497 expr
= cp_parser_throw_expression (parser
);
9498 /* Otherwise, it must be that we are looking at a
9499 logical-or-expression. */
9502 /* Parse the binary expressions (logical-or-expression). */
9503 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9505 PREC_NOT_OPERATOR
, pidk
);
9506 /* If the next token is a `?' then we're actually looking at a
9507 conditional-expression. */
9508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9509 return cp_parser_question_colon_clause (parser
, expr
);
9512 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9514 /* If it's an assignment-operator, we're using the second
9516 enum tree_code assignment_operator
9517 = cp_parser_assignment_operator_opt (parser
);
9518 if (assignment_operator
!= ERROR_MARK
)
9520 bool non_constant_p
;
9522 /* Parse the right-hand side of the assignment. */
9523 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9526 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9527 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9529 /* An assignment may not appear in a
9530 constant-expression. */
9531 if (cp_parser_non_integral_constant_expression (parser
,
9533 return error_mark_node
;
9534 /* Build the assignment expression. Its default
9538 is the location of the '=' token as the
9539 caret, ranging from the start of the lhs to the
9541 loc
= make_location (loc
,
9544 expr
= build_x_modify_expr (loc
, expr
,
9545 assignment_operator
,
9547 complain_flags (decltype_p
));
9548 /* TODO: build_x_modify_expr doesn't honor the location,
9549 so we must set it here. */
9550 expr
.set_location (loc
);
9558 /* Parse an (optional) assignment-operator.
9560 assignment-operator: one of
9561 = *= /= %= += -= >>= <<= &= ^= |=
9565 assignment-operator: one of
9568 If the next token is an assignment operator, the corresponding tree
9569 code is returned, and the token is consumed. For example, for
9570 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9571 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9572 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9573 operator, ERROR_MARK is returned. */
9575 static enum tree_code
9576 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9581 /* Peek at the next token. */
9582 token
= cp_lexer_peek_token (parser
->lexer
);
9584 switch (token
->type
)
9595 op
= TRUNC_DIV_EXPR
;
9599 op
= TRUNC_MOD_EXPR
;
9631 /* Nothing else is an assignment operator. */
9635 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9636 if (op
!= ERROR_MARK
9637 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9640 /* If it was an assignment operator, consume it. */
9641 if (op
!= ERROR_MARK
)
9642 cp_lexer_consume_token (parser
->lexer
);
9647 /* Parse an expression.
9650 assignment-expression
9651 expression , assignment-expression
9653 CAST_P is true if this expression is the target of a cast.
9654 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9655 except possibly parenthesized or on the RHS of a comma (N3276).
9657 Returns a representation of the expression. */
9660 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9661 bool cast_p
, bool decltype_p
)
9663 cp_expr expression
= NULL_TREE
;
9664 location_t loc
= UNKNOWN_LOCATION
;
9668 cp_expr assignment_expression
;
9670 /* Parse the next assignment-expression. */
9671 assignment_expression
9672 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9674 /* We don't create a temporary for a call that is the immediate operand
9675 of decltype or on the RHS of a comma. But when we see a comma, we
9676 need to create a temporary for a call on the LHS. */
9677 if (decltype_p
&& !processing_template_decl
9678 && TREE_CODE (assignment_expression
) == CALL_EXPR
9679 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9680 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9681 assignment_expression
9682 = build_cplus_new (TREE_TYPE (assignment_expression
),
9683 assignment_expression
, tf_warning_or_error
);
9685 /* If this is the first assignment-expression, we can just
9688 expression
= assignment_expression
;
9691 /* Create a location with caret at the comma, ranging
9692 from the start of the LHS to the end of the RHS. */
9693 loc
= make_location (loc
,
9694 expression
.get_start (),
9695 assignment_expression
.get_finish ());
9696 expression
= build_x_compound_expr (loc
, expression
,
9697 assignment_expression
,
9698 complain_flags (decltype_p
));
9699 expression
.set_location (loc
);
9701 /* If the next token is not a comma, or we're in a fold-expression, then
9702 we are done with the expression. */
9703 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9704 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9706 /* Consume the `,'. */
9707 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9708 cp_lexer_consume_token (parser
->lexer
);
9709 /* A comma operator cannot appear in a constant-expression. */
9710 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9711 expression
= error_mark_node
;
9717 /* Parse a constant-expression.
9719 constant-expression:
9720 conditional-expression
9722 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9723 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9724 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9725 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9726 only parse a conditional-expression, otherwise parse an
9727 assignment-expression. See below for rationale. */
9730 cp_parser_constant_expression (cp_parser
* parser
,
9731 bool allow_non_constant_p
,
9732 bool *non_constant_p
,
9735 bool saved_integral_constant_expression_p
;
9736 bool saved_allow_non_integral_constant_expression_p
;
9737 bool saved_non_integral_constant_expression_p
;
9740 /* It might seem that we could simply parse the
9741 conditional-expression, and then check to see if it were
9742 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9743 one that the compiler can figure out is constant, possibly after
9744 doing some simplifications or optimizations. The standard has a
9745 precise definition of constant-expression, and we must honor
9746 that, even though it is somewhat more restrictive.
9752 is not a legal declaration, because `(2, 3)' is not a
9753 constant-expression. The `,' operator is forbidden in a
9754 constant-expression. However, GCC's constant-folding machinery
9755 will fold this operation to an INTEGER_CST for `3'. */
9757 /* Save the old settings. */
9758 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9759 saved_allow_non_integral_constant_expression_p
9760 = parser
->allow_non_integral_constant_expression_p
;
9761 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9762 /* We are now parsing a constant-expression. */
9763 parser
->integral_constant_expression_p
= true;
9764 parser
->allow_non_integral_constant_expression_p
9765 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9766 parser
->non_integral_constant_expression_p
= false;
9767 /* Although the grammar says "conditional-expression", when not STRICT_P,
9768 we parse an "assignment-expression", which also permits
9769 "throw-expression" and the use of assignment operators. In the case
9770 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9771 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9772 actually essential that we look for an assignment-expression.
9773 For example, cp_parser_initializer_clauses uses this function to
9774 determine whether a particular assignment-expression is in fact
9778 /* Parse the binary expressions (logical-or-expression). */
9779 expression
= cp_parser_binary_expression (parser
, false, false, false,
9780 PREC_NOT_OPERATOR
, NULL
);
9781 /* If the next token is a `?' then we're actually looking at
9782 a conditional-expression; otherwise we're done. */
9783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9784 expression
= cp_parser_question_colon_clause (parser
, expression
);
9787 expression
= cp_parser_assignment_expression (parser
);
9788 /* Restore the old settings. */
9789 parser
->integral_constant_expression_p
9790 = saved_integral_constant_expression_p
;
9791 parser
->allow_non_integral_constant_expression_p
9792 = saved_allow_non_integral_constant_expression_p
;
9793 if (cxx_dialect
>= cxx11
)
9795 /* Require an rvalue constant expression here; that's what our
9796 callers expect. Reference constant expressions are handled
9797 separately in e.g. cp_parser_template_argument. */
9798 tree decay
= expression
;
9799 if (TREE_TYPE (expression
)
9800 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9801 decay
= build_address (expression
);
9802 bool is_const
= potential_rvalue_constant_expression (decay
);
9803 parser
->non_integral_constant_expression_p
= !is_const
;
9804 if (!is_const
&& !allow_non_constant_p
)
9805 require_potential_rvalue_constant_expression (decay
);
9807 if (allow_non_constant_p
)
9808 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9809 parser
->non_integral_constant_expression_p
9810 = saved_non_integral_constant_expression_p
;
9815 /* Parse __builtin_offsetof.
9817 offsetof-expression:
9818 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9820 offsetof-member-designator:
9822 | offsetof-member-designator "." id-expression
9823 | offsetof-member-designator "[" expression "]"
9824 | offsetof-member-designator "->" id-expression */
9827 cp_parser_builtin_offsetof (cp_parser
*parser
)
9829 int save_ice_p
, save_non_ice_p
;
9834 location_t finish_loc
;
9836 /* We're about to accept non-integral-constant things, but will
9837 definitely yield an integral constant expression. Save and
9838 restore these values around our local parsing. */
9839 save_ice_p
= parser
->integral_constant_expression_p
;
9840 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9842 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9844 /* Consume the "__builtin_offsetof" token. */
9845 cp_lexer_consume_token (parser
->lexer
);
9846 /* Consume the opening `('. */
9847 matching_parens parens
;
9848 parens
.require_open (parser
);
9849 /* Parse the type-id. */
9850 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9852 const char *saved_message
= parser
->type_definition_forbidden_message
;
9853 parser
->type_definition_forbidden_message
9854 = G_("types may not be defined within __builtin_offsetof");
9855 type
= cp_parser_type_id (parser
);
9856 parser
->type_definition_forbidden_message
= saved_message
;
9858 /* Look for the `,'. */
9859 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9860 token
= cp_lexer_peek_token (parser
->lexer
);
9862 /* Build the (type *)null that begins the traditional offsetof macro. */
9864 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9865 tf_warning_or_error
);
9867 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9868 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9869 true, &dummy
, token
->location
);
9872 token
= cp_lexer_peek_token (parser
->lexer
);
9873 switch (token
->type
)
9875 case CPP_OPEN_SQUARE
:
9876 /* offsetof-member-designator "[" expression "]" */
9877 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9882 /* offsetof-member-designator "->" identifier */
9883 expr
= grok_array_decl (token
->location
, expr
,
9884 integer_zero_node
, false);
9888 /* offsetof-member-designator "." identifier */
9889 cp_lexer_consume_token (parser
->lexer
);
9890 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9895 case CPP_CLOSE_PAREN
:
9896 /* Consume the ")" token. */
9897 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9898 cp_lexer_consume_token (parser
->lexer
);
9902 /* Error. We know the following require will fail, but
9903 that gives the proper error message. */
9904 parens
.require_close (parser
);
9905 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9906 expr
= error_mark_node
;
9912 /* Make a location of the form:
9913 __builtin_offsetof (struct s, f)
9914 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9915 with caret at the type-id, ranging from the start of the
9916 "_builtin_offsetof" token to the close paren. */
9917 loc
= make_location (loc
, start_loc
, finish_loc
);
9918 /* The result will be an INTEGER_CST, so we need to explicitly
9919 preserve the location. */
9920 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9923 parser
->integral_constant_expression_p
= save_ice_p
;
9924 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9926 expr
= expr
.maybe_add_location_wrapper ();
9930 /* Parse a trait expression.
9932 Returns a representation of the expression, the underlying type
9933 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9936 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9939 tree type1
, type2
= NULL_TREE
;
9940 bool binary
= false;
9941 bool variadic
= false;
9945 case RID_HAS_NOTHROW_ASSIGN
:
9946 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9948 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9949 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9951 case RID_HAS_NOTHROW_COPY
:
9952 kind
= CPTK_HAS_NOTHROW_COPY
;
9954 case RID_HAS_TRIVIAL_ASSIGN
:
9955 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9957 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9958 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9960 case RID_HAS_TRIVIAL_COPY
:
9961 kind
= CPTK_HAS_TRIVIAL_COPY
;
9963 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9964 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9966 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9967 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9969 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9970 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9972 case RID_IS_ABSTRACT
:
9973 kind
= CPTK_IS_ABSTRACT
;
9975 case RID_IS_AGGREGATE
:
9976 kind
= CPTK_IS_AGGREGATE
;
9978 case RID_IS_BASE_OF
:
9979 kind
= CPTK_IS_BASE_OF
;
9983 kind
= CPTK_IS_CLASS
;
9986 kind
= CPTK_IS_EMPTY
;
9989 kind
= CPTK_IS_ENUM
;
9992 kind
= CPTK_IS_FINAL
;
9994 case RID_IS_LITERAL_TYPE
:
9995 kind
= CPTK_IS_LITERAL_TYPE
;
10000 case RID_IS_POLYMORPHIC
:
10001 kind
= CPTK_IS_POLYMORPHIC
;
10003 case RID_IS_SAME_AS
:
10004 kind
= CPTK_IS_SAME_AS
;
10007 case RID_IS_STD_LAYOUT
:
10008 kind
= CPTK_IS_STD_LAYOUT
;
10010 case RID_IS_TRIVIAL
:
10011 kind
= CPTK_IS_TRIVIAL
;
10013 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10014 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10017 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10018 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10021 case RID_IS_TRIVIALLY_COPYABLE
:
10022 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10025 kind
= CPTK_IS_UNION
;
10027 case RID_UNDERLYING_TYPE
:
10028 kind
= CPTK_UNDERLYING_TYPE
;
10033 case RID_DIRECT_BASES
:
10034 kind
= CPTK_DIRECT_BASES
;
10036 case RID_IS_ASSIGNABLE
:
10037 kind
= CPTK_IS_ASSIGNABLE
;
10040 case RID_IS_CONSTRUCTIBLE
:
10041 kind
= CPTK_IS_CONSTRUCTIBLE
;
10045 gcc_unreachable ();
10048 /* Get location of initial token. */
10049 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10051 /* Consume the token. */
10052 cp_lexer_consume_token (parser
->lexer
);
10054 matching_parens parens
;
10055 parens
.require_open (parser
);
10058 type_id_in_expr_sentinel
s (parser
);
10059 type1
= cp_parser_type_id (parser
);
10062 if (type1
== error_mark_node
)
10063 return error_mark_node
;
10067 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10070 type_id_in_expr_sentinel
s (parser
);
10071 type2
= cp_parser_type_id (parser
);
10074 if (type2
== error_mark_node
)
10075 return error_mark_node
;
10079 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10081 cp_lexer_consume_token (parser
->lexer
);
10082 tree elt
= cp_parser_type_id (parser
);
10083 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10085 cp_lexer_consume_token (parser
->lexer
);
10086 elt
= make_pack_expansion (elt
);
10088 if (elt
== error_mark_node
)
10089 return error_mark_node
;
10090 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10094 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10095 parens
.require_close (parser
);
10097 /* Construct a location of the form:
10098 __is_trivially_copyable(_Tp)
10099 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10100 with start == caret, finishing at the close-paren. */
10101 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10103 /* Complete the trait expression, which may mean either processing
10104 the trait expr now or saving it for template instantiation. */
10107 case CPTK_UNDERLYING_TYPE
:
10108 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10110 return cp_expr (finish_bases (type1
, false), trait_loc
);
10111 case CPTK_DIRECT_BASES
:
10112 return cp_expr (finish_bases (type1
, true), trait_loc
);
10114 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10118 /* Parse a lambda expression.
10121 lambda-introducer lambda-declarator [opt] compound-statement
10123 Returns a representation of the expression. */
10126 cp_parser_lambda_expression (cp_parser
* parser
)
10128 tree lambda_expr
= build_lambda_expr ();
10131 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10132 cp_token_position start
= 0;
10134 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10136 if (cp_unevaluated_operand
)
10138 if (!token
->error_reported
)
10140 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10141 "lambda-expression in unevaluated context");
10142 token
->error_reported
= true;
10146 else if (parser
->in_template_argument_list_p
)
10148 if (!token
->error_reported
)
10150 error_at (token
->location
, "lambda-expression in template-argument");
10151 token
->error_reported
= true;
10156 /* We may be in the middle of deferred access check. Disable
10158 push_deferring_access_checks (dk_no_deferred
);
10160 cp_parser_lambda_introducer (parser
, lambda_expr
);
10162 type
= begin_lambda_type (lambda_expr
);
10163 if (type
== error_mark_node
)
10164 return error_mark_node
;
10166 record_lambda_scope (lambda_expr
);
10168 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10169 determine_visibility (TYPE_NAME (type
));
10171 /* Now that we've started the type, add the capture fields for any
10172 explicit captures. */
10173 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10176 /* Inside the class, surrounding template-parameter-lists do not apply. */
10177 unsigned int saved_num_template_parameter_lists
10178 = parser
->num_template_parameter_lists
;
10179 unsigned char in_statement
= parser
->in_statement
;
10180 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10181 bool fully_implicit_function_template_p
10182 = parser
->fully_implicit_function_template_p
;
10183 tree implicit_template_parms
= parser
->implicit_template_parms
;
10184 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10185 bool auto_is_implicit_function_template_parm_p
10186 = parser
->auto_is_implicit_function_template_parm_p
;
10188 parser
->num_template_parameter_lists
= 0;
10189 parser
->in_statement
= 0;
10190 parser
->in_switch_statement_p
= false;
10191 parser
->fully_implicit_function_template_p
= false;
10192 parser
->implicit_template_parms
= 0;
10193 parser
->implicit_template_scope
= 0;
10194 parser
->auto_is_implicit_function_template_parm_p
= false;
10196 /* By virtue of defining a local class, a lambda expression has access to
10197 the private variables of enclosing classes. */
10199 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10201 if (ok
&& cp_parser_error_occurred (parser
))
10206 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10207 && cp_parser_start_tentative_firewall (parser
))
10209 cp_parser_lambda_body (parser
, lambda_expr
);
10211 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10213 if (cp_parser_skip_to_closing_brace (parser
))
10214 cp_lexer_consume_token (parser
->lexer
);
10217 /* The capture list was built up in reverse order; fix that now. */
10218 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10219 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10222 maybe_add_lambda_conv_op (type
);
10224 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10226 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10227 parser
->in_statement
= in_statement
;
10228 parser
->in_switch_statement_p
= in_switch_statement_p
;
10229 parser
->fully_implicit_function_template_p
10230 = fully_implicit_function_template_p
;
10231 parser
->implicit_template_parms
= implicit_template_parms
;
10232 parser
->implicit_template_scope
= implicit_template_scope
;
10233 parser
->auto_is_implicit_function_template_parm_p
10234 = auto_is_implicit_function_template_parm_p
;
10237 /* This field is only used during parsing of the lambda. */
10238 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10240 /* This lambda shouldn't have any proxies left at this point. */
10241 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10242 /* And now that we're done, push proxies for an enclosing lambda. */
10243 insert_pending_capture_proxies ();
10245 /* Update the lambda expression to a range. */
10246 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10247 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10249 end_tok
->location
);
10252 lambda_expr
= build_lambda_object (lambda_expr
);
10254 lambda_expr
= error_mark_node
;
10256 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10258 pop_deferring_access_checks ();
10260 return lambda_expr
;
10263 /* Parse the beginning of a lambda expression.
10266 [ lambda-capture [opt] ]
10268 LAMBDA_EXPR is the current representation of the lambda expression. */
10271 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10273 /* Need commas after the first capture. */
10276 /* Eat the leading `['. */
10277 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10279 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10280 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10281 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10282 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10283 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10284 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10286 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10288 cp_lexer_consume_token (parser
->lexer
);
10291 if (!(at_function_scope_p () || parsing_nsdmi ()))
10292 error ("non-local lambda expression cannot have a capture-default");
10295 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10297 cp_token
* capture_token
;
10299 tree capture_init_expr
;
10300 cp_id_kind idk
= CP_ID_KIND_NONE
;
10301 bool explicit_init_p
= false;
10303 enum capture_kind_type
10308 enum capture_kind_type capture_kind
= BY_COPY
;
10310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10312 error ("expected end of capture-list");
10319 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10321 /* Possibly capture `this'. */
10322 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10324 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10325 if (cxx_dialect
< cxx2a
10326 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10327 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10328 "with by-copy capture default");
10329 cp_lexer_consume_token (parser
->lexer
);
10330 add_capture (lambda_expr
,
10331 /*id=*/this_identifier
,
10332 /*initializer=*/finish_this_expr (),
10333 /*by_reference_p=*/true,
10338 /* Possibly capture `*this'. */
10339 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10340 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10342 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10343 if (cxx_dialect
< cxx17
)
10344 pedwarn (loc
, 0, "%<*this%> capture only available with "
10345 "-std=c++17 or -std=gnu++17");
10346 cp_lexer_consume_token (parser
->lexer
);
10347 cp_lexer_consume_token (parser
->lexer
);
10348 add_capture (lambda_expr
,
10349 /*id=*/this_identifier
,
10350 /*initializer=*/finish_this_expr (),
10351 /*by_reference_p=*/false,
10356 /* Remember whether we want to capture as a reference or not. */
10357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10359 capture_kind
= BY_REFERENCE
;
10360 cp_lexer_consume_token (parser
->lexer
);
10363 /* Get the identifier. */
10364 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10365 capture_id
= cp_parser_identifier (parser
);
10367 if (capture_id
== error_mark_node
)
10368 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10369 delimiters, but I modified this to stop on unnested ']' as well. It
10370 was already changed to stop on unnested '}', so the
10371 "closing_parenthesis" name is no more misleading with my change. */
10373 cp_parser_skip_to_closing_parenthesis (parser
,
10374 /*recovering=*/true,
10376 /*consume_paren=*/true);
10380 /* Find the initializer for this capture. */
10381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10382 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10383 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10385 bool direct
, non_constant
;
10386 /* An explicit initializer exists. */
10387 if (cxx_dialect
< cxx14
)
10388 pedwarn (input_location
, 0,
10389 "lambda capture initializers "
10390 "only available with -std=c++14 or -std=gnu++14");
10391 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10392 &non_constant
, true);
10393 explicit_init_p
= true;
10394 if (capture_init_expr
== NULL_TREE
)
10396 error ("empty initializer for lambda init-capture");
10397 capture_init_expr
= error_mark_node
;
10402 const char* error_msg
;
10404 /* Turn the identifier into an id-expression. */
10406 = cp_parser_lookup_name_simple (parser
, capture_id
,
10407 capture_token
->location
);
10409 if (capture_init_expr
== error_mark_node
)
10411 unqualified_name_lookup_error (capture_id
);
10414 else if (!VAR_P (capture_init_expr
)
10415 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10417 error_at (capture_token
->location
,
10418 "capture of non-variable %qE",
10419 capture_init_expr
);
10420 if (DECL_P (capture_init_expr
))
10421 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10422 "%q#D declared here", capture_init_expr
);
10425 if (VAR_P (capture_init_expr
)
10426 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10428 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10429 "%qD with non-automatic storage duration",
10430 capture_init_expr
))
10431 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10432 "%q#D declared here", capture_init_expr
);
10437 = finish_id_expression
10442 /*integral_constant_expression_p=*/false,
10443 /*allow_non_integral_constant_expression_p=*/false,
10444 /*non_integral_constant_expression_p=*/NULL
,
10445 /*template_p=*/false,
10447 /*address_p=*/false,
10448 /*template_arg_p=*/false,
10450 capture_token
->location
);
10452 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10454 cp_lexer_consume_token (parser
->lexer
);
10455 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10459 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10460 && !explicit_init_p
)
10462 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10463 && capture_kind
== BY_COPY
)
10464 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10465 "of %qD redundant with by-copy capture default",
10467 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10468 && capture_kind
== BY_REFERENCE
)
10469 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10470 "capture of %qD redundant with by-reference capture "
10471 "default", capture_id
);
10474 add_capture (lambda_expr
,
10477 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10480 /* If there is any qualification still in effect, clear it
10481 now; we will be starting fresh with the next capture. */
10482 parser
->scope
= NULL_TREE
;
10483 parser
->qualifying_scope
= NULL_TREE
;
10484 parser
->object_scope
= NULL_TREE
;
10487 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10490 /* Parse the (optional) middle of a lambda expression.
10493 < template-parameter-list [opt] >
10494 ( parameter-declaration-clause [opt] )
10495 attribute-specifier [opt]
10496 decl-specifier-seq [opt]
10497 exception-specification [opt]
10498 lambda-return-type-clause [opt]
10500 LAMBDA_EXPR is the current representation of the lambda expression. */
10503 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10505 /* 5.1.1.4 of the standard says:
10506 If a lambda-expression does not include a lambda-declarator, it is as if
10507 the lambda-declarator were ().
10508 This means an empty parameter list, no attributes, and no exception
10510 tree param_list
= void_list_node
;
10511 tree attributes
= NULL_TREE
;
10512 tree exception_spec
= NULL_TREE
;
10513 tree template_param_list
= NULL_TREE
;
10514 tree tx_qual
= NULL_TREE
;
10515 tree return_type
= NULL_TREE
;
10516 cp_decl_specifier_seq lambda_specs
;
10517 clear_decl_specs (&lambda_specs
);
10519 /* The template-parameter-list is optional, but must begin with
10520 an opening angle if present. */
10521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10523 if (cxx_dialect
< cxx14
)
10524 pedwarn (parser
->lexer
->next_token
->location
, 0,
10525 "lambda templates are only available with "
10526 "-std=c++14 or -std=gnu++14");
10527 else if (cxx_dialect
< cxx2a
)
10528 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10529 "lambda templates are only available with "
10530 "-std=c++2a or -std=gnu++2a");
10532 cp_lexer_consume_token (parser
->lexer
);
10534 template_param_list
= cp_parser_template_parameter_list (parser
);
10536 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10538 /* We just processed one more parameter list. */
10539 ++parser
->num_template_parameter_lists
;
10542 /* The parameter-declaration-clause is optional (unless
10543 template-parameter-list was given), but must begin with an
10544 opening parenthesis if present. */
10545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10547 matching_parens parens
;
10548 parens
.consume_open (parser
);
10550 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10552 /* Parse parameters. */
10553 param_list
= cp_parser_parameter_declaration_clause (parser
);
10555 /* Default arguments shall not be specified in the
10556 parameter-declaration-clause of a lambda-declarator. */
10557 if (cxx_dialect
< cxx14
)
10558 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10559 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10560 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10561 "default argument specified for lambda parameter");
10563 parens
.require_close (parser
);
10565 attributes
= cp_parser_attributes_opt (parser
);
10567 /* In the decl-specifier-seq of the lambda-declarator, each
10568 decl-specifier shall either be mutable or constexpr. */
10569 int declares_class_or_enum
;
10570 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10571 cp_parser_decl_specifier_seq (parser
,
10572 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10573 &lambda_specs
, &declares_class_or_enum
);
10574 if (lambda_specs
.storage_class
== sc_mutable
)
10576 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10577 if (lambda_specs
.conflicting_specifiers_p
)
10578 error_at (lambda_specs
.locations
[ds_storage_class
],
10579 "duplicate %<mutable%>");
10582 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10584 /* Parse optional exception specification. */
10585 exception_spec
= cp_parser_exception_specification_opt (parser
);
10587 /* Parse optional trailing return type. */
10588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10590 cp_lexer_consume_token (parser
->lexer
);
10591 return_type
= cp_parser_trailing_type_id (parser
);
10594 /* The function parameters must be in scope all the way until after the
10595 trailing-return-type in case of decltype. */
10596 pop_bindings_and_leave_scope ();
10598 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10599 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10601 /* Create the function call operator.
10603 Messing with declarators like this is no uglier than building up the
10604 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10607 cp_decl_specifier_seq return_type_specs
;
10608 cp_declarator
* declarator
;
10613 clear_decl_specs (&return_type_specs
);
10614 return_type_specs
.type
= make_auto ();
10616 if (lambda_specs
.locations
[ds_constexpr
])
10618 if (cxx_dialect
>= cxx17
)
10619 return_type_specs
.locations
[ds_constexpr
]
10620 = lambda_specs
.locations
[ds_constexpr
];
10622 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10623 "lambda only available with -std=c++17 or -std=gnu++17");
10626 p
= obstack_alloc (&declarator_obstack
, 0);
10628 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10630 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10631 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10632 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10633 VIRT_SPEC_UNSPECIFIED
,
10637 /*late_return_type=*/NULL_TREE
,
10638 /*requires_clause*/NULL_TREE
);
10639 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10641 declarator
->u
.function
.late_return_type
= return_type
;
10643 fco
= grokmethod (&return_type_specs
,
10646 if (fco
!= error_mark_node
)
10648 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10649 DECL_ARTIFICIAL (fco
) = 1;
10650 /* Give the object parameter a different name. */
10651 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
10653 if (template_param_list
)
10655 fco
= finish_member_template_decl (fco
);
10656 finish_template_decl (template_param_list
);
10657 --parser
->num_template_parameter_lists
;
10659 else if (parser
->fully_implicit_function_template_p
)
10660 fco
= finish_fully_implicit_template (parser
, fco
);
10662 finish_member_declaration (fco
);
10664 obstack_free (&declarator_obstack
, p
);
10666 return (fco
!= error_mark_node
);
10670 /* Parse the body of a lambda expression, which is simply
10674 but which requires special handling.
10675 LAMBDA_EXPR is the current representation of the lambda expression. */
10678 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10680 bool nested
= (current_function_decl
!= NULL_TREE
);
10681 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10682 bool in_function_body
= parser
->in_function_body
;
10685 push_function_context ();
10687 /* Still increment function_depth so that we don't GC in the
10688 middle of an expression. */
10691 vec
<tree
> omp_privatization_save
;
10692 save_omp_privatization_clauses (omp_privatization_save
);
10693 /* Clear this in case we're in the middle of a default argument. */
10694 parser
->local_variables_forbidden_p
= false;
10695 parser
->in_function_body
= true;
10698 local_specialization_stack
s (lss_copy
);
10699 tree fco
= lambda_function (lambda_expr
);
10700 tree body
= start_lambda_function (fco
, lambda_expr
);
10701 matching_braces braces
;
10703 if (braces
.require_open (parser
))
10705 tree compound_stmt
= begin_compound_stmt (0);
10707 /* Originally C++11 required us to peek for 'return expr'; and
10708 process it specially here to deduce the return type. N3638
10709 removed the need for that. */
10711 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10712 cp_parser_label_declaration (parser
);
10713 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10714 braces
.require_close (parser
);
10716 finish_compound_stmt (compound_stmt
);
10719 finish_lambda_function (body
);
10722 restore_omp_privatization_clauses (omp_privatization_save
);
10723 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10724 parser
->in_function_body
= in_function_body
;
10726 pop_function_context();
10731 /* Statements [gram.stmt.stmt] */
10733 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10736 add_debug_begin_stmt (location_t loc
)
10738 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10740 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10741 /* A concept is never expanded normally. */
10744 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10745 SET_EXPR_LOCATION (stmt
, loc
);
10749 /* Parse a statement.
10753 expression-statement
10755 selection-statement
10756 iteration-statement
10758 declaration-statement
10765 attribute-specifier-seq (opt) expression-statement
10766 attribute-specifier-seq (opt) compound-statement
10767 attribute-specifier-seq (opt) selection-statement
10768 attribute-specifier-seq (opt) iteration-statement
10769 attribute-specifier-seq (opt) jump-statement
10770 declaration-statement
10771 attribute-specifier-seq (opt) try-block
10774 expression-statement
10782 IN_COMPOUND is true when the statement is nested inside a
10783 cp_parser_compound_statement; this matters for certain pragmas.
10785 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10786 is a (possibly labeled) if statement which is not enclosed in braces
10787 and has an else clause. This is used to implement -Wparentheses.
10789 CHAIN is a vector of if-else-if conditions. */
10792 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10793 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10794 location_t
*loc_after_labels
)
10796 tree statement
, std_attrs
= NULL_TREE
;
10798 location_t statement_location
, attrs_location
;
10803 /* There is no statement yet. */
10804 statement
= NULL_TREE
;
10806 saved_token_sentinel
saved_tokens (parser
->lexer
);
10807 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10808 if (c_dialect_objc ())
10809 /* In obj-c++, seeing '[[' might be the either the beginning of
10810 c++11 attributes, or a nested objc-message-expression. So
10811 let's parse the c++11 attributes tentatively. */
10812 cp_parser_parse_tentatively (parser
);
10813 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10814 if (c_dialect_objc ())
10816 if (!cp_parser_parse_definitely (parser
))
10817 std_attrs
= NULL_TREE
;
10820 /* Peek at the next token. */
10821 token
= cp_lexer_peek_token (parser
->lexer
);
10822 /* Remember the location of the first token in the statement. */
10823 statement_location
= token
->location
;
10824 add_debug_begin_stmt (statement_location
);
10825 /* If this is a keyword, then that will often determine what kind of
10826 statement we have. */
10827 if (token
->type
== CPP_KEYWORD
)
10829 enum rid keyword
= token
->keyword
;
10835 /* Looks like a labeled-statement with a case label.
10836 Parse the label, and then use tail recursion to parse
10838 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10839 in_compound
= false;
10844 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10850 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10857 statement
= cp_parser_jump_statement (parser
);
10860 /* Objective-C++ exception-handling constructs. */
10863 case RID_AT_FINALLY
:
10864 case RID_AT_SYNCHRONIZED
:
10866 statement
= cp_parser_objc_statement (parser
);
10870 statement
= cp_parser_try_block (parser
);
10873 case RID_NAMESPACE
:
10874 /* This must be a namespace alias definition. */
10875 cp_parser_declaration_statement (parser
);
10878 case RID_TRANSACTION_ATOMIC
:
10879 case RID_TRANSACTION_RELAXED
:
10880 case RID_SYNCHRONIZED
:
10881 case RID_ATOMIC_NOEXCEPT
:
10882 case RID_ATOMIC_CANCEL
:
10883 statement
= cp_parser_transaction (parser
, token
);
10885 case RID_TRANSACTION_CANCEL
:
10886 statement
= cp_parser_transaction_cancel (parser
);
10890 /* It might be a keyword like `int' that can start a
10891 declaration-statement. */
10895 else if (token
->type
== CPP_NAME
)
10897 /* If the next token is a `:', then we are looking at a
10898 labeled-statement. */
10899 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10900 if (token
->type
== CPP_COLON
)
10902 /* Looks like a labeled-statement with an ordinary label.
10903 Parse the label, and then use tail recursion to parse
10906 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10907 in_compound
= false;
10911 /* Anything that starts with a `{' must be a compound-statement. */
10912 else if (token
->type
== CPP_OPEN_BRACE
)
10913 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10914 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10915 a statement all its own. */
10916 else if (token
->type
== CPP_PRAGMA
)
10918 /* Only certain OpenMP pragmas are attached to statements, and thus
10919 are considered statements themselves. All others are not. In
10920 the context of a compound, accept the pragma as a "statement" and
10921 return so that we can check for a close brace. Otherwise we
10922 require a real statement and must go back and read one. */
10924 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10925 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10929 else if (token
->type
== CPP_EOF
)
10931 cp_parser_error (parser
, "expected statement");
10935 /* Everything else must be a declaration-statement or an
10936 expression-statement. Try for the declaration-statement
10937 first, unless we are looking at a `;', in which case we know that
10938 we have an expression-statement. */
10941 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10943 if (std_attrs
!= NULL_TREE
)
10945 /* Attributes should be parsed as part of the the
10946 declaration, so let's un-parse them. */
10947 saved_tokens
.rollback();
10948 std_attrs
= NULL_TREE
;
10951 cp_parser_parse_tentatively (parser
);
10952 /* Try to parse the declaration-statement. */
10953 cp_parser_declaration_statement (parser
);
10954 /* If that worked, we're done. */
10955 if (cp_parser_parse_definitely (parser
))
10958 /* All preceding labels have been parsed at this point. */
10959 if (loc_after_labels
!= NULL
)
10960 *loc_after_labels
= statement_location
;
10962 /* Look for an expression-statement instead. */
10963 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10965 /* Handle [[fallthrough]];. */
10966 if (attribute_fallthrough_p (std_attrs
))
10968 /* The next token after the fallthrough attribute is ';'. */
10969 if (statement
== NULL_TREE
)
10971 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10972 statement
= build_call_expr_internal_loc (statement_location
,
10974 void_type_node
, 0);
10975 finish_expr_stmt (statement
);
10978 warning_at (statement_location
, OPT_Wattributes
,
10979 "%<fallthrough%> attribute not followed by %<;%>");
10980 std_attrs
= NULL_TREE
;
10984 /* Set the line number for the statement. */
10985 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10986 SET_EXPR_LOCATION (statement
, statement_location
);
10988 /* Allow "[[fallthrough]];", but warn otherwise. */
10989 if (std_attrs
!= NULL_TREE
)
10990 warning_at (attrs_location
,
10992 "attributes at the beginning of statement are ignored");
10995 /* Append ATTR to attribute list ATTRS. */
10998 attr_chainon (tree attrs
, tree attr
)
11000 if (attrs
== error_mark_node
)
11001 return error_mark_node
;
11002 if (attr
== error_mark_node
)
11003 return error_mark_node
;
11004 return chainon (attrs
, attr
);
11007 /* Parse the label for a labeled-statement, i.e.
11010 case constant-expression :
11014 case constant-expression ... constant-expression : statement
11016 When a label is parsed without errors, the label is added to the
11017 parse tree by the finish_* functions, so this function doesn't
11018 have to return the label. */
11021 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11024 tree label
= NULL_TREE
;
11025 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11027 /* The next token should be an identifier. */
11028 token
= cp_lexer_peek_token (parser
->lexer
);
11029 if (token
->type
!= CPP_NAME
11030 && token
->type
!= CPP_KEYWORD
)
11032 cp_parser_error (parser
, "expected labeled-statement");
11036 /* Remember whether this case or a user-defined label is allowed to fall
11038 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11040 parser
->colon_corrects_to_scope_p
= false;
11041 switch (token
->keyword
)
11045 tree expr
, expr_hi
;
11046 cp_token
*ellipsis
;
11048 /* Consume the `case' token. */
11049 cp_lexer_consume_token (parser
->lexer
);
11050 /* Parse the constant-expression. */
11051 expr
= cp_parser_constant_expression (parser
);
11052 if (check_for_bare_parameter_packs (expr
))
11053 expr
= error_mark_node
;
11055 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11056 if (ellipsis
->type
== CPP_ELLIPSIS
)
11058 /* Consume the `...' token. */
11059 cp_lexer_consume_token (parser
->lexer
);
11060 expr_hi
= cp_parser_constant_expression (parser
);
11061 if (check_for_bare_parameter_packs (expr_hi
))
11062 expr_hi
= error_mark_node
;
11064 /* We don't need to emit warnings here, as the common code
11065 will do this for us. */
11068 expr_hi
= NULL_TREE
;
11070 if (parser
->in_switch_statement_p
)
11072 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11073 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11074 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11077 error_at (token
->location
,
11078 "case label %qE not within a switch statement",
11084 /* Consume the `default' token. */
11085 cp_lexer_consume_token (parser
->lexer
);
11087 if (parser
->in_switch_statement_p
)
11089 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11090 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11091 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11094 error_at (token
->location
, "case label not within a switch statement");
11098 /* Anything else must be an ordinary label. */
11099 label
= finish_label_stmt (cp_parser_identifier (parser
));
11100 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11101 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11105 /* Require the `:' token. */
11106 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11108 /* An ordinary label may optionally be followed by attributes.
11109 However, this is only permitted if the attributes are then
11110 followed by a semicolon. This is because, for backward
11111 compatibility, when parsing
11112 lab: __attribute__ ((unused)) int i;
11113 we want the attribute to attach to "i", not "lab". */
11114 if (label
!= NULL_TREE
11115 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11118 cp_parser_parse_tentatively (parser
);
11119 attrs
= cp_parser_gnu_attributes_opt (parser
);
11120 if (attrs
== NULL_TREE
11121 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11122 cp_parser_abort_tentative_parse (parser
);
11123 else if (!cp_parser_parse_definitely (parser
))
11126 attributes
= attr_chainon (attributes
, attrs
);
11129 if (attributes
!= NULL_TREE
)
11130 cplus_decl_attributes (&label
, attributes
, 0);
11132 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11135 /* Parse an expression-statement.
11137 expression-statement:
11140 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11141 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11142 indicates whether this expression-statement is part of an
11143 expression statement. */
11146 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11148 tree statement
= NULL_TREE
;
11149 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11150 location_t loc
= token
->location
;
11152 /* There might be attribute fallthrough. */
11153 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11155 /* If the next token is a ';', then there is no expression
11157 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11159 statement
= cp_parser_expression (parser
);
11160 if (statement
== error_mark_node
11161 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11163 cp_parser_skip_to_end_of_block_or_statement (parser
);
11164 return error_mark_node
;
11168 /* Handle [[fallthrough]];. */
11169 if (attribute_fallthrough_p (attr
))
11171 /* The next token after the fallthrough attribute is ';'. */
11172 if (statement
== NULL_TREE
)
11173 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11174 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11175 void_type_node
, 0);
11177 warning_at (loc
, OPT_Wattributes
,
11178 "%<fallthrough%> attribute not followed by %<;%>");
11182 /* Allow "[[fallthrough]];", but warn otherwise. */
11183 if (attr
!= NULL_TREE
)
11184 warning_at (loc
, OPT_Wattributes
,
11185 "attributes at the beginning of statement are ignored");
11187 /* Give a helpful message for "A<T>::type t;" and the like. */
11188 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11189 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11191 if (TREE_CODE (statement
) == SCOPE_REF
)
11192 error_at (token
->location
, "need %<typename%> before %qE because "
11193 "%qT is a dependent scope",
11194 statement
, TREE_OPERAND (statement
, 0));
11195 else if (is_overloaded_fn (statement
)
11196 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11199 tree fn
= get_first_fn (statement
);
11200 error_at (token
->location
,
11201 "%<%T::%D%> names the constructor, not the type",
11202 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11206 /* Consume the final `;'. */
11207 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11209 if (in_statement_expr
11210 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11211 /* This is the final expression statement of a statement
11213 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11214 else if (statement
)
11215 statement
= finish_expr_stmt (statement
);
11220 /* Parse a compound-statement.
11222 compound-statement:
11223 { statement-seq [opt] }
11227 compound-statement:
11228 { label-declaration-seq [opt] statement-seq [opt] }
11230 label-declaration-seq:
11232 label-declaration-seq label-declaration
11234 Returns a tree representing the statement. */
11237 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11238 int bcs_flags
, bool function_body
)
11240 tree compound_stmt
;
11241 matching_braces braces
;
11243 /* Consume the `{'. */
11244 if (!braces
.require_open (parser
))
11245 return error_mark_node
;
11246 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11247 && !function_body
&& cxx_dialect
< cxx14
)
11248 pedwarn (input_location
, OPT_Wpedantic
,
11249 "compound-statement in %<constexpr%> function");
11250 /* Begin the compound-statement. */
11251 compound_stmt
= begin_compound_stmt (bcs_flags
);
11252 /* If the next keyword is `__label__' we have a label declaration. */
11253 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11254 cp_parser_label_declaration (parser
);
11255 /* Parse an (optional) statement-seq. */
11256 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11257 /* Finish the compound-statement. */
11258 finish_compound_stmt (compound_stmt
);
11259 /* Consume the `}'. */
11260 braces
.require_close (parser
);
11262 return compound_stmt
;
11265 /* Parse an (optional) statement-seq.
11269 statement-seq [opt] statement */
11272 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11274 /* Scan statements until there aren't any more. */
11277 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11279 /* If we are looking at a `}', then we have run out of
11280 statements; the same is true if we have reached the end
11281 of file, or have stumbled upon a stray '@end'. */
11282 if (token
->type
== CPP_CLOSE_BRACE
11283 || token
->type
== CPP_EOF
11284 || token
->type
== CPP_PRAGMA_EOL
11285 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11288 /* If we are in a compound statement and find 'else' then
11289 something went wrong. */
11290 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11292 if (parser
->in_statement
& IN_IF_STMT
)
11296 token
= cp_lexer_consume_token (parser
->lexer
);
11297 error_at (token
->location
, "%<else%> without a previous %<if%>");
11301 /* Parse the statement. */
11302 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11306 /* Return true if this is the C++20 version of range-based-for with
11310 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11314 /* Save tokens so that we can put them back. */
11315 cp_lexer_save_tokens (parser
->lexer
);
11317 /* There has to be an unnested ; followed by an unnested :. */
11318 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11319 /*recovering=*/false,
11321 /*consume_paren=*/false) != -1)
11324 /* We found the semicolon, eat it now. */
11325 cp_lexer_consume_token (parser
->lexer
);
11327 /* Now look for ':' that is not nested in () or {}. */
11328 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11329 /*recovering=*/false,
11331 /*consume_paren=*/false) == -1);
11334 /* Roll back the tokens we skipped. */
11335 cp_lexer_rollback_tokens (parser
->lexer
);
11340 /* Return true if we're looking at (init; cond), false otherwise. */
11343 cp_parser_init_statement_p (cp_parser
*parser
)
11345 /* Save tokens so that we can put them back. */
11346 cp_lexer_save_tokens (parser
->lexer
);
11348 /* Look for ';' that is not nested in () or {}. */
11349 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11350 /*recovering=*/false,
11352 /*consume_paren=*/false);
11354 /* Roll back the tokens we skipped. */
11355 cp_lexer_rollback_tokens (parser
->lexer
);
11360 /* Parse a selection-statement.
11362 selection-statement:
11363 if ( init-statement [opt] condition ) statement
11364 if ( init-statement [opt] condition ) statement else statement
11365 switch ( init-statement [opt] condition ) statement
11367 Returns the new IF_STMT or SWITCH_STMT.
11369 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11370 is a (possibly labeled) if statement which is not enclosed in
11371 braces and has an else clause. This is used to implement
11374 CHAIN is a vector of if-else-if conditions. This is used to implement
11375 -Wduplicated-cond. */
11378 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11383 token_indent_info guard_tinfo
;
11388 /* Peek at the next token. */
11389 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11390 guard_tinfo
= get_token_indent_info (token
);
11392 /* See what kind of keyword it is. */
11393 keyword
= token
->keyword
;
11403 if (keyword
== RID_IF
11404 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11408 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11409 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11410 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11411 "with -std=c++17 or -std=gnu++17");
11414 /* Look for the `('. */
11415 matching_parens parens
;
11416 if (!parens
.require_open (parser
))
11418 cp_parser_skip_to_end_of_statement (parser
);
11419 return error_mark_node
;
11422 /* Begin the selection-statement. */
11423 if (keyword
== RID_IF
)
11425 statement
= begin_if_stmt ();
11426 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11429 statement
= begin_switch_stmt ();
11431 /* Parse the optional init-statement. */
11432 if (cp_parser_init_statement_p (parser
))
11435 if (cxx_dialect
< cxx17
)
11436 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11437 "init-statement in selection statements only available "
11438 "with -std=c++17 or -std=gnu++17");
11439 cp_parser_init_statement (parser
, &decl
);
11442 /* Parse the condition. */
11443 condition
= cp_parser_condition (parser
);
11444 /* Look for the `)'. */
11445 if (!parens
.require_close (parser
))
11446 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11447 /*consume_paren=*/true);
11449 if (keyword
== RID_IF
)
11452 unsigned char in_statement
;
11454 /* Add the condition. */
11455 condition
= finish_if_stmt_cond (condition
, statement
);
11457 if (warn_duplicated_cond
)
11458 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11461 /* Parse the then-clause. */
11462 in_statement
= parser
->in_statement
;
11463 parser
->in_statement
|= IN_IF_STMT
;
11465 /* Outside a template, the non-selected branch of a constexpr
11466 if is a 'discarded statement', i.e. unevaluated. */
11467 bool was_discarded
= in_discarded_stmt
;
11468 bool discard_then
= (cx
&& !processing_template_decl
11469 && integer_zerop (condition
));
11472 in_discarded_stmt
= true;
11473 ++c_inhibit_evaluation_warnings
;
11476 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11479 parser
->in_statement
= in_statement
;
11481 finish_then_clause (statement
);
11485 THEN_CLAUSE (statement
) = NULL_TREE
;
11486 in_discarded_stmt
= was_discarded
;
11487 --c_inhibit_evaluation_warnings
;
11490 /* If the next token is `else', parse the else-clause. */
11491 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11494 bool discard_else
= (cx
&& !processing_template_decl
11495 && integer_nonzerop (condition
));
11498 in_discarded_stmt
= true;
11499 ++c_inhibit_evaluation_warnings
;
11503 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11504 /* Consume the `else' keyword. */
11505 cp_lexer_consume_token (parser
->lexer
);
11506 if (warn_duplicated_cond
)
11508 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11512 /* We've got "if (COND) else if (COND2)". Start
11513 the condition chain and add COND as the first
11515 chain
= new vec
<tree
> ();
11516 if (!CONSTANT_CLASS_P (condition
)
11517 && !TREE_SIDE_EFFECTS (condition
))
11519 /* Wrap it in a NOP_EXPR so that we can set the
11520 location of the condition. */
11521 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11523 SET_EXPR_LOCATION (e
, token
->location
);
11524 chain
->safe_push (e
);
11527 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11530 /* This is if-else without subsequent if. Zap the
11531 condition chain; we would have already warned at
11537 begin_else_clause (statement
);
11538 /* Parse the else-clause. */
11539 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11540 guard_tinfo
, chain
);
11542 finish_else_clause (statement
);
11544 /* If we are currently parsing a then-clause, then
11545 IF_P will not be NULL. We set it to true to
11546 indicate that this if statement has an else clause.
11547 This may trigger the Wparentheses warning below
11548 when we get back up to the parent if statement. */
11554 ELSE_CLAUSE (statement
) = NULL_TREE
;
11555 in_discarded_stmt
= was_discarded
;
11556 --c_inhibit_evaluation_warnings
;
11561 /* This if statement does not have an else clause. If
11562 NESTED_IF is true, then the then-clause has an if
11563 statement which does have an else clause. We warn
11564 about the potential ambiguity. */
11566 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11567 "suggest explicit braces to avoid ambiguous"
11569 if (warn_duplicated_cond
)
11571 /* We don't need the condition chain anymore. */
11577 /* Now we're all done with the if-statement. */
11578 finish_if_stmt (statement
);
11582 bool in_switch_statement_p
;
11583 unsigned char in_statement
;
11585 /* Add the condition. */
11586 finish_switch_cond (condition
, statement
);
11588 /* Parse the body of the switch-statement. */
11589 in_switch_statement_p
= parser
->in_switch_statement_p
;
11590 in_statement
= parser
->in_statement
;
11591 parser
->in_switch_statement_p
= true;
11592 parser
->in_statement
|= IN_SWITCH_STMT
;
11593 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11595 parser
->in_switch_statement_p
= in_switch_statement_p
;
11596 parser
->in_statement
= in_statement
;
11598 /* Now we're all done with the switch-statement. */
11599 finish_switch_stmt (statement
);
11607 cp_parser_error (parser
, "expected selection-statement");
11608 return error_mark_node
;
11612 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11613 If we have seen at least one decl-specifier, and the next token
11614 is not a parenthesis, then we must be looking at a declaration.
11615 (After "int (" we might be looking at a functional cast.) */
11618 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11619 bool any_specifiers_p
)
11621 if (any_specifiers_p
11622 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11623 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11624 && !cp_parser_error_occurred (parser
))
11625 cp_parser_commit_to_tentative_parse (parser
);
11628 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11629 The declarator shall not specify a function or an array. Returns
11630 TRUE if the declarator is valid, FALSE otherwise. */
11633 cp_parser_check_condition_declarator (cp_parser
* parser
,
11634 cp_declarator
*declarator
,
11637 if (declarator
== cp_error_declarator
11638 || function_declarator_p (declarator
)
11639 || declarator
->kind
== cdk_array
)
11641 if (declarator
== cp_error_declarator
)
11642 /* Already complained. */;
11643 else if (declarator
->kind
== cdk_array
)
11644 error_at (loc
, "condition declares an array");
11646 error_at (loc
, "condition declares a function");
11647 if (parser
->fully_implicit_function_template_p
)
11648 abort_fully_implicit_template (parser
);
11649 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11650 /*or_comma=*/false,
11651 /*consume_paren=*/false);
11658 /* Parse a condition.
11662 type-specifier-seq declarator = initializer-clause
11663 type-specifier-seq declarator braced-init-list
11668 type-specifier-seq declarator asm-specification [opt]
11669 attributes [opt] = assignment-expression
11671 Returns the expression that should be tested. */
11674 cp_parser_condition (cp_parser
* parser
)
11676 cp_decl_specifier_seq type_specifiers
;
11677 const char *saved_message
;
11678 int declares_class_or_enum
;
11680 /* Try the declaration first. */
11681 cp_parser_parse_tentatively (parser
);
11682 /* New types are not allowed in the type-specifier-seq for a
11684 saved_message
= parser
->type_definition_forbidden_message
;
11685 parser
->type_definition_forbidden_message
11686 = G_("types may not be defined in conditions");
11687 /* Parse the type-specifier-seq. */
11688 cp_parser_decl_specifier_seq (parser
,
11689 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11691 &declares_class_or_enum
);
11692 /* Restore the saved message. */
11693 parser
->type_definition_forbidden_message
= saved_message
;
11695 cp_parser_maybe_commit_to_declaration (parser
,
11696 type_specifiers
.any_specifiers_p
);
11698 /* If all is well, we might be looking at a declaration. */
11699 if (!cp_parser_error_occurred (parser
))
11702 tree asm_specification
;
11704 cp_declarator
*declarator
;
11705 tree initializer
= NULL_TREE
;
11706 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11708 /* Parse the declarator. */
11709 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11710 /*ctor_dtor_or_conv_p=*/NULL
,
11711 /*parenthesized_p=*/NULL
,
11712 /*member_p=*/false,
11713 /*friend_p=*/false);
11714 /* Parse the attributes. */
11715 attributes
= cp_parser_attributes_opt (parser
);
11716 /* Parse the asm-specification. */
11717 asm_specification
= cp_parser_asm_specification_opt (parser
);
11718 /* If the next token is not an `=' or '{', then we might still be
11719 looking at an expression. For example:
11723 looks like a decl-specifier-seq and a declarator -- but then
11724 there is no `=', so this is an expression. */
11725 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11726 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11727 cp_parser_simulate_error (parser
);
11729 /* If we did see an `=' or '{', then we are looking at a declaration
11731 if (cp_parser_parse_definitely (parser
))
11734 bool non_constant_p
= false;
11735 int flags
= LOOKUP_ONLYCONVERTING
;
11737 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
11738 return error_mark_node
;
11740 /* Create the declaration. */
11741 decl
= start_decl (declarator
, &type_specifiers
,
11742 /*initialized_p=*/true,
11743 attributes
, /*prefix_attributes=*/NULL_TREE
,
11746 /* Parse the initializer. */
11747 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11749 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11750 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11753 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11755 /* Consume the `='. */
11756 cp_lexer_consume_token (parser
->lexer
);
11757 initializer
= cp_parser_initializer_clause (parser
,
11762 cp_parser_error (parser
, "expected initializer");
11763 initializer
= error_mark_node
;
11765 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11766 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11768 /* Process the initializer. */
11769 cp_finish_decl (decl
,
11770 initializer
, !non_constant_p
,
11775 pop_scope (pushed_scope
);
11777 return convert_from_reference (decl
);
11780 /* If we didn't even get past the declarator successfully, we are
11781 definitely not looking at a declaration. */
11783 cp_parser_abort_tentative_parse (parser
);
11785 /* Otherwise, we are looking at an expression. */
11786 return cp_parser_expression (parser
);
11789 /* Parses a for-statement or range-for-statement until the closing ')',
11793 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11795 tree init
, scope
, decl
;
11798 /* Begin the for-statement. */
11799 scope
= begin_for_scope (&init
);
11801 /* Parse the initialization. */
11802 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11805 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11807 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11811 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11812 unsigned short unroll
)
11814 /* Normal for loop */
11815 tree condition
= NULL_TREE
;
11816 tree expression
= NULL_TREE
;
11819 stmt
= begin_for_stmt (scope
, init
);
11820 /* The init-statement has already been parsed in
11821 cp_parser_init_statement, so no work is needed here. */
11822 finish_init_stmt (stmt
);
11824 /* If there's a condition, process it. */
11825 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11826 condition
= cp_parser_condition (parser
);
11829 cp_parser_error (parser
, "missing loop condition in loop with "
11830 "%<GCC ivdep%> pragma");
11831 condition
= error_mark_node
;
11835 cp_parser_error (parser
, "missing loop condition in loop with "
11836 "%<GCC unroll%> pragma");
11837 condition
= error_mark_node
;
11839 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11840 /* Look for the `;'. */
11841 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11843 /* If there's an expression, process it. */
11844 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11845 expression
= cp_parser_expression (parser
);
11846 finish_for_expr (expression
, stmt
);
11851 /* Tries to parse a range-based for-statement:
11854 decl-specifier-seq declarator : expression
11856 The decl-specifier-seq declarator and the `:' are already parsed by
11857 cp_parser_init_statement. If processing_template_decl it returns a
11858 newly created RANGE_FOR_STMT; if not, it is converted to a
11859 regular FOR_STMT. */
11862 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11863 bool ivdep
, unsigned short unroll
)
11865 tree stmt
, range_expr
;
11866 auto_vec
<cxx_binding
*, 16> bindings
;
11867 auto_vec
<tree
, 16> names
;
11868 tree decomp_first_name
= NULL_TREE
;
11869 unsigned int decomp_cnt
= 0;
11871 /* Get the range declaration momentarily out of the way so that
11872 the range expression doesn't clash with it. */
11873 if (range_decl
!= error_mark_node
)
11875 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11877 tree v
= DECL_VALUE_EXPR (range_decl
);
11878 /* For decomposition declaration get all of the corresponding
11879 declarations out of the way. */
11880 if (TREE_CODE (v
) == ARRAY_REF
11881 && VAR_P (TREE_OPERAND (v
, 0))
11882 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11884 tree d
= range_decl
;
11885 range_decl
= TREE_OPERAND (v
, 0);
11886 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11887 decomp_first_name
= d
;
11888 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11890 tree name
= DECL_NAME (d
);
11891 names
.safe_push (name
);
11892 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11893 IDENTIFIER_BINDING (name
)
11894 = IDENTIFIER_BINDING (name
)->previous
;
11898 if (names
.is_empty ())
11900 tree name
= DECL_NAME (range_decl
);
11901 names
.safe_push (name
);
11902 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11903 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11907 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11909 bool expr_non_constant_p
;
11910 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11913 range_expr
= cp_parser_expression (parser
);
11915 /* Put the range declaration(s) back into scope. */
11916 for (unsigned int i
= 0; i
< names
.length (); i
++)
11918 cxx_binding
*binding
= bindings
[i
];
11919 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11920 IDENTIFIER_BINDING (names
[i
]) = binding
;
11923 /* If in template, STMT is converted to a normal for-statement
11924 at instantiation. If not, it is done just ahead. */
11925 if (processing_template_decl
)
11927 if (check_for_bare_parameter_packs (range_expr
))
11928 range_expr
= error_mark_node
;
11929 stmt
= begin_range_for_stmt (scope
, init
);
11931 RANGE_FOR_IVDEP (stmt
) = 1;
11933 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11934 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11935 if (!type_dependent_expression_p (range_expr
)
11936 /* do_auto_deduction doesn't mess with template init-lists. */
11937 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11938 do_range_for_auto_deduction (range_decl
, range_expr
);
11942 stmt
= begin_for_stmt (scope
, init
);
11943 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11944 decomp_first_name
, decomp_cnt
, ivdep
,
11950 /* Subroutine of cp_convert_range_for: given the initializer expression,
11951 builds up the range temporary. */
11954 build_range_temp (tree range_expr
)
11956 tree range_type
, range_temp
;
11958 /* Find out the type deduced by the declaration
11959 `auto &&__range = range_expr'. */
11960 range_type
= cp_build_reference_type (make_auto (), true);
11961 range_type
= do_auto_deduction (range_type
, range_expr
,
11962 type_uses_auto (range_type
));
11964 /* Create the __range variable. */
11965 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
11967 TREE_USED (range_temp
) = 1;
11968 DECL_ARTIFICIAL (range_temp
) = 1;
11973 /* Used by cp_parser_range_for in template context: we aren't going to
11974 do a full conversion yet, but we still need to resolve auto in the
11975 type of the for-range-declaration if present. This is basically
11976 a shortcut version of cp_convert_range_for. */
11979 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11981 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11984 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11985 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11986 iter_type
= (cp_parser_perform_range_for_lookup
11987 (range_temp
, &begin_dummy
, &end_dummy
));
11990 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11992 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11994 tf_warning_or_error
);
11995 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11996 iter_decl
, auto_node
);
12001 /* Converts a range-based for-statement into a normal
12002 for-statement, as per the definition.
12004 for (RANGE_DECL : RANGE_EXPR)
12007 should be equivalent to:
12010 auto &&__range = RANGE_EXPR;
12011 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12015 RANGE_DECL = *__begin;
12020 If RANGE_EXPR is an array:
12021 BEGIN_EXPR = __range
12022 END_EXPR = __range + ARRAY_SIZE(__range)
12023 Else if RANGE_EXPR has a member 'begin' or 'end':
12024 BEGIN_EXPR = __range.begin()
12025 END_EXPR = __range.end()
12027 BEGIN_EXPR = begin(__range)
12028 END_EXPR = end(__range);
12030 If __range has a member 'begin' but not 'end', or vice versa, we must
12031 still use the second alternative (it will surely fail, however).
12032 When calling begin()/end() in the third alternative we must use
12033 argument dependent lookup, but always considering 'std' as an associated
12037 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12038 tree decomp_first_name
, unsigned int decomp_cnt
,
12039 bool ivdep
, unsigned short unroll
)
12042 tree iter_type
, begin_expr
, end_expr
;
12043 tree condition
, expression
;
12045 range_expr
= mark_lvalue_use (range_expr
);
12047 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12048 /* If an error happened previously do nothing or else a lot of
12049 unhelpful errors would be issued. */
12050 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12055 if (VAR_P (range_expr
)
12056 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12057 /* Can't bind a reference to an array of runtime bound. */
12058 range_temp
= range_expr
;
12061 range_temp
= build_range_temp (range_expr
);
12062 pushdecl (range_temp
);
12063 cp_finish_decl (range_temp
, range_expr
,
12064 /*is_constant_init*/false, NULL_TREE
,
12065 LOOKUP_ONLYCONVERTING
);
12066 range_temp
= convert_from_reference (range_temp
);
12068 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12069 &begin_expr
, &end_expr
);
12072 /* The new for initialization statement. */
12073 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12075 TREE_USED (begin
) = 1;
12076 DECL_ARTIFICIAL (begin
) = 1;
12078 cp_finish_decl (begin
, begin_expr
,
12079 /*is_constant_init*/false, NULL_TREE
,
12080 LOOKUP_ONLYCONVERTING
);
12082 if (cxx_dialect
>= cxx17
)
12083 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12084 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12085 TREE_USED (end
) = 1;
12086 DECL_ARTIFICIAL (end
) = 1;
12088 cp_finish_decl (end
, end_expr
,
12089 /*is_constant_init*/false, NULL_TREE
,
12090 LOOKUP_ONLYCONVERTING
);
12092 finish_init_stmt (statement
);
12094 /* The new for condition. */
12095 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12098 NULL
, tf_warning_or_error
);
12099 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12101 /* The new increment expression. */
12102 expression
= finish_unary_op_expr (input_location
,
12103 PREINCREMENT_EXPR
, begin
,
12104 tf_warning_or_error
);
12105 finish_for_expr (expression
, statement
);
12107 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12108 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12110 /* The declaration is initialized with *__begin inside the loop body. */
12111 cp_finish_decl (range_decl
,
12112 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12113 tf_warning_or_error
),
12114 /*is_constant_init*/false, NULL_TREE
,
12115 LOOKUP_ONLYCONVERTING
);
12116 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12117 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12122 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12123 We need to solve both at the same time because the method used
12124 depends on the existence of members begin or end.
12125 Returns the type deduced for the iterator expression. */
12128 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12130 if (error_operand_p (range
))
12132 *begin
= *end
= error_mark_node
;
12133 return error_mark_node
;
12136 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12138 error ("range-based %<for%> expression of type %qT "
12139 "has incomplete type", TREE_TYPE (range
));
12140 *begin
= *end
= error_mark_node
;
12141 return error_mark_node
;
12143 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12145 /* If RANGE is an array, we will use pointer arithmetic. */
12146 *begin
= decay_conversion (range
, tf_warning_or_error
);
12147 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12149 array_type_nelts_top (TREE_TYPE (range
)),
12151 return TREE_TYPE (*begin
);
12155 /* If it is not an array, we must do a bit of magic. */
12156 tree id_begin
, id_end
;
12157 tree member_begin
, member_end
;
12159 *begin
= *end
= error_mark_node
;
12161 id_begin
= get_identifier ("begin");
12162 id_end
= get_identifier ("end");
12163 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12164 /*protect=*/2, /*want_type=*/false,
12165 tf_warning_or_error
);
12166 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12167 /*protect=*/2, /*want_type=*/false,
12168 tf_warning_or_error
);
12170 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12172 /* Use the member functions. */
12173 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12174 *end
= cp_parser_range_for_member_function (range
, id_end
);
12178 /* Use global functions with ADL. */
12179 vec
<tree
, va_gc
> *vec
;
12180 vec
= make_tree_vector ();
12182 vec_safe_push (vec
, range
);
12184 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12185 tf_warning_or_error
);
12186 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12187 tf_warning_or_error
);
12188 member_end
= perform_koenig_lookup (id_end
, vec
,
12189 tf_warning_or_error
);
12190 *end
= finish_call_expr (member_end
, &vec
, false, true,
12191 tf_warning_or_error
);
12193 release_tree_vector (vec
);
12196 /* Last common checks. */
12197 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12199 /* If one of the expressions is an error do no more checks. */
12200 *begin
= *end
= error_mark_node
;
12201 return error_mark_node
;
12203 else if (type_dependent_expression_p (*begin
)
12204 || type_dependent_expression_p (*end
))
12205 /* Can happen, when, eg, in a template context, Koenig lookup
12206 can't resolve begin/end (c++/58503). */
12210 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12211 /* The unqualified type of the __begin and __end temporaries should
12212 be the same, as required by the multiple auto declaration. */
12213 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12215 if (cxx_dialect
>= cxx17
12216 && (build_x_binary_op (input_location
, NE_EXPR
,
12217 *begin
, ERROR_MARK
,
12220 != error_mark_node
))
12221 /* P0184R0 allows __begin and __end to have different types,
12222 but make sure they are comparable so we can give a better
12225 error ("inconsistent begin/end types in range-based %<for%> "
12226 "statement: %qT and %qT",
12227 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12234 /* Helper function for cp_parser_perform_range_for_lookup.
12235 Builds a tree for RANGE.IDENTIFIER(). */
12238 cp_parser_range_for_member_function (tree range
, tree identifier
)
12241 vec
<tree
, va_gc
> *vec
;
12243 member
= finish_class_member_access_expr (range
, identifier
,
12244 false, tf_warning_or_error
);
12245 if (member
== error_mark_node
)
12246 return error_mark_node
;
12248 vec
= make_tree_vector ();
12249 res
= finish_call_expr (member
, &vec
,
12250 /*disallow_virtual=*/false,
12251 /*koenig_p=*/false,
12252 tf_warning_or_error
);
12253 release_tree_vector (vec
);
12257 /* Parse an iteration-statement.
12259 iteration-statement:
12260 while ( condition ) statement
12261 do statement while ( expression ) ;
12262 for ( init-statement condition [opt] ; expression [opt] )
12265 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12268 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12269 unsigned short unroll
)
12274 unsigned char in_statement
;
12275 token_indent_info guard_tinfo
;
12277 /* Peek at the next token. */
12278 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12280 return error_mark_node
;
12282 guard_tinfo
= get_token_indent_info (token
);
12284 /* Remember whether or not we are already within an iteration
12286 in_statement
= parser
->in_statement
;
12288 /* See what kind of keyword it is. */
12289 keyword
= token
->keyword
;
12296 /* Begin the while-statement. */
12297 statement
= begin_while_stmt ();
12298 /* Look for the `('. */
12299 matching_parens parens
;
12300 parens
.require_open (parser
);
12301 /* Parse the condition. */
12302 condition
= cp_parser_condition (parser
);
12303 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12304 /* Look for the `)'. */
12305 parens
.require_close (parser
);
12306 /* Parse the dependent statement. */
12307 parser
->in_statement
= IN_ITERATION_STMT
;
12308 bool prev
= note_iteration_stmt_body_start ();
12309 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12310 note_iteration_stmt_body_end (prev
);
12311 parser
->in_statement
= in_statement
;
12312 /* We're done with the while-statement. */
12313 finish_while_stmt (statement
);
12321 /* Begin the do-statement. */
12322 statement
= begin_do_stmt ();
12323 /* Parse the body of the do-statement. */
12324 parser
->in_statement
= IN_ITERATION_STMT
;
12325 bool prev
= note_iteration_stmt_body_start ();
12326 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12327 note_iteration_stmt_body_end (prev
);
12328 parser
->in_statement
= in_statement
;
12329 finish_do_body (statement
);
12330 /* Look for the `while' keyword. */
12331 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12332 /* Look for the `('. */
12333 matching_parens parens
;
12334 parens
.require_open (parser
);
12335 /* Parse the expression. */
12336 expression
= cp_parser_expression (parser
);
12337 /* We're done with the do-statement. */
12338 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12339 /* Look for the `)'. */
12340 parens
.require_close (parser
);
12341 /* Look for the `;'. */
12342 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12348 /* Look for the `('. */
12349 matching_parens parens
;
12350 parens
.require_open (parser
);
12352 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12354 /* Look for the `)'. */
12355 parens
.require_close (parser
);
12357 /* Parse the body of the for-statement. */
12358 parser
->in_statement
= IN_ITERATION_STMT
;
12359 bool prev
= note_iteration_stmt_body_start ();
12360 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12361 note_iteration_stmt_body_end (prev
);
12362 parser
->in_statement
= in_statement
;
12364 /* We're done with the for-statement. */
12365 finish_for_stmt (statement
);
12370 cp_parser_error (parser
, "expected iteration-statement");
12371 statement
= error_mark_node
;
12378 /* Parse a init-statement or the declarator of a range-based-for.
12379 Returns true if a range-based-for declaration is seen.
12382 expression-statement
12383 simple-declaration */
12386 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12388 /* If the next token is a `;', then we have an empty
12389 expression-statement. Grammatically, this is also a
12390 simple-declaration, but an invalid one, because it does not
12391 declare anything. Therefore, if we did not handle this case
12392 specially, we would issue an error message about an invalid
12394 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12396 bool is_range_for
= false;
12397 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12399 /* Try to parse the init-statement. */
12400 if (cp_parser_range_based_for_with_init_p (parser
))
12403 cp_parser_parse_tentatively (parser
);
12404 /* Parse the declaration. */
12405 cp_parser_simple_declaration (parser
,
12406 /*function_definition_allowed_p=*/false,
12408 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12409 if (!cp_parser_parse_definitely (parser
))
12410 /* That didn't work, try to parse it as an expression-statement. */
12411 cp_parser_expression_statement (parser
, NULL_TREE
);
12413 if (cxx_dialect
< cxx2a
)
12415 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12416 "range-based %<for%> loops with initializer only "
12417 "available with -std=c++2a or -std=gnu++2a");
12418 *decl
= error_mark_node
;
12422 /* A colon is used in range-based for. */
12423 parser
->colon_corrects_to_scope_p
= false;
12425 /* We're going to speculatively look for a declaration, falling back
12426 to an expression, if necessary. */
12427 cp_parser_parse_tentatively (parser
);
12428 /* Parse the declaration. */
12429 cp_parser_simple_declaration (parser
,
12430 /*function_definition_allowed_p=*/false,
12432 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12433 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12435 /* It is a range-for, consume the ':'. */
12436 cp_lexer_consume_token (parser
->lexer
);
12437 is_range_for
= true;
12438 if (cxx_dialect
< cxx11
)
12439 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12440 "range-based %<for%> loops only available with "
12441 "-std=c++11 or -std=gnu++11");
12444 /* The ';' is not consumed yet because we told
12445 cp_parser_simple_declaration not to. */
12446 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12448 if (cp_parser_parse_definitely (parser
))
12449 return is_range_for
;
12450 /* If the tentative parse failed, then we shall need to look for an
12451 expression-statement. */
12453 /* If we are here, it is an expression-statement. */
12454 cp_parser_expression_statement (parser
, NULL_TREE
);
12458 /* Parse a jump-statement.
12463 return expression [opt] ;
12464 return braced-init-list ;
12470 goto * expression ;
12472 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12475 cp_parser_jump_statement (cp_parser
* parser
)
12477 tree statement
= error_mark_node
;
12480 unsigned char in_statement
;
12482 /* Peek at the next token. */
12483 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12485 return error_mark_node
;
12487 /* See what kind of keyword it is. */
12488 keyword
= token
->keyword
;
12492 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12493 switch (in_statement
)
12496 error_at (token
->location
, "break statement not within loop or switch");
12499 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12500 || in_statement
== IN_ITERATION_STMT
);
12501 statement
= finish_break_stmt ();
12502 if (in_statement
== IN_ITERATION_STMT
)
12503 break_maybe_infinite_loop ();
12506 error_at (token
->location
, "invalid exit from OpenMP structured block");
12509 error_at (token
->location
, "break statement used with OpenMP for loop");
12512 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12516 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12519 error_at (token
->location
, "continue statement not within a loop");
12521 /* Fall through. */
12522 case IN_ITERATION_STMT
:
12524 statement
= finish_continue_stmt ();
12527 error_at (token
->location
, "invalid exit from OpenMP structured block");
12530 gcc_unreachable ();
12532 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12538 bool expr_non_constant_p
;
12540 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12542 cp_lexer_set_source_position (parser
->lexer
);
12543 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12544 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12546 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12547 expr
= cp_parser_expression (parser
);
12549 /* If the next token is a `;', then there is no
12552 /* Build the return-statement. */
12553 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12554 /* Don't deduce from a discarded return statement. */;
12556 statement
= finish_return_stmt (expr
);
12557 /* Look for the final `;'. */
12558 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12563 if (parser
->in_function_body
12564 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12566 error ("%<goto%> in %<constexpr%> function");
12567 cp_function_chain
->invalid_constexpr
= true;
12570 /* Create the goto-statement. */
12571 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12573 /* Issue a warning about this use of a GNU extension. */
12574 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12575 /* Consume the '*' token. */
12576 cp_lexer_consume_token (parser
->lexer
);
12577 /* Parse the dependent expression. */
12578 finish_goto_stmt (cp_parser_expression (parser
));
12581 finish_goto_stmt (cp_parser_identifier (parser
));
12582 /* Look for the final `;'. */
12583 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12587 cp_parser_error (parser
, "expected jump-statement");
12594 /* Parse a declaration-statement.
12596 declaration-statement:
12597 block-declaration */
12600 cp_parser_declaration_statement (cp_parser
* parser
)
12604 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12605 p
= obstack_alloc (&declarator_obstack
, 0);
12607 /* Parse the block-declaration. */
12608 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12610 /* Free any declarators allocated. */
12611 obstack_free (&declarator_obstack
, p
);
12614 /* Some dependent statements (like `if (cond) statement'), are
12615 implicitly in their own scope. In other words, if the statement is
12616 a single statement (as opposed to a compound-statement), it is
12617 none-the-less treated as if it were enclosed in braces. Any
12618 declarations appearing in the dependent statement are out of scope
12619 after control passes that point. This function parses a statement,
12620 but ensures that is in its own scope, even if it is not a
12621 compound-statement.
12623 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12624 is a (possibly labeled) if statement which is not enclosed in
12625 braces and has an else clause. This is used to implement
12628 CHAIN is a vector of if-else-if conditions. This is used to implement
12631 Returns the new statement. */
12634 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12635 const token_indent_info
&guard_tinfo
,
12639 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12640 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12641 token_indent_info body_tinfo
12642 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12647 /* Mark if () ; with a special NOP_EXPR. */
12648 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12650 cp_lexer_consume_token (parser
->lexer
);
12651 statement
= add_stmt (build_empty_stmt (body_loc
));
12653 if (guard_tinfo
.keyword
== RID_IF
12654 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12655 warning_at (body_loc
, OPT_Wempty_body
,
12656 "suggest braces around empty body in an %<if%> statement");
12657 else if (guard_tinfo
.keyword
== RID_ELSE
)
12658 warning_at (body_loc
, OPT_Wempty_body
,
12659 "suggest braces around empty body in an %<else%> statement");
12661 /* if a compound is opened, we simply parse the statement directly. */
12662 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12663 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12664 /* If the token is not a `{', then we must take special action. */
12667 /* Create a compound-statement. */
12668 statement
= begin_compound_stmt (0);
12669 /* Parse the dependent-statement. */
12670 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12671 &body_loc_after_labels
);
12672 /* Finish the dummy compound-statement. */
12673 finish_compound_stmt (statement
);
12676 token_indent_info next_tinfo
12677 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12678 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12680 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12681 && next_tinfo
.type
!= CPP_SEMICOLON
)
12682 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12683 guard_tinfo
.location
, guard_tinfo
.keyword
);
12685 /* Return the statement. */
12689 /* For some dependent statements (like `while (cond) statement'), we
12690 have already created a scope. Therefore, even if the dependent
12691 statement is a compound-statement, we do not want to create another
12695 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12696 const token_indent_info
&guard_tinfo
)
12698 /* If the token is a `{', then we must take special action. */
12699 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12701 token_indent_info body_tinfo
12702 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12703 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12705 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12706 &loc_after_labels
);
12707 token_indent_info next_tinfo
12708 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12709 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12711 if (loc_after_labels
!= UNKNOWN_LOCATION
12712 && next_tinfo
.type
!= CPP_SEMICOLON
)
12713 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12714 guard_tinfo
.location
,
12715 guard_tinfo
.keyword
);
12719 /* Avoid calling cp_parser_compound_statement, so that we
12720 don't create a new scope. Do everything else by hand. */
12721 matching_braces braces
;
12722 braces
.require_open (parser
);
12723 /* If the next keyword is `__label__' we have a label declaration. */
12724 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12725 cp_parser_label_declaration (parser
);
12726 /* Parse an (optional) statement-seq. */
12727 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12728 braces
.require_close (parser
);
12732 /* Declarations [gram.dcl.dcl] */
12734 /* Parse an optional declaration-sequence.
12738 declaration-seq declaration */
12741 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12747 token
= cp_lexer_peek_token (parser
->lexer
);
12749 if (token
->type
== CPP_CLOSE_BRACE
12750 || token
->type
== CPP_EOF
12751 || token
->type
== CPP_PRAGMA_EOL
)
12754 if (token
->type
== CPP_SEMICOLON
)
12756 /* A declaration consisting of a single semicolon is
12757 invalid. Allow it unless we're being pedantic. */
12758 cp_lexer_consume_token (parser
->lexer
);
12759 if (!in_system_header_at (input_location
))
12760 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12764 /* If we're entering or exiting a region that's implicitly
12765 extern "C", modify the lang context appropriately. */
12766 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12768 push_lang_context (lang_name_c
);
12769 parser
->implicit_extern_c
= true;
12771 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12773 pop_lang_context ();
12774 parser
->implicit_extern_c
= false;
12777 if (token
->type
== CPP_PRAGMA
)
12779 /* A top-level declaration can consist solely of a #pragma.
12780 A nested declaration cannot, so this is done here and not
12781 in cp_parser_declaration. (A #pragma at block scope is
12782 handled in cp_parser_statement.) */
12783 cp_parser_pragma (parser
, pragma_external
, NULL
);
12787 /* Parse the declaration itself. */
12788 cp_parser_declaration (parser
);
12792 /* Parse a declaration.
12796 function-definition
12797 template-declaration
12798 explicit-instantiation
12799 explicit-specialization
12800 linkage-specification
12801 namespace-definition
12809 __extension__ declaration */
12812 cp_parser_declaration (cp_parser
* parser
)
12816 int saved_pedantic
;
12818 tree attributes
= NULL_TREE
;
12820 /* Check for the `__extension__' keyword. */
12821 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12823 /* Parse the qualified declaration. */
12824 cp_parser_declaration (parser
);
12825 /* Restore the PEDANTIC flag. */
12826 pedantic
= saved_pedantic
;
12831 /* Try to figure out what kind of declaration is present. */
12832 token1
= *cp_lexer_peek_token (parser
->lexer
);
12834 if (token1
.type
!= CPP_EOF
)
12835 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12838 token2
.type
= CPP_EOF
;
12839 token2
.keyword
= RID_MAX
;
12842 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12843 p
= obstack_alloc (&declarator_obstack
, 0);
12845 /* If the next token is `extern' and the following token is a string
12846 literal, then we have a linkage specification. */
12847 if (token1
.keyword
== RID_EXTERN
12848 && cp_parser_is_pure_string_literal (&token2
))
12849 cp_parser_linkage_specification (parser
);
12850 /* If the next token is `template', then we have either a template
12851 declaration, an explicit instantiation, or an explicit
12853 else if (token1
.keyword
== RID_TEMPLATE
)
12855 /* `template <>' indicates a template specialization. */
12856 if (token2
.type
== CPP_LESS
12857 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12858 cp_parser_explicit_specialization (parser
);
12859 /* `template <' indicates a template declaration. */
12860 else if (token2
.type
== CPP_LESS
)
12861 cp_parser_template_declaration (parser
, /*member_p=*/false);
12862 /* Anything else must be an explicit instantiation. */
12864 cp_parser_explicit_instantiation (parser
);
12866 /* If the next token is `export', then we have a template
12868 else if (token1
.keyword
== RID_EXPORT
)
12869 cp_parser_template_declaration (parser
, /*member_p=*/false);
12870 /* If the next token is `extern', 'static' or 'inline' and the one
12871 after that is `template', we have a GNU extended explicit
12872 instantiation directive. */
12873 else if (cp_parser_allow_gnu_extensions_p (parser
)
12874 && (token1
.keyword
== RID_EXTERN
12875 || token1
.keyword
== RID_STATIC
12876 || token1
.keyword
== RID_INLINE
)
12877 && token2
.keyword
== RID_TEMPLATE
)
12878 cp_parser_explicit_instantiation (parser
);
12879 /* If the next token is `namespace', check for a named or unnamed
12880 namespace definition. */
12881 else if (token1
.keyword
== RID_NAMESPACE
12882 && (/* A named namespace definition. */
12883 (token2
.type
== CPP_NAME
12884 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12886 || (token2
.type
== CPP_OPEN_SQUARE
12887 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12888 == CPP_OPEN_SQUARE
)
12889 /* An unnamed namespace definition. */
12890 || token2
.type
== CPP_OPEN_BRACE
12891 || token2
.keyword
== RID_ATTRIBUTE
))
12892 cp_parser_namespace_definition (parser
);
12893 /* An inline (associated) namespace definition. */
12894 else if (token1
.keyword
== RID_INLINE
12895 && token2
.keyword
== RID_NAMESPACE
)
12896 cp_parser_namespace_definition (parser
);
12897 /* Objective-C++ declaration/definition. */
12898 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12899 cp_parser_objc_declaration (parser
, NULL_TREE
);
12900 else if (c_dialect_objc ()
12901 && token1
.keyword
== RID_ATTRIBUTE
12902 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12903 cp_parser_objc_declaration (parser
, attributes
);
12904 /* At this point we may have a template declared by a concept
12906 else if (flag_concepts
12907 && cp_parser_template_declaration_after_export (parser
,
12908 /*member_p=*/false))
12911 /* Try to parse a block-declaration, or a function-definition. */
12912 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12914 /* Free any declarators allocated. */
12915 obstack_free (&declarator_obstack
, p
);
12918 /* Parse a block-declaration.
12923 namespace-alias-definition
12930 __extension__ block-declaration
12935 static_assert-declaration
12937 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12938 part of a declaration-statement. */
12941 cp_parser_block_declaration (cp_parser
*parser
,
12945 int saved_pedantic
;
12947 /* Check for the `__extension__' keyword. */
12948 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12950 /* Parse the qualified declaration. */
12951 cp_parser_block_declaration (parser
, statement_p
);
12952 /* Restore the PEDANTIC flag. */
12953 pedantic
= saved_pedantic
;
12958 /* Peek at the next token to figure out which kind of declaration is
12960 token1
= cp_lexer_peek_token (parser
->lexer
);
12962 /* If the next keyword is `asm', we have an asm-definition. */
12963 if (token1
->keyword
== RID_ASM
)
12966 cp_parser_commit_to_tentative_parse (parser
);
12967 cp_parser_asm_definition (parser
);
12969 /* If the next keyword is `namespace', we have a
12970 namespace-alias-definition. */
12971 else if (token1
->keyword
== RID_NAMESPACE
)
12972 cp_parser_namespace_alias_definition (parser
);
12973 /* If the next keyword is `using', we have a
12974 using-declaration, a using-directive, or an alias-declaration. */
12975 else if (token1
->keyword
== RID_USING
)
12980 cp_parser_commit_to_tentative_parse (parser
);
12981 /* If the token after `using' is `namespace', then we have a
12982 using-directive. */
12983 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12984 if (token2
->keyword
== RID_NAMESPACE
)
12985 cp_parser_using_directive (parser
);
12986 /* If the second token after 'using' is '=', then we have an
12987 alias-declaration. */
12988 else if (cxx_dialect
>= cxx11
12989 && token2
->type
== CPP_NAME
12990 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12991 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12992 cp_parser_alias_declaration (parser
);
12993 /* Otherwise, it's a using-declaration. */
12995 cp_parser_using_declaration (parser
,
12996 /*access_declaration_p=*/false);
12998 /* If the next keyword is `__label__' we have a misplaced label
13000 else if (token1
->keyword
== RID_LABEL
)
13002 cp_lexer_consume_token (parser
->lexer
);
13003 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13004 cp_parser_skip_to_end_of_statement (parser
);
13005 /* If the next token is now a `;', consume it. */
13006 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13007 cp_lexer_consume_token (parser
->lexer
);
13009 /* If the next token is `static_assert' we have a static assertion. */
13010 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13011 cp_parser_static_assert (parser
, /*member_p=*/false);
13012 /* Anything else must be a simple-declaration. */
13014 cp_parser_simple_declaration (parser
, !statement_p
,
13015 /*maybe_range_for_decl*/NULL
);
13018 /* Parse a simple-declaration.
13020 simple-declaration:
13021 decl-specifier-seq [opt] init-declarator-list [opt] ;
13022 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13023 brace-or-equal-initializer ;
13025 init-declarator-list:
13027 init-declarator-list , init-declarator
13029 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13030 function-definition as a simple-declaration.
13032 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13033 parsed declaration if it is an uninitialized single declarator not followed
13034 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13035 if present, will not be consumed. */
13038 cp_parser_simple_declaration (cp_parser
* parser
,
13039 bool function_definition_allowed_p
,
13040 tree
*maybe_range_for_decl
)
13042 cp_decl_specifier_seq decl_specifiers
;
13043 int declares_class_or_enum
;
13044 bool saw_declarator
;
13045 location_t comma_loc
= UNKNOWN_LOCATION
;
13046 location_t init_loc
= UNKNOWN_LOCATION
;
13048 if (maybe_range_for_decl
)
13049 *maybe_range_for_decl
= NULL_TREE
;
13051 /* Defer access checks until we know what is being declared; the
13052 checks for names appearing in the decl-specifier-seq should be
13053 done as if we were in the scope of the thing being declared. */
13054 push_deferring_access_checks (dk_deferred
);
13056 /* Parse the decl-specifier-seq. We have to keep track of whether
13057 or not the decl-specifier-seq declares a named class or
13058 enumeration type, since that is the only case in which the
13059 init-declarator-list is allowed to be empty.
13063 In a simple-declaration, the optional init-declarator-list can be
13064 omitted only when declaring a class or enumeration, that is when
13065 the decl-specifier-seq contains either a class-specifier, an
13066 elaborated-type-specifier, or an enum-specifier. */
13067 cp_parser_decl_specifier_seq (parser
,
13068 CP_PARSER_FLAGS_OPTIONAL
,
13070 &declares_class_or_enum
);
13071 /* We no longer need to defer access checks. */
13072 stop_deferring_access_checks ();
13074 /* In a block scope, a valid declaration must always have a
13075 decl-specifier-seq. By not trying to parse declarators, we can
13076 resolve the declaration/expression ambiguity more quickly. */
13077 if (!function_definition_allowed_p
13078 && !decl_specifiers
.any_specifiers_p
)
13080 cp_parser_error (parser
, "expected declaration");
13084 /* If the next two tokens are both identifiers, the code is
13085 erroneous. The usual cause of this situation is code like:
13089 where "T" should name a type -- but does not. */
13090 if (!decl_specifiers
.any_type_specifiers_p
13091 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13093 /* If parsing tentatively, we should commit; we really are
13094 looking at a declaration. */
13095 cp_parser_commit_to_tentative_parse (parser
);
13100 cp_parser_maybe_commit_to_declaration (parser
,
13101 decl_specifiers
.any_specifiers_p
);
13103 /* Look for C++17 decomposition declaration. */
13104 for (size_t n
= 1; ; n
++)
13105 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13106 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13108 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13109 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13110 && decl_specifiers
.any_specifiers_p
)
13113 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13114 maybe_range_for_decl
,
13117 /* The next token should be either a `,' or a `;'. */
13118 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13119 /* If it's a `;', we are done. */
13120 if (token
->type
== CPP_SEMICOLON
)
13122 else if (maybe_range_for_decl
)
13124 if (*maybe_range_for_decl
== NULL_TREE
)
13125 *maybe_range_for_decl
= error_mark_node
;
13128 /* Anything else is an error. */
13131 /* If we have already issued an error message we don't need
13132 to issue another one. */
13133 if ((decl
!= error_mark_node
13134 && DECL_INITIAL (decl
) != error_mark_node
)
13135 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13136 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13137 /* Skip tokens until we reach the end of the statement. */
13138 cp_parser_skip_to_end_of_statement (parser
);
13139 /* If the next token is now a `;', consume it. */
13140 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13141 cp_lexer_consume_token (parser
->lexer
);
13149 bool auto_specifier_p
;
13150 /* NULL_TREE if both variable and function declaration are allowed,
13151 error_mark_node if function declaration are not allowed and
13152 a FUNCTION_DECL that should be diagnosed if it is followed by
13153 variable declarations. */
13154 tree auto_function_declaration
;
13156 last_type
= NULL_TREE
;
13158 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13159 auto_function_declaration
= NULL_TREE
;
13161 /* Keep going until we hit the `;' at the end of the simple
13163 saw_declarator
= false;
13164 while (cp_lexer_next_token_is_not (parser
->lexer
,
13168 bool function_definition_p
;
13170 tree auto_result
= NULL_TREE
;
13172 if (saw_declarator
)
13174 /* If we are processing next declarator, comma is expected */
13175 token
= cp_lexer_peek_token (parser
->lexer
);
13176 gcc_assert (token
->type
== CPP_COMMA
);
13177 cp_lexer_consume_token (parser
->lexer
);
13178 if (maybe_range_for_decl
)
13180 *maybe_range_for_decl
= error_mark_node
;
13181 if (comma_loc
== UNKNOWN_LOCATION
)
13182 comma_loc
= token
->location
;
13186 saw_declarator
= true;
13188 /* Parse the init-declarator. */
13189 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13191 function_definition_allowed_p
,
13192 /*member_p=*/false,
13193 declares_class_or_enum
,
13194 &function_definition_p
,
13195 maybe_range_for_decl
,
13198 /* If an error occurred while parsing tentatively, exit quickly.
13199 (That usually happens when in the body of a function; each
13200 statement is treated as a declaration-statement until proven
13202 if (cp_parser_error_occurred (parser
))
13205 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13207 /* If the init-declarator-list contains more than one
13208 init-declarator, they shall all form declarations of
13210 if (auto_function_declaration
== NULL_TREE
)
13211 auto_function_declaration
13212 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13213 else if (TREE_CODE (decl
) == FUNCTION_DECL
13214 || auto_function_declaration
!= error_mark_node
)
13216 error_at (decl_specifiers
.locations
[ds_type_spec
],
13217 "non-variable %qD in declaration with more than one "
13218 "declarator with placeholder type",
13219 TREE_CODE (decl
) == FUNCTION_DECL
13220 ? decl
: auto_function_declaration
);
13221 auto_function_declaration
= error_mark_node
;
13226 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13229 && last_type
!= error_mark_node
13230 && !same_type_p (auto_result
, last_type
))
13232 /* If the list of declarators contains more than one declarator,
13233 the type of each declared variable is determined as described
13234 above. If the type deduced for the template parameter U is not
13235 the same in each deduction, the program is ill-formed. */
13236 error_at (decl_specifiers
.locations
[ds_type_spec
],
13237 "inconsistent deduction for %qT: %qT and then %qT",
13238 decl_specifiers
.type
, last_type
, auto_result
);
13239 last_type
= error_mark_node
;
13242 last_type
= auto_result
;
13245 /* Handle function definitions specially. */
13246 if (function_definition_p
)
13248 /* If the next token is a `,', then we are probably
13249 processing something like:
13253 which is erroneous. */
13254 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13256 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13257 error_at (token
->location
,
13259 " declarations and function-definitions is forbidden");
13261 /* Otherwise, we're done with the list of declarators. */
13264 pop_deferring_access_checks ();
13268 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13269 *maybe_range_for_decl
= decl
;
13270 /* The next token should be either a `,' or a `;'. */
13271 token
= cp_lexer_peek_token (parser
->lexer
);
13272 /* If it's a `,', there are more declarators to come. */
13273 if (token
->type
== CPP_COMMA
)
13274 /* will be consumed next time around */;
13275 /* If it's a `;', we are done. */
13276 else if (token
->type
== CPP_SEMICOLON
)
13278 else if (maybe_range_for_decl
)
13280 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13281 permerror (decl_specifiers
.locations
[ds_type_spec
],
13282 "types may not be defined in a for-range-declaration");
13285 /* Anything else is an error. */
13288 /* If we have already issued an error message we don't need
13289 to issue another one. */
13290 if ((decl
!= error_mark_node
13291 && DECL_INITIAL (decl
) != error_mark_node
)
13292 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13293 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13294 /* Skip tokens until we reach the end of the statement. */
13295 cp_parser_skip_to_end_of_statement (parser
);
13296 /* If the next token is now a `;', consume it. */
13297 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13298 cp_lexer_consume_token (parser
->lexer
);
13301 /* After the first time around, a function-definition is not
13302 allowed -- even if it was OK at first. For example:
13307 function_definition_allowed_p
= false;
13310 /* Issue an error message if no declarators are present, and the
13311 decl-specifier-seq does not itself declare a class or
13312 enumeration: [dcl.dcl]/3. */
13313 if (!saw_declarator
)
13315 if (cp_parser_declares_only_class_p (parser
))
13317 if (!declares_class_or_enum
13318 && decl_specifiers
.type
13319 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13320 /* Ensure an error is issued anyway when finish_decltype_type,
13321 called via cp_parser_decl_specifier_seq, returns a class or
13322 an enumeration (c++/51786). */
13323 decl_specifiers
.type
= NULL_TREE
;
13324 shadow_tag (&decl_specifiers
);
13326 /* Perform any deferred access checks. */
13327 perform_deferred_access_checks (tf_warning_or_error
);
13330 /* Consume the `;'. */
13332 if (!maybe_range_for_decl
)
13333 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13334 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13336 if (init_loc
!= UNKNOWN_LOCATION
)
13337 error_at (init_loc
, "initializer in range-based %<for%> loop");
13338 if (comma_loc
!= UNKNOWN_LOCATION
)
13339 error_at (comma_loc
,
13340 "multiple declarations in range-based %<for%> loop");
13344 pop_deferring_access_checks ();
13347 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13348 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13352 cp_parser_decomposition_declaration (cp_parser
*parser
,
13353 cp_decl_specifier_seq
*decl_specifiers
,
13354 tree
*maybe_range_for_decl
,
13355 location_t
*init_loc
)
13357 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13358 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13359 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13361 /* Parse the identifier-list. */
13362 auto_vec
<cp_expr
, 10> v
;
13363 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13366 cp_expr e
= cp_parser_identifier (parser
);
13367 if (e
.get_value () == error_mark_node
)
13370 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13372 cp_lexer_consume_token (parser
->lexer
);
13375 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13376 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13378 end_loc
= UNKNOWN_LOCATION
;
13379 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13382 cp_lexer_consume_token (parser
->lexer
);
13385 cp_parser_skip_to_end_of_statement (parser
);
13386 return error_mark_node
;
13390 if (cxx_dialect
< cxx17
)
13391 pedwarn (loc
, 0, "structured bindings only available with "
13392 "-std=c++17 or -std=gnu++17");
13395 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13396 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13397 declarator
->id_loc
= loc
;
13398 if (ref_qual
!= REF_QUAL_NONE
)
13399 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13400 ref_qual
== REF_QUAL_RVALUE
,
13402 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13403 NULL_TREE
, decl_specifiers
->attributes
,
13405 tree orig_decl
= decl
;
13409 cp_decl_specifier_seq decl_specs
;
13410 clear_decl_specs (&decl_specs
);
13411 decl_specs
.type
= make_auto ();
13413 FOR_EACH_VEC_ELT (v
, i
, e
)
13416 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13418 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13419 declarator
->id_loc
= e
.get_location ();
13420 tree elt_pushed_scope
;
13421 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13422 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13423 if (decl2
== error_mark_node
)
13424 decl
= error_mark_node
;
13425 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13427 /* Ensure we've diagnosed redeclaration if we aren't creating
13429 gcc_assert (errorcount
);
13430 decl
= error_mark_node
;
13434 if (elt_pushed_scope
)
13435 pop_scope (elt_pushed_scope
);
13440 error_at (loc
, "empty structured binding declaration");
13441 decl
= error_mark_node
;
13444 if (maybe_range_for_decl
== NULL
13445 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13447 bool non_constant_p
= false, is_direct_init
= false;
13448 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13449 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13451 if (initializer
== NULL_TREE
13452 || (TREE_CODE (initializer
) == TREE_LIST
13453 && TREE_CHAIN (initializer
))
13455 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13456 && CONSTRUCTOR_NELTS (initializer
) != 1))
13458 error_at (loc
, "invalid initializer for structured binding "
13460 initializer
= error_mark_node
;
13463 if (decl
!= error_mark_node
)
13465 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13466 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13467 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13468 cp_finish_decomp (decl
, prev
, v
.length ());
13471 else if (decl
!= error_mark_node
)
13473 *maybe_range_for_decl
= prev
;
13474 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13475 the underlying DECL. */
13476 cp_finish_decomp (decl
, prev
, v
.length ());
13480 pop_scope (pushed_scope
);
13482 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13484 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13485 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13491 /* Parse a decl-specifier-seq.
13493 decl-specifier-seq:
13494 decl-specifier-seq [opt] decl-specifier
13495 decl-specifier attribute-specifier-seq [opt] (C++11)
13498 storage-class-specifier
13509 Concepts Extension:
13514 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13516 The parser flags FLAGS is used to control type-specifier parsing.
13518 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13521 1: one of the decl-specifiers is an elaborated-type-specifier
13522 (i.e., a type declaration)
13523 2: one of the decl-specifiers is an enum-specifier or a
13524 class-specifier (i.e., a type definition)
13529 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13530 cp_parser_flags flags
,
13531 cp_decl_specifier_seq
*decl_specs
,
13532 int* declares_class_or_enum
)
13534 bool constructor_possible_p
= !parser
->in_declarator_p
;
13535 bool found_decl_spec
= false;
13536 cp_token
*start_token
= NULL
;
13539 /* Clear DECL_SPECS. */
13540 clear_decl_specs (decl_specs
);
13542 /* Assume no class or enumeration type is declared. */
13543 *declares_class_or_enum
= 0;
13545 /* Keep reading specifiers until there are no more to read. */
13548 bool constructor_p
;
13552 /* Peek at the next token. */
13553 token
= cp_lexer_peek_token (parser
->lexer
);
13555 /* Save the first token of the decl spec list for error
13558 start_token
= token
;
13559 /* Handle attributes. */
13560 if (cp_next_tokens_can_be_attribute_p (parser
))
13562 /* Parse the attributes. */
13563 tree attrs
= cp_parser_attributes_opt (parser
);
13565 /* In a sequence of declaration specifiers, c++11 attributes
13566 appertain to the type that precede them. In that case
13569 The attribute-specifier-seq affects the type only for
13570 the declaration it appears in, not other declarations
13571 involving the same type.
13573 But for now let's force the user to position the
13574 attribute either at the beginning of the declaration or
13575 after the declarator-id, which would clearly mean that it
13576 applies to the declarator. */
13577 if (cxx11_attribute_p (attrs
))
13579 if (!found_decl_spec
)
13580 /* The c++11 attribute is at the beginning of the
13581 declaration. It appertains to the entity being
13585 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13587 /* This is an attribute following a
13588 class-specifier. */
13589 if (decl_specs
->type_definition_p
)
13590 warn_misplaced_attr_for_class_type (token
->location
,
13596 decl_specs
->std_attributes
13597 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13598 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13599 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13605 decl_specs
->attributes
13606 = attr_chainon (decl_specs
->attributes
, attrs
);
13607 if (decl_specs
->locations
[ds_attribute
] == 0)
13608 decl_specs
->locations
[ds_attribute
] = token
->location
;
13611 /* Assume we will find a decl-specifier keyword. */
13612 found_decl_spec
= true;
13613 /* If the next token is an appropriate keyword, we can simply
13614 add it to the list. */
13615 switch (token
->keyword
)
13621 if (!at_class_scope_p ())
13623 gcc_rich_location
richloc (token
->location
);
13624 richloc
.add_fixit_remove ();
13625 error_at (&richloc
, "%<friend%> used outside of class");
13626 cp_lexer_purge_token (parser
->lexer
);
13631 /* Consume the token. */
13632 cp_lexer_consume_token (parser
->lexer
);
13636 case RID_CONSTEXPR
:
13638 cp_lexer_consume_token (parser
->lexer
);
13643 cp_lexer_consume_token (parser
->lexer
);
13646 /* function-specifier:
13653 cp_parser_function_specifier_opt (parser
, decl_specs
);
13660 /* Consume the token. */
13661 cp_lexer_consume_token (parser
->lexer
);
13662 /* A constructor declarator cannot appear in a typedef. */
13663 constructor_possible_p
= false;
13664 /* The "typedef" keyword can only occur in a declaration; we
13665 may as well commit at this point. */
13666 cp_parser_commit_to_tentative_parse (parser
);
13668 if (decl_specs
->storage_class
!= sc_none
)
13669 decl_specs
->conflicting_specifiers_p
= true;
13672 /* storage-class-specifier:
13682 if (cxx_dialect
== cxx98
)
13684 /* Consume the token. */
13685 cp_lexer_consume_token (parser
->lexer
);
13687 /* Complain about `auto' as a storage specifier, if
13688 we're complaining about C++0x compatibility. */
13689 gcc_rich_location
richloc (token
->location
);
13690 richloc
.add_fixit_remove ();
13691 warning_at (&richloc
, OPT_Wc__11_compat
,
13692 "%<auto%> changes meaning in C++11; "
13693 "please remove it");
13695 /* Set the storage class anyway. */
13696 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13700 /* C++0x auto type-specifier. */
13701 found_decl_spec
= false;
13708 /* Consume the token. */
13709 cp_lexer_consume_token (parser
->lexer
);
13710 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13714 /* Consume the token. */
13716 cp_lexer_consume_token (parser
->lexer
);
13720 /* We did not yet find a decl-specifier yet. */
13721 found_decl_spec
= false;
13725 if (found_decl_spec
13726 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13727 && token
->keyword
!= RID_CONSTEXPR
)
13728 error ("decl-specifier invalid in condition");
13730 if (found_decl_spec
13731 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13732 && token
->keyword
!= RID_MUTABLE
13733 && token
->keyword
!= RID_CONSTEXPR
)
13734 error_at (token
->location
, "%qD invalid in lambda",
13735 ridpointers
[token
->keyword
]);
13738 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13740 /* Constructors are a special case. The `S' in `S()' is not a
13741 decl-specifier; it is the beginning of the declarator. */
13743 = (!found_decl_spec
13744 && constructor_possible_p
13745 && (cp_parser_constructor_declarator_p
13746 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13748 /* If we don't have a DECL_SPEC yet, then we must be looking at
13749 a type-specifier. */
13750 if (!found_decl_spec
&& !constructor_p
)
13752 int decl_spec_declares_class_or_enum
;
13753 bool is_cv_qualifier
;
13757 = cp_parser_type_specifier (parser
, flags
,
13759 /*is_declaration=*/true,
13760 &decl_spec_declares_class_or_enum
,
13762 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13764 /* If this type-specifier referenced a user-defined type
13765 (a typedef, class-name, etc.), then we can't allow any
13766 more such type-specifiers henceforth.
13770 The longest sequence of decl-specifiers that could
13771 possibly be a type name is taken as the
13772 decl-specifier-seq of a declaration. The sequence shall
13773 be self-consistent as described below.
13777 As a general rule, at most one type-specifier is allowed
13778 in the complete decl-specifier-seq of a declaration. The
13779 only exceptions are the following:
13781 -- const or volatile can be combined with any other
13784 -- signed or unsigned can be combined with char, long,
13792 void g (const int Pc);
13794 Here, Pc is *not* part of the decl-specifier seq; it's
13795 the declarator. Therefore, once we see a type-specifier
13796 (other than a cv-qualifier), we forbid any additional
13797 user-defined types. We *do* still allow things like `int
13798 int' to be considered a decl-specifier-seq, and issue the
13799 error message later. */
13800 if (type_spec
&& !is_cv_qualifier
)
13801 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13802 /* A constructor declarator cannot follow a type-specifier. */
13805 constructor_possible_p
= false;
13806 found_decl_spec
= true;
13807 if (!is_cv_qualifier
)
13808 decl_specs
->any_type_specifiers_p
= true;
13810 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
13811 error_at (token
->location
, "type-specifier invalid in lambda");
13815 /* If we still do not have a DECL_SPEC, then there are no more
13816 decl-specifiers. */
13817 if (!found_decl_spec
)
13820 decl_specs
->any_specifiers_p
= true;
13821 /* After we see one decl-specifier, further decl-specifiers are
13822 always optional. */
13823 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13826 /* Don't allow a friend specifier with a class definition. */
13827 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13828 && (*declares_class_or_enum
& 2))
13829 error_at (decl_specs
->locations
[ds_friend
],
13830 "class definition may not be declared a friend");
13833 /* Parse an (optional) storage-class-specifier.
13835 storage-class-specifier:
13844 storage-class-specifier:
13847 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13850 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13852 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13855 if (cxx_dialect
!= cxx98
)
13857 /* Fall through for C++98. */
13858 gcc_fallthrough ();
13865 /* Consume the token. */
13866 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13873 /* Parse an (optional) function-specifier.
13875 function-specifier:
13880 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13881 Updates DECL_SPECS, if it is non-NULL. */
13884 cp_parser_function_specifier_opt (cp_parser
* parser
,
13885 cp_decl_specifier_seq
*decl_specs
)
13887 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13888 switch (token
->keyword
)
13891 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13895 /* 14.5.2.3 [temp.mem]
13897 A member function template shall not be virtual. */
13898 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13899 && current_class_type
)
13900 error_at (token
->location
, "templates may not be %<virtual%>");
13902 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13906 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13913 /* Consume the token. */
13914 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13917 /* Parse a linkage-specification.
13919 linkage-specification:
13920 extern string-literal { declaration-seq [opt] }
13921 extern string-literal declaration */
13924 cp_parser_linkage_specification (cp_parser
* parser
)
13928 /* Look for the `extern' keyword. */
13929 cp_token
*extern_token
13930 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13932 /* Look for the string-literal. */
13933 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13934 linkage
= cp_parser_string_literal (parser
, false, false);
13936 /* Transform the literal into an identifier. If the literal is a
13937 wide-character string, or contains embedded NULs, then we can't
13938 handle it as the user wants. */
13939 if (strlen (TREE_STRING_POINTER (linkage
))
13940 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13942 cp_parser_error (parser
, "invalid linkage-specification");
13943 /* Assume C++ linkage. */
13944 linkage
= lang_name_cplusplus
;
13947 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13949 /* We're now using the new linkage. */
13950 push_lang_context (linkage
);
13952 /* Preserve the location of the the innermost linkage specification,
13953 tracking the locations of nested specifications via a local. */
13954 location_t saved_location
13955 = parser
->innermost_linkage_specification_location
;
13956 /* Construct a location ranging from the start of the "extern" to
13957 the end of the string-literal, with the caret at the start, e.g.:
13961 parser
->innermost_linkage_specification_location
13962 = make_location (extern_token
->location
,
13963 extern_token
->location
,
13964 get_finish (string_token
->location
));
13966 /* If the next token is a `{', then we're using the first
13968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13970 cp_ensure_no_omp_declare_simd (parser
);
13971 cp_ensure_no_oacc_routine (parser
);
13973 /* Consume the `{' token. */
13974 matching_braces braces
;
13975 braces
.consume_open (parser
)->location
;
13976 /* Parse the declarations. */
13977 cp_parser_declaration_seq_opt (parser
);
13978 /* Look for the closing `}'. */
13979 braces
.require_close (parser
);
13981 /* Otherwise, there's just one declaration. */
13984 bool saved_in_unbraced_linkage_specification_p
;
13986 saved_in_unbraced_linkage_specification_p
13987 = parser
->in_unbraced_linkage_specification_p
;
13988 parser
->in_unbraced_linkage_specification_p
= true;
13989 cp_parser_declaration (parser
);
13990 parser
->in_unbraced_linkage_specification_p
13991 = saved_in_unbraced_linkage_specification_p
;
13994 /* We're done with the linkage-specification. */
13995 pop_lang_context ();
13997 /* Restore location of parent linkage specification, if any. */
13998 parser
->innermost_linkage_specification_location
= saved_location
;
14001 /* Parse a static_assert-declaration.
14003 static_assert-declaration:
14004 static_assert ( constant-expression , string-literal ) ;
14005 static_assert ( constant-expression ) ; (C++17)
14007 If MEMBER_P, this static_assert is a class member. */
14010 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14013 location_t token_loc
;
14017 /* Peek at the `static_assert' token so we can keep track of exactly
14018 where the static assertion started. */
14019 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14021 /* Look for the `static_assert' keyword. */
14022 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14026 /* We know we are in a static assertion; commit to any tentative
14028 if (cp_parser_parsing_tentatively (parser
))
14029 cp_parser_commit_to_tentative_parse (parser
);
14031 /* Parse the `(' starting the static assertion condition. */
14032 matching_parens parens
;
14033 parens
.require_open (parser
);
14035 /* Parse the constant-expression. Allow a non-constant expression
14036 here in order to give better diagnostics in finish_static_assert. */
14038 cp_parser_constant_expression (parser
,
14039 /*allow_non_constant_p=*/true,
14040 /*non_constant_p=*/&dummy
);
14042 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14044 if (cxx_dialect
< cxx17
)
14045 pedwarn (input_location
, OPT_Wpedantic
,
14046 "static_assert without a message "
14047 "only available with -std=c++17 or -std=gnu++17");
14049 cp_lexer_consume_token (parser
->lexer
);
14050 message
= build_string (1, "");
14051 TREE_TYPE (message
) = char_array_type_node
;
14052 fix_string_type (message
);
14056 /* Parse the separating `,'. */
14057 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14059 /* Parse the string-literal message. */
14060 message
= cp_parser_string_literal (parser
,
14061 /*translate=*/false,
14064 /* A `)' completes the static assertion. */
14065 if (!parens
.require_close (parser
))
14066 cp_parser_skip_to_closing_parenthesis (parser
,
14067 /*recovering=*/true,
14068 /*or_comma=*/false,
14069 /*consume_paren=*/true);
14072 /* A semicolon terminates the declaration. */
14073 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14075 /* Get the location for the static assertion. Use that of the
14076 condition if available, otherwise, use that of the "static_assert"
14078 location_t assert_loc
= condition
.get_location ();
14079 if (assert_loc
== UNKNOWN_LOCATION
)
14080 assert_loc
= token_loc
;
14082 /* Complete the static assertion, which may mean either processing
14083 the static assert now or saving it for template instantiation. */
14084 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14087 /* Parse the expression in decltype ( expression ). */
14090 cp_parser_decltype_expr (cp_parser
*parser
,
14091 bool &id_expression_or_member_access_p
)
14093 cp_token
*id_expr_start_token
;
14096 /* Since we're going to preserve any side-effects from this parse, set up a
14097 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14098 in the expression. */
14099 tentative_firewall
firewall (parser
);
14101 /* First, try parsing an id-expression. */
14102 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14103 cp_parser_parse_tentatively (parser
);
14104 expr
= cp_parser_id_expression (parser
,
14105 /*template_keyword_p=*/false,
14106 /*check_dependency_p=*/true,
14107 /*template_p=*/NULL
,
14108 /*declarator_p=*/false,
14109 /*optional_p=*/false);
14111 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14113 bool non_integral_constant_expression_p
= false;
14114 tree id_expression
= expr
;
14116 const char *error_msg
;
14118 if (identifier_p (expr
))
14119 /* Lookup the name we got back from the id-expression. */
14120 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14121 id_expr_start_token
->location
);
14123 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14124 /* A template without args is not a complete id-expression. */
14125 expr
= error_mark_node
;
14128 && expr
!= error_mark_node
14129 && TREE_CODE (expr
) != TYPE_DECL
14130 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14131 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14132 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14134 /* Complete lookup of the id-expression. */
14135 expr
= (finish_id_expression
14136 (id_expression
, expr
, parser
->scope
, &idk
,
14137 /*integral_constant_expression_p=*/false,
14138 /*allow_non_integral_constant_expression_p=*/true,
14139 &non_integral_constant_expression_p
,
14140 /*template_p=*/false,
14142 /*address_p=*/false,
14143 /*template_arg_p=*/false,
14145 id_expr_start_token
->location
));
14147 if (expr
== error_mark_node
)
14148 /* We found an id-expression, but it was something that we
14149 should not have found. This is an error, not something
14150 we can recover from, so note that we found an
14151 id-expression and we'll recover as gracefully as
14153 id_expression_or_member_access_p
= true;
14157 && expr
!= error_mark_node
14158 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14159 /* We have an id-expression. */
14160 id_expression_or_member_access_p
= true;
14163 if (!id_expression_or_member_access_p
)
14165 /* Abort the id-expression parse. */
14166 cp_parser_abort_tentative_parse (parser
);
14168 /* Parsing tentatively, again. */
14169 cp_parser_parse_tentatively (parser
);
14171 /* Parse a class member access. */
14172 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14173 /*cast_p=*/false, /*decltype*/true,
14174 /*member_access_only_p=*/true, NULL
);
14177 && expr
!= error_mark_node
14178 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14179 /* We have an id-expression. */
14180 id_expression_or_member_access_p
= true;
14183 if (id_expression_or_member_access_p
)
14184 /* We have parsed the complete id-expression or member access. */
14185 cp_parser_parse_definitely (parser
);
14188 /* Abort our attempt to parse an id-expression or member access
14190 cp_parser_abort_tentative_parse (parser
);
14192 /* Commit to the tentative_firewall so we get syntax errors. */
14193 cp_parser_commit_to_tentative_parse (parser
);
14195 /* Parse a full expression. */
14196 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14197 /*decltype_p=*/true);
14203 /* Parse a `decltype' type. Returns the type.
14205 simple-type-specifier:
14206 decltype ( expression )
14208 decltype ( auto ) */
14211 cp_parser_decltype (cp_parser
*parser
)
14213 bool id_expression_or_member_access_p
= false;
14214 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14216 if (start_token
->type
== CPP_DECLTYPE
)
14218 /* Already parsed. */
14219 cp_lexer_consume_token (parser
->lexer
);
14220 return saved_checks_value (start_token
->u
.tree_check_value
);
14223 /* Look for the `decltype' token. */
14224 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14225 return error_mark_node
;
14227 /* Parse the opening `('. */
14228 matching_parens parens
;
14229 if (!parens
.require_open (parser
))
14230 return error_mark_node
;
14232 push_deferring_access_checks (dk_deferred
);
14234 tree expr
= NULL_TREE
;
14236 if (cxx_dialect
>= cxx14
14237 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14238 /* decltype (auto) */
14239 cp_lexer_consume_token (parser
->lexer
);
14242 /* decltype (expression) */
14244 /* Types cannot be defined in a `decltype' expression. Save away the
14245 old message and set the new one. */
14246 const char *saved_message
= parser
->type_definition_forbidden_message
;
14247 parser
->type_definition_forbidden_message
14248 = G_("types may not be defined in %<decltype%> expressions");
14250 /* The restrictions on constant-expressions do not apply inside
14251 decltype expressions. */
14252 bool saved_integral_constant_expression_p
14253 = parser
->integral_constant_expression_p
;
14254 bool saved_non_integral_constant_expression_p
14255 = parser
->non_integral_constant_expression_p
;
14256 parser
->integral_constant_expression_p
= false;
14258 /* Within a parenthesized expression, a `>' token is always
14259 the greater-than operator. */
14260 bool saved_greater_than_is_operator_p
14261 = parser
->greater_than_is_operator_p
;
14262 parser
->greater_than_is_operator_p
= true;
14264 /* Do not actually evaluate the expression. */
14265 ++cp_unevaluated_operand
;
14267 /* Do not warn about problems with the expression. */
14268 ++c_inhibit_evaluation_warnings
;
14270 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14272 /* Go back to evaluating expressions. */
14273 --cp_unevaluated_operand
;
14274 --c_inhibit_evaluation_warnings
;
14276 /* The `>' token might be the end of a template-id or
14277 template-parameter-list now. */
14278 parser
->greater_than_is_operator_p
14279 = saved_greater_than_is_operator_p
;
14281 /* Restore the old message and the integral constant expression
14283 parser
->type_definition_forbidden_message
= saved_message
;
14284 parser
->integral_constant_expression_p
14285 = saved_integral_constant_expression_p
;
14286 parser
->non_integral_constant_expression_p
14287 = saved_non_integral_constant_expression_p
;
14290 /* Parse to the closing `)'. */
14291 if (!parens
.require_close (parser
))
14293 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14294 /*consume_paren=*/true);
14295 pop_deferring_access_checks ();
14296 return error_mark_node
;
14302 expr
= make_decltype_auto ();
14303 AUTO_IS_DECLTYPE (expr
) = true;
14306 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14307 tf_warning_or_error
);
14309 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14311 start_token
->type
= CPP_DECLTYPE
;
14312 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14313 start_token
->u
.tree_check_value
->value
= expr
;
14314 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14315 start_token
->keyword
= RID_MAX
;
14316 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14318 pop_to_parent_deferring_access_checks ();
14323 /* Special member functions [gram.special] */
14325 /* Parse a conversion-function-id.
14327 conversion-function-id:
14328 operator conversion-type-id
14330 Returns an IDENTIFIER_NODE representing the operator. */
14333 cp_parser_conversion_function_id (cp_parser
* parser
)
14337 tree saved_qualifying_scope
;
14338 tree saved_object_scope
;
14339 tree pushed_scope
= NULL_TREE
;
14341 /* Look for the `operator' token. */
14342 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14343 return error_mark_node
;
14344 /* When we parse the conversion-type-id, the current scope will be
14345 reset. However, we need that information in able to look up the
14346 conversion function later, so we save it here. */
14347 saved_scope
= parser
->scope
;
14348 saved_qualifying_scope
= parser
->qualifying_scope
;
14349 saved_object_scope
= parser
->object_scope
;
14350 /* We must enter the scope of the class so that the names of
14351 entities declared within the class are available in the
14352 conversion-type-id. For example, consider:
14359 S::operator I() { ... }
14361 In order to see that `I' is a type-name in the definition, we
14362 must be in the scope of `S'. */
14364 pushed_scope
= push_scope (saved_scope
);
14365 /* Parse the conversion-type-id. */
14366 type
= cp_parser_conversion_type_id (parser
);
14367 /* Leave the scope of the class, if any. */
14369 pop_scope (pushed_scope
);
14370 /* Restore the saved scope. */
14371 parser
->scope
= saved_scope
;
14372 parser
->qualifying_scope
= saved_qualifying_scope
;
14373 parser
->object_scope
= saved_object_scope
;
14374 /* If the TYPE is invalid, indicate failure. */
14375 if (type
== error_mark_node
)
14376 return error_mark_node
;
14377 return make_conv_op_name (type
);
14380 /* Parse a conversion-type-id:
14382 conversion-type-id:
14383 type-specifier-seq conversion-declarator [opt]
14385 Returns the TYPE specified. */
14388 cp_parser_conversion_type_id (cp_parser
* parser
)
14391 cp_decl_specifier_seq type_specifiers
;
14392 cp_declarator
*declarator
;
14393 tree type_specified
;
14394 const char *saved_message
;
14396 /* Parse the attributes. */
14397 attributes
= cp_parser_attributes_opt (parser
);
14399 saved_message
= parser
->type_definition_forbidden_message
;
14400 parser
->type_definition_forbidden_message
14401 = G_("types may not be defined in a conversion-type-id");
14403 /* Parse the type-specifiers. */
14404 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14405 /*is_trailing_return=*/false,
14408 parser
->type_definition_forbidden_message
= saved_message
;
14410 /* If that didn't work, stop. */
14411 if (type_specifiers
.type
== error_mark_node
)
14412 return error_mark_node
;
14413 /* Parse the conversion-declarator. */
14414 declarator
= cp_parser_conversion_declarator_opt (parser
);
14416 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14417 /*initialized=*/0, &attributes
);
14419 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14421 /* Don't give this error when parsing tentatively. This happens to
14422 work because we always parse this definitively once. */
14423 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14424 && type_uses_auto (type_specified
))
14426 if (cxx_dialect
< cxx14
)
14428 error ("invalid use of %<auto%> in conversion operator");
14429 return error_mark_node
;
14431 else if (template_parm_scope_p ())
14432 warning (0, "use of %<auto%> in member template "
14433 "conversion operator can never be deduced");
14436 return type_specified
;
14439 /* Parse an (optional) conversion-declarator.
14441 conversion-declarator:
14442 ptr-operator conversion-declarator [opt]
14446 static cp_declarator
*
14447 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14449 enum tree_code code
;
14450 tree class_type
, std_attributes
= NULL_TREE
;
14451 cp_cv_quals cv_quals
;
14453 /* We don't know if there's a ptr-operator next, or not. */
14454 cp_parser_parse_tentatively (parser
);
14455 /* Try the ptr-operator. */
14456 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14458 /* If it worked, look for more conversion-declarators. */
14459 if (cp_parser_parse_definitely (parser
))
14461 cp_declarator
*declarator
;
14463 /* Parse another optional declarator. */
14464 declarator
= cp_parser_conversion_declarator_opt (parser
);
14466 declarator
= cp_parser_make_indirect_declarator
14467 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14475 /* Parse an (optional) ctor-initializer.
14478 : mem-initializer-list */
14481 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14483 /* If the next token is not a `:', then there is no
14484 ctor-initializer. */
14485 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14487 /* Do default initialization of any bases and members. */
14488 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14489 finish_mem_initializers (NULL_TREE
);
14493 /* Consume the `:' token. */
14494 cp_lexer_consume_token (parser
->lexer
);
14495 /* And the mem-initializer-list. */
14496 cp_parser_mem_initializer_list (parser
);
14499 /* Parse a mem-initializer-list.
14501 mem-initializer-list:
14502 mem-initializer ... [opt]
14503 mem-initializer ... [opt] , mem-initializer-list */
14506 cp_parser_mem_initializer_list (cp_parser
* parser
)
14508 tree mem_initializer_list
= NULL_TREE
;
14509 tree target_ctor
= error_mark_node
;
14510 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14512 /* Let the semantic analysis code know that we are starting the
14513 mem-initializer-list. */
14514 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14515 error_at (token
->location
,
14516 "only constructors take member initializers");
14518 /* Loop through the list. */
14521 tree mem_initializer
;
14523 token
= cp_lexer_peek_token (parser
->lexer
);
14524 /* Parse the mem-initializer. */
14525 mem_initializer
= cp_parser_mem_initializer (parser
);
14526 /* If the next token is a `...', we're expanding member initializers. */
14527 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14529 || (mem_initializer
!= error_mark_node
14530 && check_for_bare_parameter_packs (TREE_PURPOSE
14531 (mem_initializer
))))
14533 /* Consume the `...'. */
14535 cp_lexer_consume_token (parser
->lexer
);
14537 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14538 can be expanded but members cannot. */
14539 if (mem_initializer
!= error_mark_node
14540 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14542 error_at (token
->location
,
14543 "cannot expand initializer for member %qD",
14544 TREE_PURPOSE (mem_initializer
));
14545 mem_initializer
= error_mark_node
;
14548 /* Construct the pack expansion type. */
14549 if (mem_initializer
!= error_mark_node
)
14550 mem_initializer
= make_pack_expansion (mem_initializer
);
14552 if (target_ctor
!= error_mark_node
14553 && mem_initializer
!= error_mark_node
)
14555 error ("mem-initializer for %qD follows constructor delegation",
14556 TREE_PURPOSE (mem_initializer
));
14557 mem_initializer
= error_mark_node
;
14559 /* Look for a target constructor. */
14560 if (mem_initializer
!= error_mark_node
14561 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14562 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14564 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14565 if (mem_initializer_list
)
14567 error ("constructor delegation follows mem-initializer for %qD",
14568 TREE_PURPOSE (mem_initializer_list
));
14569 mem_initializer
= error_mark_node
;
14571 target_ctor
= mem_initializer
;
14573 /* Add it to the list, unless it was erroneous. */
14574 if (mem_initializer
!= error_mark_node
)
14576 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14577 mem_initializer_list
= mem_initializer
;
14579 /* If the next token is not a `,', we're done. */
14580 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14582 /* Consume the `,' token. */
14583 cp_lexer_consume_token (parser
->lexer
);
14586 /* Perform semantic analysis. */
14587 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14588 finish_mem_initializers (mem_initializer_list
);
14591 /* Parse a mem-initializer.
14594 mem-initializer-id ( expression-list [opt] )
14595 mem-initializer-id braced-init-list
14600 ( expression-list [opt] )
14602 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14603 class) or FIELD_DECL (for a non-static data member) to initialize;
14604 the TREE_VALUE is the expression-list. An empty initialization
14605 list is represented by void_list_node. */
14608 cp_parser_mem_initializer (cp_parser
* parser
)
14610 tree mem_initializer_id
;
14611 tree expression_list
;
14613 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14615 /* Find out what is being initialized. */
14616 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14618 permerror (token
->location
,
14619 "anachronistic old-style base class initializer");
14620 mem_initializer_id
= NULL_TREE
;
14624 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14625 if (mem_initializer_id
== error_mark_node
)
14626 return mem_initializer_id
;
14628 member
= expand_member_init (mem_initializer_id
);
14629 if (member
&& !DECL_P (member
))
14630 in_base_initializer
= 1;
14632 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14634 bool expr_non_constant_p
;
14635 cp_lexer_set_source_position (parser
->lexer
);
14636 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14637 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14638 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14639 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14643 vec
<tree
, va_gc
> *vec
;
14644 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14646 /*allow_expansion_p=*/true,
14647 /*non_constant_p=*/NULL
);
14649 return error_mark_node
;
14650 expression_list
= build_tree_list_vec (vec
);
14651 release_tree_vector (vec
);
14654 if (expression_list
== error_mark_node
)
14655 return error_mark_node
;
14656 if (!expression_list
)
14657 expression_list
= void_type_node
;
14659 in_base_initializer
= 0;
14661 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14664 /* Parse a mem-initializer-id.
14666 mem-initializer-id:
14667 :: [opt] nested-name-specifier [opt] class-name
14668 decltype-specifier (C++11)
14671 Returns a TYPE indicating the class to be initialized for the first
14672 production (and the second in C++11). Returns an IDENTIFIER_NODE
14673 indicating the data member to be initialized for the last production. */
14676 cp_parser_mem_initializer_id (cp_parser
* parser
)
14678 bool global_scope_p
;
14679 bool nested_name_specifier_p
;
14680 bool template_p
= false;
14683 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14685 /* `typename' is not allowed in this context ([temp.res]). */
14686 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14688 error_at (token
->location
,
14689 "keyword %<typename%> not allowed in this context (a qualified "
14690 "member initializer is implicitly a type)");
14691 cp_lexer_consume_token (parser
->lexer
);
14693 /* Look for the optional `::' operator. */
14695 = (cp_parser_global_scope_opt (parser
,
14696 /*current_scope_valid_p=*/false)
14698 /* Look for the optional nested-name-specifier. The simplest way to
14703 The keyword `typename' is not permitted in a base-specifier or
14704 mem-initializer; in these contexts a qualified name that
14705 depends on a template-parameter is implicitly assumed to be a
14708 is to assume that we have seen the `typename' keyword at this
14710 nested_name_specifier_p
14711 = (cp_parser_nested_name_specifier_opt (parser
,
14712 /*typename_keyword_p=*/true,
14713 /*check_dependency_p=*/true,
14715 /*is_declaration=*/true)
14717 if (nested_name_specifier_p
)
14718 template_p
= cp_parser_optional_template_keyword (parser
);
14719 /* If there is a `::' operator or a nested-name-specifier, then we
14720 are definitely looking for a class-name. */
14721 if (global_scope_p
|| nested_name_specifier_p
)
14722 return cp_parser_class_name (parser
,
14723 /*typename_keyword_p=*/true,
14724 /*template_keyword_p=*/template_p
,
14726 /*check_dependency_p=*/true,
14727 /*class_head_p=*/false,
14728 /*is_declaration=*/true);
14729 /* Otherwise, we could also be looking for an ordinary identifier. */
14730 cp_parser_parse_tentatively (parser
);
14731 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14732 /* Try a decltype-specifier. */
14733 id
= cp_parser_decltype (parser
);
14735 /* Otherwise, try a class-name. */
14736 id
= cp_parser_class_name (parser
,
14737 /*typename_keyword_p=*/true,
14738 /*template_keyword_p=*/false,
14740 /*check_dependency_p=*/true,
14741 /*class_head_p=*/false,
14742 /*is_declaration=*/true);
14743 /* If we found one, we're done. */
14744 if (cp_parser_parse_definitely (parser
))
14746 /* Otherwise, look for an ordinary identifier. */
14747 return cp_parser_identifier (parser
);
14750 /* Overloading [gram.over] */
14752 /* Parse an operator-function-id.
14754 operator-function-id:
14757 Returns an IDENTIFIER_NODE for the operator which is a
14758 human-readable spelling of the identifier, e.g., `operator +'. */
14761 cp_parser_operator_function_id (cp_parser
* parser
)
14763 /* Look for the `operator' keyword. */
14764 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14765 return error_mark_node
;
14766 /* And then the name of the operator itself. */
14767 return cp_parser_operator (parser
);
14770 /* Return an identifier node for a user-defined literal operator.
14771 The suffix identifier is chained to the operator name identifier. */
14774 cp_literal_operator_id (const char* name
)
14777 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14778 + strlen (name
) + 10);
14779 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14780 identifier
= get_identifier (buffer
);
14785 /* Parse an operator.
14788 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14789 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14790 || ++ -- , ->* -> () []
14797 Returns an IDENTIFIER_NODE for the operator which is a
14798 human-readable spelling of the identifier, e.g., `operator +'. */
14801 cp_parser_operator (cp_parser
* parser
)
14803 tree id
= NULL_TREE
;
14807 /* Peek at the next token. */
14808 token
= cp_lexer_peek_token (parser
->lexer
);
14810 location_t start_loc
= token
->location
;
14812 /* Figure out which operator we have. */
14813 enum tree_code op
= ERROR_MARK
;
14814 bool assop
= false;
14815 bool consumed
= false;
14816 switch (token
->type
)
14820 /* The keyword should be either `new' or `delete'. */
14821 if (token
->keyword
== RID_NEW
)
14823 else if (token
->keyword
== RID_DELETE
)
14828 /* Consume the `new' or `delete' token. */
14829 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14831 /* Peek at the next token. */
14832 token
= cp_lexer_peek_token (parser
->lexer
);
14833 /* If it's a `[' token then this is the array variant of the
14835 if (token
->type
== CPP_OPEN_SQUARE
)
14837 /* Consume the `[' token. */
14838 cp_lexer_consume_token (parser
->lexer
);
14839 /* Look for the `]' token. */
14840 if (cp_token
*close_token
14841 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14842 end_loc
= close_token
->location
;
14843 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14845 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14863 op
= TRUNC_DIV_EXPR
;
14867 op
= TRUNC_MOD_EXPR
;
14887 op
= TRUTH_NOT_EXPR
;
14920 op
= TRUNC_DIV_EXPR
;
14925 op
= TRUNC_MOD_EXPR
;
14951 case CPP_LSHIFT_EQ
:
14956 case CPP_RSHIFT_EQ
:
14973 case CPP_GREATER_EQ
:
14978 op
= TRUTH_ANDIF_EXPR
;
14982 op
= TRUTH_ORIF_EXPR
;
14985 case CPP_PLUS_PLUS
:
14986 op
= POSTINCREMENT_EXPR
;
14989 case CPP_MINUS_MINUS
:
14990 op
= PREDECREMENT_EXPR
;
14994 op
= COMPOUND_EXPR
;
14997 case CPP_DEREF_STAR
:
15002 op
= COMPONENT_REF
;
15005 case CPP_OPEN_PAREN
:
15007 /* Consume the `('. */
15008 matching_parens parens
;
15009 parens
.consume_open (parser
);
15010 /* Look for the matching `)'. */
15011 parens
.require_close (parser
);
15017 case CPP_OPEN_SQUARE
:
15018 /* Consume the `['. */
15019 cp_lexer_consume_token (parser
->lexer
);
15020 /* Look for the matching `]'. */
15021 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15026 case CPP_UTF8STRING
:
15027 case CPP_UTF8STRING_USERDEF
:
15034 case CPP_STRING_USERDEF
:
15035 case CPP_WSTRING_USERDEF
:
15036 case CPP_STRING16_USERDEF
:
15037 case CPP_STRING32_USERDEF
:
15039 tree str
, string_tree
;
15042 if (cxx_dialect
== cxx98
)
15043 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15045 /* Consume the string. */
15046 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15047 /*wide_ok=*/true, /*lookup_udlit=*/false);
15048 if (str
== error_mark_node
)
15049 return error_mark_node
;
15050 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15052 string_tree
= USERDEF_LITERAL_VALUE (str
);
15053 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
15058 /* Look for the suffix identifier. */
15059 token
= cp_lexer_peek_token (parser
->lexer
);
15060 if (token
->type
== CPP_NAME
)
15061 id
= cp_parser_identifier (parser
);
15062 else if (token
->type
== CPP_KEYWORD
)
15064 error ("unexpected keyword;"
15065 " remove space between quotes and suffix identifier");
15066 return error_mark_node
;
15070 error ("expected suffix identifier");
15071 return error_mark_node
;
15074 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15075 (TREE_TYPE (TREE_TYPE (string_tree
))));
15076 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15079 error ("expected empty string after %<operator%> keyword");
15080 return error_mark_node
;
15082 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15085 error ("invalid encoding prefix in literal operator");
15086 return error_mark_node
;
15088 if (id
!= error_mark_node
)
15090 const char *name
= IDENTIFIER_POINTER (id
);
15091 id
= cp_literal_operator_id (name
);
15097 /* Anything else is an error. */
15101 /* If we have selected an identifier, we need to consume the
15103 if (op
!= ERROR_MARK
)
15105 id
= ovl_op_identifier (assop
, op
);
15107 cp_lexer_consume_token (parser
->lexer
);
15109 /* Otherwise, no valid operator name was present. */
15112 cp_parser_error (parser
, "expected operator");
15113 id
= error_mark_node
;
15116 return cp_expr (id
, start_loc
);
15119 /* Parse a template-declaration.
15121 template-declaration:
15122 export [opt] template < template-parameter-list > declaration
15124 If MEMBER_P is TRUE, this template-declaration occurs within a
15127 The grammar rule given by the standard isn't correct. What
15128 is really meant is:
15130 template-declaration:
15131 export [opt] template-parameter-list-seq
15132 decl-specifier-seq [opt] init-declarator [opt] ;
15133 export [opt] template-parameter-list-seq
15134 function-definition
15136 template-parameter-list-seq:
15137 template-parameter-list-seq [opt]
15138 template < template-parameter-list >
15140 Concept Extensions:
15142 template-parameter-list-seq:
15143 template < template-parameter-list > requires-clause [opt]
15146 requires logical-or-expression */
15149 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15151 /* Check for `export'. */
15152 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15154 /* Consume the `export' token. */
15155 cp_lexer_consume_token (parser
->lexer
);
15156 /* Warn that we do not support `export'. */
15157 warning (0, "keyword %<export%> not implemented, and will be ignored");
15160 cp_parser_template_declaration_after_export (parser
, member_p
);
15163 /* Parse a template-parameter-list.
15165 template-parameter-list:
15167 template-parameter-list , template-parameter
15169 Returns a TREE_LIST. Each node represents a template parameter.
15170 The nodes are connected via their TREE_CHAINs. */
15173 cp_parser_template_parameter_list (cp_parser
* parser
)
15175 tree parameter_list
= NULL_TREE
;
15177 begin_template_parm_list ();
15179 /* The loop below parses the template parms. We first need to know
15180 the total number of template parms to be able to compute proper
15181 canonical types of each dependent type. So after the loop, when
15182 we know the total number of template parms,
15183 end_template_parm_list computes the proper canonical types and
15184 fixes up the dependent types accordingly. */
15189 bool is_parameter_pack
;
15190 location_t parm_loc
;
15192 /* Parse the template-parameter. */
15193 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15194 parameter
= cp_parser_template_parameter (parser
,
15196 &is_parameter_pack
);
15197 /* Add it to the list. */
15198 if (parameter
!= error_mark_node
)
15199 parameter_list
= process_template_parm (parameter_list
,
15203 is_parameter_pack
);
15206 tree err_parm
= build_tree_list (parameter
, parameter
);
15207 parameter_list
= chainon (parameter_list
, err_parm
);
15210 /* If the next token is not a `,', we're done. */
15211 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15213 /* Otherwise, consume the `,' token. */
15214 cp_lexer_consume_token (parser
->lexer
);
15217 return end_template_parm_list (parameter_list
);
15220 /* Parse a introduction-list.
15223 introduced-parameter
15224 introduction-list , introduced-parameter
15226 introduced-parameter:
15227 ...[opt] identifier
15229 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15230 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15231 WILDCARD_DECL will also have DECL_NAME set and token location in
15232 DECL_SOURCE_LOCATION. */
15235 cp_parser_introduction_list (cp_parser
*parser
)
15237 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15241 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15243 cp_lexer_consume_token (parser
->lexer
);
15245 /* Build placeholder. */
15246 tree parm
= build_nt (WILDCARD_DECL
);
15247 DECL_SOURCE_LOCATION (parm
)
15248 = cp_lexer_peek_token (parser
->lexer
)->location
;
15249 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15250 WILDCARD_PACK_P (parm
) = is_pack
;
15251 vec_safe_push (introduction_vec
, parm
);
15253 /* If the next token is not a `,', we're done. */
15254 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15256 /* Otherwise, consume the `,' token. */
15257 cp_lexer_consume_token (parser
->lexer
);
15260 /* Convert the vec into a TREE_VEC. */
15261 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15264 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15265 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15267 release_tree_vector (introduction_vec
);
15268 return introduction_list
;
15271 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15272 is an abstract declarator. */
15274 static inline cp_declarator
*
15275 get_id_declarator (cp_declarator
*declarator
)
15277 cp_declarator
*d
= declarator
;
15278 while (d
&& d
->kind
!= cdk_id
)
15283 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15284 is an abstract declarator. */
15287 get_unqualified_id (cp_declarator
*declarator
)
15289 declarator
= get_id_declarator (declarator
);
15291 return declarator
->u
.id
.unqualified_name
;
15296 /* Returns true if DECL represents a constrained-parameter. */
15299 is_constrained_parameter (tree decl
)
15302 && TREE_CODE (decl
) == TYPE_DECL
15303 && CONSTRAINED_PARM_CONCEPT (decl
)
15304 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15307 /* Returns true if PARM declares a constrained-parameter. */
15310 is_constrained_parameter (cp_parameter_declarator
*parm
)
15312 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15315 /* Check that the type parameter is only a declarator-id, and that its
15316 type is not cv-qualified. */
15319 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15320 cp_parameter_declarator
*parm
)
15322 if (!parm
->declarator
)
15325 if (parm
->declarator
->kind
!= cdk_id
)
15327 cp_parser_error (parser
, "invalid constrained type parameter");
15331 /* Don't allow cv-qualified type parameters. */
15332 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15333 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15335 cp_parser_error (parser
, "cv-qualified type parameter");
15342 /* Finish parsing/processing a template type parameter and checking
15343 various restrictions. */
15346 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15348 cp_parameter_declarator
* parmdecl
)
15350 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15351 return finish_template_type_parm (class_type_node
, id
);
15353 return error_mark_node
;
15357 finish_constrained_template_template_parm (tree proto
, tree id
)
15359 /* FIXME: This should probably be copied, and we may need to adjust
15360 the template parameter depths. */
15361 tree saved_parms
= current_template_parms
;
15362 begin_template_parm_list ();
15363 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15364 end_template_parm_list ();
15366 tree parm
= finish_template_template_parm (class_type_node
, id
);
15367 current_template_parms
= saved_parms
;
15372 /* Finish parsing/processing a template template parameter by borrowing
15373 the template parameter list from the prototype parameter. */
15376 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15379 cp_parameter_declarator
*parmdecl
)
15381 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15382 return error_mark_node
;
15383 return finish_constrained_template_template_parm (proto
, id
);
15386 /* Create a new non-type template parameter from the given PARM
15390 constrained_non_type_template_parm (bool *is_non_type
,
15391 cp_parameter_declarator
*parm
)
15393 *is_non_type
= true;
15394 cp_declarator
*decl
= parm
->declarator
;
15395 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15396 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15397 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15400 /* Build a constrained template parameter based on the PARMDECL
15401 declarator. The type of PARMDECL is the constrained type, which
15402 refers to the prototype template parameter that ultimately
15403 specifies the type of the declared parameter. */
15406 finish_constrained_parameter (cp_parser
*parser
,
15407 cp_parameter_declarator
*parmdecl
,
15409 bool *is_parameter_pack
)
15411 tree decl
= parmdecl
->decl_specifiers
.type
;
15412 tree id
= get_unqualified_id (parmdecl
->declarator
);
15413 tree def
= parmdecl
->default_argument
;
15414 tree proto
= DECL_INITIAL (decl
);
15416 /* A template parameter constrained by a variadic concept shall also
15417 be declared as a template parameter pack. */
15418 bool is_variadic
= template_parameter_pack_p (proto
);
15419 if (is_variadic
&& !*is_parameter_pack
)
15420 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15422 /* Build the parameter. Return an error if the declarator was invalid. */
15424 if (TREE_CODE (proto
) == TYPE_DECL
)
15425 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15426 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15427 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15430 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15431 if (parm
== error_mark_node
)
15432 return error_mark_node
;
15434 /* Finish the parameter decl and create a node attaching the
15435 default argument and constraint. */
15436 parm
= build_tree_list (def
, parm
);
15437 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15442 /* Returns true if the parsed type actually represents the declaration
15443 of a type template-parameter. */
15446 declares_constrained_type_template_parameter (tree type
)
15448 return (is_constrained_parameter (type
)
15449 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15453 /* Returns true if the parsed type actually represents the declaration of
15454 a template template-parameter. */
15457 declares_constrained_template_template_parameter (tree type
)
15459 return (is_constrained_parameter (type
)
15460 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15463 /* Parse a default argument for a type template-parameter.
15464 Note that diagnostics are handled in cp_parser_template_parameter. */
15467 cp_parser_default_type_template_argument (cp_parser
*parser
)
15469 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15471 /* Consume the `=' token. */
15472 cp_lexer_consume_token (parser
->lexer
);
15474 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15476 /* Parse the default-argument. */
15477 push_deferring_access_checks (dk_no_deferred
);
15478 tree default_argument
= cp_parser_type_id (parser
);
15479 pop_deferring_access_checks ();
15481 if (flag_concepts
&& type_uses_auto (default_argument
))
15483 error_at (token
->location
,
15484 "invalid use of %<auto%> in default template argument");
15485 return error_mark_node
;
15488 return default_argument
;
15491 /* Parse a default argument for a template template-parameter. */
15494 cp_parser_default_template_template_argument (cp_parser
*parser
)
15496 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15500 /* Consume the `='. */
15501 cp_lexer_consume_token (parser
->lexer
);
15502 /* Parse the id-expression. */
15503 push_deferring_access_checks (dk_no_deferred
);
15504 /* save token before parsing the id-expression, for error
15506 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15507 tree default_argument
15508 = cp_parser_id_expression (parser
,
15509 /*template_keyword_p=*/false,
15510 /*check_dependency_p=*/true,
15511 /*template_p=*/&is_template
,
15512 /*declarator_p=*/false,
15513 /*optional_p=*/false);
15514 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15515 /* If the id-expression was a template-id that refers to
15516 a template-class, we already have the declaration here,
15517 so no further lookup is needed. */
15520 /* Look up the name. */
15522 = cp_parser_lookup_name (parser
, default_argument
,
15524 /*is_template=*/is_template
,
15525 /*is_namespace=*/false,
15526 /*check_dependency=*/true,
15527 /*ambiguous_decls=*/NULL
,
15529 /* See if the default argument is valid. */
15530 default_argument
= check_template_template_default_arg (default_argument
);
15531 pop_deferring_access_checks ();
15532 return default_argument
;
15535 /* Parse a template-parameter.
15537 template-parameter:
15539 parameter-declaration
15541 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15542 the parameter. The TREE_PURPOSE is the default value, if any.
15543 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15544 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15545 set to true iff this parameter is a parameter pack. */
15548 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15549 bool *is_parameter_pack
)
15552 cp_parameter_declarator
*parameter_declarator
;
15555 /* Assume it is a type parameter or a template parameter. */
15556 *is_non_type
= false;
15557 /* Assume it not a parameter pack. */
15558 *is_parameter_pack
= false;
15559 /* Peek at the next token. */
15560 token
= cp_lexer_peek_token (parser
->lexer
);
15561 /* If it is `template', we have a type-parameter. */
15562 if (token
->keyword
== RID_TEMPLATE
)
15563 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15564 /* If it is `class' or `typename' we do not know yet whether it is a
15565 type parameter or a non-type parameter. Consider:
15567 template <typename T, typename T::X X> ...
15571 template <class C, class D*> ...
15573 Here, the first parameter is a type parameter, and the second is
15574 a non-type parameter. We can tell by looking at the token after
15575 the identifier -- if it is a `,', `=', or `>' then we have a type
15577 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15579 /* Peek at the token after `class' or `typename'. */
15580 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15581 /* If it's an ellipsis, we have a template type parameter
15583 if (token
->type
== CPP_ELLIPSIS
)
15584 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15585 /* If it's an identifier, skip it. */
15586 if (token
->type
== CPP_NAME
)
15587 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15588 /* Now, see if the token looks like the end of a template
15590 if (token
->type
== CPP_COMMA
15591 || token
->type
== CPP_EQ
15592 || token
->type
== CPP_GREATER
)
15593 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15596 /* Otherwise, it is a non-type parameter or a constrained parameter.
15600 When parsing a default template-argument for a non-type
15601 template-parameter, the first non-nested `>' is taken as the end
15602 of the template parameter-list rather than a greater-than
15604 parameter_declarator
15605 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15606 /*parenthesized_p=*/NULL
);
15608 if (!parameter_declarator
)
15609 return error_mark_node
;
15611 /* If the parameter declaration is marked as a parameter pack, set
15612 *IS_PARAMETER_PACK to notify the caller. */
15613 if (parameter_declarator
->template_parameter_pack_p
)
15614 *is_parameter_pack
= true;
15616 if (parameter_declarator
->default_argument
)
15618 /* Can happen in some cases of erroneous input (c++/34892). */
15619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15620 /* Consume the `...' for better error recovery. */
15621 cp_lexer_consume_token (parser
->lexer
);
15624 // The parameter may have been constrained.
15625 if (is_constrained_parameter (parameter_declarator
))
15626 return finish_constrained_parameter (parser
,
15627 parameter_declarator
,
15629 is_parameter_pack
);
15631 // Now we're sure that the parameter is a non-type parameter.
15632 *is_non_type
= true;
15634 parm
= grokdeclarator (parameter_declarator
->declarator
,
15635 ¶meter_declarator
->decl_specifiers
,
15636 TPARM
, /*initialized=*/0,
15637 /*attrlist=*/NULL
);
15638 if (parm
== error_mark_node
)
15639 return error_mark_node
;
15641 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15644 /* Parse a type-parameter.
15647 class identifier [opt]
15648 class identifier [opt] = type-id
15649 typename identifier [opt]
15650 typename identifier [opt] = type-id
15651 template < template-parameter-list > class identifier [opt]
15652 template < template-parameter-list > class identifier [opt]
15655 GNU Extension (variadic templates):
15658 class ... identifier [opt]
15659 typename ... identifier [opt]
15661 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15662 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15663 the declaration of the parameter.
15665 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15668 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15673 /* Look for a keyword to tell us what kind of parameter this is. */
15674 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15676 return error_mark_node
;
15678 switch (token
->keyword
)
15684 tree default_argument
;
15686 /* If the next token is an ellipsis, we have a template
15688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15690 /* Consume the `...' token. */
15691 cp_lexer_consume_token (parser
->lexer
);
15692 maybe_warn_variadic_templates ();
15694 *is_parameter_pack
= true;
15697 /* If the next token is an identifier, then it names the
15699 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15700 identifier
= cp_parser_identifier (parser
);
15702 identifier
= NULL_TREE
;
15704 /* Create the parameter. */
15705 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15707 /* If the next token is an `=', we have a default argument. */
15708 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15711 = cp_parser_default_type_template_argument (parser
);
15713 /* Template parameter packs cannot have default
15715 if (*is_parameter_pack
)
15718 error_at (token
->location
,
15719 "template parameter pack %qD cannot have a "
15720 "default argument", identifier
);
15722 error_at (token
->location
,
15723 "template parameter packs cannot have "
15724 "default arguments");
15725 default_argument
= NULL_TREE
;
15727 else if (check_for_bare_parameter_packs (default_argument
))
15728 default_argument
= error_mark_node
;
15731 default_argument
= NULL_TREE
;
15733 /* Create the combined representation of the parameter and the
15734 default argument. */
15735 parameter
= build_tree_list (default_argument
, parameter
);
15742 tree default_argument
;
15744 /* Look for the `<'. */
15745 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15746 /* Parse the template-parameter-list. */
15747 cp_parser_template_parameter_list (parser
);
15748 /* Look for the `>'. */
15749 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15751 // If template requirements are present, parse them.
15754 tree reqs
= get_shorthand_constraints (current_template_parms
);
15755 if (tree r
= cp_parser_requires_clause_opt (parser
))
15756 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15757 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15760 /* Look for the `class' or 'typename' keywords. */
15761 cp_parser_type_parameter_key (parser
);
15762 /* If the next token is an ellipsis, we have a template
15764 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15766 /* Consume the `...' token. */
15767 cp_lexer_consume_token (parser
->lexer
);
15768 maybe_warn_variadic_templates ();
15770 *is_parameter_pack
= true;
15772 /* If the next token is an `=', then there is a
15773 default-argument. If the next token is a `>', we are at
15774 the end of the parameter-list. If the next token is a `,',
15775 then we are at the end of this parameter. */
15776 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15777 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15778 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15780 identifier
= cp_parser_identifier (parser
);
15781 /* Treat invalid names as if the parameter were nameless. */
15782 if (identifier
== error_mark_node
)
15783 identifier
= NULL_TREE
;
15786 identifier
= NULL_TREE
;
15788 /* Create the template parameter. */
15789 parameter
= finish_template_template_parm (class_type_node
,
15792 /* If the next token is an `=', then there is a
15793 default-argument. */
15794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15797 = cp_parser_default_template_template_argument (parser
);
15799 /* Template parameter packs cannot have default
15801 if (*is_parameter_pack
)
15804 error_at (token
->location
,
15805 "template parameter pack %qD cannot "
15806 "have a default argument",
15809 error_at (token
->location
, "template parameter packs cannot "
15810 "have default arguments");
15811 default_argument
= NULL_TREE
;
15815 default_argument
= NULL_TREE
;
15817 /* Create the combined representation of the parameter and the
15818 default argument. */
15819 parameter
= build_tree_list (default_argument
, parameter
);
15824 gcc_unreachable ();
15831 /* Parse a template-id.
15834 template-name < template-argument-list [opt] >
15836 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15837 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15838 returned. Otherwise, if the template-name names a function, or set
15839 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15840 names a class, returns a TYPE_DECL for the specialization.
15842 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15843 uninstantiated templates. */
15846 cp_parser_template_id (cp_parser
*parser
,
15847 bool template_keyword_p
,
15848 bool check_dependency_p
,
15849 enum tag_types tag_type
,
15850 bool is_declaration
)
15855 cp_token_position start_of_id
= 0;
15856 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15857 bool is_identifier
;
15859 /* If the next token corresponds to a template-id, there is no need
15861 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15862 if (token
->type
== CPP_TEMPLATE_ID
)
15864 cp_lexer_consume_token (parser
->lexer
);
15865 return saved_checks_value (token
->u
.tree_check_value
);
15868 /* Avoid performing name lookup if there is no possibility of
15869 finding a template-id. */
15870 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15871 || (token
->type
== CPP_NAME
15872 && !cp_parser_nth_token_starts_template_argument_list_p
15875 cp_parser_error (parser
, "expected template-id");
15876 return error_mark_node
;
15879 /* Remember where the template-id starts. */
15880 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15881 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15883 push_deferring_access_checks (dk_deferred
);
15885 /* Parse the template-name. */
15886 is_identifier
= false;
15887 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15888 check_dependency_p
,
15892 if (templ
== error_mark_node
|| is_identifier
)
15894 pop_deferring_access_checks ();
15898 /* Since we're going to preserve any side-effects from this parse, set up a
15899 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15900 in the template arguments. */
15901 tentative_firewall
firewall (parser
);
15903 /* If we find the sequence `[:' after a template-name, it's probably
15904 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15905 parse correctly the argument list. */
15906 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15907 == CPP_OPEN_SQUARE
)
15908 && next_token
->flags
& DIGRAPH
15909 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15911 && !(next_token_2
->flags
& PREV_WHITE
))
15913 cp_parser_parse_tentatively (parser
);
15914 /* Change `:' into `::'. */
15915 next_token_2
->type
= CPP_SCOPE
;
15916 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15918 cp_lexer_consume_token (parser
->lexer
);
15920 /* Parse the arguments. */
15921 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15922 if (!cp_parser_parse_definitely (parser
))
15924 /* If we couldn't parse an argument list, then we revert our changes
15925 and return simply an error. Maybe this is not a template-id
15927 next_token_2
->type
= CPP_COLON
;
15928 cp_parser_error (parser
, "expected %<<%>");
15929 pop_deferring_access_checks ();
15930 return error_mark_node
;
15932 /* Otherwise, emit an error about the invalid digraph, but continue
15933 parsing because we got our argument list. */
15934 if (permerror (next_token
->location
,
15935 "%<<::%> cannot begin a template-argument list"))
15937 static bool hint
= false;
15938 inform (next_token
->location
,
15939 "%<<:%> is an alternate spelling for %<[%>."
15940 " Insert whitespace between %<<%> and %<::%>");
15941 if (!hint
&& !flag_permissive
)
15943 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15944 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15945 "accept your code)");
15952 /* Look for the `<' that starts the template-argument-list. */
15953 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15955 pop_deferring_access_checks ();
15956 return error_mark_node
;
15958 /* Parse the arguments. */
15959 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15962 /* Set the location to be of the form:
15963 template-name < template-argument-list [opt] >
15964 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15965 with caret == start at the start of the template-name,
15966 ranging until the closing '>'. */
15967 location_t finish_loc
15968 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15969 location_t combined_loc
15970 = make_location (token
->location
, token
->location
, finish_loc
);
15972 /* Check for concepts autos where they don't belong. We could
15973 identify types in some cases of idnetifier TEMPL, looking ahead
15974 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15975 types. We reject them in functions, but if what we have is an
15976 identifier, even with none_type we can't conclude it's NOT a
15977 type, we have to wait for template substitution. */
15978 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
15979 template_id
= error_mark_node
;
15980 /* Build a representation of the specialization. */
15981 else if (identifier_p (templ
))
15982 template_id
= build_min_nt_loc (combined_loc
,
15985 else if (DECL_TYPE_TEMPLATE_P (templ
)
15986 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15988 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15989 template (rather than some instantiation thereof) only if
15990 is not nested within some other construct. For example, in
15991 "template <typename T> void f(T) { A<T>::", A<T> is just an
15992 instantiation of A. */
15993 bool entering_scope
15994 = (template_parm_scope_p ()
15995 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
15997 = finish_template_type (templ
, arguments
, entering_scope
);
15999 /* A template-like identifier may be a partial concept id. */
16000 else if (flag_concepts
16001 && (template_id
= (cp_parser_maybe_partial_concept_id
16002 (parser
, templ
, arguments
))))
16003 return template_id
;
16004 else if (variable_template_p (templ
))
16006 template_id
= lookup_template_variable (templ
, arguments
);
16007 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16008 SET_EXPR_LOCATION (template_id
, combined_loc
);
16012 /* If it's not a class-template or a template-template, it should be
16013 a function-template. */
16014 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16015 || TREE_CODE (templ
) == OVERLOAD
16016 || BASELINK_P (templ
)));
16018 template_id
= lookup_template_function (templ
, arguments
);
16019 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16020 SET_EXPR_LOCATION (template_id
, combined_loc
);
16023 /* If parsing tentatively, replace the sequence of tokens that makes
16024 up the template-id with a CPP_TEMPLATE_ID token. That way,
16025 should we re-parse the token stream, we will not have to repeat
16026 the effort required to do the parse, nor will we issue duplicate
16027 error messages about problems during instantiation of the
16030 /* Don't do this if we had a parse error in a declarator; re-parsing
16031 might succeed if a name changes meaning (60361). */
16032 && !(cp_parser_error_occurred (parser
)
16033 && cp_parser_parsing_tentatively (parser
)
16034 && parser
->in_declarator_p
))
16036 /* Reset the contents of the START_OF_ID token. */
16037 token
->type
= CPP_TEMPLATE_ID
;
16038 token
->location
= combined_loc
;
16040 /* Retrieve any deferred checks. Do not pop this access checks yet
16041 so the memory will not be reclaimed during token replacing below. */
16042 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16043 token
->u
.tree_check_value
->value
= template_id
;
16044 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16045 token
->keyword
= RID_MAX
;
16047 /* Purge all subsequent tokens. */
16048 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16050 /* ??? Can we actually assume that, if template_id ==
16051 error_mark_node, we will have issued a diagnostic to the
16052 user, as opposed to simply marking the tentative parse as
16054 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16055 error_at (token
->location
, "parse error in template argument list");
16058 pop_to_parent_deferring_access_checks ();
16059 return template_id
;
16062 /* Parse a template-name.
16067 The standard should actually say:
16071 operator-function-id
16073 A defect report has been filed about this issue.
16075 A conversion-function-id cannot be a template name because they cannot
16076 be part of a template-id. In fact, looking at this code:
16078 a.operator K<int>()
16080 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16081 It is impossible to call a templated conversion-function-id with an
16082 explicit argument list, since the only allowed template parameter is
16083 the type to which it is converting.
16085 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16086 `template' keyword, in a construction like:
16090 In that case `f' is taken to be a template-name, even though there
16091 is no way of knowing for sure.
16093 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16094 name refers to a set of overloaded functions, at least one of which
16095 is a template, or an IDENTIFIER_NODE with the name of the template,
16096 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16097 names are looked up inside uninstantiated templates. */
16100 cp_parser_template_name (cp_parser
* parser
,
16101 bool template_keyword_p
,
16102 bool check_dependency_p
,
16103 bool is_declaration
,
16104 enum tag_types tag_type
,
16105 bool *is_identifier
)
16109 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16111 /* If the next token is `operator', then we have either an
16112 operator-function-id or a conversion-function-id. */
16113 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16115 /* We don't know whether we're looking at an
16116 operator-function-id or a conversion-function-id. */
16117 cp_parser_parse_tentatively (parser
);
16118 /* Try an operator-function-id. */
16119 identifier
= cp_parser_operator_function_id (parser
);
16120 /* If that didn't work, try a conversion-function-id. */
16121 if (!cp_parser_parse_definitely (parser
))
16123 cp_parser_error (parser
, "expected template-name");
16124 return error_mark_node
;
16127 /* Look for the identifier. */
16129 identifier
= cp_parser_identifier (parser
);
16131 /* If we didn't find an identifier, we don't have a template-id. */
16132 if (identifier
== error_mark_node
)
16133 return error_mark_node
;
16135 /* If the name immediately followed the `template' keyword, then it
16136 is a template-name. However, if the next token is not `<', then
16137 we do not treat it as a template-name, since it is not being used
16138 as part of a template-id. This enables us to handle constructs
16141 template <typename T> struct S { S(); };
16142 template <typename T> S<T>::S();
16144 correctly. We would treat `S' as a template -- if it were `S<T>'
16145 -- but we do not if there is no `<'. */
16147 if (processing_template_decl
16148 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16150 /* In a declaration, in a dependent context, we pretend that the
16151 "template" keyword was present in order to improve error
16152 recovery. For example, given:
16154 template <typename T> void f(T::X<int>);
16156 we want to treat "X<int>" as a template-id. */
16158 && !template_keyword_p
16159 && parser
->scope
&& TYPE_P (parser
->scope
)
16160 && check_dependency_p
16161 && dependent_scope_p (parser
->scope
)
16162 /* Do not do this for dtors (or ctors), since they never
16163 need the template keyword before their name. */
16164 && !constructor_name_p (identifier
, parser
->scope
))
16166 cp_token_position start
= 0;
16168 /* Explain what went wrong. */
16169 error_at (token
->location
, "non-template %qD used as template",
16171 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16172 parser
->scope
, identifier
);
16173 /* If parsing tentatively, find the location of the "<" token. */
16174 if (cp_parser_simulate_error (parser
))
16175 start
= cp_lexer_token_position (parser
->lexer
, true);
16176 /* Parse the template arguments so that we can issue error
16177 messages about them. */
16178 cp_lexer_consume_token (parser
->lexer
);
16179 cp_parser_enclosed_template_argument_list (parser
);
16180 /* Skip tokens until we find a good place from which to
16181 continue parsing. */
16182 cp_parser_skip_to_closing_parenthesis (parser
,
16183 /*recovering=*/true,
16185 /*consume_paren=*/false);
16186 /* If parsing tentatively, permanently remove the
16187 template argument list. That will prevent duplicate
16188 error messages from being issued about the missing
16189 "template" keyword. */
16191 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16193 *is_identifier
= true;
16194 parser
->context
->object_type
= NULL_TREE
;
16198 /* If the "template" keyword is present, then there is generally
16199 no point in doing name-lookup, so we just return IDENTIFIER.
16200 But, if the qualifying scope is non-dependent then we can
16201 (and must) do name-lookup normally. */
16202 if (template_keyword_p
)
16204 tree scope
= (parser
->scope
? parser
->scope
16205 : parser
->context
->object_type
);
16206 if (scope
&& TYPE_P (scope
)
16207 && (!CLASS_TYPE_P (scope
)
16208 || (check_dependency_p
&& dependent_type_p (scope
))))
16210 /* We're optimizing away the call to cp_parser_lookup_name, but
16211 we still need to do this. */
16212 parser
->context
->object_type
= NULL_TREE
;
16218 /* Look up the name. */
16219 decl
= cp_parser_lookup_name (parser
, identifier
,
16221 /*is_template=*/true,
16222 /*is_namespace=*/false,
16223 check_dependency_p
,
16224 /*ambiguous_decls=*/NULL
,
16227 decl
= strip_using_decl (decl
);
16229 /* If DECL is a template, then the name was a template-name. */
16230 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16232 if (TREE_DEPRECATED (decl
)
16233 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16234 warn_deprecated_use (decl
, NULL_TREE
);
16238 /* The standard does not explicitly indicate whether a name that
16239 names a set of overloaded declarations, some of which are
16240 templates, is a template-name. However, such a name should
16241 be a template-name; otherwise, there is no way to form a
16242 template-id for the overloaded templates. */
16243 bool found
= false;
16245 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16246 !found
&& iter
; ++iter
)
16247 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16252 /* The name does not name a template. */
16253 cp_parser_error (parser
, "expected template-name");
16254 return error_mark_node
;
16258 /* If DECL is dependent, and refers to a function, then just return
16259 its name; we will look it up again during template instantiation. */
16260 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16262 tree scope
= ovl_scope (decl
);
16263 if (TYPE_P (scope
) && dependent_type_p (scope
))
16270 /* Parse a template-argument-list.
16272 template-argument-list:
16273 template-argument ... [opt]
16274 template-argument-list , template-argument ... [opt]
16276 Returns a TREE_VEC containing the arguments. */
16279 cp_parser_template_argument_list (cp_parser
* parser
)
16281 tree fixed_args
[10];
16282 unsigned n_args
= 0;
16283 unsigned alloced
= 10;
16284 tree
*arg_ary
= fixed_args
;
16286 bool saved_in_template_argument_list_p
;
16288 bool saved_non_ice_p
;
16290 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16291 parser
->in_template_argument_list_p
= true;
16292 /* Even if the template-id appears in an integral
16293 constant-expression, the contents of the argument list do
16295 saved_ice_p
= parser
->integral_constant_expression_p
;
16296 parser
->integral_constant_expression_p
= false;
16297 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16298 parser
->non_integral_constant_expression_p
= false;
16300 /* Parse the arguments. */
16306 /* Consume the comma. */
16307 cp_lexer_consume_token (parser
->lexer
);
16309 /* Parse the template-argument. */
16310 argument
= cp_parser_template_argument (parser
);
16312 /* If the next token is an ellipsis, we're expanding a template
16314 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16316 if (argument
== error_mark_node
)
16318 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16319 error_at (token
->location
,
16320 "expected parameter pack before %<...%>");
16322 /* Consume the `...' token. */
16323 cp_lexer_consume_token (parser
->lexer
);
16325 /* Make the argument into a TYPE_PACK_EXPANSION or
16326 EXPR_PACK_EXPANSION. */
16327 argument
= make_pack_expansion (argument
);
16330 if (n_args
== alloced
)
16334 if (arg_ary
== fixed_args
)
16336 arg_ary
= XNEWVEC (tree
, alloced
);
16337 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16340 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16342 arg_ary
[n_args
++] = argument
;
16344 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16346 vec
= make_tree_vec (n_args
);
16349 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16351 if (arg_ary
!= fixed_args
)
16353 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16354 parser
->integral_constant_expression_p
= saved_ice_p
;
16355 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16357 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16361 /* Parse a template-argument.
16364 assignment-expression
16368 The representation is that of an assignment-expression, type-id, or
16369 id-expression -- except that the qualified id-expression is
16370 evaluated, so that the value returned is either a DECL or an
16373 Although the standard says "assignment-expression", it forbids
16374 throw-expressions or assignments in the template argument.
16375 Therefore, we use "conditional-expression" instead. */
16378 cp_parser_template_argument (cp_parser
* parser
)
16383 bool maybe_type_id
= false;
16384 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16385 location_t loc
= 0;
16388 /* There's really no way to know what we're looking at, so we just
16389 try each alternative in order.
16393 In a template-argument, an ambiguity between a type-id and an
16394 expression is resolved to a type-id, regardless of the form of
16395 the corresponding template-parameter.
16397 Therefore, we try a type-id first. */
16398 cp_parser_parse_tentatively (parser
);
16399 argument
= cp_parser_template_type_arg (parser
);
16400 /* If there was no error parsing the type-id but the next token is a
16401 '>>', our behavior depends on which dialect of C++ we're
16402 parsing. In C++98, we probably found a typo for '> >'. But there
16403 are type-id which are also valid expressions. For instance:
16405 struct X { int operator >> (int); };
16406 template <int V> struct Foo {};
16409 Here 'X()' is a valid type-id of a function type, but the user just
16410 wanted to write the expression "X() >> 5". Thus, we remember that we
16411 found a valid type-id, but we still try to parse the argument as an
16412 expression to see what happens.
16414 In C++0x, the '>>' will be considered two separate '>'
16416 if (!cp_parser_error_occurred (parser
)
16417 && cxx_dialect
== cxx98
16418 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16420 maybe_type_id
= true;
16421 cp_parser_abort_tentative_parse (parser
);
16425 /* If the next token isn't a `,' or a `>', then this argument wasn't
16426 really finished. This means that the argument is not a valid
16428 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16429 cp_parser_error (parser
, "expected template-argument");
16430 /* If that worked, we're done. */
16431 if (cp_parser_parse_definitely (parser
))
16434 /* We're still not sure what the argument will be. */
16435 cp_parser_parse_tentatively (parser
);
16436 /* Try a template. */
16437 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16438 argument
= cp_parser_id_expression (parser
,
16439 /*template_keyword_p=*/false,
16440 /*check_dependency_p=*/true,
16442 /*declarator_p=*/false,
16443 /*optional_p=*/false);
16444 /* If the next token isn't a `,' or a `>', then this argument wasn't
16445 really finished. */
16446 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16447 cp_parser_error (parser
, "expected template-argument");
16448 if (!cp_parser_error_occurred (parser
))
16450 /* Figure out what is being referred to. If the id-expression
16451 was for a class template specialization, then we will have a
16452 TYPE_DECL at this point. There is no need to do name lookup
16453 at this point in that case. */
16454 if (TREE_CODE (argument
) != TYPE_DECL
)
16455 argument
= cp_parser_lookup_name (parser
, argument
,
16457 /*is_template=*/template_p
,
16458 /*is_namespace=*/false,
16459 /*check_dependency=*/true,
16460 /*ambiguous_decls=*/NULL
,
16461 argument_start_token
->location
);
16462 /* Handle a constrained-type-specifier for a non-type template
16464 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16466 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16467 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16468 cp_parser_error (parser
, "expected template-name");
16470 if (cp_parser_parse_definitely (parser
))
16472 if (TREE_DEPRECATED (argument
))
16473 warn_deprecated_use (argument
, NULL_TREE
);
16476 /* It must be a non-type argument. In C++17 any constant-expression is
16478 if (cxx_dialect
> cxx14
)
16481 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16483 -- an integral constant-expression of integral or enumeration
16486 -- the name of a non-type template-parameter; or
16488 -- the name of an object or function with external linkage...
16490 -- the address of an object or function with external linkage...
16492 -- a pointer to member... */
16493 /* Look for a non-type template parameter. */
16494 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16496 cp_parser_parse_tentatively (parser
);
16497 argument
= cp_parser_primary_expression (parser
,
16498 /*address_p=*/false,
16500 /*template_arg_p=*/true,
16502 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16503 || !cp_parser_next_token_ends_template_argument_p (parser
))
16504 cp_parser_simulate_error (parser
);
16505 if (cp_parser_parse_definitely (parser
))
16509 /* If the next token is "&", the argument must be the address of an
16510 object or function with external linkage. */
16511 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16514 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16515 cp_lexer_consume_token (parser
->lexer
);
16517 /* See if we might have an id-expression. */
16518 token
= cp_lexer_peek_token (parser
->lexer
);
16519 if (token
->type
== CPP_NAME
16520 || token
->keyword
== RID_OPERATOR
16521 || token
->type
== CPP_SCOPE
16522 || token
->type
== CPP_TEMPLATE_ID
16523 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16525 cp_parser_parse_tentatively (parser
);
16526 argument
= cp_parser_primary_expression (parser
,
16529 /*template_arg_p=*/true,
16531 if (cp_parser_error_occurred (parser
)
16532 || !cp_parser_next_token_ends_template_argument_p (parser
))
16533 cp_parser_abort_tentative_parse (parser
);
16538 if (INDIRECT_REF_P (argument
))
16540 /* Strip the dereference temporarily. */
16541 gcc_assert (REFERENCE_REF_P (argument
));
16542 argument
= TREE_OPERAND (argument
, 0);
16545 /* If we're in a template, we represent a qualified-id referring
16546 to a static data member as a SCOPE_REF even if the scope isn't
16547 dependent so that we can check access control later. */
16549 if (TREE_CODE (probe
) == SCOPE_REF
)
16550 probe
= TREE_OPERAND (probe
, 1);
16553 /* A variable without external linkage might still be a
16554 valid constant-expression, so no error is issued here
16555 if the external-linkage check fails. */
16556 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16557 cp_parser_simulate_error (parser
);
16559 else if (is_overloaded_fn (argument
))
16560 /* All overloaded functions are allowed; if the external
16561 linkage test does not pass, an error will be issued
16565 && (TREE_CODE (argument
) == OFFSET_REF
16566 || TREE_CODE (argument
) == SCOPE_REF
))
16567 /* A pointer-to-member. */
16569 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16572 cp_parser_simulate_error (parser
);
16574 if (cp_parser_parse_definitely (parser
))
16577 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16578 tf_warning_or_error
);
16580 argument
= convert_from_reference (argument
);
16585 /* If the argument started with "&", there are no other valid
16586 alternatives at this point. */
16589 cp_parser_error (parser
, "invalid non-type template argument");
16590 return error_mark_node
;
16594 /* If the argument wasn't successfully parsed as a type-id followed
16595 by '>>', the argument can only be a constant expression now.
16596 Otherwise, we try parsing the constant-expression tentatively,
16597 because the argument could really be a type-id. */
16599 cp_parser_parse_tentatively (parser
);
16601 if (cxx_dialect
<= cxx14
)
16602 argument
= cp_parser_constant_expression (parser
);
16605 /* With C++17 generalized non-type template arguments we need to handle
16606 lvalue constant expressions, too. */
16607 argument
= cp_parser_assignment_expression (parser
);
16608 require_potential_constant_expression (argument
);
16611 if (!maybe_type_id
)
16613 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16614 cp_parser_error (parser
, "expected template-argument");
16615 if (cp_parser_parse_definitely (parser
))
16617 /* We did our best to parse the argument as a non type-id, but that
16618 was the only alternative that matched (albeit with a '>' after
16619 it). We can assume it's just a typo from the user, and a
16620 diagnostic will then be issued. */
16621 return cp_parser_template_type_arg (parser
);
16624 /* Parse an explicit-instantiation.
16626 explicit-instantiation:
16627 template declaration
16629 Although the standard says `declaration', what it really means is:
16631 explicit-instantiation:
16632 template decl-specifier-seq [opt] declarator [opt] ;
16634 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16635 supposed to be allowed. A defect report has been filed about this
16640 explicit-instantiation:
16641 storage-class-specifier template
16642 decl-specifier-seq [opt] declarator [opt] ;
16643 function-specifier template
16644 decl-specifier-seq [opt] declarator [opt] ; */
16647 cp_parser_explicit_instantiation (cp_parser
* parser
)
16649 int declares_class_or_enum
;
16650 cp_decl_specifier_seq decl_specifiers
;
16651 tree extension_specifier
= NULL_TREE
;
16653 timevar_push (TV_TEMPLATE_INST
);
16655 /* Look for an (optional) storage-class-specifier or
16656 function-specifier. */
16657 if (cp_parser_allow_gnu_extensions_p (parser
))
16659 extension_specifier
16660 = cp_parser_storage_class_specifier_opt (parser
);
16661 if (!extension_specifier
)
16662 extension_specifier
16663 = cp_parser_function_specifier_opt (parser
,
16664 /*decl_specs=*/NULL
);
16667 /* Look for the `template' keyword. */
16668 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16669 /* Let the front end know that we are processing an explicit
16671 begin_explicit_instantiation ();
16672 /* [temp.explicit] says that we are supposed to ignore access
16673 control while processing explicit instantiation directives. */
16674 push_deferring_access_checks (dk_no_check
);
16675 /* Parse a decl-specifier-seq. */
16676 cp_parser_decl_specifier_seq (parser
,
16677 CP_PARSER_FLAGS_OPTIONAL
,
16679 &declares_class_or_enum
);
16680 /* If there was exactly one decl-specifier, and it declared a class,
16681 and there's no declarator, then we have an explicit type
16683 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16687 type
= check_tag_decl (&decl_specifiers
,
16688 /*explicit_type_instantiation_p=*/true);
16689 /* Turn access control back on for names used during
16690 template instantiation. */
16691 pop_deferring_access_checks ();
16693 do_type_instantiation (type
, extension_specifier
,
16694 /*complain=*/tf_error
);
16698 cp_declarator
*declarator
;
16701 /* Parse the declarator. */
16703 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16704 /*ctor_dtor_or_conv_p=*/NULL
,
16705 /*parenthesized_p=*/NULL
,
16706 /*member_p=*/false,
16707 /*friend_p=*/false);
16708 if (declares_class_or_enum
& 2)
16709 cp_parser_check_for_definition_in_return_type (declarator
,
16710 decl_specifiers
.type
,
16711 decl_specifiers
.locations
[ds_type_spec
]);
16712 if (declarator
!= cp_error_declarator
)
16714 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16715 permerror (decl_specifiers
.locations
[ds_inline
],
16716 "explicit instantiation shall not use"
16717 " %<inline%> specifier");
16718 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16719 permerror (decl_specifiers
.locations
[ds_constexpr
],
16720 "explicit instantiation shall not use"
16721 " %<constexpr%> specifier");
16723 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16724 NORMAL
, 0, &decl_specifiers
.attributes
);
16725 /* Turn access control back on for names used during
16726 template instantiation. */
16727 pop_deferring_access_checks ();
16728 /* Do the explicit instantiation. */
16729 do_decl_instantiation (decl
, extension_specifier
);
16733 pop_deferring_access_checks ();
16734 /* Skip the body of the explicit instantiation. */
16735 cp_parser_skip_to_end_of_statement (parser
);
16738 /* We're done with the instantiation. */
16739 end_explicit_instantiation ();
16741 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16743 timevar_pop (TV_TEMPLATE_INST
);
16746 /* Parse an explicit-specialization.
16748 explicit-specialization:
16749 template < > declaration
16751 Although the standard says `declaration', what it really means is:
16753 explicit-specialization:
16754 template <> decl-specifier [opt] init-declarator [opt] ;
16755 template <> function-definition
16756 template <> explicit-specialization
16757 template <> template-declaration */
16760 cp_parser_explicit_specialization (cp_parser
* parser
)
16762 bool need_lang_pop
;
16763 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16765 /* Look for the `template' keyword. */
16766 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16767 /* Look for the `<'. */
16768 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16769 /* Look for the `>'. */
16770 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16771 /* We have processed another parameter list. */
16772 ++parser
->num_template_parameter_lists
;
16775 A template ... explicit specialization ... shall not have C
16777 if (current_lang_name
== lang_name_c
)
16779 error_at (token
->location
, "template specialization with C linkage");
16780 maybe_show_extern_c_location ();
16781 /* Give it C++ linkage to avoid confusing other parts of the
16783 push_lang_context (lang_name_cplusplus
);
16784 need_lang_pop
= true;
16787 need_lang_pop
= false;
16788 /* Let the front end know that we are beginning a specialization. */
16789 if (!begin_specialization ())
16791 end_specialization ();
16795 /* If the next keyword is `template', we need to figure out whether
16796 or not we're looking a template-declaration. */
16797 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16799 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16800 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16801 cp_parser_template_declaration_after_export (parser
,
16802 /*member_p=*/false);
16804 cp_parser_explicit_specialization (parser
);
16807 /* Parse the dependent declaration. */
16808 cp_parser_single_declaration (parser
,
16810 /*member_p=*/false,
16811 /*explicit_specialization_p=*/true,
16812 /*friend_p=*/NULL
);
16813 /* We're done with the specialization. */
16814 end_specialization ();
16815 /* For the erroneous case of a template with C linkage, we pushed an
16816 implicit C++ linkage scope; exit that scope now. */
16818 pop_lang_context ();
16819 /* We're done with this parameter list. */
16820 --parser
->num_template_parameter_lists
;
16823 /* Parse a type-specifier.
16826 simple-type-specifier
16829 elaborated-type-specifier
16837 Returns a representation of the type-specifier. For a
16838 class-specifier, enum-specifier, or elaborated-type-specifier, a
16839 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16841 The parser flags FLAGS is used to control type-specifier parsing.
16843 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16844 in a decl-specifier-seq.
16846 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16847 class-specifier, enum-specifier, or elaborated-type-specifier, then
16848 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16849 if a type is declared; 2 if it is defined. Otherwise, it is set to
16852 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16853 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16854 is set to FALSE. */
16857 cp_parser_type_specifier (cp_parser
* parser
,
16858 cp_parser_flags flags
,
16859 cp_decl_specifier_seq
*decl_specs
,
16860 bool is_declaration
,
16861 int* declares_class_or_enum
,
16862 bool* is_cv_qualifier
)
16864 tree type_spec
= NULL_TREE
;
16867 cp_decl_spec ds
= ds_last
;
16869 /* Assume this type-specifier does not declare a new type. */
16870 if (declares_class_or_enum
)
16871 *declares_class_or_enum
= 0;
16872 /* And that it does not specify a cv-qualifier. */
16873 if (is_cv_qualifier
)
16874 *is_cv_qualifier
= false;
16875 /* Peek at the next token. */
16876 token
= cp_lexer_peek_token (parser
->lexer
);
16878 /* If we're looking at a keyword, we can use that to guide the
16879 production we choose. */
16880 keyword
= token
->keyword
;
16884 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16885 goto elaborated_type_specifier
;
16887 /* Look for the enum-specifier. */
16888 type_spec
= cp_parser_enum_specifier (parser
);
16889 /* If that worked, we're done. */
16892 if (declares_class_or_enum
)
16893 *declares_class_or_enum
= 2;
16895 cp_parser_set_decl_spec_type (decl_specs
,
16898 /*type_definition_p=*/true);
16902 goto elaborated_type_specifier
;
16904 /* Any of these indicate either a class-specifier, or an
16905 elaborated-type-specifier. */
16909 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16910 goto elaborated_type_specifier
;
16912 /* Parse tentatively so that we can back up if we don't find a
16913 class-specifier. */
16914 cp_parser_parse_tentatively (parser
);
16915 /* Look for the class-specifier. */
16916 type_spec
= cp_parser_class_specifier (parser
);
16917 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16918 /* If that worked, we're done. */
16919 if (cp_parser_parse_definitely (parser
))
16921 if (declares_class_or_enum
)
16922 *declares_class_or_enum
= 2;
16924 cp_parser_set_decl_spec_type (decl_specs
,
16927 /*type_definition_p=*/true);
16931 /* Fall through. */
16932 elaborated_type_specifier
:
16933 /* We're declaring (not defining) a class or enum. */
16934 if (declares_class_or_enum
)
16935 *declares_class_or_enum
= 1;
16937 /* Fall through. */
16939 /* Look for an elaborated-type-specifier. */
16941 = (cp_parser_elaborated_type_specifier
16943 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16946 cp_parser_set_decl_spec_type (decl_specs
,
16949 /*type_definition_p=*/false);
16954 if (is_cv_qualifier
)
16955 *is_cv_qualifier
= true;
16960 if (is_cv_qualifier
)
16961 *is_cv_qualifier
= true;
16966 if (is_cv_qualifier
)
16967 *is_cv_qualifier
= true;
16971 /* The `__complex__' keyword is a GNU extension. */
16979 /* Handle simple keywords. */
16984 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16985 decl_specs
->any_specifiers_p
= true;
16987 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16990 /* If we do not already have a type-specifier, assume we are looking
16991 at a simple-type-specifier. */
16992 type_spec
= cp_parser_simple_type_specifier (parser
,
16996 /* If we didn't find a type-specifier, and a type-specifier was not
16997 optional in this context, issue an error message. */
16998 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17000 cp_parser_error (parser
, "expected type specifier");
17001 return error_mark_node
;
17007 /* Parse a simple-type-specifier.
17009 simple-type-specifier:
17010 :: [opt] nested-name-specifier [opt] type-name
17011 :: [opt] nested-name-specifier template template-id
17026 simple-type-specifier:
17028 decltype ( expression )
17031 __underlying_type ( type-id )
17035 nested-name-specifier(opt) template-name
17039 simple-type-specifier:
17041 __typeof__ unary-expression
17042 __typeof__ ( type-id )
17043 __typeof__ ( type-id ) { initializer-list , [opt] }
17045 Concepts Extension:
17047 simple-type-specifier:
17048 constrained-type-specifier
17050 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17051 appropriately updated. */
17054 cp_parser_simple_type_specifier (cp_parser
* parser
,
17055 cp_decl_specifier_seq
*decl_specs
,
17056 cp_parser_flags flags
)
17058 tree type
= NULL_TREE
;
17062 /* Peek at the next token. */
17063 token
= cp_lexer_peek_token (parser
->lexer
);
17065 /* If we're looking at a keyword, things are easy. */
17066 switch (token
->keyword
)
17070 decl_specs
->explicit_char_p
= true;
17071 type
= char_type_node
;
17074 type
= char16_type_node
;
17077 type
= char32_type_node
;
17080 type
= wchar_type_node
;
17083 type
= boolean_type_node
;
17086 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17087 type
= short_integer_type_node
;
17091 decl_specs
->explicit_int_p
= true;
17092 type
= integer_type_node
;
17098 idx
= token
->keyword
- RID_INT_N_0
;
17099 if (! int_n_enabled_p
[idx
])
17103 decl_specs
->explicit_intN_p
= true;
17104 decl_specs
->int_n_idx
= idx
;
17106 type
= int_n_trees
[idx
].signed_type
;
17110 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17111 type
= long_integer_type_node
;
17114 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17115 type
= integer_type_node
;
17118 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17119 type
= unsigned_type_node
;
17122 type
= float_type_node
;
17125 type
= double_type_node
;
17128 type
= void_type_node
;
17132 maybe_warn_cpp0x (CPP0X_AUTO
);
17133 if (parser
->auto_is_implicit_function_template_parm_p
)
17135 /* The 'auto' might be the placeholder return type for a function decl
17136 with trailing return type. */
17137 bool have_trailing_return_fn_decl
= false;
17139 cp_parser_parse_tentatively (parser
);
17140 cp_lexer_consume_token (parser
->lexer
);
17141 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17142 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17143 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17144 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17146 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17148 cp_lexer_consume_token (parser
->lexer
);
17149 cp_parser_skip_to_closing_parenthesis (parser
,
17150 /*recovering*/false,
17152 /*consume_paren*/true);
17156 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17158 have_trailing_return_fn_decl
= true;
17162 cp_lexer_consume_token (parser
->lexer
);
17164 cp_parser_abort_tentative_parse (parser
);
17166 if (have_trailing_return_fn_decl
)
17168 type
= make_auto ();
17172 if (cxx_dialect
>= cxx14
)
17174 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17175 type
= TREE_TYPE (type
);
17178 type
= error_mark_node
;
17180 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17182 if (cxx_dialect
< cxx14
)
17183 error_at (token
->location
,
17184 "use of %<auto%> in lambda parameter declaration "
17185 "only available with "
17186 "-std=c++14 or -std=gnu++14");
17188 else if (cxx_dialect
< cxx14
)
17189 error_at (token
->location
,
17190 "use of %<auto%> in parameter declaration "
17191 "only available with "
17192 "-std=c++14 or -std=gnu++14");
17193 else if (!flag_concepts
)
17194 pedwarn (token
->location
, 0,
17195 "use of %<auto%> in parameter declaration "
17196 "only available with -fconcepts");
17199 type
= make_auto ();
17203 /* Since DR 743, decltype can either be a simple-type-specifier by
17204 itself or begin a nested-name-specifier. Parsing it will replace
17205 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17206 handling below decide what to do. */
17207 cp_parser_decltype (parser
);
17208 cp_lexer_set_token_position (parser
->lexer
, token
);
17212 /* Consume the `typeof' token. */
17213 cp_lexer_consume_token (parser
->lexer
);
17214 /* Parse the operand to `typeof'. */
17215 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17216 /* If it is not already a TYPE, take its type. */
17217 if (!TYPE_P (type
))
17218 type
= finish_typeof (type
);
17221 cp_parser_set_decl_spec_type (decl_specs
, type
,
17223 /*type_definition_p=*/false);
17227 case RID_UNDERLYING_TYPE
:
17228 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17230 cp_parser_set_decl_spec_type (decl_specs
, type
,
17232 /*type_definition_p=*/false);
17237 case RID_DIRECT_BASES
:
17238 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17240 cp_parser_set_decl_spec_type (decl_specs
, type
,
17242 /*type_definition_p=*/false);
17248 /* If token is an already-parsed decltype not followed by ::,
17249 it's a simple-type-specifier. */
17250 if (token
->type
== CPP_DECLTYPE
17251 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17253 type
= saved_checks_value (token
->u
.tree_check_value
);
17256 cp_parser_set_decl_spec_type (decl_specs
, type
,
17258 /*type_definition_p=*/false);
17259 /* Remember that we are handling a decltype in order to
17260 implement the resolution of DR 1510 when the argument
17261 isn't instantiation dependent. */
17262 decl_specs
->decltype_p
= true;
17264 cp_lexer_consume_token (parser
->lexer
);
17268 /* If the type-specifier was for a built-in type, we're done. */
17271 /* Record the type. */
17273 && (token
->keyword
!= RID_SIGNED
17274 && token
->keyword
!= RID_UNSIGNED
17275 && token
->keyword
!= RID_SHORT
17276 && token
->keyword
!= RID_LONG
))
17277 cp_parser_set_decl_spec_type (decl_specs
,
17280 /*type_definition_p=*/false);
17282 decl_specs
->any_specifiers_p
= true;
17284 /* Consume the token. */
17285 cp_lexer_consume_token (parser
->lexer
);
17287 if (type
== error_mark_node
)
17288 return error_mark_node
;
17290 /* There is no valid C++ program where a non-template type is
17291 followed by a "<". That usually indicates that the user thought
17292 that the type was a template. */
17293 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17296 return TYPE_NAME (type
);
17299 /* The type-specifier must be a user-defined type. */
17300 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17305 /* Don't gobble tokens or issue error messages if this is an
17306 optional type-specifier. */
17307 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17308 cp_parser_parse_tentatively (parser
);
17310 token
= cp_lexer_peek_token (parser
->lexer
);
17312 /* Look for the optional `::' operator. */
17314 = (cp_parser_global_scope_opt (parser
,
17315 /*current_scope_valid_p=*/false)
17317 /* Look for the nested-name specifier. */
17319 = (cp_parser_nested_name_specifier_opt (parser
,
17320 /*typename_keyword_p=*/false,
17321 /*check_dependency_p=*/true,
17323 /*is_declaration=*/false)
17325 /* If we have seen a nested-name-specifier, and the next token
17326 is `template', then we are using the template-id production. */
17328 && cp_parser_optional_template_keyword (parser
))
17330 /* Look for the template-id. */
17331 type
= cp_parser_template_id (parser
,
17332 /*template_keyword_p=*/true,
17333 /*check_dependency_p=*/true,
17335 /*is_declaration=*/false);
17336 /* If the template-id did not name a type, we are out of
17338 if (TREE_CODE (type
) != TYPE_DECL
)
17340 cp_parser_error (parser
, "expected template-id for type");
17344 /* Otherwise, look for a type-name. */
17346 type
= cp_parser_type_name (parser
);
17347 /* Keep track of all name-lookups performed in class scopes. */
17351 && TREE_CODE (type
) == TYPE_DECL
17352 && identifier_p (DECL_NAME (type
)))
17353 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17354 /* If it didn't work out, we don't have a TYPE. */
17355 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17356 && !cp_parser_parse_definitely (parser
))
17358 if (!type
&& cxx_dialect
>= cxx17
)
17360 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17361 cp_parser_parse_tentatively (parser
);
17363 cp_parser_global_scope_opt (parser
,
17364 /*current_scope_valid_p=*/false);
17365 cp_parser_nested_name_specifier_opt (parser
,
17366 /*typename_keyword_p=*/false,
17367 /*check_dependency_p=*/true,
17369 /*is_declaration=*/false);
17370 tree name
= cp_parser_identifier (parser
);
17371 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17372 && parser
->scope
!= error_mark_node
)
17374 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17376 /*is_template=*/false,
17377 /*is_namespace=*/false,
17378 /*check_dependency=*/true,
17379 /*ambiguous_decls=*/NULL
,
17381 if (tmpl
&& tmpl
!= error_mark_node
17382 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17383 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17384 type
= make_template_placeholder (tmpl
);
17387 type
= error_mark_node
;
17388 if (!cp_parser_simulate_error (parser
))
17389 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17390 NLE_TYPE
, token
->location
);
17394 type
= error_mark_node
;
17396 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17397 && !cp_parser_parse_definitely (parser
))
17400 if (type
&& decl_specs
)
17401 cp_parser_set_decl_spec_type (decl_specs
, type
,
17403 /*type_definition_p=*/false);
17406 /* If we didn't get a type-name, issue an error message. */
17407 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17409 cp_parser_error (parser
, "expected type-name");
17410 return error_mark_node
;
17413 if (type
&& type
!= error_mark_node
)
17415 /* See if TYPE is an Objective-C type, and if so, parse and
17416 accept any protocol references following it. Do this before
17417 the cp_parser_check_for_invalid_template_id() call, because
17418 Objective-C types can be followed by '<...>' which would
17419 enclose protocol names rather than template arguments, and so
17420 everything is fine. */
17421 if (c_dialect_objc () && !parser
->scope
17422 && (objc_is_id (type
) || objc_is_class_name (type
)))
17424 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17425 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17427 /* Clobber the "unqualified" type previously entered into
17428 DECL_SPECS with the new, improved protocol-qualified version. */
17430 decl_specs
->type
= qual_type
;
17435 /* There is no valid C++ program where a non-template type is
17436 followed by a "<". That usually indicates that the user
17437 thought that the type was a template. */
17438 cp_parser_check_for_invalid_template_id (parser
, type
,
17446 /* Parse a type-name.
17452 simple-template-id [in c++0x]
17469 Returns a TYPE_DECL for the type. */
17472 cp_parser_type_name (cp_parser
* parser
)
17474 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17479 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17483 /* We can't know yet whether it is a class-name or not. */
17484 cp_parser_parse_tentatively (parser
);
17485 /* Try a class-name. */
17486 type_decl
= cp_parser_class_name (parser
,
17487 typename_keyword_p
,
17488 /*template_keyword_p=*/false,
17490 /*check_dependency_p=*/true,
17491 /*class_head_p=*/false,
17492 /*is_declaration=*/false);
17493 /* If it's not a class-name, keep looking. */
17494 if (!cp_parser_parse_definitely (parser
))
17496 if (cxx_dialect
< cxx11
)
17497 /* It must be a typedef-name or an enum-name. */
17498 return cp_parser_nonclass_name (parser
);
17500 cp_parser_parse_tentatively (parser
);
17501 /* It is either a simple-template-id representing an
17502 instantiation of an alias template... */
17503 type_decl
= cp_parser_template_id (parser
,
17504 /*template_keyword_p=*/false,
17505 /*check_dependency_p=*/true,
17507 /*is_declaration=*/false);
17508 /* Note that this must be an instantiation of an alias template
17509 because [temp.names]/6 says:
17511 A template-id that names an alias template specialization
17514 Whereas [temp.names]/7 says:
17516 A simple-template-id that names a class template
17517 specialization is a class-name.
17519 With concepts, this could also be a partial-concept-id that
17520 declares a non-type template parameter. */
17521 if (type_decl
!= NULL_TREE
17522 && TREE_CODE (type_decl
) == TYPE_DECL
17523 && TYPE_DECL_ALIAS_P (type_decl
))
17524 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17525 else if (is_constrained_parameter (type_decl
))
17526 /* Don't do anything. */ ;
17528 cp_parser_simulate_error (parser
);
17530 if (!cp_parser_parse_definitely (parser
))
17531 /* ... Or a typedef-name or an enum-name. */
17532 return cp_parser_nonclass_name (parser
);
17538 /* Check if DECL and ARGS can form a constrained-type-specifier.
17539 If ARGS is non-null, we try to form a concept check of the
17540 form DECL<?, ARGS> where ? is a wildcard that matches any
17541 kind of template argument. If ARGS is NULL, then we try to
17542 form a concept check of the form DECL<?>. */
17545 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17546 tree decl
, tree args
)
17548 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17550 /* If we a constrained-type-specifier cannot be deduced. */
17551 if (parser
->prevent_constrained_type_specifiers
)
17554 /* A constrained type specifier can only be found in an
17555 overload set or as a reference to a template declaration.
17557 FIXME: This might be masking a bug. It's possible that
17558 that the deduction below is causing template specializations
17559 to be formed with the wildcard as an argument. */
17560 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17563 /* Try to build a call expression that evaluates the
17564 concept. This can fail if the overload set refers
17565 only to non-templates. */
17566 tree placeholder
= build_nt (WILDCARD_DECL
);
17567 tree check
= build_concept_check (decl
, placeholder
, args
);
17568 if (check
== error_mark_node
)
17571 /* Deduce the checked constraint and the prototype parameter.
17573 FIXME: In certain cases, failure to deduce should be a
17574 diagnosable error. */
17577 if (!deduce_constrained_parameter (check
, conc
, proto
))
17580 /* In template parameter scope, this results in a constrained
17581 parameter. Return a descriptor of that parm. */
17582 if (processing_template_parmlist
)
17583 return build_constrained_parameter (conc
, proto
, args
);
17585 /* In a parameter-declaration-clause, constrained-type
17586 specifiers result in invented template parameters. */
17587 if (parser
->auto_is_implicit_function_template_parm_p
)
17589 tree x
= build_constrained_parameter (conc
, proto
, args
);
17590 return synthesize_implicit_template_parm (parser
, x
);
17594 /* Otherwise, we're in a context where the constrained
17595 type name is deduced and the constraint applies
17596 after deduction. */
17597 return make_constrained_auto (conc
, args
);
17603 /* If DECL refers to a concept, return a TYPE_DECL representing
17604 the result of using the constrained type specifier in the
17605 current context. DECL refers to a concept if
17607 - it is an overload set containing a function concept taking a single
17610 - it is a variable concept taking a single type argument. */
17613 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17616 && (TREE_CODE (decl
) == OVERLOAD
17617 || BASELINK_P (decl
)
17618 || variable_concept_p (decl
)))
17619 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17624 /* Check if DECL and ARGS form a partial-concept-id. If so,
17625 assign ID to the resulting constrained placeholder.
17627 Returns true if the partial-concept-id designates a placeholder
17628 and false otherwise. Note that *id is set to NULL_TREE in
17632 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17634 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17637 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17649 Returns a TYPE_DECL for the type. */
17652 cp_parser_nonclass_name (cp_parser
* parser
)
17657 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17658 identifier
= cp_parser_identifier (parser
);
17659 if (identifier
== error_mark_node
)
17660 return error_mark_node
;
17662 /* Look up the type-name. */
17663 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17665 type_decl
= strip_using_decl (type_decl
);
17667 /* If we found an overload set, then it may refer to a concept-name. */
17668 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17671 if (TREE_CODE (type_decl
) != TYPE_DECL
17672 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17674 /* See if this is an Objective-C type. */
17675 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17676 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17678 type_decl
= TYPE_NAME (type
);
17681 /* Issue an error if we did not find a type-name. */
17682 if (TREE_CODE (type_decl
) != TYPE_DECL
17683 /* In Objective-C, we have the complication that class names are
17684 normally type names and start declarations (eg, the
17685 "NSObject" in "NSObject *object;"), but can be used in an
17686 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17687 is an expression. So, a classname followed by a dot is not a
17688 valid type-name. */
17689 || (objc_is_class_name (TREE_TYPE (type_decl
))
17690 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17692 if (!cp_parser_simulate_error (parser
))
17693 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17694 NLE_TYPE
, token
->location
);
17695 return error_mark_node
;
17697 /* Remember that the name was used in the definition of the
17698 current class so that we can check later to see if the
17699 meaning would have been different after the class was
17700 entirely defined. */
17701 else if (type_decl
!= error_mark_node
17703 maybe_note_name_used_in_class (identifier
, type_decl
);
17708 /* Parse an elaborated-type-specifier. Note that the grammar given
17709 here incorporates the resolution to DR68.
17711 elaborated-type-specifier:
17712 class-key :: [opt] nested-name-specifier [opt] identifier
17713 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17714 enum-key :: [opt] nested-name-specifier [opt] identifier
17715 typename :: [opt] nested-name-specifier identifier
17716 typename :: [opt] nested-name-specifier template [opt]
17721 elaborated-type-specifier:
17722 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17723 class-key attributes :: [opt] nested-name-specifier [opt]
17724 template [opt] template-id
17725 enum attributes :: [opt] nested-name-specifier [opt] identifier
17727 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17728 declared `friend'. If IS_DECLARATION is TRUE, then this
17729 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17730 something is being declared.
17732 Returns the TYPE specified. */
17735 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17737 bool is_declaration
)
17739 enum tag_types tag_type
;
17741 tree type
= NULL_TREE
;
17742 tree attributes
= NULL_TREE
;
17744 cp_token
*token
= NULL
;
17746 /* See if we're looking at the `enum' keyword. */
17747 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17749 /* Consume the `enum' token. */
17750 cp_lexer_consume_token (parser
->lexer
);
17751 /* Remember that it's an enumeration type. */
17752 tag_type
= enum_type
;
17753 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17754 enums) is used here. */
17755 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17756 if (cp_parser_is_keyword (token
, RID_CLASS
)
17757 || cp_parser_is_keyword (token
, RID_STRUCT
))
17759 gcc_rich_location
richloc (token
->location
);
17760 richloc
.add_range (input_location
);
17761 richloc
.add_fixit_remove ();
17762 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17763 "a scoped enum must not use the %qD keyword",
17765 /* Consume the `struct' or `class' and parse it anyway. */
17766 cp_lexer_consume_token (parser
->lexer
);
17768 /* Parse the attributes. */
17769 attributes
= cp_parser_attributes_opt (parser
);
17771 /* Or, it might be `typename'. */
17772 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17775 /* Consume the `typename' token. */
17776 cp_lexer_consume_token (parser
->lexer
);
17777 /* Remember that it's a `typename' type. */
17778 tag_type
= typename_type
;
17780 /* Otherwise it must be a class-key. */
17783 tag_type
= cp_parser_class_key (parser
);
17784 if (tag_type
== none_type
)
17785 return error_mark_node
;
17786 /* Parse the attributes. */
17787 attributes
= cp_parser_attributes_opt (parser
);
17790 /* Look for the `::' operator. */
17791 globalscope
= cp_parser_global_scope_opt (parser
,
17792 /*current_scope_valid_p=*/false);
17793 /* Look for the nested-name-specifier. */
17794 tree nested_name_specifier
;
17795 if (tag_type
== typename_type
&& !globalscope
)
17797 nested_name_specifier
17798 = cp_parser_nested_name_specifier (parser
,
17799 /*typename_keyword_p=*/true,
17800 /*check_dependency_p=*/true,
17803 if (!nested_name_specifier
)
17804 return error_mark_node
;
17807 /* Even though `typename' is not present, the proposed resolution
17808 to Core Issue 180 says that in `class A<T>::B', `B' should be
17809 considered a type-name, even if `A<T>' is dependent. */
17810 nested_name_specifier
17811 = cp_parser_nested_name_specifier_opt (parser
,
17812 /*typename_keyword_p=*/true,
17813 /*check_dependency_p=*/true,
17816 /* For everything but enumeration types, consider a template-id.
17817 For an enumeration type, consider only a plain identifier. */
17818 if (tag_type
!= enum_type
)
17820 bool template_p
= false;
17823 /* Allow the `template' keyword. */
17824 template_p
= cp_parser_optional_template_keyword (parser
);
17825 /* If we didn't see `template', we don't know if there's a
17826 template-id or not. */
17828 cp_parser_parse_tentatively (parser
);
17829 /* Parse the template-id. */
17830 token
= cp_lexer_peek_token (parser
->lexer
);
17831 decl
= cp_parser_template_id (parser
, template_p
,
17832 /*check_dependency_p=*/true,
17835 /* If we didn't find a template-id, look for an ordinary
17837 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17839 /* We can get here when cp_parser_template_id, called by
17840 cp_parser_class_name with tag_type == none_type, succeeds
17841 and caches a BASELINK. Then, when called again here,
17842 instead of failing and returning an error_mark_node
17843 returns it (see template/typename17.C in C++11).
17844 ??? Could we diagnose this earlier? */
17845 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17847 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17848 type
= error_mark_node
;
17850 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17851 in effect, then we must assume that, upon instantiation, the
17852 template will correspond to a class. */
17853 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17854 && tag_type
== typename_type
)
17855 type
= make_typename_type (parser
->scope
, decl
,
17857 /*complain=*/tf_error
);
17858 /* If the `typename' keyword is in effect and DECL is not a type
17859 decl, then type is non existent. */
17860 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17862 else if (TREE_CODE (decl
) == TYPE_DECL
)
17864 type
= check_elaborated_type_specifier (tag_type
, decl
,
17865 /*allow_template_p=*/true);
17867 /* If the next token is a semicolon, this must be a specialization,
17868 instantiation, or friend declaration. Check the scope while we
17869 still know whether or not we had a nested-name-specifier. */
17870 if (type
!= error_mark_node
17871 && !nested_name_specifier
&& !is_friend
17872 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17873 check_unqualified_spec_or_inst (type
, token
->location
);
17875 else if (decl
== error_mark_node
)
17876 type
= error_mark_node
;
17881 token
= cp_lexer_peek_token (parser
->lexer
);
17882 identifier
= cp_parser_identifier (parser
);
17884 if (identifier
== error_mark_node
)
17886 parser
->scope
= NULL_TREE
;
17887 return error_mark_node
;
17890 /* For a `typename', we needn't call xref_tag. */
17891 if (tag_type
== typename_type
17892 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17893 return cp_parser_make_typename_type (parser
, identifier
,
17896 /* Template parameter lists apply only if we are not within a
17897 function parameter list. */
17898 bool template_parm_lists_apply
17899 = parser
->num_template_parameter_lists
;
17900 if (template_parm_lists_apply
)
17901 for (cp_binding_level
*s
= current_binding_level
;
17902 s
&& s
->kind
!= sk_template_parms
;
17903 s
= s
->level_chain
)
17904 if (s
->kind
== sk_function_parms
)
17905 template_parm_lists_apply
= false;
17907 /* Look up a qualified name in the usual way. */
17911 tree ambiguous_decls
;
17913 decl
= cp_parser_lookup_name (parser
, identifier
,
17915 /*is_template=*/false,
17916 /*is_namespace=*/false,
17917 /*check_dependency=*/true,
17921 /* If the lookup was ambiguous, an error will already have been
17923 if (ambiguous_decls
)
17924 return error_mark_node
;
17926 /* If we are parsing friend declaration, DECL may be a
17927 TEMPLATE_DECL tree node here. However, we need to check
17928 whether this TEMPLATE_DECL results in valid code. Consider
17929 the following example:
17932 template <class T> class C {};
17935 template <class T> friend class N::C; // #1, valid code
17937 template <class T> class Y {
17938 friend class N::C; // #2, invalid code
17941 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17942 name lookup of `N::C'. We see that friend declaration must
17943 be template for the code to be valid. Note that
17944 processing_template_decl does not work here since it is
17945 always 1 for the above two cases. */
17947 decl
= (cp_parser_maybe_treat_template_as_class
17948 (decl
, /*tag_name_p=*/is_friend
17949 && template_parm_lists_apply
));
17951 if (TREE_CODE (decl
) != TYPE_DECL
)
17953 cp_parser_diagnose_invalid_type_name (parser
,
17956 return error_mark_node
;
17959 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17961 bool allow_template
= (template_parm_lists_apply
17962 || DECL_SELF_REFERENCE_P (decl
));
17963 type
= check_elaborated_type_specifier (tag_type
, decl
,
17966 if (type
== error_mark_node
)
17967 return error_mark_node
;
17970 /* Forward declarations of nested types, such as
17975 are invalid unless all components preceding the final '::'
17976 are complete. If all enclosing types are complete, these
17977 declarations become merely pointless.
17979 Invalid forward declarations of nested types are errors
17980 caught elsewhere in parsing. Those that are pointless arrive
17983 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17984 && !is_friend
&& !processing_explicit_instantiation
)
17985 warning (0, "declaration %qD does not declare anything", decl
);
17987 type
= TREE_TYPE (decl
);
17991 /* An elaborated-type-specifier sometimes introduces a new type and
17992 sometimes names an existing type. Normally, the rule is that it
17993 introduces a new type only if there is not an existing type of
17994 the same name already in scope. For example, given:
17997 void f() { struct S s; }
17999 the `struct S' in the body of `f' is the same `struct S' as in
18000 the global scope; the existing definition is used. However, if
18001 there were no global declaration, this would introduce a new
18002 local class named `S'.
18004 An exception to this rule applies to the following code:
18006 namespace N { struct S; }
18008 Here, the elaborated-type-specifier names a new type
18009 unconditionally; even if there is already an `S' in the
18010 containing scope this declaration names a new type.
18011 This exception only applies if the elaborated-type-specifier
18012 forms the complete declaration:
18016 A declaration consisting solely of `class-key identifier ;' is
18017 either a redeclaration of the name in the current scope or a
18018 forward declaration of the identifier as a class name. It
18019 introduces the name into the current scope.
18021 We are in this situation precisely when the next token is a `;'.
18023 An exception to the exception is that a `friend' declaration does
18024 *not* name a new type; i.e., given:
18026 struct S { friend struct T; };
18028 `T' is not a new type in the scope of `S'.
18030 Also, `new struct S' or `sizeof (struct S)' never results in the
18031 definition of a new type; a new type can only be declared in a
18032 declaration context. */
18038 /* Friends have special name lookup rules. */
18039 ts
= ts_within_enclosing_non_class
;
18040 else if (is_declaration
18041 && cp_lexer_next_token_is (parser
->lexer
,
18043 /* This is a `class-key identifier ;' */
18049 (template_parm_lists_apply
18050 && (cp_parser_next_token_starts_class_definition_p (parser
)
18051 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18052 /* An unqualified name was used to reference this type, so
18053 there were no qualifying templates. */
18054 if (template_parm_lists_apply
18055 && !cp_parser_check_template_parameters (parser
,
18056 /*num_templates=*/0,
18057 /*template_id*/false,
18059 /*declarator=*/NULL
))
18060 return error_mark_node
;
18061 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18065 if (type
== error_mark_node
)
18066 return error_mark_node
;
18068 /* Allow attributes on forward declarations of classes. */
18071 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18072 warning (OPT_Wattributes
,
18073 "attributes ignored on uninstantiated type");
18074 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18075 && ! processing_explicit_instantiation
)
18076 warning (OPT_Wattributes
,
18077 "attributes ignored on template instantiation");
18078 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18079 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18081 warning (OPT_Wattributes
,
18082 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18085 if (tag_type
!= enum_type
)
18087 /* Indicate whether this class was declared as a `class' or as a
18089 if (CLASS_TYPE_P (type
))
18090 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18091 cp_parser_check_class_key (tag_type
, type
);
18094 /* A "<" cannot follow an elaborated type specifier. If that
18095 happens, the user was probably trying to form a template-id. */
18096 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18102 /* Parse an enum-specifier.
18105 enum-head { enumerator-list [opt] }
18106 enum-head { enumerator-list , } [C++0x]
18109 enum-key identifier [opt] enum-base [opt]
18110 enum-key nested-name-specifier identifier enum-base [opt]
18115 enum struct [C++0x]
18118 : type-specifier-seq
18120 opaque-enum-specifier:
18121 enum-key identifier enum-base [opt] ;
18124 enum-key attributes[opt] identifier [opt] enum-base [opt]
18125 { enumerator-list [opt] }attributes[opt]
18126 enum-key attributes[opt] identifier [opt] enum-base [opt]
18127 { enumerator-list, }attributes[opt] [C++0x]
18129 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18130 if the token stream isn't an enum-specifier after all. */
18133 cp_parser_enum_specifier (cp_parser
* parser
)
18136 tree type
= NULL_TREE
;
18138 tree nested_name_specifier
= NULL_TREE
;
18140 bool scoped_enum_p
= false;
18141 bool has_underlying_type
= false;
18142 bool nested_being_defined
= false;
18143 bool new_value_list
= false;
18144 bool is_new_type
= false;
18145 bool is_unnamed
= false;
18146 tree underlying_type
= NULL_TREE
;
18147 cp_token
*type_start_token
= NULL
;
18148 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18150 parser
->colon_corrects_to_scope_p
= false;
18152 /* Parse tentatively so that we can back up if we don't find a
18154 cp_parser_parse_tentatively (parser
);
18156 /* Caller guarantees that the current token is 'enum', an identifier
18157 possibly follows, and the token after that is an opening brace.
18158 If we don't have an identifier, fabricate an anonymous name for
18159 the enumeration being defined. */
18160 cp_lexer_consume_token (parser
->lexer
);
18162 /* Parse the "class" or "struct", which indicates a scoped
18163 enumeration type in C++0x. */
18164 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18165 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18167 if (cxx_dialect
< cxx11
)
18168 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18170 /* Consume the `struct' or `class' token. */
18171 cp_lexer_consume_token (parser
->lexer
);
18173 scoped_enum_p
= true;
18176 attributes
= cp_parser_attributes_opt (parser
);
18178 /* Clear the qualification. */
18179 parser
->scope
= NULL_TREE
;
18180 parser
->qualifying_scope
= NULL_TREE
;
18181 parser
->object_scope
= NULL_TREE
;
18183 /* Figure out in what scope the declaration is being placed. */
18184 prev_scope
= current_scope ();
18186 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18188 push_deferring_access_checks (dk_no_check
);
18189 nested_name_specifier
18190 = cp_parser_nested_name_specifier_opt (parser
,
18191 /*typename_keyword_p=*/true,
18192 /*check_dependency_p=*/false,
18194 /*is_declaration=*/false);
18196 if (nested_name_specifier
)
18200 identifier
= cp_parser_identifier (parser
);
18201 name
= cp_parser_lookup_name (parser
, identifier
,
18203 /*is_template=*/false,
18204 /*is_namespace=*/false,
18205 /*check_dependency=*/true,
18206 /*ambiguous_decls=*/NULL
,
18208 if (name
&& name
!= error_mark_node
)
18210 type
= TREE_TYPE (name
);
18211 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18213 /* Are template enums allowed in ISO? */
18214 if (template_parm_scope_p ())
18215 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18216 "%qD is an enumeration template", name
);
18217 /* ignore a typename reference, for it will be solved by name
18222 else if (nested_name_specifier
== error_mark_node
)
18223 /* We already issued an error. */;
18226 error_at (type_start_token
->location
,
18227 "%qD does not name an enumeration in %qT",
18228 identifier
, nested_name_specifier
);
18229 nested_name_specifier
= error_mark_node
;
18234 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18235 identifier
= cp_parser_identifier (parser
);
18238 identifier
= make_anon_name ();
18241 error_at (type_start_token
->location
,
18242 "unnamed scoped enum is not allowed");
18245 pop_deferring_access_checks ();
18247 /* Check for the `:' that denotes a specified underlying type in C++0x.
18248 Note that a ':' could also indicate a bitfield width, however. */
18249 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18251 cp_decl_specifier_seq type_specifiers
;
18253 /* Consume the `:'. */
18254 cp_lexer_consume_token (parser
->lexer
);
18256 /* Parse the type-specifier-seq. */
18257 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18258 /*is_trailing_return=*/false,
18261 /* At this point this is surely not elaborated type specifier. */
18262 if (!cp_parser_parse_definitely (parser
))
18265 if (cxx_dialect
< cxx11
)
18266 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18268 has_underlying_type
= true;
18270 /* If that didn't work, stop. */
18271 if (type_specifiers
.type
!= error_mark_node
)
18273 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18274 /*initialized=*/0, NULL
);
18275 if (underlying_type
== error_mark_node
18276 || check_for_bare_parameter_packs (underlying_type
))
18277 underlying_type
= NULL_TREE
;
18281 /* Look for the `{' but don't consume it yet. */
18282 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18284 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18286 cp_parser_error (parser
, "expected %<{%>");
18287 if (has_underlying_type
)
18293 /* An opaque-enum-specifier must have a ';' here. */
18294 if ((scoped_enum_p
|| underlying_type
)
18295 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18297 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18298 if (has_underlying_type
)
18306 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18309 if (nested_name_specifier
)
18311 if (CLASS_TYPE_P (nested_name_specifier
))
18313 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18314 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18315 push_scope (nested_name_specifier
);
18317 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18319 push_nested_namespace (nested_name_specifier
);
18323 /* Issue an error message if type-definitions are forbidden here. */
18324 if (!cp_parser_check_type_definition (parser
))
18325 type
= error_mark_node
;
18327 /* Create the new type. We do this before consuming the opening
18328 brace so the enum will be recorded as being on the line of its
18329 tag (or the 'enum' keyword, if there is no tag). */
18330 type
= start_enum (identifier
, type
, underlying_type
,
18331 attributes
, scoped_enum_p
, &is_new_type
);
18333 /* If the next token is not '{' it is an opaque-enum-specifier or an
18334 elaborated-type-specifier. */
18335 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18337 timevar_push (TV_PARSE_ENUM
);
18338 if (nested_name_specifier
18339 && nested_name_specifier
!= error_mark_node
)
18341 /* The following catches invalid code such as:
18342 enum class S<int>::E { A, B, C }; */
18343 if (!processing_specialization
18344 && CLASS_TYPE_P (nested_name_specifier
)
18345 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18346 error_at (type_start_token
->location
, "cannot add an enumerator "
18347 "list to a template instantiation");
18349 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18351 error_at (type_start_token
->location
,
18352 "%<%T::%E%> has not been declared",
18353 TYPE_CONTEXT (nested_name_specifier
),
18354 nested_name_specifier
);
18355 type
= error_mark_node
;
18357 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18358 && !CLASS_TYPE_P (nested_name_specifier
))
18360 error_at (type_start_token
->location
, "nested name specifier "
18361 "%qT for enum declaration does not name a class "
18362 "or namespace", nested_name_specifier
);
18363 type
= error_mark_node
;
18365 /* If that scope does not contain the scope in which the
18366 class was originally declared, the program is invalid. */
18367 else if (prev_scope
&& !is_ancestor (prev_scope
,
18368 nested_name_specifier
))
18370 if (at_namespace_scope_p ())
18371 error_at (type_start_token
->location
,
18372 "declaration of %qD in namespace %qD which does not "
18374 type
, prev_scope
, nested_name_specifier
);
18376 error_at (type_start_token
->location
,
18377 "declaration of %qD in %qD which does not "
18379 type
, prev_scope
, nested_name_specifier
);
18380 type
= error_mark_node
;
18382 /* If that scope is the scope where the declaration is being placed
18383 the program is invalid. */
18384 else if (CLASS_TYPE_P (nested_name_specifier
)
18385 && CLASS_TYPE_P (prev_scope
)
18386 && same_type_p (nested_name_specifier
, prev_scope
))
18388 permerror (type_start_token
->location
,
18389 "extra qualification not allowed");
18390 nested_name_specifier
= NULL_TREE
;
18395 begin_scope (sk_scoped_enum
, type
);
18397 /* Consume the opening brace. */
18398 matching_braces braces
;
18399 braces
.consume_open (parser
);
18401 if (type
== error_mark_node
)
18402 ; /* Nothing to add */
18403 else if (OPAQUE_ENUM_P (type
)
18404 || (cxx_dialect
> cxx98
&& processing_specialization
))
18406 new_value_list
= true;
18407 SET_OPAQUE_ENUM_P (type
, false);
18408 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18412 error_at (type_start_token
->location
,
18413 "multiple definition of %q#T", type
);
18414 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18415 "previous definition here");
18416 type
= error_mark_node
;
18419 if (type
== error_mark_node
)
18420 cp_parser_skip_to_end_of_block_or_statement (parser
);
18421 /* If the next token is not '}', then there are some enumerators. */
18422 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18424 if (is_unnamed
&& !scoped_enum_p
)
18425 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18426 "ISO C++ forbids empty unnamed enum");
18429 cp_parser_enumerator_list (parser
, type
);
18431 /* Consume the final '}'. */
18432 braces
.require_close (parser
);
18436 timevar_pop (TV_PARSE_ENUM
);
18440 /* If a ';' follows, then it is an opaque-enum-specifier
18441 and additional restrictions apply. */
18442 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18445 error_at (type_start_token
->location
,
18446 "opaque-enum-specifier without name");
18447 else if (nested_name_specifier
)
18448 error_at (type_start_token
->location
,
18449 "opaque-enum-specifier must use a simple identifier");
18453 /* Look for trailing attributes to apply to this enumeration, and
18454 apply them if appropriate. */
18455 if (cp_parser_allow_gnu_extensions_p (parser
))
18457 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18458 cplus_decl_attributes (&type
,
18460 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18463 /* Finish up the enumeration. */
18464 if (type
!= error_mark_node
)
18466 if (new_value_list
)
18467 finish_enum_value_list (type
);
18469 finish_enum (type
);
18472 if (nested_name_specifier
)
18474 if (CLASS_TYPE_P (nested_name_specifier
))
18476 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18477 pop_scope (nested_name_specifier
);
18479 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18481 pop_nested_namespace (nested_name_specifier
);
18485 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18489 /* Parse an enumerator-list. The enumerators all have the indicated
18493 enumerator-definition
18494 enumerator-list , enumerator-definition */
18497 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18501 /* Parse an enumerator-definition. */
18502 cp_parser_enumerator_definition (parser
, type
);
18504 /* If the next token is not a ',', we've reached the end of
18506 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18508 /* Otherwise, consume the `,' and keep going. */
18509 cp_lexer_consume_token (parser
->lexer
);
18510 /* If the next token is a `}', there is a trailing comma. */
18511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18513 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18514 pedwarn (input_location
, OPT_Wpedantic
,
18515 "comma at end of enumerator list");
18521 /* Parse an enumerator-definition. The enumerator has the indicated
18524 enumerator-definition:
18526 enumerator = constant-expression
18533 enumerator-definition:
18534 enumerator attributes [opt]
18535 enumerator attributes [opt] = constant-expression */
18538 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18544 /* Save the input location because we are interested in the location
18545 of the identifier and not the location of the explicit value. */
18546 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18548 /* Look for the identifier. */
18549 identifier
= cp_parser_identifier (parser
);
18550 if (identifier
== error_mark_node
)
18553 /* Parse any specified attributes. */
18554 tree attrs
= cp_parser_attributes_opt (parser
);
18556 /* If the next token is an '=', then there is an explicit value. */
18557 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18559 /* Consume the `=' token. */
18560 cp_lexer_consume_token (parser
->lexer
);
18561 /* Parse the value. */
18562 value
= cp_parser_constant_expression (parser
);
18567 /* If we are processing a template, make sure the initializer of the
18568 enumerator doesn't contain any bare template parameter pack. */
18569 if (check_for_bare_parameter_packs (value
))
18570 value
= error_mark_node
;
18572 /* Create the enumerator. */
18573 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18576 /* Parse a namespace-name.
18579 original-namespace-name
18582 Returns the NAMESPACE_DECL for the namespace. */
18585 cp_parser_namespace_name (cp_parser
* parser
)
18588 tree namespace_decl
;
18590 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18592 /* Get the name of the namespace. */
18593 identifier
= cp_parser_identifier (parser
);
18594 if (identifier
== error_mark_node
)
18595 return error_mark_node
;
18597 /* Look up the identifier in the currently active scope. Look only
18598 for namespaces, due to:
18600 [basic.lookup.udir]
18602 When looking up a namespace-name in a using-directive or alias
18603 definition, only namespace names are considered.
18607 [basic.lookup.qual]
18609 During the lookup of a name preceding the :: scope resolution
18610 operator, object, function, and enumerator names are ignored.
18612 (Note that cp_parser_qualifying_entity only calls this
18613 function if the token after the name is the scope resolution
18615 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18617 /*is_template=*/false,
18618 /*is_namespace=*/true,
18619 /*check_dependency=*/true,
18620 /*ambiguous_decls=*/NULL
,
18622 /* If it's not a namespace, issue an error. */
18623 if (namespace_decl
== error_mark_node
18624 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18626 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18628 auto_diagnostic_group d
;
18629 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18630 if (namespace_decl
== error_mark_node
18631 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18632 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18635 cp_parser_error (parser
, "expected namespace-name");
18636 namespace_decl
= error_mark_node
;
18639 return namespace_decl
;
18642 /* Parse a namespace-definition.
18644 namespace-definition:
18645 named-namespace-definition
18646 unnamed-namespace-definition
18648 named-namespace-definition:
18649 original-namespace-definition
18650 extension-namespace-definition
18652 original-namespace-definition:
18653 namespace identifier { namespace-body }
18655 extension-namespace-definition:
18656 namespace original-namespace-name { namespace-body }
18658 unnamed-namespace-definition:
18659 namespace { namespace-body } */
18662 cp_parser_namespace_definition (cp_parser
* parser
)
18665 int nested_definition_count
= 0;
18667 cp_ensure_no_omp_declare_simd (parser
);
18668 cp_ensure_no_oacc_routine (parser
);
18670 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18674 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18675 cp_lexer_consume_token (parser
->lexer
);
18678 /* Look for the `namespace' keyword. */
18680 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18682 /* Parse any specified attributes before the identifier. */
18683 tree attribs
= cp_parser_attributes_opt (parser
);
18687 identifier
= NULL_TREE
;
18689 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18691 identifier
= cp_parser_identifier (parser
);
18693 if (cp_next_tokens_can_be_std_attribute_p (parser
))
18694 pedwarn (input_location
, OPT_Wpedantic
,
18695 "standard attributes on namespaces must precede "
18696 "the namespace name");
18698 /* Parse any attributes specified after the identifier. */
18699 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18702 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18705 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18706 pedwarn (input_location
, OPT_Wpedantic
,
18707 "nested namespace definitions only available with "
18708 "-std=c++17 or -std=gnu++17");
18710 /* Nested namespace names can create new namespaces (unlike
18711 other qualified-ids). */
18712 if (int count
= identifier
? push_namespace (identifier
) : 0)
18713 nested_definition_count
+= count
;
18715 cp_parser_error (parser
, "nested namespace name required");
18716 cp_lexer_consume_token (parser
->lexer
);
18719 if (nested_definition_count
&& !identifier
)
18720 cp_parser_error (parser
, "namespace name required");
18722 if (nested_definition_count
&& attribs
)
18723 error_at (token
->location
,
18724 "a nested namespace definition cannot have attributes");
18725 if (nested_definition_count
&& is_inline
)
18726 error_at (token
->location
,
18727 "a nested namespace definition cannot be inline");
18729 /* Start the namespace. */
18730 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18732 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18734 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18736 /* Look for the `{' to validate starting the namespace. */
18737 matching_braces braces
;
18738 if (braces
.require_open (parser
))
18740 /* Parse the body of the namespace. */
18741 cp_parser_namespace_body (parser
);
18743 /* Look for the final `}'. */
18744 braces
.require_close (parser
);
18747 if (has_visibility
)
18748 pop_visibility (1);
18750 /* Pop the nested namespace definitions. */
18751 while (nested_definition_count
--)
18755 /* Parse a namespace-body.
18758 declaration-seq [opt] */
18761 cp_parser_namespace_body (cp_parser
* parser
)
18763 cp_parser_declaration_seq_opt (parser
);
18766 /* Parse a namespace-alias-definition.
18768 namespace-alias-definition:
18769 namespace identifier = qualified-namespace-specifier ; */
18772 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18775 tree namespace_specifier
;
18777 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18779 /* Look for the `namespace' keyword. */
18780 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18781 /* Look for the identifier. */
18782 identifier
= cp_parser_identifier (parser
);
18783 if (identifier
== error_mark_node
)
18785 /* Look for the `=' token. */
18786 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18787 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18789 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18790 /* Skip the definition. */
18791 cp_lexer_consume_token (parser
->lexer
);
18792 if (cp_parser_skip_to_closing_brace (parser
))
18793 cp_lexer_consume_token (parser
->lexer
);
18796 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18797 /* Look for the qualified-namespace-specifier. */
18798 namespace_specifier
18799 = cp_parser_qualified_namespace_specifier (parser
);
18800 /* Look for the `;' token. */
18801 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18803 /* Register the alias in the symbol table. */
18804 do_namespace_alias (identifier
, namespace_specifier
);
18807 /* Parse a qualified-namespace-specifier.
18809 qualified-namespace-specifier:
18810 :: [opt] nested-name-specifier [opt] namespace-name
18812 Returns a NAMESPACE_DECL corresponding to the specified
18816 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18818 /* Look for the optional `::'. */
18819 cp_parser_global_scope_opt (parser
,
18820 /*current_scope_valid_p=*/false);
18822 /* Look for the optional nested-name-specifier. */
18823 cp_parser_nested_name_specifier_opt (parser
,
18824 /*typename_keyword_p=*/false,
18825 /*check_dependency_p=*/true,
18827 /*is_declaration=*/true);
18829 return cp_parser_namespace_name (parser
);
18832 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18833 access declaration.
18836 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18837 using :: unqualified-id ;
18839 access-declaration:
18845 cp_parser_using_declaration (cp_parser
* parser
,
18846 bool access_declaration_p
)
18849 bool typename_p
= false;
18850 bool global_scope_p
;
18854 int oldcount
= errorcount
;
18855 cp_token
*diag_token
= NULL
;
18857 if (access_declaration_p
)
18859 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18860 cp_parser_parse_tentatively (parser
);
18864 /* Look for the `using' keyword. */
18865 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18868 /* Peek at the next token. */
18869 token
= cp_lexer_peek_token (parser
->lexer
);
18870 /* See if it's `typename'. */
18871 if (token
->keyword
== RID_TYPENAME
)
18873 /* Remember that we've seen it. */
18875 /* Consume the `typename' token. */
18876 cp_lexer_consume_token (parser
->lexer
);
18880 /* Look for the optional global scope qualification. */
18882 = (cp_parser_global_scope_opt (parser
,
18883 /*current_scope_valid_p=*/false)
18886 /* If we saw `typename', or didn't see `::', then there must be a
18887 nested-name-specifier present. */
18888 if (typename_p
|| !global_scope_p
)
18890 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18891 /*check_dependency_p=*/true,
18893 /*is_declaration=*/true);
18894 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18896 cp_parser_skip_to_end_of_block_or_statement (parser
);
18900 /* Otherwise, we could be in either of the two productions. In that
18901 case, treat the nested-name-specifier as optional. */
18903 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18904 /*typename_keyword_p=*/false,
18905 /*check_dependency_p=*/true,
18907 /*is_declaration=*/true);
18909 qscope
= global_namespace
;
18910 else if (UNSCOPED_ENUM_P (qscope
))
18911 qscope
= CP_TYPE_CONTEXT (qscope
);
18913 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18914 /* Something has already gone wrong; there's no need to parse
18915 further. Since an error has occurred, the return value of
18916 cp_parser_parse_definitely will be false, as required. */
18917 return cp_parser_parse_definitely (parser
);
18919 token
= cp_lexer_peek_token (parser
->lexer
);
18920 /* Parse the unqualified-id. */
18921 identifier
= cp_parser_unqualified_id (parser
,
18922 /*template_keyword_p=*/false,
18923 /*check_dependency_p=*/true,
18924 /*declarator_p=*/true,
18925 /*optional_p=*/false);
18927 if (access_declaration_p
)
18929 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18930 cp_parser_simulate_error (parser
);
18931 if (!cp_parser_parse_definitely (parser
))
18934 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18936 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18937 if (cxx_dialect
< cxx17
18938 && !in_system_header_at (ell
->location
))
18939 pedwarn (ell
->location
, 0,
18940 "pack expansion in using-declaration only available "
18941 "with -std=c++17 or -std=gnu++17");
18942 qscope
= make_pack_expansion (qscope
);
18945 /* The function we call to handle a using-declaration is different
18946 depending on what scope we are in. */
18947 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18949 else if (!identifier_p (identifier
)
18950 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18951 /* [namespace.udecl]
18953 A using declaration shall not name a template-id. */
18954 error_at (token
->location
,
18955 "a template-id may not appear in a using-declaration");
18958 if (at_class_scope_p ())
18960 /* Create the USING_DECL. */
18961 decl
= do_class_using_decl (qscope
, identifier
);
18963 if (decl
&& typename_p
)
18964 USING_DECL_TYPENAME_P (decl
) = 1;
18966 if (check_for_bare_parameter_packs (decl
))
18968 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18972 /* Add it to the list of members in this class. */
18973 finish_member_declaration (decl
);
18977 decl
= cp_parser_lookup_name_simple (parser
,
18980 if (decl
== error_mark_node
)
18981 cp_parser_name_lookup_error (parser
, identifier
,
18984 else if (check_for_bare_parameter_packs (decl
))
18986 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18989 else if (!at_namespace_scope_p ())
18990 finish_local_using_decl (decl
, qscope
, identifier
);
18992 finish_namespace_using_decl (decl
, qscope
, identifier
);
18996 if (!access_declaration_p
18997 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18999 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
19000 if (cxx_dialect
< cxx17
)
19001 pedwarn (comma
->location
, 0,
19002 "comma-separated list in using-declaration only available "
19003 "with -std=c++17 or -std=gnu++17");
19007 /* Look for the final `;'. */
19008 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19010 if (access_declaration_p
&& errorcount
== oldcount
)
19011 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19012 "access declarations are deprecated "
19013 "in favour of using-declarations; "
19014 "suggestion: add the %<using%> keyword");
19019 /* Parse an alias-declaration.
19022 using identifier attribute-specifier-seq [opt] = type-id */
19025 cp_parser_alias_declaration (cp_parser
* parser
)
19027 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19028 location_t id_location
;
19029 cp_declarator
*declarator
;
19030 cp_decl_specifier_seq decl_specs
;
19032 const char *saved_message
= NULL
;
19034 /* Look for the `using' keyword. */
19035 cp_token
*using_token
19036 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19037 if (using_token
== NULL
)
19038 return error_mark_node
;
19040 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19041 id
= cp_parser_identifier (parser
);
19042 if (id
== error_mark_node
)
19043 return error_mark_node
;
19045 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19046 attributes
= cp_parser_attributes_opt (parser
);
19047 if (attributes
== error_mark_node
)
19048 return error_mark_node
;
19050 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19052 if (cp_parser_error_occurred (parser
))
19053 return error_mark_node
;
19055 cp_parser_commit_to_tentative_parse (parser
);
19057 /* Now we are going to parse the type-id of the declaration. */
19062 "A type-specifier-seq shall not define a class or enumeration
19063 unless it appears in the type-id of an alias-declaration (7.1.3) that
19064 is not the declaration of a template-declaration."
19066 In other words, if we currently are in an alias template, the
19067 type-id should not define a type.
19069 So let's set parser->type_definition_forbidden_message in that
19070 case; cp_parser_check_type_definition (called by
19071 cp_parser_class_specifier) will then emit an error if a type is
19072 defined in the type-id. */
19073 if (parser
->num_template_parameter_lists
)
19075 saved_message
= parser
->type_definition_forbidden_message
;
19076 parser
->type_definition_forbidden_message
=
19077 G_("types may not be defined in alias template declarations");
19080 type
= cp_parser_type_id (parser
);
19082 /* Restore the error message if need be. */
19083 if (parser
->num_template_parameter_lists
)
19084 parser
->type_definition_forbidden_message
= saved_message
;
19086 if (type
== error_mark_node
19087 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19089 cp_parser_skip_to_end_of_block_or_statement (parser
);
19090 return error_mark_node
;
19093 /* A typedef-name can also be introduced by an alias-declaration. The
19094 identifier following the using keyword becomes a typedef-name. It has
19095 the same semantics as if it were introduced by the typedef
19096 specifier. In particular, it does not define a new type and it shall
19097 not appear in the type-id. */
19099 clear_decl_specs (&decl_specs
);
19100 decl_specs
.type
= type
;
19101 if (attributes
!= NULL_TREE
)
19103 decl_specs
.attributes
= attributes
;
19104 set_and_check_decl_spec_loc (&decl_specs
,
19108 set_and_check_decl_spec_loc (&decl_specs
,
19111 set_and_check_decl_spec_loc (&decl_specs
,
19115 if (parser
->num_template_parameter_lists
19116 && !cp_parser_check_template_parameters (parser
,
19117 /*num_templates=*/0,
19118 /*template_id*/false,
19120 /*declarator=*/NULL
))
19121 return error_mark_node
;
19123 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
19124 declarator
->id_loc
= id_location
;
19126 member_p
= at_class_scope_p ();
19128 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19129 NULL_TREE
, attributes
);
19131 decl
= start_decl (declarator
, &decl_specs
, 0,
19132 attributes
, NULL_TREE
, &pushed_scope
);
19133 if (decl
== error_mark_node
)
19136 // Attach constraints to the alias declaration.
19137 if (flag_concepts
&& current_template_parms
)
19139 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19140 tree constr
= build_constraints (reqs
, NULL_TREE
);
19141 set_constraints (decl
, constr
);
19144 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19147 pop_scope (pushed_scope
);
19149 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19150 added into the symbol table; otherwise, return the TYPE_DECL. */
19151 if (DECL_LANG_SPECIFIC (decl
)
19152 && DECL_TEMPLATE_INFO (decl
)
19153 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19155 decl
= DECL_TI_TEMPLATE (decl
);
19157 check_member_template (decl
);
19163 /* Parse a using-directive.
19166 using namespace :: [opt] nested-name-specifier [opt]
19167 namespace-name ; */
19170 cp_parser_using_directive (cp_parser
* parser
)
19172 tree namespace_decl
;
19175 /* Look for the `using' keyword. */
19176 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19177 /* And the `namespace' keyword. */
19178 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19179 /* Look for the optional `::' operator. */
19180 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19181 /* And the optional nested-name-specifier. */
19182 cp_parser_nested_name_specifier_opt (parser
,
19183 /*typename_keyword_p=*/false,
19184 /*check_dependency_p=*/true,
19186 /*is_declaration=*/true);
19187 /* Get the namespace being used. */
19188 namespace_decl
= cp_parser_namespace_name (parser
);
19189 /* And any specified attributes. */
19190 attribs
= cp_parser_attributes_opt (parser
);
19192 /* Update the symbol table. */
19193 if (namespace_bindings_p ())
19194 finish_namespace_using_directive (namespace_decl
, attribs
);
19196 finish_local_using_directive (namespace_decl
, attribs
);
19198 /* Look for the final `;'. */
19199 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19202 /* Parse an asm-definition.
19205 asm ( string-literal ) ;
19210 asm volatile [opt] ( string-literal ) ;
19211 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19212 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19213 : asm-operand-list [opt] ) ;
19214 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19215 : asm-operand-list [opt]
19216 : asm-clobber-list [opt] ) ;
19217 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19218 : asm-clobber-list [opt]
19219 : asm-goto-list ) ; */
19222 cp_parser_asm_definition (cp_parser
* parser
)
19225 tree outputs
= NULL_TREE
;
19226 tree inputs
= NULL_TREE
;
19227 tree clobbers
= NULL_TREE
;
19228 tree labels
= NULL_TREE
;
19230 bool volatile_p
= false;
19231 bool extended_p
= false;
19232 bool invalid_inputs_p
= false;
19233 bool invalid_outputs_p
= false;
19234 bool goto_p
= false;
19235 required_token missing
= RT_NONE
;
19237 /* Look for the `asm' keyword. */
19238 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19240 if (parser
->in_function_body
19241 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19243 error ("%<asm%> in %<constexpr%> function");
19244 cp_function_chain
->invalid_constexpr
= true;
19247 /* See if the next token is `volatile'. */
19248 if (cp_parser_allow_gnu_extensions_p (parser
)
19249 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19251 /* Remember that we saw the `volatile' keyword. */
19253 /* Consume the token. */
19254 cp_lexer_consume_token (parser
->lexer
);
19256 if (cp_parser_allow_gnu_extensions_p (parser
)
19257 && parser
->in_function_body
19258 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19260 /* Remember that we saw the `goto' keyword. */
19262 /* Consume the token. */
19263 cp_lexer_consume_token (parser
->lexer
);
19265 /* Look for the opening `('. */
19266 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19268 /* Look for the string. */
19269 string
= cp_parser_string_literal (parser
, false, false);
19270 if (string
== error_mark_node
)
19272 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19273 /*consume_paren=*/true);
19277 /* If we're allowing GNU extensions, check for the extended assembly
19278 syntax. Unfortunately, the `:' tokens need not be separated by
19279 a space in C, and so, for compatibility, we tolerate that here
19280 too. Doing that means that we have to treat the `::' operator as
19282 if (cp_parser_allow_gnu_extensions_p (parser
)
19283 && parser
->in_function_body
19284 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19285 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19287 bool inputs_p
= false;
19288 bool clobbers_p
= false;
19289 bool labels_p
= false;
19291 /* The extended syntax was used. */
19294 /* Look for outputs. */
19295 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19297 /* Consume the `:'. */
19298 cp_lexer_consume_token (parser
->lexer
);
19299 /* Parse the output-operands. */
19300 if (cp_lexer_next_token_is_not (parser
->lexer
,
19302 && cp_lexer_next_token_is_not (parser
->lexer
,
19304 && cp_lexer_next_token_is_not (parser
->lexer
,
19308 outputs
= cp_parser_asm_operand_list (parser
);
19309 if (outputs
== error_mark_node
)
19310 invalid_outputs_p
= true;
19313 /* If the next token is `::', there are no outputs, and the
19314 next token is the beginning of the inputs. */
19315 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19316 /* The inputs are coming next. */
19319 /* Look for inputs. */
19321 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19323 /* Consume the `:' or `::'. */
19324 cp_lexer_consume_token (parser
->lexer
);
19325 /* Parse the output-operands. */
19326 if (cp_lexer_next_token_is_not (parser
->lexer
,
19328 && cp_lexer_next_token_is_not (parser
->lexer
,
19330 && cp_lexer_next_token_is_not (parser
->lexer
,
19333 inputs
= cp_parser_asm_operand_list (parser
);
19334 if (inputs
== error_mark_node
)
19335 invalid_inputs_p
= true;
19338 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19339 /* The clobbers are coming next. */
19342 /* Look for clobbers. */
19344 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19347 /* Consume the `:' or `::'. */
19348 cp_lexer_consume_token (parser
->lexer
);
19349 /* Parse the clobbers. */
19350 if (cp_lexer_next_token_is_not (parser
->lexer
,
19352 && cp_lexer_next_token_is_not (parser
->lexer
,
19354 clobbers
= cp_parser_asm_clobber_list (parser
);
19357 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19358 /* The labels are coming next. */
19361 /* Look for labels. */
19363 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19366 /* Consume the `:' or `::'. */
19367 cp_lexer_consume_token (parser
->lexer
);
19368 /* Parse the labels. */
19369 labels
= cp_parser_asm_label_list (parser
);
19372 if (goto_p
&& !labels_p
)
19373 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19376 missing
= RT_COLON_SCOPE
;
19378 /* Look for the closing `)'. */
19379 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19380 missing
? missing
: RT_CLOSE_PAREN
))
19381 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19382 /*consume_paren=*/true);
19383 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19385 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19387 /* Create the ASM_EXPR. */
19388 if (parser
->in_function_body
)
19390 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19391 inputs
, clobbers
, labels
);
19392 /* If the extended syntax was not used, mark the ASM_EXPR. */
19395 tree temp
= asm_stmt
;
19396 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19397 temp
= TREE_OPERAND (temp
, 0);
19399 ASM_INPUT_P (temp
) = 1;
19403 symtab
->finalize_toplevel_asm (string
);
19407 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19408 type that comes from the decl-specifier-seq. */
19411 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19413 for (cp_declarator
*d
= declarator
; d
;)
19423 if (TYPE_PTRMEMFUNC_P (type
))
19424 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19425 type
= TREE_TYPE (type
);
19433 /* Declarators [gram.dcl.decl] */
19435 /* Parse an init-declarator.
19438 declarator initializer [opt]
19443 declarator asm-specification [opt] attributes [opt] initializer [opt]
19445 function-definition:
19446 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19448 decl-specifier-seq [opt] declarator function-try-block
19452 function-definition:
19453 __extension__ function-definition
19457 function-definition:
19458 decl-specifier-seq [opt] declarator function-transaction-block
19460 The DECL_SPECIFIERS apply to this declarator. Returns a
19461 representation of the entity declared. If MEMBER_P is TRUE, then
19462 this declarator appears in a class scope. The new DECL created by
19463 this declarator is returned.
19465 The CHECKS are access checks that should be performed once we know
19466 what entity is being declared (and, therefore, what classes have
19469 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19470 for a function-definition here as well. If the declarator is a
19471 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19472 be TRUE upon return. By that point, the function-definition will
19473 have been completely parsed.
19475 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19478 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19479 parsed declaration if it is an uninitialized single declarator not followed
19480 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19481 if present, will not be consumed. If returned, this declarator will be
19482 created with SD_INITIALIZED but will not call cp_finish_decl.
19484 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19485 and there is an initializer, the pointed location_t is set to the
19486 location of the '=' or `(', or '{' in C++11 token introducing the
19490 cp_parser_init_declarator (cp_parser
* parser
,
19491 cp_decl_specifier_seq
*decl_specifiers
,
19492 vec
<deferred_access_check
, va_gc
> *checks
,
19493 bool function_definition_allowed_p
,
19495 int declares_class_or_enum
,
19496 bool* function_definition_p
,
19497 tree
* maybe_range_for_decl
,
19498 location_t
* init_loc
,
19501 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19502 *attributes_start_token
= NULL
;
19503 cp_declarator
*declarator
;
19504 tree prefix_attributes
;
19505 tree attributes
= NULL
;
19506 tree asm_specification
;
19508 tree decl
= NULL_TREE
;
19510 int is_initialized
;
19511 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19512 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19514 enum cpp_ttype initialization_kind
;
19515 bool is_direct_init
= false;
19516 bool is_non_constant_init
;
19517 int ctor_dtor_or_conv_p
;
19518 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19519 tree pushed_scope
= NULL_TREE
;
19520 bool range_for_decl_p
= false;
19521 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19522 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19524 /* Gather the attributes that were provided with the
19525 decl-specifiers. */
19526 prefix_attributes
= decl_specifiers
->attributes
;
19528 /* Assume that this is not the declarator for a function
19530 if (function_definition_p
)
19531 *function_definition_p
= false;
19533 /* Default arguments are only permitted for function parameters. */
19534 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19535 parser
->default_arg_ok_p
= false;
19537 /* Defer access checks while parsing the declarator; we cannot know
19538 what names are accessible until we know what is being
19540 resume_deferring_access_checks ();
19542 token
= cp_lexer_peek_token (parser
->lexer
);
19544 /* Parse the declarator. */
19546 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19547 &ctor_dtor_or_conv_p
,
19548 /*parenthesized_p=*/NULL
,
19549 member_p
, friend_p
);
19550 /* Gather up the deferred checks. */
19551 stop_deferring_access_checks ();
19553 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19555 /* If the DECLARATOR was erroneous, there's no need to go
19557 if (declarator
== cp_error_declarator
)
19558 return error_mark_node
;
19560 /* Check that the number of template-parameter-lists is OK. */
19561 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19563 return error_mark_node
;
19565 if (declares_class_or_enum
& 2)
19566 cp_parser_check_for_definition_in_return_type (declarator
,
19567 decl_specifiers
->type
,
19568 decl_specifiers
->locations
[ds_type_spec
]);
19570 /* Figure out what scope the entity declared by the DECLARATOR is
19571 located in. `grokdeclarator' sometimes changes the scope, so
19572 we compute it now. */
19573 scope
= get_scope_of_declarator (declarator
);
19575 /* Perform any lookups in the declared type which were thought to be
19576 dependent, but are not in the scope of the declarator. */
19577 decl_specifiers
->type
19578 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19580 /* If we're allowing GNU extensions, look for an
19581 asm-specification. */
19582 if (cp_parser_allow_gnu_extensions_p (parser
))
19584 /* Look for an asm-specification. */
19585 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19586 asm_specification
= cp_parser_asm_specification_opt (parser
);
19589 asm_specification
= NULL_TREE
;
19591 /* Look for attributes. */
19592 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19593 attributes
= cp_parser_attributes_opt (parser
);
19595 /* Peek at the next token. */
19596 token
= cp_lexer_peek_token (parser
->lexer
);
19598 bool bogus_implicit_tmpl
= false;
19600 if (function_declarator_p (declarator
))
19602 /* Handle C++17 deduction guides. */
19603 if (!decl_specifiers
->type
19604 && ctor_dtor_or_conv_p
<= 0
19605 && cxx_dialect
>= cxx17
)
19607 cp_declarator
*id
= get_id_declarator (declarator
);
19608 tree name
= id
->u
.id
.unqualified_name
;
19609 parser
->scope
= id
->u
.id
.qualifying_scope
;
19610 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19612 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19613 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19615 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19616 id
->u
.id
.sfk
= sfk_deduction_guide
;
19617 ctor_dtor_or_conv_p
= 1;
19621 /* Check to see if the token indicates the start of a
19622 function-definition. */
19623 if (cp_parser_token_starts_function_definition_p (token
))
19625 if (!function_definition_allowed_p
)
19627 /* If a function-definition should not appear here, issue an
19629 cp_parser_error (parser
,
19630 "a function-definition is not allowed here");
19631 return error_mark_node
;
19634 location_t func_brace_location
19635 = cp_lexer_peek_token (parser
->lexer
)->location
;
19637 /* Neither attributes nor an asm-specification are allowed
19638 on a function-definition. */
19639 if (asm_specification
)
19640 error_at (asm_spec_start_token
->location
,
19641 "an asm-specification is not allowed "
19642 "on a function-definition");
19644 error_at (attributes_start_token
->location
,
19645 "attributes are not allowed "
19646 "on a function-definition");
19647 /* This is a function-definition. */
19648 *function_definition_p
= true;
19650 /* Parse the function definition. */
19652 decl
= cp_parser_save_member_function_body (parser
,
19655 prefix_attributes
);
19658 (cp_parser_function_definition_from_specifiers_and_declarator
19659 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19661 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19663 /* This is where the prologue starts... */
19664 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19665 = func_brace_location
;
19671 else if (parser
->fully_implicit_function_template_p
)
19673 /* A non-template declaration involving a function parameter list
19674 containing an implicit template parameter will be made into a
19675 template. If the resulting declaration is not going to be an
19676 actual function then finish the template scope here to prevent it.
19677 An error message will be issued once we have a decl to talk about.
19679 FIXME probably we should do type deduction rather than create an
19680 implicit template, but the standard currently doesn't allow it. */
19681 bogus_implicit_tmpl
= true;
19682 finish_fully_implicit_template (parser
, NULL_TREE
);
19687 Only in function declarations for constructors, destructors, type
19688 conversions, and deduction guides can the decl-specifier-seq be omitted.
19690 We explicitly postpone this check past the point where we handle
19691 function-definitions because we tolerate function-definitions
19692 that are missing their return types in some modes. */
19693 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19695 cp_parser_error (parser
,
19696 "expected constructor, destructor, or type conversion");
19697 return error_mark_node
;
19700 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19701 if (token
->type
== CPP_EQ
19702 || token
->type
== CPP_OPEN_PAREN
19703 || token
->type
== CPP_OPEN_BRACE
)
19705 is_initialized
= SD_INITIALIZED
;
19706 initialization_kind
= token
->type
;
19707 if (maybe_range_for_decl
)
19708 *maybe_range_for_decl
= error_mark_node
;
19709 tmp_init_loc
= token
->location
;
19710 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19711 *init_loc
= tmp_init_loc
;
19713 if (token
->type
== CPP_EQ
19714 && function_declarator_p (declarator
))
19716 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19717 if (t2
->keyword
== RID_DEFAULT
)
19718 is_initialized
= SD_DEFAULTED
;
19719 else if (t2
->keyword
== RID_DELETE
)
19720 is_initialized
= SD_DELETED
;
19725 /* If the init-declarator isn't initialized and isn't followed by a
19726 `,' or `;', it's not a valid init-declarator. */
19727 if (token
->type
!= CPP_COMMA
19728 && token
->type
!= CPP_SEMICOLON
)
19730 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19731 range_for_decl_p
= true;
19734 if (!maybe_range_for_decl
)
19735 cp_parser_error (parser
, "expected initializer");
19736 return error_mark_node
;
19739 is_initialized
= SD_UNINITIALIZED
;
19740 initialization_kind
= CPP_EOF
;
19743 /* Because start_decl has side-effects, we should only call it if we
19744 know we're going ahead. By this point, we know that we cannot
19745 possibly be looking at any other construct. */
19746 cp_parser_commit_to_tentative_parse (parser
);
19748 /* Enter the newly declared entry in the symbol table. If we're
19749 processing a declaration in a class-specifier, we wait until
19750 after processing the initializer. */
19753 if (parser
->in_unbraced_linkage_specification_p
)
19754 decl_specifiers
->storage_class
= sc_extern
;
19755 decl
= start_decl (declarator
, decl_specifiers
,
19756 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19757 attributes
, prefix_attributes
, &pushed_scope
);
19758 cp_finalize_omp_declare_simd (parser
, decl
);
19759 cp_finalize_oacc_routine (parser
, decl
, false);
19760 /* Adjust location of decl if declarator->id_loc is more appropriate:
19761 set, and decl wasn't merged with another decl, in which case its
19762 location would be different from input_location, and more accurate. */
19764 && declarator
->id_loc
!= UNKNOWN_LOCATION
19765 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19766 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19769 /* Enter the SCOPE. That way unqualified names appearing in the
19770 initializer will be looked up in SCOPE. */
19771 pushed_scope
= push_scope (scope
);
19773 /* Perform deferred access control checks, now that we know in which
19774 SCOPE the declared entity resides. */
19775 if (!member_p
&& decl
)
19777 tree saved_current_function_decl
= NULL_TREE
;
19779 /* If the entity being declared is a function, pretend that we
19780 are in its scope. If it is a `friend', it may have access to
19781 things that would not otherwise be accessible. */
19782 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19784 saved_current_function_decl
= current_function_decl
;
19785 current_function_decl
= decl
;
19788 /* Perform access checks for template parameters. */
19789 cp_parser_perform_template_parameter_access_checks (checks
);
19791 /* Perform the access control checks for the declarator and the
19792 decl-specifiers. */
19793 perform_deferred_access_checks (tf_warning_or_error
);
19795 /* Restore the saved value. */
19796 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19797 current_function_decl
= saved_current_function_decl
;
19800 /* Parse the initializer. */
19801 initializer
= NULL_TREE
;
19802 is_direct_init
= false;
19803 is_non_constant_init
= true;
19804 if (is_initialized
)
19806 if (function_declarator_p (declarator
))
19808 if (initialization_kind
== CPP_EQ
)
19809 initializer
= cp_parser_pure_specifier (parser
);
19812 /* If the declaration was erroneous, we don't really
19813 know what the user intended, so just silently
19814 consume the initializer. */
19815 if (decl
!= error_mark_node
)
19816 error_at (tmp_init_loc
, "initializer provided for function");
19817 cp_parser_skip_to_closing_parenthesis (parser
,
19818 /*recovering=*/true,
19819 /*or_comma=*/false,
19820 /*consume_paren=*/true);
19825 /* We want to record the extra mangling scope for in-class
19826 initializers of class members and initializers of static data
19827 member templates. The former involves deferring
19828 parsing of the initializer until end of class as with default
19829 arguments. So right here we only handle the latter. */
19830 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19831 start_lambda_scope (decl
);
19832 initializer
= cp_parser_initializer (parser
,
19834 &is_non_constant_init
);
19835 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19836 finish_lambda_scope ();
19837 if (initializer
== error_mark_node
)
19838 cp_parser_skip_to_end_of_statement (parser
);
19842 /* The old parser allows attributes to appear after a parenthesized
19843 initializer. Mark Mitchell proposed removing this functionality
19844 on the GCC mailing lists on 2002-08-13. This parser accepts the
19845 attributes -- but ignores them. Made a permerror in GCC 8. */
19846 if (cp_parser_allow_gnu_extensions_p (parser
)
19847 && initialization_kind
== CPP_OPEN_PAREN
19848 && cp_parser_attributes_opt (parser
)
19849 && permerror (input_location
,
19850 "attributes after parenthesized initializer ignored"))
19853 if (flag_permissive
&& !hint
)
19856 inform (input_location
,
19857 "this flexibility is deprecated and will be removed");
19861 /* And now complain about a non-function implicit template. */
19862 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19863 error_at (DECL_SOURCE_LOCATION (decl
),
19864 "non-function %qD declared as implicit template", decl
);
19866 /* For an in-class declaration, use `grokfield' to create the
19872 pop_scope (pushed_scope
);
19873 pushed_scope
= NULL_TREE
;
19875 decl
= grokfield (declarator
, decl_specifiers
,
19876 initializer
, !is_non_constant_init
,
19877 /*asmspec=*/NULL_TREE
,
19878 attr_chainon (attributes
, prefix_attributes
));
19879 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19880 cp_parser_save_default_args (parser
, decl
);
19881 cp_finalize_omp_declare_simd (parser
, decl
);
19882 cp_finalize_oacc_routine (parser
, decl
, false);
19885 /* Finish processing the declaration. But, skip member
19887 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19889 cp_finish_decl (decl
,
19890 initializer
, !is_non_constant_init
,
19892 /* If the initializer is in parentheses, then this is
19893 a direct-initialization, which means that an
19894 `explicit' constructor is OK. Otherwise, an
19895 `explicit' constructor cannot be used. */
19896 ((is_direct_init
|| !is_initialized
)
19897 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19899 else if ((cxx_dialect
!= cxx98
) && friend_p
19900 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19901 /* Core issue #226 (C++0x only): A default template-argument
19902 shall not be specified in a friend class template
19904 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19905 /*is_partial=*/false, /*is_friend_decl=*/1);
19907 if (!friend_p
&& pushed_scope
)
19908 pop_scope (pushed_scope
);
19910 if (function_declarator_p (declarator
)
19911 && parser
->fully_implicit_function_template_p
)
19914 decl
= finish_fully_implicit_template (parser
, decl
);
19916 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19919 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19920 && type_uses_auto (decl_specifiers
->type
))
19921 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19926 /* Parse a declarator.
19930 ptr-operator declarator
19932 abstract-declarator:
19933 ptr-operator abstract-declarator [opt]
19934 direct-abstract-declarator
19939 attributes [opt] direct-declarator
19940 attributes [opt] ptr-operator declarator
19942 abstract-declarator:
19943 attributes [opt] ptr-operator abstract-declarator [opt]
19944 attributes [opt] direct-abstract-declarator
19946 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19947 detect constructors, destructors, deduction guides, or conversion operators.
19948 It is set to -1 if the declarator is a name, and +1 if it is a
19949 function. Otherwise it is set to zero. Usually you just want to
19950 test for >0, but internally the negative value is used.
19952 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19953 a decl-specifier-seq unless it declares a constructor, destructor,
19954 or conversion. It might seem that we could check this condition in
19955 semantic analysis, rather than parsing, but that makes it difficult
19956 to handle something like `f()'. We want to notice that there are
19957 no decl-specifiers, and therefore realize that this is an
19958 expression, not a declaration.)
19960 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19961 the declarator is a direct-declarator of the form "(...)".
19963 MEMBER_P is true iff this declarator is a member-declarator.
19965 FRIEND_P is true iff this declarator is a friend. */
19967 static cp_declarator
*
19968 cp_parser_declarator (cp_parser
* parser
,
19969 cp_parser_declarator_kind dcl_kind
,
19970 int* ctor_dtor_or_conv_p
,
19971 bool* parenthesized_p
,
19972 bool member_p
, bool friend_p
)
19974 cp_declarator
*declarator
;
19975 enum tree_code code
;
19976 cp_cv_quals cv_quals
;
19978 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19980 /* Assume this is not a constructor, destructor, or type-conversion
19982 if (ctor_dtor_or_conv_p
)
19983 *ctor_dtor_or_conv_p
= 0;
19985 if (cp_parser_allow_gnu_extensions_p (parser
))
19986 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19988 /* Check for the ptr-operator production. */
19989 cp_parser_parse_tentatively (parser
);
19990 /* Parse the ptr-operator. */
19991 code
= cp_parser_ptr_operator (parser
,
19996 /* If that worked, then we have a ptr-operator. */
19997 if (cp_parser_parse_definitely (parser
))
19999 /* If a ptr-operator was found, then this declarator was not
20001 if (parenthesized_p
)
20002 *parenthesized_p
= true;
20003 /* The dependent declarator is optional if we are parsing an
20004 abstract-declarator. */
20005 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20006 cp_parser_parse_tentatively (parser
);
20008 /* Parse the dependent declarator. */
20009 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20010 /*ctor_dtor_or_conv_p=*/NULL
,
20011 /*parenthesized_p=*/NULL
,
20012 /*member_p=*/false,
20015 /* If we are parsing an abstract-declarator, we must handle the
20016 case where the dependent declarator is absent. */
20017 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20018 && !cp_parser_parse_definitely (parser
))
20021 declarator
= cp_parser_make_indirect_declarator
20022 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20024 /* Everything else is a direct-declarator. */
20027 if (parenthesized_p
)
20028 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20030 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20031 ctor_dtor_or_conv_p
,
20032 member_p
, friend_p
);
20035 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20036 declarator
->attributes
= gnu_attributes
;
20040 /* Parse a direct-declarator or direct-abstract-declarator.
20044 direct-declarator ( parameter-declaration-clause )
20045 cv-qualifier-seq [opt]
20046 ref-qualifier [opt]
20047 exception-specification [opt]
20048 direct-declarator [ constant-expression [opt] ]
20051 direct-abstract-declarator:
20052 direct-abstract-declarator [opt]
20053 ( parameter-declaration-clause )
20054 cv-qualifier-seq [opt]
20055 ref-qualifier [opt]
20056 exception-specification [opt]
20057 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20058 ( abstract-declarator )
20060 Returns a representation of the declarator. DCL_KIND is
20061 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20062 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20063 we are parsing a direct-declarator. It is
20064 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20065 of ambiguity we prefer an abstract declarator, as per
20066 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20067 as for cp_parser_declarator. */
20069 static cp_declarator
*
20070 cp_parser_direct_declarator (cp_parser
* parser
,
20071 cp_parser_declarator_kind dcl_kind
,
20072 int* ctor_dtor_or_conv_p
,
20073 bool member_p
, bool friend_p
)
20076 cp_declarator
*declarator
= NULL
;
20077 tree scope
= NULL_TREE
;
20078 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20079 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20081 tree pushed_scope
= NULL_TREE
;
20082 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20086 /* Peek at the next token. */
20087 token
= cp_lexer_peek_token (parser
->lexer
);
20088 if (token
->type
== CPP_OPEN_PAREN
)
20090 /* This is either a parameter-declaration-clause, or a
20091 parenthesized declarator. When we know we are parsing a
20092 named declarator, it must be a parenthesized declarator
20093 if FIRST is true. For instance, `(int)' is a
20094 parameter-declaration-clause, with an omitted
20095 direct-abstract-declarator. But `((*))', is a
20096 parenthesized abstract declarator. Finally, when T is a
20097 template parameter `(T)' is a
20098 parameter-declaration-clause, and not a parenthesized
20101 We first try and parse a parameter-declaration-clause,
20102 and then try a nested declarator (if FIRST is true).
20104 It is not an error for it not to be a
20105 parameter-declaration-clause, even when FIRST is
20111 The first is the declaration of a function while the
20112 second is the definition of a variable, including its
20115 Having seen only the parenthesis, we cannot know which of
20116 these two alternatives should be selected. Even more
20117 complex are examples like:
20122 The former is a function-declaration; the latter is a
20123 variable initialization.
20125 Thus again, we try a parameter-declaration-clause, and if
20126 that fails, we back out and return. */
20128 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20131 bool is_declarator
= false;
20135 /* In a member-declarator, the only valid interpretation
20136 of a parenthesis is the start of a
20137 parameter-declaration-clause. (It is invalid to
20138 initialize a static data member with a parenthesized
20139 initializer; only the "=" form of initialization is
20142 cp_parser_parse_tentatively (parser
);
20144 /* Consume the `('. */
20145 matching_parens parens
;
20146 parens
.consume_open (parser
);
20149 /* If this is going to be an abstract declarator, we're
20150 in a declarator and we can't have default args. */
20151 parser
->default_arg_ok_p
= false;
20152 parser
->in_declarator_p
= true;
20155 begin_scope (sk_function_parms
, NULL_TREE
);
20157 /* Parse the parameter-declaration-clause. */
20158 params
= cp_parser_parameter_declaration_clause (parser
);
20160 /* Consume the `)'. */
20161 parens
.require_close (parser
);
20163 /* If all went well, parse the cv-qualifier-seq,
20164 ref-qualifier and the exception-specification. */
20165 if (member_p
|| cp_parser_parse_definitely (parser
))
20167 cp_cv_quals cv_quals
;
20168 cp_virt_specifiers virt_specifiers
;
20169 cp_ref_qualifier ref_qual
;
20170 tree exception_specification
;
20173 bool memfn
= (member_p
|| (pushed_scope
20174 && CLASS_TYPE_P (pushed_scope
)));
20176 is_declarator
= true;
20178 if (ctor_dtor_or_conv_p
)
20179 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20182 /* Parse the cv-qualifier-seq. */
20183 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20184 /* Parse the ref-qualifier. */
20185 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20186 /* Parse the tx-qualifier. */
20187 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20188 /* And the exception-specification. */
20189 exception_specification
20190 = cp_parser_exception_specification_opt (parser
);
20192 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20194 /* In here, we handle cases where attribute is used after
20195 the function declaration. For example:
20196 void func (int x) __attribute__((vector(..))); */
20197 tree gnu_attrs
= NULL_TREE
;
20198 tree requires_clause
= NULL_TREE
;
20199 late_return
= (cp_parser_late_return_type_opt
20200 (parser
, declarator
, requires_clause
,
20201 memfn
? cv_quals
: -1));
20203 /* Parse the virt-specifier-seq. */
20204 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20206 /* Create the function-declarator. */
20207 declarator
= make_call_declarator (declarator
,
20213 exception_specification
,
20216 declarator
->std_attributes
= attrs
;
20217 declarator
->attributes
= gnu_attrs
;
20218 /* Any subsequent parameter lists are to do with
20219 return type, so are not those of the declared
20221 parser
->default_arg_ok_p
= false;
20224 /* Remove the function parms from scope. */
20225 pop_bindings_and_leave_scope ();
20228 /* Repeat the main loop. */
20232 /* If this is the first, we can try a parenthesized
20236 bool saved_in_type_id_in_expr_p
;
20238 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20239 parser
->in_declarator_p
= saved_in_declarator_p
;
20241 open_paren
= token
;
20242 /* Consume the `('. */
20243 matching_parens parens
;
20244 parens
.consume_open (parser
);
20245 /* Parse the nested declarator. */
20246 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20247 parser
->in_type_id_in_expr_p
= true;
20249 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20250 /*parenthesized_p=*/NULL
,
20251 member_p
, friend_p
);
20252 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20254 /* Expect a `)'. */
20255 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20256 if (!parens
.require_close (parser
))
20257 declarator
= cp_error_declarator
;
20258 if (declarator
== cp_error_declarator
)
20261 goto handle_declarator
;
20263 /* Otherwise, we must be done. */
20267 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20268 && token
->type
== CPP_OPEN_SQUARE
20269 && !cp_next_tokens_can_be_attribute_p (parser
))
20271 /* Parse an array-declarator. */
20272 tree bounds
, attrs
;
20274 if (ctor_dtor_or_conv_p
)
20275 *ctor_dtor_or_conv_p
= 0;
20279 parser
->default_arg_ok_p
= false;
20280 parser
->in_declarator_p
= true;
20281 /* Consume the `['. */
20282 cp_lexer_consume_token (parser
->lexer
);
20283 /* Peek at the next token. */
20284 token
= cp_lexer_peek_token (parser
->lexer
);
20285 /* If the next token is `]', then there is no
20286 constant-expression. */
20287 if (token
->type
!= CPP_CLOSE_SQUARE
)
20289 bool non_constant_p
;
20291 = cp_parser_constant_expression (parser
,
20292 /*allow_non_constant=*/true,
20294 if (!non_constant_p
)
20296 else if (error_operand_p (bounds
))
20297 /* Already gave an error. */;
20298 else if (!parser
->in_function_body
20299 || current_binding_level
->kind
== sk_function_parms
)
20301 /* Normally, the array bound must be an integral constant
20302 expression. However, as an extension, we allow VLAs
20303 in function scopes as long as they aren't part of a
20304 parameter declaration. */
20305 cp_parser_error (parser
,
20306 "array bound is not an integer constant");
20307 bounds
= error_mark_node
;
20309 else if (processing_template_decl
20310 && !type_dependent_expression_p (bounds
))
20312 /* Remember this wasn't a constant-expression. */
20313 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20314 TREE_SIDE_EFFECTS (bounds
) = 1;
20318 bounds
= NULL_TREE
;
20319 /* Look for the closing `]'. */
20320 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20322 declarator
= cp_error_declarator
;
20326 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20327 declarator
= make_array_declarator (declarator
, bounds
);
20328 declarator
->std_attributes
= attrs
;
20330 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20333 tree qualifying_scope
;
20334 tree unqualified_name
;
20336 special_function_kind sfk
;
20338 bool pack_expansion_p
= false;
20339 cp_token
*declarator_id_start_token
;
20341 /* Parse a declarator-id */
20342 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20345 cp_parser_parse_tentatively (parser
);
20347 /* If we see an ellipsis, we should be looking at a
20349 if (token
->type
== CPP_ELLIPSIS
)
20351 /* Consume the `...' */
20352 cp_lexer_consume_token (parser
->lexer
);
20354 pack_expansion_p
= true;
20358 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20360 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20361 qualifying_scope
= parser
->scope
;
20366 if (!unqualified_name
&& pack_expansion_p
)
20368 /* Check whether an error occurred. */
20369 okay
= !cp_parser_error_occurred (parser
);
20371 /* We already consumed the ellipsis to mark a
20372 parameter pack, but we have no way to report it,
20373 so abort the tentative parse. We will be exiting
20374 immediately anyway. */
20375 cp_parser_abort_tentative_parse (parser
);
20378 okay
= cp_parser_parse_definitely (parser
);
20381 unqualified_name
= error_mark_node
;
20382 else if (unqualified_name
20383 && (qualifying_scope
20384 || (!identifier_p (unqualified_name
))))
20386 cp_parser_error (parser
, "expected unqualified-id");
20387 unqualified_name
= error_mark_node
;
20391 if (!unqualified_name
)
20393 if (unqualified_name
== error_mark_node
)
20395 declarator
= cp_error_declarator
;
20396 pack_expansion_p
= false;
20397 declarator
->parameter_pack_p
= false;
20401 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20403 if (qualifying_scope
&& at_namespace_scope_p ()
20404 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20406 /* In the declaration of a member of a template class
20407 outside of the class itself, the SCOPE will sometimes
20408 be a TYPENAME_TYPE. For example, given:
20410 template <typename T>
20411 int S<T>::R::i = 3;
20413 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20414 this context, we must resolve S<T>::R to an ordinary
20415 type, rather than a typename type.
20417 The reason we normally avoid resolving TYPENAME_TYPEs
20418 is that a specialization of `S' might render
20419 `S<T>::R' not a type. However, if `S' is
20420 specialized, then this `i' will not be used, so there
20421 is no harm in resolving the types here. */
20424 /* Resolve the TYPENAME_TYPE. */
20425 type
= resolve_typename_type (qualifying_scope
,
20426 /*only_current_p=*/false);
20427 /* If that failed, the declarator is invalid. */
20428 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20430 if (typedef_variant_p (type
))
20431 error_at (declarator_id_start_token
->location
,
20432 "cannot define member of dependent typedef "
20435 error_at (declarator_id_start_token
->location
,
20436 "%<%T::%E%> is not a type",
20437 TYPE_CONTEXT (qualifying_scope
),
20438 TYPE_IDENTIFIER (qualifying_scope
));
20440 qualifying_scope
= type
;
20445 if (unqualified_name
)
20449 if (qualifying_scope
20450 && CLASS_TYPE_P (qualifying_scope
))
20451 class_type
= qualifying_scope
;
20453 class_type
= current_class_type
;
20455 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20457 tree name_type
= TREE_TYPE (unqualified_name
);
20459 if (!class_type
|| !same_type_p (name_type
, class_type
))
20461 /* We do not attempt to print the declarator
20462 here because we do not have enough
20463 information about its original syntactic
20465 cp_parser_error (parser
, "invalid declarator");
20466 declarator
= cp_error_declarator
;
20469 else if (qualifying_scope
20470 && CLASSTYPE_USE_TEMPLATE (name_type
))
20472 error_at (declarator_id_start_token
->location
,
20473 "invalid use of constructor as a template");
20474 inform (declarator_id_start_token
->location
,
20475 "use %<%T::%D%> instead of %<%T::%D%> to "
20476 "name the constructor in a qualified name",
20478 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20479 class_type
, name_type
);
20480 declarator
= cp_error_declarator
;
20483 unqualified_name
= constructor_name (class_type
);
20488 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20489 sfk
= sfk_destructor
;
20490 else if (identifier_p (unqualified_name
)
20491 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20492 sfk
= sfk_conversion
;
20493 else if (/* There's no way to declare a constructor
20494 for an unnamed type, even if the type
20495 got a name for linkage purposes. */
20496 !TYPE_WAS_UNNAMED (class_type
)
20497 /* Handle correctly (c++/19200):
20511 friend void N::S();
20513 && (!friend_p
|| class_type
== qualifying_scope
)
20514 && constructor_name_p (unqualified_name
,
20516 sfk
= sfk_constructor
;
20517 else if (is_overloaded_fn (unqualified_name
)
20518 && DECL_CONSTRUCTOR_P (get_first_fn
20519 (unqualified_name
)))
20520 sfk
= sfk_constructor
;
20522 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20523 *ctor_dtor_or_conv_p
= -1;
20526 declarator
= make_id_declarator (qualifying_scope
,
20529 declarator
->std_attributes
= attrs
;
20530 declarator
->id_loc
= token
->location
;
20531 declarator
->parameter_pack_p
= pack_expansion_p
;
20533 if (pack_expansion_p
)
20534 maybe_warn_variadic_templates ();
20537 handle_declarator
:;
20538 scope
= get_scope_of_declarator (declarator
);
20541 /* Any names that appear after the declarator-id for a
20542 member are looked up in the containing scope. */
20543 if (at_function_scope_p ())
20545 /* But declarations with qualified-ids can't appear in a
20547 cp_parser_error (parser
, "qualified-id in declaration");
20548 declarator
= cp_error_declarator
;
20551 pushed_scope
= push_scope (scope
);
20553 parser
->in_declarator_p
= true;
20554 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20555 || (declarator
&& declarator
->kind
== cdk_id
))
20556 /* Default args are only allowed on function
20558 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20560 parser
->default_arg_ok_p
= false;
20569 /* For an abstract declarator, we might wind up with nothing at this
20570 point. That's an error; the declarator is not optional. */
20572 cp_parser_error (parser
, "expected declarator");
20573 else if (open_paren
)
20575 /* Record overly parenthesized declarator so we can give a
20576 diagnostic about confusing decl/expr disambiguation. */
20577 if (declarator
->kind
== cdk_array
)
20579 /* If the open and close parens are on different lines, this
20580 is probably a formatting thing, so ignore. */
20581 expanded_location open
= expand_location (open_paren
->location
);
20582 expanded_location close
= expand_location (close_paren
->location
);
20583 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20587 declarator
->parenthesized
= open_paren
->location
;
20590 /* If we entered a scope, we must exit it now. */
20592 pop_scope (pushed_scope
);
20594 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20595 parser
->in_declarator_p
= saved_in_declarator_p
;
20600 /* Parse a ptr-operator.
20603 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20604 * cv-qualifier-seq [opt]
20606 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20607 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20612 & cv-qualifier-seq [opt]
20614 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20615 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20616 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20617 filled in with the TYPE containing the member. *CV_QUALS is
20618 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20619 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20620 Note that the tree codes returned by this function have nothing
20621 to do with the types of trees that will be eventually be created
20622 to represent the pointer or reference type being parsed. They are
20623 just constants with suggestive names. */
20624 static enum tree_code
20625 cp_parser_ptr_operator (cp_parser
* parser
,
20627 cp_cv_quals
*cv_quals
,
20630 enum tree_code code
= ERROR_MARK
;
20632 tree attrs
= NULL_TREE
;
20634 /* Assume that it's not a pointer-to-member. */
20636 /* And that there are no cv-qualifiers. */
20637 *cv_quals
= TYPE_UNQUALIFIED
;
20639 /* Peek at the next token. */
20640 token
= cp_lexer_peek_token (parser
->lexer
);
20642 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20643 if (token
->type
== CPP_MULT
)
20644 code
= INDIRECT_REF
;
20645 else if (token
->type
== CPP_AND
)
20647 else if ((cxx_dialect
!= cxx98
) &&
20648 token
->type
== CPP_AND_AND
) /* C++0x only */
20649 code
= NON_LVALUE_EXPR
;
20651 if (code
!= ERROR_MARK
)
20653 /* Consume the `*', `&' or `&&'. */
20654 cp_lexer_consume_token (parser
->lexer
);
20656 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20657 `&', if we are allowing GNU extensions. (The only qualifier
20658 that can legally appear after `&' is `restrict', but that is
20659 enforced during semantic analysis. */
20660 if (code
== INDIRECT_REF
20661 || cp_parser_allow_gnu_extensions_p (parser
))
20662 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20664 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20665 if (attributes
!= NULL
)
20666 *attributes
= attrs
;
20670 /* Try the pointer-to-member case. */
20671 cp_parser_parse_tentatively (parser
);
20672 /* Look for the optional `::' operator. */
20673 cp_parser_global_scope_opt (parser
,
20674 /*current_scope_valid_p=*/false);
20675 /* Look for the nested-name specifier. */
20676 token
= cp_lexer_peek_token (parser
->lexer
);
20677 cp_parser_nested_name_specifier (parser
,
20678 /*typename_keyword_p=*/false,
20679 /*check_dependency_p=*/true,
20681 /*is_declaration=*/false);
20682 /* If we found it, and the next token is a `*', then we are
20683 indeed looking at a pointer-to-member operator. */
20684 if (!cp_parser_error_occurred (parser
)
20685 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20687 /* Indicate that the `*' operator was used. */
20688 code
= INDIRECT_REF
;
20690 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20691 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20692 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20693 error_at (token
->location
, "cannot form pointer to member of "
20694 "non-class %q#T", parser
->scope
);
20697 /* The type of which the member is a member is given by the
20699 *type
= parser
->scope
;
20700 /* The next name will not be qualified. */
20701 parser
->scope
= NULL_TREE
;
20702 parser
->qualifying_scope
= NULL_TREE
;
20703 parser
->object_scope
= NULL_TREE
;
20704 /* Look for optional c++11 attributes. */
20705 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20706 if (attributes
!= NULL
)
20707 *attributes
= attrs
;
20708 /* Look for the optional cv-qualifier-seq. */
20709 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20712 /* If that didn't work we don't have a ptr-operator. */
20713 if (!cp_parser_parse_definitely (parser
))
20714 cp_parser_error (parser
, "expected ptr-operator");
20720 /* Parse an (optional) cv-qualifier-seq.
20723 cv-qualifier cv-qualifier-seq [opt]
20734 Returns a bitmask representing the cv-qualifiers. */
20737 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20739 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20744 cp_cv_quals cv_qualifier
;
20746 /* Peek at the next token. */
20747 token
= cp_lexer_peek_token (parser
->lexer
);
20748 /* See if it's a cv-qualifier. */
20749 switch (token
->keyword
)
20752 cv_qualifier
= TYPE_QUAL_CONST
;
20756 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20760 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20764 cv_qualifier
= TYPE_UNQUALIFIED
;
20771 if (cv_quals
& cv_qualifier
)
20773 gcc_rich_location
richloc (token
->location
);
20774 richloc
.add_fixit_remove ();
20775 error_at (&richloc
, "duplicate cv-qualifier");
20776 cp_lexer_purge_token (parser
->lexer
);
20780 cp_lexer_consume_token (parser
->lexer
);
20781 cv_quals
|= cv_qualifier
;
20788 /* Parse an (optional) ref-qualifier
20794 Returns cp_ref_qualifier representing ref-qualifier. */
20796 static cp_ref_qualifier
20797 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20799 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20801 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20802 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20807 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20808 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20810 switch (token
->type
)
20813 curr_ref_qual
= REF_QUAL_LVALUE
;
20817 curr_ref_qual
= REF_QUAL_RVALUE
;
20821 curr_ref_qual
= REF_QUAL_NONE
;
20825 if (!curr_ref_qual
)
20829 error_at (token
->location
, "multiple ref-qualifiers");
20830 cp_lexer_purge_token (parser
->lexer
);
20834 ref_qual
= curr_ref_qual
;
20835 cp_lexer_consume_token (parser
->lexer
);
20842 /* Parse an optional tx-qualifier.
20846 transaction_safe_dynamic */
20849 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20851 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20852 if (token
->type
== CPP_NAME
)
20854 tree name
= token
->u
.value
;
20855 const char *p
= IDENTIFIER_POINTER (name
);
20856 const int len
= strlen ("transaction_safe");
20857 if (!strncmp (p
, "transaction_safe", len
))
20861 || !strcmp (p
, "_dynamic"))
20863 cp_lexer_consume_token (parser
->lexer
);
20866 error ("%qE requires %<-fgnu-tm%>", name
);
20877 /* Parse an (optional) virt-specifier-seq.
20879 virt-specifier-seq:
20880 virt-specifier virt-specifier-seq [opt]
20886 Returns a bitmask representing the virt-specifiers. */
20888 static cp_virt_specifiers
20889 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20891 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20896 cp_virt_specifiers virt_specifier
;
20898 /* Peek at the next token. */
20899 token
= cp_lexer_peek_token (parser
->lexer
);
20900 /* See if it's a virt-specifier-qualifier. */
20901 if (token
->type
!= CPP_NAME
)
20903 if (id_equal (token
->u
.value
, "override"))
20905 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20906 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20908 else if (id_equal (token
->u
.value
, "final"))
20910 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20911 virt_specifier
= VIRT_SPEC_FINAL
;
20913 else if (id_equal (token
->u
.value
, "__final"))
20915 virt_specifier
= VIRT_SPEC_FINAL
;
20920 if (virt_specifiers
& virt_specifier
)
20922 gcc_rich_location
richloc (token
->location
);
20923 richloc
.add_fixit_remove ();
20924 error_at (&richloc
, "duplicate virt-specifier");
20925 cp_lexer_purge_token (parser
->lexer
);
20929 cp_lexer_consume_token (parser
->lexer
);
20930 virt_specifiers
|= virt_specifier
;
20933 return virt_specifiers
;
20936 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20937 is in scope even though it isn't real. */
20940 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20944 if (current_class_ptr
)
20946 /* We don't clear this between NSDMIs. Is it already what we want? */
20947 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20948 if (DECL_P (current_class_ptr
)
20949 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20950 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20951 && cp_type_quals (type
) == quals
)
20955 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20956 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20957 current_class_ptr
= NULL_TREE
;
20959 = cp_build_fold_indirect_ref (this_parm
);
20960 current_class_ptr
= this_parm
;
20963 /* Return true iff our current scope is a non-static data member
20967 parsing_nsdmi (void)
20969 /* We recognize NSDMI context by the context-less 'this' pointer set up
20970 by the function above. */
20971 if (current_class_ptr
20972 && TREE_CODE (current_class_ptr
) == PARM_DECL
20973 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20978 /* Parse a late-specified return type, if any. This is not a separate
20979 non-terminal, but part of a function declarator, which looks like
20981 -> trailing-type-specifier-seq abstract-declarator(opt)
20983 Returns the type indicated by the type-id.
20985 In addition to this, parse any queued up #pragma omp declare simd
20986 clauses, and #pragma acc routine clauses.
20988 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20992 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20993 tree
& requires_clause
, cp_cv_quals quals
)
20996 tree type
= NULL_TREE
;
20997 bool declare_simd_p
= (parser
->omp_declare_simd
20999 && declarator
->kind
== cdk_id
);
21001 bool oacc_routine_p
= (parser
->oacc_routine
21003 && declarator
->kind
== cdk_id
);
21005 /* Peek at the next token. */
21006 token
= cp_lexer_peek_token (parser
->lexer
);
21007 /* A late-specified return type is indicated by an initial '->'. */
21008 if (token
->type
!= CPP_DEREF
21009 && token
->keyword
!= RID_REQUIRES
21010 && !(token
->type
== CPP_NAME
21011 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21012 && !(declare_simd_p
|| oacc_routine_p
))
21015 tree save_ccp
= current_class_ptr
;
21016 tree save_ccr
= current_class_ref
;
21019 /* DR 1207: 'this' is in scope in the trailing return type. */
21020 inject_this_parameter (current_class_type
, quals
);
21023 if (token
->type
== CPP_DEREF
)
21025 /* Consume the ->. */
21026 cp_lexer_consume_token (parser
->lexer
);
21028 type
= cp_parser_trailing_type_id (parser
);
21031 /* Function declarations may be followed by a trailing
21032 requires-clause. */
21033 requires_clause
= cp_parser_requires_clause_opt (parser
);
21035 if (declare_simd_p
)
21036 declarator
->attributes
21037 = cp_parser_late_parsing_omp_declare_simd (parser
,
21038 declarator
->attributes
);
21039 if (oacc_routine_p
)
21040 declarator
->attributes
21041 = cp_parser_late_parsing_oacc_routine (parser
,
21042 declarator
->attributes
);
21046 current_class_ptr
= save_ccp
;
21047 current_class_ref
= save_ccr
;
21053 /* Parse a declarator-id.
21057 :: [opt] nested-name-specifier [opt] type-name
21059 In the `id-expression' case, the value returned is as for
21060 cp_parser_id_expression if the id-expression was an unqualified-id.
21061 If the id-expression was a qualified-id, then a SCOPE_REF is
21062 returned. The first operand is the scope (either a NAMESPACE_DECL
21063 or TREE_TYPE), but the second is still just a representation of an
21067 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21070 /* The expression must be an id-expression. Assume that qualified
21071 names are the names of types so that:
21074 int S<T>::R::i = 3;
21076 will work; we must treat `S<T>::R' as the name of a type.
21077 Similarly, assume that qualified names are templates, where
21081 int S<T>::R<T>::i = 3;
21084 id
= cp_parser_id_expression (parser
,
21085 /*template_keyword_p=*/false,
21086 /*check_dependency_p=*/false,
21087 /*template_p=*/NULL
,
21088 /*declarator_p=*/true,
21090 if (id
&& BASELINK_P (id
))
21091 id
= BASELINK_FUNCTIONS (id
);
21095 /* Parse a type-id.
21098 type-specifier-seq abstract-declarator [opt]
21100 Returns the TYPE specified. */
21103 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
21104 bool is_trailing_return
)
21106 cp_decl_specifier_seq type_specifier_seq
;
21107 cp_declarator
*abstract_declarator
;
21109 /* Parse the type-specifier-seq. */
21110 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
21111 is_trailing_return
,
21112 &type_specifier_seq
);
21113 if (is_template_arg
&& type_specifier_seq
.type
21114 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21115 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21116 /* A bare template name as a template argument is a template template
21117 argument, not a placeholder, so fail parsing it as a type argument. */
21119 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21120 cp_parser_simulate_error (parser
);
21121 return error_mark_node
;
21123 if (type_specifier_seq
.type
== error_mark_node
)
21124 return error_mark_node
;
21126 /* There might or might not be an abstract declarator. */
21127 cp_parser_parse_tentatively (parser
);
21128 /* Look for the declarator. */
21129 abstract_declarator
21130 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
21131 /*parenthesized_p=*/NULL
,
21132 /*member_p=*/false,
21133 /*friend_p=*/false);
21134 /* Check to see if there really was a declarator. */
21135 if (!cp_parser_parse_definitely (parser
))
21136 abstract_declarator
= NULL
;
21138 if (type_specifier_seq
.type
21139 /* The concepts TS allows 'auto' as a type-id. */
21140 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21141 /* None of the valid uses of 'auto' in C++14 involve the type-id
21142 nonterminal, but it is valid in a trailing-return-type. */
21143 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21144 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21146 /* A type-id with type 'auto' is only ok if the abstract declarator
21147 is a function declarator with a late-specified return type.
21149 A type-id with 'auto' is also valid in a trailing-return-type
21150 in a compound-requirement. */
21151 if (abstract_declarator
21152 && abstract_declarator
->kind
== cdk_function
21153 && abstract_declarator
->u
.function
.late_return_type
)
21155 else if (parser
->in_result_type_constraint_p
)
21159 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21160 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21162 error_at (loc
, "missing template arguments after %qT",
21164 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21168 error_at (loc
, "invalid use of %qT", auto_node
);
21169 return error_mark_node
;
21173 return groktypename (&type_specifier_seq
, abstract_declarator
,
21178 cp_parser_type_id (cp_parser
*parser
)
21180 return cp_parser_type_id_1 (parser
, false, false);
21184 cp_parser_template_type_arg (cp_parser
*parser
)
21187 const char *saved_message
= parser
->type_definition_forbidden_message
;
21188 parser
->type_definition_forbidden_message
21189 = G_("types may not be defined in template arguments");
21190 r
= cp_parser_type_id_1 (parser
, true, false);
21191 parser
->type_definition_forbidden_message
= saved_message
;
21192 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21194 error ("invalid use of %<auto%> in template argument");
21195 r
= error_mark_node
;
21201 cp_parser_trailing_type_id (cp_parser
*parser
)
21203 return cp_parser_type_id_1 (parser
, false, true);
21206 /* Parse a type-specifier-seq.
21208 type-specifier-seq:
21209 type-specifier type-specifier-seq [opt]
21213 type-specifier-seq:
21214 attributes type-specifier-seq [opt]
21216 If IS_DECLARATION is true, we are at the start of a "condition" or
21217 exception-declaration, so we might be followed by a declarator-id.
21219 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21220 i.e. we've just seen "->".
21222 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21225 cp_parser_type_specifier_seq (cp_parser
* parser
,
21226 bool is_declaration
,
21227 bool is_trailing_return
,
21228 cp_decl_specifier_seq
*type_specifier_seq
)
21230 bool seen_type_specifier
= false;
21231 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21232 cp_token
*start_token
= NULL
;
21234 /* Clear the TYPE_SPECIFIER_SEQ. */
21235 clear_decl_specs (type_specifier_seq
);
21237 /* In the context of a trailing return type, enum E { } is an
21238 elaborated-type-specifier followed by a function-body, not an
21240 if (is_trailing_return
)
21241 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21243 /* Parse the type-specifiers and attributes. */
21246 tree type_specifier
;
21247 bool is_cv_qualifier
;
21249 /* Check for attributes first. */
21250 if (cp_next_tokens_can_be_attribute_p (parser
))
21252 type_specifier_seq
->attributes
21253 = attr_chainon (type_specifier_seq
->attributes
,
21254 cp_parser_attributes_opt (parser
));
21258 /* record the token of the beginning of the type specifier seq,
21259 for error reporting purposes*/
21261 start_token
= cp_lexer_peek_token (parser
->lexer
);
21263 /* Look for the type-specifier. */
21264 type_specifier
= cp_parser_type_specifier (parser
,
21266 type_specifier_seq
,
21267 /*is_declaration=*/false,
21270 if (!type_specifier
)
21272 /* If the first type-specifier could not be found, this is not a
21273 type-specifier-seq at all. */
21274 if (!seen_type_specifier
)
21276 /* Set in_declarator_p to avoid skipping to the semicolon. */
21277 int in_decl
= parser
->in_declarator_p
;
21278 parser
->in_declarator_p
= true;
21280 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21281 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21282 cp_parser_error (parser
, "expected type-specifier");
21284 parser
->in_declarator_p
= in_decl
;
21286 type_specifier_seq
->type
= error_mark_node
;
21289 /* If subsequent type-specifiers could not be found, the
21290 type-specifier-seq is complete. */
21294 seen_type_specifier
= true;
21295 /* The standard says that a condition can be:
21297 type-specifier-seq declarator = assignment-expression
21304 we should treat the "S" as a declarator, not as a
21305 type-specifier. The standard doesn't say that explicitly for
21306 type-specifier-seq, but it does say that for
21307 decl-specifier-seq in an ordinary declaration. Perhaps it
21308 would be clearer just to allow a decl-specifier-seq here, and
21309 then add a semantic restriction that if any decl-specifiers
21310 that are not type-specifiers appear, the program is invalid. */
21311 if (is_declaration
&& !is_cv_qualifier
)
21312 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21316 /* Return whether the function currently being declared has an associated
21317 template parameter list. */
21320 function_being_declared_is_template_p (cp_parser
* parser
)
21322 if (!current_template_parms
|| processing_template_parmlist
)
21325 if (parser
->implicit_template_scope
)
21328 if (at_class_scope_p ()
21329 && TYPE_BEING_DEFINED (current_class_type
))
21330 return parser
->num_template_parameter_lists
!= 0;
21332 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21333 (current_class_type
));
21336 /* Parse a parameter-declaration-clause.
21338 parameter-declaration-clause:
21339 parameter-declaration-list [opt] ... [opt]
21340 parameter-declaration-list , ...
21342 Returns a representation for the parameter declarations. A return
21343 value of NULL indicates a parameter-declaration-clause consisting
21344 only of an ellipsis. */
21347 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21353 temp_override
<bool> cleanup
21354 (parser
->auto_is_implicit_function_template_parm_p
);
21356 if (!processing_specialization
21357 && !processing_template_parmlist
21358 && !processing_explicit_instantiation
21359 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21360 actual function or a random abstract declarator. */
21361 && parser
->default_arg_ok_p
)
21362 if (!current_function_decl
21363 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21364 parser
->auto_is_implicit_function_template_parm_p
= true;
21366 /* Peek at the next token. */
21367 token
= cp_lexer_peek_token (parser
->lexer
);
21368 /* Check for trivial parameter-declaration-clauses. */
21369 if (token
->type
== CPP_ELLIPSIS
)
21371 /* Consume the `...' token. */
21372 cp_lexer_consume_token (parser
->lexer
);
21375 else if (token
->type
== CPP_CLOSE_PAREN
)
21376 /* There are no parameters. */
21378 #ifdef SYSTEM_IMPLICIT_EXTERN_C
21379 if (in_system_header_at (input_location
)
21380 && current_class_type
== NULL
21381 && current_lang_name
== lang_name_c
)
21385 return void_list_node
;
21387 /* Check for `(void)', too, which is a special case. */
21388 else if (token
->keyword
== RID_VOID
21389 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21390 == CPP_CLOSE_PAREN
))
21392 /* Consume the `void' token. */
21393 cp_lexer_consume_token (parser
->lexer
);
21394 /* There are no parameters. */
21395 return void_list_node
;
21398 /* Parse the parameter-declaration-list. */
21399 parameters
= cp_parser_parameter_declaration_list (parser
);
21400 /* If a parse error occurred while parsing the
21401 parameter-declaration-list, then the entire
21402 parameter-declaration-clause is erroneous. */
21403 if (parameters
== error_mark_node
)
21406 /* Peek at the next token. */
21407 token
= cp_lexer_peek_token (parser
->lexer
);
21408 /* If it's a `,', the clause should terminate with an ellipsis. */
21409 if (token
->type
== CPP_COMMA
)
21411 /* Consume the `,'. */
21412 cp_lexer_consume_token (parser
->lexer
);
21413 /* Expect an ellipsis. */
21415 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21417 /* It might also be `...' if the optional trailing `,' was
21419 else if (token
->type
== CPP_ELLIPSIS
)
21421 /* Consume the `...' token. */
21422 cp_lexer_consume_token (parser
->lexer
);
21423 /* And remember that we saw it. */
21427 ellipsis_p
= false;
21429 /* Finish the parameter list. */
21431 parameters
= chainon (parameters
, void_list_node
);
21436 /* Parse a parameter-declaration-list.
21438 parameter-declaration-list:
21439 parameter-declaration
21440 parameter-declaration-list , parameter-declaration
21442 Returns a representation of the parameter-declaration-list, as for
21443 cp_parser_parameter_declaration_clause. However, the
21444 `void_list_node' is never appended to the list. */
21447 cp_parser_parameter_declaration_list (cp_parser
* parser
)
21449 tree parameters
= NULL_TREE
;
21450 tree
*tail
= ¶meters
;
21451 bool saved_in_unbraced_linkage_specification_p
;
21454 /* The special considerations that apply to a function within an
21455 unbraced linkage specifications do not apply to the parameters
21456 to the function. */
21457 saved_in_unbraced_linkage_specification_p
21458 = parser
->in_unbraced_linkage_specification_p
;
21459 parser
->in_unbraced_linkage_specification_p
= false;
21461 /* Look for more parameters. */
21464 cp_parameter_declarator
*parameter
;
21465 tree decl
= error_mark_node
;
21466 bool parenthesized_p
= false;
21468 /* Parse the parameter. */
21470 = cp_parser_parameter_declaration (parser
,
21471 /*template_parm_p=*/false,
21474 /* We don't know yet if the enclosing context is deprecated, so wait
21475 and warn in grokparms if appropriate. */
21476 deprecated_state
= DEPRECATED_SUPPRESS
;
21480 decl
= grokdeclarator (parameter
->declarator
,
21481 ¶meter
->decl_specifiers
,
21483 parameter
->default_argument
!= NULL_TREE
,
21484 ¶meter
->decl_specifiers
.attributes
);
21485 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21486 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21489 deprecated_state
= DEPRECATED_NORMAL
;
21491 /* If a parse error occurred parsing the parameter declaration,
21492 then the entire parameter-declaration-list is erroneous. */
21493 if (decl
== error_mark_node
)
21495 parameters
= error_mark_node
;
21499 if (parameter
->decl_specifiers
.attributes
)
21500 cplus_decl_attributes (&decl
,
21501 parameter
->decl_specifiers
.attributes
,
21503 if (DECL_NAME (decl
))
21504 decl
= pushdecl (decl
);
21506 if (decl
!= error_mark_node
)
21508 retrofit_lang_decl (decl
);
21509 DECL_PARM_INDEX (decl
) = ++index
;
21510 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21513 /* Add the new parameter to the list. */
21514 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21515 tail
= &TREE_CHAIN (*tail
);
21517 /* Peek at the next token. */
21518 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21519 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21520 /* These are for Objective-C++ */
21521 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21522 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21523 /* The parameter-declaration-list is complete. */
21525 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21529 /* Peek at the next token. */
21530 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21531 /* If it's an ellipsis, then the list is complete. */
21532 if (token
->type
== CPP_ELLIPSIS
)
21534 /* Otherwise, there must be more parameters. Consume the
21536 cp_lexer_consume_token (parser
->lexer
);
21537 /* When parsing something like:
21539 int i(float f, double d)
21541 we can tell after seeing the declaration for "f" that we
21542 are not looking at an initialization of a variable "i",
21543 but rather at the declaration of a function "i".
21545 Due to the fact that the parsing of template arguments
21546 (as specified to a template-id) requires backtracking we
21547 cannot use this technique when inside a template argument
21549 if (!parser
->in_template_argument_list_p
21550 && !parser
->in_type_id_in_expr_p
21551 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21552 /* However, a parameter-declaration of the form
21553 "float(f)" (which is a valid declaration of a
21554 parameter "f") can also be interpreted as an
21555 expression (the conversion of "f" to "float"). */
21556 && !parenthesized_p
)
21557 cp_parser_commit_to_tentative_parse (parser
);
21561 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21562 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21563 cp_parser_skip_to_closing_parenthesis (parser
,
21564 /*recovering=*/true,
21565 /*or_comma=*/false,
21566 /*consume_paren=*/false);
21571 parser
->in_unbraced_linkage_specification_p
21572 = saved_in_unbraced_linkage_specification_p
;
21574 /* Reset implicit_template_scope if we are about to leave the function
21575 parameter list that introduced it. Note that for out-of-line member
21576 definitions, there will be one or more class scopes before we get to
21577 the template parameter scope. */
21579 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21580 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21582 while (maybe_its
->kind
== sk_class
)
21583 maybe_its
= maybe_its
->level_chain
;
21584 if (maybe_its
== its
)
21586 parser
->implicit_template_parms
= 0;
21587 parser
->implicit_template_scope
= 0;
21594 /* Parse a parameter declaration.
21596 parameter-declaration:
21597 decl-specifier-seq ... [opt] declarator
21598 decl-specifier-seq declarator = assignment-expression
21599 decl-specifier-seq ... [opt] abstract-declarator [opt]
21600 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21602 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21603 declares a template parameter. (In that case, a non-nested `>'
21604 token encountered during the parsing of the assignment-expression
21605 is not interpreted as a greater-than operator.)
21607 Returns a representation of the parameter, or NULL if an error
21608 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21609 true iff the declarator is of the form "(p)". */
21611 static cp_parameter_declarator
*
21612 cp_parser_parameter_declaration (cp_parser
*parser
,
21613 bool template_parm_p
,
21614 bool *parenthesized_p
)
21616 int declares_class_or_enum
;
21617 cp_decl_specifier_seq decl_specifiers
;
21618 cp_declarator
*declarator
;
21619 tree default_argument
;
21620 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21621 const char *saved_message
;
21622 bool template_parameter_pack_p
= false;
21624 /* In a template parameter, `>' is not an operator.
21628 When parsing a default template-argument for a non-type
21629 template-parameter, the first non-nested `>' is taken as the end
21630 of the template parameter-list rather than a greater-than
21633 /* Type definitions may not appear in parameter types. */
21634 saved_message
= parser
->type_definition_forbidden_message
;
21635 parser
->type_definition_forbidden_message
21636 = G_("types may not be defined in parameter types");
21638 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21639 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21640 (current_template_parms
)) : 0);
21642 /* Parse the declaration-specifiers. */
21643 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21644 cp_parser_decl_specifier_seq (parser
,
21645 CP_PARSER_FLAGS_NONE
,
21647 &declares_class_or_enum
);
21649 /* Complain about missing 'typename' or other invalid type names. */
21650 if (!decl_specifiers
.any_type_specifiers_p
21651 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21652 decl_specifiers
.type
= error_mark_node
;
21654 /* If an error occurred, there's no reason to attempt to parse the
21655 rest of the declaration. */
21656 if (cp_parser_error_occurred (parser
))
21658 parser
->type_definition_forbidden_message
= saved_message
;
21662 /* Peek at the next token. */
21663 token
= cp_lexer_peek_token (parser
->lexer
);
21665 /* If the next token is a `)', `,', `=', `>', or `...', then there
21666 is no declarator. However, when variadic templates are enabled,
21667 there may be a declarator following `...'. */
21668 if (token
->type
== CPP_CLOSE_PAREN
21669 || token
->type
== CPP_COMMA
21670 || token
->type
== CPP_EQ
21671 || token
->type
== CPP_GREATER
)
21674 if (parenthesized_p
)
21675 *parenthesized_p
= false;
21677 /* Otherwise, there should be a declarator. */
21680 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21681 parser
->default_arg_ok_p
= false;
21683 /* After seeing a decl-specifier-seq, if the next token is not a
21684 "(", there is no possibility that the code is a valid
21685 expression. Therefore, if parsing tentatively, we commit at
21687 if (!parser
->in_template_argument_list_p
21688 /* In an expression context, having seen:
21692 we cannot be sure whether we are looking at a
21693 function-type (taking a "char" as a parameter) or a cast
21694 of some object of type "char" to "int". */
21695 && !parser
->in_type_id_in_expr_p
21696 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21697 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21698 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21699 cp_parser_commit_to_tentative_parse (parser
);
21700 /* Parse the declarator. */
21701 declarator_token_start
= token
;
21702 declarator
= cp_parser_declarator (parser
,
21703 CP_PARSER_DECLARATOR_EITHER
,
21704 /*ctor_dtor_or_conv_p=*/NULL
,
21706 /*member_p=*/false,
21707 /*friend_p=*/false);
21708 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21709 /* After the declarator, allow more attributes. */
21710 decl_specifiers
.attributes
21711 = attr_chainon (decl_specifiers
.attributes
,
21712 cp_parser_attributes_opt (parser
));
21714 /* If the declarator is a template parameter pack, remember that and
21715 clear the flag in the declarator itself so we don't get errors
21716 from grokdeclarator. */
21717 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21719 declarator
->parameter_pack_p
= false;
21720 template_parameter_pack_p
= true;
21724 /* If the next token is an ellipsis, and we have not seen a declarator
21725 name, and if either the type of the declarator contains parameter
21726 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21727 for, eg, abbreviated integral type names), then we actually have a
21728 parameter pack expansion expression. Otherwise, leave the ellipsis
21729 for a C-style variadic function. */
21730 token
= cp_lexer_peek_token (parser
->lexer
);
21732 /* If a function parameter pack was specified and an implicit template
21733 parameter was introduced during cp_parser_parameter_declaration,
21734 change any implicit parameters introduced into packs. */
21735 if (parser
->implicit_template_parms
21736 && ((token
->type
== CPP_ELLIPSIS
21737 && declarator_can_be_parameter_pack (declarator
))
21738 || (declarator
&& declarator
->parameter_pack_p
)))
21740 int latest_template_parm_idx
= TREE_VEC_LENGTH
21741 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21743 if (latest_template_parm_idx
!= template_parm_idx
)
21744 decl_specifiers
.type
= convert_generic_types_to_packs
21745 (decl_specifiers
.type
,
21746 template_parm_idx
, latest_template_parm_idx
);
21749 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21751 tree type
= decl_specifiers
.type
;
21753 if (type
&& DECL_P (type
))
21754 type
= TREE_TYPE (type
);
21757 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21758 && (template_parm_p
|| uses_parameter_packs (type
)))
21759 || (!type
&& template_parm_p
))
21760 && declarator_can_be_parameter_pack (declarator
))
21762 /* Consume the `...'. */
21763 cp_lexer_consume_token (parser
->lexer
);
21764 maybe_warn_variadic_templates ();
21766 /* Build a pack expansion type */
21767 if (template_parm_p
)
21768 template_parameter_pack_p
= true;
21769 else if (declarator
)
21770 declarator
->parameter_pack_p
= true;
21772 decl_specifiers
.type
= make_pack_expansion (type
);
21776 /* The restriction on defining new types applies only to the type
21777 of the parameter, not to the default argument. */
21778 parser
->type_definition_forbidden_message
= saved_message
;
21780 /* If the next token is `=', then process a default argument. */
21781 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21783 tree type
= decl_specifiers
.type
;
21784 token
= cp_lexer_peek_token (parser
->lexer
);
21785 /* If we are defining a class, then the tokens that make up the
21786 default argument must be saved and processed later. */
21787 if (!template_parm_p
&& at_class_scope_p ()
21788 && TYPE_BEING_DEFINED (current_class_type
)
21789 && !LAMBDA_TYPE_P (current_class_type
))
21790 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21792 // A constrained-type-specifier may declare a type template-parameter.
21793 else if (declares_constrained_type_template_parameter (type
))
21795 = cp_parser_default_type_template_argument (parser
);
21797 // A constrained-type-specifier may declare a template-template-parameter.
21798 else if (declares_constrained_template_template_parameter (type
))
21800 = cp_parser_default_template_template_argument (parser
);
21802 /* Outside of a class definition, we can just parse the
21803 assignment-expression. */
21806 = cp_parser_default_argument (parser
, template_parm_p
);
21808 if (!parser
->default_arg_ok_p
)
21810 permerror (token
->location
,
21811 "default arguments are only "
21812 "permitted for function parameters");
21814 else if ((declarator
&& declarator
->parameter_pack_p
)
21815 || template_parameter_pack_p
21816 || (decl_specifiers
.type
21817 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21819 /* Find the name of the parameter pack. */
21820 cp_declarator
*id_declarator
= declarator
;
21821 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21822 id_declarator
= id_declarator
->declarator
;
21824 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21825 error_at (declarator_token_start
->location
,
21827 ? G_("template parameter pack %qD "
21828 "cannot have a default argument")
21829 : G_("parameter pack %qD cannot have "
21830 "a default argument"),
21831 id_declarator
->u
.id
.unqualified_name
);
21833 error_at (declarator_token_start
->location
,
21835 ? G_("template parameter pack cannot have "
21836 "a default argument")
21837 : G_("parameter pack cannot have a "
21838 "default argument"));
21840 default_argument
= NULL_TREE
;
21844 default_argument
= NULL_TREE
;
21846 /* Generate a location for the parameter, ranging from the start of the
21847 initial token to the end of the final token (using input_location for
21848 the latter, set up by cp_lexer_set_source_position_from_token when
21851 If we have a identifier, then use it for the caret location, e.g.
21853 extern int callee (int one, int (*two)(int, int), float three);
21854 ~~~~~~^~~~~~~~~~~~~~
21856 otherwise, reuse the start location for the caret location e.g.:
21858 extern int callee (int one, int (*)(int, int), float three);
21862 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21863 ? declarator
->id_loc
21864 : decl_spec_token_start
->location
);
21865 location_t param_loc
= make_location (caret_loc
,
21866 decl_spec_token_start
->location
,
21869 return make_parameter_declarator (&decl_specifiers
,
21873 template_parameter_pack_p
);
21876 /* Parse a default argument and return it.
21878 TEMPLATE_PARM_P is true if this is a default argument for a
21879 non-type template parameter. */
21881 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21883 tree default_argument
= NULL_TREE
;
21884 bool saved_greater_than_is_operator_p
;
21885 bool saved_local_variables_forbidden_p
;
21886 bool non_constant_p
, is_direct_init
;
21888 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21890 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21891 parser
->greater_than_is_operator_p
= !template_parm_p
;
21892 /* Local variable names (and the `this' keyword) may not
21893 appear in a default argument. */
21894 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21895 parser
->local_variables_forbidden_p
= true;
21896 /* Parse the assignment-expression. */
21897 if (template_parm_p
)
21898 push_deferring_access_checks (dk_no_deferred
);
21899 tree saved_class_ptr
= NULL_TREE
;
21900 tree saved_class_ref
= NULL_TREE
;
21901 /* The "this" pointer is not valid in a default argument. */
21904 saved_class_ptr
= current_class_ptr
;
21905 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21906 saved_class_ref
= current_class_ref
;
21907 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21910 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21911 /* Restore the "this" pointer. */
21914 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21915 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21917 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21918 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21919 if (template_parm_p
)
21920 pop_deferring_access_checks ();
21921 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21922 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21924 return default_argument
;
21927 /* Parse a function-body.
21930 compound_statement */
21933 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21935 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21936 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21940 /* Parse a ctor-initializer-opt followed by a function-body. Return
21941 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21942 is true we are parsing a function-try-block. */
21945 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21946 bool in_function_try_block
)
21949 const bool check_body_p
=
21950 DECL_CONSTRUCTOR_P (current_function_decl
)
21951 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21954 /* Begin the function body. */
21955 body
= begin_function_body ();
21956 /* Parse the optional ctor-initializer. */
21957 cp_parser_ctor_initializer_opt (parser
);
21959 /* If we're parsing a constexpr constructor definition, we need
21960 to check that the constructor body is indeed empty. However,
21961 before we get to cp_parser_function_body lot of junk has been
21962 generated, so we can't just check that we have an empty block.
21963 Rather we take a snapshot of the outermost block, and check whether
21964 cp_parser_function_body changed its state. */
21967 list
= cur_stmt_list
;
21968 if (STATEMENT_LIST_TAIL (list
))
21969 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21971 /* Parse the function-body. */
21972 cp_parser_function_body (parser
, in_function_try_block
);
21974 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21975 /* Finish the function body. */
21976 finish_function_body (body
);
21979 /* Parse an initializer.
21982 = initializer-clause
21983 ( expression-list )
21985 Returns an expression representing the initializer. If no
21986 initializer is present, NULL_TREE is returned.
21988 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21989 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21990 set to TRUE if there is no initializer present. If there is an
21991 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21992 is set to true; otherwise it is set to false. */
21995 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21996 bool* non_constant_p
, bool subexpression_p
)
22001 /* Peek at the next token. */
22002 token
= cp_lexer_peek_token (parser
->lexer
);
22004 /* Let our caller know whether or not this initializer was
22006 *is_direct_init
= (token
->type
!= CPP_EQ
);
22007 /* Assume that the initializer is constant. */
22008 *non_constant_p
= false;
22010 if (token
->type
== CPP_EQ
)
22012 /* Consume the `='. */
22013 cp_lexer_consume_token (parser
->lexer
);
22014 /* Parse the initializer-clause. */
22015 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22017 else if (token
->type
== CPP_OPEN_PAREN
)
22019 vec
<tree
, va_gc
> *vec
;
22020 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22022 /*allow_expansion_p=*/true,
22025 return error_mark_node
;
22026 init
= build_tree_list_vec (vec
);
22027 release_tree_vector (vec
);
22029 else if (token
->type
== CPP_OPEN_BRACE
)
22031 cp_lexer_set_source_position (parser
->lexer
);
22032 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22033 init
= cp_parser_braced_list (parser
, non_constant_p
);
22034 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22038 /* Anything else is an error. */
22039 cp_parser_error (parser
, "expected initializer");
22040 init
= error_mark_node
;
22043 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22044 init
= error_mark_node
;
22049 /* Parse an initializer-clause.
22051 initializer-clause:
22052 assignment-expression
22055 Returns an expression representing the initializer.
22057 If the `assignment-expression' production is used the value
22058 returned is simply a representation for the expression.
22060 Otherwise, calls cp_parser_braced_list. */
22063 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22065 cp_expr initializer
;
22067 /* Assume the expression is constant. */
22068 *non_constant_p
= false;
22070 /* If it is not a `{', then we are looking at an
22071 assignment-expression. */
22072 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22075 = cp_parser_constant_expression (parser
,
22076 /*allow_non_constant_p=*/true,
22080 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22082 return initializer
;
22085 /* Parse a brace-enclosed initializer list.
22088 { initializer-list , [opt] }
22089 { designated-initializer-list , [opt] }
22092 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22093 the elements of the initializer-list (or NULL, if the last
22094 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22095 NULL_TREE. There is no way to detect whether or not the optional
22096 trailing `,' was provided. NON_CONSTANT_P is as for
22097 cp_parser_initializer. */
22100 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22103 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22105 /* Consume the `{' token. */
22106 matching_braces braces
;
22107 braces
.require_open (parser
);
22108 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22109 initializer
= make_node (CONSTRUCTOR
);
22110 /* If it's not a `}', then there is a non-trivial initializer. */
22111 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22113 /* Parse the initializer list. */
22114 CONSTRUCTOR_ELTS (initializer
)
22115 = cp_parser_initializer_list (parser
, non_constant_p
);
22116 /* A trailing `,' token is allowed. */
22117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22118 cp_lexer_consume_token (parser
->lexer
);
22121 *non_constant_p
= false;
22122 /* Now, there should be a trailing `}'. */
22123 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22124 braces
.require_close (parser
);
22125 TREE_TYPE (initializer
) = init_list_type_node
;
22127 cp_expr
result (initializer
);
22128 /* Build a location of the form:
22131 with caret==start at the open brace, finish at the close brace. */
22132 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22133 result
.set_location (combined_loc
);
22137 /* Consume tokens up to, and including, the next non-nested closing `]'.
22138 Returns true iff we found a closing `]'. */
22141 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22143 unsigned square_depth
= 0;
22147 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22149 switch (token
->type
)
22152 case CPP_PRAGMA_EOL
:
22153 /* If we've run out of tokens, then there is no closing `]'. */
22156 case CPP_OPEN_SQUARE
:
22160 case CPP_CLOSE_SQUARE
:
22161 if (!square_depth
--)
22163 cp_lexer_consume_token (parser
->lexer
);
22172 /* Consume the token. */
22173 cp_lexer_consume_token (parser
->lexer
);
22177 /* Return true if we are looking at an array-designator, false otherwise. */
22180 cp_parser_array_designator_p (cp_parser
*parser
)
22182 /* Consume the `['. */
22183 cp_lexer_consume_token (parser
->lexer
);
22185 cp_lexer_save_tokens (parser
->lexer
);
22187 /* Skip tokens until the next token is a closing square bracket.
22188 If we find the closing `]', and the next token is a `=', then
22189 we are looking at an array designator. */
22190 bool array_designator_p
22191 = (cp_parser_skip_to_closing_square_bracket (parser
)
22192 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22194 /* Roll back the tokens we skipped. */
22195 cp_lexer_rollback_tokens (parser
->lexer
);
22197 return array_designator_p
;
22200 /* Parse an initializer-list.
22203 initializer-clause ... [opt]
22204 initializer-list , initializer-clause ... [opt]
22208 designated-initializer-list:
22209 designated-initializer-clause
22210 designated-initializer-list , designated-initializer-clause
22212 designated-initializer-clause:
22213 designator brace-or-equal-initializer
22221 designation initializer-clause ...[opt]
22222 initializer-list , designation initializer-clause ...[opt]
22227 [ constant-expression ] =
22229 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22230 for the initializer. If the INDEX of the elt is non-NULL, it is the
22231 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22232 as for cp_parser_initializer. */
22234 static vec
<constructor_elt
, va_gc
> *
22235 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22237 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22238 bool first_p
= true;
22239 tree first_designator
= NULL_TREE
;
22241 /* Assume all of the expressions are constant. */
22242 *non_constant_p
= false;
22244 /* Parse the rest of the list. */
22250 bool clause_non_constant_p
;
22251 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22253 /* Handle the C++2A syntax, '. id ='. */
22254 if ((cxx_dialect
>= cxx2a
22255 || cp_parser_allow_gnu_extensions_p (parser
))
22256 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22257 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22258 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22259 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22260 == CPP_OPEN_BRACE
)))
22262 if (cxx_dialect
< cxx2a
)
22263 pedwarn (loc
, OPT_Wpedantic
,
22264 "C++ designated initializers only available with "
22265 "-std=c++2a or -std=gnu++2a");
22266 /* Consume the `.'. */
22267 cp_lexer_consume_token (parser
->lexer
);
22268 /* Consume the identifier. */
22269 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22270 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22271 /* Consume the `='. */
22272 cp_lexer_consume_token (parser
->lexer
);
22274 /* Also, if the next token is an identifier and the following one is a
22275 colon, we are looking at the GNU designated-initializer
22277 else if (cp_parser_allow_gnu_extensions_p (parser
)
22278 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22279 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22282 /* Warn the user that they are using an extension. */
22283 pedwarn (loc
, OPT_Wpedantic
,
22284 "ISO C++ does not allow GNU designated initializers");
22285 /* Consume the identifier. */
22286 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22287 /* Consume the `:'. */
22288 cp_lexer_consume_token (parser
->lexer
);
22290 /* Also handle C99 array designators, '[ const ] ='. */
22291 else if (cp_parser_allow_gnu_extensions_p (parser
)
22292 && !c_dialect_objc ()
22293 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22295 /* In C++11, [ could start a lambda-introducer. */
22296 bool non_const
= false;
22298 cp_parser_parse_tentatively (parser
);
22300 if (!cp_parser_array_designator_p (parser
))
22302 cp_parser_simulate_error (parser
);
22303 designator
= NULL_TREE
;
22307 designator
= cp_parser_constant_expression (parser
, true,
22309 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22310 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22313 if (!cp_parser_parse_definitely (parser
))
22314 designator
= NULL_TREE
;
22316 && (!require_potential_rvalue_constant_expression
22318 designator
= NULL_TREE
;
22320 /* Warn the user that they are using an extension. */
22321 pedwarn (loc
, OPT_Wpedantic
,
22322 "ISO C++ does not allow C99 designated initializers");
22325 designator
= NULL_TREE
;
22329 first_designator
= designator
;
22332 else if (cxx_dialect
>= cxx2a
22333 && first_designator
!= error_mark_node
22334 && (!first_designator
!= !designator
))
22336 error_at (loc
, "either all initializer clauses should be designated "
22337 "or none of them should be");
22338 first_designator
= error_mark_node
;
22340 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22341 first_designator
= designator
;
22343 /* Parse the initializer. */
22344 initializer
= cp_parser_initializer_clause (parser
,
22345 &clause_non_constant_p
);
22346 /* If any clause is non-constant, so is the entire initializer. */
22347 if (clause_non_constant_p
)
22348 *non_constant_p
= true;
22350 /* If we have an ellipsis, this is an initializer pack
22352 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22354 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22356 /* Consume the `...'. */
22357 cp_lexer_consume_token (parser
->lexer
);
22359 if (designator
&& cxx_dialect
>= cxx2a
)
22361 "%<...%> not allowed in designated initializer list");
22363 /* Turn the initializer into an initializer expansion. */
22364 initializer
= make_pack_expansion (initializer
);
22367 /* Add it to the vector. */
22368 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22370 /* If the next token is not a comma, we have reached the end of
22372 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22375 /* Peek at the next token. */
22376 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22377 /* If the next token is a `}', then we're still done. An
22378 initializer-clause can have a trailing `,' after the
22379 initializer-list and before the closing `}'. */
22380 if (token
->type
== CPP_CLOSE_BRACE
)
22383 /* Consume the `,' token. */
22384 cp_lexer_consume_token (parser
->lexer
);
22387 /* The same identifier shall not appear in multiple designators
22388 of a designated-initializer-list. */
22389 if (first_designator
)
22392 tree designator
, val
;
22393 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22394 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22396 if (IDENTIFIER_MARKED (designator
))
22398 error_at (cp_expr_loc_or_loc (val
, input_location
),
22399 "%<.%s%> designator used multiple times in "
22400 "the same initializer list",
22401 IDENTIFIER_POINTER (designator
));
22402 (*v
)[i
].index
= NULL_TREE
;
22405 IDENTIFIER_MARKED (designator
) = 1;
22407 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22408 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22409 IDENTIFIER_MARKED (designator
) = 0;
22415 /* Classes [gram.class] */
22417 /* Parse a class-name.
22423 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22424 to indicate that names looked up in dependent types should be
22425 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22426 keyword has been used to indicate that the name that appears next
22427 is a template. TAG_TYPE indicates the explicit tag given before
22428 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22429 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22430 is the class being defined in a class-head. If ENUM_OK is TRUE,
22431 enum-names are also accepted.
22433 Returns the TYPE_DECL representing the class. */
22436 cp_parser_class_name (cp_parser
*parser
,
22437 bool typename_keyword_p
,
22438 bool template_keyword_p
,
22439 enum tag_types tag_type
,
22440 bool check_dependency_p
,
22442 bool is_declaration
,
22449 tree identifier
= NULL_TREE
;
22451 /* All class-names start with an identifier. */
22452 token
= cp_lexer_peek_token (parser
->lexer
);
22453 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22455 cp_parser_error (parser
, "expected class-name");
22456 return error_mark_node
;
22459 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22460 to a template-id, so we save it here. */
22461 scope
= parser
->scope
;
22462 if (scope
== error_mark_node
)
22463 return error_mark_node
;
22465 /* Any name names a type if we're following the `typename' keyword
22466 in a qualified name where the enclosing scope is type-dependent. */
22467 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22468 && dependent_type_p (scope
));
22469 /* Handle the common case (an identifier, but not a template-id)
22471 if (token
->type
== CPP_NAME
22472 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22474 cp_token
*identifier_token
;
22477 /* Look for the identifier. */
22478 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22479 ambiguous_p
= identifier_token
->error_reported
;
22480 identifier
= cp_parser_identifier (parser
);
22481 /* If the next token isn't an identifier, we are certainly not
22482 looking at a class-name. */
22483 if (identifier
== error_mark_node
)
22484 decl
= error_mark_node
;
22485 /* If we know this is a type-name, there's no need to look it
22487 else if (typename_p
)
22491 tree ambiguous_decls
;
22492 /* If we already know that this lookup is ambiguous, then
22493 we've already issued an error message; there's no reason
22497 cp_parser_simulate_error (parser
);
22498 return error_mark_node
;
22500 /* If the next token is a `::', then the name must be a type
22503 [basic.lookup.qual]
22505 During the lookup for a name preceding the :: scope
22506 resolution operator, object, function, and enumerator
22507 names are ignored. */
22508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22509 tag_type
= scope_type
;
22510 /* Look up the name. */
22511 decl
= cp_parser_lookup_name (parser
, identifier
,
22513 /*is_template=*/false,
22514 /*is_namespace=*/false,
22515 check_dependency_p
,
22517 identifier_token
->location
);
22518 if (ambiguous_decls
)
22520 if (cp_parser_parsing_tentatively (parser
))
22521 cp_parser_simulate_error (parser
);
22522 return error_mark_node
;
22528 /* Try a template-id. */
22529 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22530 check_dependency_p
,
22533 if (decl
== error_mark_node
)
22534 return error_mark_node
;
22537 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22539 /* If this is a typename, create a TYPENAME_TYPE. */
22540 if (typename_p
&& decl
!= error_mark_node
)
22542 decl
= make_typename_type (scope
, decl
, typename_type
,
22543 /*complain=*/tf_error
);
22544 if (decl
!= error_mark_node
)
22545 decl
= TYPE_NAME (decl
);
22548 decl
= strip_using_decl (decl
);
22550 /* Check to see that it is really the name of a class. */
22551 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22552 && identifier_p (TREE_OPERAND (decl
, 0))
22553 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22554 /* Situations like this:
22556 template <typename T> struct A {
22557 typename T::template X<int>::I i;
22560 are problematic. Is `T::template X<int>' a class-name? The
22561 standard does not seem to be definitive, but there is no other
22562 valid interpretation of the following `::'. Therefore, those
22563 names are considered class-names. */
22565 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22566 if (decl
!= error_mark_node
)
22567 decl
= TYPE_NAME (decl
);
22569 else if (TREE_CODE (decl
) != TYPE_DECL
22570 || TREE_TYPE (decl
) == error_mark_node
22571 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22572 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22573 /* In Objective-C 2.0, a classname followed by '.' starts a
22574 dot-syntax expression, and it's not a type-name. */
22575 || (c_dialect_objc ()
22576 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22577 && objc_is_class_name (decl
)))
22578 decl
= error_mark_node
;
22580 if (decl
== error_mark_node
)
22581 cp_parser_error (parser
, "expected class-name");
22582 else if (identifier
&& !parser
->scope
)
22583 maybe_note_name_used_in_class (identifier
, decl
);
22588 /* Parse a class-specifier.
22591 class-head { member-specification [opt] }
22593 Returns the TREE_TYPE representing the class. */
22596 cp_parser_class_specifier_1 (cp_parser
* parser
)
22599 tree attributes
= NULL_TREE
;
22600 bool nested_name_specifier_p
;
22601 unsigned saved_num_template_parameter_lists
;
22602 bool saved_in_function_body
;
22603 unsigned char in_statement
;
22604 bool in_switch_statement_p
;
22605 bool saved_in_unbraced_linkage_specification_p
;
22606 tree old_scope
= NULL_TREE
;
22607 tree scope
= NULL_TREE
;
22608 cp_token
*closing_brace
;
22610 push_deferring_access_checks (dk_no_deferred
);
22612 /* Parse the class-head. */
22613 type
= cp_parser_class_head (parser
,
22614 &nested_name_specifier_p
);
22615 /* If the class-head was a semantic disaster, skip the entire body
22619 cp_parser_skip_to_end_of_block_or_statement (parser
);
22620 pop_deferring_access_checks ();
22621 return error_mark_node
;
22624 /* Look for the `{'. */
22625 matching_braces braces
;
22626 if (!braces
.require_open (parser
))
22628 pop_deferring_access_checks ();
22629 return error_mark_node
;
22632 cp_ensure_no_omp_declare_simd (parser
);
22633 cp_ensure_no_oacc_routine (parser
);
22635 /* Issue an error message if type-definitions are forbidden here. */
22636 cp_parser_check_type_definition (parser
);
22637 /* Remember that we are defining one more class. */
22638 ++parser
->num_classes_being_defined
;
22639 /* Inside the class, surrounding template-parameter-lists do not
22641 saved_num_template_parameter_lists
22642 = parser
->num_template_parameter_lists
;
22643 parser
->num_template_parameter_lists
= 0;
22644 /* We are not in a function body. */
22645 saved_in_function_body
= parser
->in_function_body
;
22646 parser
->in_function_body
= false;
22647 /* Or in a loop. */
22648 in_statement
= parser
->in_statement
;
22649 parser
->in_statement
= 0;
22650 /* Or in a switch. */
22651 in_switch_statement_p
= parser
->in_switch_statement_p
;
22652 parser
->in_switch_statement_p
= false;
22653 /* We are not immediately inside an extern "lang" block. */
22654 saved_in_unbraced_linkage_specification_p
22655 = parser
->in_unbraced_linkage_specification_p
;
22656 parser
->in_unbraced_linkage_specification_p
= false;
22658 // Associate constraints with the type.
22660 type
= associate_classtype_constraints (type
);
22662 /* Start the class. */
22663 if (nested_name_specifier_p
)
22665 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22666 old_scope
= push_inner_scope (scope
);
22668 type
= begin_class_definition (type
);
22670 if (type
== error_mark_node
)
22671 /* If the type is erroneous, skip the entire body of the class. */
22672 cp_parser_skip_to_closing_brace (parser
);
22674 /* Parse the member-specification. */
22675 cp_parser_member_specification_opt (parser
);
22677 /* Look for the trailing `}'. */
22678 closing_brace
= braces
.require_close (parser
);
22679 /* Look for trailing attributes to apply to this class. */
22680 if (cp_parser_allow_gnu_extensions_p (parser
))
22681 attributes
= cp_parser_gnu_attributes_opt (parser
);
22682 if (type
!= error_mark_node
)
22683 type
= finish_struct (type
, attributes
);
22684 if (nested_name_specifier_p
)
22685 pop_inner_scope (old_scope
, scope
);
22687 /* We've finished a type definition. Check for the common syntax
22688 error of forgetting a semicolon after the definition. We need to
22689 be careful, as we can't just check for not-a-semicolon and be done
22690 with it; the user might have typed:
22692 class X { } c = ...;
22693 class X { } *p = ...;
22695 and so forth. Instead, enumerate all the possible tokens that
22696 might follow this production; if we don't see one of them, then
22697 complain and silently insert the semicolon. */
22699 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22700 bool want_semicolon
= true;
22702 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22703 /* Don't try to parse c++11 attributes here. As per the
22704 grammar, that should be a task for
22705 cp_parser_decl_specifier_seq. */
22706 want_semicolon
= false;
22708 switch (token
->type
)
22711 case CPP_SEMICOLON
:
22714 case CPP_OPEN_PAREN
:
22715 case CPP_CLOSE_PAREN
:
22717 want_semicolon
= false;
22720 /* While it's legal for type qualifiers and storage class
22721 specifiers to follow type definitions in the grammar, only
22722 compiler testsuites contain code like that. Assume that if
22723 we see such code, then what we're really seeing is a case
22727 const <type> var = ...;
22732 static <type> func (...) ...
22734 i.e. the qualifier or specifier applies to the next
22735 declaration. To do so, however, we need to look ahead one
22736 more token to see if *that* token is a type specifier.
22738 This code could be improved to handle:
22741 static const <type> var = ...; */
22743 if (keyword_is_decl_specifier (token
->keyword
))
22745 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22747 /* Handling user-defined types here would be nice, but very
22750 = (lookahead
->type
== CPP_KEYWORD
22751 && keyword_begins_type_specifier (lookahead
->keyword
));
22758 /* If we don't have a type, then something is very wrong and we
22759 shouldn't try to do anything clever. Likewise for not seeing the
22761 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22763 /* Locate the closing brace. */
22764 cp_token_position prev
22765 = cp_lexer_previous_token_position (parser
->lexer
);
22766 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22767 location_t loc
= prev_token
->location
;
22769 /* We want to suggest insertion of a ';' immediately *after* the
22770 closing brace, so, if we can, offset the location by 1 column. */
22771 location_t next_loc
= loc
;
22772 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22773 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22775 rich_location
richloc (line_table
, next_loc
);
22777 /* If we successfully offset the location, suggest the fix-it. */
22778 if (next_loc
!= loc
)
22779 richloc
.add_fixit_insert_before (next_loc
, ";");
22781 if (CLASSTYPE_DECLARED_CLASS (type
))
22782 error_at (&richloc
,
22783 "expected %<;%> after class definition");
22784 else if (TREE_CODE (type
) == RECORD_TYPE
)
22785 error_at (&richloc
,
22786 "expected %<;%> after struct definition");
22787 else if (TREE_CODE (type
) == UNION_TYPE
)
22788 error_at (&richloc
,
22789 "expected %<;%> after union definition");
22791 gcc_unreachable ();
22793 /* Unget one token and smash it to look as though we encountered
22794 a semicolon in the input stream. */
22795 cp_lexer_set_token_position (parser
->lexer
, prev
);
22796 token
= cp_lexer_peek_token (parser
->lexer
);
22797 token
->type
= CPP_SEMICOLON
;
22798 token
->keyword
= RID_MAX
;
22802 /* If this class is not itself within the scope of another class,
22803 then we need to parse the bodies of all of the queued function
22804 definitions. Note that the queued functions defined in a class
22805 are not always processed immediately following the
22806 class-specifier for that class. Consider:
22809 struct B { void f() { sizeof (A); } };
22812 If `f' were processed before the processing of `A' were
22813 completed, there would be no way to compute the size of `A'.
22814 Note that the nesting we are interested in here is lexical --
22815 not the semantic nesting given by TYPE_CONTEXT. In particular,
22818 struct A { struct B; };
22819 struct A::B { void f() { } };
22821 there is no need to delay the parsing of `A::B::f'. */
22822 if (--parser
->num_classes_being_defined
== 0)
22825 tree class_type
= NULL_TREE
;
22826 tree pushed_scope
= NULL_TREE
;
22828 cp_default_arg_entry
*e
;
22829 tree save_ccp
, save_ccr
;
22831 if (any_erroneous_template_args_p (type
))
22833 /* Skip default arguments, NSDMIs, etc, in order to improve
22834 error recovery (c++/71169, c++/71832). */
22835 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22836 vec_safe_truncate (unparsed_nsdmis
, 0);
22837 vec_safe_truncate (unparsed_classes
, 0);
22838 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22841 /* In a first pass, parse default arguments to the functions.
22842 Then, in a second pass, parse the bodies of the functions.
22843 This two-phased approach handles cases like:
22851 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22854 /* If there are default arguments that have not yet been processed,
22855 take care of them now. */
22856 if (class_type
!= e
->class_type
)
22859 pop_scope (pushed_scope
);
22860 class_type
= e
->class_type
;
22861 pushed_scope
= push_scope (class_type
);
22863 /* Make sure that any template parameters are in scope. */
22864 maybe_begin_member_template_processing (decl
);
22865 /* Parse the default argument expressions. */
22866 cp_parser_late_parsing_default_args (parser
, decl
);
22867 /* Remove any template parameters from the symbol table. */
22868 maybe_end_member_template_processing ();
22870 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22871 /* Now parse any NSDMIs. */
22872 save_ccp
= current_class_ptr
;
22873 save_ccr
= current_class_ref
;
22874 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22876 if (class_type
!= DECL_CONTEXT (decl
))
22879 pop_scope (pushed_scope
);
22880 class_type
= DECL_CONTEXT (decl
);
22881 pushed_scope
= push_scope (class_type
);
22883 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22884 cp_parser_late_parsing_nsdmi (parser
, decl
);
22886 vec_safe_truncate (unparsed_nsdmis
, 0);
22887 current_class_ptr
= save_ccp
;
22888 current_class_ref
= save_ccr
;
22890 pop_scope (pushed_scope
);
22892 /* Now do some post-NSDMI bookkeeping. */
22893 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22894 after_nsdmi_defaulted_late_checks (class_type
);
22895 vec_safe_truncate (unparsed_classes
, 0);
22896 after_nsdmi_defaulted_late_checks (type
);
22898 /* Now parse the body of the functions. */
22901 /* OpenMP UDRs need to be parsed before all other functions. */
22902 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22903 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22904 cp_parser_late_parsing_for_member (parser
, decl
);
22905 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22906 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22907 cp_parser_late_parsing_for_member (parser
, decl
);
22910 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22911 cp_parser_late_parsing_for_member (parser
, decl
);
22912 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22915 vec_safe_push (unparsed_classes
, type
);
22917 /* Put back any saved access checks. */
22918 pop_deferring_access_checks ();
22920 /* Restore saved state. */
22921 parser
->in_switch_statement_p
= in_switch_statement_p
;
22922 parser
->in_statement
= in_statement
;
22923 parser
->in_function_body
= saved_in_function_body
;
22924 parser
->num_template_parameter_lists
22925 = saved_num_template_parameter_lists
;
22926 parser
->in_unbraced_linkage_specification_p
22927 = saved_in_unbraced_linkage_specification_p
;
22933 cp_parser_class_specifier (cp_parser
* parser
)
22936 timevar_push (TV_PARSE_STRUCT
);
22937 ret
= cp_parser_class_specifier_1 (parser
);
22938 timevar_pop (TV_PARSE_STRUCT
);
22942 /* Parse a class-head.
22945 class-key identifier [opt] base-clause [opt]
22946 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22947 class-key nested-name-specifier [opt] template-id
22950 class-virt-specifier:
22954 class-key attributes identifier [opt] base-clause [opt]
22955 class-key attributes nested-name-specifier identifier base-clause [opt]
22956 class-key attributes nested-name-specifier [opt] template-id
22959 Upon return BASES is initialized to the list of base classes (or
22960 NULL, if there are none) in the same form returned by
22961 cp_parser_base_clause.
22963 Returns the TYPE of the indicated class. Sets
22964 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22965 involving a nested-name-specifier was used, and FALSE otherwise.
22967 Returns error_mark_node if this is not a class-head.
22969 Returns NULL_TREE if the class-head is syntactically valid, but
22970 semantically invalid in a way that means we should skip the entire
22971 body of the class. */
22974 cp_parser_class_head (cp_parser
* parser
,
22975 bool* nested_name_specifier_p
)
22977 tree nested_name_specifier
;
22978 enum tag_types class_key
;
22979 tree id
= NULL_TREE
;
22980 tree type
= NULL_TREE
;
22983 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22984 bool template_id_p
= false;
22985 bool qualified_p
= false;
22986 bool invalid_nested_name_p
= false;
22987 bool invalid_explicit_specialization_p
= false;
22988 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22989 tree pushed_scope
= NULL_TREE
;
22990 unsigned num_templates
;
22991 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22992 /* Assume no nested-name-specifier will be present. */
22993 *nested_name_specifier_p
= false;
22994 /* Assume no template parameter lists will be used in defining the
22997 parser
->colon_corrects_to_scope_p
= false;
22999 /* Look for the class-key. */
23000 class_key
= cp_parser_class_key (parser
);
23001 if (class_key
== none_type
)
23002 return error_mark_node
;
23004 location_t class_head_start_location
= input_location
;
23006 /* Parse the attributes. */
23007 attributes
= cp_parser_attributes_opt (parser
);
23009 /* If the next token is `::', that is invalid -- but sometimes
23010 people do try to write:
23014 Handle this gracefully by accepting the extra qualifier, and then
23015 issuing an error about it later if this really is a
23016 class-head. If it turns out just to be an elaborated type
23017 specifier, remain silent. */
23018 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23019 qualified_p
= true;
23021 push_deferring_access_checks (dk_no_check
);
23023 /* Determine the name of the class. Begin by looking for an
23024 optional nested-name-specifier. */
23025 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23026 nested_name_specifier
23027 = cp_parser_nested_name_specifier_opt (parser
,
23028 /*typename_keyword_p=*/false,
23029 /*check_dependency_p=*/false,
23031 /*is_declaration=*/false);
23032 /* If there was a nested-name-specifier, then there *must* be an
23035 cp_token
*bad_template_keyword
= NULL
;
23037 if (nested_name_specifier
)
23039 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23040 /* Although the grammar says `identifier', it really means
23041 `class-name' or `template-name'. You are only allowed to
23042 define a class that has already been declared with this
23045 The proposed resolution for Core Issue 180 says that wherever
23046 you see `class T::X' you should treat `X' as a type-name.
23048 It is OK to define an inaccessible class; for example:
23050 class A { class B; };
23053 We do not know if we will see a class-name, or a
23054 template-name. We look for a class-name first, in case the
23055 class-name is a template-id; if we looked for the
23056 template-name first we would stop after the template-name. */
23057 cp_parser_parse_tentatively (parser
);
23058 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23059 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23060 type
= cp_parser_class_name (parser
,
23061 /*typename_keyword_p=*/false,
23062 /*template_keyword_p=*/false,
23064 /*check_dependency_p=*/false,
23065 /*class_head_p=*/true,
23066 /*is_declaration=*/false);
23067 /* If that didn't work, ignore the nested-name-specifier. */
23068 if (!cp_parser_parse_definitely (parser
))
23070 invalid_nested_name_p
= true;
23071 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23072 id
= cp_parser_identifier (parser
);
23073 if (id
== error_mark_node
)
23076 /* If we could not find a corresponding TYPE, treat this
23077 declaration like an unqualified declaration. */
23078 if (type
== error_mark_node
)
23079 nested_name_specifier
= NULL_TREE
;
23080 /* Otherwise, count the number of templates used in TYPE and its
23081 containing scopes. */
23083 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23085 /* Otherwise, the identifier is optional. */
23088 /* We don't know whether what comes next is a template-id,
23089 an identifier, or nothing at all. */
23090 cp_parser_parse_tentatively (parser
);
23091 /* Check for a template-id. */
23092 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23093 id
= cp_parser_template_id (parser
,
23094 /*template_keyword_p=*/false,
23095 /*check_dependency_p=*/true,
23097 /*is_declaration=*/true);
23098 /* If that didn't work, it could still be an identifier. */
23099 if (!cp_parser_parse_definitely (parser
))
23101 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23103 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23104 id
= cp_parser_identifier (parser
);
23111 template_id_p
= true;
23116 pop_deferring_access_checks ();
23120 cp_parser_check_for_invalid_template_id (parser
, id
,
23122 type_start_token
->location
);
23124 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23126 /* If it's not a `:' or a `{' then we can't really be looking at a
23127 class-head, since a class-head only appears as part of a
23128 class-specifier. We have to detect this situation before calling
23129 xref_tag, since that has irreversible side-effects. */
23130 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23132 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23133 type
= error_mark_node
;
23137 /* At this point, we're going ahead with the class-specifier, even
23138 if some other problem occurs. */
23139 cp_parser_commit_to_tentative_parse (parser
);
23140 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23142 cp_parser_error (parser
,
23143 "cannot specify %<override%> for a class");
23144 type
= error_mark_node
;
23147 /* Issue the error about the overly-qualified name now. */
23150 cp_parser_error (parser
,
23151 "global qualification of class name is invalid");
23152 type
= error_mark_node
;
23155 else if (invalid_nested_name_p
)
23157 cp_parser_error (parser
,
23158 "qualified name does not name a class");
23159 type
= error_mark_node
;
23162 else if (nested_name_specifier
)
23166 if (bad_template_keyword
)
23167 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23168 keyword template shall not appear at the top level. */
23169 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23170 "keyword %<template%> not allowed in class-head-name");
23172 /* Reject typedef-names in class heads. */
23173 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23175 error_at (type_start_token
->location
,
23176 "invalid class name in declaration of %qD",
23182 /* Figure out in what scope the declaration is being placed. */
23183 scope
= current_scope ();
23184 /* If that scope does not contain the scope in which the
23185 class was originally declared, the program is invalid. */
23186 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23188 if (at_namespace_scope_p ())
23189 error_at (type_start_token
->location
,
23190 "declaration of %qD in namespace %qD which does not "
23192 type
, scope
, nested_name_specifier
);
23194 error_at (type_start_token
->location
,
23195 "declaration of %qD in %qD which does not enclose %qD",
23196 type
, scope
, nested_name_specifier
);
23202 A declarator-id shall not be qualified except for the
23203 definition of a ... nested class outside of its class
23204 ... [or] the definition or explicit instantiation of a
23205 class member of a namespace outside of its namespace. */
23206 if (scope
== nested_name_specifier
)
23208 permerror (nested_name_specifier_token_start
->location
,
23209 "extra qualification not allowed");
23210 nested_name_specifier
= NULL_TREE
;
23214 /* An explicit-specialization must be preceded by "template <>". If
23215 it is not, try to recover gracefully. */
23216 if (at_namespace_scope_p ()
23217 && parser
->num_template_parameter_lists
== 0
23218 && !processing_template_parmlist
23221 /* Build a location of this form:
23222 struct typename <ARGS>
23223 ^~~~~~~~~~~~~~~~~~~~~~
23224 with caret==start at the start token, and
23225 finishing at the end of the type. */
23226 location_t reported_loc
23227 = make_location (class_head_start_location
,
23228 class_head_start_location
,
23229 get_finish (type_start_token
->location
));
23230 rich_location
richloc (line_table
, reported_loc
);
23231 richloc
.add_fixit_insert_before (class_head_start_location
,
23233 error_at (&richloc
,
23234 "an explicit specialization must be preceded by"
23235 " %<template <>%>");
23236 invalid_explicit_specialization_p
= true;
23237 /* Take the same action that would have been taken by
23238 cp_parser_explicit_specialization. */
23239 ++parser
->num_template_parameter_lists
;
23240 begin_specialization ();
23242 /* There must be no "return" statements between this point and the
23243 end of this function; set "type "to the correct return value and
23244 use "goto done;" to return. */
23245 /* Make sure that the right number of template parameters were
23247 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23249 type_start_token
->location
,
23250 /*declarator=*/NULL
))
23252 /* If something went wrong, there is no point in even trying to
23253 process the class-definition. */
23258 /* Look up the type. */
23261 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23262 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23263 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23265 error_at (type_start_token
->location
,
23266 "function template %qD redeclared as a class template", id
);
23267 type
= error_mark_node
;
23271 type
= TREE_TYPE (id
);
23272 type
= maybe_process_partial_specialization (type
);
23274 /* Check the scope while we still know whether or not we had a
23275 nested-name-specifier. */
23276 if (type
!= error_mark_node
)
23277 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23279 if (nested_name_specifier
)
23280 pushed_scope
= push_scope (nested_name_specifier
);
23282 else if (nested_name_specifier
)
23288 template <typename T> struct S { struct T };
23289 template <typename T> struct S<T>::T { };
23291 we will get a TYPENAME_TYPE when processing the definition of
23292 `S::T'. We need to resolve it to the actual type before we
23293 try to define it. */
23294 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23296 class_type
= resolve_typename_type (TREE_TYPE (type
),
23297 /*only_current_p=*/false);
23298 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23299 type
= TYPE_NAME (class_type
);
23302 cp_parser_error (parser
, "could not resolve typename type");
23303 type
= error_mark_node
;
23307 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23308 == error_mark_node
)
23314 class_type
= current_class_type
;
23315 /* Enter the scope indicated by the nested-name-specifier. */
23316 pushed_scope
= push_scope (nested_name_specifier
);
23317 /* Get the canonical version of this type. */
23318 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23319 /* Call push_template_decl if it seems like we should be defining a
23320 template either from the template headers or the type we're
23321 defining, so that we diagnose both extra and missing headers. */
23322 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23323 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23324 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23326 type
= push_template_decl (type
);
23327 if (type
== error_mark_node
)
23334 type
= TREE_TYPE (type
);
23335 *nested_name_specifier_p
= true;
23337 else /* The name is not a nested name. */
23339 /* If the class was unnamed, create a dummy name. */
23341 id
= make_anon_name ();
23342 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23343 ? ts_within_enclosing_non_class
23345 type
= xref_tag (class_key
, id
, tag_scope
,
23346 parser
->num_template_parameter_lists
);
23349 /* Indicate whether this class was declared as a `class' or as a
23351 if (TREE_CODE (type
) == RECORD_TYPE
)
23352 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23353 cp_parser_check_class_key (class_key
, type
);
23355 /* If this type was already complete, and we see another definition,
23356 that's an error. */
23357 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23359 error_at (type_start_token
->location
, "redefinition of %q#T",
23361 inform (location_of (type
), "previous definition of %q#T",
23366 else if (type
== error_mark_node
)
23371 /* Apply attributes now, before any use of the class as a template
23372 argument in its base list. */
23373 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23374 fixup_attribute_variants (type
);
23377 /* We will have entered the scope containing the class; the names of
23378 base classes should be looked up in that context. For example:
23380 struct A { struct B {}; struct C; };
23381 struct A::C : B {};
23385 /* Get the list of base-classes, if there is one. */
23386 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23388 /* PR59482: enter the class scope so that base-specifiers are looked
23392 bases
= cp_parser_base_clause (parser
);
23393 /* PR59482: get out of the previously pushed class scope so that the
23394 subsequent pops pop the right thing. */
23401 /* If we're really defining a class, process the base classes.
23402 If they're invalid, fail. */
23403 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23404 xref_basetypes (type
, bases
);
23407 /* Leave the scope given by the nested-name-specifier. We will
23408 enter the class scope itself while processing the members. */
23410 pop_scope (pushed_scope
);
23412 if (invalid_explicit_specialization_p
)
23414 end_specialization ();
23415 --parser
->num_template_parameter_lists
;
23419 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23420 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23421 CLASSTYPE_FINAL (type
) = 1;
23423 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23427 /* Parse a class-key.
23434 Returns the kind of class-key specified, or none_type to indicate
23437 static enum tag_types
23438 cp_parser_class_key (cp_parser
* parser
)
23441 enum tag_types tag_type
;
23443 /* Look for the class-key. */
23444 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23448 /* Check to see if the TOKEN is a class-key. */
23449 tag_type
= cp_parser_token_is_class_key (token
);
23451 cp_parser_error (parser
, "expected class-key");
23455 /* Parse a type-parameter-key.
23457 type-parameter-key:
23463 cp_parser_type_parameter_key (cp_parser
* parser
)
23465 /* Look for the type-parameter-key. */
23466 enum tag_types tag_type
= none_type
;
23467 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23468 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23470 cp_lexer_consume_token (parser
->lexer
);
23471 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23472 /* typename is not allowed in a template template parameter
23473 by the standard until C++17. */
23474 pedwarn (token
->location
, OPT_Wpedantic
,
23475 "ISO C++ forbids typename key in template template parameter;"
23476 " use -std=c++17 or -std=gnu++17");
23479 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23484 /* Parse an (optional) member-specification.
23486 member-specification:
23487 member-declaration member-specification [opt]
23488 access-specifier : member-specification [opt] */
23491 cp_parser_member_specification_opt (cp_parser
* parser
)
23498 /* Peek at the next token. */
23499 token
= cp_lexer_peek_token (parser
->lexer
);
23500 /* If it's a `}', or EOF then we've seen all the members. */
23501 if (token
->type
== CPP_CLOSE_BRACE
23502 || token
->type
== CPP_EOF
23503 || token
->type
== CPP_PRAGMA_EOL
)
23506 /* See if this token is a keyword. */
23507 keyword
= token
->keyword
;
23511 case RID_PROTECTED
:
23513 /* Consume the access-specifier. */
23514 cp_lexer_consume_token (parser
->lexer
);
23515 /* Remember which access-specifier is active. */
23516 current_access_specifier
= token
->u
.value
;
23517 /* Look for the `:'. */
23518 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23522 /* Accept #pragmas at class scope. */
23523 if (token
->type
== CPP_PRAGMA
)
23525 cp_parser_pragma (parser
, pragma_member
, NULL
);
23529 /* Otherwise, the next construction must be a
23530 member-declaration. */
23531 cp_parser_member_declaration (parser
);
23536 /* Parse a member-declaration.
23538 member-declaration:
23539 decl-specifier-seq [opt] member-declarator-list [opt] ;
23540 function-definition ; [opt]
23541 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23543 template-declaration
23546 member-declarator-list:
23548 member-declarator-list , member-declarator
23551 declarator pure-specifier [opt]
23552 declarator constant-initializer [opt]
23553 identifier [opt] : constant-expression
23557 member-declaration:
23558 __extension__ member-declaration
23561 declarator attributes [opt] pure-specifier [opt]
23562 declarator attributes [opt] constant-initializer [opt]
23563 identifier [opt] attributes [opt] : constant-expression
23567 member-declaration:
23568 static_assert-declaration */
23571 cp_parser_member_declaration (cp_parser
* parser
)
23573 cp_decl_specifier_seq decl_specifiers
;
23574 tree prefix_attributes
;
23576 int declares_class_or_enum
;
23578 cp_token
*token
= NULL
;
23579 cp_token
*decl_spec_token_start
= NULL
;
23580 cp_token
*initializer_token_start
= NULL
;
23581 int saved_pedantic
;
23582 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23584 /* Check for the `__extension__' keyword. */
23585 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23588 cp_parser_member_declaration (parser
);
23589 /* Restore the old value of the PEDANTIC flag. */
23590 pedantic
= saved_pedantic
;
23595 /* Check for a template-declaration. */
23596 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23598 /* An explicit specialization here is an error condition, and we
23599 expect the specialization handler to detect and report this. */
23600 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23601 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23602 cp_parser_explicit_specialization (parser
);
23604 cp_parser_template_declaration (parser
, /*member_p=*/true);
23608 /* Check for a template introduction. */
23609 else if (cp_parser_template_declaration_after_export (parser
, true))
23612 /* Check for a using-declaration. */
23613 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23615 if (cxx_dialect
< cxx11
)
23617 /* Parse the using-declaration. */
23618 cp_parser_using_declaration (parser
,
23619 /*access_declaration_p=*/false);
23625 bool alias_decl_expected
;
23626 cp_parser_parse_tentatively (parser
);
23627 decl
= cp_parser_alias_declaration (parser
);
23628 /* Note that if we actually see the '=' token after the
23629 identifier, cp_parser_alias_declaration commits the
23630 tentative parse. In that case, we really expect an
23631 alias-declaration. Otherwise, we expect a using
23633 alias_decl_expected
=
23634 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23635 cp_parser_parse_definitely (parser
);
23637 if (alias_decl_expected
)
23638 finish_member_declaration (decl
);
23640 cp_parser_using_declaration (parser
,
23641 /*access_declaration_p=*/false);
23646 /* Check for @defs. */
23647 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23650 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23651 ivar
= ivar_chains
;
23655 ivar
= TREE_CHAIN (member
);
23656 TREE_CHAIN (member
) = NULL_TREE
;
23657 finish_member_declaration (member
);
23662 /* If the next token is `static_assert' we have a static assertion. */
23663 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23665 cp_parser_static_assert (parser
, /*member_p=*/true);
23669 parser
->colon_corrects_to_scope_p
= false;
23671 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23674 /* Parse the decl-specifier-seq. */
23675 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23676 cp_parser_decl_specifier_seq (parser
,
23677 CP_PARSER_FLAGS_OPTIONAL
,
23679 &declares_class_or_enum
);
23680 /* Check for an invalid type-name. */
23681 if (!decl_specifiers
.any_type_specifiers_p
23682 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23684 /* If there is no declarator, then the decl-specifier-seq should
23686 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23688 /* If there was no decl-specifier-seq, and the next token is a
23689 `;', then we have something like:
23695 Each member-declaration shall declare at least one member
23696 name of the class. */
23697 if (!decl_specifiers
.any_specifiers_p
)
23699 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23700 if (!in_system_header_at (token
->location
))
23702 gcc_rich_location
richloc (token
->location
);
23703 richloc
.add_fixit_remove ();
23704 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23711 /* See if this declaration is a friend. */
23712 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23713 /* If there were decl-specifiers, check to see if there was
23714 a class-declaration. */
23715 type
= check_tag_decl (&decl_specifiers
,
23716 /*explicit_type_instantiation_p=*/false);
23717 /* Nested classes have already been added to the class, but
23718 a `friend' needs to be explicitly registered. */
23721 /* If the `friend' keyword was present, the friend must
23722 be introduced with a class-key. */
23723 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23724 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23725 "in C++03 a class-key must be used "
23726 "when declaring a friend");
23729 template <typename T> struct A {
23730 friend struct A<T>::B;
23733 A<T>::B will be represented by a TYPENAME_TYPE, and
23734 therefore not recognized by check_tag_decl. */
23737 type
= decl_specifiers
.type
;
23738 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23739 type
= TREE_TYPE (type
);
23741 if (!type
|| !TYPE_P (type
))
23742 error_at (decl_spec_token_start
->location
,
23743 "friend declaration does not name a class or "
23746 make_friend_class (current_class_type
, type
,
23747 /*complain=*/true);
23749 /* If there is no TYPE, an error message will already have
23751 else if (!type
|| type
== error_mark_node
)
23753 /* An anonymous aggregate has to be handled specially; such
23754 a declaration really declares a data member (with a
23755 particular type), as opposed to a nested class. */
23756 else if (ANON_AGGR_TYPE_P (type
))
23759 if (decl_specifiers
.storage_class
!= sc_none
)
23760 error_at (decl_spec_token_start
->location
,
23761 "a storage class on an anonymous aggregate "
23762 "in class scope is not allowed");
23764 /* Remove constructors and such from TYPE, now that we
23765 know it is an anonymous aggregate. */
23766 fixup_anonymous_aggr (type
);
23767 /* And make the corresponding data member. */
23768 decl
= build_decl (decl_spec_token_start
->location
,
23769 FIELD_DECL
, NULL_TREE
, type
);
23770 /* Add it to the class. */
23771 finish_member_declaration (decl
);
23774 cp_parser_check_access_in_redeclaration
23776 decl_spec_token_start
->location
);
23781 bool assume_semicolon
= false;
23783 /* Clear attributes from the decl_specifiers but keep them
23784 around as prefix attributes that apply them to the entity
23786 prefix_attributes
= decl_specifiers
.attributes
;
23787 decl_specifiers
.attributes
= NULL_TREE
;
23789 /* See if these declarations will be friends. */
23790 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23792 /* Keep going until we hit the `;' at the end of the
23794 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23796 tree attributes
= NULL_TREE
;
23797 tree first_attribute
;
23799 bool named_bitfld
= false;
23801 /* Peek at the next token. */
23802 token
= cp_lexer_peek_token (parser
->lexer
);
23804 /* The following code wants to know early if it is a bit-field
23805 or some other declaration. Attributes can appear before
23806 the `:' token. Skip over them without consuming any tokens
23807 to peek if they are followed by `:'. */
23808 if (cp_next_tokens_can_be_attribute_p (parser
)
23809 || (token
->type
== CPP_NAME
23810 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23811 && (named_bitfld
= true)))
23814 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23815 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23818 /* Check for a bitfield declaration. */
23819 if (token
->type
== CPP_COLON
23820 || (token
->type
== CPP_NAME
23821 && token
== cp_lexer_peek_token (parser
->lexer
)
23822 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23823 && (named_bitfld
= true)))
23827 tree late_attributes
= NULL_TREE
;
23830 identifier
= cp_parser_identifier (parser
);
23832 identifier
= NULL_TREE
;
23834 /* Look for attributes that apply to the bitfield. */
23835 attributes
= cp_parser_attributes_opt (parser
);
23837 /* Consume the `:' token. */
23838 cp_lexer_consume_token (parser
->lexer
);
23840 /* Get the width of the bitfield. */
23841 width
= cp_parser_constant_expression (parser
, false, NULL
,
23842 cxx_dialect
>= cxx11
);
23844 /* In C++2A and as extension for C++11 and above we allow
23845 default member initializers for bit-fields. */
23846 initializer
= NULL_TREE
;
23847 if (cxx_dialect
>= cxx11
23848 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23849 || cp_lexer_next_token_is (parser
->lexer
,
23853 = cp_lexer_peek_token (parser
->lexer
)->location
;
23854 if (cxx_dialect
< cxx2a
23855 && !in_system_header_at (loc
)
23856 && identifier
!= NULL_TREE
)
23858 "default member initializers for bit-fields "
23859 "only available with -std=c++2a or "
23862 initializer
= cp_parser_save_nsdmi (parser
);
23863 if (identifier
== NULL_TREE
)
23865 error_at (loc
, "default member initializer for "
23866 "unnamed bit-field");
23867 initializer
= NULL_TREE
;
23872 /* Look for attributes that apply to the bitfield after
23873 the `:' token and width. This is where GCC used to
23874 parse attributes in the past, pedwarn if there is
23875 a std attribute. */
23876 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23877 pedwarn (input_location
, OPT_Wpedantic
,
23878 "ISO C++ allows bit-field attributes only "
23879 "before the %<:%> token");
23881 late_attributes
= cp_parser_attributes_opt (parser
);
23884 attributes
= attr_chainon (attributes
, late_attributes
);
23886 /* Remember which attributes are prefix attributes and
23888 first_attribute
= attributes
;
23889 /* Combine the attributes. */
23890 attributes
= attr_chainon (prefix_attributes
, attributes
);
23892 /* Create the bitfield declaration. */
23893 decl
= grokbitfield (identifier
23894 ? make_id_declarator (NULL_TREE
,
23899 width
, initializer
,
23904 cp_declarator
*declarator
;
23905 tree asm_specification
;
23906 int ctor_dtor_or_conv_p
;
23908 /* Parse the declarator. */
23910 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23911 &ctor_dtor_or_conv_p
,
23912 /*parenthesized_p=*/NULL
,
23916 /* If something went wrong parsing the declarator, make sure
23917 that we at least consume some tokens. */
23918 if (declarator
== cp_error_declarator
)
23920 /* Skip to the end of the statement. */
23921 cp_parser_skip_to_end_of_statement (parser
);
23922 /* If the next token is not a semicolon, that is
23923 probably because we just skipped over the body of
23924 a function. So, we consume a semicolon if
23925 present, but do not issue an error message if it
23927 if (cp_lexer_next_token_is (parser
->lexer
,
23929 cp_lexer_consume_token (parser
->lexer
);
23933 if (declares_class_or_enum
& 2)
23934 cp_parser_check_for_definition_in_return_type
23935 (declarator
, decl_specifiers
.type
,
23936 decl_specifiers
.locations
[ds_type_spec
]);
23938 /* Look for an asm-specification. */
23939 asm_specification
= cp_parser_asm_specification_opt (parser
);
23940 /* Look for attributes that apply to the declaration. */
23941 attributes
= cp_parser_attributes_opt (parser
);
23942 /* Remember which attributes are prefix attributes and
23944 first_attribute
= attributes
;
23945 /* Combine the attributes. */
23946 attributes
= attr_chainon (prefix_attributes
, attributes
);
23948 /* If it's an `=', then we have a constant-initializer or a
23949 pure-specifier. It is not correct to parse the
23950 initializer before registering the member declaration
23951 since the member declaration should be in scope while
23952 its initializer is processed. However, the rest of the
23953 front end does not yet provide an interface that allows
23954 us to handle this correctly. */
23955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23959 A pure-specifier shall be used only in the declaration of
23960 a virtual function.
23962 A member-declarator can contain a constant-initializer
23963 only if it declares a static member of integral or
23966 Therefore, if the DECLARATOR is for a function, we look
23967 for a pure-specifier; otherwise, we look for a
23968 constant-initializer. When we call `grokfield', it will
23969 perform more stringent semantics checks. */
23970 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23971 if (function_declarator_p (declarator
)
23972 || (decl_specifiers
.type
23973 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23974 && declarator
->kind
== cdk_id
23975 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23976 == FUNCTION_TYPE
)))
23977 initializer
= cp_parser_pure_specifier (parser
);
23978 else if (decl_specifiers
.storage_class
!= sc_static
)
23979 initializer
= cp_parser_save_nsdmi (parser
);
23980 else if (cxx_dialect
>= cxx11
)
23983 /* Don't require a constant rvalue in C++11, since we
23984 might want a reference constant. We'll enforce
23985 constancy later. */
23986 cp_lexer_consume_token (parser
->lexer
);
23987 /* Parse the initializer. */
23988 initializer
= cp_parser_initializer_clause (parser
,
23992 /* Parse the initializer. */
23993 initializer
= cp_parser_constant_initializer (parser
);
23995 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23996 && !function_declarator_p (declarator
))
23999 if (decl_specifiers
.storage_class
!= sc_static
)
24000 initializer
= cp_parser_save_nsdmi (parser
);
24002 initializer
= cp_parser_initializer (parser
, &x
, &x
);
24004 /* Otherwise, there is no initializer. */
24006 initializer
= NULL_TREE
;
24008 /* See if we are probably looking at a function
24009 definition. We are certainly not looking at a
24010 member-declarator. Calling `grokfield' has
24011 side-effects, so we must not do it unless we are sure
24012 that we are looking at a member-declarator. */
24013 if (cp_parser_token_starts_function_definition_p
24014 (cp_lexer_peek_token (parser
->lexer
)))
24016 /* The grammar does not allow a pure-specifier to be
24017 used when a member function is defined. (It is
24018 possible that this fact is an oversight in the
24019 standard, since a pure function may be defined
24020 outside of the class-specifier. */
24021 if (initializer
&& initializer_token_start
)
24022 error_at (initializer_token_start
->location
,
24023 "pure-specifier on function-definition");
24024 decl
= cp_parser_save_member_function_body (parser
,
24028 if (parser
->fully_implicit_function_template_p
)
24029 decl
= finish_fully_implicit_template (parser
, decl
);
24030 /* If the member was not a friend, declare it here. */
24032 finish_member_declaration (decl
);
24033 /* Peek at the next token. */
24034 token
= cp_lexer_peek_token (parser
->lexer
);
24035 /* If the next token is a semicolon, consume it. */
24036 if (token
->type
== CPP_SEMICOLON
)
24038 location_t semicolon_loc
24039 = cp_lexer_consume_token (parser
->lexer
)->location
;
24040 gcc_rich_location
richloc (semicolon_loc
);
24041 richloc
.add_fixit_remove ();
24042 warning_at (&richloc
, OPT_Wextra_semi
,
24043 "extra %<;%> after in-class "
24044 "function definition");
24049 if (declarator
->kind
== cdk_function
)
24050 declarator
->id_loc
= token
->location
;
24051 /* Create the declaration. */
24052 decl
= grokfield (declarator
, &decl_specifiers
,
24053 initializer
, /*init_const_expr_p=*/true,
24054 asm_specification
, attributes
);
24055 if (parser
->fully_implicit_function_template_p
)
24058 finish_fully_implicit_template (parser
, 0);
24060 decl
= finish_fully_implicit_template (parser
, decl
);
24064 cp_finalize_omp_declare_simd (parser
, decl
);
24065 cp_finalize_oacc_routine (parser
, decl
, false);
24067 /* Reset PREFIX_ATTRIBUTES. */
24068 if (attributes
!= error_mark_node
)
24070 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24071 attributes
= TREE_CHAIN (attributes
);
24073 TREE_CHAIN (attributes
) = NULL_TREE
;
24076 /* If there is any qualification still in effect, clear it
24077 now; we will be starting fresh with the next declarator. */
24078 parser
->scope
= NULL_TREE
;
24079 parser
->qualifying_scope
= NULL_TREE
;
24080 parser
->object_scope
= NULL_TREE
;
24081 /* If it's a `,', then there are more declarators. */
24082 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24084 cp_lexer_consume_token (parser
->lexer
);
24085 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24087 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24088 gcc_rich_location
richloc (token
->location
);
24089 richloc
.add_fixit_remove ();
24090 error_at (&richloc
, "stray %<,%> at end of "
24091 "member declaration");
24094 /* If the next token isn't a `;', then we have a parse error. */
24095 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24098 /* The next token might be a ways away from where the
24099 actual semicolon is missing. Find the previous token
24100 and use that for our error position. */
24101 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24102 gcc_rich_location
richloc (token
->location
);
24103 richloc
.add_fixit_insert_after (";");
24104 error_at (&richloc
, "expected %<;%> at end of "
24105 "member declaration");
24107 /* Assume that the user meant to provide a semicolon. If
24108 we were to cp_parser_skip_to_end_of_statement, we might
24109 skip to a semicolon inside a member function definition
24110 and issue nonsensical error messages. */
24111 assume_semicolon
= true;
24116 /* Add DECL to the list of members. */
24118 /* Explicitly include, eg, NSDMIs, for better error
24119 recovery (c++/58650). */
24120 || !DECL_DECLARES_FUNCTION_P (decl
))
24121 finish_member_declaration (decl
);
24123 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24124 cp_parser_save_default_args (parser
, decl
);
24125 else if (TREE_CODE (decl
) == FIELD_DECL
24126 && DECL_INITIAL (decl
))
24127 /* Add DECL to the queue of NSDMI to be parsed later. */
24128 vec_safe_push (unparsed_nsdmis
, decl
);
24131 if (assume_semicolon
)
24136 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24138 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24141 /* Parse a pure-specifier.
24146 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24147 Otherwise, ERROR_MARK_NODE is returned. */
24150 cp_parser_pure_specifier (cp_parser
* parser
)
24154 /* Look for the `=' token. */
24155 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24156 return error_mark_node
;
24157 /* Look for the `0' token. */
24158 token
= cp_lexer_peek_token (parser
->lexer
);
24160 if (token
->type
== CPP_EOF
24161 || token
->type
== CPP_PRAGMA_EOL
)
24162 return error_mark_node
;
24164 cp_lexer_consume_token (parser
->lexer
);
24166 /* Accept = default or = delete in c++0x mode. */
24167 if (token
->keyword
== RID_DEFAULT
24168 || token
->keyword
== RID_DELETE
)
24170 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24171 return token
->u
.value
;
24174 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24175 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24177 cp_parser_error (parser
,
24178 "invalid pure specifier (only %<= 0%> is allowed)");
24179 cp_parser_skip_to_end_of_statement (parser
);
24180 return error_mark_node
;
24182 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24184 error_at (token
->location
, "templates may not be %<virtual%>");
24185 return error_mark_node
;
24188 return integer_zero_node
;
24191 /* Parse a constant-initializer.
24193 constant-initializer:
24194 = constant-expression
24196 Returns a representation of the constant-expression. */
24199 cp_parser_constant_initializer (cp_parser
* parser
)
24201 /* Look for the `=' token. */
24202 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24203 return error_mark_node
;
24205 /* It is invalid to write:
24207 struct S { static const int i = { 7 }; };
24210 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24212 cp_parser_error (parser
,
24213 "a brace-enclosed initializer is not allowed here");
24214 /* Consume the opening brace. */
24215 matching_braces braces
;
24216 braces
.consume_open (parser
);
24217 /* Skip the initializer. */
24218 cp_parser_skip_to_closing_brace (parser
);
24219 /* Look for the trailing `}'. */
24220 braces
.require_close (parser
);
24222 return error_mark_node
;
24225 return cp_parser_constant_expression (parser
);
24228 /* Derived classes [gram.class.derived] */
24230 /* Parse a base-clause.
24233 : base-specifier-list
24235 base-specifier-list:
24236 base-specifier ... [opt]
24237 base-specifier-list , base-specifier ... [opt]
24239 Returns a TREE_LIST representing the base-classes, in the order in
24240 which they were declared. The representation of each node is as
24241 described by cp_parser_base_specifier.
24243 In the case that no bases are specified, this function will return
24244 NULL_TREE, not ERROR_MARK_NODE. */
24247 cp_parser_base_clause (cp_parser
* parser
)
24249 tree bases
= NULL_TREE
;
24251 /* Look for the `:' that begins the list. */
24252 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24254 /* Scan the base-specifier-list. */
24259 bool pack_expansion_p
= false;
24261 /* Look for the base-specifier. */
24262 base
= cp_parser_base_specifier (parser
);
24263 /* Look for the (optional) ellipsis. */
24264 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24266 /* Consume the `...'. */
24267 cp_lexer_consume_token (parser
->lexer
);
24269 pack_expansion_p
= true;
24272 /* Add BASE to the front of the list. */
24273 if (base
&& base
!= error_mark_node
)
24275 if (pack_expansion_p
)
24276 /* Make this a pack expansion type. */
24277 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24279 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24281 TREE_CHAIN (base
) = bases
;
24285 /* Peek at the next token. */
24286 token
= cp_lexer_peek_token (parser
->lexer
);
24287 /* If it's not a comma, then the list is complete. */
24288 if (token
->type
!= CPP_COMMA
)
24290 /* Consume the `,'. */
24291 cp_lexer_consume_token (parser
->lexer
);
24294 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24295 base class had a qualified name. However, the next name that
24296 appears is certainly not qualified. */
24297 parser
->scope
= NULL_TREE
;
24298 parser
->qualifying_scope
= NULL_TREE
;
24299 parser
->object_scope
= NULL_TREE
;
24301 return nreverse (bases
);
24304 /* Parse a base-specifier.
24307 :: [opt] nested-name-specifier [opt] class-name
24308 virtual access-specifier [opt] :: [opt] nested-name-specifier
24310 access-specifier virtual [opt] :: [opt] nested-name-specifier
24313 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24314 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24315 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24316 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24319 cp_parser_base_specifier (cp_parser
* parser
)
24323 bool virtual_p
= false;
24324 bool duplicate_virtual_error_issued_p
= false;
24325 bool duplicate_access_error_issued_p
= false;
24326 bool class_scope_p
, template_p
;
24327 tree access
= access_default_node
;
24330 /* Process the optional `virtual' and `access-specifier'. */
24333 /* Peek at the next token. */
24334 token
= cp_lexer_peek_token (parser
->lexer
);
24335 /* Process `virtual'. */
24336 switch (token
->keyword
)
24339 /* If `virtual' appears more than once, issue an error. */
24340 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24342 cp_parser_error (parser
,
24343 "%<virtual%> specified more than once in base-specifier");
24344 duplicate_virtual_error_issued_p
= true;
24349 /* Consume the `virtual' token. */
24350 cp_lexer_consume_token (parser
->lexer
);
24355 case RID_PROTECTED
:
24357 /* If more than one access specifier appears, issue an
24359 if (access
!= access_default_node
24360 && !duplicate_access_error_issued_p
)
24362 cp_parser_error (parser
,
24363 "more than one access specifier in base-specifier");
24364 duplicate_access_error_issued_p
= true;
24367 access
= ridpointers
[(int) token
->keyword
];
24369 /* Consume the access-specifier. */
24370 cp_lexer_consume_token (parser
->lexer
);
24379 /* It is not uncommon to see programs mechanically, erroneously, use
24380 the 'typename' keyword to denote (dependent) qualified types
24381 as base classes. */
24382 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24384 token
= cp_lexer_peek_token (parser
->lexer
);
24385 if (!processing_template_decl
)
24386 error_at (token
->location
,
24387 "keyword %<typename%> not allowed outside of templates");
24389 error_at (token
->location
,
24390 "keyword %<typename%> not allowed in this context "
24391 "(the base class is implicitly a type)");
24392 cp_lexer_consume_token (parser
->lexer
);
24395 /* Look for the optional `::' operator. */
24396 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24397 /* Look for the nested-name-specifier. The simplest way to
24402 The keyword `typename' is not permitted in a base-specifier or
24403 mem-initializer; in these contexts a qualified name that
24404 depends on a template-parameter is implicitly assumed to be a
24407 is to pretend that we have seen the `typename' keyword at this
24409 cp_parser_nested_name_specifier_opt (parser
,
24410 /*typename_keyword_p=*/true,
24411 /*check_dependency_p=*/true,
24413 /*is_declaration=*/true);
24414 /* If the base class is given by a qualified name, assume that names
24415 we see are type names or templates, as appropriate. */
24416 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24417 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24420 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24421 /* DR 950 allows decltype as a base-specifier. */
24422 type
= cp_parser_decltype (parser
);
24425 /* Otherwise, look for the class-name. */
24426 type
= cp_parser_class_name (parser
,
24430 /*check_dependency_p=*/true,
24431 /*class_head_p=*/false,
24432 /*is_declaration=*/true);
24433 type
= TREE_TYPE (type
);
24436 if (type
== error_mark_node
)
24437 return error_mark_node
;
24439 return finish_base_specifier (type
, access
, virtual_p
);
24442 /* Exception handling [gram.exception] */
24444 /* Parse an (optional) noexcept-specification.
24446 noexcept-specification:
24447 noexcept ( constant-expression ) [opt]
24449 If no noexcept-specification is present, returns NULL_TREE.
24450 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24451 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24452 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24453 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24454 in which case a boolean condition is returned instead. */
24457 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24458 bool require_constexpr
,
24459 bool* consumed_expr
,
24463 const char *saved_message
;
24465 /* Peek at the next token. */
24466 token
= cp_lexer_peek_token (parser
->lexer
);
24468 /* Is it a noexcept-specification? */
24469 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24472 cp_lexer_consume_token (parser
->lexer
);
24474 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24476 matching_parens parens
;
24477 parens
.consume_open (parser
);
24479 if (require_constexpr
)
24481 /* Types may not be defined in an exception-specification. */
24482 saved_message
= parser
->type_definition_forbidden_message
;
24483 parser
->type_definition_forbidden_message
24484 = G_("types may not be defined in an exception-specification");
24486 expr
= cp_parser_constant_expression (parser
);
24488 /* Restore the saved message. */
24489 parser
->type_definition_forbidden_message
= saved_message
;
24493 expr
= cp_parser_expression (parser
);
24494 *consumed_expr
= true;
24497 parens
.require_close (parser
);
24501 expr
= boolean_true_node
;
24502 if (!require_constexpr
)
24503 *consumed_expr
= false;
24506 /* We cannot build a noexcept-spec right away because this will check
24507 that expr is a constexpr. */
24509 return build_noexcept_spec (expr
, tf_warning_or_error
);
24517 /* Parse an (optional) exception-specification.
24519 exception-specification:
24520 throw ( type-id-list [opt] )
24522 Returns a TREE_LIST representing the exception-specification. The
24523 TREE_VALUE of each node is a type. */
24526 cp_parser_exception_specification_opt (cp_parser
* parser
)
24530 const char *saved_message
;
24532 /* Peek at the next token. */
24533 token
= cp_lexer_peek_token (parser
->lexer
);
24535 /* Is it a noexcept-specification? */
24536 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24538 if (type_id_list
!= NULL_TREE
)
24539 return type_id_list
;
24541 /* If it's not `throw', then there's no exception-specification. */
24542 if (!cp_parser_is_keyword (token
, RID_THROW
))
24545 location_t loc
= token
->location
;
24547 /* Consume the `throw'. */
24548 cp_lexer_consume_token (parser
->lexer
);
24550 /* Look for the `('. */
24551 matching_parens parens
;
24552 parens
.require_open (parser
);
24554 /* Peek at the next token. */
24555 token
= cp_lexer_peek_token (parser
->lexer
);
24556 /* If it's not a `)', then there is a type-id-list. */
24557 if (token
->type
!= CPP_CLOSE_PAREN
)
24559 /* Types may not be defined in an exception-specification. */
24560 saved_message
= parser
->type_definition_forbidden_message
;
24561 parser
->type_definition_forbidden_message
24562 = G_("types may not be defined in an exception-specification");
24563 /* Parse the type-id-list. */
24564 type_id_list
= cp_parser_type_id_list (parser
);
24565 /* Restore the saved message. */
24566 parser
->type_definition_forbidden_message
= saved_message
;
24568 if (cxx_dialect
>= cxx17
)
24570 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24572 type_id_list
= NULL_TREE
;
24574 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24575 warning_at (loc
, OPT_Wdeprecated
,
24576 "dynamic exception specifications are deprecated in "
24579 /* In C++17, throw() is equivalent to noexcept (true). throw()
24580 is deprecated in C++11 and above as well, but is still widely used,
24581 so don't warn about it yet. */
24582 else if (cxx_dialect
>= cxx17
)
24583 type_id_list
= noexcept_true_spec
;
24585 type_id_list
= empty_except_spec
;
24587 /* Look for the `)'. */
24588 parens
.require_close (parser
);
24590 return type_id_list
;
24593 /* Parse an (optional) type-id-list.
24597 type-id-list , type-id ... [opt]
24599 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24600 in the order that the types were presented. */
24603 cp_parser_type_id_list (cp_parser
* parser
)
24605 tree types
= NULL_TREE
;
24612 token
= cp_lexer_peek_token (parser
->lexer
);
24614 /* Get the next type-id. */
24615 type
= cp_parser_type_id (parser
);
24616 /* Check for invalid 'auto'. */
24617 if (flag_concepts
&& type_uses_auto (type
))
24619 error_at (token
->location
,
24620 "invalid use of %<auto%> in exception-specification");
24621 type
= error_mark_node
;
24623 /* Parse the optional ellipsis. */
24624 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24626 /* Consume the `...'. */
24627 cp_lexer_consume_token (parser
->lexer
);
24629 /* Turn the type into a pack expansion expression. */
24630 type
= make_pack_expansion (type
);
24632 /* Add it to the list. */
24633 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24634 /* Peek at the next token. */
24635 token
= cp_lexer_peek_token (parser
->lexer
);
24636 /* If it is not a `,', we are done. */
24637 if (token
->type
!= CPP_COMMA
)
24639 /* Consume the `,'. */
24640 cp_lexer_consume_token (parser
->lexer
);
24643 return nreverse (types
);
24646 /* Parse a try-block.
24649 try compound-statement handler-seq */
24652 cp_parser_try_block (cp_parser
* parser
)
24656 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24657 if (parser
->in_function_body
24658 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24659 error ("%<try%> in %<constexpr%> function");
24661 try_block
= begin_try_block ();
24662 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24663 finish_try_block (try_block
);
24664 cp_parser_handler_seq (parser
);
24665 finish_handler_sequence (try_block
);
24670 /* Parse a function-try-block.
24672 function-try-block:
24673 try ctor-initializer [opt] function-body handler-seq */
24676 cp_parser_function_try_block (cp_parser
* parser
)
24678 tree compound_stmt
;
24681 /* Look for the `try' keyword. */
24682 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24684 /* Let the rest of the front end know where we are. */
24685 try_block
= begin_function_try_block (&compound_stmt
);
24686 /* Parse the function-body. */
24687 cp_parser_ctor_initializer_opt_and_function_body
24688 (parser
, /*in_function_try_block=*/true);
24689 /* We're done with the `try' part. */
24690 finish_function_try_block (try_block
);
24691 /* Parse the handlers. */
24692 cp_parser_handler_seq (parser
);
24693 /* We're done with the handlers. */
24694 finish_function_handler_sequence (try_block
, compound_stmt
);
24697 /* Parse a handler-seq.
24700 handler handler-seq [opt] */
24703 cp_parser_handler_seq (cp_parser
* parser
)
24709 /* Parse the handler. */
24710 cp_parser_handler (parser
);
24711 /* Peek at the next token. */
24712 token
= cp_lexer_peek_token (parser
->lexer
);
24713 /* If it's not `catch' then there are no more handlers. */
24714 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24719 /* Parse a handler.
24722 catch ( exception-declaration ) compound-statement */
24725 cp_parser_handler (cp_parser
* parser
)
24730 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24731 handler
= begin_handler ();
24732 matching_parens parens
;
24733 parens
.require_open (parser
);
24734 declaration
= cp_parser_exception_declaration (parser
);
24735 finish_handler_parms (declaration
, handler
);
24736 parens
.require_close (parser
);
24737 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24738 finish_handler (handler
);
24741 /* Parse an exception-declaration.
24743 exception-declaration:
24744 type-specifier-seq declarator
24745 type-specifier-seq abstract-declarator
24749 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24750 ellipsis variant is used. */
24753 cp_parser_exception_declaration (cp_parser
* parser
)
24755 cp_decl_specifier_seq type_specifiers
;
24756 cp_declarator
*declarator
;
24757 const char *saved_message
;
24759 /* If it's an ellipsis, it's easy to handle. */
24760 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24762 /* Consume the `...' token. */
24763 cp_lexer_consume_token (parser
->lexer
);
24767 /* Types may not be defined in exception-declarations. */
24768 saved_message
= parser
->type_definition_forbidden_message
;
24769 parser
->type_definition_forbidden_message
24770 = G_("types may not be defined in exception-declarations");
24772 /* Parse the type-specifier-seq. */
24773 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24774 /*is_trailing_return=*/false,
24776 /* If it's a `)', then there is no declarator. */
24777 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24780 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24781 /*ctor_dtor_or_conv_p=*/NULL
,
24782 /*parenthesized_p=*/NULL
,
24783 /*member_p=*/false,
24784 /*friend_p=*/false);
24786 /* Restore the saved message. */
24787 parser
->type_definition_forbidden_message
= saved_message
;
24789 if (!type_specifiers
.any_specifiers_p
)
24790 return error_mark_node
;
24792 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24795 /* Parse a throw-expression.
24798 throw assignment-expression [opt]
24800 Returns a THROW_EXPR representing the throw-expression. */
24803 cp_parser_throw_expression (cp_parser
* parser
)
24808 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24809 token
= cp_lexer_peek_token (parser
->lexer
);
24810 /* Figure out whether or not there is an assignment-expression
24811 following the "throw" keyword. */
24812 if (token
->type
== CPP_COMMA
24813 || token
->type
== CPP_SEMICOLON
24814 || token
->type
== CPP_CLOSE_PAREN
24815 || token
->type
== CPP_CLOSE_SQUARE
24816 || token
->type
== CPP_CLOSE_BRACE
24817 || token
->type
== CPP_COLON
)
24818 expression
= NULL_TREE
;
24820 expression
= cp_parser_assignment_expression (parser
);
24822 return build_throw (expression
);
24825 /* GNU Extensions */
24827 /* Parse an (optional) asm-specification.
24830 asm ( string-literal )
24832 If the asm-specification is present, returns a STRING_CST
24833 corresponding to the string-literal. Otherwise, returns
24837 cp_parser_asm_specification_opt (cp_parser
* parser
)
24840 tree asm_specification
;
24842 /* Peek at the next token. */
24843 token
= cp_lexer_peek_token (parser
->lexer
);
24844 /* If the next token isn't the `asm' keyword, then there's no
24845 asm-specification. */
24846 if (!cp_parser_is_keyword (token
, RID_ASM
))
24849 /* Consume the `asm' token. */
24850 cp_lexer_consume_token (parser
->lexer
);
24851 /* Look for the `('. */
24852 matching_parens parens
;
24853 parens
.require_open (parser
);
24855 /* Look for the string-literal. */
24856 asm_specification
= cp_parser_string_literal (parser
, false, false);
24858 /* Look for the `)'. */
24859 parens
.require_close (parser
);
24861 return asm_specification
;
24864 /* Parse an asm-operand-list.
24868 asm-operand-list , asm-operand
24871 string-literal ( expression )
24872 [ string-literal ] string-literal ( expression )
24874 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24875 each node is the expression. The TREE_PURPOSE is itself a
24876 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24877 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24878 is a STRING_CST for the string literal before the parenthesis. Returns
24879 ERROR_MARK_NODE if any of the operands are invalid. */
24882 cp_parser_asm_operand_list (cp_parser
* parser
)
24884 tree asm_operands
= NULL_TREE
;
24885 bool invalid_operands
= false;
24889 tree string_literal
;
24893 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24895 /* Consume the `[' token. */
24896 cp_lexer_consume_token (parser
->lexer
);
24897 /* Read the operand name. */
24898 name
= cp_parser_identifier (parser
);
24899 if (name
!= error_mark_node
)
24900 name
= build_string (IDENTIFIER_LENGTH (name
),
24901 IDENTIFIER_POINTER (name
));
24902 /* Look for the closing `]'. */
24903 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24907 /* Look for the string-literal. */
24908 string_literal
= cp_parser_string_literal (parser
, false, false);
24910 /* Look for the `('. */
24911 matching_parens parens
;
24912 parens
.require_open (parser
);
24913 /* Parse the expression. */
24914 expression
= cp_parser_expression (parser
);
24915 /* Look for the `)'. */
24916 parens
.require_close (parser
);
24918 if (name
== error_mark_node
24919 || string_literal
== error_mark_node
24920 || expression
== error_mark_node
)
24921 invalid_operands
= true;
24923 /* Add this operand to the list. */
24924 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24927 /* If the next token is not a `,', there are no more
24929 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24931 /* Consume the `,'. */
24932 cp_lexer_consume_token (parser
->lexer
);
24935 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24938 /* Parse an asm-clobber-list.
24942 asm-clobber-list , string-literal
24944 Returns a TREE_LIST, indicating the clobbers in the order that they
24945 appeared. The TREE_VALUE of each node is a STRING_CST. */
24948 cp_parser_asm_clobber_list (cp_parser
* parser
)
24950 tree clobbers
= NULL_TREE
;
24954 tree string_literal
;
24956 /* Look for the string literal. */
24957 string_literal
= cp_parser_string_literal (parser
, false, false);
24958 /* Add it to the list. */
24959 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24960 /* If the next token is not a `,', then the list is
24962 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24964 /* Consume the `,' token. */
24965 cp_lexer_consume_token (parser
->lexer
);
24971 /* Parse an asm-label-list.
24975 asm-label-list , identifier
24977 Returns a TREE_LIST, indicating the labels in the order that they
24978 appeared. The TREE_VALUE of each node is a label. */
24981 cp_parser_asm_label_list (cp_parser
* parser
)
24983 tree labels
= NULL_TREE
;
24987 tree identifier
, label
, name
;
24989 /* Look for the identifier. */
24990 identifier
= cp_parser_identifier (parser
);
24991 if (!error_operand_p (identifier
))
24993 label
= lookup_label (identifier
);
24994 if (TREE_CODE (label
) == LABEL_DECL
)
24996 TREE_USED (label
) = 1;
24997 check_goto (label
);
24998 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24999 IDENTIFIER_POINTER (identifier
));
25000 labels
= tree_cons (name
, label
, labels
);
25003 /* If the next token is not a `,', then the list is
25005 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
25007 /* Consume the `,' token. */
25008 cp_lexer_consume_token (parser
->lexer
);
25011 return nreverse (labels
);
25014 /* Return TRUE iff the next tokens in the stream are possibly the
25015 beginning of a GNU extension attribute. */
25018 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25020 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25023 /* Return TRUE iff the next tokens in the stream are possibly the
25024 beginning of a standard C++-11 attribute specifier. */
25027 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25029 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25032 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25033 beginning of a standard C++-11 attribute specifier. */
25036 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25038 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25040 return (cxx_dialect
>= cxx11
25041 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25042 || (token
->type
== CPP_OPEN_SQUARE
25043 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25044 && token
->type
== CPP_OPEN_SQUARE
)));
25047 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25048 beginning of a GNU extension attribute. */
25051 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25053 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25055 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25058 /* Return true iff the next tokens can be the beginning of either a
25059 GNU attribute list, or a standard C++11 attribute sequence. */
25062 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25064 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25065 || cp_next_tokens_can_be_std_attribute_p (parser
));
25068 /* Return true iff the next Nth tokens can be the beginning of either
25069 a GNU attribute list, or a standard C++11 attribute sequence. */
25072 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25074 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25075 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25078 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25079 of GNU attributes, or return NULL. */
25082 cp_parser_attributes_opt (cp_parser
*parser
)
25084 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25085 return cp_parser_gnu_attributes_opt (parser
);
25086 return cp_parser_std_attribute_spec_seq (parser
);
25089 /* Parse an (optional) series of attributes.
25092 attributes attribute
25095 __attribute__ (( attribute-list [opt] ))
25097 The return value is as for cp_parser_gnu_attribute_list. */
25100 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25102 tree attributes
= NULL_TREE
;
25104 temp_override
<bool> cleanup
25105 (parser
->auto_is_implicit_function_template_parm_p
, false);
25110 tree attribute_list
;
25113 /* Peek at the next token. */
25114 token
= cp_lexer_peek_token (parser
->lexer
);
25115 /* If it's not `__attribute__', then we're done. */
25116 if (token
->keyword
!= RID_ATTRIBUTE
)
25119 /* Consume the `__attribute__' keyword. */
25120 cp_lexer_consume_token (parser
->lexer
);
25121 /* Look for the two `(' tokens. */
25122 matching_parens outer_parens
;
25123 outer_parens
.require_open (parser
);
25124 matching_parens inner_parens
;
25125 inner_parens
.require_open (parser
);
25127 /* Peek at the next token. */
25128 token
= cp_lexer_peek_token (parser
->lexer
);
25129 if (token
->type
!= CPP_CLOSE_PAREN
)
25130 /* Parse the attribute-list. */
25131 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25133 /* If the next token is a `)', then there is no attribute
25135 attribute_list
= NULL
;
25137 /* Look for the two `)' tokens. */
25138 if (!inner_parens
.require_close (parser
))
25140 if (!outer_parens
.require_close (parser
))
25143 cp_parser_skip_to_end_of_statement (parser
);
25145 /* Add these new attributes to the list. */
25146 attributes
= attr_chainon (attributes
, attribute_list
);
25152 /* Parse a GNU attribute-list.
25156 attribute-list , attribute
25160 identifier ( identifier )
25161 identifier ( identifier , expression-list )
25162 identifier ( expression-list )
25164 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25165 to an attribute. The TREE_PURPOSE of each node is the identifier
25166 indicating which attribute is in use. The TREE_VALUE represents
25167 the arguments, if any. */
25170 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25172 tree attribute_list
= NULL_TREE
;
25173 bool save_translate_strings_p
= parser
->translate_strings_p
;
25175 parser
->translate_strings_p
= false;
25182 /* Look for the identifier. We also allow keywords here; for
25183 example `__attribute__ ((const))' is legal. */
25184 token
= cp_lexer_peek_token (parser
->lexer
);
25185 if (token
->type
== CPP_NAME
25186 || token
->type
== CPP_KEYWORD
)
25188 tree arguments
= NULL_TREE
;
25190 /* Consume the token, but save it since we need it for the
25191 SIMD enabled function parsing. */
25192 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25194 /* Save away the identifier that indicates which attribute
25196 identifier
= (token
->type
== CPP_KEYWORD
)
25197 /* For keywords, use the canonical spelling, not the
25198 parsed identifier. */
25199 ? ridpointers
[(int) token
->keyword
]
25200 : id_token
->u
.value
;
25202 identifier
= canonicalize_attr_name (identifier
);
25203 attribute
= build_tree_list (identifier
, NULL_TREE
);
25205 /* Peek at the next token. */
25206 token
= cp_lexer_peek_token (parser
->lexer
);
25207 /* If it's an `(', then parse the attribute arguments. */
25208 if (token
->type
== CPP_OPEN_PAREN
)
25210 vec
<tree
, va_gc
> *vec
;
25211 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25212 ? id_attr
: normal_attr
);
25213 vec
= cp_parser_parenthesized_expression_list
25214 (parser
, attr_flag
, /*cast_p=*/false,
25215 /*allow_expansion_p=*/false,
25216 /*non_constant_p=*/NULL
);
25218 arguments
= error_mark_node
;
25221 arguments
= build_tree_list_vec (vec
);
25222 release_tree_vector (vec
);
25224 /* Save the arguments away. */
25225 TREE_VALUE (attribute
) = arguments
;
25228 if (arguments
!= error_mark_node
)
25230 /* Add this attribute to the list. */
25231 TREE_CHAIN (attribute
) = attribute_list
;
25232 attribute_list
= attribute
;
25235 token
= cp_lexer_peek_token (parser
->lexer
);
25237 /* Now, look for more attributes. If the next token isn't a
25238 `,', we're done. */
25239 if (token
->type
!= CPP_COMMA
)
25242 /* Consume the comma and keep going. */
25243 cp_lexer_consume_token (parser
->lexer
);
25245 parser
->translate_strings_p
= save_translate_strings_p
;
25247 /* We built up the list in reverse order. */
25248 return nreverse (attribute_list
);
25251 /* Parse a standard C++11 attribute.
25253 The returned representation is a TREE_LIST which TREE_PURPOSE is
25254 the scoped name of the attribute, and the TREE_VALUE is its
25257 Note that the scoped name of the attribute is itself a TREE_LIST
25258 which TREE_PURPOSE is the namespace of the attribute, and
25259 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25260 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25261 and which TREE_PURPOSE is directly the attribute name.
25263 Clients of the attribute code should use get_attribute_namespace
25264 and get_attribute_name to get the actual namespace and name of
25265 attributes, regardless of their being GNU or C++11 attributes.
25268 attribute-token attribute-argument-clause [opt]
25272 attribute-scoped-token
25274 attribute-scoped-token:
25275 attribute-namespace :: identifier
25277 attribute-namespace:
25280 attribute-argument-clause:
25281 ( balanced-token-seq )
25283 balanced-token-seq:
25284 balanced-token [opt]
25285 balanced-token-seq balanced-token
25288 ( balanced-token-seq )
25289 [ balanced-token-seq ]
25290 { balanced-token-seq }. */
25293 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25295 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25298 temp_override
<bool> cleanup
25299 (parser
->auto_is_implicit_function_template_parm_p
, false);
25301 /* First, parse name of the attribute, a.k.a attribute-token. */
25303 token
= cp_lexer_peek_token (parser
->lexer
);
25304 if (token
->type
== CPP_NAME
)
25305 attr_id
= token
->u
.value
;
25306 else if (token
->type
== CPP_KEYWORD
)
25307 attr_id
= ridpointers
[(int) token
->keyword
];
25308 else if (token
->flags
& NAMED_OP
)
25309 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25311 if (attr_id
== NULL_TREE
)
25314 cp_lexer_consume_token (parser
->lexer
);
25316 token
= cp_lexer_peek_token (parser
->lexer
);
25317 if (token
->type
== CPP_SCOPE
)
25319 /* We are seeing a scoped attribute token. */
25321 cp_lexer_consume_token (parser
->lexer
);
25323 error_at (token
->location
, "attribute using prefix used together "
25324 "with scoped attribute token");
25327 token
= cp_lexer_consume_token (parser
->lexer
);
25328 if (token
->type
== CPP_NAME
)
25329 attr_id
= token
->u
.value
;
25330 else if (token
->type
== CPP_KEYWORD
)
25331 attr_id
= ridpointers
[(int) token
->keyword
];
25332 else if (token
->flags
& NAMED_OP
)
25333 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25336 error_at (token
->location
,
25337 "expected an identifier for the attribute name");
25338 return error_mark_node
;
25341 attr_id
= canonicalize_attr_name (attr_id
);
25342 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25344 token
= cp_lexer_peek_token (parser
->lexer
);
25347 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25351 attr_id
= canonicalize_attr_name (attr_id
);
25352 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25354 /* C++11 noreturn attribute is equivalent to GNU's. */
25355 if (is_attribute_p ("noreturn", attr_id
))
25356 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25357 /* C++14 deprecated attribute is equivalent to GNU's. */
25358 else if (is_attribute_p ("deprecated", attr_id
))
25359 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25360 /* C++17 fallthrough attribute is equivalent to GNU's. */
25361 else if (is_attribute_p ("fallthrough", attr_id
))
25362 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25363 /* Transactional Memory TS optimize_for_synchronized attribute is
25364 equivalent to GNU transaction_callable. */
25365 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25366 TREE_PURPOSE (attribute
)
25367 = get_identifier ("transaction_callable");
25368 /* Transactional Memory attributes are GNU attributes. */
25369 else if (tm_attr_to_mask (attr_id
))
25370 TREE_PURPOSE (attribute
) = attr_id
;
25373 /* Now parse the optional argument clause of the attribute. */
25375 if (token
->type
!= CPP_OPEN_PAREN
)
25379 vec
<tree
, va_gc
> *vec
;
25380 int attr_flag
= normal_attr
;
25382 if (attr_ns
== gnu_identifier
25383 && attribute_takes_identifier_p (attr_id
))
25384 /* A GNU attribute that takes an identifier in parameter. */
25385 attr_flag
= id_attr
;
25387 vec
= cp_parser_parenthesized_expression_list
25388 (parser
, attr_flag
, /*cast_p=*/false,
25389 /*allow_expansion_p=*/true,
25390 /*non_constant_p=*/NULL
);
25392 arguments
= error_mark_node
;
25395 arguments
= build_tree_list_vec (vec
);
25396 release_tree_vector (vec
);
25399 if (arguments
== error_mark_node
)
25400 attribute
= error_mark_node
;
25402 TREE_VALUE (attribute
) = arguments
;
25408 /* Check that the attribute ATTRIBUTE appears at most once in the
25409 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25410 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25411 isn't implemented yet in GCC. */
25414 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25418 tree name
= get_attribute_name (attribute
);
25419 if (is_attribute_p ("noreturn", name
)
25420 && lookup_attribute ("noreturn", attributes
))
25421 error ("attribute %<noreturn%> can appear at most once "
25422 "in an attribute-list");
25423 else if (is_attribute_p ("deprecated", name
)
25424 && lookup_attribute ("deprecated", attributes
))
25425 error ("attribute %<deprecated%> can appear at most once "
25426 "in an attribute-list");
25430 /* Parse a list of standard C++-11 attributes.
25434 attribute-list , attribute[opt]
25436 attribute-list , attribute ...
25440 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25442 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25443 cp_token
*token
= NULL
;
25447 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25448 if (attribute
== error_mark_node
)
25450 if (attribute
!= NULL_TREE
)
25452 cp_parser_check_std_attribute (attributes
, attribute
);
25453 TREE_CHAIN (attribute
) = attributes
;
25454 attributes
= attribute
;
25456 token
= cp_lexer_peek_token (parser
->lexer
);
25457 if (token
->type
== CPP_ELLIPSIS
)
25459 cp_lexer_consume_token (parser
->lexer
);
25460 if (attribute
== NULL_TREE
)
25461 error_at (token
->location
,
25462 "expected attribute before %<...%>");
25465 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25466 if (pack
== error_mark_node
)
25467 return error_mark_node
;
25468 TREE_VALUE (attribute
) = pack
;
25470 token
= cp_lexer_peek_token (parser
->lexer
);
25472 if (token
->type
!= CPP_COMMA
)
25474 cp_lexer_consume_token (parser
->lexer
);
25476 attributes
= nreverse (attributes
);
25480 /* Parse a standard C++-11 attribute specifier.
25482 attribute-specifier:
25483 [ [ attribute-using-prefix [opt] attribute-list ] ]
25484 alignment-specifier
25486 attribute-using-prefix:
25487 using attribute-namespace :
25489 alignment-specifier:
25490 alignas ( type-id ... [opt] )
25491 alignas ( alignment-expression ... [opt] ). */
25494 cp_parser_std_attribute_spec (cp_parser
*parser
)
25496 tree attributes
= NULL_TREE
;
25497 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25499 if (token
->type
== CPP_OPEN_SQUARE
25500 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25502 tree attr_ns
= NULL_TREE
;
25504 cp_lexer_consume_token (parser
->lexer
);
25505 cp_lexer_consume_token (parser
->lexer
);
25507 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25509 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25510 if (token
->type
== CPP_NAME
)
25511 attr_ns
= token
->u
.value
;
25512 else if (token
->type
== CPP_KEYWORD
)
25513 attr_ns
= ridpointers
[(int) token
->keyword
];
25514 else if (token
->flags
& NAMED_OP
)
25515 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25518 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25520 if (cxx_dialect
< cxx17
25521 && !in_system_header_at (input_location
))
25522 pedwarn (input_location
, 0,
25523 "attribute using prefix only available "
25524 "with -std=c++17 or -std=gnu++17");
25526 cp_lexer_consume_token (parser
->lexer
);
25527 cp_lexer_consume_token (parser
->lexer
);
25528 cp_lexer_consume_token (parser
->lexer
);
25531 attr_ns
= NULL_TREE
;
25534 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25536 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25537 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25538 cp_parser_skip_to_end_of_statement (parser
);
25540 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25541 when we are sure that we have actually parsed them. */
25542 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25548 /* Look for an alignment-specifier. */
25550 token
= cp_lexer_peek_token (parser
->lexer
);
25552 if (token
->type
!= CPP_KEYWORD
25553 || token
->keyword
!= RID_ALIGNAS
)
25556 cp_lexer_consume_token (parser
->lexer
);
25557 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25559 matching_parens parens
;
25560 if (!parens
.require_open (parser
))
25561 return error_mark_node
;
25563 cp_parser_parse_tentatively (parser
);
25564 alignas_expr
= cp_parser_type_id (parser
);
25566 if (!cp_parser_parse_definitely (parser
))
25568 alignas_expr
= cp_parser_assignment_expression (parser
);
25569 if (alignas_expr
== error_mark_node
)
25570 cp_parser_skip_to_end_of_statement (parser
);
25571 if (alignas_expr
== NULL_TREE
25572 || alignas_expr
== error_mark_node
)
25573 return alignas_expr
;
25576 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25577 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25579 /* Handle alignas (pack...). */
25580 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25582 cp_lexer_consume_token (parser
->lexer
);
25583 alignas_expr
= make_pack_expansion (alignas_expr
);
25586 /* Something went wrong, so don't build the attribute. */
25587 if (alignas_expr
== error_mark_node
)
25588 return error_mark_node
;
25590 if (!parens
.require_close (parser
))
25591 return error_mark_node
;
25593 /* Build the C++-11 representation of an 'aligned'
25596 = build_tree_list (build_tree_list (gnu_identifier
,
25597 aligned_identifier
), alignas_expr
);
25603 /* Parse a standard C++-11 attribute-specifier-seq.
25605 attribute-specifier-seq:
25606 attribute-specifier-seq [opt] attribute-specifier
25610 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25612 tree attr_specs
= NULL_TREE
;
25613 tree attr_last
= NULL_TREE
;
25617 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25618 if (attr_spec
== NULL_TREE
)
25620 if (attr_spec
== error_mark_node
)
25621 return error_mark_node
;
25624 TREE_CHAIN (attr_last
) = attr_spec
;
25626 attr_specs
= attr_last
= attr_spec
;
25627 attr_last
= tree_last (attr_last
);
25633 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25634 return index of the first token after balanced-token, or N on failure. */
25637 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25640 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25642 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25645 case CPP_PRAGMA_EOL
:
25646 /* Ran out of tokens. */
25648 case CPP_OPEN_PAREN
:
25651 case CPP_OPEN_BRACE
:
25654 case CPP_OPEN_SQUARE
:
25657 case CPP_CLOSE_PAREN
:
25660 case CPP_CLOSE_BRACE
:
25663 case CPP_CLOSE_SQUARE
:
25669 while (nparens
|| nbraces
|| nsquares
);
25673 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25674 return index of the first token after the GNU attribute tokens, or N on
25678 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25682 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25683 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25684 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25687 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25690 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25697 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25698 next token), return index of the first token after the standard C++11
25699 attribute tokens, or N on failure. */
25702 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25706 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25707 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25709 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25712 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25716 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25717 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25719 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25730 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25731 as the next token), return index of the first token after the attribute
25732 tokens, or N on failure. */
25735 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25737 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25738 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25739 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25742 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25743 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25744 current value of the PEDANTIC flag, regardless of whether or not
25745 the `__extension__' keyword is present. The caller is responsible
25746 for restoring the value of the PEDANTIC flag. */
25749 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25751 /* Save the old value of the PEDANTIC flag. */
25752 *saved_pedantic
= pedantic
;
25754 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25756 /* Consume the `__extension__' token. */
25757 cp_lexer_consume_token (parser
->lexer
);
25758 /* We're not being pedantic while the `__extension__' keyword is
25768 /* Parse a label declaration.
25771 __label__ label-declarator-seq ;
25773 label-declarator-seq:
25774 identifier , label-declarator-seq
25778 cp_parser_label_declaration (cp_parser
* parser
)
25780 /* Look for the `__label__' keyword. */
25781 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25787 /* Look for an identifier. */
25788 identifier
= cp_parser_identifier (parser
);
25789 /* If we failed, stop. */
25790 if (identifier
== error_mark_node
)
25792 /* Declare it as a label. */
25793 finish_label_decl (identifier
);
25794 /* If the next token is a `;', stop. */
25795 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25797 /* Look for the `,' separating the label declarations. */
25798 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25801 /* Look for the final `;'. */
25802 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25805 // -------------------------------------------------------------------------- //
25808 // Parse a requires clause.
25810 // requires-clause:
25811 // 'requires' logical-or-expression
25813 // The required logical-or-expression must be a constant expression. Note
25814 // that we don't check that the expression is constepxr here. We defer until
25815 // we analyze constraints and then, we only check atomic constraints.
25817 cp_parser_requires_clause (cp_parser
*parser
)
25819 // Parse the requires clause so that it is not automatically folded.
25820 ++processing_template_decl
;
25821 tree expr
= cp_parser_binary_expression (parser
, false, false,
25822 PREC_NOT_OPERATOR
, NULL
);
25823 if (check_for_bare_parameter_packs (expr
))
25824 expr
= error_mark_node
;
25825 --processing_template_decl
;
25829 // Optionally parse a requires clause:
25831 cp_parser_requires_clause_opt (cp_parser
*parser
)
25833 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25834 if (tok
->keyword
!= RID_REQUIRES
)
25836 if (!flag_concepts
&& tok
->type
== CPP_NAME
25837 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25839 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25840 "%<requires%> only available with -fconcepts");
25841 /* Parse and discard the requires-clause. */
25842 cp_lexer_consume_token (parser
->lexer
);
25843 cp_parser_requires_clause (parser
);
25847 cp_lexer_consume_token (parser
->lexer
);
25848 return cp_parser_requires_clause (parser
);
25852 /*---------------------------------------------------------------------------
25853 Requires expressions
25854 ---------------------------------------------------------------------------*/
25856 /* Parse a requires expression
25858 requirement-expression:
25859 'requires' requirement-parameter-list [opt] requirement-body */
25861 cp_parser_requires_expression (cp_parser
*parser
)
25863 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25864 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25866 /* A requires-expression shall appear only within a concept
25867 definition or a requires-clause.
25869 TODO: Implement this diagnostic correctly. */
25870 if (!processing_template_decl
)
25872 error_at (loc
, "a requires expression cannot appear outside a template");
25873 cp_parser_skip_to_end_of_statement (parser
);
25874 return error_mark_node
;
25879 /* Local parameters are delared as variables within the scope
25880 of the expression. They are not visible past the end of
25881 the expression. Expressions within the requires-expression
25882 are unevaluated. */
25883 struct scope_sentinel
25887 ++cp_unevaluated_operand
;
25888 begin_scope (sk_block
, NULL_TREE
);
25893 pop_bindings_and_leave_scope ();
25894 --cp_unevaluated_operand
;
25898 /* Parse the optional parameter list. */
25899 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25901 parms
= cp_parser_requirement_parameter_list (parser
);
25902 if (parms
== error_mark_node
)
25903 return error_mark_node
;
25908 /* Parse the requirement body. */
25909 reqs
= cp_parser_requirement_body (parser
);
25910 if (reqs
== error_mark_node
)
25911 return error_mark_node
;
25914 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25916 grokparms (parms
, &parms
);
25917 return finish_requires_expr (parms
, reqs
);
25920 /* Parse a parameterized requirement.
25922 requirement-parameter-list:
25923 '(' parameter-declaration-clause ')' */
25925 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25927 matching_parens parens
;
25928 if (!parens
.require_open (parser
))
25929 return error_mark_node
;
25931 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25933 if (!parens
.require_close (parser
))
25934 return error_mark_node
;
25939 /* Parse the body of a requirement.
25942 '{' requirement-list '}' */
25944 cp_parser_requirement_body (cp_parser
*parser
)
25946 matching_braces braces
;
25947 if (!braces
.require_open (parser
))
25948 return error_mark_node
;
25950 tree reqs
= cp_parser_requirement_list (parser
);
25952 if (!braces
.require_close (parser
))
25953 return error_mark_node
;
25958 /* Parse a list of requirements.
25962 requirement-list ';' requirement[opt] */
25964 cp_parser_requirement_list (cp_parser
*parser
)
25966 tree result
= NULL_TREE
;
25969 tree req
= cp_parser_requirement (parser
);
25970 if (req
== error_mark_node
)
25971 return error_mark_node
;
25973 result
= tree_cons (NULL_TREE
, req
, result
);
25975 /* If we see a semi-colon, consume it. */
25976 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25977 cp_lexer_consume_token (parser
->lexer
);
25979 /* Stop processing at the end of the list. */
25980 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25984 /* Reverse the order of requirements so they are analyzed in
25985 declaration order. */
25986 return nreverse (result
);
25989 /* Parse a syntactic requirement or type requirement.
25993 compound-requirement
25995 nested-requirement */
25997 cp_parser_requirement (cp_parser
*parser
)
25999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26000 return cp_parser_compound_requirement (parser
);
26001 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26002 return cp_parser_type_requirement (parser
);
26003 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
26004 return cp_parser_nested_requirement (parser
);
26006 return cp_parser_simple_requirement (parser
);
26009 /* Parse a simple requirement.
26011 simple-requirement:
26014 cp_parser_simple_requirement (cp_parser
*parser
)
26016 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26017 if (!expr
|| expr
== error_mark_node
)
26018 return error_mark_node
;
26020 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26021 return error_mark_node
;
26023 return finish_simple_requirement (expr
);
26026 /* Parse a type requirement
26029 nested-name-specifier [opt] required-type-name ';'
26031 required-type-name:
26033 'template' [opt] simple-template-id */
26035 cp_parser_type_requirement (cp_parser
*parser
)
26037 cp_lexer_consume_token (parser
->lexer
);
26039 // Save the scope before parsing name specifiers.
26040 tree saved_scope
= parser
->scope
;
26041 tree saved_object_scope
= parser
->object_scope
;
26042 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26043 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26044 cp_parser_nested_name_specifier_opt (parser
,
26045 /*typename_keyword_p=*/true,
26046 /*check_dependency_p=*/false,
26048 /*is_declaration=*/false);
26051 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26053 cp_lexer_consume_token (parser
->lexer
);
26054 type
= cp_parser_template_id (parser
,
26055 /*template_keyword_p=*/true,
26056 /*check_dependency=*/false,
26057 /*tag_type=*/none_type
,
26058 /*is_declaration=*/false);
26059 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26060 /*complain=*/tf_error
);
26063 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26065 if (TREE_CODE (type
) == TYPE_DECL
)
26066 type
= TREE_TYPE (type
);
26068 parser
->scope
= saved_scope
;
26069 parser
->object_scope
= saved_object_scope
;
26070 parser
->qualifying_scope
= saved_qualifying_scope
;
26072 if (type
== error_mark_node
)
26073 cp_parser_skip_to_end_of_statement (parser
);
26075 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26076 return error_mark_node
;
26077 if (type
== error_mark_node
)
26078 return error_mark_node
;
26080 return finish_type_requirement (type
);
26083 /* Parse a compound requirement
26085 compound-requirement:
26086 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26088 cp_parser_compound_requirement (cp_parser
*parser
)
26090 /* Parse an expression enclosed in '{ }'s. */
26091 matching_braces braces
;
26092 if (!braces
.require_open (parser
))
26093 return error_mark_node
;
26095 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26096 if (!expr
|| expr
== error_mark_node
)
26097 return error_mark_node
;
26099 if (!braces
.require_close (parser
))
26100 return error_mark_node
;
26102 /* Parse the optional noexcept. */
26103 bool noexcept_p
= false;
26104 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26106 cp_lexer_consume_token (parser
->lexer
);
26110 /* Parse the optional trailing return type. */
26111 tree type
= NULL_TREE
;
26112 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26114 cp_lexer_consume_token (parser
->lexer
);
26115 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26116 parser
->in_result_type_constraint_p
= true;
26117 type
= cp_parser_trailing_type_id (parser
);
26118 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26119 if (type
== error_mark_node
)
26120 return error_mark_node
;
26123 return finish_compound_requirement (expr
, type
, noexcept_p
);
26126 /* Parse a nested requirement. This is the same as a requires clause.
26128 nested-requirement:
26131 cp_parser_nested_requirement (cp_parser
*parser
)
26133 cp_lexer_consume_token (parser
->lexer
);
26134 tree req
= cp_parser_requires_clause (parser
);
26135 if (req
== error_mark_node
)
26136 return error_mark_node
;
26137 return finish_nested_requirement (req
);
26140 /* Support Functions */
26142 /* Return the appropriate prefer_type argument for lookup_name_real based on
26143 tag_type and template_mem_access. */
26146 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26148 /* DR 141: When looking in the current enclosing context for a template-name
26149 after -> or ., only consider class templates. */
26150 if (template_mem_access
)
26154 case none_type
: return 0; // No preference.
26155 case scope_type
: return 1; // Type or namespace.
26156 default: return 2; // Type only.
26160 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26161 NAME should have one of the representations used for an
26162 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26163 is returned. If PARSER->SCOPE is a dependent type, then a
26164 SCOPE_REF is returned.
26166 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26167 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26168 was formed. Abstractly, such entities should not be passed to this
26169 function, because they do not need to be looked up, but it is
26170 simpler to check for this special case here, rather than at the
26173 In cases not explicitly covered above, this function returns a
26174 DECL, OVERLOAD, or baselink representing the result of the lookup.
26175 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26178 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26179 (e.g., "struct") that was used. In that case bindings that do not
26180 refer to types are ignored.
26182 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26185 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26188 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26191 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26192 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26193 NULL_TREE otherwise. */
26196 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26197 enum tag_types tag_type
,
26200 bool check_dependency
,
26201 tree
*ambiguous_decls
,
26202 location_t name_location
)
26205 tree object_type
= parser
->context
->object_type
;
26207 /* Assume that the lookup will be unambiguous. */
26208 if (ambiguous_decls
)
26209 *ambiguous_decls
= NULL_TREE
;
26211 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26212 no longer valid. Note that if we are parsing tentatively, and
26213 the parse fails, OBJECT_TYPE will be automatically restored. */
26214 parser
->context
->object_type
= NULL_TREE
;
26216 if (name
== error_mark_node
)
26217 return error_mark_node
;
26219 /* A template-id has already been resolved; there is no lookup to
26221 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26223 if (BASELINK_P (name
))
26225 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26226 == TEMPLATE_ID_EXPR
);
26230 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26231 it should already have been checked to make sure that the name
26232 used matches the type being destroyed. */
26233 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26237 /* Figure out to which type this destructor applies. */
26239 type
= parser
->scope
;
26240 else if (object_type
)
26241 type
= object_type
;
26243 type
= current_class_type
;
26244 /* If that's not a class type, there is no destructor. */
26245 if (!type
|| !CLASS_TYPE_P (type
))
26246 return error_mark_node
;
26248 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26249 lazily_declare_fn (sfk_destructor
, type
);
26251 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26254 return error_mark_node
;
26257 /* By this point, the NAME should be an ordinary identifier. If
26258 the id-expression was a qualified name, the qualifying scope is
26259 stored in PARSER->SCOPE at this point. */
26260 gcc_assert (identifier_p (name
));
26262 /* Perform the lookup. */
26267 if (parser
->scope
== error_mark_node
)
26268 return error_mark_node
;
26270 /* If the SCOPE is dependent, the lookup must be deferred until
26271 the template is instantiated -- unless we are explicitly
26272 looking up names in uninstantiated templates. Even then, we
26273 cannot look up the name if the scope is not a class type; it
26274 might, for example, be a template type parameter. */
26275 dependent_p
= (TYPE_P (parser
->scope
)
26276 && dependent_scope_p (parser
->scope
));
26277 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26279 /* Defer lookup. */
26280 decl
= error_mark_node
;
26283 tree pushed_scope
= NULL_TREE
;
26285 /* If PARSER->SCOPE is a dependent type, then it must be a
26286 class type, and we must not be checking dependencies;
26287 otherwise, we would have processed this lookup above. So
26288 that PARSER->SCOPE is not considered a dependent base by
26289 lookup_member, we must enter the scope here. */
26291 pushed_scope
= push_scope (parser
->scope
);
26293 /* If the PARSER->SCOPE is a template specialization, it
26294 may be instantiated during name lookup. In that case,
26295 errors may be issued. Even if we rollback the current
26296 tentative parse, those errors are valid. */
26297 decl
= lookup_qualified_name (parser
->scope
, name
,
26298 prefer_type_arg (tag_type
),
26299 /*complain=*/true);
26301 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26302 lookup result and the nested-name-specifier nominates a class C:
26303 * if the name specified after the nested-name-specifier, when
26304 looked up in C, is the injected-class-name of C (Clause 9), or
26305 * if the name specified after the nested-name-specifier is the
26306 same as the identifier or the simple-template-id's template-
26307 name in the last component of the nested-name-specifier,
26308 the name is instead considered to name the constructor of
26309 class C. [ Note: for example, the constructor is not an
26310 acceptable lookup result in an elaborated-type-specifier so
26311 the constructor would not be used in place of the
26312 injected-class-name. --end note ] Such a constructor name
26313 shall be used only in the declarator-id of a declaration that
26314 names a constructor or in a using-declaration. */
26315 if (tag_type
== none_type
26316 && DECL_SELF_REFERENCE_P (decl
)
26317 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26318 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26319 prefer_type_arg (tag_type
),
26320 /*complain=*/true);
26322 /* If we have a single function from a using decl, pull it out. */
26323 if (TREE_CODE (decl
) == OVERLOAD
26324 && !really_overloaded_fn (decl
))
26325 decl
= OVL_FUNCTION (decl
);
26328 pop_scope (pushed_scope
);
26331 /* If the scope is a dependent type and either we deferred lookup or
26332 we did lookup but didn't find the name, rememeber the name. */
26333 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26334 && dependent_type_p (parser
->scope
))
26340 /* The resolution to Core Issue 180 says that `struct
26341 A::B' should be considered a type-name, even if `A'
26343 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26344 /*complain=*/tf_error
);
26345 if (type
!= error_mark_node
)
26346 decl
= TYPE_NAME (type
);
26348 else if (is_template
26349 && (cp_parser_next_token_ends_template_argument_p (parser
)
26350 || cp_lexer_next_token_is (parser
->lexer
,
26352 decl
= make_unbound_class_template (parser
->scope
,
26354 /*complain=*/tf_error
);
26356 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26357 parser
->scope
, name
,
26360 parser
->qualifying_scope
= parser
->scope
;
26361 parser
->object_scope
= NULL_TREE
;
26363 else if (object_type
)
26365 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26366 OBJECT_TYPE is not a class. */
26367 if (CLASS_TYPE_P (object_type
))
26368 /* If the OBJECT_TYPE is a template specialization, it may
26369 be instantiated during name lookup. In that case, errors
26370 may be issued. Even if we rollback the current tentative
26371 parse, those errors are valid. */
26372 decl
= lookup_member (object_type
,
26375 prefer_type_arg (tag_type
),
26376 tf_warning_or_error
);
26381 /* Look it up in the enclosing context. DR 141: When looking for a
26382 template-name after -> or ., only consider class templates. */
26383 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26385 /*block_p=*/true, is_namespace
, 0);
26386 if (object_type
== unknown_type_node
)
26387 /* The object is type-dependent, so we can't look anything up; we used
26388 this to get the DR 141 behavior. */
26389 object_type
= NULL_TREE
;
26390 parser
->object_scope
= object_type
;
26391 parser
->qualifying_scope
= NULL_TREE
;
26395 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26397 /*block_p=*/true, is_namespace
, 0);
26398 parser
->qualifying_scope
= NULL_TREE
;
26399 parser
->object_scope
= NULL_TREE
;
26402 /* If the lookup failed, let our caller know. */
26403 if (!decl
|| decl
== error_mark_node
)
26404 return error_mark_node
;
26406 /* Pull out the template from an injected-class-name (or multiple). */
26408 decl
= maybe_get_template_decl_from_type_decl (decl
);
26410 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26411 if (TREE_CODE (decl
) == TREE_LIST
)
26413 if (ambiguous_decls
)
26414 *ambiguous_decls
= decl
;
26415 /* The error message we have to print is too complicated for
26416 cp_parser_error, so we incorporate its actions directly. */
26417 if (!cp_parser_simulate_error (parser
))
26419 error_at (name_location
, "reference to %qD is ambiguous",
26421 print_candidates (decl
);
26423 return error_mark_node
;
26426 gcc_assert (DECL_P (decl
)
26427 || TREE_CODE (decl
) == OVERLOAD
26428 || TREE_CODE (decl
) == SCOPE_REF
26429 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26430 || BASELINK_P (decl
));
26432 /* If we have resolved the name of a member declaration, check to
26433 see if the declaration is accessible. When the name resolves to
26434 set of overloaded functions, accessibility is checked when
26435 overload resolution is done.
26437 During an explicit instantiation, access is not checked at all,
26438 as per [temp.explicit]. */
26440 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26442 maybe_record_typedef_use (decl
);
26444 return cp_expr (decl
, name_location
);
26447 /* Like cp_parser_lookup_name, but for use in the typical case where
26448 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26449 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26452 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26454 return cp_parser_lookup_name (parser
, name
,
26456 /*is_template=*/false,
26457 /*is_namespace=*/false,
26458 /*check_dependency=*/true,
26459 /*ambiguous_decls=*/NULL
,
26463 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26464 the current context, return the TYPE_DECL. If TAG_NAME_P is
26465 true, the DECL indicates the class being defined in a class-head,
26466 or declared in an elaborated-type-specifier.
26468 Otherwise, return DECL. */
26471 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26473 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26474 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26477 template <typename T> struct B;
26480 template <typename T> struct A::B {};
26482 Similarly, in an elaborated-type-specifier:
26484 namespace N { struct X{}; }
26487 template <typename T> friend struct N::X;
26490 However, if the DECL refers to a class type, and we are in
26491 the scope of the class, then the name lookup automatically
26492 finds the TYPE_DECL created by build_self_reference rather
26493 than a TEMPLATE_DECL. For example, in:
26495 template <class T> struct S {
26499 there is no need to handle such case. */
26501 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26502 return DECL_TEMPLATE_RESULT (decl
);
26507 /* If too many, or too few, template-parameter lists apply to the
26508 declarator, issue an error message. Returns TRUE if all went well,
26509 and FALSE otherwise. */
26512 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26513 cp_declarator
*declarator
,
26514 location_t declarator_location
)
26516 switch (declarator
->kind
)
26520 unsigned num_templates
= 0;
26521 tree scope
= declarator
->u
.id
.qualifying_scope
;
26522 bool template_id_p
= false;
26525 num_templates
= num_template_headers_for_class (scope
);
26526 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26527 == TEMPLATE_ID_EXPR
)
26529 /* If the DECLARATOR has the form `X<y>' then it uses one
26530 additional level of template parameters. */
26532 template_id_p
= true;
26535 return cp_parser_check_template_parameters
26536 (parser
, num_templates
, template_id_p
, declarator_location
,
26543 case cdk_reference
:
26545 return (cp_parser_check_declarator_template_parameters
26546 (parser
, declarator
->declarator
, declarator_location
));
26553 gcc_unreachable ();
26558 /* NUM_TEMPLATES were used in the current declaration. If that is
26559 invalid, return FALSE and issue an error messages. Otherwise,
26560 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26561 declarator and we can print more accurate diagnostics. */
26564 cp_parser_check_template_parameters (cp_parser
* parser
,
26565 unsigned num_templates
,
26566 bool template_id_p
,
26567 location_t location
,
26568 cp_declarator
*declarator
)
26570 /* If there are the same number of template classes and parameter
26571 lists, that's OK. */
26572 if (parser
->num_template_parameter_lists
== num_templates
)
26574 /* If there are more, but only one more, and the name ends in an identifier,
26575 then we are declaring a primary template. That's OK too. */
26577 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26579 /* If there are more template classes than parameter lists, we have
26582 template <class T> void S<T>::R<T>::f (); */
26583 if (parser
->num_template_parameter_lists
< num_templates
)
26585 if (declarator
&& !current_function_decl
)
26586 error_at (location
, "specializing member %<%T::%E%> "
26587 "requires %<template<>%> syntax",
26588 declarator
->u
.id
.qualifying_scope
,
26589 declarator
->u
.id
.unqualified_name
);
26590 else if (declarator
)
26591 error_at (location
, "invalid declaration of %<%T::%E%>",
26592 declarator
->u
.id
.qualifying_scope
,
26593 declarator
->u
.id
.unqualified_name
);
26595 error_at (location
, "too few template-parameter-lists");
26598 /* Otherwise, there are too many template parameter lists. We have
26601 template <class T> template <class U> void S::f(); */
26602 error_at (location
, "too many template-parameter-lists");
26606 /* Parse an optional `::' token indicating that the following name is
26607 from the global namespace. If so, PARSER->SCOPE is set to the
26608 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26609 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26610 Returns the new value of PARSER->SCOPE, if the `::' token is
26611 present, and NULL_TREE otherwise. */
26614 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26618 /* Peek at the next token. */
26619 token
= cp_lexer_peek_token (parser
->lexer
);
26620 /* If we're looking at a `::' token then we're starting from the
26621 global namespace, not our current location. */
26622 if (token
->type
== CPP_SCOPE
)
26624 /* Consume the `::' token. */
26625 cp_lexer_consume_token (parser
->lexer
);
26626 /* Set the SCOPE so that we know where to start the lookup. */
26627 parser
->scope
= global_namespace
;
26628 parser
->qualifying_scope
= global_namespace
;
26629 parser
->object_scope
= NULL_TREE
;
26631 return parser
->scope
;
26633 else if (!current_scope_valid_p
)
26635 parser
->scope
= NULL_TREE
;
26636 parser
->qualifying_scope
= NULL_TREE
;
26637 parser
->object_scope
= NULL_TREE
;
26643 /* Returns TRUE if the upcoming token sequence is the start of a
26644 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26645 declarator is preceded by the `friend' specifier. */
26648 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26650 bool constructor_p
;
26651 bool outside_class_specifier_p
;
26652 tree nested_name_specifier
;
26653 cp_token
*next_token
;
26655 /* The common case is that this is not a constructor declarator, so
26656 try to avoid doing lots of work if at all possible. It's not
26657 valid declare a constructor at function scope. */
26658 if (parser
->in_function_body
)
26660 /* And only certain tokens can begin a constructor declarator. */
26661 next_token
= cp_lexer_peek_token (parser
->lexer
);
26662 if (next_token
->type
!= CPP_NAME
26663 && next_token
->type
!= CPP_SCOPE
26664 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26665 && next_token
->type
!= CPP_TEMPLATE_ID
)
26668 /* Parse tentatively; we are going to roll back all of the tokens
26670 cp_parser_parse_tentatively (parser
);
26671 /* Assume that we are looking at a constructor declarator. */
26672 constructor_p
= true;
26674 /* Look for the optional `::' operator. */
26675 cp_parser_global_scope_opt (parser
,
26676 /*current_scope_valid_p=*/false);
26677 /* Look for the nested-name-specifier. */
26678 nested_name_specifier
26679 = (cp_parser_nested_name_specifier_opt (parser
,
26680 /*typename_keyword_p=*/false,
26681 /*check_dependency_p=*/false,
26683 /*is_declaration=*/false));
26685 outside_class_specifier_p
= (!at_class_scope_p ()
26686 || !TYPE_BEING_DEFINED (current_class_type
)
26689 /* Outside of a class-specifier, there must be a
26690 nested-name-specifier. Except in C++17 mode, where we
26691 might be declaring a guiding declaration. */
26692 if (!nested_name_specifier
&& outside_class_specifier_p
26693 && cxx_dialect
< cxx17
)
26694 constructor_p
= false;
26695 else if (nested_name_specifier
== error_mark_node
)
26696 constructor_p
= false;
26698 /* If we have a class scope, this is easy; DR 147 says that S::S always
26699 names the constructor, and no other qualified name could. */
26700 if (constructor_p
&& nested_name_specifier
26701 && CLASS_TYPE_P (nested_name_specifier
))
26703 tree id
= cp_parser_unqualified_id (parser
,
26704 /*template_keyword_p=*/false,
26705 /*check_dependency_p=*/false,
26706 /*declarator_p=*/true,
26707 /*optional_p=*/false);
26708 if (is_overloaded_fn (id
))
26709 id
= DECL_NAME (get_first_fn (id
));
26710 if (!constructor_name_p (id
, nested_name_specifier
))
26711 constructor_p
= false;
26713 /* If we still think that this might be a constructor-declarator,
26714 look for a class-name. */
26715 else if (constructor_p
)
26719 template <typename T> struct S {
26723 we must recognize that the nested `S' names a class. */
26724 if (cxx_dialect
>= cxx17
)
26725 cp_parser_parse_tentatively (parser
);
26728 type_decl
= cp_parser_class_name (parser
,
26729 /*typename_keyword_p=*/false,
26730 /*template_keyword_p=*/false,
26732 /*check_dependency_p=*/false,
26733 /*class_head_p=*/false,
26734 /*is_declaration=*/false);
26736 if (cxx_dialect
>= cxx17
26737 && !cp_parser_parse_definitely (parser
))
26739 type_decl
= NULL_TREE
;
26740 tree tmpl
= cp_parser_template_name (parser
,
26741 /*template_keyword*/false,
26742 /*check_dependency_p*/false,
26743 /*is_declaration*/false,
26745 /*is_identifier*/NULL
);
26746 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26747 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26748 /* It's a deduction guide, return true. */;
26750 cp_parser_simulate_error (parser
);
26753 /* If there was no class-name, then this is not a constructor.
26754 Otherwise, if we are in a class-specifier and we aren't
26755 handling a friend declaration, check that its type matches
26756 current_class_type (c++/38313). Note: error_mark_node
26757 is left alone for error recovery purposes. */
26758 constructor_p
= (!cp_parser_error_occurred (parser
)
26759 && (outside_class_specifier_p
26760 || type_decl
== NULL_TREE
26761 || type_decl
== error_mark_node
26762 || same_type_p (current_class_type
,
26763 TREE_TYPE (type_decl
))));
26765 /* If we're still considering a constructor, we have to see a `(',
26766 to begin the parameter-declaration-clause, followed by either a
26767 `)', an `...', or a decl-specifier. We need to check for a
26768 type-specifier to avoid being fooled into thinking that:
26772 is a constructor. (It is actually a function named `f' that
26773 takes one parameter (of type `int') and returns a value of type
26776 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26777 constructor_p
= false;
26780 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26781 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26782 /* A parameter declaration begins with a decl-specifier,
26783 which is either the "attribute" keyword, a storage class
26784 specifier, or (usually) a type-specifier. */
26785 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26788 tree pushed_scope
= NULL_TREE
;
26789 unsigned saved_num_template_parameter_lists
;
26791 /* Names appearing in the type-specifier should be looked up
26792 in the scope of the class. */
26793 if (current_class_type
)
26795 else if (type_decl
)
26797 type
= TREE_TYPE (type_decl
);
26798 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26800 type
= resolve_typename_type (type
,
26801 /*only_current_p=*/false);
26802 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26804 cp_parser_abort_tentative_parse (parser
);
26808 pushed_scope
= push_scope (type
);
26811 /* Inside the constructor parameter list, surrounding
26812 template-parameter-lists do not apply. */
26813 saved_num_template_parameter_lists
26814 = parser
->num_template_parameter_lists
;
26815 parser
->num_template_parameter_lists
= 0;
26817 /* Look for the type-specifier. */
26818 cp_parser_type_specifier (parser
,
26819 CP_PARSER_FLAGS_NONE
,
26820 /*decl_specs=*/NULL
,
26821 /*is_declarator=*/true,
26822 /*declares_class_or_enum=*/NULL
,
26823 /*is_cv_qualifier=*/NULL
);
26825 parser
->num_template_parameter_lists
26826 = saved_num_template_parameter_lists
;
26828 /* Leave the scope of the class. */
26830 pop_scope (pushed_scope
);
26832 constructor_p
= !cp_parser_error_occurred (parser
);
26836 /* We did not really want to consume any tokens. */
26837 cp_parser_abort_tentative_parse (parser
);
26839 return constructor_p
;
26842 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26843 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26844 they must be performed once we are in the scope of the function.
26846 Returns the function defined. */
26849 cp_parser_function_definition_from_specifiers_and_declarator
26850 (cp_parser
* parser
,
26851 cp_decl_specifier_seq
*decl_specifiers
,
26853 const cp_declarator
*declarator
)
26858 /* Begin the function-definition. */
26859 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26861 /* The things we're about to see are not directly qualified by any
26862 template headers we've seen thus far. */
26863 reset_specialization ();
26865 /* If there were names looked up in the decl-specifier-seq that we
26866 did not check, check them now. We must wait until we are in the
26867 scope of the function to perform the checks, since the function
26868 might be a friend. */
26869 perform_deferred_access_checks (tf_warning_or_error
);
26873 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26874 parser
->omp_declare_simd
= NULL
;
26875 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26876 parser
->oacc_routine
= NULL
;
26881 /* Skip the entire function. */
26882 cp_parser_skip_to_end_of_block_or_statement (parser
);
26883 fn
= error_mark_node
;
26885 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26887 /* Seen already, skip it. An error message has already been output. */
26888 cp_parser_skip_to_end_of_block_or_statement (parser
);
26889 fn
= current_function_decl
;
26890 current_function_decl
= NULL_TREE
;
26891 /* If this is a function from a class, pop the nested class. */
26892 if (current_class_name
)
26893 pop_nested_class ();
26898 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26899 tv
= TV_PARSE_INLINE
;
26901 tv
= TV_PARSE_FUNC
;
26903 fn
= cp_parser_function_definition_after_declarator (parser
,
26904 /*inline_p=*/false);
26911 /* Parse the part of a function-definition that follows the
26912 declarator. INLINE_P is TRUE iff this function is an inline
26913 function defined within a class-specifier.
26915 Returns the function defined. */
26918 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26922 bool saved_in_unbraced_linkage_specification_p
;
26923 bool saved_in_function_body
;
26924 unsigned saved_num_template_parameter_lists
;
26926 bool fully_implicit_function_template_p
26927 = parser
->fully_implicit_function_template_p
;
26928 parser
->fully_implicit_function_template_p
= false;
26929 tree implicit_template_parms
26930 = parser
->implicit_template_parms
;
26931 parser
->implicit_template_parms
= 0;
26932 cp_binding_level
* implicit_template_scope
26933 = parser
->implicit_template_scope
;
26934 parser
->implicit_template_scope
= 0;
26936 saved_in_function_body
= parser
->in_function_body
;
26937 parser
->in_function_body
= true;
26938 /* If the next token is `return', then the code may be trying to
26939 make use of the "named return value" extension that G++ used to
26941 token
= cp_lexer_peek_token (parser
->lexer
);
26942 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26944 /* Consume the `return' keyword. */
26945 cp_lexer_consume_token (parser
->lexer
);
26946 /* Look for the identifier that indicates what value is to be
26948 cp_parser_identifier (parser
);
26949 /* Issue an error message. */
26950 error_at (token
->location
,
26951 "named return values are no longer supported");
26952 /* Skip tokens until we reach the start of the function body. */
26955 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26956 if (token
->type
== CPP_OPEN_BRACE
26957 || token
->type
== CPP_EOF
26958 || token
->type
== CPP_PRAGMA_EOL
)
26960 cp_lexer_consume_token (parser
->lexer
);
26963 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26964 anything declared inside `f'. */
26965 saved_in_unbraced_linkage_specification_p
26966 = parser
->in_unbraced_linkage_specification_p
;
26967 parser
->in_unbraced_linkage_specification_p
= false;
26968 /* Inside the function, surrounding template-parameter-lists do not
26970 saved_num_template_parameter_lists
26971 = parser
->num_template_parameter_lists
;
26972 parser
->num_template_parameter_lists
= 0;
26974 /* If the next token is `try', `__transaction_atomic', or
26975 `__transaction_relaxed`, then we are looking at either function-try-block
26976 or function-transaction-block. Note that all of these include the
26978 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26979 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26980 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26981 RID_TRANSACTION_RELAXED
))
26982 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26983 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26984 cp_parser_function_try_block (parser
);
26986 cp_parser_ctor_initializer_opt_and_function_body
26987 (parser
, /*in_function_try_block=*/false);
26989 /* Finish the function. */
26990 fn
= finish_function (inline_p
);
26991 /* Generate code for it, if necessary. */
26992 expand_or_defer_fn (fn
);
26993 /* Restore the saved values. */
26994 parser
->in_unbraced_linkage_specification_p
26995 = saved_in_unbraced_linkage_specification_p
;
26996 parser
->num_template_parameter_lists
26997 = saved_num_template_parameter_lists
;
26998 parser
->in_function_body
= saved_in_function_body
;
27000 parser
->fully_implicit_function_template_p
27001 = fully_implicit_function_template_p
;
27002 parser
->implicit_template_parms
27003 = implicit_template_parms
;
27004 parser
->implicit_template_scope
27005 = implicit_template_scope
;
27007 if (parser
->fully_implicit_function_template_p
)
27008 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27013 /* Parse a template-declaration body (following argument list). */
27016 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27017 tree parameter_list
,
27020 tree decl
= NULL_TREE
;
27021 bool friend_p
= false;
27023 /* We just processed one more parameter list. */
27024 ++parser
->num_template_parameter_lists
;
27026 /* Get the deferred access checks from the parameter list. These
27027 will be checked once we know what is being declared, as for a
27028 member template the checks must be performed in the scope of the
27029 class containing the member. */
27030 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27032 /* Tentatively parse for a new template parameter list, which can either be
27033 the template keyword or a template introduction. */
27034 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27036 else if (cxx_dialect
>= cxx11
27037 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27038 decl
= cp_parser_alias_declaration (parser
);
27041 /* There are no access checks when parsing a template, as we do not
27042 know if a specialization will be a friend. */
27043 push_deferring_access_checks (dk_no_check
);
27044 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27045 decl
= cp_parser_single_declaration (parser
,
27048 /*explicit_specialization_p=*/false,
27050 pop_deferring_access_checks ();
27052 /* If this is a member template declaration, let the front
27054 if (member_p
&& !friend_p
&& decl
)
27056 if (TREE_CODE (decl
) == TYPE_DECL
)
27057 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27059 decl
= finish_member_template_decl (decl
);
27061 else if (friend_p
&& decl
27062 && DECL_DECLARES_TYPE_P (decl
))
27063 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27064 /*complain=*/true);
27066 /* We are done with the current parameter list. */
27067 --parser
->num_template_parameter_lists
;
27069 pop_deferring_access_checks ();
27072 finish_template_decl (parameter_list
);
27074 /* Check the template arguments for a literal operator template. */
27076 && DECL_DECLARES_FUNCTION_P (decl
)
27077 && UDLIT_OPER_P (DECL_NAME (decl
)))
27080 if (parameter_list
== NULL_TREE
)
27084 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27085 if (num_parms
== 1)
27087 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27088 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27089 if (TREE_TYPE (parm
) != char_type_node
27090 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27093 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27095 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27096 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27097 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27098 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27099 if (parm
== error_mark_node
27100 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27101 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27109 if (cxx_dialect
>= cxx14
)
27110 error ("literal operator template %qD has invalid parameter list."
27111 " Expected non-type template argument pack <char...>"
27112 " or <typename CharT, CharT...>",
27115 error ("literal operator template %qD has invalid parameter list."
27116 " Expected non-type template argument pack <char...>",
27121 /* Register member declarations. */
27122 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27123 finish_member_declaration (decl
);
27124 /* If DECL is a function template, we must return to parse it later.
27125 (Even though there is no definition, there might be default
27126 arguments that need handling.) */
27127 if (member_p
&& decl
27128 && DECL_DECLARES_FUNCTION_P (decl
))
27129 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27132 /* Parse a template introduction header for a template-declaration. Returns
27133 false if tentative parse fails. */
27136 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27138 cp_parser_parse_tentatively (parser
);
27140 tree saved_scope
= parser
->scope
;
27141 tree saved_object_scope
= parser
->object_scope
;
27142 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27144 /* Look for the optional `::' operator. */
27145 cp_parser_global_scope_opt (parser
,
27146 /*current_scope_valid_p=*/false);
27147 /* Look for the nested-name-specifier. */
27148 cp_parser_nested_name_specifier_opt (parser
,
27149 /*typename_keyword_p=*/false,
27150 /*check_dependency_p=*/true,
27152 /*is_declaration=*/false);
27154 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27155 tree concept_name
= cp_parser_identifier (parser
);
27157 /* Look up the concept for which we will be matching
27158 template parameters. */
27159 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27161 parser
->scope
= saved_scope
;
27162 parser
->object_scope
= saved_object_scope
;
27163 parser
->qualifying_scope
= saved_qualifying_scope
;
27165 if (concept_name
== error_mark_node
)
27166 cp_parser_simulate_error (parser
);
27168 /* Look for opening brace for introduction. */
27169 matching_braces braces
;
27170 braces
.require_open (parser
);
27172 if (!cp_parser_parse_definitely (parser
))
27175 push_deferring_access_checks (dk_deferred
);
27177 /* Build vector of placeholder parameters and grab
27178 matching identifiers. */
27179 tree introduction_list
= cp_parser_introduction_list (parser
);
27181 /* The introduction-list shall not be empty. */
27182 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27185 error ("empty introduction-list");
27189 /* Look for closing brace for introduction. */
27190 if (!braces
.require_close (parser
))
27193 if (tmpl_decl
== error_mark_node
)
27195 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27200 /* Build and associate the constraint. */
27201 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27202 if (parms
&& parms
!= error_mark_node
)
27204 cp_parser_template_declaration_after_parameters (parser
, parms
,
27209 error_at (token
->location
, "no matching concept for template-introduction");
27213 /* Parse a normal template-declaration following the template keyword. */
27216 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27218 tree parameter_list
;
27219 bool need_lang_pop
;
27220 location_t location
= input_location
;
27222 /* Look for the `<' token. */
27223 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27225 if (at_class_scope_p () && current_function_decl
)
27227 /* 14.5.2.2 [temp.mem]
27229 A local class shall not have member templates. */
27230 error_at (location
,
27231 "invalid declaration of member template in local class");
27232 cp_parser_skip_to_end_of_block_or_statement (parser
);
27237 A template ... shall not have C linkage. */
27238 if (current_lang_name
== lang_name_c
)
27240 error_at (location
, "template with C linkage");
27241 maybe_show_extern_c_location ();
27242 /* Give it C++ linkage to avoid confusing other parts of the
27244 push_lang_context (lang_name_cplusplus
);
27245 need_lang_pop
= true;
27248 need_lang_pop
= false;
27250 /* We cannot perform access checks on the template parameter
27251 declarations until we know what is being declared, just as we
27252 cannot check the decl-specifier list. */
27253 push_deferring_access_checks (dk_deferred
);
27255 /* If the next token is `>', then we have an invalid
27256 specialization. Rather than complain about an invalid template
27257 parameter, issue an error message here. */
27258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27260 cp_parser_error (parser
, "invalid explicit specialization");
27261 begin_specialization ();
27262 parameter_list
= NULL_TREE
;
27266 /* Parse the template parameters. */
27267 parameter_list
= cp_parser_template_parameter_list (parser
);
27270 /* Look for the `>'. */
27271 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27273 /* Manage template requirements */
27276 tree reqs
= get_shorthand_constraints (current_template_parms
);
27277 if (tree r
= cp_parser_requires_clause_opt (parser
))
27278 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27279 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27282 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27285 /* For the erroneous case of a template with C linkage, we pushed an
27286 implicit C++ linkage scope; exit that scope now. */
27288 pop_lang_context ();
27291 /* Parse a template-declaration, assuming that the `export' (and
27292 `extern') keywords, if present, has already been scanned. MEMBER_P
27293 is as for cp_parser_template_declaration. */
27296 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27298 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27300 cp_lexer_consume_token (parser
->lexer
);
27301 cp_parser_explicit_template_declaration (parser
, member_p
);
27304 else if (flag_concepts
)
27305 return cp_parser_template_introduction (parser
, member_p
);
27310 /* Perform the deferred access checks from a template-parameter-list.
27311 CHECKS is a TREE_LIST of access checks, as returned by
27312 get_deferred_access_checks. */
27315 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27317 ++processing_template_parmlist
;
27318 perform_access_checks (checks
, tf_warning_or_error
);
27319 --processing_template_parmlist
;
27322 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27323 `function-definition' sequence that follows a template header.
27324 If MEMBER_P is true, this declaration appears in a class scope.
27326 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27327 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27330 cp_parser_single_declaration (cp_parser
* parser
,
27331 vec
<deferred_access_check
, va_gc
> *checks
,
27333 bool explicit_specialization_p
,
27336 int declares_class_or_enum
;
27337 tree decl
= NULL_TREE
;
27338 cp_decl_specifier_seq decl_specifiers
;
27339 bool function_definition_p
= false;
27340 cp_token
*decl_spec_token_start
;
27342 /* This function is only used when processing a template
27344 gcc_assert (innermost_scope_kind () == sk_template_parms
27345 || innermost_scope_kind () == sk_template_spec
);
27347 /* Defer access checks until we know what is being declared. */
27348 push_deferring_access_checks (dk_deferred
);
27350 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27352 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27353 cp_parser_decl_specifier_seq (parser
,
27354 CP_PARSER_FLAGS_OPTIONAL
,
27356 &declares_class_or_enum
);
27358 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27360 /* There are no template typedefs. */
27361 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27363 error_at (decl_spec_token_start
->location
,
27364 "template declaration of %<typedef%>");
27365 decl
= error_mark_node
;
27368 /* Gather up the access checks that occurred the
27369 decl-specifier-seq. */
27370 stop_deferring_access_checks ();
27372 /* Check for the declaration of a template class. */
27373 if (declares_class_or_enum
)
27375 if (cp_parser_declares_only_class_p (parser
)
27376 || (declares_class_or_enum
& 2))
27378 // If this is a declaration, but not a definition, associate
27379 // any constraints with the type declaration. Constraints
27380 // are associated with definitions in cp_parser_class_specifier.
27381 if (declares_class_or_enum
== 1)
27382 associate_classtype_constraints (decl_specifiers
.type
);
27384 decl
= shadow_tag (&decl_specifiers
);
27389 friend template <typename T> struct A<T>::B;
27392 A<T>::B will be represented by a TYPENAME_TYPE, and
27393 therefore not recognized by shadow_tag. */
27394 if (friend_p
&& *friend_p
27396 && decl_specifiers
.type
27397 && TYPE_P (decl_specifiers
.type
))
27398 decl
= decl_specifiers
.type
;
27400 if (decl
&& decl
!= error_mark_node
)
27401 decl
= TYPE_NAME (decl
);
27403 decl
= error_mark_node
;
27405 /* Perform access checks for template parameters. */
27406 cp_parser_perform_template_parameter_access_checks (checks
);
27408 /* Give a helpful diagnostic for
27409 template <class T> struct A { } a;
27410 if we aren't already recovering from an error. */
27411 if (!cp_parser_declares_only_class_p (parser
)
27414 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27415 "a class template declaration must not declare "
27417 cp_parser_skip_to_end_of_block_or_statement (parser
);
27423 /* Complain about missing 'typename' or other invalid type names. */
27424 if (!decl_specifiers
.any_type_specifiers_p
27425 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27427 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27428 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27429 the rest of this declaration. */
27430 decl
= error_mark_node
;
27434 /* If it's not a template class, try for a template function. If
27435 the next token is a `;', then this declaration does not declare
27436 anything. But, if there were errors in the decl-specifiers, then
27437 the error might well have come from an attempted class-specifier.
27438 In that case, there's no need to warn about a missing declarator. */
27440 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27441 || decl_specifiers
.type
!= error_mark_node
))
27443 decl
= cp_parser_init_declarator (parser
,
27446 /*function_definition_allowed_p=*/true,
27448 declares_class_or_enum
,
27449 &function_definition_p
,
27452 /* 7.1.1-1 [dcl.stc]
27454 A storage-class-specifier shall not be specified in an explicit
27455 specialization... */
27457 && explicit_specialization_p
27458 && decl_specifiers
.storage_class
!= sc_none
)
27460 error_at (decl_spec_token_start
->location
,
27461 "explicit template specialization cannot have a storage class");
27462 decl
= error_mark_node
;
27465 if (decl
&& VAR_P (decl
))
27466 check_template_variable (decl
);
27469 /* Look for a trailing `;' after the declaration. */
27470 if (!function_definition_p
27471 && (decl
== error_mark_node
27472 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27473 cp_parser_skip_to_end_of_block_or_statement (parser
);
27476 pop_deferring_access_checks ();
27478 /* Clear any current qualification; whatever comes next is the start
27479 of something new. */
27480 parser
->scope
= NULL_TREE
;
27481 parser
->qualifying_scope
= NULL_TREE
;
27482 parser
->object_scope
= NULL_TREE
;
27487 /* Parse a cast-expression that is not the operand of a unary "&". */
27490 cp_parser_simple_cast_expression (cp_parser
*parser
)
27492 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27493 /*cast_p=*/false, /*decltype*/false, NULL
);
27496 /* Parse a functional cast to TYPE. Returns an expression
27497 representing the cast. */
27500 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27502 vec
<tree
, va_gc
> *vec
;
27503 tree expression_list
;
27507 location_t start_loc
= input_location
;
27510 type
= error_mark_node
;
27512 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27514 cp_lexer_set_source_position (parser
->lexer
);
27515 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27516 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27517 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27518 if (TREE_CODE (type
) == TYPE_DECL
)
27519 type
= TREE_TYPE (type
);
27521 cast
= finish_compound_literal (type
, expression_list
,
27522 tf_warning_or_error
, fcl_functional
);
27523 /* Create a location of the form:
27526 with caret == start at the start of the type name,
27527 finishing at the closing brace. */
27528 location_t finish_loc
27529 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27530 location_t combined_loc
= make_location (start_loc
, start_loc
,
27532 cast
.set_location (combined_loc
);
27537 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27539 /*allow_expansion_p=*/true,
27540 /*non_constant_p=*/NULL
);
27542 expression_list
= error_mark_node
;
27545 expression_list
= build_tree_list_vec (vec
);
27546 release_tree_vector (vec
);
27549 cast
= build_functional_cast (type
, expression_list
,
27550 tf_warning_or_error
);
27551 /* [expr.const]/1: In an integral constant expression "only type
27552 conversions to integral or enumeration type can be used". */
27553 if (TREE_CODE (type
) == TYPE_DECL
)
27554 type
= TREE_TYPE (type
);
27555 if (cast
!= error_mark_node
27556 && !cast_valid_in_integral_constant_expression_p (type
)
27557 && cp_parser_non_integral_constant_expression (parser
,
27559 return error_mark_node
;
27561 /* Create a location of the form:
27564 with caret == start at the start of the type name,
27565 finishing at the closing paren. */
27566 location_t finish_loc
27567 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27568 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27569 cast
.set_location (combined_loc
);
27573 /* Save the tokens that make up the body of a member function defined
27574 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27575 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27576 specifiers applied to the declaration. Returns the FUNCTION_DECL
27577 for the member function. */
27580 cp_parser_save_member_function_body (cp_parser
* parser
,
27581 cp_decl_specifier_seq
*decl_specifiers
,
27582 cp_declarator
*declarator
,
27588 bool function_try_block
= false;
27590 /* Create the FUNCTION_DECL. */
27591 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27592 cp_finalize_omp_declare_simd (parser
, fn
);
27593 cp_finalize_oacc_routine (parser
, fn
, true);
27594 /* If something went badly wrong, bail out now. */
27595 if (fn
== error_mark_node
)
27597 /* If there's a function-body, skip it. */
27598 if (cp_parser_token_starts_function_definition_p
27599 (cp_lexer_peek_token (parser
->lexer
)))
27600 cp_parser_skip_to_end_of_block_or_statement (parser
);
27601 return error_mark_node
;
27604 /* Remember it, if there default args to post process. */
27605 cp_parser_save_default_args (parser
, fn
);
27607 /* Save away the tokens that make up the body of the
27609 first
= parser
->lexer
->next_token
;
27611 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27612 cp_lexer_consume_token (parser
->lexer
);
27613 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27614 RID_TRANSACTION_ATOMIC
))
27616 cp_lexer_consume_token (parser
->lexer
);
27617 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27618 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27619 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27620 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27621 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27622 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27623 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27625 cp_lexer_consume_token (parser
->lexer
);
27626 cp_lexer_consume_token (parser
->lexer
);
27627 cp_lexer_consume_token (parser
->lexer
);
27628 cp_lexer_consume_token (parser
->lexer
);
27629 cp_lexer_consume_token (parser
->lexer
);
27632 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27633 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27635 cp_lexer_consume_token (parser
->lexer
);
27636 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27641 /* Handle function try blocks. */
27642 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27644 cp_lexer_consume_token (parser
->lexer
);
27645 function_try_block
= true;
27647 /* We can have braced-init-list mem-initializers before the fn body. */
27648 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27650 cp_lexer_consume_token (parser
->lexer
);
27651 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27653 /* cache_group will stop after an un-nested { } pair, too. */
27654 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27657 /* variadic mem-inits have ... after the ')'. */
27658 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27659 cp_lexer_consume_token (parser
->lexer
);
27662 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27663 /* Handle function try blocks. */
27664 if (function_try_block
)
27665 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27666 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27667 last
= parser
->lexer
->next_token
;
27669 /* Save away the inline definition; we will process it when the
27670 class is complete. */
27671 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27672 DECL_PENDING_INLINE_P (fn
) = 1;
27674 /* We need to know that this was defined in the class, so that
27675 friend templates are handled correctly. */
27676 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27678 /* Add FN to the queue of functions to be parsed later. */
27679 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27684 /* Save the tokens that make up the in-class initializer for a non-static
27685 data member. Returns a DEFAULT_ARG. */
27688 cp_parser_save_nsdmi (cp_parser
* parser
)
27690 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27693 /* Parse a template-argument-list, as well as the trailing ">" (but
27694 not the opening "<"). See cp_parser_template_argument_list for the
27698 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27702 tree saved_qualifying_scope
;
27703 tree saved_object_scope
;
27704 bool saved_greater_than_is_operator_p
;
27705 int saved_unevaluated_operand
;
27706 int saved_inhibit_evaluation_warnings
;
27710 When parsing a template-id, the first non-nested `>' is taken as
27711 the end of the template-argument-list rather than a greater-than
27713 saved_greater_than_is_operator_p
27714 = parser
->greater_than_is_operator_p
;
27715 parser
->greater_than_is_operator_p
= false;
27716 /* Parsing the argument list may modify SCOPE, so we save it
27718 saved_scope
= parser
->scope
;
27719 saved_qualifying_scope
= parser
->qualifying_scope
;
27720 saved_object_scope
= parser
->object_scope
;
27721 /* We need to evaluate the template arguments, even though this
27722 template-id may be nested within a "sizeof". */
27723 saved_unevaluated_operand
= cp_unevaluated_operand
;
27724 cp_unevaluated_operand
= 0;
27725 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27726 c_inhibit_evaluation_warnings
= 0;
27727 /* Parse the template-argument-list itself. */
27728 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27729 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27730 arguments
= NULL_TREE
;
27732 arguments
= cp_parser_template_argument_list (parser
);
27733 /* Look for the `>' that ends the template-argument-list. If we find
27734 a '>>' instead, it's probably just a typo. */
27735 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27737 if (cxx_dialect
!= cxx98
)
27739 /* In C++0x, a `>>' in a template argument list or cast
27740 expression is considered to be two separate `>'
27741 tokens. So, change the current token to a `>', but don't
27742 consume it: it will be consumed later when the outer
27743 template argument list (or cast expression) is parsed.
27744 Note that this replacement of `>' for `>>' is necessary
27745 even if we are parsing tentatively: in the tentative
27746 case, after calling
27747 cp_parser_enclosed_template_argument_list we will always
27748 throw away all of the template arguments and the first
27749 closing `>', either because the template argument list
27750 was erroneous or because we are replacing those tokens
27751 with a CPP_TEMPLATE_ID token. The second `>' (which will
27752 not have been thrown away) is needed either to close an
27753 outer template argument list or to complete a new-style
27755 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27756 token
->type
= CPP_GREATER
;
27758 else if (!saved_greater_than_is_operator_p
)
27760 /* If we're in a nested template argument list, the '>>' has
27761 to be a typo for '> >'. We emit the error message, but we
27762 continue parsing and we push a '>' as next token, so that
27763 the argument list will be parsed correctly. Note that the
27764 global source location is still on the token before the
27765 '>>', so we need to say explicitly where we want it. */
27766 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27767 gcc_rich_location
richloc (token
->location
);
27768 richloc
.add_fixit_replace ("> >");
27769 error_at (&richloc
, "%<>>%> should be %<> >%> "
27770 "within a nested template argument list");
27772 token
->type
= CPP_GREATER
;
27776 /* If this is not a nested template argument list, the '>>'
27777 is a typo for '>'. Emit an error message and continue.
27778 Same deal about the token location, but here we can get it
27779 right by consuming the '>>' before issuing the diagnostic. */
27780 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27781 error_at (token
->location
,
27782 "spurious %<>>%>, use %<>%> to terminate "
27783 "a template argument list");
27787 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27788 /* The `>' token might be a greater-than operator again now. */
27789 parser
->greater_than_is_operator_p
27790 = saved_greater_than_is_operator_p
;
27791 /* Restore the SAVED_SCOPE. */
27792 parser
->scope
= saved_scope
;
27793 parser
->qualifying_scope
= saved_qualifying_scope
;
27794 parser
->object_scope
= saved_object_scope
;
27795 cp_unevaluated_operand
= saved_unevaluated_operand
;
27796 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27801 /* MEMBER_FUNCTION is a member function, or a friend. If default
27802 arguments, or the body of the function have not yet been parsed,
27806 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27808 timevar_push (TV_PARSE_INMETH
);
27809 /* If this member is a template, get the underlying
27811 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27812 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27814 /* There should not be any class definitions in progress at this
27815 point; the bodies of members are only parsed outside of all class
27817 gcc_assert (parser
->num_classes_being_defined
== 0);
27818 /* While we're parsing the member functions we might encounter more
27819 classes. We want to handle them right away, but we don't want
27820 them getting mixed up with functions that are currently in the
27822 push_unparsed_function_queues (parser
);
27824 /* Make sure that any template parameters are in scope. */
27825 maybe_begin_member_template_processing (member_function
);
27827 /* If the body of the function has not yet been parsed, parse it
27829 if (DECL_PENDING_INLINE_P (member_function
))
27831 tree function_scope
;
27832 cp_token_cache
*tokens
;
27834 /* The function is no longer pending; we are processing it. */
27835 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27836 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27837 DECL_PENDING_INLINE_P (member_function
) = 0;
27839 /* If this is a local class, enter the scope of the containing
27841 function_scope
= current_function_decl
;
27842 if (function_scope
)
27843 push_function_context ();
27845 /* Push the body of the function onto the lexer stack. */
27846 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27848 /* Let the front end know that we going to be defining this
27850 start_preparsed_function (member_function
, NULL_TREE
,
27851 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27853 /* Don't do access checking if it is a templated function. */
27854 if (processing_template_decl
)
27855 push_deferring_access_checks (dk_no_check
);
27857 /* #pragma omp declare reduction needs special parsing. */
27858 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27860 parser
->lexer
->in_pragma
= true;
27861 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27862 finish_function (/*inline_p=*/true);
27863 cp_check_omp_declare_reduction (member_function
);
27866 /* Now, parse the body of the function. */
27867 cp_parser_function_definition_after_declarator (parser
,
27868 /*inline_p=*/true);
27870 if (processing_template_decl
)
27871 pop_deferring_access_checks ();
27873 /* Leave the scope of the containing function. */
27874 if (function_scope
)
27875 pop_function_context ();
27876 cp_parser_pop_lexer (parser
);
27879 /* Remove any template parameters from the symbol table. */
27880 maybe_end_member_template_processing ();
27882 /* Restore the queue. */
27883 pop_unparsed_function_queues (parser
);
27884 timevar_pop (TV_PARSE_INMETH
);
27887 /* If DECL contains any default args, remember it on the unparsed
27888 functions queue. */
27891 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27895 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27897 probe
= TREE_CHAIN (probe
))
27898 if (TREE_PURPOSE (probe
))
27900 cp_default_arg_entry entry
= {current_class_type
, decl
};
27901 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27906 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27907 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27908 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27909 from the parameter-type-list. */
27912 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27913 tree default_arg
, tree parmtype
)
27915 cp_token_cache
*tokens
;
27919 if (default_arg
== error_mark_node
)
27920 return error_mark_node
;
27922 /* Push the saved tokens for the default argument onto the parser's
27924 tokens
= DEFARG_TOKENS (default_arg
);
27925 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27927 start_lambda_scope (decl
);
27929 /* Parse the default argument. */
27930 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27931 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27932 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27934 finish_lambda_scope ();
27936 if (parsed_arg
== error_mark_node
)
27937 cp_parser_skip_to_end_of_statement (parser
);
27939 if (!processing_template_decl
)
27941 /* In a non-template class, check conversions now. In a template,
27942 we'll wait and instantiate these as needed. */
27943 if (TREE_CODE (decl
) == PARM_DECL
)
27944 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27945 tf_warning_or_error
);
27946 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27947 parsed_arg
= error_mark_node
;
27949 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27952 /* If the token stream has not been completely used up, then
27953 there was extra junk after the end of the default
27955 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27957 if (TREE_CODE (decl
) == PARM_DECL
)
27958 cp_parser_error (parser
, "expected %<,%>");
27960 cp_parser_error (parser
, "expected %<;%>");
27963 /* Revert to the main lexer. */
27964 cp_parser_pop_lexer (parser
);
27969 /* FIELD is a non-static data member with an initializer which we saved for
27970 later; parse it now. */
27973 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27977 maybe_begin_member_template_processing (field
);
27979 push_unparsed_function_queues (parser
);
27980 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27981 DECL_INITIAL (field
),
27983 pop_unparsed_function_queues (parser
);
27985 maybe_end_member_template_processing ();
27987 DECL_INITIAL (field
) = def
;
27990 /* FN is a FUNCTION_DECL which may contains a parameter with an
27991 unparsed DEFAULT_ARG. Parse the default args now. This function
27992 assumes that the current scope is the scope in which the default
27993 argument should be processed. */
27996 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27998 bool saved_local_variables_forbidden_p
;
27999 tree parm
, parmdecl
;
28001 /* While we're parsing the default args, we might (due to the
28002 statement expression extension) encounter more classes. We want
28003 to handle them right away, but we don't want them getting mixed
28004 up with default args that are currently in the queue. */
28005 push_unparsed_function_queues (parser
);
28007 /* Local variable names (and the `this' keyword) may not appear
28008 in a default argument. */
28009 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
28010 parser
->local_variables_forbidden_p
= true;
28012 push_defarg_context (fn
);
28014 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28015 parmdecl
= DECL_ARGUMENTS (fn
);
28016 parm
&& parm
!= void_list_node
;
28017 parm
= TREE_CHAIN (parm
),
28018 parmdecl
= DECL_CHAIN (parmdecl
))
28020 tree default_arg
= TREE_PURPOSE (parm
);
28022 vec
<tree
, va_gc
> *insts
;
28029 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28030 /* This can happen for a friend declaration for a function
28031 already declared with default arguments. */
28035 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28037 TREE_VALUE (parm
));
28038 TREE_PURPOSE (parm
) = parsed_arg
;
28040 /* Update any instantiations we've already created. */
28041 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28042 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28043 TREE_PURPOSE (copy
) = parsed_arg
;
28046 pop_defarg_context ();
28048 /* Make sure no default arg is missing. */
28049 check_default_args (fn
);
28051 /* Restore the state of local_variables_forbidden_p. */
28052 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28054 /* Restore the queue. */
28055 pop_unparsed_function_queues (parser
);
28058 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28060 sizeof ... ( identifier )
28062 where the 'sizeof' token has already been consumed. */
28065 cp_parser_sizeof_pack (cp_parser
*parser
)
28067 /* Consume the `...'. */
28068 cp_lexer_consume_token (parser
->lexer
);
28069 maybe_warn_variadic_templates ();
28071 matching_parens parens
;
28072 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28074 parens
.consume_open (parser
);
28076 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28077 "%<sizeof...%> argument must be surrounded by parentheses");
28079 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28080 tree name
= cp_parser_identifier (parser
);
28081 if (name
== error_mark_node
)
28082 return error_mark_node
;
28083 /* The name is not qualified. */
28084 parser
->scope
= NULL_TREE
;
28085 parser
->qualifying_scope
= NULL_TREE
;
28086 parser
->object_scope
= NULL_TREE
;
28087 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28088 if (expr
== error_mark_node
)
28089 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28091 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28092 expr
= TREE_TYPE (expr
);
28093 else if (TREE_CODE (expr
) == CONST_DECL
)
28094 expr
= DECL_INITIAL (expr
);
28095 expr
= make_pack_expansion (expr
);
28096 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28099 parens
.require_close (parser
);
28104 /* Parse the operand of `sizeof' (or a similar operator). Returns
28105 either a TYPE or an expression, depending on the form of the
28106 input. The KEYWORD indicates which kind of expression we have
28110 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28112 tree expr
= NULL_TREE
;
28113 const char *saved_message
;
28115 bool saved_integral_constant_expression_p
;
28116 bool saved_non_integral_constant_expression_p
;
28118 /* If it's a `...', then we are computing the length of a parameter
28120 if (keyword
== RID_SIZEOF
28121 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28122 return cp_parser_sizeof_pack (parser
);
28124 /* Types cannot be defined in a `sizeof' expression. Save away the
28126 saved_message
= parser
->type_definition_forbidden_message
;
28127 /* And create the new one. */
28128 tmp
= concat ("types may not be defined in %<",
28129 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28130 "%> expressions", NULL
);
28131 parser
->type_definition_forbidden_message
= tmp
;
28133 /* The restrictions on constant-expressions do not apply inside
28134 sizeof expressions. */
28135 saved_integral_constant_expression_p
28136 = parser
->integral_constant_expression_p
;
28137 saved_non_integral_constant_expression_p
28138 = parser
->non_integral_constant_expression_p
;
28139 parser
->integral_constant_expression_p
= false;
28141 /* Do not actually evaluate the expression. */
28142 ++cp_unevaluated_operand
;
28143 ++c_inhibit_evaluation_warnings
;
28144 /* If it's a `(', then we might be looking at the type-id
28146 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28148 tree type
= NULL_TREE
;
28150 /* We can't be sure yet whether we're looking at a type-id or an
28152 cp_parser_parse_tentatively (parser
);
28154 matching_parens parens
;
28155 parens
.consume_open (parser
);
28157 /* Note: as a GNU Extension, compound literals are considered
28158 postfix-expressions as they are in C99, so they are valid
28159 arguments to sizeof. See comment in cp_parser_cast_expression
28161 if (cp_parser_compound_literal_p (parser
))
28162 cp_parser_simulate_error (parser
);
28165 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28166 parser
->in_type_id_in_expr_p
= true;
28167 /* Look for the type-id. */
28168 type
= cp_parser_type_id (parser
);
28169 /* Look for the closing `)'. */
28170 parens
.require_close (parser
);
28171 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28174 /* If all went well, then we're done. */
28175 if (cp_parser_parse_definitely (parser
))
28177 cp_decl_specifier_seq decl_specs
;
28179 /* Build a trivial decl-specifier-seq. */
28180 clear_decl_specs (&decl_specs
);
28181 decl_specs
.type
= type
;
28183 /* Call grokdeclarator to figure out what type this is. */
28184 expr
= grokdeclarator (NULL
,
28188 /*attrlist=*/NULL
);
28192 /* If the type-id production did not work out, then we must be
28193 looking at the unary-expression production. */
28195 expr
= cp_parser_unary_expression (parser
);
28197 /* Go back to evaluating expressions. */
28198 --cp_unevaluated_operand
;
28199 --c_inhibit_evaluation_warnings
;
28201 /* Free the message we created. */
28203 /* And restore the old one. */
28204 parser
->type_definition_forbidden_message
= saved_message
;
28205 parser
->integral_constant_expression_p
28206 = saved_integral_constant_expression_p
;
28207 parser
->non_integral_constant_expression_p
28208 = saved_non_integral_constant_expression_p
;
28213 /* If the current declaration has no declarator, return true. */
28216 cp_parser_declares_only_class_p (cp_parser
*parser
)
28218 /* If the next token is a `;' or a `,' then there is no
28220 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28221 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28224 /* Update the DECL_SPECS to reflect the storage class indicated by
28228 cp_parser_set_storage_class (cp_parser
*parser
,
28229 cp_decl_specifier_seq
*decl_specs
,
28233 cp_storage_class storage_class
;
28235 if (parser
->in_unbraced_linkage_specification_p
)
28237 error_at (token
->location
, "invalid use of %qD in linkage specification",
28238 ridpointers
[keyword
]);
28241 else if (decl_specs
->storage_class
!= sc_none
)
28243 decl_specs
->conflicting_specifiers_p
= true;
28247 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28248 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28249 && decl_specs
->gnu_thread_keyword_p
)
28251 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28252 "%<__thread%> before %qD", ridpointers
[keyword
]);
28258 storage_class
= sc_auto
;
28261 storage_class
= sc_register
;
28264 storage_class
= sc_static
;
28267 storage_class
= sc_extern
;
28270 storage_class
= sc_mutable
;
28273 gcc_unreachable ();
28275 decl_specs
->storage_class
= storage_class
;
28276 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28278 /* A storage class specifier cannot be applied alongside a typedef
28279 specifier. If there is a typedef specifier present then set
28280 conflicting_specifiers_p which will trigger an error later
28281 on in grokdeclarator. */
28282 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28283 decl_specs
->conflicting_specifiers_p
= true;
28286 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28287 is true, the type is a class or enum definition. */
28290 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28293 bool type_definition_p
)
28295 decl_specs
->any_specifiers_p
= true;
28297 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28298 (with, for example, in "typedef int wchar_t;") we remember that
28299 this is what happened. In system headers, we ignore these
28300 declarations so that G++ can work with system headers that are not
28302 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28303 && !type_definition_p
28304 && (type_spec
== boolean_type_node
28305 || type_spec
== char16_type_node
28306 || type_spec
== char32_type_node
28307 || type_spec
== wchar_type_node
)
28308 && (decl_specs
->type
28309 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28310 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28311 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28312 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28314 decl_specs
->redefined_builtin_type
= type_spec
;
28315 set_and_check_decl_spec_loc (decl_specs
,
28316 ds_redefined_builtin_type_spec
,
28318 if (!decl_specs
->type
)
28320 decl_specs
->type
= type_spec
;
28321 decl_specs
->type_definition_p
= false;
28322 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28325 else if (decl_specs
->type
)
28326 decl_specs
->multiple_types_p
= true;
28329 decl_specs
->type
= type_spec
;
28330 decl_specs
->type_definition_p
= type_definition_p
;
28331 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28332 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28336 /* True iff TOKEN is the GNU keyword __thread. */
28339 token_is__thread (cp_token
*token
)
28341 gcc_assert (token
->keyword
== RID_THREAD
);
28342 return id_equal (token
->u
.value
, "__thread");
28345 /* Set the location for a declarator specifier and check if it is
28348 DECL_SPECS is the sequence of declarator specifiers onto which to
28351 DS is the single declarator specifier to set which location is to
28352 be set onto the existing sequence of declarators.
28354 LOCATION is the location for the declarator specifier to
28358 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28359 cp_decl_spec ds
, cp_token
*token
)
28361 gcc_assert (ds
< ds_last
);
28363 if (decl_specs
== NULL
)
28366 source_location location
= token
->location
;
28368 if (decl_specs
->locations
[ds
] == 0)
28370 decl_specs
->locations
[ds
] = location
;
28371 if (ds
== ds_thread
)
28372 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28378 if (decl_specs
->locations
[ds_long_long
] != 0)
28379 error_at (location
,
28380 "%<long long long%> is too long for GCC");
28383 decl_specs
->locations
[ds_long_long
] = location
;
28384 pedwarn_cxx98 (location
,
28386 "ISO C++ 1998 does not support %<long long%>");
28389 else if (ds
== ds_thread
)
28391 bool gnu
= token_is__thread (token
);
28392 gcc_rich_location
richloc (location
);
28393 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28395 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
28396 error_at (&richloc
,
28397 "both %<__thread%> and %<thread_local%> specified");
28401 richloc
.add_fixit_remove ();
28402 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28407 static const char *const decl_spec_names
[] = {
28424 gcc_rich_location
richloc (location
);
28425 richloc
.add_fixit_remove ();
28426 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28431 /* Return true iff the declarator specifier DS is present in the
28432 sequence of declarator specifiers DECL_SPECS. */
28435 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28438 gcc_assert (ds
< ds_last
);
28440 if (decl_specs
== NULL
)
28443 return decl_specs
->locations
[ds
] != 0;
28446 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28447 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28450 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28452 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28455 /* Issue an error message indicating that TOKEN_DESC was expected.
28456 If KEYWORD is true, it indicated this function is called by
28457 cp_parser_require_keword and the required token can only be
28458 a indicated keyword.
28460 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28461 within any error as the location of an "opening" token matching
28462 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28463 RT_CLOSE_PAREN). */
28466 cp_parser_required_error (cp_parser
*parser
,
28467 required_token token_desc
,
28469 location_t matching_location
)
28471 if (cp_parser_simulate_error (parser
))
28474 const char *gmsgid
= NULL
;
28475 switch (token_desc
)
28478 gmsgid
= G_("expected %<new%>");
28481 gmsgid
= G_("expected %<delete%>");
28484 gmsgid
= G_("expected %<return%>");
28487 gmsgid
= G_("expected %<while%>");
28490 gmsgid
= G_("expected %<extern%>");
28492 case RT_STATIC_ASSERT
:
28493 gmsgid
= G_("expected %<static_assert%>");
28496 gmsgid
= G_("expected %<decltype%>");
28499 gmsgid
= G_("expected %<operator%>");
28502 gmsgid
= G_("expected %<class%>");
28505 gmsgid
= G_("expected %<template%>");
28508 gmsgid
= G_("expected %<namespace%>");
28511 gmsgid
= G_("expected %<using%>");
28514 gmsgid
= G_("expected %<asm%>");
28517 gmsgid
= G_("expected %<try%>");
28520 gmsgid
= G_("expected %<catch%>");
28523 gmsgid
= G_("expected %<throw%>");
28526 gmsgid
= G_("expected %<__label__%>");
28529 gmsgid
= G_("expected %<@try%>");
28531 case RT_AT_SYNCHRONIZED
:
28532 gmsgid
= G_("expected %<@synchronized%>");
28535 gmsgid
= G_("expected %<@throw%>");
28537 case RT_TRANSACTION_ATOMIC
:
28538 gmsgid
= G_("expected %<__transaction_atomic%>");
28540 case RT_TRANSACTION_RELAXED
:
28541 gmsgid
= G_("expected %<__transaction_relaxed%>");
28547 if (!gmsgid
&& !keyword
)
28549 switch (token_desc
)
28552 gmsgid
= G_("expected %<;%>");
28554 case RT_OPEN_PAREN
:
28555 gmsgid
= G_("expected %<(%>");
28557 case RT_CLOSE_BRACE
:
28558 gmsgid
= G_("expected %<}%>");
28560 case RT_OPEN_BRACE
:
28561 gmsgid
= G_("expected %<{%>");
28563 case RT_CLOSE_SQUARE
:
28564 gmsgid
= G_("expected %<]%>");
28566 case RT_OPEN_SQUARE
:
28567 gmsgid
= G_("expected %<[%>");
28570 gmsgid
= G_("expected %<,%>");
28573 gmsgid
= G_("expected %<::%>");
28576 gmsgid
= G_("expected %<<%>");
28579 gmsgid
= G_("expected %<>%>");
28582 gmsgid
= G_("expected %<=%>");
28585 gmsgid
= G_("expected %<...%>");
28588 gmsgid
= G_("expected %<*%>");
28591 gmsgid
= G_("expected %<~%>");
28594 gmsgid
= G_("expected %<:%>");
28596 case RT_COLON_SCOPE
:
28597 gmsgid
= G_("expected %<:%> or %<::%>");
28599 case RT_CLOSE_PAREN
:
28600 gmsgid
= G_("expected %<)%>");
28602 case RT_COMMA_CLOSE_PAREN
:
28603 gmsgid
= G_("expected %<,%> or %<)%>");
28605 case RT_PRAGMA_EOL
:
28606 gmsgid
= G_("expected end of line");
28609 gmsgid
= G_("expected identifier");
28612 gmsgid
= G_("expected selection-statement");
28615 gmsgid
= G_("expected iteration-statement");
28618 gmsgid
= G_("expected jump-statement");
28621 gmsgid
= G_("expected class-key");
28623 case RT_CLASS_TYPENAME_TEMPLATE
:
28624 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28627 gcc_unreachable ();
28632 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28636 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28637 issue an error message indicating that TOKEN_DESC was expected.
28639 Returns the token consumed, if the token had the appropriate type.
28640 Otherwise, returns NULL.
28642 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28643 within any error as the location of an "opening" token matching
28644 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28645 RT_CLOSE_PAREN). */
28648 cp_parser_require (cp_parser
* parser
,
28649 enum cpp_ttype type
,
28650 required_token token_desc
,
28651 location_t matching_location
)
28653 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28654 return cp_lexer_consume_token (parser
->lexer
);
28657 /* Output the MESSAGE -- unless we're parsing tentatively. */
28658 if (!cp_parser_simulate_error (parser
))
28659 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28660 matching_location
);
28665 /* An error message is produced if the next token is not '>'.
28666 All further tokens are skipped until the desired token is
28667 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28670 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28672 /* Current level of '< ... >'. */
28673 unsigned level
= 0;
28674 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28675 unsigned nesting_depth
= 0;
28677 /* Are we ready, yet? If not, issue error message. */
28678 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28681 /* Skip tokens until the desired token is found. */
28684 /* Peek at the next token. */
28685 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28688 if (!nesting_depth
)
28693 if (cxx_dialect
== cxx98
)
28694 /* C++0x views the `>>' operator as two `>' tokens, but
28697 else if (!nesting_depth
&& level
-- == 0)
28699 /* We've hit a `>>' where the first `>' closes the
28700 template argument list, and the second `>' is
28701 spurious. Just consume the `>>' and stop; we've
28702 already produced at least one error. */
28703 cp_lexer_consume_token (parser
->lexer
);
28706 /* Fall through for C++0x, so we handle the second `>' in
28708 gcc_fallthrough ();
28711 if (!nesting_depth
&& level
-- == 0)
28713 /* We've reached the token we want, consume it and stop. */
28714 cp_lexer_consume_token (parser
->lexer
);
28719 case CPP_OPEN_PAREN
:
28720 case CPP_OPEN_SQUARE
:
28724 case CPP_CLOSE_PAREN
:
28725 case CPP_CLOSE_SQUARE
:
28726 if (nesting_depth
-- == 0)
28731 case CPP_PRAGMA_EOL
:
28732 case CPP_SEMICOLON
:
28733 case CPP_OPEN_BRACE
:
28734 case CPP_CLOSE_BRACE
:
28735 /* The '>' was probably forgotten, don't look further. */
28742 /* Consume this token. */
28743 cp_lexer_consume_token (parser
->lexer
);
28747 /* If the next token is the indicated keyword, consume it. Otherwise,
28748 issue an error message indicating that TOKEN_DESC was expected.
28750 Returns the token consumed, if the token had the appropriate type.
28751 Otherwise, returns NULL. */
28754 cp_parser_require_keyword (cp_parser
* parser
,
28756 required_token token_desc
)
28758 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28760 if (token
&& token
->keyword
!= keyword
)
28762 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28770 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28771 function-definition. */
28774 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28776 return (/* An ordinary function-body begins with an `{'. */
28777 token
->type
== CPP_OPEN_BRACE
28778 /* A ctor-initializer begins with a `:'. */
28779 || token
->type
== CPP_COLON
28780 /* A function-try-block begins with `try'. */
28781 || token
->keyword
== RID_TRY
28782 /* A function-transaction-block begins with `__transaction_atomic'
28783 or `__transaction_relaxed'. */
28784 || token
->keyword
== RID_TRANSACTION_ATOMIC
28785 || token
->keyword
== RID_TRANSACTION_RELAXED
28786 /* The named return value extension begins with `return'. */
28787 || token
->keyword
== RID_RETURN
);
28790 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28794 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28798 token
= cp_lexer_peek_token (parser
->lexer
);
28799 return (token
->type
== CPP_OPEN_BRACE
28800 || (token
->type
== CPP_COLON
28801 && !parser
->colon_doesnt_start_class_def_p
));
28804 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28805 C++0x) ending a template-argument. */
28808 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28812 token
= cp_lexer_peek_token (parser
->lexer
);
28813 return (token
->type
== CPP_COMMA
28814 || token
->type
== CPP_GREATER
28815 || token
->type
== CPP_ELLIPSIS
28816 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28819 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28820 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28823 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28828 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28829 if (token
->type
== CPP_LESS
)
28831 /* Check for the sequence `<::' in the original code. It would be lexed as
28832 `[:', where `[' is a digraph, and there is no whitespace before
28834 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28837 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28838 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28844 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28845 or none_type otherwise. */
28847 static enum tag_types
28848 cp_parser_token_is_class_key (cp_token
* token
)
28850 switch (token
->keyword
)
28855 return record_type
;
28864 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28865 or none_type otherwise or if the token is null. */
28867 static enum tag_types
28868 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28873 switch (token
->keyword
)
28878 return typename_type
;
28885 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28888 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28890 if (type
== error_mark_node
)
28892 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28894 if (permerror (input_location
, "%qs tag used in naming %q#T",
28895 class_key
== union_type
? "union"
28896 : class_key
== record_type
? "struct" : "class",
28898 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28899 "%q#T was previously declared here", type
);
28903 /* Issue an error message if DECL is redeclared with different
28904 access than its original declaration [class.access.spec/3].
28905 This applies to nested classes, nested class templates and
28906 enumerations [class.mem/1]. */
28909 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28912 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28913 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28916 if ((TREE_PRIVATE (decl
)
28917 != (current_access_specifier
== access_private_node
))
28918 || (TREE_PROTECTED (decl
)
28919 != (current_access_specifier
== access_protected_node
)))
28920 error_at (location
, "%qD redeclared with different access", decl
);
28923 /* Look for the `template' keyword, as a syntactic disambiguator.
28924 Return TRUE iff it is present, in which case it will be
28928 cp_parser_optional_template_keyword (cp_parser
*parser
)
28930 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28932 /* In C++98 the `template' keyword can only be used within templates;
28933 outside templates the parser can always figure out what is a
28934 template and what is not. In C++11, per the resolution of DR 468,
28935 `template' is allowed in cases where it is not strictly necessary. */
28936 if (!processing_template_decl
28937 && pedantic
&& cxx_dialect
== cxx98
)
28939 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28940 pedwarn (token
->location
, OPT_Wpedantic
,
28941 "in C++98 %<template%> (as a disambiguator) is only "
28942 "allowed within templates");
28943 /* If this part of the token stream is rescanned, the same
28944 error message would be generated. So, we purge the token
28945 from the stream. */
28946 cp_lexer_purge_token (parser
->lexer
);
28951 /* Consume the `template' keyword. */
28952 cp_lexer_consume_token (parser
->lexer
);
28959 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28960 set PARSER->SCOPE, and perform other related actions. */
28963 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28965 struct tree_check
*check_value
;
28967 /* Get the stored value. */
28968 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28969 /* Set the scope from the stored value. */
28970 parser
->scope
= saved_checks_value (check_value
);
28971 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28972 parser
->object_scope
= NULL_TREE
;
28975 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28976 encounter the end of a block before what we were looking for. */
28979 cp_parser_cache_group (cp_parser
*parser
,
28980 enum cpp_ttype end
,
28985 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28987 /* Abort a parenthesized expression if we encounter a semicolon. */
28988 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28989 && token
->type
== CPP_SEMICOLON
)
28991 /* If we've reached the end of the file, stop. */
28992 if (token
->type
== CPP_EOF
28993 || (end
!= CPP_PRAGMA_EOL
28994 && token
->type
== CPP_PRAGMA_EOL
))
28996 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28997 /* We've hit the end of an enclosing block, so there's been some
28998 kind of syntax error. */
29001 /* Consume the token. */
29002 cp_lexer_consume_token (parser
->lexer
);
29003 /* See if it starts a new group. */
29004 if (token
->type
== CPP_OPEN_BRACE
)
29006 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
29007 /* In theory this should probably check end == '}', but
29008 cp_parser_save_member_function_body needs it to exit
29009 after either '}' or ')' when called with ')'. */
29013 else if (token
->type
== CPP_OPEN_PAREN
)
29015 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29016 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29019 else if (token
->type
== CPP_PRAGMA
)
29020 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29021 else if (token
->type
== end
)
29026 /* Like above, for caching a default argument or NSDMI. Both of these are
29027 terminated by a non-nested comma, but it can be unclear whether or not a
29028 comma is nested in a template argument list unless we do more parsing.
29029 In order to handle this ambiguity, when we encounter a ',' after a '<'
29030 we try to parse what follows as a parameter-declaration-list (in the
29031 case of a default argument) or a member-declarator (in the case of an
29032 NSDMI). If that succeeds, then we stop caching. */
29035 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29037 unsigned depth
= 0;
29038 int maybe_template_id
= 0;
29039 cp_token
*first_token
;
29041 tree default_argument
;
29043 /* Add tokens until we have processed the entire default
29044 argument. We add the range [first_token, token). */
29045 first_token
= cp_lexer_peek_token (parser
->lexer
);
29046 if (first_token
->type
== CPP_OPEN_BRACE
)
29048 /* For list-initialization, this is straightforward. */
29049 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29050 token
= cp_lexer_peek_token (parser
->lexer
);
29056 /* Peek at the next token. */
29057 token
= cp_lexer_peek_token (parser
->lexer
);
29058 /* What we do depends on what token we have. */
29059 switch (token
->type
)
29061 /* In valid code, a default argument must be
29062 immediately followed by a `,' `)', or `...'. */
29064 if (depth
== 0 && maybe_template_id
)
29066 /* If we've seen a '<', we might be in a
29067 template-argument-list. Until Core issue 325 is
29068 resolved, we don't know how this situation ought
29069 to be handled, so try to DTRT. We check whether
29070 what comes after the comma is a valid parameter
29071 declaration list. If it is, then the comma ends
29072 the default argument; otherwise the default
29073 argument continues. */
29074 bool error
= false;
29077 /* Set ITALP so cp_parser_parameter_declaration_list
29078 doesn't decide to commit to this parse. */
29079 bool saved_italp
= parser
->in_template_argument_list_p
;
29080 parser
->in_template_argument_list_p
= true;
29082 cp_parser_parse_tentatively (parser
);
29086 /* Parse declarators until we reach a non-comma or
29087 somthing that cannot be an initializer.
29088 Just checking whether we're looking at a single
29089 declarator is insufficient. Consider:
29090 int var = tuple<T,U>::x;
29091 The template parameter 'U' looks exactly like a
29095 int ctor_dtor_or_conv_p
;
29096 cp_lexer_consume_token (parser
->lexer
);
29097 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29098 &ctor_dtor_or_conv_p
,
29099 /*parenthesized_p=*/NULL
,
29101 /*friend_p=*/false);
29102 peek
= cp_lexer_peek_token (parser
->lexer
);
29103 if (cp_parser_error_occurred (parser
))
29106 while (peek
->type
== CPP_COMMA
);
29107 /* If we met an '=' or ';' then the original comma
29108 was the end of the NSDMI. Otherwise assume
29109 we're still in the NSDMI. */
29110 error
= (peek
->type
!= CPP_EQ
29111 && peek
->type
!= CPP_SEMICOLON
);
29115 cp_lexer_consume_token (parser
->lexer
);
29116 begin_scope (sk_function_parms
, NULL_TREE
);
29117 if (cp_parser_parameter_declaration_list (parser
)
29118 == error_mark_node
)
29120 pop_bindings_and_leave_scope ();
29122 if (!cp_parser_error_occurred (parser
) && !error
)
29124 cp_parser_abort_tentative_parse (parser
);
29126 parser
->in_template_argument_list_p
= saved_italp
;
29130 case CPP_CLOSE_PAREN
:
29132 /* If we run into a non-nested `;', `}', or `]',
29133 then the code is invalid -- but the default
29134 argument is certainly over. */
29135 case CPP_SEMICOLON
:
29136 case CPP_CLOSE_BRACE
:
29137 case CPP_CLOSE_SQUARE
:
29139 /* Handle correctly int n = sizeof ... ( p ); */
29140 && token
->type
!= CPP_ELLIPSIS
)
29142 /* Update DEPTH, if necessary. */
29143 else if (token
->type
== CPP_CLOSE_PAREN
29144 || token
->type
== CPP_CLOSE_BRACE
29145 || token
->type
== CPP_CLOSE_SQUARE
)
29149 case CPP_OPEN_PAREN
:
29150 case CPP_OPEN_SQUARE
:
29151 case CPP_OPEN_BRACE
:
29157 /* This might be the comparison operator, or it might
29158 start a template argument list. */
29159 ++maybe_template_id
;
29163 if (cxx_dialect
== cxx98
)
29165 /* Fall through for C++0x, which treats the `>>'
29166 operator like two `>' tokens in certain
29168 gcc_fallthrough ();
29173 /* This might be an operator, or it might close a
29174 template argument list. But if a previous '<'
29175 started a template argument list, this will have
29176 closed it, so we can't be in one anymore. */
29177 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29178 if (maybe_template_id
< 0)
29179 maybe_template_id
= 0;
29183 /* If we run out of tokens, issue an error message. */
29185 case CPP_PRAGMA_EOL
:
29186 error_at (token
->location
, "file ends in default argument");
29187 return error_mark_node
;
29191 /* In these cases, we should look for template-ids.
29192 For example, if the default argument is
29193 `X<int, double>()', we need to do name lookup to
29194 figure out whether or not `X' is a template; if
29195 so, the `,' does not end the default argument.
29197 That is not yet done. */
29204 /* If we've reached the end, stop. */
29208 /* Add the token to the token block. */
29209 token
= cp_lexer_consume_token (parser
->lexer
);
29212 /* Create a DEFAULT_ARG to represent the unparsed default
29214 default_argument
= make_node (DEFAULT_ARG
);
29215 DEFARG_TOKENS (default_argument
)
29216 = cp_token_cache_new (first_token
, token
);
29217 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29219 return default_argument
;
29222 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29225 defarg_location (tree default_argument
)
29227 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29228 location_t start
= tokens
->first
->location
;
29229 location_t end
= tokens
->last
->location
;
29230 return make_location (start
, start
, end
);
29233 /* Begin parsing tentatively. We always save tokens while parsing
29234 tentatively so that if the tentative parsing fails we can restore the
29238 cp_parser_parse_tentatively (cp_parser
* parser
)
29240 /* Enter a new parsing context. */
29241 parser
->context
= cp_parser_context_new (parser
->context
);
29242 /* Begin saving tokens. */
29243 cp_lexer_save_tokens (parser
->lexer
);
29244 /* In order to avoid repetitive access control error messages,
29245 access checks are queued up until we are no longer parsing
29247 push_deferring_access_checks (dk_deferred
);
29250 /* Commit to the currently active tentative parse. */
29253 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29255 cp_parser_context
*context
;
29258 /* Mark all of the levels as committed. */
29259 lexer
= parser
->lexer
;
29260 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29262 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29264 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29265 while (!cp_lexer_saving_tokens (lexer
))
29266 lexer
= lexer
->next
;
29267 cp_lexer_commit_tokens (lexer
);
29271 /* Commit to the topmost currently active tentative parse.
29273 Note that this function shouldn't be called when there are
29274 irreversible side-effects while in a tentative state. For
29275 example, we shouldn't create a permanent entry in the symbol
29276 table, or issue an error message that might not apply if the
29277 tentative parse is aborted. */
29280 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29282 cp_parser_context
*context
= parser
->context
;
29283 cp_lexer
*lexer
= parser
->lexer
;
29287 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29289 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29291 while (!cp_lexer_saving_tokens (lexer
))
29292 lexer
= lexer
->next
;
29293 cp_lexer_commit_tokens (lexer
);
29297 /* Abort the currently active tentative parse. All consumed tokens
29298 will be rolled back, and no diagnostics will be issued. */
29301 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29303 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29304 || errorcount
> 0);
29305 cp_parser_simulate_error (parser
);
29306 /* Now, pretend that we want to see if the construct was
29307 successfully parsed. */
29308 cp_parser_parse_definitely (parser
);
29311 /* Stop parsing tentatively. If a parse error has occurred, restore the
29312 token stream. Otherwise, commit to the tokens we have consumed.
29313 Returns true if no error occurred; false otherwise. */
29316 cp_parser_parse_definitely (cp_parser
* parser
)
29318 bool error_occurred
;
29319 cp_parser_context
*context
;
29321 /* Remember whether or not an error occurred, since we are about to
29322 destroy that information. */
29323 error_occurred
= cp_parser_error_occurred (parser
);
29324 /* Remove the topmost context from the stack. */
29325 context
= parser
->context
;
29326 parser
->context
= context
->next
;
29327 /* If no parse errors occurred, commit to the tentative parse. */
29328 if (!error_occurred
)
29330 /* Commit to the tokens read tentatively, unless that was
29332 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29333 cp_lexer_commit_tokens (parser
->lexer
);
29335 pop_to_parent_deferring_access_checks ();
29337 /* Otherwise, if errors occurred, roll back our state so that things
29338 are just as they were before we began the tentative parse. */
29341 cp_lexer_rollback_tokens (parser
->lexer
);
29342 pop_deferring_access_checks ();
29344 /* Add the context to the front of the free list. */
29345 context
->next
= cp_parser_context_free_list
;
29346 cp_parser_context_free_list
= context
;
29348 return !error_occurred
;
29351 /* Returns true if we are parsing tentatively and are not committed to
29352 this tentative parse. */
29355 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29357 return (cp_parser_parsing_tentatively (parser
)
29358 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29361 /* Returns nonzero iff an error has occurred during the most recent
29362 tentative parse. */
29365 cp_parser_error_occurred (cp_parser
* parser
)
29367 return (cp_parser_parsing_tentatively (parser
)
29368 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29371 /* Returns nonzero if GNU extensions are allowed. */
29374 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29376 return parser
->allow_gnu_extensions_p
;
29379 /* Objective-C++ Productions */
29382 /* Parse an Objective-C expression, which feeds into a primary-expression
29386 objc-message-expression
29387 objc-string-literal
29388 objc-encode-expression
29389 objc-protocol-expression
29390 objc-selector-expression
29392 Returns a tree representation of the expression. */
29395 cp_parser_objc_expression (cp_parser
* parser
)
29397 /* Try to figure out what kind of declaration is present. */
29398 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29402 case CPP_OPEN_SQUARE
:
29403 return cp_parser_objc_message_expression (parser
);
29405 case CPP_OBJC_STRING
:
29406 kwd
= cp_lexer_consume_token (parser
->lexer
);
29407 return objc_build_string_object (kwd
->u
.value
);
29410 switch (kwd
->keyword
)
29412 case RID_AT_ENCODE
:
29413 return cp_parser_objc_encode_expression (parser
);
29415 case RID_AT_PROTOCOL
:
29416 return cp_parser_objc_protocol_expression (parser
);
29418 case RID_AT_SELECTOR
:
29419 return cp_parser_objc_selector_expression (parser
);
29426 error_at (kwd
->location
,
29427 "misplaced %<@%D%> Objective-C++ construct",
29429 cp_parser_skip_to_end_of_block_or_statement (parser
);
29432 return error_mark_node
;
29435 /* Parse an Objective-C message expression.
29437 objc-message-expression:
29438 [ objc-message-receiver objc-message-args ]
29440 Returns a representation of an Objective-C message. */
29443 cp_parser_objc_message_expression (cp_parser
* parser
)
29445 tree receiver
, messageargs
;
29447 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29448 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29449 receiver
= cp_parser_objc_message_receiver (parser
);
29450 messageargs
= cp_parser_objc_message_args (parser
);
29451 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29452 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29454 tree result
= objc_build_message_expr (receiver
, messageargs
);
29456 /* Construct a location e.g.
29459 ranging from the '[' to the ']', with the caret at the start. */
29460 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29461 protected_set_expr_location (result
, combined_loc
);
29466 /* Parse an objc-message-receiver.
29468 objc-message-receiver:
29470 simple-type-specifier
29472 Returns a representation of the type or expression. */
29475 cp_parser_objc_message_receiver (cp_parser
* parser
)
29479 /* An Objective-C message receiver may be either (1) a type
29480 or (2) an expression. */
29481 cp_parser_parse_tentatively (parser
);
29482 rcv
= cp_parser_expression (parser
);
29484 /* If that worked out, fine. */
29485 if (cp_parser_parse_definitely (parser
))
29488 cp_parser_parse_tentatively (parser
);
29489 rcv
= cp_parser_simple_type_specifier (parser
,
29490 /*decl_specs=*/NULL
,
29491 CP_PARSER_FLAGS_NONE
);
29493 if (cp_parser_parse_definitely (parser
))
29494 return objc_get_class_reference (rcv
);
29496 cp_parser_error (parser
, "objective-c++ message receiver expected");
29497 return error_mark_node
;
29500 /* Parse the arguments and selectors comprising an Objective-C message.
29505 objc-selector-args , objc-comma-args
29507 objc-selector-args:
29508 objc-selector [opt] : assignment-expression
29509 objc-selector-args objc-selector [opt] : assignment-expression
29512 assignment-expression
29513 objc-comma-args , assignment-expression
29515 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29516 selector arguments and TREE_VALUE containing a list of comma
29520 cp_parser_objc_message_args (cp_parser
* parser
)
29522 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29523 bool maybe_unary_selector_p
= true;
29524 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29526 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29528 tree selector
= NULL_TREE
, arg
;
29530 if (token
->type
!= CPP_COLON
)
29531 selector
= cp_parser_objc_selector (parser
);
29533 /* Detect if we have a unary selector. */
29534 if (maybe_unary_selector_p
29535 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29536 return build_tree_list (selector
, NULL_TREE
);
29538 maybe_unary_selector_p
= false;
29539 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29540 arg
= cp_parser_assignment_expression (parser
);
29543 = chainon (sel_args
,
29544 build_tree_list (selector
, arg
));
29546 token
= cp_lexer_peek_token (parser
->lexer
);
29549 /* Handle non-selector arguments, if any. */
29550 while (token
->type
== CPP_COMMA
)
29554 cp_lexer_consume_token (parser
->lexer
);
29555 arg
= cp_parser_assignment_expression (parser
);
29558 = chainon (addl_args
,
29559 build_tree_list (NULL_TREE
, arg
));
29561 token
= cp_lexer_peek_token (parser
->lexer
);
29564 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29566 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29567 return build_tree_list (error_mark_node
, error_mark_node
);
29570 return build_tree_list (sel_args
, addl_args
);
29573 /* Parse an Objective-C encode expression.
29575 objc-encode-expression:
29576 @encode objc-typename
29578 Returns an encoded representation of the type argument. */
29581 cp_parser_objc_encode_expression (cp_parser
* parser
)
29585 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29587 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29588 matching_parens parens
;
29589 parens
.require_open (parser
);
29590 token
= cp_lexer_peek_token (parser
->lexer
);
29591 type
= complete_type (cp_parser_type_id (parser
));
29592 parens
.require_close (parser
);
29596 error_at (token
->location
,
29597 "%<@encode%> must specify a type as an argument");
29598 return error_mark_node
;
29601 /* This happens if we find @encode(T) (where T is a template
29602 typename or something dependent on a template typename) when
29603 parsing a template. In that case, we can't compile it
29604 immediately, but we rather create an AT_ENCODE_EXPR which will
29605 need to be instantiated when the template is used.
29607 if (dependent_type_p (type
))
29609 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29610 TREE_READONLY (value
) = 1;
29615 /* Build a location of the form:
29618 with caret==start at the @ token, finishing at the close paren. */
29619 location_t combined_loc
29620 = make_location (start_loc
, start_loc
,
29621 cp_lexer_previous_token (parser
->lexer
)->location
);
29623 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29626 /* Parse an Objective-C @defs expression. */
29629 cp_parser_objc_defs_expression (cp_parser
*parser
)
29633 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29634 matching_parens parens
;
29635 parens
.require_open (parser
);
29636 name
= cp_parser_identifier (parser
);
29637 parens
.require_close (parser
);
29639 return objc_get_class_ivars (name
);
29642 /* Parse an Objective-C protocol expression.
29644 objc-protocol-expression:
29645 @protocol ( identifier )
29647 Returns a representation of the protocol expression. */
29650 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29653 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29655 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29656 matching_parens parens
;
29657 parens
.require_open (parser
);
29658 proto
= cp_parser_identifier (parser
);
29659 parens
.require_close (parser
);
29661 /* Build a location of the form:
29664 with caret==start at the @ token, finishing at the close paren. */
29665 location_t combined_loc
29666 = make_location (start_loc
, start_loc
,
29667 cp_lexer_previous_token (parser
->lexer
)->location
);
29668 tree result
= objc_build_protocol_expr (proto
);
29669 protected_set_expr_location (result
, combined_loc
);
29673 /* Parse an Objective-C selector expression.
29675 objc-selector-expression:
29676 @selector ( objc-method-signature )
29678 objc-method-signature:
29684 objc-selector-seq objc-selector :
29686 Returns a representation of the method selector. */
29689 cp_parser_objc_selector_expression (cp_parser
* parser
)
29691 tree sel_seq
= NULL_TREE
;
29692 bool maybe_unary_selector_p
= true;
29694 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29696 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29697 matching_parens parens
;
29698 parens
.require_open (parser
);
29699 token
= cp_lexer_peek_token (parser
->lexer
);
29701 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29702 || token
->type
== CPP_SCOPE
)
29704 tree selector
= NULL_TREE
;
29706 if (token
->type
!= CPP_COLON
29707 || token
->type
== CPP_SCOPE
)
29708 selector
= cp_parser_objc_selector (parser
);
29710 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29711 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29713 /* Detect if we have a unary selector. */
29714 if (maybe_unary_selector_p
)
29716 sel_seq
= selector
;
29717 goto finish_selector
;
29721 cp_parser_error (parser
, "expected %<:%>");
29724 maybe_unary_selector_p
= false;
29725 token
= cp_lexer_consume_token (parser
->lexer
);
29727 if (token
->type
== CPP_SCOPE
)
29730 = chainon (sel_seq
,
29731 build_tree_list (selector
, NULL_TREE
));
29733 = chainon (sel_seq
,
29734 build_tree_list (NULL_TREE
, NULL_TREE
));
29738 = chainon (sel_seq
,
29739 build_tree_list (selector
, NULL_TREE
));
29741 token
= cp_lexer_peek_token (parser
->lexer
);
29745 parens
.require_close (parser
);
29748 /* Build a location of the form:
29751 with caret==start at the @ token, finishing at the close paren. */
29752 location_t combined_loc
29753 = make_location (loc
, loc
,
29754 cp_lexer_previous_token (parser
->lexer
)->location
);
29755 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29756 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29757 protected_set_expr_location (result
, combined_loc
);
29761 /* Parse a list of identifiers.
29763 objc-identifier-list:
29765 objc-identifier-list , identifier
29767 Returns a TREE_LIST of identifier nodes. */
29770 cp_parser_objc_identifier_list (cp_parser
* parser
)
29776 identifier
= cp_parser_identifier (parser
);
29777 if (identifier
== error_mark_node
)
29778 return error_mark_node
;
29780 list
= build_tree_list (NULL_TREE
, identifier
);
29781 sep
= cp_lexer_peek_token (parser
->lexer
);
29783 while (sep
->type
== CPP_COMMA
)
29785 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29786 identifier
= cp_parser_identifier (parser
);
29787 if (identifier
== error_mark_node
)
29790 list
= chainon (list
, build_tree_list (NULL_TREE
,
29792 sep
= cp_lexer_peek_token (parser
->lexer
);
29798 /* Parse an Objective-C alias declaration.
29800 objc-alias-declaration:
29801 @compatibility_alias identifier identifier ;
29803 This function registers the alias mapping with the Objective-C front end.
29804 It returns nothing. */
29807 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29811 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29812 alias
= cp_parser_identifier (parser
);
29813 orig
= cp_parser_identifier (parser
);
29814 objc_declare_alias (alias
, orig
);
29815 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29818 /* Parse an Objective-C class forward-declaration.
29820 objc-class-declaration:
29821 @class objc-identifier-list ;
29823 The function registers the forward declarations with the Objective-C
29824 front end. It returns nothing. */
29827 cp_parser_objc_class_declaration (cp_parser
* parser
)
29829 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29834 id
= cp_parser_identifier (parser
);
29835 if (id
== error_mark_node
)
29838 objc_declare_class (id
);
29840 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29841 cp_lexer_consume_token (parser
->lexer
);
29845 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29848 /* Parse a list of Objective-C protocol references.
29850 objc-protocol-refs-opt:
29851 objc-protocol-refs [opt]
29853 objc-protocol-refs:
29854 < objc-identifier-list >
29856 Returns a TREE_LIST of identifiers, if any. */
29859 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29861 tree protorefs
= NULL_TREE
;
29863 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29865 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29866 protorefs
= cp_parser_objc_identifier_list (parser
);
29867 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29873 /* Parse a Objective-C visibility specification. */
29876 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29878 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29880 switch (vis
->keyword
)
29882 case RID_AT_PRIVATE
:
29883 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29885 case RID_AT_PROTECTED
:
29886 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29888 case RID_AT_PUBLIC
:
29889 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29891 case RID_AT_PACKAGE
:
29892 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29898 /* Eat '@private'/'@protected'/'@public'. */
29899 cp_lexer_consume_token (parser
->lexer
);
29902 /* Parse an Objective-C method type. Return 'true' if it is a class
29903 (+) method, and 'false' if it is an instance (-) method. */
29906 cp_parser_objc_method_type (cp_parser
* parser
)
29908 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29914 /* Parse an Objective-C protocol qualifier. */
29917 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29919 tree quals
= NULL_TREE
, node
;
29920 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29922 node
= token
->u
.value
;
29924 while (node
&& identifier_p (node
)
29925 && (node
== ridpointers
[(int) RID_IN
]
29926 || node
== ridpointers
[(int) RID_OUT
]
29927 || node
== ridpointers
[(int) RID_INOUT
]
29928 || node
== ridpointers
[(int) RID_BYCOPY
]
29929 || node
== ridpointers
[(int) RID_BYREF
]
29930 || node
== ridpointers
[(int) RID_ONEWAY
]))
29932 quals
= tree_cons (NULL_TREE
, node
, quals
);
29933 cp_lexer_consume_token (parser
->lexer
);
29934 token
= cp_lexer_peek_token (parser
->lexer
);
29935 node
= token
->u
.value
;
29941 /* Parse an Objective-C typename. */
29944 cp_parser_objc_typename (cp_parser
* parser
)
29946 tree type_name
= NULL_TREE
;
29948 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29950 tree proto_quals
, cp_type
= NULL_TREE
;
29952 matching_parens parens
;
29953 parens
.consume_open (parser
); /* Eat '('. */
29954 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29956 /* An ObjC type name may consist of just protocol qualifiers, in which
29957 case the type shall default to 'id'. */
29958 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29960 cp_type
= cp_parser_type_id (parser
);
29962 /* If the type could not be parsed, an error has already
29963 been produced. For error recovery, behave as if it had
29964 not been specified, which will use the default type
29966 if (cp_type
== error_mark_node
)
29968 cp_type
= NULL_TREE
;
29969 /* We need to skip to the closing parenthesis as
29970 cp_parser_type_id() does not seem to do it for
29972 cp_parser_skip_to_closing_parenthesis (parser
,
29973 /*recovering=*/true,
29974 /*or_comma=*/false,
29975 /*consume_paren=*/false);
29979 parens
.require_close (parser
);
29980 type_name
= build_tree_list (proto_quals
, cp_type
);
29986 /* Check to see if TYPE refers to an Objective-C selector name. */
29989 cp_parser_objc_selector_p (enum cpp_ttype type
)
29991 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29992 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29993 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29994 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29995 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29998 /* Parse an Objective-C selector. */
30001 cp_parser_objc_selector (cp_parser
* parser
)
30003 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30005 if (!cp_parser_objc_selector_p (token
->type
))
30007 error_at (token
->location
, "invalid Objective-C++ selector name");
30008 return error_mark_node
;
30011 /* C++ operator names are allowed to appear in ObjC selectors. */
30012 switch (token
->type
)
30014 case CPP_AND_AND
: return get_identifier ("and");
30015 case CPP_AND_EQ
: return get_identifier ("and_eq");
30016 case CPP_AND
: return get_identifier ("bitand");
30017 case CPP_OR
: return get_identifier ("bitor");
30018 case CPP_COMPL
: return get_identifier ("compl");
30019 case CPP_NOT
: return get_identifier ("not");
30020 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30021 case CPP_OR_OR
: return get_identifier ("or");
30022 case CPP_OR_EQ
: return get_identifier ("or_eq");
30023 case CPP_XOR
: return get_identifier ("xor");
30024 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30025 default: return token
->u
.value
;
30029 /* Parse an Objective-C params list. */
30032 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30034 tree params
= NULL_TREE
;
30035 bool maybe_unary_selector_p
= true;
30036 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30038 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30040 tree selector
= NULL_TREE
, type_name
, identifier
;
30041 tree parm_attr
= NULL_TREE
;
30043 if (token
->keyword
== RID_ATTRIBUTE
)
30046 if (token
->type
!= CPP_COLON
)
30047 selector
= cp_parser_objc_selector (parser
);
30049 /* Detect if we have a unary selector. */
30050 if (maybe_unary_selector_p
30051 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30053 params
= selector
; /* Might be followed by attributes. */
30057 maybe_unary_selector_p
= false;
30058 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30060 /* Something went quite wrong. There should be a colon
30061 here, but there is not. Stop parsing parameters. */
30064 type_name
= cp_parser_objc_typename (parser
);
30065 /* New ObjC allows attributes on parameters too. */
30066 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30067 parm_attr
= cp_parser_attributes_opt (parser
);
30068 identifier
= cp_parser_identifier (parser
);
30072 objc_build_keyword_decl (selector
,
30077 token
= cp_lexer_peek_token (parser
->lexer
);
30080 if (params
== NULL_TREE
)
30082 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30083 return error_mark_node
;
30086 /* We allow tail attributes for the method. */
30087 if (token
->keyword
== RID_ATTRIBUTE
)
30089 *attributes
= cp_parser_attributes_opt (parser
);
30090 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30091 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30093 cp_parser_error (parser
,
30094 "method attributes must be specified at the end");
30095 return error_mark_node
;
30098 if (params
== NULL_TREE
)
30100 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30101 return error_mark_node
;
30106 /* Parse the non-keyword Objective-C params. */
30109 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30112 tree params
= make_node (TREE_LIST
);
30113 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30114 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30116 while (token
->type
== CPP_COMMA
)
30118 cp_parameter_declarator
*parmdecl
;
30121 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30122 token
= cp_lexer_peek_token (parser
->lexer
);
30124 if (token
->type
== CPP_ELLIPSIS
)
30126 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30128 token
= cp_lexer_peek_token (parser
->lexer
);
30132 /* TODO: parse attributes for tail parameters. */
30133 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
30134 parm
= grokdeclarator (parmdecl
->declarator
,
30135 &parmdecl
->decl_specifiers
,
30136 PARM
, /*initialized=*/0,
30137 /*attrlist=*/NULL
);
30139 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30140 token
= cp_lexer_peek_token (parser
->lexer
);
30143 /* We allow tail attributes for the method. */
30144 if (token
->keyword
== RID_ATTRIBUTE
)
30146 if (*attributes
== NULL_TREE
)
30148 *attributes
= cp_parser_attributes_opt (parser
);
30149 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30150 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30154 /* We have an error, but parse the attributes, so that we can
30156 *attributes
= cp_parser_attributes_opt (parser
);
30158 cp_parser_error (parser
,
30159 "method attributes must be specified at the end");
30160 return error_mark_node
;
30166 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30169 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30171 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30173 /* If the next token is `extern' and the following token is a string
30174 literal, then we have a linkage specification. */
30175 if (token
->keyword
== RID_EXTERN
30176 && cp_parser_is_pure_string_literal
30177 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30178 cp_parser_linkage_specification (parser
);
30179 /* Handle #pragma, if any. */
30180 else if (token
->type
== CPP_PRAGMA
)
30181 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30182 /* Allow stray semicolons. */
30183 else if (token
->type
== CPP_SEMICOLON
)
30184 cp_lexer_consume_token (parser
->lexer
);
30185 /* Mark methods as optional or required, when building protocols. */
30186 else if (token
->keyword
== RID_AT_OPTIONAL
)
30188 cp_lexer_consume_token (parser
->lexer
);
30189 objc_set_method_opt (true);
30191 else if (token
->keyword
== RID_AT_REQUIRED
)
30193 cp_lexer_consume_token (parser
->lexer
);
30194 objc_set_method_opt (false);
30196 else if (token
->keyword
== RID_NAMESPACE
)
30197 cp_parser_namespace_definition (parser
);
30198 /* Other stray characters must generate errors. */
30199 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30201 cp_lexer_consume_token (parser
->lexer
);
30202 error ("stray %qs between Objective-C++ methods",
30203 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30205 /* Finally, try to parse a block-declaration, or a function-definition. */
30207 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30210 /* Parse a method signature. */
30213 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30215 tree rettype
, kwdparms
, optparms
;
30216 bool ellipsis
= false;
30217 bool is_class_method
;
30219 is_class_method
= cp_parser_objc_method_type (parser
);
30220 rettype
= cp_parser_objc_typename (parser
);
30221 *attributes
= NULL_TREE
;
30222 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30223 if (kwdparms
== error_mark_node
)
30224 return error_mark_node
;
30225 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30226 if (optparms
== error_mark_node
)
30227 return error_mark_node
;
30229 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30233 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30236 cp_lexer_save_tokens (parser
->lexer
);
30237 tattr
= cp_parser_attributes_opt (parser
);
30238 gcc_assert (tattr
) ;
30240 /* If the attributes are followed by a method introducer, this is not allowed.
30241 Dump the attributes and flag the situation. */
30242 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30243 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30246 /* Otherwise, the attributes introduce some interstitial code, possibly so
30247 rewind to allow that check. */
30248 cp_lexer_rollback_tokens (parser
->lexer
);
30252 /* Parse an Objective-C method prototype list. */
30255 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30257 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30259 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30261 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30263 tree attributes
, sig
;
30264 bool is_class_method
;
30265 if (token
->type
== CPP_PLUS
)
30266 is_class_method
= true;
30268 is_class_method
= false;
30269 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30270 if (sig
== error_mark_node
)
30272 cp_parser_skip_to_end_of_block_or_statement (parser
);
30273 token
= cp_lexer_peek_token (parser
->lexer
);
30276 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30277 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30279 else if (token
->keyword
== RID_AT_PROPERTY
)
30280 cp_parser_objc_at_property_declaration (parser
);
30281 else if (token
->keyword
== RID_ATTRIBUTE
30282 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30283 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30285 "prefix attributes are ignored for methods");
30287 /* Allow for interspersed non-ObjC++ code. */
30288 cp_parser_objc_interstitial_code (parser
);
30290 token
= cp_lexer_peek_token (parser
->lexer
);
30293 if (token
->type
!= CPP_EOF
)
30294 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30296 cp_parser_error (parser
, "expected %<@end%>");
30298 objc_finish_interface ();
30301 /* Parse an Objective-C method definition list. */
30304 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30306 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30308 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30312 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30315 tree sig
, attribute
;
30316 bool is_class_method
;
30317 if (token
->type
== CPP_PLUS
)
30318 is_class_method
= true;
30320 is_class_method
= false;
30321 push_deferring_access_checks (dk_deferred
);
30322 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30323 if (sig
== error_mark_node
)
30325 cp_parser_skip_to_end_of_block_or_statement (parser
);
30326 token
= cp_lexer_peek_token (parser
->lexer
);
30329 objc_start_method_definition (is_class_method
, sig
, attribute
,
30332 /* For historical reasons, we accept an optional semicolon. */
30333 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30334 cp_lexer_consume_token (parser
->lexer
);
30336 ptk
= cp_lexer_peek_token (parser
->lexer
);
30337 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30338 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30340 perform_deferred_access_checks (tf_warning_or_error
);
30341 stop_deferring_access_checks ();
30342 meth
= cp_parser_function_definition_after_declarator (parser
,
30344 pop_deferring_access_checks ();
30345 objc_finish_method_definition (meth
);
30348 /* The following case will be removed once @synthesize is
30349 completely implemented. */
30350 else if (token
->keyword
== RID_AT_PROPERTY
)
30351 cp_parser_objc_at_property_declaration (parser
);
30352 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30353 cp_parser_objc_at_synthesize_declaration (parser
);
30354 else if (token
->keyword
== RID_AT_DYNAMIC
)
30355 cp_parser_objc_at_dynamic_declaration (parser
);
30356 else if (token
->keyword
== RID_ATTRIBUTE
30357 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30358 warning_at (token
->location
, OPT_Wattributes
,
30359 "prefix attributes are ignored for methods");
30361 /* Allow for interspersed non-ObjC++ code. */
30362 cp_parser_objc_interstitial_code (parser
);
30364 token
= cp_lexer_peek_token (parser
->lexer
);
30367 if (token
->type
!= CPP_EOF
)
30368 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30370 cp_parser_error (parser
, "expected %<@end%>");
30372 objc_finish_implementation ();
30375 /* Parse Objective-C ivars. */
30378 cp_parser_objc_class_ivars (cp_parser
* parser
)
30380 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30382 if (token
->type
!= CPP_OPEN_BRACE
)
30383 return; /* No ivars specified. */
30385 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30386 token
= cp_lexer_peek_token (parser
->lexer
);
30388 while (token
->type
!= CPP_CLOSE_BRACE
30389 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30391 cp_decl_specifier_seq declspecs
;
30392 int decl_class_or_enum_p
;
30393 tree prefix_attributes
;
30395 cp_parser_objc_visibility_spec (parser
);
30397 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30400 cp_parser_decl_specifier_seq (parser
,
30401 CP_PARSER_FLAGS_OPTIONAL
,
30403 &decl_class_or_enum_p
);
30405 /* auto, register, static, extern, mutable. */
30406 if (declspecs
.storage_class
!= sc_none
)
30408 cp_parser_error (parser
, "invalid type for instance variable");
30409 declspecs
.storage_class
= sc_none
;
30412 /* thread_local. */
30413 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30415 cp_parser_error (parser
, "invalid type for instance variable");
30416 declspecs
.locations
[ds_thread
] = 0;
30420 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30422 cp_parser_error (parser
, "invalid type for instance variable");
30423 declspecs
.locations
[ds_typedef
] = 0;
30426 prefix_attributes
= declspecs
.attributes
;
30427 declspecs
.attributes
= NULL_TREE
;
30429 /* Keep going until we hit the `;' at the end of the
30431 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30433 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30434 cp_declarator
*declarator
= NULL
;
30435 int ctor_dtor_or_conv_p
;
30437 /* Check for a (possibly unnamed) bitfield declaration. */
30438 token
= cp_lexer_peek_token (parser
->lexer
);
30439 if (token
->type
== CPP_COLON
)
30442 if (token
->type
== CPP_NAME
30443 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30446 /* Get the name of the bitfield. */
30447 declarator
= make_id_declarator (NULL_TREE
,
30448 cp_parser_identifier (parser
),
30452 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30453 /* Get the width of the bitfield. */
30455 = cp_parser_constant_expression (parser
);
30459 /* Parse the declarator. */
30461 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30462 &ctor_dtor_or_conv_p
,
30463 /*parenthesized_p=*/NULL
,
30464 /*member_p=*/false,
30465 /*friend_p=*/false);
30468 /* Look for attributes that apply to the ivar. */
30469 attributes
= cp_parser_attributes_opt (parser
);
30470 /* Remember which attributes are prefix attributes and
30472 first_attribute
= attributes
;
30473 /* Combine the attributes. */
30474 attributes
= attr_chainon (prefix_attributes
, attributes
);
30477 /* Create the bitfield declaration. */
30478 decl
= grokbitfield (declarator
, &declspecs
,
30479 width
, NULL_TREE
, attributes
);
30481 decl
= grokfield (declarator
, &declspecs
,
30482 NULL_TREE
, /*init_const_expr_p=*/false,
30483 NULL_TREE
, attributes
);
30485 /* Add the instance variable. */
30486 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30487 objc_add_instance_variable (decl
);
30489 /* Reset PREFIX_ATTRIBUTES. */
30490 if (attributes
!= error_mark_node
)
30492 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30493 attributes
= TREE_CHAIN (attributes
);
30495 TREE_CHAIN (attributes
) = NULL_TREE
;
30498 token
= cp_lexer_peek_token (parser
->lexer
);
30500 if (token
->type
== CPP_COMMA
)
30502 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30508 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30509 token
= cp_lexer_peek_token (parser
->lexer
);
30512 if (token
->keyword
== RID_AT_END
)
30513 cp_parser_error (parser
, "expected %<}%>");
30515 /* Do not consume the RID_AT_END, so it will be read again as terminating
30516 the @interface of @implementation. */
30517 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30518 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30520 /* For historical reasons, we accept an optional semicolon. */
30521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30522 cp_lexer_consume_token (parser
->lexer
);
30525 /* Parse an Objective-C protocol declaration. */
30528 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30530 tree proto
, protorefs
;
30533 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30534 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30536 tok
= cp_lexer_peek_token (parser
->lexer
);
30537 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30538 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30542 /* See if we have a forward declaration or a definition. */
30543 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30545 /* Try a forward declaration first. */
30546 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30552 id
= cp_parser_identifier (parser
);
30553 if (id
== error_mark_node
)
30556 objc_declare_protocol (id
, attributes
);
30558 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30559 cp_lexer_consume_token (parser
->lexer
);
30563 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30566 /* Ok, we got a full-fledged definition (or at least should). */
30569 proto
= cp_parser_identifier (parser
);
30570 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30571 objc_start_protocol (proto
, protorefs
, attributes
);
30572 cp_parser_objc_method_prototype_list (parser
);
30576 /* Parse an Objective-C superclass or category. */
30579 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30582 tree
*categ
, bool *is_class_extension
)
30584 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30586 *super
= *categ
= NULL_TREE
;
30587 *is_class_extension
= false;
30588 if (next
->type
== CPP_COLON
)
30590 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30591 *super
= cp_parser_identifier (parser
);
30593 else if (next
->type
== CPP_OPEN_PAREN
)
30595 matching_parens parens
;
30596 parens
.consume_open (parser
); /* Eat '('. */
30598 /* If there is no category name, and this is an @interface, we
30599 have a class extension. */
30600 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30602 *categ
= NULL_TREE
;
30603 *is_class_extension
= true;
30606 *categ
= cp_parser_identifier (parser
);
30608 parens
.require_close (parser
);
30612 /* Parse an Objective-C class interface. */
30615 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30617 tree name
, super
, categ
, protos
;
30618 bool is_class_extension
;
30620 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30621 name
= cp_parser_identifier (parser
);
30622 if (name
== error_mark_node
)
30624 /* It's hard to recover because even if valid @interface stuff
30625 is to follow, we can't compile it (or validate it) if we
30626 don't even know which class it refers to. Let's assume this
30627 was a stray '@interface' token in the stream and skip it.
30631 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30632 &is_class_extension
);
30633 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30635 /* We have either a class or a category on our hands. */
30636 if (categ
|| is_class_extension
)
30637 objc_start_category_interface (name
, categ
, protos
, attributes
);
30640 objc_start_class_interface (name
, super
, protos
, attributes
);
30641 /* Handle instance variable declarations, if any. */
30642 cp_parser_objc_class_ivars (parser
);
30643 objc_continue_interface ();
30646 cp_parser_objc_method_prototype_list (parser
);
30649 /* Parse an Objective-C class implementation. */
30652 cp_parser_objc_class_implementation (cp_parser
* parser
)
30654 tree name
, super
, categ
;
30655 bool is_class_extension
;
30657 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30658 name
= cp_parser_identifier (parser
);
30659 if (name
== error_mark_node
)
30661 /* It's hard to recover because even if valid @implementation
30662 stuff is to follow, we can't compile it (or validate it) if
30663 we don't even know which class it refers to. Let's assume
30664 this was a stray '@implementation' token in the stream and
30669 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30670 &is_class_extension
);
30672 /* We have either a class or a category on our hands. */
30674 objc_start_category_implementation (name
, categ
);
30677 objc_start_class_implementation (name
, super
);
30678 /* Handle instance variable declarations, if any. */
30679 cp_parser_objc_class_ivars (parser
);
30680 objc_continue_implementation ();
30683 cp_parser_objc_method_definition_list (parser
);
30686 /* Consume the @end token and finish off the implementation. */
30689 cp_parser_objc_end_implementation (cp_parser
* parser
)
30691 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30692 objc_finish_implementation ();
30695 /* Parse an Objective-C declaration. */
30698 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30700 /* Try to figure out what kind of declaration is present. */
30701 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30704 switch (kwd
->keyword
)
30709 error_at (kwd
->location
, "attributes may not be specified before"
30710 " the %<@%D%> Objective-C++ keyword",
30714 case RID_AT_IMPLEMENTATION
:
30715 warning_at (kwd
->location
, OPT_Wattributes
,
30716 "prefix attributes are ignored before %<@%D%>",
30723 switch (kwd
->keyword
)
30726 cp_parser_objc_alias_declaration (parser
);
30729 cp_parser_objc_class_declaration (parser
);
30731 case RID_AT_PROTOCOL
:
30732 cp_parser_objc_protocol_declaration (parser
, attributes
);
30734 case RID_AT_INTERFACE
:
30735 cp_parser_objc_class_interface (parser
, attributes
);
30737 case RID_AT_IMPLEMENTATION
:
30738 cp_parser_objc_class_implementation (parser
);
30741 cp_parser_objc_end_implementation (parser
);
30744 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30746 cp_parser_skip_to_end_of_block_or_statement (parser
);
30750 /* Parse an Objective-C try-catch-finally statement.
30752 objc-try-catch-finally-stmt:
30753 @try compound-statement objc-catch-clause-seq [opt]
30754 objc-finally-clause [opt]
30756 objc-catch-clause-seq:
30757 objc-catch-clause objc-catch-clause-seq [opt]
30760 @catch ( objc-exception-declaration ) compound-statement
30762 objc-finally-clause:
30763 @finally compound-statement
30765 objc-exception-declaration:
30766 parameter-declaration
30769 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30773 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30774 for C. Keep them in sync. */
30777 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30779 location_t location
;
30782 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30783 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30784 objc_maybe_warn_exceptions (location
);
30785 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30786 node, lest it get absorbed into the surrounding block. */
30787 stmt
= push_stmt_list ();
30788 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30789 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30791 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30793 cp_parameter_declarator
*parm
;
30794 tree parameter_declaration
= error_mark_node
;
30795 bool seen_open_paren
= false;
30796 matching_parens parens
;
30798 cp_lexer_consume_token (parser
->lexer
);
30799 if (parens
.require_open (parser
))
30800 seen_open_paren
= true;
30801 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30803 /* We have "@catch (...)" (where the '...' are literally
30804 what is in the code). Skip the '...'.
30805 parameter_declaration is set to NULL_TREE, and
30806 objc_being_catch_clauses() knows that that means
30808 cp_lexer_consume_token (parser
->lexer
);
30809 parameter_declaration
= NULL_TREE
;
30813 /* We have "@catch (NSException *exception)" or something
30814 like that. Parse the parameter declaration. */
30815 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30817 parameter_declaration
= error_mark_node
;
30819 parameter_declaration
= grokdeclarator (parm
->declarator
,
30820 &parm
->decl_specifiers
,
30821 PARM
, /*initialized=*/0,
30822 /*attrlist=*/NULL
);
30824 if (seen_open_paren
)
30825 parens
.require_close (parser
);
30828 /* If there was no open parenthesis, we are recovering from
30829 an error, and we are trying to figure out what mistake
30830 the user has made. */
30832 /* If there is an immediate closing parenthesis, the user
30833 probably forgot the opening one (ie, they typed "@catch
30834 NSException *e)". Parse the closing parenthesis and keep
30836 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30837 cp_lexer_consume_token (parser
->lexer
);
30839 /* If these is no immediate closing parenthesis, the user
30840 probably doesn't know that parenthesis are required at
30841 all (ie, they typed "@catch NSException *e"). So, just
30842 forget about the closing parenthesis and keep going. */
30844 objc_begin_catch_clause (parameter_declaration
);
30845 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30846 objc_finish_catch_clause ();
30848 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30850 cp_lexer_consume_token (parser
->lexer
);
30851 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30852 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30853 node, lest it get absorbed into the surrounding block. */
30854 stmt
= push_stmt_list ();
30855 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30856 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30859 return objc_finish_try_stmt ();
30862 /* Parse an Objective-C synchronized statement.
30864 objc-synchronized-stmt:
30865 @synchronized ( expression ) compound-statement
30867 Returns NULL_TREE. */
30870 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30872 location_t location
;
30875 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30877 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30878 objc_maybe_warn_exceptions (location
);
30879 matching_parens parens
;
30880 parens
.require_open (parser
);
30881 lock
= cp_parser_expression (parser
);
30882 parens
.require_close (parser
);
30884 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30885 node, lest it get absorbed into the surrounding block. */
30886 stmt
= push_stmt_list ();
30887 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30889 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30892 /* Parse an Objective-C throw statement.
30895 @throw assignment-expression [opt] ;
30897 Returns a constructed '@throw' statement. */
30900 cp_parser_objc_throw_statement (cp_parser
*parser
)
30902 tree expr
= NULL_TREE
;
30903 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30905 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30907 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30908 expr
= cp_parser_expression (parser
);
30910 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30912 return objc_build_throw_stmt (loc
, expr
);
30915 /* Parse an Objective-C statement. */
30918 cp_parser_objc_statement (cp_parser
* parser
)
30920 /* Try to figure out what kind of declaration is present. */
30921 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30923 switch (kwd
->keyword
)
30926 return cp_parser_objc_try_catch_finally_statement (parser
);
30927 case RID_AT_SYNCHRONIZED
:
30928 return cp_parser_objc_synchronized_statement (parser
);
30930 return cp_parser_objc_throw_statement (parser
);
30932 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30934 cp_parser_skip_to_end_of_block_or_statement (parser
);
30937 return error_mark_node
;
30940 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30941 look ahead to see if an objc keyword follows the attributes. This
30942 is to detect the use of prefix attributes on ObjC @interface and
30946 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30948 cp_lexer_save_tokens (parser
->lexer
);
30949 *attrib
= cp_parser_attributes_opt (parser
);
30950 gcc_assert (*attrib
);
30951 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30953 cp_lexer_commit_tokens (parser
->lexer
);
30956 cp_lexer_rollback_tokens (parser
->lexer
);
30960 /* This routine is a minimal replacement for
30961 c_parser_struct_declaration () used when parsing the list of
30962 types/names or ObjC++ properties. For example, when parsing the
30965 @property (readonly) int a, b, c;
30967 this function is responsible for parsing "int a, int b, int c" and
30968 returning the declarations as CHAIN of DECLs.
30970 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30971 similar parsing. */
30973 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30975 tree decls
= NULL_TREE
;
30976 cp_decl_specifier_seq declspecs
;
30977 int decl_class_or_enum_p
;
30978 tree prefix_attributes
;
30980 cp_parser_decl_specifier_seq (parser
,
30981 CP_PARSER_FLAGS_NONE
,
30983 &decl_class_or_enum_p
);
30985 if (declspecs
.type
== error_mark_node
)
30986 return error_mark_node
;
30988 /* auto, register, static, extern, mutable. */
30989 if (declspecs
.storage_class
!= sc_none
)
30991 cp_parser_error (parser
, "invalid type for property");
30992 declspecs
.storage_class
= sc_none
;
30995 /* thread_local. */
30996 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30998 cp_parser_error (parser
, "invalid type for property");
30999 declspecs
.locations
[ds_thread
] = 0;
31003 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
31005 cp_parser_error (parser
, "invalid type for property");
31006 declspecs
.locations
[ds_typedef
] = 0;
31009 prefix_attributes
= declspecs
.attributes
;
31010 declspecs
.attributes
= NULL_TREE
;
31012 /* Keep going until we hit the `;' at the end of the declaration. */
31013 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31015 tree attributes
, first_attribute
, decl
;
31016 cp_declarator
*declarator
;
31019 /* Parse the declarator. */
31020 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31021 NULL
, NULL
, false, false);
31023 /* Look for attributes that apply to the ivar. */
31024 attributes
= cp_parser_attributes_opt (parser
);
31025 /* Remember which attributes are prefix attributes and
31027 first_attribute
= attributes
;
31028 /* Combine the attributes. */
31029 attributes
= attr_chainon (prefix_attributes
, attributes
);
31031 decl
= grokfield (declarator
, &declspecs
,
31032 NULL_TREE
, /*init_const_expr_p=*/false,
31033 NULL_TREE
, attributes
);
31035 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31036 return error_mark_node
;
31038 /* Reset PREFIX_ATTRIBUTES. */
31039 if (attributes
!= error_mark_node
)
31041 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31042 attributes
= TREE_CHAIN (attributes
);
31044 TREE_CHAIN (attributes
) = NULL_TREE
;
31047 DECL_CHAIN (decl
) = decls
;
31050 token
= cp_lexer_peek_token (parser
->lexer
);
31051 if (token
->type
== CPP_COMMA
)
31053 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31062 /* Parse an Objective-C @property declaration. The syntax is:
31064 objc-property-declaration:
31065 '@property' objc-property-attributes[opt] struct-declaration ;
31067 objc-property-attributes:
31068 '(' objc-property-attribute-list ')'
31070 objc-property-attribute-list:
31071 objc-property-attribute
31072 objc-property-attribute-list, objc-property-attribute
31074 objc-property-attribute
31075 'getter' = identifier
31076 'setter' = identifier
31085 @property NSString *name;
31086 @property (readonly) id object;
31087 @property (retain, nonatomic, getter=getTheName) id name;
31088 @property int a, b, c;
31090 PS: This function is identical to
31091 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31093 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31095 /* The following variables hold the attributes of the properties as
31096 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31097 seen. When we see an attribute, we set them to 'true' (if they
31098 are boolean properties) or to the identifier (if they have an
31099 argument, ie, for getter and setter). Note that here we only
31100 parse the list of attributes, check the syntax and accumulate the
31101 attributes that we find. objc_add_property_declaration() will
31102 then process the information. */
31103 bool property_assign
= false;
31104 bool property_copy
= false;
31105 tree property_getter_ident
= NULL_TREE
;
31106 bool property_nonatomic
= false;
31107 bool property_readonly
= false;
31108 bool property_readwrite
= false;
31109 bool property_retain
= false;
31110 tree property_setter_ident
= NULL_TREE
;
31112 /* 'properties' is the list of properties that we read. Usually a
31113 single one, but maybe more (eg, in "@property int a, b, c;" there
31118 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31120 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31122 /* Parse the optional attribute list... */
31123 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31126 matching_parens parens
;
31127 parens
.consume_open (parser
);
31131 bool syntax_error
= false;
31132 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31135 if (token
->type
!= CPP_NAME
)
31137 cp_parser_error (parser
, "expected identifier");
31140 keyword
= C_RID_CODE (token
->u
.value
);
31141 cp_lexer_consume_token (parser
->lexer
);
31144 case RID_ASSIGN
: property_assign
= true; break;
31145 case RID_COPY
: property_copy
= true; break;
31146 case RID_NONATOMIC
: property_nonatomic
= true; break;
31147 case RID_READONLY
: property_readonly
= true; break;
31148 case RID_READWRITE
: property_readwrite
= true; break;
31149 case RID_RETAIN
: property_retain
= true; break;
31153 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31155 if (keyword
== RID_GETTER
)
31156 cp_parser_error (parser
,
31157 "missing %<=%> (after %<getter%> attribute)");
31159 cp_parser_error (parser
,
31160 "missing %<=%> (after %<setter%> attribute)");
31161 syntax_error
= true;
31164 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31165 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31167 cp_parser_error (parser
, "expected identifier");
31168 syntax_error
= true;
31171 if (keyword
== RID_SETTER
)
31173 if (property_setter_ident
!= NULL_TREE
)
31175 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31176 cp_lexer_consume_token (parser
->lexer
);
31179 property_setter_ident
= cp_parser_objc_selector (parser
);
31180 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31181 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31183 cp_lexer_consume_token (parser
->lexer
);
31187 if (property_getter_ident
!= NULL_TREE
)
31189 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31190 cp_lexer_consume_token (parser
->lexer
);
31193 property_getter_ident
= cp_parser_objc_selector (parser
);
31197 cp_parser_error (parser
, "unknown property attribute");
31198 syntax_error
= true;
31205 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31206 cp_lexer_consume_token (parser
->lexer
);
31211 /* FIXME: "@property (setter, assign);" will generate a spurious
31212 "error: expected ‘)’ before ‘,’ token". This is because
31213 cp_parser_require, unlike the C counterpart, will produce an
31214 error even if we are in error recovery. */
31215 if (!parens
.require_close (parser
))
31217 cp_parser_skip_to_closing_parenthesis (parser
,
31218 /*recovering=*/true,
31219 /*or_comma=*/false,
31220 /*consume_paren=*/true);
31224 /* ... and the property declaration(s). */
31225 properties
= cp_parser_objc_struct_declaration (parser
);
31227 if (properties
== error_mark_node
)
31229 cp_parser_skip_to_end_of_statement (parser
);
31230 /* If the next token is now a `;', consume it. */
31231 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31232 cp_lexer_consume_token (parser
->lexer
);
31236 if (properties
== NULL_TREE
)
31237 cp_parser_error (parser
, "expected identifier");
31240 /* Comma-separated properties are chained together in
31241 reverse order; add them one by one. */
31242 properties
= nreverse (properties
);
31244 for (; properties
; properties
= TREE_CHAIN (properties
))
31245 objc_add_property_declaration (loc
, copy_node (properties
),
31246 property_readonly
, property_readwrite
,
31247 property_assign
, property_retain
,
31248 property_copy
, property_nonatomic
,
31249 property_getter_ident
, property_setter_ident
);
31252 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31255 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31257 objc-synthesize-declaration:
31258 @synthesize objc-synthesize-identifier-list ;
31260 objc-synthesize-identifier-list:
31261 objc-synthesize-identifier
31262 objc-synthesize-identifier-list, objc-synthesize-identifier
31264 objc-synthesize-identifier
31266 identifier = identifier
31269 @synthesize MyProperty;
31270 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31272 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31273 for C. Keep them in sync.
31276 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31278 tree list
= NULL_TREE
;
31280 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31282 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31285 tree property
, ivar
;
31286 property
= cp_parser_identifier (parser
);
31287 if (property
== error_mark_node
)
31289 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31294 cp_lexer_consume_token (parser
->lexer
);
31295 ivar
= cp_parser_identifier (parser
);
31296 if (ivar
== error_mark_node
)
31298 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31304 list
= chainon (list
, build_tree_list (ivar
, property
));
31305 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31306 cp_lexer_consume_token (parser
->lexer
);
31310 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31311 objc_add_synthesize_declaration (loc
, list
);
31314 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31316 objc-dynamic-declaration:
31317 @dynamic identifier-list ;
31320 @dynamic MyProperty;
31321 @dynamic MyProperty, AnotherProperty;
31323 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31324 for C. Keep them in sync.
31327 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31329 tree list
= NULL_TREE
;
31331 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31333 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31337 property
= cp_parser_identifier (parser
);
31338 if (property
== error_mark_node
)
31340 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31343 list
= chainon (list
, build_tree_list (NULL
, property
));
31344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31345 cp_lexer_consume_token (parser
->lexer
);
31349 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31350 objc_add_dynamic_declaration (loc
, list
);
31354 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31356 /* Returns name of the next clause.
31357 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31358 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31359 returned and the token is consumed. */
31361 static pragma_omp_clause
31362 cp_parser_omp_clause_name (cp_parser
*parser
)
31364 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31366 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31367 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31368 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31369 result
= PRAGMA_OMP_CLAUSE_IF
;
31370 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31371 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31372 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31373 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31374 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31375 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31376 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31377 result
= PRAGMA_OMP_CLAUSE_FOR
;
31378 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31380 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31381 const char *p
= IDENTIFIER_POINTER (id
);
31386 if (!strcmp ("aligned", p
))
31387 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31388 else if (!strcmp ("async", p
))
31389 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31392 if (!strcmp ("collapse", p
))
31393 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31394 else if (!strcmp ("copy", p
))
31395 result
= PRAGMA_OACC_CLAUSE_COPY
;
31396 else if (!strcmp ("copyin", p
))
31397 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31398 else if (!strcmp ("copyout", p
))
31399 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31400 else if (!strcmp ("copyprivate", p
))
31401 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31402 else if (!strcmp ("create", p
))
31403 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31406 if (!strcmp ("defaultmap", p
))
31407 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31408 else if (!strcmp ("depend", p
))
31409 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31410 else if (!strcmp ("device", p
))
31411 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31412 else if (!strcmp ("deviceptr", p
))
31413 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31414 else if (!strcmp ("device_resident", p
))
31415 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31416 else if (!strcmp ("dist_schedule", p
))
31417 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31420 if (!strcmp ("final", p
))
31421 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31422 else if (!strcmp ("finalize", p
))
31423 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
31424 else if (!strcmp ("firstprivate", p
))
31425 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31426 else if (!strcmp ("from", p
))
31427 result
= PRAGMA_OMP_CLAUSE_FROM
;
31430 if (!strcmp ("gang", p
))
31431 result
= PRAGMA_OACC_CLAUSE_GANG
;
31432 else if (!strcmp ("grainsize", p
))
31433 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31436 if (!strcmp ("hint", p
))
31437 result
= PRAGMA_OMP_CLAUSE_HINT
;
31438 else if (!strcmp ("host", p
))
31439 result
= PRAGMA_OACC_CLAUSE_HOST
;
31442 if (!strcmp ("if_present", p
))
31443 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
31444 else if (!strcmp ("inbranch", p
))
31445 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31446 else if (!strcmp ("independent", p
))
31447 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31448 else if (!strcmp ("is_device_ptr", p
))
31449 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31452 if (!strcmp ("lastprivate", p
))
31453 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31454 else if (!strcmp ("linear", p
))
31455 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31456 else if (!strcmp ("link", p
))
31457 result
= PRAGMA_OMP_CLAUSE_LINK
;
31460 if (!strcmp ("map", p
))
31461 result
= PRAGMA_OMP_CLAUSE_MAP
;
31462 else if (!strcmp ("mergeable", p
))
31463 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31466 if (!strcmp ("nogroup", p
))
31467 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31468 else if (!strcmp ("notinbranch", p
))
31469 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31470 else if (!strcmp ("nowait", p
))
31471 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31472 else if (!strcmp ("num_gangs", p
))
31473 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31474 else if (!strcmp ("num_tasks", p
))
31475 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31476 else if (!strcmp ("num_teams", p
))
31477 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31478 else if (!strcmp ("num_threads", p
))
31479 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31480 else if (!strcmp ("num_workers", p
))
31481 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31484 if (!strcmp ("ordered", p
))
31485 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31488 if (!strcmp ("parallel", p
))
31489 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31490 else if (!strcmp ("present", p
))
31491 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31492 else if (!strcmp ("present_or_copy", p
)
31493 || !strcmp ("pcopy", p
))
31494 result
= PRAGMA_OACC_CLAUSE_COPY
;
31495 else if (!strcmp ("present_or_copyin", p
)
31496 || !strcmp ("pcopyin", p
))
31497 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
31498 else if (!strcmp ("present_or_copyout", p
)
31499 || !strcmp ("pcopyout", p
))
31500 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31501 else if (!strcmp ("present_or_create", p
)
31502 || !strcmp ("pcreate", p
))
31503 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31504 else if (!strcmp ("priority", p
))
31505 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31506 else if (!strcmp ("proc_bind", p
))
31507 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31510 if (!strcmp ("reduction", p
))
31511 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31514 if (!strcmp ("safelen", p
))
31515 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31516 else if (!strcmp ("schedule", p
))
31517 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31518 else if (!strcmp ("sections", p
))
31519 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31520 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
31521 result
= PRAGMA_OACC_CLAUSE_HOST
;
31522 else if (!strcmp ("seq", p
))
31523 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31524 else if (!strcmp ("shared", p
))
31525 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31526 else if (!strcmp ("simd", p
))
31527 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31528 else if (!strcmp ("simdlen", p
))
31529 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31532 if (!strcmp ("taskgroup", p
))
31533 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31534 else if (!strcmp ("thread_limit", p
))
31535 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31536 else if (!strcmp ("threads", p
))
31537 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31538 else if (!strcmp ("tile", p
))
31539 result
= PRAGMA_OACC_CLAUSE_TILE
;
31540 else if (!strcmp ("to", p
))
31541 result
= PRAGMA_OMP_CLAUSE_TO
;
31544 if (!strcmp ("uniform", p
))
31545 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31546 else if (!strcmp ("untied", p
))
31547 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31548 else if (!strcmp ("use_device", p
))
31549 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31550 else if (!strcmp ("use_device_ptr", p
))
31551 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31554 if (!strcmp ("vector", p
))
31555 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31556 else if (!strcmp ("vector_length", p
))
31557 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31560 if (!strcmp ("wait", p
))
31561 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31562 else if (!strcmp ("worker", p
))
31563 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31568 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31569 cp_lexer_consume_token (parser
->lexer
);
31574 /* Validate that a clause of the given type does not already exist. */
31577 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31578 const char *name
, location_t location
)
31582 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31583 if (OMP_CLAUSE_CODE (c
) == code
)
31585 error_at (location
, "too many %qs clauses", name
);
31593 variable-list , identifier
31595 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31596 colon). An opening parenthesis will have been consumed by the caller.
31598 If KIND is nonzero, create the appropriate node and install the decl
31599 in OMP_CLAUSE_DECL and add the node to the head of the list.
31601 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31602 return the list created.
31604 COLON can be NULL if only closing parenthesis should end the list,
31605 or pointer to bool which will receive false if the list is terminated
31606 by closing parenthesis or true if the list is terminated by colon. */
31609 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31610 tree list
, bool *colon
)
31613 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31616 parser
->colon_corrects_to_scope_p
= false;
31623 token
= cp_lexer_peek_token (parser
->lexer
);
31625 && current_class_ptr
31626 && cp_parser_is_keyword (token
, RID_THIS
))
31628 decl
= finish_this_expr ();
31629 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31630 || CONVERT_EXPR_P (decl
))
31631 decl
= TREE_OPERAND (decl
, 0);
31632 cp_lexer_consume_token (parser
->lexer
);
31636 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31637 /*check_dependency_p=*/true,
31638 /*template_p=*/NULL
,
31639 /*declarator_p=*/false,
31640 /*optional_p=*/false);
31641 if (name
== error_mark_node
)
31644 if (identifier_p (name
))
31645 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31648 if (decl
== error_mark_node
)
31649 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31652 if (decl
== error_mark_node
)
31654 else if (kind
!= 0)
31658 case OMP_CLAUSE__CACHE_
:
31659 /* The OpenACC cache directive explicitly only allows "array
31660 elements or subarrays". */
31661 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31663 error_at (token
->location
, "expected %<[%>");
31664 decl
= error_mark_node
;
31668 case OMP_CLAUSE_MAP
:
31669 case OMP_CLAUSE_FROM
:
31670 case OMP_CLAUSE_TO
:
31671 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31674 = cp_lexer_peek_token (parser
->lexer
)->location
;
31675 cp_id_kind idk
= CP_ID_KIND_NONE
;
31676 cp_lexer_consume_token (parser
->lexer
);
31677 decl
= convert_from_reference (decl
);
31679 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31684 case OMP_CLAUSE_DEPEND
:
31685 case OMP_CLAUSE_REDUCTION
:
31686 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31688 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31690 parser
->colon_corrects_to_scope_p
= false;
31691 cp_lexer_consume_token (parser
->lexer
);
31692 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31693 low_bound
= cp_parser_expression (parser
);
31695 parser
->colon_corrects_to_scope_p
31696 = saved_colon_corrects_to_scope_p
;
31697 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31698 length
= integer_one_node
;
31701 /* Look for `:'. */
31702 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31704 if (!cp_lexer_next_token_is (parser
->lexer
,
31706 length
= cp_parser_expression (parser
);
31708 /* Look for the closing `]'. */
31709 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31713 decl
= tree_cons (low_bound
, length
, decl
);
31720 tree u
= build_omp_clause (token
->location
, kind
);
31721 OMP_CLAUSE_DECL (u
) = decl
;
31722 OMP_CLAUSE_CHAIN (u
) = list
;
31726 list
= tree_cons (decl
, NULL_TREE
, list
);
31729 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31731 cp_lexer_consume_token (parser
->lexer
);
31735 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31737 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31740 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31744 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31748 /* Try to resync to an unnested comma. Copied from
31749 cp_parser_parenthesized_expression_list. */
31752 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31753 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31754 /*recovering=*/true,
31756 /*consume_paren=*/true);
31764 /* Similarly, but expect leading and trailing parenthesis. This is a very
31765 common case for omp clauses. */
31768 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31770 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31771 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31776 copy ( variable-list )
31777 copyin ( variable-list )
31778 copyout ( variable-list )
31779 create ( variable-list )
31780 delete ( variable-list )
31781 present ( variable-list ) */
31784 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31787 enum gomp_map_kind kind
;
31790 case PRAGMA_OACC_CLAUSE_COPY
:
31791 kind
= GOMP_MAP_TOFROM
;
31793 case PRAGMA_OACC_CLAUSE_COPYIN
:
31794 kind
= GOMP_MAP_TO
;
31796 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31797 kind
= GOMP_MAP_FROM
;
31799 case PRAGMA_OACC_CLAUSE_CREATE
:
31800 kind
= GOMP_MAP_ALLOC
;
31802 case PRAGMA_OACC_CLAUSE_DELETE
:
31803 kind
= GOMP_MAP_RELEASE
;
31805 case PRAGMA_OACC_CLAUSE_DEVICE
:
31806 kind
= GOMP_MAP_FORCE_TO
;
31808 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31809 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31811 case PRAGMA_OACC_CLAUSE_HOST
:
31812 kind
= GOMP_MAP_FORCE_FROM
;
31814 case PRAGMA_OACC_CLAUSE_LINK
:
31815 kind
= GOMP_MAP_LINK
;
31817 case PRAGMA_OACC_CLAUSE_PRESENT
:
31818 kind
= GOMP_MAP_FORCE_PRESENT
;
31821 gcc_unreachable ();
31824 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31826 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31827 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31833 deviceptr ( variable-list ) */
31836 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31838 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31841 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31842 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31843 variable-list must only allow for pointer variables. */
31844 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31845 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31847 tree v
= TREE_PURPOSE (t
);
31848 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31849 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31850 OMP_CLAUSE_DECL (u
) = v
;
31851 OMP_CLAUSE_CHAIN (u
) = list
;
31866 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31867 enum omp_clause_code code
,
31868 tree list
, location_t location
)
31870 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31871 tree c
= build_omp_clause (location
, code
);
31872 OMP_CLAUSE_CHAIN (c
) = list
;
31877 num_gangs ( expression )
31878 num_workers ( expression )
31879 vector_length ( expression ) */
31882 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31883 const char *str
, tree list
)
31885 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31887 matching_parens parens
;
31888 if (!parens
.require_open (parser
))
31891 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31893 if (t
== error_mark_node
31894 || !parens
.require_close (parser
))
31896 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31897 /*or_comma=*/false,
31898 /*consume_paren=*/true);
31902 check_no_duplicate_clause (list
, code
, str
, loc
);
31904 tree c
= build_omp_clause (loc
, code
);
31905 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31906 OMP_CLAUSE_CHAIN (c
) = list
;
31912 gang [( gang-arg-list )]
31913 worker [( [num:] int-expr )]
31914 vector [( [length:] int-expr )]
31916 where gang-arg is one of:
31921 and size-expr may be:
31928 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31929 const char *str
, tree list
)
31931 const char *id
= "num";
31932 cp_lexer
*lexer
= parser
->lexer
;
31933 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31934 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31936 if (kind
== OMP_CLAUSE_VECTOR
)
31939 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31941 matching_parens parens
;
31942 parens
.consume_open (parser
);
31946 cp_token
*next
= cp_lexer_peek_token (lexer
);
31949 /* Gang static argument. */
31950 if (kind
== OMP_CLAUSE_GANG
31951 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31953 cp_lexer_consume_token (lexer
);
31955 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31956 goto cleanup_error
;
31959 if (ops
[idx
] != NULL
)
31961 cp_parser_error (parser
, "too many %<static%> arguments");
31962 goto cleanup_error
;
31965 /* Check for the '*' argument. */
31966 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31967 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31968 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31971 cp_lexer_consume_token (lexer
);
31972 ops
[idx
] = integer_minus_one_node
;
31974 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31976 cp_lexer_consume_token (lexer
);
31982 /* Worker num: argument and vector length: arguments. */
31983 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31984 && id_equal (next
->u
.value
, id
)
31985 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31987 cp_lexer_consume_token (lexer
); /* id */
31988 cp_lexer_consume_token (lexer
); /* ':' */
31991 /* Now collect the actual argument. */
31992 if (ops
[idx
] != NULL_TREE
)
31994 cp_parser_error (parser
, "unexpected argument");
31995 goto cleanup_error
;
31998 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
32000 if (expr
== error_mark_node
)
32001 goto cleanup_error
;
32003 mark_exp_read (expr
);
32006 if (kind
== OMP_CLAUSE_GANG
32007 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
32009 cp_lexer_consume_token (lexer
);
32016 if (!parens
.require_close (parser
))
32017 goto cleanup_error
;
32020 check_no_duplicate_clause (list
, kind
, str
, loc
);
32022 c
= build_omp_clause (loc
, kind
);
32025 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32027 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32028 OMP_CLAUSE_CHAIN (c
) = list
;
32033 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32038 tile ( size-expr-list ) */
32041 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32043 tree c
, expr
= error_mark_node
;
32044 tree tile
= NULL_TREE
;
32046 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32047 so, but the spec authors never considered such a case and have
32048 differing opinions on what it might mean, including 'not
32050 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32051 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32054 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32059 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32062 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32063 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32064 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32066 cp_lexer_consume_token (parser
->lexer
);
32067 expr
= integer_zero_node
;
32070 expr
= cp_parser_constant_expression (parser
);
32072 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32074 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32076 /* Consume the trailing ')'. */
32077 cp_lexer_consume_token (parser
->lexer
);
32079 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32080 tile
= nreverse (tile
);
32081 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32082 OMP_CLAUSE_CHAIN (c
) = list
;
32087 Parse wait clause or directive parameters. */
32090 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32092 vec
<tree
, va_gc
> *args
;
32095 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32097 /*allow_expansion_p=*/true,
32098 /*non_constant_p=*/NULL
);
32100 if (args
== NULL
|| args
->length () == 0)
32102 cp_parser_error (parser
, "expected integer expression before ')'");
32104 release_tree_vector (args
);
32108 args_tree
= build_tree_list_vec (args
);
32110 release_tree_vector (args
);
32112 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32114 tree targ
= TREE_VALUE (t
);
32116 if (targ
!= error_mark_node
)
32118 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32119 error ("%<wait%> expression must be integral");
32122 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32124 targ
= mark_rvalue_use (targ
);
32125 OMP_CLAUSE_DECL (c
) = targ
;
32126 OMP_CLAUSE_CHAIN (c
) = list
;
32136 wait ( int-expr-list ) */
32139 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32141 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32143 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32146 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32152 collapse ( constant-expression ) */
32155 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32161 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32162 matching_parens parens
;
32163 if (!parens
.require_open (parser
))
32166 num
= cp_parser_constant_expression (parser
);
32168 if (!parens
.require_close (parser
))
32169 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32170 /*or_comma=*/false,
32171 /*consume_paren=*/true);
32173 if (num
== error_mark_node
)
32175 num
= fold_non_dependent_expr (num
);
32176 if (!tree_fits_shwi_p (num
)
32177 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32178 || (n
= tree_to_shwi (num
)) <= 0
32181 error_at (loc
, "collapse argument needs positive constant integer expression");
32185 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32186 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32187 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32188 OMP_CLAUSE_CHAIN (c
) = list
;
32189 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32195 default ( none | shared )
32198 default ( none | present ) */
32201 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32202 location_t location
, bool is_oacc
)
32204 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32207 matching_parens parens
;
32208 if (!parens
.require_open (parser
))
32210 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32212 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32213 const char *p
= IDENTIFIER_POINTER (id
);
32218 if (strcmp ("none", p
) != 0)
32220 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32224 if (strcmp ("present", p
) != 0 || !is_oacc
)
32226 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32230 if (strcmp ("shared", p
) != 0 || is_oacc
)
32232 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32239 cp_lexer_consume_token (parser
->lexer
);
32245 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32247 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32250 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32251 || !parens
.require_close (parser
))
32252 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32253 /*or_comma=*/false,
32254 /*consume_paren=*/true);
32256 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32259 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32260 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32261 OMP_CLAUSE_CHAIN (c
) = list
;
32262 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32268 final ( expression ) */
32271 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32275 matching_parens parens
;
32276 if (!parens
.require_open (parser
))
32279 t
= cp_parser_condition (parser
);
32281 if (t
== error_mark_node
32282 || !parens
.require_close (parser
))
32283 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32284 /*or_comma=*/false,
32285 /*consume_paren=*/true);
32287 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32289 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32290 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32291 OMP_CLAUSE_CHAIN (c
) = list
;
32300 if ( directive-name-modifier : expression )
32302 directive-name-modifier:
32303 parallel | task | taskloop | target data | target | target update
32304 | target enter data | target exit data */
32307 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32311 enum tree_code if_modifier
= ERROR_MARK
;
32313 matching_parens parens
;
32314 if (!parens
.require_open (parser
))
32317 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32319 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32320 const char *p
= IDENTIFIER_POINTER (id
);
32323 if (strcmp ("parallel", p
) == 0)
32324 if_modifier
= OMP_PARALLEL
;
32325 else if (strcmp ("task", p
) == 0)
32326 if_modifier
= OMP_TASK
;
32327 else if (strcmp ("taskloop", p
) == 0)
32328 if_modifier
= OMP_TASKLOOP
;
32329 else if (strcmp ("target", p
) == 0)
32331 if_modifier
= OMP_TARGET
;
32332 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32334 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32335 p
= IDENTIFIER_POINTER (id
);
32336 if (strcmp ("data", p
) == 0)
32337 if_modifier
= OMP_TARGET_DATA
;
32338 else if (strcmp ("update", p
) == 0)
32339 if_modifier
= OMP_TARGET_UPDATE
;
32340 else if (strcmp ("enter", p
) == 0)
32341 if_modifier
= OMP_TARGET_ENTER_DATA
;
32342 else if (strcmp ("exit", p
) == 0)
32343 if_modifier
= OMP_TARGET_EXIT_DATA
;
32344 if (if_modifier
!= OMP_TARGET
)
32349 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32350 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32352 if_modifier
= ERROR_MARK
;
32354 if (if_modifier
== OMP_TARGET_ENTER_DATA
32355 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32357 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32359 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32360 p
= IDENTIFIER_POINTER (id
);
32361 if (strcmp ("data", p
) == 0)
32367 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32368 error_at (loc
, "expected %<data%>");
32369 if_modifier
= ERROR_MARK
;
32374 if (if_modifier
!= ERROR_MARK
)
32376 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32379 cp_lexer_consume_token (parser
->lexer
);
32386 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32387 error_at (loc
, "expected %<:%>");
32389 if_modifier
= ERROR_MARK
;
32394 t
= cp_parser_condition (parser
);
32396 if (t
== error_mark_node
32397 || !parens
.require_close (parser
))
32398 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32399 /*or_comma=*/false,
32400 /*consume_paren=*/true);
32402 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32403 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32405 if (if_modifier
!= ERROR_MARK
32406 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32408 const char *p
= NULL
;
32409 switch (if_modifier
)
32411 case OMP_PARALLEL
: p
= "parallel"; break;
32412 case OMP_TASK
: p
= "task"; break;
32413 case OMP_TASKLOOP
: p
= "taskloop"; break;
32414 case OMP_TARGET_DATA
: p
= "target data"; break;
32415 case OMP_TARGET
: p
= "target"; break;
32416 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32417 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32418 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32419 default: gcc_unreachable ();
32421 error_at (location
, "too many %<if%> clauses with %qs modifier",
32425 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32428 error_at (location
, "too many %<if%> clauses");
32430 error_at (location
, "too many %<if%> clauses without modifier");
32433 else if (if_modifier
== ERROR_MARK
32434 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32436 error_at (location
, "if any %<if%> clause has modifier, then all "
32437 "%<if%> clauses have to use modifier");
32442 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32443 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32444 OMP_CLAUSE_IF_EXPR (c
) = t
;
32445 OMP_CLAUSE_CHAIN (c
) = list
;
32454 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32455 tree list
, location_t location
)
32459 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32462 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32463 OMP_CLAUSE_CHAIN (c
) = list
;
32471 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32472 tree list
, location_t location
)
32476 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32478 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32479 OMP_CLAUSE_CHAIN (c
) = list
;
32484 num_threads ( expression ) */
32487 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32488 location_t location
)
32492 matching_parens parens
;
32493 if (!parens
.require_open (parser
))
32496 t
= cp_parser_expression (parser
);
32498 if (t
== error_mark_node
32499 || !parens
.require_close (parser
))
32500 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32501 /*or_comma=*/false,
32502 /*consume_paren=*/true);
32504 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32505 "num_threads", location
);
32507 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32508 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32509 OMP_CLAUSE_CHAIN (c
) = list
;
32515 num_tasks ( expression ) */
32518 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32519 location_t location
)
32523 matching_parens parens
;
32524 if (!parens
.require_open (parser
))
32527 t
= cp_parser_expression (parser
);
32529 if (t
== error_mark_node
32530 || !parens
.require_close (parser
))
32531 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32532 /*or_comma=*/false,
32533 /*consume_paren=*/true);
32535 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32536 "num_tasks", location
);
32538 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32539 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32540 OMP_CLAUSE_CHAIN (c
) = list
;
32546 grainsize ( expression ) */
32549 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32550 location_t location
)
32554 matching_parens parens
;
32555 if (!parens
.require_open (parser
))
32558 t
= cp_parser_expression (parser
);
32560 if (t
== error_mark_node
32561 || !parens
.require_close (parser
))
32562 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32563 /*or_comma=*/false,
32564 /*consume_paren=*/true);
32566 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32567 "grainsize", location
);
32569 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32570 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32571 OMP_CLAUSE_CHAIN (c
) = list
;
32577 priority ( expression ) */
32580 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32581 location_t location
)
32585 matching_parens parens
;
32586 if (!parens
.require_open (parser
))
32589 t
= cp_parser_expression (parser
);
32591 if (t
== error_mark_node
32592 || !parens
.require_close (parser
))
32593 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32594 /*or_comma=*/false,
32595 /*consume_paren=*/true);
32597 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32598 "priority", location
);
32600 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32601 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32602 OMP_CLAUSE_CHAIN (c
) = list
;
32608 hint ( expression ) */
32611 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32612 location_t location
)
32616 matching_parens parens
;
32617 if (!parens
.require_open (parser
))
32620 t
= cp_parser_expression (parser
);
32622 if (t
== error_mark_node
32623 || !parens
.require_close (parser
))
32624 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32625 /*or_comma=*/false,
32626 /*consume_paren=*/true);
32628 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32630 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32631 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32632 OMP_CLAUSE_CHAIN (c
) = list
;
32638 defaultmap ( tofrom : scalar ) */
32641 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32642 location_t location
)
32647 matching_parens parens
;
32648 if (!parens
.require_open (parser
))
32651 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32653 cp_parser_error (parser
, "expected %<tofrom%>");
32656 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32657 p
= IDENTIFIER_POINTER (id
);
32658 if (strcmp (p
, "tofrom") != 0)
32660 cp_parser_error (parser
, "expected %<tofrom%>");
32663 cp_lexer_consume_token (parser
->lexer
);
32664 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32667 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32669 cp_parser_error (parser
, "expected %<scalar%>");
32672 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32673 p
= IDENTIFIER_POINTER (id
);
32674 if (strcmp (p
, "scalar") != 0)
32676 cp_parser_error (parser
, "expected %<scalar%>");
32679 cp_lexer_consume_token (parser
->lexer
);
32680 if (!parens
.require_close (parser
))
32683 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32686 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32687 OMP_CLAUSE_CHAIN (c
) = list
;
32691 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32692 /*or_comma=*/false,
32693 /*consume_paren=*/true);
32701 ordered ( constant-expression ) */
32704 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32705 tree list
, location_t location
)
32707 tree c
, num
= NULL_TREE
;
32710 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32711 "ordered", location
);
32713 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32715 matching_parens parens
;
32716 parens
.consume_open (parser
);
32718 num
= cp_parser_constant_expression (parser
);
32720 if (!parens
.require_close (parser
))
32721 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32722 /*or_comma=*/false,
32723 /*consume_paren=*/true);
32725 if (num
== error_mark_node
)
32727 num
= fold_non_dependent_expr (num
);
32728 if (!tree_fits_shwi_p (num
)
32729 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32730 || (n
= tree_to_shwi (num
)) <= 0
32733 error_at (location
,
32734 "ordered argument needs positive constant integer "
32740 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32741 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32742 OMP_CLAUSE_CHAIN (c
) = list
;
32747 reduction ( reduction-operator : variable-list )
32749 reduction-operator:
32750 One of: + * - & ^ | && ||
32754 reduction-operator:
32755 One of: + * - & ^ | && || min max
32759 reduction-operator:
32760 One of: + * - & ^ | && ||
32764 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32766 enum tree_code code
= ERROR_MARK
;
32767 tree nlist
, c
, id
= NULL_TREE
;
32769 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32772 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32774 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32775 case CPP_MULT
: code
= MULT_EXPR
; break;
32776 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32777 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32778 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32779 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32780 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32781 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32785 if (code
!= ERROR_MARK
)
32786 cp_lexer_consume_token (parser
->lexer
);
32789 bool saved_colon_corrects_to_scope_p
;
32790 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32791 parser
->colon_corrects_to_scope_p
= false;
32792 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32793 /*check_dependency_p=*/true,
32794 /*template_p=*/NULL
,
32795 /*declarator_p=*/false,
32796 /*optional_p=*/false);
32797 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32798 if (identifier_p (id
))
32800 const char *p
= IDENTIFIER_POINTER (id
);
32802 if (strcmp (p
, "min") == 0)
32804 else if (strcmp (p
, "max") == 0)
32806 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32808 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32810 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32812 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32813 code
= BIT_AND_EXPR
;
32814 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32815 code
= BIT_IOR_EXPR
;
32816 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32817 code
= BIT_XOR_EXPR
;
32818 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32819 code
= TRUTH_ANDIF_EXPR
;
32820 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32821 code
= TRUTH_ORIF_EXPR
;
32822 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32823 tree scope
= parser
->scope
;
32825 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32826 parser
->scope
= NULL_TREE
;
32827 parser
->qualifying_scope
= NULL_TREE
;
32828 parser
->object_scope
= NULL_TREE
;
32832 error ("invalid reduction-identifier");
32834 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32835 /*or_comma=*/false,
32836 /*consume_paren=*/true);
32841 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32844 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32846 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32848 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32849 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32856 schedule ( schedule-kind )
32857 schedule ( schedule-kind , expression )
32860 static | dynamic | guided | runtime | auto
32863 schedule ( schedule-modifier : schedule-kind )
32864 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32872 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32875 int modifiers
= 0, nmodifiers
= 0;
32877 matching_parens parens
;
32878 if (!parens
.require_open (parser
))
32881 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32883 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32885 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32886 const char *p
= IDENTIFIER_POINTER (id
);
32887 if (strcmp ("simd", p
) == 0)
32888 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32889 else if (strcmp ("monotonic", p
) == 0)
32890 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32891 else if (strcmp ("nonmonotonic", p
) == 0)
32892 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32895 cp_lexer_consume_token (parser
->lexer
);
32896 if (nmodifiers
++ == 0
32897 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32898 cp_lexer_consume_token (parser
->lexer
);
32901 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32906 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32908 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32909 const char *p
= IDENTIFIER_POINTER (id
);
32914 if (strcmp ("dynamic", p
) != 0)
32916 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32920 if (strcmp ("guided", p
) != 0)
32922 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32926 if (strcmp ("runtime", p
) != 0)
32928 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32935 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32936 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32937 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32938 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32941 cp_lexer_consume_token (parser
->lexer
);
32943 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32944 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32945 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32946 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32948 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32953 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32956 cp_lexer_consume_token (parser
->lexer
);
32958 token
= cp_lexer_peek_token (parser
->lexer
);
32959 t
= cp_parser_assignment_expression (parser
);
32961 if (t
== error_mark_node
)
32963 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32964 error_at (token
->location
, "schedule %<runtime%> does not take "
32965 "a %<chunk_size%> parameter");
32966 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32967 error_at (token
->location
, "schedule %<auto%> does not take "
32968 "a %<chunk_size%> parameter");
32970 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32972 if (!parens
.require_close (parser
))
32975 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32978 OMP_CLAUSE_SCHEDULE_KIND (c
)
32979 = (enum omp_clause_schedule_kind
)
32980 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32982 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32983 OMP_CLAUSE_CHAIN (c
) = list
;
32987 cp_parser_error (parser
, "invalid schedule kind");
32989 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32990 /*or_comma=*/false,
32991 /*consume_paren=*/true);
32999 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
33000 tree list
, location_t location
)
33004 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
33006 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
33007 OMP_CLAUSE_CHAIN (c
) = list
;
33016 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
33017 tree list
, location_t location
)
33019 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33020 tree c
= build_omp_clause (location
, code
);
33021 OMP_CLAUSE_CHAIN (c
) = list
;
33032 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33033 enum omp_clause_code code
,
33034 tree list
, location_t location
)
33036 tree c
= build_omp_clause (location
, code
);
33037 OMP_CLAUSE_CHAIN (c
) = list
;
33045 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33046 tree list
, location_t location
)
33048 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33049 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33050 OMP_CLAUSE_CHAIN (c
) = list
;
33059 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33060 enum omp_clause_code code
,
33061 tree list
, location_t location
)
33063 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33064 tree c
= build_omp_clause (location
, code
);
33065 OMP_CLAUSE_CHAIN (c
) = list
;
33070 num_teams ( expression ) */
33073 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33074 location_t location
)
33078 matching_parens parens
;
33079 if (!parens
.require_open (parser
))
33082 t
= cp_parser_expression (parser
);
33084 if (t
== error_mark_node
33085 || !parens
.require_close (parser
))
33086 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33087 /*or_comma=*/false,
33088 /*consume_paren=*/true);
33090 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33091 "num_teams", location
);
33093 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33094 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33095 OMP_CLAUSE_CHAIN (c
) = list
;
33101 thread_limit ( expression ) */
33104 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33105 location_t location
)
33109 matching_parens parens
;
33110 if (!parens
.require_open (parser
))
33113 t
= cp_parser_expression (parser
);
33115 if (t
== error_mark_node
33116 || !parens
.require_close (parser
))
33117 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33118 /*or_comma=*/false,
33119 /*consume_paren=*/true);
33121 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33122 "thread_limit", location
);
33124 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33125 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33126 OMP_CLAUSE_CHAIN (c
) = list
;
33132 aligned ( variable-list )
33133 aligned ( variable-list : constant-expression ) */
33136 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33138 tree nlist
, c
, alignment
= NULL_TREE
;
33141 matching_parens parens
;
33142 if (!parens
.require_open (parser
))
33145 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33150 alignment
= cp_parser_constant_expression (parser
);
33152 if (!parens
.require_close (parser
))
33153 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33154 /*or_comma=*/false,
33155 /*consume_paren=*/true);
33157 if (alignment
== error_mark_node
)
33158 alignment
= NULL_TREE
;
33161 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33162 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33168 linear ( variable-list )
33169 linear ( variable-list : expression )
33172 linear ( modifier ( variable-list ) )
33173 linear ( modifier ( variable-list ) : expression ) */
33176 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33179 tree nlist
, c
, step
= integer_one_node
;
33181 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33183 matching_parens parens
;
33184 if (!parens
.require_open (parser
))
33187 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33189 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33190 const char *p
= IDENTIFIER_POINTER (id
);
33192 if (strcmp ("ref", p
) == 0)
33193 kind
= OMP_CLAUSE_LINEAR_REF
;
33194 else if (strcmp ("val", p
) == 0)
33195 kind
= OMP_CLAUSE_LINEAR_VAL
;
33196 else if (strcmp ("uval", p
) == 0)
33197 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33198 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33199 cp_lexer_consume_token (parser
->lexer
);
33201 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33204 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33205 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33209 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33210 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33212 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33213 else if (!parens
.require_close (parser
))
33214 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33215 /*or_comma=*/false,
33216 /*consume_paren=*/true);
33223 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33224 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33226 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33227 cp_parser_parse_tentatively (parser
);
33228 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33229 /*check_dependency_p=*/true,
33230 /*template_p=*/NULL
,
33231 /*declarator_p=*/false,
33232 /*optional_p=*/false);
33233 if (step
!= error_mark_node
)
33234 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33235 if (step
== error_mark_node
)
33238 cp_parser_abort_tentative_parse (parser
);
33240 else if (!cp_parser_parse_definitely (parser
))
33244 step
= cp_parser_expression (parser
);
33246 if (!parens
.require_close (parser
))
33247 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33248 /*or_comma=*/false,
33249 /*consume_paren=*/true);
33251 if (step
== error_mark_node
)
33255 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33257 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33258 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33265 safelen ( constant-expression ) */
33268 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33269 location_t location
)
33273 matching_parens parens
;
33274 if (!parens
.require_open (parser
))
33277 t
= cp_parser_constant_expression (parser
);
33279 if (t
== error_mark_node
33280 || !parens
.require_close (parser
))
33281 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33282 /*or_comma=*/false,
33283 /*consume_paren=*/true);
33285 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33287 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33288 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33289 OMP_CLAUSE_CHAIN (c
) = list
;
33295 simdlen ( constant-expression ) */
33298 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33299 location_t location
)
33303 matching_parens parens
;
33304 if (!parens
.require_open (parser
))
33307 t
= cp_parser_constant_expression (parser
);
33309 if (t
== error_mark_node
33310 || !parens
.require_close (parser
))
33311 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33312 /*or_comma=*/false,
33313 /*consume_paren=*/true);
33315 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33317 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33318 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33319 OMP_CLAUSE_CHAIN (c
) = list
;
33326 identifier [+/- integer]
33327 vec , identifier [+/- integer]
33331 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33336 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33338 cp_parser_error (parser
, "expected identifier");
33342 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33344 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33345 tree t
, identifier
= cp_parser_identifier (parser
);
33346 tree addend
= NULL
;
33348 if (identifier
== error_mark_node
)
33349 t
= error_mark_node
;
33352 t
= cp_parser_lookup_name_simple
33353 (parser
, identifier
,
33354 cp_lexer_peek_token (parser
->lexer
)->location
);
33355 if (t
== error_mark_node
)
33356 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33361 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33363 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33365 addend
= integer_zero_node
;
33366 goto add_to_vector
;
33368 cp_lexer_consume_token (parser
->lexer
);
33370 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33372 cp_parser_error (parser
, "expected integer");
33376 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33377 if (TREE_CODE (addend
) != INTEGER_CST
)
33379 cp_parser_error (parser
, "expected integer");
33382 cp_lexer_consume_token (parser
->lexer
);
33385 if (t
!= error_mark_node
)
33387 vec
= tree_cons (addend
, t
, vec
);
33389 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33392 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33395 cp_lexer_consume_token (parser
->lexer
);
33398 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33400 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33401 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33402 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33403 OMP_CLAUSE_CHAIN (u
) = list
;
33410 depend ( depend-kind : variable-list )
33418 depend ( sink : vec ) */
33421 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33424 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33426 matching_parens parens
;
33427 if (!parens
.require_open (parser
))
33430 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33432 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33433 const char *p
= IDENTIFIER_POINTER (id
);
33435 if (strcmp ("in", p
) == 0)
33436 kind
= OMP_CLAUSE_DEPEND_IN
;
33437 else if (strcmp ("inout", p
) == 0)
33438 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33439 else if (strcmp ("out", p
) == 0)
33440 kind
= OMP_CLAUSE_DEPEND_OUT
;
33441 else if (strcmp ("source", p
) == 0)
33442 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33443 else if (strcmp ("sink", p
) == 0)
33444 kind
= OMP_CLAUSE_DEPEND_SINK
;
33451 cp_lexer_consume_token (parser
->lexer
);
33453 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33455 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33456 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33457 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33458 OMP_CLAUSE_CHAIN (c
) = list
;
33459 if (!parens
.require_close (parser
))
33460 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33461 /*or_comma=*/false,
33462 /*consume_paren=*/true);
33466 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33469 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33470 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33473 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33476 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33477 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33482 cp_parser_error (parser
, "invalid depend kind");
33484 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33485 /*or_comma=*/false,
33486 /*consume_paren=*/true);
33491 map ( map-kind : variable-list )
33492 map ( variable-list )
33495 alloc | to | from | tofrom
33499 alloc | to | from | tofrom | release | delete
33501 map ( always [,] map-kind: variable-list ) */
33504 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33507 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33508 bool always
= false;
33510 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33513 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33515 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33516 const char *p
= IDENTIFIER_POINTER (id
);
33518 if (strcmp ("always", p
) == 0)
33521 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33523 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33524 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33526 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33530 cp_lexer_consume_token (parser
->lexer
);
33532 cp_lexer_consume_token (parser
->lexer
);
33537 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33538 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33540 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33541 const char *p
= IDENTIFIER_POINTER (id
);
33543 if (strcmp ("alloc", p
) == 0)
33544 kind
= GOMP_MAP_ALLOC
;
33545 else if (strcmp ("to", p
) == 0)
33546 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33547 else if (strcmp ("from", p
) == 0)
33548 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33549 else if (strcmp ("tofrom", p
) == 0)
33550 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33551 else if (strcmp ("release", p
) == 0)
33552 kind
= GOMP_MAP_RELEASE
;
33555 cp_parser_error (parser
, "invalid map kind");
33556 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33557 /*or_comma=*/false,
33558 /*consume_paren=*/true);
33561 cp_lexer_consume_token (parser
->lexer
);
33562 cp_lexer_consume_token (parser
->lexer
);
33564 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33565 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33567 kind
= GOMP_MAP_DELETE
;
33568 cp_lexer_consume_token (parser
->lexer
);
33569 cp_lexer_consume_token (parser
->lexer
);
33572 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33575 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33576 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33582 device ( expression ) */
33585 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33586 location_t location
)
33590 matching_parens parens
;
33591 if (!parens
.require_open (parser
))
33594 t
= cp_parser_expression (parser
);
33596 if (t
== error_mark_node
33597 || !parens
.require_close (parser
))
33598 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33599 /*or_comma=*/false,
33600 /*consume_paren=*/true);
33602 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33603 "device", location
);
33605 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33606 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33607 OMP_CLAUSE_CHAIN (c
) = list
;
33613 dist_schedule ( static )
33614 dist_schedule ( static , expression ) */
33617 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33618 location_t location
)
33622 matching_parens parens
;
33623 if (!parens
.require_open (parser
))
33626 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33628 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33630 cp_lexer_consume_token (parser
->lexer
);
33632 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33634 cp_lexer_consume_token (parser
->lexer
);
33636 t
= cp_parser_assignment_expression (parser
);
33638 if (t
== error_mark_node
)
33640 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33642 if (!parens
.require_close (parser
))
33645 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33648 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33650 OMP_CLAUSE_CHAIN (c
) = list
;
33654 cp_parser_error (parser
, "invalid dist_schedule kind");
33656 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33657 /*or_comma=*/false,
33658 /*consume_paren=*/true);
33663 proc_bind ( proc-bind-kind )
33666 master | close | spread */
33669 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33670 location_t location
)
33673 enum omp_clause_proc_bind_kind kind
;
33675 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33678 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33680 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33681 const char *p
= IDENTIFIER_POINTER (id
);
33683 if (strcmp ("master", p
) == 0)
33684 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33685 else if (strcmp ("close", p
) == 0)
33686 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33687 else if (strcmp ("spread", p
) == 0)
33688 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33695 cp_lexer_consume_token (parser
->lexer
);
33696 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33699 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33700 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33702 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33703 OMP_CLAUSE_CHAIN (c
) = list
;
33707 cp_parser_error (parser
, "invalid depend kind");
33709 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33710 /*or_comma=*/false,
33711 /*consume_paren=*/true);
33716 async [( int-expr )] */
33719 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33722 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33724 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33726 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33728 matching_parens parens
;
33729 parens
.consume_open (parser
);
33731 t
= cp_parser_expression (parser
);
33732 if (t
== error_mark_node
33733 || !parens
.require_close (parser
))
33734 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33735 /*or_comma=*/false,
33736 /*consume_paren=*/true);
33739 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33741 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33742 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33743 OMP_CLAUSE_CHAIN (c
) = list
;
33749 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33750 is a bitmask in MASK. Return the list of clauses found. */
33753 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33754 const char *where
, cp_token
*pragma_tok
,
33755 bool finish_p
= true)
33757 tree clauses
= NULL
;
33760 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33763 pragma_omp_clause c_kind
;
33764 omp_clause_code code
;
33765 const char *c_name
;
33766 tree prev
= clauses
;
33768 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33769 cp_lexer_consume_token (parser
->lexer
);
33771 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33772 c_kind
= cp_parser_omp_clause_name (parser
);
33776 case PRAGMA_OACC_CLAUSE_ASYNC
:
33777 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33780 case PRAGMA_OACC_CLAUSE_AUTO
:
33781 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33785 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33786 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33787 c_name
= "collapse";
33789 case PRAGMA_OACC_CLAUSE_COPY
:
33790 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33793 case PRAGMA_OACC_CLAUSE_COPYIN
:
33794 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33797 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33798 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33799 c_name
= "copyout";
33801 case PRAGMA_OACC_CLAUSE_CREATE
:
33802 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33805 case PRAGMA_OACC_CLAUSE_DELETE
:
33806 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33809 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33810 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33811 c_name
= "default";
33813 case PRAGMA_OACC_CLAUSE_DEVICE
:
33814 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33817 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33818 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33819 c_name
= "deviceptr";
33821 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33822 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33823 c_name
= "device_resident";
33825 case PRAGMA_OACC_CLAUSE_FINALIZE
:
33826 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_FINALIZE
,
33828 c_name
= "finalize";
33830 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33831 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33833 c_name
= "firstprivate";
33835 case PRAGMA_OACC_CLAUSE_GANG
:
33837 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33840 case PRAGMA_OACC_CLAUSE_HOST
:
33841 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33844 case PRAGMA_OACC_CLAUSE_IF
:
33845 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33848 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
33849 clauses
= cp_parser_oacc_simple_clause (parser
,
33850 OMP_CLAUSE_IF_PRESENT
,
33852 c_name
= "if_present";
33854 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33855 clauses
= cp_parser_oacc_simple_clause (parser
,
33856 OMP_CLAUSE_INDEPENDENT
,
33858 c_name
= "independent";
33860 case PRAGMA_OACC_CLAUSE_LINK
:
33861 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33864 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33865 code
= OMP_CLAUSE_NUM_GANGS
;
33866 c_name
= "num_gangs";
33867 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33870 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33871 c_name
= "num_workers";
33872 code
= OMP_CLAUSE_NUM_WORKERS
;
33873 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33876 case PRAGMA_OACC_CLAUSE_PRESENT
:
33877 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33878 c_name
= "present";
33880 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33881 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33883 c_name
= "private";
33885 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33886 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33887 c_name
= "reduction";
33889 case PRAGMA_OACC_CLAUSE_SEQ
:
33890 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33894 case PRAGMA_OACC_CLAUSE_TILE
:
33895 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33898 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33899 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33901 c_name
= "use_device";
33903 case PRAGMA_OACC_CLAUSE_VECTOR
:
33905 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33908 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33909 c_name
= "vector_length";
33910 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33911 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33914 case PRAGMA_OACC_CLAUSE_WAIT
:
33915 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33918 case PRAGMA_OACC_CLAUSE_WORKER
:
33920 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33924 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33930 if (((mask
>> c_kind
) & 1) == 0)
33932 /* Remove the invalid clause(s) from the list to avoid
33933 confusing the rest of the compiler. */
33935 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33940 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33943 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33948 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33949 is a bitmask in MASK. Return the list of clauses found; the result
33950 of clause default goes in *pdefault. */
33953 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33954 const char *where
, cp_token
*pragma_tok
,
33955 bool finish_p
= true)
33957 tree clauses
= NULL
;
33959 cp_token
*token
= NULL
;
33961 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33963 pragma_omp_clause c_kind
;
33964 const char *c_name
;
33965 tree prev
= clauses
;
33967 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33968 cp_lexer_consume_token (parser
->lexer
);
33970 token
= cp_lexer_peek_token (parser
->lexer
);
33971 c_kind
= cp_parser_omp_clause_name (parser
);
33975 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33976 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33978 c_name
= "collapse";
33980 case PRAGMA_OMP_CLAUSE_COPYIN
:
33981 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33984 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33985 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33987 c_name
= "copyprivate";
33989 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33990 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33991 token
->location
, false);
33992 c_name
= "default";
33994 case PRAGMA_OMP_CLAUSE_FINAL
:
33995 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33998 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33999 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
34001 c_name
= "firstprivate";
34003 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
34004 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
34006 c_name
= "grainsize";
34008 case PRAGMA_OMP_CLAUSE_HINT
:
34009 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
34013 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
34014 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
34016 c_name
= "defaultmap";
34018 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
34019 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34021 c_name
= "use_device_ptr";
34023 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
34024 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
34026 c_name
= "is_device_ptr";
34028 case PRAGMA_OMP_CLAUSE_IF
:
34029 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
34033 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
34034 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
34036 c_name
= "lastprivate";
34038 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
34039 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
34041 c_name
= "mergeable";
34043 case PRAGMA_OMP_CLAUSE_NOWAIT
:
34044 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
34047 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
34048 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
34050 c_name
= "num_tasks";
34052 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
34053 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
34055 c_name
= "num_threads";
34057 case PRAGMA_OMP_CLAUSE_ORDERED
:
34058 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
34060 c_name
= "ordered";
34062 case PRAGMA_OMP_CLAUSE_PRIORITY
:
34063 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
34065 c_name
= "priority";
34067 case PRAGMA_OMP_CLAUSE_PRIVATE
:
34068 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34070 c_name
= "private";
34072 case PRAGMA_OMP_CLAUSE_REDUCTION
:
34073 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
34074 c_name
= "reduction";
34076 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
34077 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
34079 c_name
= "schedule";
34081 case PRAGMA_OMP_CLAUSE_SHARED
:
34082 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
34086 case PRAGMA_OMP_CLAUSE_UNTIED
:
34087 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
34091 case PRAGMA_OMP_CLAUSE_INBRANCH
:
34092 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
34093 clauses
, token
->location
);
34094 c_name
= "inbranch";
34096 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
34097 clauses
= cp_parser_omp_clause_branch (parser
,
34098 OMP_CLAUSE_NOTINBRANCH
,
34099 clauses
, token
->location
);
34100 c_name
= "notinbranch";
34102 case PRAGMA_OMP_CLAUSE_PARALLEL
:
34103 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
34104 clauses
, token
->location
);
34105 c_name
= "parallel";
34109 error_at (token
->location
, "%qs must be the first clause of %qs",
34114 case PRAGMA_OMP_CLAUSE_FOR
:
34115 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34116 clauses
, token
->location
);
34119 goto clause_not_first
;
34121 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34122 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34123 clauses
, token
->location
);
34124 c_name
= "sections";
34126 goto clause_not_first
;
34128 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34129 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34130 clauses
, token
->location
);
34131 c_name
= "taskgroup";
34133 goto clause_not_first
;
34135 case PRAGMA_OMP_CLAUSE_LINK
:
34136 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34139 case PRAGMA_OMP_CLAUSE_TO
:
34140 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34141 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34144 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34147 case PRAGMA_OMP_CLAUSE_FROM
:
34148 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34151 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34152 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34154 c_name
= "uniform";
34156 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34157 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34159 c_name
= "num_teams";
34161 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34162 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34164 c_name
= "thread_limit";
34166 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34167 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34168 c_name
= "aligned";
34170 case PRAGMA_OMP_CLAUSE_LINEAR
:
34172 bool declare_simd
= false;
34173 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34174 declare_simd
= true;
34175 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34179 case PRAGMA_OMP_CLAUSE_DEPEND
:
34180 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34184 case PRAGMA_OMP_CLAUSE_MAP
:
34185 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34188 case PRAGMA_OMP_CLAUSE_DEVICE
:
34189 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34193 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34194 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34196 c_name
= "dist_schedule";
34198 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34199 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34201 c_name
= "proc_bind";
34203 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34204 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34206 c_name
= "safelen";
34208 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34209 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34211 c_name
= "simdlen";
34213 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34214 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34216 c_name
= "nogroup";
34218 case PRAGMA_OMP_CLAUSE_THREADS
:
34220 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34221 clauses
, token
->location
);
34222 c_name
= "threads";
34224 case PRAGMA_OMP_CLAUSE_SIMD
:
34226 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34227 clauses
, token
->location
);
34231 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34237 if (((mask
>> c_kind
) & 1) == 0)
34239 /* Remove the invalid clause(s) from the list to avoid
34240 confusing the rest of the compiler. */
34242 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34246 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34249 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34250 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34252 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34261 In practice, we're also interested in adding the statement to an
34262 outer node. So it is convenient if we work around the fact that
34263 cp_parser_statement calls add_stmt. */
34266 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34268 unsigned save
= parser
->in_statement
;
34270 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34271 This preserves the "not within loop or switch" style error messages
34272 for nonsense cases like
34278 if (parser
->in_statement
)
34279 parser
->in_statement
= IN_OMP_BLOCK
;
34285 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34287 parser
->in_statement
= save
;
34291 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34293 tree stmt
= begin_omp_structured_block ();
34294 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34296 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34298 cp_parser_end_omp_structured_block (parser
, save
);
34299 return finish_omp_structured_block (stmt
);
34303 # pragma omp atomic new-line
34307 x binop= expr | x++ | ++x | x-- | --x
34309 +, *, -, /, &, ^, |, <<, >>
34311 where x is an lvalue expression with scalar type.
34314 # pragma omp atomic new-line
34317 # pragma omp atomic read new-line
34320 # pragma omp atomic write new-line
34323 # pragma omp atomic update new-line
34326 # pragma omp atomic capture new-line
34329 # pragma omp atomic capture new-line
34337 expression-stmt | x = x binop expr
34339 v = expression-stmt
34341 { v = x; update-stmt; } | { update-stmt; v = x; }
34345 expression-stmt | x = x binop expr | x = expr binop x
34349 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34351 where x and v are lvalue expressions with scalar type. */
34354 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34356 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34357 tree rhs1
= NULL_TREE
, orig_lhs
;
34358 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34359 bool structured_block
= false;
34360 bool seq_cst
= false;
34362 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34364 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34365 const char *p
= IDENTIFIER_POINTER (id
);
34367 if (!strcmp (p
, "seq_cst"))
34370 cp_lexer_consume_token (parser
->lexer
);
34371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34372 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34373 cp_lexer_consume_token (parser
->lexer
);
34376 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34378 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34379 const char *p
= IDENTIFIER_POINTER (id
);
34381 if (!strcmp (p
, "read"))
34382 code
= OMP_ATOMIC_READ
;
34383 else if (!strcmp (p
, "write"))
34385 else if (!strcmp (p
, "update"))
34387 else if (!strcmp (p
, "capture"))
34388 code
= OMP_ATOMIC_CAPTURE_NEW
;
34392 cp_lexer_consume_token (parser
->lexer
);
34396 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34397 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34398 cp_lexer_consume_token (parser
->lexer
);
34400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34402 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34403 const char *p
= IDENTIFIER_POINTER (id
);
34405 if (!strcmp (p
, "seq_cst"))
34408 cp_lexer_consume_token (parser
->lexer
);
34412 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34416 case OMP_ATOMIC_READ
:
34417 case NOP_EXPR
: /* atomic write */
34418 v
= cp_parser_unary_expression (parser
);
34419 if (v
== error_mark_node
)
34421 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34423 if (code
== NOP_EXPR
)
34424 lhs
= cp_parser_expression (parser
);
34426 lhs
= cp_parser_unary_expression (parser
);
34427 if (lhs
== error_mark_node
)
34429 if (code
== NOP_EXPR
)
34431 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34439 case OMP_ATOMIC_CAPTURE_NEW
:
34440 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34442 cp_lexer_consume_token (parser
->lexer
);
34443 structured_block
= true;
34447 v
= cp_parser_unary_expression (parser
);
34448 if (v
== error_mark_node
)
34450 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34458 lhs
= cp_parser_unary_expression (parser
);
34460 switch (TREE_CODE (lhs
))
34465 case POSTINCREMENT_EXPR
:
34466 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34467 code
= OMP_ATOMIC_CAPTURE_OLD
;
34469 case PREINCREMENT_EXPR
:
34470 lhs
= TREE_OPERAND (lhs
, 0);
34471 opcode
= PLUS_EXPR
;
34472 rhs
= integer_one_node
;
34475 case POSTDECREMENT_EXPR
:
34476 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34477 code
= OMP_ATOMIC_CAPTURE_OLD
;
34479 case PREDECREMENT_EXPR
:
34480 lhs
= TREE_OPERAND (lhs
, 0);
34481 opcode
= MINUS_EXPR
;
34482 rhs
= integer_one_node
;
34485 case COMPOUND_EXPR
:
34486 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34487 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34488 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34489 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34490 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34491 (TREE_OPERAND (lhs
, 1), 0), 0)))
34493 /* Undo effects of boolean_increment for post {in,de}crement. */
34494 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34497 if (TREE_CODE (lhs
) == MODIFY_EXPR
34498 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34500 /* Undo effects of boolean_increment. */
34501 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34503 /* This is pre or post increment. */
34504 rhs
= TREE_OPERAND (lhs
, 1);
34505 lhs
= TREE_OPERAND (lhs
, 0);
34507 if (code
== OMP_ATOMIC_CAPTURE_NEW
34508 && !structured_block
34509 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34510 code
= OMP_ATOMIC_CAPTURE_OLD
;
34516 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34519 opcode
= MULT_EXPR
;
34522 opcode
= TRUNC_DIV_EXPR
;
34525 opcode
= PLUS_EXPR
;
34528 opcode
= MINUS_EXPR
;
34530 case CPP_LSHIFT_EQ
:
34531 opcode
= LSHIFT_EXPR
;
34533 case CPP_RSHIFT_EQ
:
34534 opcode
= RSHIFT_EXPR
;
34537 opcode
= BIT_AND_EXPR
;
34540 opcode
= BIT_IOR_EXPR
;
34543 opcode
= BIT_XOR_EXPR
;
34546 enum cp_parser_prec oprec
;
34548 cp_lexer_consume_token (parser
->lexer
);
34549 cp_parser_parse_tentatively (parser
);
34550 rhs1
= cp_parser_simple_cast_expression (parser
);
34551 if (rhs1
== error_mark_node
)
34553 cp_parser_abort_tentative_parse (parser
);
34554 cp_parser_simple_cast_expression (parser
);
34557 token
= cp_lexer_peek_token (parser
->lexer
);
34558 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34560 cp_parser_abort_tentative_parse (parser
);
34561 cp_parser_parse_tentatively (parser
);
34562 rhs
= cp_parser_binary_expression (parser
, false, true,
34563 PREC_NOT_OPERATOR
, NULL
);
34564 if (rhs
== error_mark_node
)
34566 cp_parser_abort_tentative_parse (parser
);
34567 cp_parser_binary_expression (parser
, false, true,
34568 PREC_NOT_OPERATOR
, NULL
);
34571 switch (TREE_CODE (rhs
))
34574 case TRUNC_DIV_EXPR
:
34583 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34585 if (cp_parser_parse_definitely (parser
))
34587 opcode
= TREE_CODE (rhs
);
34588 rhs1
= TREE_OPERAND (rhs
, 0);
34589 rhs
= TREE_OPERAND (rhs
, 1);
34599 cp_parser_abort_tentative_parse (parser
);
34600 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34602 rhs
= cp_parser_expression (parser
);
34603 if (rhs
== error_mark_node
)
34609 cp_parser_error (parser
,
34610 "invalid form of %<#pragma omp atomic%>");
34613 if (!cp_parser_parse_definitely (parser
))
34615 switch (token
->type
)
34617 case CPP_SEMICOLON
:
34618 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34620 code
= OMP_ATOMIC_CAPTURE_OLD
;
34625 cp_lexer_consume_token (parser
->lexer
);
34628 else if (structured_block
)
34635 cp_parser_error (parser
,
34636 "invalid form of %<#pragma omp atomic%>");
34639 opcode
= MULT_EXPR
;
34642 opcode
= TRUNC_DIV_EXPR
;
34645 opcode
= PLUS_EXPR
;
34648 opcode
= MINUS_EXPR
;
34651 opcode
= LSHIFT_EXPR
;
34654 opcode
= RSHIFT_EXPR
;
34657 opcode
= BIT_AND_EXPR
;
34660 opcode
= BIT_IOR_EXPR
;
34663 opcode
= BIT_XOR_EXPR
;
34666 cp_parser_error (parser
,
34667 "invalid operator for %<#pragma omp atomic%>");
34670 oprec
= TOKEN_PRECEDENCE (token
);
34671 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34672 if (commutative_tree_code (opcode
))
34673 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34674 cp_lexer_consume_token (parser
->lexer
);
34675 rhs
= cp_parser_binary_expression (parser
, false, false,
34677 if (rhs
== error_mark_node
)
34682 cp_parser_error (parser
,
34683 "invalid operator for %<#pragma omp atomic%>");
34686 cp_lexer_consume_token (parser
->lexer
);
34688 rhs
= cp_parser_expression (parser
);
34689 if (rhs
== error_mark_node
)
34694 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34696 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34698 v
= cp_parser_unary_expression (parser
);
34699 if (v
== error_mark_node
)
34701 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34703 lhs1
= cp_parser_unary_expression (parser
);
34704 if (lhs1
== error_mark_node
)
34707 if (structured_block
)
34709 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34710 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34713 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34714 if (!structured_block
)
34715 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34719 cp_parser_skip_to_end_of_block_or_statement (parser
);
34720 if (structured_block
)
34722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34723 cp_lexer_consume_token (parser
->lexer
);
34724 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34726 cp_parser_skip_to_end_of_block_or_statement (parser
);
34727 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34728 cp_lexer_consume_token (parser
->lexer
);
34735 # pragma omp barrier new-line */
34738 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34740 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34741 finish_omp_barrier ();
34745 # pragma omp critical [(name)] new-line
34749 # pragma omp critical [(name) [hint(expression)]] new-line
34750 structured-block */
34752 #define OMP_CRITICAL_CLAUSE_MASK \
34753 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34756 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34758 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34760 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34762 matching_parens parens
;
34763 parens
.consume_open (parser
);
34765 name
= cp_parser_identifier (parser
);
34767 if (name
== error_mark_node
34768 || !parens
.require_close (parser
))
34769 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34770 /*or_comma=*/false,
34771 /*consume_paren=*/true);
34772 if (name
== error_mark_node
)
34775 clauses
= cp_parser_omp_all_clauses (parser
,
34776 OMP_CRITICAL_CLAUSE_MASK
,
34777 "#pragma omp critical", pragma_tok
);
34780 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34782 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34783 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34787 # pragma omp flush flush-vars[opt] new-line
34790 ( variable-list ) */
34793 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34795 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34796 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34797 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34799 finish_omp_flush ();
34802 /* Helper function, to parse omp for increment expression. */
34805 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34807 tree cond
= cp_parser_binary_expression (parser
, false, true,
34808 PREC_NOT_OPERATOR
, NULL
);
34809 if (cond
== error_mark_node
34810 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34812 cp_parser_skip_to_end_of_statement (parser
);
34813 return error_mark_node
;
34816 switch (TREE_CODE (cond
))
34824 /* Fall through: OpenMP disallows NE_EXPR. */
34825 gcc_fallthrough ();
34827 return error_mark_node
;
34830 /* If decl is an iterator, preserve LHS and RHS of the relational
34831 expr until finish_omp_for. */
34833 && (type_dependent_expression_p (decl
)
34834 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34837 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
34839 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34840 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34841 /*overload=*/NULL
, tf_warning_or_error
);
34844 /* Helper function, to parse omp for increment expression. */
34847 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34849 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34855 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34857 op
= (token
->type
== CPP_PLUS_PLUS
34858 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34859 cp_lexer_consume_token (parser
->lexer
);
34860 lhs
= cp_parser_simple_cast_expression (parser
);
34862 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34863 return error_mark_node
;
34864 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34867 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34869 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34870 return error_mark_node
;
34872 token
= cp_lexer_peek_token (parser
->lexer
);
34873 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34875 op
= (token
->type
== CPP_PLUS_PLUS
34876 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34877 cp_lexer_consume_token (parser
->lexer
);
34878 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34881 op
= cp_parser_assignment_operator_opt (parser
);
34882 if (op
== ERROR_MARK
)
34883 return error_mark_node
;
34885 if (op
!= NOP_EXPR
)
34887 rhs
= cp_parser_assignment_expression (parser
);
34888 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34889 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34892 lhs
= cp_parser_binary_expression (parser
, false, false,
34893 PREC_ADDITIVE_EXPRESSION
, NULL
);
34894 token
= cp_lexer_peek_token (parser
->lexer
);
34895 decl_first
= (lhs
== decl
34896 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34899 if (token
->type
!= CPP_PLUS
34900 && token
->type
!= CPP_MINUS
)
34901 return error_mark_node
;
34905 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34906 cp_lexer_consume_token (parser
->lexer
);
34907 rhs
= cp_parser_binary_expression (parser
, false, false,
34908 PREC_ADDITIVE_EXPRESSION
, NULL
);
34909 token
= cp_lexer_peek_token (parser
->lexer
);
34910 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34912 if (lhs
== NULL_TREE
)
34914 if (op
== PLUS_EXPR
)
34917 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34918 tf_warning_or_error
);
34921 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34922 ERROR_MARK
, NULL
, tf_warning_or_error
);
34925 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34930 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34931 || op
== MINUS_EXPR
)
34932 return error_mark_node
;
34933 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34936 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34938 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34941 /* Parse the initialization statement of an OpenMP for loop.
34943 Return true if the resulting construct should have an
34944 OMP_CLAUSE_PRIVATE added to it. */
34947 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34948 tree
&this_pre_body
,
34949 vec
<tree
, va_gc
> *&for_block
,
34955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34958 tree add_private_clause
= NULL_TREE
;
34960 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34964 integer-type var = lb
34965 random-access-iterator-type var = lb
34966 pointer-type var = lb
34968 cp_decl_specifier_seq type_specifiers
;
34970 /* First, try to parse as an initialized declaration. See
34971 cp_parser_condition, from whence the bulk of this is copied. */
34973 cp_parser_parse_tentatively (parser
);
34974 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34975 /*is_trailing_return=*/false,
34977 if (cp_parser_parse_definitely (parser
))
34979 /* If parsing a type specifier seq succeeded, then this
34980 MUST be a initialized declaration. */
34981 tree asm_specification
, attributes
;
34982 cp_declarator
*declarator
;
34984 declarator
= cp_parser_declarator (parser
,
34985 CP_PARSER_DECLARATOR_NAMED
,
34986 /*ctor_dtor_or_conv_p=*/NULL
,
34987 /*parenthesized_p=*/NULL
,
34988 /*member_p=*/false,
34989 /*friend_p=*/false);
34990 attributes
= cp_parser_attributes_opt (parser
);
34991 asm_specification
= cp_parser_asm_specification_opt (parser
);
34993 if (declarator
== cp_error_declarator
)
34994 cp_parser_skip_to_end_of_statement (parser
);
34998 tree pushed_scope
, auto_node
;
35000 decl
= start_decl (declarator
, &type_specifiers
,
35001 SD_INITIALIZED
, attributes
,
35002 /*prefix_attributes=*/NULL_TREE
,
35005 auto_node
= type_uses_auto (TREE_TYPE (decl
));
35006 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
35008 if (cp_lexer_next_token_is (parser
->lexer
,
35010 error ("parenthesized initialization is not allowed in "
35011 "OpenMP %<for%> loop");
35013 /* Trigger an error. */
35014 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35016 init
= error_mark_node
;
35017 cp_parser_skip_to_end_of_statement (parser
);
35019 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
35020 || type_dependent_expression_p (decl
)
35023 bool is_direct_init
, is_non_constant_init
;
35025 init
= cp_parser_initializer (parser
,
35027 &is_non_constant_init
);
35032 = do_auto_deduction (TREE_TYPE (decl
), init
,
35035 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
35036 && !type_dependent_expression_p (decl
))
35040 cp_finish_decl (decl
, init
, !is_non_constant_init
,
35042 LOOKUP_ONLYCONVERTING
);
35044 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
35046 vec_safe_push (for_block
, this_pre_body
);
35051 init
= pop_stmt_list (this_pre_body
);
35052 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
35054 tree_stmt_iterator i
= tsi_start (init
);
35055 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35056 while (!tsi_end_p (i
))
35058 tree t
= tsi_stmt (i
);
35059 if (TREE_CODE (t
) == DECL_EXPR
35060 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
35063 vec_safe_push (for_block
, t
);
35068 if (tsi_one_before_end_p (i
))
35070 tree t
= tsi_stmt (i
);
35072 free_stmt_list (init
);
35077 this_pre_body
= NULL_TREE
;
35082 cp_lexer_consume_token (parser
->lexer
);
35083 init
= cp_parser_assignment_expression (parser
);
35086 if (TYPE_REF_P (TREE_TYPE (decl
)))
35087 init
= error_mark_node
;
35089 cp_finish_decl (decl
, NULL_TREE
,
35090 /*init_const_expr_p=*/false,
35092 LOOKUP_ONLYCONVERTING
);
35096 pop_scope (pushed_scope
);
35102 /* If parsing a type specifier sequence failed, then
35103 this MUST be a simple expression. */
35104 cp_parser_parse_tentatively (parser
);
35105 decl
= cp_parser_primary_expression (parser
, false, false,
35107 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
35108 if (!cp_parser_error_occurred (parser
)
35110 && (TREE_CODE (decl
) == COMPONENT_REF
35111 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
35113 cp_parser_abort_tentative_parse (parser
);
35114 cp_parser_parse_tentatively (parser
);
35115 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35116 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35117 /*check_dependency_p=*/true,
35118 /*template_p=*/NULL
,
35119 /*declarator_p=*/false,
35120 /*optional_p=*/false);
35121 if (name
!= error_mark_node
35122 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35124 decl
= cp_parser_lookup_name_simple (parser
, name
,
35126 if (TREE_CODE (decl
) == FIELD_DECL
)
35127 add_private_clause
= omp_privatize_field (decl
, false);
35129 cp_parser_abort_tentative_parse (parser
);
35130 cp_parser_parse_tentatively (parser
);
35131 decl
= cp_parser_primary_expression (parser
, false, false,
35134 if (!cp_parser_error_occurred (parser
)
35137 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35141 cp_parser_parse_definitely (parser
);
35142 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35143 rhs
= cp_parser_assignment_expression (parser
);
35145 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35148 tf_warning_or_error
));
35149 if (!add_private_clause
)
35150 add_private_clause
= decl
;
35155 cp_parser_abort_tentative_parse (parser
);
35156 init
= cp_parser_expression (parser
);
35159 if (TREE_CODE (init
) == MODIFY_EXPR
35160 || TREE_CODE (init
) == MODOP_EXPR
)
35161 real_decl
= TREE_OPERAND (init
, 0);
35165 return add_private_clause
;
35168 /* Parse the restricted form of the for statement allowed by OpenMP. */
35171 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35172 tree
*cclauses
, bool *if_p
)
35174 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35175 tree real_decl
, initv
, condv
, incrv
, declv
;
35176 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35177 location_t loc_first
;
35178 bool collapse_err
= false;
35179 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35180 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35181 auto_vec
<tree
, 4> orig_inits
;
35182 bool tiling
= false;
35184 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35185 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35186 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35187 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35190 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35192 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35193 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35196 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35199 if (ordered
&& ordered
< collapse
)
35201 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35202 "%<ordered%> clause parameter is less than %<collapse%>");
35203 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35204 = build_int_cst (NULL_TREE
, collapse
);
35205 ordered
= collapse
;
35209 for (tree
*pc
= &clauses
; *pc
; )
35210 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35212 error_at (OMP_CLAUSE_LOCATION (*pc
),
35213 "%<linear%> clause may not be specified together "
35214 "with %<ordered%> clause with a parameter");
35215 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35218 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35221 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35222 count
= ordered
? ordered
: collapse
;
35224 declv
= make_tree_vec (count
);
35225 initv
= make_tree_vec (count
);
35226 condv
= make_tree_vec (count
);
35227 incrv
= make_tree_vec (count
);
35229 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35231 for (i
= 0; i
< count
; i
++)
35233 int bracecount
= 0;
35234 tree add_private_clause
= NULL_TREE
;
35237 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35240 cp_parser_error (parser
, "for statement expected");
35243 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35245 matching_parens parens
;
35246 if (!parens
.require_open (parser
))
35249 init
= orig_init
= decl
= real_decl
= NULL
;
35250 this_pre_body
= push_stmt_list ();
35253 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35254 init
, orig_init
, decl
, real_decl
);
35256 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35259 this_pre_body
= pop_stmt_list (this_pre_body
);
35263 pre_body
= push_stmt_list ();
35265 add_stmt (this_pre_body
);
35266 pre_body
= pop_stmt_list (pre_body
);
35269 pre_body
= this_pre_body
;
35274 if (cclauses
!= NULL
35275 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35276 && real_decl
!= NULL_TREE
)
35279 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35280 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35281 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35283 error_at (loc
, "iteration variable %qD"
35284 " should not be firstprivate", real_decl
);
35285 *c
= OMP_CLAUSE_CHAIN (*c
);
35287 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35288 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35290 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35292 *c
= OMP_CLAUSE_CHAIN (*c
);
35293 if (code
== OMP_SIMD
)
35295 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35296 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35300 OMP_CLAUSE_CHAIN (l
) = clauses
;
35303 add_private_clause
= NULL_TREE
;
35307 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35308 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35309 add_private_clause
= NULL_TREE
;
35310 c
= &OMP_CLAUSE_CHAIN (*c
);
35314 if (add_private_clause
)
35317 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35319 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35320 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35321 && OMP_CLAUSE_DECL (c
) == decl
)
35323 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35324 && OMP_CLAUSE_DECL (c
) == decl
)
35325 error_at (loc
, "iteration variable %qD "
35326 "should not be firstprivate",
35328 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35329 && OMP_CLAUSE_DECL (c
) == decl
)
35330 error_at (loc
, "iteration variable %qD should not be reduction",
35335 if (code
!= OMP_SIMD
)
35336 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35337 else if (collapse
== 1)
35338 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35340 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35341 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35342 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35345 OMP_CLAUSE_CHAIN (c
) = clauses
;
35347 /* For linear, signal that we need to fill up
35348 the so far unknown linear step. */
35349 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35350 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35356 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35357 cond
= cp_parser_omp_for_cond (parser
, decl
);
35358 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35361 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35363 /* If decl is an iterator, preserve the operator on decl
35364 until finish_omp_for. */
35366 && ((processing_template_decl
35367 && (TREE_TYPE (real_decl
) == NULL_TREE
35368 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
35369 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35370 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35372 incr
= cp_parser_expression (parser
);
35373 if (!EXPR_HAS_LOCATION (incr
))
35374 protected_set_expr_location (incr
, input_location
);
35377 if (!parens
.require_close (parser
))
35378 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35379 /*or_comma=*/false,
35380 /*consume_paren=*/true);
35382 TREE_VEC_ELT (declv
, i
) = decl
;
35383 TREE_VEC_ELT (initv
, i
) = init
;
35384 TREE_VEC_ELT (condv
, i
) = cond
;
35385 TREE_VEC_ELT (incrv
, i
) = incr
;
35388 orig_inits
.safe_grow_cleared (i
+ 1);
35389 orig_inits
[i
] = orig_init
;
35392 if (i
== count
- 1)
35395 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35396 in between the collapsed for loops to be still considered perfectly
35397 nested. Hopefully the final version clarifies this.
35398 For now handle (multiple) {'s and empty statements. */
35399 cp_parser_parse_tentatively (parser
);
35402 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35404 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35406 cp_lexer_consume_token (parser
->lexer
);
35409 else if (bracecount
35410 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35411 cp_lexer_consume_token (parser
->lexer
);
35414 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35415 error_at (loc
, "not enough for loops to collapse");
35416 collapse_err
= true;
35417 cp_parser_abort_tentative_parse (parser
);
35425 cp_parser_parse_definitely (parser
);
35426 nbraces
+= bracecount
;
35433 /* Note that we saved the original contents of this flag when we entered
35434 the structured block, and so we don't need to re-save it here. */
35435 parser
->in_statement
= IN_OMP_FOR
;
35437 /* Note that the grammar doesn't call for a structured block here,
35438 though the loop as a whole is a structured block. */
35439 body
= push_stmt_list ();
35440 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35441 body
= pop_stmt_list (body
);
35443 if (declv
== NULL_TREE
)
35446 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35447 body
, pre_body
, &orig_inits
, clauses
);
35451 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35453 cp_lexer_consume_token (parser
->lexer
);
35456 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35457 cp_lexer_consume_token (parser
->lexer
);
35462 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35463 "collapsed loops not perfectly nested");
35465 collapse_err
= true;
35466 cp_parser_statement_seq_opt (parser
, NULL
);
35467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35472 while (!for_block
->is_empty ())
35474 tree t
= for_block
->pop ();
35475 if (TREE_CODE (t
) == STATEMENT_LIST
)
35476 add_stmt (pop_stmt_list (t
));
35480 release_tree_vector (for_block
);
35485 /* Helper function for OpenMP parsing, split clauses and call
35486 finish_omp_clauses on each of the set of clauses afterwards. */
35489 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35490 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35493 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35494 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35496 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35500 #pragma omp simd simd-clause[optseq] new-line
35503 #define OMP_SIMD_CLAUSE_MASK \
35504 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35514 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35515 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35518 tree clauses
, sb
, ret
;
35520 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35522 strcat (p_name
, " simd");
35523 mask
|= OMP_SIMD_CLAUSE_MASK
;
35525 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35529 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35530 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35531 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35532 OMP_CLAUSE_ORDERED
);
35533 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35535 error_at (OMP_CLAUSE_LOCATION (c
),
35536 "%<ordered%> clause with parameter may not be specified "
35537 "on %qs construct", p_name
);
35538 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35542 sb
= begin_omp_structured_block ();
35543 save
= cp_parser_begin_omp_structured_block (parser
);
35545 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35547 cp_parser_end_omp_structured_block (parser
, save
);
35548 add_stmt (finish_omp_structured_block (sb
));
35554 #pragma omp for for-clause[optseq] new-line
35558 #pragma omp for simd for-simd-clause[optseq] new-line
35561 #define OMP_FOR_CLAUSE_MASK \
35562 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35573 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35574 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35577 tree clauses
, sb
, ret
;
35579 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35581 strcat (p_name
, " for");
35582 mask
|= OMP_FOR_CLAUSE_MASK
;
35583 /* parallel for{, simd} disallows nowait clause, but for
35584 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35585 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35586 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35587 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35588 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35589 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35591 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35593 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35594 const char *p
= IDENTIFIER_POINTER (id
);
35596 if (strcmp (p
, "simd") == 0)
35598 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35599 if (cclauses
== NULL
)
35600 cclauses
= cclauses_buf
;
35602 cp_lexer_consume_token (parser
->lexer
);
35603 if (!flag_openmp
) /* flag_openmp_simd */
35604 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35606 sb
= begin_omp_structured_block ();
35607 save
= cp_parser_begin_omp_structured_block (parser
);
35608 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35610 cp_parser_end_omp_structured_block (parser
, save
);
35611 tree body
= finish_omp_structured_block (sb
);
35614 ret
= make_node (OMP_FOR
);
35615 TREE_TYPE (ret
) = void_type_node
;
35616 OMP_FOR_BODY (ret
) = body
;
35617 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35618 SET_EXPR_LOCATION (ret
, loc
);
35623 if (!flag_openmp
) /* flag_openmp_simd */
35625 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35629 /* Composite distribute parallel for disallows linear clause. */
35630 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35631 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35633 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35637 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35638 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35641 sb
= begin_omp_structured_block ();
35642 save
= cp_parser_begin_omp_structured_block (parser
);
35644 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35646 cp_parser_end_omp_structured_block (parser
, save
);
35647 add_stmt (finish_omp_structured_block (sb
));
35653 # pragma omp master new-line
35654 structured-block */
35657 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35659 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35660 return c_finish_omp_master (input_location
,
35661 cp_parser_omp_structured_block (parser
, if_p
));
35665 # pragma omp ordered new-line
35669 # pragma omp ordered ordered-clauses new-line
35670 structured-block */
35672 #define OMP_ORDERED_CLAUSE_MASK \
35673 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35676 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35677 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35680 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35681 enum pragma_context context
, bool *if_p
)
35683 location_t loc
= pragma_tok
->location
;
35685 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35687 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35688 const char *p
= IDENTIFIER_POINTER (id
);
35690 if (strcmp (p
, "depend") == 0)
35692 if (!flag_openmp
) /* flag_openmp_simd */
35694 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35697 if (context
== pragma_stmt
)
35699 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35700 "%<depend%> clause may only be used in compound "
35702 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35706 = cp_parser_omp_all_clauses (parser
,
35707 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35708 "#pragma omp ordered", pragma_tok
);
35709 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35715 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35716 "#pragma omp ordered", pragma_tok
);
35718 if (!flag_openmp
/* flag_openmp_simd */
35719 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35722 c_finish_omp_ordered (loc
, clauses
,
35723 cp_parser_omp_structured_block (parser
, if_p
));
35730 { section-sequence }
35733 section-directive[opt] structured-block
35734 section-sequence section-directive structured-block */
35737 cp_parser_omp_sections_scope (cp_parser
*parser
)
35739 tree stmt
, substmt
;
35740 bool error_suppress
= false;
35743 matching_braces braces
;
35744 if (!braces
.require_open (parser
))
35747 stmt
= push_stmt_list ();
35749 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35750 != PRAGMA_OMP_SECTION
)
35752 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35753 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35754 add_stmt (substmt
);
35759 tok
= cp_lexer_peek_token (parser
->lexer
);
35760 if (tok
->type
== CPP_CLOSE_BRACE
)
35762 if (tok
->type
== CPP_EOF
)
35765 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35767 cp_lexer_consume_token (parser
->lexer
);
35768 cp_parser_require_pragma_eol (parser
, tok
);
35769 error_suppress
= false;
35771 else if (!error_suppress
)
35773 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35774 error_suppress
= true;
35777 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35778 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35779 add_stmt (substmt
);
35781 braces
.require_close (parser
);
35783 substmt
= pop_stmt_list (stmt
);
35785 stmt
= make_node (OMP_SECTIONS
);
35786 TREE_TYPE (stmt
) = void_type_node
;
35787 OMP_SECTIONS_BODY (stmt
) = substmt
;
35794 # pragma omp sections sections-clause[optseq] newline
35797 #define OMP_SECTIONS_CLAUSE_MASK \
35798 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35805 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35806 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35809 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35811 strcat (p_name
, " sections");
35812 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35814 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35816 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35820 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35821 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35824 ret
= cp_parser_omp_sections_scope (parser
);
35826 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35832 # pragma omp parallel parallel-clause[optseq] new-line
35834 # pragma omp parallel for parallel-for-clause[optseq] new-line
35836 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35840 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35841 structured-block */
35843 #define OMP_PARALLEL_CLAUSE_MASK \
35844 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35855 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35856 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35859 tree stmt
, clauses
, block
;
35861 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35863 strcat (p_name
, " parallel");
35864 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35865 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35866 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35867 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35868 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35870 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35872 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35873 if (cclauses
== NULL
)
35874 cclauses
= cclauses_buf
;
35876 cp_lexer_consume_token (parser
->lexer
);
35877 if (!flag_openmp
) /* flag_openmp_simd */
35878 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35880 block
= begin_omp_parallel ();
35881 save
= cp_parser_begin_omp_structured_block (parser
);
35882 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35884 cp_parser_end_omp_structured_block (parser
, save
);
35885 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35887 if (ret
== NULL_TREE
)
35889 OMP_PARALLEL_COMBINED (stmt
) = 1;
35892 /* When combined with distribute, parallel has to be followed by for.
35893 #pragma omp target parallel is allowed though. */
35895 && (mask
& (OMP_CLAUSE_MASK_1
35896 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35898 error_at (loc
, "expected %<for%> after %qs", p_name
);
35899 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35902 else if (!flag_openmp
) /* flag_openmp_simd */
35904 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35907 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35909 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35910 const char *p
= IDENTIFIER_POINTER (id
);
35911 if (strcmp (p
, "sections") == 0)
35913 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35914 cclauses
= cclauses_buf
;
35916 cp_lexer_consume_token (parser
->lexer
);
35917 block
= begin_omp_parallel ();
35918 save
= cp_parser_begin_omp_structured_block (parser
);
35919 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35920 cp_parser_end_omp_structured_block (parser
, save
);
35921 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35923 OMP_PARALLEL_COMBINED (stmt
) = 1;
35928 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35932 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35933 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35936 block
= begin_omp_parallel ();
35937 save
= cp_parser_begin_omp_structured_block (parser
);
35938 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35939 cp_parser_end_omp_structured_block (parser
, save
);
35940 stmt
= finish_omp_parallel (clauses
, block
);
35945 # pragma omp single single-clause[optseq] new-line
35946 structured-block */
35948 #define OMP_SINGLE_CLAUSE_MASK \
35949 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35955 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35957 tree stmt
= make_node (OMP_SINGLE
);
35958 TREE_TYPE (stmt
) = void_type_node
;
35960 OMP_SINGLE_CLAUSES (stmt
)
35961 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35962 "#pragma omp single", pragma_tok
);
35963 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35965 return add_stmt (stmt
);
35969 # pragma omp task task-clause[optseq] new-line
35970 structured-block */
35972 #define OMP_TASK_CLAUSE_MASK \
35973 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35985 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35987 tree clauses
, block
;
35990 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35991 "#pragma omp task", pragma_tok
);
35992 block
= begin_omp_task ();
35993 save
= cp_parser_begin_omp_structured_block (parser
);
35994 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35995 cp_parser_end_omp_structured_block (parser
, save
);
35996 return finish_omp_task (clauses
, block
);
36000 # pragma omp taskwait new-line */
36003 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
36005 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36006 finish_omp_taskwait ();
36010 # pragma omp taskyield new-line */
36013 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
36015 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36016 finish_omp_taskyield ();
36020 # pragma omp taskgroup new-line
36021 structured-block */
36024 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36026 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36027 return c_finish_omp_taskgroup (input_location
,
36028 cp_parser_omp_structured_block (parser
,
36034 # pragma omp threadprivate (variable-list) */
36037 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
36041 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36042 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36044 finish_omp_threadprivate (vars
);
36048 # pragma omp cancel cancel-clause[optseq] new-line */
36050 #define OMP_CANCEL_CLAUSE_MASK \
36051 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36058 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
36060 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
36061 "#pragma omp cancel", pragma_tok
);
36062 finish_omp_cancel (clauses
);
36066 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36068 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36069 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36075 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
36076 enum pragma_context context
)
36079 bool point_seen
= false;
36081 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36083 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36084 const char *p
= IDENTIFIER_POINTER (id
);
36086 if (strcmp (p
, "point") == 0)
36088 cp_lexer_consume_token (parser
->lexer
);
36094 cp_parser_error (parser
, "expected %<point%>");
36095 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36099 if (context
!= pragma_compound
)
36101 if (context
== pragma_stmt
)
36102 error_at (pragma_tok
->location
,
36103 "%<#pragma %s%> may only be used in compound statements",
36104 "omp cancellation point");
36106 cp_parser_error (parser
, "expected declaration specifiers");
36107 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36111 clauses
= cp_parser_omp_all_clauses (parser
,
36112 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
36113 "#pragma omp cancellation point",
36115 finish_omp_cancellation_point (clauses
);
36119 #pragma omp distribute distribute-clause[optseq] new-line
36122 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36123 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36130 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
36131 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36134 tree clauses
, sb
, ret
;
36136 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36138 strcat (p_name
, " distribute");
36139 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
36141 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36143 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36144 const char *p
= IDENTIFIER_POINTER (id
);
36146 bool parallel
= false;
36148 if (strcmp (p
, "simd") == 0)
36151 parallel
= strcmp (p
, "parallel") == 0;
36152 if (parallel
|| simd
)
36154 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36155 if (cclauses
== NULL
)
36156 cclauses
= cclauses_buf
;
36157 cp_lexer_consume_token (parser
->lexer
);
36158 if (!flag_openmp
) /* flag_openmp_simd */
36161 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36164 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36167 sb
= begin_omp_structured_block ();
36168 save
= cp_parser_begin_omp_structured_block (parser
);
36170 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36173 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36175 cp_parser_end_omp_structured_block (parser
, save
);
36176 tree body
= finish_omp_structured_block (sb
);
36179 ret
= make_node (OMP_DISTRIBUTE
);
36180 TREE_TYPE (ret
) = void_type_node
;
36181 OMP_FOR_BODY (ret
) = body
;
36182 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36183 SET_EXPR_LOCATION (ret
, loc
);
36188 if (!flag_openmp
) /* flag_openmp_simd */
36190 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36194 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36198 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36199 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36202 sb
= begin_omp_structured_block ();
36203 save
= cp_parser_begin_omp_structured_block (parser
);
36205 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36207 cp_parser_end_omp_structured_block (parser
, save
);
36208 add_stmt (finish_omp_structured_block (sb
));
36214 # pragma omp teams teams-clause[optseq] new-line
36215 structured-block */
36217 #define OMP_TEAMS_CLAUSE_MASK \
36218 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36227 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36228 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36231 tree clauses
, sb
, ret
;
36233 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36235 strcat (p_name
, " teams");
36236 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36240 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36241 const char *p
= IDENTIFIER_POINTER (id
);
36242 if (strcmp (p
, "distribute") == 0)
36244 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36245 if (cclauses
== NULL
)
36246 cclauses
= cclauses_buf
;
36248 cp_lexer_consume_token (parser
->lexer
);
36249 if (!flag_openmp
) /* flag_openmp_simd */
36250 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36252 sb
= begin_omp_structured_block ();
36253 save
= cp_parser_begin_omp_structured_block (parser
);
36254 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36256 cp_parser_end_omp_structured_block (parser
, save
);
36257 tree body
= finish_omp_structured_block (sb
);
36260 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36261 ret
= make_node (OMP_TEAMS
);
36262 TREE_TYPE (ret
) = void_type_node
;
36263 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36264 OMP_TEAMS_BODY (ret
) = body
;
36265 OMP_TEAMS_COMBINED (ret
) = 1;
36266 SET_EXPR_LOCATION (ret
, loc
);
36267 return add_stmt (ret
);
36270 if (!flag_openmp
) /* flag_openmp_simd */
36272 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36276 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36280 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36281 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36284 tree stmt
= make_node (OMP_TEAMS
);
36285 TREE_TYPE (stmt
) = void_type_node
;
36286 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36287 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36288 SET_EXPR_LOCATION (stmt
, loc
);
36290 return add_stmt (stmt
);
36294 # pragma omp target data target-data-clause[optseq] new-line
36295 structured-block */
36297 #define OMP_TARGET_DATA_CLAUSE_MASK \
36298 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36304 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36307 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36308 "#pragma omp target data", pragma_tok
);
36310 for (tree
*pc
= &clauses
; *pc
;)
36312 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36313 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36316 case GOMP_MAP_ALWAYS_TO
:
36317 case GOMP_MAP_FROM
:
36318 case GOMP_MAP_ALWAYS_FROM
:
36319 case GOMP_MAP_TOFROM
:
36320 case GOMP_MAP_ALWAYS_TOFROM
:
36321 case GOMP_MAP_ALLOC
:
36324 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36325 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36326 case GOMP_MAP_ALWAYS_POINTER
:
36330 error_at (OMP_CLAUSE_LOCATION (*pc
),
36331 "%<#pragma omp target data%> with map-type other "
36332 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36333 "on %<map%> clause");
36334 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36337 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36343 error_at (pragma_tok
->location
,
36344 "%<#pragma omp target data%> must contain at least "
36345 "one %<map%> clause");
36349 tree stmt
= make_node (OMP_TARGET_DATA
);
36350 TREE_TYPE (stmt
) = void_type_node
;
36351 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36353 keep_next_level (true);
36354 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36356 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36357 return add_stmt (stmt
);
36361 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36362 structured-block */
36364 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36365 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36372 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36373 enum pragma_context context
)
36375 bool data_seen
= false;
36376 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36378 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36379 const char *p
= IDENTIFIER_POINTER (id
);
36381 if (strcmp (p
, "data") == 0)
36383 cp_lexer_consume_token (parser
->lexer
);
36389 cp_parser_error (parser
, "expected %<data%>");
36390 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36394 if (context
== pragma_stmt
)
36396 error_at (pragma_tok
->location
,
36397 "%<#pragma %s%> may only be used in compound statements",
36398 "omp target enter data");
36399 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36404 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36405 "#pragma omp target enter data", pragma_tok
);
36407 for (tree
*pc
= &clauses
; *pc
;)
36409 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36410 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36413 case GOMP_MAP_ALWAYS_TO
:
36414 case GOMP_MAP_ALLOC
:
36417 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36418 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36419 case GOMP_MAP_ALWAYS_POINTER
:
36423 error_at (OMP_CLAUSE_LOCATION (*pc
),
36424 "%<#pragma omp target enter data%> with map-type other "
36425 "than %<to%> or %<alloc%> on %<map%> clause");
36426 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36429 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36435 error_at (pragma_tok
->location
,
36436 "%<#pragma omp target enter data%> must contain at least "
36437 "one %<map%> clause");
36441 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36442 TREE_TYPE (stmt
) = void_type_node
;
36443 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36444 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36445 return add_stmt (stmt
);
36449 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36450 structured-block */
36452 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36453 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36460 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36461 enum pragma_context context
)
36463 bool data_seen
= false;
36464 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36466 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36467 const char *p
= IDENTIFIER_POINTER (id
);
36469 if (strcmp (p
, "data") == 0)
36471 cp_lexer_consume_token (parser
->lexer
);
36477 cp_parser_error (parser
, "expected %<data%>");
36478 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36482 if (context
== pragma_stmt
)
36484 error_at (pragma_tok
->location
,
36485 "%<#pragma %s%> may only be used in compound statements",
36486 "omp target exit data");
36487 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36492 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36493 "#pragma omp target exit data", pragma_tok
);
36495 for (tree
*pc
= &clauses
; *pc
;)
36497 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36498 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36500 case GOMP_MAP_FROM
:
36501 case GOMP_MAP_ALWAYS_FROM
:
36502 case GOMP_MAP_RELEASE
:
36503 case GOMP_MAP_DELETE
:
36506 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36507 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36508 case GOMP_MAP_ALWAYS_POINTER
:
36512 error_at (OMP_CLAUSE_LOCATION (*pc
),
36513 "%<#pragma omp target exit data%> with map-type other "
36514 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36516 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36519 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36525 error_at (pragma_tok
->location
,
36526 "%<#pragma omp target exit data%> must contain at least "
36527 "one %<map%> clause");
36531 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36532 TREE_TYPE (stmt
) = void_type_node
;
36533 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36534 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36535 return add_stmt (stmt
);
36539 # pragma omp target update target-update-clause[optseq] new-line */
36541 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36542 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36550 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36551 enum pragma_context context
)
36553 if (context
== pragma_stmt
)
36555 error_at (pragma_tok
->location
,
36556 "%<#pragma %s%> may only be used in compound statements",
36557 "omp target update");
36558 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36563 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36564 "#pragma omp target update", pragma_tok
);
36565 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36566 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36568 error_at (pragma_tok
->location
,
36569 "%<#pragma omp target update%> must contain at least one "
36570 "%<from%> or %<to%> clauses");
36574 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36575 TREE_TYPE (stmt
) = void_type_node
;
36576 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36577 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36583 # pragma omp target target-clause[optseq] new-line
36584 structured-block */
36586 #define OMP_TARGET_CLAUSE_MASK \
36587 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36598 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36599 enum pragma_context context
, bool *if_p
)
36601 tree
*pc
= NULL
, stmt
;
36603 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36605 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36606 const char *p
= IDENTIFIER_POINTER (id
);
36607 enum tree_code ccode
= ERROR_MARK
;
36609 if (strcmp (p
, "teams") == 0)
36611 else if (strcmp (p
, "parallel") == 0)
36612 ccode
= OMP_PARALLEL
;
36613 else if (strcmp (p
, "simd") == 0)
36615 if (ccode
!= ERROR_MARK
)
36617 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36618 char p_name
[sizeof ("#pragma omp target teams distribute "
36619 "parallel for simd")];
36621 cp_lexer_consume_token (parser
->lexer
);
36622 strcpy (p_name
, "#pragma omp target");
36623 if (!flag_openmp
) /* flag_openmp_simd */
36629 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36630 OMP_TARGET_CLAUSE_MASK
,
36634 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36635 OMP_TARGET_CLAUSE_MASK
,
36639 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36640 OMP_TARGET_CLAUSE_MASK
,
36644 gcc_unreachable ();
36646 return stmt
!= NULL_TREE
;
36648 keep_next_level (true);
36649 tree sb
= begin_omp_structured_block (), ret
;
36650 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36654 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36655 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36659 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36660 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36664 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36665 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36669 gcc_unreachable ();
36671 cp_parser_end_omp_structured_block (parser
, save
);
36672 tree body
= finish_omp_structured_block (sb
);
36673 if (ret
== NULL_TREE
)
36675 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36677 /* For combined target teams, ensure the num_teams and
36678 thread_limit clause expressions are evaluated on the host,
36679 before entering the target construct. */
36681 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36682 c
; c
= OMP_CLAUSE_CHAIN (c
))
36683 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36684 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36685 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36687 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36688 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36689 if (expr
== error_mark_node
)
36691 tree tmp
= TARGET_EXPR_SLOT (expr
);
36693 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36694 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36695 OMP_CLAUSE_FIRSTPRIVATE
);
36696 OMP_CLAUSE_DECL (tc
) = tmp
;
36697 OMP_CLAUSE_CHAIN (tc
)
36698 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36699 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36702 tree stmt
= make_node (OMP_TARGET
);
36703 TREE_TYPE (stmt
) = void_type_node
;
36704 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36705 OMP_TARGET_BODY (stmt
) = body
;
36706 OMP_TARGET_COMBINED (stmt
) = 1;
36707 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36709 pc
= &OMP_TARGET_CLAUSES (stmt
);
36710 goto check_clauses
;
36712 else if (!flag_openmp
) /* flag_openmp_simd */
36714 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36717 else if (strcmp (p
, "data") == 0)
36719 cp_lexer_consume_token (parser
->lexer
);
36720 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36723 else if (strcmp (p
, "enter") == 0)
36725 cp_lexer_consume_token (parser
->lexer
);
36726 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36729 else if (strcmp (p
, "exit") == 0)
36731 cp_lexer_consume_token (parser
->lexer
);
36732 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36735 else if (strcmp (p
, "update") == 0)
36737 cp_lexer_consume_token (parser
->lexer
);
36738 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36741 if (!flag_openmp
) /* flag_openmp_simd */
36743 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36747 stmt
= make_node (OMP_TARGET
);
36748 TREE_TYPE (stmt
) = void_type_node
;
36750 OMP_TARGET_CLAUSES (stmt
)
36751 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36752 "#pragma omp target", pragma_tok
);
36753 pc
= &OMP_TARGET_CLAUSES (stmt
);
36754 keep_next_level (true);
36755 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36757 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36763 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36764 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36767 case GOMP_MAP_ALWAYS_TO
:
36768 case GOMP_MAP_FROM
:
36769 case GOMP_MAP_ALWAYS_FROM
:
36770 case GOMP_MAP_TOFROM
:
36771 case GOMP_MAP_ALWAYS_TOFROM
:
36772 case GOMP_MAP_ALLOC
:
36773 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36774 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36775 case GOMP_MAP_ALWAYS_POINTER
:
36778 error_at (OMP_CLAUSE_LOCATION (*pc
),
36779 "%<#pragma omp target%> with map-type other "
36780 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36781 "on %<map%> clause");
36782 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36785 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36791 # pragma acc cache (variable-list) new-line
36795 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36797 tree stmt
, clauses
;
36799 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36800 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36802 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36804 stmt
= make_node (OACC_CACHE
);
36805 TREE_TYPE (stmt
) = void_type_node
;
36806 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36807 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36814 # pragma acc data oacc-data-clause[optseq] new-line
36815 structured-block */
36817 #define OACC_DATA_CLAUSE_MASK \
36818 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36827 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36829 tree stmt
, clauses
, block
;
36832 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36833 "#pragma acc data", pragma_tok
);
36835 block
= begin_omp_parallel ();
36836 save
= cp_parser_begin_omp_structured_block (parser
);
36837 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36838 cp_parser_end_omp_structured_block (parser
, save
);
36839 stmt
= finish_oacc_data (clauses
, block
);
36844 # pragma acc host_data <clauses> new-line
36845 structured-block */
36847 #define OACC_HOST_DATA_CLAUSE_MASK \
36848 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36851 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36853 tree stmt
, clauses
, block
;
36856 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36857 "#pragma acc host_data", pragma_tok
);
36859 block
= begin_omp_parallel ();
36860 save
= cp_parser_begin_omp_structured_block (parser
);
36861 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36862 cp_parser_end_omp_structured_block (parser
, save
);
36863 stmt
= finish_oacc_host_data (clauses
, block
);
36868 # pragma acc declare oacc-data-clause[optseq] new-line
36871 #define OACC_DECLARE_CLAUSE_MASK \
36872 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36882 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36884 tree clauses
, stmt
;
36885 bool error
= false;
36887 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36888 "#pragma acc declare", pragma_tok
, true);
36891 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36893 error_at (pragma_tok
->location
,
36894 "no valid clauses specified in %<#pragma acc declare%>");
36898 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36900 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36901 tree decl
= OMP_CLAUSE_DECL (t
);
36902 if (!DECL_P (decl
))
36904 error_at (loc
, "array section in %<#pragma acc declare%>");
36908 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36909 switch (OMP_CLAUSE_MAP_KIND (t
))
36911 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36912 case GOMP_MAP_ALLOC
:
36914 case GOMP_MAP_FORCE_DEVICEPTR
:
36915 case GOMP_MAP_DEVICE_RESIDENT
:
36918 case GOMP_MAP_LINK
:
36919 if (!global_bindings_p ()
36920 && (TREE_STATIC (decl
)
36921 || !DECL_EXTERNAL (decl
)))
36924 "%qD must be a global variable in "
36925 "%<#pragma acc declare link%>",
36933 if (global_bindings_p ())
36935 error_at (loc
, "invalid OpenACC clause at file scope");
36939 if (DECL_EXTERNAL (decl
))
36942 "invalid use of %<extern%> variable %qD "
36943 "in %<#pragma acc declare%>", decl
);
36947 else if (TREE_PUBLIC (decl
))
36950 "invalid use of %<global%> variable %qD "
36951 "in %<#pragma acc declare%>", decl
);
36958 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36959 || lookup_attribute ("omp declare target link",
36960 DECL_ATTRIBUTES (decl
)))
36962 error_at (loc
, "variable %qD used more than once with "
36963 "%<#pragma acc declare%>", decl
);
36972 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36973 id
= get_identifier ("omp declare target link");
36975 id
= get_identifier ("omp declare target");
36977 DECL_ATTRIBUTES (decl
)
36978 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36979 if (global_bindings_p ())
36981 symtab_node
*node
= symtab_node::get (decl
);
36984 node
->offloadable
= 1;
36985 if (ENABLE_OFFLOADING
)
36987 g
->have_offload
= true;
36988 if (is_a
<varpool_node
*> (node
))
36989 vec_safe_push (offload_vars
, decl
);
36996 if (error
|| global_bindings_p ())
36999 stmt
= make_node (OACC_DECLARE
);
37000 TREE_TYPE (stmt
) = void_type_node
;
37001 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
37002 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37010 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37014 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37016 LOC is the location of the #pragma token.
37019 #define OACC_ENTER_DATA_CLAUSE_MASK \
37020 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37026 #define OACC_EXIT_DATA_CLAUSE_MASK \
37027 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
37032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37035 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37038 location_t loc
= pragma_tok
->location
;
37039 tree stmt
, clauses
;
37040 const char *p
= "";
37042 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37043 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37045 if (strcmp (p
, "data") != 0)
37047 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
37048 enter
? "enter" : "exit");
37049 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37053 cp_lexer_consume_token (parser
->lexer
);
37056 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
37057 "#pragma acc enter data", pragma_tok
);
37059 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
37060 "#pragma acc exit data", pragma_tok
);
37062 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37064 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
37065 enter
? "enter" : "exit");
37069 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
37070 TREE_TYPE (stmt
) = void_type_node
;
37071 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
37072 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37078 # pragma acc loop oacc-loop-clause[optseq] new-line
37079 structured-block */
37081 #define OACC_LOOP_CLAUSE_MASK \
37082 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37094 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
37095 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
37097 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
37099 strcat (p_name
, " loop");
37100 mask
|= OACC_LOOP_CLAUSE_MASK
;
37102 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37106 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
37108 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
37110 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
37113 tree block
= begin_omp_structured_block ();
37114 int save
= cp_parser_begin_omp_structured_block (parser
);
37115 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
37116 cp_parser_end_omp_structured_block (parser
, save
);
37117 add_stmt (finish_omp_structured_block (block
));
37123 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37128 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37132 #define OACC_KERNELS_CLAUSE_MASK \
37133 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37147 #define OACC_PARALLEL_CLAUSE_MASK \
37148 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37166 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37167 char *p_name
, bool *if_p
)
37169 omp_clause_mask mask
;
37170 enum tree_code code
;
37171 switch (cp_parser_pragma_kind (pragma_tok
))
37173 case PRAGMA_OACC_KERNELS
:
37174 strcat (p_name
, " kernels");
37175 mask
= OACC_KERNELS_CLAUSE_MASK
;
37176 code
= OACC_KERNELS
;
37178 case PRAGMA_OACC_PARALLEL
:
37179 strcat (p_name
, " parallel");
37180 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37181 code
= OACC_PARALLEL
;
37184 gcc_unreachable ();
37187 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37190 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37191 if (strcmp (p
, "loop") == 0)
37193 cp_lexer_consume_token (parser
->lexer
);
37194 tree block
= begin_omp_parallel ();
37196 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37198 return finish_omp_construct (code
, block
, clauses
);
37202 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37204 tree block
= begin_omp_parallel ();
37205 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37206 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37207 cp_parser_end_omp_structured_block (parser
, save
);
37208 return finish_omp_construct (code
, block
, clauses
);
37212 # pragma acc update oacc-update-clause[optseq] new-line
37215 #define OACC_UPDATE_CLAUSE_MASK \
37216 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
37221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37224 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37226 tree stmt
, clauses
;
37228 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37229 "#pragma acc update", pragma_tok
);
37231 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37233 error_at (pragma_tok
->location
,
37234 "%<#pragma acc update%> must contain at least one "
37235 "%<device%> or %<host%> or %<self%> clause");
37239 stmt
= make_node (OACC_UPDATE
);
37240 TREE_TYPE (stmt
) = void_type_node
;
37241 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37242 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37248 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37250 LOC is the location of the #pragma token.
37253 #define OACC_WAIT_CLAUSE_MASK \
37254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37257 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37259 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37260 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37262 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37263 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37265 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37266 "#pragma acc wait", pragma_tok
);
37268 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37269 stmt
= finish_expr_stmt (stmt
);
37275 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37277 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37278 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37286 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37287 enum pragma_context context
)
37289 bool first_p
= parser
->omp_declare_simd
== NULL
;
37290 cp_omp_declare_simd_data data
;
37293 data
.error_seen
= false;
37294 data
.fndecl_seen
= false;
37295 data
.tokens
= vNULL
;
37296 data
.clauses
= NULL_TREE
;
37297 /* It is safe to take the address of a local variable; it will only be
37298 used while this scope is live. */
37299 parser
->omp_declare_simd
= &data
;
37302 /* Store away all pragma tokens. */
37303 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37304 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37305 cp_lexer_consume_token (parser
->lexer
);
37306 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37307 parser
->omp_declare_simd
->error_seen
= true;
37308 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37309 struct cp_token_cache
*cp
37310 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37311 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37315 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37316 cp_parser_pragma (parser
, context
, NULL
);
37319 case pragma_external
:
37320 cp_parser_declaration (parser
);
37322 case pragma_member
:
37323 cp_parser_member_declaration (parser
);
37325 case pragma_objc_icode
:
37326 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37329 cp_parser_declaration_statement (parser
);
37332 if (parser
->omp_declare_simd
37333 && !parser
->omp_declare_simd
->error_seen
37334 && !parser
->omp_declare_simd
->fndecl_seen
)
37335 error_at (pragma_tok
->location
,
37336 "%<#pragma omp declare simd%> not immediately followed by "
37337 "function declaration or definition");
37338 data
.tokens
.release ();
37339 parser
->omp_declare_simd
= NULL
;
37343 /* Finalize #pragma omp declare simd clauses after direct declarator has
37344 been parsed, and put that into "omp declare simd" attribute. */
37347 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37349 struct cp_token_cache
*ce
;
37350 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37353 if (!data
->error_seen
&& data
->fndecl_seen
)
37355 error ("%<#pragma omp declare simd%> not immediately followed by "
37356 "a single function declaration or definition");
37357 data
->error_seen
= true;
37359 if (data
->error_seen
)
37362 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37366 cp_parser_push_lexer_for_tokens (parser
, ce
);
37367 parser
->lexer
->in_pragma
= true;
37368 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37369 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37370 cp_lexer_consume_token (parser
->lexer
);
37371 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37372 "#pragma omp declare simd", pragma_tok
);
37373 cp_parser_pop_lexer (parser
);
37375 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37376 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37377 TREE_CHAIN (c
) = attrs
;
37378 if (processing_template_decl
)
37379 ATTR_IS_DEPENDENT (c
) = 1;
37383 data
->fndecl_seen
= true;
37389 # pragma omp declare target new-line
37390 declarations and definitions
37391 # pragma omp end declare target new-line
37394 # pragma omp declare target ( extended-list ) new-line
37396 # pragma omp declare target declare-target-clauses[seq] new-line */
37398 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37399 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37403 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37405 tree clauses
= NULL_TREE
;
37406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37408 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37409 "#pragma omp declare target", pragma_tok
);
37410 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37412 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37414 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37415 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37419 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37420 scope_chain
->omp_declare_target_attribute
++;
37423 if (scope_chain
->omp_declare_target_attribute
)
37424 error_at (pragma_tok
->location
,
37425 "%<#pragma omp declare target%> with clauses in between "
37426 "%<#pragma omp declare target%> without clauses and "
37427 "%<#pragma omp end declare target%>");
37428 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37430 tree t
= OMP_CLAUSE_DECL (c
), id
;
37431 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37432 tree at2
= lookup_attribute ("omp declare target link",
37433 DECL_ATTRIBUTES (t
));
37434 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37436 id
= get_identifier ("omp declare target link");
37437 std::swap (at1
, at2
);
37440 id
= get_identifier ("omp declare target");
37443 error_at (OMP_CLAUSE_LOCATION (c
),
37444 "%qD specified both in declare target %<link%> and %<to%>"
37450 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37451 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37454 symtab_node
*node
= symtab_node::get (t
);
37457 node
->offloadable
= 1;
37458 if (ENABLE_OFFLOADING
)
37460 g
->have_offload
= true;
37461 if (is_a
<varpool_node
*> (node
))
37462 vec_safe_push (offload_vars
, t
);
37470 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37472 const char *p
= "";
37473 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37475 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37476 p
= IDENTIFIER_POINTER (id
);
37478 if (strcmp (p
, "declare") == 0)
37480 cp_lexer_consume_token (parser
->lexer
);
37482 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37484 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37485 p
= IDENTIFIER_POINTER (id
);
37487 if (strcmp (p
, "target") == 0)
37488 cp_lexer_consume_token (parser
->lexer
);
37491 cp_parser_error (parser
, "expected %<target%>");
37492 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37498 cp_parser_error (parser
, "expected %<declare%>");
37499 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37502 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37503 if (!scope_chain
->omp_declare_target_attribute
)
37504 error_at (pragma_tok
->location
,
37505 "%<#pragma omp end declare target%> without corresponding "
37506 "%<#pragma omp declare target%>");
37508 scope_chain
->omp_declare_target_attribute
--;
37511 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37512 expression and optional initializer clause of
37513 #pragma omp declare reduction. We store the expression(s) as
37514 either 3, 6 or 7 special statements inside of the artificial function's
37515 body. The first two statements are DECL_EXPRs for the artificial
37516 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37517 expression that uses those variables.
37518 If there was any INITIALIZER clause, this is followed by further statements,
37519 the fourth and fifth statements are DECL_EXPRs for the artificial
37520 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37521 constructor variant (first token after open paren is not omp_priv),
37522 then the sixth statement is a statement with the function call expression
37523 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37524 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37525 to initialize the OMP_PRIV artificial variable and there is seventh
37526 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37529 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37531 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37532 gcc_assert (TYPE_REF_P (type
));
37533 type
= TREE_TYPE (type
);
37534 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37535 DECL_ARTIFICIAL (omp_out
) = 1;
37536 pushdecl (omp_out
);
37537 add_decl_expr (omp_out
);
37538 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37539 DECL_ARTIFICIAL (omp_in
) = 1;
37541 add_decl_expr (omp_in
);
37543 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37545 keep_next_level (true);
37546 tree block
= begin_omp_structured_block ();
37547 combiner
= cp_parser_expression (parser
);
37548 finish_expr_stmt (combiner
);
37549 block
= finish_omp_structured_block (block
);
37552 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37555 const char *p
= "";
37556 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37558 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37559 p
= IDENTIFIER_POINTER (id
);
37562 if (strcmp (p
, "initializer") == 0)
37564 cp_lexer_consume_token (parser
->lexer
);
37565 matching_parens parens
;
37566 if (!parens
.require_open (parser
))
37570 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37572 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37573 p
= IDENTIFIER_POINTER (id
);
37576 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37577 DECL_ARTIFICIAL (omp_priv
) = 1;
37578 pushdecl (omp_priv
);
37579 add_decl_expr (omp_priv
);
37580 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37581 DECL_ARTIFICIAL (omp_orig
) = 1;
37582 pushdecl (omp_orig
);
37583 add_decl_expr (omp_orig
);
37585 keep_next_level (true);
37586 block
= begin_omp_structured_block ();
37589 if (strcmp (p
, "omp_priv") == 0)
37591 bool is_direct_init
, is_non_constant_init
;
37593 cp_lexer_consume_token (parser
->lexer
);
37594 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37595 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37596 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37597 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37599 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37600 == CPP_CLOSE_PAREN
))
37602 finish_omp_structured_block (block
);
37603 error ("invalid initializer clause");
37606 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37607 &is_non_constant_init
);
37608 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37609 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37613 cp_parser_parse_tentatively (parser
);
37614 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37615 /*check_dependency_p=*/true,
37616 /*template_p=*/NULL
,
37617 /*declarator_p=*/false,
37618 /*optional_p=*/false);
37619 vec
<tree
, va_gc
> *args
;
37620 if (fn_name
== error_mark_node
37621 || cp_parser_error_occurred (parser
)
37622 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37623 || ((args
= cp_parser_parenthesized_expression_list
37624 (parser
, non_attr
, /*cast_p=*/false,
37625 /*allow_expansion_p=*/true,
37626 /*non_constant_p=*/NULL
)),
37627 cp_parser_error_occurred (parser
)))
37629 finish_omp_structured_block (block
);
37630 cp_parser_abort_tentative_parse (parser
);
37631 cp_parser_error (parser
, "expected id-expression (arguments)");
37636 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37637 if (arg
== omp_priv
37638 || (TREE_CODE (arg
) == ADDR_EXPR
37639 && TREE_OPERAND (arg
, 0) == omp_priv
))
37641 cp_parser_abort_tentative_parse (parser
);
37642 if (arg
== NULL_TREE
)
37643 error ("one of the initializer call arguments should be %<omp_priv%>"
37644 " or %<&omp_priv%>");
37645 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37647 finish_expr_stmt (initializer
);
37650 block
= finish_omp_structured_block (block
);
37651 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37655 add_decl_expr (omp_orig
);
37657 if (!parens
.require_close (parser
))
37661 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37662 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37669 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37670 initializer-clause[opt] new-line
37672 initializer-clause:
37673 initializer (omp_priv initializer)
37674 initializer (function-name (argument-list)) */
37677 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37678 enum pragma_context
)
37680 auto_vec
<tree
> types
;
37681 enum tree_code reduc_code
= ERROR_MARK
;
37682 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37684 cp_token
*first_token
;
37685 cp_token_cache
*cp
;
37689 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37690 p
= obstack_alloc (&declarator_obstack
, 0);
37692 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37695 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37698 reduc_code
= PLUS_EXPR
;
37701 reduc_code
= MULT_EXPR
;
37704 reduc_code
= MINUS_EXPR
;
37707 reduc_code
= BIT_AND_EXPR
;
37710 reduc_code
= BIT_XOR_EXPR
;
37713 reduc_code
= BIT_IOR_EXPR
;
37716 reduc_code
= TRUTH_ANDIF_EXPR
;
37719 reduc_code
= TRUTH_ORIF_EXPR
;
37722 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37725 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37726 "%<|%>, %<&&%>, %<||%> or identifier");
37730 if (reduc_code
!= ERROR_MARK
)
37731 cp_lexer_consume_token (parser
->lexer
);
37733 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37734 if (reduc_id
== error_mark_node
)
37737 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37740 /* Types may not be defined in declare reduction type list. */
37741 const char *saved_message
;
37742 saved_message
= parser
->type_definition_forbidden_message
;
37743 parser
->type_definition_forbidden_message
37744 = G_("types may not be defined in declare reduction type list");
37745 bool saved_colon_corrects_to_scope_p
;
37746 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37747 parser
->colon_corrects_to_scope_p
= false;
37748 bool saved_colon_doesnt_start_class_def_p
;
37749 saved_colon_doesnt_start_class_def_p
37750 = parser
->colon_doesnt_start_class_def_p
;
37751 parser
->colon_doesnt_start_class_def_p
= true;
37755 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37756 type
= cp_parser_type_id (parser
);
37757 if (type
== error_mark_node
)
37759 else if (ARITHMETIC_TYPE_P (type
)
37760 && (orig_reduc_id
== NULL_TREE
37761 || (TREE_CODE (type
) != COMPLEX_TYPE
37762 && (id_equal (orig_reduc_id
, "min")
37763 || id_equal (orig_reduc_id
, "max")))))
37764 error_at (loc
, "predeclared arithmetic type %qT in "
37765 "%<#pragma omp declare reduction%>", type
);
37766 else if (TREE_CODE (type
) == FUNCTION_TYPE
37767 || TREE_CODE (type
) == METHOD_TYPE
37768 || TREE_CODE (type
) == ARRAY_TYPE
)
37769 error_at (loc
, "function or array type %qT in "
37770 "%<#pragma omp declare reduction%>", type
);
37771 else if (TYPE_REF_P (type
))
37772 error_at (loc
, "reference type %qT in "
37773 "%<#pragma omp declare reduction%>", type
);
37774 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37775 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37776 "%<#pragma omp declare reduction%>", type
);
37778 types
.safe_push (type
);
37780 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37781 cp_lexer_consume_token (parser
->lexer
);
37786 /* Restore the saved message. */
37787 parser
->type_definition_forbidden_message
= saved_message
;
37788 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37789 parser
->colon_doesnt_start_class_def_p
37790 = saved_colon_doesnt_start_class_def_p
;
37792 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37793 || types
.is_empty ())
37796 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37800 first_token
= cp_lexer_peek_token (parser
->lexer
);
37803 FOR_EACH_VEC_ELT (types
, i
, type
)
37806 = build_function_type_list (void_type_node
,
37807 cp_build_reference_type (type
, false),
37809 tree this_reduc_id
= reduc_id
;
37810 if (!dependent_type_p (type
))
37811 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37812 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37813 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37814 DECL_ARTIFICIAL (fndecl
) = 1;
37815 DECL_EXTERNAL (fndecl
) = 1;
37816 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37817 DECL_IGNORED_P (fndecl
) = 1;
37818 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37819 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37820 DECL_ATTRIBUTES (fndecl
)
37821 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37822 DECL_ATTRIBUTES (fndecl
));
37823 if (processing_template_decl
)
37824 fndecl
= push_template_decl (fndecl
);
37825 bool block_scope
= false;
37826 tree block
= NULL_TREE
;
37827 if (current_function_decl
)
37829 block_scope
= true;
37830 DECL_CONTEXT (fndecl
) = global_namespace
;
37831 if (!processing_template_decl
)
37834 else if (current_class_type
)
37838 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37839 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37840 cp_lexer_consume_token (parser
->lexer
);
37841 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37843 cp
= cp_token_cache_new (first_token
,
37844 cp_lexer_peek_nth_token (parser
->lexer
,
37847 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37848 finish_member_declaration (fndecl
);
37849 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37850 DECL_PENDING_INLINE_P (fndecl
) = 1;
37851 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37856 DECL_CONTEXT (fndecl
) = current_namespace
;
37860 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37862 block
= begin_omp_structured_block ();
37865 cp_parser_push_lexer_for_tokens (parser
, cp
);
37866 parser
->lexer
->in_pragma
= true;
37868 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37871 finish_function (/*inline_p=*/false);
37873 DECL_CONTEXT (fndecl
) = current_function_decl
;
37875 cp_parser_pop_lexer (parser
);
37879 cp_parser_pop_lexer (parser
);
37881 finish_function (/*inline_p=*/false);
37884 DECL_CONTEXT (fndecl
) = current_function_decl
;
37885 block
= finish_omp_structured_block (block
);
37886 if (TREE_CODE (block
) == BIND_EXPR
)
37887 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37888 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37889 DECL_SAVED_TREE (fndecl
) = block
;
37890 if (processing_template_decl
)
37891 add_decl_expr (fndecl
);
37893 cp_check_omp_declare_reduction (fndecl
);
37894 if (cp
== NULL
&& types
.length () > 1)
37895 cp
= cp_token_cache_new (first_token
,
37896 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37897 if (errs
!= errorcount
)
37901 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37904 /* Free any declarators allocated. */
37905 obstack_free (&declarator_obstack
, p
);
37909 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37910 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37911 initializer-clause[opt] new-line
37912 #pragma omp declare target new-line */
37915 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37916 enum pragma_context context
)
37918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37920 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37921 const char *p
= IDENTIFIER_POINTER (id
);
37923 if (strcmp (p
, "simd") == 0)
37925 cp_lexer_consume_token (parser
->lexer
);
37926 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37930 cp_ensure_no_omp_declare_simd (parser
);
37931 if (strcmp (p
, "reduction") == 0)
37933 cp_lexer_consume_token (parser
->lexer
);
37934 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37938 if (!flag_openmp
) /* flag_openmp_simd */
37940 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37943 if (strcmp (p
, "target") == 0)
37945 cp_lexer_consume_token (parser
->lexer
);
37946 cp_parser_omp_declare_target (parser
, pragma_tok
);
37950 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37952 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37957 #pragma omp taskloop taskloop-clause[optseq] new-line
37960 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37963 #define OMP_TASKLOOP_CLAUSE_MASK \
37964 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37980 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37981 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37984 tree clauses
, sb
, ret
;
37986 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37988 strcat (p_name
, " taskloop");
37989 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37991 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37993 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37994 const char *p
= IDENTIFIER_POINTER (id
);
37996 if (strcmp (p
, "simd") == 0)
37998 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37999 if (cclauses
== NULL
)
38000 cclauses
= cclauses_buf
;
38002 cp_lexer_consume_token (parser
->lexer
);
38003 if (!flag_openmp
) /* flag_openmp_simd */
38004 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38006 sb
= begin_omp_structured_block ();
38007 save
= cp_parser_begin_omp_structured_block (parser
);
38008 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38010 cp_parser_end_omp_structured_block (parser
, save
);
38011 tree body
= finish_omp_structured_block (sb
);
38014 ret
= make_node (OMP_TASKLOOP
);
38015 TREE_TYPE (ret
) = void_type_node
;
38016 OMP_FOR_BODY (ret
) = body
;
38017 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38018 SET_EXPR_LOCATION (ret
, loc
);
38023 if (!flag_openmp
) /* flag_openmp_simd */
38025 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38029 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38033 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
38034 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38037 sb
= begin_omp_structured_block ();
38038 save
= cp_parser_begin_omp_structured_block (parser
);
38040 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
38043 cp_parser_end_omp_structured_block (parser
, save
);
38044 add_stmt (finish_omp_structured_block (sb
));
38051 # pragma acc routine oacc-routine-clause[optseq] new-line
38052 function-definition
38054 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38057 #define OACC_ROUTINE_CLAUSE_MASK \
38058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38064 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38065 component, which must resolve to a declared namespace-scope
38066 function. The clauses are either processed directly (for a named
38067 function), or defered until the immediatley following declaration
38071 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
38072 enum pragma_context context
)
38074 gcc_checking_assert (context
== pragma_external
);
38075 /* The checking for "another pragma following this one" in the "no optional
38076 '( name )'" case makes sure that we dont re-enter. */
38077 gcc_checking_assert (parser
->oacc_routine
== NULL
);
38079 cp_oacc_routine_data data
;
38080 data
.error_seen
= false;
38081 data
.fndecl_seen
= false;
38082 data
.tokens
= vNULL
;
38083 data
.clauses
= NULL_TREE
;
38084 data
.loc
= pragma_tok
->location
;
38085 /* It is safe to take the address of a local variable; it will only be
38086 used while this scope is live. */
38087 parser
->oacc_routine
= &data
;
38089 /* Look for optional '( name )'. */
38090 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38092 matching_parens parens
;
38093 parens
.consume_open (parser
); /* '(' */
38095 /* We parse the name as an id-expression. If it resolves to
38096 anything other than a non-overloaded function at namespace
38097 scope, it's an error. */
38098 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38099 tree name
= cp_parser_id_expression (parser
,
38100 /*template_keyword_p=*/false,
38101 /*check_dependency_p=*/false,
38102 /*template_p=*/NULL
,
38103 /*declarator_p=*/false,
38104 /*optional_p=*/false);
38105 tree decl
= (identifier_p (name
)
38106 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
38108 if (name
!= error_mark_node
&& decl
== error_mark_node
)
38109 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
38111 if (decl
== error_mark_node
38112 || !parens
.require_close (parser
))
38114 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38115 parser
->oacc_routine
= NULL
;
38120 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38121 "#pragma acc routine",
38122 cp_lexer_peek_token (parser
->lexer
));
38124 if (decl
&& is_overloaded_fn (decl
)
38125 && (TREE_CODE (decl
) != FUNCTION_DECL
38126 || DECL_FUNCTION_TEMPLATE_P (decl
)))
38128 error_at (name_loc
,
38129 "%<#pragma acc routine%> names a set of overloads");
38130 parser
->oacc_routine
= NULL
;
38134 /* Perhaps we should use the same rule as declarations in different
38136 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38138 error_at (name_loc
,
38139 "%qD does not refer to a namespace scope function", decl
);
38140 parser
->oacc_routine
= NULL
;
38144 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38146 error_at (name_loc
, "%qD does not refer to a function", decl
);
38147 parser
->oacc_routine
= NULL
;
38151 cp_finalize_oacc_routine (parser
, decl
, false);
38152 parser
->oacc_routine
= NULL
;
38154 else /* No optional '( name )'. */
38156 /* Store away all pragma tokens. */
38157 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38158 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38159 cp_lexer_consume_token (parser
->lexer
);
38160 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38161 parser
->oacc_routine
->error_seen
= true;
38162 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38163 struct cp_token_cache
*cp
38164 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38165 parser
->oacc_routine
->tokens
.safe_push (cp
);
38167 /* Emit a helpful diagnostic if there's another pragma following this
38169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38171 cp_ensure_no_oacc_routine (parser
);
38172 data
.tokens
.release ();
38173 /* ..., and then just keep going. */
38177 /* We only have to consider the pragma_external case here. */
38178 cp_parser_declaration (parser
);
38179 if (parser
->oacc_routine
38180 && !parser
->oacc_routine
->fndecl_seen
)
38181 cp_ensure_no_oacc_routine (parser
);
38183 parser
->oacc_routine
= NULL
;
38184 data
.tokens
.release ();
38188 /* Finalize #pragma acc routine clauses after direct declarator has
38192 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38194 struct cp_token_cache
*ce
;
38195 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38197 if (!data
->error_seen
&& data
->fndecl_seen
)
38199 error_at (data
->loc
,
38200 "%<#pragma acc routine%> not immediately followed by "
38201 "a single function declaration or definition");
38202 data
->error_seen
= true;
38204 if (data
->error_seen
)
38207 gcc_checking_assert (data
->tokens
.length () == 1);
38208 ce
= data
->tokens
[0];
38210 cp_parser_push_lexer_for_tokens (parser
, ce
);
38211 parser
->lexer
->in_pragma
= true;
38212 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38214 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38215 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38216 parser
->oacc_routine
->clauses
38217 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38218 "#pragma acc routine", pragma_tok
);
38219 cp_parser_pop_lexer (parser
);
38220 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38226 /* Apply any saved OpenACC routine clauses to a just-parsed
38230 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38232 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38234 /* Keep going if we're in error reporting mode. */
38235 if (parser
->oacc_routine
->error_seen
38236 || fndecl
== error_mark_node
)
38239 if (parser
->oacc_routine
->fndecl_seen
)
38241 error_at (parser
->oacc_routine
->loc
,
38242 "%<#pragma acc routine%> not immediately followed by"
38243 " a single function declaration or definition");
38244 parser
->oacc_routine
= NULL
;
38247 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38249 cp_ensure_no_oacc_routine (parser
);
38253 if (oacc_get_fn_attrib (fndecl
))
38255 error_at (parser
->oacc_routine
->loc
,
38256 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38257 parser
->oacc_routine
= NULL
;
38261 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38263 error_at (parser
->oacc_routine
->loc
,
38265 ? G_("%<#pragma acc routine%> must be applied before use")
38266 : G_("%<#pragma acc routine%> must be applied before "
38268 parser
->oacc_routine
= NULL
;
38272 /* Process the routine's dimension clauses. */
38273 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38274 oacc_replace_fn_attrib (fndecl
, dims
);
38276 /* Add an "omp declare target" attribute. */
38277 DECL_ATTRIBUTES (fndecl
)
38278 = tree_cons (get_identifier ("omp declare target"),
38279 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38281 /* Don't unset parser->oacc_routine here: we may still need it to
38282 diagnose wrong usage. But, remember that we've used this "#pragma acc
38284 parser
->oacc_routine
->fndecl_seen
= true;
38288 /* Main entry point to OpenMP statement pragmas. */
38291 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38294 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38295 omp_clause_mask
mask (0);
38297 switch (cp_parser_pragma_kind (pragma_tok
))
38299 case PRAGMA_OACC_ATOMIC
:
38300 cp_parser_omp_atomic (parser
, pragma_tok
);
38302 case PRAGMA_OACC_CACHE
:
38303 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38305 case PRAGMA_OACC_DATA
:
38306 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38308 case PRAGMA_OACC_ENTER_DATA
:
38309 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38311 case PRAGMA_OACC_EXIT_DATA
:
38312 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38314 case PRAGMA_OACC_HOST_DATA
:
38315 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38317 case PRAGMA_OACC_KERNELS
:
38318 case PRAGMA_OACC_PARALLEL
:
38319 strcpy (p_name
, "#pragma acc");
38320 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38323 case PRAGMA_OACC_LOOP
:
38324 strcpy (p_name
, "#pragma acc");
38325 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38328 case PRAGMA_OACC_UPDATE
:
38329 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38331 case PRAGMA_OACC_WAIT
:
38332 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38334 case PRAGMA_OMP_ATOMIC
:
38335 cp_parser_omp_atomic (parser
, pragma_tok
);
38337 case PRAGMA_OMP_CRITICAL
:
38338 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38340 case PRAGMA_OMP_DISTRIBUTE
:
38341 strcpy (p_name
, "#pragma omp");
38342 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38345 case PRAGMA_OMP_FOR
:
38346 strcpy (p_name
, "#pragma omp");
38347 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38350 case PRAGMA_OMP_MASTER
:
38351 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38353 case PRAGMA_OMP_PARALLEL
:
38354 strcpy (p_name
, "#pragma omp");
38355 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38358 case PRAGMA_OMP_SECTIONS
:
38359 strcpy (p_name
, "#pragma omp");
38360 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38362 case PRAGMA_OMP_SIMD
:
38363 strcpy (p_name
, "#pragma omp");
38364 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38367 case PRAGMA_OMP_SINGLE
:
38368 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38370 case PRAGMA_OMP_TASK
:
38371 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38373 case PRAGMA_OMP_TASKGROUP
:
38374 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38376 case PRAGMA_OMP_TASKLOOP
:
38377 strcpy (p_name
, "#pragma omp");
38378 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38381 case PRAGMA_OMP_TEAMS
:
38382 strcpy (p_name
, "#pragma omp");
38383 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38387 gcc_unreachable ();
38390 protected_set_expr_location (stmt
, pragma_tok
->location
);
38393 /* Transactional Memory parsing routines. */
38395 /* Parse a transaction attribute.
38401 We use this instead of cp_parser_attributes_opt for transactions to avoid
38402 the pedwarn in C++98 mode. */
38405 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38408 tree attr_name
, attr
= NULL
;
38410 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38411 return cp_parser_attributes_opt (parser
);
38413 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38415 cp_lexer_consume_token (parser
->lexer
);
38416 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38419 token
= cp_lexer_peek_token (parser
->lexer
);
38420 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38422 token
= cp_lexer_consume_token (parser
->lexer
);
38424 attr_name
= (token
->type
== CPP_KEYWORD
38425 /* For keywords, use the canonical spelling,
38426 not the parsed identifier. */
38427 ? ridpointers
[(int) token
->keyword
]
38429 attr
= build_tree_list (attr_name
, NULL_TREE
);
38432 cp_parser_error (parser
, "expected identifier");
38434 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38436 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38440 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38442 transaction-statement:
38443 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38445 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38449 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38451 unsigned char old_in
= parser
->in_transaction
;
38452 unsigned char this_in
= 1, new_in
;
38453 enum rid keyword
= token
->keyword
;
38454 tree stmt
, attrs
, noex
;
38456 cp_lexer_consume_token (parser
->lexer
);
38458 if (keyword
== RID_TRANSACTION_RELAXED
38459 || keyword
== RID_SYNCHRONIZED
)
38460 this_in
|= TM_STMT_ATTR_RELAXED
;
38463 attrs
= cp_parser_txn_attribute_opt (parser
);
38465 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38468 /* Parse a noexcept specification. */
38469 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38470 noex
= boolean_true_node
;
38471 else if (keyword
== RID_ATOMIC_CANCEL
)
38473 /* cancel-and-throw is unimplemented. */
38474 sorry ("atomic_cancel");
38478 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38480 /* Keep track if we're in the lexical scope of an outer transaction. */
38481 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38483 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38485 parser
->in_transaction
= new_in
;
38486 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38487 parser
->in_transaction
= old_in
;
38489 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38494 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38496 transaction-expression:
38497 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38498 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38502 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38504 unsigned char old_in
= parser
->in_transaction
;
38505 unsigned char this_in
= 1;
38509 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38511 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38512 || keyword
== RID_TRANSACTION_RELAXED
);
38516 keyword
== RID_TRANSACTION_RELAXED
38517 ? G_("%<__transaction_relaxed%> without transactional memory "
38519 : G_("%<__transaction_atomic%> without transactional memory "
38520 "support enabled"));
38522 token
= cp_parser_require_keyword (parser
, keyword
,
38523 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38524 : RT_TRANSACTION_RELAXED
));
38525 gcc_assert (token
!= NULL
);
38527 if (keyword
== RID_TRANSACTION_RELAXED
)
38528 this_in
|= TM_STMT_ATTR_RELAXED
;
38530 /* Set this early. This might mean that we allow transaction_cancel in
38531 an expression that we find out later actually has to be a constexpr.
38532 However, we expect that cxx_constant_value will be able to deal with
38533 this; also, if the noexcept has no constexpr, then what we parse next
38534 really is a transaction's body. */
38535 parser
->in_transaction
= this_in
;
38537 /* Parse a noexcept specification. */
38538 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38541 if (!noex
|| !noex_expr
38542 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38544 matching_parens parens
;
38545 parens
.require_open (parser
);
38547 expr
= cp_parser_expression (parser
);
38548 expr
= finish_parenthesized_expr (expr
);
38550 parens
.require_close (parser
);
38554 /* The only expression that is available got parsed for the noexcept
38555 already. noexcept is true then. */
38557 noex
= boolean_true_node
;
38560 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38561 parser
->in_transaction
= old_in
;
38563 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38564 return error_mark_node
;
38566 return (flag_tm
? expr
: error_mark_node
);
38569 /* Parse a function-transaction-block.
38571 function-transaction-block:
38572 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38574 __transaction_atomic txn-attribute[opt] function-try-block
38575 __transaction_relaxed ctor-initializer[opt] function-body
38576 __transaction_relaxed function-try-block
38580 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38582 unsigned char old_in
= parser
->in_transaction
;
38583 unsigned char new_in
= 1;
38584 tree compound_stmt
, stmt
, attrs
;
38587 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38588 || keyword
== RID_TRANSACTION_RELAXED
);
38589 token
= cp_parser_require_keyword (parser
, keyword
,
38590 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38591 : RT_TRANSACTION_RELAXED
));
38592 gcc_assert (token
!= NULL
);
38594 if (keyword
== RID_TRANSACTION_RELAXED
)
38595 new_in
|= TM_STMT_ATTR_RELAXED
;
38598 attrs
= cp_parser_txn_attribute_opt (parser
);
38600 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38603 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38605 parser
->in_transaction
= new_in
;
38607 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38608 cp_parser_function_try_block (parser
);
38610 cp_parser_ctor_initializer_opt_and_function_body
38611 (parser
, /*in_function_try_block=*/false);
38613 parser
->in_transaction
= old_in
;
38615 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38618 /* Parse a __transaction_cancel statement.
38621 __transaction_cancel txn-attribute[opt] ;
38622 __transaction_cancel txn-attribute[opt] throw-expression ;
38624 ??? Cancel and throw is not yet implemented. */
38627 cp_parser_transaction_cancel (cp_parser
*parser
)
38630 bool is_outer
= false;
38633 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38634 RT_TRANSACTION_CANCEL
);
38635 gcc_assert (token
!= NULL
);
38637 attrs
= cp_parser_txn_attribute_opt (parser
);
38639 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38641 /* ??? Parse cancel-and-throw here. */
38643 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38647 error_at (token
->location
, "%<__transaction_cancel%> without "
38648 "transactional memory support enabled");
38649 return error_mark_node
;
38651 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38653 error_at (token
->location
, "%<__transaction_cancel%> within a "
38654 "%<__transaction_relaxed%>");
38655 return error_mark_node
;
38659 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38660 && !is_tm_may_cancel_outer (current_function_decl
))
38662 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38663 "within outer %<__transaction_atomic%>");
38664 error_at (token
->location
,
38665 " or a %<transaction_may_cancel_outer%> function");
38666 return error_mark_node
;
38669 else if (parser
->in_transaction
== 0)
38671 error_at (token
->location
, "%<__transaction_cancel%> not within "
38672 "%<__transaction_atomic%>");
38673 return error_mark_node
;
38676 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38684 static GTY (()) cp_parser
*the_parser
;
38687 /* Special handling for the first token or line in the file. The first
38688 thing in the file might be #pragma GCC pch_preprocess, which loads a
38689 PCH file, which is a GC collection point. So we need to handle this
38690 first pragma without benefit of an existing lexer structure.
38692 Always returns one token to the caller in *FIRST_TOKEN. This is
38693 either the true first token of the file, or the first token after
38694 the initial pragma. */
38697 cp_parser_initial_pragma (cp_token
*first_token
)
38701 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38702 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38705 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38706 if (first_token
->type
== CPP_STRING
)
38708 name
= first_token
->u
.value
;
38710 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38711 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38712 error_at (first_token
->location
,
38713 "junk at end of %<#pragma GCC pch_preprocess%>");
38716 error_at (first_token
->location
, "expected string literal");
38718 /* Skip to the end of the pragma. */
38719 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38720 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38722 /* Now actually load the PCH file. */
38724 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38726 /* Read one more token to return to our caller. We have to do this
38727 after reading the PCH file in, since its pointers have to be
38729 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38732 /* Parse a pragma GCC ivdep. */
38735 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38737 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38741 /* Parse a pragma GCC unroll. */
38743 static unsigned short
38744 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38746 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38747 tree expr
= cp_parser_constant_expression (parser
);
38748 unsigned short unroll
;
38749 expr
= maybe_constant_value (expr
);
38750 HOST_WIDE_INT lunroll
= 0;
38751 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38752 || TREE_CODE (expr
) != INTEGER_CST
38753 || (lunroll
= tree_to_shwi (expr
)) < 0
38754 || lunroll
>= USHRT_MAX
)
38756 error_at (location
, "%<#pragma GCC unroll%> requires an"
38757 " assignment-expression that evaluates to a non-negative"
38758 " integral constant less than %u", USHRT_MAX
);
38763 unroll
= (unsigned short)lunroll
;
38767 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38771 /* Normal parsing of a pragma token. Here we can (and must) use the
38775 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38777 cp_token
*pragma_tok
;
38782 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38783 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38784 parser
->lexer
->in_pragma
= true;
38786 id
= cp_parser_pragma_kind (pragma_tok
);
38787 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38788 cp_ensure_no_omp_declare_simd (parser
);
38791 case PRAGMA_GCC_PCH_PREPROCESS
:
38792 error_at (pragma_tok
->location
,
38793 "%<#pragma GCC pch_preprocess%> must be first");
38796 case PRAGMA_OMP_BARRIER
:
38799 case pragma_compound
:
38800 cp_parser_omp_barrier (parser
, pragma_tok
);
38803 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38804 "used in compound statements", "omp barrier");
38811 case PRAGMA_OMP_FLUSH
:
38814 case pragma_compound
:
38815 cp_parser_omp_flush (parser
, pragma_tok
);
38818 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38819 "used in compound statements", "omp flush");
38826 case PRAGMA_OMP_TASKWAIT
:
38829 case pragma_compound
:
38830 cp_parser_omp_taskwait (parser
, pragma_tok
);
38833 error_at (pragma_tok
->location
,
38834 "%<#pragma %s%> may only be used in compound statements",
38842 case PRAGMA_OMP_TASKYIELD
:
38845 case pragma_compound
:
38846 cp_parser_omp_taskyield (parser
, pragma_tok
);
38849 error_at (pragma_tok
->location
,
38850 "%<#pragma %s%> may only be used in compound statements",
38858 case PRAGMA_OMP_CANCEL
:
38861 case pragma_compound
:
38862 cp_parser_omp_cancel (parser
, pragma_tok
);
38865 error_at (pragma_tok
->location
,
38866 "%<#pragma %s%> may only be used in compound statements",
38874 case PRAGMA_OMP_CANCELLATION_POINT
:
38875 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38878 case PRAGMA_OMP_THREADPRIVATE
:
38879 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38882 case PRAGMA_OMP_DECLARE
:
38883 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38885 case PRAGMA_OACC_DECLARE
:
38886 cp_parser_oacc_declare (parser
, pragma_tok
);
38889 case PRAGMA_OACC_ENTER_DATA
:
38890 if (context
== pragma_stmt
)
38892 error_at (pragma_tok
->location
,
38893 "%<#pragma %s%> may only be used in compound statements",
38897 else if (context
!= pragma_compound
)
38899 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38902 case PRAGMA_OACC_EXIT_DATA
:
38903 if (context
== pragma_stmt
)
38905 error_at (pragma_tok
->location
,
38906 "%<#pragma %s%> may only be used in compound statements",
38910 else if (context
!= pragma_compound
)
38912 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38915 case PRAGMA_OACC_ROUTINE
:
38916 if (context
!= pragma_external
)
38918 error_at (pragma_tok
->location
,
38919 "%<#pragma acc routine%> must be at file scope");
38922 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38925 case PRAGMA_OACC_UPDATE
:
38926 if (context
== pragma_stmt
)
38928 error_at (pragma_tok
->location
,
38929 "%<#pragma %s%> may only be used in compound statements",
38933 else if (context
!= pragma_compound
)
38935 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38938 case PRAGMA_OACC_WAIT
:
38939 if (context
== pragma_stmt
)
38941 error_at (pragma_tok
->location
,
38942 "%<#pragma %s%> may only be used in compound statements",
38946 else if (context
!= pragma_compound
)
38948 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38951 case PRAGMA_OACC_ATOMIC
:
38952 case PRAGMA_OACC_CACHE
:
38953 case PRAGMA_OACC_DATA
:
38954 case PRAGMA_OACC_HOST_DATA
:
38955 case PRAGMA_OACC_KERNELS
:
38956 case PRAGMA_OACC_PARALLEL
:
38957 case PRAGMA_OACC_LOOP
:
38958 case PRAGMA_OMP_ATOMIC
:
38959 case PRAGMA_OMP_CRITICAL
:
38960 case PRAGMA_OMP_DISTRIBUTE
:
38961 case PRAGMA_OMP_FOR
:
38962 case PRAGMA_OMP_MASTER
:
38963 case PRAGMA_OMP_PARALLEL
:
38964 case PRAGMA_OMP_SECTIONS
:
38965 case PRAGMA_OMP_SIMD
:
38966 case PRAGMA_OMP_SINGLE
:
38967 case PRAGMA_OMP_TASK
:
38968 case PRAGMA_OMP_TASKGROUP
:
38969 case PRAGMA_OMP_TASKLOOP
:
38970 case PRAGMA_OMP_TEAMS
:
38971 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38973 stmt
= push_omp_privatization_clauses (false);
38974 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38975 pop_omp_privatization_clauses (stmt
);
38978 case PRAGMA_OMP_ORDERED
:
38979 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38981 stmt
= push_omp_privatization_clauses (false);
38982 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38983 pop_omp_privatization_clauses (stmt
);
38986 case PRAGMA_OMP_TARGET
:
38987 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38989 stmt
= push_omp_privatization_clauses (false);
38990 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38991 pop_omp_privatization_clauses (stmt
);
38994 case PRAGMA_OMP_END_DECLARE_TARGET
:
38995 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38998 case PRAGMA_OMP_SECTION
:
38999 error_at (pragma_tok
->location
,
39000 "%<#pragma omp section%> may only be used in "
39001 "%<#pragma omp sections%> construct");
39006 if (context
== pragma_external
)
39008 error_at (pragma_tok
->location
,
39009 "%<#pragma GCC ivdep%> must be inside a function");
39012 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
39013 unsigned short unroll
;
39014 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39015 if (tok
->type
== CPP_PRAGMA
39016 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
39018 tok
= cp_lexer_consume_token (parser
->lexer
);
39019 unroll
= cp_parser_pragma_unroll (parser
, tok
);
39020 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39024 if (tok
->type
!= CPP_KEYWORD
39025 || (tok
->keyword
!= RID_FOR
39026 && tok
->keyword
!= RID_WHILE
39027 && tok
->keyword
!= RID_DO
))
39029 cp_parser_error (parser
, "for, while or do statement expected");
39032 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39036 case PRAGMA_UNROLL
:
39038 if (context
== pragma_external
)
39040 error_at (pragma_tok
->location
,
39041 "%<#pragma GCC unroll%> must be inside a function");
39044 const unsigned short unroll
39045 = cp_parser_pragma_unroll (parser
, pragma_tok
);
39047 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39048 if (tok
->type
== CPP_PRAGMA
39049 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
39051 tok
= cp_lexer_consume_token (parser
->lexer
);
39052 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
39053 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39057 if (tok
->type
!= CPP_KEYWORD
39058 || (tok
->keyword
!= RID_FOR
39059 && tok
->keyword
!= RID_WHILE
39060 && tok
->keyword
!= RID_DO
))
39062 cp_parser_error (parser
, "for, while or do statement expected");
39065 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39070 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
39071 c_invoke_pragma_handler (id
);
39075 cp_parser_error (parser
, "expected declaration specifiers");
39079 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39083 /* The interface the pragma parsers have to the lexer. */
39086 pragma_lex (tree
*value
, location_t
*loc
)
39088 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39089 enum cpp_ttype ret
= tok
->type
;
39091 *value
= tok
->u
.value
;
39093 *loc
= tok
->location
;
39095 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
39097 else if (ret
== CPP_STRING
)
39098 *value
= cp_parser_string_literal (the_parser
, false, false);
39101 if (ret
== CPP_KEYWORD
)
39103 cp_lexer_consume_token (the_parser
->lexer
);
39110 /* External interface. */
39112 /* Parse one entire translation unit. */
39115 c_parse_file (void)
39117 static bool already_called
= false;
39119 if (already_called
)
39120 fatal_error (input_location
,
39121 "inter-module optimizations not implemented for C++");
39122 already_called
= true;
39124 the_parser
= cp_parser_new ();
39125 push_deferring_access_checks (flag_access_control
39126 ? dk_no_deferred
: dk_no_check
);
39127 cp_parser_translation_unit (the_parser
);
39131 /* Create an identifier for a generic parameter type (a synthesized
39132 template parameter implied by `auto' or a concept identifier). */
39134 static GTY(()) int generic_parm_count
;
39136 make_generic_type_name ()
39139 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39140 return get_identifier (buf
);
39143 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39144 (creating a new template parameter list if necessary). Returns the newly
39145 created template type parm. */
39148 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39150 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39152 /* Before committing to modifying any scope, if we're in an
39153 implicit template scope, and we're trying to synthesize a
39154 constrained parameter, try to find a previous parameter with
39155 the same name. This is the same-type rule for abbreviated
39156 function templates.
39158 NOTE: We can generate implicit parameters when tentatively
39159 parsing a nested name specifier, only to reject that parse
39160 later. However, matching the same template-id as part of a
39161 direct-declarator should generate an identical template
39162 parameter, so this rule will merge them. */
39163 if (parser
->implicit_template_scope
&& constr
)
39165 tree t
= parser
->implicit_template_parms
;
39168 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39170 tree d
= TREE_VALUE (t
);
39171 if (TREE_CODE (d
) == PARM_DECL
)
39172 /* Return the TEMPLATE_PARM_INDEX. */
39173 d
= DECL_INITIAL (d
);
39176 t
= TREE_CHAIN (t
);
39180 /* We are either continuing a function template that already contains implicit
39181 template parameters, creating a new fully-implicit function template, or
39182 extending an existing explicit function template with implicit template
39185 cp_binding_level
*const entry_scope
= current_binding_level
;
39187 bool become_template
= false;
39188 cp_binding_level
*parent_scope
= 0;
39190 if (parser
->implicit_template_scope
)
39192 gcc_assert (parser
->implicit_template_parms
);
39194 current_binding_level
= parser
->implicit_template_scope
;
39198 /* Roll back to the existing template parameter scope (in the case of
39199 extending an explicit function template) or introduce a new template
39200 parameter scope ahead of the function parameter scope (or class scope
39201 in the case of out-of-line member definitions). The function scope is
39202 added back after template parameter synthesis below. */
39204 cp_binding_level
*scope
= entry_scope
;
39206 while (scope
->kind
== sk_function_parms
)
39208 parent_scope
= scope
;
39209 scope
= scope
->level_chain
;
39211 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39213 /* If not defining a class, then any class scope is a scope level in
39214 an out-of-line member definition. In this case simply wind back
39215 beyond the first such scope to inject the template parameter list.
39216 Otherwise wind back to the class being defined. The latter can
39217 occur in class member friend declarations such as:
39223 friend void A::foo (auto);
39226 The template parameter list synthesized for the friend declaration
39227 must be injected in the scope of 'B'. This can also occur in
39228 erroneous cases such as:
39234 void B::foo (auto) {}
39237 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39238 but, nevertheless, the template parameter list synthesized for the
39239 declarator should be injected into the scope of 'A' as if the
39240 ill-formed template was specified explicitly. */
39242 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39244 parent_scope
= scope
;
39245 scope
= scope
->level_chain
;
39249 current_binding_level
= scope
;
39251 if (scope
->kind
!= sk_template_parms
39252 || !function_being_declared_is_template_p (parser
))
39254 /* Introduce a new template parameter list for implicit template
39257 become_template
= true;
39259 parser
->implicit_template_scope
39260 = begin_scope (sk_template_parms
, NULL
);
39262 ++processing_template_decl
;
39264 parser
->fully_implicit_function_template_p
= true;
39265 ++parser
->num_template_parameter_lists
;
39269 /* Synthesize implicit template parameters at the end of the explicit
39270 template parameter list. */
39272 gcc_assert (current_template_parms
);
39274 parser
->implicit_template_scope
= scope
;
39276 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39277 parser
->implicit_template_parms
39278 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39282 /* Synthesize a new template parameter and track the current template
39283 parameter chain with implicit_template_parms. */
39285 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39286 tree synth_id
= make_generic_type_name ();
39287 tree synth_tmpl_parm
;
39288 bool non_type
= false;
39290 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39292 = finish_template_type_parm (class_type_node
, synth_id
);
39293 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39295 = finish_constrained_template_template_parm (proto
, synth_id
);
39298 synth_tmpl_parm
= copy_decl (proto
);
39299 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39303 // Attach the constraint to the parm before processing.
39304 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39305 TREE_TYPE (node
) = constr
;
39307 = process_template_parm (parser
->implicit_template_parms
,
39310 /*non_type=*/non_type
,
39311 /*param_pack=*/false);
39313 // Chain the new parameter to the list of implicit parameters.
39314 if (parser
->implicit_template_parms
)
39315 parser
->implicit_template_parms
39316 = TREE_CHAIN (parser
->implicit_template_parms
);
39318 parser
->implicit_template_parms
= new_parm
;
39320 tree new_decl
= get_local_decls ();
39322 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39323 new_decl
= DECL_INITIAL (new_decl
);
39325 /* If creating a fully implicit function template, start the new implicit
39326 template parameter list with this synthesized type, otherwise grow the
39327 current template parameter list. */
39329 if (become_template
)
39331 parent_scope
->level_chain
= current_binding_level
;
39333 tree new_parms
= make_tree_vec (1);
39334 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39335 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39336 new_parms
, current_template_parms
);
39340 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39341 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39342 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39343 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39346 // If the new parameter was constrained, we need to add that to the
39347 // constraints in the template parameter list.
39348 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39350 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39351 reqs
= conjoin_constraints (reqs
, req
);
39352 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39355 current_binding_level
= entry_scope
;
39360 /* Finish the declaration of a fully implicit function template. Such a
39361 template has no explicit template parameter list so has not been through the
39362 normal template head and tail processing. synthesize_implicit_template_parm
39363 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39364 provided if the declaration is a class member such that its template
39365 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39366 form is returned. Otherwise NULL_TREE is returned. */
39369 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39371 gcc_assert (parser
->fully_implicit_function_template_p
);
39373 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39374 && DECL_VIRTUAL_P (member_decl_opt
))
39376 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39377 "implicit templates may not be %<virtual%>");
39378 DECL_VIRTUAL_P (member_decl_opt
) = false;
39381 if (member_decl_opt
)
39382 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39383 end_template_decl ();
39385 parser
->fully_implicit_function_template_p
= false;
39386 parser
->implicit_template_parms
= 0;
39387 parser
->implicit_template_scope
= 0;
39388 --parser
->num_template_parameter_lists
;
39390 return member_decl_opt
;
39393 /* Like finish_fully_implicit_template, but to be used in error
39394 recovery, rearranging scopes so that we restore the state we had
39395 before synthesize_implicit_template_parm inserted the implement
39396 template parms scope. */
39399 abort_fully_implicit_template (cp_parser
*parser
)
39401 cp_binding_level
*return_to_scope
= current_binding_level
;
39403 if (parser
->implicit_template_scope
39404 && return_to_scope
!= parser
->implicit_template_scope
)
39406 cp_binding_level
*child
= return_to_scope
;
39407 for (cp_binding_level
*scope
= child
->level_chain
;
39408 scope
!= parser
->implicit_template_scope
;
39409 scope
= child
->level_chain
)
39411 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
39412 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
39413 current_binding_level
= parser
->implicit_template_scope
;
39416 return_to_scope
= return_to_scope
->level_chain
;
39418 finish_fully_implicit_template (parser
, NULL
);
39420 gcc_assert (current_binding_level
== return_to_scope
);
39423 /* Helper function for diagnostics that have complained about things
39424 being used with 'extern "C"' linkage.
39426 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39429 maybe_show_extern_c_location (void)
39431 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39432 inform (the_parser
->innermost_linkage_specification_location
,
39433 "%<extern \"C\"%> linkage started here");
39436 #include "gt-cp-parser.h"