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 KEYWORD can start a decl-specifier. */
928 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
932 /* auto specifier: storage-class-specifier in C++,
933 simple-type-specifier in C++0x. */
935 /* Storage classes. */
941 /* Elaborated type specifiers. */
947 /* Simple type specifiers. */
961 /* GNU extensions. */
964 /* C++0x extensions. */
966 case RID_UNDERLYING_TYPE
:
971 if (keyword
>= RID_FIRST_INT_N
972 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
973 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
979 /* Return true if the next token is a keyword for a decl-specifier. */
982 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
986 token
= cp_lexer_peek_token (lexer
);
987 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
990 /* Returns TRUE iff the token T begins a decltype type. */
993 token_is_decltype (cp_token
*t
)
995 return (t
->keyword
== RID_DECLTYPE
996 || t
->type
== CPP_DECLTYPE
);
999 /* Returns TRUE iff the next token begins a decltype type. */
1002 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1004 cp_token
*t
= cp_lexer_peek_token (lexer
);
1005 return token_is_decltype (t
);
1008 /* Called when processing a token with tree_check_value; perform or defer the
1009 associated checks and return the value. */
1012 saved_checks_value (struct tree_check
*check_value
)
1014 /* Perform any access checks that were deferred. */
1015 vec
<deferred_access_check
, va_gc
> *checks
;
1016 deferred_access_check
*chk
;
1017 checks
= check_value
->checks
;
1021 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1022 perform_or_defer_access_check (chk
->binfo
,
1024 chk
->diag_decl
, tf_warning_or_error
);
1026 /* Return the stored value. */
1027 return check_value
->value
;
1030 /* Return a pointer to the Nth token in the token stream. If N is 1,
1031 then this is precisely equivalent to cp_lexer_peek_token (except
1032 that it is not inline). One would like to disallow that case, but
1033 there is one case (cp_parser_nth_token_starts_template_id) where
1034 the caller passes a variable for N and it might be 1. */
1037 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1041 /* N is 1-based, not zero-based. */
1044 if (cp_lexer_debugging_p (lexer
))
1045 fprintf (cp_lexer_debug_stream
,
1046 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1049 token
= lexer
->next_token
;
1050 gcc_assert (!n
|| token
!= &eof_token
);
1054 if (token
== lexer
->last_token
)
1060 if (!token
->purged_p
)
1064 if (cp_lexer_debugging_p (lexer
))
1066 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1067 putc ('\n', cp_lexer_debug_stream
);
1073 /* Return the next token, and advance the lexer's next_token pointer
1074 to point to the next non-purged token. */
1077 cp_lexer_consume_token (cp_lexer
* lexer
)
1079 cp_token
*token
= lexer
->next_token
;
1081 gcc_assert (token
!= &eof_token
);
1082 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1086 lexer
->next_token
++;
1087 if (lexer
->next_token
== lexer
->last_token
)
1089 lexer
->next_token
= &eof_token
;
1094 while (lexer
->next_token
->purged_p
);
1096 cp_lexer_set_source_position_from_token (token
);
1098 /* Provide debugging output. */
1099 if (cp_lexer_debugging_p (lexer
))
1101 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1102 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1103 putc ('\n', cp_lexer_debug_stream
);
1109 /* Permanently remove the next token from the token stream, and
1110 advance the next_token pointer to refer to the next non-purged
1114 cp_lexer_purge_token (cp_lexer
*lexer
)
1116 cp_token
*tok
= lexer
->next_token
;
1118 gcc_assert (tok
!= &eof_token
);
1119 tok
->purged_p
= true;
1120 tok
->location
= UNKNOWN_LOCATION
;
1121 tok
->u
.value
= NULL_TREE
;
1122 tok
->keyword
= RID_MAX
;
1127 if (tok
== lexer
->last_token
)
1133 while (tok
->purged_p
);
1134 lexer
->next_token
= tok
;
1137 /* Permanently remove all tokens after TOK, up to, but not
1138 including, the token that will be returned next by
1139 cp_lexer_peek_token. */
1142 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1144 cp_token
*peek
= lexer
->next_token
;
1146 if (peek
== &eof_token
)
1147 peek
= lexer
->last_token
;
1149 gcc_assert (tok
< peek
);
1151 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1153 tok
->purged_p
= true;
1154 tok
->location
= UNKNOWN_LOCATION
;
1155 tok
->u
.value
= NULL_TREE
;
1156 tok
->keyword
= RID_MAX
;
1160 /* Begin saving tokens. All tokens consumed after this point will be
1164 cp_lexer_save_tokens (cp_lexer
* lexer
)
1166 /* Provide debugging output. */
1167 if (cp_lexer_debugging_p (lexer
))
1168 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1170 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1173 /* Commit to the portion of the token stream most recently saved. */
1176 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1178 /* Provide debugging output. */
1179 if (cp_lexer_debugging_p (lexer
))
1180 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1182 lexer
->saved_tokens
.pop ();
1185 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1186 to the token stream. Stop saving tokens. */
1189 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1191 /* Provide debugging output. */
1192 if (cp_lexer_debugging_p (lexer
))
1193 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1195 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1198 /* RAII wrapper around the above functions, with sanity checking. Creating
1199 a variable saves tokens, which are committed when the variable is
1200 destroyed unless they are explicitly rolled back by calling the rollback
1203 struct saved_token_sentinel
1208 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1210 len
= lexer
->saved_tokens
.length ();
1211 cp_lexer_save_tokens (lexer
);
1215 cp_lexer_rollback_tokens (lexer
);
1218 ~saved_token_sentinel()
1221 cp_lexer_commit_tokens (lexer
);
1222 gcc_assert (lexer
->saved_tokens
.length () == len
);
1226 /* Print a representation of the TOKEN on the STREAM. */
1229 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1231 /* We don't use cpp_type2name here because the parser defines
1232 a few tokens of its own. */
1233 static const char *const token_names
[] = {
1234 /* cpplib-defined token types */
1235 #define OP(e, s) #e,
1236 #define TK(e, s) #e,
1240 /* C++ parser token types - see "Manifest constants", above. */
1243 "NESTED_NAME_SPECIFIER",
1246 /* For some tokens, print the associated data. */
1247 switch (token
->type
)
1250 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1251 For example, `struct' is mapped to an INTEGER_CST. */
1252 if (!identifier_p (token
->u
.value
))
1256 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1263 case CPP_UTF8STRING
:
1264 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1268 print_generic_expr (stream
, token
->u
.value
);
1272 /* If we have a name for the token, print it out. Otherwise, we
1273 simply give the numeric code. */
1274 if (token
->type
< ARRAY_SIZE(token_names
))
1275 fputs (token_names
[token
->type
], stream
);
1277 fprintf (stream
, "[%d]", token
->type
);
1283 debug (cp_token
&ref
)
1285 cp_lexer_print_token (stderr
, &ref
);
1286 fprintf (stderr
, "\n");
1290 debug (cp_token
*ptr
)
1295 fprintf (stderr
, "<nil>\n");
1299 /* Start emitting debugging information. */
1302 cp_lexer_start_debugging (cp_lexer
* lexer
)
1304 if (!LEXER_DEBUGGING_ENABLED_P
)
1305 fatal_error (input_location
,
1306 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1308 lexer
->debugging_p
= true;
1309 cp_lexer_debug_stream
= stderr
;
1312 /* Stop emitting debugging information. */
1315 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1317 if (!LEXER_DEBUGGING_ENABLED_P
)
1318 fatal_error (input_location
,
1319 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1321 lexer
->debugging_p
= false;
1322 cp_lexer_debug_stream
= NULL
;
1325 /* Create a new cp_token_cache, representing a range of tokens. */
1327 static cp_token_cache
*
1328 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1330 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1331 cache
->first
= first
;
1336 /* Diagnose if #pragma omp declare simd isn't followed immediately
1337 by function declaration or definition. */
1340 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1342 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1344 error ("%<#pragma omp declare simd%> not immediately followed by "
1345 "function declaration or definition");
1346 parser
->omp_declare_simd
= NULL
;
1350 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1351 and put that into "omp declare simd" attribute. */
1354 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1356 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1358 if (fndecl
== error_mark_node
)
1360 parser
->omp_declare_simd
= NULL
;
1363 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1365 cp_ensure_no_omp_declare_simd (parser
);
1371 /* Diagnose if #pragma acc routine isn't followed immediately by function
1372 declaration or definition. */
1375 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1377 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1379 error_at (parser
->oacc_routine
->loc
,
1380 "%<#pragma acc routine%> not immediately followed by "
1381 "function declaration or definition");
1382 parser
->oacc_routine
= NULL
;
1386 /* Decl-specifiers. */
1388 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1391 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1393 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1398 /* Nothing other than the parser should be creating declarators;
1399 declarators are a semi-syntactic representation of C++ entities.
1400 Other parts of the front end that need to create entities (like
1401 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1403 static cp_declarator
*make_call_declarator
1404 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1405 static cp_declarator
*make_array_declarator
1406 (cp_declarator
*, tree
);
1407 static cp_declarator
*make_pointer_declarator
1408 (cp_cv_quals
, cp_declarator
*, tree
);
1409 static cp_declarator
*make_reference_declarator
1410 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1411 static cp_declarator
*make_ptrmem_declarator
1412 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1414 /* An erroneous declarator. */
1415 static cp_declarator
*cp_error_declarator
;
1417 /* The obstack on which declarators and related data structures are
1419 static struct obstack declarator_obstack
;
1421 /* Alloc BYTES from the declarator memory pool. */
1423 static inline void *
1424 alloc_declarator (size_t bytes
)
1426 return obstack_alloc (&declarator_obstack
, bytes
);
1429 /* Allocate a declarator of the indicated KIND. Clear fields that are
1430 common to all declarators. */
1432 static cp_declarator
*
1433 make_declarator (cp_declarator_kind kind
)
1435 cp_declarator
*declarator
;
1437 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1438 declarator
->kind
= kind
;
1439 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1440 declarator
->attributes
= NULL_TREE
;
1441 declarator
->std_attributes
= NULL_TREE
;
1442 declarator
->declarator
= NULL
;
1443 declarator
->parameter_pack_p
= false;
1444 declarator
->id_loc
= UNKNOWN_LOCATION
;
1449 /* Make a declarator for a generalized identifier. If
1450 QUALIFYING_SCOPE is non-NULL, the identifier is
1451 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1452 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1455 static cp_declarator
*
1456 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1457 special_function_kind sfk
)
1459 cp_declarator
*declarator
;
1461 /* It is valid to write:
1463 class C { void f(); };
1467 The standard is not clear about whether `typedef const C D' is
1468 legal; as of 2002-09-15 the committee is considering that
1469 question. EDG 3.0 allows that syntax. Therefore, we do as
1471 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1472 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1474 gcc_assert (identifier_p (unqualified_name
)
1475 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1476 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1478 declarator
= make_declarator (cdk_id
);
1479 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1480 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1481 declarator
->u
.id
.sfk
= sfk
;
1486 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1487 of modifiers such as const or volatile to apply to the pointer
1488 type, represented as identifiers. ATTRIBUTES represent the attributes that
1489 appertain to the pointer or reference. */
1492 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1495 cp_declarator
*declarator
;
1497 declarator
= make_declarator (cdk_pointer
);
1498 declarator
->declarator
= target
;
1499 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1500 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1503 declarator
->id_loc
= target
->id_loc
;
1504 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1505 target
->parameter_pack_p
= false;
1508 declarator
->parameter_pack_p
= false;
1510 declarator
->std_attributes
= attributes
;
1515 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1516 represent the attributes that appertain to the pointer or
1520 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1521 bool rvalue_ref
, tree attributes
)
1523 cp_declarator
*declarator
;
1525 declarator
= make_declarator (cdk_reference
);
1526 declarator
->declarator
= target
;
1527 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1528 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1531 declarator
->id_loc
= target
->id_loc
;
1532 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1533 target
->parameter_pack_p
= false;
1536 declarator
->parameter_pack_p
= false;
1538 declarator
->std_attributes
= attributes
;
1543 /* Like make_pointer_declarator -- but for a pointer to a non-static
1544 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1545 appertain to the pointer or reference. */
1548 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1549 cp_declarator
*pointee
,
1552 cp_declarator
*declarator
;
1554 declarator
= make_declarator (cdk_ptrmem
);
1555 declarator
->declarator
= pointee
;
1556 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1557 declarator
->u
.pointer
.class_type
= class_type
;
1561 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1562 pointee
->parameter_pack_p
= false;
1565 declarator
->parameter_pack_p
= false;
1567 declarator
->std_attributes
= attributes
;
1572 /* Make a declarator for the function given by TARGET, with the
1573 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1574 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1575 indicates what exceptions can be thrown. */
1578 make_call_declarator (cp_declarator
*target
,
1580 cp_cv_quals cv_qualifiers
,
1581 cp_virt_specifiers virt_specifiers
,
1582 cp_ref_qualifier ref_qualifier
,
1584 tree exception_specification
,
1585 tree late_return_type
,
1586 tree requires_clause
)
1588 cp_declarator
*declarator
;
1590 declarator
= make_declarator (cdk_function
);
1591 declarator
->declarator
= target
;
1592 declarator
->u
.function
.parameters
= parms
;
1593 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1594 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1595 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1596 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1597 declarator
->u
.function
.exception_specification
= exception_specification
;
1598 declarator
->u
.function
.late_return_type
= late_return_type
;
1599 declarator
->u
.function
.requires_clause
= requires_clause
;
1602 declarator
->id_loc
= target
->id_loc
;
1603 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1604 target
->parameter_pack_p
= false;
1607 declarator
->parameter_pack_p
= false;
1612 /* Make a declarator for an array of BOUNDS elements, each of which is
1613 defined by ELEMENT. */
1616 make_array_declarator (cp_declarator
*element
, tree bounds
)
1618 cp_declarator
*declarator
;
1620 declarator
= make_declarator (cdk_array
);
1621 declarator
->declarator
= element
;
1622 declarator
->u
.array
.bounds
= bounds
;
1625 declarator
->id_loc
= element
->id_loc
;
1626 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1627 element
->parameter_pack_p
= false;
1630 declarator
->parameter_pack_p
= false;
1635 /* Determine whether the declarator we've seen so far can be a
1636 parameter pack, when followed by an ellipsis. */
1638 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1640 if (declarator
&& declarator
->parameter_pack_p
)
1641 /* We already saw an ellipsis. */
1644 /* Search for a declarator name, or any other declarator that goes
1645 after the point where the ellipsis could appear in a parameter
1646 pack. If we find any of these, then this declarator can not be
1647 made into a parameter pack. */
1649 while (declarator
&& !found
)
1651 switch ((int)declarator
->kind
)
1663 declarator
= declarator
->declarator
;
1671 cp_parameter_declarator
*no_parameters
;
1673 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1674 DECLARATOR and DEFAULT_ARGUMENT. */
1676 cp_parameter_declarator
*
1677 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1678 cp_declarator
*declarator
,
1679 tree default_argument
,
1681 bool template_parameter_pack_p
= false)
1683 cp_parameter_declarator
*parameter
;
1685 parameter
= ((cp_parameter_declarator
*)
1686 alloc_declarator (sizeof (cp_parameter_declarator
)));
1687 parameter
->next
= NULL
;
1688 if (decl_specifiers
)
1689 parameter
->decl_specifiers
= *decl_specifiers
;
1691 clear_decl_specs (¶meter
->decl_specifiers
);
1692 parameter
->declarator
= declarator
;
1693 parameter
->default_argument
= default_argument
;
1694 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1695 parameter
->loc
= loc
;
1700 /* Returns true iff DECLARATOR is a declaration for a function. */
1703 function_declarator_p (const cp_declarator
*declarator
)
1707 if (declarator
->kind
== cdk_function
1708 && declarator
->declarator
->kind
== cdk_id
)
1710 if (declarator
->kind
== cdk_id
1711 || declarator
->kind
== cdk_decomp
1712 || declarator
->kind
== cdk_error
)
1714 declarator
= declarator
->declarator
;
1724 A cp_parser parses the token stream as specified by the C++
1725 grammar. Its job is purely parsing, not semantic analysis. For
1726 example, the parser breaks the token stream into declarators,
1727 expressions, statements, and other similar syntactic constructs.
1728 It does not check that the types of the expressions on either side
1729 of an assignment-statement are compatible, or that a function is
1730 not declared with a parameter of type `void'.
1732 The parser invokes routines elsewhere in the compiler to perform
1733 semantic analysis and to build up the abstract syntax tree for the
1736 The parser (and the template instantiation code, which is, in a
1737 way, a close relative of parsing) are the only parts of the
1738 compiler that should be calling push_scope and pop_scope, or
1739 related functions. The parser (and template instantiation code)
1740 keeps track of what scope is presently active; everything else
1741 should simply honor that. (The code that generates static
1742 initializers may also need to set the scope, in order to check
1743 access control correctly when emitting the initializers.)
1748 The parser is of the standard recursive-descent variety. Upcoming
1749 tokens in the token stream are examined in order to determine which
1750 production to use when parsing a non-terminal. Some C++ constructs
1751 require arbitrary look ahead to disambiguate. For example, it is
1752 impossible, in the general case, to tell whether a statement is an
1753 expression or declaration without scanning the entire statement.
1754 Therefore, the parser is capable of "parsing tentatively." When the
1755 parser is not sure what construct comes next, it enters this mode.
1756 Then, while we attempt to parse the construct, the parser queues up
1757 error messages, rather than issuing them immediately, and saves the
1758 tokens it consumes. If the construct is parsed successfully, the
1759 parser "commits", i.e., it issues any queued error messages and
1760 the tokens that were being preserved are permanently discarded.
1761 If, however, the construct is not parsed successfully, the parser
1762 rolls back its state completely so that it can resume parsing using
1763 a different alternative.
1768 The performance of the parser could probably be improved substantially.
1769 We could often eliminate the need to parse tentatively by looking ahead
1770 a little bit. In some places, this approach might not entirely eliminate
1771 the need to parse tentatively, but it might still speed up the average
1774 /* Flags that are passed to some parsing functions. These values can
1775 be bitwise-ored together. */
1780 CP_PARSER_FLAGS_NONE
= 0x0,
1781 /* The construct is optional. If it is not present, then no error
1782 should be issued. */
1783 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1784 /* When parsing a type-specifier, treat user-defined type-names
1785 as non-type identifiers. */
1786 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1787 /* When parsing a type-specifier, do not try to parse a class-specifier
1788 or enum-specifier. */
1789 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1790 /* When parsing a decl-specifier-seq, only allow type-specifier or
1792 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1793 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1794 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1797 /* This type is used for parameters and variables which hold
1798 combinations of the above flags. */
1799 typedef int cp_parser_flags
;
1801 /* The different kinds of declarators we want to parse. */
1803 enum cp_parser_declarator_kind
1805 /* We want an abstract declarator. */
1806 CP_PARSER_DECLARATOR_ABSTRACT
,
1807 /* We want a named declarator. */
1808 CP_PARSER_DECLARATOR_NAMED
,
1809 /* We don't mind, but the name must be an unqualified-id. */
1810 CP_PARSER_DECLARATOR_EITHER
1813 /* The precedence values used to parse binary expressions. The minimum value
1814 of PREC must be 1, because zero is reserved to quickly discriminate
1815 binary operators from other tokens. */
1820 PREC_LOGICAL_OR_EXPRESSION
,
1821 PREC_LOGICAL_AND_EXPRESSION
,
1822 PREC_INCLUSIVE_OR_EXPRESSION
,
1823 PREC_EXCLUSIVE_OR_EXPRESSION
,
1824 PREC_AND_EXPRESSION
,
1825 PREC_EQUALITY_EXPRESSION
,
1826 PREC_RELATIONAL_EXPRESSION
,
1827 PREC_SHIFT_EXPRESSION
,
1828 PREC_ADDITIVE_EXPRESSION
,
1829 PREC_MULTIPLICATIVE_EXPRESSION
,
1831 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1834 /* A mapping from a token type to a corresponding tree node type, with a
1835 precedence value. */
1837 struct cp_parser_binary_operations_map_node
1839 /* The token type. */
1840 enum cpp_ttype token_type
;
1841 /* The corresponding tree code. */
1842 enum tree_code tree_type
;
1843 /* The precedence of this operator. */
1844 enum cp_parser_prec prec
;
1847 struct cp_parser_expression_stack_entry
1849 /* Left hand side of the binary operation we are currently
1852 /* Original tree code for left hand side, if it was a binary
1853 expression itself (used for -Wparentheses). */
1854 enum tree_code lhs_type
;
1855 /* Tree code for the binary operation we are parsing. */
1856 enum tree_code tree_type
;
1857 /* Precedence of the binary operation we are parsing. */
1858 enum cp_parser_prec prec
;
1859 /* Location of the binary operation we are parsing. */
1863 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1864 entries because precedence levels on the stack are monotonically
1866 typedef struct cp_parser_expression_stack_entry
1867 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1871 /* Constructors and destructors. */
1873 static cp_parser_context
*cp_parser_context_new
1874 (cp_parser_context
*);
1876 /* Class variables. */
1878 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1880 /* The operator-precedence table used by cp_parser_binary_expression.
1881 Transformed into an associative array (binops_by_token) by
1884 static const cp_parser_binary_operations_map_node binops
[] = {
1885 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1886 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1888 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1889 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1890 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1892 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1893 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1895 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1896 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1898 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1899 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1900 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1901 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1903 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1904 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1906 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1908 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1910 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1912 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1914 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1917 /* The same as binops, but initialized by cp_parser_new so that
1918 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1920 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1922 /* Constructors and destructors. */
1924 /* Construct a new context. The context below this one on the stack
1925 is given by NEXT. */
1927 static cp_parser_context
*
1928 cp_parser_context_new (cp_parser_context
* next
)
1930 cp_parser_context
*context
;
1932 /* Allocate the storage. */
1933 if (cp_parser_context_free_list
!= NULL
)
1935 /* Pull the first entry from the free list. */
1936 context
= cp_parser_context_free_list
;
1937 cp_parser_context_free_list
= context
->next
;
1938 memset (context
, 0, sizeof (*context
));
1941 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1943 /* No errors have occurred yet in this context. */
1944 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1945 /* If this is not the bottommost context, copy information that we
1946 need from the previous context. */
1949 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1950 expression, then we are parsing one in this context, too. */
1951 context
->object_type
= next
->object_type
;
1952 /* Thread the stack. */
1953 context
->next
= next
;
1959 /* Managing the unparsed function queues. */
1961 #define unparsed_funs_with_default_args \
1962 parser->unparsed_queues->last ().funs_with_default_args
1963 #define unparsed_funs_with_definitions \
1964 parser->unparsed_queues->last ().funs_with_definitions
1965 #define unparsed_nsdmis \
1966 parser->unparsed_queues->last ().nsdmis
1967 #define unparsed_classes \
1968 parser->unparsed_queues->last ().classes
1971 push_unparsed_function_queues (cp_parser
*parser
)
1973 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1974 vec_safe_push (parser
->unparsed_queues
, e
);
1978 pop_unparsed_function_queues (cp_parser
*parser
)
1980 release_tree_vector (unparsed_funs_with_definitions
);
1981 parser
->unparsed_queues
->pop ();
1986 /* Constructors and destructors. */
1988 static cp_parser
*cp_parser_new
1991 /* Routines to parse various constructs.
1993 Those that return `tree' will return the error_mark_node (rather
1994 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1995 Sometimes, they will return an ordinary node if error-recovery was
1996 attempted, even though a parse error occurred. So, to check
1997 whether or not a parse error occurred, you should always use
1998 cp_parser_error_occurred. If the construct is optional (indicated
1999 either by an `_opt' in the name of the function that does the
2000 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2001 the construct is not present. */
2003 /* Lexical conventions [gram.lex] */
2005 static cp_expr cp_parser_identifier
2007 static cp_expr cp_parser_string_literal
2008 (cp_parser
*, bool, bool, bool);
2009 static cp_expr cp_parser_userdef_char_literal
2011 static tree cp_parser_userdef_string_literal
2013 static cp_expr cp_parser_userdef_numeric_literal
2016 /* Basic concepts [gram.basic] */
2018 static bool cp_parser_translation_unit
2021 /* Expressions [gram.expr] */
2023 static cp_expr cp_parser_primary_expression
2024 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2025 static cp_expr cp_parser_id_expression
2026 (cp_parser
*, bool, bool, bool *, bool, bool);
2027 static cp_expr cp_parser_unqualified_id
2028 (cp_parser
*, bool, bool, bool, bool);
2029 static tree cp_parser_nested_name_specifier_opt
2030 (cp_parser
*, bool, bool, bool, bool, bool = false);
2031 static tree cp_parser_nested_name_specifier
2032 (cp_parser
*, bool, bool, bool, bool);
2033 static tree cp_parser_qualifying_entity
2034 (cp_parser
*, bool, bool, bool, bool, bool);
2035 static cp_expr cp_parser_postfix_expression
2036 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2037 static tree cp_parser_postfix_open_square_expression
2038 (cp_parser
*, tree
, bool, bool);
2039 static tree cp_parser_postfix_dot_deref_expression
2040 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2041 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2042 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2044 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2045 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2046 static void cp_parser_pseudo_destructor_name
2047 (cp_parser
*, tree
, tree
*, tree
*);
2048 static cp_expr cp_parser_unary_expression
2049 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2050 static enum tree_code cp_parser_unary_operator
2052 static tree cp_parser_new_expression
2054 static vec
<tree
, va_gc
> *cp_parser_new_placement
2056 static tree cp_parser_new_type_id
2057 (cp_parser
*, tree
*);
2058 static cp_declarator
*cp_parser_new_declarator_opt
2060 static cp_declarator
*cp_parser_direct_new_declarator
2062 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2064 static tree cp_parser_delete_expression
2066 static cp_expr cp_parser_cast_expression
2067 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2068 static cp_expr cp_parser_binary_expression
2069 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2070 static tree cp_parser_question_colon_clause
2071 (cp_parser
*, cp_expr
);
2072 static cp_expr cp_parser_assignment_expression
2073 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2074 static enum tree_code cp_parser_assignment_operator_opt
2076 static cp_expr cp_parser_expression
2077 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2078 static cp_expr cp_parser_constant_expression
2079 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2080 static cp_expr cp_parser_builtin_offsetof
2082 static cp_expr cp_parser_lambda_expression
2084 static void cp_parser_lambda_introducer
2085 (cp_parser
*, tree
);
2086 static bool cp_parser_lambda_declarator_opt
2087 (cp_parser
*, tree
);
2088 static void cp_parser_lambda_body
2089 (cp_parser
*, tree
);
2091 /* Statements [gram.stmt.stmt] */
2093 static void cp_parser_statement
2094 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2095 static void cp_parser_label_for_labeled_statement
2096 (cp_parser
*, tree
);
2097 static tree cp_parser_expression_statement
2098 (cp_parser
*, tree
);
2099 static tree cp_parser_compound_statement
2100 (cp_parser
*, tree
, int, bool);
2101 static void cp_parser_statement_seq_opt
2102 (cp_parser
*, tree
);
2103 static tree cp_parser_selection_statement
2104 (cp_parser
*, bool *, vec
<tree
> *);
2105 static tree cp_parser_condition
2107 static tree cp_parser_iteration_statement
2108 (cp_parser
*, bool *, bool, unsigned short);
2109 static bool cp_parser_init_statement
2110 (cp_parser
*, tree
*decl
);
2111 static tree cp_parser_for
2112 (cp_parser
*, bool, unsigned short);
2113 static tree cp_parser_c_for
2114 (cp_parser
*, tree
, tree
, bool, unsigned short);
2115 static tree cp_parser_range_for
2116 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short);
2117 static void do_range_for_auto_deduction
2119 static tree cp_parser_perform_range_for_lookup
2120 (tree
, tree
*, tree
*);
2121 static tree cp_parser_range_for_member_function
2123 static tree cp_parser_jump_statement
2125 static void cp_parser_declaration_statement
2128 static tree cp_parser_implicitly_scoped_statement
2129 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2130 static void cp_parser_already_scoped_statement
2131 (cp_parser
*, bool *, const token_indent_info
&);
2133 /* Declarations [gram.dcl.dcl] */
2135 static void cp_parser_declaration_seq_opt
2137 static void cp_parser_declaration
2139 static void cp_parser_block_declaration
2140 (cp_parser
*, bool);
2141 static void cp_parser_simple_declaration
2142 (cp_parser
*, bool, tree
*);
2143 static void cp_parser_decl_specifier_seq
2144 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2145 static tree cp_parser_storage_class_specifier_opt
2147 static tree cp_parser_function_specifier_opt
2148 (cp_parser
*, cp_decl_specifier_seq
*);
2149 static tree cp_parser_type_specifier
2150 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2152 static tree cp_parser_simple_type_specifier
2153 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2154 static tree cp_parser_type_name
2155 (cp_parser
*, bool);
2156 static tree cp_parser_type_name
2158 static tree cp_parser_nonclass_name
2159 (cp_parser
* parser
);
2160 static tree cp_parser_elaborated_type_specifier
2161 (cp_parser
*, bool, bool);
2162 static tree cp_parser_enum_specifier
2164 static void cp_parser_enumerator_list
2165 (cp_parser
*, tree
);
2166 static void cp_parser_enumerator_definition
2167 (cp_parser
*, tree
);
2168 static tree cp_parser_namespace_name
2170 static void cp_parser_namespace_definition
2172 static void cp_parser_namespace_body
2174 static tree cp_parser_qualified_namespace_specifier
2176 static void cp_parser_namespace_alias_definition
2178 static bool cp_parser_using_declaration
2179 (cp_parser
*, bool);
2180 static void cp_parser_using_directive
2182 static tree cp_parser_alias_declaration
2184 static void cp_parser_asm_definition
2186 static void cp_parser_linkage_specification
2188 static void cp_parser_static_assert
2189 (cp_parser
*, bool);
2190 static tree cp_parser_decltype
2192 static tree cp_parser_decomposition_declaration
2193 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2195 /* Declarators [gram.dcl.decl] */
2197 static tree cp_parser_init_declarator
2198 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2199 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2200 static cp_declarator
*cp_parser_declarator
2201 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2202 static cp_declarator
*cp_parser_direct_declarator
2203 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2204 static enum tree_code cp_parser_ptr_operator
2205 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2206 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2208 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2210 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2212 static tree cp_parser_tx_qualifier_opt
2214 static tree cp_parser_late_return_type_opt
2215 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2216 static tree cp_parser_declarator_id
2217 (cp_parser
*, bool);
2218 static tree cp_parser_type_id
2220 static tree cp_parser_template_type_arg
2222 static tree
cp_parser_trailing_type_id (cp_parser
*);
2223 static tree cp_parser_type_id_1
2224 (cp_parser
*, bool, bool);
2225 static void cp_parser_type_specifier_seq
2226 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2227 static tree cp_parser_parameter_declaration_clause
2229 static tree cp_parser_parameter_declaration_list
2231 static cp_parameter_declarator
*cp_parser_parameter_declaration
2232 (cp_parser
*, bool, bool *);
2233 static tree cp_parser_default_argument
2234 (cp_parser
*, bool);
2235 static void cp_parser_function_body
2236 (cp_parser
*, bool);
2237 static tree cp_parser_initializer
2238 (cp_parser
*, bool *, bool *, bool = false);
2239 static cp_expr cp_parser_initializer_clause
2240 (cp_parser
*, bool *);
2241 static cp_expr cp_parser_braced_list
2242 (cp_parser
*, bool*);
2243 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2244 (cp_parser
*, bool *);
2246 static void cp_parser_ctor_initializer_opt_and_function_body
2247 (cp_parser
*, bool);
2249 static tree cp_parser_late_parsing_omp_declare_simd
2250 (cp_parser
*, tree
);
2252 static tree cp_parser_late_parsing_oacc_routine
2253 (cp_parser
*, tree
);
2255 static tree synthesize_implicit_template_parm
2256 (cp_parser
*, tree
);
2257 static tree finish_fully_implicit_template
2258 (cp_parser
*, tree
);
2259 static void abort_fully_implicit_template
2262 /* Classes [gram.class] */
2264 static tree cp_parser_class_name
2265 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2266 static tree cp_parser_class_specifier
2268 static tree cp_parser_class_head
2269 (cp_parser
*, bool *);
2270 static enum tag_types cp_parser_class_key
2272 static void cp_parser_type_parameter_key
2273 (cp_parser
* parser
);
2274 static void cp_parser_member_specification_opt
2276 static void cp_parser_member_declaration
2278 static tree cp_parser_pure_specifier
2280 static tree cp_parser_constant_initializer
2283 /* Derived classes [gram.class.derived] */
2285 static tree cp_parser_base_clause
2287 static tree cp_parser_base_specifier
2290 /* Special member functions [gram.special] */
2292 static tree cp_parser_conversion_function_id
2294 static tree cp_parser_conversion_type_id
2296 static cp_declarator
*cp_parser_conversion_declarator_opt
2298 static void cp_parser_ctor_initializer_opt
2300 static void cp_parser_mem_initializer_list
2302 static tree cp_parser_mem_initializer
2304 static tree cp_parser_mem_initializer_id
2307 /* Overloading [gram.over] */
2309 static cp_expr cp_parser_operator_function_id
2311 static cp_expr cp_parser_operator
2314 /* Templates [gram.temp] */
2316 static void cp_parser_template_declaration
2317 (cp_parser
*, bool);
2318 static tree cp_parser_template_parameter_list
2320 static tree cp_parser_template_parameter
2321 (cp_parser
*, bool *, bool *);
2322 static tree cp_parser_type_parameter
2323 (cp_parser
*, bool *);
2324 static tree cp_parser_template_id
2325 (cp_parser
*, bool, bool, enum tag_types
, bool);
2326 static tree cp_parser_template_name
2327 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2328 static tree cp_parser_template_argument_list
2330 static tree cp_parser_template_argument
2332 static void cp_parser_explicit_instantiation
2334 static void cp_parser_explicit_specialization
2337 /* Exception handling [gram.exception] */
2339 static tree cp_parser_try_block
2341 static void cp_parser_function_try_block
2343 static void cp_parser_handler_seq
2345 static void cp_parser_handler
2347 static tree cp_parser_exception_declaration
2349 static tree cp_parser_throw_expression
2351 static tree cp_parser_exception_specification_opt
2353 static tree cp_parser_type_id_list
2356 /* GNU Extensions */
2358 static tree cp_parser_asm_specification_opt
2360 static tree cp_parser_asm_operand_list
2362 static tree cp_parser_asm_clobber_list
2364 static tree cp_parser_asm_label_list
2366 static bool cp_next_tokens_can_be_attribute_p
2368 static bool cp_next_tokens_can_be_gnu_attribute_p
2370 static bool cp_next_tokens_can_be_std_attribute_p
2372 static bool cp_nth_tokens_can_be_std_attribute_p
2373 (cp_parser
*, size_t);
2374 static bool cp_nth_tokens_can_be_gnu_attribute_p
2375 (cp_parser
*, size_t);
2376 static bool cp_nth_tokens_can_be_attribute_p
2377 (cp_parser
*, size_t);
2378 static tree cp_parser_attributes_opt
2380 static tree cp_parser_gnu_attributes_opt
2382 static tree cp_parser_gnu_attribute_list
2384 static tree cp_parser_std_attribute
2385 (cp_parser
*, tree
);
2386 static tree cp_parser_std_attribute_spec
2388 static tree cp_parser_std_attribute_spec_seq
2390 static size_t cp_parser_skip_attributes_opt
2391 (cp_parser
*, size_t);
2392 static bool cp_parser_extension_opt
2393 (cp_parser
*, int *);
2394 static void cp_parser_label_declaration
2397 /* Concept Extensions */
2399 static tree cp_parser_requires_clause
2401 static tree cp_parser_requires_clause_opt
2403 static tree cp_parser_requires_expression
2405 static tree cp_parser_requirement_parameter_list
2407 static tree cp_parser_requirement_body
2409 static tree cp_parser_requirement_list
2411 static tree cp_parser_requirement
2413 static tree cp_parser_simple_requirement
2415 static tree cp_parser_compound_requirement
2417 static tree cp_parser_type_requirement
2419 static tree cp_parser_nested_requirement
2422 /* Transactional Memory Extensions */
2424 static tree cp_parser_transaction
2425 (cp_parser
*, cp_token
*);
2426 static tree cp_parser_transaction_expression
2427 (cp_parser
*, enum rid
);
2428 static void cp_parser_function_transaction
2429 (cp_parser
*, enum rid
);
2430 static tree cp_parser_transaction_cancel
2433 enum pragma_context
{
2440 static bool cp_parser_pragma
2441 (cp_parser
*, enum pragma_context
, bool *);
2443 /* Objective-C++ Productions */
2445 static tree cp_parser_objc_message_receiver
2447 static tree cp_parser_objc_message_args
2449 static tree cp_parser_objc_message_expression
2451 static cp_expr cp_parser_objc_encode_expression
2453 static tree cp_parser_objc_defs_expression
2455 static tree cp_parser_objc_protocol_expression
2457 static tree cp_parser_objc_selector_expression
2459 static cp_expr cp_parser_objc_expression
2461 static bool cp_parser_objc_selector_p
2463 static tree cp_parser_objc_selector
2465 static tree cp_parser_objc_protocol_refs_opt
2467 static void cp_parser_objc_declaration
2468 (cp_parser
*, tree
);
2469 static tree cp_parser_objc_statement
2471 static bool cp_parser_objc_valid_prefix_attributes
2472 (cp_parser
*, tree
*);
2473 static void cp_parser_objc_at_property_declaration
2475 static void cp_parser_objc_at_synthesize_declaration
2477 static void cp_parser_objc_at_dynamic_declaration
2479 static tree cp_parser_objc_struct_declaration
2482 /* Utility Routines */
2484 static cp_expr cp_parser_lookup_name
2485 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2486 static tree cp_parser_lookup_name_simple
2487 (cp_parser
*, tree
, location_t
);
2488 static tree cp_parser_maybe_treat_template_as_class
2490 static bool cp_parser_check_declarator_template_parameters
2491 (cp_parser
*, cp_declarator
*, location_t
);
2492 static bool cp_parser_check_template_parameters
2493 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2494 static cp_expr cp_parser_simple_cast_expression
2496 static tree cp_parser_global_scope_opt
2497 (cp_parser
*, bool);
2498 static bool cp_parser_constructor_declarator_p
2499 (cp_parser
*, bool);
2500 static tree cp_parser_function_definition_from_specifiers_and_declarator
2501 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2502 static tree cp_parser_function_definition_after_declarator
2503 (cp_parser
*, bool);
2504 static bool cp_parser_template_declaration_after_export
2505 (cp_parser
*, bool);
2506 static void cp_parser_perform_template_parameter_access_checks
2507 (vec
<deferred_access_check
, va_gc
> *);
2508 static tree cp_parser_single_declaration
2509 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2510 static cp_expr cp_parser_functional_cast
2511 (cp_parser
*, tree
);
2512 static tree cp_parser_save_member_function_body
2513 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2514 static tree cp_parser_save_nsdmi
2516 static tree cp_parser_enclosed_template_argument_list
2518 static void cp_parser_save_default_args
2519 (cp_parser
*, tree
);
2520 static void cp_parser_late_parsing_for_member
2521 (cp_parser
*, tree
);
2522 static tree cp_parser_late_parse_one_default_arg
2523 (cp_parser
*, tree
, tree
, tree
);
2524 static void cp_parser_late_parsing_nsdmi
2525 (cp_parser
*, tree
);
2526 static void cp_parser_late_parsing_default_args
2527 (cp_parser
*, tree
);
2528 static tree cp_parser_sizeof_operand
2529 (cp_parser
*, enum rid
);
2530 static cp_expr cp_parser_trait_expr
2531 (cp_parser
*, enum rid
);
2532 static bool cp_parser_declares_only_class_p
2534 static void cp_parser_set_storage_class
2535 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2536 static void cp_parser_set_decl_spec_type
2537 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2538 static void set_and_check_decl_spec_loc
2539 (cp_decl_specifier_seq
*decl_specs
,
2540 cp_decl_spec ds
, cp_token
*);
2541 static bool cp_parser_friend_p
2542 (const cp_decl_specifier_seq
*);
2543 static void cp_parser_required_error
2544 (cp_parser
*, required_token
, bool, location_t
);
2545 static cp_token
*cp_parser_require
2546 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2547 static cp_token
*cp_parser_require_keyword
2548 (cp_parser
*, enum rid
, required_token
);
2549 static bool cp_parser_token_starts_function_definition_p
2551 static bool cp_parser_next_token_starts_class_definition_p
2553 static bool cp_parser_next_token_ends_template_argument_p
2555 static bool cp_parser_nth_token_starts_template_argument_list_p
2556 (cp_parser
*, size_t);
2557 static enum tag_types cp_parser_token_is_class_key
2559 static enum tag_types cp_parser_token_is_type_parameter_key
2561 static void cp_parser_check_class_key
2562 (enum tag_types
, tree type
);
2563 static void cp_parser_check_access_in_redeclaration
2564 (tree type
, location_t location
);
2565 static bool cp_parser_optional_template_keyword
2567 static void cp_parser_pre_parsed_nested_name_specifier
2569 static bool cp_parser_cache_group
2570 (cp_parser
*, enum cpp_ttype
, unsigned);
2571 static tree cp_parser_cache_defarg
2572 (cp_parser
*parser
, bool nsdmi
);
2573 static void cp_parser_parse_tentatively
2575 static void cp_parser_commit_to_tentative_parse
2577 static void cp_parser_commit_to_topmost_tentative_parse
2579 static void cp_parser_abort_tentative_parse
2581 static bool cp_parser_parse_definitely
2583 static inline bool cp_parser_parsing_tentatively
2585 static bool cp_parser_uncommitted_to_tentative_parse_p
2587 static void cp_parser_error
2588 (cp_parser
*, const char *);
2589 static void cp_parser_name_lookup_error
2590 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2591 static bool cp_parser_simulate_error
2593 static bool cp_parser_check_type_definition
2595 static void cp_parser_check_for_definition_in_return_type
2596 (cp_declarator
*, tree
, location_t type_location
);
2597 static void cp_parser_check_for_invalid_template_id
2598 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2599 static bool cp_parser_non_integral_constant_expression
2600 (cp_parser
*, non_integral_constant
);
2601 static void cp_parser_diagnose_invalid_type_name
2602 (cp_parser
*, tree
, location_t
);
2603 static bool cp_parser_parse_and_diagnose_invalid_type_name
2605 static int cp_parser_skip_to_closing_parenthesis
2606 (cp_parser
*, bool, bool, bool);
2607 static void cp_parser_skip_to_end_of_statement
2609 static void cp_parser_consume_semicolon_at_end_of_statement
2611 static void cp_parser_skip_to_end_of_block_or_statement
2613 static bool cp_parser_skip_to_closing_brace
2615 static void cp_parser_skip_to_end_of_template_parameter_list
2617 static void cp_parser_skip_to_pragma_eol
2618 (cp_parser
*, cp_token
*);
2619 static bool cp_parser_error_occurred
2621 static bool cp_parser_allow_gnu_extensions_p
2623 static bool cp_parser_is_pure_string_literal
2625 static bool cp_parser_is_string_literal
2627 static bool cp_parser_is_keyword
2628 (cp_token
*, enum rid
);
2629 static tree cp_parser_make_typename_type
2630 (cp_parser
*, tree
, location_t location
);
2631 static cp_declarator
* cp_parser_make_indirect_declarator
2632 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2633 static bool cp_parser_compound_literal_p
2635 static bool cp_parser_array_designator_p
2637 static bool cp_parser_init_statement_p
2639 static bool cp_parser_skip_to_closing_square_bracket
2642 /* Concept-related syntactic transformations */
2644 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2645 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2647 // -------------------------------------------------------------------------- //
2648 // Unevaluated Operand Guard
2650 // Implementation of an RAII helper for unevaluated operand parsing.
2651 cp_unevaluated::cp_unevaluated ()
2653 ++cp_unevaluated_operand
;
2654 ++c_inhibit_evaluation_warnings
;
2657 cp_unevaluated::~cp_unevaluated ()
2659 --c_inhibit_evaluation_warnings
;
2660 --cp_unevaluated_operand
;
2663 // -------------------------------------------------------------------------- //
2664 // Tentative Parsing
2666 /* Returns nonzero if we are parsing tentatively. */
2669 cp_parser_parsing_tentatively (cp_parser
* parser
)
2671 return parser
->context
->next
!= NULL
;
2674 /* Returns nonzero if TOKEN is a string literal. */
2677 cp_parser_is_pure_string_literal (cp_token
* token
)
2679 return (token
->type
== CPP_STRING
||
2680 token
->type
== CPP_STRING16
||
2681 token
->type
== CPP_STRING32
||
2682 token
->type
== CPP_WSTRING
||
2683 token
->type
== CPP_UTF8STRING
);
2686 /* Returns nonzero if TOKEN is a string literal
2687 of a user-defined string literal. */
2690 cp_parser_is_string_literal (cp_token
* token
)
2692 return (cp_parser_is_pure_string_literal (token
) ||
2693 token
->type
== CPP_STRING_USERDEF
||
2694 token
->type
== CPP_STRING16_USERDEF
||
2695 token
->type
== CPP_STRING32_USERDEF
||
2696 token
->type
== CPP_WSTRING_USERDEF
||
2697 token
->type
== CPP_UTF8STRING_USERDEF
);
2700 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2703 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2705 return token
->keyword
== keyword
;
2708 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2711 static enum pragma_kind
2712 cp_parser_pragma_kind (cp_token
*token
)
2714 if (token
->type
!= CPP_PRAGMA
)
2716 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2717 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2720 /* Helper function for cp_parser_error.
2721 Having peeked a token of kind TOK1_KIND that might signify
2722 a conflict marker, peek successor tokens to determine
2723 if we actually do have a conflict marker.
2724 Specifically, we consider a run of 7 '<', '=' or '>' characters
2725 at the start of a line as a conflict marker.
2726 These come through the lexer as three pairs and a single,
2727 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2728 If it returns true, *OUT_LOC is written to with the location/range
2732 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2733 location_t
*out_loc
)
2735 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2736 if (token2
->type
!= tok1_kind
)
2738 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2739 if (token3
->type
!= tok1_kind
)
2741 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2742 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2745 /* It must be at the start of the line. */
2746 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2747 if (LOCATION_COLUMN (start_loc
) != 1)
2750 /* We have a conflict marker. Construct a location of the form:
2753 with start == caret, finishing at the end of the marker. */
2754 location_t finish_loc
= get_finish (token4
->location
);
2755 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2760 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2764 get_matching_symbol (required_token token_desc
)
2771 case RT_CLOSE_BRACE
:
2773 case RT_CLOSE_PAREN
:
2778 /* Attempt to convert TOKEN_DESC from a required_token to an
2779 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2781 static enum cpp_ttype
2782 get_required_cpp_ttype (required_token token_desc
)
2787 return CPP_SEMICOLON
;
2789 return CPP_OPEN_PAREN
;
2790 case RT_CLOSE_BRACE
:
2791 return CPP_CLOSE_BRACE
;
2793 return CPP_OPEN_BRACE
;
2794 case RT_CLOSE_SQUARE
:
2795 return CPP_CLOSE_SQUARE
;
2796 case RT_OPEN_SQUARE
:
2797 return CPP_OPEN_SQUARE
;
2802 case RT_CLOSE_PAREN
:
2803 return CPP_CLOSE_PAREN
;
2806 /* Use CPP_EOF as a "no completions possible" code. */
2812 /* Subroutine of cp_parser_error and cp_parser_required_error.
2814 Issue a diagnostic of the form
2815 FILE:LINE: MESSAGE before TOKEN
2816 where TOKEN is the next token in the input stream. MESSAGE
2817 (specified by the caller) is usually of the form "expected
2820 This bypasses the check for tentative passing, and potentially
2821 adds material needed by cp_parser_required_error.
2823 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2824 suggesting insertion of the missing token.
2826 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2827 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2831 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2832 required_token missing_token_desc
,
2833 location_t matching_location
)
2835 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2836 /* This diagnostic makes more sense if it is tagged to the line
2837 of the token we just peeked at. */
2838 cp_lexer_set_source_position_from_token (token
);
2840 if (token
->type
== CPP_PRAGMA
)
2842 error_at (token
->location
,
2843 "%<#pragma%> is not allowed here");
2844 cp_parser_skip_to_pragma_eol (parser
, token
);
2848 /* If this is actually a conflict marker, report it as such. */
2849 if (token
->type
== CPP_LSHIFT
2850 || token
->type
== CPP_RSHIFT
2851 || token
->type
== CPP_EQ_EQ
)
2854 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2856 error_at (loc
, "version control conflict marker in file");
2857 expanded_location token_exploc
= expand_location (token
->location
);
2858 /* Consume tokens until the end of the source line. */
2861 cp_lexer_consume_token (parser
->lexer
);
2862 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2865 expanded_location next_exploc
= expand_location (next
->location
);
2866 if (next_exploc
.file
!= token_exploc
.file
)
2868 if (next_exploc
.line
!= token_exploc
.line
)
2875 gcc_rich_location
richloc (input_location
);
2877 bool added_matching_location
= false;
2879 if (missing_token_desc
!= RT_NONE
)
2881 /* Potentially supply a fix-it hint, suggesting to add the
2882 missing token immediately after the *previous* token.
2883 This may move the primary location within richloc. */
2884 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2885 location_t prev_token_loc
2886 = cp_lexer_previous_token (parser
->lexer
)->location
;
2887 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2889 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2890 Attempt to consolidate diagnostics by printing it as a
2891 secondary range within the main diagnostic. */
2892 if (matching_location
!= UNKNOWN_LOCATION
)
2893 added_matching_location
2894 = richloc
.add_location_if_nearby (matching_location
);
2897 /* Actually emit the error. */
2898 c_parse_error (gmsgid
,
2899 /* Because c_parser_error does not understand
2900 CPP_KEYWORD, keywords are treated like
2902 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2903 token
->u
.value
, token
->flags
, &richloc
);
2905 if (missing_token_desc
!= RT_NONE
)
2907 /* If we weren't able to consolidate matching_location, then
2908 print it as a secondary diagnostic. */
2909 if (matching_location
!= UNKNOWN_LOCATION
2910 && !added_matching_location
)
2911 inform (matching_location
, "to match this %qs",
2912 get_matching_symbol (missing_token_desc
));
2916 /* If not parsing tentatively, issue a diagnostic of the form
2917 FILE:LINE: MESSAGE before TOKEN
2918 where TOKEN is the next token in the input stream. MESSAGE
2919 (specified by the caller) is usually of the form "expected
2923 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2925 if (!cp_parser_simulate_error (parser
))
2926 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2929 /* Issue an error about name-lookup failing. NAME is the
2930 IDENTIFIER_NODE DECL is the result of
2931 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2932 the thing that we hoped to find. */
2935 cp_parser_name_lookup_error (cp_parser
* parser
,
2938 name_lookup_error desired
,
2939 location_t location
)
2941 /* If name lookup completely failed, tell the user that NAME was not
2943 if (decl
== error_mark_node
)
2945 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2946 error_at (location
, "%<%E::%E%> has not been declared",
2947 parser
->scope
, name
);
2948 else if (parser
->scope
== global_namespace
)
2949 error_at (location
, "%<::%E%> has not been declared", name
);
2950 else if (parser
->object_scope
2951 && !CLASS_TYPE_P (parser
->object_scope
))
2952 error_at (location
, "request for member %qE in non-class type %qT",
2953 name
, parser
->object_scope
);
2954 else if (parser
->object_scope
)
2955 error_at (location
, "%<%T::%E%> has not been declared",
2956 parser
->object_scope
, name
);
2958 error_at (location
, "%qE has not been declared", name
);
2960 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2965 error_at (location
, "%<%E::%E%> is not a type",
2966 parser
->scope
, name
);
2969 error_at (location
, "%<%E::%E%> is not a class or namespace",
2970 parser
->scope
, name
);
2974 "%<%E::%E%> is not a class, namespace, or enumeration",
2975 parser
->scope
, name
);
2982 else if (parser
->scope
== global_namespace
)
2987 error_at (location
, "%<::%E%> is not a type", name
);
2990 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2994 "%<::%E%> is not a class, namespace, or enumeration",
3006 error_at (location
, "%qE is not a type", name
);
3009 error_at (location
, "%qE is not a class or namespace", name
);
3013 "%qE is not a class, namespace, or enumeration", name
);
3021 /* If we are parsing tentatively, remember that an error has occurred
3022 during this tentative parse. Returns true if the error was
3023 simulated; false if a message should be issued by the caller. */
3026 cp_parser_simulate_error (cp_parser
* parser
)
3028 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3030 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3036 /* This function is called when a type is defined. If type
3037 definitions are forbidden at this point, an error message is
3041 cp_parser_check_type_definition (cp_parser
* parser
)
3043 /* If types are forbidden here, issue a message. */
3044 if (parser
->type_definition_forbidden_message
)
3046 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3047 in the message need to be interpreted. */
3048 error (parser
->type_definition_forbidden_message
);
3054 /* This function is called when the DECLARATOR is processed. The TYPE
3055 was a type defined in the decl-specifiers. If it is invalid to
3056 define a type in the decl-specifiers for DECLARATOR, an error is
3057 issued. TYPE_LOCATION is the location of TYPE and is used
3058 for error reporting. */
3061 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3062 tree type
, location_t type_location
)
3064 /* [dcl.fct] forbids type definitions in return types.
3065 Unfortunately, it's not easy to know whether or not we are
3066 processing a return type until after the fact. */
3068 && (declarator
->kind
== cdk_pointer
3069 || declarator
->kind
== cdk_reference
3070 || declarator
->kind
== cdk_ptrmem
))
3071 declarator
= declarator
->declarator
;
3073 && declarator
->kind
== cdk_function
)
3075 error_at (type_location
,
3076 "new types may not be defined in a return type");
3077 inform (type_location
,
3078 "(perhaps a semicolon is missing after the definition of %qT)",
3083 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3084 "<" in any valid C++ program. If the next token is indeed "<",
3085 issue a message warning the user about what appears to be an
3086 invalid attempt to form a template-id. LOCATION is the location
3087 of the type-specifier (TYPE) */
3090 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3092 enum tag_types tag_type
,
3093 location_t location
)
3095 cp_token_position start
= 0;
3097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3099 if (TREE_CODE (type
) == TYPE_DECL
)
3100 type
= TREE_TYPE (type
);
3101 if (TYPE_P (type
) && !template_placeholder_p (type
))
3102 error_at (location
, "%qT is not a template", type
);
3103 else if (identifier_p (type
))
3105 if (tag_type
!= none_type
)
3106 error_at (location
, "%qE is not a class template", type
);
3108 error_at (location
, "%qE is not a template", type
);
3111 error_at (location
, "invalid template-id");
3112 /* Remember the location of the invalid "<". */
3113 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3114 start
= cp_lexer_token_position (parser
->lexer
, true);
3115 /* Consume the "<". */
3116 cp_lexer_consume_token (parser
->lexer
);
3117 /* Parse the template arguments. */
3118 cp_parser_enclosed_template_argument_list (parser
);
3119 /* Permanently remove the invalid template arguments so that
3120 this error message is not issued again. */
3122 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3126 /* If parsing an integral constant-expression, issue an error message
3127 about the fact that THING appeared and return true. Otherwise,
3128 return false. In either case, set
3129 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3132 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3133 non_integral_constant thing
)
3135 parser
->non_integral_constant_expression_p
= true;
3136 if (parser
->integral_constant_expression_p
)
3138 if (!parser
->allow_non_integral_constant_expression_p
)
3140 const char *msg
= NULL
;
3144 pedwarn (input_location
, OPT_Wpedantic
,
3145 "ISO C++ forbids using a floating-point literal "
3146 "in a constant-expression");
3149 error ("a cast to a type other than an integral or "
3150 "enumeration type cannot appear in a "
3151 "constant-expression");
3154 error ("%<typeid%> operator "
3155 "cannot appear in a constant-expression");
3158 error ("non-constant compound literals "
3159 "cannot appear in a constant-expression");
3162 error ("a function call "
3163 "cannot appear in a constant-expression");
3166 error ("an increment "
3167 "cannot appear in a constant-expression");
3170 error ("an decrement "
3171 "cannot appear in a constant-expression");
3174 error ("an array reference "
3175 "cannot appear in a constant-expression");
3177 case NIC_ADDR_LABEL
:
3178 error ("the address of a label "
3179 "cannot appear in a constant-expression");
3181 case NIC_OVERLOADED
:
3182 error ("calls to overloaded operators "
3183 "cannot appear in a constant-expression");
3185 case NIC_ASSIGNMENT
:
3186 error ("an assignment cannot appear in a constant-expression");
3189 error ("a comma operator "
3190 "cannot appear in a constant-expression");
3192 case NIC_CONSTRUCTOR
:
3193 error ("a call to a constructor "
3194 "cannot appear in a constant-expression");
3196 case NIC_TRANSACTION
:
3197 error ("a transaction expression "
3198 "cannot appear in a constant-expression");
3204 msg
= "__FUNCTION__";
3206 case NIC_PRETTY_FUNC
:
3207 msg
= "__PRETTY_FUNCTION__";
3227 case NIC_PREINCREMENT
:
3230 case NIC_PREDECREMENT
:
3243 error ("%qs cannot appear in a constant-expression", msg
);
3250 /* Emit a diagnostic for an invalid type name. This function commits
3251 to the current active tentative parse, if any. (Otherwise, the
3252 problematic construct might be encountered again later, resulting
3253 in duplicate error messages.) LOCATION is the location of ID. */
3256 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3257 location_t location
)
3259 tree decl
, ambiguous_decls
;
3260 cp_parser_commit_to_tentative_parse (parser
);
3261 /* Try to lookup the identifier. */
3262 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3263 /*is_template=*/false,
3264 /*is_namespace=*/false,
3265 /*check_dependency=*/true,
3266 &ambiguous_decls
, location
);
3267 if (ambiguous_decls
)
3268 /* If the lookup was ambiguous, an error will already have
3271 /* If the lookup found a template-name, it means that the user forgot
3272 to specify an argument list. Emit a useful error message. */
3273 if (DECL_TYPE_TEMPLATE_P (decl
))
3275 auto_diagnostic_group d
;
3277 "invalid use of template-name %qE without an argument list",
3279 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3280 inform (location
, "class template argument deduction is only available "
3281 "with -std=c++17 or -std=gnu++17");
3282 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3284 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3285 error_at (location
, "invalid use of destructor %qD as a type", id
);
3286 else if (TREE_CODE (decl
) == TYPE_DECL
)
3287 /* Something like 'unsigned A a;' */
3288 error_at (location
, "invalid combination of multiple type-specifiers");
3289 else if (!parser
->scope
)
3291 /* Issue an error message. */
3292 auto_diagnostic_group d
;
3294 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3295 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3298 gcc_rich_location
richloc (location
);
3299 richloc
.add_fixit_replace (hint
.suggestion ());
3301 "%qE does not name a type; did you mean %qs?",
3302 id
, hint
.suggestion ());
3305 error_at (location
, "%qE does not name a type", id
);
3306 /* If we're in a template class, it's possible that the user was
3307 referring to a type from a base class. For example:
3309 template <typename T> struct A { typedef T X; };
3310 template <typename T> struct B : public A<T> { X x; };
3312 The user should have said "typename A<T>::X". */
3313 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3314 inform (location
, "C++11 %<constexpr%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3317 inform (location
, "C++11 %<noexcept%> only available with "
3318 "-std=c++11 or -std=gnu++11");
3319 else if (cxx_dialect
< cxx11
3320 && TREE_CODE (id
) == IDENTIFIER_NODE
3321 && id_equal (id
, "thread_local"))
3322 inform (location
, "C++11 %<thread_local%> only available with "
3323 "-std=c++11 or -std=gnu++11");
3324 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3325 inform (location
, "%<concept%> only available with -fconcepts");
3326 else if (processing_template_decl
&& current_class_type
3327 && TYPE_BINFO (current_class_type
))
3331 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3335 tree base_type
= BINFO_TYPE (b
);
3336 if (CLASS_TYPE_P (base_type
)
3337 && dependent_type_p (base_type
))
3340 /* Go from a particular instantiation of the
3341 template (which will have an empty TYPE_FIELDs),
3342 to the main version. */
3343 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3344 for (field
= TYPE_FIELDS (base_type
);
3346 field
= DECL_CHAIN (field
))
3347 if (TREE_CODE (field
) == TYPE_DECL
3348 && DECL_NAME (field
) == id
)
3351 "(perhaps %<typename %T::%E%> was intended)",
3352 BINFO_TYPE (b
), id
);
3361 /* Here we diagnose qualified-ids where the scope is actually correct,
3362 but the identifier does not resolve to a valid type name. */
3363 else if (parser
->scope
!= error_mark_node
)
3365 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3367 auto_diagnostic_group d
;
3368 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3369 error_at (location_of (id
),
3370 "%qE in namespace %qE does not name a template type",
3372 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3373 error_at (location_of (id
),
3374 "%qE in namespace %qE does not name a template type",
3375 TREE_OPERAND (id
, 0), parser
->scope
);
3377 error_at (location_of (id
),
3378 "%qE in namespace %qE does not name a type",
3381 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3382 else if (decl
== error_mark_node
)
3383 suggest_alternative_in_explicit_scope (location
, id
,
3386 else if (CLASS_TYPE_P (parser
->scope
)
3387 && constructor_name_p (id
, parser
->scope
))
3390 auto_diagnostic_group d
;
3391 error_at (location
, "%<%T::%E%> names the constructor, not"
3392 " the type", parser
->scope
, id
);
3393 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3394 error_at (location
, "and %qT has no template constructors",
3397 else if (TYPE_P (parser
->scope
)
3398 && dependent_scope_p (parser
->scope
))
3400 gcc_rich_location
richloc (location
);
3401 richloc
.add_fixit_insert_before ("typename ");
3402 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3404 "need %<typename%> before %<%T::%D::%E%> because "
3405 "%<%T::%D%> is a dependent scope",
3406 TYPE_CONTEXT (parser
->scope
),
3407 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3409 TYPE_CONTEXT (parser
->scope
),
3410 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3412 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3413 "%qT is a dependent scope",
3414 parser
->scope
, id
, parser
->scope
);
3416 else if (TYPE_P (parser
->scope
))
3418 auto_diagnostic_group d
;
3419 if (!COMPLETE_TYPE_P (parser
->scope
))
3420 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3422 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3423 error_at (location_of (id
),
3424 "%qE in %q#T does not name a template type",
3426 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3427 error_at (location_of (id
),
3428 "%qE in %q#T does not name a template type",
3429 TREE_OPERAND (id
, 0), parser
->scope
);
3431 error_at (location_of (id
),
3432 "%qE in %q#T does not name a type",
3435 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3442 /* Check for a common situation where a type-name should be present,
3443 but is not, and issue a sensible error message. Returns true if an
3444 invalid type-name was detected.
3446 The situation handled by this function are variable declarations of the
3447 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3448 Usually, `ID' should name a type, but if we got here it means that it
3449 does not. We try to emit the best possible error message depending on
3450 how exactly the id-expression looks like. */
3453 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3456 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3458 /* Avoid duplicate error about ambiguous lookup. */
3459 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3461 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3462 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3466 cp_parser_parse_tentatively (parser
);
3467 id
= cp_parser_id_expression (parser
,
3468 /*template_keyword_p=*/false,
3469 /*check_dependency_p=*/true,
3470 /*template_p=*/NULL
,
3471 /*declarator_p=*/false,
3472 /*optional_p=*/false);
3473 /* If the next token is a (, this is a function with no explicit return
3474 type, i.e. constructor, destructor or conversion op. */
3475 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3476 || TREE_CODE (id
) == TYPE_DECL
)
3478 cp_parser_abort_tentative_parse (parser
);
3481 if (!cp_parser_parse_definitely (parser
))
3484 /* Emit a diagnostic for the invalid type. */
3485 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3487 /* If we aren't in the middle of a declarator (i.e. in a
3488 parameter-declaration-clause), skip to the end of the declaration;
3489 there's no point in trying to process it. */
3490 if (!parser
->in_declarator_p
)
3491 cp_parser_skip_to_end_of_block_or_statement (parser
);
3495 /* Consume tokens up to, and including, the next non-nested closing `)'.
3496 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3497 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3498 found an unnested token of that type. */
3501 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3506 unsigned paren_depth
= 0;
3507 unsigned brace_depth
= 0;
3508 unsigned square_depth
= 0;
3509 unsigned condop_depth
= 0;
3511 if (recovering
&& or_ttype
== CPP_EOF
3512 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3517 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3519 /* Have we found what we're looking for before the closing paren? */
3520 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3521 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3524 switch (token
->type
)
3527 case CPP_PRAGMA_EOL
:
3528 /* If we've run out of tokens, then there is no closing `)'. */
3531 /* This is good for lambda expression capture-lists. */
3532 case CPP_OPEN_SQUARE
:
3535 case CPP_CLOSE_SQUARE
:
3536 if (!square_depth
--)
3541 /* This matches the processing in skip_to_end_of_statement. */
3546 case CPP_OPEN_BRACE
:
3549 case CPP_CLOSE_BRACE
:
3554 case CPP_OPEN_PAREN
:
3559 case CPP_CLOSE_PAREN
:
3560 if (!brace_depth
&& !paren_depth
--)
3563 cp_lexer_consume_token (parser
->lexer
);
3569 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3574 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3582 /* Consume the token. */
3583 cp_lexer_consume_token (parser
->lexer
);
3587 /* Consume tokens up to, and including, the next non-nested closing `)'.
3588 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3589 are doing error recovery. Returns -1 if OR_COMMA is true and we
3590 found an unnested token of that type. */
3593 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3598 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3599 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3600 ttype
, consume_paren
);
3603 /* Consume tokens until we reach the end of the current statement.
3604 Normally, that will be just before consuming a `;'. However, if a
3605 non-nested `}' comes first, then we stop before consuming that. */
3608 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3610 unsigned nesting_depth
= 0;
3612 /* Unwind generic function template scope if necessary. */
3613 if (parser
->fully_implicit_function_template_p
)
3614 abort_fully_implicit_template (parser
);
3618 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3620 switch (token
->type
)
3623 case CPP_PRAGMA_EOL
:
3624 /* If we've run out of tokens, stop. */
3628 /* If the next token is a `;', we have reached the end of the
3634 case CPP_CLOSE_BRACE
:
3635 /* If this is a non-nested '}', stop before consuming it.
3636 That way, when confronted with something like:
3640 we stop before consuming the closing '}', even though we
3641 have not yet reached a `;'. */
3642 if (nesting_depth
== 0)
3645 /* If it is the closing '}' for a block that we have
3646 scanned, stop -- but only after consuming the token.
3652 we will stop after the body of the erroneously declared
3653 function, but before consuming the following `typedef'
3655 if (--nesting_depth
== 0)
3657 cp_lexer_consume_token (parser
->lexer
);
3662 case CPP_OPEN_BRACE
:
3670 /* Consume the token. */
3671 cp_lexer_consume_token (parser
->lexer
);
3675 /* This function is called at the end of a statement or declaration.
3676 If the next token is a semicolon, it is consumed; otherwise, error
3677 recovery is attempted. */
3680 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3682 /* Look for the trailing `;'. */
3683 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3685 /* If there is additional (erroneous) input, skip to the end of
3687 cp_parser_skip_to_end_of_statement (parser
);
3688 /* If the next token is now a `;', consume it. */
3689 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3690 cp_lexer_consume_token (parser
->lexer
);
3694 /* Skip tokens until we have consumed an entire block, or until we
3695 have consumed a non-nested `;'. */
3698 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3700 int nesting_depth
= 0;
3702 /* Unwind generic function template scope if necessary. */
3703 if (parser
->fully_implicit_function_template_p
)
3704 abort_fully_implicit_template (parser
);
3706 while (nesting_depth
>= 0)
3708 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3710 switch (token
->type
)
3713 case CPP_PRAGMA_EOL
:
3714 /* If we've run out of tokens, stop. */
3718 /* Stop if this is an unnested ';'. */
3723 case CPP_CLOSE_BRACE
:
3724 /* Stop if this is an unnested '}', or closes the outermost
3727 if (nesting_depth
< 0)
3733 case CPP_OPEN_BRACE
:
3742 /* Consume the token. */
3743 cp_lexer_consume_token (parser
->lexer
);
3747 /* Skip tokens until a non-nested closing curly brace is the next
3748 token, or there are no more tokens. Return true in the first case,
3752 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3754 unsigned nesting_depth
= 0;
3758 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3760 switch (token
->type
)
3763 case CPP_PRAGMA_EOL
:
3764 /* If we've run out of tokens, stop. */
3767 case CPP_CLOSE_BRACE
:
3768 /* If the next token is a non-nested `}', then we have reached
3769 the end of the current block. */
3770 if (nesting_depth
-- == 0)
3774 case CPP_OPEN_BRACE
:
3775 /* If it the next token is a `{', then we are entering a new
3776 block. Consume the entire block. */
3784 /* Consume the token. */
3785 cp_lexer_consume_token (parser
->lexer
);
3789 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3790 parameter is the PRAGMA token, allowing us to purge the entire pragma
3794 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3798 parser
->lexer
->in_pragma
= false;
3801 token
= cp_lexer_consume_token (parser
->lexer
);
3802 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3804 /* Ensure that the pragma is not parsed again. */
3805 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3808 /* Require pragma end of line, resyncing with it as necessary. The
3809 arguments are as for cp_parser_skip_to_pragma_eol. */
3812 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3814 parser
->lexer
->in_pragma
= false;
3815 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3816 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3819 /* This is a simple wrapper around make_typename_type. When the id is
3820 an unresolved identifier node, we can provide a superior diagnostic
3821 using cp_parser_diagnose_invalid_type_name. */
3824 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3825 location_t id_location
)
3828 if (identifier_p (id
))
3830 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3831 /*complain=*/tf_none
);
3832 if (result
== error_mark_node
)
3833 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3836 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3839 /* This is a wrapper around the
3840 make_{pointer,ptrmem,reference}_declarator functions that decides
3841 which one to call based on the CODE and CLASS_TYPE arguments. The
3842 CODE argument should be one of the values returned by
3843 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3844 appertain to the pointer or reference. */
3846 static cp_declarator
*
3847 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3848 cp_cv_quals cv_qualifiers
,
3849 cp_declarator
*target
,
3852 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3853 return cp_error_declarator
;
3855 if (code
== INDIRECT_REF
)
3856 if (class_type
== NULL_TREE
)
3857 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3859 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3860 target
, attributes
);
3861 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3862 return make_reference_declarator (cv_qualifiers
, target
,
3864 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3865 return make_reference_declarator (cv_qualifiers
, target
,
3870 /* Create a new C++ parser. */
3873 cp_parser_new (void)
3879 /* cp_lexer_new_main is called before doing GC allocation because
3880 cp_lexer_new_main might load a PCH file. */
3881 lexer
= cp_lexer_new_main ();
3883 /* Initialize the binops_by_token so that we can get the tree
3884 directly from the token. */
3885 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3886 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3888 parser
= ggc_cleared_alloc
<cp_parser
> ();
3889 parser
->lexer
= lexer
;
3890 parser
->context
= cp_parser_context_new (NULL
);
3892 /* For now, we always accept GNU extensions. */
3893 parser
->allow_gnu_extensions_p
= 1;
3895 /* The `>' token is a greater-than operator, not the end of a
3897 parser
->greater_than_is_operator_p
= true;
3899 parser
->default_arg_ok_p
= true;
3901 /* We are not parsing a constant-expression. */
3902 parser
->integral_constant_expression_p
= false;
3903 parser
->allow_non_integral_constant_expression_p
= false;
3904 parser
->non_integral_constant_expression_p
= false;
3906 /* Local variable names are not forbidden. */
3907 parser
->local_variables_forbidden_p
= false;
3909 /* We are not processing an `extern "C"' declaration. */
3910 parser
->in_unbraced_linkage_specification_p
= false;
3912 /* We are not processing a declarator. */
3913 parser
->in_declarator_p
= false;
3915 /* We are not processing a template-argument-list. */
3916 parser
->in_template_argument_list_p
= false;
3918 /* We are not in an iteration statement. */
3919 parser
->in_statement
= 0;
3921 /* We are not in a switch statement. */
3922 parser
->in_switch_statement_p
= false;
3924 /* We are not parsing a type-id inside an expression. */
3925 parser
->in_type_id_in_expr_p
= false;
3927 /* Declarations aren't implicitly extern "C". */
3928 parser
->implicit_extern_c
= false;
3930 /* String literals should be translated to the execution character set. */
3931 parser
->translate_strings_p
= true;
3933 /* We are not parsing a function body. */
3934 parser
->in_function_body
= false;
3936 /* We can correct until told otherwise. */
3937 parser
->colon_corrects_to_scope_p
= true;
3939 /* The unparsed function queue is empty. */
3940 push_unparsed_function_queues (parser
);
3942 /* There are no classes being defined. */
3943 parser
->num_classes_being_defined
= 0;
3945 /* No template parameters apply. */
3946 parser
->num_template_parameter_lists
= 0;
3948 /* Special parsing data structures. */
3949 parser
->omp_declare_simd
= NULL
;
3950 parser
->oacc_routine
= NULL
;
3952 /* Not declaring an implicit function template. */
3953 parser
->auto_is_implicit_function_template_parm_p
= false;
3954 parser
->fully_implicit_function_template_p
= false;
3955 parser
->implicit_template_parms
= 0;
3956 parser
->implicit_template_scope
= 0;
3958 /* Allow constrained-type-specifiers. */
3959 parser
->prevent_constrained_type_specifiers
= 0;
3961 /* We haven't yet seen an 'extern "C"'. */
3962 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3967 /* Create a cp_lexer structure which will emit the tokens in CACHE
3968 and push it onto the parser's lexer stack. This is used for delayed
3969 parsing of in-class method bodies and default arguments, and should
3970 not be confused with tentative parsing. */
3972 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3974 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3975 lexer
->next
= parser
->lexer
;
3976 parser
->lexer
= lexer
;
3978 /* Move the current source position to that of the first token in the
3980 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3983 /* Pop the top lexer off the parser stack. This is never used for the
3984 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3986 cp_parser_pop_lexer (cp_parser
*parser
)
3988 cp_lexer
*lexer
= parser
->lexer
;
3989 parser
->lexer
= lexer
->next
;
3990 cp_lexer_destroy (lexer
);
3992 /* Put the current source position back where it was before this
3993 lexer was pushed. */
3994 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3997 /* Lexical conventions [gram.lex] */
3999 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4003 cp_parser_identifier (cp_parser
* parser
)
4007 /* Look for the identifier. */
4008 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4009 /* Return the value. */
4011 return cp_expr (token
->u
.value
, token
->location
);
4013 return error_mark_node
;
4016 /* Parse a sequence of adjacent string constants. Returns a
4017 TREE_STRING representing the combined, nul-terminated string
4018 constant. If TRANSLATE is true, translate the string to the
4019 execution character set. If WIDE_OK is true, a wide string is
4022 C++98 [lex.string] says that if a narrow string literal token is
4023 adjacent to a wide string literal token, the behavior is undefined.
4024 However, C99 6.4.5p4 says that this results in a wide string literal.
4025 We follow C99 here, for consistency with the C front end.
4027 This code is largely lifted from lex_string() in c-lex.c.
4029 FUTURE: ObjC++ will need to handle @-strings here. */
4031 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4032 bool lookup_udlit
= true)
4036 struct obstack str_ob
;
4037 cpp_string str
, istr
, *strs
;
4039 enum cpp_ttype type
, curr_type
;
4040 int have_suffix_p
= 0;
4042 tree suffix_id
= NULL_TREE
;
4043 bool curr_tok_is_userdef_p
= false;
4045 tok
= cp_lexer_peek_token (parser
->lexer
);
4046 if (!cp_parser_is_string_literal (tok
))
4048 cp_parser_error (parser
, "expected string-literal");
4049 return error_mark_node
;
4052 location_t loc
= tok
->location
;
4054 if (cpp_userdef_string_p (tok
->type
))
4056 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4057 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4058 curr_tok_is_userdef_p
= true;
4062 string_tree
= tok
->u
.value
;
4063 curr_type
= tok
->type
;
4067 /* Try to avoid the overhead of creating and destroying an obstack
4068 for the common case of just one string. */
4069 if (!cp_parser_is_string_literal
4070 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4072 cp_lexer_consume_token (parser
->lexer
);
4074 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4075 str
.len
= TREE_STRING_LENGTH (string_tree
);
4078 if (curr_tok_is_userdef_p
)
4080 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4082 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4085 curr_type
= tok
->type
;
4091 location_t last_tok_loc
= tok
->location
;
4092 gcc_obstack_init (&str_ob
);
4097 cp_lexer_consume_token (parser
->lexer
);
4099 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4100 str
.len
= TREE_STRING_LENGTH (string_tree
);
4102 if (curr_tok_is_userdef_p
)
4104 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4105 if (have_suffix_p
== 0)
4107 suffix_id
= curr_suffix_id
;
4110 else if (have_suffix_p
== 1
4111 && curr_suffix_id
!= suffix_id
)
4113 error ("inconsistent user-defined literal suffixes"
4114 " %qD and %qD in string literal",
4115 suffix_id
, curr_suffix_id
);
4118 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4121 curr_type
= tok
->type
;
4123 if (type
!= curr_type
)
4125 if (type
== CPP_STRING
)
4127 else if (curr_type
!= CPP_STRING
)
4129 rich_location
rich_loc (line_table
, tok
->location
);
4130 rich_loc
.add_range (last_tok_loc
);
4131 error_at (&rich_loc
,
4132 "unsupported non-standard concatenation "
4133 "of string literals");
4137 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4139 last_tok_loc
= tok
->location
;
4141 tok
= cp_lexer_peek_token (parser
->lexer
);
4142 if (cpp_userdef_string_p (tok
->type
))
4144 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4145 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4146 curr_tok_is_userdef_p
= true;
4150 string_tree
= tok
->u
.value
;
4151 curr_type
= tok
->type
;
4152 curr_tok_is_userdef_p
= false;
4155 while (cp_parser_is_string_literal (tok
));
4157 /* A string literal built by concatenation has its caret=start at
4158 the start of the initial string, and its finish at the finish of
4159 the final string literal. */
4160 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4162 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4165 if (type
!= CPP_STRING
&& !wide_ok
)
4167 cp_parser_error (parser
, "a wide string is invalid in this context");
4171 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4172 (parse_in
, strs
, count
, &istr
, type
))
4174 value
= build_string (istr
.len
, (const char *)istr
.text
);
4175 free (CONST_CAST (unsigned char *, istr
.text
));
4181 case CPP_UTF8STRING
:
4182 TREE_TYPE (value
) = char_array_type_node
;
4185 TREE_TYPE (value
) = char16_array_type_node
;
4188 TREE_TYPE (value
) = char32_array_type_node
;
4191 TREE_TYPE (value
) = wchar_array_type_node
;
4195 value
= fix_string_type (value
);
4199 tree literal
= build_userdef_literal (suffix_id
, value
,
4200 OT_NONE
, NULL_TREE
);
4202 value
= cp_parser_userdef_string_literal (literal
);
4208 /* cpp_interpret_string has issued an error. */
4209 value
= error_mark_node
;
4212 obstack_free (&str_ob
, 0);
4214 return cp_expr (value
, loc
);
4217 /* Look up a literal operator with the name and the exact arguments. */
4220 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4223 decl
= lookup_name (name
);
4224 if (!decl
|| !is_overloaded_fn (decl
))
4225 return error_mark_node
;
4227 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4232 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4233 if (parmtypes
!= NULL_TREE
)
4235 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4236 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4238 tree tparm
= TREE_VALUE (parmtypes
);
4239 tree targ
= TREE_TYPE ((*args
)[ix
]);
4240 bool ptr
= TYPE_PTR_P (tparm
);
4241 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4242 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4244 || !same_type_p (TREE_TYPE (tparm
),
4249 && ix
== vec_safe_length (args
)
4250 /* May be this should be sufficient_parms_p instead,
4251 depending on how exactly should user-defined literals
4252 work in presence of default arguments on the literal
4253 operator parameters. */
4254 && parmtypes
== void_list_node
)
4259 return error_mark_node
;
4262 /* Parse a user-defined char constant. Returns a call to a user-defined
4263 literal operator taking the character as an argument. */
4266 cp_parser_userdef_char_literal (cp_parser
*parser
)
4268 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4269 tree literal
= token
->u
.value
;
4270 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4271 tree value
= USERDEF_LITERAL_VALUE (literal
);
4272 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4275 /* Build up a call to the user-defined operator */
4276 /* Lookup the name we got back from the id-expression. */
4277 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4278 vec_safe_push (args
, value
);
4279 decl
= lookup_literal_operator (name
, args
);
4280 if (!decl
|| decl
== error_mark_node
)
4282 error ("unable to find character literal operator %qD with %qT argument",
4283 name
, TREE_TYPE (value
));
4284 release_tree_vector (args
);
4285 return error_mark_node
;
4287 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4288 release_tree_vector (args
);
4292 /* A subroutine of cp_parser_userdef_numeric_literal to
4293 create a char... template parameter pack from a string node. */
4296 make_char_string_pack (tree value
)
4299 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4300 const char *str
= TREE_STRING_POINTER (value
);
4301 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4302 tree argvec
= make_tree_vec (1);
4304 /* Fill in CHARVEC with all of the parameters. */
4305 charvec
= make_tree_vec (len
);
4306 for (i
= 0; i
< len
; ++i
)
4308 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4309 cpp_string in
= { 3, s
};
4310 cpp_string out
= { 0, 0 };
4311 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4313 gcc_assert (out
.len
== 2);
4314 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4318 /* Build the argument packs. */
4319 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4321 TREE_VEC_ELT (argvec
, 0) = argpack
;
4326 /* A subroutine of cp_parser_userdef_numeric_literal to
4327 create a char... template parameter pack from a string node. */
4330 make_string_pack (tree value
)
4333 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4334 const unsigned char *str
4335 = (const unsigned char *) TREE_STRING_POINTER (value
);
4336 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4337 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4338 tree argvec
= make_tree_vec (2);
4340 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4341 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4343 /* First template parm is character type. */
4344 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4346 /* Fill in CHARVEC with all of the parameters. */
4347 charvec
= make_tree_vec (len
);
4348 for (int i
= 0; i
< len
; ++i
)
4349 TREE_VEC_ELT (charvec
, i
)
4350 = double_int_to_tree (str_char_type_node
,
4351 double_int::from_buffer (str
+ i
* sz
, sz
));
4353 /* Build the argument packs. */
4354 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4356 TREE_VEC_ELT (argvec
, 1) = argpack
;
4361 /* Parse a user-defined numeric constant. returns a call to a user-defined
4362 literal operator. */
4365 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4367 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4368 tree literal
= token
->u
.value
;
4369 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4370 tree value
= USERDEF_LITERAL_VALUE (literal
);
4371 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4372 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4373 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4375 vec
<tree
, va_gc
> *args
;
4377 /* Look for a literal operator taking the exact type of numeric argument
4378 as the literal value. */
4379 args
= make_tree_vector ();
4380 vec_safe_push (args
, value
);
4381 decl
= lookup_literal_operator (name
, args
);
4382 if (decl
&& decl
!= error_mark_node
)
4384 result
= finish_call_expr (decl
, &args
, false, true,
4385 tf_warning_or_error
);
4387 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4389 warning_at (token
->location
, OPT_Woverflow
,
4390 "integer literal exceeds range of %qT type",
4391 long_long_unsigned_type_node
);
4396 warning_at (token
->location
, OPT_Woverflow
,
4397 "floating literal exceeds range of %qT type",
4398 long_double_type_node
);
4399 else if (overflow
< 0)
4400 warning_at (token
->location
, OPT_Woverflow
,
4401 "floating literal truncated to zero");
4404 release_tree_vector (args
);
4407 release_tree_vector (args
);
4409 /* If the numeric argument didn't work, look for a raw literal
4410 operator taking a const char* argument consisting of the number
4411 in string format. */
4412 args
= make_tree_vector ();
4413 vec_safe_push (args
, num_string
);
4414 decl
= lookup_literal_operator (name
, args
);
4415 if (decl
&& decl
!= error_mark_node
)
4417 result
= finish_call_expr (decl
, &args
, false, true,
4418 tf_warning_or_error
);
4419 release_tree_vector (args
);
4422 release_tree_vector (args
);
4424 /* If the raw literal didn't work, look for a non-type template
4425 function with parameter pack char.... Call the function with
4426 template parameter characters representing the number. */
4427 args
= make_tree_vector ();
4428 decl
= lookup_literal_operator (name
, args
);
4429 if (decl
&& decl
!= error_mark_node
)
4431 tree tmpl_args
= make_char_string_pack (num_string
);
4432 if (tmpl_args
== NULL_TREE
)
4434 error ("failed to translate literal to execution character set %qT",
4436 return error_mark_node
;
4438 decl
= lookup_template_function (decl
, tmpl_args
);
4439 result
= finish_call_expr (decl
, &args
, false, true,
4440 tf_warning_or_error
);
4441 release_tree_vector (args
);
4445 release_tree_vector (args
);
4447 /* In C++14 the standard library defines complex number suffixes that
4448 conflict with GNU extensions. Prefer them if <complex> is #included. */
4449 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4450 bool i14
= (cxx_dialect
> cxx11
4451 && (id_equal (suffix_id
, "i")
4452 || id_equal (suffix_id
, "if")
4453 || id_equal (suffix_id
, "il")));
4454 diagnostic_t kind
= DK_ERROR
;
4459 tree cxlit
= lookup_qualified_name (std_node
,
4460 get_identifier ("complex_literals"),
4462 if (cxlit
== error_mark_node
)
4464 /* No <complex>, so pedwarn and use GNU semantics. */
4466 opt
= OPT_Wpedantic
;
4471 = emit_diagnostic (kind
, input_location
, opt
,
4472 "unable to find numeric literal operator %qD", name
);
4475 /* Don't inform either. */;
4478 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4479 "(from <complex>) to enable the C++14 user-defined literal "
4482 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4483 "GNU built-in suffix");
4486 inform (token
->location
, "use -fext-numeric-literals "
4487 "to enable more built-in suffixes");
4489 if (kind
== DK_ERROR
)
4490 value
= error_mark_node
;
4493 /* Use the built-in semantics. */
4495 if (id_equal (suffix_id
, "i"))
4497 if (TREE_CODE (value
) == INTEGER_CST
)
4498 type
= integer_type_node
;
4500 type
= double_type_node
;
4502 else if (id_equal (suffix_id
, "if"))
4503 type
= float_type_node
;
4504 else /* if (id_equal (suffix_id, "il")) */
4505 type
= long_double_type_node
;
4507 value
= build_complex (build_complex_type (type
),
4508 fold_convert (type
, integer_zero_node
),
4509 fold_convert (type
, value
));
4512 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4513 /* Avoid repeated diagnostics. */
4514 token
->u
.value
= value
;
4518 /* Parse a user-defined string constant. Returns a call to a user-defined
4519 literal operator taking a character pointer and the length of the string
4523 cp_parser_userdef_string_literal (tree literal
)
4525 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4526 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4527 tree value
= USERDEF_LITERAL_VALUE (literal
);
4528 int len
= TREE_STRING_LENGTH (value
)
4529 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4531 vec
<tree
, va_gc
> *args
;
4533 /* Build up a call to the user-defined operator. */
4534 /* Lookup the name we got back from the id-expression. */
4535 args
= make_tree_vector ();
4536 vec_safe_push (args
, value
);
4537 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4538 decl
= lookup_literal_operator (name
, args
);
4540 if (decl
&& decl
!= error_mark_node
)
4542 result
= finish_call_expr (decl
, &args
, false, true,
4543 tf_warning_or_error
);
4544 release_tree_vector (args
);
4547 release_tree_vector (args
);
4549 /* Look for a template function with typename parameter CharT
4550 and parameter pack CharT... Call the function with
4551 template parameter characters representing the string. */
4552 args
= make_tree_vector ();
4553 decl
= lookup_literal_operator (name
, args
);
4554 if (decl
&& decl
!= error_mark_node
)
4556 tree tmpl_args
= make_string_pack (value
);
4557 decl
= lookup_template_function (decl
, tmpl_args
);
4558 result
= finish_call_expr (decl
, &args
, false, true,
4559 tf_warning_or_error
);
4560 release_tree_vector (args
);
4563 release_tree_vector (args
);
4565 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4566 name
, TREE_TYPE (value
), size_type_node
);
4567 return error_mark_node
;
4571 /* Basic concepts [gram.basic] */
4573 /* Parse a translation-unit.
4576 declaration-seq [opt]
4578 Returns TRUE if all went well. */
4581 cp_parser_translation_unit (cp_parser
* parser
)
4583 /* The address of the first non-permanent object on the declarator
4585 static void *declarator_obstack_base
;
4589 /* Create the declarator obstack, if necessary. */
4590 if (!cp_error_declarator
)
4592 gcc_obstack_init (&declarator_obstack
);
4593 /* Create the error declarator. */
4594 cp_error_declarator
= make_declarator (cdk_error
);
4595 /* Create the empty parameter list. */
4596 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4598 /* Remember where the base of the declarator obstack lies. */
4599 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4602 cp_parser_declaration_seq_opt (parser
);
4604 /* If there are no tokens left then all went well. */
4605 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4607 /* Get rid of the token array; we don't need it any more. */
4608 cp_lexer_destroy (parser
->lexer
);
4609 parser
->lexer
= NULL
;
4611 /* This file might have been a context that's implicitly extern
4612 "C". If so, pop the lang context. (Only relevant for PCH.) */
4613 if (parser
->implicit_extern_c
)
4615 pop_lang_context ();
4616 parser
->implicit_extern_c
= false;
4620 finish_translation_unit ();
4626 cp_parser_error (parser
, "expected declaration");
4630 /* Make sure the declarator obstack was fully cleaned up. */
4631 gcc_assert (obstack_next_free (&declarator_obstack
)
4632 == declarator_obstack_base
);
4634 /* All went well. */
4638 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4639 decltype context. */
4641 static inline tsubst_flags_t
4642 complain_flags (bool decltype_p
)
4644 tsubst_flags_t complain
= tf_warning_or_error
;
4646 complain
|= tf_decltype
;
4650 /* We're about to parse a collection of statements. If we're currently
4651 parsing tentatively, set up a firewall so that any nested
4652 cp_parser_commit_to_tentative_parse won't affect the current context. */
4654 static cp_token_position
4655 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4657 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4660 cp_parser_parse_tentatively (parser
);
4661 cp_parser_commit_to_topmost_tentative_parse (parser
);
4662 return cp_lexer_token_position (parser
->lexer
, false);
4665 /* We've finished parsing the collection of statements. Wrap up the
4666 firewall and replace the relevant tokens with the parsed form. */
4669 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4675 /* Finish the firewall level. */
4676 cp_parser_parse_definitely (parser
);
4677 /* And remember the result of the parse for when we try again. */
4678 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4679 token
->type
= CPP_PREPARSED_EXPR
;
4680 token
->u
.value
= expr
;
4681 token
->keyword
= RID_MAX
;
4682 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4685 /* Like the above functions, but let the user modify the tokens. Used by
4686 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4687 later parses, so it makes sense to localize the effects of
4688 cp_parser_commit_to_tentative_parse. */
4690 struct tentative_firewall
4695 tentative_firewall (cp_parser
*p
): parser(p
)
4697 /* If we're currently parsing tentatively, start a committed level as a
4698 firewall and then an inner tentative parse. */
4699 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4701 cp_parser_parse_tentatively (parser
);
4702 cp_parser_commit_to_topmost_tentative_parse (parser
);
4703 cp_parser_parse_tentatively (parser
);
4707 ~tentative_firewall()
4711 /* Finish the inner tentative parse and the firewall, propagating any
4712 uncommitted error state to the outer tentative parse. */
4713 bool err
= cp_parser_error_occurred (parser
);
4714 cp_parser_parse_definitely (parser
);
4715 cp_parser_parse_definitely (parser
);
4717 cp_parser_simulate_error (parser
);
4722 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4723 This class is for tracking such a matching pair of symbols.
4724 In particular, it tracks the location of the first token,
4725 so that if the second token is missing, we can highlight the
4726 location of the first token when notifying the user about the
4729 template <typename traits_t
>
4733 /* token_pair's ctor. */
4734 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4736 /* If the next token is the opening symbol for this pair, consume it and
4738 Otherwise, issue an error and return false.
4739 In either case, record the location of the opening token. */
4741 bool require_open (cp_parser
*parser
)
4743 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4744 return cp_parser_require (parser
, traits_t::open_token_type
,
4745 traits_t::required_token_open
);
4748 /* Consume the next token from PARSER, recording its location as
4749 that of the opening token within the pair. */
4751 cp_token
* consume_open (cp_parser
*parser
)
4753 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4754 gcc_assert (tok
->type
== traits_t::open_token_type
);
4755 m_open_loc
= tok
->location
;
4759 /* If the next token is the closing symbol for this pair, consume it
4761 Otherwise, issue an error, highlighting the location of the
4762 corresponding opening token, and return NULL. */
4764 cp_token
*require_close (cp_parser
*parser
) const
4766 return cp_parser_require (parser
, traits_t::close_token_type
,
4767 traits_t::required_token_close
,
4772 location_t m_open_loc
;
4775 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4777 struct matching_paren_traits
4779 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4780 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4781 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4782 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4785 /* "matching_parens" is a token_pair<T> class for tracking matching
4786 pairs of parentheses. */
4788 typedef token_pair
<matching_paren_traits
> matching_parens
;
4790 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4792 struct matching_brace_traits
4794 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4795 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4796 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4797 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4800 /* "matching_braces" is a token_pair<T> class for tracking matching
4803 typedef token_pair
<matching_brace_traits
> matching_braces
;
4806 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4807 enclosing parentheses. */
4810 cp_parser_statement_expr (cp_parser
*parser
)
4812 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4814 /* Consume the '('. */
4815 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4816 matching_parens parens
;
4817 parens
.consume_open (parser
);
4818 /* Start the statement-expression. */
4819 tree expr
= begin_stmt_expr ();
4820 /* Parse the compound-statement. */
4821 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4823 expr
= finish_stmt_expr (expr
, false);
4824 /* Consume the ')'. */
4825 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4826 if (!parens
.require_close (parser
))
4827 cp_parser_skip_to_end_of_statement (parser
);
4829 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4830 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4831 return cp_expr (expr
, combined_loc
);
4834 /* Expressions [gram.expr] */
4836 /* Parse a fold-operator.
4839 - * / % ^ & | = < > << >>
4840 = -= *= /= %= ^= &= |= <<= >>=
4841 == != <= >= && || , .* ->*
4843 This returns the tree code corresponding to the matched operator
4844 as an int. When the current token matches a compound assignment
4845 opertor, the resulting tree code is the negative value of the
4846 non-assignment operator. */
4849 cp_parser_fold_operator (cp_token
*token
)
4851 switch (token
->type
)
4853 case CPP_PLUS
: return PLUS_EXPR
;
4854 case CPP_MINUS
: return MINUS_EXPR
;
4855 case CPP_MULT
: return MULT_EXPR
;
4856 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4857 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4858 case CPP_XOR
: return BIT_XOR_EXPR
;
4859 case CPP_AND
: return BIT_AND_EXPR
;
4860 case CPP_OR
: return BIT_IOR_EXPR
;
4861 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4862 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4864 case CPP_EQ
: return -NOP_EXPR
;
4865 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4866 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4867 case CPP_MULT_EQ
: return -MULT_EXPR
;
4868 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4869 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4870 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4871 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4872 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4873 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4874 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4876 case CPP_EQ_EQ
: return EQ_EXPR
;
4877 case CPP_NOT_EQ
: return NE_EXPR
;
4878 case CPP_LESS
: return LT_EXPR
;
4879 case CPP_GREATER
: return GT_EXPR
;
4880 case CPP_LESS_EQ
: return LE_EXPR
;
4881 case CPP_GREATER_EQ
: return GE_EXPR
;
4883 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4884 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4886 case CPP_COMMA
: return COMPOUND_EXPR
;
4888 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4889 case CPP_DEREF_STAR
: return MEMBER_REF
;
4891 default: return ERROR_MARK
;
4895 /* Returns true if CODE indicates a binary expression, which is not allowed in
4896 the LHS of a fold-expression. More codes will need to be added to use this
4897 function in other contexts. */
4900 is_binary_op (tree_code code
)
4905 case POINTER_PLUS_EXPR
:
4908 case TRUNC_DIV_EXPR
:
4909 case TRUNC_MOD_EXPR
:
4925 case TRUTH_ANDIF_EXPR
:
4926 case TRUTH_ORIF_EXPR
:
4939 /* If the next token is a suitable fold operator, consume it and return as
4940 the function above. */
4943 cp_parser_fold_operator (cp_parser
*parser
)
4945 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4946 int code
= cp_parser_fold_operator (token
);
4947 if (code
!= ERROR_MARK
)
4948 cp_lexer_consume_token (parser
->lexer
);
4952 /* Parse a fold-expression.
4955 ( ... folding-operator cast-expression)
4956 ( cast-expression folding-operator ... )
4957 ( cast-expression folding operator ... folding-operator cast-expression)
4959 Note that the '(' and ')' are matched in primary expression. */
4962 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4967 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4969 cp_lexer_consume_token (parser
->lexer
);
4970 int op
= cp_parser_fold_operator (parser
);
4971 if (op
== ERROR_MARK
)
4973 cp_parser_error (parser
, "expected binary operator");
4974 return error_mark_node
;
4977 tree expr
= cp_parser_cast_expression (parser
, false, false,
4979 if (expr
== error_mark_node
)
4980 return error_mark_node
;
4981 return finish_left_unary_fold_expr (expr
, op
);
4984 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4985 int op
= cp_parser_fold_operator (parser
);
4986 if (op
== ERROR_MARK
)
4988 cp_parser_error (parser
, "expected binary operator");
4989 return error_mark_node
;
4992 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4994 cp_parser_error (parser
, "expected ...");
4995 return error_mark_node
;
4997 cp_lexer_consume_token (parser
->lexer
);
4999 /* The operands of a fold-expression are cast-expressions, so binary or
5000 conditional expressions are not allowed. We check this here to avoid
5001 tentative parsing. */
5002 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5003 /* OK, the expression was parenthesized. */;
5004 else if (is_binary_op (TREE_CODE (expr1
)))
5005 error_at (location_of (expr1
),
5006 "binary expression in operand of fold-expression");
5007 else if (TREE_CODE (expr1
) == COND_EXPR
5008 || (REFERENCE_REF_P (expr1
)
5009 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5010 error_at (location_of (expr1
),
5011 "conditional expression in operand of fold-expression");
5014 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5015 return finish_right_unary_fold_expr (expr1
, op
);
5017 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5019 cp_parser_error (parser
, "mismatched operator in fold-expression");
5020 return error_mark_node
;
5022 cp_lexer_consume_token (parser
->lexer
);
5024 // Binary left or right fold.
5025 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5026 if (expr2
== error_mark_node
)
5027 return error_mark_node
;
5028 return finish_binary_fold_expr (expr1
, expr2
, op
);
5031 /* Parse a primary-expression.
5038 lambda-expression (C++11)
5043 ( compound-statement )
5044 __builtin_va_arg ( assignment-expression , type-id )
5045 __builtin_offsetof ( type-id , offsetof-expression )
5048 __has_nothrow_assign ( type-id )
5049 __has_nothrow_constructor ( type-id )
5050 __has_nothrow_copy ( type-id )
5051 __has_trivial_assign ( type-id )
5052 __has_trivial_constructor ( type-id )
5053 __has_trivial_copy ( type-id )
5054 __has_trivial_destructor ( type-id )
5055 __has_virtual_destructor ( type-id )
5056 __is_abstract ( type-id )
5057 __is_base_of ( type-id , type-id )
5058 __is_class ( type-id )
5059 __is_empty ( type-id )
5060 __is_enum ( type-id )
5061 __is_final ( type-id )
5062 __is_literal_type ( type-id )
5063 __is_pod ( type-id )
5064 __is_polymorphic ( type-id )
5065 __is_std_layout ( type-id )
5066 __is_trivial ( type-id )
5067 __is_union ( type-id )
5069 Objective-C++ Extension:
5077 ADDRESS_P is true iff this expression was immediately preceded by
5078 "&" and therefore might denote a pointer-to-member. CAST_P is true
5079 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5080 true iff this expression is a template argument.
5082 Returns a representation of the expression. Upon return, *IDK
5083 indicates what kind of id-expression (if any) was present. */
5086 cp_parser_primary_expression (cp_parser
*parser
,
5089 bool template_arg_p
,
5093 cp_token
*token
= NULL
;
5095 /* Assume the primary expression is not an id-expression. */
5096 *idk
= CP_ID_KIND_NONE
;
5098 /* Peek at the next token. */
5099 token
= cp_lexer_peek_token (parser
->lexer
);
5100 switch ((int) token
->type
)
5109 user-defined-literal */
5116 case CPP_PREPARSED_EXPR
:
5117 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5118 return cp_parser_userdef_numeric_literal (parser
);
5119 token
= cp_lexer_consume_token (parser
->lexer
);
5120 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5122 error_at (token
->location
,
5123 "fixed-point types not supported in C++");
5124 return error_mark_node
;
5126 /* Floating-point literals are only allowed in an integral
5127 constant expression if they are cast to an integral or
5128 enumeration type. */
5129 if (TREE_CODE (token
->u
.value
) == REAL_CST
5130 && parser
->integral_constant_expression_p
5133 /* CAST_P will be set even in invalid code like "int(2.7 +
5134 ...)". Therefore, we have to check that the next token
5135 is sure to end the cast. */
5138 cp_token
*next_token
;
5140 next_token
= cp_lexer_peek_token (parser
->lexer
);
5141 if (/* The comma at the end of an
5142 enumerator-definition. */
5143 next_token
->type
!= CPP_COMMA
5144 /* The curly brace at the end of an enum-specifier. */
5145 && next_token
->type
!= CPP_CLOSE_BRACE
5146 /* The end of a statement. */
5147 && next_token
->type
!= CPP_SEMICOLON
5148 /* The end of the cast-expression. */
5149 && next_token
->type
!= CPP_CLOSE_PAREN
5150 /* The end of an array bound. */
5151 && next_token
->type
!= CPP_CLOSE_SQUARE
5152 /* The closing ">" in a template-argument-list. */
5153 && (next_token
->type
!= CPP_GREATER
5154 || parser
->greater_than_is_operator_p
)
5155 /* C++0x only: A ">>" treated like two ">" tokens,
5156 in a template-argument-list. */
5157 && (next_token
->type
!= CPP_RSHIFT
5158 || (cxx_dialect
== cxx98
)
5159 || parser
->greater_than_is_operator_p
))
5163 /* If we are within a cast, then the constraint that the
5164 cast is to an integral or enumeration type will be
5165 checked at that point. If we are not within a cast, then
5166 this code is invalid. */
5168 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5170 return cp_expr (token
->u
.value
, token
->location
);
5172 case CPP_CHAR_USERDEF
:
5173 case CPP_CHAR16_USERDEF
:
5174 case CPP_CHAR32_USERDEF
:
5175 case CPP_WCHAR_USERDEF
:
5176 case CPP_UTF8CHAR_USERDEF
:
5177 return cp_parser_userdef_char_literal (parser
);
5183 case CPP_UTF8STRING
:
5184 case CPP_STRING_USERDEF
:
5185 case CPP_STRING16_USERDEF
:
5186 case CPP_STRING32_USERDEF
:
5187 case CPP_WSTRING_USERDEF
:
5188 case CPP_UTF8STRING_USERDEF
:
5189 /* ??? Should wide strings be allowed when parser->translate_strings_p
5190 is false (i.e. in attributes)? If not, we can kill the third
5191 argument to cp_parser_string_literal. */
5192 return cp_parser_string_literal (parser
,
5193 parser
->translate_strings_p
,
5196 case CPP_OPEN_PAREN
:
5197 /* If we see `( { ' then we are looking at the beginning of
5198 a GNU statement-expression. */
5199 if (cp_parser_allow_gnu_extensions_p (parser
)
5200 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5202 /* Statement-expressions are not allowed by the standard. */
5203 pedwarn (token
->location
, OPT_Wpedantic
,
5204 "ISO C++ forbids braced-groups within expressions");
5206 /* And they're not allowed outside of a function-body; you
5207 cannot, for example, write:
5209 int i = ({ int j = 3; j + 1; });
5211 at class or namespace scope. */
5212 if (!parser
->in_function_body
5213 || parser
->in_template_argument_list_p
)
5215 error_at (token
->location
,
5216 "statement-expressions are not allowed outside "
5217 "functions nor in template-argument lists");
5218 cp_parser_skip_to_end_of_block_or_statement (parser
);
5219 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5220 cp_lexer_consume_token (parser
->lexer
);
5221 return error_mark_node
;
5224 return cp_parser_statement_expr (parser
);
5226 /* Otherwise it's a normal parenthesized expression. */
5229 bool saved_greater_than_is_operator_p
;
5231 location_t open_paren_loc
= token
->location
;
5233 /* Consume the `('. */
5234 matching_parens parens
;
5235 parens
.consume_open (parser
);
5236 /* Within a parenthesized expression, a `>' token is always
5237 the greater-than operator. */
5238 saved_greater_than_is_operator_p
5239 = parser
->greater_than_is_operator_p
;
5240 parser
->greater_than_is_operator_p
= true;
5242 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5243 /* Left fold expression. */
5246 /* Parse the parenthesized expression. */
5247 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5249 token
= cp_lexer_peek_token (parser
->lexer
);
5250 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5252 expr
= cp_parser_fold_expression (parser
, expr
);
5253 if (expr
!= error_mark_node
5254 && cxx_dialect
< cxx17
5255 && !in_system_header_at (input_location
))
5256 pedwarn (input_location
, 0, "fold-expressions only available "
5257 "with -std=c++17 or -std=gnu++17");
5260 /* Let the front end know that this expression was
5261 enclosed in parentheses. This matters in case, for
5262 example, the expression is of the form `A::B', since
5263 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5265 expr
= finish_parenthesized_expr (expr
);
5267 /* DR 705: Wrapping an unqualified name in parentheses
5268 suppresses arg-dependent lookup. We want to pass back
5269 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5270 (c++/37862), but none of the others. */
5271 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5272 *idk
= CP_ID_KIND_NONE
;
5274 /* The `>' token might be the end of a template-id or
5275 template-parameter-list now. */
5276 parser
->greater_than_is_operator_p
5277 = saved_greater_than_is_operator_p
;
5279 /* Consume the `)'. */
5280 token
= cp_lexer_peek_token (parser
->lexer
);
5281 location_t close_paren_loc
= token
->location
;
5282 expr
.set_range (open_paren_loc
, close_paren_loc
);
5283 if (!parens
.require_close (parser
)
5284 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5285 cp_parser_skip_to_end_of_statement (parser
);
5290 case CPP_OPEN_SQUARE
:
5292 if (c_dialect_objc ())
5294 /* We might have an Objective-C++ message. */
5295 cp_parser_parse_tentatively (parser
);
5296 tree msg
= cp_parser_objc_message_expression (parser
);
5297 /* If that works out, we're done ... */
5298 if (cp_parser_parse_definitely (parser
))
5300 /* ... else, fall though to see if it's a lambda. */
5302 cp_expr lam
= cp_parser_lambda_expression (parser
);
5303 /* Don't warn about a failed tentative parse. */
5304 if (cp_parser_error_occurred (parser
))
5305 return error_mark_node
;
5306 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5310 case CPP_OBJC_STRING
:
5311 if (c_dialect_objc ())
5312 /* We have an Objective-C++ string literal. */
5313 return cp_parser_objc_expression (parser
);
5314 cp_parser_error (parser
, "expected primary-expression");
5315 return error_mark_node
;
5318 switch (token
->keyword
)
5320 /* These two are the boolean literals. */
5322 cp_lexer_consume_token (parser
->lexer
);
5323 return cp_expr (boolean_true_node
, token
->location
);
5325 cp_lexer_consume_token (parser
->lexer
);
5326 return cp_expr (boolean_false_node
, token
->location
);
5328 /* The `__null' literal. */
5330 cp_lexer_consume_token (parser
->lexer
);
5331 return cp_expr (null_node
, token
->location
);
5333 /* The `nullptr' literal. */
5335 cp_lexer_consume_token (parser
->lexer
);
5336 return cp_expr (nullptr_node
, token
->location
);
5338 /* Recognize the `this' keyword. */
5340 cp_lexer_consume_token (parser
->lexer
);
5341 if (parser
->local_variables_forbidden_p
)
5343 error_at (token
->location
,
5344 "%<this%> may not be used in this context");
5345 return error_mark_node
;
5347 /* Pointers cannot appear in constant-expressions. */
5348 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5349 return error_mark_node
;
5350 return cp_expr (finish_this_expr (), token
->location
);
5352 /* The `operator' keyword can be the beginning of an
5357 case RID_FUNCTION_NAME
:
5358 case RID_PRETTY_FUNCTION_NAME
:
5359 case RID_C99_FUNCTION_NAME
:
5361 non_integral_constant name
;
5363 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5364 __func__ are the names of variables -- but they are
5365 treated specially. Therefore, they are handled here,
5366 rather than relying on the generic id-expression logic
5367 below. Grammatically, these names are id-expressions.
5369 Consume the token. */
5370 token
= cp_lexer_consume_token (parser
->lexer
);
5372 switch (token
->keyword
)
5374 case RID_FUNCTION_NAME
:
5375 name
= NIC_FUNC_NAME
;
5377 case RID_PRETTY_FUNCTION_NAME
:
5378 name
= NIC_PRETTY_FUNC
;
5380 case RID_C99_FUNCTION_NAME
:
5381 name
= NIC_C99_FUNC
;
5387 if (cp_parser_non_integral_constant_expression (parser
, name
))
5388 return error_mark_node
;
5390 /* Look up the name. */
5391 return finish_fname (token
->u
.value
);
5398 source_location type_location
;
5399 location_t start_loc
5400 = cp_lexer_peek_token (parser
->lexer
)->location
;
5401 /* The `__builtin_va_arg' construct is used to handle
5402 `va_arg'. Consume the `__builtin_va_arg' token. */
5403 cp_lexer_consume_token (parser
->lexer
);
5404 /* Look for the opening `('. */
5405 matching_parens parens
;
5406 parens
.require_open (parser
);
5407 /* Now, parse the assignment-expression. */
5408 expression
= cp_parser_assignment_expression (parser
);
5409 /* Look for the `,'. */
5410 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5411 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5412 /* Parse the type-id. */
5414 type_id_in_expr_sentinel
s (parser
);
5415 type
= cp_parser_type_id (parser
);
5417 /* Look for the closing `)'. */
5418 location_t finish_loc
5419 = cp_lexer_peek_token (parser
->lexer
)->location
;
5420 parens
.require_close (parser
);
5421 /* Using `va_arg' in a constant-expression is not
5423 if (cp_parser_non_integral_constant_expression (parser
,
5425 return error_mark_node
;
5426 /* Construct a location of the form:
5427 __builtin_va_arg (v, int)
5428 ~~~~~~~~~~~~~~~~~~~~~^~~~
5429 with the caret at the type, ranging from the start of the
5430 "__builtin_va_arg" token to the close paren. */
5431 location_t combined_loc
5432 = make_location (type_location
, start_loc
, finish_loc
);
5433 return build_x_va_arg (combined_loc
, expression
, type
);
5437 return cp_parser_builtin_offsetof (parser
);
5439 case RID_HAS_NOTHROW_ASSIGN
:
5440 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5441 case RID_HAS_NOTHROW_COPY
:
5442 case RID_HAS_TRIVIAL_ASSIGN
:
5443 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5444 case RID_HAS_TRIVIAL_COPY
:
5445 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5446 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5447 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5448 case RID_IS_ABSTRACT
:
5449 case RID_IS_AGGREGATE
:
5450 case RID_IS_BASE_OF
:
5455 case RID_IS_LITERAL_TYPE
:
5457 case RID_IS_POLYMORPHIC
:
5458 case RID_IS_SAME_AS
:
5459 case RID_IS_STD_LAYOUT
:
5460 case RID_IS_TRIVIAL
:
5461 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5462 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5463 case RID_IS_TRIVIALLY_COPYABLE
:
5465 case RID_IS_ASSIGNABLE
:
5466 case RID_IS_CONSTRUCTIBLE
:
5467 return cp_parser_trait_expr (parser
, token
->keyword
);
5471 return cp_parser_requires_expression (parser
);
5473 /* Objective-C++ expressions. */
5475 case RID_AT_PROTOCOL
:
5476 case RID_AT_SELECTOR
:
5477 return cp_parser_objc_expression (parser
);
5480 if (parser
->in_function_body
5481 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5484 error_at (token
->location
,
5485 "a template declaration cannot appear at block scope");
5486 cp_parser_skip_to_end_of_block_or_statement (parser
);
5487 return error_mark_node
;
5491 cp_parser_error (parser
, "expected primary-expression");
5492 return error_mark_node
;
5495 /* An id-expression can start with either an identifier, a
5496 `::' as the beginning of a qualified-id, or the "operator"
5500 case CPP_TEMPLATE_ID
:
5501 case CPP_NESTED_NAME_SPECIFIER
:
5504 cp_expr id_expression
;
5506 const char *error_msg
;
5509 cp_token
*id_expr_token
;
5511 /* Parse the id-expression. */
5513 = cp_parser_id_expression (parser
,
5514 /*template_keyword_p=*/false,
5515 /*check_dependency_p=*/true,
5517 /*declarator_p=*/false,
5518 /*optional_p=*/false);
5519 if (id_expression
== error_mark_node
)
5520 return error_mark_node
;
5521 id_expr_token
= token
;
5522 token
= cp_lexer_peek_token (parser
->lexer
);
5523 done
= (token
->type
!= CPP_OPEN_SQUARE
5524 && token
->type
!= CPP_OPEN_PAREN
5525 && token
->type
!= CPP_DOT
5526 && token
->type
!= CPP_DEREF
5527 && token
->type
!= CPP_PLUS_PLUS
5528 && token
->type
!= CPP_MINUS_MINUS
);
5529 /* If we have a template-id, then no further lookup is
5530 required. If the template-id was for a template-class, we
5531 will sometimes have a TYPE_DECL at this point. */
5532 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5533 || TREE_CODE (id_expression
) == TYPE_DECL
)
5534 decl
= id_expression
;
5535 /* Look up the name. */
5538 tree ambiguous_decls
;
5540 /* If we already know that this lookup is ambiguous, then
5541 we've already issued an error message; there's no reason
5543 if (id_expr_token
->type
== CPP_NAME
5544 && id_expr_token
->error_reported
)
5546 cp_parser_simulate_error (parser
);
5547 return error_mark_node
;
5550 decl
= cp_parser_lookup_name (parser
, id_expression
,
5553 /*is_namespace=*/false,
5554 /*check_dependency=*/true,
5556 id_expr_token
->location
);
5557 /* If the lookup was ambiguous, an error will already have
5559 if (ambiguous_decls
)
5560 return error_mark_node
;
5562 /* In Objective-C++, we may have an Objective-C 2.0
5563 dot-syntax for classes here. */
5564 if (c_dialect_objc ()
5565 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5566 && TREE_CODE (decl
) == TYPE_DECL
5567 && objc_is_class_name (decl
))
5570 cp_lexer_consume_token (parser
->lexer
);
5571 component
= cp_parser_identifier (parser
);
5572 if (component
== error_mark_node
)
5573 return error_mark_node
;
5575 tree result
= objc_build_class_component_ref (id_expression
,
5577 /* Build a location of the form:
5580 with caret at the start of the component name (at
5581 input_location), ranging from the start of the id_expression
5582 to the end of the component name. */
5583 location_t combined_loc
5584 = make_location (input_location
, id_expression
.get_start (),
5585 get_finish (input_location
));
5586 protected_set_expr_location (result
, combined_loc
);
5590 /* In Objective-C++, an instance variable (ivar) may be preferred
5591 to whatever cp_parser_lookup_name() found.
5592 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5593 rest of c-family, we have to do a little extra work to preserve
5594 any location information in cp_expr "decl". Given that
5595 objc_lookup_ivar is implemented in "c-family" and "objc", we
5596 have a trip through the pure "tree" type, rather than cp_expr.
5597 Naively copying it back to "decl" would implicitly give the
5598 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5599 store an EXPR_LOCATION. Hence we only update "decl" (and
5600 hence its location_t) if we get back a different tree node. */
5601 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5603 if (decl_tree
!= decl
.get_value ())
5604 decl
= cp_expr (decl_tree
);
5606 /* If name lookup gives us a SCOPE_REF, then the
5607 qualifying scope was dependent. */
5608 if (TREE_CODE (decl
) == SCOPE_REF
)
5610 /* At this point, we do not know if DECL is a valid
5611 integral constant expression. We assume that it is
5612 in fact such an expression, so that code like:
5614 template <int N> struct A {
5618 is accepted. At template-instantiation time, we
5619 will check that B<N>::i is actually a constant. */
5622 /* Check to see if DECL is a local variable in a context
5623 where that is forbidden. */
5624 if (parser
->local_variables_forbidden_p
5625 && local_variable_p (decl
))
5627 error_at (id_expr_token
->location
,
5628 "local variable %qD may not appear in this context",
5630 return error_mark_node
;
5634 if (processing_template_decl
)
5635 if (tree fns
= maybe_get_fns (decl
))
5636 /* It's too difficult to mark ths in all the places where
5637 we know for sure we need to keep the lookup, so do it
5638 now. The cost is extra GC to recycle the lookups
5639 resolved at parse time. */
5642 decl
= (finish_id_expression
5643 (id_expression
, decl
, parser
->scope
,
5645 parser
->integral_constant_expression_p
,
5646 parser
->allow_non_integral_constant_expression_p
,
5647 &parser
->non_integral_constant_expression_p
,
5648 template_p
, done
, address_p
,
5651 id_expression
.get_location ()));
5653 cp_parser_error (parser
, error_msg
);
5654 decl
.set_location (id_expr_token
->location
);
5658 /* Anything else is an error. */
5660 cp_parser_error (parser
, "expected primary-expression");
5661 return error_mark_node
;
5665 static inline cp_expr
5666 cp_parser_primary_expression (cp_parser
*parser
,
5669 bool template_arg_p
,
5672 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5673 /*decltype*/false, idk
);
5676 /* Parse an id-expression.
5683 :: [opt] nested-name-specifier template [opt] unqualified-id
5685 :: operator-function-id
5688 Return a representation of the unqualified portion of the
5689 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5690 a `::' or nested-name-specifier.
5692 Often, if the id-expression was a qualified-id, the caller will
5693 want to make a SCOPE_REF to represent the qualified-id. This
5694 function does not do this in order to avoid wastefully creating
5695 SCOPE_REFs when they are not required.
5697 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5700 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5701 uninstantiated templates.
5703 If *TEMPLATE_P is non-NULL, it is set to true iff the
5704 `template' keyword is used to explicitly indicate that the entity
5705 named is a template.
5707 If DECLARATOR_P is true, the id-expression is appearing as part of
5708 a declarator, rather than as part of an expression. */
5711 cp_parser_id_expression (cp_parser
*parser
,
5712 bool template_keyword_p
,
5713 bool check_dependency_p
,
5718 bool global_scope_p
;
5719 bool nested_name_specifier_p
;
5721 /* Assume the `template' keyword was not used. */
5723 *template_p
= template_keyword_p
;
5725 /* Look for the optional `::' operator. */
5727 = (!template_keyword_p
5728 && (cp_parser_global_scope_opt (parser
,
5729 /*current_scope_valid_p=*/false)
5732 /* Look for the optional nested-name-specifier. */
5733 nested_name_specifier_p
5734 = (cp_parser_nested_name_specifier_opt (parser
,
5735 /*typename_keyword_p=*/false,
5742 /* If there is a nested-name-specifier, then we are looking at
5743 the first qualified-id production. */
5744 if (nested_name_specifier_p
)
5747 tree saved_object_scope
;
5748 tree saved_qualifying_scope
;
5749 cp_expr unqualified_id
;
5752 /* See if the next token is the `template' keyword. */
5754 template_p
= &is_template
;
5755 *template_p
= cp_parser_optional_template_keyword (parser
);
5756 /* Name lookup we do during the processing of the
5757 unqualified-id might obliterate SCOPE. */
5758 saved_scope
= parser
->scope
;
5759 saved_object_scope
= parser
->object_scope
;
5760 saved_qualifying_scope
= parser
->qualifying_scope
;
5761 /* Process the final unqualified-id. */
5762 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5765 /*optional_p=*/false);
5766 /* Restore the SAVED_SCOPE for our caller. */
5767 parser
->scope
= saved_scope
;
5768 parser
->object_scope
= saved_object_scope
;
5769 parser
->qualifying_scope
= saved_qualifying_scope
;
5771 return unqualified_id
;
5773 /* Otherwise, if we are in global scope, then we are looking at one
5774 of the other qualified-id productions. */
5775 else if (global_scope_p
)
5780 /* Peek at the next token. */
5781 token
= cp_lexer_peek_token (parser
->lexer
);
5783 /* If it's an identifier, and the next token is not a "<", then
5784 we can avoid the template-id case. This is an optimization
5785 for this common case. */
5786 if (token
->type
== CPP_NAME
5787 && !cp_parser_nth_token_starts_template_argument_list_p
5789 return cp_parser_identifier (parser
);
5791 cp_parser_parse_tentatively (parser
);
5792 /* Try a template-id. */
5793 id
= cp_parser_template_id (parser
,
5794 /*template_keyword_p=*/false,
5795 /*check_dependency_p=*/true,
5798 /* If that worked, we're done. */
5799 if (cp_parser_parse_definitely (parser
))
5802 /* Peek at the next token. (Changes in the token buffer may
5803 have invalidated the pointer obtained above.) */
5804 token
= cp_lexer_peek_token (parser
->lexer
);
5806 switch (token
->type
)
5809 return cp_parser_identifier (parser
);
5812 if (token
->keyword
== RID_OPERATOR
)
5813 return cp_parser_operator_function_id (parser
);
5817 cp_parser_error (parser
, "expected id-expression");
5818 return error_mark_node
;
5822 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5823 /*check_dependency_p=*/true,
5828 /* Parse an unqualified-id.
5832 operator-function-id
5833 conversion-function-id
5837 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5838 keyword, in a construct like `A::template ...'.
5840 Returns a representation of unqualified-id. For the `identifier'
5841 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5842 production a BIT_NOT_EXPR is returned; the operand of the
5843 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5844 other productions, see the documentation accompanying the
5845 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5846 names are looked up in uninstantiated templates. If DECLARATOR_P
5847 is true, the unqualified-id is appearing as part of a declarator,
5848 rather than as part of an expression. */
5851 cp_parser_unqualified_id (cp_parser
* parser
,
5852 bool template_keyword_p
,
5853 bool check_dependency_p
,
5859 /* Peek at the next token. */
5860 token
= cp_lexer_peek_token (parser
->lexer
);
5862 switch ((int) token
->type
)
5868 /* We don't know yet whether or not this will be a
5870 cp_parser_parse_tentatively (parser
);
5871 /* Try a template-id. */
5872 id
= cp_parser_template_id (parser
, template_keyword_p
,
5876 /* If it worked, we're done. */
5877 if (cp_parser_parse_definitely (parser
))
5879 /* Otherwise, it's an ordinary identifier. */
5880 return cp_parser_identifier (parser
);
5883 case CPP_TEMPLATE_ID
:
5884 return cp_parser_template_id (parser
, template_keyword_p
,
5892 tree qualifying_scope
;
5897 /* Consume the `~' token. */
5898 cp_lexer_consume_token (parser
->lexer
);
5899 /* Parse the class-name. The standard, as written, seems to
5902 template <typename T> struct S { ~S (); };
5903 template <typename T> S<T>::~S() {}
5905 is invalid, since `~' must be followed by a class-name, but
5906 `S<T>' is dependent, and so not known to be a class.
5907 That's not right; we need to look in uninstantiated
5908 templates. A further complication arises from:
5910 template <typename T> void f(T t) {
5914 Here, it is not possible to look up `T' in the scope of `T'
5915 itself. We must look in both the current scope, and the
5916 scope of the containing complete expression.
5918 Yet another issue is:
5927 The standard does not seem to say that the `S' in `~S'
5928 should refer to the type `S' and not the data member
5931 /* DR 244 says that we look up the name after the "~" in the
5932 same scope as we looked up the qualifying name. That idea
5933 isn't fully worked out; it's more complicated than that. */
5934 scope
= parser
->scope
;
5935 object_scope
= parser
->object_scope
;
5936 qualifying_scope
= parser
->qualifying_scope
;
5938 /* Check for invalid scopes. */
5939 if (scope
== error_mark_node
)
5941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5942 cp_lexer_consume_token (parser
->lexer
);
5943 return error_mark_node
;
5945 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5947 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5948 error_at (token
->location
,
5949 "scope %qT before %<~%> is not a class-name",
5951 cp_parser_simulate_error (parser
);
5952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5953 cp_lexer_consume_token (parser
->lexer
);
5954 return error_mark_node
;
5956 gcc_assert (!scope
|| TYPE_P (scope
));
5958 /* If the name is of the form "X::~X" it's OK even if X is a
5960 token
= cp_lexer_peek_token (parser
->lexer
);
5962 && token
->type
== CPP_NAME
5963 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5965 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5966 || (CLASS_TYPE_P (scope
)
5967 && constructor_name_p (token
->u
.value
, scope
))))
5969 cp_lexer_consume_token (parser
->lexer
);
5970 return build_nt (BIT_NOT_EXPR
, scope
);
5973 /* ~auto means the destructor of whatever the object is. */
5974 if (cp_parser_is_keyword (token
, RID_AUTO
))
5976 if (cxx_dialect
< cxx14
)
5977 pedwarn (input_location
, 0,
5978 "%<~auto%> only available with "
5979 "-std=c++14 or -std=gnu++14");
5980 cp_lexer_consume_token (parser
->lexer
);
5981 return build_nt (BIT_NOT_EXPR
, make_auto ());
5984 /* If there was an explicit qualification (S::~T), first look
5985 in the scope given by the qualification (i.e., S).
5987 Note: in the calls to cp_parser_class_name below we pass
5988 typename_type so that lookup finds the injected-class-name
5989 rather than the constructor. */
5991 type_decl
= NULL_TREE
;
5994 cp_parser_parse_tentatively (parser
);
5995 type_decl
= cp_parser_class_name (parser
,
5996 /*typename_keyword_p=*/false,
5997 /*template_keyword_p=*/false,
5999 /*check_dependency=*/false,
6000 /*class_head_p=*/false,
6002 if (cp_parser_parse_definitely (parser
))
6005 /* In "N::S::~S", look in "N" as well. */
6006 if (!done
&& scope
&& qualifying_scope
)
6008 cp_parser_parse_tentatively (parser
);
6009 parser
->scope
= qualifying_scope
;
6010 parser
->object_scope
= NULL_TREE
;
6011 parser
->qualifying_scope
= NULL_TREE
;
6013 = cp_parser_class_name (parser
,
6014 /*typename_keyword_p=*/false,
6015 /*template_keyword_p=*/false,
6017 /*check_dependency=*/false,
6018 /*class_head_p=*/false,
6020 if (cp_parser_parse_definitely (parser
))
6023 /* In "p->S::~T", look in the scope given by "*p" as well. */
6024 else if (!done
&& object_scope
)
6026 cp_parser_parse_tentatively (parser
);
6027 parser
->scope
= object_scope
;
6028 parser
->object_scope
= NULL_TREE
;
6029 parser
->qualifying_scope
= NULL_TREE
;
6031 = cp_parser_class_name (parser
,
6032 /*typename_keyword_p=*/false,
6033 /*template_keyword_p=*/false,
6035 /*check_dependency=*/false,
6036 /*class_head_p=*/false,
6038 if (cp_parser_parse_definitely (parser
))
6041 /* Look in the surrounding context. */
6044 parser
->scope
= NULL_TREE
;
6045 parser
->object_scope
= NULL_TREE
;
6046 parser
->qualifying_scope
= NULL_TREE
;
6047 if (processing_template_decl
)
6048 cp_parser_parse_tentatively (parser
);
6050 = cp_parser_class_name (parser
,
6051 /*typename_keyword_p=*/false,
6052 /*template_keyword_p=*/false,
6054 /*check_dependency=*/false,
6055 /*class_head_p=*/false,
6057 if (processing_template_decl
6058 && ! cp_parser_parse_definitely (parser
))
6060 /* We couldn't find a type with this name. If we're parsing
6061 tentatively, fail and try something else. */
6062 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6064 cp_parser_simulate_error (parser
);
6065 return error_mark_node
;
6067 /* Otherwise, accept it and check for a match at instantiation
6069 type_decl
= cp_parser_identifier (parser
);
6070 if (type_decl
!= error_mark_node
)
6071 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6075 /* If an error occurred, assume that the name of the
6076 destructor is the same as the name of the qualifying
6077 class. That allows us to keep parsing after running
6078 into ill-formed destructor names. */
6079 if (type_decl
== error_mark_node
&& scope
)
6080 return build_nt (BIT_NOT_EXPR
, scope
);
6081 else if (type_decl
== error_mark_node
)
6082 return error_mark_node
;
6084 /* Check that destructor name and scope match. */
6085 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6087 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6088 error_at (token
->location
,
6089 "declaration of %<~%T%> as member of %qT",
6091 cp_parser_simulate_error (parser
);
6092 return error_mark_node
;
6097 A typedef-name that names a class shall not be used as the
6098 identifier in the declarator for a destructor declaration. */
6100 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6101 && !DECL_SELF_REFERENCE_P (type_decl
)
6102 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6103 error_at (token
->location
,
6104 "typedef-name %qD used as destructor declarator",
6107 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6111 if (token
->keyword
== RID_OPERATOR
)
6115 /* This could be a template-id, so we try that first. */
6116 cp_parser_parse_tentatively (parser
);
6117 /* Try a template-id. */
6118 id
= cp_parser_template_id (parser
, template_keyword_p
,
6119 /*check_dependency_p=*/true,
6122 /* If that worked, we're done. */
6123 if (cp_parser_parse_definitely (parser
))
6125 /* We still don't know whether we're looking at an
6126 operator-function-id or a conversion-function-id. */
6127 cp_parser_parse_tentatively (parser
);
6128 /* Try an operator-function-id. */
6129 id
= cp_parser_operator_function_id (parser
);
6130 /* If that didn't work, try a conversion-function-id. */
6131 if (!cp_parser_parse_definitely (parser
))
6132 id
= cp_parser_conversion_function_id (parser
);
6141 cp_parser_error (parser
, "expected unqualified-id");
6142 return error_mark_node
;
6146 /* Parse an (optional) nested-name-specifier.
6148 nested-name-specifier: [C++98]
6149 class-or-namespace-name :: nested-name-specifier [opt]
6150 class-or-namespace-name :: template nested-name-specifier [opt]
6152 nested-name-specifier: [C++0x]
6155 nested-name-specifier identifier ::
6156 nested-name-specifier template [opt] simple-template-id ::
6158 PARSER->SCOPE should be set appropriately before this function is
6159 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6160 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6163 Sets PARSER->SCOPE to the class (TYPE) or namespace
6164 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6165 it unchanged if there is no nested-name-specifier. Returns the new
6166 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6168 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6169 part of a declaration and/or decl-specifier. */
6172 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6173 bool typename_keyword_p
,
6174 bool check_dependency_p
,
6176 bool is_declaration
,
6177 bool template_keyword_p
/* = false */)
6179 bool success
= false;
6180 cp_token_position start
= 0;
6183 /* Remember where the nested-name-specifier starts. */
6184 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6186 start
= cp_lexer_token_position (parser
->lexer
, false);
6187 push_deferring_access_checks (dk_deferred
);
6194 tree saved_qualifying_scope
;
6196 /* Spot cases that cannot be the beginning of a
6197 nested-name-specifier. */
6198 token
= cp_lexer_peek_token (parser
->lexer
);
6200 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6201 the already parsed nested-name-specifier. */
6202 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6204 /* Grab the nested-name-specifier and continue the loop. */
6205 cp_parser_pre_parsed_nested_name_specifier (parser
);
6206 /* If we originally encountered this nested-name-specifier
6207 with IS_DECLARATION set to false, we will not have
6208 resolved TYPENAME_TYPEs, so we must do so here. */
6210 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6212 new_scope
= resolve_typename_type (parser
->scope
,
6213 /*only_current_p=*/false);
6214 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6215 parser
->scope
= new_scope
;
6221 /* Spot cases that cannot be the beginning of a
6222 nested-name-specifier. On the second and subsequent times
6223 through the loop, we look for the `template' keyword. */
6224 if (success
&& token
->keyword
== RID_TEMPLATE
)
6226 /* A template-id can start a nested-name-specifier. */
6227 else if (token
->type
== CPP_TEMPLATE_ID
)
6229 /* DR 743: decltype can be used in a nested-name-specifier. */
6230 else if (token_is_decltype (token
))
6234 /* If the next token is not an identifier, then it is
6235 definitely not a type-name or namespace-name. */
6236 if (token
->type
!= CPP_NAME
)
6238 /* If the following token is neither a `<' (to begin a
6239 template-id), nor a `::', then we are not looking at a
6240 nested-name-specifier. */
6241 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6243 if (token
->type
== CPP_COLON
6244 && parser
->colon_corrects_to_scope_p
6245 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6247 gcc_rich_location
richloc (token
->location
);
6248 richloc
.add_fixit_replace ("::");
6250 "found %<:%> in nested-name-specifier, "
6252 token
->type
= CPP_SCOPE
;
6255 if (token
->type
!= CPP_SCOPE
6256 && !cp_parser_nth_token_starts_template_argument_list_p
6261 /* The nested-name-specifier is optional, so we parse
6263 cp_parser_parse_tentatively (parser
);
6265 /* Look for the optional `template' keyword, if this isn't the
6266 first time through the loop. */
6268 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6270 /* Save the old scope since the name lookup we are about to do
6271 might destroy it. */
6272 old_scope
= parser
->scope
;
6273 saved_qualifying_scope
= parser
->qualifying_scope
;
6274 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6275 look up names in "X<T>::I" in order to determine that "Y" is
6276 a template. So, if we have a typename at this point, we make
6277 an effort to look through it. */
6279 && !typename_keyword_p
6281 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6282 parser
->scope
= resolve_typename_type (parser
->scope
,
6283 /*only_current_p=*/false);
6284 /* Parse the qualifying entity. */
6286 = cp_parser_qualifying_entity (parser
,
6292 /* Look for the `::' token. */
6293 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6295 /* If we found what we wanted, we keep going; otherwise, we're
6297 if (!cp_parser_parse_definitely (parser
))
6299 bool error_p
= false;
6301 /* Restore the OLD_SCOPE since it was valid before the
6302 failed attempt at finding the last
6303 class-or-namespace-name. */
6304 parser
->scope
= old_scope
;
6305 parser
->qualifying_scope
= saved_qualifying_scope
;
6307 /* If the next token is a decltype, and the one after that is a
6308 `::', then the decltype has failed to resolve to a class or
6309 enumeration type. Give this error even when parsing
6310 tentatively since it can't possibly be valid--and we're going
6311 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6312 won't get another chance.*/
6313 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6314 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6317 token
= cp_lexer_consume_token (parser
->lexer
);
6318 error_at (token
->location
, "decltype evaluates to %qT, "
6319 "which is not a class or enumeration type",
6320 token
->u
.tree_check_value
->value
);
6321 parser
->scope
= error_mark_node
;
6325 cp_lexer_consume_token (parser
->lexer
);
6328 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6329 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6331 /* If we have a non-type template-id followed by ::, it can't
6332 possibly be valid. */
6333 token
= cp_lexer_peek_token (parser
->lexer
);
6334 tree tid
= token
->u
.tree_check_value
->value
;
6335 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6336 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6338 tree tmpl
= NULL_TREE
;
6339 if (is_overloaded_fn (tid
))
6341 tree fns
= get_fns (tid
);
6342 if (OVL_SINGLE_P (fns
))
6343 tmpl
= OVL_FIRST (fns
);
6344 error_at (token
->location
, "function template-id %qD "
6345 "in nested-name-specifier", tid
);
6349 /* Variable template. */
6350 tmpl
= TREE_OPERAND (tid
, 0);
6351 gcc_assert (variable_template_p (tmpl
));
6352 error_at (token
->location
, "variable template-id %qD "
6353 "in nested-name-specifier", tid
);
6356 inform (DECL_SOURCE_LOCATION (tmpl
),
6357 "%qD declared here", tmpl
);
6359 parser
->scope
= error_mark_node
;
6363 cp_lexer_consume_token (parser
->lexer
);
6364 cp_lexer_consume_token (parser
->lexer
);
6368 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6370 /* If the next token is an identifier, and the one after
6371 that is a `::', then any valid interpretation would have
6372 found a class-or-namespace-name. */
6373 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6374 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6376 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6379 token
= cp_lexer_consume_token (parser
->lexer
);
6382 if (!token
->error_reported
)
6385 tree ambiguous_decls
;
6387 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6389 /*is_template=*/false,
6390 /*is_namespace=*/false,
6391 /*check_dependency=*/true,
6394 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6395 error_at (token
->location
,
6396 "%qD used without template arguments",
6398 else if (ambiguous_decls
)
6400 // cp_parser_lookup_name has the same diagnostic,
6401 // thus make sure to emit it at most once.
6402 if (cp_parser_uncommitted_to_tentative_parse_p
6405 error_at (token
->location
,
6406 "reference to %qD is ambiguous",
6408 print_candidates (ambiguous_decls
);
6410 decl
= error_mark_node
;
6414 if (cxx_dialect
!= cxx98
)
6415 cp_parser_name_lookup_error
6416 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6419 cp_parser_name_lookup_error
6420 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6424 parser
->scope
= error_mark_node
;
6426 /* Treat this as a successful nested-name-specifier
6431 If the name found is not a class-name (clause
6432 _class_) or namespace-name (_namespace.def_), the
6433 program is ill-formed. */
6436 cp_lexer_consume_token (parser
->lexer
);
6440 /* We've found one valid nested-name-specifier. */
6442 /* Name lookup always gives us a DECL. */
6443 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6444 new_scope
= TREE_TYPE (new_scope
);
6445 /* Uses of "template" must be followed by actual templates. */
6446 if (template_keyword_p
6447 && !(CLASS_TYPE_P (new_scope
)
6448 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6449 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6450 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6451 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6452 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6453 == TEMPLATE_ID_EXPR
)))
6454 permerror (input_location
, TYPE_P (new_scope
)
6455 ? G_("%qT is not a template")
6456 : G_("%qD is not a template"),
6458 /* If it is a class scope, try to complete it; we are about to
6459 be looking up names inside the class. */
6460 if (TYPE_P (new_scope
)
6461 /* Since checking types for dependency can be expensive,
6462 avoid doing it if the type is already complete. */
6463 && !COMPLETE_TYPE_P (new_scope
)
6464 /* Do not try to complete dependent types. */
6465 && !dependent_type_p (new_scope
))
6467 new_scope
= complete_type (new_scope
);
6468 /* If it is a typedef to current class, use the current
6469 class instead, as the typedef won't have any names inside
6471 if (!COMPLETE_TYPE_P (new_scope
)
6472 && currently_open_class (new_scope
))
6473 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6475 /* Make sure we look in the right scope the next time through
6477 parser
->scope
= new_scope
;
6480 /* If parsing tentatively, replace the sequence of tokens that makes
6481 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6482 token. That way, should we re-parse the token stream, we will
6483 not have to repeat the effort required to do the parse, nor will
6484 we issue duplicate error messages. */
6485 if (success
&& start
)
6489 token
= cp_lexer_token_at (parser
->lexer
, start
);
6490 /* Reset the contents of the START token. */
6491 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6492 /* Retrieve any deferred checks. Do not pop this access checks yet
6493 so the memory will not be reclaimed during token replacing below. */
6494 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6495 token
->u
.tree_check_value
->value
= parser
->scope
;
6496 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6497 token
->u
.tree_check_value
->qualifying_scope
=
6498 parser
->qualifying_scope
;
6499 token
->keyword
= RID_MAX
;
6501 /* Purge all subsequent tokens. */
6502 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6506 pop_to_parent_deferring_access_checks ();
6508 return success
? parser
->scope
: NULL_TREE
;
6511 /* Parse a nested-name-specifier. See
6512 cp_parser_nested_name_specifier_opt for details. This function
6513 behaves identically, except that it will an issue an error if no
6514 nested-name-specifier is present. */
6517 cp_parser_nested_name_specifier (cp_parser
*parser
,
6518 bool typename_keyword_p
,
6519 bool check_dependency_p
,
6521 bool is_declaration
)
6525 /* Look for the nested-name-specifier. */
6526 scope
= cp_parser_nested_name_specifier_opt (parser
,
6531 /* If it was not present, issue an error message. */
6534 cp_parser_error (parser
, "expected nested-name-specifier");
6535 parser
->scope
= NULL_TREE
;
6541 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6542 this is either a class-name or a namespace-name (which corresponds
6543 to the class-or-namespace-name production in the grammar). For
6544 C++0x, it can also be a type-name that refers to an enumeration
6545 type or a simple-template-id.
6547 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6548 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6549 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6550 TYPE_P is TRUE iff the next name should be taken as a class-name,
6551 even the same name is declared to be another entity in the same
6554 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6555 specified by the class-or-namespace-name. If neither is found the
6556 ERROR_MARK_NODE is returned. */
6559 cp_parser_qualifying_entity (cp_parser
*parser
,
6560 bool typename_keyword_p
,
6561 bool template_keyword_p
,
6562 bool check_dependency_p
,
6564 bool is_declaration
)
6567 tree saved_qualifying_scope
;
6568 tree saved_object_scope
;
6571 bool successful_parse_p
;
6573 /* DR 743: decltype can appear in a nested-name-specifier. */
6574 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6576 scope
= cp_parser_decltype (parser
);
6577 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6578 && !MAYBE_CLASS_TYPE_P (scope
))
6580 cp_parser_simulate_error (parser
);
6581 return error_mark_node
;
6583 if (TYPE_NAME (scope
))
6584 scope
= TYPE_NAME (scope
);
6588 /* Before we try to parse the class-name, we must save away the
6589 current PARSER->SCOPE since cp_parser_class_name will destroy
6591 saved_scope
= parser
->scope
;
6592 saved_qualifying_scope
= parser
->qualifying_scope
;
6593 saved_object_scope
= parser
->object_scope
;
6594 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6595 there is no need to look for a namespace-name. */
6596 only_class_p
= template_keyword_p
6597 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6599 cp_parser_parse_tentatively (parser
);
6600 scope
= cp_parser_class_name (parser
,
6603 type_p
? class_type
: none_type
,
6605 /*class_head_p=*/false,
6607 /*enum_ok=*/cxx_dialect
> cxx98
);
6608 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6609 /* If that didn't work, try for a namespace-name. */
6610 if (!only_class_p
&& !successful_parse_p
)
6612 /* Restore the saved scope. */
6613 parser
->scope
= saved_scope
;
6614 parser
->qualifying_scope
= saved_qualifying_scope
;
6615 parser
->object_scope
= saved_object_scope
;
6616 /* If we are not looking at an identifier followed by the scope
6617 resolution operator, then this is not part of a
6618 nested-name-specifier. (Note that this function is only used
6619 to parse the components of a nested-name-specifier.) */
6620 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6621 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6622 return error_mark_node
;
6623 scope
= cp_parser_namespace_name (parser
);
6629 /* Return true if we are looking at a compound-literal, false otherwise. */
6632 cp_parser_compound_literal_p (cp_parser
*parser
)
6634 cp_lexer_save_tokens (parser
->lexer
);
6636 /* Skip tokens until the next token is a closing parenthesis.
6637 If we find the closing `)', and the next token is a `{', then
6638 we are looking at a compound-literal. */
6639 bool compound_literal_p
6640 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6641 /*consume_paren=*/true)
6642 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6644 /* Roll back the tokens we skipped. */
6645 cp_lexer_rollback_tokens (parser
->lexer
);
6647 return compound_literal_p
;
6650 /* Return true if EXPR is the integer constant zero or a complex constant
6651 of zero, without any folding, but ignoring location wrappers. */
6654 literal_integer_zerop (const_tree expr
)
6656 return (location_wrapper_p (expr
)
6657 && integer_zerop (TREE_OPERAND (expr
, 0)));
6660 /* Parse a postfix-expression.
6664 postfix-expression [ expression ]
6665 postfix-expression ( expression-list [opt] )
6666 simple-type-specifier ( expression-list [opt] )
6667 typename :: [opt] nested-name-specifier identifier
6668 ( expression-list [opt] )
6669 typename :: [opt] nested-name-specifier template [opt] template-id
6670 ( expression-list [opt] )
6671 postfix-expression . template [opt] id-expression
6672 postfix-expression -> template [opt] id-expression
6673 postfix-expression . pseudo-destructor-name
6674 postfix-expression -> pseudo-destructor-name
6675 postfix-expression ++
6676 postfix-expression --
6677 dynamic_cast < type-id > ( expression )
6678 static_cast < type-id > ( expression )
6679 reinterpret_cast < type-id > ( expression )
6680 const_cast < type-id > ( expression )
6681 typeid ( expression )
6687 ( type-id ) { initializer-list , [opt] }
6689 This extension is a GNU version of the C99 compound-literal
6690 construct. (The C99 grammar uses `type-name' instead of `type-id',
6691 but they are essentially the same concept.)
6693 If ADDRESS_P is true, the postfix expression is the operand of the
6694 `&' operator. CAST_P is true if this expression is the target of a
6697 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6698 class member access expressions [expr.ref].
6700 Returns a representation of the expression. */
6703 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6704 bool member_access_only_p
, bool decltype_p
,
6705 cp_id_kind
* pidk_return
)
6710 cp_id_kind idk
= CP_ID_KIND_NONE
;
6711 cp_expr postfix_expression
= NULL_TREE
;
6712 bool is_member_access
= false;
6714 /* Peek at the next token. */
6715 token
= cp_lexer_peek_token (parser
->lexer
);
6716 loc
= token
->location
;
6717 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6719 /* Some of the productions are determined by keywords. */
6720 keyword
= token
->keyword
;
6730 const char *saved_message
;
6731 bool saved_in_type_id_in_expr_p
;
6733 /* All of these can be handled in the same way from the point
6734 of view of parsing. Begin by consuming the token
6735 identifying the cast. */
6736 cp_lexer_consume_token (parser
->lexer
);
6738 /* New types cannot be defined in the cast. */
6739 saved_message
= parser
->type_definition_forbidden_message
;
6740 parser
->type_definition_forbidden_message
6741 = G_("types may not be defined in casts");
6743 /* Look for the opening `<'. */
6744 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6745 /* Parse the type to which we are casting. */
6746 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6747 parser
->in_type_id_in_expr_p
= true;
6748 type
= cp_parser_type_id (parser
);
6749 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6750 /* Look for the closing `>'. */
6751 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6752 /* Restore the old message. */
6753 parser
->type_definition_forbidden_message
= saved_message
;
6755 bool saved_greater_than_is_operator_p
6756 = parser
->greater_than_is_operator_p
;
6757 parser
->greater_than_is_operator_p
= true;
6759 /* And the expression which is being cast. */
6760 matching_parens parens
;
6761 parens
.require_open (parser
);
6762 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6763 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6765 location_t end_loc
= close_paren
?
6766 close_paren
->location
: UNKNOWN_LOCATION
;
6768 parser
->greater_than_is_operator_p
6769 = saved_greater_than_is_operator_p
;
6771 /* Only type conversions to integral or enumeration types
6772 can be used in constant-expressions. */
6773 if (!cast_valid_in_integral_constant_expression_p (type
)
6774 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6776 postfix_expression
= error_mark_node
;
6784 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6788 = build_static_cast (type
, expression
, tf_warning_or_error
);
6792 = build_reinterpret_cast (type
, expression
,
6793 tf_warning_or_error
);
6797 = build_const_cast (type
, expression
, tf_warning_or_error
);
6803 /* Construct a location e.g. :
6804 reinterpret_cast <int *> (expr)
6805 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6806 ranging from the start of the "*_cast" token to the final closing
6807 paren, with the caret at the start. */
6808 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6809 postfix_expression
.set_location (cp_cast_loc
);
6816 const char *saved_message
;
6817 bool saved_in_type_id_in_expr_p
;
6819 /* Consume the `typeid' token. */
6820 cp_lexer_consume_token (parser
->lexer
);
6821 /* Look for the `(' token. */
6822 matching_parens parens
;
6823 parens
.require_open (parser
);
6824 /* Types cannot be defined in a `typeid' expression. */
6825 saved_message
= parser
->type_definition_forbidden_message
;
6826 parser
->type_definition_forbidden_message
6827 = G_("types may not be defined in a %<typeid%> expression");
6828 /* We can't be sure yet whether we're looking at a type-id or an
6830 cp_parser_parse_tentatively (parser
);
6831 /* Try a type-id first. */
6832 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6833 parser
->in_type_id_in_expr_p
= true;
6834 type
= cp_parser_type_id (parser
);
6835 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6836 /* Look for the `)' token. Otherwise, we can't be sure that
6837 we're not looking at an expression: consider `typeid (int
6838 (3))', for example. */
6839 cp_token
*close_paren
= parens
.require_close (parser
);
6840 /* If all went well, simply lookup the type-id. */
6841 if (cp_parser_parse_definitely (parser
))
6842 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6843 /* Otherwise, fall back to the expression variant. */
6848 /* Look for an expression. */
6849 expression
= cp_parser_expression (parser
, & idk
);
6850 /* Compute its typeid. */
6851 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6852 /* Look for the `)' token. */
6853 close_paren
= parens
.require_close (parser
);
6855 /* Restore the saved message. */
6856 parser
->type_definition_forbidden_message
= saved_message
;
6857 /* `typeid' may not appear in an integral constant expression. */
6858 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6859 postfix_expression
= error_mark_node
;
6861 /* Construct a location e.g. :
6864 ranging from the start of the "typeid" token to the final closing
6865 paren, with the caret at the start. */
6868 location_t typeid_loc
6869 = make_location (start_loc
, start_loc
, close_paren
->location
);
6870 postfix_expression
.set_location (typeid_loc
);
6871 postfix_expression
.maybe_add_location_wrapper ();
6879 /* The syntax permitted here is the same permitted for an
6880 elaborated-type-specifier. */
6881 ++parser
->prevent_constrained_type_specifiers
;
6882 type
= cp_parser_elaborated_type_specifier (parser
,
6883 /*is_friend=*/false,
6884 /*is_declaration=*/false);
6885 --parser
->prevent_constrained_type_specifiers
;
6886 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6891 case RID_BUILTIN_SHUFFLE
:
6892 case RID_BUILTIN_LAUNDER
:
6894 vec
<tree
, va_gc
> *vec
;
6898 cp_lexer_consume_token (parser
->lexer
);
6899 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6900 /*cast_p=*/false, /*allow_expansion_p=*/true,
6901 /*non_constant_p=*/NULL
);
6904 postfix_expression
= error_mark_node
;
6908 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6914 if (vec
->length () == 1)
6916 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6919 error_at (loc
, "wrong number of arguments to "
6920 "%<__builtin_addressof%>");
6921 postfix_expression
= error_mark_node
;
6925 case RID_BUILTIN_LAUNDER
:
6926 if (vec
->length () == 1)
6927 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6928 tf_warning_or_error
);
6931 error_at (loc
, "wrong number of arguments to "
6932 "%<__builtin_launder%>");
6933 postfix_expression
= error_mark_node
;
6937 case RID_BUILTIN_SHUFFLE
:
6938 if (vec
->length () == 2)
6940 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6941 (*vec
)[1], tf_warning_or_error
);
6942 else if (vec
->length () == 3)
6944 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6945 (*vec
)[2], tf_warning_or_error
);
6948 error_at (loc
, "wrong number of arguments to "
6949 "%<__builtin_shuffle%>");
6950 postfix_expression
= error_mark_node
;
6964 /* If the next thing is a simple-type-specifier, we may be
6965 looking at a functional cast. We could also be looking at
6966 an id-expression. So, we try the functional cast, and if
6967 that doesn't work we fall back to the primary-expression. */
6968 cp_parser_parse_tentatively (parser
);
6969 /* Look for the simple-type-specifier. */
6970 ++parser
->prevent_constrained_type_specifiers
;
6971 type
= cp_parser_simple_type_specifier (parser
,
6972 /*decl_specs=*/NULL
,
6973 CP_PARSER_FLAGS_NONE
);
6974 --parser
->prevent_constrained_type_specifiers
;
6975 /* Parse the cast itself. */
6976 if (!cp_parser_error_occurred (parser
))
6978 = cp_parser_functional_cast (parser
, type
);
6979 /* If that worked, we're done. */
6980 if (cp_parser_parse_definitely (parser
))
6983 /* If the functional-cast didn't work out, try a
6984 compound-literal. */
6985 if (cp_parser_allow_gnu_extensions_p (parser
)
6986 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6988 cp_expr initializer
= NULL_TREE
;
6990 cp_parser_parse_tentatively (parser
);
6992 matching_parens parens
;
6993 parens
.consume_open (parser
);
6995 /* Avoid calling cp_parser_type_id pointlessly, see comment
6996 in cp_parser_cast_expression about c++/29234. */
6997 if (!cp_parser_compound_literal_p (parser
))
6998 cp_parser_simulate_error (parser
);
7001 /* Parse the type. */
7002 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7003 parser
->in_type_id_in_expr_p
= true;
7004 type
= cp_parser_type_id (parser
);
7005 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7006 parens
.require_close (parser
);
7009 /* If things aren't going well, there's no need to
7011 if (!cp_parser_error_occurred (parser
))
7013 bool non_constant_p
;
7014 /* Parse the brace-enclosed initializer list. */
7015 initializer
= cp_parser_braced_list (parser
,
7018 /* If that worked, we're definitely looking at a
7019 compound-literal expression. */
7020 if (cp_parser_parse_definitely (parser
))
7022 /* Warn the user that a compound literal is not
7023 allowed in standard C++. */
7024 pedwarn (input_location
, OPT_Wpedantic
,
7025 "ISO C++ forbids compound-literals");
7026 /* For simplicity, we disallow compound literals in
7027 constant-expressions. We could
7028 allow compound literals of integer type, whose
7029 initializer was a constant, in constant
7030 expressions. Permitting that usage, as a further
7031 extension, would not change the meaning of any
7032 currently accepted programs. (Of course, as
7033 compound literals are not part of ISO C++, the
7034 standard has nothing to say.) */
7035 if (cp_parser_non_integral_constant_expression (parser
,
7038 postfix_expression
= error_mark_node
;
7041 /* Form the representation of the compound-literal. */
7043 = finish_compound_literal (type
, initializer
,
7044 tf_warning_or_error
, fcl_c99
);
7045 postfix_expression
.set_location (initializer
.get_location ());
7050 /* It must be a primary-expression. */
7052 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7053 /*template_arg_p=*/false,
7060 /* Note that we don't need to worry about calling build_cplus_new on a
7061 class-valued CALL_EXPR in decltype when it isn't the end of the
7062 postfix-expression; unary_complex_lvalue will take care of that for
7065 /* Keep looping until the postfix-expression is complete. */
7068 if (idk
== CP_ID_KIND_UNQUALIFIED
7069 && identifier_p (postfix_expression
)
7070 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7071 /* It is not a Koenig lookup function call. */
7073 = unqualified_name_lookup_error (postfix_expression
);
7075 /* Peek at the next token. */
7076 token
= cp_lexer_peek_token (parser
->lexer
);
7078 switch (token
->type
)
7080 case CPP_OPEN_SQUARE
:
7081 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7083 cp_parser_error (parser
,
7084 "two consecutive %<[%> shall "
7085 "only introduce an attribute");
7086 return error_mark_node
;
7089 = cp_parser_postfix_open_square_expression (parser
,
7093 postfix_expression
.set_range (start_loc
,
7094 postfix_expression
.get_location ());
7096 idk
= CP_ID_KIND_NONE
;
7097 is_member_access
= false;
7100 case CPP_OPEN_PAREN
:
7101 /* postfix-expression ( expression-list [opt] ) */
7104 bool is_builtin_constant_p
;
7105 bool saved_integral_constant_expression_p
= false;
7106 bool saved_non_integral_constant_expression_p
= false;
7107 tsubst_flags_t complain
= complain_flags (decltype_p
);
7108 vec
<tree
, va_gc
> *args
;
7109 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7111 is_member_access
= false;
7113 is_builtin_constant_p
7114 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7115 if (is_builtin_constant_p
)
7117 /* The whole point of __builtin_constant_p is to allow
7118 non-constant expressions to appear as arguments. */
7119 saved_integral_constant_expression_p
7120 = parser
->integral_constant_expression_p
;
7121 saved_non_integral_constant_expression_p
7122 = parser
->non_integral_constant_expression_p
;
7123 parser
->integral_constant_expression_p
= false;
7125 args
= (cp_parser_parenthesized_expression_list
7127 /*cast_p=*/false, /*allow_expansion_p=*/true,
7128 /*non_constant_p=*/NULL
,
7129 /*close_paren_loc=*/&close_paren_loc
,
7130 /*wrap_locations_p=*/true));
7131 if (is_builtin_constant_p
)
7133 parser
->integral_constant_expression_p
7134 = saved_integral_constant_expression_p
;
7135 parser
->non_integral_constant_expression_p
7136 = saved_non_integral_constant_expression_p
;
7141 postfix_expression
= error_mark_node
;
7145 /* Function calls are not permitted in
7146 constant-expressions. */
7147 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7148 && cp_parser_non_integral_constant_expression (parser
,
7151 postfix_expression
= error_mark_node
;
7152 release_tree_vector (args
);
7157 if (idk
== CP_ID_KIND_UNQUALIFIED
7158 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7160 if (identifier_p (postfix_expression
))
7162 if (!args
->is_empty ())
7165 if (!any_type_dependent_arguments_p (args
))
7167 = perform_koenig_lookup (postfix_expression
, args
,
7172 = unqualified_fn_lookup_error (postfix_expression
);
7174 /* We do not perform argument-dependent lookup if
7175 normal lookup finds a non-function, in accordance
7176 with the expected resolution of DR 218. */
7177 else if (!args
->is_empty ()
7178 && is_overloaded_fn (postfix_expression
))
7180 tree fn
= get_first_fn (postfix_expression
);
7181 fn
= STRIP_TEMPLATE (fn
);
7183 /* Do not do argument dependent lookup if regular
7184 lookup finds a member function or a block-scope
7185 function declaration. [basic.lookup.argdep]/3 */
7186 if (!DECL_FUNCTION_MEMBER_P (fn
)
7187 && !DECL_LOCAL_FUNCTION_P (fn
))
7190 if (!any_type_dependent_arguments_p (args
))
7192 = perform_koenig_lookup (postfix_expression
, args
,
7198 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7200 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7201 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7203 if (processing_template_decl
7204 && (type_dependent_object_expression_p (instance
)
7205 || (!BASELINK_P (fn
)
7206 && TREE_CODE (fn
) != FIELD_DECL
)
7207 || type_dependent_expression_p (fn
)
7208 || any_type_dependent_arguments_p (args
)))
7210 maybe_generic_this_capture (instance
, fn
);
7212 = build_min_nt_call_vec (postfix_expression
, args
);
7213 release_tree_vector (args
);
7217 if (BASELINK_P (fn
))
7220 = (build_new_method_call
7221 (instance
, fn
, &args
, NULL_TREE
,
7222 (idk
== CP_ID_KIND_QUALIFIED
7223 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7230 = finish_call_expr (postfix_expression
, &args
,
7231 /*disallow_virtual=*/false,
7235 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7236 || TREE_CODE (postfix_expression
) == MEMBER_REF
7237 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7238 postfix_expression
= (build_offset_ref_call_from_tree
7239 (postfix_expression
, &args
,
7241 else if (idk
== CP_ID_KIND_QUALIFIED
)
7242 /* A call to a static class member, or a namespace-scope
7245 = finish_call_expr (postfix_expression
, &args
,
7246 /*disallow_virtual=*/true,
7250 /* All other function calls. */
7252 = finish_call_expr (postfix_expression
, &args
,
7253 /*disallow_virtual=*/false,
7257 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7259 location_t combined_loc
= make_location (token
->location
,
7262 postfix_expression
.set_location (combined_loc
);
7265 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7266 idk
= CP_ID_KIND_NONE
;
7268 release_tree_vector (args
);
7274 /* postfix-expression . template [opt] id-expression
7275 postfix-expression . pseudo-destructor-name
7276 postfix-expression -> template [opt] id-expression
7277 postfix-expression -> pseudo-destructor-name */
7279 /* Consume the `.' or `->' operator. */
7280 cp_lexer_consume_token (parser
->lexer
);
7283 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7287 is_member_access
= true;
7291 /* postfix-expression ++ */
7292 /* Consume the `++' token. */
7293 cp_lexer_consume_token (parser
->lexer
);
7294 /* Generate a representation for the complete expression. */
7296 = finish_increment_expr (postfix_expression
,
7297 POSTINCREMENT_EXPR
);
7298 /* Increments may not appear in constant-expressions. */
7299 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7300 postfix_expression
= error_mark_node
;
7301 idk
= CP_ID_KIND_NONE
;
7302 is_member_access
= false;
7305 case CPP_MINUS_MINUS
:
7306 /* postfix-expression -- */
7307 /* Consume the `--' token. */
7308 cp_lexer_consume_token (parser
->lexer
);
7309 /* Generate a representation for the complete expression. */
7311 = finish_increment_expr (postfix_expression
,
7312 POSTDECREMENT_EXPR
);
7313 /* Decrements may not appear in constant-expressions. */
7314 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7315 postfix_expression
= error_mark_node
;
7316 idk
= CP_ID_KIND_NONE
;
7317 is_member_access
= false;
7321 if (pidk_return
!= NULL
)
7322 * pidk_return
= idk
;
7323 if (member_access_only_p
)
7324 return is_member_access
7325 ? postfix_expression
7326 : cp_expr (error_mark_node
);
7328 return postfix_expression
;
7332 /* We should never get here. */
7334 return error_mark_node
;
7337 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7338 by cp_parser_builtin_offsetof. We're looking for
7340 postfix-expression [ expression ]
7341 postfix-expression [ braced-init-list ] (C++11)
7343 FOR_OFFSETOF is set if we're being called in that context, which
7344 changes how we deal with integer constant expressions. */
7347 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7348 tree postfix_expression
,
7352 tree index
= NULL_TREE
;
7353 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7354 bool saved_greater_than_is_operator_p
;
7356 /* Consume the `[' token. */
7357 cp_lexer_consume_token (parser
->lexer
);
7359 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7360 parser
->greater_than_is_operator_p
= true;
7362 /* Parse the index expression. */
7363 /* ??? For offsetof, there is a question of what to allow here. If
7364 offsetof is not being used in an integral constant expression context,
7365 then we *could* get the right answer by computing the value at runtime.
7366 If we are in an integral constant expression context, then we might
7367 could accept any constant expression; hard to say without analysis.
7368 Rather than open the barn door too wide right away, allow only integer
7369 constant expressions here. */
7371 index
= cp_parser_constant_expression (parser
);
7374 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7376 bool expr_nonconst_p
;
7377 cp_lexer_set_source_position (parser
->lexer
);
7378 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7379 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7382 index
= cp_parser_expression (parser
);
7385 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7387 /* Look for the closing `]'. */
7388 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7390 /* Build the ARRAY_REF. */
7391 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7394 /* When not doing offsetof, array references are not permitted in
7395 constant-expressions. */
7397 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7398 postfix_expression
= error_mark_node
;
7400 return postfix_expression
;
7403 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7404 dereference of incomplete type, returns true if error_mark_node should
7405 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7406 and *DEPENDENT_P. */
7409 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7412 /* In a template, be permissive by treating an object expression
7413 of incomplete type as dependent (after a pedwarn). */
7414 diagnostic_t kind
= (processing_template_decl
7415 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7417 switch (TREE_CODE (*postfix_expression
))
7420 case REINTERPRET_CAST_EXPR
:
7421 case CONST_CAST_EXPR
:
7422 case STATIC_CAST_EXPR
:
7423 case DYNAMIC_CAST_EXPR
:
7424 case IMPLICIT_CONV_EXPR
:
7425 case VIEW_CONVERT_EXPR
:
7426 case NON_LVALUE_EXPR
:
7430 /* Don't emit any diagnostic for OVERLOADs. */
7434 /* Avoid clobbering e.g. DECLs. */
7435 if (!EXPR_P (*postfix_expression
))
7440 if (kind
== DK_IGNORED
)
7443 location_t exploc
= location_of (*postfix_expression
);
7444 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7445 if (!MAYBE_CLASS_TYPE_P (*scope
))
7447 if (kind
== DK_ERROR
)
7448 *scope
= *postfix_expression
= error_mark_node
;
7449 else if (processing_template_decl
)
7451 *dependent_p
= true;
7452 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7457 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7458 by cp_parser_builtin_offsetof. We're looking for
7460 postfix-expression . template [opt] id-expression
7461 postfix-expression . pseudo-destructor-name
7462 postfix-expression -> template [opt] id-expression
7463 postfix-expression -> pseudo-destructor-name
7465 FOR_OFFSETOF is set if we're being called in that context. That sorta
7466 limits what of the above we'll actually accept, but nevermind.
7467 TOKEN_TYPE is the "." or "->" token, which will already have been
7468 removed from the stream. */
7471 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7472 enum cpp_ttype token_type
,
7473 cp_expr postfix_expression
,
7474 bool for_offsetof
, cp_id_kind
*idk
,
7475 location_t location
)
7479 bool pseudo_destructor_p
;
7480 tree scope
= NULL_TREE
;
7481 location_t start_loc
= postfix_expression
.get_start ();
7483 /* If this is a `->' operator, dereference the pointer. */
7484 if (token_type
== CPP_DEREF
)
7485 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7486 tf_warning_or_error
);
7487 /* Check to see whether or not the expression is type-dependent and
7488 not the current instantiation. */
7489 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7490 /* The identifier following the `->' or `.' is not qualified. */
7491 parser
->scope
= NULL_TREE
;
7492 parser
->qualifying_scope
= NULL_TREE
;
7493 parser
->object_scope
= NULL_TREE
;
7494 *idk
= CP_ID_KIND_NONE
;
7496 /* Enter the scope corresponding to the type of the object
7497 given by the POSTFIX_EXPRESSION. */
7500 scope
= TREE_TYPE (postfix_expression
);
7501 /* According to the standard, no expression should ever have
7502 reference type. Unfortunately, we do not currently match
7503 the standard in this respect in that our internal representation
7504 of an expression may have reference type even when the standard
7505 says it does not. Therefore, we have to manually obtain the
7506 underlying type here. */
7507 scope
= non_reference (scope
);
7508 /* The type of the POSTFIX_EXPRESSION must be complete. */
7509 /* Unlike the object expression in other contexts, *this is not
7510 required to be of complete type for purposes of class member
7511 access (5.2.5) outside the member function body. */
7512 if (postfix_expression
!= current_class_ref
7513 && scope
!= error_mark_node
7514 && !currently_open_class (scope
))
7516 scope
= complete_type (scope
);
7517 if (!COMPLETE_TYPE_P (scope
)
7518 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7520 return error_mark_node
;
7525 /* Let the name lookup machinery know that we are processing a
7526 class member access expression. */
7527 parser
->context
->object_type
= scope
;
7528 /* If something went wrong, we want to be able to discern that case,
7529 as opposed to the case where there was no SCOPE due to the type
7530 of expression being dependent. */
7532 scope
= error_mark_node
;
7533 /* If the SCOPE was erroneous, make the various semantic analysis
7534 functions exit quickly -- and without issuing additional error
7536 if (scope
== error_mark_node
)
7537 postfix_expression
= error_mark_node
;
7542 /* Tell cp_parser_lookup_name that there was an object, even though it's
7544 parser
->context
->object_type
= unknown_type_node
;
7546 /* Assume this expression is not a pseudo-destructor access. */
7547 pseudo_destructor_p
= false;
7549 /* If the SCOPE is a scalar type, then, if this is a valid program,
7550 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7551 is type dependent, it can be pseudo-destructor-name or something else.
7552 Try to parse it as pseudo-destructor-name first. */
7553 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7558 cp_parser_parse_tentatively (parser
);
7559 /* Parse the pseudo-destructor-name. */
7561 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7564 && (cp_parser_error_occurred (parser
)
7565 || !SCALAR_TYPE_P (type
)))
7566 cp_parser_abort_tentative_parse (parser
);
7567 else if (cp_parser_parse_definitely (parser
))
7569 pseudo_destructor_p
= true;
7571 = finish_pseudo_destructor_expr (postfix_expression
,
7576 if (!pseudo_destructor_p
)
7578 /* If the SCOPE is not a scalar type, we are looking at an
7579 ordinary class member access expression, rather than a
7580 pseudo-destructor-name. */
7582 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7583 /* Parse the id-expression. */
7584 name
= (cp_parser_id_expression
7586 cp_parser_optional_template_keyword (parser
),
7587 /*check_dependency_p=*/true,
7589 /*declarator_p=*/false,
7590 /*optional_p=*/false));
7591 /* In general, build a SCOPE_REF if the member name is qualified.
7592 However, if the name was not dependent and has already been
7593 resolved; there is no need to build the SCOPE_REF. For example;
7595 struct X { void f(); };
7596 template <typename T> void f(T* t) { t->X::f(); }
7598 Even though "t" is dependent, "X::f" is not and has been resolved
7599 to a BASELINK; there is no need to include scope information. */
7601 /* But we do need to remember that there was an explicit scope for
7602 virtual function calls. */
7604 *idk
= CP_ID_KIND_QUALIFIED
;
7606 /* If the name is a template-id that names a type, we will get a
7607 TYPE_DECL here. That is invalid code. */
7608 if (TREE_CODE (name
) == TYPE_DECL
)
7610 error_at (token
->location
, "invalid use of %qD", name
);
7611 postfix_expression
= error_mark_node
;
7615 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7617 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7619 error_at (token
->location
, "%<%D::%D%> is not a class member",
7620 parser
->scope
, name
);
7621 postfix_expression
= error_mark_node
;
7624 name
= build_qualified_name (/*type=*/NULL_TREE
,
7628 parser
->scope
= NULL_TREE
;
7629 parser
->qualifying_scope
= NULL_TREE
;
7630 parser
->object_scope
= NULL_TREE
;
7632 if (parser
->scope
&& name
&& BASELINK_P (name
))
7633 adjust_result_of_qualified_name_lookup
7634 (name
, parser
->scope
, scope
);
7636 = finish_class_member_access_expr (postfix_expression
, name
,
7638 tf_warning_or_error
);
7639 /* Build a location e.g.:
7642 where the caret is at the deref token, ranging from
7643 the start of postfix_expression to the end of the access expr. */
7645 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7646 location_t combined_loc
7647 = make_location (input_location
, start_loc
, end_loc
);
7648 protected_set_expr_location (postfix_expression
, combined_loc
);
7652 /* We no longer need to look up names in the scope of the object on
7653 the left-hand side of the `.' or `->' operator. */
7654 parser
->context
->object_type
= NULL_TREE
;
7656 /* Outside of offsetof, these operators may not appear in
7657 constant-expressions. */
7659 && (cp_parser_non_integral_constant_expression
7660 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7661 postfix_expression
= error_mark_node
;
7663 return postfix_expression
;
7666 /* Parse a parenthesized expression-list.
7669 assignment-expression
7670 expression-list, assignment-expression
7675 identifier, expression-list
7677 CAST_P is true if this expression is the target of a cast.
7679 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7682 WRAP_LOCATIONS_P is true if expressions within this list for which
7683 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7684 their source locations.
7686 Returns a vector of trees. Each element is a representation of an
7687 assignment-expression. NULL is returned if the ( and or ) are
7688 missing. An empty, but allocated, vector is returned on no
7689 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7690 if we are parsing an attribute list for an attribute that wants a
7691 plain identifier argument, normal_attr for an attribute that wants
7692 an expression, or non_attr if we aren't parsing an attribute list. If
7693 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7694 not all of the expressions in the list were constant.
7695 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7696 will be written to with the location of the closing parenthesis. If
7697 an error occurs, it may or may not be written to. */
7699 static vec
<tree
, va_gc
> *
7700 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7701 int is_attribute_list
,
7703 bool allow_expansion_p
,
7704 bool *non_constant_p
,
7705 location_t
*close_paren_loc
,
7706 bool wrap_locations_p
)
7708 vec
<tree
, va_gc
> *expression_list
;
7709 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7710 tree identifier
= NULL_TREE
;
7711 bool saved_greater_than_is_operator_p
;
7713 /* Assume all the expressions will be constant. */
7715 *non_constant_p
= false;
7717 matching_parens parens
;
7718 if (!parens
.require_open (parser
))
7721 expression_list
= make_tree_vector ();
7723 /* Within a parenthesized expression, a `>' token is always
7724 the greater-than operator. */
7725 saved_greater_than_is_operator_p
7726 = parser
->greater_than_is_operator_p
;
7727 parser
->greater_than_is_operator_p
= true;
7729 cp_expr
expr (NULL_TREE
);
7731 /* Consume expressions until there are no more. */
7732 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7735 /* At the beginning of attribute lists, check to see if the
7736 next token is an identifier. */
7737 if (is_attribute_list
== id_attr
7738 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7742 /* Consume the identifier. */
7743 token
= cp_lexer_consume_token (parser
->lexer
);
7744 /* Save the identifier. */
7745 identifier
= token
->u
.value
;
7749 bool expr_non_constant_p
;
7751 /* Parse the next assignment-expression. */
7752 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7754 /* A braced-init-list. */
7755 cp_lexer_set_source_position (parser
->lexer
);
7756 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7757 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7758 if (non_constant_p
&& expr_non_constant_p
)
7759 *non_constant_p
= true;
7761 else if (non_constant_p
)
7763 expr
= (cp_parser_constant_expression
7764 (parser
, /*allow_non_constant_p=*/true,
7765 &expr_non_constant_p
));
7766 if (expr_non_constant_p
)
7767 *non_constant_p
= true;
7770 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7774 expr
= instantiate_non_dependent_expr (expr
);
7776 /* If we have an ellipsis, then this is an expression
7778 if (allow_expansion_p
7779 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7781 /* Consume the `...'. */
7782 cp_lexer_consume_token (parser
->lexer
);
7784 /* Build the argument pack. */
7785 expr
= make_pack_expansion (expr
);
7788 if (wrap_locations_p
)
7789 expr
.maybe_add_location_wrapper ();
7791 /* Add it to the list. We add error_mark_node
7792 expressions to the list, so that we can still tell if
7793 the correct form for a parenthesized expression-list
7794 is found. That gives better errors. */
7795 vec_safe_push (expression_list
, expr
.get_value ());
7797 if (expr
== error_mark_node
)
7801 /* After the first item, attribute lists look the same as
7802 expression lists. */
7803 is_attribute_list
= non_attr
;
7806 /* If the next token isn't a `,', then we are done. */
7807 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7810 /* Otherwise, consume the `,' and keep going. */
7811 cp_lexer_consume_token (parser
->lexer
);
7814 if (close_paren_loc
)
7815 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7817 if (!parens
.require_close (parser
))
7822 /* We try and resync to an unnested comma, as that will give the
7823 user better diagnostics. */
7824 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7825 /*recovering=*/true,
7827 /*consume_paren=*/true);
7832 parser
->greater_than_is_operator_p
7833 = saved_greater_than_is_operator_p
;
7838 parser
->greater_than_is_operator_p
7839 = saved_greater_than_is_operator_p
;
7842 vec_safe_insert (expression_list
, 0, identifier
);
7844 return expression_list
;
7847 /* Parse a pseudo-destructor-name.
7849 pseudo-destructor-name:
7850 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7851 :: [opt] nested-name-specifier template template-id :: ~ type-name
7852 :: [opt] nested-name-specifier [opt] ~ type-name
7854 If either of the first two productions is used, sets *SCOPE to the
7855 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7856 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7857 or ERROR_MARK_NODE if the parse fails. */
7860 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7865 bool nested_name_specifier_p
;
7868 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7869 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7870 && !type_dependent_expression_p (object
))
7872 if (cxx_dialect
< cxx14
)
7873 pedwarn (input_location
, 0,
7874 "%<~auto%> only available with "
7875 "-std=c++14 or -std=gnu++14");
7876 cp_lexer_consume_token (parser
->lexer
);
7877 cp_lexer_consume_token (parser
->lexer
);
7879 *type
= TREE_TYPE (object
);
7883 /* Assume that things will not work out. */
7884 *type
= error_mark_node
;
7886 /* Look for the optional `::' operator. */
7887 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7888 /* Look for the optional nested-name-specifier. */
7889 nested_name_specifier_p
7890 = (cp_parser_nested_name_specifier_opt (parser
,
7891 /*typename_keyword_p=*/false,
7892 /*check_dependency_p=*/true,
7894 /*is_declaration=*/false)
7896 /* Now, if we saw a nested-name-specifier, we might be doing the
7897 second production. */
7898 if (nested_name_specifier_p
7899 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7901 /* Consume the `template' keyword. */
7902 cp_lexer_consume_token (parser
->lexer
);
7903 /* Parse the template-id. */
7904 cp_parser_template_id (parser
,
7905 /*template_keyword_p=*/true,
7906 /*check_dependency_p=*/false,
7908 /*is_declaration=*/true);
7909 /* Look for the `::' token. */
7910 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7912 /* If the next token is not a `~', then there might be some
7913 additional qualification. */
7914 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7916 /* At this point, we're looking for "type-name :: ~". The type-name
7917 must not be a class-name, since this is a pseudo-destructor. So,
7918 it must be either an enum-name, or a typedef-name -- both of which
7919 are just identifiers. So, we peek ahead to check that the "::"
7920 and "~" tokens are present; if they are not, then we can avoid
7921 calling type_name. */
7922 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7923 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7924 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7926 cp_parser_error (parser
, "non-scalar type");
7930 /* Look for the type-name. */
7931 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7932 if (*scope
== error_mark_node
)
7935 /* Look for the `::' token. */
7936 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7941 /* Look for the `~'. */
7942 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7944 /* Once we see the ~, this has to be a pseudo-destructor. */
7945 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7946 cp_parser_commit_to_topmost_tentative_parse (parser
);
7948 /* Look for the type-name again. We are not responsible for
7949 checking that it matches the first type-name. */
7950 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7953 /* Parse a unary-expression.
7959 unary-operator cast-expression
7960 sizeof unary-expression
7962 alignof ( type-id ) [C++0x]
7969 __extension__ cast-expression
7970 __alignof__ unary-expression
7971 __alignof__ ( type-id )
7972 alignof unary-expression [C++0x]
7973 __real__ cast-expression
7974 __imag__ cast-expression
7976 sizeof ( type-id ) { initializer-list , [opt] }
7977 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7978 __alignof__ ( type-id ) { initializer-list , [opt] }
7980 ADDRESS_P is true iff the unary-expression is appearing as the
7981 operand of the `&' operator. CAST_P is true if this expression is
7982 the target of a cast.
7984 Returns a representation of the expression. */
7987 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7988 bool address_p
, bool cast_p
, bool decltype_p
)
7991 enum tree_code unary_operator
;
7993 /* Peek at the next token. */
7994 token
= cp_lexer_peek_token (parser
->lexer
);
7995 /* Some keywords give away the kind of expression. */
7996 if (token
->type
== CPP_KEYWORD
)
7998 enum rid keyword
= token
->keyword
;
8007 location_t start_loc
= token
->location
;
8009 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8010 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8012 /* Consume the token. */
8013 cp_lexer_consume_token (parser
->lexer
);
8014 /* Parse the operand. */
8015 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8017 if (TYPE_P (operand
))
8018 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8022 /* ISO C++ defines alignof only with types, not with
8023 expressions. So pedwarn if alignof is used with a non-
8024 type expression. However, __alignof__ is ok. */
8026 pedwarn (token
->location
, OPT_Wpedantic
,
8027 "ISO C++ does not allow %<alignof%> "
8030 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8032 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8033 SIZEOF_EXPR with the original operand. */
8034 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8036 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8038 if (!processing_template_decl
&& TYPE_P (operand
))
8040 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8041 build1 (NOP_EXPR
, operand
,
8043 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8046 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8047 TREE_SIDE_EFFECTS (ret
) = 0;
8048 TREE_READONLY (ret
) = 1;
8052 /* Construct a location e.g. :
8055 with start == caret at the start of the "alignof"/"sizeof"
8056 token, with the endpoint at the final closing paren. */
8057 location_t finish_loc
8058 = cp_lexer_previous_token (parser
->lexer
)->location
;
8059 location_t compound_loc
8060 = make_location (start_loc
, start_loc
, finish_loc
);
8062 cp_expr
ret_expr (ret
);
8063 ret_expr
.set_location (compound_loc
);
8064 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8069 return cp_parser_new_expression (parser
);
8072 return cp_parser_delete_expression (parser
);
8076 /* The saved value of the PEDANTIC flag. */
8080 /* Save away the PEDANTIC flag. */
8081 cp_parser_extension_opt (parser
, &saved_pedantic
);
8082 /* Parse the cast-expression. */
8083 expr
= cp_parser_simple_cast_expression (parser
);
8084 /* Restore the PEDANTIC flag. */
8085 pedantic
= saved_pedantic
;
8095 /* Consume the `__real__' or `__imag__' token. */
8096 cp_lexer_consume_token (parser
->lexer
);
8097 /* Parse the cast-expression. */
8098 expression
= cp_parser_simple_cast_expression (parser
);
8099 /* Create the complete representation. */
8100 return build_x_unary_op (token
->location
,
8101 (keyword
== RID_REALPART
8102 ? REALPART_EXPR
: IMAGPART_EXPR
),
8104 tf_warning_or_error
);
8108 case RID_TRANSACTION_ATOMIC
:
8109 case RID_TRANSACTION_RELAXED
:
8110 return cp_parser_transaction_expression (parser
, keyword
);
8115 const char *saved_message
;
8116 bool saved_integral_constant_expression_p
;
8117 bool saved_non_integral_constant_expression_p
;
8118 bool saved_greater_than_is_operator_p
;
8120 location_t start_loc
= token
->location
;
8122 cp_lexer_consume_token (parser
->lexer
);
8123 matching_parens parens
;
8124 parens
.require_open (parser
);
8126 saved_message
= parser
->type_definition_forbidden_message
;
8127 parser
->type_definition_forbidden_message
8128 = G_("types may not be defined in %<noexcept%> expressions");
8130 saved_integral_constant_expression_p
8131 = parser
->integral_constant_expression_p
;
8132 saved_non_integral_constant_expression_p
8133 = parser
->non_integral_constant_expression_p
;
8134 parser
->integral_constant_expression_p
= false;
8136 saved_greater_than_is_operator_p
8137 = parser
->greater_than_is_operator_p
;
8138 parser
->greater_than_is_operator_p
= true;
8140 ++cp_unevaluated_operand
;
8141 ++c_inhibit_evaluation_warnings
;
8142 ++cp_noexcept_operand
;
8143 expr
= cp_parser_expression (parser
);
8144 --cp_noexcept_operand
;
8145 --c_inhibit_evaluation_warnings
;
8146 --cp_unevaluated_operand
;
8148 parser
->greater_than_is_operator_p
8149 = saved_greater_than_is_operator_p
;
8151 parser
->integral_constant_expression_p
8152 = saved_integral_constant_expression_p
;
8153 parser
->non_integral_constant_expression_p
8154 = saved_non_integral_constant_expression_p
;
8156 parser
->type_definition_forbidden_message
= saved_message
;
8158 location_t finish_loc
8159 = cp_lexer_peek_token (parser
->lexer
)->location
;
8160 parens
.require_close (parser
);
8162 /* Construct a location of the form:
8165 with start == caret, finishing at the close-paren. */
8166 location_t noexcept_loc
8167 = make_location (start_loc
, start_loc
, finish_loc
);
8169 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8178 /* Look for the `:: new' and `:: delete', which also signal the
8179 beginning of a new-expression, or delete-expression,
8180 respectively. If the next token is `::', then it might be one of
8182 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8186 /* See if the token after the `::' is one of the keywords in
8187 which we're interested. */
8188 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8189 /* If it's `new', we have a new-expression. */
8190 if (keyword
== RID_NEW
)
8191 return cp_parser_new_expression (parser
);
8192 /* Similarly, for `delete'. */
8193 else if (keyword
== RID_DELETE
)
8194 return cp_parser_delete_expression (parser
);
8197 /* Look for a unary operator. */
8198 unary_operator
= cp_parser_unary_operator (token
);
8199 /* The `++' and `--' operators can be handled similarly, even though
8200 they are not technically unary-operators in the grammar. */
8201 if (unary_operator
== ERROR_MARK
)
8203 if (token
->type
== CPP_PLUS_PLUS
)
8204 unary_operator
= PREINCREMENT_EXPR
;
8205 else if (token
->type
== CPP_MINUS_MINUS
)
8206 unary_operator
= PREDECREMENT_EXPR
;
8207 /* Handle the GNU address-of-label extension. */
8208 else if (cp_parser_allow_gnu_extensions_p (parser
)
8209 && token
->type
== CPP_AND_AND
)
8213 location_t start_loc
= token
->location
;
8215 /* Consume the '&&' token. */
8216 cp_lexer_consume_token (parser
->lexer
);
8217 /* Look for the identifier. */
8218 location_t finish_loc
8219 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8220 identifier
= cp_parser_identifier (parser
);
8221 /* Construct a location of the form:
8224 with caret==start at the "&&", finish at the end of the label. */
8225 location_t combined_loc
8226 = make_location (start_loc
, start_loc
, finish_loc
);
8227 /* Create an expression representing the address. */
8228 expression
= finish_label_address_expr (identifier
, combined_loc
);
8229 if (cp_parser_non_integral_constant_expression (parser
,
8231 expression
= error_mark_node
;
8235 if (unary_operator
!= ERROR_MARK
)
8237 cp_expr cast_expression
;
8238 cp_expr expression
= error_mark_node
;
8239 non_integral_constant non_constant_p
= NIC_NONE
;
8240 location_t loc
= token
->location
;
8241 tsubst_flags_t complain
= complain_flags (decltype_p
);
8243 /* Consume the operator token. */
8244 token
= cp_lexer_consume_token (parser
->lexer
);
8245 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8247 /* Parse the cast-expression. */
8249 = cp_parser_cast_expression (parser
,
8250 unary_operator
== ADDR_EXPR
,
8256 OP_TOKEN CAST_EXPRESSION
8257 ^~~~~~~~~~~~~~~~~~~~~~~~~
8258 with start==caret at the operator token, and
8259 extending to the end of the cast_expression. */
8260 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8262 /* Now, build an appropriate representation. */
8263 switch (unary_operator
)
8266 non_constant_p
= NIC_STAR
;
8267 expression
= build_x_indirect_ref (loc
, cast_expression
,
8270 /* TODO: build_x_indirect_ref does not always honor the
8271 location, so ensure it is set. */
8272 expression
.set_location (loc
);
8276 non_constant_p
= NIC_ADDR
;
8279 expression
= build_x_unary_op (loc
, unary_operator
,
8282 /* TODO: build_x_unary_op does not always honor the location,
8283 so ensure it is set. */
8284 expression
.set_location (loc
);
8287 case PREINCREMENT_EXPR
:
8288 case PREDECREMENT_EXPR
:
8289 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8290 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8293 /* Immediately fold negation of a constant, unless the constant is 0
8294 (since -0 == 0) or it would overflow. */
8295 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8296 && CONSTANT_CLASS_P (cast_expression
)
8297 && !integer_zerop (cast_expression
)
8298 && !TREE_OVERFLOW (cast_expression
))
8300 tree folded
= fold_build1 (unary_operator
,
8301 TREE_TYPE (cast_expression
),
8303 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8305 expression
= cp_expr (folded
, loc
);
8310 case UNARY_PLUS_EXPR
:
8311 case TRUTH_NOT_EXPR
:
8312 expression
= finish_unary_op_expr (loc
, unary_operator
,
8313 cast_expression
, complain
);
8320 if (non_constant_p
!= NIC_NONE
8321 && cp_parser_non_integral_constant_expression (parser
,
8323 expression
= error_mark_node
;
8328 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8329 /*member_access_only_p=*/false,
8334 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8335 unary-operator, the corresponding tree code is returned. */
8337 static enum tree_code
8338 cp_parser_unary_operator (cp_token
* token
)
8340 switch (token
->type
)
8343 return INDIRECT_REF
;
8349 return UNARY_PLUS_EXPR
;
8355 return TRUTH_NOT_EXPR
;
8358 return BIT_NOT_EXPR
;
8365 /* Parse a new-expression.
8368 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8369 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8371 Returns a representation of the expression. */
8374 cp_parser_new_expression (cp_parser
* parser
)
8376 bool global_scope_p
;
8377 vec
<tree
, va_gc
> *placement
;
8379 vec
<tree
, va_gc
> *initializer
;
8380 tree nelts
= NULL_TREE
;
8383 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8385 /* Look for the optional `::' operator. */
8387 = (cp_parser_global_scope_opt (parser
,
8388 /*current_scope_valid_p=*/false)
8390 /* Look for the `new' operator. */
8391 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8392 /* There's no easy way to tell a new-placement from the
8393 `( type-id )' construct. */
8394 cp_parser_parse_tentatively (parser
);
8395 /* Look for a new-placement. */
8396 placement
= cp_parser_new_placement (parser
);
8397 /* If that didn't work out, there's no new-placement. */
8398 if (!cp_parser_parse_definitely (parser
))
8400 if (placement
!= NULL
)
8401 release_tree_vector (placement
);
8405 /* If the next token is a `(', then we have a parenthesized
8407 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8410 const char *saved_message
= parser
->type_definition_forbidden_message
;
8412 /* Consume the `('. */
8413 matching_parens parens
;
8414 parens
.consume_open (parser
);
8416 /* Parse the type-id. */
8417 parser
->type_definition_forbidden_message
8418 = G_("types may not be defined in a new-expression");
8420 type_id_in_expr_sentinel
s (parser
);
8421 type
= cp_parser_type_id (parser
);
8423 parser
->type_definition_forbidden_message
= saved_message
;
8425 /* Look for the closing `)'. */
8426 parens
.require_close (parser
);
8427 token
= cp_lexer_peek_token (parser
->lexer
);
8428 /* There should not be a direct-new-declarator in this production,
8429 but GCC used to allowed this, so we check and emit a sensible error
8430 message for this case. */
8431 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8433 error_at (token
->location
,
8434 "array bound forbidden after parenthesized type-id");
8435 inform (token
->location
,
8436 "try removing the parentheses around the type-id");
8437 cp_parser_direct_new_declarator (parser
);
8440 /* Otherwise, there must be a new-type-id. */
8442 type
= cp_parser_new_type_id (parser
, &nelts
);
8444 /* If the next token is a `(' or '{', then we have a new-initializer. */
8445 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8446 if (token
->type
== CPP_OPEN_PAREN
8447 || token
->type
== CPP_OPEN_BRACE
)
8448 initializer
= cp_parser_new_initializer (parser
);
8452 /* A new-expression may not appear in an integral constant
8454 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8455 ret
= error_mark_node
;
8456 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8457 of a new-type-id or type-id of a new-expression, the new-expression shall
8458 contain a new-initializer of the form ( assignment-expression )".
8459 Additionally, consistently with the spirit of DR 1467, we want to accept
8460 'new auto { 2 }' too. */
8461 else if ((ret
= type_uses_auto (type
))
8462 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8463 && (vec_safe_length (initializer
) != 1
8464 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8465 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8467 error_at (token
->location
,
8468 "initialization of new-expression for type %<auto%> "
8469 "requires exactly one element");
8470 ret
= error_mark_node
;
8474 /* Construct a location e.g.:
8477 with caret == start at the start of the "new" token, and the end
8478 at the end of the final token we consumed. */
8479 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8480 location_t end_loc
= get_finish (end_tok
->location
);
8481 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8483 /* Create a representation of the new-expression. */
8484 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8485 tf_warning_or_error
);
8486 protected_set_expr_location (ret
, combined_loc
);
8489 if (placement
!= NULL
)
8490 release_tree_vector (placement
);
8491 if (initializer
!= NULL
)
8492 release_tree_vector (initializer
);
8497 /* Parse a new-placement.
8502 Returns the same representation as for an expression-list. */
8504 static vec
<tree
, va_gc
> *
8505 cp_parser_new_placement (cp_parser
* parser
)
8507 vec
<tree
, va_gc
> *expression_list
;
8509 /* Parse the expression-list. */
8510 expression_list
= (cp_parser_parenthesized_expression_list
8511 (parser
, non_attr
, /*cast_p=*/false,
8512 /*allow_expansion_p=*/true,
8513 /*non_constant_p=*/NULL
));
8515 if (expression_list
&& expression_list
->is_empty ())
8516 error ("expected expression-list or type-id");
8518 return expression_list
;
8521 /* Parse a new-type-id.
8524 type-specifier-seq new-declarator [opt]
8526 Returns the TYPE allocated. If the new-type-id indicates an array
8527 type, *NELTS is set to the number of elements in the last array
8528 bound; the TYPE will not include the last array bound. */
8531 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8533 cp_decl_specifier_seq type_specifier_seq
;
8534 cp_declarator
*new_declarator
;
8535 cp_declarator
*declarator
;
8536 cp_declarator
*outer_declarator
;
8537 const char *saved_message
;
8539 /* The type-specifier sequence must not contain type definitions.
8540 (It cannot contain declarations of new types either, but if they
8541 are not definitions we will catch that because they are not
8543 saved_message
= parser
->type_definition_forbidden_message
;
8544 parser
->type_definition_forbidden_message
8545 = G_("types may not be defined in a new-type-id");
8546 /* Parse the type-specifier-seq. */
8547 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8548 /*is_trailing_return=*/false,
8549 &type_specifier_seq
);
8550 /* Restore the old message. */
8551 parser
->type_definition_forbidden_message
= saved_message
;
8553 if (type_specifier_seq
.type
== error_mark_node
)
8554 return error_mark_node
;
8556 /* Parse the new-declarator. */
8557 new_declarator
= cp_parser_new_declarator_opt (parser
);
8559 /* Determine the number of elements in the last array dimension, if
8562 /* Skip down to the last array dimension. */
8563 declarator
= new_declarator
;
8564 outer_declarator
= NULL
;
8565 while (declarator
&& (declarator
->kind
== cdk_pointer
8566 || declarator
->kind
== cdk_ptrmem
))
8568 outer_declarator
= declarator
;
8569 declarator
= declarator
->declarator
;
8572 && declarator
->kind
== cdk_array
8573 && declarator
->declarator
8574 && declarator
->declarator
->kind
== cdk_array
)
8576 outer_declarator
= declarator
;
8577 declarator
= declarator
->declarator
;
8580 if (declarator
&& declarator
->kind
== cdk_array
)
8582 *nelts
= declarator
->u
.array
.bounds
;
8583 if (*nelts
== error_mark_node
)
8584 *nelts
= integer_one_node
;
8586 if (outer_declarator
)
8587 outer_declarator
->declarator
= declarator
->declarator
;
8589 new_declarator
= NULL
;
8592 return groktypename (&type_specifier_seq
, new_declarator
, false);
8595 /* Parse an (optional) new-declarator.
8598 ptr-operator new-declarator [opt]
8599 direct-new-declarator
8601 Returns the declarator. */
8603 static cp_declarator
*
8604 cp_parser_new_declarator_opt (cp_parser
* parser
)
8606 enum tree_code code
;
8607 tree type
, std_attributes
= NULL_TREE
;
8608 cp_cv_quals cv_quals
;
8610 /* We don't know if there's a ptr-operator next, or not. */
8611 cp_parser_parse_tentatively (parser
);
8612 /* Look for a ptr-operator. */
8613 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8614 /* If that worked, look for more new-declarators. */
8615 if (cp_parser_parse_definitely (parser
))
8617 cp_declarator
*declarator
;
8619 /* Parse another optional declarator. */
8620 declarator
= cp_parser_new_declarator_opt (parser
);
8622 declarator
= cp_parser_make_indirect_declarator
8623 (code
, type
, cv_quals
, declarator
, std_attributes
);
8628 /* If the next token is a `[', there is a direct-new-declarator. */
8629 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8630 return cp_parser_direct_new_declarator (parser
);
8635 /* Parse a direct-new-declarator.
8637 direct-new-declarator:
8639 direct-new-declarator [constant-expression]
8643 static cp_declarator
*
8644 cp_parser_direct_new_declarator (cp_parser
* parser
)
8646 cp_declarator
*declarator
= NULL
;
8653 /* Look for the opening `['. */
8654 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8656 token
= cp_lexer_peek_token (parser
->lexer
);
8657 expression
= cp_parser_expression (parser
);
8658 /* The standard requires that the expression have integral
8659 type. DR 74 adds enumeration types. We believe that the
8660 real intent is that these expressions be handled like the
8661 expression in a `switch' condition, which also allows
8662 classes with a single conversion to integral or
8663 enumeration type. */
8664 if (!processing_template_decl
)
8667 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8672 error_at (token
->location
,
8673 "expression in new-declarator must have integral "
8674 "or enumeration type");
8675 expression
= error_mark_node
;
8679 /* Look for the closing `]'. */
8680 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8682 /* Add this bound to the declarator. */
8683 declarator
= make_array_declarator (declarator
, expression
);
8685 /* If the next token is not a `[', then there are no more
8687 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8694 /* Parse a new-initializer.
8697 ( expression-list [opt] )
8700 Returns a representation of the expression-list. */
8702 static vec
<tree
, va_gc
> *
8703 cp_parser_new_initializer (cp_parser
* parser
)
8705 vec
<tree
, va_gc
> *expression_list
;
8707 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8710 bool expr_non_constant_p
;
8711 cp_lexer_set_source_position (parser
->lexer
);
8712 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8713 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8714 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8715 expression_list
= make_tree_vector_single (t
);
8718 expression_list
= (cp_parser_parenthesized_expression_list
8719 (parser
, non_attr
, /*cast_p=*/false,
8720 /*allow_expansion_p=*/true,
8721 /*non_constant_p=*/NULL
));
8723 return expression_list
;
8726 /* Parse a delete-expression.
8729 :: [opt] delete cast-expression
8730 :: [opt] delete [ ] cast-expression
8732 Returns a representation of the expression. */
8735 cp_parser_delete_expression (cp_parser
* parser
)
8737 bool global_scope_p
;
8741 /* Look for the optional `::' operator. */
8743 = (cp_parser_global_scope_opt (parser
,
8744 /*current_scope_valid_p=*/false)
8746 /* Look for the `delete' keyword. */
8747 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8748 /* See if the array syntax is in use. */
8749 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8751 /* Consume the `[' token. */
8752 cp_lexer_consume_token (parser
->lexer
);
8753 /* Look for the `]' token. */
8754 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8755 /* Remember that this is the `[]' construct. */
8761 /* Parse the cast-expression. */
8762 expression
= cp_parser_simple_cast_expression (parser
);
8764 /* A delete-expression may not appear in an integral constant
8766 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8767 return error_mark_node
;
8769 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8770 tf_warning_or_error
);
8773 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8774 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8778 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8780 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8781 switch (token
->type
)
8787 case CPP_CLOSE_SQUARE
:
8788 case CPP_CLOSE_PAREN
:
8789 case CPP_CLOSE_BRACE
:
8790 case CPP_OPEN_BRACE
:
8794 case CPP_DEREF_STAR
:
8802 case CPP_GREATER_EQ
:
8823 case CPP_OPEN_PAREN
:
8824 /* In ((type ()) () the last () isn't a valid cast-expression,
8825 so the whole must be parsed as postfix-expression. */
8826 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8829 case CPP_OPEN_SQUARE
:
8830 /* '[' may start a primary-expression in obj-c++ and in C++11,
8831 as a lambda-expression, eg, '(void)[]{}'. */
8832 if (cxx_dialect
>= cxx11
)
8834 return c_dialect_objc ();
8837 case CPP_MINUS_MINUS
:
8838 /* '++' and '--' may or may not start a cast-expression:
8840 struct T { void operator++(int); };
8841 void f() { (T())++; }
8854 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8855 in the order: const_cast, static_cast, reinterpret_cast.
8857 Don't suggest dynamic_cast.
8859 Return the first legal cast kind found, or NULL otherwise. */
8862 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8866 /* Reuse the parser logic by attempting to build the various kinds of
8867 cast, with "complain" disabled.
8868 Identify the first such cast that is valid. */
8870 /* Don't attempt to run such logic within template processing. */
8871 if (processing_template_decl
)
8874 /* First try const_cast. */
8875 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8876 if (trial
!= error_mark_node
)
8877 return "const_cast";
8879 /* If that fails, try static_cast. */
8880 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8881 if (trial
!= error_mark_node
)
8882 return "static_cast";
8884 /* Finally, try reinterpret_cast. */
8885 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8886 if (trial
!= error_mark_node
)
8887 return "reinterpret_cast";
8889 /* No such cast possible. */
8893 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8894 suggesting how to convert a C-style cast of the form:
8898 to a C++-style cast.
8900 The primary range of RICHLOC is asssumed to be that of the original
8901 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8902 of the parens in the C-style cast. */
8905 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8906 location_t close_paren_loc
, tree orig_expr
,
8909 /* This function is non-trivial, so bail out now if the warning isn't
8910 going to be emitted. */
8911 if (!warn_old_style_cast
)
8914 /* Try to find a legal C++ cast, trying them in order:
8915 const_cast, static_cast, reinterpret_cast. */
8916 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8917 if (!cast_suggestion
)
8920 /* Replace the open paren with "CAST_SUGGESTION<". */
8922 pp_printf (&pp
, "%s<", cast_suggestion
);
8923 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8925 /* Replace the close paren with "> (". */
8926 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8928 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8929 rich_loc
->add_fixit_insert_after (")");
8933 /* Parse a cast-expression.
8937 ( type-id ) cast-expression
8939 ADDRESS_P is true iff the unary-expression is appearing as the
8940 operand of the `&' operator. CAST_P is true if this expression is
8941 the target of a cast.
8943 Returns a representation of the expression. */
8946 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8947 bool decltype_p
, cp_id_kind
* pidk
)
8949 /* If it's a `(', then we might be looking at a cast. */
8950 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8952 tree type
= NULL_TREE
;
8953 cp_expr
expr (NULL_TREE
);
8954 int cast_expression
= 0;
8955 const char *saved_message
;
8957 /* There's no way to know yet whether or not this is a cast.
8958 For example, `(int (3))' is a unary-expression, while `(int)
8959 3' is a cast. So, we resort to parsing tentatively. */
8960 cp_parser_parse_tentatively (parser
);
8961 /* Types may not be defined in a cast. */
8962 saved_message
= parser
->type_definition_forbidden_message
;
8963 parser
->type_definition_forbidden_message
8964 = G_("types may not be defined in casts");
8965 /* Consume the `('. */
8966 matching_parens parens
;
8967 cp_token
*open_paren
= parens
.consume_open (parser
);
8968 location_t open_paren_loc
= open_paren
->location
;
8969 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8971 /* A very tricky bit is that `(struct S) { 3 }' is a
8972 compound-literal (which we permit in C++ as an extension).
8973 But, that construct is not a cast-expression -- it is a
8974 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8975 is legal; if the compound-literal were a cast-expression,
8976 you'd need an extra set of parentheses.) But, if we parse
8977 the type-id, and it happens to be a class-specifier, then we
8978 will commit to the parse at that point, because we cannot
8979 undo the action that is done when creating a new class. So,
8980 then we cannot back up and do a postfix-expression.
8982 Another tricky case is the following (c++/29234):
8984 struct S { void operator () (); };
8991 As a type-id we parse the parenthesized S()() as a function
8992 returning a function, groktypename complains and we cannot
8993 back up in this case either.
8995 Therefore, we scan ahead to the closing `)', and check to see
8996 if the tokens after the `)' can start a cast-expression. Otherwise
8997 we are dealing with an unary-expression, a postfix-expression
9000 Yet another tricky case, in C++11, is the following (c++/54891):
9004 The issue is that usually, besides the case of lambda-expressions,
9005 the parenthesized type-id cannot be followed by '[', and, eg, we
9006 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9007 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9008 we don't commit, we try a cast-expression, then an unary-expression.
9010 Save tokens so that we can put them back. */
9011 cp_lexer_save_tokens (parser
->lexer
);
9013 /* We may be looking at a cast-expression. */
9014 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9015 /*consume_paren=*/true))
9017 = cp_parser_tokens_start_cast_expression (parser
);
9019 /* Roll back the tokens we skipped. */
9020 cp_lexer_rollback_tokens (parser
->lexer
);
9021 /* If we aren't looking at a cast-expression, simulate an error so
9022 that the call to cp_parser_error_occurred below returns true. */
9023 if (!cast_expression
)
9024 cp_parser_simulate_error (parser
);
9027 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9028 parser
->in_type_id_in_expr_p
= true;
9029 /* Look for the type-id. */
9030 type
= cp_parser_type_id (parser
);
9031 /* Look for the closing `)'. */
9032 cp_token
*close_paren
= parens
.require_close (parser
);
9034 close_paren_loc
= close_paren
->location
;
9035 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9038 /* Restore the saved message. */
9039 parser
->type_definition_forbidden_message
= saved_message
;
9041 /* At this point this can only be either a cast or a
9042 parenthesized ctor such as `(T ())' that looks like a cast to
9043 function returning T. */
9044 if (!cp_parser_error_occurred (parser
))
9046 /* Only commit if the cast-expression doesn't start with
9047 '++', '--', or '[' in C++11. */
9048 if (cast_expression
> 0)
9049 cp_parser_commit_to_topmost_tentative_parse (parser
);
9051 expr
= cp_parser_cast_expression (parser
,
9052 /*address_p=*/false,
9054 /*decltype_p=*/false,
9057 if (cp_parser_parse_definitely (parser
))
9059 /* Warn about old-style casts, if so requested. */
9060 if (warn_old_style_cast
9061 && !in_system_header_at (input_location
)
9062 && !VOID_TYPE_P (type
)
9063 && current_lang_name
!= lang_name_c
)
9065 gcc_rich_location
rich_loc (input_location
);
9066 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9068 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9069 "use of old-style cast to %q#T", type
);
9072 /* Only type conversions to integral or enumeration types
9073 can be used in constant-expressions. */
9074 if (!cast_valid_in_integral_constant_expression_p (type
)
9075 && cp_parser_non_integral_constant_expression (parser
,
9077 return error_mark_node
;
9079 /* Perform the cast. */
9083 with start==caret at the open paren, extending to the
9085 location_t cast_loc
= make_location (open_paren_loc
,
9087 expr
.get_finish ());
9088 expr
= build_c_cast (cast_loc
, type
, expr
);
9093 cp_parser_abort_tentative_parse (parser
);
9096 /* If we get here, then it's not a cast, so it must be a
9097 unary-expression. */
9098 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9099 cast_p
, decltype_p
);
9102 /* Parse a binary expression of the general form:
9106 pm-expression .* cast-expression
9107 pm-expression ->* cast-expression
9109 multiplicative-expression:
9111 multiplicative-expression * pm-expression
9112 multiplicative-expression / pm-expression
9113 multiplicative-expression % pm-expression
9115 additive-expression:
9116 multiplicative-expression
9117 additive-expression + multiplicative-expression
9118 additive-expression - multiplicative-expression
9122 shift-expression << additive-expression
9123 shift-expression >> additive-expression
9125 relational-expression:
9127 relational-expression < shift-expression
9128 relational-expression > shift-expression
9129 relational-expression <= shift-expression
9130 relational-expression >= shift-expression
9134 relational-expression:
9135 relational-expression <? shift-expression
9136 relational-expression >? shift-expression
9138 equality-expression:
9139 relational-expression
9140 equality-expression == relational-expression
9141 equality-expression != relational-expression
9145 and-expression & equality-expression
9147 exclusive-or-expression:
9149 exclusive-or-expression ^ and-expression
9151 inclusive-or-expression:
9152 exclusive-or-expression
9153 inclusive-or-expression | exclusive-or-expression
9155 logical-and-expression:
9156 inclusive-or-expression
9157 logical-and-expression && inclusive-or-expression
9159 logical-or-expression:
9160 logical-and-expression
9161 logical-or-expression || logical-and-expression
9163 All these are implemented with a single function like:
9166 simple-cast-expression
9167 binary-expression <token> binary-expression
9169 CAST_P is true if this expression is the target of a cast.
9171 The binops_by_token map is used to get the tree codes for each <token> type.
9172 binary-expressions are associated according to a precedence table. */
9174 #define TOKEN_PRECEDENCE(token) \
9175 (((token->type == CPP_GREATER \
9176 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9177 && !parser->greater_than_is_operator_p) \
9178 ? PREC_NOT_OPERATOR \
9179 : binops_by_token[token->type].prec)
9182 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9183 bool no_toplevel_fold_p
,
9185 enum cp_parser_prec prec
,
9188 cp_parser_expression_stack stack
;
9189 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9190 cp_parser_expression_stack_entry current
;
9193 enum tree_code rhs_type
;
9194 enum cp_parser_prec new_prec
, lookahead_prec
;
9197 /* Parse the first expression. */
9198 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9199 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9200 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9201 cast_p
, decltype_p
, pidk
);
9202 current
.prec
= prec
;
9204 if (cp_parser_error_occurred (parser
))
9205 return error_mark_node
;
9209 /* Get an operator token. */
9210 token
= cp_lexer_peek_token (parser
->lexer
);
9212 if (warn_cxx11_compat
9213 && token
->type
== CPP_RSHIFT
9214 && !parser
->greater_than_is_operator_p
)
9216 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9217 "%<>>%> operator is treated"
9218 " as two right angle brackets in C++11"))
9219 inform (token
->location
,
9220 "suggest parentheses around %<>>%> expression");
9223 new_prec
= TOKEN_PRECEDENCE (token
);
9224 if (new_prec
!= PREC_NOT_OPERATOR
9225 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9226 /* This is a fold-expression; handle it later. */
9227 new_prec
= PREC_NOT_OPERATOR
;
9229 /* Popping an entry off the stack means we completed a subexpression:
9230 - either we found a token which is not an operator (`>' where it is not
9231 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9232 will happen repeatedly;
9233 - or, we found an operator which has lower priority. This is the case
9234 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9236 if (new_prec
<= current
.prec
)
9245 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9246 current
.loc
= token
->location
;
9248 /* We used the operator token. */
9249 cp_lexer_consume_token (parser
->lexer
);
9251 /* For "false && x" or "true || x", x will never be executed;
9252 disable warnings while evaluating it. */
9253 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9254 c_inhibit_evaluation_warnings
+=
9255 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9256 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9257 c_inhibit_evaluation_warnings
+=
9258 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9260 /* Extract another operand. It may be the RHS of this expression
9261 or the LHS of a new, higher priority expression. */
9262 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9263 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9264 rhs
= cp_parser_simple_cast_expression (parser
);
9266 /* Get another operator token. Look up its precedence to avoid
9267 building a useless (immediately popped) stack entry for common
9268 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9269 token
= cp_lexer_peek_token (parser
->lexer
);
9270 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9271 if (lookahead_prec
!= PREC_NOT_OPERATOR
9272 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9273 lookahead_prec
= PREC_NOT_OPERATOR
;
9274 if (lookahead_prec
> new_prec
)
9276 /* ... and prepare to parse the RHS of the new, higher priority
9277 expression. Since precedence levels on the stack are
9278 monotonically increasing, we do not have to care about
9283 current
.lhs_type
= rhs_type
;
9284 current
.prec
= new_prec
;
9285 new_prec
= lookahead_prec
;
9289 lookahead_prec
= new_prec
;
9290 /* If the stack is not empty, we have parsed into LHS the right side
9291 (`4' in the example above) of an expression we had suspended.
9292 We can use the information on the stack to recover the LHS (`3')
9293 from the stack together with the tree code (`MULT_EXPR'), and
9294 the precedence of the higher level subexpression
9295 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9296 which will be used to actually build the additive expression. */
9298 rhs_type
= current
.lhs_type
;
9303 /* Undo the disabling of warnings done above. */
9304 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9305 c_inhibit_evaluation_warnings
-=
9306 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9307 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9308 c_inhibit_evaluation_warnings
-=
9309 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9311 if (warn_logical_not_paren
9312 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9313 && current
.lhs_type
== TRUTH_NOT_EXPR
9314 /* Avoid warning for !!x == y. */
9315 && (TREE_CODE (current
.lhs
) != NE_EXPR
9316 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9317 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9318 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9319 /* Avoid warning for !b == y where b is boolean. */
9320 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9321 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9323 /* Avoid warning for !!b == y where b is boolean. */
9324 && (!DECL_P (current
.lhs
)
9325 || TREE_TYPE (current
.lhs
) == NULL_TREE
9326 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9327 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9328 current
.lhs
, maybe_constant_value (rhs
));
9332 location_t combined_loc
= make_location (current
.loc
,
9333 current
.lhs
.get_start (),
9336 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9337 ERROR_MARK for everything that is not a binary expression.
9338 This makes warn_about_parentheses miss some warnings that
9339 involve unary operators. For unary expressions we should
9340 pass the correct tree_code unless the unary expression was
9341 surrounded by parentheses.
9343 if (no_toplevel_fold_p
9344 && lookahead_prec
<= current
.prec
9347 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9348 current
.lhs
= error_mark_node
;
9352 = build_min (current
.tree_type
,
9353 TREE_CODE_CLASS (current
.tree_type
)
9355 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9356 current
.lhs
.get_value (), rhs
.get_value ());
9357 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9362 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9363 current
.lhs
, current
.lhs_type
,
9364 rhs
, rhs_type
, &overload
,
9365 complain_flags (decltype_p
));
9366 /* TODO: build_x_binary_op doesn't always honor the location. */
9367 current
.lhs
.set_location (combined_loc
);
9369 current
.lhs_type
= current
.tree_type
;
9371 /* If the binary operator required the use of an overloaded operator,
9372 then this expression cannot be an integral constant-expression.
9373 An overloaded operator can be used even if both operands are
9374 otherwise permissible in an integral constant-expression if at
9375 least one of the operands is of enumeration type. */
9378 && cp_parser_non_integral_constant_expression (parser
,
9380 return error_mark_node
;
9387 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9388 bool no_toplevel_fold_p
,
9389 enum cp_parser_prec prec
,
9392 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9393 /*decltype*/false, prec
, pidk
);
9396 /* Parse the `? expression : assignment-expression' part of a
9397 conditional-expression. The LOGICAL_OR_EXPR is the
9398 logical-or-expression that started the conditional-expression.
9399 Returns a representation of the entire conditional-expression.
9401 This routine is used by cp_parser_assignment_expression.
9403 ? expression : assignment-expression
9407 ? : assignment-expression */
9410 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9412 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9413 cp_expr assignment_expr
;
9414 struct cp_token
*token
;
9415 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9417 /* Consume the `?' token. */
9418 cp_lexer_consume_token (parser
->lexer
);
9419 token
= cp_lexer_peek_token (parser
->lexer
);
9420 if (cp_parser_allow_gnu_extensions_p (parser
)
9421 && token
->type
== CPP_COLON
)
9423 pedwarn (token
->location
, OPT_Wpedantic
,
9424 "ISO C++ does not allow ?: with omitted middle operand");
9425 /* Implicit true clause. */
9427 c_inhibit_evaluation_warnings
+=
9428 folded_logical_or_expr
== truthvalue_true_node
;
9429 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9433 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9434 parser
->colon_corrects_to_scope_p
= false;
9435 /* Parse the expression. */
9436 c_inhibit_evaluation_warnings
+=
9437 folded_logical_or_expr
== truthvalue_false_node
;
9438 expr
= cp_parser_expression (parser
);
9439 c_inhibit_evaluation_warnings
+=
9440 ((folded_logical_or_expr
== truthvalue_true_node
)
9441 - (folded_logical_or_expr
== truthvalue_false_node
));
9442 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9445 /* The next token should be a `:'. */
9446 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9447 /* Parse the assignment-expression. */
9448 assignment_expr
= cp_parser_assignment_expression (parser
);
9449 c_inhibit_evaluation_warnings
-=
9450 folded_logical_or_expr
== truthvalue_true_node
;
9453 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9454 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9455 with the caret at the "?", ranging from the start of
9456 the logical_or_expr to the end of the assignment_expr. */
9457 loc
= make_location (loc
,
9458 logical_or_expr
.get_start (),
9459 assignment_expr
.get_finish ());
9461 /* Build the conditional-expression. */
9462 return build_x_conditional_expr (loc
, logical_or_expr
,
9465 tf_warning_or_error
);
9468 /* Parse an assignment-expression.
9470 assignment-expression:
9471 conditional-expression
9472 logical-or-expression assignment-operator assignment_expression
9475 CAST_P is true if this expression is the target of a cast.
9476 DECLTYPE_P is true if this expression is the operand of decltype.
9478 Returns a representation for the expression. */
9481 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9482 bool cast_p
, bool decltype_p
)
9486 /* If the next token is the `throw' keyword, then we're looking at
9487 a throw-expression. */
9488 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9489 expr
= cp_parser_throw_expression (parser
);
9490 /* Otherwise, it must be that we are looking at a
9491 logical-or-expression. */
9494 /* Parse the binary expressions (logical-or-expression). */
9495 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9497 PREC_NOT_OPERATOR
, pidk
);
9498 /* If the next token is a `?' then we're actually looking at a
9499 conditional-expression. */
9500 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9501 return cp_parser_question_colon_clause (parser
, expr
);
9504 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9506 /* If it's an assignment-operator, we're using the second
9508 enum tree_code assignment_operator
9509 = cp_parser_assignment_operator_opt (parser
);
9510 if (assignment_operator
!= ERROR_MARK
)
9512 bool non_constant_p
;
9514 /* Parse the right-hand side of the assignment. */
9515 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9518 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9519 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9521 /* An assignment may not appear in a
9522 constant-expression. */
9523 if (cp_parser_non_integral_constant_expression (parser
,
9525 return error_mark_node
;
9526 /* Build the assignment expression. Its default
9530 is the location of the '=' token as the
9531 caret, ranging from the start of the lhs to the
9533 loc
= make_location (loc
,
9536 expr
= build_x_modify_expr (loc
, expr
,
9537 assignment_operator
,
9539 complain_flags (decltype_p
));
9540 /* TODO: build_x_modify_expr doesn't honor the location,
9541 so we must set it here. */
9542 expr
.set_location (loc
);
9550 /* Parse an (optional) assignment-operator.
9552 assignment-operator: one of
9553 = *= /= %= += -= >>= <<= &= ^= |=
9557 assignment-operator: one of
9560 If the next token is an assignment operator, the corresponding tree
9561 code is returned, and the token is consumed. For example, for
9562 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9563 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9564 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9565 operator, ERROR_MARK is returned. */
9567 static enum tree_code
9568 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9573 /* Peek at the next token. */
9574 token
= cp_lexer_peek_token (parser
->lexer
);
9576 switch (token
->type
)
9587 op
= TRUNC_DIV_EXPR
;
9591 op
= TRUNC_MOD_EXPR
;
9623 /* Nothing else is an assignment operator. */
9627 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9628 if (op
!= ERROR_MARK
9629 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9632 /* If it was an assignment operator, consume it. */
9633 if (op
!= ERROR_MARK
)
9634 cp_lexer_consume_token (parser
->lexer
);
9639 /* Parse an expression.
9642 assignment-expression
9643 expression , assignment-expression
9645 CAST_P is true if this expression is the target of a cast.
9646 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9647 except possibly parenthesized or on the RHS of a comma (N3276).
9649 Returns a representation of the expression. */
9652 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9653 bool cast_p
, bool decltype_p
)
9655 cp_expr expression
= NULL_TREE
;
9656 location_t loc
= UNKNOWN_LOCATION
;
9660 cp_expr assignment_expression
;
9662 /* Parse the next assignment-expression. */
9663 assignment_expression
9664 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9666 /* We don't create a temporary for a call that is the immediate operand
9667 of decltype or on the RHS of a comma. But when we see a comma, we
9668 need to create a temporary for a call on the LHS. */
9669 if (decltype_p
&& !processing_template_decl
9670 && TREE_CODE (assignment_expression
) == CALL_EXPR
9671 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9672 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9673 assignment_expression
9674 = build_cplus_new (TREE_TYPE (assignment_expression
),
9675 assignment_expression
, tf_warning_or_error
);
9677 /* If this is the first assignment-expression, we can just
9680 expression
= assignment_expression
;
9683 /* Create a location with caret at the comma, ranging
9684 from the start of the LHS to the end of the RHS. */
9685 loc
= make_location (loc
,
9686 expression
.get_start (),
9687 assignment_expression
.get_finish ());
9688 expression
= build_x_compound_expr (loc
, expression
,
9689 assignment_expression
,
9690 complain_flags (decltype_p
));
9691 expression
.set_location (loc
);
9693 /* If the next token is not a comma, or we're in a fold-expression, then
9694 we are done with the expression. */
9695 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9696 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9698 /* Consume the `,'. */
9699 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9700 cp_lexer_consume_token (parser
->lexer
);
9701 /* A comma operator cannot appear in a constant-expression. */
9702 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9703 expression
= error_mark_node
;
9709 /* Parse a constant-expression.
9711 constant-expression:
9712 conditional-expression
9714 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9715 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9716 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9717 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9718 only parse a conditional-expression, otherwise parse an
9719 assignment-expression. See below for rationale. */
9722 cp_parser_constant_expression (cp_parser
* parser
,
9723 bool allow_non_constant_p
,
9724 bool *non_constant_p
,
9727 bool saved_integral_constant_expression_p
;
9728 bool saved_allow_non_integral_constant_expression_p
;
9729 bool saved_non_integral_constant_expression_p
;
9732 /* It might seem that we could simply parse the
9733 conditional-expression, and then check to see if it were
9734 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9735 one that the compiler can figure out is constant, possibly after
9736 doing some simplifications or optimizations. The standard has a
9737 precise definition of constant-expression, and we must honor
9738 that, even though it is somewhat more restrictive.
9744 is not a legal declaration, because `(2, 3)' is not a
9745 constant-expression. The `,' operator is forbidden in a
9746 constant-expression. However, GCC's constant-folding machinery
9747 will fold this operation to an INTEGER_CST for `3'. */
9749 /* Save the old settings. */
9750 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9751 saved_allow_non_integral_constant_expression_p
9752 = parser
->allow_non_integral_constant_expression_p
;
9753 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9754 /* We are now parsing a constant-expression. */
9755 parser
->integral_constant_expression_p
= true;
9756 parser
->allow_non_integral_constant_expression_p
9757 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9758 parser
->non_integral_constant_expression_p
= false;
9759 /* Although the grammar says "conditional-expression", when not STRICT_P,
9760 we parse an "assignment-expression", which also permits
9761 "throw-expression" and the use of assignment operators. In the case
9762 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9763 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9764 actually essential that we look for an assignment-expression.
9765 For example, cp_parser_initializer_clauses uses this function to
9766 determine whether a particular assignment-expression is in fact
9770 /* Parse the binary expressions (logical-or-expression). */
9771 expression
= cp_parser_binary_expression (parser
, false, false, false,
9772 PREC_NOT_OPERATOR
, NULL
);
9773 /* If the next token is a `?' then we're actually looking at
9774 a conditional-expression; otherwise we're done. */
9775 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9776 expression
= cp_parser_question_colon_clause (parser
, expression
);
9779 expression
= cp_parser_assignment_expression (parser
);
9780 /* Restore the old settings. */
9781 parser
->integral_constant_expression_p
9782 = saved_integral_constant_expression_p
;
9783 parser
->allow_non_integral_constant_expression_p
9784 = saved_allow_non_integral_constant_expression_p
;
9785 if (cxx_dialect
>= cxx11
)
9787 /* Require an rvalue constant expression here; that's what our
9788 callers expect. Reference constant expressions are handled
9789 separately in e.g. cp_parser_template_argument. */
9790 tree decay
= expression
;
9791 if (TREE_TYPE (expression
)
9792 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9793 decay
= build_address (expression
);
9794 bool is_const
= potential_rvalue_constant_expression (decay
);
9795 parser
->non_integral_constant_expression_p
= !is_const
;
9796 if (!is_const
&& !allow_non_constant_p
)
9797 require_potential_rvalue_constant_expression (decay
);
9799 if (allow_non_constant_p
)
9800 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9801 parser
->non_integral_constant_expression_p
9802 = saved_non_integral_constant_expression_p
;
9807 /* Parse __builtin_offsetof.
9809 offsetof-expression:
9810 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9812 offsetof-member-designator:
9814 | offsetof-member-designator "." id-expression
9815 | offsetof-member-designator "[" expression "]"
9816 | offsetof-member-designator "->" id-expression */
9819 cp_parser_builtin_offsetof (cp_parser
*parser
)
9821 int save_ice_p
, save_non_ice_p
;
9826 location_t finish_loc
;
9828 /* We're about to accept non-integral-constant things, but will
9829 definitely yield an integral constant expression. Save and
9830 restore these values around our local parsing. */
9831 save_ice_p
= parser
->integral_constant_expression_p
;
9832 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9834 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9836 /* Consume the "__builtin_offsetof" token. */
9837 cp_lexer_consume_token (parser
->lexer
);
9838 /* Consume the opening `('. */
9839 matching_parens parens
;
9840 parens
.require_open (parser
);
9841 /* Parse the type-id. */
9842 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9844 const char *saved_message
= parser
->type_definition_forbidden_message
;
9845 parser
->type_definition_forbidden_message
9846 = G_("types may not be defined within __builtin_offsetof");
9847 type
= cp_parser_type_id (parser
);
9848 parser
->type_definition_forbidden_message
= saved_message
;
9850 /* Look for the `,'. */
9851 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9852 token
= cp_lexer_peek_token (parser
->lexer
);
9854 /* Build the (type *)null that begins the traditional offsetof macro. */
9856 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9857 tf_warning_or_error
);
9859 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9860 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9861 true, &dummy
, token
->location
);
9864 token
= cp_lexer_peek_token (parser
->lexer
);
9865 switch (token
->type
)
9867 case CPP_OPEN_SQUARE
:
9868 /* offsetof-member-designator "[" expression "]" */
9869 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9874 /* offsetof-member-designator "->" identifier */
9875 expr
= grok_array_decl (token
->location
, expr
,
9876 integer_zero_node
, false);
9880 /* offsetof-member-designator "." identifier */
9881 cp_lexer_consume_token (parser
->lexer
);
9882 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9887 case CPP_CLOSE_PAREN
:
9888 /* Consume the ")" token. */
9889 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9890 cp_lexer_consume_token (parser
->lexer
);
9894 /* Error. We know the following require will fail, but
9895 that gives the proper error message. */
9896 parens
.require_close (parser
);
9897 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9898 expr
= error_mark_node
;
9904 /* Make a location of the form:
9905 __builtin_offsetof (struct s, f)
9906 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9907 with caret at the type-id, ranging from the start of the
9908 "_builtin_offsetof" token to the close paren. */
9909 loc
= make_location (loc
, start_loc
, finish_loc
);
9910 /* The result will be an INTEGER_CST, so we need to explicitly
9911 preserve the location. */
9912 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9915 parser
->integral_constant_expression_p
= save_ice_p
;
9916 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9918 expr
= expr
.maybe_add_location_wrapper ();
9922 /* Parse a trait expression.
9924 Returns a representation of the expression, the underlying type
9925 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9928 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9931 tree type1
, type2
= NULL_TREE
;
9932 bool binary
= false;
9933 bool variadic
= false;
9937 case RID_HAS_NOTHROW_ASSIGN
:
9938 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9940 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9941 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9943 case RID_HAS_NOTHROW_COPY
:
9944 kind
= CPTK_HAS_NOTHROW_COPY
;
9946 case RID_HAS_TRIVIAL_ASSIGN
:
9947 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9949 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9950 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9952 case RID_HAS_TRIVIAL_COPY
:
9953 kind
= CPTK_HAS_TRIVIAL_COPY
;
9955 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9956 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9958 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9959 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9961 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9962 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9964 case RID_IS_ABSTRACT
:
9965 kind
= CPTK_IS_ABSTRACT
;
9967 case RID_IS_AGGREGATE
:
9968 kind
= CPTK_IS_AGGREGATE
;
9970 case RID_IS_BASE_OF
:
9971 kind
= CPTK_IS_BASE_OF
;
9975 kind
= CPTK_IS_CLASS
;
9978 kind
= CPTK_IS_EMPTY
;
9981 kind
= CPTK_IS_ENUM
;
9984 kind
= CPTK_IS_FINAL
;
9986 case RID_IS_LITERAL_TYPE
:
9987 kind
= CPTK_IS_LITERAL_TYPE
;
9992 case RID_IS_POLYMORPHIC
:
9993 kind
= CPTK_IS_POLYMORPHIC
;
9995 case RID_IS_SAME_AS
:
9996 kind
= CPTK_IS_SAME_AS
;
9999 case RID_IS_STD_LAYOUT
:
10000 kind
= CPTK_IS_STD_LAYOUT
;
10002 case RID_IS_TRIVIAL
:
10003 kind
= CPTK_IS_TRIVIAL
;
10005 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10006 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10009 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10010 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10013 case RID_IS_TRIVIALLY_COPYABLE
:
10014 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10017 kind
= CPTK_IS_UNION
;
10019 case RID_UNDERLYING_TYPE
:
10020 kind
= CPTK_UNDERLYING_TYPE
;
10025 case RID_DIRECT_BASES
:
10026 kind
= CPTK_DIRECT_BASES
;
10028 case RID_IS_ASSIGNABLE
:
10029 kind
= CPTK_IS_ASSIGNABLE
;
10032 case RID_IS_CONSTRUCTIBLE
:
10033 kind
= CPTK_IS_CONSTRUCTIBLE
;
10037 gcc_unreachable ();
10040 /* Get location of initial token. */
10041 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10043 /* Consume the token. */
10044 cp_lexer_consume_token (parser
->lexer
);
10046 matching_parens parens
;
10047 parens
.require_open (parser
);
10050 type_id_in_expr_sentinel
s (parser
);
10051 type1
= cp_parser_type_id (parser
);
10054 if (type1
== error_mark_node
)
10055 return error_mark_node
;
10059 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10062 type_id_in_expr_sentinel
s (parser
);
10063 type2
= cp_parser_type_id (parser
);
10066 if (type2
== error_mark_node
)
10067 return error_mark_node
;
10071 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10073 cp_lexer_consume_token (parser
->lexer
);
10074 tree elt
= cp_parser_type_id (parser
);
10075 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10077 cp_lexer_consume_token (parser
->lexer
);
10078 elt
= make_pack_expansion (elt
);
10080 if (elt
== error_mark_node
)
10081 return error_mark_node
;
10082 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10086 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10087 parens
.require_close (parser
);
10089 /* Construct a location of the form:
10090 __is_trivially_copyable(_Tp)
10091 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10092 with start == caret, finishing at the close-paren. */
10093 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10095 /* Complete the trait expression, which may mean either processing
10096 the trait expr now or saving it for template instantiation. */
10099 case CPTK_UNDERLYING_TYPE
:
10100 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10102 return cp_expr (finish_bases (type1
, false), trait_loc
);
10103 case CPTK_DIRECT_BASES
:
10104 return cp_expr (finish_bases (type1
, true), trait_loc
);
10106 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10110 /* Parse a lambda expression.
10113 lambda-introducer lambda-declarator [opt] compound-statement
10115 Returns a representation of the expression. */
10118 cp_parser_lambda_expression (cp_parser
* parser
)
10120 tree lambda_expr
= build_lambda_expr ();
10123 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10124 cp_token_position start
= 0;
10126 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10128 if (cp_unevaluated_operand
)
10130 if (!token
->error_reported
)
10132 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10133 "lambda-expression in unevaluated context");
10134 token
->error_reported
= true;
10138 else if (parser
->in_template_argument_list_p
)
10140 if (!token
->error_reported
)
10142 error_at (token
->location
, "lambda-expression in template-argument");
10143 token
->error_reported
= true;
10148 /* We may be in the middle of deferred access check. Disable
10150 push_deferring_access_checks (dk_no_deferred
);
10152 cp_parser_lambda_introducer (parser
, lambda_expr
);
10154 type
= begin_lambda_type (lambda_expr
);
10155 if (type
== error_mark_node
)
10156 return error_mark_node
;
10158 record_lambda_scope (lambda_expr
);
10160 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10161 determine_visibility (TYPE_NAME (type
));
10163 /* Now that we've started the type, add the capture fields for any
10164 explicit captures. */
10165 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10168 /* Inside the class, surrounding template-parameter-lists do not apply. */
10169 unsigned int saved_num_template_parameter_lists
10170 = parser
->num_template_parameter_lists
;
10171 unsigned char in_statement
= parser
->in_statement
;
10172 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10173 bool fully_implicit_function_template_p
10174 = parser
->fully_implicit_function_template_p
;
10175 tree implicit_template_parms
= parser
->implicit_template_parms
;
10176 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10177 bool auto_is_implicit_function_template_parm_p
10178 = parser
->auto_is_implicit_function_template_parm_p
;
10180 parser
->num_template_parameter_lists
= 0;
10181 parser
->in_statement
= 0;
10182 parser
->in_switch_statement_p
= false;
10183 parser
->fully_implicit_function_template_p
= false;
10184 parser
->implicit_template_parms
= 0;
10185 parser
->implicit_template_scope
= 0;
10186 parser
->auto_is_implicit_function_template_parm_p
= false;
10188 /* By virtue of defining a local class, a lambda expression has access to
10189 the private variables of enclosing classes. */
10191 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10193 if (ok
&& cp_parser_error_occurred (parser
))
10198 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10199 && cp_parser_start_tentative_firewall (parser
))
10201 cp_parser_lambda_body (parser
, lambda_expr
);
10203 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10205 if (cp_parser_skip_to_closing_brace (parser
))
10206 cp_lexer_consume_token (parser
->lexer
);
10209 /* The capture list was built up in reverse order; fix that now. */
10210 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10211 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10214 maybe_add_lambda_conv_op (type
);
10216 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10218 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10219 parser
->in_statement
= in_statement
;
10220 parser
->in_switch_statement_p
= in_switch_statement_p
;
10221 parser
->fully_implicit_function_template_p
10222 = fully_implicit_function_template_p
;
10223 parser
->implicit_template_parms
= implicit_template_parms
;
10224 parser
->implicit_template_scope
= implicit_template_scope
;
10225 parser
->auto_is_implicit_function_template_parm_p
10226 = auto_is_implicit_function_template_parm_p
;
10229 /* This field is only used during parsing of the lambda. */
10230 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10232 /* This lambda shouldn't have any proxies left at this point. */
10233 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10234 /* And now that we're done, push proxies for an enclosing lambda. */
10235 insert_pending_capture_proxies ();
10237 /* Update the lambda expression to a range. */
10238 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10239 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10241 end_tok
->location
);
10244 lambda_expr
= build_lambda_object (lambda_expr
);
10246 lambda_expr
= error_mark_node
;
10248 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10250 pop_deferring_access_checks ();
10252 return lambda_expr
;
10255 /* Parse the beginning of a lambda expression.
10258 [ lambda-capture [opt] ]
10260 LAMBDA_EXPR is the current representation of the lambda expression. */
10263 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10265 /* Need commas after the first capture. */
10268 /* Eat the leading `['. */
10269 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10271 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10272 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10273 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10274 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10275 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10276 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10278 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10280 cp_lexer_consume_token (parser
->lexer
);
10283 if (!(at_function_scope_p () || parsing_nsdmi ()))
10284 error ("non-local lambda expression cannot have a capture-default");
10287 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10289 cp_token
* capture_token
;
10291 tree capture_init_expr
;
10292 cp_id_kind idk
= CP_ID_KIND_NONE
;
10293 bool explicit_init_p
= false;
10295 enum capture_kind_type
10300 enum capture_kind_type capture_kind
= BY_COPY
;
10302 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10304 error ("expected end of capture-list");
10311 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10313 /* Possibly capture `this'. */
10314 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10316 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10317 if (cxx_dialect
< cxx2a
10318 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10319 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10320 "with by-copy capture default");
10321 cp_lexer_consume_token (parser
->lexer
);
10322 add_capture (lambda_expr
,
10323 /*id=*/this_identifier
,
10324 /*initializer=*/finish_this_expr (),
10325 /*by_reference_p=*/true,
10330 /* Possibly capture `*this'. */
10331 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10332 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10334 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10335 if (cxx_dialect
< cxx17
)
10336 pedwarn (loc
, 0, "%<*this%> capture only available with "
10337 "-std=c++17 or -std=gnu++17");
10338 cp_lexer_consume_token (parser
->lexer
);
10339 cp_lexer_consume_token (parser
->lexer
);
10340 add_capture (lambda_expr
,
10341 /*id=*/this_identifier
,
10342 /*initializer=*/finish_this_expr (),
10343 /*by_reference_p=*/false,
10348 /* Remember whether we want to capture as a reference or not. */
10349 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10351 capture_kind
= BY_REFERENCE
;
10352 cp_lexer_consume_token (parser
->lexer
);
10355 /* Get the identifier. */
10356 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10357 capture_id
= cp_parser_identifier (parser
);
10359 if (capture_id
== error_mark_node
)
10360 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10361 delimiters, but I modified this to stop on unnested ']' as well. It
10362 was already changed to stop on unnested '}', so the
10363 "closing_parenthesis" name is no more misleading with my change. */
10365 cp_parser_skip_to_closing_parenthesis (parser
,
10366 /*recovering=*/true,
10368 /*consume_paren=*/true);
10372 /* Find the initializer for this capture. */
10373 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10374 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10375 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10377 bool direct
, non_constant
;
10378 /* An explicit initializer exists. */
10379 if (cxx_dialect
< cxx14
)
10380 pedwarn (input_location
, 0,
10381 "lambda capture initializers "
10382 "only available with -std=c++14 or -std=gnu++14");
10383 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10384 &non_constant
, true);
10385 explicit_init_p
= true;
10386 if (capture_init_expr
== NULL_TREE
)
10388 error ("empty initializer for lambda init-capture");
10389 capture_init_expr
= error_mark_node
;
10394 const char* error_msg
;
10396 /* Turn the identifier into an id-expression. */
10398 = cp_parser_lookup_name_simple (parser
, capture_id
,
10399 capture_token
->location
);
10401 if (capture_init_expr
== error_mark_node
)
10403 unqualified_name_lookup_error (capture_id
);
10406 else if (!VAR_P (capture_init_expr
)
10407 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10409 error_at (capture_token
->location
,
10410 "capture of non-variable %qE",
10411 capture_init_expr
);
10412 if (DECL_P (capture_init_expr
))
10413 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10414 "%q#D declared here", capture_init_expr
);
10417 if (VAR_P (capture_init_expr
)
10418 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10420 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10421 "%qD with non-automatic storage duration",
10422 capture_init_expr
))
10423 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10424 "%q#D declared here", capture_init_expr
);
10429 = finish_id_expression
10434 /*integral_constant_expression_p=*/false,
10435 /*allow_non_integral_constant_expression_p=*/false,
10436 /*non_integral_constant_expression_p=*/NULL
,
10437 /*template_p=*/false,
10439 /*address_p=*/false,
10440 /*template_arg_p=*/false,
10442 capture_token
->location
);
10444 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10446 cp_lexer_consume_token (parser
->lexer
);
10447 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10451 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10452 && !explicit_init_p
)
10454 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10455 && capture_kind
== BY_COPY
)
10456 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10457 "of %qD redundant with by-copy capture default",
10459 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10460 && capture_kind
== BY_REFERENCE
)
10461 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10462 "capture of %qD redundant with by-reference capture "
10463 "default", capture_id
);
10466 add_capture (lambda_expr
,
10469 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10472 /* If there is any qualification still in effect, clear it
10473 now; we will be starting fresh with the next capture. */
10474 parser
->scope
= NULL_TREE
;
10475 parser
->qualifying_scope
= NULL_TREE
;
10476 parser
->object_scope
= NULL_TREE
;
10479 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10482 /* Parse the (optional) middle of a lambda expression.
10485 < template-parameter-list [opt] >
10486 ( parameter-declaration-clause [opt] )
10487 attribute-specifier [opt]
10488 decl-specifier-seq [opt]
10489 exception-specification [opt]
10490 lambda-return-type-clause [opt]
10492 LAMBDA_EXPR is the current representation of the lambda expression. */
10495 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10497 /* 5.1.1.4 of the standard says:
10498 If a lambda-expression does not include a lambda-declarator, it is as if
10499 the lambda-declarator were ().
10500 This means an empty parameter list, no attributes, and no exception
10502 tree param_list
= void_list_node
;
10503 tree attributes
= NULL_TREE
;
10504 tree exception_spec
= NULL_TREE
;
10505 tree template_param_list
= NULL_TREE
;
10506 tree tx_qual
= NULL_TREE
;
10507 tree return_type
= NULL_TREE
;
10508 cp_decl_specifier_seq lambda_specs
;
10509 clear_decl_specs (&lambda_specs
);
10511 /* The template-parameter-list is optional, but must begin with
10512 an opening angle if present. */
10513 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10515 if (cxx_dialect
< cxx14
)
10516 pedwarn (parser
->lexer
->next_token
->location
, 0,
10517 "lambda templates are only available with "
10518 "-std=c++14 or -std=gnu++14");
10519 else if (cxx_dialect
< cxx2a
)
10520 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10521 "lambda templates are only available with "
10522 "-std=c++2a or -std=gnu++2a");
10524 cp_lexer_consume_token (parser
->lexer
);
10526 template_param_list
= cp_parser_template_parameter_list (parser
);
10528 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10530 /* We just processed one more parameter list. */
10531 ++parser
->num_template_parameter_lists
;
10534 /* The parameter-declaration-clause is optional (unless
10535 template-parameter-list was given), but must begin with an
10536 opening parenthesis if present. */
10537 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10539 matching_parens parens
;
10540 parens
.consume_open (parser
);
10542 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10544 /* Parse parameters. */
10545 param_list
= cp_parser_parameter_declaration_clause (parser
);
10547 /* Default arguments shall not be specified in the
10548 parameter-declaration-clause of a lambda-declarator. */
10549 if (cxx_dialect
< cxx14
)
10550 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10551 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10552 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10553 "default argument specified for lambda parameter");
10555 parens
.require_close (parser
);
10557 attributes
= cp_parser_attributes_opt (parser
);
10559 /* In the decl-specifier-seq of the lambda-declarator, each
10560 decl-specifier shall either be mutable or constexpr. */
10561 int declares_class_or_enum
;
10562 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10563 cp_parser_decl_specifier_seq (parser
,
10564 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10565 &lambda_specs
, &declares_class_or_enum
);
10566 if (lambda_specs
.storage_class
== sc_mutable
)
10568 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10569 if (lambda_specs
.conflicting_specifiers_p
)
10570 error_at (lambda_specs
.locations
[ds_storage_class
],
10571 "duplicate %<mutable%>");
10574 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10576 /* Parse optional exception specification. */
10577 exception_spec
= cp_parser_exception_specification_opt (parser
);
10579 /* Parse optional trailing return type. */
10580 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10582 cp_lexer_consume_token (parser
->lexer
);
10583 return_type
= cp_parser_trailing_type_id (parser
);
10586 /* The function parameters must be in scope all the way until after the
10587 trailing-return-type in case of decltype. */
10588 pop_bindings_and_leave_scope ();
10590 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10591 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10593 /* Create the function call operator.
10595 Messing with declarators like this is no uglier than building up the
10596 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10599 cp_decl_specifier_seq return_type_specs
;
10600 cp_declarator
* declarator
;
10605 clear_decl_specs (&return_type_specs
);
10606 return_type_specs
.type
= make_auto ();
10608 if (lambda_specs
.locations
[ds_constexpr
])
10610 if (cxx_dialect
>= cxx17
)
10611 return_type_specs
.locations
[ds_constexpr
]
10612 = lambda_specs
.locations
[ds_constexpr
];
10614 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10615 "lambda only available with -std=c++17 or -std=gnu++17");
10618 p
= obstack_alloc (&declarator_obstack
, 0);
10620 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10622 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10623 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10624 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10625 VIRT_SPEC_UNSPECIFIED
,
10629 /*late_return_type=*/NULL_TREE
,
10630 /*requires_clause*/NULL_TREE
);
10631 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10633 declarator
->u
.function
.late_return_type
= return_type
;
10635 fco
= grokmethod (&return_type_specs
,
10638 if (fco
!= error_mark_node
)
10640 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10641 DECL_ARTIFICIAL (fco
) = 1;
10642 /* Give the object parameter a different name. */
10643 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
10644 DECL_LAMBDA_FUNCTION (fco
) = 1;
10646 if (template_param_list
)
10648 fco
= finish_member_template_decl (fco
);
10649 finish_template_decl (template_param_list
);
10650 --parser
->num_template_parameter_lists
;
10652 else if (parser
->fully_implicit_function_template_p
)
10653 fco
= finish_fully_implicit_template (parser
, fco
);
10655 finish_member_declaration (fco
);
10657 obstack_free (&declarator_obstack
, p
);
10659 return (fco
!= error_mark_node
);
10663 /* Parse the body of a lambda expression, which is simply
10667 but which requires special handling.
10668 LAMBDA_EXPR is the current representation of the lambda expression. */
10671 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10673 bool nested
= (current_function_decl
!= NULL_TREE
);
10674 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10675 bool in_function_body
= parser
->in_function_body
;
10678 push_function_context ();
10680 /* Still increment function_depth so that we don't GC in the
10681 middle of an expression. */
10684 vec
<tree
> omp_privatization_save
;
10685 save_omp_privatization_clauses (omp_privatization_save
);
10686 /* Clear this in case we're in the middle of a default argument. */
10687 parser
->local_variables_forbidden_p
= false;
10688 parser
->in_function_body
= true;
10691 local_specialization_stack
s (lss_copy
);
10692 tree fco
= lambda_function (lambda_expr
);
10693 tree body
= start_lambda_function (fco
, lambda_expr
);
10694 matching_braces braces
;
10696 if (braces
.require_open (parser
))
10698 tree compound_stmt
= begin_compound_stmt (0);
10700 /* Originally C++11 required us to peek for 'return expr'; and
10701 process it specially here to deduce the return type. N3638
10702 removed the need for that. */
10704 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10705 cp_parser_label_declaration (parser
);
10706 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10707 braces
.require_close (parser
);
10709 finish_compound_stmt (compound_stmt
);
10712 finish_lambda_function (body
);
10715 restore_omp_privatization_clauses (omp_privatization_save
);
10716 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10717 parser
->in_function_body
= in_function_body
;
10719 pop_function_context();
10724 /* Statements [gram.stmt.stmt] */
10726 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10729 add_debug_begin_stmt (location_t loc
)
10731 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10733 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10734 /* A concept is never expanded normally. */
10737 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10738 SET_EXPR_LOCATION (stmt
, loc
);
10742 /* Parse a statement.
10746 expression-statement
10748 selection-statement
10749 iteration-statement
10751 declaration-statement
10758 attribute-specifier-seq (opt) expression-statement
10759 attribute-specifier-seq (opt) compound-statement
10760 attribute-specifier-seq (opt) selection-statement
10761 attribute-specifier-seq (opt) iteration-statement
10762 attribute-specifier-seq (opt) jump-statement
10763 declaration-statement
10764 attribute-specifier-seq (opt) try-block
10767 expression-statement
10775 IN_COMPOUND is true when the statement is nested inside a
10776 cp_parser_compound_statement; this matters for certain pragmas.
10778 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10779 is a (possibly labeled) if statement which is not enclosed in braces
10780 and has an else clause. This is used to implement -Wparentheses.
10782 CHAIN is a vector of if-else-if conditions. */
10785 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10786 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10787 location_t
*loc_after_labels
)
10789 tree statement
, std_attrs
= NULL_TREE
;
10791 location_t statement_location
, attrs_location
;
10796 /* There is no statement yet. */
10797 statement
= NULL_TREE
;
10799 saved_token_sentinel
saved_tokens (parser
->lexer
);
10800 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10801 if (c_dialect_objc ())
10802 /* In obj-c++, seeing '[[' might be the either the beginning of
10803 c++11 attributes, or a nested objc-message-expression. So
10804 let's parse the c++11 attributes tentatively. */
10805 cp_parser_parse_tentatively (parser
);
10806 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10807 if (c_dialect_objc ())
10809 if (!cp_parser_parse_definitely (parser
))
10810 std_attrs
= NULL_TREE
;
10813 /* Peek at the next token. */
10814 token
= cp_lexer_peek_token (parser
->lexer
);
10815 /* Remember the location of the first token in the statement. */
10816 statement_location
= token
->location
;
10817 add_debug_begin_stmt (statement_location
);
10818 /* If this is a keyword, then that will often determine what kind of
10819 statement we have. */
10820 if (token
->type
== CPP_KEYWORD
)
10822 enum rid keyword
= token
->keyword
;
10828 /* Looks like a labeled-statement with a case label.
10829 Parse the label, and then use tail recursion to parse
10831 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10832 in_compound
= false;
10837 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10843 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10850 statement
= cp_parser_jump_statement (parser
);
10853 /* Objective-C++ exception-handling constructs. */
10856 case RID_AT_FINALLY
:
10857 case RID_AT_SYNCHRONIZED
:
10859 statement
= cp_parser_objc_statement (parser
);
10863 statement
= cp_parser_try_block (parser
);
10866 case RID_NAMESPACE
:
10867 /* This must be a namespace alias definition. */
10868 cp_parser_declaration_statement (parser
);
10871 case RID_TRANSACTION_ATOMIC
:
10872 case RID_TRANSACTION_RELAXED
:
10873 case RID_SYNCHRONIZED
:
10874 case RID_ATOMIC_NOEXCEPT
:
10875 case RID_ATOMIC_CANCEL
:
10876 statement
= cp_parser_transaction (parser
, token
);
10878 case RID_TRANSACTION_CANCEL
:
10879 statement
= cp_parser_transaction_cancel (parser
);
10883 /* It might be a keyword like `int' that can start a
10884 declaration-statement. */
10888 else if (token
->type
== CPP_NAME
)
10890 /* If the next token is a `:', then we are looking at a
10891 labeled-statement. */
10892 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10893 if (token
->type
== CPP_COLON
)
10895 /* Looks like a labeled-statement with an ordinary label.
10896 Parse the label, and then use tail recursion to parse
10899 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10900 in_compound
= false;
10904 /* Anything that starts with a `{' must be a compound-statement. */
10905 else if (token
->type
== CPP_OPEN_BRACE
)
10906 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10907 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10908 a statement all its own. */
10909 else if (token
->type
== CPP_PRAGMA
)
10911 /* Only certain OpenMP pragmas are attached to statements, and thus
10912 are considered statements themselves. All others are not. In
10913 the context of a compound, accept the pragma as a "statement" and
10914 return so that we can check for a close brace. Otherwise we
10915 require a real statement and must go back and read one. */
10917 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10918 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10922 else if (token
->type
== CPP_EOF
)
10924 cp_parser_error (parser
, "expected statement");
10928 /* Everything else must be a declaration-statement or an
10929 expression-statement. Try for the declaration-statement
10930 first, unless we are looking at a `;', in which case we know that
10931 we have an expression-statement. */
10934 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10936 if (std_attrs
!= NULL_TREE
)
10938 /* Attributes should be parsed as part of the the
10939 declaration, so let's un-parse them. */
10940 saved_tokens
.rollback();
10941 std_attrs
= NULL_TREE
;
10944 cp_parser_parse_tentatively (parser
);
10945 /* Try to parse the declaration-statement. */
10946 cp_parser_declaration_statement (parser
);
10947 /* If that worked, we're done. */
10948 if (cp_parser_parse_definitely (parser
))
10951 /* All preceding labels have been parsed at this point. */
10952 if (loc_after_labels
!= NULL
)
10953 *loc_after_labels
= statement_location
;
10955 /* Look for an expression-statement instead. */
10956 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10958 /* Handle [[fallthrough]];. */
10959 if (attribute_fallthrough_p (std_attrs
))
10961 /* The next token after the fallthrough attribute is ';'. */
10962 if (statement
== NULL_TREE
)
10964 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10965 statement
= build_call_expr_internal_loc (statement_location
,
10967 void_type_node
, 0);
10968 finish_expr_stmt (statement
);
10971 warning_at (statement_location
, OPT_Wattributes
,
10972 "%<fallthrough%> attribute not followed by %<;%>");
10973 std_attrs
= NULL_TREE
;
10977 /* Set the line number for the statement. */
10978 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10979 SET_EXPR_LOCATION (statement
, statement_location
);
10981 /* Allow "[[fallthrough]];", but warn otherwise. */
10982 if (std_attrs
!= NULL_TREE
)
10983 warning_at (attrs_location
,
10985 "attributes at the beginning of statement are ignored");
10988 /* Append ATTR to attribute list ATTRS. */
10991 attr_chainon (tree attrs
, tree attr
)
10993 if (attrs
== error_mark_node
)
10994 return error_mark_node
;
10995 if (attr
== error_mark_node
)
10996 return error_mark_node
;
10997 return chainon (attrs
, attr
);
11000 /* Parse the label for a labeled-statement, i.e.
11003 case constant-expression :
11007 case constant-expression ... constant-expression : statement
11009 When a label is parsed without errors, the label is added to the
11010 parse tree by the finish_* functions, so this function doesn't
11011 have to return the label. */
11014 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11017 tree label
= NULL_TREE
;
11018 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11020 /* The next token should be an identifier. */
11021 token
= cp_lexer_peek_token (parser
->lexer
);
11022 if (token
->type
!= CPP_NAME
11023 && token
->type
!= CPP_KEYWORD
)
11025 cp_parser_error (parser
, "expected labeled-statement");
11029 /* Remember whether this case or a user-defined label is allowed to fall
11031 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11033 parser
->colon_corrects_to_scope_p
= false;
11034 switch (token
->keyword
)
11038 tree expr
, expr_hi
;
11039 cp_token
*ellipsis
;
11041 /* Consume the `case' token. */
11042 cp_lexer_consume_token (parser
->lexer
);
11043 /* Parse the constant-expression. */
11044 expr
= cp_parser_constant_expression (parser
);
11045 if (check_for_bare_parameter_packs (expr
))
11046 expr
= error_mark_node
;
11048 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11049 if (ellipsis
->type
== CPP_ELLIPSIS
)
11051 /* Consume the `...' token. */
11052 cp_lexer_consume_token (parser
->lexer
);
11053 expr_hi
= cp_parser_constant_expression (parser
);
11054 if (check_for_bare_parameter_packs (expr_hi
))
11055 expr_hi
= error_mark_node
;
11057 /* We don't need to emit warnings here, as the common code
11058 will do this for us. */
11061 expr_hi
= NULL_TREE
;
11063 if (parser
->in_switch_statement_p
)
11065 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11066 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11067 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11070 error_at (token
->location
,
11071 "case label %qE not within a switch statement",
11077 /* Consume the `default' token. */
11078 cp_lexer_consume_token (parser
->lexer
);
11080 if (parser
->in_switch_statement_p
)
11082 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11083 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11084 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11087 error_at (token
->location
, "case label not within a switch statement");
11091 /* Anything else must be an ordinary label. */
11092 label
= finish_label_stmt (cp_parser_identifier (parser
));
11093 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11094 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11098 /* Require the `:' token. */
11099 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11101 /* An ordinary label may optionally be followed by attributes.
11102 However, this is only permitted if the attributes are then
11103 followed by a semicolon. This is because, for backward
11104 compatibility, when parsing
11105 lab: __attribute__ ((unused)) int i;
11106 we want the attribute to attach to "i", not "lab". */
11107 if (label
!= NULL_TREE
11108 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11111 cp_parser_parse_tentatively (parser
);
11112 attrs
= cp_parser_gnu_attributes_opt (parser
);
11113 if (attrs
== NULL_TREE
11114 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11115 cp_parser_abort_tentative_parse (parser
);
11116 else if (!cp_parser_parse_definitely (parser
))
11119 attributes
= attr_chainon (attributes
, attrs
);
11122 if (attributes
!= NULL_TREE
)
11123 cplus_decl_attributes (&label
, attributes
, 0);
11125 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11128 /* Parse an expression-statement.
11130 expression-statement:
11133 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11134 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11135 indicates whether this expression-statement is part of an
11136 expression statement. */
11139 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11141 tree statement
= NULL_TREE
;
11142 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11143 location_t loc
= token
->location
;
11145 /* There might be attribute fallthrough. */
11146 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11148 /* If the next token is a ';', then there is no expression
11150 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11152 statement
= cp_parser_expression (parser
);
11153 if (statement
== error_mark_node
11154 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11156 cp_parser_skip_to_end_of_block_or_statement (parser
);
11157 return error_mark_node
;
11161 /* Handle [[fallthrough]];. */
11162 if (attribute_fallthrough_p (attr
))
11164 /* The next token after the fallthrough attribute is ';'. */
11165 if (statement
== NULL_TREE
)
11166 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11167 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11168 void_type_node
, 0);
11170 warning_at (loc
, OPT_Wattributes
,
11171 "%<fallthrough%> attribute not followed by %<;%>");
11175 /* Allow "[[fallthrough]];", but warn otherwise. */
11176 if (attr
!= NULL_TREE
)
11177 warning_at (loc
, OPT_Wattributes
,
11178 "attributes at the beginning of statement are ignored");
11180 /* Give a helpful message for "A<T>::type t;" and the like. */
11181 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11182 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11184 if (TREE_CODE (statement
) == SCOPE_REF
)
11185 error_at (token
->location
, "need %<typename%> before %qE because "
11186 "%qT is a dependent scope",
11187 statement
, TREE_OPERAND (statement
, 0));
11188 else if (is_overloaded_fn (statement
)
11189 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11192 tree fn
= get_first_fn (statement
);
11193 error_at (token
->location
,
11194 "%<%T::%D%> names the constructor, not the type",
11195 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11199 /* Consume the final `;'. */
11200 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11202 if (in_statement_expr
11203 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11204 /* This is the final expression statement of a statement
11206 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11207 else if (statement
)
11208 statement
= finish_expr_stmt (statement
);
11213 /* Parse a compound-statement.
11215 compound-statement:
11216 { statement-seq [opt] }
11220 compound-statement:
11221 { label-declaration-seq [opt] statement-seq [opt] }
11223 label-declaration-seq:
11225 label-declaration-seq label-declaration
11227 Returns a tree representing the statement. */
11230 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11231 int bcs_flags
, bool function_body
)
11233 tree compound_stmt
;
11234 matching_braces braces
;
11236 /* Consume the `{'. */
11237 if (!braces
.require_open (parser
))
11238 return error_mark_node
;
11239 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11240 && !function_body
&& cxx_dialect
< cxx14
)
11241 pedwarn (input_location
, OPT_Wpedantic
,
11242 "compound-statement in %<constexpr%> function");
11243 /* Begin the compound-statement. */
11244 compound_stmt
= begin_compound_stmt (bcs_flags
);
11245 /* If the next keyword is `__label__' we have a label declaration. */
11246 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11247 cp_parser_label_declaration (parser
);
11248 /* Parse an (optional) statement-seq. */
11249 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11250 /* Finish the compound-statement. */
11251 finish_compound_stmt (compound_stmt
);
11252 /* Consume the `}'. */
11253 braces
.require_close (parser
);
11255 return compound_stmt
;
11258 /* Parse an (optional) statement-seq.
11262 statement-seq [opt] statement */
11265 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11267 /* Scan statements until there aren't any more. */
11270 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11272 /* If we are looking at a `}', then we have run out of
11273 statements; the same is true if we have reached the end
11274 of file, or have stumbled upon a stray '@end'. */
11275 if (token
->type
== CPP_CLOSE_BRACE
11276 || token
->type
== CPP_EOF
11277 || token
->type
== CPP_PRAGMA_EOL
11278 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11281 /* If we are in a compound statement and find 'else' then
11282 something went wrong. */
11283 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11285 if (parser
->in_statement
& IN_IF_STMT
)
11289 token
= cp_lexer_consume_token (parser
->lexer
);
11290 error_at (token
->location
, "%<else%> without a previous %<if%>");
11294 /* Parse the statement. */
11295 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11299 /* Return true if this is the C++20 version of range-based-for with
11303 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11307 /* Save tokens so that we can put them back. */
11308 cp_lexer_save_tokens (parser
->lexer
);
11310 /* There has to be an unnested ; followed by an unnested :. */
11311 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11312 /*recovering=*/false,
11314 /*consume_paren=*/false) != -1)
11317 /* We found the semicolon, eat it now. */
11318 cp_lexer_consume_token (parser
->lexer
);
11320 /* Now look for ':' that is not nested in () or {}. */
11321 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11322 /*recovering=*/false,
11324 /*consume_paren=*/false) == -1);
11327 /* Roll back the tokens we skipped. */
11328 cp_lexer_rollback_tokens (parser
->lexer
);
11333 /* Return true if we're looking at (init; cond), false otherwise. */
11336 cp_parser_init_statement_p (cp_parser
*parser
)
11338 /* Save tokens so that we can put them back. */
11339 cp_lexer_save_tokens (parser
->lexer
);
11341 /* Look for ';' that is not nested in () or {}. */
11342 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11343 /*recovering=*/false,
11345 /*consume_paren=*/false);
11347 /* Roll back the tokens we skipped. */
11348 cp_lexer_rollback_tokens (parser
->lexer
);
11353 /* Parse a selection-statement.
11355 selection-statement:
11356 if ( init-statement [opt] condition ) statement
11357 if ( init-statement [opt] condition ) statement else statement
11358 switch ( init-statement [opt] condition ) statement
11360 Returns the new IF_STMT or SWITCH_STMT.
11362 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11363 is a (possibly labeled) if statement which is not enclosed in
11364 braces and has an else clause. This is used to implement
11367 CHAIN is a vector of if-else-if conditions. This is used to implement
11368 -Wduplicated-cond. */
11371 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11376 token_indent_info guard_tinfo
;
11381 /* Peek at the next token. */
11382 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11383 guard_tinfo
= get_token_indent_info (token
);
11385 /* See what kind of keyword it is. */
11386 keyword
= token
->keyword
;
11396 if (keyword
== RID_IF
11397 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11401 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11402 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11403 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11404 "with -std=c++17 or -std=gnu++17");
11407 /* Look for the `('. */
11408 matching_parens parens
;
11409 if (!parens
.require_open (parser
))
11411 cp_parser_skip_to_end_of_statement (parser
);
11412 return error_mark_node
;
11415 /* Begin the selection-statement. */
11416 if (keyword
== RID_IF
)
11418 statement
= begin_if_stmt ();
11419 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11422 statement
= begin_switch_stmt ();
11424 /* Parse the optional init-statement. */
11425 if (cp_parser_init_statement_p (parser
))
11428 if (cxx_dialect
< cxx17
)
11429 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11430 "init-statement in selection statements only available "
11431 "with -std=c++17 or -std=gnu++17");
11432 cp_parser_init_statement (parser
, &decl
);
11435 /* Parse the condition. */
11436 condition
= cp_parser_condition (parser
);
11437 /* Look for the `)'. */
11438 if (!parens
.require_close (parser
))
11439 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11440 /*consume_paren=*/true);
11442 if (keyword
== RID_IF
)
11445 unsigned char in_statement
;
11447 /* Add the condition. */
11448 condition
= finish_if_stmt_cond (condition
, statement
);
11450 if (warn_duplicated_cond
)
11451 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11454 /* Parse the then-clause. */
11455 in_statement
= parser
->in_statement
;
11456 parser
->in_statement
|= IN_IF_STMT
;
11458 /* Outside a template, the non-selected branch of a constexpr
11459 if is a 'discarded statement', i.e. unevaluated. */
11460 bool was_discarded
= in_discarded_stmt
;
11461 bool discard_then
= (cx
&& !processing_template_decl
11462 && integer_zerop (condition
));
11465 in_discarded_stmt
= true;
11466 ++c_inhibit_evaluation_warnings
;
11469 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11472 parser
->in_statement
= in_statement
;
11474 finish_then_clause (statement
);
11478 THEN_CLAUSE (statement
) = NULL_TREE
;
11479 in_discarded_stmt
= was_discarded
;
11480 --c_inhibit_evaluation_warnings
;
11483 /* If the next token is `else', parse the else-clause. */
11484 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11487 bool discard_else
= (cx
&& !processing_template_decl
11488 && integer_nonzerop (condition
));
11491 in_discarded_stmt
= true;
11492 ++c_inhibit_evaluation_warnings
;
11496 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11497 /* Consume the `else' keyword. */
11498 cp_lexer_consume_token (parser
->lexer
);
11499 if (warn_duplicated_cond
)
11501 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11505 /* We've got "if (COND) else if (COND2)". Start
11506 the condition chain and add COND as the first
11508 chain
= new vec
<tree
> ();
11509 if (!CONSTANT_CLASS_P (condition
)
11510 && !TREE_SIDE_EFFECTS (condition
))
11512 /* Wrap it in a NOP_EXPR so that we can set the
11513 location of the condition. */
11514 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11516 SET_EXPR_LOCATION (e
, token
->location
);
11517 chain
->safe_push (e
);
11520 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11523 /* This is if-else without subsequent if. Zap the
11524 condition chain; we would have already warned at
11530 begin_else_clause (statement
);
11531 /* Parse the else-clause. */
11532 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11533 guard_tinfo
, chain
);
11535 finish_else_clause (statement
);
11537 /* If we are currently parsing a then-clause, then
11538 IF_P will not be NULL. We set it to true to
11539 indicate that this if statement has an else clause.
11540 This may trigger the Wparentheses warning below
11541 when we get back up to the parent if statement. */
11547 ELSE_CLAUSE (statement
) = NULL_TREE
;
11548 in_discarded_stmt
= was_discarded
;
11549 --c_inhibit_evaluation_warnings
;
11554 /* This if statement does not have an else clause. If
11555 NESTED_IF is true, then the then-clause has an if
11556 statement which does have an else clause. We warn
11557 about the potential ambiguity. */
11559 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11560 "suggest explicit braces to avoid ambiguous"
11562 if (warn_duplicated_cond
)
11564 /* We don't need the condition chain anymore. */
11570 /* Now we're all done with the if-statement. */
11571 finish_if_stmt (statement
);
11575 bool in_switch_statement_p
;
11576 unsigned char in_statement
;
11578 /* Add the condition. */
11579 finish_switch_cond (condition
, statement
);
11581 /* Parse the body of the switch-statement. */
11582 in_switch_statement_p
= parser
->in_switch_statement_p
;
11583 in_statement
= parser
->in_statement
;
11584 parser
->in_switch_statement_p
= true;
11585 parser
->in_statement
|= IN_SWITCH_STMT
;
11586 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11588 parser
->in_switch_statement_p
= in_switch_statement_p
;
11589 parser
->in_statement
= in_statement
;
11591 /* Now we're all done with the switch-statement. */
11592 finish_switch_stmt (statement
);
11600 cp_parser_error (parser
, "expected selection-statement");
11601 return error_mark_node
;
11605 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11606 If we have seen at least one decl-specifier, and the next token
11607 is not a parenthesis, then we must be looking at a declaration.
11608 (After "int (" we might be looking at a functional cast.) */
11611 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11612 bool any_specifiers_p
)
11614 if (any_specifiers_p
11615 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11616 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11617 && !cp_parser_error_occurred (parser
))
11618 cp_parser_commit_to_tentative_parse (parser
);
11621 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11622 The declarator shall not specify a function or an array. Returns
11623 TRUE if the declarator is valid, FALSE otherwise. */
11626 cp_parser_check_condition_declarator (cp_parser
* parser
,
11627 cp_declarator
*declarator
,
11630 if (declarator
== cp_error_declarator
11631 || function_declarator_p (declarator
)
11632 || declarator
->kind
== cdk_array
)
11634 if (declarator
== cp_error_declarator
)
11635 /* Already complained. */;
11636 else if (declarator
->kind
== cdk_array
)
11637 error_at (loc
, "condition declares an array");
11639 error_at (loc
, "condition declares a function");
11640 if (parser
->fully_implicit_function_template_p
)
11641 abort_fully_implicit_template (parser
);
11642 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11643 /*or_comma=*/false,
11644 /*consume_paren=*/false);
11651 /* Parse a condition.
11655 type-specifier-seq declarator = initializer-clause
11656 type-specifier-seq declarator braced-init-list
11661 type-specifier-seq declarator asm-specification [opt]
11662 attributes [opt] = assignment-expression
11664 Returns the expression that should be tested. */
11667 cp_parser_condition (cp_parser
* parser
)
11669 cp_decl_specifier_seq type_specifiers
;
11670 const char *saved_message
;
11671 int declares_class_or_enum
;
11673 /* Try the declaration first. */
11674 cp_parser_parse_tentatively (parser
);
11675 /* New types are not allowed in the type-specifier-seq for a
11677 saved_message
= parser
->type_definition_forbidden_message
;
11678 parser
->type_definition_forbidden_message
11679 = G_("types may not be defined in conditions");
11680 /* Parse the type-specifier-seq. */
11681 cp_parser_decl_specifier_seq (parser
,
11682 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11684 &declares_class_or_enum
);
11685 /* Restore the saved message. */
11686 parser
->type_definition_forbidden_message
= saved_message
;
11688 cp_parser_maybe_commit_to_declaration (parser
,
11689 type_specifiers
.any_specifiers_p
);
11691 /* If all is well, we might be looking at a declaration. */
11692 if (!cp_parser_error_occurred (parser
))
11695 tree asm_specification
;
11697 cp_declarator
*declarator
;
11698 tree initializer
= NULL_TREE
;
11699 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11701 /* Parse the declarator. */
11702 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11703 /*ctor_dtor_or_conv_p=*/NULL
,
11704 /*parenthesized_p=*/NULL
,
11705 /*member_p=*/false,
11706 /*friend_p=*/false);
11707 /* Parse the attributes. */
11708 attributes
= cp_parser_attributes_opt (parser
);
11709 /* Parse the asm-specification. */
11710 asm_specification
= cp_parser_asm_specification_opt (parser
);
11711 /* If the next token is not an `=' or '{', then we might still be
11712 looking at an expression. For example:
11716 looks like a decl-specifier-seq and a declarator -- but then
11717 there is no `=', so this is an expression. */
11718 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11719 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11720 cp_parser_simulate_error (parser
);
11722 /* If we did see an `=' or '{', then we are looking at a declaration
11724 if (cp_parser_parse_definitely (parser
))
11727 bool non_constant_p
= false;
11728 int flags
= LOOKUP_ONLYCONVERTING
;
11730 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
11731 return error_mark_node
;
11733 /* Create the declaration. */
11734 decl
= start_decl (declarator
, &type_specifiers
,
11735 /*initialized_p=*/true,
11736 attributes
, /*prefix_attributes=*/NULL_TREE
,
11739 /* Parse the initializer. */
11740 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11742 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11743 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11746 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11748 /* Consume the `='. */
11749 cp_lexer_consume_token (parser
->lexer
);
11750 initializer
= cp_parser_initializer_clause (parser
,
11755 cp_parser_error (parser
, "expected initializer");
11756 initializer
= error_mark_node
;
11758 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11759 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11761 /* Process the initializer. */
11762 cp_finish_decl (decl
,
11763 initializer
, !non_constant_p
,
11768 pop_scope (pushed_scope
);
11770 return convert_from_reference (decl
);
11773 /* If we didn't even get past the declarator successfully, we are
11774 definitely not looking at a declaration. */
11776 cp_parser_abort_tentative_parse (parser
);
11778 /* Otherwise, we are looking at an expression. */
11779 return cp_parser_expression (parser
);
11782 /* Parses a for-statement or range-for-statement until the closing ')',
11786 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11788 tree init
, scope
, decl
;
11791 /* Begin the for-statement. */
11792 scope
= begin_for_scope (&init
);
11794 /* Parse the initialization. */
11795 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11798 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11800 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11804 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11805 unsigned short unroll
)
11807 /* Normal for loop */
11808 tree condition
= NULL_TREE
;
11809 tree expression
= NULL_TREE
;
11812 stmt
= begin_for_stmt (scope
, init
);
11813 /* The init-statement has already been parsed in
11814 cp_parser_init_statement, so no work is needed here. */
11815 finish_init_stmt (stmt
);
11817 /* If there's a condition, process it. */
11818 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11819 condition
= cp_parser_condition (parser
);
11822 cp_parser_error (parser
, "missing loop condition in loop with "
11823 "%<GCC ivdep%> pragma");
11824 condition
= error_mark_node
;
11828 cp_parser_error (parser
, "missing loop condition in loop with "
11829 "%<GCC unroll%> pragma");
11830 condition
= error_mark_node
;
11832 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11833 /* Look for the `;'. */
11834 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11836 /* If there's an expression, process it. */
11837 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11838 expression
= cp_parser_expression (parser
);
11839 finish_for_expr (expression
, stmt
);
11844 /* Tries to parse a range-based for-statement:
11847 decl-specifier-seq declarator : expression
11849 The decl-specifier-seq declarator and the `:' are already parsed by
11850 cp_parser_init_statement. If processing_template_decl it returns a
11851 newly created RANGE_FOR_STMT; if not, it is converted to a
11852 regular FOR_STMT. */
11855 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11856 bool ivdep
, unsigned short unroll
)
11858 tree stmt
, range_expr
;
11859 auto_vec
<cxx_binding
*, 16> bindings
;
11860 auto_vec
<tree
, 16> names
;
11861 tree decomp_first_name
= NULL_TREE
;
11862 unsigned int decomp_cnt
= 0;
11864 /* Get the range declaration momentarily out of the way so that
11865 the range expression doesn't clash with it. */
11866 if (range_decl
!= error_mark_node
)
11868 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11870 tree v
= DECL_VALUE_EXPR (range_decl
);
11871 /* For decomposition declaration get all of the corresponding
11872 declarations out of the way. */
11873 if (TREE_CODE (v
) == ARRAY_REF
11874 && VAR_P (TREE_OPERAND (v
, 0))
11875 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11877 tree d
= range_decl
;
11878 range_decl
= TREE_OPERAND (v
, 0);
11879 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11880 decomp_first_name
= d
;
11881 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11883 tree name
= DECL_NAME (d
);
11884 names
.safe_push (name
);
11885 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11886 IDENTIFIER_BINDING (name
)
11887 = IDENTIFIER_BINDING (name
)->previous
;
11891 if (names
.is_empty ())
11893 tree name
= DECL_NAME (range_decl
);
11894 names
.safe_push (name
);
11895 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11896 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11900 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11902 bool expr_non_constant_p
;
11903 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11906 range_expr
= cp_parser_expression (parser
);
11908 /* Put the range declaration(s) back into scope. */
11909 for (unsigned int i
= 0; i
< names
.length (); i
++)
11911 cxx_binding
*binding
= bindings
[i
];
11912 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11913 IDENTIFIER_BINDING (names
[i
]) = binding
;
11916 /* If in template, STMT is converted to a normal for-statement
11917 at instantiation. If not, it is done just ahead. */
11918 if (processing_template_decl
)
11920 if (check_for_bare_parameter_packs (range_expr
))
11921 range_expr
= error_mark_node
;
11922 stmt
= begin_range_for_stmt (scope
, init
);
11924 RANGE_FOR_IVDEP (stmt
) = 1;
11926 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11927 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11928 if (!type_dependent_expression_p (range_expr
)
11929 /* do_auto_deduction doesn't mess with template init-lists. */
11930 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11931 do_range_for_auto_deduction (range_decl
, range_expr
);
11935 stmt
= begin_for_stmt (scope
, init
);
11936 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11937 decomp_first_name
, decomp_cnt
, ivdep
,
11943 /* Subroutine of cp_convert_range_for: given the initializer expression,
11944 builds up the range temporary. */
11947 build_range_temp (tree range_expr
)
11949 tree range_type
, range_temp
;
11951 /* Find out the type deduced by the declaration
11952 `auto &&__range = range_expr'. */
11953 range_type
= cp_build_reference_type (make_auto (), true);
11954 range_type
= do_auto_deduction (range_type
, range_expr
,
11955 type_uses_auto (range_type
));
11957 /* Create the __range variable. */
11958 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
11960 TREE_USED (range_temp
) = 1;
11961 DECL_ARTIFICIAL (range_temp
) = 1;
11966 /* Used by cp_parser_range_for in template context: we aren't going to
11967 do a full conversion yet, but we still need to resolve auto in the
11968 type of the for-range-declaration if present. This is basically
11969 a shortcut version of cp_convert_range_for. */
11972 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11974 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11977 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11978 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11979 iter_type
= (cp_parser_perform_range_for_lookup
11980 (range_temp
, &begin_dummy
, &end_dummy
));
11983 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11985 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11987 tf_warning_or_error
);
11988 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11989 iter_decl
, auto_node
);
11994 /* Converts a range-based for-statement into a normal
11995 for-statement, as per the definition.
11997 for (RANGE_DECL : RANGE_EXPR)
12000 should be equivalent to:
12003 auto &&__range = RANGE_EXPR;
12004 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12008 RANGE_DECL = *__begin;
12013 If RANGE_EXPR is an array:
12014 BEGIN_EXPR = __range
12015 END_EXPR = __range + ARRAY_SIZE(__range)
12016 Else if RANGE_EXPR has a member 'begin' or 'end':
12017 BEGIN_EXPR = __range.begin()
12018 END_EXPR = __range.end()
12020 BEGIN_EXPR = begin(__range)
12021 END_EXPR = end(__range);
12023 If __range has a member 'begin' but not 'end', or vice versa, we must
12024 still use the second alternative (it will surely fail, however).
12025 When calling begin()/end() in the third alternative we must use
12026 argument dependent lookup, but always considering 'std' as an associated
12030 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12031 tree decomp_first_name
, unsigned int decomp_cnt
,
12032 bool ivdep
, unsigned short unroll
)
12035 tree iter_type
, begin_expr
, end_expr
;
12036 tree condition
, expression
;
12038 range_expr
= mark_lvalue_use (range_expr
);
12040 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12041 /* If an error happened previously do nothing or else a lot of
12042 unhelpful errors would be issued. */
12043 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12048 if (VAR_P (range_expr
)
12049 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12050 /* Can't bind a reference to an array of runtime bound. */
12051 range_temp
= range_expr
;
12054 range_temp
= build_range_temp (range_expr
);
12055 pushdecl (range_temp
);
12056 cp_finish_decl (range_temp
, range_expr
,
12057 /*is_constant_init*/false, NULL_TREE
,
12058 LOOKUP_ONLYCONVERTING
);
12059 range_temp
= convert_from_reference (range_temp
);
12061 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12062 &begin_expr
, &end_expr
);
12065 /* The new for initialization statement. */
12066 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12068 TREE_USED (begin
) = 1;
12069 DECL_ARTIFICIAL (begin
) = 1;
12071 cp_finish_decl (begin
, begin_expr
,
12072 /*is_constant_init*/false, NULL_TREE
,
12073 LOOKUP_ONLYCONVERTING
);
12075 if (cxx_dialect
>= cxx17
)
12076 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12077 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12078 TREE_USED (end
) = 1;
12079 DECL_ARTIFICIAL (end
) = 1;
12081 cp_finish_decl (end
, end_expr
,
12082 /*is_constant_init*/false, NULL_TREE
,
12083 LOOKUP_ONLYCONVERTING
);
12085 finish_init_stmt (statement
);
12087 /* The new for condition. */
12088 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12091 NULL
, tf_warning_or_error
);
12092 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12094 /* The new increment expression. */
12095 expression
= finish_unary_op_expr (input_location
,
12096 PREINCREMENT_EXPR
, begin
,
12097 tf_warning_or_error
);
12098 finish_for_expr (expression
, statement
);
12100 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12101 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12103 /* The declaration is initialized with *__begin inside the loop body. */
12104 cp_finish_decl (range_decl
,
12105 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12106 tf_warning_or_error
),
12107 /*is_constant_init*/false, NULL_TREE
,
12108 LOOKUP_ONLYCONVERTING
);
12109 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12110 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12115 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12116 We need to solve both at the same time because the method used
12117 depends on the existence of members begin or end.
12118 Returns the type deduced for the iterator expression. */
12121 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12123 if (error_operand_p (range
))
12125 *begin
= *end
= error_mark_node
;
12126 return error_mark_node
;
12129 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12131 error ("range-based %<for%> expression of type %qT "
12132 "has incomplete type", TREE_TYPE (range
));
12133 *begin
= *end
= error_mark_node
;
12134 return error_mark_node
;
12136 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12138 /* If RANGE is an array, we will use pointer arithmetic. */
12139 *begin
= decay_conversion (range
, tf_warning_or_error
);
12140 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12142 array_type_nelts_top (TREE_TYPE (range
)),
12144 return TREE_TYPE (*begin
);
12148 /* If it is not an array, we must do a bit of magic. */
12149 tree id_begin
, id_end
;
12150 tree member_begin
, member_end
;
12152 *begin
= *end
= error_mark_node
;
12154 id_begin
= get_identifier ("begin");
12155 id_end
= get_identifier ("end");
12156 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12157 /*protect=*/2, /*want_type=*/false,
12158 tf_warning_or_error
);
12159 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12160 /*protect=*/2, /*want_type=*/false,
12161 tf_warning_or_error
);
12163 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12165 /* Use the member functions. */
12166 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12167 *end
= cp_parser_range_for_member_function (range
, id_end
);
12171 /* Use global functions with ADL. */
12172 vec
<tree
, va_gc
> *vec
;
12173 vec
= make_tree_vector ();
12175 vec_safe_push (vec
, range
);
12177 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12178 tf_warning_or_error
);
12179 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12180 tf_warning_or_error
);
12181 member_end
= perform_koenig_lookup (id_end
, vec
,
12182 tf_warning_or_error
);
12183 *end
= finish_call_expr (member_end
, &vec
, false, true,
12184 tf_warning_or_error
);
12186 release_tree_vector (vec
);
12189 /* Last common checks. */
12190 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12192 /* If one of the expressions is an error do no more checks. */
12193 *begin
= *end
= error_mark_node
;
12194 return error_mark_node
;
12196 else if (type_dependent_expression_p (*begin
)
12197 || type_dependent_expression_p (*end
))
12198 /* Can happen, when, eg, in a template context, Koenig lookup
12199 can't resolve begin/end (c++/58503). */
12203 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12204 /* The unqualified type of the __begin and __end temporaries should
12205 be the same, as required by the multiple auto declaration. */
12206 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12208 if (cxx_dialect
>= cxx17
12209 && (build_x_binary_op (input_location
, NE_EXPR
,
12210 *begin
, ERROR_MARK
,
12213 != error_mark_node
))
12214 /* P0184R0 allows __begin and __end to have different types,
12215 but make sure they are comparable so we can give a better
12218 error ("inconsistent begin/end types in range-based %<for%> "
12219 "statement: %qT and %qT",
12220 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12227 /* Helper function for cp_parser_perform_range_for_lookup.
12228 Builds a tree for RANGE.IDENTIFIER(). */
12231 cp_parser_range_for_member_function (tree range
, tree identifier
)
12234 vec
<tree
, va_gc
> *vec
;
12236 member
= finish_class_member_access_expr (range
, identifier
,
12237 false, tf_warning_or_error
);
12238 if (member
== error_mark_node
)
12239 return error_mark_node
;
12241 vec
= make_tree_vector ();
12242 res
= finish_call_expr (member
, &vec
,
12243 /*disallow_virtual=*/false,
12244 /*koenig_p=*/false,
12245 tf_warning_or_error
);
12246 release_tree_vector (vec
);
12250 /* Parse an iteration-statement.
12252 iteration-statement:
12253 while ( condition ) statement
12254 do statement while ( expression ) ;
12255 for ( init-statement condition [opt] ; expression [opt] )
12258 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12261 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12262 unsigned short unroll
)
12267 unsigned char in_statement
;
12268 token_indent_info guard_tinfo
;
12270 /* Peek at the next token. */
12271 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12273 return error_mark_node
;
12275 guard_tinfo
= get_token_indent_info (token
);
12277 /* Remember whether or not we are already within an iteration
12279 in_statement
= parser
->in_statement
;
12281 /* See what kind of keyword it is. */
12282 keyword
= token
->keyword
;
12289 /* Begin the while-statement. */
12290 statement
= begin_while_stmt ();
12291 /* Look for the `('. */
12292 matching_parens parens
;
12293 parens
.require_open (parser
);
12294 /* Parse the condition. */
12295 condition
= cp_parser_condition (parser
);
12296 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12297 /* Look for the `)'. */
12298 parens
.require_close (parser
);
12299 /* Parse the dependent statement. */
12300 parser
->in_statement
= IN_ITERATION_STMT
;
12301 bool prev
= note_iteration_stmt_body_start ();
12302 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12303 note_iteration_stmt_body_end (prev
);
12304 parser
->in_statement
= in_statement
;
12305 /* We're done with the while-statement. */
12306 finish_while_stmt (statement
);
12314 /* Begin the do-statement. */
12315 statement
= begin_do_stmt ();
12316 /* Parse the body of the do-statement. */
12317 parser
->in_statement
= IN_ITERATION_STMT
;
12318 bool prev
= note_iteration_stmt_body_start ();
12319 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12320 note_iteration_stmt_body_end (prev
);
12321 parser
->in_statement
= in_statement
;
12322 finish_do_body (statement
);
12323 /* Look for the `while' keyword. */
12324 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12325 /* Look for the `('. */
12326 matching_parens parens
;
12327 parens
.require_open (parser
);
12328 /* Parse the expression. */
12329 expression
= cp_parser_expression (parser
);
12330 /* We're done with the do-statement. */
12331 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12332 /* Look for the `)'. */
12333 parens
.require_close (parser
);
12334 /* Look for the `;'. */
12335 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12341 /* Look for the `('. */
12342 matching_parens parens
;
12343 parens
.require_open (parser
);
12345 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12347 /* Look for the `)'. */
12348 parens
.require_close (parser
);
12350 /* Parse the body of the for-statement. */
12351 parser
->in_statement
= IN_ITERATION_STMT
;
12352 bool prev
= note_iteration_stmt_body_start ();
12353 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12354 note_iteration_stmt_body_end (prev
);
12355 parser
->in_statement
= in_statement
;
12357 /* We're done with the for-statement. */
12358 finish_for_stmt (statement
);
12363 cp_parser_error (parser
, "expected iteration-statement");
12364 statement
= error_mark_node
;
12371 /* Parse a init-statement or the declarator of a range-based-for.
12372 Returns true if a range-based-for declaration is seen.
12375 expression-statement
12376 simple-declaration */
12379 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12381 /* If the next token is a `;', then we have an empty
12382 expression-statement. Grammatically, this is also a
12383 simple-declaration, but an invalid one, because it does not
12384 declare anything. Therefore, if we did not handle this case
12385 specially, we would issue an error message about an invalid
12387 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12389 bool is_range_for
= false;
12390 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12392 /* Try to parse the init-statement. */
12393 if (cp_parser_range_based_for_with_init_p (parser
))
12396 cp_parser_parse_tentatively (parser
);
12397 /* Parse the declaration. */
12398 cp_parser_simple_declaration (parser
,
12399 /*function_definition_allowed_p=*/false,
12401 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12402 if (!cp_parser_parse_definitely (parser
))
12403 /* That didn't work, try to parse it as an expression-statement. */
12404 cp_parser_expression_statement (parser
, NULL_TREE
);
12406 if (cxx_dialect
< cxx2a
)
12408 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12409 "range-based %<for%> loops with initializer only "
12410 "available with -std=c++2a or -std=gnu++2a");
12411 *decl
= error_mark_node
;
12415 /* A colon is used in range-based for. */
12416 parser
->colon_corrects_to_scope_p
= false;
12418 /* We're going to speculatively look for a declaration, falling back
12419 to an expression, if necessary. */
12420 cp_parser_parse_tentatively (parser
);
12421 /* Parse the declaration. */
12422 cp_parser_simple_declaration (parser
,
12423 /*function_definition_allowed_p=*/false,
12425 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12428 /* It is a range-for, consume the ':'. */
12429 cp_lexer_consume_token (parser
->lexer
);
12430 is_range_for
= true;
12431 if (cxx_dialect
< cxx11
)
12432 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12433 "range-based %<for%> loops only available with "
12434 "-std=c++11 or -std=gnu++11");
12437 /* The ';' is not consumed yet because we told
12438 cp_parser_simple_declaration not to. */
12439 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12441 if (cp_parser_parse_definitely (parser
))
12442 return is_range_for
;
12443 /* If the tentative parse failed, then we shall need to look for an
12444 expression-statement. */
12446 /* If we are here, it is an expression-statement. */
12447 cp_parser_expression_statement (parser
, NULL_TREE
);
12451 /* Parse a jump-statement.
12456 return expression [opt] ;
12457 return braced-init-list ;
12463 goto * expression ;
12465 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12468 cp_parser_jump_statement (cp_parser
* parser
)
12470 tree statement
= error_mark_node
;
12473 unsigned char in_statement
;
12475 /* Peek at the next token. */
12476 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12478 return error_mark_node
;
12480 /* See what kind of keyword it is. */
12481 keyword
= token
->keyword
;
12485 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12486 switch (in_statement
)
12489 error_at (token
->location
, "break statement not within loop or switch");
12492 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12493 || in_statement
== IN_ITERATION_STMT
);
12494 statement
= finish_break_stmt ();
12495 if (in_statement
== IN_ITERATION_STMT
)
12496 break_maybe_infinite_loop ();
12499 error_at (token
->location
, "invalid exit from OpenMP structured block");
12502 error_at (token
->location
, "break statement used with OpenMP for loop");
12505 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12509 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12512 error_at (token
->location
, "continue statement not within a loop");
12514 /* Fall through. */
12515 case IN_ITERATION_STMT
:
12517 statement
= finish_continue_stmt ();
12520 error_at (token
->location
, "invalid exit from OpenMP structured block");
12523 gcc_unreachable ();
12525 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12531 bool expr_non_constant_p
;
12533 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12535 cp_lexer_set_source_position (parser
->lexer
);
12536 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12537 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12539 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12540 expr
= cp_parser_expression (parser
);
12542 /* If the next token is a `;', then there is no
12545 /* Build the return-statement. */
12546 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12547 /* Don't deduce from a discarded return statement. */;
12549 statement
= finish_return_stmt (expr
);
12550 /* Look for the final `;'. */
12551 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12556 if (parser
->in_function_body
12557 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12559 error ("%<goto%> in %<constexpr%> function");
12560 cp_function_chain
->invalid_constexpr
= true;
12563 /* Create the goto-statement. */
12564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12566 /* Issue a warning about this use of a GNU extension. */
12567 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12568 /* Consume the '*' token. */
12569 cp_lexer_consume_token (parser
->lexer
);
12570 /* Parse the dependent expression. */
12571 finish_goto_stmt (cp_parser_expression (parser
));
12574 finish_goto_stmt (cp_parser_identifier (parser
));
12575 /* Look for the final `;'. */
12576 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12580 cp_parser_error (parser
, "expected jump-statement");
12587 /* Parse a declaration-statement.
12589 declaration-statement:
12590 block-declaration */
12593 cp_parser_declaration_statement (cp_parser
* parser
)
12597 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12598 p
= obstack_alloc (&declarator_obstack
, 0);
12600 /* Parse the block-declaration. */
12601 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12603 /* Free any declarators allocated. */
12604 obstack_free (&declarator_obstack
, p
);
12607 /* Some dependent statements (like `if (cond) statement'), are
12608 implicitly in their own scope. In other words, if the statement is
12609 a single statement (as opposed to a compound-statement), it is
12610 none-the-less treated as if it were enclosed in braces. Any
12611 declarations appearing in the dependent statement are out of scope
12612 after control passes that point. This function parses a statement,
12613 but ensures that is in its own scope, even if it is not a
12614 compound-statement.
12616 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12617 is a (possibly labeled) if statement which is not enclosed in
12618 braces and has an else clause. This is used to implement
12621 CHAIN is a vector of if-else-if conditions. This is used to implement
12624 Returns the new statement. */
12627 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12628 const token_indent_info
&guard_tinfo
,
12632 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12633 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12634 token_indent_info body_tinfo
12635 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12640 /* Mark if () ; with a special NOP_EXPR. */
12641 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12643 cp_lexer_consume_token (parser
->lexer
);
12644 statement
= add_stmt (build_empty_stmt (body_loc
));
12646 if (guard_tinfo
.keyword
== RID_IF
12647 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12648 warning_at (body_loc
, OPT_Wempty_body
,
12649 "suggest braces around empty body in an %<if%> statement");
12650 else if (guard_tinfo
.keyword
== RID_ELSE
)
12651 warning_at (body_loc
, OPT_Wempty_body
,
12652 "suggest braces around empty body in an %<else%> statement");
12654 /* if a compound is opened, we simply parse the statement directly. */
12655 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12656 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12657 /* If the token is not a `{', then we must take special action. */
12660 /* Create a compound-statement. */
12661 statement
= begin_compound_stmt (0);
12662 /* Parse the dependent-statement. */
12663 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12664 &body_loc_after_labels
);
12665 /* Finish the dummy compound-statement. */
12666 finish_compound_stmt (statement
);
12669 token_indent_info next_tinfo
12670 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12671 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12673 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12674 && next_tinfo
.type
!= CPP_SEMICOLON
)
12675 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12676 guard_tinfo
.location
, guard_tinfo
.keyword
);
12678 /* Return the statement. */
12682 /* For some dependent statements (like `while (cond) statement'), we
12683 have already created a scope. Therefore, even if the dependent
12684 statement is a compound-statement, we do not want to create another
12688 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12689 const token_indent_info
&guard_tinfo
)
12691 /* If the token is a `{', then we must take special action. */
12692 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12694 token_indent_info body_tinfo
12695 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12696 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12698 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12699 &loc_after_labels
);
12700 token_indent_info next_tinfo
12701 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12702 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12704 if (loc_after_labels
!= UNKNOWN_LOCATION
12705 && next_tinfo
.type
!= CPP_SEMICOLON
)
12706 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12707 guard_tinfo
.location
,
12708 guard_tinfo
.keyword
);
12712 /* Avoid calling cp_parser_compound_statement, so that we
12713 don't create a new scope. Do everything else by hand. */
12714 matching_braces braces
;
12715 braces
.require_open (parser
);
12716 /* If the next keyword is `__label__' we have a label declaration. */
12717 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12718 cp_parser_label_declaration (parser
);
12719 /* Parse an (optional) statement-seq. */
12720 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12721 braces
.require_close (parser
);
12725 /* Declarations [gram.dcl.dcl] */
12727 /* Parse an optional declaration-sequence.
12731 declaration-seq declaration */
12734 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12740 token
= cp_lexer_peek_token (parser
->lexer
);
12742 if (token
->type
== CPP_CLOSE_BRACE
12743 || token
->type
== CPP_EOF
12744 || token
->type
== CPP_PRAGMA_EOL
)
12747 if (token
->type
== CPP_SEMICOLON
)
12749 /* A declaration consisting of a single semicolon is
12750 invalid. Allow it unless we're being pedantic. */
12751 cp_lexer_consume_token (parser
->lexer
);
12752 if (!in_system_header_at (input_location
))
12753 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12757 /* If we're entering or exiting a region that's implicitly
12758 extern "C", modify the lang context appropriately. */
12759 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12761 push_lang_context (lang_name_c
);
12762 parser
->implicit_extern_c
= true;
12764 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12766 pop_lang_context ();
12767 parser
->implicit_extern_c
= false;
12770 if (token
->type
== CPP_PRAGMA
)
12772 /* A top-level declaration can consist solely of a #pragma.
12773 A nested declaration cannot, so this is done here and not
12774 in cp_parser_declaration. (A #pragma at block scope is
12775 handled in cp_parser_statement.) */
12776 cp_parser_pragma (parser
, pragma_external
, NULL
);
12780 /* Parse the declaration itself. */
12781 cp_parser_declaration (parser
);
12785 /* Parse a declaration.
12789 function-definition
12790 template-declaration
12791 explicit-instantiation
12792 explicit-specialization
12793 linkage-specification
12794 namespace-definition
12802 __extension__ declaration */
12805 cp_parser_declaration (cp_parser
* parser
)
12809 int saved_pedantic
;
12811 tree attributes
= NULL_TREE
;
12813 /* Check for the `__extension__' keyword. */
12814 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12816 /* Parse the qualified declaration. */
12817 cp_parser_declaration (parser
);
12818 /* Restore the PEDANTIC flag. */
12819 pedantic
= saved_pedantic
;
12824 /* Try to figure out what kind of declaration is present. */
12825 token1
= *cp_lexer_peek_token (parser
->lexer
);
12827 if (token1
.type
!= CPP_EOF
)
12828 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12831 token2
.type
= CPP_EOF
;
12832 token2
.keyword
= RID_MAX
;
12835 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12836 p
= obstack_alloc (&declarator_obstack
, 0);
12838 /* If the next token is `extern' and the following token is a string
12839 literal, then we have a linkage specification. */
12840 if (token1
.keyword
== RID_EXTERN
12841 && cp_parser_is_pure_string_literal (&token2
))
12842 cp_parser_linkage_specification (parser
);
12843 /* If the next token is `template', then we have either a template
12844 declaration, an explicit instantiation, or an explicit
12846 else if (token1
.keyword
== RID_TEMPLATE
)
12848 /* `template <>' indicates a template specialization. */
12849 if (token2
.type
== CPP_LESS
12850 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12851 cp_parser_explicit_specialization (parser
);
12852 /* `template <' indicates a template declaration. */
12853 else if (token2
.type
== CPP_LESS
)
12854 cp_parser_template_declaration (parser
, /*member_p=*/false);
12855 /* Anything else must be an explicit instantiation. */
12857 cp_parser_explicit_instantiation (parser
);
12859 /* If the next token is `export', then we have a template
12861 else if (token1
.keyword
== RID_EXPORT
)
12862 cp_parser_template_declaration (parser
, /*member_p=*/false);
12863 /* If the next token is `extern', 'static' or 'inline' and the one
12864 after that is `template', we have a GNU extended explicit
12865 instantiation directive. */
12866 else if (cp_parser_allow_gnu_extensions_p (parser
)
12867 && (token1
.keyword
== RID_EXTERN
12868 || token1
.keyword
== RID_STATIC
12869 || token1
.keyword
== RID_INLINE
)
12870 && token2
.keyword
== RID_TEMPLATE
)
12871 cp_parser_explicit_instantiation (parser
);
12872 /* If the next token is `namespace', check for a named or unnamed
12873 namespace definition. */
12874 else if (token1
.keyword
== RID_NAMESPACE
12875 && (/* A named namespace definition. */
12876 (token2
.type
== CPP_NAME
12877 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12879 || (token2
.type
== CPP_OPEN_SQUARE
12880 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12881 == CPP_OPEN_SQUARE
)
12882 /* An unnamed namespace definition. */
12883 || token2
.type
== CPP_OPEN_BRACE
12884 || token2
.keyword
== RID_ATTRIBUTE
))
12885 cp_parser_namespace_definition (parser
);
12886 /* An inline (associated) namespace definition. */
12887 else if (token1
.keyword
== RID_INLINE
12888 && token2
.keyword
== RID_NAMESPACE
)
12889 cp_parser_namespace_definition (parser
);
12890 /* Objective-C++ declaration/definition. */
12891 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12892 cp_parser_objc_declaration (parser
, NULL_TREE
);
12893 else if (c_dialect_objc ()
12894 && token1
.keyword
== RID_ATTRIBUTE
12895 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12896 cp_parser_objc_declaration (parser
, attributes
);
12897 /* At this point we may have a template declared by a concept
12899 else if (flag_concepts
12900 && cp_parser_template_declaration_after_export (parser
,
12901 /*member_p=*/false))
12904 /* Try to parse a block-declaration, or a function-definition. */
12905 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12907 /* Free any declarators allocated. */
12908 obstack_free (&declarator_obstack
, p
);
12911 /* Parse a block-declaration.
12916 namespace-alias-definition
12923 __extension__ block-declaration
12928 static_assert-declaration
12930 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12931 part of a declaration-statement. */
12934 cp_parser_block_declaration (cp_parser
*parser
,
12938 int saved_pedantic
;
12940 /* Check for the `__extension__' keyword. */
12941 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12943 /* Parse the qualified declaration. */
12944 cp_parser_block_declaration (parser
, statement_p
);
12945 /* Restore the PEDANTIC flag. */
12946 pedantic
= saved_pedantic
;
12951 /* Peek at the next token to figure out which kind of declaration is
12953 token1
= cp_lexer_peek_token (parser
->lexer
);
12955 /* If the next keyword is `asm', we have an asm-definition. */
12956 if (token1
->keyword
== RID_ASM
)
12959 cp_parser_commit_to_tentative_parse (parser
);
12960 cp_parser_asm_definition (parser
);
12962 /* If the next keyword is `namespace', we have a
12963 namespace-alias-definition. */
12964 else if (token1
->keyword
== RID_NAMESPACE
)
12965 cp_parser_namespace_alias_definition (parser
);
12966 /* If the next keyword is `using', we have a
12967 using-declaration, a using-directive, or an alias-declaration. */
12968 else if (token1
->keyword
== RID_USING
)
12973 cp_parser_commit_to_tentative_parse (parser
);
12974 /* If the token after `using' is `namespace', then we have a
12975 using-directive. */
12976 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12977 if (token2
->keyword
== RID_NAMESPACE
)
12978 cp_parser_using_directive (parser
);
12979 /* If the second token after 'using' is '=', then we have an
12980 alias-declaration. */
12981 else if (cxx_dialect
>= cxx11
12982 && token2
->type
== CPP_NAME
12983 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12984 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12985 cp_parser_alias_declaration (parser
);
12986 /* Otherwise, it's a using-declaration. */
12988 cp_parser_using_declaration (parser
,
12989 /*access_declaration_p=*/false);
12991 /* If the next keyword is `__label__' we have a misplaced label
12993 else if (token1
->keyword
== RID_LABEL
)
12995 cp_lexer_consume_token (parser
->lexer
);
12996 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12997 cp_parser_skip_to_end_of_statement (parser
);
12998 /* If the next token is now a `;', consume it. */
12999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13000 cp_lexer_consume_token (parser
->lexer
);
13002 /* If the next token is `static_assert' we have a static assertion. */
13003 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13004 cp_parser_static_assert (parser
, /*member_p=*/false);
13005 /* Anything else must be a simple-declaration. */
13007 cp_parser_simple_declaration (parser
, !statement_p
,
13008 /*maybe_range_for_decl*/NULL
);
13011 /* Parse a simple-declaration.
13013 simple-declaration:
13014 decl-specifier-seq [opt] init-declarator-list [opt] ;
13015 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13016 brace-or-equal-initializer ;
13018 init-declarator-list:
13020 init-declarator-list , init-declarator
13022 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13023 function-definition as a simple-declaration.
13025 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13026 parsed declaration if it is an uninitialized single declarator not followed
13027 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13028 if present, will not be consumed. */
13031 cp_parser_simple_declaration (cp_parser
* parser
,
13032 bool function_definition_allowed_p
,
13033 tree
*maybe_range_for_decl
)
13035 cp_decl_specifier_seq decl_specifiers
;
13036 int declares_class_or_enum
;
13037 bool saw_declarator
;
13038 location_t comma_loc
= UNKNOWN_LOCATION
;
13039 location_t init_loc
= UNKNOWN_LOCATION
;
13041 if (maybe_range_for_decl
)
13042 *maybe_range_for_decl
= NULL_TREE
;
13044 /* Defer access checks until we know what is being declared; the
13045 checks for names appearing in the decl-specifier-seq should be
13046 done as if we were in the scope of the thing being declared. */
13047 push_deferring_access_checks (dk_deferred
);
13049 /* Parse the decl-specifier-seq. We have to keep track of whether
13050 or not the decl-specifier-seq declares a named class or
13051 enumeration type, since that is the only case in which the
13052 init-declarator-list is allowed to be empty.
13056 In a simple-declaration, the optional init-declarator-list can be
13057 omitted only when declaring a class or enumeration, that is when
13058 the decl-specifier-seq contains either a class-specifier, an
13059 elaborated-type-specifier, or an enum-specifier. */
13060 cp_parser_decl_specifier_seq (parser
,
13061 CP_PARSER_FLAGS_OPTIONAL
,
13063 &declares_class_or_enum
);
13064 /* We no longer need to defer access checks. */
13065 stop_deferring_access_checks ();
13067 /* In a block scope, a valid declaration must always have a
13068 decl-specifier-seq. By not trying to parse declarators, we can
13069 resolve the declaration/expression ambiguity more quickly. */
13070 if (!function_definition_allowed_p
13071 && !decl_specifiers
.any_specifiers_p
)
13073 cp_parser_error (parser
, "expected declaration");
13077 /* If the next two tokens are both identifiers, the code is
13078 erroneous. The usual cause of this situation is code like:
13082 where "T" should name a type -- but does not. */
13083 if (!decl_specifiers
.any_type_specifiers_p
13084 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13086 /* If parsing tentatively, we should commit; we really are
13087 looking at a declaration. */
13088 cp_parser_commit_to_tentative_parse (parser
);
13093 cp_parser_maybe_commit_to_declaration (parser
,
13094 decl_specifiers
.any_specifiers_p
);
13096 /* Look for C++17 decomposition declaration. */
13097 for (size_t n
= 1; ; n
++)
13098 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13099 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13101 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13102 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13103 && decl_specifiers
.any_specifiers_p
)
13106 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13107 maybe_range_for_decl
,
13110 /* The next token should be either a `,' or a `;'. */
13111 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13112 /* If it's a `;', we are done. */
13113 if (token
->type
== CPP_SEMICOLON
)
13115 else if (maybe_range_for_decl
)
13117 if (*maybe_range_for_decl
== NULL_TREE
)
13118 *maybe_range_for_decl
= error_mark_node
;
13121 /* Anything else is an error. */
13124 /* If we have already issued an error message we don't need
13125 to issue another one. */
13126 if ((decl
!= error_mark_node
13127 && DECL_INITIAL (decl
) != error_mark_node
)
13128 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13129 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13130 /* Skip tokens until we reach the end of the statement. */
13131 cp_parser_skip_to_end_of_statement (parser
);
13132 /* If the next token is now a `;', consume it. */
13133 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13134 cp_lexer_consume_token (parser
->lexer
);
13142 bool auto_specifier_p
;
13143 /* NULL_TREE if both variable and function declaration are allowed,
13144 error_mark_node if function declaration are not allowed and
13145 a FUNCTION_DECL that should be diagnosed if it is followed by
13146 variable declarations. */
13147 tree auto_function_declaration
;
13149 last_type
= NULL_TREE
;
13151 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13152 auto_function_declaration
= NULL_TREE
;
13154 /* Keep going until we hit the `;' at the end of the simple
13156 saw_declarator
= false;
13157 while (cp_lexer_next_token_is_not (parser
->lexer
,
13161 bool function_definition_p
;
13163 tree auto_result
= NULL_TREE
;
13165 if (saw_declarator
)
13167 /* If we are processing next declarator, comma is expected */
13168 token
= cp_lexer_peek_token (parser
->lexer
);
13169 gcc_assert (token
->type
== CPP_COMMA
);
13170 cp_lexer_consume_token (parser
->lexer
);
13171 if (maybe_range_for_decl
)
13173 *maybe_range_for_decl
= error_mark_node
;
13174 if (comma_loc
== UNKNOWN_LOCATION
)
13175 comma_loc
= token
->location
;
13179 saw_declarator
= true;
13181 /* Parse the init-declarator. */
13182 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13184 function_definition_allowed_p
,
13185 /*member_p=*/false,
13186 declares_class_or_enum
,
13187 &function_definition_p
,
13188 maybe_range_for_decl
,
13191 /* If an error occurred while parsing tentatively, exit quickly.
13192 (That usually happens when in the body of a function; each
13193 statement is treated as a declaration-statement until proven
13195 if (cp_parser_error_occurred (parser
))
13198 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13200 /* If the init-declarator-list contains more than one
13201 init-declarator, they shall all form declarations of
13203 if (auto_function_declaration
== NULL_TREE
)
13204 auto_function_declaration
13205 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13206 else if (TREE_CODE (decl
) == FUNCTION_DECL
13207 || auto_function_declaration
!= error_mark_node
)
13209 error_at (decl_specifiers
.locations
[ds_type_spec
],
13210 "non-variable %qD in declaration with more than one "
13211 "declarator with placeholder type",
13212 TREE_CODE (decl
) == FUNCTION_DECL
13213 ? decl
: auto_function_declaration
);
13214 auto_function_declaration
= error_mark_node
;
13219 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13222 && last_type
!= error_mark_node
13223 && !same_type_p (auto_result
, last_type
))
13225 /* If the list of declarators contains more than one declarator,
13226 the type of each declared variable is determined as described
13227 above. If the type deduced for the template parameter U is not
13228 the same in each deduction, the program is ill-formed. */
13229 error_at (decl_specifiers
.locations
[ds_type_spec
],
13230 "inconsistent deduction for %qT: %qT and then %qT",
13231 decl_specifiers
.type
, last_type
, auto_result
);
13232 last_type
= error_mark_node
;
13235 last_type
= auto_result
;
13238 /* Handle function definitions specially. */
13239 if (function_definition_p
)
13241 /* If the next token is a `,', then we are probably
13242 processing something like:
13246 which is erroneous. */
13247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13249 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13250 error_at (token
->location
,
13252 " declarations and function-definitions is forbidden");
13254 /* Otherwise, we're done with the list of declarators. */
13257 pop_deferring_access_checks ();
13261 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13262 *maybe_range_for_decl
= decl
;
13263 /* The next token should be either a `,' or a `;'. */
13264 token
= cp_lexer_peek_token (parser
->lexer
);
13265 /* If it's a `,', there are more declarators to come. */
13266 if (token
->type
== CPP_COMMA
)
13267 /* will be consumed next time around */;
13268 /* If it's a `;', we are done. */
13269 else if (token
->type
== CPP_SEMICOLON
)
13271 else if (maybe_range_for_decl
)
13273 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13274 permerror (decl_specifiers
.locations
[ds_type_spec
],
13275 "types may not be defined in a for-range-declaration");
13278 /* Anything else is an error. */
13281 /* If we have already issued an error message we don't need
13282 to issue another one. */
13283 if ((decl
!= error_mark_node
13284 && DECL_INITIAL (decl
) != error_mark_node
)
13285 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13286 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13287 /* Skip tokens until we reach the end of the statement. */
13288 cp_parser_skip_to_end_of_statement (parser
);
13289 /* If the next token is now a `;', consume it. */
13290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13291 cp_lexer_consume_token (parser
->lexer
);
13294 /* After the first time around, a function-definition is not
13295 allowed -- even if it was OK at first. For example:
13300 function_definition_allowed_p
= false;
13303 /* Issue an error message if no declarators are present, and the
13304 decl-specifier-seq does not itself declare a class or
13305 enumeration: [dcl.dcl]/3. */
13306 if (!saw_declarator
)
13308 if (cp_parser_declares_only_class_p (parser
))
13310 if (!declares_class_or_enum
13311 && decl_specifiers
.type
13312 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13313 /* Ensure an error is issued anyway when finish_decltype_type,
13314 called via cp_parser_decl_specifier_seq, returns a class or
13315 an enumeration (c++/51786). */
13316 decl_specifiers
.type
= NULL_TREE
;
13317 shadow_tag (&decl_specifiers
);
13319 /* Perform any deferred access checks. */
13320 perform_deferred_access_checks (tf_warning_or_error
);
13323 /* Consume the `;'. */
13325 if (!maybe_range_for_decl
)
13326 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13327 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13329 if (init_loc
!= UNKNOWN_LOCATION
)
13330 error_at (init_loc
, "initializer in range-based %<for%> loop");
13331 if (comma_loc
!= UNKNOWN_LOCATION
)
13332 error_at (comma_loc
,
13333 "multiple declarations in range-based %<for%> loop");
13337 pop_deferring_access_checks ();
13340 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13341 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13345 cp_parser_decomposition_declaration (cp_parser
*parser
,
13346 cp_decl_specifier_seq
*decl_specifiers
,
13347 tree
*maybe_range_for_decl
,
13348 location_t
*init_loc
)
13350 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13351 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13352 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13354 /* Parse the identifier-list. */
13355 auto_vec
<cp_expr
, 10> v
;
13356 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13359 cp_expr e
= cp_parser_identifier (parser
);
13360 if (e
.get_value () == error_mark_node
)
13363 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13365 cp_lexer_consume_token (parser
->lexer
);
13368 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13369 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13371 end_loc
= UNKNOWN_LOCATION
;
13372 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13374 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13375 cp_lexer_consume_token (parser
->lexer
);
13378 cp_parser_skip_to_end_of_statement (parser
);
13379 return error_mark_node
;
13383 if (cxx_dialect
< cxx17
)
13384 pedwarn (loc
, 0, "structured bindings only available with "
13385 "-std=c++17 or -std=gnu++17");
13388 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13389 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13390 declarator
->id_loc
= loc
;
13391 if (ref_qual
!= REF_QUAL_NONE
)
13392 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13393 ref_qual
== REF_QUAL_RVALUE
,
13395 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13396 NULL_TREE
, decl_specifiers
->attributes
,
13398 tree orig_decl
= decl
;
13402 cp_decl_specifier_seq decl_specs
;
13403 clear_decl_specs (&decl_specs
);
13404 decl_specs
.type
= make_auto ();
13406 FOR_EACH_VEC_ELT (v
, i
, e
)
13409 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13411 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13412 declarator
->id_loc
= e
.get_location ();
13413 tree elt_pushed_scope
;
13414 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13415 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13416 if (decl2
== error_mark_node
)
13417 decl
= error_mark_node
;
13418 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13420 /* Ensure we've diagnosed redeclaration if we aren't creating
13422 gcc_assert (errorcount
);
13423 decl
= error_mark_node
;
13427 if (elt_pushed_scope
)
13428 pop_scope (elt_pushed_scope
);
13433 error_at (loc
, "empty structured binding declaration");
13434 decl
= error_mark_node
;
13437 if (maybe_range_for_decl
== NULL
13438 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13440 bool non_constant_p
= false, is_direct_init
= false;
13441 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13442 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13444 if (initializer
== NULL_TREE
13445 || (TREE_CODE (initializer
) == TREE_LIST
13446 && TREE_CHAIN (initializer
))
13448 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13449 && CONSTRUCTOR_NELTS (initializer
) != 1))
13451 error_at (loc
, "invalid initializer for structured binding "
13453 initializer
= error_mark_node
;
13456 if (decl
!= error_mark_node
)
13458 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13459 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13460 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13461 cp_finish_decomp (decl
, prev
, v
.length ());
13464 else if (decl
!= error_mark_node
)
13466 *maybe_range_for_decl
= prev
;
13467 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13468 the underlying DECL. */
13469 cp_finish_decomp (decl
, prev
, v
.length ());
13473 pop_scope (pushed_scope
);
13475 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13477 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13478 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13484 /* Parse a decl-specifier-seq.
13486 decl-specifier-seq:
13487 decl-specifier-seq [opt] decl-specifier
13488 decl-specifier attribute-specifier-seq [opt] (C++11)
13491 storage-class-specifier
13502 Concepts Extension:
13507 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13509 The parser flags FLAGS is used to control type-specifier parsing.
13511 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13514 1: one of the decl-specifiers is an elaborated-type-specifier
13515 (i.e., a type declaration)
13516 2: one of the decl-specifiers is an enum-specifier or a
13517 class-specifier (i.e., a type definition)
13522 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13523 cp_parser_flags flags
,
13524 cp_decl_specifier_seq
*decl_specs
,
13525 int* declares_class_or_enum
)
13527 bool constructor_possible_p
= !parser
->in_declarator_p
;
13528 bool found_decl_spec
= false;
13529 cp_token
*start_token
= NULL
;
13532 /* Clear DECL_SPECS. */
13533 clear_decl_specs (decl_specs
);
13535 /* Assume no class or enumeration type is declared. */
13536 *declares_class_or_enum
= 0;
13538 /* Keep reading specifiers until there are no more to read. */
13541 bool constructor_p
;
13545 /* Peek at the next token. */
13546 token
= cp_lexer_peek_token (parser
->lexer
);
13548 /* Save the first token of the decl spec list for error
13551 start_token
= token
;
13552 /* Handle attributes. */
13553 if (cp_next_tokens_can_be_attribute_p (parser
))
13555 /* Parse the attributes. */
13556 tree attrs
= cp_parser_attributes_opt (parser
);
13558 /* In a sequence of declaration specifiers, c++11 attributes
13559 appertain to the type that precede them. In that case
13562 The attribute-specifier-seq affects the type only for
13563 the declaration it appears in, not other declarations
13564 involving the same type.
13566 But for now let's force the user to position the
13567 attribute either at the beginning of the declaration or
13568 after the declarator-id, which would clearly mean that it
13569 applies to the declarator. */
13570 if (cxx11_attribute_p (attrs
))
13572 if (!found_decl_spec
)
13573 /* The c++11 attribute is at the beginning of the
13574 declaration. It appertains to the entity being
13578 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13580 /* This is an attribute following a
13581 class-specifier. */
13582 if (decl_specs
->type_definition_p
)
13583 warn_misplaced_attr_for_class_type (token
->location
,
13589 decl_specs
->std_attributes
13590 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13591 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13592 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13598 decl_specs
->attributes
13599 = attr_chainon (decl_specs
->attributes
, attrs
);
13600 if (decl_specs
->locations
[ds_attribute
] == 0)
13601 decl_specs
->locations
[ds_attribute
] = token
->location
;
13604 /* Assume we will find a decl-specifier keyword. */
13605 found_decl_spec
= true;
13606 /* If the next token is an appropriate keyword, we can simply
13607 add it to the list. */
13608 switch (token
->keyword
)
13614 if (!at_class_scope_p ())
13616 gcc_rich_location
richloc (token
->location
);
13617 richloc
.add_fixit_remove ();
13618 error_at (&richloc
, "%<friend%> used outside of class");
13619 cp_lexer_purge_token (parser
->lexer
);
13624 /* Consume the token. */
13625 cp_lexer_consume_token (parser
->lexer
);
13629 case RID_CONSTEXPR
:
13631 cp_lexer_consume_token (parser
->lexer
);
13636 cp_lexer_consume_token (parser
->lexer
);
13639 /* function-specifier:
13646 cp_parser_function_specifier_opt (parser
, decl_specs
);
13653 /* Consume the token. */
13654 cp_lexer_consume_token (parser
->lexer
);
13655 /* A constructor declarator cannot appear in a typedef. */
13656 constructor_possible_p
= false;
13657 /* The "typedef" keyword can only occur in a declaration; we
13658 may as well commit at this point. */
13659 cp_parser_commit_to_tentative_parse (parser
);
13661 if (decl_specs
->storage_class
!= sc_none
)
13662 decl_specs
->conflicting_specifiers_p
= true;
13665 /* storage-class-specifier:
13675 if (cxx_dialect
== cxx98
)
13677 /* Consume the token. */
13678 cp_lexer_consume_token (parser
->lexer
);
13680 /* Complain about `auto' as a storage specifier, if
13681 we're complaining about C++0x compatibility. */
13682 gcc_rich_location
richloc (token
->location
);
13683 richloc
.add_fixit_remove ();
13684 warning_at (&richloc
, OPT_Wc__11_compat
,
13685 "%<auto%> changes meaning in C++11; "
13686 "please remove it");
13688 /* Set the storage class anyway. */
13689 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13693 /* C++0x auto type-specifier. */
13694 found_decl_spec
= false;
13701 /* Consume the token. */
13702 cp_lexer_consume_token (parser
->lexer
);
13703 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13707 /* Consume the token. */
13709 cp_lexer_consume_token (parser
->lexer
);
13713 /* We did not yet find a decl-specifier yet. */
13714 found_decl_spec
= false;
13718 if (found_decl_spec
13719 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13720 && token
->keyword
!= RID_CONSTEXPR
)
13721 error ("decl-specifier invalid in condition");
13723 if (found_decl_spec
13724 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13725 && token
->keyword
!= RID_MUTABLE
13726 && token
->keyword
!= RID_CONSTEXPR
)
13727 error_at (token
->location
, "%qD invalid in lambda",
13728 ridpointers
[token
->keyword
]);
13731 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13733 /* Constructors are a special case. The `S' in `S()' is not a
13734 decl-specifier; it is the beginning of the declarator. */
13736 = (!found_decl_spec
13737 && constructor_possible_p
13738 && (cp_parser_constructor_declarator_p
13739 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13741 /* If we don't have a DECL_SPEC yet, then we must be looking at
13742 a type-specifier. */
13743 if (!found_decl_spec
&& !constructor_p
)
13745 int decl_spec_declares_class_or_enum
;
13746 bool is_cv_qualifier
;
13750 = cp_parser_type_specifier (parser
, flags
,
13752 /*is_declaration=*/true,
13753 &decl_spec_declares_class_or_enum
,
13755 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13757 /* If this type-specifier referenced a user-defined type
13758 (a typedef, class-name, etc.), then we can't allow any
13759 more such type-specifiers henceforth.
13763 The longest sequence of decl-specifiers that could
13764 possibly be a type name is taken as the
13765 decl-specifier-seq of a declaration. The sequence shall
13766 be self-consistent as described below.
13770 As a general rule, at most one type-specifier is allowed
13771 in the complete decl-specifier-seq of a declaration. The
13772 only exceptions are the following:
13774 -- const or volatile can be combined with any other
13777 -- signed or unsigned can be combined with char, long,
13785 void g (const int Pc);
13787 Here, Pc is *not* part of the decl-specifier seq; it's
13788 the declarator. Therefore, once we see a type-specifier
13789 (other than a cv-qualifier), we forbid any additional
13790 user-defined types. We *do* still allow things like `int
13791 int' to be considered a decl-specifier-seq, and issue the
13792 error message later. */
13793 if (type_spec
&& !is_cv_qualifier
)
13794 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13795 /* A constructor declarator cannot follow a type-specifier. */
13798 constructor_possible_p
= false;
13799 found_decl_spec
= true;
13800 if (!is_cv_qualifier
)
13801 decl_specs
->any_type_specifiers_p
= true;
13803 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
13804 error_at (token
->location
, "type-specifier invalid in lambda");
13808 /* If we still do not have a DECL_SPEC, then there are no more
13809 decl-specifiers. */
13810 if (!found_decl_spec
)
13813 decl_specs
->any_specifiers_p
= true;
13814 /* After we see one decl-specifier, further decl-specifiers are
13815 always optional. */
13816 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13819 /* Don't allow a friend specifier with a class definition. */
13820 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13821 && (*declares_class_or_enum
& 2))
13822 error_at (decl_specs
->locations
[ds_friend
],
13823 "class definition may not be declared a friend");
13826 /* Parse an (optional) storage-class-specifier.
13828 storage-class-specifier:
13837 storage-class-specifier:
13840 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13843 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13845 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13848 if (cxx_dialect
!= cxx98
)
13850 /* Fall through for C++98. */
13851 gcc_fallthrough ();
13858 /* Consume the token. */
13859 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13866 /* Parse an (optional) function-specifier.
13868 function-specifier:
13873 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13874 Updates DECL_SPECS, if it is non-NULL. */
13877 cp_parser_function_specifier_opt (cp_parser
* parser
,
13878 cp_decl_specifier_seq
*decl_specs
)
13880 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13881 switch (token
->keyword
)
13884 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13888 /* 14.5.2.3 [temp.mem]
13890 A member function template shall not be virtual. */
13891 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13892 && current_class_type
)
13893 error_at (token
->location
, "templates may not be %<virtual%>");
13895 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13899 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13906 /* Consume the token. */
13907 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13910 /* Parse a linkage-specification.
13912 linkage-specification:
13913 extern string-literal { declaration-seq [opt] }
13914 extern string-literal declaration */
13917 cp_parser_linkage_specification (cp_parser
* parser
)
13921 /* Look for the `extern' keyword. */
13922 cp_token
*extern_token
13923 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13925 /* Look for the string-literal. */
13926 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13927 linkage
= cp_parser_string_literal (parser
, false, false);
13929 /* Transform the literal into an identifier. If the literal is a
13930 wide-character string, or contains embedded NULs, then we can't
13931 handle it as the user wants. */
13932 if (strlen (TREE_STRING_POINTER (linkage
))
13933 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13935 cp_parser_error (parser
, "invalid linkage-specification");
13936 /* Assume C++ linkage. */
13937 linkage
= lang_name_cplusplus
;
13940 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13942 /* We're now using the new linkage. */
13943 push_lang_context (linkage
);
13945 /* Preserve the location of the the innermost linkage specification,
13946 tracking the locations of nested specifications via a local. */
13947 location_t saved_location
13948 = parser
->innermost_linkage_specification_location
;
13949 /* Construct a location ranging from the start of the "extern" to
13950 the end of the string-literal, with the caret at the start, e.g.:
13954 parser
->innermost_linkage_specification_location
13955 = make_location (extern_token
->location
,
13956 extern_token
->location
,
13957 get_finish (string_token
->location
));
13959 /* If the next token is a `{', then we're using the first
13961 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13963 cp_ensure_no_omp_declare_simd (parser
);
13964 cp_ensure_no_oacc_routine (parser
);
13966 /* Consume the `{' token. */
13967 matching_braces braces
;
13968 braces
.consume_open (parser
)->location
;
13969 /* Parse the declarations. */
13970 cp_parser_declaration_seq_opt (parser
);
13971 /* Look for the closing `}'. */
13972 braces
.require_close (parser
);
13974 /* Otherwise, there's just one declaration. */
13977 bool saved_in_unbraced_linkage_specification_p
;
13979 saved_in_unbraced_linkage_specification_p
13980 = parser
->in_unbraced_linkage_specification_p
;
13981 parser
->in_unbraced_linkage_specification_p
= true;
13982 cp_parser_declaration (parser
);
13983 parser
->in_unbraced_linkage_specification_p
13984 = saved_in_unbraced_linkage_specification_p
;
13987 /* We're done with the linkage-specification. */
13988 pop_lang_context ();
13990 /* Restore location of parent linkage specification, if any. */
13991 parser
->innermost_linkage_specification_location
= saved_location
;
13994 /* Parse a static_assert-declaration.
13996 static_assert-declaration:
13997 static_assert ( constant-expression , string-literal ) ;
13998 static_assert ( constant-expression ) ; (C++17)
14000 If MEMBER_P, this static_assert is a class member. */
14003 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14006 location_t token_loc
;
14010 /* Peek at the `static_assert' token so we can keep track of exactly
14011 where the static assertion started. */
14012 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14014 /* Look for the `static_assert' keyword. */
14015 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14019 /* We know we are in a static assertion; commit to any tentative
14021 if (cp_parser_parsing_tentatively (parser
))
14022 cp_parser_commit_to_tentative_parse (parser
);
14024 /* Parse the `(' starting the static assertion condition. */
14025 matching_parens parens
;
14026 parens
.require_open (parser
);
14028 /* Parse the constant-expression. Allow a non-constant expression
14029 here in order to give better diagnostics in finish_static_assert. */
14031 cp_parser_constant_expression (parser
,
14032 /*allow_non_constant_p=*/true,
14033 /*non_constant_p=*/&dummy
);
14035 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14037 if (cxx_dialect
< cxx17
)
14038 pedwarn (input_location
, OPT_Wpedantic
,
14039 "static_assert without a message "
14040 "only available with -std=c++17 or -std=gnu++17");
14042 cp_lexer_consume_token (parser
->lexer
);
14043 message
= build_string (1, "");
14044 TREE_TYPE (message
) = char_array_type_node
;
14045 fix_string_type (message
);
14049 /* Parse the separating `,'. */
14050 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14052 /* Parse the string-literal message. */
14053 message
= cp_parser_string_literal (parser
,
14054 /*translate=*/false,
14057 /* A `)' completes the static assertion. */
14058 if (!parens
.require_close (parser
))
14059 cp_parser_skip_to_closing_parenthesis (parser
,
14060 /*recovering=*/true,
14061 /*or_comma=*/false,
14062 /*consume_paren=*/true);
14065 /* A semicolon terminates the declaration. */
14066 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14068 /* Get the location for the static assertion. Use that of the
14069 condition if available, otherwise, use that of the "static_assert"
14071 location_t assert_loc
= condition
.get_location ();
14072 if (assert_loc
== UNKNOWN_LOCATION
)
14073 assert_loc
= token_loc
;
14075 /* Complete the static assertion, which may mean either processing
14076 the static assert now or saving it for template instantiation. */
14077 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14080 /* Parse the expression in decltype ( expression ). */
14083 cp_parser_decltype_expr (cp_parser
*parser
,
14084 bool &id_expression_or_member_access_p
)
14086 cp_token
*id_expr_start_token
;
14089 /* Since we're going to preserve any side-effects from this parse, set up a
14090 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14091 in the expression. */
14092 tentative_firewall
firewall (parser
);
14094 /* First, try parsing an id-expression. */
14095 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14096 cp_parser_parse_tentatively (parser
);
14097 expr
= cp_parser_id_expression (parser
,
14098 /*template_keyword_p=*/false,
14099 /*check_dependency_p=*/true,
14100 /*template_p=*/NULL
,
14101 /*declarator_p=*/false,
14102 /*optional_p=*/false);
14104 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14106 bool non_integral_constant_expression_p
= false;
14107 tree id_expression
= expr
;
14109 const char *error_msg
;
14111 if (identifier_p (expr
))
14112 /* Lookup the name we got back from the id-expression. */
14113 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14114 id_expr_start_token
->location
);
14116 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14117 /* A template without args is not a complete id-expression. */
14118 expr
= error_mark_node
;
14121 && expr
!= error_mark_node
14122 && TREE_CODE (expr
) != TYPE_DECL
14123 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14124 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14125 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14127 /* Complete lookup of the id-expression. */
14128 expr
= (finish_id_expression
14129 (id_expression
, expr
, parser
->scope
, &idk
,
14130 /*integral_constant_expression_p=*/false,
14131 /*allow_non_integral_constant_expression_p=*/true,
14132 &non_integral_constant_expression_p
,
14133 /*template_p=*/false,
14135 /*address_p=*/false,
14136 /*template_arg_p=*/false,
14138 id_expr_start_token
->location
));
14140 if (expr
== error_mark_node
)
14141 /* We found an id-expression, but it was something that we
14142 should not have found. This is an error, not something
14143 we can recover from, so note that we found an
14144 id-expression and we'll recover as gracefully as
14146 id_expression_or_member_access_p
= true;
14150 && expr
!= error_mark_node
14151 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14152 /* We have an id-expression. */
14153 id_expression_or_member_access_p
= true;
14156 if (!id_expression_or_member_access_p
)
14158 /* Abort the id-expression parse. */
14159 cp_parser_abort_tentative_parse (parser
);
14161 /* Parsing tentatively, again. */
14162 cp_parser_parse_tentatively (parser
);
14164 /* Parse a class member access. */
14165 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14166 /*cast_p=*/false, /*decltype*/true,
14167 /*member_access_only_p=*/true, NULL
);
14170 && expr
!= error_mark_node
14171 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14172 /* We have an id-expression. */
14173 id_expression_or_member_access_p
= true;
14176 if (id_expression_or_member_access_p
)
14177 /* We have parsed the complete id-expression or member access. */
14178 cp_parser_parse_definitely (parser
);
14181 /* Abort our attempt to parse an id-expression or member access
14183 cp_parser_abort_tentative_parse (parser
);
14185 /* Commit to the tentative_firewall so we get syntax errors. */
14186 cp_parser_commit_to_tentative_parse (parser
);
14188 /* Parse a full expression. */
14189 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14190 /*decltype_p=*/true);
14196 /* Parse a `decltype' type. Returns the type.
14198 simple-type-specifier:
14199 decltype ( expression )
14201 decltype ( auto ) */
14204 cp_parser_decltype (cp_parser
*parser
)
14206 bool id_expression_or_member_access_p
= false;
14207 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14209 if (start_token
->type
== CPP_DECLTYPE
)
14211 /* Already parsed. */
14212 cp_lexer_consume_token (parser
->lexer
);
14213 return saved_checks_value (start_token
->u
.tree_check_value
);
14216 /* Look for the `decltype' token. */
14217 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14218 return error_mark_node
;
14220 /* Parse the opening `('. */
14221 matching_parens parens
;
14222 if (!parens
.require_open (parser
))
14223 return error_mark_node
;
14225 push_deferring_access_checks (dk_deferred
);
14227 tree expr
= NULL_TREE
;
14229 if (cxx_dialect
>= cxx14
14230 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14231 /* decltype (auto) */
14232 cp_lexer_consume_token (parser
->lexer
);
14235 /* decltype (expression) */
14237 /* Types cannot be defined in a `decltype' expression. Save away the
14238 old message and set the new one. */
14239 const char *saved_message
= parser
->type_definition_forbidden_message
;
14240 parser
->type_definition_forbidden_message
14241 = G_("types may not be defined in %<decltype%> expressions");
14243 /* The restrictions on constant-expressions do not apply inside
14244 decltype expressions. */
14245 bool saved_integral_constant_expression_p
14246 = parser
->integral_constant_expression_p
;
14247 bool saved_non_integral_constant_expression_p
14248 = parser
->non_integral_constant_expression_p
;
14249 parser
->integral_constant_expression_p
= false;
14251 /* Within a parenthesized expression, a `>' token is always
14252 the greater-than operator. */
14253 bool saved_greater_than_is_operator_p
14254 = parser
->greater_than_is_operator_p
;
14255 parser
->greater_than_is_operator_p
= true;
14257 /* Do not actually evaluate the expression. */
14258 ++cp_unevaluated_operand
;
14260 /* Do not warn about problems with the expression. */
14261 ++c_inhibit_evaluation_warnings
;
14263 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14265 /* Go back to evaluating expressions. */
14266 --cp_unevaluated_operand
;
14267 --c_inhibit_evaluation_warnings
;
14269 /* The `>' token might be the end of a template-id or
14270 template-parameter-list now. */
14271 parser
->greater_than_is_operator_p
14272 = saved_greater_than_is_operator_p
;
14274 /* Restore the old message and the integral constant expression
14276 parser
->type_definition_forbidden_message
= saved_message
;
14277 parser
->integral_constant_expression_p
14278 = saved_integral_constant_expression_p
;
14279 parser
->non_integral_constant_expression_p
14280 = saved_non_integral_constant_expression_p
;
14283 /* Parse to the closing `)'. */
14284 if (!parens
.require_close (parser
))
14286 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14287 /*consume_paren=*/true);
14288 pop_deferring_access_checks ();
14289 return error_mark_node
;
14295 expr
= make_decltype_auto ();
14296 AUTO_IS_DECLTYPE (expr
) = true;
14299 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14300 tf_warning_or_error
);
14302 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14304 start_token
->type
= CPP_DECLTYPE
;
14305 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14306 start_token
->u
.tree_check_value
->value
= expr
;
14307 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14308 start_token
->keyword
= RID_MAX
;
14309 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14311 pop_to_parent_deferring_access_checks ();
14316 /* Special member functions [gram.special] */
14318 /* Parse a conversion-function-id.
14320 conversion-function-id:
14321 operator conversion-type-id
14323 Returns an IDENTIFIER_NODE representing the operator. */
14326 cp_parser_conversion_function_id (cp_parser
* parser
)
14330 tree saved_qualifying_scope
;
14331 tree saved_object_scope
;
14332 tree pushed_scope
= NULL_TREE
;
14334 /* Look for the `operator' token. */
14335 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14336 return error_mark_node
;
14337 /* When we parse the conversion-type-id, the current scope will be
14338 reset. However, we need that information in able to look up the
14339 conversion function later, so we save it here. */
14340 saved_scope
= parser
->scope
;
14341 saved_qualifying_scope
= parser
->qualifying_scope
;
14342 saved_object_scope
= parser
->object_scope
;
14343 /* We must enter the scope of the class so that the names of
14344 entities declared within the class are available in the
14345 conversion-type-id. For example, consider:
14352 S::operator I() { ... }
14354 In order to see that `I' is a type-name in the definition, we
14355 must be in the scope of `S'. */
14357 pushed_scope
= push_scope (saved_scope
);
14358 /* Parse the conversion-type-id. */
14359 type
= cp_parser_conversion_type_id (parser
);
14360 /* Leave the scope of the class, if any. */
14362 pop_scope (pushed_scope
);
14363 /* Restore the saved scope. */
14364 parser
->scope
= saved_scope
;
14365 parser
->qualifying_scope
= saved_qualifying_scope
;
14366 parser
->object_scope
= saved_object_scope
;
14367 /* If the TYPE is invalid, indicate failure. */
14368 if (type
== error_mark_node
)
14369 return error_mark_node
;
14370 return make_conv_op_name (type
);
14373 /* Parse a conversion-type-id:
14375 conversion-type-id:
14376 type-specifier-seq conversion-declarator [opt]
14378 Returns the TYPE specified. */
14381 cp_parser_conversion_type_id (cp_parser
* parser
)
14384 cp_decl_specifier_seq type_specifiers
;
14385 cp_declarator
*declarator
;
14386 tree type_specified
;
14387 const char *saved_message
;
14389 /* Parse the attributes. */
14390 attributes
= cp_parser_attributes_opt (parser
);
14392 saved_message
= parser
->type_definition_forbidden_message
;
14393 parser
->type_definition_forbidden_message
14394 = G_("types may not be defined in a conversion-type-id");
14396 /* Parse the type-specifiers. */
14397 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14398 /*is_trailing_return=*/false,
14401 parser
->type_definition_forbidden_message
= saved_message
;
14403 /* If that didn't work, stop. */
14404 if (type_specifiers
.type
== error_mark_node
)
14405 return error_mark_node
;
14406 /* Parse the conversion-declarator. */
14407 declarator
= cp_parser_conversion_declarator_opt (parser
);
14409 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14410 /*initialized=*/0, &attributes
);
14412 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14414 /* Don't give this error when parsing tentatively. This happens to
14415 work because we always parse this definitively once. */
14416 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14417 && type_uses_auto (type_specified
))
14419 if (cxx_dialect
< cxx14
)
14421 error ("invalid use of %<auto%> in conversion operator");
14422 return error_mark_node
;
14424 else if (template_parm_scope_p ())
14425 warning (0, "use of %<auto%> in member template "
14426 "conversion operator can never be deduced");
14429 return type_specified
;
14432 /* Parse an (optional) conversion-declarator.
14434 conversion-declarator:
14435 ptr-operator conversion-declarator [opt]
14439 static cp_declarator
*
14440 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14442 enum tree_code code
;
14443 tree class_type
, std_attributes
= NULL_TREE
;
14444 cp_cv_quals cv_quals
;
14446 /* We don't know if there's a ptr-operator next, or not. */
14447 cp_parser_parse_tentatively (parser
);
14448 /* Try the ptr-operator. */
14449 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14451 /* If it worked, look for more conversion-declarators. */
14452 if (cp_parser_parse_definitely (parser
))
14454 cp_declarator
*declarator
;
14456 /* Parse another optional declarator. */
14457 declarator
= cp_parser_conversion_declarator_opt (parser
);
14459 declarator
= cp_parser_make_indirect_declarator
14460 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14468 /* Parse an (optional) ctor-initializer.
14471 : mem-initializer-list */
14474 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14476 /* If the next token is not a `:', then there is no
14477 ctor-initializer. */
14478 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14480 /* Do default initialization of any bases and members. */
14481 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14482 finish_mem_initializers (NULL_TREE
);
14486 /* Consume the `:' token. */
14487 cp_lexer_consume_token (parser
->lexer
);
14488 /* And the mem-initializer-list. */
14489 cp_parser_mem_initializer_list (parser
);
14492 /* Parse a mem-initializer-list.
14494 mem-initializer-list:
14495 mem-initializer ... [opt]
14496 mem-initializer ... [opt] , mem-initializer-list */
14499 cp_parser_mem_initializer_list (cp_parser
* parser
)
14501 tree mem_initializer_list
= NULL_TREE
;
14502 tree target_ctor
= error_mark_node
;
14503 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14505 /* Let the semantic analysis code know that we are starting the
14506 mem-initializer-list. */
14507 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14508 error_at (token
->location
,
14509 "only constructors take member initializers");
14511 /* Loop through the list. */
14514 tree mem_initializer
;
14516 token
= cp_lexer_peek_token (parser
->lexer
);
14517 /* Parse the mem-initializer. */
14518 mem_initializer
= cp_parser_mem_initializer (parser
);
14519 /* If the next token is a `...', we're expanding member initializers. */
14520 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14522 || (mem_initializer
!= error_mark_node
14523 && check_for_bare_parameter_packs (TREE_PURPOSE
14524 (mem_initializer
))))
14526 /* Consume the `...'. */
14528 cp_lexer_consume_token (parser
->lexer
);
14530 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14531 can be expanded but members cannot. */
14532 if (mem_initializer
!= error_mark_node
14533 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14535 error_at (token
->location
,
14536 "cannot expand initializer for member %qD",
14537 TREE_PURPOSE (mem_initializer
));
14538 mem_initializer
= error_mark_node
;
14541 /* Construct the pack expansion type. */
14542 if (mem_initializer
!= error_mark_node
)
14543 mem_initializer
= make_pack_expansion (mem_initializer
);
14545 if (target_ctor
!= error_mark_node
14546 && mem_initializer
!= error_mark_node
)
14548 error ("mem-initializer for %qD follows constructor delegation",
14549 TREE_PURPOSE (mem_initializer
));
14550 mem_initializer
= error_mark_node
;
14552 /* Look for a target constructor. */
14553 if (mem_initializer
!= error_mark_node
14554 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14555 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14557 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14558 if (mem_initializer_list
)
14560 error ("constructor delegation follows mem-initializer for %qD",
14561 TREE_PURPOSE (mem_initializer_list
));
14562 mem_initializer
= error_mark_node
;
14564 target_ctor
= mem_initializer
;
14566 /* Add it to the list, unless it was erroneous. */
14567 if (mem_initializer
!= error_mark_node
)
14569 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14570 mem_initializer_list
= mem_initializer
;
14572 /* If the next token is not a `,', we're done. */
14573 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14575 /* Consume the `,' token. */
14576 cp_lexer_consume_token (parser
->lexer
);
14579 /* Perform semantic analysis. */
14580 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14581 finish_mem_initializers (mem_initializer_list
);
14584 /* Parse a mem-initializer.
14587 mem-initializer-id ( expression-list [opt] )
14588 mem-initializer-id braced-init-list
14593 ( expression-list [opt] )
14595 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14596 class) or FIELD_DECL (for a non-static data member) to initialize;
14597 the TREE_VALUE is the expression-list. An empty initialization
14598 list is represented by void_list_node. */
14601 cp_parser_mem_initializer (cp_parser
* parser
)
14603 tree mem_initializer_id
;
14604 tree expression_list
;
14606 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14608 /* Find out what is being initialized. */
14609 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14611 permerror (token
->location
,
14612 "anachronistic old-style base class initializer");
14613 mem_initializer_id
= NULL_TREE
;
14617 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14618 if (mem_initializer_id
== error_mark_node
)
14619 return mem_initializer_id
;
14621 member
= expand_member_init (mem_initializer_id
);
14622 if (member
&& !DECL_P (member
))
14623 in_base_initializer
= 1;
14625 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14627 bool expr_non_constant_p
;
14628 cp_lexer_set_source_position (parser
->lexer
);
14629 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14630 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14631 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14632 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14636 vec
<tree
, va_gc
> *vec
;
14637 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14639 /*allow_expansion_p=*/true,
14640 /*non_constant_p=*/NULL
);
14642 return error_mark_node
;
14643 expression_list
= build_tree_list_vec (vec
);
14644 release_tree_vector (vec
);
14647 if (expression_list
== error_mark_node
)
14648 return error_mark_node
;
14649 if (!expression_list
)
14650 expression_list
= void_type_node
;
14652 in_base_initializer
= 0;
14654 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14657 /* Parse a mem-initializer-id.
14659 mem-initializer-id:
14660 :: [opt] nested-name-specifier [opt] class-name
14661 decltype-specifier (C++11)
14664 Returns a TYPE indicating the class to be initialized for the first
14665 production (and the second in C++11). Returns an IDENTIFIER_NODE
14666 indicating the data member to be initialized for the last production. */
14669 cp_parser_mem_initializer_id (cp_parser
* parser
)
14671 bool global_scope_p
;
14672 bool nested_name_specifier_p
;
14673 bool template_p
= false;
14676 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14678 /* `typename' is not allowed in this context ([temp.res]). */
14679 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14681 error_at (token
->location
,
14682 "keyword %<typename%> not allowed in this context (a qualified "
14683 "member initializer is implicitly a type)");
14684 cp_lexer_consume_token (parser
->lexer
);
14686 /* Look for the optional `::' operator. */
14688 = (cp_parser_global_scope_opt (parser
,
14689 /*current_scope_valid_p=*/false)
14691 /* Look for the optional nested-name-specifier. The simplest way to
14696 The keyword `typename' is not permitted in a base-specifier or
14697 mem-initializer; in these contexts a qualified name that
14698 depends on a template-parameter is implicitly assumed to be a
14701 is to assume that we have seen the `typename' keyword at this
14703 nested_name_specifier_p
14704 = (cp_parser_nested_name_specifier_opt (parser
,
14705 /*typename_keyword_p=*/true,
14706 /*check_dependency_p=*/true,
14708 /*is_declaration=*/true)
14710 if (nested_name_specifier_p
)
14711 template_p
= cp_parser_optional_template_keyword (parser
);
14712 /* If there is a `::' operator or a nested-name-specifier, then we
14713 are definitely looking for a class-name. */
14714 if (global_scope_p
|| nested_name_specifier_p
)
14715 return cp_parser_class_name (parser
,
14716 /*typename_keyword_p=*/true,
14717 /*template_keyword_p=*/template_p
,
14719 /*check_dependency_p=*/true,
14720 /*class_head_p=*/false,
14721 /*is_declaration=*/true);
14722 /* Otherwise, we could also be looking for an ordinary identifier. */
14723 cp_parser_parse_tentatively (parser
);
14724 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14725 /* Try a decltype-specifier. */
14726 id
= cp_parser_decltype (parser
);
14728 /* Otherwise, try a class-name. */
14729 id
= cp_parser_class_name (parser
,
14730 /*typename_keyword_p=*/true,
14731 /*template_keyword_p=*/false,
14733 /*check_dependency_p=*/true,
14734 /*class_head_p=*/false,
14735 /*is_declaration=*/true);
14736 /* If we found one, we're done. */
14737 if (cp_parser_parse_definitely (parser
))
14739 /* Otherwise, look for an ordinary identifier. */
14740 return cp_parser_identifier (parser
);
14743 /* Overloading [gram.over] */
14745 /* Parse an operator-function-id.
14747 operator-function-id:
14750 Returns an IDENTIFIER_NODE for the operator which is a
14751 human-readable spelling of the identifier, e.g., `operator +'. */
14754 cp_parser_operator_function_id (cp_parser
* parser
)
14756 /* Look for the `operator' keyword. */
14757 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14758 return error_mark_node
;
14759 /* And then the name of the operator itself. */
14760 return cp_parser_operator (parser
);
14763 /* Return an identifier node for a user-defined literal operator.
14764 The suffix identifier is chained to the operator name identifier. */
14767 cp_literal_operator_id (const char* name
)
14770 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14771 + strlen (name
) + 10);
14772 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14773 identifier
= get_identifier (buffer
);
14778 /* Parse an operator.
14781 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14782 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14783 || ++ -- , ->* -> () []
14790 Returns an IDENTIFIER_NODE for the operator which is a
14791 human-readable spelling of the identifier, e.g., `operator +'. */
14794 cp_parser_operator (cp_parser
* parser
)
14796 tree id
= NULL_TREE
;
14800 /* Peek at the next token. */
14801 token
= cp_lexer_peek_token (parser
->lexer
);
14803 location_t start_loc
= token
->location
;
14805 /* Figure out which operator we have. */
14806 enum tree_code op
= ERROR_MARK
;
14807 bool assop
= false;
14808 bool consumed
= false;
14809 switch (token
->type
)
14813 /* The keyword should be either `new' or `delete'. */
14814 if (token
->keyword
== RID_NEW
)
14816 else if (token
->keyword
== RID_DELETE
)
14821 /* Consume the `new' or `delete' token. */
14822 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14824 /* Peek at the next token. */
14825 token
= cp_lexer_peek_token (parser
->lexer
);
14826 /* If it's a `[' token then this is the array variant of the
14828 if (token
->type
== CPP_OPEN_SQUARE
)
14830 /* Consume the `[' token. */
14831 cp_lexer_consume_token (parser
->lexer
);
14832 /* Look for the `]' token. */
14833 if (cp_token
*close_token
14834 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14835 end_loc
= close_token
->location
;
14836 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14838 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14856 op
= TRUNC_DIV_EXPR
;
14860 op
= TRUNC_MOD_EXPR
;
14880 op
= TRUTH_NOT_EXPR
;
14913 op
= TRUNC_DIV_EXPR
;
14918 op
= TRUNC_MOD_EXPR
;
14944 case CPP_LSHIFT_EQ
:
14949 case CPP_RSHIFT_EQ
:
14966 case CPP_GREATER_EQ
:
14971 op
= TRUTH_ANDIF_EXPR
;
14975 op
= TRUTH_ORIF_EXPR
;
14978 case CPP_PLUS_PLUS
:
14979 op
= POSTINCREMENT_EXPR
;
14982 case CPP_MINUS_MINUS
:
14983 op
= PREDECREMENT_EXPR
;
14987 op
= COMPOUND_EXPR
;
14990 case CPP_DEREF_STAR
:
14995 op
= COMPONENT_REF
;
14998 case CPP_OPEN_PAREN
:
15000 /* Consume the `('. */
15001 matching_parens parens
;
15002 parens
.consume_open (parser
);
15003 /* Look for the matching `)'. */
15004 parens
.require_close (parser
);
15010 case CPP_OPEN_SQUARE
:
15011 /* Consume the `['. */
15012 cp_lexer_consume_token (parser
->lexer
);
15013 /* Look for the matching `]'. */
15014 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15019 case CPP_UTF8STRING
:
15020 case CPP_UTF8STRING_USERDEF
:
15027 case CPP_STRING_USERDEF
:
15028 case CPP_WSTRING_USERDEF
:
15029 case CPP_STRING16_USERDEF
:
15030 case CPP_STRING32_USERDEF
:
15032 tree str
, string_tree
;
15035 if (cxx_dialect
== cxx98
)
15036 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15038 /* Consume the string. */
15039 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15040 /*wide_ok=*/true, /*lookup_udlit=*/false);
15041 if (str
== error_mark_node
)
15042 return error_mark_node
;
15043 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15045 string_tree
= USERDEF_LITERAL_VALUE (str
);
15046 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
15051 /* Look for the suffix identifier. */
15052 token
= cp_lexer_peek_token (parser
->lexer
);
15053 if (token
->type
== CPP_NAME
)
15054 id
= cp_parser_identifier (parser
);
15055 else if (token
->type
== CPP_KEYWORD
)
15057 error ("unexpected keyword;"
15058 " remove space between quotes and suffix identifier");
15059 return error_mark_node
;
15063 error ("expected suffix identifier");
15064 return error_mark_node
;
15067 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15068 (TREE_TYPE (TREE_TYPE (string_tree
))));
15069 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15072 error ("expected empty string after %<operator%> keyword");
15073 return error_mark_node
;
15075 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15078 error ("invalid encoding prefix in literal operator");
15079 return error_mark_node
;
15081 if (id
!= error_mark_node
)
15083 const char *name
= IDENTIFIER_POINTER (id
);
15084 id
= cp_literal_operator_id (name
);
15090 /* Anything else is an error. */
15094 /* If we have selected an identifier, we need to consume the
15096 if (op
!= ERROR_MARK
)
15098 id
= ovl_op_identifier (assop
, op
);
15100 cp_lexer_consume_token (parser
->lexer
);
15102 /* Otherwise, no valid operator name was present. */
15105 cp_parser_error (parser
, "expected operator");
15106 id
= error_mark_node
;
15109 return cp_expr (id
, start_loc
);
15112 /* Parse a template-declaration.
15114 template-declaration:
15115 export [opt] template < template-parameter-list > declaration
15117 If MEMBER_P is TRUE, this template-declaration occurs within a
15120 The grammar rule given by the standard isn't correct. What
15121 is really meant is:
15123 template-declaration:
15124 export [opt] template-parameter-list-seq
15125 decl-specifier-seq [opt] init-declarator [opt] ;
15126 export [opt] template-parameter-list-seq
15127 function-definition
15129 template-parameter-list-seq:
15130 template-parameter-list-seq [opt]
15131 template < template-parameter-list >
15133 Concept Extensions:
15135 template-parameter-list-seq:
15136 template < template-parameter-list > requires-clause [opt]
15139 requires logical-or-expression */
15142 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15144 /* Check for `export'. */
15145 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15147 /* Consume the `export' token. */
15148 cp_lexer_consume_token (parser
->lexer
);
15149 /* Warn that we do not support `export'. */
15150 warning (0, "keyword %<export%> not implemented, and will be ignored");
15153 cp_parser_template_declaration_after_export (parser
, member_p
);
15156 /* Parse a template-parameter-list.
15158 template-parameter-list:
15160 template-parameter-list , template-parameter
15162 Returns a TREE_LIST. Each node represents a template parameter.
15163 The nodes are connected via their TREE_CHAINs. */
15166 cp_parser_template_parameter_list (cp_parser
* parser
)
15168 tree parameter_list
= NULL_TREE
;
15170 begin_template_parm_list ();
15172 /* The loop below parses the template parms. We first need to know
15173 the total number of template parms to be able to compute proper
15174 canonical types of each dependent type. So after the loop, when
15175 we know the total number of template parms,
15176 end_template_parm_list computes the proper canonical types and
15177 fixes up the dependent types accordingly. */
15182 bool is_parameter_pack
;
15183 location_t parm_loc
;
15185 /* Parse the template-parameter. */
15186 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15187 parameter
= cp_parser_template_parameter (parser
,
15189 &is_parameter_pack
);
15190 /* Add it to the list. */
15191 if (parameter
!= error_mark_node
)
15192 parameter_list
= process_template_parm (parameter_list
,
15196 is_parameter_pack
);
15199 tree err_parm
= build_tree_list (parameter
, parameter
);
15200 parameter_list
= chainon (parameter_list
, err_parm
);
15203 /* If the next token is not a `,', we're done. */
15204 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15206 /* Otherwise, consume the `,' token. */
15207 cp_lexer_consume_token (parser
->lexer
);
15210 return end_template_parm_list (parameter_list
);
15213 /* Parse a introduction-list.
15216 introduced-parameter
15217 introduction-list , introduced-parameter
15219 introduced-parameter:
15220 ...[opt] identifier
15222 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15223 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15224 WILDCARD_DECL will also have DECL_NAME set and token location in
15225 DECL_SOURCE_LOCATION. */
15228 cp_parser_introduction_list (cp_parser
*parser
)
15230 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15234 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15236 cp_lexer_consume_token (parser
->lexer
);
15238 tree identifier
= cp_parser_identifier (parser
);
15239 if (identifier
== error_mark_node
)
15242 /* Build placeholder. */
15243 tree parm
= build_nt (WILDCARD_DECL
);
15244 DECL_SOURCE_LOCATION (parm
)
15245 = cp_lexer_peek_token (parser
->lexer
)->location
;
15246 DECL_NAME (parm
) = identifier
;
15247 WILDCARD_PACK_P (parm
) = is_pack
;
15248 vec_safe_push (introduction_vec
, parm
);
15250 /* If the next token is not a `,', we're done. */
15251 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15253 /* Otherwise, consume the `,' token. */
15254 cp_lexer_consume_token (parser
->lexer
);
15257 /* Convert the vec into a TREE_VEC. */
15258 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15261 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15262 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15264 release_tree_vector (introduction_vec
);
15265 return introduction_list
;
15268 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15269 is an abstract declarator. */
15271 static inline cp_declarator
*
15272 get_id_declarator (cp_declarator
*declarator
)
15274 cp_declarator
*d
= declarator
;
15275 while (d
&& d
->kind
!= cdk_id
)
15280 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15281 is an abstract declarator. */
15284 get_unqualified_id (cp_declarator
*declarator
)
15286 declarator
= get_id_declarator (declarator
);
15288 return declarator
->u
.id
.unqualified_name
;
15293 /* Returns true if DECL represents a constrained-parameter. */
15296 is_constrained_parameter (tree decl
)
15299 && TREE_CODE (decl
) == TYPE_DECL
15300 && CONSTRAINED_PARM_CONCEPT (decl
)
15301 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15304 /* Returns true if PARM declares a constrained-parameter. */
15307 is_constrained_parameter (cp_parameter_declarator
*parm
)
15309 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15312 /* Check that the type parameter is only a declarator-id, and that its
15313 type is not cv-qualified. */
15316 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15317 cp_parameter_declarator
*parm
)
15319 if (!parm
->declarator
)
15322 if (parm
->declarator
->kind
!= cdk_id
)
15324 cp_parser_error (parser
, "invalid constrained type parameter");
15328 /* Don't allow cv-qualified type parameters. */
15329 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15330 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15332 cp_parser_error (parser
, "cv-qualified type parameter");
15339 /* Finish parsing/processing a template type parameter and checking
15340 various restrictions. */
15343 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15345 cp_parameter_declarator
* parmdecl
)
15347 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15348 return finish_template_type_parm (class_type_node
, id
);
15350 return error_mark_node
;
15354 finish_constrained_template_template_parm (tree proto
, tree id
)
15356 /* FIXME: This should probably be copied, and we may need to adjust
15357 the template parameter depths. */
15358 tree saved_parms
= current_template_parms
;
15359 begin_template_parm_list ();
15360 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15361 end_template_parm_list ();
15363 tree parm
= finish_template_template_parm (class_type_node
, id
);
15364 current_template_parms
= saved_parms
;
15369 /* Finish parsing/processing a template template parameter by borrowing
15370 the template parameter list from the prototype parameter. */
15373 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15376 cp_parameter_declarator
*parmdecl
)
15378 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15379 return error_mark_node
;
15380 return finish_constrained_template_template_parm (proto
, id
);
15383 /* Create a new non-type template parameter from the given PARM
15387 constrained_non_type_template_parm (bool *is_non_type
,
15388 cp_parameter_declarator
*parm
)
15390 *is_non_type
= true;
15391 cp_declarator
*decl
= parm
->declarator
;
15392 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15393 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15394 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15397 /* Build a constrained template parameter based on the PARMDECL
15398 declarator. The type of PARMDECL is the constrained type, which
15399 refers to the prototype template parameter that ultimately
15400 specifies the type of the declared parameter. */
15403 finish_constrained_parameter (cp_parser
*parser
,
15404 cp_parameter_declarator
*parmdecl
,
15406 bool *is_parameter_pack
)
15408 tree decl
= parmdecl
->decl_specifiers
.type
;
15409 tree id
= get_unqualified_id (parmdecl
->declarator
);
15410 tree def
= parmdecl
->default_argument
;
15411 tree proto
= DECL_INITIAL (decl
);
15413 /* A template parameter constrained by a variadic concept shall also
15414 be declared as a template parameter pack. */
15415 bool is_variadic
= template_parameter_pack_p (proto
);
15416 if (is_variadic
&& !*is_parameter_pack
)
15417 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15419 /* Build the parameter. Return an error if the declarator was invalid. */
15421 if (TREE_CODE (proto
) == TYPE_DECL
)
15422 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15423 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15424 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15427 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15428 if (parm
== error_mark_node
)
15429 return error_mark_node
;
15431 /* Finish the parameter decl and create a node attaching the
15432 default argument and constraint. */
15433 parm
= build_tree_list (def
, parm
);
15434 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15439 /* Returns true if the parsed type actually represents the declaration
15440 of a type template-parameter. */
15443 declares_constrained_type_template_parameter (tree type
)
15445 return (is_constrained_parameter (type
)
15446 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15450 /* Returns true if the parsed type actually represents the declaration of
15451 a template template-parameter. */
15454 declares_constrained_template_template_parameter (tree type
)
15456 return (is_constrained_parameter (type
)
15457 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15460 /* Parse a default argument for a type template-parameter.
15461 Note that diagnostics are handled in cp_parser_template_parameter. */
15464 cp_parser_default_type_template_argument (cp_parser
*parser
)
15466 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15468 /* Consume the `=' token. */
15469 cp_lexer_consume_token (parser
->lexer
);
15471 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15473 /* Parse the default-argument. */
15474 push_deferring_access_checks (dk_no_deferred
);
15475 tree default_argument
= cp_parser_type_id (parser
);
15476 pop_deferring_access_checks ();
15478 if (flag_concepts
&& type_uses_auto (default_argument
))
15480 error_at (token
->location
,
15481 "invalid use of %<auto%> in default template argument");
15482 return error_mark_node
;
15485 return default_argument
;
15488 /* Parse a default argument for a template template-parameter. */
15491 cp_parser_default_template_template_argument (cp_parser
*parser
)
15493 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15497 /* Consume the `='. */
15498 cp_lexer_consume_token (parser
->lexer
);
15499 /* Parse the id-expression. */
15500 push_deferring_access_checks (dk_no_deferred
);
15501 /* save token before parsing the id-expression, for error
15503 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15504 tree default_argument
15505 = cp_parser_id_expression (parser
,
15506 /*template_keyword_p=*/false,
15507 /*check_dependency_p=*/true,
15508 /*template_p=*/&is_template
,
15509 /*declarator_p=*/false,
15510 /*optional_p=*/false);
15511 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15512 /* If the id-expression was a template-id that refers to
15513 a template-class, we already have the declaration here,
15514 so no further lookup is needed. */
15517 /* Look up the name. */
15519 = cp_parser_lookup_name (parser
, default_argument
,
15521 /*is_template=*/is_template
,
15522 /*is_namespace=*/false,
15523 /*check_dependency=*/true,
15524 /*ambiguous_decls=*/NULL
,
15526 /* See if the default argument is valid. */
15527 default_argument
= check_template_template_default_arg (default_argument
);
15528 pop_deferring_access_checks ();
15529 return default_argument
;
15532 /* Parse a template-parameter.
15534 template-parameter:
15536 parameter-declaration
15538 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15539 the parameter. The TREE_PURPOSE is the default value, if any.
15540 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15541 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15542 set to true iff this parameter is a parameter pack. */
15545 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15546 bool *is_parameter_pack
)
15549 cp_parameter_declarator
*parameter_declarator
;
15552 /* Assume it is a type parameter or a template parameter. */
15553 *is_non_type
= false;
15554 /* Assume it not a parameter pack. */
15555 *is_parameter_pack
= false;
15556 /* Peek at the next token. */
15557 token
= cp_lexer_peek_token (parser
->lexer
);
15558 /* If it is `template', we have a type-parameter. */
15559 if (token
->keyword
== RID_TEMPLATE
)
15560 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15561 /* If it is `class' or `typename' we do not know yet whether it is a
15562 type parameter or a non-type parameter. Consider:
15564 template <typename T, typename T::X X> ...
15568 template <class C, class D*> ...
15570 Here, the first parameter is a type parameter, and the second is
15571 a non-type parameter. We can tell by looking at the token after
15572 the identifier -- if it is a `,', `=', or `>' then we have a type
15574 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15576 /* Peek at the token after `class' or `typename'. */
15577 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15578 /* If it's an ellipsis, we have a template type parameter
15580 if (token
->type
== CPP_ELLIPSIS
)
15581 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15582 /* If it's an identifier, skip it. */
15583 if (token
->type
== CPP_NAME
)
15584 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15585 /* Now, see if the token looks like the end of a template
15587 if (token
->type
== CPP_COMMA
15588 || token
->type
== CPP_EQ
15589 || token
->type
== CPP_GREATER
)
15590 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15593 /* Otherwise, it is a non-type parameter or a constrained parameter.
15597 When parsing a default template-argument for a non-type
15598 template-parameter, the first non-nested `>' is taken as the end
15599 of the template parameter-list rather than a greater-than
15601 parameter_declarator
15602 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15603 /*parenthesized_p=*/NULL
);
15605 if (!parameter_declarator
)
15606 return error_mark_node
;
15608 /* If the parameter declaration is marked as a parameter pack, set
15609 *IS_PARAMETER_PACK to notify the caller. */
15610 if (parameter_declarator
->template_parameter_pack_p
)
15611 *is_parameter_pack
= true;
15613 if (parameter_declarator
->default_argument
)
15615 /* Can happen in some cases of erroneous input (c++/34892). */
15616 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15617 /* Consume the `...' for better error recovery. */
15618 cp_lexer_consume_token (parser
->lexer
);
15621 // The parameter may have been constrained.
15622 if (is_constrained_parameter (parameter_declarator
))
15623 return finish_constrained_parameter (parser
,
15624 parameter_declarator
,
15626 is_parameter_pack
);
15628 // Now we're sure that the parameter is a non-type parameter.
15629 *is_non_type
= true;
15631 parm
= grokdeclarator (parameter_declarator
->declarator
,
15632 ¶meter_declarator
->decl_specifiers
,
15633 TPARM
, /*initialized=*/0,
15634 /*attrlist=*/NULL
);
15635 if (parm
== error_mark_node
)
15636 return error_mark_node
;
15638 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15641 /* Parse a type-parameter.
15644 class identifier [opt]
15645 class identifier [opt] = type-id
15646 typename identifier [opt]
15647 typename identifier [opt] = type-id
15648 template < template-parameter-list > class identifier [opt]
15649 template < template-parameter-list > class identifier [opt]
15652 GNU Extension (variadic templates):
15655 class ... identifier [opt]
15656 typename ... identifier [opt]
15658 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15659 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15660 the declaration of the parameter.
15662 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15665 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15670 /* Look for a keyword to tell us what kind of parameter this is. */
15671 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15673 return error_mark_node
;
15675 switch (token
->keyword
)
15681 tree default_argument
;
15683 /* If the next token is an ellipsis, we have a template
15685 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15687 /* Consume the `...' token. */
15688 cp_lexer_consume_token (parser
->lexer
);
15689 maybe_warn_variadic_templates ();
15691 *is_parameter_pack
= true;
15694 /* If the next token is an identifier, then it names the
15696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15697 identifier
= cp_parser_identifier (parser
);
15699 identifier
= NULL_TREE
;
15701 /* Create the parameter. */
15702 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15704 /* If the next token is an `=', we have a default argument. */
15705 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15708 = cp_parser_default_type_template_argument (parser
);
15710 /* Template parameter packs cannot have default
15712 if (*is_parameter_pack
)
15715 error_at (token
->location
,
15716 "template parameter pack %qD cannot have a "
15717 "default argument", identifier
);
15719 error_at (token
->location
,
15720 "template parameter packs cannot have "
15721 "default arguments");
15722 default_argument
= NULL_TREE
;
15724 else if (check_for_bare_parameter_packs (default_argument
))
15725 default_argument
= error_mark_node
;
15728 default_argument
= NULL_TREE
;
15730 /* Create the combined representation of the parameter and the
15731 default argument. */
15732 parameter
= build_tree_list (default_argument
, parameter
);
15739 tree default_argument
;
15741 /* Look for the `<'. */
15742 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15743 /* Parse the template-parameter-list. */
15744 cp_parser_template_parameter_list (parser
);
15745 /* Look for the `>'. */
15746 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15748 // If template requirements are present, parse them.
15751 tree reqs
= get_shorthand_constraints (current_template_parms
);
15752 if (tree r
= cp_parser_requires_clause_opt (parser
))
15753 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15754 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15757 /* Look for the `class' or 'typename' keywords. */
15758 cp_parser_type_parameter_key (parser
);
15759 /* If the next token is an ellipsis, we have a template
15761 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15763 /* Consume the `...' token. */
15764 cp_lexer_consume_token (parser
->lexer
);
15765 maybe_warn_variadic_templates ();
15767 *is_parameter_pack
= true;
15769 /* If the next token is an `=', then there is a
15770 default-argument. If the next token is a `>', we are at
15771 the end of the parameter-list. If the next token is a `,',
15772 then we are at the end of this parameter. */
15773 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15774 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15775 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15777 identifier
= cp_parser_identifier (parser
);
15778 /* Treat invalid names as if the parameter were nameless. */
15779 if (identifier
== error_mark_node
)
15780 identifier
= NULL_TREE
;
15783 identifier
= NULL_TREE
;
15785 /* Create the template parameter. */
15786 parameter
= finish_template_template_parm (class_type_node
,
15789 /* If the next token is an `=', then there is a
15790 default-argument. */
15791 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15794 = cp_parser_default_template_template_argument (parser
);
15796 /* Template parameter packs cannot have default
15798 if (*is_parameter_pack
)
15801 error_at (token
->location
,
15802 "template parameter pack %qD cannot "
15803 "have a default argument",
15806 error_at (token
->location
, "template parameter packs cannot "
15807 "have default arguments");
15808 default_argument
= NULL_TREE
;
15812 default_argument
= NULL_TREE
;
15814 /* Create the combined representation of the parameter and the
15815 default argument. */
15816 parameter
= build_tree_list (default_argument
, parameter
);
15821 gcc_unreachable ();
15828 /* Parse a template-id.
15831 template-name < template-argument-list [opt] >
15833 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15834 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15835 returned. Otherwise, if the template-name names a function, or set
15836 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15837 names a class, returns a TYPE_DECL for the specialization.
15839 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15840 uninstantiated templates. */
15843 cp_parser_template_id (cp_parser
*parser
,
15844 bool template_keyword_p
,
15845 bool check_dependency_p
,
15846 enum tag_types tag_type
,
15847 bool is_declaration
)
15852 cp_token_position start_of_id
= 0;
15853 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15854 bool is_identifier
;
15856 /* If the next token corresponds to a template-id, there is no need
15858 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15859 if (token
->type
== CPP_TEMPLATE_ID
)
15861 cp_lexer_consume_token (parser
->lexer
);
15862 return saved_checks_value (token
->u
.tree_check_value
);
15865 /* Avoid performing name lookup if there is no possibility of
15866 finding a template-id. */
15867 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15868 || (token
->type
== CPP_NAME
15869 && !cp_parser_nth_token_starts_template_argument_list_p
15872 cp_parser_error (parser
, "expected template-id");
15873 return error_mark_node
;
15876 /* Remember where the template-id starts. */
15877 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15878 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15880 push_deferring_access_checks (dk_deferred
);
15882 /* Parse the template-name. */
15883 is_identifier
= false;
15884 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15885 check_dependency_p
,
15889 if (templ
== error_mark_node
|| is_identifier
)
15891 pop_deferring_access_checks ();
15895 /* Since we're going to preserve any side-effects from this parse, set up a
15896 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15897 in the template arguments. */
15898 tentative_firewall
firewall (parser
);
15900 /* If we find the sequence `[:' after a template-name, it's probably
15901 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15902 parse correctly the argument list. */
15903 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15904 == CPP_OPEN_SQUARE
)
15905 && next_token
->flags
& DIGRAPH
15906 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15908 && !(next_token_2
->flags
& PREV_WHITE
))
15910 cp_parser_parse_tentatively (parser
);
15911 /* Change `:' into `::'. */
15912 next_token_2
->type
= CPP_SCOPE
;
15913 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15915 cp_lexer_consume_token (parser
->lexer
);
15917 /* Parse the arguments. */
15918 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15919 if (!cp_parser_parse_definitely (parser
))
15921 /* If we couldn't parse an argument list, then we revert our changes
15922 and return simply an error. Maybe this is not a template-id
15924 next_token_2
->type
= CPP_COLON
;
15925 cp_parser_error (parser
, "expected %<<%>");
15926 pop_deferring_access_checks ();
15927 return error_mark_node
;
15929 /* Otherwise, emit an error about the invalid digraph, but continue
15930 parsing because we got our argument list. */
15931 if (permerror (next_token
->location
,
15932 "%<<::%> cannot begin a template-argument list"))
15934 static bool hint
= false;
15935 inform (next_token
->location
,
15936 "%<<:%> is an alternate spelling for %<[%>."
15937 " Insert whitespace between %<<%> and %<::%>");
15938 if (!hint
&& !flag_permissive
)
15940 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15941 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15942 "accept your code)");
15949 /* Look for the `<' that starts the template-argument-list. */
15950 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15952 pop_deferring_access_checks ();
15953 return error_mark_node
;
15955 /* Parse the arguments. */
15956 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15959 /* Set the location to be of the form:
15960 template-name < template-argument-list [opt] >
15961 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15962 with caret == start at the start of the template-name,
15963 ranging until the closing '>'. */
15964 location_t finish_loc
15965 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15966 location_t combined_loc
15967 = make_location (token
->location
, token
->location
, finish_loc
);
15969 /* Check for concepts autos where they don't belong. We could
15970 identify types in some cases of idnetifier TEMPL, looking ahead
15971 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15972 types. We reject them in functions, but if what we have is an
15973 identifier, even with none_type we can't conclude it's NOT a
15974 type, we have to wait for template substitution. */
15975 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
15976 template_id
= error_mark_node
;
15977 /* Build a representation of the specialization. */
15978 else if (identifier_p (templ
))
15979 template_id
= build_min_nt_loc (combined_loc
,
15982 else if (DECL_TYPE_TEMPLATE_P (templ
)
15983 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15985 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15986 template (rather than some instantiation thereof) only if
15987 is not nested within some other construct. For example, in
15988 "template <typename T> void f(T) { A<T>::", A<T> is just an
15989 instantiation of A. */
15990 bool entering_scope
15991 = (template_parm_scope_p ()
15992 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
15994 = finish_template_type (templ
, arguments
, entering_scope
);
15996 /* A template-like identifier may be a partial concept id. */
15997 else if (flag_concepts
15998 && (template_id
= (cp_parser_maybe_partial_concept_id
15999 (parser
, templ
, arguments
))))
16000 return template_id
;
16001 else if (variable_template_p (templ
))
16003 template_id
= lookup_template_variable (templ
, arguments
);
16004 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16005 SET_EXPR_LOCATION (template_id
, combined_loc
);
16009 /* If it's not a class-template or a template-template, it should be
16010 a function-template. */
16011 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16012 || TREE_CODE (templ
) == OVERLOAD
16013 || BASELINK_P (templ
)));
16015 template_id
= lookup_template_function (templ
, arguments
);
16016 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16017 SET_EXPR_LOCATION (template_id
, combined_loc
);
16020 /* If parsing tentatively, replace the sequence of tokens that makes
16021 up the template-id with a CPP_TEMPLATE_ID token. That way,
16022 should we re-parse the token stream, we will not have to repeat
16023 the effort required to do the parse, nor will we issue duplicate
16024 error messages about problems during instantiation of the
16027 /* Don't do this if we had a parse error in a declarator; re-parsing
16028 might succeed if a name changes meaning (60361). */
16029 && !(cp_parser_error_occurred (parser
)
16030 && cp_parser_parsing_tentatively (parser
)
16031 && parser
->in_declarator_p
))
16033 /* Reset the contents of the START_OF_ID token. */
16034 token
->type
= CPP_TEMPLATE_ID
;
16035 token
->location
= combined_loc
;
16037 /* Retrieve any deferred checks. Do not pop this access checks yet
16038 so the memory will not be reclaimed during token replacing below. */
16039 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16040 token
->u
.tree_check_value
->value
= template_id
;
16041 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16042 token
->keyword
= RID_MAX
;
16044 /* Purge all subsequent tokens. */
16045 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16047 /* ??? Can we actually assume that, if template_id ==
16048 error_mark_node, we will have issued a diagnostic to the
16049 user, as opposed to simply marking the tentative parse as
16051 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16052 error_at (token
->location
, "parse error in template argument list");
16055 pop_to_parent_deferring_access_checks ();
16056 return template_id
;
16059 /* Parse a template-name.
16064 The standard should actually say:
16068 operator-function-id
16070 A defect report has been filed about this issue.
16072 A conversion-function-id cannot be a template name because they cannot
16073 be part of a template-id. In fact, looking at this code:
16075 a.operator K<int>()
16077 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16078 It is impossible to call a templated conversion-function-id with an
16079 explicit argument list, since the only allowed template parameter is
16080 the type to which it is converting.
16082 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16083 `template' keyword, in a construction like:
16087 In that case `f' is taken to be a template-name, even though there
16088 is no way of knowing for sure.
16090 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16091 name refers to a set of overloaded functions, at least one of which
16092 is a template, or an IDENTIFIER_NODE with the name of the template,
16093 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16094 names are looked up inside uninstantiated templates. */
16097 cp_parser_template_name (cp_parser
* parser
,
16098 bool template_keyword_p
,
16099 bool check_dependency_p
,
16100 bool is_declaration
,
16101 enum tag_types tag_type
,
16102 bool *is_identifier
)
16106 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16108 /* If the next token is `operator', then we have either an
16109 operator-function-id or a conversion-function-id. */
16110 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16112 /* We don't know whether we're looking at an
16113 operator-function-id or a conversion-function-id. */
16114 cp_parser_parse_tentatively (parser
);
16115 /* Try an operator-function-id. */
16116 identifier
= cp_parser_operator_function_id (parser
);
16117 /* If that didn't work, try a conversion-function-id. */
16118 if (!cp_parser_parse_definitely (parser
))
16120 cp_parser_error (parser
, "expected template-name");
16121 return error_mark_node
;
16124 /* Look for the identifier. */
16126 identifier
= cp_parser_identifier (parser
);
16128 /* If we didn't find an identifier, we don't have a template-id. */
16129 if (identifier
== error_mark_node
)
16130 return error_mark_node
;
16132 /* If the name immediately followed the `template' keyword, then it
16133 is a template-name. However, if the next token is not `<', then
16134 we do not treat it as a template-name, since it is not being used
16135 as part of a template-id. This enables us to handle constructs
16138 template <typename T> struct S { S(); };
16139 template <typename T> S<T>::S();
16141 correctly. We would treat `S' as a template -- if it were `S<T>'
16142 -- but we do not if there is no `<'. */
16144 if (processing_template_decl
16145 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16147 /* In a declaration, in a dependent context, we pretend that the
16148 "template" keyword was present in order to improve error
16149 recovery. For example, given:
16151 template <typename T> void f(T::X<int>);
16153 we want to treat "X<int>" as a template-id. */
16155 && !template_keyword_p
16156 && parser
->scope
&& TYPE_P (parser
->scope
)
16157 && check_dependency_p
16158 && dependent_scope_p (parser
->scope
)
16159 /* Do not do this for dtors (or ctors), since they never
16160 need the template keyword before their name. */
16161 && !constructor_name_p (identifier
, parser
->scope
))
16163 cp_token_position start
= 0;
16165 /* Explain what went wrong. */
16166 error_at (token
->location
, "non-template %qD used as template",
16168 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16169 parser
->scope
, identifier
);
16170 /* If parsing tentatively, find the location of the "<" token. */
16171 if (cp_parser_simulate_error (parser
))
16172 start
= cp_lexer_token_position (parser
->lexer
, true);
16173 /* Parse the template arguments so that we can issue error
16174 messages about them. */
16175 cp_lexer_consume_token (parser
->lexer
);
16176 cp_parser_enclosed_template_argument_list (parser
);
16177 /* Skip tokens until we find a good place from which to
16178 continue parsing. */
16179 cp_parser_skip_to_closing_parenthesis (parser
,
16180 /*recovering=*/true,
16182 /*consume_paren=*/false);
16183 /* If parsing tentatively, permanently remove the
16184 template argument list. That will prevent duplicate
16185 error messages from being issued about the missing
16186 "template" keyword. */
16188 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16190 *is_identifier
= true;
16191 parser
->context
->object_type
= NULL_TREE
;
16195 /* If the "template" keyword is present, then there is generally
16196 no point in doing name-lookup, so we just return IDENTIFIER.
16197 But, if the qualifying scope is non-dependent then we can
16198 (and must) do name-lookup normally. */
16199 if (template_keyword_p
)
16201 tree scope
= (parser
->scope
? parser
->scope
16202 : parser
->context
->object_type
);
16203 if (scope
&& TYPE_P (scope
)
16204 && (!CLASS_TYPE_P (scope
)
16205 || (check_dependency_p
&& dependent_type_p (scope
))))
16207 /* We're optimizing away the call to cp_parser_lookup_name, but
16208 we still need to do this. */
16209 parser
->context
->object_type
= NULL_TREE
;
16215 /* Look up the name. */
16216 decl
= cp_parser_lookup_name (parser
, identifier
,
16218 /*is_template=*/true,
16219 /*is_namespace=*/false,
16220 check_dependency_p
,
16221 /*ambiguous_decls=*/NULL
,
16224 decl
= strip_using_decl (decl
);
16226 /* If DECL is a template, then the name was a template-name. */
16227 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16229 if (TREE_DEPRECATED (decl
)
16230 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16231 warn_deprecated_use (decl
, NULL_TREE
);
16235 /* The standard does not explicitly indicate whether a name that
16236 names a set of overloaded declarations, some of which are
16237 templates, is a template-name. However, such a name should
16238 be a template-name; otherwise, there is no way to form a
16239 template-id for the overloaded templates. */
16240 bool found
= false;
16242 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16243 !found
&& iter
; ++iter
)
16244 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16249 /* The name does not name a template. */
16250 cp_parser_error (parser
, "expected template-name");
16251 return error_mark_node
;
16255 /* If DECL is dependent, and refers to a function, then just return
16256 its name; we will look it up again during template instantiation. */
16257 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16259 tree scope
= ovl_scope (decl
);
16260 if (TYPE_P (scope
) && dependent_type_p (scope
))
16267 /* Parse a template-argument-list.
16269 template-argument-list:
16270 template-argument ... [opt]
16271 template-argument-list , template-argument ... [opt]
16273 Returns a TREE_VEC containing the arguments. */
16276 cp_parser_template_argument_list (cp_parser
* parser
)
16278 tree fixed_args
[10];
16279 unsigned n_args
= 0;
16280 unsigned alloced
= 10;
16281 tree
*arg_ary
= fixed_args
;
16283 bool saved_in_template_argument_list_p
;
16285 bool saved_non_ice_p
;
16287 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16288 parser
->in_template_argument_list_p
= true;
16289 /* Even if the template-id appears in an integral
16290 constant-expression, the contents of the argument list do
16292 saved_ice_p
= parser
->integral_constant_expression_p
;
16293 parser
->integral_constant_expression_p
= false;
16294 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16295 parser
->non_integral_constant_expression_p
= false;
16297 /* Parse the arguments. */
16303 /* Consume the comma. */
16304 cp_lexer_consume_token (parser
->lexer
);
16306 /* Parse the template-argument. */
16307 argument
= cp_parser_template_argument (parser
);
16309 /* If the next token is an ellipsis, we're expanding a template
16311 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16313 if (argument
== error_mark_node
)
16315 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16316 error_at (token
->location
,
16317 "expected parameter pack before %<...%>");
16319 /* Consume the `...' token. */
16320 cp_lexer_consume_token (parser
->lexer
);
16322 /* Make the argument into a TYPE_PACK_EXPANSION or
16323 EXPR_PACK_EXPANSION. */
16324 argument
= make_pack_expansion (argument
);
16327 if (n_args
== alloced
)
16331 if (arg_ary
== fixed_args
)
16333 arg_ary
= XNEWVEC (tree
, alloced
);
16334 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16337 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16339 arg_ary
[n_args
++] = argument
;
16341 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16343 vec
= make_tree_vec (n_args
);
16346 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16348 if (arg_ary
!= fixed_args
)
16350 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16351 parser
->integral_constant_expression_p
= saved_ice_p
;
16352 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16354 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16358 /* Parse a template-argument.
16361 assignment-expression
16365 The representation is that of an assignment-expression, type-id, or
16366 id-expression -- except that the qualified id-expression is
16367 evaluated, so that the value returned is either a DECL or an
16370 Although the standard says "assignment-expression", it forbids
16371 throw-expressions or assignments in the template argument.
16372 Therefore, we use "conditional-expression" instead. */
16375 cp_parser_template_argument (cp_parser
* parser
)
16380 bool maybe_type_id
= false;
16381 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16382 location_t loc
= 0;
16385 /* There's really no way to know what we're looking at, so we just
16386 try each alternative in order.
16390 In a template-argument, an ambiguity between a type-id and an
16391 expression is resolved to a type-id, regardless of the form of
16392 the corresponding template-parameter.
16394 Therefore, we try a type-id first. */
16395 cp_parser_parse_tentatively (parser
);
16396 argument
= cp_parser_template_type_arg (parser
);
16397 /* If there was no error parsing the type-id but the next token is a
16398 '>>', our behavior depends on which dialect of C++ we're
16399 parsing. In C++98, we probably found a typo for '> >'. But there
16400 are type-id which are also valid expressions. For instance:
16402 struct X { int operator >> (int); };
16403 template <int V> struct Foo {};
16406 Here 'X()' is a valid type-id of a function type, but the user just
16407 wanted to write the expression "X() >> 5". Thus, we remember that we
16408 found a valid type-id, but we still try to parse the argument as an
16409 expression to see what happens.
16411 In C++0x, the '>>' will be considered two separate '>'
16413 if (!cp_parser_error_occurred (parser
)
16414 && cxx_dialect
== cxx98
16415 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16417 maybe_type_id
= true;
16418 cp_parser_abort_tentative_parse (parser
);
16422 /* If the next token isn't a `,' or a `>', then this argument wasn't
16423 really finished. This means that the argument is not a valid
16425 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16426 cp_parser_error (parser
, "expected template-argument");
16427 /* If that worked, we're done. */
16428 if (cp_parser_parse_definitely (parser
))
16431 /* We're still not sure what the argument will be. */
16432 cp_parser_parse_tentatively (parser
);
16433 /* Try a template. */
16434 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16435 argument
= cp_parser_id_expression (parser
,
16436 /*template_keyword_p=*/false,
16437 /*check_dependency_p=*/true,
16439 /*declarator_p=*/false,
16440 /*optional_p=*/false);
16441 /* If the next token isn't a `,' or a `>', then this argument wasn't
16442 really finished. */
16443 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16444 cp_parser_error (parser
, "expected template-argument");
16445 if (!cp_parser_error_occurred (parser
))
16447 /* Figure out what is being referred to. If the id-expression
16448 was for a class template specialization, then we will have a
16449 TYPE_DECL at this point. There is no need to do name lookup
16450 at this point in that case. */
16451 if (TREE_CODE (argument
) != TYPE_DECL
)
16452 argument
= cp_parser_lookup_name (parser
, argument
,
16454 /*is_template=*/template_p
,
16455 /*is_namespace=*/false,
16456 /*check_dependency=*/true,
16457 /*ambiguous_decls=*/NULL
,
16458 argument_start_token
->location
);
16459 /* Handle a constrained-type-specifier for a non-type template
16461 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16463 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16464 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16465 cp_parser_error (parser
, "expected template-name");
16467 if (cp_parser_parse_definitely (parser
))
16469 if (TREE_DEPRECATED (argument
))
16470 warn_deprecated_use (argument
, NULL_TREE
);
16473 /* It must be a non-type argument. In C++17 any constant-expression is
16475 if (cxx_dialect
> cxx14
)
16478 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16480 -- an integral constant-expression of integral or enumeration
16483 -- the name of a non-type template-parameter; or
16485 -- the name of an object or function with external linkage...
16487 -- the address of an object or function with external linkage...
16489 -- a pointer to member... */
16490 /* Look for a non-type template parameter. */
16491 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16493 cp_parser_parse_tentatively (parser
);
16494 argument
= cp_parser_primary_expression (parser
,
16495 /*address_p=*/false,
16497 /*template_arg_p=*/true,
16499 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16500 || !cp_parser_next_token_ends_template_argument_p (parser
))
16501 cp_parser_simulate_error (parser
);
16502 if (cp_parser_parse_definitely (parser
))
16506 /* If the next token is "&", the argument must be the address of an
16507 object or function with external linkage. */
16508 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16511 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16512 cp_lexer_consume_token (parser
->lexer
);
16514 /* See if we might have an id-expression. */
16515 token
= cp_lexer_peek_token (parser
->lexer
);
16516 if (token
->type
== CPP_NAME
16517 || token
->keyword
== RID_OPERATOR
16518 || token
->type
== CPP_SCOPE
16519 || token
->type
== CPP_TEMPLATE_ID
16520 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16522 cp_parser_parse_tentatively (parser
);
16523 argument
= cp_parser_primary_expression (parser
,
16526 /*template_arg_p=*/true,
16528 if (cp_parser_error_occurred (parser
)
16529 || !cp_parser_next_token_ends_template_argument_p (parser
))
16530 cp_parser_abort_tentative_parse (parser
);
16535 if (INDIRECT_REF_P (argument
))
16537 /* Strip the dereference temporarily. */
16538 gcc_assert (REFERENCE_REF_P (argument
));
16539 argument
= TREE_OPERAND (argument
, 0);
16542 /* If we're in a template, we represent a qualified-id referring
16543 to a static data member as a SCOPE_REF even if the scope isn't
16544 dependent so that we can check access control later. */
16546 if (TREE_CODE (probe
) == SCOPE_REF
)
16547 probe
= TREE_OPERAND (probe
, 1);
16550 /* A variable without external linkage might still be a
16551 valid constant-expression, so no error is issued here
16552 if the external-linkage check fails. */
16553 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16554 cp_parser_simulate_error (parser
);
16556 else if (is_overloaded_fn (argument
))
16557 /* All overloaded functions are allowed; if the external
16558 linkage test does not pass, an error will be issued
16562 && (TREE_CODE (argument
) == OFFSET_REF
16563 || TREE_CODE (argument
) == SCOPE_REF
))
16564 /* A pointer-to-member. */
16566 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16569 cp_parser_simulate_error (parser
);
16571 if (cp_parser_parse_definitely (parser
))
16574 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16575 tf_warning_or_error
);
16577 argument
= convert_from_reference (argument
);
16582 /* If the argument started with "&", there are no other valid
16583 alternatives at this point. */
16586 cp_parser_error (parser
, "invalid non-type template argument");
16587 return error_mark_node
;
16591 /* If the argument wasn't successfully parsed as a type-id followed
16592 by '>>', the argument can only be a constant expression now.
16593 Otherwise, we try parsing the constant-expression tentatively,
16594 because the argument could really be a type-id. */
16596 cp_parser_parse_tentatively (parser
);
16598 if (cxx_dialect
<= cxx14
)
16599 argument
= cp_parser_constant_expression (parser
);
16602 /* With C++17 generalized non-type template arguments we need to handle
16603 lvalue constant expressions, too. */
16604 argument
= cp_parser_assignment_expression (parser
);
16605 require_potential_constant_expression (argument
);
16608 if (!maybe_type_id
)
16610 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16611 cp_parser_error (parser
, "expected template-argument");
16612 if (cp_parser_parse_definitely (parser
))
16614 /* We did our best to parse the argument as a non type-id, but that
16615 was the only alternative that matched (albeit with a '>' after
16616 it). We can assume it's just a typo from the user, and a
16617 diagnostic will then be issued. */
16618 return cp_parser_template_type_arg (parser
);
16621 /* Parse an explicit-instantiation.
16623 explicit-instantiation:
16624 template declaration
16626 Although the standard says `declaration', what it really means is:
16628 explicit-instantiation:
16629 template decl-specifier-seq [opt] declarator [opt] ;
16631 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16632 supposed to be allowed. A defect report has been filed about this
16637 explicit-instantiation:
16638 storage-class-specifier template
16639 decl-specifier-seq [opt] declarator [opt] ;
16640 function-specifier template
16641 decl-specifier-seq [opt] declarator [opt] ; */
16644 cp_parser_explicit_instantiation (cp_parser
* parser
)
16646 int declares_class_or_enum
;
16647 cp_decl_specifier_seq decl_specifiers
;
16648 tree extension_specifier
= NULL_TREE
;
16650 timevar_push (TV_TEMPLATE_INST
);
16652 /* Look for an (optional) storage-class-specifier or
16653 function-specifier. */
16654 if (cp_parser_allow_gnu_extensions_p (parser
))
16656 extension_specifier
16657 = cp_parser_storage_class_specifier_opt (parser
);
16658 if (!extension_specifier
)
16659 extension_specifier
16660 = cp_parser_function_specifier_opt (parser
,
16661 /*decl_specs=*/NULL
);
16664 /* Look for the `template' keyword. */
16665 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16666 /* Let the front end know that we are processing an explicit
16668 begin_explicit_instantiation ();
16669 /* [temp.explicit] says that we are supposed to ignore access
16670 control while processing explicit instantiation directives. */
16671 push_deferring_access_checks (dk_no_check
);
16672 /* Parse a decl-specifier-seq. */
16673 cp_parser_decl_specifier_seq (parser
,
16674 CP_PARSER_FLAGS_OPTIONAL
,
16676 &declares_class_or_enum
);
16677 /* If there was exactly one decl-specifier, and it declared a class,
16678 and there's no declarator, then we have an explicit type
16680 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16684 type
= check_tag_decl (&decl_specifiers
,
16685 /*explicit_type_instantiation_p=*/true);
16686 /* Turn access control back on for names used during
16687 template instantiation. */
16688 pop_deferring_access_checks ();
16690 do_type_instantiation (type
, extension_specifier
,
16691 /*complain=*/tf_error
);
16695 cp_declarator
*declarator
;
16698 /* Parse the declarator. */
16700 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16701 /*ctor_dtor_or_conv_p=*/NULL
,
16702 /*parenthesized_p=*/NULL
,
16703 /*member_p=*/false,
16704 /*friend_p=*/false);
16705 if (declares_class_or_enum
& 2)
16706 cp_parser_check_for_definition_in_return_type (declarator
,
16707 decl_specifiers
.type
,
16708 decl_specifiers
.locations
[ds_type_spec
]);
16709 if (declarator
!= cp_error_declarator
)
16711 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16712 permerror (decl_specifiers
.locations
[ds_inline
],
16713 "explicit instantiation shall not use"
16714 " %<inline%> specifier");
16715 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16716 permerror (decl_specifiers
.locations
[ds_constexpr
],
16717 "explicit instantiation shall not use"
16718 " %<constexpr%> specifier");
16720 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16721 NORMAL
, 0, &decl_specifiers
.attributes
);
16722 /* Turn access control back on for names used during
16723 template instantiation. */
16724 pop_deferring_access_checks ();
16725 /* Do the explicit instantiation. */
16726 do_decl_instantiation (decl
, extension_specifier
);
16730 pop_deferring_access_checks ();
16731 /* Skip the body of the explicit instantiation. */
16732 cp_parser_skip_to_end_of_statement (parser
);
16735 /* We're done with the instantiation. */
16736 end_explicit_instantiation ();
16738 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16740 timevar_pop (TV_TEMPLATE_INST
);
16743 /* Parse an explicit-specialization.
16745 explicit-specialization:
16746 template < > declaration
16748 Although the standard says `declaration', what it really means is:
16750 explicit-specialization:
16751 template <> decl-specifier [opt] init-declarator [opt] ;
16752 template <> function-definition
16753 template <> explicit-specialization
16754 template <> template-declaration */
16757 cp_parser_explicit_specialization (cp_parser
* parser
)
16759 bool need_lang_pop
;
16760 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16762 /* Look for the `template' keyword. */
16763 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16764 /* Look for the `<'. */
16765 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16766 /* Look for the `>'. */
16767 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16768 /* We have processed another parameter list. */
16769 ++parser
->num_template_parameter_lists
;
16772 A template ... explicit specialization ... shall not have C
16774 if (current_lang_name
== lang_name_c
)
16776 error_at (token
->location
, "template specialization with C linkage");
16777 maybe_show_extern_c_location ();
16778 /* Give it C++ linkage to avoid confusing other parts of the
16780 push_lang_context (lang_name_cplusplus
);
16781 need_lang_pop
= true;
16784 need_lang_pop
= false;
16785 /* Let the front end know that we are beginning a specialization. */
16786 if (!begin_specialization ())
16788 end_specialization ();
16792 /* If the next keyword is `template', we need to figure out whether
16793 or not we're looking a template-declaration. */
16794 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16796 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16797 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16798 cp_parser_template_declaration_after_export (parser
,
16799 /*member_p=*/false);
16801 cp_parser_explicit_specialization (parser
);
16804 /* Parse the dependent declaration. */
16805 cp_parser_single_declaration (parser
,
16807 /*member_p=*/false,
16808 /*explicit_specialization_p=*/true,
16809 /*friend_p=*/NULL
);
16810 /* We're done with the specialization. */
16811 end_specialization ();
16812 /* For the erroneous case of a template with C linkage, we pushed an
16813 implicit C++ linkage scope; exit that scope now. */
16815 pop_lang_context ();
16816 /* We're done with this parameter list. */
16817 --parser
->num_template_parameter_lists
;
16820 /* Parse a type-specifier.
16823 simple-type-specifier
16826 elaborated-type-specifier
16834 Returns a representation of the type-specifier. For a
16835 class-specifier, enum-specifier, or elaborated-type-specifier, a
16836 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16838 The parser flags FLAGS is used to control type-specifier parsing.
16840 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16841 in a decl-specifier-seq.
16843 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16844 class-specifier, enum-specifier, or elaborated-type-specifier, then
16845 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16846 if a type is declared; 2 if it is defined. Otherwise, it is set to
16849 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16850 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16851 is set to FALSE. */
16854 cp_parser_type_specifier (cp_parser
* parser
,
16855 cp_parser_flags flags
,
16856 cp_decl_specifier_seq
*decl_specs
,
16857 bool is_declaration
,
16858 int* declares_class_or_enum
,
16859 bool* is_cv_qualifier
)
16861 tree type_spec
= NULL_TREE
;
16864 cp_decl_spec ds
= ds_last
;
16866 /* Assume this type-specifier does not declare a new type. */
16867 if (declares_class_or_enum
)
16868 *declares_class_or_enum
= 0;
16869 /* And that it does not specify a cv-qualifier. */
16870 if (is_cv_qualifier
)
16871 *is_cv_qualifier
= false;
16872 /* Peek at the next token. */
16873 token
= cp_lexer_peek_token (parser
->lexer
);
16875 /* If we're looking at a keyword, we can use that to guide the
16876 production we choose. */
16877 keyword
= token
->keyword
;
16881 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16882 goto elaborated_type_specifier
;
16884 /* Look for the enum-specifier. */
16885 type_spec
= cp_parser_enum_specifier (parser
);
16886 /* If that worked, we're done. */
16889 if (declares_class_or_enum
)
16890 *declares_class_or_enum
= 2;
16892 cp_parser_set_decl_spec_type (decl_specs
,
16895 /*type_definition_p=*/true);
16899 goto elaborated_type_specifier
;
16901 /* Any of these indicate either a class-specifier, or an
16902 elaborated-type-specifier. */
16906 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16907 goto elaborated_type_specifier
;
16909 /* Parse tentatively so that we can back up if we don't find a
16910 class-specifier. */
16911 cp_parser_parse_tentatively (parser
);
16912 /* Look for the class-specifier. */
16913 type_spec
= cp_parser_class_specifier (parser
);
16914 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16915 /* If that worked, we're done. */
16916 if (cp_parser_parse_definitely (parser
))
16918 if (declares_class_or_enum
)
16919 *declares_class_or_enum
= 2;
16921 cp_parser_set_decl_spec_type (decl_specs
,
16924 /*type_definition_p=*/true);
16928 /* Fall through. */
16929 elaborated_type_specifier
:
16930 /* We're declaring (not defining) a class or enum. */
16931 if (declares_class_or_enum
)
16932 *declares_class_or_enum
= 1;
16934 /* Fall through. */
16936 /* Look for an elaborated-type-specifier. */
16938 = (cp_parser_elaborated_type_specifier
16940 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16943 cp_parser_set_decl_spec_type (decl_specs
,
16946 /*type_definition_p=*/false);
16951 if (is_cv_qualifier
)
16952 *is_cv_qualifier
= true;
16957 if (is_cv_qualifier
)
16958 *is_cv_qualifier
= true;
16963 if (is_cv_qualifier
)
16964 *is_cv_qualifier
= true;
16968 /* The `__complex__' keyword is a GNU extension. */
16976 /* Handle simple keywords. */
16981 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16982 decl_specs
->any_specifiers_p
= true;
16984 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16987 /* If we do not already have a type-specifier, assume we are looking
16988 at a simple-type-specifier. */
16989 type_spec
= cp_parser_simple_type_specifier (parser
,
16993 /* If we didn't find a type-specifier, and a type-specifier was not
16994 optional in this context, issue an error message. */
16995 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16997 cp_parser_error (parser
, "expected type specifier");
16998 return error_mark_node
;
17004 /* Parse a simple-type-specifier.
17006 simple-type-specifier:
17007 :: [opt] nested-name-specifier [opt] type-name
17008 :: [opt] nested-name-specifier template template-id
17023 simple-type-specifier:
17025 decltype ( expression )
17028 __underlying_type ( type-id )
17032 nested-name-specifier(opt) template-name
17036 simple-type-specifier:
17038 __typeof__ unary-expression
17039 __typeof__ ( type-id )
17040 __typeof__ ( type-id ) { initializer-list , [opt] }
17042 Concepts Extension:
17044 simple-type-specifier:
17045 constrained-type-specifier
17047 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17048 appropriately updated. */
17051 cp_parser_simple_type_specifier (cp_parser
* parser
,
17052 cp_decl_specifier_seq
*decl_specs
,
17053 cp_parser_flags flags
)
17055 tree type
= NULL_TREE
;
17059 /* Peek at the next token. */
17060 token
= cp_lexer_peek_token (parser
->lexer
);
17062 /* If we're looking at a keyword, things are easy. */
17063 switch (token
->keyword
)
17067 decl_specs
->explicit_char_p
= true;
17068 type
= char_type_node
;
17071 type
= char16_type_node
;
17074 type
= char32_type_node
;
17077 type
= wchar_type_node
;
17080 type
= boolean_type_node
;
17083 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17084 type
= short_integer_type_node
;
17088 decl_specs
->explicit_int_p
= true;
17089 type
= integer_type_node
;
17095 idx
= token
->keyword
- RID_INT_N_0
;
17096 if (! int_n_enabled_p
[idx
])
17100 decl_specs
->explicit_intN_p
= true;
17101 decl_specs
->int_n_idx
= idx
;
17103 type
= int_n_trees
[idx
].signed_type
;
17107 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17108 type
= long_integer_type_node
;
17111 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17112 type
= integer_type_node
;
17115 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17116 type
= unsigned_type_node
;
17119 type
= float_type_node
;
17122 type
= double_type_node
;
17125 type
= void_type_node
;
17129 maybe_warn_cpp0x (CPP0X_AUTO
);
17130 if (parser
->auto_is_implicit_function_template_parm_p
)
17132 /* The 'auto' might be the placeholder return type for a function decl
17133 with trailing return type. */
17134 bool have_trailing_return_fn_decl
= false;
17136 cp_parser_parse_tentatively (parser
);
17137 cp_lexer_consume_token (parser
->lexer
);
17138 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17139 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17140 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17141 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17143 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17145 cp_lexer_consume_token (parser
->lexer
);
17146 cp_parser_skip_to_closing_parenthesis (parser
,
17147 /*recovering*/false,
17149 /*consume_paren*/true);
17153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17155 have_trailing_return_fn_decl
= true;
17159 cp_lexer_consume_token (parser
->lexer
);
17161 cp_parser_abort_tentative_parse (parser
);
17163 if (have_trailing_return_fn_decl
)
17165 type
= make_auto ();
17169 if (cxx_dialect
>= cxx14
)
17171 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17172 type
= TREE_TYPE (type
);
17175 type
= error_mark_node
;
17177 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17179 if (cxx_dialect
< cxx14
)
17180 error_at (token
->location
,
17181 "use of %<auto%> in lambda parameter declaration "
17182 "only available with "
17183 "-std=c++14 or -std=gnu++14");
17185 else if (cxx_dialect
< cxx14
)
17186 error_at (token
->location
,
17187 "use of %<auto%> in parameter declaration "
17188 "only available with "
17189 "-std=c++14 or -std=gnu++14");
17190 else if (!flag_concepts
)
17191 pedwarn (token
->location
, 0,
17192 "use of %<auto%> in parameter declaration "
17193 "only available with -fconcepts");
17196 type
= make_auto ();
17200 /* Since DR 743, decltype can either be a simple-type-specifier by
17201 itself or begin a nested-name-specifier. Parsing it will replace
17202 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17203 handling below decide what to do. */
17204 cp_parser_decltype (parser
);
17205 cp_lexer_set_token_position (parser
->lexer
, token
);
17209 /* Consume the `typeof' token. */
17210 cp_lexer_consume_token (parser
->lexer
);
17211 /* Parse the operand to `typeof'. */
17212 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17213 /* If it is not already a TYPE, take its type. */
17214 if (!TYPE_P (type
))
17215 type
= finish_typeof (type
);
17218 cp_parser_set_decl_spec_type (decl_specs
, type
,
17220 /*type_definition_p=*/false);
17224 case RID_UNDERLYING_TYPE
:
17225 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17227 cp_parser_set_decl_spec_type (decl_specs
, type
,
17229 /*type_definition_p=*/false);
17234 case RID_DIRECT_BASES
:
17235 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17237 cp_parser_set_decl_spec_type (decl_specs
, type
,
17239 /*type_definition_p=*/false);
17245 /* If token is an already-parsed decltype not followed by ::,
17246 it's a simple-type-specifier. */
17247 if (token
->type
== CPP_DECLTYPE
17248 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17250 type
= saved_checks_value (token
->u
.tree_check_value
);
17253 cp_parser_set_decl_spec_type (decl_specs
, type
,
17255 /*type_definition_p=*/false);
17256 /* Remember that we are handling a decltype in order to
17257 implement the resolution of DR 1510 when the argument
17258 isn't instantiation dependent. */
17259 decl_specs
->decltype_p
= true;
17261 cp_lexer_consume_token (parser
->lexer
);
17265 /* If the type-specifier was for a built-in type, we're done. */
17268 /* Record the type. */
17270 && (token
->keyword
!= RID_SIGNED
17271 && token
->keyword
!= RID_UNSIGNED
17272 && token
->keyword
!= RID_SHORT
17273 && token
->keyword
!= RID_LONG
))
17274 cp_parser_set_decl_spec_type (decl_specs
,
17277 /*type_definition_p=*/false);
17279 decl_specs
->any_specifiers_p
= true;
17281 /* Consume the token. */
17282 cp_lexer_consume_token (parser
->lexer
);
17284 if (type
== error_mark_node
)
17285 return error_mark_node
;
17287 /* There is no valid C++ program where a non-template type is
17288 followed by a "<". That usually indicates that the user thought
17289 that the type was a template. */
17290 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17293 return TYPE_NAME (type
);
17296 /* The type-specifier must be a user-defined type. */
17297 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17302 /* Don't gobble tokens or issue error messages if this is an
17303 optional type-specifier. */
17304 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17305 cp_parser_parse_tentatively (parser
);
17307 token
= cp_lexer_peek_token (parser
->lexer
);
17309 /* Look for the optional `::' operator. */
17311 = (cp_parser_global_scope_opt (parser
,
17312 /*current_scope_valid_p=*/false)
17314 /* Look for the nested-name specifier. */
17316 = (cp_parser_nested_name_specifier_opt (parser
,
17317 /*typename_keyword_p=*/false,
17318 /*check_dependency_p=*/true,
17320 /*is_declaration=*/false)
17322 /* If we have seen a nested-name-specifier, and the next token
17323 is `template', then we are using the template-id production. */
17325 && cp_parser_optional_template_keyword (parser
))
17327 /* Look for the template-id. */
17328 type
= cp_parser_template_id (parser
,
17329 /*template_keyword_p=*/true,
17330 /*check_dependency_p=*/true,
17332 /*is_declaration=*/false);
17333 /* If the template-id did not name a type, we are out of
17335 if (TREE_CODE (type
) != TYPE_DECL
)
17337 cp_parser_error (parser
, "expected template-id for type");
17341 /* Otherwise, look for a type-name. */
17343 type
= cp_parser_type_name (parser
);
17344 /* Keep track of all name-lookups performed in class scopes. */
17348 && TREE_CODE (type
) == TYPE_DECL
17349 && identifier_p (DECL_NAME (type
)))
17350 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17351 /* If it didn't work out, we don't have a TYPE. */
17352 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17353 && !cp_parser_parse_definitely (parser
))
17355 if (!type
&& cxx_dialect
>= cxx17
)
17357 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17358 cp_parser_parse_tentatively (parser
);
17360 cp_parser_global_scope_opt (parser
,
17361 /*current_scope_valid_p=*/false);
17362 cp_parser_nested_name_specifier_opt (parser
,
17363 /*typename_keyword_p=*/false,
17364 /*check_dependency_p=*/true,
17366 /*is_declaration=*/false);
17367 tree name
= cp_parser_identifier (parser
);
17368 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17369 && parser
->scope
!= error_mark_node
)
17371 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17373 /*is_template=*/false,
17374 /*is_namespace=*/false,
17375 /*check_dependency=*/true,
17376 /*ambiguous_decls=*/NULL
,
17378 if (tmpl
&& tmpl
!= error_mark_node
17379 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17380 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17381 type
= make_template_placeholder (tmpl
);
17384 type
= error_mark_node
;
17385 if (!cp_parser_simulate_error (parser
))
17386 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17387 NLE_TYPE
, token
->location
);
17391 type
= error_mark_node
;
17393 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17394 && !cp_parser_parse_definitely (parser
))
17397 if (type
&& decl_specs
)
17398 cp_parser_set_decl_spec_type (decl_specs
, type
,
17400 /*type_definition_p=*/false);
17403 /* If we didn't get a type-name, issue an error message. */
17404 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17406 cp_parser_error (parser
, "expected type-name");
17407 return error_mark_node
;
17410 if (type
&& type
!= error_mark_node
)
17412 /* See if TYPE is an Objective-C type, and if so, parse and
17413 accept any protocol references following it. Do this before
17414 the cp_parser_check_for_invalid_template_id() call, because
17415 Objective-C types can be followed by '<...>' which would
17416 enclose protocol names rather than template arguments, and so
17417 everything is fine. */
17418 if (c_dialect_objc () && !parser
->scope
17419 && (objc_is_id (type
) || objc_is_class_name (type
)))
17421 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17422 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17424 /* Clobber the "unqualified" type previously entered into
17425 DECL_SPECS with the new, improved protocol-qualified version. */
17427 decl_specs
->type
= qual_type
;
17432 /* There is no valid C++ program where a non-template type is
17433 followed by a "<". That usually indicates that the user
17434 thought that the type was a template. */
17435 cp_parser_check_for_invalid_template_id (parser
, type
,
17443 /* Parse a type-name.
17449 simple-template-id [in c++0x]
17466 Returns a TYPE_DECL for the type. */
17469 cp_parser_type_name (cp_parser
* parser
)
17471 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17476 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17480 /* We can't know yet whether it is a class-name or not. */
17481 cp_parser_parse_tentatively (parser
);
17482 /* Try a class-name. */
17483 type_decl
= cp_parser_class_name (parser
,
17484 typename_keyword_p
,
17485 /*template_keyword_p=*/false,
17487 /*check_dependency_p=*/true,
17488 /*class_head_p=*/false,
17489 /*is_declaration=*/false);
17490 /* If it's not a class-name, keep looking. */
17491 if (!cp_parser_parse_definitely (parser
))
17493 if (cxx_dialect
< cxx11
)
17494 /* It must be a typedef-name or an enum-name. */
17495 return cp_parser_nonclass_name (parser
);
17497 cp_parser_parse_tentatively (parser
);
17498 /* It is either a simple-template-id representing an
17499 instantiation of an alias template... */
17500 type_decl
= cp_parser_template_id (parser
,
17501 /*template_keyword_p=*/false,
17502 /*check_dependency_p=*/true,
17504 /*is_declaration=*/false);
17505 /* Note that this must be an instantiation of an alias template
17506 because [temp.names]/6 says:
17508 A template-id that names an alias template specialization
17511 Whereas [temp.names]/7 says:
17513 A simple-template-id that names a class template
17514 specialization is a class-name.
17516 With concepts, this could also be a partial-concept-id that
17517 declares a non-type template parameter. */
17518 if (type_decl
!= NULL_TREE
17519 && TREE_CODE (type_decl
) == TYPE_DECL
17520 && TYPE_DECL_ALIAS_P (type_decl
))
17521 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17522 else if (is_constrained_parameter (type_decl
))
17523 /* Don't do anything. */ ;
17525 cp_parser_simulate_error (parser
);
17527 if (!cp_parser_parse_definitely (parser
))
17528 /* ... Or a typedef-name or an enum-name. */
17529 return cp_parser_nonclass_name (parser
);
17535 /* Check if DECL and ARGS can form a constrained-type-specifier.
17536 If ARGS is non-null, we try to form a concept check of the
17537 form DECL<?, ARGS> where ? is a wildcard that matches any
17538 kind of template argument. If ARGS is NULL, then we try to
17539 form a concept check of the form DECL<?>. */
17542 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17543 tree decl
, tree args
)
17545 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17547 /* If we a constrained-type-specifier cannot be deduced. */
17548 if (parser
->prevent_constrained_type_specifiers
)
17551 /* A constrained type specifier can only be found in an
17552 overload set or as a reference to a template declaration.
17554 FIXME: This might be masking a bug. It's possible that
17555 that the deduction below is causing template specializations
17556 to be formed with the wildcard as an argument. */
17557 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17560 /* Try to build a call expression that evaluates the
17561 concept. This can fail if the overload set refers
17562 only to non-templates. */
17563 tree placeholder
= build_nt (WILDCARD_DECL
);
17564 tree check
= build_concept_check (decl
, placeholder
, args
);
17565 if (check
== error_mark_node
)
17568 /* Deduce the checked constraint and the prototype parameter.
17570 FIXME: In certain cases, failure to deduce should be a
17571 diagnosable error. */
17574 if (!deduce_constrained_parameter (check
, conc
, proto
))
17577 /* In template parameter scope, this results in a constrained
17578 parameter. Return a descriptor of that parm. */
17579 if (processing_template_parmlist
)
17580 return build_constrained_parameter (conc
, proto
, args
);
17582 /* In a parameter-declaration-clause, constrained-type
17583 specifiers result in invented template parameters. */
17584 if (parser
->auto_is_implicit_function_template_parm_p
)
17586 tree x
= build_constrained_parameter (conc
, proto
, args
);
17587 return synthesize_implicit_template_parm (parser
, x
);
17591 /* Otherwise, we're in a context where the constrained
17592 type name is deduced and the constraint applies
17593 after deduction. */
17594 return make_constrained_auto (conc
, args
);
17600 /* If DECL refers to a concept, return a TYPE_DECL representing
17601 the result of using the constrained type specifier in the
17602 current context. DECL refers to a concept if
17604 - it is an overload set containing a function concept taking a single
17607 - it is a variable concept taking a single type argument. */
17610 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17613 && (TREE_CODE (decl
) == OVERLOAD
17614 || BASELINK_P (decl
)
17615 || variable_concept_p (decl
)))
17616 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17621 /* Check if DECL and ARGS form a partial-concept-id. If so,
17622 assign ID to the resulting constrained placeholder.
17624 Returns true if the partial-concept-id designates a placeholder
17625 and false otherwise. Note that *id is set to NULL_TREE in
17629 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17631 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17634 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17646 Returns a TYPE_DECL for the type. */
17649 cp_parser_nonclass_name (cp_parser
* parser
)
17654 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17655 identifier
= cp_parser_identifier (parser
);
17656 if (identifier
== error_mark_node
)
17657 return error_mark_node
;
17659 /* Look up the type-name. */
17660 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17662 type_decl
= strip_using_decl (type_decl
);
17664 /* If we found an overload set, then it may refer to a concept-name. */
17665 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17668 if (TREE_CODE (type_decl
) != TYPE_DECL
17669 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17671 /* See if this is an Objective-C type. */
17672 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17673 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17675 type_decl
= TYPE_NAME (type
);
17678 /* Issue an error if we did not find a type-name. */
17679 if (TREE_CODE (type_decl
) != TYPE_DECL
17680 /* In Objective-C, we have the complication that class names are
17681 normally type names and start declarations (eg, the
17682 "NSObject" in "NSObject *object;"), but can be used in an
17683 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17684 is an expression. So, a classname followed by a dot is not a
17685 valid type-name. */
17686 || (objc_is_class_name (TREE_TYPE (type_decl
))
17687 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17689 if (!cp_parser_simulate_error (parser
))
17690 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17691 NLE_TYPE
, token
->location
);
17692 return error_mark_node
;
17694 /* Remember that the name was used in the definition of the
17695 current class so that we can check later to see if the
17696 meaning would have been different after the class was
17697 entirely defined. */
17698 else if (type_decl
!= error_mark_node
17700 maybe_note_name_used_in_class (identifier
, type_decl
);
17705 /* Parse an elaborated-type-specifier. Note that the grammar given
17706 here incorporates the resolution to DR68.
17708 elaborated-type-specifier:
17709 class-key :: [opt] nested-name-specifier [opt] identifier
17710 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17711 enum-key :: [opt] nested-name-specifier [opt] identifier
17712 typename :: [opt] nested-name-specifier identifier
17713 typename :: [opt] nested-name-specifier template [opt]
17718 elaborated-type-specifier:
17719 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17720 class-key attributes :: [opt] nested-name-specifier [opt]
17721 template [opt] template-id
17722 enum attributes :: [opt] nested-name-specifier [opt] identifier
17724 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17725 declared `friend'. If IS_DECLARATION is TRUE, then this
17726 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17727 something is being declared.
17729 Returns the TYPE specified. */
17732 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17734 bool is_declaration
)
17736 enum tag_types tag_type
;
17738 tree type
= NULL_TREE
;
17739 tree attributes
= NULL_TREE
;
17741 cp_token
*token
= NULL
;
17743 /* See if we're looking at the `enum' keyword. */
17744 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17746 /* Consume the `enum' token. */
17747 cp_lexer_consume_token (parser
->lexer
);
17748 /* Remember that it's an enumeration type. */
17749 tag_type
= enum_type
;
17750 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17751 enums) is used here. */
17752 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17753 if (cp_parser_is_keyword (token
, RID_CLASS
)
17754 || cp_parser_is_keyword (token
, RID_STRUCT
))
17756 gcc_rich_location
richloc (token
->location
);
17757 richloc
.add_range (input_location
);
17758 richloc
.add_fixit_remove ();
17759 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17760 "a scoped enum must not use the %qD keyword",
17762 /* Consume the `struct' or `class' and parse it anyway. */
17763 cp_lexer_consume_token (parser
->lexer
);
17765 /* Parse the attributes. */
17766 attributes
= cp_parser_attributes_opt (parser
);
17768 /* Or, it might be `typename'. */
17769 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17772 /* Consume the `typename' token. */
17773 cp_lexer_consume_token (parser
->lexer
);
17774 /* Remember that it's a `typename' type. */
17775 tag_type
= typename_type
;
17777 /* Otherwise it must be a class-key. */
17780 tag_type
= cp_parser_class_key (parser
);
17781 if (tag_type
== none_type
)
17782 return error_mark_node
;
17783 /* Parse the attributes. */
17784 attributes
= cp_parser_attributes_opt (parser
);
17787 /* Look for the `::' operator. */
17788 globalscope
= cp_parser_global_scope_opt (parser
,
17789 /*current_scope_valid_p=*/false);
17790 /* Look for the nested-name-specifier. */
17791 tree nested_name_specifier
;
17792 if (tag_type
== typename_type
&& !globalscope
)
17794 nested_name_specifier
17795 = cp_parser_nested_name_specifier (parser
,
17796 /*typename_keyword_p=*/true,
17797 /*check_dependency_p=*/true,
17800 if (!nested_name_specifier
)
17801 return error_mark_node
;
17804 /* Even though `typename' is not present, the proposed resolution
17805 to Core Issue 180 says that in `class A<T>::B', `B' should be
17806 considered a type-name, even if `A<T>' is dependent. */
17807 nested_name_specifier
17808 = cp_parser_nested_name_specifier_opt (parser
,
17809 /*typename_keyword_p=*/true,
17810 /*check_dependency_p=*/true,
17813 /* For everything but enumeration types, consider a template-id.
17814 For an enumeration type, consider only a plain identifier. */
17815 if (tag_type
!= enum_type
)
17817 bool template_p
= false;
17820 /* Allow the `template' keyword. */
17821 template_p
= cp_parser_optional_template_keyword (parser
);
17822 /* If we didn't see `template', we don't know if there's a
17823 template-id or not. */
17825 cp_parser_parse_tentatively (parser
);
17826 /* Parse the template-id. */
17827 token
= cp_lexer_peek_token (parser
->lexer
);
17828 decl
= cp_parser_template_id (parser
, template_p
,
17829 /*check_dependency_p=*/true,
17832 /* If we didn't find a template-id, look for an ordinary
17834 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17836 /* We can get here when cp_parser_template_id, called by
17837 cp_parser_class_name with tag_type == none_type, succeeds
17838 and caches a BASELINK. Then, when called again here,
17839 instead of failing and returning an error_mark_node
17840 returns it (see template/typename17.C in C++11).
17841 ??? Could we diagnose this earlier? */
17842 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17844 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17845 type
= error_mark_node
;
17847 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17848 in effect, then we must assume that, upon instantiation, the
17849 template will correspond to a class. */
17850 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17851 && tag_type
== typename_type
)
17852 type
= make_typename_type (parser
->scope
, decl
,
17854 /*complain=*/tf_error
);
17855 /* If the `typename' keyword is in effect and DECL is not a type
17856 decl, then type is non existent. */
17857 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17859 else if (TREE_CODE (decl
) == TYPE_DECL
)
17861 type
= check_elaborated_type_specifier (tag_type
, decl
,
17862 /*allow_template_p=*/true);
17864 /* If the next token is a semicolon, this must be a specialization,
17865 instantiation, or friend declaration. Check the scope while we
17866 still know whether or not we had a nested-name-specifier. */
17867 if (type
!= error_mark_node
17868 && !nested_name_specifier
&& !is_friend
17869 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17870 check_unqualified_spec_or_inst (type
, token
->location
);
17872 else if (decl
== error_mark_node
)
17873 type
= error_mark_node
;
17878 token
= cp_lexer_peek_token (parser
->lexer
);
17879 identifier
= cp_parser_identifier (parser
);
17881 if (identifier
== error_mark_node
)
17883 parser
->scope
= NULL_TREE
;
17884 return error_mark_node
;
17887 /* For a `typename', we needn't call xref_tag. */
17888 if (tag_type
== typename_type
17889 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17890 return cp_parser_make_typename_type (parser
, identifier
,
17893 /* Template parameter lists apply only if we are not within a
17894 function parameter list. */
17895 bool template_parm_lists_apply
17896 = parser
->num_template_parameter_lists
;
17897 if (template_parm_lists_apply
)
17898 for (cp_binding_level
*s
= current_binding_level
;
17899 s
&& s
->kind
!= sk_template_parms
;
17900 s
= s
->level_chain
)
17901 if (s
->kind
== sk_function_parms
)
17902 template_parm_lists_apply
= false;
17904 /* Look up a qualified name in the usual way. */
17908 tree ambiguous_decls
;
17910 decl
= cp_parser_lookup_name (parser
, identifier
,
17912 /*is_template=*/false,
17913 /*is_namespace=*/false,
17914 /*check_dependency=*/true,
17918 /* If the lookup was ambiguous, an error will already have been
17920 if (ambiguous_decls
)
17921 return error_mark_node
;
17923 /* If we are parsing friend declaration, DECL may be a
17924 TEMPLATE_DECL tree node here. However, we need to check
17925 whether this TEMPLATE_DECL results in valid code. Consider
17926 the following example:
17929 template <class T> class C {};
17932 template <class T> friend class N::C; // #1, valid code
17934 template <class T> class Y {
17935 friend class N::C; // #2, invalid code
17938 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17939 name lookup of `N::C'. We see that friend declaration must
17940 be template for the code to be valid. Note that
17941 processing_template_decl does not work here since it is
17942 always 1 for the above two cases. */
17944 decl
= (cp_parser_maybe_treat_template_as_class
17945 (decl
, /*tag_name_p=*/is_friend
17946 && template_parm_lists_apply
));
17948 if (TREE_CODE (decl
) != TYPE_DECL
)
17950 cp_parser_diagnose_invalid_type_name (parser
,
17953 return error_mark_node
;
17956 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17958 bool allow_template
= (template_parm_lists_apply
17959 || DECL_SELF_REFERENCE_P (decl
));
17960 type
= check_elaborated_type_specifier (tag_type
, decl
,
17963 if (type
== error_mark_node
)
17964 return error_mark_node
;
17967 /* Forward declarations of nested types, such as
17972 are invalid unless all components preceding the final '::'
17973 are complete. If all enclosing types are complete, these
17974 declarations become merely pointless.
17976 Invalid forward declarations of nested types are errors
17977 caught elsewhere in parsing. Those that are pointless arrive
17980 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17981 && !is_friend
&& !processing_explicit_instantiation
)
17982 warning (0, "declaration %qD does not declare anything", decl
);
17984 type
= TREE_TYPE (decl
);
17988 /* An elaborated-type-specifier sometimes introduces a new type and
17989 sometimes names an existing type. Normally, the rule is that it
17990 introduces a new type only if there is not an existing type of
17991 the same name already in scope. For example, given:
17994 void f() { struct S s; }
17996 the `struct S' in the body of `f' is the same `struct S' as in
17997 the global scope; the existing definition is used. However, if
17998 there were no global declaration, this would introduce a new
17999 local class named `S'.
18001 An exception to this rule applies to the following code:
18003 namespace N { struct S; }
18005 Here, the elaborated-type-specifier names a new type
18006 unconditionally; even if there is already an `S' in the
18007 containing scope this declaration names a new type.
18008 This exception only applies if the elaborated-type-specifier
18009 forms the complete declaration:
18013 A declaration consisting solely of `class-key identifier ;' is
18014 either a redeclaration of the name in the current scope or a
18015 forward declaration of the identifier as a class name. It
18016 introduces the name into the current scope.
18018 We are in this situation precisely when the next token is a `;'.
18020 An exception to the exception is that a `friend' declaration does
18021 *not* name a new type; i.e., given:
18023 struct S { friend struct T; };
18025 `T' is not a new type in the scope of `S'.
18027 Also, `new struct S' or `sizeof (struct S)' never results in the
18028 definition of a new type; a new type can only be declared in a
18029 declaration context. */
18035 /* Friends have special name lookup rules. */
18036 ts
= ts_within_enclosing_non_class
;
18037 else if (is_declaration
18038 && cp_lexer_next_token_is (parser
->lexer
,
18040 /* This is a `class-key identifier ;' */
18046 (template_parm_lists_apply
18047 && (cp_parser_next_token_starts_class_definition_p (parser
)
18048 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18049 /* An unqualified name was used to reference this type, so
18050 there were no qualifying templates. */
18051 if (template_parm_lists_apply
18052 && !cp_parser_check_template_parameters (parser
,
18053 /*num_templates=*/0,
18054 /*template_id*/false,
18056 /*declarator=*/NULL
))
18057 return error_mark_node
;
18058 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18062 if (type
== error_mark_node
)
18063 return error_mark_node
;
18065 /* Allow attributes on forward declarations of classes. */
18068 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18069 warning (OPT_Wattributes
,
18070 "attributes ignored on uninstantiated type");
18071 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18072 && ! processing_explicit_instantiation
)
18073 warning (OPT_Wattributes
,
18074 "attributes ignored on template instantiation");
18075 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18076 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18078 warning (OPT_Wattributes
,
18079 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18082 if (tag_type
!= enum_type
)
18084 /* Indicate whether this class was declared as a `class' or as a
18086 if (CLASS_TYPE_P (type
))
18087 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18088 cp_parser_check_class_key (tag_type
, type
);
18091 /* A "<" cannot follow an elaborated type specifier. If that
18092 happens, the user was probably trying to form a template-id. */
18093 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18099 /* Parse an enum-specifier.
18102 enum-head { enumerator-list [opt] }
18103 enum-head { enumerator-list , } [C++0x]
18106 enum-key identifier [opt] enum-base [opt]
18107 enum-key nested-name-specifier identifier enum-base [opt]
18112 enum struct [C++0x]
18115 : type-specifier-seq
18117 opaque-enum-specifier:
18118 enum-key identifier enum-base [opt] ;
18121 enum-key attributes[opt] identifier [opt] enum-base [opt]
18122 { enumerator-list [opt] }attributes[opt]
18123 enum-key attributes[opt] identifier [opt] enum-base [opt]
18124 { enumerator-list, }attributes[opt] [C++0x]
18126 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18127 if the token stream isn't an enum-specifier after all. */
18130 cp_parser_enum_specifier (cp_parser
* parser
)
18133 tree type
= NULL_TREE
;
18135 tree nested_name_specifier
= NULL_TREE
;
18137 bool scoped_enum_p
= false;
18138 bool has_underlying_type
= false;
18139 bool nested_being_defined
= false;
18140 bool new_value_list
= false;
18141 bool is_new_type
= false;
18142 bool is_unnamed
= false;
18143 tree underlying_type
= NULL_TREE
;
18144 cp_token
*type_start_token
= NULL
;
18145 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18147 parser
->colon_corrects_to_scope_p
= false;
18149 /* Parse tentatively so that we can back up if we don't find a
18151 cp_parser_parse_tentatively (parser
);
18153 /* Caller guarantees that the current token is 'enum', an identifier
18154 possibly follows, and the token after that is an opening brace.
18155 If we don't have an identifier, fabricate an anonymous name for
18156 the enumeration being defined. */
18157 cp_lexer_consume_token (parser
->lexer
);
18159 /* Parse the "class" or "struct", which indicates a scoped
18160 enumeration type in C++0x. */
18161 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18162 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18164 if (cxx_dialect
< cxx11
)
18165 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18167 /* Consume the `struct' or `class' token. */
18168 cp_lexer_consume_token (parser
->lexer
);
18170 scoped_enum_p
= true;
18173 attributes
= cp_parser_attributes_opt (parser
);
18175 /* Clear the qualification. */
18176 parser
->scope
= NULL_TREE
;
18177 parser
->qualifying_scope
= NULL_TREE
;
18178 parser
->object_scope
= NULL_TREE
;
18180 /* Figure out in what scope the declaration is being placed. */
18181 prev_scope
= current_scope ();
18183 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18185 push_deferring_access_checks (dk_no_check
);
18186 nested_name_specifier
18187 = cp_parser_nested_name_specifier_opt (parser
,
18188 /*typename_keyword_p=*/true,
18189 /*check_dependency_p=*/false,
18191 /*is_declaration=*/false);
18193 if (nested_name_specifier
)
18197 identifier
= cp_parser_identifier (parser
);
18198 name
= cp_parser_lookup_name (parser
, identifier
,
18200 /*is_template=*/false,
18201 /*is_namespace=*/false,
18202 /*check_dependency=*/true,
18203 /*ambiguous_decls=*/NULL
,
18205 if (name
&& name
!= error_mark_node
)
18207 type
= TREE_TYPE (name
);
18208 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18210 /* Are template enums allowed in ISO? */
18211 if (template_parm_scope_p ())
18212 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18213 "%qD is an enumeration template", name
);
18214 /* ignore a typename reference, for it will be solved by name
18219 else if (nested_name_specifier
== error_mark_node
)
18220 /* We already issued an error. */;
18223 error_at (type_start_token
->location
,
18224 "%qD does not name an enumeration in %qT",
18225 identifier
, nested_name_specifier
);
18226 nested_name_specifier
= error_mark_node
;
18231 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18232 identifier
= cp_parser_identifier (parser
);
18235 identifier
= make_anon_name ();
18238 error_at (type_start_token
->location
,
18239 "unnamed scoped enum is not allowed");
18242 pop_deferring_access_checks ();
18244 /* Check for the `:' that denotes a specified underlying type in C++0x.
18245 Note that a ':' could also indicate a bitfield width, however. */
18246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18248 cp_decl_specifier_seq type_specifiers
;
18250 /* Consume the `:'. */
18251 cp_lexer_consume_token (parser
->lexer
);
18253 /* Parse the type-specifier-seq. */
18254 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18255 /*is_trailing_return=*/false,
18258 /* At this point this is surely not elaborated type specifier. */
18259 if (!cp_parser_parse_definitely (parser
))
18262 if (cxx_dialect
< cxx11
)
18263 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18265 has_underlying_type
= true;
18267 /* If that didn't work, stop. */
18268 if (type_specifiers
.type
!= error_mark_node
)
18270 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18271 /*initialized=*/0, NULL
);
18272 if (underlying_type
== error_mark_node
18273 || check_for_bare_parameter_packs (underlying_type
))
18274 underlying_type
= NULL_TREE
;
18278 /* Look for the `{' but don't consume it yet. */
18279 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18281 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18283 cp_parser_error (parser
, "expected %<{%>");
18284 if (has_underlying_type
)
18290 /* An opaque-enum-specifier must have a ';' here. */
18291 if ((scoped_enum_p
|| underlying_type
)
18292 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18294 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18295 if (has_underlying_type
)
18303 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18306 if (nested_name_specifier
)
18308 if (CLASS_TYPE_P (nested_name_specifier
))
18310 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18311 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18312 push_scope (nested_name_specifier
);
18314 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18316 push_nested_namespace (nested_name_specifier
);
18320 /* Issue an error message if type-definitions are forbidden here. */
18321 if (!cp_parser_check_type_definition (parser
))
18322 type
= error_mark_node
;
18324 /* Create the new type. We do this before consuming the opening
18325 brace so the enum will be recorded as being on the line of its
18326 tag (or the 'enum' keyword, if there is no tag). */
18327 type
= start_enum (identifier
, type
, underlying_type
,
18328 attributes
, scoped_enum_p
, &is_new_type
);
18330 /* If the next token is not '{' it is an opaque-enum-specifier or an
18331 elaborated-type-specifier. */
18332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18334 timevar_push (TV_PARSE_ENUM
);
18335 if (nested_name_specifier
18336 && nested_name_specifier
!= error_mark_node
)
18338 /* The following catches invalid code such as:
18339 enum class S<int>::E { A, B, C }; */
18340 if (!processing_specialization
18341 && CLASS_TYPE_P (nested_name_specifier
)
18342 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18343 error_at (type_start_token
->location
, "cannot add an enumerator "
18344 "list to a template instantiation");
18346 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18348 error_at (type_start_token
->location
,
18349 "%<%T::%E%> has not been declared",
18350 TYPE_CONTEXT (nested_name_specifier
),
18351 nested_name_specifier
);
18352 type
= error_mark_node
;
18354 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18355 && !CLASS_TYPE_P (nested_name_specifier
))
18357 error_at (type_start_token
->location
, "nested name specifier "
18358 "%qT for enum declaration does not name a class "
18359 "or namespace", nested_name_specifier
);
18360 type
= error_mark_node
;
18362 /* If that scope does not contain the scope in which the
18363 class was originally declared, the program is invalid. */
18364 else if (prev_scope
&& !is_ancestor (prev_scope
,
18365 nested_name_specifier
))
18367 if (at_namespace_scope_p ())
18368 error_at (type_start_token
->location
,
18369 "declaration of %qD in namespace %qD which does not "
18371 type
, prev_scope
, nested_name_specifier
);
18373 error_at (type_start_token
->location
,
18374 "declaration of %qD in %qD which does not "
18376 type
, prev_scope
, nested_name_specifier
);
18377 type
= error_mark_node
;
18379 /* If that scope is the scope where the declaration is being placed
18380 the program is invalid. */
18381 else if (CLASS_TYPE_P (nested_name_specifier
)
18382 && CLASS_TYPE_P (prev_scope
)
18383 && same_type_p (nested_name_specifier
, prev_scope
))
18385 permerror (type_start_token
->location
,
18386 "extra qualification not allowed");
18387 nested_name_specifier
= NULL_TREE
;
18392 begin_scope (sk_scoped_enum
, type
);
18394 /* Consume the opening brace. */
18395 matching_braces braces
;
18396 braces
.consume_open (parser
);
18398 if (type
== error_mark_node
)
18399 ; /* Nothing to add */
18400 else if (OPAQUE_ENUM_P (type
)
18401 || (cxx_dialect
> cxx98
&& processing_specialization
))
18403 new_value_list
= true;
18404 SET_OPAQUE_ENUM_P (type
, false);
18405 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18409 error_at (type_start_token
->location
,
18410 "multiple definition of %q#T", type
);
18411 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18412 "previous definition here");
18413 type
= error_mark_node
;
18416 if (type
== error_mark_node
)
18417 cp_parser_skip_to_end_of_block_or_statement (parser
);
18418 /* If the next token is not '}', then there are some enumerators. */
18419 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18421 if (is_unnamed
&& !scoped_enum_p
)
18422 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18423 "ISO C++ forbids empty unnamed enum");
18426 cp_parser_enumerator_list (parser
, type
);
18428 /* Consume the final '}'. */
18429 braces
.require_close (parser
);
18433 timevar_pop (TV_PARSE_ENUM
);
18437 /* If a ';' follows, then it is an opaque-enum-specifier
18438 and additional restrictions apply. */
18439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18442 error_at (type_start_token
->location
,
18443 "opaque-enum-specifier without name");
18444 else if (nested_name_specifier
)
18445 error_at (type_start_token
->location
,
18446 "opaque-enum-specifier must use a simple identifier");
18450 /* Look for trailing attributes to apply to this enumeration, and
18451 apply them if appropriate. */
18452 if (cp_parser_allow_gnu_extensions_p (parser
))
18454 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18455 cplus_decl_attributes (&type
,
18457 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18460 /* Finish up the enumeration. */
18461 if (type
!= error_mark_node
)
18463 if (new_value_list
)
18464 finish_enum_value_list (type
);
18466 finish_enum (type
);
18469 if (nested_name_specifier
)
18471 if (CLASS_TYPE_P (nested_name_specifier
))
18473 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18474 pop_scope (nested_name_specifier
);
18476 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18478 pop_nested_namespace (nested_name_specifier
);
18482 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18486 /* Parse an enumerator-list. The enumerators all have the indicated
18490 enumerator-definition
18491 enumerator-list , enumerator-definition */
18494 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18498 /* Parse an enumerator-definition. */
18499 cp_parser_enumerator_definition (parser
, type
);
18501 /* If the next token is not a ',', we've reached the end of
18503 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18505 /* Otherwise, consume the `,' and keep going. */
18506 cp_lexer_consume_token (parser
->lexer
);
18507 /* If the next token is a `}', there is a trailing comma. */
18508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18510 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18511 pedwarn (input_location
, OPT_Wpedantic
,
18512 "comma at end of enumerator list");
18518 /* Parse an enumerator-definition. The enumerator has the indicated
18521 enumerator-definition:
18523 enumerator = constant-expression
18530 enumerator-definition:
18531 enumerator attributes [opt]
18532 enumerator attributes [opt] = constant-expression */
18535 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18541 /* Save the input location because we are interested in the location
18542 of the identifier and not the location of the explicit value. */
18543 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18545 /* Look for the identifier. */
18546 identifier
= cp_parser_identifier (parser
);
18547 if (identifier
== error_mark_node
)
18550 /* Parse any specified attributes. */
18551 tree attrs
= cp_parser_attributes_opt (parser
);
18553 /* If the next token is an '=', then there is an explicit value. */
18554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18556 /* Consume the `=' token. */
18557 cp_lexer_consume_token (parser
->lexer
);
18558 /* Parse the value. */
18559 value
= cp_parser_constant_expression (parser
);
18564 /* If we are processing a template, make sure the initializer of the
18565 enumerator doesn't contain any bare template parameter pack. */
18566 if (check_for_bare_parameter_packs (value
))
18567 value
= error_mark_node
;
18569 /* Create the enumerator. */
18570 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18573 /* Parse a namespace-name.
18576 original-namespace-name
18579 Returns the NAMESPACE_DECL for the namespace. */
18582 cp_parser_namespace_name (cp_parser
* parser
)
18585 tree namespace_decl
;
18587 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18589 /* Get the name of the namespace. */
18590 identifier
= cp_parser_identifier (parser
);
18591 if (identifier
== error_mark_node
)
18592 return error_mark_node
;
18594 /* Look up the identifier in the currently active scope. Look only
18595 for namespaces, due to:
18597 [basic.lookup.udir]
18599 When looking up a namespace-name in a using-directive or alias
18600 definition, only namespace names are considered.
18604 [basic.lookup.qual]
18606 During the lookup of a name preceding the :: scope resolution
18607 operator, object, function, and enumerator names are ignored.
18609 (Note that cp_parser_qualifying_entity only calls this
18610 function if the token after the name is the scope resolution
18612 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18614 /*is_template=*/false,
18615 /*is_namespace=*/true,
18616 /*check_dependency=*/true,
18617 /*ambiguous_decls=*/NULL
,
18619 /* If it's not a namespace, issue an error. */
18620 if (namespace_decl
== error_mark_node
18621 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18623 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18625 auto_diagnostic_group d
;
18626 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18627 if (namespace_decl
== error_mark_node
18628 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18629 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18632 cp_parser_error (parser
, "expected namespace-name");
18633 namespace_decl
= error_mark_node
;
18636 return namespace_decl
;
18639 /* Parse a namespace-definition.
18641 namespace-definition:
18642 named-namespace-definition
18643 unnamed-namespace-definition
18645 named-namespace-definition:
18646 original-namespace-definition
18647 extension-namespace-definition
18649 original-namespace-definition:
18650 namespace identifier { namespace-body }
18652 extension-namespace-definition:
18653 namespace original-namespace-name { namespace-body }
18655 unnamed-namespace-definition:
18656 namespace { namespace-body } */
18659 cp_parser_namespace_definition (cp_parser
* parser
)
18662 int nested_definition_count
= 0;
18664 cp_ensure_no_omp_declare_simd (parser
);
18665 cp_ensure_no_oacc_routine (parser
);
18667 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18671 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18672 cp_lexer_consume_token (parser
->lexer
);
18675 /* Look for the `namespace' keyword. */
18677 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18679 /* Parse any specified attributes before the identifier. */
18680 tree attribs
= cp_parser_attributes_opt (parser
);
18684 identifier
= NULL_TREE
;
18686 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18688 identifier
= cp_parser_identifier (parser
);
18690 if (cp_next_tokens_can_be_std_attribute_p (parser
))
18691 pedwarn (input_location
, OPT_Wpedantic
,
18692 "standard attributes on namespaces must precede "
18693 "the namespace name");
18695 /* Parse any attributes specified after the identifier. */
18696 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18699 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18702 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18703 pedwarn (input_location
, OPT_Wpedantic
,
18704 "nested namespace definitions only available with "
18705 "-std=c++17 or -std=gnu++17");
18707 /* Nested namespace names can create new namespaces (unlike
18708 other qualified-ids). */
18709 if (int count
= identifier
? push_namespace (identifier
) : 0)
18710 nested_definition_count
+= count
;
18712 cp_parser_error (parser
, "nested namespace name required");
18713 cp_lexer_consume_token (parser
->lexer
);
18716 if (nested_definition_count
&& !identifier
)
18717 cp_parser_error (parser
, "namespace name required");
18719 if (nested_definition_count
&& attribs
)
18720 error_at (token
->location
,
18721 "a nested namespace definition cannot have attributes");
18722 if (nested_definition_count
&& is_inline
)
18723 error_at (token
->location
,
18724 "a nested namespace definition cannot be inline");
18726 /* Start the namespace. */
18727 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18729 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18731 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18733 /* Look for the `{' to validate starting the namespace. */
18734 matching_braces braces
;
18735 if (braces
.require_open (parser
))
18737 /* Parse the body of the namespace. */
18738 cp_parser_namespace_body (parser
);
18740 /* Look for the final `}'. */
18741 braces
.require_close (parser
);
18744 if (has_visibility
)
18745 pop_visibility (1);
18747 /* Pop the nested namespace definitions. */
18748 while (nested_definition_count
--)
18752 /* Parse a namespace-body.
18755 declaration-seq [opt] */
18758 cp_parser_namespace_body (cp_parser
* parser
)
18760 cp_parser_declaration_seq_opt (parser
);
18763 /* Parse a namespace-alias-definition.
18765 namespace-alias-definition:
18766 namespace identifier = qualified-namespace-specifier ; */
18769 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18772 tree namespace_specifier
;
18774 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18776 /* Look for the `namespace' keyword. */
18777 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18778 /* Look for the identifier. */
18779 identifier
= cp_parser_identifier (parser
);
18780 if (identifier
== error_mark_node
)
18782 /* Look for the `=' token. */
18783 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18784 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18786 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18787 /* Skip the definition. */
18788 cp_lexer_consume_token (parser
->lexer
);
18789 if (cp_parser_skip_to_closing_brace (parser
))
18790 cp_lexer_consume_token (parser
->lexer
);
18793 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18794 /* Look for the qualified-namespace-specifier. */
18795 namespace_specifier
18796 = cp_parser_qualified_namespace_specifier (parser
);
18797 /* Look for the `;' token. */
18798 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18800 /* Register the alias in the symbol table. */
18801 do_namespace_alias (identifier
, namespace_specifier
);
18804 /* Parse a qualified-namespace-specifier.
18806 qualified-namespace-specifier:
18807 :: [opt] nested-name-specifier [opt] namespace-name
18809 Returns a NAMESPACE_DECL corresponding to the specified
18813 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18815 /* Look for the optional `::'. */
18816 cp_parser_global_scope_opt (parser
,
18817 /*current_scope_valid_p=*/false);
18819 /* Look for the optional nested-name-specifier. */
18820 cp_parser_nested_name_specifier_opt (parser
,
18821 /*typename_keyword_p=*/false,
18822 /*check_dependency_p=*/true,
18824 /*is_declaration=*/true);
18826 return cp_parser_namespace_name (parser
);
18829 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18830 access declaration.
18833 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18834 using :: unqualified-id ;
18836 access-declaration:
18842 cp_parser_using_declaration (cp_parser
* parser
,
18843 bool access_declaration_p
)
18846 bool typename_p
= false;
18847 bool global_scope_p
;
18851 int oldcount
= errorcount
;
18852 cp_token
*diag_token
= NULL
;
18854 if (access_declaration_p
)
18856 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18857 cp_parser_parse_tentatively (parser
);
18861 /* Look for the `using' keyword. */
18862 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18865 /* Peek at the next token. */
18866 token
= cp_lexer_peek_token (parser
->lexer
);
18867 /* See if it's `typename'. */
18868 if (token
->keyword
== RID_TYPENAME
)
18870 /* Remember that we've seen it. */
18872 /* Consume the `typename' token. */
18873 cp_lexer_consume_token (parser
->lexer
);
18877 /* Look for the optional global scope qualification. */
18879 = (cp_parser_global_scope_opt (parser
,
18880 /*current_scope_valid_p=*/false)
18883 /* If we saw `typename', or didn't see `::', then there must be a
18884 nested-name-specifier present. */
18885 if (typename_p
|| !global_scope_p
)
18887 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18888 /*check_dependency_p=*/true,
18890 /*is_declaration=*/true);
18891 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18893 cp_parser_skip_to_end_of_block_or_statement (parser
);
18897 /* Otherwise, we could be in either of the two productions. In that
18898 case, treat the nested-name-specifier as optional. */
18900 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18901 /*typename_keyword_p=*/false,
18902 /*check_dependency_p=*/true,
18904 /*is_declaration=*/true);
18906 qscope
= global_namespace
;
18907 else if (UNSCOPED_ENUM_P (qscope
))
18908 qscope
= CP_TYPE_CONTEXT (qscope
);
18910 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18911 /* Something has already gone wrong; there's no need to parse
18912 further. Since an error has occurred, the return value of
18913 cp_parser_parse_definitely will be false, as required. */
18914 return cp_parser_parse_definitely (parser
);
18916 token
= cp_lexer_peek_token (parser
->lexer
);
18917 /* Parse the unqualified-id. */
18918 identifier
= cp_parser_unqualified_id (parser
,
18919 /*template_keyword_p=*/false,
18920 /*check_dependency_p=*/true,
18921 /*declarator_p=*/true,
18922 /*optional_p=*/false);
18924 if (access_declaration_p
)
18926 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18927 cp_parser_simulate_error (parser
);
18928 if (!cp_parser_parse_definitely (parser
))
18931 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18933 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18934 if (cxx_dialect
< cxx17
18935 && !in_system_header_at (ell
->location
))
18936 pedwarn (ell
->location
, 0,
18937 "pack expansion in using-declaration only available "
18938 "with -std=c++17 or -std=gnu++17");
18939 qscope
= make_pack_expansion (qscope
);
18942 /* The function we call to handle a using-declaration is different
18943 depending on what scope we are in. */
18944 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18946 else if (!identifier_p (identifier
)
18947 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18948 /* [namespace.udecl]
18950 A using declaration shall not name a template-id. */
18951 error_at (token
->location
,
18952 "a template-id may not appear in a using-declaration");
18955 if (at_class_scope_p ())
18957 /* Create the USING_DECL. */
18958 decl
= do_class_using_decl (qscope
, identifier
);
18960 if (decl
&& typename_p
)
18961 USING_DECL_TYPENAME_P (decl
) = 1;
18963 if (check_for_bare_parameter_packs (decl
))
18965 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18969 /* Add it to the list of members in this class. */
18970 finish_member_declaration (decl
);
18974 decl
= cp_parser_lookup_name_simple (parser
,
18977 if (decl
== error_mark_node
)
18978 cp_parser_name_lookup_error (parser
, identifier
,
18981 else if (check_for_bare_parameter_packs (decl
))
18983 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18986 else if (!at_namespace_scope_p ())
18987 finish_local_using_decl (decl
, qscope
, identifier
);
18989 finish_namespace_using_decl (decl
, qscope
, identifier
);
18993 if (!access_declaration_p
18994 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18996 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18997 if (cxx_dialect
< cxx17
)
18998 pedwarn (comma
->location
, 0,
18999 "comma-separated list in using-declaration only available "
19000 "with -std=c++17 or -std=gnu++17");
19004 /* Look for the final `;'. */
19005 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19007 if (access_declaration_p
&& errorcount
== oldcount
)
19008 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19009 "access declarations are deprecated "
19010 "in favour of using-declarations; "
19011 "suggestion: add the %<using%> keyword");
19016 /* Parse an alias-declaration.
19019 using identifier attribute-specifier-seq [opt] = type-id */
19022 cp_parser_alias_declaration (cp_parser
* parser
)
19024 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19025 location_t id_location
;
19026 cp_declarator
*declarator
;
19027 cp_decl_specifier_seq decl_specs
;
19029 const char *saved_message
= NULL
;
19031 /* Look for the `using' keyword. */
19032 cp_token
*using_token
19033 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19034 if (using_token
== NULL
)
19035 return error_mark_node
;
19037 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19038 id
= cp_parser_identifier (parser
);
19039 if (id
== error_mark_node
)
19040 return error_mark_node
;
19042 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19043 attributes
= cp_parser_attributes_opt (parser
);
19044 if (attributes
== error_mark_node
)
19045 return error_mark_node
;
19047 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19049 if (cp_parser_error_occurred (parser
))
19050 return error_mark_node
;
19052 cp_parser_commit_to_tentative_parse (parser
);
19054 /* Now we are going to parse the type-id of the declaration. */
19059 "A type-specifier-seq shall not define a class or enumeration
19060 unless it appears in the type-id of an alias-declaration (7.1.3) that
19061 is not the declaration of a template-declaration."
19063 In other words, if we currently are in an alias template, the
19064 type-id should not define a type.
19066 So let's set parser->type_definition_forbidden_message in that
19067 case; cp_parser_check_type_definition (called by
19068 cp_parser_class_specifier) will then emit an error if a type is
19069 defined in the type-id. */
19070 if (parser
->num_template_parameter_lists
)
19072 saved_message
= parser
->type_definition_forbidden_message
;
19073 parser
->type_definition_forbidden_message
=
19074 G_("types may not be defined in alias template declarations");
19077 type
= cp_parser_type_id (parser
);
19079 /* Restore the error message if need be. */
19080 if (parser
->num_template_parameter_lists
)
19081 parser
->type_definition_forbidden_message
= saved_message
;
19083 if (type
== error_mark_node
19084 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19086 cp_parser_skip_to_end_of_block_or_statement (parser
);
19087 return error_mark_node
;
19090 /* A typedef-name can also be introduced by an alias-declaration. The
19091 identifier following the using keyword becomes a typedef-name. It has
19092 the same semantics as if it were introduced by the typedef
19093 specifier. In particular, it does not define a new type and it shall
19094 not appear in the type-id. */
19096 clear_decl_specs (&decl_specs
);
19097 decl_specs
.type
= type
;
19098 if (attributes
!= NULL_TREE
)
19100 decl_specs
.attributes
= attributes
;
19101 set_and_check_decl_spec_loc (&decl_specs
,
19105 set_and_check_decl_spec_loc (&decl_specs
,
19108 set_and_check_decl_spec_loc (&decl_specs
,
19112 if (parser
->num_template_parameter_lists
19113 && !cp_parser_check_template_parameters (parser
,
19114 /*num_templates=*/0,
19115 /*template_id*/false,
19117 /*declarator=*/NULL
))
19118 return error_mark_node
;
19120 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
19121 declarator
->id_loc
= id_location
;
19123 member_p
= at_class_scope_p ();
19125 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19126 NULL_TREE
, attributes
);
19128 decl
= start_decl (declarator
, &decl_specs
, 0,
19129 attributes
, NULL_TREE
, &pushed_scope
);
19130 if (decl
== error_mark_node
)
19133 // Attach constraints to the alias declaration.
19134 if (flag_concepts
&& current_template_parms
)
19136 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19137 tree constr
= build_constraints (reqs
, NULL_TREE
);
19138 set_constraints (decl
, constr
);
19141 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19144 pop_scope (pushed_scope
);
19146 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19147 added into the symbol table; otherwise, return the TYPE_DECL. */
19148 if (DECL_LANG_SPECIFIC (decl
)
19149 && DECL_TEMPLATE_INFO (decl
)
19150 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19152 decl
= DECL_TI_TEMPLATE (decl
);
19154 check_member_template (decl
);
19160 /* Parse a using-directive.
19163 using namespace :: [opt] nested-name-specifier [opt]
19164 namespace-name ; */
19167 cp_parser_using_directive (cp_parser
* parser
)
19169 tree namespace_decl
;
19172 /* Look for the `using' keyword. */
19173 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19174 /* And the `namespace' keyword. */
19175 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19176 /* Look for the optional `::' operator. */
19177 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19178 /* And the optional nested-name-specifier. */
19179 cp_parser_nested_name_specifier_opt (parser
,
19180 /*typename_keyword_p=*/false,
19181 /*check_dependency_p=*/true,
19183 /*is_declaration=*/true);
19184 /* Get the namespace being used. */
19185 namespace_decl
= cp_parser_namespace_name (parser
);
19186 /* And any specified attributes. */
19187 attribs
= cp_parser_attributes_opt (parser
);
19189 /* Update the symbol table. */
19190 if (namespace_bindings_p ())
19191 finish_namespace_using_directive (namespace_decl
, attribs
);
19193 finish_local_using_directive (namespace_decl
, attribs
);
19195 /* Look for the final `;'. */
19196 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19199 /* Parse an asm-definition.
19202 asm ( string-literal ) ;
19207 asm volatile [opt] ( string-literal ) ;
19208 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19209 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19210 : asm-operand-list [opt] ) ;
19211 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19212 : asm-operand-list [opt]
19213 : asm-clobber-list [opt] ) ;
19214 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19215 : asm-clobber-list [opt]
19216 : asm-goto-list ) ; */
19219 cp_parser_asm_definition (cp_parser
* parser
)
19222 tree outputs
= NULL_TREE
;
19223 tree inputs
= NULL_TREE
;
19224 tree clobbers
= NULL_TREE
;
19225 tree labels
= NULL_TREE
;
19227 bool volatile_p
= false;
19228 bool extended_p
= false;
19229 bool invalid_inputs_p
= false;
19230 bool invalid_outputs_p
= false;
19231 bool goto_p
= false;
19232 required_token missing
= RT_NONE
;
19234 /* Look for the `asm' keyword. */
19235 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19237 if (parser
->in_function_body
19238 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19240 error ("%<asm%> in %<constexpr%> function");
19241 cp_function_chain
->invalid_constexpr
= true;
19244 /* See if the next token is `volatile'. */
19245 if (cp_parser_allow_gnu_extensions_p (parser
)
19246 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19248 /* Remember that we saw the `volatile' keyword. */
19250 /* Consume the token. */
19251 cp_lexer_consume_token (parser
->lexer
);
19253 if (cp_parser_allow_gnu_extensions_p (parser
)
19254 && parser
->in_function_body
19255 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19257 /* Remember that we saw the `goto' keyword. */
19259 /* Consume the token. */
19260 cp_lexer_consume_token (parser
->lexer
);
19262 /* Look for the opening `('. */
19263 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19265 /* Look for the string. */
19266 string
= cp_parser_string_literal (parser
, false, false);
19267 if (string
== error_mark_node
)
19269 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19270 /*consume_paren=*/true);
19274 /* If we're allowing GNU extensions, check for the extended assembly
19275 syntax. Unfortunately, the `:' tokens need not be separated by
19276 a space in C, and so, for compatibility, we tolerate that here
19277 too. Doing that means that we have to treat the `::' operator as
19279 if (cp_parser_allow_gnu_extensions_p (parser
)
19280 && parser
->in_function_body
19281 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19282 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19284 bool inputs_p
= false;
19285 bool clobbers_p
= false;
19286 bool labels_p
= false;
19288 /* The extended syntax was used. */
19291 /* Look for outputs. */
19292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19294 /* Consume the `:'. */
19295 cp_lexer_consume_token (parser
->lexer
);
19296 /* Parse the output-operands. */
19297 if (cp_lexer_next_token_is_not (parser
->lexer
,
19299 && cp_lexer_next_token_is_not (parser
->lexer
,
19301 && cp_lexer_next_token_is_not (parser
->lexer
,
19305 outputs
= cp_parser_asm_operand_list (parser
);
19306 if (outputs
== error_mark_node
)
19307 invalid_outputs_p
= true;
19310 /* If the next token is `::', there are no outputs, and the
19311 next token is the beginning of the inputs. */
19312 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19313 /* The inputs are coming next. */
19316 /* Look for inputs. */
19318 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19320 /* Consume the `:' or `::'. */
19321 cp_lexer_consume_token (parser
->lexer
);
19322 /* Parse the output-operands. */
19323 if (cp_lexer_next_token_is_not (parser
->lexer
,
19325 && cp_lexer_next_token_is_not (parser
->lexer
,
19327 && cp_lexer_next_token_is_not (parser
->lexer
,
19330 inputs
= cp_parser_asm_operand_list (parser
);
19331 if (inputs
== error_mark_node
)
19332 invalid_inputs_p
= true;
19335 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19336 /* The clobbers are coming next. */
19339 /* Look for clobbers. */
19341 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19344 /* Consume the `:' or `::'. */
19345 cp_lexer_consume_token (parser
->lexer
);
19346 /* Parse the clobbers. */
19347 if (cp_lexer_next_token_is_not (parser
->lexer
,
19349 && cp_lexer_next_token_is_not (parser
->lexer
,
19351 clobbers
= cp_parser_asm_clobber_list (parser
);
19354 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19355 /* The labels are coming next. */
19358 /* Look for labels. */
19360 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19363 /* Consume the `:' or `::'. */
19364 cp_lexer_consume_token (parser
->lexer
);
19365 /* Parse the labels. */
19366 labels
= cp_parser_asm_label_list (parser
);
19369 if (goto_p
&& !labels_p
)
19370 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19373 missing
= RT_COLON_SCOPE
;
19375 /* Look for the closing `)'. */
19376 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19377 missing
? missing
: RT_CLOSE_PAREN
))
19378 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19379 /*consume_paren=*/true);
19380 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19382 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19384 /* Create the ASM_EXPR. */
19385 if (parser
->in_function_body
)
19387 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19388 inputs
, clobbers
, labels
);
19389 /* If the extended syntax was not used, mark the ASM_EXPR. */
19392 tree temp
= asm_stmt
;
19393 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19394 temp
= TREE_OPERAND (temp
, 0);
19396 ASM_INPUT_P (temp
) = 1;
19400 symtab
->finalize_toplevel_asm (string
);
19404 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19405 type that comes from the decl-specifier-seq. */
19408 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19410 for (cp_declarator
*d
= declarator
; d
;)
19420 if (TYPE_PTRMEMFUNC_P (type
))
19421 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19422 type
= TREE_TYPE (type
);
19430 /* Declarators [gram.dcl.decl] */
19432 /* Parse an init-declarator.
19435 declarator initializer [opt]
19440 declarator asm-specification [opt] attributes [opt] initializer [opt]
19442 function-definition:
19443 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19445 decl-specifier-seq [opt] declarator function-try-block
19449 function-definition:
19450 __extension__ function-definition
19454 function-definition:
19455 decl-specifier-seq [opt] declarator function-transaction-block
19457 The DECL_SPECIFIERS apply to this declarator. Returns a
19458 representation of the entity declared. If MEMBER_P is TRUE, then
19459 this declarator appears in a class scope. The new DECL created by
19460 this declarator is returned.
19462 The CHECKS are access checks that should be performed once we know
19463 what entity is being declared (and, therefore, what classes have
19466 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19467 for a function-definition here as well. If the declarator is a
19468 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19469 be TRUE upon return. By that point, the function-definition will
19470 have been completely parsed.
19472 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19475 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19476 parsed declaration if it is an uninitialized single declarator not followed
19477 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19478 if present, will not be consumed. If returned, this declarator will be
19479 created with SD_INITIALIZED but will not call cp_finish_decl.
19481 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19482 and there is an initializer, the pointed location_t is set to the
19483 location of the '=' or `(', or '{' in C++11 token introducing the
19487 cp_parser_init_declarator (cp_parser
* parser
,
19488 cp_decl_specifier_seq
*decl_specifiers
,
19489 vec
<deferred_access_check
, va_gc
> *checks
,
19490 bool function_definition_allowed_p
,
19492 int declares_class_or_enum
,
19493 bool* function_definition_p
,
19494 tree
* maybe_range_for_decl
,
19495 location_t
* init_loc
,
19498 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19499 *attributes_start_token
= NULL
;
19500 cp_declarator
*declarator
;
19501 tree prefix_attributes
;
19502 tree attributes
= NULL
;
19503 tree asm_specification
;
19505 tree decl
= NULL_TREE
;
19507 int is_initialized
;
19508 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19509 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19511 enum cpp_ttype initialization_kind
;
19512 bool is_direct_init
= false;
19513 bool is_non_constant_init
;
19514 int ctor_dtor_or_conv_p
;
19515 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19516 tree pushed_scope
= NULL_TREE
;
19517 bool range_for_decl_p
= false;
19518 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19519 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19521 /* Gather the attributes that were provided with the
19522 decl-specifiers. */
19523 prefix_attributes
= decl_specifiers
->attributes
;
19525 /* Assume that this is not the declarator for a function
19527 if (function_definition_p
)
19528 *function_definition_p
= false;
19530 /* Default arguments are only permitted for function parameters. */
19531 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19532 parser
->default_arg_ok_p
= false;
19534 /* Defer access checks while parsing the declarator; we cannot know
19535 what names are accessible until we know what is being
19537 resume_deferring_access_checks ();
19539 token
= cp_lexer_peek_token (parser
->lexer
);
19541 /* Parse the declarator. */
19543 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19544 &ctor_dtor_or_conv_p
,
19545 /*parenthesized_p=*/NULL
,
19546 member_p
, friend_p
);
19547 /* Gather up the deferred checks. */
19548 stop_deferring_access_checks ();
19550 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19552 /* If the DECLARATOR was erroneous, there's no need to go
19554 if (declarator
== cp_error_declarator
)
19555 return error_mark_node
;
19557 /* Check that the number of template-parameter-lists is OK. */
19558 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19560 return error_mark_node
;
19562 if (declares_class_or_enum
& 2)
19563 cp_parser_check_for_definition_in_return_type (declarator
,
19564 decl_specifiers
->type
,
19565 decl_specifiers
->locations
[ds_type_spec
]);
19567 /* Figure out what scope the entity declared by the DECLARATOR is
19568 located in. `grokdeclarator' sometimes changes the scope, so
19569 we compute it now. */
19570 scope
= get_scope_of_declarator (declarator
);
19572 /* Perform any lookups in the declared type which were thought to be
19573 dependent, but are not in the scope of the declarator. */
19574 decl_specifiers
->type
19575 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19577 /* If we're allowing GNU extensions, look for an
19578 asm-specification. */
19579 if (cp_parser_allow_gnu_extensions_p (parser
))
19581 /* Look for an asm-specification. */
19582 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19583 asm_specification
= cp_parser_asm_specification_opt (parser
);
19586 asm_specification
= NULL_TREE
;
19588 /* Look for attributes. */
19589 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19590 attributes
= cp_parser_attributes_opt (parser
);
19592 /* Peek at the next token. */
19593 token
= cp_lexer_peek_token (parser
->lexer
);
19595 bool bogus_implicit_tmpl
= false;
19597 if (function_declarator_p (declarator
))
19599 /* Handle C++17 deduction guides. */
19600 if (!decl_specifiers
->type
19601 && ctor_dtor_or_conv_p
<= 0
19602 && cxx_dialect
>= cxx17
)
19604 cp_declarator
*id
= get_id_declarator (declarator
);
19605 tree name
= id
->u
.id
.unqualified_name
;
19606 parser
->scope
= id
->u
.id
.qualifying_scope
;
19607 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19609 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19610 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19612 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19613 id
->u
.id
.sfk
= sfk_deduction_guide
;
19614 ctor_dtor_or_conv_p
= 1;
19618 /* Check to see if the token indicates the start of a
19619 function-definition. */
19620 if (cp_parser_token_starts_function_definition_p (token
))
19622 if (!function_definition_allowed_p
)
19624 /* If a function-definition should not appear here, issue an
19626 cp_parser_error (parser
,
19627 "a function-definition is not allowed here");
19628 return error_mark_node
;
19631 location_t func_brace_location
19632 = cp_lexer_peek_token (parser
->lexer
)->location
;
19634 /* Neither attributes nor an asm-specification are allowed
19635 on a function-definition. */
19636 if (asm_specification
)
19637 error_at (asm_spec_start_token
->location
,
19638 "an asm-specification is not allowed "
19639 "on a function-definition");
19641 error_at (attributes_start_token
->location
,
19642 "attributes are not allowed "
19643 "on a function-definition");
19644 /* This is a function-definition. */
19645 *function_definition_p
= true;
19647 /* Parse the function definition. */
19649 decl
= cp_parser_save_member_function_body (parser
,
19652 prefix_attributes
);
19655 (cp_parser_function_definition_from_specifiers_and_declarator
19656 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19658 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19660 /* This is where the prologue starts... */
19661 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19662 = func_brace_location
;
19668 else if (parser
->fully_implicit_function_template_p
)
19670 /* A non-template declaration involving a function parameter list
19671 containing an implicit template parameter will be made into a
19672 template. If the resulting declaration is not going to be an
19673 actual function then finish the template scope here to prevent it.
19674 An error message will be issued once we have a decl to talk about.
19676 FIXME probably we should do type deduction rather than create an
19677 implicit template, but the standard currently doesn't allow it. */
19678 bogus_implicit_tmpl
= true;
19679 finish_fully_implicit_template (parser
, NULL_TREE
);
19684 Only in function declarations for constructors, destructors, type
19685 conversions, and deduction guides can the decl-specifier-seq be omitted.
19687 We explicitly postpone this check past the point where we handle
19688 function-definitions because we tolerate function-definitions
19689 that are missing their return types in some modes. */
19690 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19692 cp_parser_error (parser
,
19693 "expected constructor, destructor, or type conversion");
19694 return error_mark_node
;
19697 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19698 if (token
->type
== CPP_EQ
19699 || token
->type
== CPP_OPEN_PAREN
19700 || token
->type
== CPP_OPEN_BRACE
)
19702 is_initialized
= SD_INITIALIZED
;
19703 initialization_kind
= token
->type
;
19704 if (maybe_range_for_decl
)
19705 *maybe_range_for_decl
= error_mark_node
;
19706 tmp_init_loc
= token
->location
;
19707 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19708 *init_loc
= tmp_init_loc
;
19710 if (token
->type
== CPP_EQ
19711 && function_declarator_p (declarator
))
19713 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19714 if (t2
->keyword
== RID_DEFAULT
)
19715 is_initialized
= SD_DEFAULTED
;
19716 else if (t2
->keyword
== RID_DELETE
)
19717 is_initialized
= SD_DELETED
;
19722 /* If the init-declarator isn't initialized and isn't followed by a
19723 `,' or `;', it's not a valid init-declarator. */
19724 if (token
->type
!= CPP_COMMA
19725 && token
->type
!= CPP_SEMICOLON
)
19727 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19728 range_for_decl_p
= true;
19731 if (!maybe_range_for_decl
)
19732 cp_parser_error (parser
, "expected initializer");
19733 return error_mark_node
;
19736 is_initialized
= SD_UNINITIALIZED
;
19737 initialization_kind
= CPP_EOF
;
19740 /* Because start_decl has side-effects, we should only call it if we
19741 know we're going ahead. By this point, we know that we cannot
19742 possibly be looking at any other construct. */
19743 cp_parser_commit_to_tentative_parse (parser
);
19745 /* Enter the newly declared entry in the symbol table. If we're
19746 processing a declaration in a class-specifier, we wait until
19747 after processing the initializer. */
19750 if (parser
->in_unbraced_linkage_specification_p
)
19751 decl_specifiers
->storage_class
= sc_extern
;
19752 decl
= start_decl (declarator
, decl_specifiers
,
19753 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19754 attributes
, prefix_attributes
, &pushed_scope
);
19755 cp_finalize_omp_declare_simd (parser
, decl
);
19756 cp_finalize_oacc_routine (parser
, decl
, false);
19757 /* Adjust location of decl if declarator->id_loc is more appropriate:
19758 set, and decl wasn't merged with another decl, in which case its
19759 location would be different from input_location, and more accurate. */
19761 && declarator
->id_loc
!= UNKNOWN_LOCATION
19762 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19763 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19766 /* Enter the SCOPE. That way unqualified names appearing in the
19767 initializer will be looked up in SCOPE. */
19768 pushed_scope
= push_scope (scope
);
19770 /* Perform deferred access control checks, now that we know in which
19771 SCOPE the declared entity resides. */
19772 if (!member_p
&& decl
)
19774 tree saved_current_function_decl
= NULL_TREE
;
19776 /* If the entity being declared is a function, pretend that we
19777 are in its scope. If it is a `friend', it may have access to
19778 things that would not otherwise be accessible. */
19779 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19781 saved_current_function_decl
= current_function_decl
;
19782 current_function_decl
= decl
;
19785 /* Perform access checks for template parameters. */
19786 cp_parser_perform_template_parameter_access_checks (checks
);
19788 /* Perform the access control checks for the declarator and the
19789 decl-specifiers. */
19790 perform_deferred_access_checks (tf_warning_or_error
);
19792 /* Restore the saved value. */
19793 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19794 current_function_decl
= saved_current_function_decl
;
19797 /* Parse the initializer. */
19798 initializer
= NULL_TREE
;
19799 is_direct_init
= false;
19800 is_non_constant_init
= true;
19801 if (is_initialized
)
19803 if (function_declarator_p (declarator
))
19805 if (initialization_kind
== CPP_EQ
)
19806 initializer
= cp_parser_pure_specifier (parser
);
19809 /* If the declaration was erroneous, we don't really
19810 know what the user intended, so just silently
19811 consume the initializer. */
19812 if (decl
!= error_mark_node
)
19813 error_at (tmp_init_loc
, "initializer provided for function");
19814 cp_parser_skip_to_closing_parenthesis (parser
,
19815 /*recovering=*/true,
19816 /*or_comma=*/false,
19817 /*consume_paren=*/true);
19822 /* We want to record the extra mangling scope for in-class
19823 initializers of class members and initializers of static data
19824 member templates. The former involves deferring
19825 parsing of the initializer until end of class as with default
19826 arguments. So right here we only handle the latter. */
19827 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19828 start_lambda_scope (decl
);
19829 initializer
= cp_parser_initializer (parser
,
19831 &is_non_constant_init
);
19832 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19833 finish_lambda_scope ();
19834 if (initializer
== error_mark_node
)
19835 cp_parser_skip_to_end_of_statement (parser
);
19839 /* The old parser allows attributes to appear after a parenthesized
19840 initializer. Mark Mitchell proposed removing this functionality
19841 on the GCC mailing lists on 2002-08-13. This parser accepts the
19842 attributes -- but ignores them. Made a permerror in GCC 8. */
19843 if (cp_parser_allow_gnu_extensions_p (parser
)
19844 && initialization_kind
== CPP_OPEN_PAREN
19845 && cp_parser_attributes_opt (parser
)
19846 && permerror (input_location
,
19847 "attributes after parenthesized initializer ignored"))
19850 if (flag_permissive
&& !hint
)
19853 inform (input_location
,
19854 "this flexibility is deprecated and will be removed");
19858 /* And now complain about a non-function implicit template. */
19859 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19860 error_at (DECL_SOURCE_LOCATION (decl
),
19861 "non-function %qD declared as implicit template", decl
);
19863 /* For an in-class declaration, use `grokfield' to create the
19869 pop_scope (pushed_scope
);
19870 pushed_scope
= NULL_TREE
;
19872 decl
= grokfield (declarator
, decl_specifiers
,
19873 initializer
, !is_non_constant_init
,
19874 /*asmspec=*/NULL_TREE
,
19875 attr_chainon (attributes
, prefix_attributes
));
19876 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19877 cp_parser_save_default_args (parser
, decl
);
19878 cp_finalize_omp_declare_simd (parser
, decl
);
19879 cp_finalize_oacc_routine (parser
, decl
, false);
19882 /* Finish processing the declaration. But, skip member
19884 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19886 cp_finish_decl (decl
,
19887 initializer
, !is_non_constant_init
,
19889 /* If the initializer is in parentheses, then this is
19890 a direct-initialization, which means that an
19891 `explicit' constructor is OK. Otherwise, an
19892 `explicit' constructor cannot be used. */
19893 ((is_direct_init
|| !is_initialized
)
19894 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19896 else if ((cxx_dialect
!= cxx98
) && friend_p
19897 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19898 /* Core issue #226 (C++0x only): A default template-argument
19899 shall not be specified in a friend class template
19901 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19902 /*is_partial=*/false, /*is_friend_decl=*/1);
19904 if (!friend_p
&& pushed_scope
)
19905 pop_scope (pushed_scope
);
19907 if (function_declarator_p (declarator
)
19908 && parser
->fully_implicit_function_template_p
)
19911 decl
= finish_fully_implicit_template (parser
, decl
);
19913 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19916 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19917 && type_uses_auto (decl_specifiers
->type
))
19918 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19923 /* Parse a declarator.
19927 ptr-operator declarator
19929 abstract-declarator:
19930 ptr-operator abstract-declarator [opt]
19931 direct-abstract-declarator
19936 attributes [opt] direct-declarator
19937 attributes [opt] ptr-operator declarator
19939 abstract-declarator:
19940 attributes [opt] ptr-operator abstract-declarator [opt]
19941 attributes [opt] direct-abstract-declarator
19943 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19944 detect constructors, destructors, deduction guides, or conversion operators.
19945 It is set to -1 if the declarator is a name, and +1 if it is a
19946 function. Otherwise it is set to zero. Usually you just want to
19947 test for >0, but internally the negative value is used.
19949 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19950 a decl-specifier-seq unless it declares a constructor, destructor,
19951 or conversion. It might seem that we could check this condition in
19952 semantic analysis, rather than parsing, but that makes it difficult
19953 to handle something like `f()'. We want to notice that there are
19954 no decl-specifiers, and therefore realize that this is an
19955 expression, not a declaration.)
19957 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19958 the declarator is a direct-declarator of the form "(...)".
19960 MEMBER_P is true iff this declarator is a member-declarator.
19962 FRIEND_P is true iff this declarator is a friend. */
19964 static cp_declarator
*
19965 cp_parser_declarator (cp_parser
* parser
,
19966 cp_parser_declarator_kind dcl_kind
,
19967 int* ctor_dtor_or_conv_p
,
19968 bool* parenthesized_p
,
19969 bool member_p
, bool friend_p
)
19971 cp_declarator
*declarator
;
19972 enum tree_code code
;
19973 cp_cv_quals cv_quals
;
19975 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19977 /* Assume this is not a constructor, destructor, or type-conversion
19979 if (ctor_dtor_or_conv_p
)
19980 *ctor_dtor_or_conv_p
= 0;
19982 if (cp_parser_allow_gnu_extensions_p (parser
))
19983 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19985 /* Check for the ptr-operator production. */
19986 cp_parser_parse_tentatively (parser
);
19987 /* Parse the ptr-operator. */
19988 code
= cp_parser_ptr_operator (parser
,
19993 /* If that worked, then we have a ptr-operator. */
19994 if (cp_parser_parse_definitely (parser
))
19996 /* If a ptr-operator was found, then this declarator was not
19998 if (parenthesized_p
)
19999 *parenthesized_p
= true;
20000 /* The dependent declarator is optional if we are parsing an
20001 abstract-declarator. */
20002 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20003 cp_parser_parse_tentatively (parser
);
20005 /* Parse the dependent declarator. */
20006 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20007 /*ctor_dtor_or_conv_p=*/NULL
,
20008 /*parenthesized_p=*/NULL
,
20009 /*member_p=*/false,
20012 /* If we are parsing an abstract-declarator, we must handle the
20013 case where the dependent declarator is absent. */
20014 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20015 && !cp_parser_parse_definitely (parser
))
20018 declarator
= cp_parser_make_indirect_declarator
20019 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20021 /* Everything else is a direct-declarator. */
20024 if (parenthesized_p
)
20025 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20027 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20028 ctor_dtor_or_conv_p
,
20029 member_p
, friend_p
);
20032 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20033 declarator
->attributes
= gnu_attributes
;
20037 /* Parse a direct-declarator or direct-abstract-declarator.
20041 direct-declarator ( parameter-declaration-clause )
20042 cv-qualifier-seq [opt]
20043 ref-qualifier [opt]
20044 exception-specification [opt]
20045 direct-declarator [ constant-expression [opt] ]
20048 direct-abstract-declarator:
20049 direct-abstract-declarator [opt]
20050 ( parameter-declaration-clause )
20051 cv-qualifier-seq [opt]
20052 ref-qualifier [opt]
20053 exception-specification [opt]
20054 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20055 ( abstract-declarator )
20057 Returns a representation of the declarator. DCL_KIND is
20058 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20059 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20060 we are parsing a direct-declarator. It is
20061 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20062 of ambiguity we prefer an abstract declarator, as per
20063 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20064 as for cp_parser_declarator. */
20066 static cp_declarator
*
20067 cp_parser_direct_declarator (cp_parser
* parser
,
20068 cp_parser_declarator_kind dcl_kind
,
20069 int* ctor_dtor_or_conv_p
,
20070 bool member_p
, bool friend_p
)
20073 cp_declarator
*declarator
= NULL
;
20074 tree scope
= NULL_TREE
;
20075 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20076 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20078 tree pushed_scope
= NULL_TREE
;
20079 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20083 /* Peek at the next token. */
20084 token
= cp_lexer_peek_token (parser
->lexer
);
20085 if (token
->type
== CPP_OPEN_PAREN
)
20087 /* This is either a parameter-declaration-clause, or a
20088 parenthesized declarator. When we know we are parsing a
20089 named declarator, it must be a parenthesized declarator
20090 if FIRST is true. For instance, `(int)' is a
20091 parameter-declaration-clause, with an omitted
20092 direct-abstract-declarator. But `((*))', is a
20093 parenthesized abstract declarator. Finally, when T is a
20094 template parameter `(T)' is a
20095 parameter-declaration-clause, and not a parenthesized
20098 We first try and parse a parameter-declaration-clause,
20099 and then try a nested declarator (if FIRST is true).
20101 It is not an error for it not to be a
20102 parameter-declaration-clause, even when FIRST is
20108 The first is the declaration of a function while the
20109 second is the definition of a variable, including its
20112 Having seen only the parenthesis, we cannot know which of
20113 these two alternatives should be selected. Even more
20114 complex are examples like:
20119 The former is a function-declaration; the latter is a
20120 variable initialization.
20122 Thus again, we try a parameter-declaration-clause, and if
20123 that fails, we back out and return. */
20125 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20128 bool is_declarator
= false;
20132 /* In a member-declarator, the only valid interpretation
20133 of a parenthesis is the start of a
20134 parameter-declaration-clause. (It is invalid to
20135 initialize a static data member with a parenthesized
20136 initializer; only the "=" form of initialization is
20139 cp_parser_parse_tentatively (parser
);
20141 /* Consume the `('. */
20142 matching_parens parens
;
20143 parens
.consume_open (parser
);
20146 /* If this is going to be an abstract declarator, we're
20147 in a declarator and we can't have default args. */
20148 parser
->default_arg_ok_p
= false;
20149 parser
->in_declarator_p
= true;
20152 begin_scope (sk_function_parms
, NULL_TREE
);
20154 /* Parse the parameter-declaration-clause. */
20155 params
= cp_parser_parameter_declaration_clause (parser
);
20157 /* Consume the `)'. */
20158 parens
.require_close (parser
);
20160 /* If all went well, parse the cv-qualifier-seq,
20161 ref-qualifier and the exception-specification. */
20162 if (member_p
|| cp_parser_parse_definitely (parser
))
20164 cp_cv_quals cv_quals
;
20165 cp_virt_specifiers virt_specifiers
;
20166 cp_ref_qualifier ref_qual
;
20167 tree exception_specification
;
20170 bool memfn
= (member_p
|| (pushed_scope
20171 && CLASS_TYPE_P (pushed_scope
)));
20173 is_declarator
= true;
20175 if (ctor_dtor_or_conv_p
)
20176 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20179 /* Parse the cv-qualifier-seq. */
20180 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20181 /* Parse the ref-qualifier. */
20182 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20183 /* Parse the tx-qualifier. */
20184 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20185 /* And the exception-specification. */
20186 exception_specification
20187 = cp_parser_exception_specification_opt (parser
);
20189 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20191 /* In here, we handle cases where attribute is used after
20192 the function declaration. For example:
20193 void func (int x) __attribute__((vector(..))); */
20194 tree gnu_attrs
= NULL_TREE
;
20195 tree requires_clause
= NULL_TREE
;
20196 late_return
= (cp_parser_late_return_type_opt
20197 (parser
, declarator
, requires_clause
,
20198 memfn
? cv_quals
: -1));
20200 /* Parse the virt-specifier-seq. */
20201 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20203 /* Create the function-declarator. */
20204 declarator
= make_call_declarator (declarator
,
20210 exception_specification
,
20213 declarator
->std_attributes
= attrs
;
20214 declarator
->attributes
= gnu_attrs
;
20215 /* Any subsequent parameter lists are to do with
20216 return type, so are not those of the declared
20218 parser
->default_arg_ok_p
= false;
20221 /* Remove the function parms from scope. */
20222 pop_bindings_and_leave_scope ();
20225 /* Repeat the main loop. */
20229 /* If this is the first, we can try a parenthesized
20233 bool saved_in_type_id_in_expr_p
;
20235 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20236 parser
->in_declarator_p
= saved_in_declarator_p
;
20238 open_paren
= token
;
20239 /* Consume the `('. */
20240 matching_parens parens
;
20241 parens
.consume_open (parser
);
20242 /* Parse the nested declarator. */
20243 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20244 parser
->in_type_id_in_expr_p
= true;
20246 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20247 /*parenthesized_p=*/NULL
,
20248 member_p
, friend_p
);
20249 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20251 /* Expect a `)'. */
20252 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20253 if (!parens
.require_close (parser
))
20254 declarator
= cp_error_declarator
;
20255 if (declarator
== cp_error_declarator
)
20258 goto handle_declarator
;
20260 /* Otherwise, we must be done. */
20264 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20265 && token
->type
== CPP_OPEN_SQUARE
20266 && !cp_next_tokens_can_be_attribute_p (parser
))
20268 /* Parse an array-declarator. */
20269 tree bounds
, attrs
;
20271 if (ctor_dtor_or_conv_p
)
20272 *ctor_dtor_or_conv_p
= 0;
20276 parser
->default_arg_ok_p
= false;
20277 parser
->in_declarator_p
= true;
20278 /* Consume the `['. */
20279 cp_lexer_consume_token (parser
->lexer
);
20280 /* Peek at the next token. */
20281 token
= cp_lexer_peek_token (parser
->lexer
);
20282 /* If the next token is `]', then there is no
20283 constant-expression. */
20284 if (token
->type
!= CPP_CLOSE_SQUARE
)
20286 bool non_constant_p
;
20288 = cp_parser_constant_expression (parser
,
20289 /*allow_non_constant=*/true,
20291 if (!non_constant_p
)
20293 else if (error_operand_p (bounds
))
20294 /* Already gave an error. */;
20295 else if (!parser
->in_function_body
20296 || current_binding_level
->kind
== sk_function_parms
)
20298 /* Normally, the array bound must be an integral constant
20299 expression. However, as an extension, we allow VLAs
20300 in function scopes as long as they aren't part of a
20301 parameter declaration. */
20302 cp_parser_error (parser
,
20303 "array bound is not an integer constant");
20304 bounds
= error_mark_node
;
20306 else if (processing_template_decl
20307 && !type_dependent_expression_p (bounds
))
20309 /* Remember this wasn't a constant-expression. */
20310 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20311 TREE_SIDE_EFFECTS (bounds
) = 1;
20315 bounds
= NULL_TREE
;
20316 /* Look for the closing `]'. */
20317 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20319 declarator
= cp_error_declarator
;
20323 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20324 declarator
= make_array_declarator (declarator
, bounds
);
20325 declarator
->std_attributes
= attrs
;
20327 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20330 tree qualifying_scope
;
20331 tree unqualified_name
;
20333 special_function_kind sfk
;
20335 bool pack_expansion_p
= false;
20336 cp_token
*declarator_id_start_token
;
20338 /* Parse a declarator-id */
20339 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20342 cp_parser_parse_tentatively (parser
);
20344 /* If we see an ellipsis, we should be looking at a
20346 if (token
->type
== CPP_ELLIPSIS
)
20348 /* Consume the `...' */
20349 cp_lexer_consume_token (parser
->lexer
);
20351 pack_expansion_p
= true;
20355 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20357 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20358 qualifying_scope
= parser
->scope
;
20363 if (!unqualified_name
&& pack_expansion_p
)
20365 /* Check whether an error occurred. */
20366 okay
= !cp_parser_error_occurred (parser
);
20368 /* We already consumed the ellipsis to mark a
20369 parameter pack, but we have no way to report it,
20370 so abort the tentative parse. We will be exiting
20371 immediately anyway. */
20372 cp_parser_abort_tentative_parse (parser
);
20375 okay
= cp_parser_parse_definitely (parser
);
20378 unqualified_name
= error_mark_node
;
20379 else if (unqualified_name
20380 && (qualifying_scope
20381 || (!identifier_p (unqualified_name
))))
20383 cp_parser_error (parser
, "expected unqualified-id");
20384 unqualified_name
= error_mark_node
;
20388 if (!unqualified_name
)
20390 if (unqualified_name
== error_mark_node
)
20392 declarator
= cp_error_declarator
;
20393 pack_expansion_p
= false;
20394 declarator
->parameter_pack_p
= false;
20398 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20400 if (qualifying_scope
&& at_namespace_scope_p ()
20401 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20403 /* In the declaration of a member of a template class
20404 outside of the class itself, the SCOPE will sometimes
20405 be a TYPENAME_TYPE. For example, given:
20407 template <typename T>
20408 int S<T>::R::i = 3;
20410 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20411 this context, we must resolve S<T>::R to an ordinary
20412 type, rather than a typename type.
20414 The reason we normally avoid resolving TYPENAME_TYPEs
20415 is that a specialization of `S' might render
20416 `S<T>::R' not a type. However, if `S' is
20417 specialized, then this `i' will not be used, so there
20418 is no harm in resolving the types here. */
20421 /* Resolve the TYPENAME_TYPE. */
20422 type
= resolve_typename_type (qualifying_scope
,
20423 /*only_current_p=*/false);
20424 /* If that failed, the declarator is invalid. */
20425 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20427 if (typedef_variant_p (type
))
20428 error_at (declarator_id_start_token
->location
,
20429 "cannot define member of dependent typedef "
20432 error_at (declarator_id_start_token
->location
,
20433 "%<%T::%E%> is not a type",
20434 TYPE_CONTEXT (qualifying_scope
),
20435 TYPE_IDENTIFIER (qualifying_scope
));
20437 qualifying_scope
= type
;
20442 if (unqualified_name
)
20446 if (qualifying_scope
20447 && CLASS_TYPE_P (qualifying_scope
))
20448 class_type
= qualifying_scope
;
20450 class_type
= current_class_type
;
20452 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20454 tree name_type
= TREE_TYPE (unqualified_name
);
20456 if (!class_type
|| !same_type_p (name_type
, class_type
))
20458 /* We do not attempt to print the declarator
20459 here because we do not have enough
20460 information about its original syntactic
20462 cp_parser_error (parser
, "invalid declarator");
20463 declarator
= cp_error_declarator
;
20466 else if (qualifying_scope
20467 && CLASSTYPE_USE_TEMPLATE (name_type
))
20469 error_at (declarator_id_start_token
->location
,
20470 "invalid use of constructor as a template");
20471 inform (declarator_id_start_token
->location
,
20472 "use %<%T::%D%> instead of %<%T::%D%> to "
20473 "name the constructor in a qualified name",
20475 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20476 class_type
, name_type
);
20477 declarator
= cp_error_declarator
;
20480 unqualified_name
= constructor_name (class_type
);
20485 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20486 sfk
= sfk_destructor
;
20487 else if (identifier_p (unqualified_name
)
20488 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20489 sfk
= sfk_conversion
;
20490 else if (/* There's no way to declare a constructor
20491 for an unnamed type, even if the type
20492 got a name for linkage purposes. */
20493 !TYPE_WAS_UNNAMED (class_type
)
20494 /* Handle correctly (c++/19200):
20508 friend void N::S();
20510 && (!friend_p
|| class_type
== qualifying_scope
)
20511 && constructor_name_p (unqualified_name
,
20513 sfk
= sfk_constructor
;
20514 else if (is_overloaded_fn (unqualified_name
)
20515 && DECL_CONSTRUCTOR_P (get_first_fn
20516 (unqualified_name
)))
20517 sfk
= sfk_constructor
;
20519 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20520 *ctor_dtor_or_conv_p
= -1;
20523 declarator
= make_id_declarator (qualifying_scope
,
20526 declarator
->std_attributes
= attrs
;
20527 declarator
->id_loc
= token
->location
;
20528 declarator
->parameter_pack_p
= pack_expansion_p
;
20530 if (pack_expansion_p
)
20531 maybe_warn_variadic_templates ();
20534 handle_declarator
:;
20535 scope
= get_scope_of_declarator (declarator
);
20538 /* Any names that appear after the declarator-id for a
20539 member are looked up in the containing scope. */
20540 if (at_function_scope_p ())
20542 /* But declarations with qualified-ids can't appear in a
20544 cp_parser_error (parser
, "qualified-id in declaration");
20545 declarator
= cp_error_declarator
;
20548 pushed_scope
= push_scope (scope
);
20550 parser
->in_declarator_p
= true;
20551 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20552 || (declarator
&& declarator
->kind
== cdk_id
))
20553 /* Default args are only allowed on function
20555 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20557 parser
->default_arg_ok_p
= false;
20566 /* For an abstract declarator, we might wind up with nothing at this
20567 point. That's an error; the declarator is not optional. */
20569 cp_parser_error (parser
, "expected declarator");
20570 else if (open_paren
)
20572 /* Record overly parenthesized declarator so we can give a
20573 diagnostic about confusing decl/expr disambiguation. */
20574 if (declarator
->kind
== cdk_array
)
20576 /* If the open and close parens are on different lines, this
20577 is probably a formatting thing, so ignore. */
20578 expanded_location open
= expand_location (open_paren
->location
);
20579 expanded_location close
= expand_location (close_paren
->location
);
20580 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20584 declarator
->parenthesized
= open_paren
->location
;
20587 /* If we entered a scope, we must exit it now. */
20589 pop_scope (pushed_scope
);
20591 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20592 parser
->in_declarator_p
= saved_in_declarator_p
;
20597 /* Parse a ptr-operator.
20600 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20601 * cv-qualifier-seq [opt]
20603 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20604 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20609 & cv-qualifier-seq [opt]
20611 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20612 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20613 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20614 filled in with the TYPE containing the member. *CV_QUALS is
20615 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20616 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20617 Note that the tree codes returned by this function have nothing
20618 to do with the types of trees that will be eventually be created
20619 to represent the pointer or reference type being parsed. They are
20620 just constants with suggestive names. */
20621 static enum tree_code
20622 cp_parser_ptr_operator (cp_parser
* parser
,
20624 cp_cv_quals
*cv_quals
,
20627 enum tree_code code
= ERROR_MARK
;
20629 tree attrs
= NULL_TREE
;
20631 /* Assume that it's not a pointer-to-member. */
20633 /* And that there are no cv-qualifiers. */
20634 *cv_quals
= TYPE_UNQUALIFIED
;
20636 /* Peek at the next token. */
20637 token
= cp_lexer_peek_token (parser
->lexer
);
20639 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20640 if (token
->type
== CPP_MULT
)
20641 code
= INDIRECT_REF
;
20642 else if (token
->type
== CPP_AND
)
20644 else if ((cxx_dialect
!= cxx98
) &&
20645 token
->type
== CPP_AND_AND
) /* C++0x only */
20646 code
= NON_LVALUE_EXPR
;
20648 if (code
!= ERROR_MARK
)
20650 /* Consume the `*', `&' or `&&'. */
20651 cp_lexer_consume_token (parser
->lexer
);
20653 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20654 `&', if we are allowing GNU extensions. (The only qualifier
20655 that can legally appear after `&' is `restrict', but that is
20656 enforced during semantic analysis. */
20657 if (code
== INDIRECT_REF
20658 || cp_parser_allow_gnu_extensions_p (parser
))
20659 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20661 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20662 if (attributes
!= NULL
)
20663 *attributes
= attrs
;
20667 /* Try the pointer-to-member case. */
20668 cp_parser_parse_tentatively (parser
);
20669 /* Look for the optional `::' operator. */
20670 cp_parser_global_scope_opt (parser
,
20671 /*current_scope_valid_p=*/false);
20672 /* Look for the nested-name specifier. */
20673 token
= cp_lexer_peek_token (parser
->lexer
);
20674 cp_parser_nested_name_specifier (parser
,
20675 /*typename_keyword_p=*/false,
20676 /*check_dependency_p=*/true,
20678 /*is_declaration=*/false);
20679 /* If we found it, and the next token is a `*', then we are
20680 indeed looking at a pointer-to-member operator. */
20681 if (!cp_parser_error_occurred (parser
)
20682 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20684 /* Indicate that the `*' operator was used. */
20685 code
= INDIRECT_REF
;
20687 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20688 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20689 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20690 error_at (token
->location
, "cannot form pointer to member of "
20691 "non-class %q#T", parser
->scope
);
20694 /* The type of which the member is a member is given by the
20696 *type
= parser
->scope
;
20697 /* The next name will not be qualified. */
20698 parser
->scope
= NULL_TREE
;
20699 parser
->qualifying_scope
= NULL_TREE
;
20700 parser
->object_scope
= NULL_TREE
;
20701 /* Look for optional c++11 attributes. */
20702 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20703 if (attributes
!= NULL
)
20704 *attributes
= attrs
;
20705 /* Look for the optional cv-qualifier-seq. */
20706 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20709 /* If that didn't work we don't have a ptr-operator. */
20710 if (!cp_parser_parse_definitely (parser
))
20711 cp_parser_error (parser
, "expected ptr-operator");
20717 /* Parse an (optional) cv-qualifier-seq.
20720 cv-qualifier cv-qualifier-seq [opt]
20731 Returns a bitmask representing the cv-qualifiers. */
20734 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20736 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20741 cp_cv_quals cv_qualifier
;
20743 /* Peek at the next token. */
20744 token
= cp_lexer_peek_token (parser
->lexer
);
20745 /* See if it's a cv-qualifier. */
20746 switch (token
->keyword
)
20749 cv_qualifier
= TYPE_QUAL_CONST
;
20753 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20757 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20761 cv_qualifier
= TYPE_UNQUALIFIED
;
20768 if (cv_quals
& cv_qualifier
)
20770 gcc_rich_location
richloc (token
->location
);
20771 richloc
.add_fixit_remove ();
20772 error_at (&richloc
, "duplicate cv-qualifier");
20773 cp_lexer_purge_token (parser
->lexer
);
20777 cp_lexer_consume_token (parser
->lexer
);
20778 cv_quals
|= cv_qualifier
;
20785 /* Parse an (optional) ref-qualifier
20791 Returns cp_ref_qualifier representing ref-qualifier. */
20793 static cp_ref_qualifier
20794 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20796 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20798 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20799 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20804 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20805 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20807 switch (token
->type
)
20810 curr_ref_qual
= REF_QUAL_LVALUE
;
20814 curr_ref_qual
= REF_QUAL_RVALUE
;
20818 curr_ref_qual
= REF_QUAL_NONE
;
20822 if (!curr_ref_qual
)
20826 error_at (token
->location
, "multiple ref-qualifiers");
20827 cp_lexer_purge_token (parser
->lexer
);
20831 ref_qual
= curr_ref_qual
;
20832 cp_lexer_consume_token (parser
->lexer
);
20839 /* Parse an optional tx-qualifier.
20843 transaction_safe_dynamic */
20846 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20848 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20849 if (token
->type
== CPP_NAME
)
20851 tree name
= token
->u
.value
;
20852 const char *p
= IDENTIFIER_POINTER (name
);
20853 const int len
= strlen ("transaction_safe");
20854 if (!strncmp (p
, "transaction_safe", len
))
20858 || !strcmp (p
, "_dynamic"))
20860 cp_lexer_consume_token (parser
->lexer
);
20863 error ("%qE requires %<-fgnu-tm%>", name
);
20874 /* Parse an (optional) virt-specifier-seq.
20876 virt-specifier-seq:
20877 virt-specifier virt-specifier-seq [opt]
20883 Returns a bitmask representing the virt-specifiers. */
20885 static cp_virt_specifiers
20886 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20888 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20893 cp_virt_specifiers virt_specifier
;
20895 /* Peek at the next token. */
20896 token
= cp_lexer_peek_token (parser
->lexer
);
20897 /* See if it's a virt-specifier-qualifier. */
20898 if (token
->type
!= CPP_NAME
)
20900 if (id_equal (token
->u
.value
, "override"))
20902 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20903 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20905 else if (id_equal (token
->u
.value
, "final"))
20907 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20908 virt_specifier
= VIRT_SPEC_FINAL
;
20910 else if (id_equal (token
->u
.value
, "__final"))
20912 virt_specifier
= VIRT_SPEC_FINAL
;
20917 if (virt_specifiers
& virt_specifier
)
20919 gcc_rich_location
richloc (token
->location
);
20920 richloc
.add_fixit_remove ();
20921 error_at (&richloc
, "duplicate virt-specifier");
20922 cp_lexer_purge_token (parser
->lexer
);
20926 cp_lexer_consume_token (parser
->lexer
);
20927 virt_specifiers
|= virt_specifier
;
20930 return virt_specifiers
;
20933 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20934 is in scope even though it isn't real. */
20937 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20941 if (current_class_ptr
)
20943 /* We don't clear this between NSDMIs. Is it already what we want? */
20944 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20945 if (DECL_P (current_class_ptr
)
20946 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20947 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20948 && cp_type_quals (type
) == quals
)
20952 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20953 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20954 current_class_ptr
= NULL_TREE
;
20956 = cp_build_fold_indirect_ref (this_parm
);
20957 current_class_ptr
= this_parm
;
20960 /* Return true iff our current scope is a non-static data member
20964 parsing_nsdmi (void)
20966 /* We recognize NSDMI context by the context-less 'this' pointer set up
20967 by the function above. */
20968 if (current_class_ptr
20969 && TREE_CODE (current_class_ptr
) == PARM_DECL
20970 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20975 /* Parse a late-specified return type, if any. This is not a separate
20976 non-terminal, but part of a function declarator, which looks like
20978 -> trailing-type-specifier-seq abstract-declarator(opt)
20980 Returns the type indicated by the type-id.
20982 In addition to this, parse any queued up #pragma omp declare simd
20983 clauses, and #pragma acc routine clauses.
20985 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20989 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20990 tree
& requires_clause
, cp_cv_quals quals
)
20993 tree type
= NULL_TREE
;
20994 bool declare_simd_p
= (parser
->omp_declare_simd
20996 && declarator
->kind
== cdk_id
);
20998 bool oacc_routine_p
= (parser
->oacc_routine
21000 && declarator
->kind
== cdk_id
);
21002 /* Peek at the next token. */
21003 token
= cp_lexer_peek_token (parser
->lexer
);
21004 /* A late-specified return type is indicated by an initial '->'. */
21005 if (token
->type
!= CPP_DEREF
21006 && token
->keyword
!= RID_REQUIRES
21007 && !(token
->type
== CPP_NAME
21008 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21009 && !(declare_simd_p
|| oacc_routine_p
))
21012 tree save_ccp
= current_class_ptr
;
21013 tree save_ccr
= current_class_ref
;
21016 /* DR 1207: 'this' is in scope in the trailing return type. */
21017 inject_this_parameter (current_class_type
, quals
);
21020 if (token
->type
== CPP_DEREF
)
21022 /* Consume the ->. */
21023 cp_lexer_consume_token (parser
->lexer
);
21025 type
= cp_parser_trailing_type_id (parser
);
21028 /* Function declarations may be followed by a trailing
21029 requires-clause. */
21030 requires_clause
= cp_parser_requires_clause_opt (parser
);
21032 if (declare_simd_p
)
21033 declarator
->attributes
21034 = cp_parser_late_parsing_omp_declare_simd (parser
,
21035 declarator
->attributes
);
21036 if (oacc_routine_p
)
21037 declarator
->attributes
21038 = cp_parser_late_parsing_oacc_routine (parser
,
21039 declarator
->attributes
);
21043 current_class_ptr
= save_ccp
;
21044 current_class_ref
= save_ccr
;
21050 /* Parse a declarator-id.
21054 :: [opt] nested-name-specifier [opt] type-name
21056 In the `id-expression' case, the value returned is as for
21057 cp_parser_id_expression if the id-expression was an unqualified-id.
21058 If the id-expression was a qualified-id, then a SCOPE_REF is
21059 returned. The first operand is the scope (either a NAMESPACE_DECL
21060 or TREE_TYPE), but the second is still just a representation of an
21064 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21067 /* The expression must be an id-expression. Assume that qualified
21068 names are the names of types so that:
21071 int S<T>::R::i = 3;
21073 will work; we must treat `S<T>::R' as the name of a type.
21074 Similarly, assume that qualified names are templates, where
21078 int S<T>::R<T>::i = 3;
21081 id
= cp_parser_id_expression (parser
,
21082 /*template_keyword_p=*/false,
21083 /*check_dependency_p=*/false,
21084 /*template_p=*/NULL
,
21085 /*declarator_p=*/true,
21087 if (id
&& BASELINK_P (id
))
21088 id
= BASELINK_FUNCTIONS (id
);
21092 /* Parse a type-id.
21095 type-specifier-seq abstract-declarator [opt]
21097 Returns the TYPE specified. */
21100 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
21101 bool is_trailing_return
)
21103 cp_decl_specifier_seq type_specifier_seq
;
21104 cp_declarator
*abstract_declarator
;
21106 /* Parse the type-specifier-seq. */
21107 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
21108 is_trailing_return
,
21109 &type_specifier_seq
);
21110 if (is_template_arg
&& type_specifier_seq
.type
21111 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21112 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21113 /* A bare template name as a template argument is a template template
21114 argument, not a placeholder, so fail parsing it as a type argument. */
21116 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21117 cp_parser_simulate_error (parser
);
21118 return error_mark_node
;
21120 if (type_specifier_seq
.type
== error_mark_node
)
21121 return error_mark_node
;
21123 /* There might or might not be an abstract declarator. */
21124 cp_parser_parse_tentatively (parser
);
21125 /* Look for the declarator. */
21126 abstract_declarator
21127 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
21128 /*parenthesized_p=*/NULL
,
21129 /*member_p=*/false,
21130 /*friend_p=*/false);
21131 /* Check to see if there really was a declarator. */
21132 if (!cp_parser_parse_definitely (parser
))
21133 abstract_declarator
= NULL
;
21135 if (type_specifier_seq
.type
21136 /* The concepts TS allows 'auto' as a type-id. */
21137 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21138 /* None of the valid uses of 'auto' in C++14 involve the type-id
21139 nonterminal, but it is valid in a trailing-return-type. */
21140 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21141 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21143 /* A type-id with type 'auto' is only ok if the abstract declarator
21144 is a function declarator with a late-specified return type.
21146 A type-id with 'auto' is also valid in a trailing-return-type
21147 in a compound-requirement. */
21148 if (abstract_declarator
21149 && abstract_declarator
->kind
== cdk_function
21150 && abstract_declarator
->u
.function
.late_return_type
)
21152 else if (parser
->in_result_type_constraint_p
)
21156 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21157 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21159 error_at (loc
, "missing template arguments after %qT",
21161 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21165 error_at (loc
, "invalid use of %qT", auto_node
);
21166 return error_mark_node
;
21170 return groktypename (&type_specifier_seq
, abstract_declarator
,
21175 cp_parser_type_id (cp_parser
*parser
)
21177 return cp_parser_type_id_1 (parser
, false, false);
21181 cp_parser_template_type_arg (cp_parser
*parser
)
21184 const char *saved_message
= parser
->type_definition_forbidden_message
;
21185 parser
->type_definition_forbidden_message
21186 = G_("types may not be defined in template arguments");
21187 r
= cp_parser_type_id_1 (parser
, true, false);
21188 parser
->type_definition_forbidden_message
= saved_message
;
21189 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21191 error ("invalid use of %<auto%> in template argument");
21192 r
= error_mark_node
;
21198 cp_parser_trailing_type_id (cp_parser
*parser
)
21200 return cp_parser_type_id_1 (parser
, false, true);
21203 /* Parse a type-specifier-seq.
21205 type-specifier-seq:
21206 type-specifier type-specifier-seq [opt]
21210 type-specifier-seq:
21211 attributes type-specifier-seq [opt]
21213 If IS_DECLARATION is true, we are at the start of a "condition" or
21214 exception-declaration, so we might be followed by a declarator-id.
21216 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21217 i.e. we've just seen "->".
21219 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21222 cp_parser_type_specifier_seq (cp_parser
* parser
,
21223 bool is_declaration
,
21224 bool is_trailing_return
,
21225 cp_decl_specifier_seq
*type_specifier_seq
)
21227 bool seen_type_specifier
= false;
21228 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21229 cp_token
*start_token
= NULL
;
21231 /* Clear the TYPE_SPECIFIER_SEQ. */
21232 clear_decl_specs (type_specifier_seq
);
21234 /* In the context of a trailing return type, enum E { } is an
21235 elaborated-type-specifier followed by a function-body, not an
21237 if (is_trailing_return
)
21238 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21240 /* Parse the type-specifiers and attributes. */
21243 tree type_specifier
;
21244 bool is_cv_qualifier
;
21246 /* Check for attributes first. */
21247 if (cp_next_tokens_can_be_attribute_p (parser
))
21249 type_specifier_seq
->attributes
21250 = attr_chainon (type_specifier_seq
->attributes
,
21251 cp_parser_attributes_opt (parser
));
21255 /* record the token of the beginning of the type specifier seq,
21256 for error reporting purposes*/
21258 start_token
= cp_lexer_peek_token (parser
->lexer
);
21260 /* Look for the type-specifier. */
21261 type_specifier
= cp_parser_type_specifier (parser
,
21263 type_specifier_seq
,
21264 /*is_declaration=*/false,
21267 if (!type_specifier
)
21269 /* If the first type-specifier could not be found, this is not a
21270 type-specifier-seq at all. */
21271 if (!seen_type_specifier
)
21273 /* Set in_declarator_p to avoid skipping to the semicolon. */
21274 int in_decl
= parser
->in_declarator_p
;
21275 parser
->in_declarator_p
= true;
21277 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21278 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21279 cp_parser_error (parser
, "expected type-specifier");
21281 parser
->in_declarator_p
= in_decl
;
21283 type_specifier_seq
->type
= error_mark_node
;
21286 /* If subsequent type-specifiers could not be found, the
21287 type-specifier-seq is complete. */
21291 seen_type_specifier
= true;
21292 /* The standard says that a condition can be:
21294 type-specifier-seq declarator = assignment-expression
21301 we should treat the "S" as a declarator, not as a
21302 type-specifier. The standard doesn't say that explicitly for
21303 type-specifier-seq, but it does say that for
21304 decl-specifier-seq in an ordinary declaration. Perhaps it
21305 would be clearer just to allow a decl-specifier-seq here, and
21306 then add a semantic restriction that if any decl-specifiers
21307 that are not type-specifiers appear, the program is invalid. */
21308 if (is_declaration
&& !is_cv_qualifier
)
21309 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21313 /* Return whether the function currently being declared has an associated
21314 template parameter list. */
21317 function_being_declared_is_template_p (cp_parser
* parser
)
21319 if (!current_template_parms
|| processing_template_parmlist
)
21322 if (parser
->implicit_template_scope
)
21325 if (at_class_scope_p ()
21326 && TYPE_BEING_DEFINED (current_class_type
))
21327 return parser
->num_template_parameter_lists
!= 0;
21329 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21330 (current_class_type
));
21333 /* Parse a parameter-declaration-clause.
21335 parameter-declaration-clause:
21336 parameter-declaration-list [opt] ... [opt]
21337 parameter-declaration-list , ...
21339 Returns a representation for the parameter declarations. A return
21340 value of NULL indicates a parameter-declaration-clause consisting
21341 only of an ellipsis. */
21344 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21350 temp_override
<bool> cleanup
21351 (parser
->auto_is_implicit_function_template_parm_p
);
21353 if (!processing_specialization
21354 && !processing_template_parmlist
21355 && !processing_explicit_instantiation
21356 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21357 actual function or a random abstract declarator. */
21358 && parser
->default_arg_ok_p
)
21359 if (!current_function_decl
21360 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21361 parser
->auto_is_implicit_function_template_parm_p
= true;
21363 /* Peek at the next token. */
21364 token
= cp_lexer_peek_token (parser
->lexer
);
21365 /* Check for trivial parameter-declaration-clauses. */
21366 if (token
->type
== CPP_ELLIPSIS
)
21368 /* Consume the `...' token. */
21369 cp_lexer_consume_token (parser
->lexer
);
21372 else if (token
->type
== CPP_CLOSE_PAREN
)
21373 /* There are no parameters. */
21374 return void_list_node
;
21375 /* Check for `(void)', too, which is a special case. */
21376 else if (token
->keyword
== RID_VOID
21377 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21378 == CPP_CLOSE_PAREN
))
21380 /* Consume the `void' token. */
21381 cp_lexer_consume_token (parser
->lexer
);
21382 /* There are no parameters. */
21383 return void_list_node
;
21386 /* Parse the parameter-declaration-list. */
21387 parameters
= cp_parser_parameter_declaration_list (parser
);
21388 /* If a parse error occurred while parsing the
21389 parameter-declaration-list, then the entire
21390 parameter-declaration-clause is erroneous. */
21391 if (parameters
== error_mark_node
)
21394 /* Peek at the next token. */
21395 token
= cp_lexer_peek_token (parser
->lexer
);
21396 /* If it's a `,', the clause should terminate with an ellipsis. */
21397 if (token
->type
== CPP_COMMA
)
21399 /* Consume the `,'. */
21400 cp_lexer_consume_token (parser
->lexer
);
21401 /* Expect an ellipsis. */
21403 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21405 /* It might also be `...' if the optional trailing `,' was
21407 else if (token
->type
== CPP_ELLIPSIS
)
21409 /* Consume the `...' token. */
21410 cp_lexer_consume_token (parser
->lexer
);
21411 /* And remember that we saw it. */
21415 ellipsis_p
= false;
21417 /* Finish the parameter list. */
21419 parameters
= chainon (parameters
, void_list_node
);
21424 /* Parse a parameter-declaration-list.
21426 parameter-declaration-list:
21427 parameter-declaration
21428 parameter-declaration-list , parameter-declaration
21430 Returns a representation of the parameter-declaration-list, as for
21431 cp_parser_parameter_declaration_clause. However, the
21432 `void_list_node' is never appended to the list. */
21435 cp_parser_parameter_declaration_list (cp_parser
* parser
)
21437 tree parameters
= NULL_TREE
;
21438 tree
*tail
= ¶meters
;
21439 bool saved_in_unbraced_linkage_specification_p
;
21442 /* The special considerations that apply to a function within an
21443 unbraced linkage specifications do not apply to the parameters
21444 to the function. */
21445 saved_in_unbraced_linkage_specification_p
21446 = parser
->in_unbraced_linkage_specification_p
;
21447 parser
->in_unbraced_linkage_specification_p
= false;
21449 /* Look for more parameters. */
21452 cp_parameter_declarator
*parameter
;
21453 tree decl
= error_mark_node
;
21454 bool parenthesized_p
= false;
21456 /* Parse the parameter. */
21458 = cp_parser_parameter_declaration (parser
,
21459 /*template_parm_p=*/false,
21462 /* We don't know yet if the enclosing context is deprecated, so wait
21463 and warn in grokparms if appropriate. */
21464 deprecated_state
= DEPRECATED_SUPPRESS
;
21468 decl
= grokdeclarator (parameter
->declarator
,
21469 ¶meter
->decl_specifiers
,
21471 parameter
->default_argument
!= NULL_TREE
,
21472 ¶meter
->decl_specifiers
.attributes
);
21473 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21474 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21477 deprecated_state
= DEPRECATED_NORMAL
;
21479 /* If a parse error occurred parsing the parameter declaration,
21480 then the entire parameter-declaration-list is erroneous. */
21481 if (decl
== error_mark_node
)
21483 parameters
= error_mark_node
;
21487 if (parameter
->decl_specifiers
.attributes
)
21488 cplus_decl_attributes (&decl
,
21489 parameter
->decl_specifiers
.attributes
,
21491 if (DECL_NAME (decl
))
21492 decl
= pushdecl (decl
);
21494 if (decl
!= error_mark_node
)
21496 retrofit_lang_decl (decl
);
21497 DECL_PARM_INDEX (decl
) = ++index
;
21498 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21501 /* Add the new parameter to the list. */
21502 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21503 tail
= &TREE_CHAIN (*tail
);
21505 /* Peek at the next token. */
21506 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21507 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21508 /* These are for Objective-C++ */
21509 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21510 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21511 /* The parameter-declaration-list is complete. */
21513 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21517 /* Peek at the next token. */
21518 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21519 /* If it's an ellipsis, then the list is complete. */
21520 if (token
->type
== CPP_ELLIPSIS
)
21522 /* Otherwise, there must be more parameters. Consume the
21524 cp_lexer_consume_token (parser
->lexer
);
21525 /* When parsing something like:
21527 int i(float f, double d)
21529 we can tell after seeing the declaration for "f" that we
21530 are not looking at an initialization of a variable "i",
21531 but rather at the declaration of a function "i".
21533 Due to the fact that the parsing of template arguments
21534 (as specified to a template-id) requires backtracking we
21535 cannot use this technique when inside a template argument
21537 if (!parser
->in_template_argument_list_p
21538 && !parser
->in_type_id_in_expr_p
21539 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21540 /* However, a parameter-declaration of the form
21541 "float(f)" (which is a valid declaration of a
21542 parameter "f") can also be interpreted as an
21543 expression (the conversion of "f" to "float"). */
21544 && !parenthesized_p
)
21545 cp_parser_commit_to_tentative_parse (parser
);
21549 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21550 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21551 cp_parser_skip_to_closing_parenthesis (parser
,
21552 /*recovering=*/true,
21553 /*or_comma=*/false,
21554 /*consume_paren=*/false);
21559 parser
->in_unbraced_linkage_specification_p
21560 = saved_in_unbraced_linkage_specification_p
;
21562 /* Reset implicit_template_scope if we are about to leave the function
21563 parameter list that introduced it. Note that for out-of-line member
21564 definitions, there will be one or more class scopes before we get to
21565 the template parameter scope. */
21567 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21568 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21570 while (maybe_its
->kind
== sk_class
)
21571 maybe_its
= maybe_its
->level_chain
;
21572 if (maybe_its
== its
)
21574 parser
->implicit_template_parms
= 0;
21575 parser
->implicit_template_scope
= 0;
21582 /* Parse a parameter declaration.
21584 parameter-declaration:
21585 decl-specifier-seq ... [opt] declarator
21586 decl-specifier-seq declarator = assignment-expression
21587 decl-specifier-seq ... [opt] abstract-declarator [opt]
21588 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21590 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21591 declares a template parameter. (In that case, a non-nested `>'
21592 token encountered during the parsing of the assignment-expression
21593 is not interpreted as a greater-than operator.)
21595 Returns a representation of the parameter, or NULL if an error
21596 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21597 true iff the declarator is of the form "(p)". */
21599 static cp_parameter_declarator
*
21600 cp_parser_parameter_declaration (cp_parser
*parser
,
21601 bool template_parm_p
,
21602 bool *parenthesized_p
)
21604 int declares_class_or_enum
;
21605 cp_decl_specifier_seq decl_specifiers
;
21606 cp_declarator
*declarator
;
21607 tree default_argument
;
21608 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21609 const char *saved_message
;
21610 bool template_parameter_pack_p
= false;
21612 /* In a template parameter, `>' is not an operator.
21616 When parsing a default template-argument for a non-type
21617 template-parameter, the first non-nested `>' is taken as the end
21618 of the template parameter-list rather than a greater-than
21621 /* Type definitions may not appear in parameter types. */
21622 saved_message
= parser
->type_definition_forbidden_message
;
21623 parser
->type_definition_forbidden_message
21624 = G_("types may not be defined in parameter types");
21626 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21627 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21628 (current_template_parms
)) : 0);
21630 /* Parse the declaration-specifiers. */
21631 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21632 cp_parser_decl_specifier_seq (parser
,
21633 CP_PARSER_FLAGS_NONE
,
21635 &declares_class_or_enum
);
21637 /* Complain about missing 'typename' or other invalid type names. */
21638 if (!decl_specifiers
.any_type_specifiers_p
21639 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21640 decl_specifiers
.type
= error_mark_node
;
21642 /* If an error occurred, there's no reason to attempt to parse the
21643 rest of the declaration. */
21644 if (cp_parser_error_occurred (parser
))
21646 parser
->type_definition_forbidden_message
= saved_message
;
21650 /* Peek at the next token. */
21651 token
= cp_lexer_peek_token (parser
->lexer
);
21653 /* If the next token is a `)', `,', `=', `>', or `...', then there
21654 is no declarator. However, when variadic templates are enabled,
21655 there may be a declarator following `...'. */
21656 if (token
->type
== CPP_CLOSE_PAREN
21657 || token
->type
== CPP_COMMA
21658 || token
->type
== CPP_EQ
21659 || token
->type
== CPP_GREATER
)
21662 if (parenthesized_p
)
21663 *parenthesized_p
= false;
21665 /* Otherwise, there should be a declarator. */
21668 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21669 parser
->default_arg_ok_p
= false;
21671 /* After seeing a decl-specifier-seq, if the next token is not a
21672 "(", there is no possibility that the code is a valid
21673 expression. Therefore, if parsing tentatively, we commit at
21675 if (!parser
->in_template_argument_list_p
21676 /* In an expression context, having seen:
21680 we cannot be sure whether we are looking at a
21681 function-type (taking a "char" as a parameter) or a cast
21682 of some object of type "char" to "int". */
21683 && !parser
->in_type_id_in_expr_p
21684 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21685 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21686 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21687 cp_parser_commit_to_tentative_parse (parser
);
21688 /* Parse the declarator. */
21689 declarator_token_start
= token
;
21690 declarator
= cp_parser_declarator (parser
,
21691 CP_PARSER_DECLARATOR_EITHER
,
21692 /*ctor_dtor_or_conv_p=*/NULL
,
21694 /*member_p=*/false,
21695 /*friend_p=*/false);
21696 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21697 /* After the declarator, allow more attributes. */
21698 decl_specifiers
.attributes
21699 = attr_chainon (decl_specifiers
.attributes
,
21700 cp_parser_attributes_opt (parser
));
21702 /* If the declarator is a template parameter pack, remember that and
21703 clear the flag in the declarator itself so we don't get errors
21704 from grokdeclarator. */
21705 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21707 declarator
->parameter_pack_p
= false;
21708 template_parameter_pack_p
= true;
21712 /* If the next token is an ellipsis, and we have not seen a declarator
21713 name, and if either the type of the declarator contains parameter
21714 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21715 for, eg, abbreviated integral type names), then we actually have a
21716 parameter pack expansion expression. Otherwise, leave the ellipsis
21717 for a C-style variadic function. */
21718 token
= cp_lexer_peek_token (parser
->lexer
);
21720 /* If a function parameter pack was specified and an implicit template
21721 parameter was introduced during cp_parser_parameter_declaration,
21722 change any implicit parameters introduced into packs. */
21723 if (parser
->implicit_template_parms
21724 && ((token
->type
== CPP_ELLIPSIS
21725 && declarator_can_be_parameter_pack (declarator
))
21726 || (declarator
&& declarator
->parameter_pack_p
)))
21728 int latest_template_parm_idx
= TREE_VEC_LENGTH
21729 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21731 if (latest_template_parm_idx
!= template_parm_idx
)
21732 decl_specifiers
.type
= convert_generic_types_to_packs
21733 (decl_specifiers
.type
,
21734 template_parm_idx
, latest_template_parm_idx
);
21737 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21739 tree type
= decl_specifiers
.type
;
21741 if (type
&& DECL_P (type
))
21742 type
= TREE_TYPE (type
);
21745 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21746 && (template_parm_p
|| uses_parameter_packs (type
)))
21747 || (!type
&& template_parm_p
))
21748 && declarator_can_be_parameter_pack (declarator
))
21750 /* Consume the `...'. */
21751 cp_lexer_consume_token (parser
->lexer
);
21752 maybe_warn_variadic_templates ();
21754 /* Build a pack expansion type */
21755 if (template_parm_p
)
21756 template_parameter_pack_p
= true;
21757 else if (declarator
)
21758 declarator
->parameter_pack_p
= true;
21760 decl_specifiers
.type
= make_pack_expansion (type
);
21764 /* The restriction on defining new types applies only to the type
21765 of the parameter, not to the default argument. */
21766 parser
->type_definition_forbidden_message
= saved_message
;
21768 /* If the next token is `=', then process a default argument. */
21769 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21771 tree type
= decl_specifiers
.type
;
21772 token
= cp_lexer_peek_token (parser
->lexer
);
21773 /* If we are defining a class, then the tokens that make up the
21774 default argument must be saved and processed later. */
21775 if (!template_parm_p
&& at_class_scope_p ()
21776 && TYPE_BEING_DEFINED (current_class_type
)
21777 && !LAMBDA_TYPE_P (current_class_type
))
21778 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21780 // A constrained-type-specifier may declare a type template-parameter.
21781 else if (declares_constrained_type_template_parameter (type
))
21783 = cp_parser_default_type_template_argument (parser
);
21785 // A constrained-type-specifier may declare a template-template-parameter.
21786 else if (declares_constrained_template_template_parameter (type
))
21788 = cp_parser_default_template_template_argument (parser
);
21790 /* Outside of a class definition, we can just parse the
21791 assignment-expression. */
21794 = cp_parser_default_argument (parser
, template_parm_p
);
21796 if (!parser
->default_arg_ok_p
)
21798 permerror (token
->location
,
21799 "default arguments are only "
21800 "permitted for function parameters");
21802 else if ((declarator
&& declarator
->parameter_pack_p
)
21803 || template_parameter_pack_p
21804 || (decl_specifiers
.type
21805 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21807 /* Find the name of the parameter pack. */
21808 cp_declarator
*id_declarator
= declarator
;
21809 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21810 id_declarator
= id_declarator
->declarator
;
21812 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21813 error_at (declarator_token_start
->location
,
21815 ? G_("template parameter pack %qD "
21816 "cannot have a default argument")
21817 : G_("parameter pack %qD cannot have "
21818 "a default argument"),
21819 id_declarator
->u
.id
.unqualified_name
);
21821 error_at (declarator_token_start
->location
,
21823 ? G_("template parameter pack cannot have "
21824 "a default argument")
21825 : G_("parameter pack cannot have a "
21826 "default argument"));
21828 default_argument
= NULL_TREE
;
21832 default_argument
= NULL_TREE
;
21834 /* Generate a location for the parameter, ranging from the start of the
21835 initial token to the end of the final token (using input_location for
21836 the latter, set up by cp_lexer_set_source_position_from_token when
21839 If we have a identifier, then use it for the caret location, e.g.
21841 extern int callee (int one, int (*two)(int, int), float three);
21842 ~~~~~~^~~~~~~~~~~~~~
21844 otherwise, reuse the start location for the caret location e.g.:
21846 extern int callee (int one, int (*)(int, int), float three);
21850 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21851 ? declarator
->id_loc
21852 : decl_spec_token_start
->location
);
21853 location_t param_loc
= make_location (caret_loc
,
21854 decl_spec_token_start
->location
,
21857 return make_parameter_declarator (&decl_specifiers
,
21861 template_parameter_pack_p
);
21864 /* Parse a default argument and return it.
21866 TEMPLATE_PARM_P is true if this is a default argument for a
21867 non-type template parameter. */
21869 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21871 tree default_argument
= NULL_TREE
;
21872 bool saved_greater_than_is_operator_p
;
21873 bool saved_local_variables_forbidden_p
;
21874 bool non_constant_p
, is_direct_init
;
21876 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21878 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21879 parser
->greater_than_is_operator_p
= !template_parm_p
;
21880 /* Local variable names (and the `this' keyword) may not
21881 appear in a default argument. */
21882 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21883 parser
->local_variables_forbidden_p
= true;
21884 /* Parse the assignment-expression. */
21885 if (template_parm_p
)
21886 push_deferring_access_checks (dk_no_deferred
);
21887 tree saved_class_ptr
= NULL_TREE
;
21888 tree saved_class_ref
= NULL_TREE
;
21889 /* The "this" pointer is not valid in a default argument. */
21892 saved_class_ptr
= current_class_ptr
;
21893 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21894 saved_class_ref
= current_class_ref
;
21895 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21898 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21899 /* Restore the "this" pointer. */
21902 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21903 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21905 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21906 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21907 if (template_parm_p
)
21908 pop_deferring_access_checks ();
21909 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21910 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21912 return default_argument
;
21915 /* Parse a function-body.
21918 compound_statement */
21921 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21923 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21924 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21928 /* Parse a ctor-initializer-opt followed by a function-body. Return
21929 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21930 is true we are parsing a function-try-block. */
21933 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21934 bool in_function_try_block
)
21937 const bool check_body_p
=
21938 DECL_CONSTRUCTOR_P (current_function_decl
)
21939 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21942 /* Begin the function body. */
21943 body
= begin_function_body ();
21944 /* Parse the optional ctor-initializer. */
21945 cp_parser_ctor_initializer_opt (parser
);
21947 /* If we're parsing a constexpr constructor definition, we need
21948 to check that the constructor body is indeed empty. However,
21949 before we get to cp_parser_function_body lot of junk has been
21950 generated, so we can't just check that we have an empty block.
21951 Rather we take a snapshot of the outermost block, and check whether
21952 cp_parser_function_body changed its state. */
21955 list
= cur_stmt_list
;
21956 if (STATEMENT_LIST_TAIL (list
))
21957 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21959 /* Parse the function-body. */
21960 cp_parser_function_body (parser
, in_function_try_block
);
21962 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21963 /* Finish the function body. */
21964 finish_function_body (body
);
21967 /* Parse an initializer.
21970 = initializer-clause
21971 ( expression-list )
21973 Returns an expression representing the initializer. If no
21974 initializer is present, NULL_TREE is returned.
21976 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21977 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21978 set to TRUE if there is no initializer present. If there is an
21979 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21980 is set to true; otherwise it is set to false. */
21983 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21984 bool* non_constant_p
, bool subexpression_p
)
21989 /* Peek at the next token. */
21990 token
= cp_lexer_peek_token (parser
->lexer
);
21992 /* Let our caller know whether or not this initializer was
21994 *is_direct_init
= (token
->type
!= CPP_EQ
);
21995 /* Assume that the initializer is constant. */
21996 *non_constant_p
= false;
21998 if (token
->type
== CPP_EQ
)
22000 /* Consume the `='. */
22001 cp_lexer_consume_token (parser
->lexer
);
22002 /* Parse the initializer-clause. */
22003 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22005 else if (token
->type
== CPP_OPEN_PAREN
)
22007 vec
<tree
, va_gc
> *vec
;
22008 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22010 /*allow_expansion_p=*/true,
22013 return error_mark_node
;
22014 init
= build_tree_list_vec (vec
);
22015 release_tree_vector (vec
);
22017 else if (token
->type
== CPP_OPEN_BRACE
)
22019 cp_lexer_set_source_position (parser
->lexer
);
22020 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22021 init
= cp_parser_braced_list (parser
, non_constant_p
);
22022 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22026 /* Anything else is an error. */
22027 cp_parser_error (parser
, "expected initializer");
22028 init
= error_mark_node
;
22031 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22032 init
= error_mark_node
;
22037 /* Parse an initializer-clause.
22039 initializer-clause:
22040 assignment-expression
22043 Returns an expression representing the initializer.
22045 If the `assignment-expression' production is used the value
22046 returned is simply a representation for the expression.
22048 Otherwise, calls cp_parser_braced_list. */
22051 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22053 cp_expr initializer
;
22055 /* Assume the expression is constant. */
22056 *non_constant_p
= false;
22058 /* If it is not a `{', then we are looking at an
22059 assignment-expression. */
22060 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22063 = cp_parser_constant_expression (parser
,
22064 /*allow_non_constant_p=*/true,
22068 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22070 return initializer
;
22073 /* Parse a brace-enclosed initializer list.
22076 { initializer-list , [opt] }
22077 { designated-initializer-list , [opt] }
22080 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22081 the elements of the initializer-list (or NULL, if the last
22082 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22083 NULL_TREE. There is no way to detect whether or not the optional
22084 trailing `,' was provided. NON_CONSTANT_P is as for
22085 cp_parser_initializer. */
22088 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22091 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22093 /* Consume the `{' token. */
22094 matching_braces braces
;
22095 braces
.require_open (parser
);
22096 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22097 initializer
= make_node (CONSTRUCTOR
);
22098 /* If it's not a `}', then there is a non-trivial initializer. */
22099 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22101 /* Parse the initializer list. */
22102 CONSTRUCTOR_ELTS (initializer
)
22103 = cp_parser_initializer_list (parser
, non_constant_p
);
22104 /* A trailing `,' token is allowed. */
22105 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22106 cp_lexer_consume_token (parser
->lexer
);
22109 *non_constant_p
= false;
22110 /* Now, there should be a trailing `}'. */
22111 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22112 braces
.require_close (parser
);
22113 TREE_TYPE (initializer
) = init_list_type_node
;
22115 cp_expr
result (initializer
);
22116 /* Build a location of the form:
22119 with caret==start at the open brace, finish at the close brace. */
22120 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22121 result
.set_location (combined_loc
);
22125 /* Consume tokens up to, and including, the next non-nested closing `]'.
22126 Returns true iff we found a closing `]'. */
22129 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22131 unsigned square_depth
= 0;
22135 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22137 switch (token
->type
)
22140 case CPP_PRAGMA_EOL
:
22141 /* If we've run out of tokens, then there is no closing `]'. */
22144 case CPP_OPEN_SQUARE
:
22148 case CPP_CLOSE_SQUARE
:
22149 if (!square_depth
--)
22151 cp_lexer_consume_token (parser
->lexer
);
22160 /* Consume the token. */
22161 cp_lexer_consume_token (parser
->lexer
);
22165 /* Return true if we are looking at an array-designator, false otherwise. */
22168 cp_parser_array_designator_p (cp_parser
*parser
)
22170 /* Consume the `['. */
22171 cp_lexer_consume_token (parser
->lexer
);
22173 cp_lexer_save_tokens (parser
->lexer
);
22175 /* Skip tokens until the next token is a closing square bracket.
22176 If we find the closing `]', and the next token is a `=', then
22177 we are looking at an array designator. */
22178 bool array_designator_p
22179 = (cp_parser_skip_to_closing_square_bracket (parser
)
22180 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22182 /* Roll back the tokens we skipped. */
22183 cp_lexer_rollback_tokens (parser
->lexer
);
22185 return array_designator_p
;
22188 /* Parse an initializer-list.
22191 initializer-clause ... [opt]
22192 initializer-list , initializer-clause ... [opt]
22196 designated-initializer-list:
22197 designated-initializer-clause
22198 designated-initializer-list , designated-initializer-clause
22200 designated-initializer-clause:
22201 designator brace-or-equal-initializer
22209 designation initializer-clause ...[opt]
22210 initializer-list , designation initializer-clause ...[opt]
22215 [ constant-expression ] =
22217 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22218 for the initializer. If the INDEX of the elt is non-NULL, it is the
22219 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22220 as for cp_parser_initializer. */
22222 static vec
<constructor_elt
, va_gc
> *
22223 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22225 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22226 bool first_p
= true;
22227 tree first_designator
= NULL_TREE
;
22229 /* Assume all of the expressions are constant. */
22230 *non_constant_p
= false;
22232 /* Parse the rest of the list. */
22238 bool clause_non_constant_p
;
22239 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22241 /* Handle the C++2A syntax, '. id ='. */
22242 if ((cxx_dialect
>= cxx2a
22243 || cp_parser_allow_gnu_extensions_p (parser
))
22244 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22245 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22246 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22247 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22248 == CPP_OPEN_BRACE
)))
22250 if (cxx_dialect
< cxx2a
)
22251 pedwarn (loc
, OPT_Wpedantic
,
22252 "C++ designated initializers only available with "
22253 "-std=c++2a or -std=gnu++2a");
22254 /* Consume the `.'. */
22255 cp_lexer_consume_token (parser
->lexer
);
22256 /* Consume the identifier. */
22257 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22259 /* Consume the `='. */
22260 cp_lexer_consume_token (parser
->lexer
);
22262 /* Also, if the next token is an identifier and the following one is a
22263 colon, we are looking at the GNU designated-initializer
22265 else if (cp_parser_allow_gnu_extensions_p (parser
)
22266 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22267 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22270 /* Warn the user that they are using an extension. */
22271 pedwarn (loc
, OPT_Wpedantic
,
22272 "ISO C++ does not allow GNU designated initializers");
22273 /* Consume the identifier. */
22274 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22275 /* Consume the `:'. */
22276 cp_lexer_consume_token (parser
->lexer
);
22278 /* Also handle C99 array designators, '[ const ] ='. */
22279 else if (cp_parser_allow_gnu_extensions_p (parser
)
22280 && !c_dialect_objc ()
22281 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22283 /* In C++11, [ could start a lambda-introducer. */
22284 bool non_const
= false;
22286 cp_parser_parse_tentatively (parser
);
22288 if (!cp_parser_array_designator_p (parser
))
22290 cp_parser_simulate_error (parser
);
22291 designator
= NULL_TREE
;
22295 designator
= cp_parser_constant_expression (parser
, true,
22297 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22298 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22301 if (!cp_parser_parse_definitely (parser
))
22302 designator
= NULL_TREE
;
22304 && (!require_potential_rvalue_constant_expression
22306 designator
= NULL_TREE
;
22308 /* Warn the user that they are using an extension. */
22309 pedwarn (loc
, OPT_Wpedantic
,
22310 "ISO C++ does not allow C99 designated initializers");
22313 designator
= NULL_TREE
;
22317 first_designator
= designator
;
22320 else if (cxx_dialect
>= cxx2a
22321 && first_designator
!= error_mark_node
22322 && (!first_designator
!= !designator
))
22324 error_at (loc
, "either all initializer clauses should be designated "
22325 "or none of them should be");
22326 first_designator
= error_mark_node
;
22328 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22329 first_designator
= designator
;
22331 /* Parse the initializer. */
22332 initializer
= cp_parser_initializer_clause (parser
,
22333 &clause_non_constant_p
);
22334 /* If any clause is non-constant, so is the entire initializer. */
22335 if (clause_non_constant_p
)
22336 *non_constant_p
= true;
22338 /* If we have an ellipsis, this is an initializer pack
22340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22342 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22344 /* Consume the `...'. */
22345 cp_lexer_consume_token (parser
->lexer
);
22347 if (designator
&& cxx_dialect
>= cxx2a
)
22349 "%<...%> not allowed in designated initializer list");
22351 /* Turn the initializer into an initializer expansion. */
22352 initializer
= make_pack_expansion (initializer
);
22355 /* Add it to the vector. */
22356 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22358 /* If the next token is not a comma, we have reached the end of
22360 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22363 /* Peek at the next token. */
22364 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22365 /* If the next token is a `}', then we're still done. An
22366 initializer-clause can have a trailing `,' after the
22367 initializer-list and before the closing `}'. */
22368 if (token
->type
== CPP_CLOSE_BRACE
)
22371 /* Consume the `,' token. */
22372 cp_lexer_consume_token (parser
->lexer
);
22375 /* The same identifier shall not appear in multiple designators
22376 of a designated-initializer-list. */
22377 if (first_designator
)
22380 tree designator
, val
;
22381 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22382 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22384 if (IDENTIFIER_MARKED (designator
))
22386 error_at (cp_expr_loc_or_loc (val
, input_location
),
22387 "%<.%s%> designator used multiple times in "
22388 "the same initializer list",
22389 IDENTIFIER_POINTER (designator
));
22390 (*v
)[i
].index
= error_mark_node
;
22393 IDENTIFIER_MARKED (designator
) = 1;
22395 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22396 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22397 IDENTIFIER_MARKED (designator
) = 0;
22403 /* Classes [gram.class] */
22405 /* Parse a class-name.
22411 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22412 to indicate that names looked up in dependent types should be
22413 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22414 keyword has been used to indicate that the name that appears next
22415 is a template. TAG_TYPE indicates the explicit tag given before
22416 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22417 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22418 is the class being defined in a class-head. If ENUM_OK is TRUE,
22419 enum-names are also accepted.
22421 Returns the TYPE_DECL representing the class. */
22424 cp_parser_class_name (cp_parser
*parser
,
22425 bool typename_keyword_p
,
22426 bool template_keyword_p
,
22427 enum tag_types tag_type
,
22428 bool check_dependency_p
,
22430 bool is_declaration
,
22437 tree identifier
= NULL_TREE
;
22439 /* All class-names start with an identifier. */
22440 token
= cp_lexer_peek_token (parser
->lexer
);
22441 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22443 cp_parser_error (parser
, "expected class-name");
22444 return error_mark_node
;
22447 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22448 to a template-id, so we save it here. */
22449 scope
= parser
->scope
;
22450 if (scope
== error_mark_node
)
22451 return error_mark_node
;
22453 /* Any name names a type if we're following the `typename' keyword
22454 in a qualified name where the enclosing scope is type-dependent. */
22455 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22456 && dependent_type_p (scope
));
22457 /* Handle the common case (an identifier, but not a template-id)
22459 if (token
->type
== CPP_NAME
22460 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22462 cp_token
*identifier_token
;
22465 /* Look for the identifier. */
22466 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22467 ambiguous_p
= identifier_token
->error_reported
;
22468 identifier
= cp_parser_identifier (parser
);
22469 /* If the next token isn't an identifier, we are certainly not
22470 looking at a class-name. */
22471 if (identifier
== error_mark_node
)
22472 decl
= error_mark_node
;
22473 /* If we know this is a type-name, there's no need to look it
22475 else if (typename_p
)
22479 tree ambiguous_decls
;
22480 /* If we already know that this lookup is ambiguous, then
22481 we've already issued an error message; there's no reason
22485 cp_parser_simulate_error (parser
);
22486 return error_mark_node
;
22488 /* If the next token is a `::', then the name must be a type
22491 [basic.lookup.qual]
22493 During the lookup for a name preceding the :: scope
22494 resolution operator, object, function, and enumerator
22495 names are ignored. */
22496 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22497 tag_type
= scope_type
;
22498 /* Look up the name. */
22499 decl
= cp_parser_lookup_name (parser
, identifier
,
22501 /*is_template=*/false,
22502 /*is_namespace=*/false,
22503 check_dependency_p
,
22505 identifier_token
->location
);
22506 if (ambiguous_decls
)
22508 if (cp_parser_parsing_tentatively (parser
))
22509 cp_parser_simulate_error (parser
);
22510 return error_mark_node
;
22516 /* Try a template-id. */
22517 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22518 check_dependency_p
,
22521 if (decl
== error_mark_node
)
22522 return error_mark_node
;
22525 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22527 /* If this is a typename, create a TYPENAME_TYPE. */
22528 if (typename_p
&& decl
!= error_mark_node
)
22530 decl
= make_typename_type (scope
, decl
, typename_type
,
22531 /*complain=*/tf_error
);
22532 if (decl
!= error_mark_node
)
22533 decl
= TYPE_NAME (decl
);
22536 decl
= strip_using_decl (decl
);
22538 /* Check to see that it is really the name of a class. */
22539 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22540 && identifier_p (TREE_OPERAND (decl
, 0))
22541 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22542 /* Situations like this:
22544 template <typename T> struct A {
22545 typename T::template X<int>::I i;
22548 are problematic. Is `T::template X<int>' a class-name? The
22549 standard does not seem to be definitive, but there is no other
22550 valid interpretation of the following `::'. Therefore, those
22551 names are considered class-names. */
22553 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22554 if (decl
!= error_mark_node
)
22555 decl
= TYPE_NAME (decl
);
22557 else if (TREE_CODE (decl
) != TYPE_DECL
22558 || TREE_TYPE (decl
) == error_mark_node
22559 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22560 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22561 /* In Objective-C 2.0, a classname followed by '.' starts a
22562 dot-syntax expression, and it's not a type-name. */
22563 || (c_dialect_objc ()
22564 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22565 && objc_is_class_name (decl
)))
22566 decl
= error_mark_node
;
22568 if (decl
== error_mark_node
)
22569 cp_parser_error (parser
, "expected class-name");
22570 else if (identifier
&& !parser
->scope
)
22571 maybe_note_name_used_in_class (identifier
, decl
);
22576 /* Parse a class-specifier.
22579 class-head { member-specification [opt] }
22581 Returns the TREE_TYPE representing the class. */
22584 cp_parser_class_specifier_1 (cp_parser
* parser
)
22587 tree attributes
= NULL_TREE
;
22588 bool nested_name_specifier_p
;
22589 unsigned saved_num_template_parameter_lists
;
22590 bool saved_in_function_body
;
22591 unsigned char in_statement
;
22592 bool in_switch_statement_p
;
22593 bool saved_in_unbraced_linkage_specification_p
;
22594 tree old_scope
= NULL_TREE
;
22595 tree scope
= NULL_TREE
;
22596 cp_token
*closing_brace
;
22598 push_deferring_access_checks (dk_no_deferred
);
22600 /* Parse the class-head. */
22601 type
= cp_parser_class_head (parser
,
22602 &nested_name_specifier_p
);
22603 /* If the class-head was a semantic disaster, skip the entire body
22607 cp_parser_skip_to_end_of_block_or_statement (parser
);
22608 pop_deferring_access_checks ();
22609 return error_mark_node
;
22612 /* Look for the `{'. */
22613 matching_braces braces
;
22614 if (!braces
.require_open (parser
))
22616 pop_deferring_access_checks ();
22617 return error_mark_node
;
22620 cp_ensure_no_omp_declare_simd (parser
);
22621 cp_ensure_no_oacc_routine (parser
);
22623 /* Issue an error message if type-definitions are forbidden here. */
22624 cp_parser_check_type_definition (parser
);
22625 /* Remember that we are defining one more class. */
22626 ++parser
->num_classes_being_defined
;
22627 /* Inside the class, surrounding template-parameter-lists do not
22629 saved_num_template_parameter_lists
22630 = parser
->num_template_parameter_lists
;
22631 parser
->num_template_parameter_lists
= 0;
22632 /* We are not in a function body. */
22633 saved_in_function_body
= parser
->in_function_body
;
22634 parser
->in_function_body
= false;
22635 /* Or in a loop. */
22636 in_statement
= parser
->in_statement
;
22637 parser
->in_statement
= 0;
22638 /* Or in a switch. */
22639 in_switch_statement_p
= parser
->in_switch_statement_p
;
22640 parser
->in_switch_statement_p
= false;
22641 /* We are not immediately inside an extern "lang" block. */
22642 saved_in_unbraced_linkage_specification_p
22643 = parser
->in_unbraced_linkage_specification_p
;
22644 parser
->in_unbraced_linkage_specification_p
= false;
22646 // Associate constraints with the type.
22648 type
= associate_classtype_constraints (type
);
22650 /* Start the class. */
22651 if (nested_name_specifier_p
)
22653 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22654 old_scope
= push_inner_scope (scope
);
22656 type
= begin_class_definition (type
);
22658 if (type
== error_mark_node
)
22659 /* If the type is erroneous, skip the entire body of the class. */
22660 cp_parser_skip_to_closing_brace (parser
);
22662 /* Parse the member-specification. */
22663 cp_parser_member_specification_opt (parser
);
22665 /* Look for the trailing `}'. */
22666 closing_brace
= braces
.require_close (parser
);
22667 /* Look for trailing attributes to apply to this class. */
22668 if (cp_parser_allow_gnu_extensions_p (parser
))
22669 attributes
= cp_parser_gnu_attributes_opt (parser
);
22670 if (type
!= error_mark_node
)
22671 type
= finish_struct (type
, attributes
);
22672 if (nested_name_specifier_p
)
22673 pop_inner_scope (old_scope
, scope
);
22675 /* We've finished a type definition. Check for the common syntax
22676 error of forgetting a semicolon after the definition. We need to
22677 be careful, as we can't just check for not-a-semicolon and be done
22678 with it; the user might have typed:
22680 class X { } c = ...;
22681 class X { } *p = ...;
22683 and so forth. Instead, enumerate all the possible tokens that
22684 might follow this production; if we don't see one of them, then
22685 complain and silently insert the semicolon. */
22687 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22688 bool want_semicolon
= true;
22690 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22691 /* Don't try to parse c++11 attributes here. As per the
22692 grammar, that should be a task for
22693 cp_parser_decl_specifier_seq. */
22694 want_semicolon
= false;
22696 switch (token
->type
)
22699 case CPP_SEMICOLON
:
22702 case CPP_OPEN_PAREN
:
22703 case CPP_CLOSE_PAREN
:
22705 want_semicolon
= false;
22708 /* While it's legal for type qualifiers and storage class
22709 specifiers to follow type definitions in the grammar, only
22710 compiler testsuites contain code like that. Assume that if
22711 we see such code, then what we're really seeing is a case
22715 const <type> var = ...;
22720 static <type> func (...) ...
22722 i.e. the qualifier or specifier applies to the next
22723 declaration. To do so, however, we need to look ahead one
22724 more token to see if *that* token is a type specifier.
22726 This code could be improved to handle:
22729 static const <type> var = ...; */
22731 if (keyword_is_decl_specifier (token
->keyword
))
22733 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22735 /* Handling user-defined types here would be nice, but very
22738 = (lookahead
->type
== CPP_KEYWORD
22739 && keyword_begins_type_specifier (lookahead
->keyword
));
22746 /* If we don't have a type, then something is very wrong and we
22747 shouldn't try to do anything clever. Likewise for not seeing the
22749 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22751 /* Locate the closing brace. */
22752 cp_token_position prev
22753 = cp_lexer_previous_token_position (parser
->lexer
);
22754 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22755 location_t loc
= prev_token
->location
;
22757 /* We want to suggest insertion of a ';' immediately *after* the
22758 closing brace, so, if we can, offset the location by 1 column. */
22759 location_t next_loc
= loc
;
22760 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22761 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22763 rich_location
richloc (line_table
, next_loc
);
22765 /* If we successfully offset the location, suggest the fix-it. */
22766 if (next_loc
!= loc
)
22767 richloc
.add_fixit_insert_before (next_loc
, ";");
22769 if (CLASSTYPE_DECLARED_CLASS (type
))
22770 error_at (&richloc
,
22771 "expected %<;%> after class definition");
22772 else if (TREE_CODE (type
) == RECORD_TYPE
)
22773 error_at (&richloc
,
22774 "expected %<;%> after struct definition");
22775 else if (TREE_CODE (type
) == UNION_TYPE
)
22776 error_at (&richloc
,
22777 "expected %<;%> after union definition");
22779 gcc_unreachable ();
22781 /* Unget one token and smash it to look as though we encountered
22782 a semicolon in the input stream. */
22783 cp_lexer_set_token_position (parser
->lexer
, prev
);
22784 token
= cp_lexer_peek_token (parser
->lexer
);
22785 token
->type
= CPP_SEMICOLON
;
22786 token
->keyword
= RID_MAX
;
22790 /* If this class is not itself within the scope of another class,
22791 then we need to parse the bodies of all of the queued function
22792 definitions. Note that the queued functions defined in a class
22793 are not always processed immediately following the
22794 class-specifier for that class. Consider:
22797 struct B { void f() { sizeof (A); } };
22800 If `f' were processed before the processing of `A' were
22801 completed, there would be no way to compute the size of `A'.
22802 Note that the nesting we are interested in here is lexical --
22803 not the semantic nesting given by TYPE_CONTEXT. In particular,
22806 struct A { struct B; };
22807 struct A::B { void f() { } };
22809 there is no need to delay the parsing of `A::B::f'. */
22810 if (--parser
->num_classes_being_defined
== 0)
22813 tree class_type
= NULL_TREE
;
22814 tree pushed_scope
= NULL_TREE
;
22816 cp_default_arg_entry
*e
;
22817 tree save_ccp
, save_ccr
;
22819 if (any_erroneous_template_args_p (type
))
22821 /* Skip default arguments, NSDMIs, etc, in order to improve
22822 error recovery (c++/71169, c++/71832). */
22823 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22824 vec_safe_truncate (unparsed_nsdmis
, 0);
22825 vec_safe_truncate (unparsed_classes
, 0);
22826 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22829 /* In a first pass, parse default arguments to the functions.
22830 Then, in a second pass, parse the bodies of the functions.
22831 This two-phased approach handles cases like:
22839 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22842 /* If there are default arguments that have not yet been processed,
22843 take care of them now. */
22844 if (class_type
!= e
->class_type
)
22847 pop_scope (pushed_scope
);
22848 class_type
= e
->class_type
;
22849 pushed_scope
= push_scope (class_type
);
22851 /* Make sure that any template parameters are in scope. */
22852 maybe_begin_member_template_processing (decl
);
22853 /* Parse the default argument expressions. */
22854 cp_parser_late_parsing_default_args (parser
, decl
);
22855 /* Remove any template parameters from the symbol table. */
22856 maybe_end_member_template_processing ();
22858 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22859 /* Now parse any NSDMIs. */
22860 save_ccp
= current_class_ptr
;
22861 save_ccr
= current_class_ref
;
22862 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22864 if (class_type
!= DECL_CONTEXT (decl
))
22867 pop_scope (pushed_scope
);
22868 class_type
= DECL_CONTEXT (decl
);
22869 pushed_scope
= push_scope (class_type
);
22871 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22872 cp_parser_late_parsing_nsdmi (parser
, decl
);
22874 vec_safe_truncate (unparsed_nsdmis
, 0);
22875 current_class_ptr
= save_ccp
;
22876 current_class_ref
= save_ccr
;
22878 pop_scope (pushed_scope
);
22880 /* Now do some post-NSDMI bookkeeping. */
22881 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22882 after_nsdmi_defaulted_late_checks (class_type
);
22883 vec_safe_truncate (unparsed_classes
, 0);
22884 after_nsdmi_defaulted_late_checks (type
);
22886 /* Now parse the body of the functions. */
22889 /* OpenMP UDRs need to be parsed before all other functions. */
22890 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22891 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22892 cp_parser_late_parsing_for_member (parser
, decl
);
22893 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22894 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22895 cp_parser_late_parsing_for_member (parser
, decl
);
22898 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22899 cp_parser_late_parsing_for_member (parser
, decl
);
22900 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22903 vec_safe_push (unparsed_classes
, type
);
22905 /* Put back any saved access checks. */
22906 pop_deferring_access_checks ();
22908 /* Restore saved state. */
22909 parser
->in_switch_statement_p
= in_switch_statement_p
;
22910 parser
->in_statement
= in_statement
;
22911 parser
->in_function_body
= saved_in_function_body
;
22912 parser
->num_template_parameter_lists
22913 = saved_num_template_parameter_lists
;
22914 parser
->in_unbraced_linkage_specification_p
22915 = saved_in_unbraced_linkage_specification_p
;
22921 cp_parser_class_specifier (cp_parser
* parser
)
22924 timevar_push (TV_PARSE_STRUCT
);
22925 ret
= cp_parser_class_specifier_1 (parser
);
22926 timevar_pop (TV_PARSE_STRUCT
);
22930 /* Parse a class-head.
22933 class-key identifier [opt] base-clause [opt]
22934 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22935 class-key nested-name-specifier [opt] template-id
22938 class-virt-specifier:
22942 class-key attributes identifier [opt] base-clause [opt]
22943 class-key attributes nested-name-specifier identifier base-clause [opt]
22944 class-key attributes nested-name-specifier [opt] template-id
22947 Upon return BASES is initialized to the list of base classes (or
22948 NULL, if there are none) in the same form returned by
22949 cp_parser_base_clause.
22951 Returns the TYPE of the indicated class. Sets
22952 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22953 involving a nested-name-specifier was used, and FALSE otherwise.
22955 Returns error_mark_node if this is not a class-head.
22957 Returns NULL_TREE if the class-head is syntactically valid, but
22958 semantically invalid in a way that means we should skip the entire
22959 body of the class. */
22962 cp_parser_class_head (cp_parser
* parser
,
22963 bool* nested_name_specifier_p
)
22965 tree nested_name_specifier
;
22966 enum tag_types class_key
;
22967 tree id
= NULL_TREE
;
22968 tree type
= NULL_TREE
;
22971 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22972 bool template_id_p
= false;
22973 bool qualified_p
= false;
22974 bool invalid_nested_name_p
= false;
22975 bool invalid_explicit_specialization_p
= false;
22976 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22977 tree pushed_scope
= NULL_TREE
;
22978 unsigned num_templates
;
22979 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22980 /* Assume no nested-name-specifier will be present. */
22981 *nested_name_specifier_p
= false;
22982 /* Assume no template parameter lists will be used in defining the
22985 parser
->colon_corrects_to_scope_p
= false;
22987 /* Look for the class-key. */
22988 class_key
= cp_parser_class_key (parser
);
22989 if (class_key
== none_type
)
22990 return error_mark_node
;
22992 location_t class_head_start_location
= input_location
;
22994 /* Parse the attributes. */
22995 attributes
= cp_parser_attributes_opt (parser
);
22997 /* If the next token is `::', that is invalid -- but sometimes
22998 people do try to write:
23002 Handle this gracefully by accepting the extra qualifier, and then
23003 issuing an error about it later if this really is a
23004 class-head. If it turns out just to be an elaborated type
23005 specifier, remain silent. */
23006 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23007 qualified_p
= true;
23009 push_deferring_access_checks (dk_no_check
);
23011 /* Determine the name of the class. Begin by looking for an
23012 optional nested-name-specifier. */
23013 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23014 nested_name_specifier
23015 = cp_parser_nested_name_specifier_opt (parser
,
23016 /*typename_keyword_p=*/false,
23017 /*check_dependency_p=*/false,
23019 /*is_declaration=*/false);
23020 /* If there was a nested-name-specifier, then there *must* be an
23023 cp_token
*bad_template_keyword
= NULL
;
23025 if (nested_name_specifier
)
23027 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23028 /* Although the grammar says `identifier', it really means
23029 `class-name' or `template-name'. You are only allowed to
23030 define a class that has already been declared with this
23033 The proposed resolution for Core Issue 180 says that wherever
23034 you see `class T::X' you should treat `X' as a type-name.
23036 It is OK to define an inaccessible class; for example:
23038 class A { class B; };
23041 We do not know if we will see a class-name, or a
23042 template-name. We look for a class-name first, in case the
23043 class-name is a template-id; if we looked for the
23044 template-name first we would stop after the template-name. */
23045 cp_parser_parse_tentatively (parser
);
23046 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23047 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23048 type
= cp_parser_class_name (parser
,
23049 /*typename_keyword_p=*/false,
23050 /*template_keyword_p=*/false,
23052 /*check_dependency_p=*/false,
23053 /*class_head_p=*/true,
23054 /*is_declaration=*/false);
23055 /* If that didn't work, ignore the nested-name-specifier. */
23056 if (!cp_parser_parse_definitely (parser
))
23058 invalid_nested_name_p
= true;
23059 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23060 id
= cp_parser_identifier (parser
);
23061 if (id
== error_mark_node
)
23064 /* If we could not find a corresponding TYPE, treat this
23065 declaration like an unqualified declaration. */
23066 if (type
== error_mark_node
)
23067 nested_name_specifier
= NULL_TREE
;
23068 /* Otherwise, count the number of templates used in TYPE and its
23069 containing scopes. */
23071 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23073 /* Otherwise, the identifier is optional. */
23076 /* We don't know whether what comes next is a template-id,
23077 an identifier, or nothing at all. */
23078 cp_parser_parse_tentatively (parser
);
23079 /* Check for a template-id. */
23080 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23081 id
= cp_parser_template_id (parser
,
23082 /*template_keyword_p=*/false,
23083 /*check_dependency_p=*/true,
23085 /*is_declaration=*/true);
23086 /* If that didn't work, it could still be an identifier. */
23087 if (!cp_parser_parse_definitely (parser
))
23089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23091 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23092 id
= cp_parser_identifier (parser
);
23099 template_id_p
= true;
23104 pop_deferring_access_checks ();
23108 cp_parser_check_for_invalid_template_id (parser
, id
,
23110 type_start_token
->location
);
23112 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23114 /* If it's not a `:' or a `{' then we can't really be looking at a
23115 class-head, since a class-head only appears as part of a
23116 class-specifier. We have to detect this situation before calling
23117 xref_tag, since that has irreversible side-effects. */
23118 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23120 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23121 type
= error_mark_node
;
23125 /* At this point, we're going ahead with the class-specifier, even
23126 if some other problem occurs. */
23127 cp_parser_commit_to_tentative_parse (parser
);
23128 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23130 cp_parser_error (parser
,
23131 "cannot specify %<override%> for a class");
23132 type
= error_mark_node
;
23135 /* Issue the error about the overly-qualified name now. */
23138 cp_parser_error (parser
,
23139 "global qualification of class name is invalid");
23140 type
= error_mark_node
;
23143 else if (invalid_nested_name_p
)
23145 cp_parser_error (parser
,
23146 "qualified name does not name a class");
23147 type
= error_mark_node
;
23150 else if (nested_name_specifier
)
23154 if (bad_template_keyword
)
23155 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23156 keyword template shall not appear at the top level. */
23157 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23158 "keyword %<template%> not allowed in class-head-name");
23160 /* Reject typedef-names in class heads. */
23161 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23163 error_at (type_start_token
->location
,
23164 "invalid class name in declaration of %qD",
23170 /* Figure out in what scope the declaration is being placed. */
23171 scope
= current_scope ();
23172 /* If that scope does not contain the scope in which the
23173 class was originally declared, the program is invalid. */
23174 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23176 if (at_namespace_scope_p ())
23177 error_at (type_start_token
->location
,
23178 "declaration of %qD in namespace %qD which does not "
23180 type
, scope
, nested_name_specifier
);
23182 error_at (type_start_token
->location
,
23183 "declaration of %qD in %qD which does not enclose %qD",
23184 type
, scope
, nested_name_specifier
);
23190 A declarator-id shall not be qualified except for the
23191 definition of a ... nested class outside of its class
23192 ... [or] the definition or explicit instantiation of a
23193 class member of a namespace outside of its namespace. */
23194 if (scope
== nested_name_specifier
)
23196 permerror (nested_name_specifier_token_start
->location
,
23197 "extra qualification not allowed");
23198 nested_name_specifier
= NULL_TREE
;
23202 /* An explicit-specialization must be preceded by "template <>". If
23203 it is not, try to recover gracefully. */
23204 if (at_namespace_scope_p ()
23205 && parser
->num_template_parameter_lists
== 0
23206 && !processing_template_parmlist
23209 /* Build a location of this form:
23210 struct typename <ARGS>
23211 ^~~~~~~~~~~~~~~~~~~~~~
23212 with caret==start at the start token, and
23213 finishing at the end of the type. */
23214 location_t reported_loc
23215 = make_location (class_head_start_location
,
23216 class_head_start_location
,
23217 get_finish (type_start_token
->location
));
23218 rich_location
richloc (line_table
, reported_loc
);
23219 richloc
.add_fixit_insert_before (class_head_start_location
,
23221 error_at (&richloc
,
23222 "an explicit specialization must be preceded by"
23223 " %<template <>%>");
23224 invalid_explicit_specialization_p
= true;
23225 /* Take the same action that would have been taken by
23226 cp_parser_explicit_specialization. */
23227 ++parser
->num_template_parameter_lists
;
23228 begin_specialization ();
23230 /* There must be no "return" statements between this point and the
23231 end of this function; set "type "to the correct return value and
23232 use "goto done;" to return. */
23233 /* Make sure that the right number of template parameters were
23235 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23237 type_start_token
->location
,
23238 /*declarator=*/NULL
))
23240 /* If something went wrong, there is no point in even trying to
23241 process the class-definition. */
23246 /* Look up the type. */
23249 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23250 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23251 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23253 error_at (type_start_token
->location
,
23254 "function template %qD redeclared as a class template", id
);
23255 type
= error_mark_node
;
23259 type
= TREE_TYPE (id
);
23260 type
= maybe_process_partial_specialization (type
);
23262 /* Check the scope while we still know whether or not we had a
23263 nested-name-specifier. */
23264 if (type
!= error_mark_node
)
23265 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23267 if (nested_name_specifier
)
23268 pushed_scope
= push_scope (nested_name_specifier
);
23270 else if (nested_name_specifier
)
23276 template <typename T> struct S { struct T };
23277 template <typename T> struct S<T>::T { };
23279 we will get a TYPENAME_TYPE when processing the definition of
23280 `S::T'. We need to resolve it to the actual type before we
23281 try to define it. */
23282 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23284 class_type
= resolve_typename_type (TREE_TYPE (type
),
23285 /*only_current_p=*/false);
23286 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23287 type
= TYPE_NAME (class_type
);
23290 cp_parser_error (parser
, "could not resolve typename type");
23291 type
= error_mark_node
;
23295 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23296 == error_mark_node
)
23302 class_type
= current_class_type
;
23303 /* Enter the scope indicated by the nested-name-specifier. */
23304 pushed_scope
= push_scope (nested_name_specifier
);
23305 /* Get the canonical version of this type. */
23306 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23307 /* Call push_template_decl if it seems like we should be defining a
23308 template either from the template headers or the type we're
23309 defining, so that we diagnose both extra and missing headers. */
23310 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23311 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23312 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23314 type
= push_template_decl (type
);
23315 if (type
== error_mark_node
)
23322 type
= TREE_TYPE (type
);
23323 *nested_name_specifier_p
= true;
23325 else /* The name is not a nested name. */
23327 /* If the class was unnamed, create a dummy name. */
23329 id
= make_anon_name ();
23330 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23331 ? ts_within_enclosing_non_class
23333 type
= xref_tag (class_key
, id
, tag_scope
,
23334 parser
->num_template_parameter_lists
);
23337 /* Indicate whether this class was declared as a `class' or as a
23339 if (TREE_CODE (type
) == RECORD_TYPE
)
23340 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23341 cp_parser_check_class_key (class_key
, type
);
23343 /* If this type was already complete, and we see another definition,
23344 that's an error. */
23345 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23347 error_at (type_start_token
->location
, "redefinition of %q#T",
23349 inform (location_of (type
), "previous definition of %q#T",
23354 else if (type
== error_mark_node
)
23359 /* Apply attributes now, before any use of the class as a template
23360 argument in its base list. */
23361 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23362 fixup_attribute_variants (type
);
23365 /* We will have entered the scope containing the class; the names of
23366 base classes should be looked up in that context. For example:
23368 struct A { struct B {}; struct C; };
23369 struct A::C : B {};
23373 /* Get the list of base-classes, if there is one. */
23374 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23376 /* PR59482: enter the class scope so that base-specifiers are looked
23380 bases
= cp_parser_base_clause (parser
);
23381 /* PR59482: get out of the previously pushed class scope so that the
23382 subsequent pops pop the right thing. */
23389 /* If we're really defining a class, process the base classes.
23390 If they're invalid, fail. */
23391 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23392 xref_basetypes (type
, bases
);
23395 /* Leave the scope given by the nested-name-specifier. We will
23396 enter the class scope itself while processing the members. */
23398 pop_scope (pushed_scope
);
23400 if (invalid_explicit_specialization_p
)
23402 end_specialization ();
23403 --parser
->num_template_parameter_lists
;
23407 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23408 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23409 CLASSTYPE_FINAL (type
) = 1;
23411 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23415 /* Parse a class-key.
23422 Returns the kind of class-key specified, or none_type to indicate
23425 static enum tag_types
23426 cp_parser_class_key (cp_parser
* parser
)
23429 enum tag_types tag_type
;
23431 /* Look for the class-key. */
23432 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23436 /* Check to see if the TOKEN is a class-key. */
23437 tag_type
= cp_parser_token_is_class_key (token
);
23439 cp_parser_error (parser
, "expected class-key");
23443 /* Parse a type-parameter-key.
23445 type-parameter-key:
23451 cp_parser_type_parameter_key (cp_parser
* parser
)
23453 /* Look for the type-parameter-key. */
23454 enum tag_types tag_type
= none_type
;
23455 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23456 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23458 cp_lexer_consume_token (parser
->lexer
);
23459 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23460 /* typename is not allowed in a template template parameter
23461 by the standard until C++17. */
23462 pedwarn (token
->location
, OPT_Wpedantic
,
23463 "ISO C++ forbids typename key in template template parameter;"
23464 " use -std=c++17 or -std=gnu++17");
23467 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23472 /* Parse an (optional) member-specification.
23474 member-specification:
23475 member-declaration member-specification [opt]
23476 access-specifier : member-specification [opt] */
23479 cp_parser_member_specification_opt (cp_parser
* parser
)
23486 /* Peek at the next token. */
23487 token
= cp_lexer_peek_token (parser
->lexer
);
23488 /* If it's a `}', or EOF then we've seen all the members. */
23489 if (token
->type
== CPP_CLOSE_BRACE
23490 || token
->type
== CPP_EOF
23491 || token
->type
== CPP_PRAGMA_EOL
)
23494 /* See if this token is a keyword. */
23495 keyword
= token
->keyword
;
23499 case RID_PROTECTED
:
23501 /* Consume the access-specifier. */
23502 cp_lexer_consume_token (parser
->lexer
);
23503 /* Remember which access-specifier is active. */
23504 current_access_specifier
= token
->u
.value
;
23505 /* Look for the `:'. */
23506 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23510 /* Accept #pragmas at class scope. */
23511 if (token
->type
== CPP_PRAGMA
)
23513 cp_parser_pragma (parser
, pragma_member
, NULL
);
23517 /* Otherwise, the next construction must be a
23518 member-declaration. */
23519 cp_parser_member_declaration (parser
);
23524 /* Parse a member-declaration.
23526 member-declaration:
23527 decl-specifier-seq [opt] member-declarator-list [opt] ;
23528 function-definition ; [opt]
23529 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23531 template-declaration
23534 member-declarator-list:
23536 member-declarator-list , member-declarator
23539 declarator pure-specifier [opt]
23540 declarator constant-initializer [opt]
23541 identifier [opt] : constant-expression
23545 member-declaration:
23546 __extension__ member-declaration
23549 declarator attributes [opt] pure-specifier [opt]
23550 declarator attributes [opt] constant-initializer [opt]
23551 identifier [opt] attributes [opt] : constant-expression
23555 member-declaration:
23556 static_assert-declaration */
23559 cp_parser_member_declaration (cp_parser
* parser
)
23561 cp_decl_specifier_seq decl_specifiers
;
23562 tree prefix_attributes
;
23564 int declares_class_or_enum
;
23566 cp_token
*token
= NULL
;
23567 cp_token
*decl_spec_token_start
= NULL
;
23568 cp_token
*initializer_token_start
= NULL
;
23569 int saved_pedantic
;
23570 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23572 /* Check for the `__extension__' keyword. */
23573 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23576 cp_parser_member_declaration (parser
);
23577 /* Restore the old value of the PEDANTIC flag. */
23578 pedantic
= saved_pedantic
;
23583 /* Check for a template-declaration. */
23584 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23586 /* An explicit specialization here is an error condition, and we
23587 expect the specialization handler to detect and report this. */
23588 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23589 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23590 cp_parser_explicit_specialization (parser
);
23592 cp_parser_template_declaration (parser
, /*member_p=*/true);
23596 /* Check for a template introduction. */
23597 else if (cp_parser_template_declaration_after_export (parser
, true))
23600 /* Check for a using-declaration. */
23601 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23603 if (cxx_dialect
< cxx11
)
23605 /* Parse the using-declaration. */
23606 cp_parser_using_declaration (parser
,
23607 /*access_declaration_p=*/false);
23613 bool alias_decl_expected
;
23614 cp_parser_parse_tentatively (parser
);
23615 decl
= cp_parser_alias_declaration (parser
);
23616 /* Note that if we actually see the '=' token after the
23617 identifier, cp_parser_alias_declaration commits the
23618 tentative parse. In that case, we really expect an
23619 alias-declaration. Otherwise, we expect a using
23621 alias_decl_expected
=
23622 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23623 cp_parser_parse_definitely (parser
);
23625 if (alias_decl_expected
)
23626 finish_member_declaration (decl
);
23628 cp_parser_using_declaration (parser
,
23629 /*access_declaration_p=*/false);
23634 /* Check for @defs. */
23635 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23638 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23639 ivar
= ivar_chains
;
23643 ivar
= TREE_CHAIN (member
);
23644 TREE_CHAIN (member
) = NULL_TREE
;
23645 finish_member_declaration (member
);
23650 /* If the next token is `static_assert' we have a static assertion. */
23651 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23653 cp_parser_static_assert (parser
, /*member_p=*/true);
23657 parser
->colon_corrects_to_scope_p
= false;
23659 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23662 /* Parse the decl-specifier-seq. */
23663 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23664 cp_parser_decl_specifier_seq (parser
,
23665 CP_PARSER_FLAGS_OPTIONAL
,
23667 &declares_class_or_enum
);
23668 /* Check for an invalid type-name. */
23669 if (!decl_specifiers
.any_type_specifiers_p
23670 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23672 /* If there is no declarator, then the decl-specifier-seq should
23674 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23676 /* If there was no decl-specifier-seq, and the next token is a
23677 `;', then we have something like:
23683 Each member-declaration shall declare at least one member
23684 name of the class. */
23685 if (!decl_specifiers
.any_specifiers_p
)
23687 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23688 if (!in_system_header_at (token
->location
))
23690 gcc_rich_location
richloc (token
->location
);
23691 richloc
.add_fixit_remove ();
23692 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23699 /* See if this declaration is a friend. */
23700 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23701 /* If there were decl-specifiers, check to see if there was
23702 a class-declaration. */
23703 type
= check_tag_decl (&decl_specifiers
,
23704 /*explicit_type_instantiation_p=*/false);
23705 /* Nested classes have already been added to the class, but
23706 a `friend' needs to be explicitly registered. */
23709 /* If the `friend' keyword was present, the friend must
23710 be introduced with a class-key. */
23711 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23712 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23713 "in C++03 a class-key must be used "
23714 "when declaring a friend");
23717 template <typename T> struct A {
23718 friend struct A<T>::B;
23721 A<T>::B will be represented by a TYPENAME_TYPE, and
23722 therefore not recognized by check_tag_decl. */
23725 type
= decl_specifiers
.type
;
23726 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23727 type
= TREE_TYPE (type
);
23729 if (!type
|| !TYPE_P (type
))
23730 error_at (decl_spec_token_start
->location
,
23731 "friend declaration does not name a class or "
23734 make_friend_class (current_class_type
, type
,
23735 /*complain=*/true);
23737 /* If there is no TYPE, an error message will already have
23739 else if (!type
|| type
== error_mark_node
)
23741 /* An anonymous aggregate has to be handled specially; such
23742 a declaration really declares a data member (with a
23743 particular type), as opposed to a nested class. */
23744 else if (ANON_AGGR_TYPE_P (type
))
23747 if (decl_specifiers
.storage_class
!= sc_none
)
23748 error_at (decl_spec_token_start
->location
,
23749 "a storage class on an anonymous aggregate "
23750 "in class scope is not allowed");
23752 /* Remove constructors and such from TYPE, now that we
23753 know it is an anonymous aggregate. */
23754 fixup_anonymous_aggr (type
);
23755 /* And make the corresponding data member. */
23756 decl
= build_decl (decl_spec_token_start
->location
,
23757 FIELD_DECL
, NULL_TREE
, type
);
23758 /* Add it to the class. */
23759 finish_member_declaration (decl
);
23762 cp_parser_check_access_in_redeclaration
23764 decl_spec_token_start
->location
);
23769 bool assume_semicolon
= false;
23771 /* Clear attributes from the decl_specifiers but keep them
23772 around as prefix attributes that apply them to the entity
23774 prefix_attributes
= decl_specifiers
.attributes
;
23775 decl_specifiers
.attributes
= NULL_TREE
;
23777 /* See if these declarations will be friends. */
23778 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23780 /* Keep going until we hit the `;' at the end of the
23782 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23784 tree attributes
= NULL_TREE
;
23785 tree first_attribute
;
23787 bool named_bitfld
= false;
23789 /* Peek at the next token. */
23790 token
= cp_lexer_peek_token (parser
->lexer
);
23792 /* The following code wants to know early if it is a bit-field
23793 or some other declaration. Attributes can appear before
23794 the `:' token. Skip over them without consuming any tokens
23795 to peek if they are followed by `:'. */
23796 if (cp_next_tokens_can_be_attribute_p (parser
)
23797 || (token
->type
== CPP_NAME
23798 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23799 && (named_bitfld
= true)))
23802 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23803 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23806 /* Check for a bitfield declaration. */
23807 if (token
->type
== CPP_COLON
23808 || (token
->type
== CPP_NAME
23809 && token
== cp_lexer_peek_token (parser
->lexer
)
23810 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23811 && (named_bitfld
= true)))
23815 tree late_attributes
= NULL_TREE
;
23818 identifier
= cp_parser_identifier (parser
);
23820 identifier
= NULL_TREE
;
23822 /* Look for attributes that apply to the bitfield. */
23823 attributes
= cp_parser_attributes_opt (parser
);
23825 /* Consume the `:' token. */
23826 cp_lexer_consume_token (parser
->lexer
);
23828 /* Get the width of the bitfield. */
23829 width
= cp_parser_constant_expression (parser
, false, NULL
,
23830 cxx_dialect
>= cxx11
);
23832 /* In C++2A and as extension for C++11 and above we allow
23833 default member initializers for bit-fields. */
23834 initializer
= NULL_TREE
;
23835 if (cxx_dialect
>= cxx11
23836 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23837 || cp_lexer_next_token_is (parser
->lexer
,
23841 = cp_lexer_peek_token (parser
->lexer
)->location
;
23842 if (cxx_dialect
< cxx2a
23843 && !in_system_header_at (loc
)
23844 && identifier
!= NULL_TREE
)
23846 "default member initializers for bit-fields "
23847 "only available with -std=c++2a or "
23850 initializer
= cp_parser_save_nsdmi (parser
);
23851 if (identifier
== NULL_TREE
)
23853 error_at (loc
, "default member initializer for "
23854 "unnamed bit-field");
23855 initializer
= NULL_TREE
;
23860 /* Look for attributes that apply to the bitfield after
23861 the `:' token and width. This is where GCC used to
23862 parse attributes in the past, pedwarn if there is
23863 a std attribute. */
23864 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23865 pedwarn (input_location
, OPT_Wpedantic
,
23866 "ISO C++ allows bit-field attributes only "
23867 "before the %<:%> token");
23869 late_attributes
= cp_parser_attributes_opt (parser
);
23872 attributes
= attr_chainon (attributes
, late_attributes
);
23874 /* Remember which attributes are prefix attributes and
23876 first_attribute
= attributes
;
23877 /* Combine the attributes. */
23878 attributes
= attr_chainon (prefix_attributes
, attributes
);
23880 /* Create the bitfield declaration. */
23881 decl
= grokbitfield (identifier
23882 ? make_id_declarator (NULL_TREE
,
23887 width
, initializer
,
23892 cp_declarator
*declarator
;
23893 tree asm_specification
;
23894 int ctor_dtor_or_conv_p
;
23896 /* Parse the declarator. */
23898 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23899 &ctor_dtor_or_conv_p
,
23900 /*parenthesized_p=*/NULL
,
23904 /* If something went wrong parsing the declarator, make sure
23905 that we at least consume some tokens. */
23906 if (declarator
== cp_error_declarator
)
23908 /* Skip to the end of the statement. */
23909 cp_parser_skip_to_end_of_statement (parser
);
23910 /* If the next token is not a semicolon, that is
23911 probably because we just skipped over the body of
23912 a function. So, we consume a semicolon if
23913 present, but do not issue an error message if it
23915 if (cp_lexer_next_token_is (parser
->lexer
,
23917 cp_lexer_consume_token (parser
->lexer
);
23921 if (declares_class_or_enum
& 2)
23922 cp_parser_check_for_definition_in_return_type
23923 (declarator
, decl_specifiers
.type
,
23924 decl_specifiers
.locations
[ds_type_spec
]);
23926 /* Look for an asm-specification. */
23927 asm_specification
= cp_parser_asm_specification_opt (parser
);
23928 /* Look for attributes that apply to the declaration. */
23929 attributes
= cp_parser_attributes_opt (parser
);
23930 /* Remember which attributes are prefix attributes and
23932 first_attribute
= attributes
;
23933 /* Combine the attributes. */
23934 attributes
= attr_chainon (prefix_attributes
, attributes
);
23936 /* If it's an `=', then we have a constant-initializer or a
23937 pure-specifier. It is not correct to parse the
23938 initializer before registering the member declaration
23939 since the member declaration should be in scope while
23940 its initializer is processed. However, the rest of the
23941 front end does not yet provide an interface that allows
23942 us to handle this correctly. */
23943 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23947 A pure-specifier shall be used only in the declaration of
23948 a virtual function.
23950 A member-declarator can contain a constant-initializer
23951 only if it declares a static member of integral or
23954 Therefore, if the DECLARATOR is for a function, we look
23955 for a pure-specifier; otherwise, we look for a
23956 constant-initializer. When we call `grokfield', it will
23957 perform more stringent semantics checks. */
23958 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23959 if (function_declarator_p (declarator
)
23960 || (decl_specifiers
.type
23961 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23962 && declarator
->kind
== cdk_id
23963 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23964 == FUNCTION_TYPE
)))
23965 initializer
= cp_parser_pure_specifier (parser
);
23966 else if (decl_specifiers
.storage_class
!= sc_static
)
23967 initializer
= cp_parser_save_nsdmi (parser
);
23968 else if (cxx_dialect
>= cxx11
)
23971 /* Don't require a constant rvalue in C++11, since we
23972 might want a reference constant. We'll enforce
23973 constancy later. */
23974 cp_lexer_consume_token (parser
->lexer
);
23975 /* Parse the initializer. */
23976 initializer
= cp_parser_initializer_clause (parser
,
23980 /* Parse the initializer. */
23981 initializer
= cp_parser_constant_initializer (parser
);
23983 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23984 && !function_declarator_p (declarator
))
23987 if (decl_specifiers
.storage_class
!= sc_static
)
23988 initializer
= cp_parser_save_nsdmi (parser
);
23990 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23992 /* Otherwise, there is no initializer. */
23994 initializer
= NULL_TREE
;
23996 /* See if we are probably looking at a function
23997 definition. We are certainly not looking at a
23998 member-declarator. Calling `grokfield' has
23999 side-effects, so we must not do it unless we are sure
24000 that we are looking at a member-declarator. */
24001 if (cp_parser_token_starts_function_definition_p
24002 (cp_lexer_peek_token (parser
->lexer
)))
24004 /* The grammar does not allow a pure-specifier to be
24005 used when a member function is defined. (It is
24006 possible that this fact is an oversight in the
24007 standard, since a pure function may be defined
24008 outside of the class-specifier. */
24009 if (initializer
&& initializer_token_start
)
24010 error_at (initializer_token_start
->location
,
24011 "pure-specifier on function-definition");
24012 decl
= cp_parser_save_member_function_body (parser
,
24016 if (parser
->fully_implicit_function_template_p
)
24017 decl
= finish_fully_implicit_template (parser
, decl
);
24018 /* If the member was not a friend, declare it here. */
24020 finish_member_declaration (decl
);
24021 /* Peek at the next token. */
24022 token
= cp_lexer_peek_token (parser
->lexer
);
24023 /* If the next token is a semicolon, consume it. */
24024 if (token
->type
== CPP_SEMICOLON
)
24026 location_t semicolon_loc
24027 = cp_lexer_consume_token (parser
->lexer
)->location
;
24028 gcc_rich_location
richloc (semicolon_loc
);
24029 richloc
.add_fixit_remove ();
24030 warning_at (&richloc
, OPT_Wextra_semi
,
24031 "extra %<;%> after in-class "
24032 "function definition");
24037 if (declarator
->kind
== cdk_function
)
24038 declarator
->id_loc
= token
->location
;
24039 /* Create the declaration. */
24040 decl
= grokfield (declarator
, &decl_specifiers
,
24041 initializer
, /*init_const_expr_p=*/true,
24042 asm_specification
, attributes
);
24043 if (parser
->fully_implicit_function_template_p
)
24046 finish_fully_implicit_template (parser
, 0);
24048 decl
= finish_fully_implicit_template (parser
, decl
);
24052 cp_finalize_omp_declare_simd (parser
, decl
);
24053 cp_finalize_oacc_routine (parser
, decl
, false);
24055 /* Reset PREFIX_ATTRIBUTES. */
24056 if (attributes
!= error_mark_node
)
24058 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24059 attributes
= TREE_CHAIN (attributes
);
24061 TREE_CHAIN (attributes
) = NULL_TREE
;
24064 /* If there is any qualification still in effect, clear it
24065 now; we will be starting fresh with the next declarator. */
24066 parser
->scope
= NULL_TREE
;
24067 parser
->qualifying_scope
= NULL_TREE
;
24068 parser
->object_scope
= NULL_TREE
;
24069 /* If it's a `,', then there are more declarators. */
24070 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24072 cp_lexer_consume_token (parser
->lexer
);
24073 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24075 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24076 gcc_rich_location
richloc (token
->location
);
24077 richloc
.add_fixit_remove ();
24078 error_at (&richloc
, "stray %<,%> at end of "
24079 "member declaration");
24082 /* If the next token isn't a `;', then we have a parse error. */
24083 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24086 /* The next token might be a ways away from where the
24087 actual semicolon is missing. Find the previous token
24088 and use that for our error position. */
24089 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24090 gcc_rich_location
richloc (token
->location
);
24091 richloc
.add_fixit_insert_after (";");
24092 error_at (&richloc
, "expected %<;%> at end of "
24093 "member declaration");
24095 /* Assume that the user meant to provide a semicolon. If
24096 we were to cp_parser_skip_to_end_of_statement, we might
24097 skip to a semicolon inside a member function definition
24098 and issue nonsensical error messages. */
24099 assume_semicolon
= true;
24104 /* Add DECL to the list of members. */
24106 /* Explicitly include, eg, NSDMIs, for better error
24107 recovery (c++/58650). */
24108 || !DECL_DECLARES_FUNCTION_P (decl
))
24109 finish_member_declaration (decl
);
24111 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24112 cp_parser_save_default_args (parser
, decl
);
24113 else if (TREE_CODE (decl
) == FIELD_DECL
24114 && DECL_INITIAL (decl
))
24115 /* Add DECL to the queue of NSDMI to be parsed later. */
24116 vec_safe_push (unparsed_nsdmis
, decl
);
24119 if (assume_semicolon
)
24124 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24126 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24129 /* Parse a pure-specifier.
24134 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24135 Otherwise, ERROR_MARK_NODE is returned. */
24138 cp_parser_pure_specifier (cp_parser
* parser
)
24142 /* Look for the `=' token. */
24143 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24144 return error_mark_node
;
24145 /* Look for the `0' token. */
24146 token
= cp_lexer_peek_token (parser
->lexer
);
24148 if (token
->type
== CPP_EOF
24149 || token
->type
== CPP_PRAGMA_EOL
)
24150 return error_mark_node
;
24152 cp_lexer_consume_token (parser
->lexer
);
24154 /* Accept = default or = delete in c++0x mode. */
24155 if (token
->keyword
== RID_DEFAULT
24156 || token
->keyword
== RID_DELETE
)
24158 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24159 return token
->u
.value
;
24162 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24163 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24165 cp_parser_error (parser
,
24166 "invalid pure specifier (only %<= 0%> is allowed)");
24167 cp_parser_skip_to_end_of_statement (parser
);
24168 return error_mark_node
;
24170 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24172 error_at (token
->location
, "templates may not be %<virtual%>");
24173 return error_mark_node
;
24176 return integer_zero_node
;
24179 /* Parse a constant-initializer.
24181 constant-initializer:
24182 = constant-expression
24184 Returns a representation of the constant-expression. */
24187 cp_parser_constant_initializer (cp_parser
* parser
)
24189 /* Look for the `=' token. */
24190 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24191 return error_mark_node
;
24193 /* It is invalid to write:
24195 struct S { static const int i = { 7 }; };
24198 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24200 cp_parser_error (parser
,
24201 "a brace-enclosed initializer is not allowed here");
24202 /* Consume the opening brace. */
24203 matching_braces braces
;
24204 braces
.consume_open (parser
);
24205 /* Skip the initializer. */
24206 cp_parser_skip_to_closing_brace (parser
);
24207 /* Look for the trailing `}'. */
24208 braces
.require_close (parser
);
24210 return error_mark_node
;
24213 return cp_parser_constant_expression (parser
);
24216 /* Derived classes [gram.class.derived] */
24218 /* Parse a base-clause.
24221 : base-specifier-list
24223 base-specifier-list:
24224 base-specifier ... [opt]
24225 base-specifier-list , base-specifier ... [opt]
24227 Returns a TREE_LIST representing the base-classes, in the order in
24228 which they were declared. The representation of each node is as
24229 described by cp_parser_base_specifier.
24231 In the case that no bases are specified, this function will return
24232 NULL_TREE, not ERROR_MARK_NODE. */
24235 cp_parser_base_clause (cp_parser
* parser
)
24237 tree bases
= NULL_TREE
;
24239 /* Look for the `:' that begins the list. */
24240 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24242 /* Scan the base-specifier-list. */
24247 bool pack_expansion_p
= false;
24249 /* Look for the base-specifier. */
24250 base
= cp_parser_base_specifier (parser
);
24251 /* Look for the (optional) ellipsis. */
24252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24254 /* Consume the `...'. */
24255 cp_lexer_consume_token (parser
->lexer
);
24257 pack_expansion_p
= true;
24260 /* Add BASE to the front of the list. */
24261 if (base
&& base
!= error_mark_node
)
24263 if (pack_expansion_p
)
24264 /* Make this a pack expansion type. */
24265 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24267 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24269 TREE_CHAIN (base
) = bases
;
24273 /* Peek at the next token. */
24274 token
= cp_lexer_peek_token (parser
->lexer
);
24275 /* If it's not a comma, then the list is complete. */
24276 if (token
->type
!= CPP_COMMA
)
24278 /* Consume the `,'. */
24279 cp_lexer_consume_token (parser
->lexer
);
24282 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24283 base class had a qualified name. However, the next name that
24284 appears is certainly not qualified. */
24285 parser
->scope
= NULL_TREE
;
24286 parser
->qualifying_scope
= NULL_TREE
;
24287 parser
->object_scope
= NULL_TREE
;
24289 return nreverse (bases
);
24292 /* Parse a base-specifier.
24295 :: [opt] nested-name-specifier [opt] class-name
24296 virtual access-specifier [opt] :: [opt] nested-name-specifier
24298 access-specifier virtual [opt] :: [opt] nested-name-specifier
24301 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24302 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24303 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24304 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24307 cp_parser_base_specifier (cp_parser
* parser
)
24311 bool virtual_p
= false;
24312 bool duplicate_virtual_error_issued_p
= false;
24313 bool duplicate_access_error_issued_p
= false;
24314 bool class_scope_p
, template_p
;
24315 tree access
= access_default_node
;
24318 /* Process the optional `virtual' and `access-specifier'. */
24321 /* Peek at the next token. */
24322 token
= cp_lexer_peek_token (parser
->lexer
);
24323 /* Process `virtual'. */
24324 switch (token
->keyword
)
24327 /* If `virtual' appears more than once, issue an error. */
24328 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24330 cp_parser_error (parser
,
24331 "%<virtual%> specified more than once in base-specifier");
24332 duplicate_virtual_error_issued_p
= true;
24337 /* Consume the `virtual' token. */
24338 cp_lexer_consume_token (parser
->lexer
);
24343 case RID_PROTECTED
:
24345 /* If more than one access specifier appears, issue an
24347 if (access
!= access_default_node
24348 && !duplicate_access_error_issued_p
)
24350 cp_parser_error (parser
,
24351 "more than one access specifier in base-specifier");
24352 duplicate_access_error_issued_p
= true;
24355 access
= ridpointers
[(int) token
->keyword
];
24357 /* Consume the access-specifier. */
24358 cp_lexer_consume_token (parser
->lexer
);
24367 /* It is not uncommon to see programs mechanically, erroneously, use
24368 the 'typename' keyword to denote (dependent) qualified types
24369 as base classes. */
24370 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24372 token
= cp_lexer_peek_token (parser
->lexer
);
24373 if (!processing_template_decl
)
24374 error_at (token
->location
,
24375 "keyword %<typename%> not allowed outside of templates");
24377 error_at (token
->location
,
24378 "keyword %<typename%> not allowed in this context "
24379 "(the base class is implicitly a type)");
24380 cp_lexer_consume_token (parser
->lexer
);
24383 /* Look for the optional `::' operator. */
24384 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24385 /* Look for the nested-name-specifier. The simplest way to
24390 The keyword `typename' is not permitted in a base-specifier or
24391 mem-initializer; in these contexts a qualified name that
24392 depends on a template-parameter is implicitly assumed to be a
24395 is to pretend that we have seen the `typename' keyword at this
24397 cp_parser_nested_name_specifier_opt (parser
,
24398 /*typename_keyword_p=*/true,
24399 /*check_dependency_p=*/true,
24401 /*is_declaration=*/true);
24402 /* If the base class is given by a qualified name, assume that names
24403 we see are type names or templates, as appropriate. */
24404 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24405 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24408 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24409 /* DR 950 allows decltype as a base-specifier. */
24410 type
= cp_parser_decltype (parser
);
24413 /* Otherwise, look for the class-name. */
24414 type
= cp_parser_class_name (parser
,
24418 /*check_dependency_p=*/true,
24419 /*class_head_p=*/false,
24420 /*is_declaration=*/true);
24421 type
= TREE_TYPE (type
);
24424 if (type
== error_mark_node
)
24425 return error_mark_node
;
24427 return finish_base_specifier (type
, access
, virtual_p
);
24430 /* Exception handling [gram.exception] */
24432 /* Parse an (optional) noexcept-specification.
24434 noexcept-specification:
24435 noexcept ( constant-expression ) [opt]
24437 If no noexcept-specification is present, returns NULL_TREE.
24438 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24439 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24440 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24441 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24442 in which case a boolean condition is returned instead. */
24445 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24446 bool require_constexpr
,
24447 bool* consumed_expr
,
24451 const char *saved_message
;
24453 /* Peek at the next token. */
24454 token
= cp_lexer_peek_token (parser
->lexer
);
24456 /* Is it a noexcept-specification? */
24457 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24460 cp_lexer_consume_token (parser
->lexer
);
24462 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24464 matching_parens parens
;
24465 parens
.consume_open (parser
);
24467 if (require_constexpr
)
24469 /* Types may not be defined in an exception-specification. */
24470 saved_message
= parser
->type_definition_forbidden_message
;
24471 parser
->type_definition_forbidden_message
24472 = G_("types may not be defined in an exception-specification");
24474 expr
= cp_parser_constant_expression (parser
);
24476 /* Restore the saved message. */
24477 parser
->type_definition_forbidden_message
= saved_message
;
24481 expr
= cp_parser_expression (parser
);
24482 *consumed_expr
= true;
24485 parens
.require_close (parser
);
24489 expr
= boolean_true_node
;
24490 if (!require_constexpr
)
24491 *consumed_expr
= false;
24494 /* We cannot build a noexcept-spec right away because this will check
24495 that expr is a constexpr. */
24497 return build_noexcept_spec (expr
, tf_warning_or_error
);
24505 /* Parse an (optional) exception-specification.
24507 exception-specification:
24508 throw ( type-id-list [opt] )
24510 Returns a TREE_LIST representing the exception-specification. The
24511 TREE_VALUE of each node is a type. */
24514 cp_parser_exception_specification_opt (cp_parser
* parser
)
24518 const char *saved_message
;
24520 /* Peek at the next token. */
24521 token
= cp_lexer_peek_token (parser
->lexer
);
24523 /* Is it a noexcept-specification? */
24524 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24526 if (type_id_list
!= NULL_TREE
)
24527 return type_id_list
;
24529 /* If it's not `throw', then there's no exception-specification. */
24530 if (!cp_parser_is_keyword (token
, RID_THROW
))
24533 location_t loc
= token
->location
;
24535 /* Consume the `throw'. */
24536 cp_lexer_consume_token (parser
->lexer
);
24538 /* Look for the `('. */
24539 matching_parens parens
;
24540 parens
.require_open (parser
);
24542 /* Peek at the next token. */
24543 token
= cp_lexer_peek_token (parser
->lexer
);
24544 /* If it's not a `)', then there is a type-id-list. */
24545 if (token
->type
!= CPP_CLOSE_PAREN
)
24547 /* Types may not be defined in an exception-specification. */
24548 saved_message
= parser
->type_definition_forbidden_message
;
24549 parser
->type_definition_forbidden_message
24550 = G_("types may not be defined in an exception-specification");
24551 /* Parse the type-id-list. */
24552 type_id_list
= cp_parser_type_id_list (parser
);
24553 /* Restore the saved message. */
24554 parser
->type_definition_forbidden_message
= saved_message
;
24556 if (cxx_dialect
>= cxx17
)
24558 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24560 type_id_list
= NULL_TREE
;
24562 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24563 warning_at (loc
, OPT_Wdeprecated
,
24564 "dynamic exception specifications are deprecated in "
24567 /* In C++17, throw() is equivalent to noexcept (true). throw()
24568 is deprecated in C++11 and above as well, but is still widely used,
24569 so don't warn about it yet. */
24570 else if (cxx_dialect
>= cxx17
)
24571 type_id_list
= noexcept_true_spec
;
24573 type_id_list
= empty_except_spec
;
24575 /* Look for the `)'. */
24576 parens
.require_close (parser
);
24578 return type_id_list
;
24581 /* Parse an (optional) type-id-list.
24585 type-id-list , type-id ... [opt]
24587 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24588 in the order that the types were presented. */
24591 cp_parser_type_id_list (cp_parser
* parser
)
24593 tree types
= NULL_TREE
;
24600 token
= cp_lexer_peek_token (parser
->lexer
);
24602 /* Get the next type-id. */
24603 type
= cp_parser_type_id (parser
);
24604 /* Check for invalid 'auto'. */
24605 if (flag_concepts
&& type_uses_auto (type
))
24607 error_at (token
->location
,
24608 "invalid use of %<auto%> in exception-specification");
24609 type
= error_mark_node
;
24611 /* Parse the optional ellipsis. */
24612 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24614 /* Consume the `...'. */
24615 cp_lexer_consume_token (parser
->lexer
);
24617 /* Turn the type into a pack expansion expression. */
24618 type
= make_pack_expansion (type
);
24620 /* Add it to the list. */
24621 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24622 /* Peek at the next token. */
24623 token
= cp_lexer_peek_token (parser
->lexer
);
24624 /* If it is not a `,', we are done. */
24625 if (token
->type
!= CPP_COMMA
)
24627 /* Consume the `,'. */
24628 cp_lexer_consume_token (parser
->lexer
);
24631 return nreverse (types
);
24634 /* Parse a try-block.
24637 try compound-statement handler-seq */
24640 cp_parser_try_block (cp_parser
* parser
)
24644 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24645 if (parser
->in_function_body
24646 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24647 error ("%<try%> in %<constexpr%> function");
24649 try_block
= begin_try_block ();
24650 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24651 finish_try_block (try_block
);
24652 cp_parser_handler_seq (parser
);
24653 finish_handler_sequence (try_block
);
24658 /* Parse a function-try-block.
24660 function-try-block:
24661 try ctor-initializer [opt] function-body handler-seq */
24664 cp_parser_function_try_block (cp_parser
* parser
)
24666 tree compound_stmt
;
24669 /* Look for the `try' keyword. */
24670 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24672 /* Let the rest of the front end know where we are. */
24673 try_block
= begin_function_try_block (&compound_stmt
);
24674 /* Parse the function-body. */
24675 cp_parser_ctor_initializer_opt_and_function_body
24676 (parser
, /*in_function_try_block=*/true);
24677 /* We're done with the `try' part. */
24678 finish_function_try_block (try_block
);
24679 /* Parse the handlers. */
24680 cp_parser_handler_seq (parser
);
24681 /* We're done with the handlers. */
24682 finish_function_handler_sequence (try_block
, compound_stmt
);
24685 /* Parse a handler-seq.
24688 handler handler-seq [opt] */
24691 cp_parser_handler_seq (cp_parser
* parser
)
24697 /* Parse the handler. */
24698 cp_parser_handler (parser
);
24699 /* Peek at the next token. */
24700 token
= cp_lexer_peek_token (parser
->lexer
);
24701 /* If it's not `catch' then there are no more handlers. */
24702 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24707 /* Parse a handler.
24710 catch ( exception-declaration ) compound-statement */
24713 cp_parser_handler (cp_parser
* parser
)
24718 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24719 handler
= begin_handler ();
24720 matching_parens parens
;
24721 parens
.require_open (parser
);
24722 declaration
= cp_parser_exception_declaration (parser
);
24723 finish_handler_parms (declaration
, handler
);
24724 parens
.require_close (parser
);
24725 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24726 finish_handler (handler
);
24729 /* Parse an exception-declaration.
24731 exception-declaration:
24732 type-specifier-seq declarator
24733 type-specifier-seq abstract-declarator
24737 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24738 ellipsis variant is used. */
24741 cp_parser_exception_declaration (cp_parser
* parser
)
24743 cp_decl_specifier_seq type_specifiers
;
24744 cp_declarator
*declarator
;
24745 const char *saved_message
;
24747 /* If it's an ellipsis, it's easy to handle. */
24748 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24750 /* Consume the `...' token. */
24751 cp_lexer_consume_token (parser
->lexer
);
24755 /* Types may not be defined in exception-declarations. */
24756 saved_message
= parser
->type_definition_forbidden_message
;
24757 parser
->type_definition_forbidden_message
24758 = G_("types may not be defined in exception-declarations");
24760 /* Parse the type-specifier-seq. */
24761 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24762 /*is_trailing_return=*/false,
24764 /* If it's a `)', then there is no declarator. */
24765 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24768 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24769 /*ctor_dtor_or_conv_p=*/NULL
,
24770 /*parenthesized_p=*/NULL
,
24771 /*member_p=*/false,
24772 /*friend_p=*/false);
24774 /* Restore the saved message. */
24775 parser
->type_definition_forbidden_message
= saved_message
;
24777 if (!type_specifiers
.any_specifiers_p
)
24778 return error_mark_node
;
24780 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24783 /* Parse a throw-expression.
24786 throw assignment-expression [opt]
24788 Returns a THROW_EXPR representing the throw-expression. */
24791 cp_parser_throw_expression (cp_parser
* parser
)
24796 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24797 token
= cp_lexer_peek_token (parser
->lexer
);
24798 /* Figure out whether or not there is an assignment-expression
24799 following the "throw" keyword. */
24800 if (token
->type
== CPP_COMMA
24801 || token
->type
== CPP_SEMICOLON
24802 || token
->type
== CPP_CLOSE_PAREN
24803 || token
->type
== CPP_CLOSE_SQUARE
24804 || token
->type
== CPP_CLOSE_BRACE
24805 || token
->type
== CPP_COLON
)
24806 expression
= NULL_TREE
;
24808 expression
= cp_parser_assignment_expression (parser
);
24810 return build_throw (expression
);
24813 /* GNU Extensions */
24815 /* Parse an (optional) asm-specification.
24818 asm ( string-literal )
24820 If the asm-specification is present, returns a STRING_CST
24821 corresponding to the string-literal. Otherwise, returns
24825 cp_parser_asm_specification_opt (cp_parser
* parser
)
24828 tree asm_specification
;
24830 /* Peek at the next token. */
24831 token
= cp_lexer_peek_token (parser
->lexer
);
24832 /* If the next token isn't the `asm' keyword, then there's no
24833 asm-specification. */
24834 if (!cp_parser_is_keyword (token
, RID_ASM
))
24837 /* Consume the `asm' token. */
24838 cp_lexer_consume_token (parser
->lexer
);
24839 /* Look for the `('. */
24840 matching_parens parens
;
24841 parens
.require_open (parser
);
24843 /* Look for the string-literal. */
24844 asm_specification
= cp_parser_string_literal (parser
, false, false);
24846 /* Look for the `)'. */
24847 parens
.require_close (parser
);
24849 return asm_specification
;
24852 /* Parse an asm-operand-list.
24856 asm-operand-list , asm-operand
24859 string-literal ( expression )
24860 [ string-literal ] string-literal ( expression )
24862 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24863 each node is the expression. The TREE_PURPOSE is itself a
24864 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24865 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24866 is a STRING_CST for the string literal before the parenthesis. Returns
24867 ERROR_MARK_NODE if any of the operands are invalid. */
24870 cp_parser_asm_operand_list (cp_parser
* parser
)
24872 tree asm_operands
= NULL_TREE
;
24873 bool invalid_operands
= false;
24877 tree string_literal
;
24881 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24883 /* Consume the `[' token. */
24884 cp_lexer_consume_token (parser
->lexer
);
24885 /* Read the operand name. */
24886 name
= cp_parser_identifier (parser
);
24887 if (name
!= error_mark_node
)
24888 name
= build_string (IDENTIFIER_LENGTH (name
),
24889 IDENTIFIER_POINTER (name
));
24890 /* Look for the closing `]'. */
24891 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24895 /* Look for the string-literal. */
24896 string_literal
= cp_parser_string_literal (parser
, false, false);
24898 /* Look for the `('. */
24899 matching_parens parens
;
24900 parens
.require_open (parser
);
24901 /* Parse the expression. */
24902 expression
= cp_parser_expression (parser
);
24903 /* Look for the `)'. */
24904 parens
.require_close (parser
);
24906 if (name
== error_mark_node
24907 || string_literal
== error_mark_node
24908 || expression
== error_mark_node
)
24909 invalid_operands
= true;
24911 /* Add this operand to the list. */
24912 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24915 /* If the next token is not a `,', there are no more
24917 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24919 /* Consume the `,'. */
24920 cp_lexer_consume_token (parser
->lexer
);
24923 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24926 /* Parse an asm-clobber-list.
24930 asm-clobber-list , string-literal
24932 Returns a TREE_LIST, indicating the clobbers in the order that they
24933 appeared. The TREE_VALUE of each node is a STRING_CST. */
24936 cp_parser_asm_clobber_list (cp_parser
* parser
)
24938 tree clobbers
= NULL_TREE
;
24942 tree string_literal
;
24944 /* Look for the string literal. */
24945 string_literal
= cp_parser_string_literal (parser
, false, false);
24946 /* Add it to the list. */
24947 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24948 /* If the next token is not a `,', then the list is
24950 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24952 /* Consume the `,' token. */
24953 cp_lexer_consume_token (parser
->lexer
);
24959 /* Parse an asm-label-list.
24963 asm-label-list , identifier
24965 Returns a TREE_LIST, indicating the labels in the order that they
24966 appeared. The TREE_VALUE of each node is a label. */
24969 cp_parser_asm_label_list (cp_parser
* parser
)
24971 tree labels
= NULL_TREE
;
24975 tree identifier
, label
, name
;
24977 /* Look for the identifier. */
24978 identifier
= cp_parser_identifier (parser
);
24979 if (!error_operand_p (identifier
))
24981 label
= lookup_label (identifier
);
24982 if (TREE_CODE (label
) == LABEL_DECL
)
24984 TREE_USED (label
) = 1;
24985 check_goto (label
);
24986 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24987 IDENTIFIER_POINTER (identifier
));
24988 labels
= tree_cons (name
, label
, labels
);
24991 /* If the next token is not a `,', then the list is
24993 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24995 /* Consume the `,' token. */
24996 cp_lexer_consume_token (parser
->lexer
);
24999 return nreverse (labels
);
25002 /* Return TRUE iff the next tokens in the stream are possibly the
25003 beginning of a GNU extension attribute. */
25006 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25008 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25011 /* Return TRUE iff the next tokens in the stream are possibly the
25012 beginning of a standard C++-11 attribute specifier. */
25015 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25017 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25020 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25021 beginning of a standard C++-11 attribute specifier. */
25024 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25026 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25028 return (cxx_dialect
>= cxx11
25029 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25030 || (token
->type
== CPP_OPEN_SQUARE
25031 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25032 && token
->type
== CPP_OPEN_SQUARE
)));
25035 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25036 beginning of a GNU extension attribute. */
25039 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25041 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25043 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25046 /* Return true iff the next tokens can be the beginning of either a
25047 GNU attribute list, or a standard C++11 attribute sequence. */
25050 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25052 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25053 || cp_next_tokens_can_be_std_attribute_p (parser
));
25056 /* Return true iff the next Nth tokens can be the beginning of either
25057 a GNU attribute list, or a standard C++11 attribute sequence. */
25060 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25062 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25063 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25066 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25067 of GNU attributes, or return NULL. */
25070 cp_parser_attributes_opt (cp_parser
*parser
)
25072 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25073 return cp_parser_gnu_attributes_opt (parser
);
25074 return cp_parser_std_attribute_spec_seq (parser
);
25077 /* Parse an (optional) series of attributes.
25080 attributes attribute
25083 __attribute__ (( attribute-list [opt] ))
25085 The return value is as for cp_parser_gnu_attribute_list. */
25088 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25090 tree attributes
= NULL_TREE
;
25092 temp_override
<bool> cleanup
25093 (parser
->auto_is_implicit_function_template_parm_p
, false);
25098 tree attribute_list
;
25101 /* Peek at the next token. */
25102 token
= cp_lexer_peek_token (parser
->lexer
);
25103 /* If it's not `__attribute__', then we're done. */
25104 if (token
->keyword
!= RID_ATTRIBUTE
)
25107 /* Consume the `__attribute__' keyword. */
25108 cp_lexer_consume_token (parser
->lexer
);
25109 /* Look for the two `(' tokens. */
25110 matching_parens outer_parens
;
25111 outer_parens
.require_open (parser
);
25112 matching_parens inner_parens
;
25113 inner_parens
.require_open (parser
);
25115 /* Peek at the next token. */
25116 token
= cp_lexer_peek_token (parser
->lexer
);
25117 if (token
->type
!= CPP_CLOSE_PAREN
)
25118 /* Parse the attribute-list. */
25119 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25121 /* If the next token is a `)', then there is no attribute
25123 attribute_list
= NULL
;
25125 /* Look for the two `)' tokens. */
25126 if (!inner_parens
.require_close (parser
))
25128 if (!outer_parens
.require_close (parser
))
25131 cp_parser_skip_to_end_of_statement (parser
);
25133 /* Add these new attributes to the list. */
25134 attributes
= attr_chainon (attributes
, attribute_list
);
25140 /* Parse a GNU attribute-list.
25144 attribute-list , attribute
25148 identifier ( identifier )
25149 identifier ( identifier , expression-list )
25150 identifier ( expression-list )
25152 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25153 to an attribute. The TREE_PURPOSE of each node is the identifier
25154 indicating which attribute is in use. The TREE_VALUE represents
25155 the arguments, if any. */
25158 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25160 tree attribute_list
= NULL_TREE
;
25161 bool save_translate_strings_p
= parser
->translate_strings_p
;
25163 parser
->translate_strings_p
= false;
25170 /* Look for the identifier. We also allow keywords here; for
25171 example `__attribute__ ((const))' is legal. */
25172 token
= cp_lexer_peek_token (parser
->lexer
);
25173 if (token
->type
== CPP_NAME
25174 || token
->type
== CPP_KEYWORD
)
25176 tree arguments
= NULL_TREE
;
25178 /* Consume the token, but save it since we need it for the
25179 SIMD enabled function parsing. */
25180 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25182 /* Save away the identifier that indicates which attribute
25184 identifier
= (token
->type
== CPP_KEYWORD
)
25185 /* For keywords, use the canonical spelling, not the
25186 parsed identifier. */
25187 ? ridpointers
[(int) token
->keyword
]
25188 : id_token
->u
.value
;
25190 identifier
= canonicalize_attr_name (identifier
);
25191 attribute
= build_tree_list (identifier
, NULL_TREE
);
25193 /* Peek at the next token. */
25194 token
= cp_lexer_peek_token (parser
->lexer
);
25195 /* If it's an `(', then parse the attribute arguments. */
25196 if (token
->type
== CPP_OPEN_PAREN
)
25198 vec
<tree
, va_gc
> *vec
;
25199 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25200 ? id_attr
: normal_attr
);
25201 vec
= cp_parser_parenthesized_expression_list
25202 (parser
, attr_flag
, /*cast_p=*/false,
25203 /*allow_expansion_p=*/false,
25204 /*non_constant_p=*/NULL
);
25206 arguments
= error_mark_node
;
25209 arguments
= build_tree_list_vec (vec
);
25210 release_tree_vector (vec
);
25212 /* Save the arguments away. */
25213 TREE_VALUE (attribute
) = arguments
;
25216 if (arguments
!= error_mark_node
)
25218 /* Add this attribute to the list. */
25219 TREE_CHAIN (attribute
) = attribute_list
;
25220 attribute_list
= attribute
;
25223 token
= cp_lexer_peek_token (parser
->lexer
);
25225 /* Now, look for more attributes. If the next token isn't a
25226 `,', we're done. */
25227 if (token
->type
!= CPP_COMMA
)
25230 /* Consume the comma and keep going. */
25231 cp_lexer_consume_token (parser
->lexer
);
25233 parser
->translate_strings_p
= save_translate_strings_p
;
25235 /* We built up the list in reverse order. */
25236 return nreverse (attribute_list
);
25239 /* Parse a standard C++11 attribute.
25241 The returned representation is a TREE_LIST which TREE_PURPOSE is
25242 the scoped name of the attribute, and the TREE_VALUE is its
25245 Note that the scoped name of the attribute is itself a TREE_LIST
25246 which TREE_PURPOSE is the namespace of the attribute, and
25247 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25248 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25249 and which TREE_PURPOSE is directly the attribute name.
25251 Clients of the attribute code should use get_attribute_namespace
25252 and get_attribute_name to get the actual namespace and name of
25253 attributes, regardless of their being GNU or C++11 attributes.
25256 attribute-token attribute-argument-clause [opt]
25260 attribute-scoped-token
25262 attribute-scoped-token:
25263 attribute-namespace :: identifier
25265 attribute-namespace:
25268 attribute-argument-clause:
25269 ( balanced-token-seq )
25271 balanced-token-seq:
25272 balanced-token [opt]
25273 balanced-token-seq balanced-token
25276 ( balanced-token-seq )
25277 [ balanced-token-seq ]
25278 { balanced-token-seq }. */
25281 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25283 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25286 temp_override
<bool> cleanup
25287 (parser
->auto_is_implicit_function_template_parm_p
, false);
25289 /* First, parse name of the attribute, a.k.a attribute-token. */
25291 token
= cp_lexer_peek_token (parser
->lexer
);
25292 if (token
->type
== CPP_NAME
)
25293 attr_id
= token
->u
.value
;
25294 else if (token
->type
== CPP_KEYWORD
)
25295 attr_id
= ridpointers
[(int) token
->keyword
];
25296 else if (token
->flags
& NAMED_OP
)
25297 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25299 if (attr_id
== NULL_TREE
)
25302 cp_lexer_consume_token (parser
->lexer
);
25304 token
= cp_lexer_peek_token (parser
->lexer
);
25305 if (token
->type
== CPP_SCOPE
)
25307 /* We are seeing a scoped attribute token. */
25309 cp_lexer_consume_token (parser
->lexer
);
25311 error_at (token
->location
, "attribute using prefix used together "
25312 "with scoped attribute token");
25315 token
= cp_lexer_consume_token (parser
->lexer
);
25316 if (token
->type
== CPP_NAME
)
25317 attr_id
= token
->u
.value
;
25318 else if (token
->type
== CPP_KEYWORD
)
25319 attr_id
= ridpointers
[(int) token
->keyword
];
25320 else if (token
->flags
& NAMED_OP
)
25321 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25324 error_at (token
->location
,
25325 "expected an identifier for the attribute name");
25326 return error_mark_node
;
25329 attr_id
= canonicalize_attr_name (attr_id
);
25330 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25332 token
= cp_lexer_peek_token (parser
->lexer
);
25335 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25339 attr_id
= canonicalize_attr_name (attr_id
);
25340 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25342 /* C++11 noreturn attribute is equivalent to GNU's. */
25343 if (is_attribute_p ("noreturn", attr_id
))
25344 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25345 /* C++14 deprecated attribute is equivalent to GNU's. */
25346 else if (is_attribute_p ("deprecated", attr_id
))
25347 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25348 /* C++17 fallthrough attribute is equivalent to GNU's. */
25349 else if (is_attribute_p ("fallthrough", attr_id
))
25350 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
25351 /* Transactional Memory TS optimize_for_synchronized attribute is
25352 equivalent to GNU transaction_callable. */
25353 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25354 TREE_PURPOSE (attribute
)
25355 = get_identifier ("transaction_callable");
25356 /* Transactional Memory attributes are GNU attributes. */
25357 else if (tm_attr_to_mask (attr_id
))
25358 TREE_PURPOSE (attribute
) = attr_id
;
25361 /* Now parse the optional argument clause of the attribute. */
25363 if (token
->type
!= CPP_OPEN_PAREN
)
25367 vec
<tree
, va_gc
> *vec
;
25368 int attr_flag
= normal_attr
;
25370 if (attr_ns
== gnu_identifier
25371 && attribute_takes_identifier_p (attr_id
))
25372 /* A GNU attribute that takes an identifier in parameter. */
25373 attr_flag
= id_attr
;
25375 vec
= cp_parser_parenthesized_expression_list
25376 (parser
, attr_flag
, /*cast_p=*/false,
25377 /*allow_expansion_p=*/true,
25378 /*non_constant_p=*/NULL
);
25380 arguments
= error_mark_node
;
25383 arguments
= build_tree_list_vec (vec
);
25384 release_tree_vector (vec
);
25387 if (arguments
== error_mark_node
)
25388 attribute
= error_mark_node
;
25390 TREE_VALUE (attribute
) = arguments
;
25396 /* Check that the attribute ATTRIBUTE appears at most once in the
25397 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25398 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25399 isn't implemented yet in GCC. */
25402 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25406 tree name
= get_attribute_name (attribute
);
25407 if (is_attribute_p ("noreturn", name
)
25408 && lookup_attribute ("noreturn", attributes
))
25409 error ("attribute %<noreturn%> can appear at most once "
25410 "in an attribute-list");
25411 else if (is_attribute_p ("deprecated", name
)
25412 && lookup_attribute ("deprecated", attributes
))
25413 error ("attribute %<deprecated%> can appear at most once "
25414 "in an attribute-list");
25418 /* Parse a list of standard C++-11 attributes.
25422 attribute-list , attribute[opt]
25424 attribute-list , attribute ...
25428 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25430 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25431 cp_token
*token
= NULL
;
25435 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25436 if (attribute
== error_mark_node
)
25438 if (attribute
!= NULL_TREE
)
25440 cp_parser_check_std_attribute (attributes
, attribute
);
25441 TREE_CHAIN (attribute
) = attributes
;
25442 attributes
= attribute
;
25444 token
= cp_lexer_peek_token (parser
->lexer
);
25445 if (token
->type
== CPP_ELLIPSIS
)
25447 cp_lexer_consume_token (parser
->lexer
);
25448 if (attribute
== NULL_TREE
)
25449 error_at (token
->location
,
25450 "expected attribute before %<...%>");
25453 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25454 if (pack
== error_mark_node
)
25455 return error_mark_node
;
25456 TREE_VALUE (attribute
) = pack
;
25458 token
= cp_lexer_peek_token (parser
->lexer
);
25460 if (token
->type
!= CPP_COMMA
)
25462 cp_lexer_consume_token (parser
->lexer
);
25464 attributes
= nreverse (attributes
);
25468 /* Parse a standard C++-11 attribute specifier.
25470 attribute-specifier:
25471 [ [ attribute-using-prefix [opt] attribute-list ] ]
25472 alignment-specifier
25474 attribute-using-prefix:
25475 using attribute-namespace :
25477 alignment-specifier:
25478 alignas ( type-id ... [opt] )
25479 alignas ( alignment-expression ... [opt] ). */
25482 cp_parser_std_attribute_spec (cp_parser
*parser
)
25484 tree attributes
= NULL_TREE
;
25485 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25487 if (token
->type
== CPP_OPEN_SQUARE
25488 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25490 tree attr_ns
= NULL_TREE
;
25492 cp_lexer_consume_token (parser
->lexer
);
25493 cp_lexer_consume_token (parser
->lexer
);
25495 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25497 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25498 if (token
->type
== CPP_NAME
)
25499 attr_ns
= token
->u
.value
;
25500 else if (token
->type
== CPP_KEYWORD
)
25501 attr_ns
= ridpointers
[(int) token
->keyword
];
25502 else if (token
->flags
& NAMED_OP
)
25503 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25506 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25508 if (cxx_dialect
< cxx17
25509 && !in_system_header_at (input_location
))
25510 pedwarn (input_location
, 0,
25511 "attribute using prefix only available "
25512 "with -std=c++17 or -std=gnu++17");
25514 cp_lexer_consume_token (parser
->lexer
);
25515 cp_lexer_consume_token (parser
->lexer
);
25516 cp_lexer_consume_token (parser
->lexer
);
25519 attr_ns
= NULL_TREE
;
25522 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25524 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25525 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25526 cp_parser_skip_to_end_of_statement (parser
);
25528 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25529 when we are sure that we have actually parsed them. */
25530 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25536 /* Look for an alignment-specifier. */
25538 token
= cp_lexer_peek_token (parser
->lexer
);
25540 if (token
->type
!= CPP_KEYWORD
25541 || token
->keyword
!= RID_ALIGNAS
)
25544 cp_lexer_consume_token (parser
->lexer
);
25545 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25547 matching_parens parens
;
25548 if (!parens
.require_open (parser
))
25549 return error_mark_node
;
25551 cp_parser_parse_tentatively (parser
);
25552 alignas_expr
= cp_parser_type_id (parser
);
25554 if (!cp_parser_parse_definitely (parser
))
25556 alignas_expr
= cp_parser_assignment_expression (parser
);
25557 if (alignas_expr
== error_mark_node
)
25558 cp_parser_skip_to_end_of_statement (parser
);
25559 if (alignas_expr
== NULL_TREE
25560 || alignas_expr
== error_mark_node
)
25561 return alignas_expr
;
25564 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25565 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25567 /* Handle alignas (pack...). */
25568 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25570 cp_lexer_consume_token (parser
->lexer
);
25571 alignas_expr
= make_pack_expansion (alignas_expr
);
25574 /* Something went wrong, so don't build the attribute. */
25575 if (alignas_expr
== error_mark_node
)
25576 return error_mark_node
;
25578 if (!parens
.require_close (parser
))
25579 return error_mark_node
;
25581 /* Build the C++-11 representation of an 'aligned'
25584 = build_tree_list (build_tree_list (gnu_identifier
,
25585 aligned_identifier
), alignas_expr
);
25591 /* Parse a standard C++-11 attribute-specifier-seq.
25593 attribute-specifier-seq:
25594 attribute-specifier-seq [opt] attribute-specifier
25598 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25600 tree attr_specs
= NULL_TREE
;
25601 tree attr_last
= NULL_TREE
;
25605 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25606 if (attr_spec
== NULL_TREE
)
25608 if (attr_spec
== error_mark_node
)
25609 return error_mark_node
;
25612 TREE_CHAIN (attr_last
) = attr_spec
;
25614 attr_specs
= attr_last
= attr_spec
;
25615 attr_last
= tree_last (attr_last
);
25621 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25622 return index of the first token after balanced-token, or N on failure. */
25625 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25628 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25630 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25633 case CPP_PRAGMA_EOL
:
25634 /* Ran out of tokens. */
25636 case CPP_OPEN_PAREN
:
25639 case CPP_OPEN_BRACE
:
25642 case CPP_OPEN_SQUARE
:
25645 case CPP_CLOSE_PAREN
:
25648 case CPP_CLOSE_BRACE
:
25651 case CPP_CLOSE_SQUARE
:
25657 while (nparens
|| nbraces
|| nsquares
);
25661 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25662 return index of the first token after the GNU attribute tokens, or N on
25666 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25670 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25671 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25672 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25675 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25678 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25685 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25686 next token), return index of the first token after the standard C++11
25687 attribute tokens, or N on failure. */
25690 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25694 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25695 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25697 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25700 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25704 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25705 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25707 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25718 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25719 as the next token), return index of the first token after the attribute
25720 tokens, or N on failure. */
25723 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25725 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25726 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25727 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25730 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25731 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25732 current value of the PEDANTIC flag, regardless of whether or not
25733 the `__extension__' keyword is present. The caller is responsible
25734 for restoring the value of the PEDANTIC flag. */
25737 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25739 /* Save the old value of the PEDANTIC flag. */
25740 *saved_pedantic
= pedantic
;
25742 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25744 /* Consume the `__extension__' token. */
25745 cp_lexer_consume_token (parser
->lexer
);
25746 /* We're not being pedantic while the `__extension__' keyword is
25756 /* Parse a label declaration.
25759 __label__ label-declarator-seq ;
25761 label-declarator-seq:
25762 identifier , label-declarator-seq
25766 cp_parser_label_declaration (cp_parser
* parser
)
25768 /* Look for the `__label__' keyword. */
25769 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25775 /* Look for an identifier. */
25776 identifier
= cp_parser_identifier (parser
);
25777 /* If we failed, stop. */
25778 if (identifier
== error_mark_node
)
25780 /* Declare it as a label. */
25781 finish_label_decl (identifier
);
25782 /* If the next token is a `;', stop. */
25783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25785 /* Look for the `,' separating the label declarations. */
25786 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25789 /* Look for the final `;'. */
25790 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25793 // -------------------------------------------------------------------------- //
25796 // Parse a requires clause.
25798 // requires-clause:
25799 // 'requires' logical-or-expression
25801 // The required logical-or-expression must be a constant expression. Note
25802 // that we don't check that the expression is constepxr here. We defer until
25803 // we analyze constraints and then, we only check atomic constraints.
25805 cp_parser_requires_clause (cp_parser
*parser
)
25807 // Parse the requires clause so that it is not automatically folded.
25808 ++processing_template_decl
;
25809 tree expr
= cp_parser_binary_expression (parser
, false, false,
25810 PREC_NOT_OPERATOR
, NULL
);
25811 if (check_for_bare_parameter_packs (expr
))
25812 expr
= error_mark_node
;
25813 --processing_template_decl
;
25817 // Optionally parse a requires clause:
25819 cp_parser_requires_clause_opt (cp_parser
*parser
)
25821 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25822 if (tok
->keyword
!= RID_REQUIRES
)
25824 if (!flag_concepts
&& tok
->type
== CPP_NAME
25825 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25827 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25828 "%<requires%> only available with -fconcepts");
25829 /* Parse and discard the requires-clause. */
25830 cp_lexer_consume_token (parser
->lexer
);
25831 cp_parser_requires_clause (parser
);
25835 cp_lexer_consume_token (parser
->lexer
);
25836 return cp_parser_requires_clause (parser
);
25840 /*---------------------------------------------------------------------------
25841 Requires expressions
25842 ---------------------------------------------------------------------------*/
25844 /* Parse a requires expression
25846 requirement-expression:
25847 'requires' requirement-parameter-list [opt] requirement-body */
25849 cp_parser_requires_expression (cp_parser
*parser
)
25851 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25852 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25854 /* A requires-expression shall appear only within a concept
25855 definition or a requires-clause.
25857 TODO: Implement this diagnostic correctly. */
25858 if (!processing_template_decl
)
25860 error_at (loc
, "a requires expression cannot appear outside a template");
25861 cp_parser_skip_to_end_of_statement (parser
);
25862 return error_mark_node
;
25867 /* Local parameters are delared as variables within the scope
25868 of the expression. They are not visible past the end of
25869 the expression. Expressions within the requires-expression
25870 are unevaluated. */
25871 struct scope_sentinel
25875 ++cp_unevaluated_operand
;
25876 begin_scope (sk_block
, NULL_TREE
);
25881 pop_bindings_and_leave_scope ();
25882 --cp_unevaluated_operand
;
25886 /* Parse the optional parameter list. */
25887 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25889 parms
= cp_parser_requirement_parameter_list (parser
);
25890 if (parms
== error_mark_node
)
25891 return error_mark_node
;
25896 /* Parse the requirement body. */
25897 reqs
= cp_parser_requirement_body (parser
);
25898 if (reqs
== error_mark_node
)
25899 return error_mark_node
;
25902 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25904 grokparms (parms
, &parms
);
25905 return finish_requires_expr (parms
, reqs
);
25908 /* Parse a parameterized requirement.
25910 requirement-parameter-list:
25911 '(' parameter-declaration-clause ')' */
25913 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25915 matching_parens parens
;
25916 if (!parens
.require_open (parser
))
25917 return error_mark_node
;
25919 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25921 if (!parens
.require_close (parser
))
25922 return error_mark_node
;
25927 /* Parse the body of a requirement.
25930 '{' requirement-list '}' */
25932 cp_parser_requirement_body (cp_parser
*parser
)
25934 matching_braces braces
;
25935 if (!braces
.require_open (parser
))
25936 return error_mark_node
;
25938 tree reqs
= cp_parser_requirement_list (parser
);
25940 if (!braces
.require_close (parser
))
25941 return error_mark_node
;
25946 /* Parse a list of requirements.
25950 requirement-list ';' requirement[opt] */
25952 cp_parser_requirement_list (cp_parser
*parser
)
25954 tree result
= NULL_TREE
;
25957 tree req
= cp_parser_requirement (parser
);
25958 if (req
== error_mark_node
)
25959 return error_mark_node
;
25961 result
= tree_cons (NULL_TREE
, req
, result
);
25963 /* If we see a semi-colon, consume it. */
25964 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25965 cp_lexer_consume_token (parser
->lexer
);
25967 /* Stop processing at the end of the list. */
25968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25972 /* Reverse the order of requirements so they are analyzed in
25973 declaration order. */
25974 return nreverse (result
);
25977 /* Parse a syntactic requirement or type requirement.
25981 compound-requirement
25983 nested-requirement */
25985 cp_parser_requirement (cp_parser
*parser
)
25987 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25988 return cp_parser_compound_requirement (parser
);
25989 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25990 return cp_parser_type_requirement (parser
);
25991 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25992 return cp_parser_nested_requirement (parser
);
25994 return cp_parser_simple_requirement (parser
);
25997 /* Parse a simple requirement.
25999 simple-requirement:
26002 cp_parser_simple_requirement (cp_parser
*parser
)
26004 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26005 if (!expr
|| expr
== error_mark_node
)
26006 return error_mark_node
;
26008 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26009 return error_mark_node
;
26011 return finish_simple_requirement (expr
);
26014 /* Parse a type requirement
26017 nested-name-specifier [opt] required-type-name ';'
26019 required-type-name:
26021 'template' [opt] simple-template-id */
26023 cp_parser_type_requirement (cp_parser
*parser
)
26025 cp_lexer_consume_token (parser
->lexer
);
26027 // Save the scope before parsing name specifiers.
26028 tree saved_scope
= parser
->scope
;
26029 tree saved_object_scope
= parser
->object_scope
;
26030 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26031 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26032 cp_parser_nested_name_specifier_opt (parser
,
26033 /*typename_keyword_p=*/true,
26034 /*check_dependency_p=*/false,
26036 /*is_declaration=*/false);
26039 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26041 cp_lexer_consume_token (parser
->lexer
);
26042 type
= cp_parser_template_id (parser
,
26043 /*template_keyword_p=*/true,
26044 /*check_dependency=*/false,
26045 /*tag_type=*/none_type
,
26046 /*is_declaration=*/false);
26047 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26048 /*complain=*/tf_error
);
26051 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26053 if (TREE_CODE (type
) == TYPE_DECL
)
26054 type
= TREE_TYPE (type
);
26056 parser
->scope
= saved_scope
;
26057 parser
->object_scope
= saved_object_scope
;
26058 parser
->qualifying_scope
= saved_qualifying_scope
;
26060 if (type
== error_mark_node
)
26061 cp_parser_skip_to_end_of_statement (parser
);
26063 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26064 return error_mark_node
;
26065 if (type
== error_mark_node
)
26066 return error_mark_node
;
26068 return finish_type_requirement (type
);
26071 /* Parse a compound requirement
26073 compound-requirement:
26074 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26076 cp_parser_compound_requirement (cp_parser
*parser
)
26078 /* Parse an expression enclosed in '{ }'s. */
26079 matching_braces braces
;
26080 if (!braces
.require_open (parser
))
26081 return error_mark_node
;
26083 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26084 if (!expr
|| expr
== error_mark_node
)
26085 return error_mark_node
;
26087 if (!braces
.require_close (parser
))
26088 return error_mark_node
;
26090 /* Parse the optional noexcept. */
26091 bool noexcept_p
= false;
26092 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26094 cp_lexer_consume_token (parser
->lexer
);
26098 /* Parse the optional trailing return type. */
26099 tree type
= NULL_TREE
;
26100 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26102 cp_lexer_consume_token (parser
->lexer
);
26103 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26104 parser
->in_result_type_constraint_p
= true;
26105 type
= cp_parser_trailing_type_id (parser
);
26106 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26107 if (type
== error_mark_node
)
26108 return error_mark_node
;
26111 return finish_compound_requirement (expr
, type
, noexcept_p
);
26114 /* Parse a nested requirement. This is the same as a requires clause.
26116 nested-requirement:
26119 cp_parser_nested_requirement (cp_parser
*parser
)
26121 cp_lexer_consume_token (parser
->lexer
);
26122 tree req
= cp_parser_requires_clause (parser
);
26123 if (req
== error_mark_node
)
26124 return error_mark_node
;
26125 return finish_nested_requirement (req
);
26128 /* Support Functions */
26130 /* Return the appropriate prefer_type argument for lookup_name_real based on
26131 tag_type and template_mem_access. */
26134 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26136 /* DR 141: When looking in the current enclosing context for a template-name
26137 after -> or ., only consider class templates. */
26138 if (template_mem_access
)
26142 case none_type
: return 0; // No preference.
26143 case scope_type
: return 1; // Type or namespace.
26144 default: return 2; // Type only.
26148 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26149 NAME should have one of the representations used for an
26150 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26151 is returned. If PARSER->SCOPE is a dependent type, then a
26152 SCOPE_REF is returned.
26154 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26155 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26156 was formed. Abstractly, such entities should not be passed to this
26157 function, because they do not need to be looked up, but it is
26158 simpler to check for this special case here, rather than at the
26161 In cases not explicitly covered above, this function returns a
26162 DECL, OVERLOAD, or baselink representing the result of the lookup.
26163 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26166 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26167 (e.g., "struct") that was used. In that case bindings that do not
26168 refer to types are ignored.
26170 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26173 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26176 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26179 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26180 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26181 NULL_TREE otherwise. */
26184 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26185 enum tag_types tag_type
,
26188 bool check_dependency
,
26189 tree
*ambiguous_decls
,
26190 location_t name_location
)
26193 tree object_type
= parser
->context
->object_type
;
26195 /* Assume that the lookup will be unambiguous. */
26196 if (ambiguous_decls
)
26197 *ambiguous_decls
= NULL_TREE
;
26199 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26200 no longer valid. Note that if we are parsing tentatively, and
26201 the parse fails, OBJECT_TYPE will be automatically restored. */
26202 parser
->context
->object_type
= NULL_TREE
;
26204 if (name
== error_mark_node
)
26205 return error_mark_node
;
26207 /* A template-id has already been resolved; there is no lookup to
26209 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26211 if (BASELINK_P (name
))
26213 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26214 == TEMPLATE_ID_EXPR
);
26218 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26219 it should already have been checked to make sure that the name
26220 used matches the type being destroyed. */
26221 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26225 /* Figure out to which type this destructor applies. */
26227 type
= parser
->scope
;
26228 else if (object_type
)
26229 type
= object_type
;
26231 type
= current_class_type
;
26232 /* If that's not a class type, there is no destructor. */
26233 if (!type
|| !CLASS_TYPE_P (type
))
26234 return error_mark_node
;
26236 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26237 lazily_declare_fn (sfk_destructor
, type
);
26239 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26242 return error_mark_node
;
26245 /* By this point, the NAME should be an ordinary identifier. If
26246 the id-expression was a qualified name, the qualifying scope is
26247 stored in PARSER->SCOPE at this point. */
26248 gcc_assert (identifier_p (name
));
26250 /* Perform the lookup. */
26255 if (parser
->scope
== error_mark_node
)
26256 return error_mark_node
;
26258 /* If the SCOPE is dependent, the lookup must be deferred until
26259 the template is instantiated -- unless we are explicitly
26260 looking up names in uninstantiated templates. Even then, we
26261 cannot look up the name if the scope is not a class type; it
26262 might, for example, be a template type parameter. */
26263 dependent_p
= (TYPE_P (parser
->scope
)
26264 && dependent_scope_p (parser
->scope
));
26265 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26267 /* Defer lookup. */
26268 decl
= error_mark_node
;
26271 tree pushed_scope
= NULL_TREE
;
26273 /* If PARSER->SCOPE is a dependent type, then it must be a
26274 class type, and we must not be checking dependencies;
26275 otherwise, we would have processed this lookup above. So
26276 that PARSER->SCOPE is not considered a dependent base by
26277 lookup_member, we must enter the scope here. */
26279 pushed_scope
= push_scope (parser
->scope
);
26281 /* If the PARSER->SCOPE is a template specialization, it
26282 may be instantiated during name lookup. In that case,
26283 errors may be issued. Even if we rollback the current
26284 tentative parse, those errors are valid. */
26285 decl
= lookup_qualified_name (parser
->scope
, name
,
26286 prefer_type_arg (tag_type
),
26287 /*complain=*/true);
26289 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26290 lookup result and the nested-name-specifier nominates a class C:
26291 * if the name specified after the nested-name-specifier, when
26292 looked up in C, is the injected-class-name of C (Clause 9), or
26293 * if the name specified after the nested-name-specifier is the
26294 same as the identifier or the simple-template-id's template-
26295 name in the last component of the nested-name-specifier,
26296 the name is instead considered to name the constructor of
26297 class C. [ Note: for example, the constructor is not an
26298 acceptable lookup result in an elaborated-type-specifier so
26299 the constructor would not be used in place of the
26300 injected-class-name. --end note ] Such a constructor name
26301 shall be used only in the declarator-id of a declaration that
26302 names a constructor or in a using-declaration. */
26303 if (tag_type
== none_type
26304 && DECL_SELF_REFERENCE_P (decl
)
26305 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26306 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26307 prefer_type_arg (tag_type
),
26308 /*complain=*/true);
26310 /* If we have a single function from a using decl, pull it out. */
26311 if (TREE_CODE (decl
) == OVERLOAD
26312 && !really_overloaded_fn (decl
))
26313 decl
= OVL_FUNCTION (decl
);
26316 pop_scope (pushed_scope
);
26319 /* If the scope is a dependent type and either we deferred lookup or
26320 we did lookup but didn't find the name, rememeber the name. */
26321 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26322 && dependent_type_p (parser
->scope
))
26328 /* The resolution to Core Issue 180 says that `struct
26329 A::B' should be considered a type-name, even if `A'
26331 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26332 /*complain=*/tf_error
);
26333 if (type
!= error_mark_node
)
26334 decl
= TYPE_NAME (type
);
26336 else if (is_template
26337 && (cp_parser_next_token_ends_template_argument_p (parser
)
26338 || cp_lexer_next_token_is (parser
->lexer
,
26340 decl
= make_unbound_class_template (parser
->scope
,
26342 /*complain=*/tf_error
);
26344 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26345 parser
->scope
, name
,
26348 parser
->qualifying_scope
= parser
->scope
;
26349 parser
->object_scope
= NULL_TREE
;
26351 else if (object_type
)
26353 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26354 OBJECT_TYPE is not a class. */
26355 if (CLASS_TYPE_P (object_type
))
26356 /* If the OBJECT_TYPE is a template specialization, it may
26357 be instantiated during name lookup. In that case, errors
26358 may be issued. Even if we rollback the current tentative
26359 parse, those errors are valid. */
26360 decl
= lookup_member (object_type
,
26363 prefer_type_arg (tag_type
),
26364 tf_warning_or_error
);
26369 /* Look it up in the enclosing context. DR 141: When looking for a
26370 template-name after -> or ., only consider class templates. */
26371 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26373 /*block_p=*/true, is_namespace
, 0);
26374 if (object_type
== unknown_type_node
)
26375 /* The object is type-dependent, so we can't look anything up; we used
26376 this to get the DR 141 behavior. */
26377 object_type
= NULL_TREE
;
26378 parser
->object_scope
= object_type
;
26379 parser
->qualifying_scope
= NULL_TREE
;
26383 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26385 /*block_p=*/true, is_namespace
, 0);
26386 parser
->qualifying_scope
= NULL_TREE
;
26387 parser
->object_scope
= NULL_TREE
;
26390 /* If the lookup failed, let our caller know. */
26391 if (!decl
|| decl
== error_mark_node
)
26392 return error_mark_node
;
26394 /* Pull out the template from an injected-class-name (or multiple). */
26396 decl
= maybe_get_template_decl_from_type_decl (decl
);
26398 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26399 if (TREE_CODE (decl
) == TREE_LIST
)
26401 if (ambiguous_decls
)
26402 *ambiguous_decls
= decl
;
26403 /* The error message we have to print is too complicated for
26404 cp_parser_error, so we incorporate its actions directly. */
26405 if (!cp_parser_simulate_error (parser
))
26407 error_at (name_location
, "reference to %qD is ambiguous",
26409 print_candidates (decl
);
26411 return error_mark_node
;
26414 gcc_assert (DECL_P (decl
)
26415 || TREE_CODE (decl
) == OVERLOAD
26416 || TREE_CODE (decl
) == SCOPE_REF
26417 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26418 || BASELINK_P (decl
));
26420 /* If we have resolved the name of a member declaration, check to
26421 see if the declaration is accessible. When the name resolves to
26422 set of overloaded functions, accessibility is checked when
26423 overload resolution is done.
26425 During an explicit instantiation, access is not checked at all,
26426 as per [temp.explicit]. */
26428 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26430 maybe_record_typedef_use (decl
);
26432 return cp_expr (decl
, name_location
);
26435 /* Like cp_parser_lookup_name, but for use in the typical case where
26436 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26437 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26440 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26442 return cp_parser_lookup_name (parser
, name
,
26444 /*is_template=*/false,
26445 /*is_namespace=*/false,
26446 /*check_dependency=*/true,
26447 /*ambiguous_decls=*/NULL
,
26451 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26452 the current context, return the TYPE_DECL. If TAG_NAME_P is
26453 true, the DECL indicates the class being defined in a class-head,
26454 or declared in an elaborated-type-specifier.
26456 Otherwise, return DECL. */
26459 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26461 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26462 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26465 template <typename T> struct B;
26468 template <typename T> struct A::B {};
26470 Similarly, in an elaborated-type-specifier:
26472 namespace N { struct X{}; }
26475 template <typename T> friend struct N::X;
26478 However, if the DECL refers to a class type, and we are in
26479 the scope of the class, then the name lookup automatically
26480 finds the TYPE_DECL created by build_self_reference rather
26481 than a TEMPLATE_DECL. For example, in:
26483 template <class T> struct S {
26487 there is no need to handle such case. */
26489 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26490 return DECL_TEMPLATE_RESULT (decl
);
26495 /* If too many, or too few, template-parameter lists apply to the
26496 declarator, issue an error message. Returns TRUE if all went well,
26497 and FALSE otherwise. */
26500 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26501 cp_declarator
*declarator
,
26502 location_t declarator_location
)
26504 switch (declarator
->kind
)
26508 unsigned num_templates
= 0;
26509 tree scope
= declarator
->u
.id
.qualifying_scope
;
26510 bool template_id_p
= false;
26513 num_templates
= num_template_headers_for_class (scope
);
26514 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26515 == TEMPLATE_ID_EXPR
)
26517 /* If the DECLARATOR has the form `X<y>' then it uses one
26518 additional level of template parameters. */
26520 template_id_p
= true;
26523 return cp_parser_check_template_parameters
26524 (parser
, num_templates
, template_id_p
, declarator_location
,
26531 case cdk_reference
:
26533 return (cp_parser_check_declarator_template_parameters
26534 (parser
, declarator
->declarator
, declarator_location
));
26541 gcc_unreachable ();
26546 /* NUM_TEMPLATES were used in the current declaration. If that is
26547 invalid, return FALSE and issue an error messages. Otherwise,
26548 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26549 declarator and we can print more accurate diagnostics. */
26552 cp_parser_check_template_parameters (cp_parser
* parser
,
26553 unsigned num_templates
,
26554 bool template_id_p
,
26555 location_t location
,
26556 cp_declarator
*declarator
)
26558 /* If there are the same number of template classes and parameter
26559 lists, that's OK. */
26560 if (parser
->num_template_parameter_lists
== num_templates
)
26562 /* If there are more, but only one more, and the name ends in an identifier,
26563 then we are declaring a primary template. That's OK too. */
26565 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26567 /* If there are more template classes than parameter lists, we have
26570 template <class T> void S<T>::R<T>::f (); */
26571 if (parser
->num_template_parameter_lists
< num_templates
)
26573 if (declarator
&& !current_function_decl
)
26574 error_at (location
, "specializing member %<%T::%E%> "
26575 "requires %<template<>%> syntax",
26576 declarator
->u
.id
.qualifying_scope
,
26577 declarator
->u
.id
.unqualified_name
);
26578 else if (declarator
)
26579 error_at (location
, "invalid declaration of %<%T::%E%>",
26580 declarator
->u
.id
.qualifying_scope
,
26581 declarator
->u
.id
.unqualified_name
);
26583 error_at (location
, "too few template-parameter-lists");
26586 /* Otherwise, there are too many template parameter lists. We have
26589 template <class T> template <class U> void S::f(); */
26590 error_at (location
, "too many template-parameter-lists");
26594 /* Parse an optional `::' token indicating that the following name is
26595 from the global namespace. If so, PARSER->SCOPE is set to the
26596 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26597 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26598 Returns the new value of PARSER->SCOPE, if the `::' token is
26599 present, and NULL_TREE otherwise. */
26602 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26606 /* Peek at the next token. */
26607 token
= cp_lexer_peek_token (parser
->lexer
);
26608 /* If we're looking at a `::' token then we're starting from the
26609 global namespace, not our current location. */
26610 if (token
->type
== CPP_SCOPE
)
26612 /* Consume the `::' token. */
26613 cp_lexer_consume_token (parser
->lexer
);
26614 /* Set the SCOPE so that we know where to start the lookup. */
26615 parser
->scope
= global_namespace
;
26616 parser
->qualifying_scope
= global_namespace
;
26617 parser
->object_scope
= NULL_TREE
;
26619 return parser
->scope
;
26621 else if (!current_scope_valid_p
)
26623 parser
->scope
= NULL_TREE
;
26624 parser
->qualifying_scope
= NULL_TREE
;
26625 parser
->object_scope
= NULL_TREE
;
26631 /* Returns TRUE if the upcoming token sequence is the start of a
26632 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26633 declarator is preceded by the `friend' specifier. */
26636 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26638 bool constructor_p
;
26639 bool outside_class_specifier_p
;
26640 tree nested_name_specifier
;
26641 cp_token
*next_token
;
26643 /* The common case is that this is not a constructor declarator, so
26644 try to avoid doing lots of work if at all possible. It's not
26645 valid declare a constructor at function scope. */
26646 if (parser
->in_function_body
)
26648 /* And only certain tokens can begin a constructor declarator. */
26649 next_token
= cp_lexer_peek_token (parser
->lexer
);
26650 if (next_token
->type
!= CPP_NAME
26651 && next_token
->type
!= CPP_SCOPE
26652 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26653 && next_token
->type
!= CPP_TEMPLATE_ID
)
26656 /* Parse tentatively; we are going to roll back all of the tokens
26658 cp_parser_parse_tentatively (parser
);
26659 /* Assume that we are looking at a constructor declarator. */
26660 constructor_p
= true;
26662 /* Look for the optional `::' operator. */
26663 cp_parser_global_scope_opt (parser
,
26664 /*current_scope_valid_p=*/false);
26665 /* Look for the nested-name-specifier. */
26666 nested_name_specifier
26667 = (cp_parser_nested_name_specifier_opt (parser
,
26668 /*typename_keyword_p=*/false,
26669 /*check_dependency_p=*/false,
26671 /*is_declaration=*/false));
26673 outside_class_specifier_p
= (!at_class_scope_p ()
26674 || !TYPE_BEING_DEFINED (current_class_type
)
26677 /* Outside of a class-specifier, there must be a
26678 nested-name-specifier. Except in C++17 mode, where we
26679 might be declaring a guiding declaration. */
26680 if (!nested_name_specifier
&& outside_class_specifier_p
26681 && cxx_dialect
< cxx17
)
26682 constructor_p
= false;
26683 else if (nested_name_specifier
== error_mark_node
)
26684 constructor_p
= false;
26686 /* If we have a class scope, this is easy; DR 147 says that S::S always
26687 names the constructor, and no other qualified name could. */
26688 if (constructor_p
&& nested_name_specifier
26689 && CLASS_TYPE_P (nested_name_specifier
))
26691 tree id
= cp_parser_unqualified_id (parser
,
26692 /*template_keyword_p=*/false,
26693 /*check_dependency_p=*/false,
26694 /*declarator_p=*/true,
26695 /*optional_p=*/false);
26696 if (is_overloaded_fn (id
))
26697 id
= DECL_NAME (get_first_fn (id
));
26698 if (!constructor_name_p (id
, nested_name_specifier
))
26699 constructor_p
= false;
26701 /* If we still think that this might be a constructor-declarator,
26702 look for a class-name. */
26703 else if (constructor_p
)
26707 template <typename T> struct S {
26711 we must recognize that the nested `S' names a class. */
26712 if (cxx_dialect
>= cxx17
)
26713 cp_parser_parse_tentatively (parser
);
26716 type_decl
= cp_parser_class_name (parser
,
26717 /*typename_keyword_p=*/false,
26718 /*template_keyword_p=*/false,
26720 /*check_dependency_p=*/false,
26721 /*class_head_p=*/false,
26722 /*is_declaration=*/false);
26724 if (cxx_dialect
>= cxx17
26725 && !cp_parser_parse_definitely (parser
))
26727 type_decl
= NULL_TREE
;
26728 tree tmpl
= cp_parser_template_name (parser
,
26729 /*template_keyword*/false,
26730 /*check_dependency_p*/false,
26731 /*is_declaration*/false,
26733 /*is_identifier*/NULL
);
26734 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26735 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26736 /* It's a deduction guide, return true. */;
26738 cp_parser_simulate_error (parser
);
26741 /* If there was no class-name, then this is not a constructor.
26742 Otherwise, if we are in a class-specifier and we aren't
26743 handling a friend declaration, check that its type matches
26744 current_class_type (c++/38313). Note: error_mark_node
26745 is left alone for error recovery purposes. */
26746 constructor_p
= (!cp_parser_error_occurred (parser
)
26747 && (outside_class_specifier_p
26748 || type_decl
== NULL_TREE
26749 || type_decl
== error_mark_node
26750 || same_type_p (current_class_type
,
26751 TREE_TYPE (type_decl
))));
26753 /* If we're still considering a constructor, we have to see a `(',
26754 to begin the parameter-declaration-clause, followed by either a
26755 `)', an `...', or a decl-specifier. We need to check for a
26756 type-specifier to avoid being fooled into thinking that:
26760 is a constructor. (It is actually a function named `f' that
26761 takes one parameter (of type `int') and returns a value of type
26764 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26765 constructor_p
= false;
26768 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26769 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26770 /* A parameter declaration begins with a decl-specifier,
26771 which is either the "attribute" keyword, a storage class
26772 specifier, or (usually) a type-specifier. */
26773 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26776 tree pushed_scope
= NULL_TREE
;
26777 unsigned saved_num_template_parameter_lists
;
26779 /* Names appearing in the type-specifier should be looked up
26780 in the scope of the class. */
26781 if (current_class_type
)
26783 else if (type_decl
)
26785 type
= TREE_TYPE (type_decl
);
26786 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26788 type
= resolve_typename_type (type
,
26789 /*only_current_p=*/false);
26790 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26792 cp_parser_abort_tentative_parse (parser
);
26796 pushed_scope
= push_scope (type
);
26799 /* Inside the constructor parameter list, surrounding
26800 template-parameter-lists do not apply. */
26801 saved_num_template_parameter_lists
26802 = parser
->num_template_parameter_lists
;
26803 parser
->num_template_parameter_lists
= 0;
26805 /* Look for the type-specifier. */
26806 cp_parser_type_specifier (parser
,
26807 CP_PARSER_FLAGS_NONE
,
26808 /*decl_specs=*/NULL
,
26809 /*is_declarator=*/true,
26810 /*declares_class_or_enum=*/NULL
,
26811 /*is_cv_qualifier=*/NULL
);
26813 parser
->num_template_parameter_lists
26814 = saved_num_template_parameter_lists
;
26816 /* Leave the scope of the class. */
26818 pop_scope (pushed_scope
);
26820 constructor_p
= !cp_parser_error_occurred (parser
);
26824 /* We did not really want to consume any tokens. */
26825 cp_parser_abort_tentative_parse (parser
);
26827 return constructor_p
;
26830 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26831 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26832 they must be performed once we are in the scope of the function.
26834 Returns the function defined. */
26837 cp_parser_function_definition_from_specifiers_and_declarator
26838 (cp_parser
* parser
,
26839 cp_decl_specifier_seq
*decl_specifiers
,
26841 const cp_declarator
*declarator
)
26846 /* Begin the function-definition. */
26847 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26849 /* The things we're about to see are not directly qualified by any
26850 template headers we've seen thus far. */
26851 reset_specialization ();
26853 /* If there were names looked up in the decl-specifier-seq that we
26854 did not check, check them now. We must wait until we are in the
26855 scope of the function to perform the checks, since the function
26856 might be a friend. */
26857 perform_deferred_access_checks (tf_warning_or_error
);
26861 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26862 parser
->omp_declare_simd
= NULL
;
26863 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26864 parser
->oacc_routine
= NULL
;
26869 /* Skip the entire function. */
26870 cp_parser_skip_to_end_of_block_or_statement (parser
);
26871 fn
= error_mark_node
;
26873 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26875 /* Seen already, skip it. An error message has already been output. */
26876 cp_parser_skip_to_end_of_block_or_statement (parser
);
26877 fn
= current_function_decl
;
26878 current_function_decl
= NULL_TREE
;
26879 /* If this is a function from a class, pop the nested class. */
26880 if (current_class_name
)
26881 pop_nested_class ();
26886 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26887 tv
= TV_PARSE_INLINE
;
26889 tv
= TV_PARSE_FUNC
;
26891 fn
= cp_parser_function_definition_after_declarator (parser
,
26892 /*inline_p=*/false);
26899 /* Parse the part of a function-definition that follows the
26900 declarator. INLINE_P is TRUE iff this function is an inline
26901 function defined within a class-specifier.
26903 Returns the function defined. */
26906 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26910 bool saved_in_unbraced_linkage_specification_p
;
26911 bool saved_in_function_body
;
26912 unsigned saved_num_template_parameter_lists
;
26914 bool fully_implicit_function_template_p
26915 = parser
->fully_implicit_function_template_p
;
26916 parser
->fully_implicit_function_template_p
= false;
26917 tree implicit_template_parms
26918 = parser
->implicit_template_parms
;
26919 parser
->implicit_template_parms
= 0;
26920 cp_binding_level
* implicit_template_scope
26921 = parser
->implicit_template_scope
;
26922 parser
->implicit_template_scope
= 0;
26924 saved_in_function_body
= parser
->in_function_body
;
26925 parser
->in_function_body
= true;
26926 /* If the next token is `return', then the code may be trying to
26927 make use of the "named return value" extension that G++ used to
26929 token
= cp_lexer_peek_token (parser
->lexer
);
26930 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26932 /* Consume the `return' keyword. */
26933 cp_lexer_consume_token (parser
->lexer
);
26934 /* Look for the identifier that indicates what value is to be
26936 cp_parser_identifier (parser
);
26937 /* Issue an error message. */
26938 error_at (token
->location
,
26939 "named return values are no longer supported");
26940 /* Skip tokens until we reach the start of the function body. */
26943 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26944 if (token
->type
== CPP_OPEN_BRACE
26945 || token
->type
== CPP_EOF
26946 || token
->type
== CPP_PRAGMA_EOL
)
26948 cp_lexer_consume_token (parser
->lexer
);
26951 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26952 anything declared inside `f'. */
26953 saved_in_unbraced_linkage_specification_p
26954 = parser
->in_unbraced_linkage_specification_p
;
26955 parser
->in_unbraced_linkage_specification_p
= false;
26956 /* Inside the function, surrounding template-parameter-lists do not
26958 saved_num_template_parameter_lists
26959 = parser
->num_template_parameter_lists
;
26960 parser
->num_template_parameter_lists
= 0;
26962 /* If the next token is `try', `__transaction_atomic', or
26963 `__transaction_relaxed`, then we are looking at either function-try-block
26964 or function-transaction-block. Note that all of these include the
26966 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26967 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26968 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26969 RID_TRANSACTION_RELAXED
))
26970 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26971 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26972 cp_parser_function_try_block (parser
);
26974 cp_parser_ctor_initializer_opt_and_function_body
26975 (parser
, /*in_function_try_block=*/false);
26977 /* Finish the function. */
26978 fn
= finish_function (inline_p
);
26979 /* Generate code for it, if necessary. */
26980 expand_or_defer_fn (fn
);
26981 /* Restore the saved values. */
26982 parser
->in_unbraced_linkage_specification_p
26983 = saved_in_unbraced_linkage_specification_p
;
26984 parser
->num_template_parameter_lists
26985 = saved_num_template_parameter_lists
;
26986 parser
->in_function_body
= saved_in_function_body
;
26988 parser
->fully_implicit_function_template_p
26989 = fully_implicit_function_template_p
;
26990 parser
->implicit_template_parms
26991 = implicit_template_parms
;
26992 parser
->implicit_template_scope
26993 = implicit_template_scope
;
26995 if (parser
->fully_implicit_function_template_p
)
26996 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27001 /* Parse a template-declaration body (following argument list). */
27004 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27005 tree parameter_list
,
27008 tree decl
= NULL_TREE
;
27009 bool friend_p
= false;
27011 /* We just processed one more parameter list. */
27012 ++parser
->num_template_parameter_lists
;
27014 /* Get the deferred access checks from the parameter list. These
27015 will be checked once we know what is being declared, as for a
27016 member template the checks must be performed in the scope of the
27017 class containing the member. */
27018 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27020 /* Tentatively parse for a new template parameter list, which can either be
27021 the template keyword or a template introduction. */
27022 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27024 else if (cxx_dialect
>= cxx11
27025 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27026 decl
= cp_parser_alias_declaration (parser
);
27029 /* There are no access checks when parsing a template, as we do not
27030 know if a specialization will be a friend. */
27031 push_deferring_access_checks (dk_no_check
);
27032 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27033 decl
= cp_parser_single_declaration (parser
,
27036 /*explicit_specialization_p=*/false,
27038 pop_deferring_access_checks ();
27040 /* If this is a member template declaration, let the front
27042 if (member_p
&& !friend_p
&& decl
)
27044 if (TREE_CODE (decl
) == TYPE_DECL
)
27045 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27047 decl
= finish_member_template_decl (decl
);
27049 else if (friend_p
&& decl
27050 && DECL_DECLARES_TYPE_P (decl
))
27051 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27052 /*complain=*/true);
27054 /* We are done with the current parameter list. */
27055 --parser
->num_template_parameter_lists
;
27057 pop_deferring_access_checks ();
27060 finish_template_decl (parameter_list
);
27062 /* Check the template arguments for a literal operator template. */
27064 && DECL_DECLARES_FUNCTION_P (decl
)
27065 && UDLIT_OPER_P (DECL_NAME (decl
)))
27068 if (parameter_list
== NULL_TREE
)
27072 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27073 if (num_parms
== 1)
27075 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27076 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27077 if (TREE_TYPE (parm
) != char_type_node
27078 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27081 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27083 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27084 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27085 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27086 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27087 if (parm
== error_mark_node
27088 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27089 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27097 if (cxx_dialect
>= cxx14
)
27098 error ("literal operator template %qD has invalid parameter list."
27099 " Expected non-type template argument pack <char...>"
27100 " or <typename CharT, CharT...>",
27103 error ("literal operator template %qD has invalid parameter list."
27104 " Expected non-type template argument pack <char...>",
27109 /* Register member declarations. */
27110 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27111 finish_member_declaration (decl
);
27112 /* If DECL is a function template, we must return to parse it later.
27113 (Even though there is no definition, there might be default
27114 arguments that need handling.) */
27115 if (member_p
&& decl
27116 && DECL_DECLARES_FUNCTION_P (decl
))
27117 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27120 /* Parse a template introduction header for a template-declaration. Returns
27121 false if tentative parse fails. */
27124 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27126 cp_parser_parse_tentatively (parser
);
27128 tree saved_scope
= parser
->scope
;
27129 tree saved_object_scope
= parser
->object_scope
;
27130 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27132 /* Look for the optional `::' operator. */
27133 cp_parser_global_scope_opt (parser
,
27134 /*current_scope_valid_p=*/false);
27135 /* Look for the nested-name-specifier. */
27136 cp_parser_nested_name_specifier_opt (parser
,
27137 /*typename_keyword_p=*/false,
27138 /*check_dependency_p=*/true,
27140 /*is_declaration=*/false);
27142 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27143 tree concept_name
= cp_parser_identifier (parser
);
27145 /* Look up the concept for which we will be matching
27146 template parameters. */
27147 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27149 parser
->scope
= saved_scope
;
27150 parser
->object_scope
= saved_object_scope
;
27151 parser
->qualifying_scope
= saved_qualifying_scope
;
27153 if (concept_name
== error_mark_node
)
27154 cp_parser_simulate_error (parser
);
27156 /* Look for opening brace for introduction. */
27157 matching_braces braces
;
27158 braces
.require_open (parser
);
27160 if (!cp_parser_parse_definitely (parser
))
27163 push_deferring_access_checks (dk_deferred
);
27165 /* Build vector of placeholder parameters and grab
27166 matching identifiers. */
27167 tree introduction_list
= cp_parser_introduction_list (parser
);
27169 /* Look for closing brace for introduction. */
27170 if (!braces
.require_close (parser
))
27173 /* The introduction-list shall not be empty. */
27174 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27177 /* In cp_parser_introduction_list we have already issued an error. */
27181 if (tmpl_decl
== error_mark_node
)
27183 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27188 /* Build and associate the constraint. */
27189 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27190 if (parms
&& parms
!= error_mark_node
)
27192 cp_parser_template_declaration_after_parameters (parser
, parms
,
27197 error_at (token
->location
, "no matching concept for template-introduction");
27201 /* Parse a normal template-declaration following the template keyword. */
27204 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27206 tree parameter_list
;
27207 bool need_lang_pop
;
27208 location_t location
= input_location
;
27210 /* Look for the `<' token. */
27211 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27213 if (at_class_scope_p () && current_function_decl
)
27215 /* 14.5.2.2 [temp.mem]
27217 A local class shall not have member templates. */
27218 error_at (location
,
27219 "invalid declaration of member template in local class");
27220 cp_parser_skip_to_end_of_block_or_statement (parser
);
27225 A template ... shall not have C linkage. */
27226 if (current_lang_name
== lang_name_c
)
27228 error_at (location
, "template with C linkage");
27229 maybe_show_extern_c_location ();
27230 /* Give it C++ linkage to avoid confusing other parts of the
27232 push_lang_context (lang_name_cplusplus
);
27233 need_lang_pop
= true;
27236 need_lang_pop
= false;
27238 /* We cannot perform access checks on the template parameter
27239 declarations until we know what is being declared, just as we
27240 cannot check the decl-specifier list. */
27241 push_deferring_access_checks (dk_deferred
);
27243 /* If the next token is `>', then we have an invalid
27244 specialization. Rather than complain about an invalid template
27245 parameter, issue an error message here. */
27246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27248 cp_parser_error (parser
, "invalid explicit specialization");
27249 begin_specialization ();
27250 parameter_list
= NULL_TREE
;
27254 /* Parse the template parameters. */
27255 parameter_list
= cp_parser_template_parameter_list (parser
);
27258 /* Look for the `>'. */
27259 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27261 /* Manage template requirements */
27264 tree reqs
= get_shorthand_constraints (current_template_parms
);
27265 if (tree r
= cp_parser_requires_clause_opt (parser
))
27266 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27267 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27270 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27273 /* For the erroneous case of a template with C linkage, we pushed an
27274 implicit C++ linkage scope; exit that scope now. */
27276 pop_lang_context ();
27279 /* Parse a template-declaration, assuming that the `export' (and
27280 `extern') keywords, if present, has already been scanned. MEMBER_P
27281 is as for cp_parser_template_declaration. */
27284 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27286 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27288 cp_lexer_consume_token (parser
->lexer
);
27289 cp_parser_explicit_template_declaration (parser
, member_p
);
27292 else if (flag_concepts
)
27293 return cp_parser_template_introduction (parser
, member_p
);
27298 /* Perform the deferred access checks from a template-parameter-list.
27299 CHECKS is a TREE_LIST of access checks, as returned by
27300 get_deferred_access_checks. */
27303 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27305 ++processing_template_parmlist
;
27306 perform_access_checks (checks
, tf_warning_or_error
);
27307 --processing_template_parmlist
;
27310 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27311 `function-definition' sequence that follows a template header.
27312 If MEMBER_P is true, this declaration appears in a class scope.
27314 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27315 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27318 cp_parser_single_declaration (cp_parser
* parser
,
27319 vec
<deferred_access_check
, va_gc
> *checks
,
27321 bool explicit_specialization_p
,
27324 int declares_class_or_enum
;
27325 tree decl
= NULL_TREE
;
27326 cp_decl_specifier_seq decl_specifiers
;
27327 bool function_definition_p
= false;
27328 cp_token
*decl_spec_token_start
;
27330 /* This function is only used when processing a template
27332 gcc_assert (innermost_scope_kind () == sk_template_parms
27333 || innermost_scope_kind () == sk_template_spec
);
27335 /* Defer access checks until we know what is being declared. */
27336 push_deferring_access_checks (dk_deferred
);
27338 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27340 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27341 cp_parser_decl_specifier_seq (parser
,
27342 CP_PARSER_FLAGS_OPTIONAL
,
27344 &declares_class_or_enum
);
27346 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27348 /* There are no template typedefs. */
27349 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27351 error_at (decl_spec_token_start
->location
,
27352 "template declaration of %<typedef%>");
27353 decl
= error_mark_node
;
27356 /* Gather up the access checks that occurred the
27357 decl-specifier-seq. */
27358 stop_deferring_access_checks ();
27360 /* Check for the declaration of a template class. */
27361 if (declares_class_or_enum
)
27363 if (cp_parser_declares_only_class_p (parser
)
27364 || (declares_class_or_enum
& 2))
27366 // If this is a declaration, but not a definition, associate
27367 // any constraints with the type declaration. Constraints
27368 // are associated with definitions in cp_parser_class_specifier.
27369 if (declares_class_or_enum
== 1)
27370 associate_classtype_constraints (decl_specifiers
.type
);
27372 decl
= shadow_tag (&decl_specifiers
);
27377 friend template <typename T> struct A<T>::B;
27380 A<T>::B will be represented by a TYPENAME_TYPE, and
27381 therefore not recognized by shadow_tag. */
27382 if (friend_p
&& *friend_p
27384 && decl_specifiers
.type
27385 && TYPE_P (decl_specifiers
.type
))
27386 decl
= decl_specifiers
.type
;
27388 if (decl
&& decl
!= error_mark_node
)
27389 decl
= TYPE_NAME (decl
);
27391 decl
= error_mark_node
;
27393 /* Perform access checks for template parameters. */
27394 cp_parser_perform_template_parameter_access_checks (checks
);
27396 /* Give a helpful diagnostic for
27397 template <class T> struct A { } a;
27398 if we aren't already recovering from an error. */
27399 if (!cp_parser_declares_only_class_p (parser
)
27402 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27403 "a class template declaration must not declare "
27405 cp_parser_skip_to_end_of_block_or_statement (parser
);
27411 /* Complain about missing 'typename' or other invalid type names. */
27412 if (!decl_specifiers
.any_type_specifiers_p
27413 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27415 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27416 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27417 the rest of this declaration. */
27418 decl
= error_mark_node
;
27422 /* If it's not a template class, try for a template function. If
27423 the next token is a `;', then this declaration does not declare
27424 anything. But, if there were errors in the decl-specifiers, then
27425 the error might well have come from an attempted class-specifier.
27426 In that case, there's no need to warn about a missing declarator. */
27428 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27429 || decl_specifiers
.type
!= error_mark_node
))
27431 decl
= cp_parser_init_declarator (parser
,
27434 /*function_definition_allowed_p=*/true,
27436 declares_class_or_enum
,
27437 &function_definition_p
,
27440 /* 7.1.1-1 [dcl.stc]
27442 A storage-class-specifier shall not be specified in an explicit
27443 specialization... */
27445 && explicit_specialization_p
27446 && decl_specifiers
.storage_class
!= sc_none
)
27448 error_at (decl_spec_token_start
->location
,
27449 "explicit template specialization cannot have a storage class");
27450 decl
= error_mark_node
;
27453 if (decl
&& VAR_P (decl
))
27454 check_template_variable (decl
);
27457 /* Look for a trailing `;' after the declaration. */
27458 if (!function_definition_p
27459 && (decl
== error_mark_node
27460 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27461 cp_parser_skip_to_end_of_block_or_statement (parser
);
27464 pop_deferring_access_checks ();
27466 /* Clear any current qualification; whatever comes next is the start
27467 of something new. */
27468 parser
->scope
= NULL_TREE
;
27469 parser
->qualifying_scope
= NULL_TREE
;
27470 parser
->object_scope
= NULL_TREE
;
27475 /* Parse a cast-expression that is not the operand of a unary "&". */
27478 cp_parser_simple_cast_expression (cp_parser
*parser
)
27480 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27481 /*cast_p=*/false, /*decltype*/false, NULL
);
27484 /* Parse a functional cast to TYPE. Returns an expression
27485 representing the cast. */
27488 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27490 vec
<tree
, va_gc
> *vec
;
27491 tree expression_list
;
27495 location_t start_loc
= input_location
;
27498 type
= error_mark_node
;
27500 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27502 cp_lexer_set_source_position (parser
->lexer
);
27503 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27504 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27505 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27506 if (TREE_CODE (type
) == TYPE_DECL
)
27507 type
= TREE_TYPE (type
);
27509 cast
= finish_compound_literal (type
, expression_list
,
27510 tf_warning_or_error
, fcl_functional
);
27511 /* Create a location of the form:
27514 with caret == start at the start of the type name,
27515 finishing at the closing brace. */
27516 location_t finish_loc
27517 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27518 location_t combined_loc
= make_location (start_loc
, start_loc
,
27520 cast
.set_location (combined_loc
);
27525 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27527 /*allow_expansion_p=*/true,
27528 /*non_constant_p=*/NULL
);
27530 expression_list
= error_mark_node
;
27533 expression_list
= build_tree_list_vec (vec
);
27534 release_tree_vector (vec
);
27537 cast
= build_functional_cast (type
, expression_list
,
27538 tf_warning_or_error
);
27539 /* [expr.const]/1: In an integral constant expression "only type
27540 conversions to integral or enumeration type can be used". */
27541 if (TREE_CODE (type
) == TYPE_DECL
)
27542 type
= TREE_TYPE (type
);
27543 if (cast
!= error_mark_node
27544 && !cast_valid_in_integral_constant_expression_p (type
)
27545 && cp_parser_non_integral_constant_expression (parser
,
27547 return error_mark_node
;
27549 /* Create a location of the form:
27552 with caret == start at the start of the type name,
27553 finishing at the closing paren. */
27554 location_t finish_loc
27555 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27556 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27557 cast
.set_location (combined_loc
);
27561 /* Save the tokens that make up the body of a member function defined
27562 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27563 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27564 specifiers applied to the declaration. Returns the FUNCTION_DECL
27565 for the member function. */
27568 cp_parser_save_member_function_body (cp_parser
* parser
,
27569 cp_decl_specifier_seq
*decl_specifiers
,
27570 cp_declarator
*declarator
,
27576 bool function_try_block
= false;
27578 /* Create the FUNCTION_DECL. */
27579 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27580 cp_finalize_omp_declare_simd (parser
, fn
);
27581 cp_finalize_oacc_routine (parser
, fn
, true);
27582 /* If something went badly wrong, bail out now. */
27583 if (fn
== error_mark_node
)
27585 /* If there's a function-body, skip it. */
27586 if (cp_parser_token_starts_function_definition_p
27587 (cp_lexer_peek_token (parser
->lexer
)))
27588 cp_parser_skip_to_end_of_block_or_statement (parser
);
27589 return error_mark_node
;
27592 /* Remember it, if there default args to post process. */
27593 cp_parser_save_default_args (parser
, fn
);
27595 /* Save away the tokens that make up the body of the
27597 first
= parser
->lexer
->next_token
;
27599 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27600 cp_lexer_consume_token (parser
->lexer
);
27601 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27602 RID_TRANSACTION_ATOMIC
))
27604 cp_lexer_consume_token (parser
->lexer
);
27605 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27606 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27607 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27608 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27609 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27610 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27611 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27613 cp_lexer_consume_token (parser
->lexer
);
27614 cp_lexer_consume_token (parser
->lexer
);
27615 cp_lexer_consume_token (parser
->lexer
);
27616 cp_lexer_consume_token (parser
->lexer
);
27617 cp_lexer_consume_token (parser
->lexer
);
27620 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27621 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27623 cp_lexer_consume_token (parser
->lexer
);
27624 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27629 /* Handle function try blocks. */
27630 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27632 cp_lexer_consume_token (parser
->lexer
);
27633 function_try_block
= true;
27635 /* We can have braced-init-list mem-initializers before the fn body. */
27636 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27638 cp_lexer_consume_token (parser
->lexer
);
27639 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27641 /* cache_group will stop after an un-nested { } pair, too. */
27642 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27645 /* variadic mem-inits have ... after the ')'. */
27646 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27647 cp_lexer_consume_token (parser
->lexer
);
27650 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27651 /* Handle function try blocks. */
27652 if (function_try_block
)
27653 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27654 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27655 last
= parser
->lexer
->next_token
;
27657 /* Save away the inline definition; we will process it when the
27658 class is complete. */
27659 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27660 DECL_PENDING_INLINE_P (fn
) = 1;
27662 /* We need to know that this was defined in the class, so that
27663 friend templates are handled correctly. */
27664 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27666 /* Add FN to the queue of functions to be parsed later. */
27667 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27672 /* Save the tokens that make up the in-class initializer for a non-static
27673 data member. Returns a DEFAULT_ARG. */
27676 cp_parser_save_nsdmi (cp_parser
* parser
)
27678 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27681 /* Parse a template-argument-list, as well as the trailing ">" (but
27682 not the opening "<"). See cp_parser_template_argument_list for the
27686 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27690 tree saved_qualifying_scope
;
27691 tree saved_object_scope
;
27692 bool saved_greater_than_is_operator_p
;
27693 int saved_unevaluated_operand
;
27694 int saved_inhibit_evaluation_warnings
;
27698 When parsing a template-id, the first non-nested `>' is taken as
27699 the end of the template-argument-list rather than a greater-than
27701 saved_greater_than_is_operator_p
27702 = parser
->greater_than_is_operator_p
;
27703 parser
->greater_than_is_operator_p
= false;
27704 /* Parsing the argument list may modify SCOPE, so we save it
27706 saved_scope
= parser
->scope
;
27707 saved_qualifying_scope
= parser
->qualifying_scope
;
27708 saved_object_scope
= parser
->object_scope
;
27709 /* We need to evaluate the template arguments, even though this
27710 template-id may be nested within a "sizeof". */
27711 saved_unevaluated_operand
= cp_unevaluated_operand
;
27712 cp_unevaluated_operand
= 0;
27713 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27714 c_inhibit_evaluation_warnings
= 0;
27715 /* Parse the template-argument-list itself. */
27716 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27717 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27718 arguments
= NULL_TREE
;
27720 arguments
= cp_parser_template_argument_list (parser
);
27721 /* Look for the `>' that ends the template-argument-list. If we find
27722 a '>>' instead, it's probably just a typo. */
27723 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27725 if (cxx_dialect
!= cxx98
)
27727 /* In C++0x, a `>>' in a template argument list or cast
27728 expression is considered to be two separate `>'
27729 tokens. So, change the current token to a `>', but don't
27730 consume it: it will be consumed later when the outer
27731 template argument list (or cast expression) is parsed.
27732 Note that this replacement of `>' for `>>' is necessary
27733 even if we are parsing tentatively: in the tentative
27734 case, after calling
27735 cp_parser_enclosed_template_argument_list we will always
27736 throw away all of the template arguments and the first
27737 closing `>', either because the template argument list
27738 was erroneous or because we are replacing those tokens
27739 with a CPP_TEMPLATE_ID token. The second `>' (which will
27740 not have been thrown away) is needed either to close an
27741 outer template argument list or to complete a new-style
27743 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27744 token
->type
= CPP_GREATER
;
27746 else if (!saved_greater_than_is_operator_p
)
27748 /* If we're in a nested template argument list, the '>>' has
27749 to be a typo for '> >'. We emit the error message, but we
27750 continue parsing and we push a '>' as next token, so that
27751 the argument list will be parsed correctly. Note that the
27752 global source location is still on the token before the
27753 '>>', so we need to say explicitly where we want it. */
27754 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27755 gcc_rich_location
richloc (token
->location
);
27756 richloc
.add_fixit_replace ("> >");
27757 error_at (&richloc
, "%<>>%> should be %<> >%> "
27758 "within a nested template argument list");
27760 token
->type
= CPP_GREATER
;
27764 /* If this is not a nested template argument list, the '>>'
27765 is a typo for '>'. Emit an error message and continue.
27766 Same deal about the token location, but here we can get it
27767 right by consuming the '>>' before issuing the diagnostic. */
27768 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27769 error_at (token
->location
,
27770 "spurious %<>>%>, use %<>%> to terminate "
27771 "a template argument list");
27775 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27776 /* The `>' token might be a greater-than operator again now. */
27777 parser
->greater_than_is_operator_p
27778 = saved_greater_than_is_operator_p
;
27779 /* Restore the SAVED_SCOPE. */
27780 parser
->scope
= saved_scope
;
27781 parser
->qualifying_scope
= saved_qualifying_scope
;
27782 parser
->object_scope
= saved_object_scope
;
27783 cp_unevaluated_operand
= saved_unevaluated_operand
;
27784 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27789 /* MEMBER_FUNCTION is a member function, or a friend. If default
27790 arguments, or the body of the function have not yet been parsed,
27794 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27796 timevar_push (TV_PARSE_INMETH
);
27797 /* If this member is a template, get the underlying
27799 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27800 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27802 /* There should not be any class definitions in progress at this
27803 point; the bodies of members are only parsed outside of all class
27805 gcc_assert (parser
->num_classes_being_defined
== 0);
27806 /* While we're parsing the member functions we might encounter more
27807 classes. We want to handle them right away, but we don't want
27808 them getting mixed up with functions that are currently in the
27810 push_unparsed_function_queues (parser
);
27812 /* Make sure that any template parameters are in scope. */
27813 maybe_begin_member_template_processing (member_function
);
27815 /* If the body of the function has not yet been parsed, parse it
27817 if (DECL_PENDING_INLINE_P (member_function
))
27819 tree function_scope
;
27820 cp_token_cache
*tokens
;
27822 /* The function is no longer pending; we are processing it. */
27823 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27824 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27825 DECL_PENDING_INLINE_P (member_function
) = 0;
27827 /* If this is a local class, enter the scope of the containing
27829 function_scope
= current_function_decl
;
27830 if (function_scope
)
27831 push_function_context ();
27833 /* Push the body of the function onto the lexer stack. */
27834 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27836 /* Let the front end know that we going to be defining this
27838 start_preparsed_function (member_function
, NULL_TREE
,
27839 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27841 /* Don't do access checking if it is a templated function. */
27842 if (processing_template_decl
)
27843 push_deferring_access_checks (dk_no_check
);
27845 /* #pragma omp declare reduction needs special parsing. */
27846 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27848 parser
->lexer
->in_pragma
= true;
27849 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27850 finish_function (/*inline_p=*/true);
27851 cp_check_omp_declare_reduction (member_function
);
27854 /* Now, parse the body of the function. */
27855 cp_parser_function_definition_after_declarator (parser
,
27856 /*inline_p=*/true);
27858 if (processing_template_decl
)
27859 pop_deferring_access_checks ();
27861 /* Leave the scope of the containing function. */
27862 if (function_scope
)
27863 pop_function_context ();
27864 cp_parser_pop_lexer (parser
);
27867 /* Remove any template parameters from the symbol table. */
27868 maybe_end_member_template_processing ();
27870 /* Restore the queue. */
27871 pop_unparsed_function_queues (parser
);
27872 timevar_pop (TV_PARSE_INMETH
);
27875 /* If DECL contains any default args, remember it on the unparsed
27876 functions queue. */
27879 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27883 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27885 probe
= TREE_CHAIN (probe
))
27886 if (TREE_PURPOSE (probe
))
27888 cp_default_arg_entry entry
= {current_class_type
, decl
};
27889 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27894 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27895 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27896 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27897 from the parameter-type-list. */
27900 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27901 tree default_arg
, tree parmtype
)
27903 cp_token_cache
*tokens
;
27907 if (default_arg
== error_mark_node
)
27908 return error_mark_node
;
27910 /* Push the saved tokens for the default argument onto the parser's
27912 tokens
= DEFARG_TOKENS (default_arg
);
27913 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27915 start_lambda_scope (decl
);
27917 /* Parse the default argument. */
27918 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27919 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27920 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27922 finish_lambda_scope ();
27924 if (parsed_arg
== error_mark_node
)
27925 cp_parser_skip_to_end_of_statement (parser
);
27927 if (!processing_template_decl
)
27929 /* In a non-template class, check conversions now. In a template,
27930 we'll wait and instantiate these as needed. */
27931 if (TREE_CODE (decl
) == PARM_DECL
)
27932 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27933 tf_warning_or_error
);
27934 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27935 parsed_arg
= error_mark_node
;
27937 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27940 /* If the token stream has not been completely used up, then
27941 there was extra junk after the end of the default
27943 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27945 if (TREE_CODE (decl
) == PARM_DECL
)
27946 cp_parser_error (parser
, "expected %<,%>");
27948 cp_parser_error (parser
, "expected %<;%>");
27951 /* Revert to the main lexer. */
27952 cp_parser_pop_lexer (parser
);
27957 /* FIELD is a non-static data member with an initializer which we saved for
27958 later; parse it now. */
27961 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27965 maybe_begin_member_template_processing (field
);
27967 push_unparsed_function_queues (parser
);
27968 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27969 DECL_INITIAL (field
),
27971 pop_unparsed_function_queues (parser
);
27973 maybe_end_member_template_processing ();
27975 DECL_INITIAL (field
) = def
;
27978 /* FN is a FUNCTION_DECL which may contains a parameter with an
27979 unparsed DEFAULT_ARG. Parse the default args now. This function
27980 assumes that the current scope is the scope in which the default
27981 argument should be processed. */
27984 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27986 bool saved_local_variables_forbidden_p
;
27987 tree parm
, parmdecl
;
27989 /* While we're parsing the default args, we might (due to the
27990 statement expression extension) encounter more classes. We want
27991 to handle them right away, but we don't want them getting mixed
27992 up with default args that are currently in the queue. */
27993 push_unparsed_function_queues (parser
);
27995 /* Local variable names (and the `this' keyword) may not appear
27996 in a default argument. */
27997 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27998 parser
->local_variables_forbidden_p
= true;
28000 push_defarg_context (fn
);
28002 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28003 parmdecl
= DECL_ARGUMENTS (fn
);
28004 parm
&& parm
!= void_list_node
;
28005 parm
= TREE_CHAIN (parm
),
28006 parmdecl
= DECL_CHAIN (parmdecl
))
28008 tree default_arg
= TREE_PURPOSE (parm
);
28010 vec
<tree
, va_gc
> *insts
;
28017 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28018 /* This can happen for a friend declaration for a function
28019 already declared with default arguments. */
28023 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28025 TREE_VALUE (parm
));
28026 TREE_PURPOSE (parm
) = parsed_arg
;
28028 /* Update any instantiations we've already created. */
28029 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28030 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28031 TREE_PURPOSE (copy
) = parsed_arg
;
28034 pop_defarg_context ();
28036 /* Make sure no default arg is missing. */
28037 check_default_args (fn
);
28039 /* Restore the state of local_variables_forbidden_p. */
28040 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28042 /* Restore the queue. */
28043 pop_unparsed_function_queues (parser
);
28046 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28048 sizeof ... ( identifier )
28050 where the 'sizeof' token has already been consumed. */
28053 cp_parser_sizeof_pack (cp_parser
*parser
)
28055 /* Consume the `...'. */
28056 cp_lexer_consume_token (parser
->lexer
);
28057 maybe_warn_variadic_templates ();
28059 matching_parens parens
;
28060 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28062 parens
.consume_open (parser
);
28064 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28065 "%<sizeof...%> argument must be surrounded by parentheses");
28067 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28068 tree name
= cp_parser_identifier (parser
);
28069 if (name
== error_mark_node
)
28070 return error_mark_node
;
28071 /* The name is not qualified. */
28072 parser
->scope
= NULL_TREE
;
28073 parser
->qualifying_scope
= NULL_TREE
;
28074 parser
->object_scope
= NULL_TREE
;
28075 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28076 if (expr
== error_mark_node
)
28077 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28079 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28080 expr
= TREE_TYPE (expr
);
28081 else if (TREE_CODE (expr
) == CONST_DECL
)
28082 expr
= DECL_INITIAL (expr
);
28083 expr
= make_pack_expansion (expr
);
28084 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28087 parens
.require_close (parser
);
28092 /* Parse the operand of `sizeof' (or a similar operator). Returns
28093 either a TYPE or an expression, depending on the form of the
28094 input. The KEYWORD indicates which kind of expression we have
28098 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28100 tree expr
= NULL_TREE
;
28101 const char *saved_message
;
28103 bool saved_integral_constant_expression_p
;
28104 bool saved_non_integral_constant_expression_p
;
28106 /* If it's a `...', then we are computing the length of a parameter
28108 if (keyword
== RID_SIZEOF
28109 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28110 return cp_parser_sizeof_pack (parser
);
28112 /* Types cannot be defined in a `sizeof' expression. Save away the
28114 saved_message
= parser
->type_definition_forbidden_message
;
28115 /* And create the new one. */
28116 tmp
= concat ("types may not be defined in %<",
28117 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28118 "%> expressions", NULL
);
28119 parser
->type_definition_forbidden_message
= tmp
;
28121 /* The restrictions on constant-expressions do not apply inside
28122 sizeof expressions. */
28123 saved_integral_constant_expression_p
28124 = parser
->integral_constant_expression_p
;
28125 saved_non_integral_constant_expression_p
28126 = parser
->non_integral_constant_expression_p
;
28127 parser
->integral_constant_expression_p
= false;
28129 /* Do not actually evaluate the expression. */
28130 ++cp_unevaluated_operand
;
28131 ++c_inhibit_evaluation_warnings
;
28132 /* If it's a `(', then we might be looking at the type-id
28134 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28136 tree type
= NULL_TREE
;
28138 /* We can't be sure yet whether we're looking at a type-id or an
28140 cp_parser_parse_tentatively (parser
);
28142 matching_parens parens
;
28143 parens
.consume_open (parser
);
28145 /* Note: as a GNU Extension, compound literals are considered
28146 postfix-expressions as they are in C99, so they are valid
28147 arguments to sizeof. See comment in cp_parser_cast_expression
28149 if (cp_parser_compound_literal_p (parser
))
28150 cp_parser_simulate_error (parser
);
28153 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28154 parser
->in_type_id_in_expr_p
= true;
28155 /* Look for the type-id. */
28156 type
= cp_parser_type_id (parser
);
28157 /* Look for the closing `)'. */
28158 parens
.require_close (parser
);
28159 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28162 /* If all went well, then we're done. */
28163 if (cp_parser_parse_definitely (parser
))
28165 cp_decl_specifier_seq decl_specs
;
28167 /* Build a trivial decl-specifier-seq. */
28168 clear_decl_specs (&decl_specs
);
28169 decl_specs
.type
= type
;
28171 /* Call grokdeclarator to figure out what type this is. */
28172 expr
= grokdeclarator (NULL
,
28176 /*attrlist=*/NULL
);
28180 /* If the type-id production did not work out, then we must be
28181 looking at the unary-expression production. */
28183 expr
= cp_parser_unary_expression (parser
);
28185 /* Go back to evaluating expressions. */
28186 --cp_unevaluated_operand
;
28187 --c_inhibit_evaluation_warnings
;
28189 /* Free the message we created. */
28191 /* And restore the old one. */
28192 parser
->type_definition_forbidden_message
= saved_message
;
28193 parser
->integral_constant_expression_p
28194 = saved_integral_constant_expression_p
;
28195 parser
->non_integral_constant_expression_p
28196 = saved_non_integral_constant_expression_p
;
28201 /* If the current declaration has no declarator, return true. */
28204 cp_parser_declares_only_class_p (cp_parser
*parser
)
28206 /* If the next token is a `;' or a `,' then there is no
28208 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28209 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28212 /* Update the DECL_SPECS to reflect the storage class indicated by
28216 cp_parser_set_storage_class (cp_parser
*parser
,
28217 cp_decl_specifier_seq
*decl_specs
,
28221 cp_storage_class storage_class
;
28223 if (parser
->in_unbraced_linkage_specification_p
)
28225 error_at (token
->location
, "invalid use of %qD in linkage specification",
28226 ridpointers
[keyword
]);
28229 else if (decl_specs
->storage_class
!= sc_none
)
28231 decl_specs
->conflicting_specifiers_p
= true;
28235 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28236 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28237 && decl_specs
->gnu_thread_keyword_p
)
28239 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28240 "%<__thread%> before %qD", ridpointers
[keyword
]);
28246 storage_class
= sc_auto
;
28249 storage_class
= sc_register
;
28252 storage_class
= sc_static
;
28255 storage_class
= sc_extern
;
28258 storage_class
= sc_mutable
;
28261 gcc_unreachable ();
28263 decl_specs
->storage_class
= storage_class
;
28264 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28266 /* A storage class specifier cannot be applied alongside a typedef
28267 specifier. If there is a typedef specifier present then set
28268 conflicting_specifiers_p which will trigger an error later
28269 on in grokdeclarator. */
28270 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28271 decl_specs
->conflicting_specifiers_p
= true;
28274 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28275 is true, the type is a class or enum definition. */
28278 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28281 bool type_definition_p
)
28283 decl_specs
->any_specifiers_p
= true;
28285 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28286 (with, for example, in "typedef int wchar_t;") we remember that
28287 this is what happened. In system headers, we ignore these
28288 declarations so that G++ can work with system headers that are not
28290 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28291 && !type_definition_p
28292 && (type_spec
== boolean_type_node
28293 || type_spec
== char16_type_node
28294 || type_spec
== char32_type_node
28295 || type_spec
== wchar_type_node
)
28296 && (decl_specs
->type
28297 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28298 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28299 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28300 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28302 decl_specs
->redefined_builtin_type
= type_spec
;
28303 set_and_check_decl_spec_loc (decl_specs
,
28304 ds_redefined_builtin_type_spec
,
28306 if (!decl_specs
->type
)
28308 decl_specs
->type
= type_spec
;
28309 decl_specs
->type_definition_p
= false;
28310 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28313 else if (decl_specs
->type
)
28314 decl_specs
->multiple_types_p
= true;
28317 decl_specs
->type
= type_spec
;
28318 decl_specs
->type_definition_p
= type_definition_p
;
28319 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28320 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28324 /* True iff TOKEN is the GNU keyword __thread. */
28327 token_is__thread (cp_token
*token
)
28329 gcc_assert (token
->keyword
== RID_THREAD
);
28330 return id_equal (token
->u
.value
, "__thread");
28333 /* Set the location for a declarator specifier and check if it is
28336 DECL_SPECS is the sequence of declarator specifiers onto which to
28339 DS is the single declarator specifier to set which location is to
28340 be set onto the existing sequence of declarators.
28342 LOCATION is the location for the declarator specifier to
28346 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28347 cp_decl_spec ds
, cp_token
*token
)
28349 gcc_assert (ds
< ds_last
);
28351 if (decl_specs
== NULL
)
28354 source_location location
= token
->location
;
28356 if (decl_specs
->locations
[ds
] == 0)
28358 decl_specs
->locations
[ds
] = location
;
28359 if (ds
== ds_thread
)
28360 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28366 if (decl_specs
->locations
[ds_long_long
] != 0)
28367 error_at (location
,
28368 "%<long long long%> is too long for GCC");
28371 decl_specs
->locations
[ds_long_long
] = location
;
28372 pedwarn_cxx98 (location
,
28374 "ISO C++ 1998 does not support %<long long%>");
28377 else if (ds
== ds_thread
)
28379 bool gnu
= token_is__thread (token
);
28380 gcc_rich_location
richloc (location
);
28381 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28383 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
28384 error_at (&richloc
,
28385 "both %<__thread%> and %<thread_local%> specified");
28389 richloc
.add_fixit_remove ();
28390 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28395 static const char *const decl_spec_names
[] = {
28412 gcc_rich_location
richloc (location
);
28413 richloc
.add_fixit_remove ();
28414 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28419 /* Return true iff the declarator specifier DS is present in the
28420 sequence of declarator specifiers DECL_SPECS. */
28423 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28426 gcc_assert (ds
< ds_last
);
28428 if (decl_specs
== NULL
)
28431 return decl_specs
->locations
[ds
] != 0;
28434 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28435 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28438 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28440 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28443 /* Issue an error message indicating that TOKEN_DESC was expected.
28444 If KEYWORD is true, it indicated this function is called by
28445 cp_parser_require_keword and the required token can only be
28446 a indicated keyword.
28448 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28449 within any error as the location of an "opening" token matching
28450 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28451 RT_CLOSE_PAREN). */
28454 cp_parser_required_error (cp_parser
*parser
,
28455 required_token token_desc
,
28457 location_t matching_location
)
28459 if (cp_parser_simulate_error (parser
))
28462 const char *gmsgid
= NULL
;
28463 switch (token_desc
)
28466 gmsgid
= G_("expected %<new%>");
28469 gmsgid
= G_("expected %<delete%>");
28472 gmsgid
= G_("expected %<return%>");
28475 gmsgid
= G_("expected %<while%>");
28478 gmsgid
= G_("expected %<extern%>");
28480 case RT_STATIC_ASSERT
:
28481 gmsgid
= G_("expected %<static_assert%>");
28484 gmsgid
= G_("expected %<decltype%>");
28487 gmsgid
= G_("expected %<operator%>");
28490 gmsgid
= G_("expected %<class%>");
28493 gmsgid
= G_("expected %<template%>");
28496 gmsgid
= G_("expected %<namespace%>");
28499 gmsgid
= G_("expected %<using%>");
28502 gmsgid
= G_("expected %<asm%>");
28505 gmsgid
= G_("expected %<try%>");
28508 gmsgid
= G_("expected %<catch%>");
28511 gmsgid
= G_("expected %<throw%>");
28514 gmsgid
= G_("expected %<__label__%>");
28517 gmsgid
= G_("expected %<@try%>");
28519 case RT_AT_SYNCHRONIZED
:
28520 gmsgid
= G_("expected %<@synchronized%>");
28523 gmsgid
= G_("expected %<@throw%>");
28525 case RT_TRANSACTION_ATOMIC
:
28526 gmsgid
= G_("expected %<__transaction_atomic%>");
28528 case RT_TRANSACTION_RELAXED
:
28529 gmsgid
= G_("expected %<__transaction_relaxed%>");
28535 if (!gmsgid
&& !keyword
)
28537 switch (token_desc
)
28540 gmsgid
= G_("expected %<;%>");
28542 case RT_OPEN_PAREN
:
28543 gmsgid
= G_("expected %<(%>");
28545 case RT_CLOSE_BRACE
:
28546 gmsgid
= G_("expected %<}%>");
28548 case RT_OPEN_BRACE
:
28549 gmsgid
= G_("expected %<{%>");
28551 case RT_CLOSE_SQUARE
:
28552 gmsgid
= G_("expected %<]%>");
28554 case RT_OPEN_SQUARE
:
28555 gmsgid
= G_("expected %<[%>");
28558 gmsgid
= G_("expected %<,%>");
28561 gmsgid
= G_("expected %<::%>");
28564 gmsgid
= G_("expected %<<%>");
28567 gmsgid
= G_("expected %<>%>");
28570 gmsgid
= G_("expected %<=%>");
28573 gmsgid
= G_("expected %<...%>");
28576 gmsgid
= G_("expected %<*%>");
28579 gmsgid
= G_("expected %<~%>");
28582 gmsgid
= G_("expected %<:%>");
28584 case RT_COLON_SCOPE
:
28585 gmsgid
= G_("expected %<:%> or %<::%>");
28587 case RT_CLOSE_PAREN
:
28588 gmsgid
= G_("expected %<)%>");
28590 case RT_COMMA_CLOSE_PAREN
:
28591 gmsgid
= G_("expected %<,%> or %<)%>");
28593 case RT_PRAGMA_EOL
:
28594 gmsgid
= G_("expected end of line");
28597 gmsgid
= G_("expected identifier");
28600 gmsgid
= G_("expected selection-statement");
28603 gmsgid
= G_("expected iteration-statement");
28606 gmsgid
= G_("expected jump-statement");
28609 gmsgid
= G_("expected class-key");
28611 case RT_CLASS_TYPENAME_TEMPLATE
:
28612 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28615 gcc_unreachable ();
28620 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28624 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28625 issue an error message indicating that TOKEN_DESC was expected.
28627 Returns the token consumed, if the token had the appropriate type.
28628 Otherwise, returns NULL.
28630 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28631 within any error as the location of an "opening" token matching
28632 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28633 RT_CLOSE_PAREN). */
28636 cp_parser_require (cp_parser
* parser
,
28637 enum cpp_ttype type
,
28638 required_token token_desc
,
28639 location_t matching_location
)
28641 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28642 return cp_lexer_consume_token (parser
->lexer
);
28645 /* Output the MESSAGE -- unless we're parsing tentatively. */
28646 if (!cp_parser_simulate_error (parser
))
28647 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28648 matching_location
);
28653 /* An error message is produced if the next token is not '>'.
28654 All further tokens are skipped until the desired token is
28655 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28658 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28660 /* Current level of '< ... >'. */
28661 unsigned level
= 0;
28662 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28663 unsigned nesting_depth
= 0;
28665 /* Are we ready, yet? If not, issue error message. */
28666 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28669 /* Skip tokens until the desired token is found. */
28672 /* Peek at the next token. */
28673 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28676 if (!nesting_depth
)
28681 if (cxx_dialect
== cxx98
)
28682 /* C++0x views the `>>' operator as two `>' tokens, but
28685 else if (!nesting_depth
&& level
-- == 0)
28687 /* We've hit a `>>' where the first `>' closes the
28688 template argument list, and the second `>' is
28689 spurious. Just consume the `>>' and stop; we've
28690 already produced at least one error. */
28691 cp_lexer_consume_token (parser
->lexer
);
28694 /* Fall through for C++0x, so we handle the second `>' in
28696 gcc_fallthrough ();
28699 if (!nesting_depth
&& level
-- == 0)
28701 /* We've reached the token we want, consume it and stop. */
28702 cp_lexer_consume_token (parser
->lexer
);
28707 case CPP_OPEN_PAREN
:
28708 case CPP_OPEN_SQUARE
:
28712 case CPP_CLOSE_PAREN
:
28713 case CPP_CLOSE_SQUARE
:
28714 if (nesting_depth
-- == 0)
28719 case CPP_PRAGMA_EOL
:
28720 case CPP_SEMICOLON
:
28721 case CPP_OPEN_BRACE
:
28722 case CPP_CLOSE_BRACE
:
28723 /* The '>' was probably forgotten, don't look further. */
28730 /* Consume this token. */
28731 cp_lexer_consume_token (parser
->lexer
);
28735 /* If the next token is the indicated keyword, consume it. Otherwise,
28736 issue an error message indicating that TOKEN_DESC was expected.
28738 Returns the token consumed, if the token had the appropriate type.
28739 Otherwise, returns NULL. */
28742 cp_parser_require_keyword (cp_parser
* parser
,
28744 required_token token_desc
)
28746 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28748 if (token
&& token
->keyword
!= keyword
)
28750 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28758 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28759 function-definition. */
28762 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28764 return (/* An ordinary function-body begins with an `{'. */
28765 token
->type
== CPP_OPEN_BRACE
28766 /* A ctor-initializer begins with a `:'. */
28767 || token
->type
== CPP_COLON
28768 /* A function-try-block begins with `try'. */
28769 || token
->keyword
== RID_TRY
28770 /* A function-transaction-block begins with `__transaction_atomic'
28771 or `__transaction_relaxed'. */
28772 || token
->keyword
== RID_TRANSACTION_ATOMIC
28773 || token
->keyword
== RID_TRANSACTION_RELAXED
28774 /* The named return value extension begins with `return'. */
28775 || token
->keyword
== RID_RETURN
);
28778 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28782 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28786 token
= cp_lexer_peek_token (parser
->lexer
);
28787 return (token
->type
== CPP_OPEN_BRACE
28788 || (token
->type
== CPP_COLON
28789 && !parser
->colon_doesnt_start_class_def_p
));
28792 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28793 C++0x) ending a template-argument. */
28796 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28800 token
= cp_lexer_peek_token (parser
->lexer
);
28801 return (token
->type
== CPP_COMMA
28802 || token
->type
== CPP_GREATER
28803 || token
->type
== CPP_ELLIPSIS
28804 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28807 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28808 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28811 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28816 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28817 if (token
->type
== CPP_LESS
)
28819 /* Check for the sequence `<::' in the original code. It would be lexed as
28820 `[:', where `[' is a digraph, and there is no whitespace before
28822 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28825 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28826 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28832 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28833 or none_type otherwise. */
28835 static enum tag_types
28836 cp_parser_token_is_class_key (cp_token
* token
)
28838 switch (token
->keyword
)
28843 return record_type
;
28852 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28853 or none_type otherwise or if the token is null. */
28855 static enum tag_types
28856 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28861 switch (token
->keyword
)
28866 return typename_type
;
28873 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28876 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28878 if (type
== error_mark_node
)
28880 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28882 if (permerror (input_location
, "%qs tag used in naming %q#T",
28883 class_key
== union_type
? "union"
28884 : class_key
== record_type
? "struct" : "class",
28886 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28887 "%q#T was previously declared here", type
);
28891 /* Issue an error message if DECL is redeclared with different
28892 access than its original declaration [class.access.spec/3].
28893 This applies to nested classes, nested class templates and
28894 enumerations [class.mem/1]. */
28897 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28900 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28901 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28904 if ((TREE_PRIVATE (decl
)
28905 != (current_access_specifier
== access_private_node
))
28906 || (TREE_PROTECTED (decl
)
28907 != (current_access_specifier
== access_protected_node
)))
28908 error_at (location
, "%qD redeclared with different access", decl
);
28911 /* Look for the `template' keyword, as a syntactic disambiguator.
28912 Return TRUE iff it is present, in which case it will be
28916 cp_parser_optional_template_keyword (cp_parser
*parser
)
28918 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28920 /* In C++98 the `template' keyword can only be used within templates;
28921 outside templates the parser can always figure out what is a
28922 template and what is not. In C++11, per the resolution of DR 468,
28923 `template' is allowed in cases where it is not strictly necessary. */
28924 if (!processing_template_decl
28925 && pedantic
&& cxx_dialect
== cxx98
)
28927 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28928 pedwarn (token
->location
, OPT_Wpedantic
,
28929 "in C++98 %<template%> (as a disambiguator) is only "
28930 "allowed within templates");
28931 /* If this part of the token stream is rescanned, the same
28932 error message would be generated. So, we purge the token
28933 from the stream. */
28934 cp_lexer_purge_token (parser
->lexer
);
28939 /* Consume the `template' keyword. */
28940 cp_lexer_consume_token (parser
->lexer
);
28947 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28948 set PARSER->SCOPE, and perform other related actions. */
28951 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28953 struct tree_check
*check_value
;
28955 /* Get the stored value. */
28956 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28957 /* Set the scope from the stored value. */
28958 parser
->scope
= saved_checks_value (check_value
);
28959 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28960 parser
->object_scope
= NULL_TREE
;
28963 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28964 encounter the end of a block before what we were looking for. */
28967 cp_parser_cache_group (cp_parser
*parser
,
28968 enum cpp_ttype end
,
28973 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28975 /* Abort a parenthesized expression if we encounter a semicolon. */
28976 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28977 && token
->type
== CPP_SEMICOLON
)
28979 /* If we've reached the end of the file, stop. */
28980 if (token
->type
== CPP_EOF
28981 || (end
!= CPP_PRAGMA_EOL
28982 && token
->type
== CPP_PRAGMA_EOL
))
28984 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28985 /* We've hit the end of an enclosing block, so there's been some
28986 kind of syntax error. */
28989 /* Consume the token. */
28990 cp_lexer_consume_token (parser
->lexer
);
28991 /* See if it starts a new group. */
28992 if (token
->type
== CPP_OPEN_BRACE
)
28994 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28995 /* In theory this should probably check end == '}', but
28996 cp_parser_save_member_function_body needs it to exit
28997 after either '}' or ')' when called with ')'. */
29001 else if (token
->type
== CPP_OPEN_PAREN
)
29003 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29004 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29007 else if (token
->type
== CPP_PRAGMA
)
29008 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29009 else if (token
->type
== end
)
29014 /* Like above, for caching a default argument or NSDMI. Both of these are
29015 terminated by a non-nested comma, but it can be unclear whether or not a
29016 comma is nested in a template argument list unless we do more parsing.
29017 In order to handle this ambiguity, when we encounter a ',' after a '<'
29018 we try to parse what follows as a parameter-declaration-list (in the
29019 case of a default argument) or a member-declarator (in the case of an
29020 NSDMI). If that succeeds, then we stop caching. */
29023 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29025 unsigned depth
= 0;
29026 int maybe_template_id
= 0;
29027 cp_token
*first_token
;
29029 tree default_argument
;
29031 /* Add tokens until we have processed the entire default
29032 argument. We add the range [first_token, token). */
29033 first_token
= cp_lexer_peek_token (parser
->lexer
);
29034 if (first_token
->type
== CPP_OPEN_BRACE
)
29036 /* For list-initialization, this is straightforward. */
29037 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29038 token
= cp_lexer_peek_token (parser
->lexer
);
29044 /* Peek at the next token. */
29045 token
= cp_lexer_peek_token (parser
->lexer
);
29046 /* What we do depends on what token we have. */
29047 switch (token
->type
)
29049 /* In valid code, a default argument must be
29050 immediately followed by a `,' `)', or `...'. */
29052 if (depth
== 0 && maybe_template_id
)
29054 /* If we've seen a '<', we might be in a
29055 template-argument-list. Until Core issue 325 is
29056 resolved, we don't know how this situation ought
29057 to be handled, so try to DTRT. We check whether
29058 what comes after the comma is a valid parameter
29059 declaration list. If it is, then the comma ends
29060 the default argument; otherwise the default
29061 argument continues. */
29062 bool error
= false;
29065 /* Set ITALP so cp_parser_parameter_declaration_list
29066 doesn't decide to commit to this parse. */
29067 bool saved_italp
= parser
->in_template_argument_list_p
;
29068 parser
->in_template_argument_list_p
= true;
29070 cp_parser_parse_tentatively (parser
);
29074 /* Parse declarators until we reach a non-comma or
29075 somthing that cannot be an initializer.
29076 Just checking whether we're looking at a single
29077 declarator is insufficient. Consider:
29078 int var = tuple<T,U>::x;
29079 The template parameter 'U' looks exactly like a
29083 int ctor_dtor_or_conv_p
;
29084 cp_lexer_consume_token (parser
->lexer
);
29085 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29086 &ctor_dtor_or_conv_p
,
29087 /*parenthesized_p=*/NULL
,
29089 /*friend_p=*/false);
29090 peek
= cp_lexer_peek_token (parser
->lexer
);
29091 if (cp_parser_error_occurred (parser
))
29094 while (peek
->type
== CPP_COMMA
);
29095 /* If we met an '=' or ';' then the original comma
29096 was the end of the NSDMI. Otherwise assume
29097 we're still in the NSDMI. */
29098 error
= (peek
->type
!= CPP_EQ
29099 && peek
->type
!= CPP_SEMICOLON
);
29103 cp_lexer_consume_token (parser
->lexer
);
29104 begin_scope (sk_function_parms
, NULL_TREE
);
29105 if (cp_parser_parameter_declaration_list (parser
)
29106 == error_mark_node
)
29108 pop_bindings_and_leave_scope ();
29110 if (!cp_parser_error_occurred (parser
) && !error
)
29112 cp_parser_abort_tentative_parse (parser
);
29114 parser
->in_template_argument_list_p
= saved_italp
;
29118 case CPP_CLOSE_PAREN
:
29120 /* If we run into a non-nested `;', `}', or `]',
29121 then the code is invalid -- but the default
29122 argument is certainly over. */
29123 case CPP_SEMICOLON
:
29124 case CPP_CLOSE_BRACE
:
29125 case CPP_CLOSE_SQUARE
:
29127 /* Handle correctly int n = sizeof ... ( p ); */
29128 && token
->type
!= CPP_ELLIPSIS
)
29130 /* Update DEPTH, if necessary. */
29131 else if (token
->type
== CPP_CLOSE_PAREN
29132 || token
->type
== CPP_CLOSE_BRACE
29133 || token
->type
== CPP_CLOSE_SQUARE
)
29137 case CPP_OPEN_PAREN
:
29138 case CPP_OPEN_SQUARE
:
29139 case CPP_OPEN_BRACE
:
29145 /* This might be the comparison operator, or it might
29146 start a template argument list. */
29147 ++maybe_template_id
;
29151 if (cxx_dialect
== cxx98
)
29153 /* Fall through for C++0x, which treats the `>>'
29154 operator like two `>' tokens in certain
29156 gcc_fallthrough ();
29161 /* This might be an operator, or it might close a
29162 template argument list. But if a previous '<'
29163 started a template argument list, this will have
29164 closed it, so we can't be in one anymore. */
29165 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29166 if (maybe_template_id
< 0)
29167 maybe_template_id
= 0;
29171 /* If we run out of tokens, issue an error message. */
29173 case CPP_PRAGMA_EOL
:
29174 error_at (token
->location
, "file ends in default argument");
29175 return error_mark_node
;
29179 /* In these cases, we should look for template-ids.
29180 For example, if the default argument is
29181 `X<int, double>()', we need to do name lookup to
29182 figure out whether or not `X' is a template; if
29183 so, the `,' does not end the default argument.
29185 That is not yet done. */
29192 /* If we've reached the end, stop. */
29196 /* Add the token to the token block. */
29197 token
= cp_lexer_consume_token (parser
->lexer
);
29200 /* Create a DEFAULT_ARG to represent the unparsed default
29202 default_argument
= make_node (DEFAULT_ARG
);
29203 DEFARG_TOKENS (default_argument
)
29204 = cp_token_cache_new (first_token
, token
);
29205 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29207 return default_argument
;
29210 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29213 defarg_location (tree default_argument
)
29215 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29216 location_t start
= tokens
->first
->location
;
29217 location_t end
= tokens
->last
->location
;
29218 return make_location (start
, start
, end
);
29221 /* Begin parsing tentatively. We always save tokens while parsing
29222 tentatively so that if the tentative parsing fails we can restore the
29226 cp_parser_parse_tentatively (cp_parser
* parser
)
29228 /* Enter a new parsing context. */
29229 parser
->context
= cp_parser_context_new (parser
->context
);
29230 /* Begin saving tokens. */
29231 cp_lexer_save_tokens (parser
->lexer
);
29232 /* In order to avoid repetitive access control error messages,
29233 access checks are queued up until we are no longer parsing
29235 push_deferring_access_checks (dk_deferred
);
29238 /* Commit to the currently active tentative parse. */
29241 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29243 cp_parser_context
*context
;
29246 /* Mark all of the levels as committed. */
29247 lexer
= parser
->lexer
;
29248 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29250 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29252 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29253 while (!cp_lexer_saving_tokens (lexer
))
29254 lexer
= lexer
->next
;
29255 cp_lexer_commit_tokens (lexer
);
29259 /* Commit to the topmost currently active tentative parse.
29261 Note that this function shouldn't be called when there are
29262 irreversible side-effects while in a tentative state. For
29263 example, we shouldn't create a permanent entry in the symbol
29264 table, or issue an error message that might not apply if the
29265 tentative parse is aborted. */
29268 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29270 cp_parser_context
*context
= parser
->context
;
29271 cp_lexer
*lexer
= parser
->lexer
;
29275 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29277 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29279 while (!cp_lexer_saving_tokens (lexer
))
29280 lexer
= lexer
->next
;
29281 cp_lexer_commit_tokens (lexer
);
29285 /* Abort the currently active tentative parse. All consumed tokens
29286 will be rolled back, and no diagnostics will be issued. */
29289 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29291 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29292 || errorcount
> 0);
29293 cp_parser_simulate_error (parser
);
29294 /* Now, pretend that we want to see if the construct was
29295 successfully parsed. */
29296 cp_parser_parse_definitely (parser
);
29299 /* Stop parsing tentatively. If a parse error has occurred, restore the
29300 token stream. Otherwise, commit to the tokens we have consumed.
29301 Returns true if no error occurred; false otherwise. */
29304 cp_parser_parse_definitely (cp_parser
* parser
)
29306 bool error_occurred
;
29307 cp_parser_context
*context
;
29309 /* Remember whether or not an error occurred, since we are about to
29310 destroy that information. */
29311 error_occurred
= cp_parser_error_occurred (parser
);
29312 /* Remove the topmost context from the stack. */
29313 context
= parser
->context
;
29314 parser
->context
= context
->next
;
29315 /* If no parse errors occurred, commit to the tentative parse. */
29316 if (!error_occurred
)
29318 /* Commit to the tokens read tentatively, unless that was
29320 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29321 cp_lexer_commit_tokens (parser
->lexer
);
29323 pop_to_parent_deferring_access_checks ();
29325 /* Otherwise, if errors occurred, roll back our state so that things
29326 are just as they were before we began the tentative parse. */
29329 cp_lexer_rollback_tokens (parser
->lexer
);
29330 pop_deferring_access_checks ();
29332 /* Add the context to the front of the free list. */
29333 context
->next
= cp_parser_context_free_list
;
29334 cp_parser_context_free_list
= context
;
29336 return !error_occurred
;
29339 /* Returns true if we are parsing tentatively and are not committed to
29340 this tentative parse. */
29343 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29345 return (cp_parser_parsing_tentatively (parser
)
29346 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29349 /* Returns nonzero iff an error has occurred during the most recent
29350 tentative parse. */
29353 cp_parser_error_occurred (cp_parser
* parser
)
29355 return (cp_parser_parsing_tentatively (parser
)
29356 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29359 /* Returns nonzero if GNU extensions are allowed. */
29362 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29364 return parser
->allow_gnu_extensions_p
;
29367 /* Objective-C++ Productions */
29370 /* Parse an Objective-C expression, which feeds into a primary-expression
29374 objc-message-expression
29375 objc-string-literal
29376 objc-encode-expression
29377 objc-protocol-expression
29378 objc-selector-expression
29380 Returns a tree representation of the expression. */
29383 cp_parser_objc_expression (cp_parser
* parser
)
29385 /* Try to figure out what kind of declaration is present. */
29386 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29390 case CPP_OPEN_SQUARE
:
29391 return cp_parser_objc_message_expression (parser
);
29393 case CPP_OBJC_STRING
:
29394 kwd
= cp_lexer_consume_token (parser
->lexer
);
29395 return objc_build_string_object (kwd
->u
.value
);
29398 switch (kwd
->keyword
)
29400 case RID_AT_ENCODE
:
29401 return cp_parser_objc_encode_expression (parser
);
29403 case RID_AT_PROTOCOL
:
29404 return cp_parser_objc_protocol_expression (parser
);
29406 case RID_AT_SELECTOR
:
29407 return cp_parser_objc_selector_expression (parser
);
29414 error_at (kwd
->location
,
29415 "misplaced %<@%D%> Objective-C++ construct",
29417 cp_parser_skip_to_end_of_block_or_statement (parser
);
29420 return error_mark_node
;
29423 /* Parse an Objective-C message expression.
29425 objc-message-expression:
29426 [ objc-message-receiver objc-message-args ]
29428 Returns a representation of an Objective-C message. */
29431 cp_parser_objc_message_expression (cp_parser
* parser
)
29433 tree receiver
, messageargs
;
29435 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29436 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29437 receiver
= cp_parser_objc_message_receiver (parser
);
29438 messageargs
= cp_parser_objc_message_args (parser
);
29439 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29440 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29442 tree result
= objc_build_message_expr (receiver
, messageargs
);
29444 /* Construct a location e.g.
29447 ranging from the '[' to the ']', with the caret at the start. */
29448 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29449 protected_set_expr_location (result
, combined_loc
);
29454 /* Parse an objc-message-receiver.
29456 objc-message-receiver:
29458 simple-type-specifier
29460 Returns a representation of the type or expression. */
29463 cp_parser_objc_message_receiver (cp_parser
* parser
)
29467 /* An Objective-C message receiver may be either (1) a type
29468 or (2) an expression. */
29469 cp_parser_parse_tentatively (parser
);
29470 rcv
= cp_parser_expression (parser
);
29472 /* If that worked out, fine. */
29473 if (cp_parser_parse_definitely (parser
))
29476 cp_parser_parse_tentatively (parser
);
29477 rcv
= cp_parser_simple_type_specifier (parser
,
29478 /*decl_specs=*/NULL
,
29479 CP_PARSER_FLAGS_NONE
);
29481 if (cp_parser_parse_definitely (parser
))
29482 return objc_get_class_reference (rcv
);
29484 cp_parser_error (parser
, "objective-c++ message receiver expected");
29485 return error_mark_node
;
29488 /* Parse the arguments and selectors comprising an Objective-C message.
29493 objc-selector-args , objc-comma-args
29495 objc-selector-args:
29496 objc-selector [opt] : assignment-expression
29497 objc-selector-args objc-selector [opt] : assignment-expression
29500 assignment-expression
29501 objc-comma-args , assignment-expression
29503 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29504 selector arguments and TREE_VALUE containing a list of comma
29508 cp_parser_objc_message_args (cp_parser
* parser
)
29510 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29511 bool maybe_unary_selector_p
= true;
29512 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29514 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29516 tree selector
= NULL_TREE
, arg
;
29518 if (token
->type
!= CPP_COLON
)
29519 selector
= cp_parser_objc_selector (parser
);
29521 /* Detect if we have a unary selector. */
29522 if (maybe_unary_selector_p
29523 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29524 return build_tree_list (selector
, NULL_TREE
);
29526 maybe_unary_selector_p
= false;
29527 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29528 arg
= cp_parser_assignment_expression (parser
);
29531 = chainon (sel_args
,
29532 build_tree_list (selector
, arg
));
29534 token
= cp_lexer_peek_token (parser
->lexer
);
29537 /* Handle non-selector arguments, if any. */
29538 while (token
->type
== CPP_COMMA
)
29542 cp_lexer_consume_token (parser
->lexer
);
29543 arg
= cp_parser_assignment_expression (parser
);
29546 = chainon (addl_args
,
29547 build_tree_list (NULL_TREE
, arg
));
29549 token
= cp_lexer_peek_token (parser
->lexer
);
29552 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29554 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29555 return build_tree_list (error_mark_node
, error_mark_node
);
29558 return build_tree_list (sel_args
, addl_args
);
29561 /* Parse an Objective-C encode expression.
29563 objc-encode-expression:
29564 @encode objc-typename
29566 Returns an encoded representation of the type argument. */
29569 cp_parser_objc_encode_expression (cp_parser
* parser
)
29573 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29575 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29576 matching_parens parens
;
29577 parens
.require_open (parser
);
29578 token
= cp_lexer_peek_token (parser
->lexer
);
29579 type
= complete_type (cp_parser_type_id (parser
));
29580 parens
.require_close (parser
);
29584 error_at (token
->location
,
29585 "%<@encode%> must specify a type as an argument");
29586 return error_mark_node
;
29589 /* This happens if we find @encode(T) (where T is a template
29590 typename or something dependent on a template typename) when
29591 parsing a template. In that case, we can't compile it
29592 immediately, but we rather create an AT_ENCODE_EXPR which will
29593 need to be instantiated when the template is used.
29595 if (dependent_type_p (type
))
29597 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29598 TREE_READONLY (value
) = 1;
29603 /* Build a location of the form:
29606 with caret==start at the @ token, finishing at the close paren. */
29607 location_t combined_loc
29608 = make_location (start_loc
, start_loc
,
29609 cp_lexer_previous_token (parser
->lexer
)->location
);
29611 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29614 /* Parse an Objective-C @defs expression. */
29617 cp_parser_objc_defs_expression (cp_parser
*parser
)
29621 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29622 matching_parens parens
;
29623 parens
.require_open (parser
);
29624 name
= cp_parser_identifier (parser
);
29625 parens
.require_close (parser
);
29627 return objc_get_class_ivars (name
);
29630 /* Parse an Objective-C protocol expression.
29632 objc-protocol-expression:
29633 @protocol ( identifier )
29635 Returns a representation of the protocol expression. */
29638 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29641 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29643 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29644 matching_parens parens
;
29645 parens
.require_open (parser
);
29646 proto
= cp_parser_identifier (parser
);
29647 parens
.require_close (parser
);
29649 /* Build a location of the form:
29652 with caret==start at the @ token, finishing at the close paren. */
29653 location_t combined_loc
29654 = make_location (start_loc
, start_loc
,
29655 cp_lexer_previous_token (parser
->lexer
)->location
);
29656 tree result
= objc_build_protocol_expr (proto
);
29657 protected_set_expr_location (result
, combined_loc
);
29661 /* Parse an Objective-C selector expression.
29663 objc-selector-expression:
29664 @selector ( objc-method-signature )
29666 objc-method-signature:
29672 objc-selector-seq objc-selector :
29674 Returns a representation of the method selector. */
29677 cp_parser_objc_selector_expression (cp_parser
* parser
)
29679 tree sel_seq
= NULL_TREE
;
29680 bool maybe_unary_selector_p
= true;
29682 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29684 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29685 matching_parens parens
;
29686 parens
.require_open (parser
);
29687 token
= cp_lexer_peek_token (parser
->lexer
);
29689 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29690 || token
->type
== CPP_SCOPE
)
29692 tree selector
= NULL_TREE
;
29694 if (token
->type
!= CPP_COLON
29695 || token
->type
== CPP_SCOPE
)
29696 selector
= cp_parser_objc_selector (parser
);
29698 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29699 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29701 /* Detect if we have a unary selector. */
29702 if (maybe_unary_selector_p
)
29704 sel_seq
= selector
;
29705 goto finish_selector
;
29709 cp_parser_error (parser
, "expected %<:%>");
29712 maybe_unary_selector_p
= false;
29713 token
= cp_lexer_consume_token (parser
->lexer
);
29715 if (token
->type
== CPP_SCOPE
)
29718 = chainon (sel_seq
,
29719 build_tree_list (selector
, NULL_TREE
));
29721 = chainon (sel_seq
,
29722 build_tree_list (NULL_TREE
, NULL_TREE
));
29726 = chainon (sel_seq
,
29727 build_tree_list (selector
, NULL_TREE
));
29729 token
= cp_lexer_peek_token (parser
->lexer
);
29733 parens
.require_close (parser
);
29736 /* Build a location of the form:
29739 with caret==start at the @ token, finishing at the close paren. */
29740 location_t combined_loc
29741 = make_location (loc
, loc
,
29742 cp_lexer_previous_token (parser
->lexer
)->location
);
29743 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29744 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29745 protected_set_expr_location (result
, combined_loc
);
29749 /* Parse a list of identifiers.
29751 objc-identifier-list:
29753 objc-identifier-list , identifier
29755 Returns a TREE_LIST of identifier nodes. */
29758 cp_parser_objc_identifier_list (cp_parser
* parser
)
29764 identifier
= cp_parser_identifier (parser
);
29765 if (identifier
== error_mark_node
)
29766 return error_mark_node
;
29768 list
= build_tree_list (NULL_TREE
, identifier
);
29769 sep
= cp_lexer_peek_token (parser
->lexer
);
29771 while (sep
->type
== CPP_COMMA
)
29773 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29774 identifier
= cp_parser_identifier (parser
);
29775 if (identifier
== error_mark_node
)
29778 list
= chainon (list
, build_tree_list (NULL_TREE
,
29780 sep
= cp_lexer_peek_token (parser
->lexer
);
29786 /* Parse an Objective-C alias declaration.
29788 objc-alias-declaration:
29789 @compatibility_alias identifier identifier ;
29791 This function registers the alias mapping with the Objective-C front end.
29792 It returns nothing. */
29795 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29799 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29800 alias
= cp_parser_identifier (parser
);
29801 orig
= cp_parser_identifier (parser
);
29802 objc_declare_alias (alias
, orig
);
29803 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29806 /* Parse an Objective-C class forward-declaration.
29808 objc-class-declaration:
29809 @class objc-identifier-list ;
29811 The function registers the forward declarations with the Objective-C
29812 front end. It returns nothing. */
29815 cp_parser_objc_class_declaration (cp_parser
* parser
)
29817 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29822 id
= cp_parser_identifier (parser
);
29823 if (id
== error_mark_node
)
29826 objc_declare_class (id
);
29828 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29829 cp_lexer_consume_token (parser
->lexer
);
29833 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29836 /* Parse a list of Objective-C protocol references.
29838 objc-protocol-refs-opt:
29839 objc-protocol-refs [opt]
29841 objc-protocol-refs:
29842 < objc-identifier-list >
29844 Returns a TREE_LIST of identifiers, if any. */
29847 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29849 tree protorefs
= NULL_TREE
;
29851 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29853 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29854 protorefs
= cp_parser_objc_identifier_list (parser
);
29855 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29861 /* Parse a Objective-C visibility specification. */
29864 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29866 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29868 switch (vis
->keyword
)
29870 case RID_AT_PRIVATE
:
29871 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29873 case RID_AT_PROTECTED
:
29874 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29876 case RID_AT_PUBLIC
:
29877 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29879 case RID_AT_PACKAGE
:
29880 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29886 /* Eat '@private'/'@protected'/'@public'. */
29887 cp_lexer_consume_token (parser
->lexer
);
29890 /* Parse an Objective-C method type. Return 'true' if it is a class
29891 (+) method, and 'false' if it is an instance (-) method. */
29894 cp_parser_objc_method_type (cp_parser
* parser
)
29896 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29902 /* Parse an Objective-C protocol qualifier. */
29905 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29907 tree quals
= NULL_TREE
, node
;
29908 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29910 node
= token
->u
.value
;
29912 while (node
&& identifier_p (node
)
29913 && (node
== ridpointers
[(int) RID_IN
]
29914 || node
== ridpointers
[(int) RID_OUT
]
29915 || node
== ridpointers
[(int) RID_INOUT
]
29916 || node
== ridpointers
[(int) RID_BYCOPY
]
29917 || node
== ridpointers
[(int) RID_BYREF
]
29918 || node
== ridpointers
[(int) RID_ONEWAY
]))
29920 quals
= tree_cons (NULL_TREE
, node
, quals
);
29921 cp_lexer_consume_token (parser
->lexer
);
29922 token
= cp_lexer_peek_token (parser
->lexer
);
29923 node
= token
->u
.value
;
29929 /* Parse an Objective-C typename. */
29932 cp_parser_objc_typename (cp_parser
* parser
)
29934 tree type_name
= NULL_TREE
;
29936 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29938 tree proto_quals
, cp_type
= NULL_TREE
;
29940 matching_parens parens
;
29941 parens
.consume_open (parser
); /* Eat '('. */
29942 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29944 /* An ObjC type name may consist of just protocol qualifiers, in which
29945 case the type shall default to 'id'. */
29946 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29948 cp_type
= cp_parser_type_id (parser
);
29950 /* If the type could not be parsed, an error has already
29951 been produced. For error recovery, behave as if it had
29952 not been specified, which will use the default type
29954 if (cp_type
== error_mark_node
)
29956 cp_type
= NULL_TREE
;
29957 /* We need to skip to the closing parenthesis as
29958 cp_parser_type_id() does not seem to do it for
29960 cp_parser_skip_to_closing_parenthesis (parser
,
29961 /*recovering=*/true,
29962 /*or_comma=*/false,
29963 /*consume_paren=*/false);
29967 parens
.require_close (parser
);
29968 type_name
= build_tree_list (proto_quals
, cp_type
);
29974 /* Check to see if TYPE refers to an Objective-C selector name. */
29977 cp_parser_objc_selector_p (enum cpp_ttype type
)
29979 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29980 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29981 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29982 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29983 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29986 /* Parse an Objective-C selector. */
29989 cp_parser_objc_selector (cp_parser
* parser
)
29991 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29993 if (!cp_parser_objc_selector_p (token
->type
))
29995 error_at (token
->location
, "invalid Objective-C++ selector name");
29996 return error_mark_node
;
29999 /* C++ operator names are allowed to appear in ObjC selectors. */
30000 switch (token
->type
)
30002 case CPP_AND_AND
: return get_identifier ("and");
30003 case CPP_AND_EQ
: return get_identifier ("and_eq");
30004 case CPP_AND
: return get_identifier ("bitand");
30005 case CPP_OR
: return get_identifier ("bitor");
30006 case CPP_COMPL
: return get_identifier ("compl");
30007 case CPP_NOT
: return get_identifier ("not");
30008 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30009 case CPP_OR_OR
: return get_identifier ("or");
30010 case CPP_OR_EQ
: return get_identifier ("or_eq");
30011 case CPP_XOR
: return get_identifier ("xor");
30012 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30013 default: return token
->u
.value
;
30017 /* Parse an Objective-C params list. */
30020 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30022 tree params
= NULL_TREE
;
30023 bool maybe_unary_selector_p
= true;
30024 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30026 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30028 tree selector
= NULL_TREE
, type_name
, identifier
;
30029 tree parm_attr
= NULL_TREE
;
30031 if (token
->keyword
== RID_ATTRIBUTE
)
30034 if (token
->type
!= CPP_COLON
)
30035 selector
= cp_parser_objc_selector (parser
);
30037 /* Detect if we have a unary selector. */
30038 if (maybe_unary_selector_p
30039 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30041 params
= selector
; /* Might be followed by attributes. */
30045 maybe_unary_selector_p
= false;
30046 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30048 /* Something went quite wrong. There should be a colon
30049 here, but there is not. Stop parsing parameters. */
30052 type_name
= cp_parser_objc_typename (parser
);
30053 /* New ObjC allows attributes on parameters too. */
30054 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30055 parm_attr
= cp_parser_attributes_opt (parser
);
30056 identifier
= cp_parser_identifier (parser
);
30060 objc_build_keyword_decl (selector
,
30065 token
= cp_lexer_peek_token (parser
->lexer
);
30068 if (params
== NULL_TREE
)
30070 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30071 return error_mark_node
;
30074 /* We allow tail attributes for the method. */
30075 if (token
->keyword
== RID_ATTRIBUTE
)
30077 *attributes
= cp_parser_attributes_opt (parser
);
30078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30079 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30081 cp_parser_error (parser
,
30082 "method attributes must be specified at the end");
30083 return error_mark_node
;
30086 if (params
== NULL_TREE
)
30088 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30089 return error_mark_node
;
30094 /* Parse the non-keyword Objective-C params. */
30097 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30100 tree params
= make_node (TREE_LIST
);
30101 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30102 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30104 while (token
->type
== CPP_COMMA
)
30106 cp_parameter_declarator
*parmdecl
;
30109 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30110 token
= cp_lexer_peek_token (parser
->lexer
);
30112 if (token
->type
== CPP_ELLIPSIS
)
30114 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30116 token
= cp_lexer_peek_token (parser
->lexer
);
30120 /* TODO: parse attributes for tail parameters. */
30121 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
30122 parm
= grokdeclarator (parmdecl
->declarator
,
30123 &parmdecl
->decl_specifiers
,
30124 PARM
, /*initialized=*/0,
30125 /*attrlist=*/NULL
);
30127 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30128 token
= cp_lexer_peek_token (parser
->lexer
);
30131 /* We allow tail attributes for the method. */
30132 if (token
->keyword
== RID_ATTRIBUTE
)
30134 if (*attributes
== NULL_TREE
)
30136 *attributes
= cp_parser_attributes_opt (parser
);
30137 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30138 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30142 /* We have an error, but parse the attributes, so that we can
30144 *attributes
= cp_parser_attributes_opt (parser
);
30146 cp_parser_error (parser
,
30147 "method attributes must be specified at the end");
30148 return error_mark_node
;
30154 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30157 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30159 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30161 /* If the next token is `extern' and the following token is a string
30162 literal, then we have a linkage specification. */
30163 if (token
->keyword
== RID_EXTERN
30164 && cp_parser_is_pure_string_literal
30165 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30166 cp_parser_linkage_specification (parser
);
30167 /* Handle #pragma, if any. */
30168 else if (token
->type
== CPP_PRAGMA
)
30169 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30170 /* Allow stray semicolons. */
30171 else if (token
->type
== CPP_SEMICOLON
)
30172 cp_lexer_consume_token (parser
->lexer
);
30173 /* Mark methods as optional or required, when building protocols. */
30174 else if (token
->keyword
== RID_AT_OPTIONAL
)
30176 cp_lexer_consume_token (parser
->lexer
);
30177 objc_set_method_opt (true);
30179 else if (token
->keyword
== RID_AT_REQUIRED
)
30181 cp_lexer_consume_token (parser
->lexer
);
30182 objc_set_method_opt (false);
30184 else if (token
->keyword
== RID_NAMESPACE
)
30185 cp_parser_namespace_definition (parser
);
30186 /* Other stray characters must generate errors. */
30187 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30189 cp_lexer_consume_token (parser
->lexer
);
30190 error ("stray %qs between Objective-C++ methods",
30191 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30193 /* Finally, try to parse a block-declaration, or a function-definition. */
30195 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30198 /* Parse a method signature. */
30201 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30203 tree rettype
, kwdparms
, optparms
;
30204 bool ellipsis
= false;
30205 bool is_class_method
;
30207 is_class_method
= cp_parser_objc_method_type (parser
);
30208 rettype
= cp_parser_objc_typename (parser
);
30209 *attributes
= NULL_TREE
;
30210 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30211 if (kwdparms
== error_mark_node
)
30212 return error_mark_node
;
30213 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30214 if (optparms
== error_mark_node
)
30215 return error_mark_node
;
30217 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30221 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30224 cp_lexer_save_tokens (parser
->lexer
);
30225 tattr
= cp_parser_attributes_opt (parser
);
30226 gcc_assert (tattr
) ;
30228 /* If the attributes are followed by a method introducer, this is not allowed.
30229 Dump the attributes and flag the situation. */
30230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30231 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30234 /* Otherwise, the attributes introduce some interstitial code, possibly so
30235 rewind to allow that check. */
30236 cp_lexer_rollback_tokens (parser
->lexer
);
30240 /* Parse an Objective-C method prototype list. */
30243 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30245 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30247 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30249 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30251 tree attributes
, sig
;
30252 bool is_class_method
;
30253 if (token
->type
== CPP_PLUS
)
30254 is_class_method
= true;
30256 is_class_method
= false;
30257 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30258 if (sig
== error_mark_node
)
30260 cp_parser_skip_to_end_of_block_or_statement (parser
);
30261 token
= cp_lexer_peek_token (parser
->lexer
);
30264 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30265 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30267 else if (token
->keyword
== RID_AT_PROPERTY
)
30268 cp_parser_objc_at_property_declaration (parser
);
30269 else if (token
->keyword
== RID_ATTRIBUTE
30270 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30271 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30273 "prefix attributes are ignored for methods");
30275 /* Allow for interspersed non-ObjC++ code. */
30276 cp_parser_objc_interstitial_code (parser
);
30278 token
= cp_lexer_peek_token (parser
->lexer
);
30281 if (token
->type
!= CPP_EOF
)
30282 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30284 cp_parser_error (parser
, "expected %<@end%>");
30286 objc_finish_interface ();
30289 /* Parse an Objective-C method definition list. */
30292 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30294 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30296 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30300 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30303 tree sig
, attribute
;
30304 bool is_class_method
;
30305 if (token
->type
== CPP_PLUS
)
30306 is_class_method
= true;
30308 is_class_method
= false;
30309 push_deferring_access_checks (dk_deferred
);
30310 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30311 if (sig
== error_mark_node
)
30313 cp_parser_skip_to_end_of_block_or_statement (parser
);
30314 token
= cp_lexer_peek_token (parser
->lexer
);
30317 objc_start_method_definition (is_class_method
, sig
, attribute
,
30320 /* For historical reasons, we accept an optional semicolon. */
30321 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30322 cp_lexer_consume_token (parser
->lexer
);
30324 ptk
= cp_lexer_peek_token (parser
->lexer
);
30325 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30326 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30328 perform_deferred_access_checks (tf_warning_or_error
);
30329 stop_deferring_access_checks ();
30330 meth
= cp_parser_function_definition_after_declarator (parser
,
30332 pop_deferring_access_checks ();
30333 objc_finish_method_definition (meth
);
30336 /* The following case will be removed once @synthesize is
30337 completely implemented. */
30338 else if (token
->keyword
== RID_AT_PROPERTY
)
30339 cp_parser_objc_at_property_declaration (parser
);
30340 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30341 cp_parser_objc_at_synthesize_declaration (parser
);
30342 else if (token
->keyword
== RID_AT_DYNAMIC
)
30343 cp_parser_objc_at_dynamic_declaration (parser
);
30344 else if (token
->keyword
== RID_ATTRIBUTE
30345 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30346 warning_at (token
->location
, OPT_Wattributes
,
30347 "prefix attributes are ignored for methods");
30349 /* Allow for interspersed non-ObjC++ code. */
30350 cp_parser_objc_interstitial_code (parser
);
30352 token
= cp_lexer_peek_token (parser
->lexer
);
30355 if (token
->type
!= CPP_EOF
)
30356 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30358 cp_parser_error (parser
, "expected %<@end%>");
30360 objc_finish_implementation ();
30363 /* Parse Objective-C ivars. */
30366 cp_parser_objc_class_ivars (cp_parser
* parser
)
30368 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30370 if (token
->type
!= CPP_OPEN_BRACE
)
30371 return; /* No ivars specified. */
30373 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30374 token
= cp_lexer_peek_token (parser
->lexer
);
30376 while (token
->type
!= CPP_CLOSE_BRACE
30377 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30379 cp_decl_specifier_seq declspecs
;
30380 int decl_class_or_enum_p
;
30381 tree prefix_attributes
;
30383 cp_parser_objc_visibility_spec (parser
);
30385 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30388 cp_parser_decl_specifier_seq (parser
,
30389 CP_PARSER_FLAGS_OPTIONAL
,
30391 &decl_class_or_enum_p
);
30393 /* auto, register, static, extern, mutable. */
30394 if (declspecs
.storage_class
!= sc_none
)
30396 cp_parser_error (parser
, "invalid type for instance variable");
30397 declspecs
.storage_class
= sc_none
;
30400 /* thread_local. */
30401 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30403 cp_parser_error (parser
, "invalid type for instance variable");
30404 declspecs
.locations
[ds_thread
] = 0;
30408 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30410 cp_parser_error (parser
, "invalid type for instance variable");
30411 declspecs
.locations
[ds_typedef
] = 0;
30414 prefix_attributes
= declspecs
.attributes
;
30415 declspecs
.attributes
= NULL_TREE
;
30417 /* Keep going until we hit the `;' at the end of the
30419 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30421 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30422 cp_declarator
*declarator
= NULL
;
30423 int ctor_dtor_or_conv_p
;
30425 /* Check for a (possibly unnamed) bitfield declaration. */
30426 token
= cp_lexer_peek_token (parser
->lexer
);
30427 if (token
->type
== CPP_COLON
)
30430 if (token
->type
== CPP_NAME
30431 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30434 /* Get the name of the bitfield. */
30435 declarator
= make_id_declarator (NULL_TREE
,
30436 cp_parser_identifier (parser
),
30440 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30441 /* Get the width of the bitfield. */
30443 = cp_parser_constant_expression (parser
);
30447 /* Parse the declarator. */
30449 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30450 &ctor_dtor_or_conv_p
,
30451 /*parenthesized_p=*/NULL
,
30452 /*member_p=*/false,
30453 /*friend_p=*/false);
30456 /* Look for attributes that apply to the ivar. */
30457 attributes
= cp_parser_attributes_opt (parser
);
30458 /* Remember which attributes are prefix attributes and
30460 first_attribute
= attributes
;
30461 /* Combine the attributes. */
30462 attributes
= attr_chainon (prefix_attributes
, attributes
);
30465 /* Create the bitfield declaration. */
30466 decl
= grokbitfield (declarator
, &declspecs
,
30467 width
, NULL_TREE
, attributes
);
30469 decl
= grokfield (declarator
, &declspecs
,
30470 NULL_TREE
, /*init_const_expr_p=*/false,
30471 NULL_TREE
, attributes
);
30473 /* Add the instance variable. */
30474 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30475 objc_add_instance_variable (decl
);
30477 /* Reset PREFIX_ATTRIBUTES. */
30478 if (attributes
!= error_mark_node
)
30480 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30481 attributes
= TREE_CHAIN (attributes
);
30483 TREE_CHAIN (attributes
) = NULL_TREE
;
30486 token
= cp_lexer_peek_token (parser
->lexer
);
30488 if (token
->type
== CPP_COMMA
)
30490 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30496 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30497 token
= cp_lexer_peek_token (parser
->lexer
);
30500 if (token
->keyword
== RID_AT_END
)
30501 cp_parser_error (parser
, "expected %<}%>");
30503 /* Do not consume the RID_AT_END, so it will be read again as terminating
30504 the @interface of @implementation. */
30505 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30506 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30508 /* For historical reasons, we accept an optional semicolon. */
30509 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30510 cp_lexer_consume_token (parser
->lexer
);
30513 /* Parse an Objective-C protocol declaration. */
30516 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30518 tree proto
, protorefs
;
30521 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30522 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30524 tok
= cp_lexer_peek_token (parser
->lexer
);
30525 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30526 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30530 /* See if we have a forward declaration or a definition. */
30531 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30533 /* Try a forward declaration first. */
30534 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30540 id
= cp_parser_identifier (parser
);
30541 if (id
== error_mark_node
)
30544 objc_declare_protocol (id
, attributes
);
30546 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30547 cp_lexer_consume_token (parser
->lexer
);
30551 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30554 /* Ok, we got a full-fledged definition (or at least should). */
30557 proto
= cp_parser_identifier (parser
);
30558 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30559 objc_start_protocol (proto
, protorefs
, attributes
);
30560 cp_parser_objc_method_prototype_list (parser
);
30564 /* Parse an Objective-C superclass or category. */
30567 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30570 tree
*categ
, bool *is_class_extension
)
30572 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30574 *super
= *categ
= NULL_TREE
;
30575 *is_class_extension
= false;
30576 if (next
->type
== CPP_COLON
)
30578 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30579 *super
= cp_parser_identifier (parser
);
30581 else if (next
->type
== CPP_OPEN_PAREN
)
30583 matching_parens parens
;
30584 parens
.consume_open (parser
); /* Eat '('. */
30586 /* If there is no category name, and this is an @interface, we
30587 have a class extension. */
30588 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30590 *categ
= NULL_TREE
;
30591 *is_class_extension
= true;
30594 *categ
= cp_parser_identifier (parser
);
30596 parens
.require_close (parser
);
30600 /* Parse an Objective-C class interface. */
30603 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30605 tree name
, super
, categ
, protos
;
30606 bool is_class_extension
;
30608 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30609 name
= cp_parser_identifier (parser
);
30610 if (name
== error_mark_node
)
30612 /* It's hard to recover because even if valid @interface stuff
30613 is to follow, we can't compile it (or validate it) if we
30614 don't even know which class it refers to. Let's assume this
30615 was a stray '@interface' token in the stream and skip it.
30619 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30620 &is_class_extension
);
30621 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30623 /* We have either a class or a category on our hands. */
30624 if (categ
|| is_class_extension
)
30625 objc_start_category_interface (name
, categ
, protos
, attributes
);
30628 objc_start_class_interface (name
, super
, protos
, attributes
);
30629 /* Handle instance variable declarations, if any. */
30630 cp_parser_objc_class_ivars (parser
);
30631 objc_continue_interface ();
30634 cp_parser_objc_method_prototype_list (parser
);
30637 /* Parse an Objective-C class implementation. */
30640 cp_parser_objc_class_implementation (cp_parser
* parser
)
30642 tree name
, super
, categ
;
30643 bool is_class_extension
;
30645 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30646 name
= cp_parser_identifier (parser
);
30647 if (name
== error_mark_node
)
30649 /* It's hard to recover because even if valid @implementation
30650 stuff is to follow, we can't compile it (or validate it) if
30651 we don't even know which class it refers to. Let's assume
30652 this was a stray '@implementation' token in the stream and
30657 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30658 &is_class_extension
);
30660 /* We have either a class or a category on our hands. */
30662 objc_start_category_implementation (name
, categ
);
30665 objc_start_class_implementation (name
, super
);
30666 /* Handle instance variable declarations, if any. */
30667 cp_parser_objc_class_ivars (parser
);
30668 objc_continue_implementation ();
30671 cp_parser_objc_method_definition_list (parser
);
30674 /* Consume the @end token and finish off the implementation. */
30677 cp_parser_objc_end_implementation (cp_parser
* parser
)
30679 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30680 objc_finish_implementation ();
30683 /* Parse an Objective-C declaration. */
30686 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30688 /* Try to figure out what kind of declaration is present. */
30689 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30692 switch (kwd
->keyword
)
30697 error_at (kwd
->location
, "attributes may not be specified before"
30698 " the %<@%D%> Objective-C++ keyword",
30702 case RID_AT_IMPLEMENTATION
:
30703 warning_at (kwd
->location
, OPT_Wattributes
,
30704 "prefix attributes are ignored before %<@%D%>",
30711 switch (kwd
->keyword
)
30714 cp_parser_objc_alias_declaration (parser
);
30717 cp_parser_objc_class_declaration (parser
);
30719 case RID_AT_PROTOCOL
:
30720 cp_parser_objc_protocol_declaration (parser
, attributes
);
30722 case RID_AT_INTERFACE
:
30723 cp_parser_objc_class_interface (parser
, attributes
);
30725 case RID_AT_IMPLEMENTATION
:
30726 cp_parser_objc_class_implementation (parser
);
30729 cp_parser_objc_end_implementation (parser
);
30732 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30734 cp_parser_skip_to_end_of_block_or_statement (parser
);
30738 /* Parse an Objective-C try-catch-finally statement.
30740 objc-try-catch-finally-stmt:
30741 @try compound-statement objc-catch-clause-seq [opt]
30742 objc-finally-clause [opt]
30744 objc-catch-clause-seq:
30745 objc-catch-clause objc-catch-clause-seq [opt]
30748 @catch ( objc-exception-declaration ) compound-statement
30750 objc-finally-clause:
30751 @finally compound-statement
30753 objc-exception-declaration:
30754 parameter-declaration
30757 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30761 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30762 for C. Keep them in sync. */
30765 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30767 location_t location
;
30770 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30771 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30772 objc_maybe_warn_exceptions (location
);
30773 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30774 node, lest it get absorbed into the surrounding block. */
30775 stmt
= push_stmt_list ();
30776 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30777 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30779 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30781 cp_parameter_declarator
*parm
;
30782 tree parameter_declaration
= error_mark_node
;
30783 bool seen_open_paren
= false;
30784 matching_parens parens
;
30786 cp_lexer_consume_token (parser
->lexer
);
30787 if (parens
.require_open (parser
))
30788 seen_open_paren
= true;
30789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30791 /* We have "@catch (...)" (where the '...' are literally
30792 what is in the code). Skip the '...'.
30793 parameter_declaration is set to NULL_TREE, and
30794 objc_being_catch_clauses() knows that that means
30796 cp_lexer_consume_token (parser
->lexer
);
30797 parameter_declaration
= NULL_TREE
;
30801 /* We have "@catch (NSException *exception)" or something
30802 like that. Parse the parameter declaration. */
30803 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30805 parameter_declaration
= error_mark_node
;
30807 parameter_declaration
= grokdeclarator (parm
->declarator
,
30808 &parm
->decl_specifiers
,
30809 PARM
, /*initialized=*/0,
30810 /*attrlist=*/NULL
);
30812 if (seen_open_paren
)
30813 parens
.require_close (parser
);
30816 /* If there was no open parenthesis, we are recovering from
30817 an error, and we are trying to figure out what mistake
30818 the user has made. */
30820 /* If there is an immediate closing parenthesis, the user
30821 probably forgot the opening one (ie, they typed "@catch
30822 NSException *e)". Parse the closing parenthesis and keep
30824 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30825 cp_lexer_consume_token (parser
->lexer
);
30827 /* If these is no immediate closing parenthesis, the user
30828 probably doesn't know that parenthesis are required at
30829 all (ie, they typed "@catch NSException *e"). So, just
30830 forget about the closing parenthesis and keep going. */
30832 objc_begin_catch_clause (parameter_declaration
);
30833 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30834 objc_finish_catch_clause ();
30836 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30838 cp_lexer_consume_token (parser
->lexer
);
30839 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30840 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30841 node, lest it get absorbed into the surrounding block. */
30842 stmt
= push_stmt_list ();
30843 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30844 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30847 return objc_finish_try_stmt ();
30850 /* Parse an Objective-C synchronized statement.
30852 objc-synchronized-stmt:
30853 @synchronized ( expression ) compound-statement
30855 Returns NULL_TREE. */
30858 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30860 location_t location
;
30863 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30865 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30866 objc_maybe_warn_exceptions (location
);
30867 matching_parens parens
;
30868 parens
.require_open (parser
);
30869 lock
= cp_parser_expression (parser
);
30870 parens
.require_close (parser
);
30872 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30873 node, lest it get absorbed into the surrounding block. */
30874 stmt
= push_stmt_list ();
30875 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30877 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30880 /* Parse an Objective-C throw statement.
30883 @throw assignment-expression [opt] ;
30885 Returns a constructed '@throw' statement. */
30888 cp_parser_objc_throw_statement (cp_parser
*parser
)
30890 tree expr
= NULL_TREE
;
30891 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30893 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30895 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30896 expr
= cp_parser_expression (parser
);
30898 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30900 return objc_build_throw_stmt (loc
, expr
);
30903 /* Parse an Objective-C statement. */
30906 cp_parser_objc_statement (cp_parser
* parser
)
30908 /* Try to figure out what kind of declaration is present. */
30909 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30911 switch (kwd
->keyword
)
30914 return cp_parser_objc_try_catch_finally_statement (parser
);
30915 case RID_AT_SYNCHRONIZED
:
30916 return cp_parser_objc_synchronized_statement (parser
);
30918 return cp_parser_objc_throw_statement (parser
);
30920 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30922 cp_parser_skip_to_end_of_block_or_statement (parser
);
30925 return error_mark_node
;
30928 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30929 look ahead to see if an objc keyword follows the attributes. This
30930 is to detect the use of prefix attributes on ObjC @interface and
30934 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30936 cp_lexer_save_tokens (parser
->lexer
);
30937 *attrib
= cp_parser_attributes_opt (parser
);
30938 gcc_assert (*attrib
);
30939 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30941 cp_lexer_commit_tokens (parser
->lexer
);
30944 cp_lexer_rollback_tokens (parser
->lexer
);
30948 /* This routine is a minimal replacement for
30949 c_parser_struct_declaration () used when parsing the list of
30950 types/names or ObjC++ properties. For example, when parsing the
30953 @property (readonly) int a, b, c;
30955 this function is responsible for parsing "int a, int b, int c" and
30956 returning the declarations as CHAIN of DECLs.
30958 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30959 similar parsing. */
30961 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30963 tree decls
= NULL_TREE
;
30964 cp_decl_specifier_seq declspecs
;
30965 int decl_class_or_enum_p
;
30966 tree prefix_attributes
;
30968 cp_parser_decl_specifier_seq (parser
,
30969 CP_PARSER_FLAGS_NONE
,
30971 &decl_class_or_enum_p
);
30973 if (declspecs
.type
== error_mark_node
)
30974 return error_mark_node
;
30976 /* auto, register, static, extern, mutable. */
30977 if (declspecs
.storage_class
!= sc_none
)
30979 cp_parser_error (parser
, "invalid type for property");
30980 declspecs
.storage_class
= sc_none
;
30983 /* thread_local. */
30984 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30986 cp_parser_error (parser
, "invalid type for property");
30987 declspecs
.locations
[ds_thread
] = 0;
30991 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30993 cp_parser_error (parser
, "invalid type for property");
30994 declspecs
.locations
[ds_typedef
] = 0;
30997 prefix_attributes
= declspecs
.attributes
;
30998 declspecs
.attributes
= NULL_TREE
;
31000 /* Keep going until we hit the `;' at the end of the declaration. */
31001 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31003 tree attributes
, first_attribute
, decl
;
31004 cp_declarator
*declarator
;
31007 /* Parse the declarator. */
31008 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31009 NULL
, NULL
, false, false);
31011 /* Look for attributes that apply to the ivar. */
31012 attributes
= cp_parser_attributes_opt (parser
);
31013 /* Remember which attributes are prefix attributes and
31015 first_attribute
= attributes
;
31016 /* Combine the attributes. */
31017 attributes
= attr_chainon (prefix_attributes
, attributes
);
31019 decl
= grokfield (declarator
, &declspecs
,
31020 NULL_TREE
, /*init_const_expr_p=*/false,
31021 NULL_TREE
, attributes
);
31023 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31024 return error_mark_node
;
31026 /* Reset PREFIX_ATTRIBUTES. */
31027 if (attributes
!= error_mark_node
)
31029 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31030 attributes
= TREE_CHAIN (attributes
);
31032 TREE_CHAIN (attributes
) = NULL_TREE
;
31035 DECL_CHAIN (decl
) = decls
;
31038 token
= cp_lexer_peek_token (parser
->lexer
);
31039 if (token
->type
== CPP_COMMA
)
31041 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31050 /* Parse an Objective-C @property declaration. The syntax is:
31052 objc-property-declaration:
31053 '@property' objc-property-attributes[opt] struct-declaration ;
31055 objc-property-attributes:
31056 '(' objc-property-attribute-list ')'
31058 objc-property-attribute-list:
31059 objc-property-attribute
31060 objc-property-attribute-list, objc-property-attribute
31062 objc-property-attribute
31063 'getter' = identifier
31064 'setter' = identifier
31073 @property NSString *name;
31074 @property (readonly) id object;
31075 @property (retain, nonatomic, getter=getTheName) id name;
31076 @property int a, b, c;
31078 PS: This function is identical to
31079 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31081 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31083 /* The following variables hold the attributes of the properties as
31084 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31085 seen. When we see an attribute, we set them to 'true' (if they
31086 are boolean properties) or to the identifier (if they have an
31087 argument, ie, for getter and setter). Note that here we only
31088 parse the list of attributes, check the syntax and accumulate the
31089 attributes that we find. objc_add_property_declaration() will
31090 then process the information. */
31091 bool property_assign
= false;
31092 bool property_copy
= false;
31093 tree property_getter_ident
= NULL_TREE
;
31094 bool property_nonatomic
= false;
31095 bool property_readonly
= false;
31096 bool property_readwrite
= false;
31097 bool property_retain
= false;
31098 tree property_setter_ident
= NULL_TREE
;
31100 /* 'properties' is the list of properties that we read. Usually a
31101 single one, but maybe more (eg, in "@property int a, b, c;" there
31106 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31108 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31110 /* Parse the optional attribute list... */
31111 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31114 matching_parens parens
;
31115 parens
.consume_open (parser
);
31119 bool syntax_error
= false;
31120 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31123 if (token
->type
!= CPP_NAME
)
31125 cp_parser_error (parser
, "expected identifier");
31128 keyword
= C_RID_CODE (token
->u
.value
);
31129 cp_lexer_consume_token (parser
->lexer
);
31132 case RID_ASSIGN
: property_assign
= true; break;
31133 case RID_COPY
: property_copy
= true; break;
31134 case RID_NONATOMIC
: property_nonatomic
= true; break;
31135 case RID_READONLY
: property_readonly
= true; break;
31136 case RID_READWRITE
: property_readwrite
= true; break;
31137 case RID_RETAIN
: property_retain
= true; break;
31141 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31143 if (keyword
== RID_GETTER
)
31144 cp_parser_error (parser
,
31145 "missing %<=%> (after %<getter%> attribute)");
31147 cp_parser_error (parser
,
31148 "missing %<=%> (after %<setter%> attribute)");
31149 syntax_error
= true;
31152 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31153 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31155 cp_parser_error (parser
, "expected identifier");
31156 syntax_error
= true;
31159 if (keyword
== RID_SETTER
)
31161 if (property_setter_ident
!= NULL_TREE
)
31163 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31164 cp_lexer_consume_token (parser
->lexer
);
31167 property_setter_ident
= cp_parser_objc_selector (parser
);
31168 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31169 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31171 cp_lexer_consume_token (parser
->lexer
);
31175 if (property_getter_ident
!= NULL_TREE
)
31177 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31178 cp_lexer_consume_token (parser
->lexer
);
31181 property_getter_ident
= cp_parser_objc_selector (parser
);
31185 cp_parser_error (parser
, "unknown property attribute");
31186 syntax_error
= true;
31193 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31194 cp_lexer_consume_token (parser
->lexer
);
31199 /* FIXME: "@property (setter, assign);" will generate a spurious
31200 "error: expected ‘)’ before ‘,’ token". This is because
31201 cp_parser_require, unlike the C counterpart, will produce an
31202 error even if we are in error recovery. */
31203 if (!parens
.require_close (parser
))
31205 cp_parser_skip_to_closing_parenthesis (parser
,
31206 /*recovering=*/true,
31207 /*or_comma=*/false,
31208 /*consume_paren=*/true);
31212 /* ... and the property declaration(s). */
31213 properties
= cp_parser_objc_struct_declaration (parser
);
31215 if (properties
== error_mark_node
)
31217 cp_parser_skip_to_end_of_statement (parser
);
31218 /* If the next token is now a `;', consume it. */
31219 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31220 cp_lexer_consume_token (parser
->lexer
);
31224 if (properties
== NULL_TREE
)
31225 cp_parser_error (parser
, "expected identifier");
31228 /* Comma-separated properties are chained together in
31229 reverse order; add them one by one. */
31230 properties
= nreverse (properties
);
31232 for (; properties
; properties
= TREE_CHAIN (properties
))
31233 objc_add_property_declaration (loc
, copy_node (properties
),
31234 property_readonly
, property_readwrite
,
31235 property_assign
, property_retain
,
31236 property_copy
, property_nonatomic
,
31237 property_getter_ident
, property_setter_ident
);
31240 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31243 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31245 objc-synthesize-declaration:
31246 @synthesize objc-synthesize-identifier-list ;
31248 objc-synthesize-identifier-list:
31249 objc-synthesize-identifier
31250 objc-synthesize-identifier-list, objc-synthesize-identifier
31252 objc-synthesize-identifier
31254 identifier = identifier
31257 @synthesize MyProperty;
31258 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31260 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31261 for C. Keep them in sync.
31264 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31266 tree list
= NULL_TREE
;
31268 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31270 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31273 tree property
, ivar
;
31274 property
= cp_parser_identifier (parser
);
31275 if (property
== error_mark_node
)
31277 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31280 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31282 cp_lexer_consume_token (parser
->lexer
);
31283 ivar
= cp_parser_identifier (parser
);
31284 if (ivar
== error_mark_node
)
31286 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31292 list
= chainon (list
, build_tree_list (ivar
, property
));
31293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31294 cp_lexer_consume_token (parser
->lexer
);
31298 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31299 objc_add_synthesize_declaration (loc
, list
);
31302 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31304 objc-dynamic-declaration:
31305 @dynamic identifier-list ;
31308 @dynamic MyProperty;
31309 @dynamic MyProperty, AnotherProperty;
31311 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31312 for C. Keep them in sync.
31315 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31317 tree list
= NULL_TREE
;
31319 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31321 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31325 property
= cp_parser_identifier (parser
);
31326 if (property
== error_mark_node
)
31328 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31331 list
= chainon (list
, build_tree_list (NULL
, property
));
31332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31333 cp_lexer_consume_token (parser
->lexer
);
31337 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31338 objc_add_dynamic_declaration (loc
, list
);
31342 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31344 /* Returns name of the next clause.
31345 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31346 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31347 returned and the token is consumed. */
31349 static pragma_omp_clause
31350 cp_parser_omp_clause_name (cp_parser
*parser
)
31352 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31354 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31355 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31356 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31357 result
= PRAGMA_OMP_CLAUSE_IF
;
31358 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31359 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31360 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31361 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31362 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31363 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31364 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31365 result
= PRAGMA_OMP_CLAUSE_FOR
;
31366 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31368 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31369 const char *p
= IDENTIFIER_POINTER (id
);
31374 if (!strcmp ("aligned", p
))
31375 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31376 else if (!strcmp ("async", p
))
31377 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31380 if (!strcmp ("collapse", p
))
31381 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31382 else if (!strcmp ("copy", p
))
31383 result
= PRAGMA_OACC_CLAUSE_COPY
;
31384 else if (!strcmp ("copyin", p
))
31385 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31386 else if (!strcmp ("copyout", p
))
31387 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31388 else if (!strcmp ("copyprivate", p
))
31389 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31390 else if (!strcmp ("create", p
))
31391 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31394 if (!strcmp ("defaultmap", p
))
31395 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31396 else if (!strcmp ("depend", p
))
31397 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31398 else if (!strcmp ("device", p
))
31399 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31400 else if (!strcmp ("deviceptr", p
))
31401 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31402 else if (!strcmp ("device_resident", p
))
31403 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31404 else if (!strcmp ("dist_schedule", p
))
31405 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31408 if (!strcmp ("final", p
))
31409 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31410 else if (!strcmp ("finalize", p
))
31411 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
31412 else if (!strcmp ("firstprivate", p
))
31413 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31414 else if (!strcmp ("from", p
))
31415 result
= PRAGMA_OMP_CLAUSE_FROM
;
31418 if (!strcmp ("gang", p
))
31419 result
= PRAGMA_OACC_CLAUSE_GANG
;
31420 else if (!strcmp ("grainsize", p
))
31421 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31424 if (!strcmp ("hint", p
))
31425 result
= PRAGMA_OMP_CLAUSE_HINT
;
31426 else if (!strcmp ("host", p
))
31427 result
= PRAGMA_OACC_CLAUSE_HOST
;
31430 if (!strcmp ("if_present", p
))
31431 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
31432 else if (!strcmp ("inbranch", p
))
31433 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31434 else if (!strcmp ("independent", p
))
31435 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31436 else if (!strcmp ("is_device_ptr", p
))
31437 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31440 if (!strcmp ("lastprivate", p
))
31441 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31442 else if (!strcmp ("linear", p
))
31443 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31444 else if (!strcmp ("link", p
))
31445 result
= PRAGMA_OMP_CLAUSE_LINK
;
31448 if (!strcmp ("map", p
))
31449 result
= PRAGMA_OMP_CLAUSE_MAP
;
31450 else if (!strcmp ("mergeable", p
))
31451 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31454 if (!strcmp ("nogroup", p
))
31455 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31456 else if (!strcmp ("notinbranch", p
))
31457 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31458 else if (!strcmp ("nowait", p
))
31459 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31460 else if (!strcmp ("num_gangs", p
))
31461 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31462 else if (!strcmp ("num_tasks", p
))
31463 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31464 else if (!strcmp ("num_teams", p
))
31465 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31466 else if (!strcmp ("num_threads", p
))
31467 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31468 else if (!strcmp ("num_workers", p
))
31469 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31472 if (!strcmp ("ordered", p
))
31473 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31476 if (!strcmp ("parallel", p
))
31477 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31478 else if (!strcmp ("present", p
))
31479 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31480 else if (!strcmp ("present_or_copy", p
)
31481 || !strcmp ("pcopy", p
))
31482 result
= PRAGMA_OACC_CLAUSE_COPY
;
31483 else if (!strcmp ("present_or_copyin", p
)
31484 || !strcmp ("pcopyin", p
))
31485 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
31486 else if (!strcmp ("present_or_copyout", p
)
31487 || !strcmp ("pcopyout", p
))
31488 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31489 else if (!strcmp ("present_or_create", p
)
31490 || !strcmp ("pcreate", p
))
31491 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31492 else if (!strcmp ("priority", p
))
31493 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31494 else if (!strcmp ("proc_bind", p
))
31495 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31498 if (!strcmp ("reduction", p
))
31499 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31502 if (!strcmp ("safelen", p
))
31503 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31504 else if (!strcmp ("schedule", p
))
31505 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31506 else if (!strcmp ("sections", p
))
31507 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31508 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
31509 result
= PRAGMA_OACC_CLAUSE_HOST
;
31510 else if (!strcmp ("seq", p
))
31511 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31512 else if (!strcmp ("shared", p
))
31513 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31514 else if (!strcmp ("simd", p
))
31515 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31516 else if (!strcmp ("simdlen", p
))
31517 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31520 if (!strcmp ("taskgroup", p
))
31521 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31522 else if (!strcmp ("thread_limit", p
))
31523 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31524 else if (!strcmp ("threads", p
))
31525 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31526 else if (!strcmp ("tile", p
))
31527 result
= PRAGMA_OACC_CLAUSE_TILE
;
31528 else if (!strcmp ("to", p
))
31529 result
= PRAGMA_OMP_CLAUSE_TO
;
31532 if (!strcmp ("uniform", p
))
31533 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31534 else if (!strcmp ("untied", p
))
31535 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31536 else if (!strcmp ("use_device", p
))
31537 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31538 else if (!strcmp ("use_device_ptr", p
))
31539 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31542 if (!strcmp ("vector", p
))
31543 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31544 else if (!strcmp ("vector_length", p
))
31545 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31548 if (!strcmp ("wait", p
))
31549 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31550 else if (!strcmp ("worker", p
))
31551 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31556 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31557 cp_lexer_consume_token (parser
->lexer
);
31562 /* Validate that a clause of the given type does not already exist. */
31565 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31566 const char *name
, location_t location
)
31570 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31571 if (OMP_CLAUSE_CODE (c
) == code
)
31573 error_at (location
, "too many %qs clauses", name
);
31581 variable-list , identifier
31583 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31584 colon). An opening parenthesis will have been consumed by the caller.
31586 If KIND is nonzero, create the appropriate node and install the decl
31587 in OMP_CLAUSE_DECL and add the node to the head of the list.
31589 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31590 return the list created.
31592 COLON can be NULL if only closing parenthesis should end the list,
31593 or pointer to bool which will receive false if the list is terminated
31594 by closing parenthesis or true if the list is terminated by colon. */
31597 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31598 tree list
, bool *colon
)
31601 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31604 parser
->colon_corrects_to_scope_p
= false;
31611 token
= cp_lexer_peek_token (parser
->lexer
);
31613 && current_class_ptr
31614 && cp_parser_is_keyword (token
, RID_THIS
))
31616 decl
= finish_this_expr ();
31617 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31618 || CONVERT_EXPR_P (decl
))
31619 decl
= TREE_OPERAND (decl
, 0);
31620 cp_lexer_consume_token (parser
->lexer
);
31624 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31625 /*check_dependency_p=*/true,
31626 /*template_p=*/NULL
,
31627 /*declarator_p=*/false,
31628 /*optional_p=*/false);
31629 if (name
== error_mark_node
)
31632 if (identifier_p (name
))
31633 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31636 if (decl
== error_mark_node
)
31637 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31640 if (decl
== error_mark_node
)
31642 else if (kind
!= 0)
31646 case OMP_CLAUSE__CACHE_
:
31647 /* The OpenACC cache directive explicitly only allows "array
31648 elements or subarrays". */
31649 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31651 error_at (token
->location
, "expected %<[%>");
31652 decl
= error_mark_node
;
31656 case OMP_CLAUSE_MAP
:
31657 case OMP_CLAUSE_FROM
:
31658 case OMP_CLAUSE_TO
:
31659 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31662 = cp_lexer_peek_token (parser
->lexer
)->location
;
31663 cp_id_kind idk
= CP_ID_KIND_NONE
;
31664 cp_lexer_consume_token (parser
->lexer
);
31665 decl
= convert_from_reference (decl
);
31667 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31672 case OMP_CLAUSE_DEPEND
:
31673 case OMP_CLAUSE_REDUCTION
:
31674 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31676 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31678 parser
->colon_corrects_to_scope_p
= false;
31679 cp_lexer_consume_token (parser
->lexer
);
31680 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31681 low_bound
= cp_parser_expression (parser
);
31683 parser
->colon_corrects_to_scope_p
31684 = saved_colon_corrects_to_scope_p
;
31685 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31686 length
= integer_one_node
;
31689 /* Look for `:'. */
31690 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31692 if (!cp_lexer_next_token_is (parser
->lexer
,
31694 length
= cp_parser_expression (parser
);
31696 /* Look for the closing `]'. */
31697 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31701 decl
= tree_cons (low_bound
, length
, decl
);
31708 tree u
= build_omp_clause (token
->location
, kind
);
31709 OMP_CLAUSE_DECL (u
) = decl
;
31710 OMP_CLAUSE_CHAIN (u
) = list
;
31714 list
= tree_cons (decl
, NULL_TREE
, list
);
31717 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31719 cp_lexer_consume_token (parser
->lexer
);
31723 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31725 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31728 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31732 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31736 /* Try to resync to an unnested comma. Copied from
31737 cp_parser_parenthesized_expression_list. */
31740 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31741 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31742 /*recovering=*/true,
31744 /*consume_paren=*/true);
31752 /* Similarly, but expect leading and trailing parenthesis. This is a very
31753 common case for omp clauses. */
31756 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31758 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31759 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31764 copy ( variable-list )
31765 copyin ( variable-list )
31766 copyout ( variable-list )
31767 create ( variable-list )
31768 delete ( variable-list )
31769 present ( variable-list ) */
31772 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31775 enum gomp_map_kind kind
;
31778 case PRAGMA_OACC_CLAUSE_COPY
:
31779 kind
= GOMP_MAP_TOFROM
;
31781 case PRAGMA_OACC_CLAUSE_COPYIN
:
31782 kind
= GOMP_MAP_TO
;
31784 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31785 kind
= GOMP_MAP_FROM
;
31787 case PRAGMA_OACC_CLAUSE_CREATE
:
31788 kind
= GOMP_MAP_ALLOC
;
31790 case PRAGMA_OACC_CLAUSE_DELETE
:
31791 kind
= GOMP_MAP_RELEASE
;
31793 case PRAGMA_OACC_CLAUSE_DEVICE
:
31794 kind
= GOMP_MAP_FORCE_TO
;
31796 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31797 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31799 case PRAGMA_OACC_CLAUSE_HOST
:
31800 kind
= GOMP_MAP_FORCE_FROM
;
31802 case PRAGMA_OACC_CLAUSE_LINK
:
31803 kind
= GOMP_MAP_LINK
;
31805 case PRAGMA_OACC_CLAUSE_PRESENT
:
31806 kind
= GOMP_MAP_FORCE_PRESENT
;
31809 gcc_unreachable ();
31812 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31814 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31815 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31821 deviceptr ( variable-list ) */
31824 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31826 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31829 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31830 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31831 variable-list must only allow for pointer variables. */
31832 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31833 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31835 tree v
= TREE_PURPOSE (t
);
31836 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31837 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31838 OMP_CLAUSE_DECL (u
) = v
;
31839 OMP_CLAUSE_CHAIN (u
) = list
;
31854 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31855 enum omp_clause_code code
,
31856 tree list
, location_t location
)
31858 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31859 tree c
= build_omp_clause (location
, code
);
31860 OMP_CLAUSE_CHAIN (c
) = list
;
31865 num_gangs ( expression )
31866 num_workers ( expression )
31867 vector_length ( expression ) */
31870 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31871 const char *str
, tree list
)
31873 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31875 matching_parens parens
;
31876 if (!parens
.require_open (parser
))
31879 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31881 if (t
== error_mark_node
31882 || !parens
.require_close (parser
))
31884 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31885 /*or_comma=*/false,
31886 /*consume_paren=*/true);
31890 check_no_duplicate_clause (list
, code
, str
, loc
);
31892 tree c
= build_omp_clause (loc
, code
);
31893 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31894 OMP_CLAUSE_CHAIN (c
) = list
;
31900 gang [( gang-arg-list )]
31901 worker [( [num:] int-expr )]
31902 vector [( [length:] int-expr )]
31904 where gang-arg is one of:
31909 and size-expr may be:
31916 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31917 const char *str
, tree list
)
31919 const char *id
= "num";
31920 cp_lexer
*lexer
= parser
->lexer
;
31921 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31922 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31924 if (kind
== OMP_CLAUSE_VECTOR
)
31927 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31929 matching_parens parens
;
31930 parens
.consume_open (parser
);
31934 cp_token
*next
= cp_lexer_peek_token (lexer
);
31937 /* Gang static argument. */
31938 if (kind
== OMP_CLAUSE_GANG
31939 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31941 cp_lexer_consume_token (lexer
);
31943 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31944 goto cleanup_error
;
31947 if (ops
[idx
] != NULL
)
31949 cp_parser_error (parser
, "too many %<static%> arguments");
31950 goto cleanup_error
;
31953 /* Check for the '*' argument. */
31954 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31955 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31956 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31959 cp_lexer_consume_token (lexer
);
31960 ops
[idx
] = integer_minus_one_node
;
31962 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31964 cp_lexer_consume_token (lexer
);
31970 /* Worker num: argument and vector length: arguments. */
31971 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31972 && id_equal (next
->u
.value
, id
)
31973 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31975 cp_lexer_consume_token (lexer
); /* id */
31976 cp_lexer_consume_token (lexer
); /* ':' */
31979 /* Now collect the actual argument. */
31980 if (ops
[idx
] != NULL_TREE
)
31982 cp_parser_error (parser
, "unexpected argument");
31983 goto cleanup_error
;
31986 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31988 if (expr
== error_mark_node
)
31989 goto cleanup_error
;
31991 mark_exp_read (expr
);
31994 if (kind
== OMP_CLAUSE_GANG
31995 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31997 cp_lexer_consume_token (lexer
);
32004 if (!parens
.require_close (parser
))
32005 goto cleanup_error
;
32008 check_no_duplicate_clause (list
, kind
, str
, loc
);
32010 c
= build_omp_clause (loc
, kind
);
32013 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32015 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32016 OMP_CLAUSE_CHAIN (c
) = list
;
32021 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32026 tile ( size-expr-list ) */
32029 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32031 tree c
, expr
= error_mark_node
;
32032 tree tile
= NULL_TREE
;
32034 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32035 so, but the spec authors never considered such a case and have
32036 differing opinions on what it might mean, including 'not
32038 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32039 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32042 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32047 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32050 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32051 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32052 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32054 cp_lexer_consume_token (parser
->lexer
);
32055 expr
= integer_zero_node
;
32058 expr
= cp_parser_constant_expression (parser
);
32060 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32062 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32064 /* Consume the trailing ')'. */
32065 cp_lexer_consume_token (parser
->lexer
);
32067 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32068 tile
= nreverse (tile
);
32069 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32070 OMP_CLAUSE_CHAIN (c
) = list
;
32075 Parse wait clause or directive parameters. */
32078 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32080 vec
<tree
, va_gc
> *args
;
32083 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32085 /*allow_expansion_p=*/true,
32086 /*non_constant_p=*/NULL
);
32088 if (args
== NULL
|| args
->length () == 0)
32090 cp_parser_error (parser
, "expected integer expression before ')'");
32092 release_tree_vector (args
);
32096 args_tree
= build_tree_list_vec (args
);
32098 release_tree_vector (args
);
32100 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32102 tree targ
= TREE_VALUE (t
);
32104 if (targ
!= error_mark_node
)
32106 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32107 error ("%<wait%> expression must be integral");
32110 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32112 targ
= mark_rvalue_use (targ
);
32113 OMP_CLAUSE_DECL (c
) = targ
;
32114 OMP_CLAUSE_CHAIN (c
) = list
;
32124 wait ( int-expr-list ) */
32127 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32129 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32131 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32134 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32140 collapse ( constant-expression ) */
32143 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32149 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32150 matching_parens parens
;
32151 if (!parens
.require_open (parser
))
32154 num
= cp_parser_constant_expression (parser
);
32156 if (!parens
.require_close (parser
))
32157 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32158 /*or_comma=*/false,
32159 /*consume_paren=*/true);
32161 if (num
== error_mark_node
)
32163 num
= fold_non_dependent_expr (num
);
32164 if (!tree_fits_shwi_p (num
)
32165 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32166 || (n
= tree_to_shwi (num
)) <= 0
32169 error_at (loc
, "collapse argument needs positive constant integer expression");
32173 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32174 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32175 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32176 OMP_CLAUSE_CHAIN (c
) = list
;
32177 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32183 default ( none | shared )
32186 default ( none | present ) */
32189 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32190 location_t location
, bool is_oacc
)
32192 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32195 matching_parens parens
;
32196 if (!parens
.require_open (parser
))
32198 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32200 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32201 const char *p
= IDENTIFIER_POINTER (id
);
32206 if (strcmp ("none", p
) != 0)
32208 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32212 if (strcmp ("present", p
) != 0 || !is_oacc
)
32214 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32218 if (strcmp ("shared", p
) != 0 || is_oacc
)
32220 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32227 cp_lexer_consume_token (parser
->lexer
);
32233 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32235 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32238 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32239 || !parens
.require_close (parser
))
32240 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32241 /*or_comma=*/false,
32242 /*consume_paren=*/true);
32244 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32247 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32248 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32249 OMP_CLAUSE_CHAIN (c
) = list
;
32250 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32256 final ( expression ) */
32259 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32263 matching_parens parens
;
32264 if (!parens
.require_open (parser
))
32267 t
= cp_parser_condition (parser
);
32269 if (t
== error_mark_node
32270 || !parens
.require_close (parser
))
32271 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32272 /*or_comma=*/false,
32273 /*consume_paren=*/true);
32275 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32277 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32278 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32279 OMP_CLAUSE_CHAIN (c
) = list
;
32288 if ( directive-name-modifier : expression )
32290 directive-name-modifier:
32291 parallel | task | taskloop | target data | target | target update
32292 | target enter data | target exit data */
32295 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32299 enum tree_code if_modifier
= ERROR_MARK
;
32301 matching_parens parens
;
32302 if (!parens
.require_open (parser
))
32305 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32307 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32308 const char *p
= IDENTIFIER_POINTER (id
);
32311 if (strcmp ("parallel", p
) == 0)
32312 if_modifier
= OMP_PARALLEL
;
32313 else if (strcmp ("task", p
) == 0)
32314 if_modifier
= OMP_TASK
;
32315 else if (strcmp ("taskloop", p
) == 0)
32316 if_modifier
= OMP_TASKLOOP
;
32317 else if (strcmp ("target", p
) == 0)
32319 if_modifier
= OMP_TARGET
;
32320 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32322 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32323 p
= IDENTIFIER_POINTER (id
);
32324 if (strcmp ("data", p
) == 0)
32325 if_modifier
= OMP_TARGET_DATA
;
32326 else if (strcmp ("update", p
) == 0)
32327 if_modifier
= OMP_TARGET_UPDATE
;
32328 else if (strcmp ("enter", p
) == 0)
32329 if_modifier
= OMP_TARGET_ENTER_DATA
;
32330 else if (strcmp ("exit", p
) == 0)
32331 if_modifier
= OMP_TARGET_EXIT_DATA
;
32332 if (if_modifier
!= OMP_TARGET
)
32337 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32338 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32340 if_modifier
= ERROR_MARK
;
32342 if (if_modifier
== OMP_TARGET_ENTER_DATA
32343 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32345 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32347 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32348 p
= IDENTIFIER_POINTER (id
);
32349 if (strcmp ("data", p
) == 0)
32355 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32356 error_at (loc
, "expected %<data%>");
32357 if_modifier
= ERROR_MARK
;
32362 if (if_modifier
!= ERROR_MARK
)
32364 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32367 cp_lexer_consume_token (parser
->lexer
);
32374 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32375 error_at (loc
, "expected %<:%>");
32377 if_modifier
= ERROR_MARK
;
32382 t
= cp_parser_condition (parser
);
32384 if (t
== error_mark_node
32385 || !parens
.require_close (parser
))
32386 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32387 /*or_comma=*/false,
32388 /*consume_paren=*/true);
32390 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32391 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32393 if (if_modifier
!= ERROR_MARK
32394 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32396 const char *p
= NULL
;
32397 switch (if_modifier
)
32399 case OMP_PARALLEL
: p
= "parallel"; break;
32400 case OMP_TASK
: p
= "task"; break;
32401 case OMP_TASKLOOP
: p
= "taskloop"; break;
32402 case OMP_TARGET_DATA
: p
= "target data"; break;
32403 case OMP_TARGET
: p
= "target"; break;
32404 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32405 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32406 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32407 default: gcc_unreachable ();
32409 error_at (location
, "too many %<if%> clauses with %qs modifier",
32413 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32416 error_at (location
, "too many %<if%> clauses");
32418 error_at (location
, "too many %<if%> clauses without modifier");
32421 else if (if_modifier
== ERROR_MARK
32422 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32424 error_at (location
, "if any %<if%> clause has modifier, then all "
32425 "%<if%> clauses have to use modifier");
32430 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32431 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32432 OMP_CLAUSE_IF_EXPR (c
) = t
;
32433 OMP_CLAUSE_CHAIN (c
) = list
;
32442 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32443 tree list
, location_t location
)
32447 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32450 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32451 OMP_CLAUSE_CHAIN (c
) = list
;
32459 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32460 tree list
, location_t location
)
32464 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32466 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32467 OMP_CLAUSE_CHAIN (c
) = list
;
32472 num_threads ( expression ) */
32475 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32476 location_t location
)
32480 matching_parens parens
;
32481 if (!parens
.require_open (parser
))
32484 t
= cp_parser_expression (parser
);
32486 if (t
== error_mark_node
32487 || !parens
.require_close (parser
))
32488 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32489 /*or_comma=*/false,
32490 /*consume_paren=*/true);
32492 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32493 "num_threads", location
);
32495 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32496 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32497 OMP_CLAUSE_CHAIN (c
) = list
;
32503 num_tasks ( expression ) */
32506 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32507 location_t location
)
32511 matching_parens parens
;
32512 if (!parens
.require_open (parser
))
32515 t
= cp_parser_expression (parser
);
32517 if (t
== error_mark_node
32518 || !parens
.require_close (parser
))
32519 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32520 /*or_comma=*/false,
32521 /*consume_paren=*/true);
32523 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32524 "num_tasks", location
);
32526 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32527 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32528 OMP_CLAUSE_CHAIN (c
) = list
;
32534 grainsize ( expression ) */
32537 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32538 location_t location
)
32542 matching_parens parens
;
32543 if (!parens
.require_open (parser
))
32546 t
= cp_parser_expression (parser
);
32548 if (t
== error_mark_node
32549 || !parens
.require_close (parser
))
32550 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32551 /*or_comma=*/false,
32552 /*consume_paren=*/true);
32554 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32555 "grainsize", location
);
32557 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32558 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32559 OMP_CLAUSE_CHAIN (c
) = list
;
32565 priority ( expression ) */
32568 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32569 location_t location
)
32573 matching_parens parens
;
32574 if (!parens
.require_open (parser
))
32577 t
= cp_parser_expression (parser
);
32579 if (t
== error_mark_node
32580 || !parens
.require_close (parser
))
32581 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32582 /*or_comma=*/false,
32583 /*consume_paren=*/true);
32585 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32586 "priority", location
);
32588 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32589 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32590 OMP_CLAUSE_CHAIN (c
) = list
;
32596 hint ( expression ) */
32599 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32600 location_t location
)
32604 matching_parens parens
;
32605 if (!parens
.require_open (parser
))
32608 t
= cp_parser_expression (parser
);
32610 if (t
== error_mark_node
32611 || !parens
.require_close (parser
))
32612 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32613 /*or_comma=*/false,
32614 /*consume_paren=*/true);
32616 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32618 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32619 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32620 OMP_CLAUSE_CHAIN (c
) = list
;
32626 defaultmap ( tofrom : scalar ) */
32629 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32630 location_t location
)
32635 matching_parens parens
;
32636 if (!parens
.require_open (parser
))
32639 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32641 cp_parser_error (parser
, "expected %<tofrom%>");
32644 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32645 p
= IDENTIFIER_POINTER (id
);
32646 if (strcmp (p
, "tofrom") != 0)
32648 cp_parser_error (parser
, "expected %<tofrom%>");
32651 cp_lexer_consume_token (parser
->lexer
);
32652 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32655 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32657 cp_parser_error (parser
, "expected %<scalar%>");
32660 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32661 p
= IDENTIFIER_POINTER (id
);
32662 if (strcmp (p
, "scalar") != 0)
32664 cp_parser_error (parser
, "expected %<scalar%>");
32667 cp_lexer_consume_token (parser
->lexer
);
32668 if (!parens
.require_close (parser
))
32671 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32674 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32675 OMP_CLAUSE_CHAIN (c
) = list
;
32679 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32680 /*or_comma=*/false,
32681 /*consume_paren=*/true);
32689 ordered ( constant-expression ) */
32692 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32693 tree list
, location_t location
)
32695 tree c
, num
= NULL_TREE
;
32698 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32699 "ordered", location
);
32701 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32703 matching_parens parens
;
32704 parens
.consume_open (parser
);
32706 num
= cp_parser_constant_expression (parser
);
32708 if (!parens
.require_close (parser
))
32709 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32710 /*or_comma=*/false,
32711 /*consume_paren=*/true);
32713 if (num
== error_mark_node
)
32715 num
= fold_non_dependent_expr (num
);
32716 if (!tree_fits_shwi_p (num
)
32717 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32718 || (n
= tree_to_shwi (num
)) <= 0
32721 error_at (location
,
32722 "ordered argument needs positive constant integer "
32728 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32729 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32730 OMP_CLAUSE_CHAIN (c
) = list
;
32735 reduction ( reduction-operator : variable-list )
32737 reduction-operator:
32738 One of: + * - & ^ | && ||
32742 reduction-operator:
32743 One of: + * - & ^ | && || min max
32747 reduction-operator:
32748 One of: + * - & ^ | && ||
32752 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32754 enum tree_code code
= ERROR_MARK
;
32755 tree nlist
, c
, id
= NULL_TREE
;
32757 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32760 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32762 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32763 case CPP_MULT
: code
= MULT_EXPR
; break;
32764 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32765 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32766 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32767 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32768 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32769 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32773 if (code
!= ERROR_MARK
)
32774 cp_lexer_consume_token (parser
->lexer
);
32777 bool saved_colon_corrects_to_scope_p
;
32778 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32779 parser
->colon_corrects_to_scope_p
= false;
32780 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32781 /*check_dependency_p=*/true,
32782 /*template_p=*/NULL
,
32783 /*declarator_p=*/false,
32784 /*optional_p=*/false);
32785 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32786 if (identifier_p (id
))
32788 const char *p
= IDENTIFIER_POINTER (id
);
32790 if (strcmp (p
, "min") == 0)
32792 else if (strcmp (p
, "max") == 0)
32794 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32796 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32798 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32800 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32801 code
= BIT_AND_EXPR
;
32802 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32803 code
= BIT_IOR_EXPR
;
32804 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32805 code
= BIT_XOR_EXPR
;
32806 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32807 code
= TRUTH_ANDIF_EXPR
;
32808 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32809 code
= TRUTH_ORIF_EXPR
;
32810 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32811 tree scope
= parser
->scope
;
32813 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32814 parser
->scope
= NULL_TREE
;
32815 parser
->qualifying_scope
= NULL_TREE
;
32816 parser
->object_scope
= NULL_TREE
;
32820 error ("invalid reduction-identifier");
32822 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32823 /*or_comma=*/false,
32824 /*consume_paren=*/true);
32829 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32832 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32834 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32836 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32837 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32844 schedule ( schedule-kind )
32845 schedule ( schedule-kind , expression )
32848 static | dynamic | guided | runtime | auto
32851 schedule ( schedule-modifier : schedule-kind )
32852 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32860 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32863 int modifiers
= 0, nmodifiers
= 0;
32865 matching_parens parens
;
32866 if (!parens
.require_open (parser
))
32869 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32871 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32873 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32874 const char *p
= IDENTIFIER_POINTER (id
);
32875 if (strcmp ("simd", p
) == 0)
32876 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32877 else if (strcmp ("monotonic", p
) == 0)
32878 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32879 else if (strcmp ("nonmonotonic", p
) == 0)
32880 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32883 cp_lexer_consume_token (parser
->lexer
);
32884 if (nmodifiers
++ == 0
32885 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32886 cp_lexer_consume_token (parser
->lexer
);
32889 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32894 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32896 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32897 const char *p
= IDENTIFIER_POINTER (id
);
32902 if (strcmp ("dynamic", p
) != 0)
32904 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32908 if (strcmp ("guided", p
) != 0)
32910 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32914 if (strcmp ("runtime", p
) != 0)
32916 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32923 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32924 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32925 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32926 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32929 cp_lexer_consume_token (parser
->lexer
);
32931 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32932 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32933 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32934 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32936 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32944 cp_lexer_consume_token (parser
->lexer
);
32946 token
= cp_lexer_peek_token (parser
->lexer
);
32947 t
= cp_parser_assignment_expression (parser
);
32949 if (t
== error_mark_node
)
32951 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32952 error_at (token
->location
, "schedule %<runtime%> does not take "
32953 "a %<chunk_size%> parameter");
32954 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32955 error_at (token
->location
, "schedule %<auto%> does not take "
32956 "a %<chunk_size%> parameter");
32958 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32960 if (!parens
.require_close (parser
))
32963 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32966 OMP_CLAUSE_SCHEDULE_KIND (c
)
32967 = (enum omp_clause_schedule_kind
)
32968 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32970 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32971 OMP_CLAUSE_CHAIN (c
) = list
;
32975 cp_parser_error (parser
, "invalid schedule kind");
32977 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32978 /*or_comma=*/false,
32979 /*consume_paren=*/true);
32987 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32988 tree list
, location_t location
)
32992 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32994 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32995 OMP_CLAUSE_CHAIN (c
) = list
;
33004 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
33005 tree list
, location_t location
)
33007 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33008 tree c
= build_omp_clause (location
, code
);
33009 OMP_CLAUSE_CHAIN (c
) = list
;
33020 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33021 enum omp_clause_code code
,
33022 tree list
, location_t location
)
33024 tree c
= build_omp_clause (location
, code
);
33025 OMP_CLAUSE_CHAIN (c
) = list
;
33033 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33034 tree list
, location_t location
)
33036 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33037 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33038 OMP_CLAUSE_CHAIN (c
) = list
;
33047 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33048 enum omp_clause_code code
,
33049 tree list
, location_t location
)
33051 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33052 tree c
= build_omp_clause (location
, code
);
33053 OMP_CLAUSE_CHAIN (c
) = list
;
33058 num_teams ( expression ) */
33061 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33062 location_t location
)
33066 matching_parens parens
;
33067 if (!parens
.require_open (parser
))
33070 t
= cp_parser_expression (parser
);
33072 if (t
== error_mark_node
33073 || !parens
.require_close (parser
))
33074 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33075 /*or_comma=*/false,
33076 /*consume_paren=*/true);
33078 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33079 "num_teams", location
);
33081 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33082 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33083 OMP_CLAUSE_CHAIN (c
) = list
;
33089 thread_limit ( expression ) */
33092 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33093 location_t location
)
33097 matching_parens parens
;
33098 if (!parens
.require_open (parser
))
33101 t
= cp_parser_expression (parser
);
33103 if (t
== error_mark_node
33104 || !parens
.require_close (parser
))
33105 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33106 /*or_comma=*/false,
33107 /*consume_paren=*/true);
33109 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33110 "thread_limit", location
);
33112 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33113 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33114 OMP_CLAUSE_CHAIN (c
) = list
;
33120 aligned ( variable-list )
33121 aligned ( variable-list : constant-expression ) */
33124 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33126 tree nlist
, c
, alignment
= NULL_TREE
;
33129 matching_parens parens
;
33130 if (!parens
.require_open (parser
))
33133 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33138 alignment
= cp_parser_constant_expression (parser
);
33140 if (!parens
.require_close (parser
))
33141 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33142 /*or_comma=*/false,
33143 /*consume_paren=*/true);
33145 if (alignment
== error_mark_node
)
33146 alignment
= NULL_TREE
;
33149 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33150 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33156 linear ( variable-list )
33157 linear ( variable-list : expression )
33160 linear ( modifier ( variable-list ) )
33161 linear ( modifier ( variable-list ) : expression ) */
33164 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33167 tree nlist
, c
, step
= integer_one_node
;
33169 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33171 matching_parens parens
;
33172 if (!parens
.require_open (parser
))
33175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33177 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33178 const char *p
= IDENTIFIER_POINTER (id
);
33180 if (strcmp ("ref", p
) == 0)
33181 kind
= OMP_CLAUSE_LINEAR_REF
;
33182 else if (strcmp ("val", p
) == 0)
33183 kind
= OMP_CLAUSE_LINEAR_VAL
;
33184 else if (strcmp ("uval", p
) == 0)
33185 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33186 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33187 cp_lexer_consume_token (parser
->lexer
);
33189 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33192 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33193 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33197 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33198 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33200 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33201 else if (!parens
.require_close (parser
))
33202 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33203 /*or_comma=*/false,
33204 /*consume_paren=*/true);
33211 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33212 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33214 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33215 cp_parser_parse_tentatively (parser
);
33216 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33217 /*check_dependency_p=*/true,
33218 /*template_p=*/NULL
,
33219 /*declarator_p=*/false,
33220 /*optional_p=*/false);
33221 if (step
!= error_mark_node
)
33222 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33223 if (step
== error_mark_node
)
33226 cp_parser_abort_tentative_parse (parser
);
33228 else if (!cp_parser_parse_definitely (parser
))
33232 step
= cp_parser_expression (parser
);
33234 if (!parens
.require_close (parser
))
33235 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33236 /*or_comma=*/false,
33237 /*consume_paren=*/true);
33239 if (step
== error_mark_node
)
33243 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33245 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33246 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33253 safelen ( constant-expression ) */
33256 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33257 location_t location
)
33261 matching_parens parens
;
33262 if (!parens
.require_open (parser
))
33265 t
= cp_parser_constant_expression (parser
);
33267 if (t
== error_mark_node
33268 || !parens
.require_close (parser
))
33269 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33270 /*or_comma=*/false,
33271 /*consume_paren=*/true);
33273 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33275 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33276 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33277 OMP_CLAUSE_CHAIN (c
) = list
;
33283 simdlen ( constant-expression ) */
33286 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33287 location_t location
)
33291 matching_parens parens
;
33292 if (!parens
.require_open (parser
))
33295 t
= cp_parser_constant_expression (parser
);
33297 if (t
== error_mark_node
33298 || !parens
.require_close (parser
))
33299 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33300 /*or_comma=*/false,
33301 /*consume_paren=*/true);
33303 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33305 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33306 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33307 OMP_CLAUSE_CHAIN (c
) = list
;
33314 identifier [+/- integer]
33315 vec , identifier [+/- integer]
33319 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33324 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33326 cp_parser_error (parser
, "expected identifier");
33330 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33332 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33333 tree t
, identifier
= cp_parser_identifier (parser
);
33334 tree addend
= NULL
;
33336 if (identifier
== error_mark_node
)
33337 t
= error_mark_node
;
33340 t
= cp_parser_lookup_name_simple
33341 (parser
, identifier
,
33342 cp_lexer_peek_token (parser
->lexer
)->location
);
33343 if (t
== error_mark_node
)
33344 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33349 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33351 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33353 addend
= integer_zero_node
;
33354 goto add_to_vector
;
33356 cp_lexer_consume_token (parser
->lexer
);
33358 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33360 cp_parser_error (parser
, "expected integer");
33364 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33365 if (TREE_CODE (addend
) != INTEGER_CST
)
33367 cp_parser_error (parser
, "expected integer");
33370 cp_lexer_consume_token (parser
->lexer
);
33373 if (t
!= error_mark_node
)
33375 vec
= tree_cons (addend
, t
, vec
);
33377 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33380 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33383 cp_lexer_consume_token (parser
->lexer
);
33386 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33388 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33389 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33390 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33391 OMP_CLAUSE_CHAIN (u
) = list
;
33398 depend ( depend-kind : variable-list )
33406 depend ( sink : vec ) */
33409 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33412 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33414 matching_parens parens
;
33415 if (!parens
.require_open (parser
))
33418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33420 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33421 const char *p
= IDENTIFIER_POINTER (id
);
33423 if (strcmp ("in", p
) == 0)
33424 kind
= OMP_CLAUSE_DEPEND_IN
;
33425 else if (strcmp ("inout", p
) == 0)
33426 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33427 else if (strcmp ("out", p
) == 0)
33428 kind
= OMP_CLAUSE_DEPEND_OUT
;
33429 else if (strcmp ("source", p
) == 0)
33430 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33431 else if (strcmp ("sink", p
) == 0)
33432 kind
= OMP_CLAUSE_DEPEND_SINK
;
33439 cp_lexer_consume_token (parser
->lexer
);
33441 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33443 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33444 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33445 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33446 OMP_CLAUSE_CHAIN (c
) = list
;
33447 if (!parens
.require_close (parser
))
33448 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33449 /*or_comma=*/false,
33450 /*consume_paren=*/true);
33454 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33457 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33458 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33461 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33464 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33465 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33470 cp_parser_error (parser
, "invalid depend kind");
33472 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33473 /*or_comma=*/false,
33474 /*consume_paren=*/true);
33479 map ( map-kind : variable-list )
33480 map ( variable-list )
33483 alloc | to | from | tofrom
33487 alloc | to | from | tofrom | release | delete
33489 map ( always [,] map-kind: variable-list ) */
33492 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33495 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33496 bool always
= false;
33498 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33501 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33503 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33504 const char *p
= IDENTIFIER_POINTER (id
);
33506 if (strcmp ("always", p
) == 0)
33509 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33511 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33512 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33514 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33518 cp_lexer_consume_token (parser
->lexer
);
33520 cp_lexer_consume_token (parser
->lexer
);
33525 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33526 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33528 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33529 const char *p
= IDENTIFIER_POINTER (id
);
33531 if (strcmp ("alloc", p
) == 0)
33532 kind
= GOMP_MAP_ALLOC
;
33533 else if (strcmp ("to", p
) == 0)
33534 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33535 else if (strcmp ("from", p
) == 0)
33536 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33537 else if (strcmp ("tofrom", p
) == 0)
33538 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33539 else if (strcmp ("release", p
) == 0)
33540 kind
= GOMP_MAP_RELEASE
;
33543 cp_parser_error (parser
, "invalid map kind");
33544 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33545 /*or_comma=*/false,
33546 /*consume_paren=*/true);
33549 cp_lexer_consume_token (parser
->lexer
);
33550 cp_lexer_consume_token (parser
->lexer
);
33552 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33553 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33555 kind
= GOMP_MAP_DELETE
;
33556 cp_lexer_consume_token (parser
->lexer
);
33557 cp_lexer_consume_token (parser
->lexer
);
33560 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33563 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33564 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33570 device ( expression ) */
33573 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33574 location_t location
)
33578 matching_parens parens
;
33579 if (!parens
.require_open (parser
))
33582 t
= cp_parser_expression (parser
);
33584 if (t
== error_mark_node
33585 || !parens
.require_close (parser
))
33586 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33587 /*or_comma=*/false,
33588 /*consume_paren=*/true);
33590 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33591 "device", location
);
33593 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33594 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33595 OMP_CLAUSE_CHAIN (c
) = list
;
33601 dist_schedule ( static )
33602 dist_schedule ( static , expression ) */
33605 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33606 location_t location
)
33610 matching_parens parens
;
33611 if (!parens
.require_open (parser
))
33614 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33616 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33618 cp_lexer_consume_token (parser
->lexer
);
33620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33622 cp_lexer_consume_token (parser
->lexer
);
33624 t
= cp_parser_assignment_expression (parser
);
33626 if (t
== error_mark_node
)
33628 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33630 if (!parens
.require_close (parser
))
33633 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33636 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33638 OMP_CLAUSE_CHAIN (c
) = list
;
33642 cp_parser_error (parser
, "invalid dist_schedule kind");
33644 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33645 /*or_comma=*/false,
33646 /*consume_paren=*/true);
33651 proc_bind ( proc-bind-kind )
33654 master | close | spread */
33657 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33658 location_t location
)
33661 enum omp_clause_proc_bind_kind kind
;
33663 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33666 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33668 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33669 const char *p
= IDENTIFIER_POINTER (id
);
33671 if (strcmp ("master", p
) == 0)
33672 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33673 else if (strcmp ("close", p
) == 0)
33674 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33675 else if (strcmp ("spread", p
) == 0)
33676 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33683 cp_lexer_consume_token (parser
->lexer
);
33684 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33687 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33688 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33690 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33691 OMP_CLAUSE_CHAIN (c
) = list
;
33695 cp_parser_error (parser
, "invalid depend kind");
33697 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33698 /*or_comma=*/false,
33699 /*consume_paren=*/true);
33704 async [( int-expr )] */
33707 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33710 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33712 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33714 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33716 matching_parens parens
;
33717 parens
.consume_open (parser
);
33719 t
= cp_parser_expression (parser
);
33720 if (t
== error_mark_node
33721 || !parens
.require_close (parser
))
33722 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33723 /*or_comma=*/false,
33724 /*consume_paren=*/true);
33727 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33729 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33730 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33731 OMP_CLAUSE_CHAIN (c
) = list
;
33737 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33738 is a bitmask in MASK. Return the list of clauses found. */
33741 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33742 const char *where
, cp_token
*pragma_tok
,
33743 bool finish_p
= true)
33745 tree clauses
= NULL
;
33748 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33751 pragma_omp_clause c_kind
;
33752 omp_clause_code code
;
33753 const char *c_name
;
33754 tree prev
= clauses
;
33756 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33757 cp_lexer_consume_token (parser
->lexer
);
33759 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33760 c_kind
= cp_parser_omp_clause_name (parser
);
33764 case PRAGMA_OACC_CLAUSE_ASYNC
:
33765 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33768 case PRAGMA_OACC_CLAUSE_AUTO
:
33769 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33773 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33774 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33775 c_name
= "collapse";
33777 case PRAGMA_OACC_CLAUSE_COPY
:
33778 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33781 case PRAGMA_OACC_CLAUSE_COPYIN
:
33782 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33785 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33786 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33787 c_name
= "copyout";
33789 case PRAGMA_OACC_CLAUSE_CREATE
:
33790 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33793 case PRAGMA_OACC_CLAUSE_DELETE
:
33794 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33797 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33798 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33799 c_name
= "default";
33801 case PRAGMA_OACC_CLAUSE_DEVICE
:
33802 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33805 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33806 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33807 c_name
= "deviceptr";
33809 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33810 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33811 c_name
= "device_resident";
33813 case PRAGMA_OACC_CLAUSE_FINALIZE
:
33814 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_FINALIZE
,
33816 c_name
= "finalize";
33818 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33819 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33821 c_name
= "firstprivate";
33823 case PRAGMA_OACC_CLAUSE_GANG
:
33825 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33828 case PRAGMA_OACC_CLAUSE_HOST
:
33829 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33832 case PRAGMA_OACC_CLAUSE_IF
:
33833 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33836 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
33837 clauses
= cp_parser_oacc_simple_clause (parser
,
33838 OMP_CLAUSE_IF_PRESENT
,
33840 c_name
= "if_present";
33842 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33843 clauses
= cp_parser_oacc_simple_clause (parser
,
33844 OMP_CLAUSE_INDEPENDENT
,
33846 c_name
= "independent";
33848 case PRAGMA_OACC_CLAUSE_LINK
:
33849 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33852 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33853 code
= OMP_CLAUSE_NUM_GANGS
;
33854 c_name
= "num_gangs";
33855 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33858 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33859 c_name
= "num_workers";
33860 code
= OMP_CLAUSE_NUM_WORKERS
;
33861 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33864 case PRAGMA_OACC_CLAUSE_PRESENT
:
33865 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33866 c_name
= "present";
33868 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33869 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33871 c_name
= "private";
33873 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33874 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33875 c_name
= "reduction";
33877 case PRAGMA_OACC_CLAUSE_SEQ
:
33878 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33882 case PRAGMA_OACC_CLAUSE_TILE
:
33883 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33886 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33887 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33889 c_name
= "use_device";
33891 case PRAGMA_OACC_CLAUSE_VECTOR
:
33893 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33896 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33897 c_name
= "vector_length";
33898 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33899 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33902 case PRAGMA_OACC_CLAUSE_WAIT
:
33903 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33906 case PRAGMA_OACC_CLAUSE_WORKER
:
33908 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33912 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33918 if (((mask
>> c_kind
) & 1) == 0)
33920 /* Remove the invalid clause(s) from the list to avoid
33921 confusing the rest of the compiler. */
33923 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33928 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33931 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33936 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33937 is a bitmask in MASK. Return the list of clauses found; the result
33938 of clause default goes in *pdefault. */
33941 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33942 const char *where
, cp_token
*pragma_tok
,
33943 bool finish_p
= true)
33945 tree clauses
= NULL
;
33947 cp_token
*token
= NULL
;
33949 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33951 pragma_omp_clause c_kind
;
33952 const char *c_name
;
33953 tree prev
= clauses
;
33955 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33956 cp_lexer_consume_token (parser
->lexer
);
33958 token
= cp_lexer_peek_token (parser
->lexer
);
33959 c_kind
= cp_parser_omp_clause_name (parser
);
33963 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33964 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33966 c_name
= "collapse";
33968 case PRAGMA_OMP_CLAUSE_COPYIN
:
33969 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33972 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33973 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33975 c_name
= "copyprivate";
33977 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33978 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33979 token
->location
, false);
33980 c_name
= "default";
33982 case PRAGMA_OMP_CLAUSE_FINAL
:
33983 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33986 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33987 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33989 c_name
= "firstprivate";
33991 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33992 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33994 c_name
= "grainsize";
33996 case PRAGMA_OMP_CLAUSE_HINT
:
33997 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
34001 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
34002 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
34004 c_name
= "defaultmap";
34006 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
34007 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34009 c_name
= "use_device_ptr";
34011 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
34012 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
34014 c_name
= "is_device_ptr";
34016 case PRAGMA_OMP_CLAUSE_IF
:
34017 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
34021 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
34022 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
34024 c_name
= "lastprivate";
34026 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
34027 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
34029 c_name
= "mergeable";
34031 case PRAGMA_OMP_CLAUSE_NOWAIT
:
34032 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
34035 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
34036 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
34038 c_name
= "num_tasks";
34040 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
34041 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
34043 c_name
= "num_threads";
34045 case PRAGMA_OMP_CLAUSE_ORDERED
:
34046 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
34048 c_name
= "ordered";
34050 case PRAGMA_OMP_CLAUSE_PRIORITY
:
34051 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
34053 c_name
= "priority";
34055 case PRAGMA_OMP_CLAUSE_PRIVATE
:
34056 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34058 c_name
= "private";
34060 case PRAGMA_OMP_CLAUSE_REDUCTION
:
34061 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
34062 c_name
= "reduction";
34064 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
34065 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
34067 c_name
= "schedule";
34069 case PRAGMA_OMP_CLAUSE_SHARED
:
34070 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
34074 case PRAGMA_OMP_CLAUSE_UNTIED
:
34075 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
34079 case PRAGMA_OMP_CLAUSE_INBRANCH
:
34080 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
34081 clauses
, token
->location
);
34082 c_name
= "inbranch";
34084 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
34085 clauses
= cp_parser_omp_clause_branch (parser
,
34086 OMP_CLAUSE_NOTINBRANCH
,
34087 clauses
, token
->location
);
34088 c_name
= "notinbranch";
34090 case PRAGMA_OMP_CLAUSE_PARALLEL
:
34091 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
34092 clauses
, token
->location
);
34093 c_name
= "parallel";
34097 error_at (token
->location
, "%qs must be the first clause of %qs",
34102 case PRAGMA_OMP_CLAUSE_FOR
:
34103 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34104 clauses
, token
->location
);
34107 goto clause_not_first
;
34109 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34110 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34111 clauses
, token
->location
);
34112 c_name
= "sections";
34114 goto clause_not_first
;
34116 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34117 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34118 clauses
, token
->location
);
34119 c_name
= "taskgroup";
34121 goto clause_not_first
;
34123 case PRAGMA_OMP_CLAUSE_LINK
:
34124 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34127 case PRAGMA_OMP_CLAUSE_TO
:
34128 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34129 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34132 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34135 case PRAGMA_OMP_CLAUSE_FROM
:
34136 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34139 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34140 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34142 c_name
= "uniform";
34144 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34145 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34147 c_name
= "num_teams";
34149 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34150 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34152 c_name
= "thread_limit";
34154 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34155 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34156 c_name
= "aligned";
34158 case PRAGMA_OMP_CLAUSE_LINEAR
:
34160 bool declare_simd
= false;
34161 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34162 declare_simd
= true;
34163 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34167 case PRAGMA_OMP_CLAUSE_DEPEND
:
34168 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34172 case PRAGMA_OMP_CLAUSE_MAP
:
34173 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34176 case PRAGMA_OMP_CLAUSE_DEVICE
:
34177 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34181 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34182 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34184 c_name
= "dist_schedule";
34186 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34187 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34189 c_name
= "proc_bind";
34191 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34192 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34194 c_name
= "safelen";
34196 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34197 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34199 c_name
= "simdlen";
34201 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34202 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34204 c_name
= "nogroup";
34206 case PRAGMA_OMP_CLAUSE_THREADS
:
34208 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34209 clauses
, token
->location
);
34210 c_name
= "threads";
34212 case PRAGMA_OMP_CLAUSE_SIMD
:
34214 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34215 clauses
, token
->location
);
34219 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34225 if (((mask
>> c_kind
) & 1) == 0)
34227 /* Remove the invalid clause(s) from the list to avoid
34228 confusing the rest of the compiler. */
34230 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34234 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34237 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34238 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34240 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34249 In practice, we're also interested in adding the statement to an
34250 outer node. So it is convenient if we work around the fact that
34251 cp_parser_statement calls add_stmt. */
34254 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34256 unsigned save
= parser
->in_statement
;
34258 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34259 This preserves the "not within loop or switch" style error messages
34260 for nonsense cases like
34266 if (parser
->in_statement
)
34267 parser
->in_statement
= IN_OMP_BLOCK
;
34273 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34275 parser
->in_statement
= save
;
34279 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34281 tree stmt
= begin_omp_structured_block ();
34282 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34284 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34286 cp_parser_end_omp_structured_block (parser
, save
);
34287 return finish_omp_structured_block (stmt
);
34291 # pragma omp atomic new-line
34295 x binop= expr | x++ | ++x | x-- | --x
34297 +, *, -, /, &, ^, |, <<, >>
34299 where x is an lvalue expression with scalar type.
34302 # pragma omp atomic new-line
34305 # pragma omp atomic read new-line
34308 # pragma omp atomic write new-line
34311 # pragma omp atomic update new-line
34314 # pragma omp atomic capture new-line
34317 # pragma omp atomic capture new-line
34325 expression-stmt | x = x binop expr
34327 v = expression-stmt
34329 { v = x; update-stmt; } | { update-stmt; v = x; }
34333 expression-stmt | x = x binop expr | x = expr binop x
34337 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34339 where x and v are lvalue expressions with scalar type. */
34342 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34344 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34345 tree rhs1
= NULL_TREE
, orig_lhs
;
34346 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34347 bool structured_block
= false;
34348 bool seq_cst
= false;
34350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34352 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34353 const char *p
= IDENTIFIER_POINTER (id
);
34355 if (!strcmp (p
, "seq_cst"))
34358 cp_lexer_consume_token (parser
->lexer
);
34359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34360 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34361 cp_lexer_consume_token (parser
->lexer
);
34364 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34366 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34367 const char *p
= IDENTIFIER_POINTER (id
);
34369 if (!strcmp (p
, "read"))
34370 code
= OMP_ATOMIC_READ
;
34371 else if (!strcmp (p
, "write"))
34373 else if (!strcmp (p
, "update"))
34375 else if (!strcmp (p
, "capture"))
34376 code
= OMP_ATOMIC_CAPTURE_NEW
;
34380 cp_lexer_consume_token (parser
->lexer
);
34384 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34385 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34386 cp_lexer_consume_token (parser
->lexer
);
34388 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34390 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34391 const char *p
= IDENTIFIER_POINTER (id
);
34393 if (!strcmp (p
, "seq_cst"))
34396 cp_lexer_consume_token (parser
->lexer
);
34400 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34404 case OMP_ATOMIC_READ
:
34405 case NOP_EXPR
: /* atomic write */
34406 v
= cp_parser_unary_expression (parser
);
34407 if (v
== error_mark_node
)
34409 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34411 if (code
== NOP_EXPR
)
34412 lhs
= cp_parser_expression (parser
);
34414 lhs
= cp_parser_unary_expression (parser
);
34415 if (lhs
== error_mark_node
)
34417 if (code
== NOP_EXPR
)
34419 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34427 case OMP_ATOMIC_CAPTURE_NEW
:
34428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34430 cp_lexer_consume_token (parser
->lexer
);
34431 structured_block
= true;
34435 v
= cp_parser_unary_expression (parser
);
34436 if (v
== error_mark_node
)
34438 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34446 lhs
= cp_parser_unary_expression (parser
);
34448 switch (TREE_CODE (lhs
))
34453 case POSTINCREMENT_EXPR
:
34454 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34455 code
= OMP_ATOMIC_CAPTURE_OLD
;
34457 case PREINCREMENT_EXPR
:
34458 lhs
= TREE_OPERAND (lhs
, 0);
34459 opcode
= PLUS_EXPR
;
34460 rhs
= integer_one_node
;
34463 case POSTDECREMENT_EXPR
:
34464 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34465 code
= OMP_ATOMIC_CAPTURE_OLD
;
34467 case PREDECREMENT_EXPR
:
34468 lhs
= TREE_OPERAND (lhs
, 0);
34469 opcode
= MINUS_EXPR
;
34470 rhs
= integer_one_node
;
34473 case COMPOUND_EXPR
:
34474 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34475 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34476 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34477 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34478 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34479 (TREE_OPERAND (lhs
, 1), 0), 0)))
34481 /* Undo effects of boolean_increment for post {in,de}crement. */
34482 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34485 if (TREE_CODE (lhs
) == MODIFY_EXPR
34486 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34488 /* Undo effects of boolean_increment. */
34489 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34491 /* This is pre or post increment. */
34492 rhs
= TREE_OPERAND (lhs
, 1);
34493 lhs
= TREE_OPERAND (lhs
, 0);
34495 if (code
== OMP_ATOMIC_CAPTURE_NEW
34496 && !structured_block
34497 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34498 code
= OMP_ATOMIC_CAPTURE_OLD
;
34504 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34507 opcode
= MULT_EXPR
;
34510 opcode
= TRUNC_DIV_EXPR
;
34513 opcode
= PLUS_EXPR
;
34516 opcode
= MINUS_EXPR
;
34518 case CPP_LSHIFT_EQ
:
34519 opcode
= LSHIFT_EXPR
;
34521 case CPP_RSHIFT_EQ
:
34522 opcode
= RSHIFT_EXPR
;
34525 opcode
= BIT_AND_EXPR
;
34528 opcode
= BIT_IOR_EXPR
;
34531 opcode
= BIT_XOR_EXPR
;
34534 enum cp_parser_prec oprec
;
34536 cp_lexer_consume_token (parser
->lexer
);
34537 cp_parser_parse_tentatively (parser
);
34538 rhs1
= cp_parser_simple_cast_expression (parser
);
34539 if (rhs1
== error_mark_node
)
34541 cp_parser_abort_tentative_parse (parser
);
34542 cp_parser_simple_cast_expression (parser
);
34545 token
= cp_lexer_peek_token (parser
->lexer
);
34546 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34548 cp_parser_abort_tentative_parse (parser
);
34549 cp_parser_parse_tentatively (parser
);
34550 rhs
= cp_parser_binary_expression (parser
, false, true,
34551 PREC_NOT_OPERATOR
, NULL
);
34552 if (rhs
== error_mark_node
)
34554 cp_parser_abort_tentative_parse (parser
);
34555 cp_parser_binary_expression (parser
, false, true,
34556 PREC_NOT_OPERATOR
, NULL
);
34559 switch (TREE_CODE (rhs
))
34562 case TRUNC_DIV_EXPR
:
34571 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34573 if (cp_parser_parse_definitely (parser
))
34575 opcode
= TREE_CODE (rhs
);
34576 rhs1
= TREE_OPERAND (rhs
, 0);
34577 rhs
= TREE_OPERAND (rhs
, 1);
34587 cp_parser_abort_tentative_parse (parser
);
34588 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34590 rhs
= cp_parser_expression (parser
);
34591 if (rhs
== error_mark_node
)
34597 cp_parser_error (parser
,
34598 "invalid form of %<#pragma omp atomic%>");
34601 if (!cp_parser_parse_definitely (parser
))
34603 switch (token
->type
)
34605 case CPP_SEMICOLON
:
34606 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34608 code
= OMP_ATOMIC_CAPTURE_OLD
;
34613 cp_lexer_consume_token (parser
->lexer
);
34616 else if (structured_block
)
34623 cp_parser_error (parser
,
34624 "invalid form of %<#pragma omp atomic%>");
34627 opcode
= MULT_EXPR
;
34630 opcode
= TRUNC_DIV_EXPR
;
34633 opcode
= PLUS_EXPR
;
34636 opcode
= MINUS_EXPR
;
34639 opcode
= LSHIFT_EXPR
;
34642 opcode
= RSHIFT_EXPR
;
34645 opcode
= BIT_AND_EXPR
;
34648 opcode
= BIT_IOR_EXPR
;
34651 opcode
= BIT_XOR_EXPR
;
34654 cp_parser_error (parser
,
34655 "invalid operator for %<#pragma omp atomic%>");
34658 oprec
= TOKEN_PRECEDENCE (token
);
34659 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34660 if (commutative_tree_code (opcode
))
34661 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34662 cp_lexer_consume_token (parser
->lexer
);
34663 rhs
= cp_parser_binary_expression (parser
, false, false,
34665 if (rhs
== error_mark_node
)
34670 cp_parser_error (parser
,
34671 "invalid operator for %<#pragma omp atomic%>");
34674 cp_lexer_consume_token (parser
->lexer
);
34676 rhs
= cp_parser_expression (parser
);
34677 if (rhs
== error_mark_node
)
34682 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34684 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34686 v
= cp_parser_unary_expression (parser
);
34687 if (v
== error_mark_node
)
34689 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34691 lhs1
= cp_parser_unary_expression (parser
);
34692 if (lhs1
== error_mark_node
)
34695 if (structured_block
)
34697 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34698 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34701 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34702 if (!structured_block
)
34703 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34707 cp_parser_skip_to_end_of_block_or_statement (parser
);
34708 if (structured_block
)
34710 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34711 cp_lexer_consume_token (parser
->lexer
);
34712 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34714 cp_parser_skip_to_end_of_block_or_statement (parser
);
34715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34716 cp_lexer_consume_token (parser
->lexer
);
34723 # pragma omp barrier new-line */
34726 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34728 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34729 finish_omp_barrier ();
34733 # pragma omp critical [(name)] new-line
34737 # pragma omp critical [(name) [hint(expression)]] new-line
34738 structured-block */
34740 #define OMP_CRITICAL_CLAUSE_MASK \
34741 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34744 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34746 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34748 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34750 matching_parens parens
;
34751 parens
.consume_open (parser
);
34753 name
= cp_parser_identifier (parser
);
34755 if (name
== error_mark_node
34756 || !parens
.require_close (parser
))
34757 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34758 /*or_comma=*/false,
34759 /*consume_paren=*/true);
34760 if (name
== error_mark_node
)
34763 clauses
= cp_parser_omp_all_clauses (parser
,
34764 OMP_CRITICAL_CLAUSE_MASK
,
34765 "#pragma omp critical", pragma_tok
);
34768 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34770 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34771 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34775 # pragma omp flush flush-vars[opt] new-line
34778 ( variable-list ) */
34781 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34784 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34785 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34787 finish_omp_flush ();
34790 /* Helper function, to parse omp for increment expression. */
34793 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34795 tree cond
= cp_parser_binary_expression (parser
, false, true,
34796 PREC_NOT_OPERATOR
, NULL
);
34797 if (cond
== error_mark_node
34798 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34800 cp_parser_skip_to_end_of_statement (parser
);
34801 return error_mark_node
;
34804 switch (TREE_CODE (cond
))
34812 /* Fall through: OpenMP disallows NE_EXPR. */
34813 gcc_fallthrough ();
34815 return error_mark_node
;
34818 /* If decl is an iterator, preserve LHS and RHS of the relational
34819 expr until finish_omp_for. */
34821 && (type_dependent_expression_p (decl
)
34822 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34825 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
34827 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34828 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34829 /*overload=*/NULL
, tf_warning_or_error
);
34832 /* Helper function, to parse omp for increment expression. */
34835 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34837 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34843 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34845 op
= (token
->type
== CPP_PLUS_PLUS
34846 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34847 cp_lexer_consume_token (parser
->lexer
);
34848 lhs
= cp_parser_simple_cast_expression (parser
);
34850 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34851 return error_mark_node
;
34852 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34855 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34857 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34858 return error_mark_node
;
34860 token
= cp_lexer_peek_token (parser
->lexer
);
34861 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34863 op
= (token
->type
== CPP_PLUS_PLUS
34864 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34865 cp_lexer_consume_token (parser
->lexer
);
34866 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34869 op
= cp_parser_assignment_operator_opt (parser
);
34870 if (op
== ERROR_MARK
)
34871 return error_mark_node
;
34873 if (op
!= NOP_EXPR
)
34875 rhs
= cp_parser_assignment_expression (parser
);
34876 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34877 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34880 lhs
= cp_parser_binary_expression (parser
, false, false,
34881 PREC_ADDITIVE_EXPRESSION
, NULL
);
34882 token
= cp_lexer_peek_token (parser
->lexer
);
34883 decl_first
= (lhs
== decl
34884 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34887 if (token
->type
!= CPP_PLUS
34888 && token
->type
!= CPP_MINUS
)
34889 return error_mark_node
;
34893 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34894 cp_lexer_consume_token (parser
->lexer
);
34895 rhs
= cp_parser_binary_expression (parser
, false, false,
34896 PREC_ADDITIVE_EXPRESSION
, NULL
);
34897 token
= cp_lexer_peek_token (parser
->lexer
);
34898 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34900 if (lhs
== NULL_TREE
)
34902 if (op
== PLUS_EXPR
)
34905 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34906 tf_warning_or_error
);
34909 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34910 ERROR_MARK
, NULL
, tf_warning_or_error
);
34913 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34918 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34919 || op
== MINUS_EXPR
)
34920 return error_mark_node
;
34921 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34924 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34926 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34929 /* Parse the initialization statement of an OpenMP for loop.
34931 Return true if the resulting construct should have an
34932 OMP_CLAUSE_PRIVATE added to it. */
34935 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34936 tree
&this_pre_body
,
34937 vec
<tree
, va_gc
> *&for_block
,
34943 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34946 tree add_private_clause
= NULL_TREE
;
34948 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34952 integer-type var = lb
34953 random-access-iterator-type var = lb
34954 pointer-type var = lb
34956 cp_decl_specifier_seq type_specifiers
;
34958 /* First, try to parse as an initialized declaration. See
34959 cp_parser_condition, from whence the bulk of this is copied. */
34961 cp_parser_parse_tentatively (parser
);
34962 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34963 /*is_trailing_return=*/false,
34965 if (cp_parser_parse_definitely (parser
))
34967 /* If parsing a type specifier seq succeeded, then this
34968 MUST be a initialized declaration. */
34969 tree asm_specification
, attributes
;
34970 cp_declarator
*declarator
;
34972 declarator
= cp_parser_declarator (parser
,
34973 CP_PARSER_DECLARATOR_NAMED
,
34974 /*ctor_dtor_or_conv_p=*/NULL
,
34975 /*parenthesized_p=*/NULL
,
34976 /*member_p=*/false,
34977 /*friend_p=*/false);
34978 attributes
= cp_parser_attributes_opt (parser
);
34979 asm_specification
= cp_parser_asm_specification_opt (parser
);
34981 if (declarator
== cp_error_declarator
)
34982 cp_parser_skip_to_end_of_statement (parser
);
34986 tree pushed_scope
, auto_node
;
34988 decl
= start_decl (declarator
, &type_specifiers
,
34989 SD_INITIALIZED
, attributes
,
34990 /*prefix_attributes=*/NULL_TREE
,
34993 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34994 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34996 if (cp_lexer_next_token_is (parser
->lexer
,
34998 error ("parenthesized initialization is not allowed in "
34999 "OpenMP %<for%> loop");
35001 /* Trigger an error. */
35002 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35004 init
= error_mark_node
;
35005 cp_parser_skip_to_end_of_statement (parser
);
35007 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
35008 || type_dependent_expression_p (decl
)
35011 bool is_direct_init
, is_non_constant_init
;
35013 init
= cp_parser_initializer (parser
,
35015 &is_non_constant_init
);
35020 = do_auto_deduction (TREE_TYPE (decl
), init
,
35023 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
35024 && !type_dependent_expression_p (decl
))
35028 cp_finish_decl (decl
, init
, !is_non_constant_init
,
35030 LOOKUP_ONLYCONVERTING
);
35032 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
35034 vec_safe_push (for_block
, this_pre_body
);
35039 init
= pop_stmt_list (this_pre_body
);
35040 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
35042 tree_stmt_iterator i
= tsi_start (init
);
35043 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35044 while (!tsi_end_p (i
))
35046 tree t
= tsi_stmt (i
);
35047 if (TREE_CODE (t
) == DECL_EXPR
35048 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
35051 vec_safe_push (for_block
, t
);
35056 if (tsi_one_before_end_p (i
))
35058 tree t
= tsi_stmt (i
);
35060 free_stmt_list (init
);
35065 this_pre_body
= NULL_TREE
;
35070 cp_lexer_consume_token (parser
->lexer
);
35071 init
= cp_parser_assignment_expression (parser
);
35074 if (TYPE_REF_P (TREE_TYPE (decl
)))
35075 init
= error_mark_node
;
35077 cp_finish_decl (decl
, NULL_TREE
,
35078 /*init_const_expr_p=*/false,
35080 LOOKUP_ONLYCONVERTING
);
35084 pop_scope (pushed_scope
);
35090 /* If parsing a type specifier sequence failed, then
35091 this MUST be a simple expression. */
35092 cp_parser_parse_tentatively (parser
);
35093 decl
= cp_parser_primary_expression (parser
, false, false,
35095 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
35096 if (!cp_parser_error_occurred (parser
)
35098 && (TREE_CODE (decl
) == COMPONENT_REF
35099 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
35101 cp_parser_abort_tentative_parse (parser
);
35102 cp_parser_parse_tentatively (parser
);
35103 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35104 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35105 /*check_dependency_p=*/true,
35106 /*template_p=*/NULL
,
35107 /*declarator_p=*/false,
35108 /*optional_p=*/false);
35109 if (name
!= error_mark_node
35110 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35112 decl
= cp_parser_lookup_name_simple (parser
, name
,
35114 if (TREE_CODE (decl
) == FIELD_DECL
)
35115 add_private_clause
= omp_privatize_field (decl
, false);
35117 cp_parser_abort_tentative_parse (parser
);
35118 cp_parser_parse_tentatively (parser
);
35119 decl
= cp_parser_primary_expression (parser
, false, false,
35122 if (!cp_parser_error_occurred (parser
)
35125 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35129 cp_parser_parse_definitely (parser
);
35130 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35131 rhs
= cp_parser_assignment_expression (parser
);
35133 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35136 tf_warning_or_error
));
35137 if (!add_private_clause
)
35138 add_private_clause
= decl
;
35143 cp_parser_abort_tentative_parse (parser
);
35144 init
= cp_parser_expression (parser
);
35147 if (TREE_CODE (init
) == MODIFY_EXPR
35148 || TREE_CODE (init
) == MODOP_EXPR
)
35149 real_decl
= TREE_OPERAND (init
, 0);
35153 return add_private_clause
;
35156 /* Parse the restricted form of the for statement allowed by OpenMP. */
35159 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35160 tree
*cclauses
, bool *if_p
)
35162 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35163 tree real_decl
, initv
, condv
, incrv
, declv
;
35164 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35165 location_t loc_first
;
35166 bool collapse_err
= false;
35167 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35168 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35169 auto_vec
<tree
, 4> orig_inits
;
35170 bool tiling
= false;
35172 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35173 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35174 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35175 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35178 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35180 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35181 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35184 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35187 if (ordered
&& ordered
< collapse
)
35189 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35190 "%<ordered%> clause parameter is less than %<collapse%>");
35191 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35192 = build_int_cst (NULL_TREE
, collapse
);
35193 ordered
= collapse
;
35197 for (tree
*pc
= &clauses
; *pc
; )
35198 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35200 error_at (OMP_CLAUSE_LOCATION (*pc
),
35201 "%<linear%> clause may not be specified together "
35202 "with %<ordered%> clause with a parameter");
35203 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35206 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35209 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35210 count
= ordered
? ordered
: collapse
;
35212 declv
= make_tree_vec (count
);
35213 initv
= make_tree_vec (count
);
35214 condv
= make_tree_vec (count
);
35215 incrv
= make_tree_vec (count
);
35217 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35219 for (i
= 0; i
< count
; i
++)
35221 int bracecount
= 0;
35222 tree add_private_clause
= NULL_TREE
;
35225 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35228 cp_parser_error (parser
, "for statement expected");
35231 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35233 matching_parens parens
;
35234 if (!parens
.require_open (parser
))
35237 init
= orig_init
= decl
= real_decl
= NULL
;
35238 this_pre_body
= push_stmt_list ();
35241 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35242 init
, orig_init
, decl
, real_decl
);
35244 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35247 this_pre_body
= pop_stmt_list (this_pre_body
);
35251 pre_body
= push_stmt_list ();
35253 add_stmt (this_pre_body
);
35254 pre_body
= pop_stmt_list (pre_body
);
35257 pre_body
= this_pre_body
;
35262 if (cclauses
!= NULL
35263 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35264 && real_decl
!= NULL_TREE
)
35267 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35268 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35269 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35271 error_at (loc
, "iteration variable %qD"
35272 " should not be firstprivate", real_decl
);
35273 *c
= OMP_CLAUSE_CHAIN (*c
);
35275 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35276 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35278 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35280 *c
= OMP_CLAUSE_CHAIN (*c
);
35281 if (code
== OMP_SIMD
)
35283 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35284 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35288 OMP_CLAUSE_CHAIN (l
) = clauses
;
35291 add_private_clause
= NULL_TREE
;
35295 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35296 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35297 add_private_clause
= NULL_TREE
;
35298 c
= &OMP_CLAUSE_CHAIN (*c
);
35302 if (add_private_clause
)
35305 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35307 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35308 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35309 && OMP_CLAUSE_DECL (c
) == decl
)
35311 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35312 && OMP_CLAUSE_DECL (c
) == decl
)
35313 error_at (loc
, "iteration variable %qD "
35314 "should not be firstprivate",
35316 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35317 && OMP_CLAUSE_DECL (c
) == decl
)
35318 error_at (loc
, "iteration variable %qD should not be reduction",
35323 if (code
!= OMP_SIMD
)
35324 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35325 else if (collapse
== 1)
35326 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35328 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35329 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35330 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35333 OMP_CLAUSE_CHAIN (c
) = clauses
;
35335 /* For linear, signal that we need to fill up
35336 the so far unknown linear step. */
35337 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35338 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35344 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35345 cond
= cp_parser_omp_for_cond (parser
, decl
);
35346 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35349 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35351 /* If decl is an iterator, preserve the operator on decl
35352 until finish_omp_for. */
35354 && ((processing_template_decl
35355 && (TREE_TYPE (real_decl
) == NULL_TREE
35356 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
35357 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35358 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35360 incr
= cp_parser_expression (parser
);
35361 if (!EXPR_HAS_LOCATION (incr
))
35362 protected_set_expr_location (incr
, input_location
);
35365 if (!parens
.require_close (parser
))
35366 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35367 /*or_comma=*/false,
35368 /*consume_paren=*/true);
35370 TREE_VEC_ELT (declv
, i
) = decl
;
35371 TREE_VEC_ELT (initv
, i
) = init
;
35372 TREE_VEC_ELT (condv
, i
) = cond
;
35373 TREE_VEC_ELT (incrv
, i
) = incr
;
35376 orig_inits
.safe_grow_cleared (i
+ 1);
35377 orig_inits
[i
] = orig_init
;
35380 if (i
== count
- 1)
35383 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35384 in between the collapsed for loops to be still considered perfectly
35385 nested. Hopefully the final version clarifies this.
35386 For now handle (multiple) {'s and empty statements. */
35387 cp_parser_parse_tentatively (parser
);
35390 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35392 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35394 cp_lexer_consume_token (parser
->lexer
);
35397 else if (bracecount
35398 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35399 cp_lexer_consume_token (parser
->lexer
);
35402 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35403 error_at (loc
, "not enough for loops to collapse");
35404 collapse_err
= true;
35405 cp_parser_abort_tentative_parse (parser
);
35413 cp_parser_parse_definitely (parser
);
35414 nbraces
+= bracecount
;
35421 /* Note that we saved the original contents of this flag when we entered
35422 the structured block, and so we don't need to re-save it here. */
35423 parser
->in_statement
= IN_OMP_FOR
;
35425 /* Note that the grammar doesn't call for a structured block here,
35426 though the loop as a whole is a structured block. */
35427 body
= push_stmt_list ();
35428 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35429 body
= pop_stmt_list (body
);
35431 if (declv
== NULL_TREE
)
35434 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35435 body
, pre_body
, &orig_inits
, clauses
);
35439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35441 cp_lexer_consume_token (parser
->lexer
);
35444 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35445 cp_lexer_consume_token (parser
->lexer
);
35450 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35451 "collapsed loops not perfectly nested");
35453 collapse_err
= true;
35454 cp_parser_statement_seq_opt (parser
, NULL
);
35455 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35460 while (!for_block
->is_empty ())
35462 tree t
= for_block
->pop ();
35463 if (TREE_CODE (t
) == STATEMENT_LIST
)
35464 add_stmt (pop_stmt_list (t
));
35468 release_tree_vector (for_block
);
35473 /* Helper function for OpenMP parsing, split clauses and call
35474 finish_omp_clauses on each of the set of clauses afterwards. */
35477 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35478 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35481 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35482 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35484 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35488 #pragma omp simd simd-clause[optseq] new-line
35491 #define OMP_SIMD_CLAUSE_MASK \
35492 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35502 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35503 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35506 tree clauses
, sb
, ret
;
35508 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35510 strcat (p_name
, " simd");
35511 mask
|= OMP_SIMD_CLAUSE_MASK
;
35513 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35517 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35518 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35519 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35520 OMP_CLAUSE_ORDERED
);
35521 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35523 error_at (OMP_CLAUSE_LOCATION (c
),
35524 "%<ordered%> clause with parameter may not be specified "
35525 "on %qs construct", p_name
);
35526 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35530 sb
= begin_omp_structured_block ();
35531 save
= cp_parser_begin_omp_structured_block (parser
);
35533 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35535 cp_parser_end_omp_structured_block (parser
, save
);
35536 add_stmt (finish_omp_structured_block (sb
));
35542 #pragma omp for for-clause[optseq] new-line
35546 #pragma omp for simd for-simd-clause[optseq] new-line
35549 #define OMP_FOR_CLAUSE_MASK \
35550 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35561 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35562 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35565 tree clauses
, sb
, ret
;
35567 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35569 strcat (p_name
, " for");
35570 mask
|= OMP_FOR_CLAUSE_MASK
;
35571 /* parallel for{, simd} disallows nowait clause, but for
35572 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35573 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35574 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35575 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35576 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35577 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35581 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35582 const char *p
= IDENTIFIER_POINTER (id
);
35584 if (strcmp (p
, "simd") == 0)
35586 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35587 if (cclauses
== NULL
)
35588 cclauses
= cclauses_buf
;
35590 cp_lexer_consume_token (parser
->lexer
);
35591 if (!flag_openmp
) /* flag_openmp_simd */
35592 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35594 sb
= begin_omp_structured_block ();
35595 save
= cp_parser_begin_omp_structured_block (parser
);
35596 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35598 cp_parser_end_omp_structured_block (parser
, save
);
35599 tree body
= finish_omp_structured_block (sb
);
35602 ret
= make_node (OMP_FOR
);
35603 TREE_TYPE (ret
) = void_type_node
;
35604 OMP_FOR_BODY (ret
) = body
;
35605 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35606 SET_EXPR_LOCATION (ret
, loc
);
35611 if (!flag_openmp
) /* flag_openmp_simd */
35613 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35617 /* Composite distribute parallel for disallows linear clause. */
35618 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35619 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35621 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35625 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35626 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35629 sb
= begin_omp_structured_block ();
35630 save
= cp_parser_begin_omp_structured_block (parser
);
35632 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35634 cp_parser_end_omp_structured_block (parser
, save
);
35635 add_stmt (finish_omp_structured_block (sb
));
35641 # pragma omp master new-line
35642 structured-block */
35645 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35647 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35648 return c_finish_omp_master (input_location
,
35649 cp_parser_omp_structured_block (parser
, if_p
));
35653 # pragma omp ordered new-line
35657 # pragma omp ordered ordered-clauses new-line
35658 structured-block */
35660 #define OMP_ORDERED_CLAUSE_MASK \
35661 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35664 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35665 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35668 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35669 enum pragma_context context
, bool *if_p
)
35671 location_t loc
= pragma_tok
->location
;
35673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35675 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35676 const char *p
= IDENTIFIER_POINTER (id
);
35678 if (strcmp (p
, "depend") == 0)
35680 if (!flag_openmp
) /* flag_openmp_simd */
35682 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35685 if (context
== pragma_stmt
)
35687 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35688 "%<depend%> clause may only be used in compound "
35690 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35694 = cp_parser_omp_all_clauses (parser
,
35695 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35696 "#pragma omp ordered", pragma_tok
);
35697 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35703 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35704 "#pragma omp ordered", pragma_tok
);
35706 if (!flag_openmp
/* flag_openmp_simd */
35707 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35710 c_finish_omp_ordered (loc
, clauses
,
35711 cp_parser_omp_structured_block (parser
, if_p
));
35718 { section-sequence }
35721 section-directive[opt] structured-block
35722 section-sequence section-directive structured-block */
35725 cp_parser_omp_sections_scope (cp_parser
*parser
)
35727 tree stmt
, substmt
;
35728 bool error_suppress
= false;
35731 matching_braces braces
;
35732 if (!braces
.require_open (parser
))
35735 stmt
= push_stmt_list ();
35737 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35738 != PRAGMA_OMP_SECTION
)
35740 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35741 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35742 add_stmt (substmt
);
35747 tok
= cp_lexer_peek_token (parser
->lexer
);
35748 if (tok
->type
== CPP_CLOSE_BRACE
)
35750 if (tok
->type
== CPP_EOF
)
35753 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35755 cp_lexer_consume_token (parser
->lexer
);
35756 cp_parser_require_pragma_eol (parser
, tok
);
35757 error_suppress
= false;
35759 else if (!error_suppress
)
35761 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35762 error_suppress
= true;
35765 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35766 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35767 add_stmt (substmt
);
35769 braces
.require_close (parser
);
35771 substmt
= pop_stmt_list (stmt
);
35773 stmt
= make_node (OMP_SECTIONS
);
35774 TREE_TYPE (stmt
) = void_type_node
;
35775 OMP_SECTIONS_BODY (stmt
) = substmt
;
35782 # pragma omp sections sections-clause[optseq] newline
35785 #define OMP_SECTIONS_CLAUSE_MASK \
35786 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35793 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35794 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35797 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35799 strcat (p_name
, " sections");
35800 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35802 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35804 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35808 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35809 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35812 ret
= cp_parser_omp_sections_scope (parser
);
35814 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35820 # pragma omp parallel parallel-clause[optseq] new-line
35822 # pragma omp parallel for parallel-for-clause[optseq] new-line
35824 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35828 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35829 structured-block */
35831 #define OMP_PARALLEL_CLAUSE_MASK \
35832 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35843 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35844 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35847 tree stmt
, clauses
, block
;
35849 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35851 strcat (p_name
, " parallel");
35852 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35853 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35854 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35855 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35856 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35858 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35860 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35861 if (cclauses
== NULL
)
35862 cclauses
= cclauses_buf
;
35864 cp_lexer_consume_token (parser
->lexer
);
35865 if (!flag_openmp
) /* flag_openmp_simd */
35866 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35868 block
= begin_omp_parallel ();
35869 save
= cp_parser_begin_omp_structured_block (parser
);
35870 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35872 cp_parser_end_omp_structured_block (parser
, save
);
35873 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35875 if (ret
== NULL_TREE
)
35877 OMP_PARALLEL_COMBINED (stmt
) = 1;
35880 /* When combined with distribute, parallel has to be followed by for.
35881 #pragma omp target parallel is allowed though. */
35883 && (mask
& (OMP_CLAUSE_MASK_1
35884 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35886 error_at (loc
, "expected %<for%> after %qs", p_name
);
35887 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35890 else if (!flag_openmp
) /* flag_openmp_simd */
35892 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35895 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35897 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35898 const char *p
= IDENTIFIER_POINTER (id
);
35899 if (strcmp (p
, "sections") == 0)
35901 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35902 cclauses
= cclauses_buf
;
35904 cp_lexer_consume_token (parser
->lexer
);
35905 block
= begin_omp_parallel ();
35906 save
= cp_parser_begin_omp_structured_block (parser
);
35907 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35908 cp_parser_end_omp_structured_block (parser
, save
);
35909 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35911 OMP_PARALLEL_COMBINED (stmt
) = 1;
35916 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35920 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35921 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35924 block
= begin_omp_parallel ();
35925 save
= cp_parser_begin_omp_structured_block (parser
);
35926 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35927 cp_parser_end_omp_structured_block (parser
, save
);
35928 stmt
= finish_omp_parallel (clauses
, block
);
35933 # pragma omp single single-clause[optseq] new-line
35934 structured-block */
35936 #define OMP_SINGLE_CLAUSE_MASK \
35937 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35943 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35945 tree stmt
= make_node (OMP_SINGLE
);
35946 TREE_TYPE (stmt
) = void_type_node
;
35948 OMP_SINGLE_CLAUSES (stmt
)
35949 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35950 "#pragma omp single", pragma_tok
);
35951 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35953 return add_stmt (stmt
);
35957 # pragma omp task task-clause[optseq] new-line
35958 structured-block */
35960 #define OMP_TASK_CLAUSE_MASK \
35961 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35973 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35975 tree clauses
, block
;
35978 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35979 "#pragma omp task", pragma_tok
);
35980 block
= begin_omp_task ();
35981 save
= cp_parser_begin_omp_structured_block (parser
);
35982 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35983 cp_parser_end_omp_structured_block (parser
, save
);
35984 return finish_omp_task (clauses
, block
);
35988 # pragma omp taskwait new-line */
35991 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35993 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35994 finish_omp_taskwait ();
35998 # pragma omp taskyield new-line */
36001 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
36003 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36004 finish_omp_taskyield ();
36008 # pragma omp taskgroup new-line
36009 structured-block */
36012 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36014 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36015 return c_finish_omp_taskgroup (input_location
,
36016 cp_parser_omp_structured_block (parser
,
36022 # pragma omp threadprivate (variable-list) */
36025 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
36029 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36030 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36032 finish_omp_threadprivate (vars
);
36036 # pragma omp cancel cancel-clause[optseq] new-line */
36038 #define OMP_CANCEL_CLAUSE_MASK \
36039 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36046 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
36048 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
36049 "#pragma omp cancel", pragma_tok
);
36050 finish_omp_cancel (clauses
);
36054 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36056 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36057 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36063 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
36064 enum pragma_context context
)
36067 bool point_seen
= false;
36069 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36071 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36072 const char *p
= IDENTIFIER_POINTER (id
);
36074 if (strcmp (p
, "point") == 0)
36076 cp_lexer_consume_token (parser
->lexer
);
36082 cp_parser_error (parser
, "expected %<point%>");
36083 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36087 if (context
!= pragma_compound
)
36089 if (context
== pragma_stmt
)
36090 error_at (pragma_tok
->location
,
36091 "%<#pragma %s%> may only be used in compound statements",
36092 "omp cancellation point");
36094 cp_parser_error (parser
, "expected declaration specifiers");
36095 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36099 clauses
= cp_parser_omp_all_clauses (parser
,
36100 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
36101 "#pragma omp cancellation point",
36103 finish_omp_cancellation_point (clauses
);
36107 #pragma omp distribute distribute-clause[optseq] new-line
36110 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36111 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36118 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
36119 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36122 tree clauses
, sb
, ret
;
36124 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36126 strcat (p_name
, " distribute");
36127 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
36129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36131 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36132 const char *p
= IDENTIFIER_POINTER (id
);
36134 bool parallel
= false;
36136 if (strcmp (p
, "simd") == 0)
36139 parallel
= strcmp (p
, "parallel") == 0;
36140 if (parallel
|| simd
)
36142 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36143 if (cclauses
== NULL
)
36144 cclauses
= cclauses_buf
;
36145 cp_lexer_consume_token (parser
->lexer
);
36146 if (!flag_openmp
) /* flag_openmp_simd */
36149 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36152 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36155 sb
= begin_omp_structured_block ();
36156 save
= cp_parser_begin_omp_structured_block (parser
);
36158 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36161 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36163 cp_parser_end_omp_structured_block (parser
, save
);
36164 tree body
= finish_omp_structured_block (sb
);
36167 ret
= make_node (OMP_DISTRIBUTE
);
36168 TREE_TYPE (ret
) = void_type_node
;
36169 OMP_FOR_BODY (ret
) = body
;
36170 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36171 SET_EXPR_LOCATION (ret
, loc
);
36176 if (!flag_openmp
) /* flag_openmp_simd */
36178 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36182 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36186 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36187 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36190 sb
= begin_omp_structured_block ();
36191 save
= cp_parser_begin_omp_structured_block (parser
);
36193 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36195 cp_parser_end_omp_structured_block (parser
, save
);
36196 add_stmt (finish_omp_structured_block (sb
));
36202 # pragma omp teams teams-clause[optseq] new-line
36203 structured-block */
36205 #define OMP_TEAMS_CLAUSE_MASK \
36206 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36215 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36216 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36219 tree clauses
, sb
, ret
;
36221 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36223 strcat (p_name
, " teams");
36224 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36226 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36228 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36229 const char *p
= IDENTIFIER_POINTER (id
);
36230 if (strcmp (p
, "distribute") == 0)
36232 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36233 if (cclauses
== NULL
)
36234 cclauses
= cclauses_buf
;
36236 cp_lexer_consume_token (parser
->lexer
);
36237 if (!flag_openmp
) /* flag_openmp_simd */
36238 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36240 sb
= begin_omp_structured_block ();
36241 save
= cp_parser_begin_omp_structured_block (parser
);
36242 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36244 cp_parser_end_omp_structured_block (parser
, save
);
36245 tree body
= finish_omp_structured_block (sb
);
36248 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36249 ret
= make_node (OMP_TEAMS
);
36250 TREE_TYPE (ret
) = void_type_node
;
36251 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36252 OMP_TEAMS_BODY (ret
) = body
;
36253 OMP_TEAMS_COMBINED (ret
) = 1;
36254 SET_EXPR_LOCATION (ret
, loc
);
36255 return add_stmt (ret
);
36258 if (!flag_openmp
) /* flag_openmp_simd */
36260 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36264 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36268 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36269 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36272 tree stmt
= make_node (OMP_TEAMS
);
36273 TREE_TYPE (stmt
) = void_type_node
;
36274 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36275 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36276 SET_EXPR_LOCATION (stmt
, loc
);
36278 return add_stmt (stmt
);
36282 # pragma omp target data target-data-clause[optseq] new-line
36283 structured-block */
36285 #define OMP_TARGET_DATA_CLAUSE_MASK \
36286 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36292 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36295 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36296 "#pragma omp target data", pragma_tok
);
36298 for (tree
*pc
= &clauses
; *pc
;)
36300 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36301 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36304 case GOMP_MAP_ALWAYS_TO
:
36305 case GOMP_MAP_FROM
:
36306 case GOMP_MAP_ALWAYS_FROM
:
36307 case GOMP_MAP_TOFROM
:
36308 case GOMP_MAP_ALWAYS_TOFROM
:
36309 case GOMP_MAP_ALLOC
:
36312 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36313 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36314 case GOMP_MAP_ALWAYS_POINTER
:
36318 error_at (OMP_CLAUSE_LOCATION (*pc
),
36319 "%<#pragma omp target data%> with map-type other "
36320 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36321 "on %<map%> clause");
36322 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36325 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36331 error_at (pragma_tok
->location
,
36332 "%<#pragma omp target data%> must contain at least "
36333 "one %<map%> clause");
36337 tree stmt
= make_node (OMP_TARGET_DATA
);
36338 TREE_TYPE (stmt
) = void_type_node
;
36339 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36341 keep_next_level (true);
36342 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36344 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36345 return add_stmt (stmt
);
36349 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36350 structured-block */
36352 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36353 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36360 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36361 enum pragma_context context
)
36363 bool data_seen
= false;
36364 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36366 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36367 const char *p
= IDENTIFIER_POINTER (id
);
36369 if (strcmp (p
, "data") == 0)
36371 cp_lexer_consume_token (parser
->lexer
);
36377 cp_parser_error (parser
, "expected %<data%>");
36378 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36382 if (context
== pragma_stmt
)
36384 error_at (pragma_tok
->location
,
36385 "%<#pragma %s%> may only be used in compound statements",
36386 "omp target enter data");
36387 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36392 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36393 "#pragma omp target enter data", pragma_tok
);
36395 for (tree
*pc
= &clauses
; *pc
;)
36397 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36398 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36401 case GOMP_MAP_ALWAYS_TO
:
36402 case GOMP_MAP_ALLOC
:
36405 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36406 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36407 case GOMP_MAP_ALWAYS_POINTER
:
36411 error_at (OMP_CLAUSE_LOCATION (*pc
),
36412 "%<#pragma omp target enter data%> with map-type other "
36413 "than %<to%> or %<alloc%> on %<map%> clause");
36414 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36417 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36423 error_at (pragma_tok
->location
,
36424 "%<#pragma omp target enter data%> must contain at least "
36425 "one %<map%> clause");
36429 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36430 TREE_TYPE (stmt
) = void_type_node
;
36431 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36432 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36433 return add_stmt (stmt
);
36437 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36438 structured-block */
36440 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36441 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36448 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36449 enum pragma_context context
)
36451 bool data_seen
= false;
36452 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36454 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36455 const char *p
= IDENTIFIER_POINTER (id
);
36457 if (strcmp (p
, "data") == 0)
36459 cp_lexer_consume_token (parser
->lexer
);
36465 cp_parser_error (parser
, "expected %<data%>");
36466 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36470 if (context
== pragma_stmt
)
36472 error_at (pragma_tok
->location
,
36473 "%<#pragma %s%> may only be used in compound statements",
36474 "omp target exit data");
36475 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36480 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36481 "#pragma omp target exit data", pragma_tok
);
36483 for (tree
*pc
= &clauses
; *pc
;)
36485 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36486 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36488 case GOMP_MAP_FROM
:
36489 case GOMP_MAP_ALWAYS_FROM
:
36490 case GOMP_MAP_RELEASE
:
36491 case GOMP_MAP_DELETE
:
36494 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36495 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36496 case GOMP_MAP_ALWAYS_POINTER
:
36500 error_at (OMP_CLAUSE_LOCATION (*pc
),
36501 "%<#pragma omp target exit data%> with map-type other "
36502 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36504 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36507 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36513 error_at (pragma_tok
->location
,
36514 "%<#pragma omp target exit data%> must contain at least "
36515 "one %<map%> clause");
36519 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36520 TREE_TYPE (stmt
) = void_type_node
;
36521 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36522 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36523 return add_stmt (stmt
);
36527 # pragma omp target update target-update-clause[optseq] new-line */
36529 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36530 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36538 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36539 enum pragma_context context
)
36541 if (context
== pragma_stmt
)
36543 error_at (pragma_tok
->location
,
36544 "%<#pragma %s%> may only be used in compound statements",
36545 "omp target update");
36546 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36551 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36552 "#pragma omp target update", pragma_tok
);
36553 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36554 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36556 error_at (pragma_tok
->location
,
36557 "%<#pragma omp target update%> must contain at least one "
36558 "%<from%> or %<to%> clauses");
36562 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36563 TREE_TYPE (stmt
) = void_type_node
;
36564 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36565 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36571 # pragma omp target target-clause[optseq] new-line
36572 structured-block */
36574 #define OMP_TARGET_CLAUSE_MASK \
36575 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36586 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36587 enum pragma_context context
, bool *if_p
)
36589 tree
*pc
= NULL
, stmt
;
36591 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36593 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36594 const char *p
= IDENTIFIER_POINTER (id
);
36595 enum tree_code ccode
= ERROR_MARK
;
36597 if (strcmp (p
, "teams") == 0)
36599 else if (strcmp (p
, "parallel") == 0)
36600 ccode
= OMP_PARALLEL
;
36601 else if (strcmp (p
, "simd") == 0)
36603 if (ccode
!= ERROR_MARK
)
36605 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36606 char p_name
[sizeof ("#pragma omp target teams distribute "
36607 "parallel for simd")];
36609 cp_lexer_consume_token (parser
->lexer
);
36610 strcpy (p_name
, "#pragma omp target");
36611 if (!flag_openmp
) /* flag_openmp_simd */
36617 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36618 OMP_TARGET_CLAUSE_MASK
,
36622 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36623 OMP_TARGET_CLAUSE_MASK
,
36627 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36628 OMP_TARGET_CLAUSE_MASK
,
36632 gcc_unreachable ();
36634 return stmt
!= NULL_TREE
;
36636 keep_next_level (true);
36637 tree sb
= begin_omp_structured_block (), ret
;
36638 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36642 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36643 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36647 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36648 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36652 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36653 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36657 gcc_unreachable ();
36659 cp_parser_end_omp_structured_block (parser
, save
);
36660 tree body
= finish_omp_structured_block (sb
);
36661 if (ret
== NULL_TREE
)
36663 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36665 /* For combined target teams, ensure the num_teams and
36666 thread_limit clause expressions are evaluated on the host,
36667 before entering the target construct. */
36669 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36670 c
; c
= OMP_CLAUSE_CHAIN (c
))
36671 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36672 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36673 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36675 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36676 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36677 if (expr
== error_mark_node
)
36679 tree tmp
= TARGET_EXPR_SLOT (expr
);
36681 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36682 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36683 OMP_CLAUSE_FIRSTPRIVATE
);
36684 OMP_CLAUSE_DECL (tc
) = tmp
;
36685 OMP_CLAUSE_CHAIN (tc
)
36686 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36687 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36690 tree stmt
= make_node (OMP_TARGET
);
36691 TREE_TYPE (stmt
) = void_type_node
;
36692 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36693 OMP_TARGET_BODY (stmt
) = body
;
36694 OMP_TARGET_COMBINED (stmt
) = 1;
36695 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36697 pc
= &OMP_TARGET_CLAUSES (stmt
);
36698 goto check_clauses
;
36700 else if (!flag_openmp
) /* flag_openmp_simd */
36702 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36705 else if (strcmp (p
, "data") == 0)
36707 cp_lexer_consume_token (parser
->lexer
);
36708 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36711 else if (strcmp (p
, "enter") == 0)
36713 cp_lexer_consume_token (parser
->lexer
);
36714 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36717 else if (strcmp (p
, "exit") == 0)
36719 cp_lexer_consume_token (parser
->lexer
);
36720 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36723 else if (strcmp (p
, "update") == 0)
36725 cp_lexer_consume_token (parser
->lexer
);
36726 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36729 if (!flag_openmp
) /* flag_openmp_simd */
36731 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36735 stmt
= make_node (OMP_TARGET
);
36736 TREE_TYPE (stmt
) = void_type_node
;
36738 OMP_TARGET_CLAUSES (stmt
)
36739 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36740 "#pragma omp target", pragma_tok
);
36741 pc
= &OMP_TARGET_CLAUSES (stmt
);
36742 keep_next_level (true);
36743 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36745 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36751 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36752 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36755 case GOMP_MAP_ALWAYS_TO
:
36756 case GOMP_MAP_FROM
:
36757 case GOMP_MAP_ALWAYS_FROM
:
36758 case GOMP_MAP_TOFROM
:
36759 case GOMP_MAP_ALWAYS_TOFROM
:
36760 case GOMP_MAP_ALLOC
:
36761 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36762 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36763 case GOMP_MAP_ALWAYS_POINTER
:
36766 error_at (OMP_CLAUSE_LOCATION (*pc
),
36767 "%<#pragma omp target%> with map-type other "
36768 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36769 "on %<map%> clause");
36770 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36773 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36779 # pragma acc cache (variable-list) new-line
36783 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36785 tree stmt
, clauses
;
36787 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36788 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36790 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36792 stmt
= make_node (OACC_CACHE
);
36793 TREE_TYPE (stmt
) = void_type_node
;
36794 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36795 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36802 # pragma acc data oacc-data-clause[optseq] new-line
36803 structured-block */
36805 #define OACC_DATA_CLAUSE_MASK \
36806 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36815 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36817 tree stmt
, clauses
, block
;
36820 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36821 "#pragma acc data", pragma_tok
);
36823 block
= begin_omp_parallel ();
36824 save
= cp_parser_begin_omp_structured_block (parser
);
36825 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36826 cp_parser_end_omp_structured_block (parser
, save
);
36827 stmt
= finish_oacc_data (clauses
, block
);
36832 # pragma acc host_data <clauses> new-line
36833 structured-block */
36835 #define OACC_HOST_DATA_CLAUSE_MASK \
36836 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36839 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36841 tree stmt
, clauses
, block
;
36844 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36845 "#pragma acc host_data", pragma_tok
);
36847 block
= begin_omp_parallel ();
36848 save
= cp_parser_begin_omp_structured_block (parser
);
36849 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36850 cp_parser_end_omp_structured_block (parser
, save
);
36851 stmt
= finish_oacc_host_data (clauses
, block
);
36856 # pragma acc declare oacc-data-clause[optseq] new-line
36859 #define OACC_DECLARE_CLAUSE_MASK \
36860 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36870 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36872 tree clauses
, stmt
;
36873 bool error
= false;
36875 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36876 "#pragma acc declare", pragma_tok
, true);
36879 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36881 error_at (pragma_tok
->location
,
36882 "no valid clauses specified in %<#pragma acc declare%>");
36886 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36888 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36889 tree decl
= OMP_CLAUSE_DECL (t
);
36890 if (!DECL_P (decl
))
36892 error_at (loc
, "array section in %<#pragma acc declare%>");
36896 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36897 switch (OMP_CLAUSE_MAP_KIND (t
))
36899 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36900 case GOMP_MAP_ALLOC
:
36902 case GOMP_MAP_FORCE_DEVICEPTR
:
36903 case GOMP_MAP_DEVICE_RESIDENT
:
36906 case GOMP_MAP_LINK
:
36907 if (!global_bindings_p ()
36908 && (TREE_STATIC (decl
)
36909 || !DECL_EXTERNAL (decl
)))
36912 "%qD must be a global variable in "
36913 "%<#pragma acc declare link%>",
36921 if (global_bindings_p ())
36923 error_at (loc
, "invalid OpenACC clause at file scope");
36927 if (DECL_EXTERNAL (decl
))
36930 "invalid use of %<extern%> variable %qD "
36931 "in %<#pragma acc declare%>", decl
);
36935 else if (TREE_PUBLIC (decl
))
36938 "invalid use of %<global%> variable %qD "
36939 "in %<#pragma acc declare%>", decl
);
36946 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36947 || lookup_attribute ("omp declare target link",
36948 DECL_ATTRIBUTES (decl
)))
36950 error_at (loc
, "variable %qD used more than once with "
36951 "%<#pragma acc declare%>", decl
);
36960 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36961 id
= get_identifier ("omp declare target link");
36963 id
= get_identifier ("omp declare target");
36965 DECL_ATTRIBUTES (decl
)
36966 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36967 if (global_bindings_p ())
36969 symtab_node
*node
= symtab_node::get (decl
);
36972 node
->offloadable
= 1;
36973 if (ENABLE_OFFLOADING
)
36975 g
->have_offload
= true;
36976 if (is_a
<varpool_node
*> (node
))
36977 vec_safe_push (offload_vars
, decl
);
36984 if (error
|| global_bindings_p ())
36987 stmt
= make_node (OACC_DECLARE
);
36988 TREE_TYPE (stmt
) = void_type_node
;
36989 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36990 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36998 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37002 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37004 LOC is the location of the #pragma token.
37007 #define OACC_ENTER_DATA_CLAUSE_MASK \
37008 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37014 #define OACC_EXIT_DATA_CLAUSE_MASK \
37015 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37023 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37026 location_t loc
= pragma_tok
->location
;
37027 tree stmt
, clauses
;
37028 const char *p
= "";
37030 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37031 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37033 if (strcmp (p
, "data") != 0)
37035 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
37036 enter
? "enter" : "exit");
37037 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37041 cp_lexer_consume_token (parser
->lexer
);
37044 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
37045 "#pragma acc enter data", pragma_tok
);
37047 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
37048 "#pragma acc exit data", pragma_tok
);
37050 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37052 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
37053 enter
? "enter" : "exit");
37057 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
37058 TREE_TYPE (stmt
) = void_type_node
;
37059 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
37060 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37066 # pragma acc loop oacc-loop-clause[optseq] new-line
37067 structured-block */
37069 #define OACC_LOOP_CLAUSE_MASK \
37070 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37082 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
37083 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
37085 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
37087 strcat (p_name
, " loop");
37088 mask
|= OACC_LOOP_CLAUSE_MASK
;
37090 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37094 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
37096 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
37098 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
37101 tree block
= begin_omp_structured_block ();
37102 int save
= cp_parser_begin_omp_structured_block (parser
);
37103 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
37104 cp_parser_end_omp_structured_block (parser
, save
);
37105 add_stmt (finish_omp_structured_block (block
));
37111 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37116 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37120 #define OACC_KERNELS_CLAUSE_MASK \
37121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37135 #define OACC_PARALLEL_CLAUSE_MASK \
37136 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37154 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37155 char *p_name
, bool *if_p
)
37157 omp_clause_mask mask
;
37158 enum tree_code code
;
37159 switch (cp_parser_pragma_kind (pragma_tok
))
37161 case PRAGMA_OACC_KERNELS
:
37162 strcat (p_name
, " kernels");
37163 mask
= OACC_KERNELS_CLAUSE_MASK
;
37164 code
= OACC_KERNELS
;
37166 case PRAGMA_OACC_PARALLEL
:
37167 strcat (p_name
, " parallel");
37168 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37169 code
= OACC_PARALLEL
;
37172 gcc_unreachable ();
37175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37178 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37179 if (strcmp (p
, "loop") == 0)
37181 cp_lexer_consume_token (parser
->lexer
);
37182 tree block
= begin_omp_parallel ();
37184 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37186 return finish_omp_construct (code
, block
, clauses
);
37190 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37192 tree block
= begin_omp_parallel ();
37193 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37194 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37195 cp_parser_end_omp_structured_block (parser
, save
);
37196 return finish_omp_construct (code
, block
, clauses
);
37200 # pragma acc update oacc-update-clause[optseq] new-line
37203 #define OACC_UPDATE_CLAUSE_MASK \
37204 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
37209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37212 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37214 tree stmt
, clauses
;
37216 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37217 "#pragma acc update", pragma_tok
);
37219 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37221 error_at (pragma_tok
->location
,
37222 "%<#pragma acc update%> must contain at least one "
37223 "%<device%> or %<host%> or %<self%> clause");
37227 stmt
= make_node (OACC_UPDATE
);
37228 TREE_TYPE (stmt
) = void_type_node
;
37229 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37230 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37236 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37238 LOC is the location of the #pragma token.
37241 #define OACC_WAIT_CLAUSE_MASK \
37242 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37245 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37247 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37248 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37250 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37251 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37253 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37254 "#pragma acc wait", pragma_tok
);
37256 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37257 stmt
= finish_expr_stmt (stmt
);
37263 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37265 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37266 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37274 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37275 enum pragma_context context
)
37277 bool first_p
= parser
->omp_declare_simd
== NULL
;
37278 cp_omp_declare_simd_data data
;
37281 data
.error_seen
= false;
37282 data
.fndecl_seen
= false;
37283 data
.tokens
= vNULL
;
37284 data
.clauses
= NULL_TREE
;
37285 /* It is safe to take the address of a local variable; it will only be
37286 used while this scope is live. */
37287 parser
->omp_declare_simd
= &data
;
37290 /* Store away all pragma tokens. */
37291 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37292 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37293 cp_lexer_consume_token (parser
->lexer
);
37294 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37295 parser
->omp_declare_simd
->error_seen
= true;
37296 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37297 struct cp_token_cache
*cp
37298 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37299 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37303 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37304 cp_parser_pragma (parser
, context
, NULL
);
37307 case pragma_external
:
37308 cp_parser_declaration (parser
);
37310 case pragma_member
:
37311 cp_parser_member_declaration (parser
);
37313 case pragma_objc_icode
:
37314 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37317 cp_parser_declaration_statement (parser
);
37320 if (parser
->omp_declare_simd
37321 && !parser
->omp_declare_simd
->error_seen
37322 && !parser
->omp_declare_simd
->fndecl_seen
)
37323 error_at (pragma_tok
->location
,
37324 "%<#pragma omp declare simd%> not immediately followed by "
37325 "function declaration or definition");
37326 data
.tokens
.release ();
37327 parser
->omp_declare_simd
= NULL
;
37331 /* Finalize #pragma omp declare simd clauses after direct declarator has
37332 been parsed, and put that into "omp declare simd" attribute. */
37335 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37337 struct cp_token_cache
*ce
;
37338 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37341 if (!data
->error_seen
&& data
->fndecl_seen
)
37343 error ("%<#pragma omp declare simd%> not immediately followed by "
37344 "a single function declaration or definition");
37345 data
->error_seen
= true;
37347 if (data
->error_seen
)
37350 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37354 cp_parser_push_lexer_for_tokens (parser
, ce
);
37355 parser
->lexer
->in_pragma
= true;
37356 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37357 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37358 cp_lexer_consume_token (parser
->lexer
);
37359 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37360 "#pragma omp declare simd", pragma_tok
);
37361 cp_parser_pop_lexer (parser
);
37363 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37364 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37365 TREE_CHAIN (c
) = attrs
;
37366 if (processing_template_decl
)
37367 ATTR_IS_DEPENDENT (c
) = 1;
37371 data
->fndecl_seen
= true;
37377 # pragma omp declare target new-line
37378 declarations and definitions
37379 # pragma omp end declare target new-line
37382 # pragma omp declare target ( extended-list ) new-line
37384 # pragma omp declare target declare-target-clauses[seq] new-line */
37386 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37387 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37391 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37393 tree clauses
= NULL_TREE
;
37394 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37396 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37397 "#pragma omp declare target", pragma_tok
);
37398 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37400 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37402 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37403 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37407 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37408 scope_chain
->omp_declare_target_attribute
++;
37411 if (scope_chain
->omp_declare_target_attribute
)
37412 error_at (pragma_tok
->location
,
37413 "%<#pragma omp declare target%> with clauses in between "
37414 "%<#pragma omp declare target%> without clauses and "
37415 "%<#pragma omp end declare target%>");
37416 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37418 tree t
= OMP_CLAUSE_DECL (c
), id
;
37419 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37420 tree at2
= lookup_attribute ("omp declare target link",
37421 DECL_ATTRIBUTES (t
));
37422 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37424 id
= get_identifier ("omp declare target link");
37425 std::swap (at1
, at2
);
37428 id
= get_identifier ("omp declare target");
37431 error_at (OMP_CLAUSE_LOCATION (c
),
37432 "%qD specified both in declare target %<link%> and %<to%>"
37438 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37439 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37442 symtab_node
*node
= symtab_node::get (t
);
37445 node
->offloadable
= 1;
37446 if (ENABLE_OFFLOADING
)
37448 g
->have_offload
= true;
37449 if (is_a
<varpool_node
*> (node
))
37450 vec_safe_push (offload_vars
, t
);
37458 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37460 const char *p
= "";
37461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37463 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37464 p
= IDENTIFIER_POINTER (id
);
37466 if (strcmp (p
, "declare") == 0)
37468 cp_lexer_consume_token (parser
->lexer
);
37470 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37472 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37473 p
= IDENTIFIER_POINTER (id
);
37475 if (strcmp (p
, "target") == 0)
37476 cp_lexer_consume_token (parser
->lexer
);
37479 cp_parser_error (parser
, "expected %<target%>");
37480 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37486 cp_parser_error (parser
, "expected %<declare%>");
37487 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37490 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37491 if (!scope_chain
->omp_declare_target_attribute
)
37492 error_at (pragma_tok
->location
,
37493 "%<#pragma omp end declare target%> without corresponding "
37494 "%<#pragma omp declare target%>");
37496 scope_chain
->omp_declare_target_attribute
--;
37499 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37500 expression and optional initializer clause of
37501 #pragma omp declare reduction. We store the expression(s) as
37502 either 3, 6 or 7 special statements inside of the artificial function's
37503 body. The first two statements are DECL_EXPRs for the artificial
37504 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37505 expression that uses those variables.
37506 If there was any INITIALIZER clause, this is followed by further statements,
37507 the fourth and fifth statements are DECL_EXPRs for the artificial
37508 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37509 constructor variant (first token after open paren is not omp_priv),
37510 then the sixth statement is a statement with the function call expression
37511 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37512 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37513 to initialize the OMP_PRIV artificial variable and there is seventh
37514 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37517 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37519 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37520 gcc_assert (TYPE_REF_P (type
));
37521 type
= TREE_TYPE (type
);
37522 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37523 DECL_ARTIFICIAL (omp_out
) = 1;
37524 pushdecl (omp_out
);
37525 add_decl_expr (omp_out
);
37526 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37527 DECL_ARTIFICIAL (omp_in
) = 1;
37529 add_decl_expr (omp_in
);
37531 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37533 keep_next_level (true);
37534 tree block
= begin_omp_structured_block ();
37535 combiner
= cp_parser_expression (parser
);
37536 finish_expr_stmt (combiner
);
37537 block
= finish_omp_structured_block (block
);
37540 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37543 const char *p
= "";
37544 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37546 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37547 p
= IDENTIFIER_POINTER (id
);
37550 if (strcmp (p
, "initializer") == 0)
37552 cp_lexer_consume_token (parser
->lexer
);
37553 matching_parens parens
;
37554 if (!parens
.require_open (parser
))
37558 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37560 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37561 p
= IDENTIFIER_POINTER (id
);
37564 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37565 DECL_ARTIFICIAL (omp_priv
) = 1;
37566 pushdecl (omp_priv
);
37567 add_decl_expr (omp_priv
);
37568 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37569 DECL_ARTIFICIAL (omp_orig
) = 1;
37570 pushdecl (omp_orig
);
37571 add_decl_expr (omp_orig
);
37573 keep_next_level (true);
37574 block
= begin_omp_structured_block ();
37577 if (strcmp (p
, "omp_priv") == 0)
37579 bool is_direct_init
, is_non_constant_init
;
37581 cp_lexer_consume_token (parser
->lexer
);
37582 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37583 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37584 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37585 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37587 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37588 == CPP_CLOSE_PAREN
))
37590 finish_omp_structured_block (block
);
37591 error ("invalid initializer clause");
37594 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37595 &is_non_constant_init
);
37596 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37597 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37601 cp_parser_parse_tentatively (parser
);
37602 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37603 /*check_dependency_p=*/true,
37604 /*template_p=*/NULL
,
37605 /*declarator_p=*/false,
37606 /*optional_p=*/false);
37607 vec
<tree
, va_gc
> *args
;
37608 if (fn_name
== error_mark_node
37609 || cp_parser_error_occurred (parser
)
37610 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37611 || ((args
= cp_parser_parenthesized_expression_list
37612 (parser
, non_attr
, /*cast_p=*/false,
37613 /*allow_expansion_p=*/true,
37614 /*non_constant_p=*/NULL
)),
37615 cp_parser_error_occurred (parser
)))
37617 finish_omp_structured_block (block
);
37618 cp_parser_abort_tentative_parse (parser
);
37619 cp_parser_error (parser
, "expected id-expression (arguments)");
37624 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37625 if (arg
== omp_priv
37626 || (TREE_CODE (arg
) == ADDR_EXPR
37627 && TREE_OPERAND (arg
, 0) == omp_priv
))
37629 cp_parser_abort_tentative_parse (parser
);
37630 if (arg
== NULL_TREE
)
37631 error ("one of the initializer call arguments should be %<omp_priv%>"
37632 " or %<&omp_priv%>");
37633 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37635 finish_expr_stmt (initializer
);
37638 block
= finish_omp_structured_block (block
);
37639 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37643 add_decl_expr (omp_orig
);
37645 if (!parens
.require_close (parser
))
37649 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37650 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37657 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37658 initializer-clause[opt] new-line
37660 initializer-clause:
37661 initializer (omp_priv initializer)
37662 initializer (function-name (argument-list)) */
37665 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37666 enum pragma_context
)
37668 auto_vec
<tree
> types
;
37669 enum tree_code reduc_code
= ERROR_MARK
;
37670 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37672 cp_token
*first_token
;
37673 cp_token_cache
*cp
;
37677 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37678 p
= obstack_alloc (&declarator_obstack
, 0);
37680 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37683 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37686 reduc_code
= PLUS_EXPR
;
37689 reduc_code
= MULT_EXPR
;
37692 reduc_code
= MINUS_EXPR
;
37695 reduc_code
= BIT_AND_EXPR
;
37698 reduc_code
= BIT_XOR_EXPR
;
37701 reduc_code
= BIT_IOR_EXPR
;
37704 reduc_code
= TRUTH_ANDIF_EXPR
;
37707 reduc_code
= TRUTH_ORIF_EXPR
;
37710 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37713 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37714 "%<|%>, %<&&%>, %<||%> or identifier");
37718 if (reduc_code
!= ERROR_MARK
)
37719 cp_lexer_consume_token (parser
->lexer
);
37721 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37722 if (reduc_id
== error_mark_node
)
37725 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37728 /* Types may not be defined in declare reduction type list. */
37729 const char *saved_message
;
37730 saved_message
= parser
->type_definition_forbidden_message
;
37731 parser
->type_definition_forbidden_message
37732 = G_("types may not be defined in declare reduction type list");
37733 bool saved_colon_corrects_to_scope_p
;
37734 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37735 parser
->colon_corrects_to_scope_p
= false;
37736 bool saved_colon_doesnt_start_class_def_p
;
37737 saved_colon_doesnt_start_class_def_p
37738 = parser
->colon_doesnt_start_class_def_p
;
37739 parser
->colon_doesnt_start_class_def_p
= true;
37743 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37744 type
= cp_parser_type_id (parser
);
37745 if (type
== error_mark_node
)
37747 else if (ARITHMETIC_TYPE_P (type
)
37748 && (orig_reduc_id
== NULL_TREE
37749 || (TREE_CODE (type
) != COMPLEX_TYPE
37750 && (id_equal (orig_reduc_id
, "min")
37751 || id_equal (orig_reduc_id
, "max")))))
37752 error_at (loc
, "predeclared arithmetic type %qT in "
37753 "%<#pragma omp declare reduction%>", type
);
37754 else if (TREE_CODE (type
) == FUNCTION_TYPE
37755 || TREE_CODE (type
) == METHOD_TYPE
37756 || TREE_CODE (type
) == ARRAY_TYPE
)
37757 error_at (loc
, "function or array type %qT in "
37758 "%<#pragma omp declare reduction%>", type
);
37759 else if (TYPE_REF_P (type
))
37760 error_at (loc
, "reference type %qT in "
37761 "%<#pragma omp declare reduction%>", type
);
37762 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37763 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37764 "%<#pragma omp declare reduction%>", type
);
37766 types
.safe_push (type
);
37768 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37769 cp_lexer_consume_token (parser
->lexer
);
37774 /* Restore the saved message. */
37775 parser
->type_definition_forbidden_message
= saved_message
;
37776 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37777 parser
->colon_doesnt_start_class_def_p
37778 = saved_colon_doesnt_start_class_def_p
;
37780 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37781 || types
.is_empty ())
37784 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37788 first_token
= cp_lexer_peek_token (parser
->lexer
);
37791 FOR_EACH_VEC_ELT (types
, i
, type
)
37794 = build_function_type_list (void_type_node
,
37795 cp_build_reference_type (type
, false),
37797 tree this_reduc_id
= reduc_id
;
37798 if (!dependent_type_p (type
))
37799 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37800 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37801 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37802 DECL_ARTIFICIAL (fndecl
) = 1;
37803 DECL_EXTERNAL (fndecl
) = 1;
37804 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37805 DECL_IGNORED_P (fndecl
) = 1;
37806 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37807 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37808 DECL_ATTRIBUTES (fndecl
)
37809 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37810 DECL_ATTRIBUTES (fndecl
));
37811 if (processing_template_decl
)
37812 fndecl
= push_template_decl (fndecl
);
37813 bool block_scope
= false;
37814 tree block
= NULL_TREE
;
37815 if (current_function_decl
)
37817 block_scope
= true;
37818 DECL_CONTEXT (fndecl
) = global_namespace
;
37819 if (!processing_template_decl
)
37822 else if (current_class_type
)
37826 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37827 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37828 cp_lexer_consume_token (parser
->lexer
);
37829 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37831 cp
= cp_token_cache_new (first_token
,
37832 cp_lexer_peek_nth_token (parser
->lexer
,
37835 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37836 finish_member_declaration (fndecl
);
37837 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37838 DECL_PENDING_INLINE_P (fndecl
) = 1;
37839 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37844 DECL_CONTEXT (fndecl
) = current_namespace
;
37848 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37850 block
= begin_omp_structured_block ();
37853 cp_parser_push_lexer_for_tokens (parser
, cp
);
37854 parser
->lexer
->in_pragma
= true;
37856 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37859 finish_function (/*inline_p=*/false);
37861 DECL_CONTEXT (fndecl
) = current_function_decl
;
37863 cp_parser_pop_lexer (parser
);
37867 cp_parser_pop_lexer (parser
);
37869 finish_function (/*inline_p=*/false);
37872 DECL_CONTEXT (fndecl
) = current_function_decl
;
37873 block
= finish_omp_structured_block (block
);
37874 if (TREE_CODE (block
) == BIND_EXPR
)
37875 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37876 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37877 DECL_SAVED_TREE (fndecl
) = block
;
37878 if (processing_template_decl
)
37879 add_decl_expr (fndecl
);
37881 cp_check_omp_declare_reduction (fndecl
);
37882 if (cp
== NULL
&& types
.length () > 1)
37883 cp
= cp_token_cache_new (first_token
,
37884 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37885 if (errs
!= errorcount
)
37889 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37892 /* Free any declarators allocated. */
37893 obstack_free (&declarator_obstack
, p
);
37897 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37898 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37899 initializer-clause[opt] new-line
37900 #pragma omp declare target new-line */
37903 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37904 enum pragma_context context
)
37906 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37908 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37909 const char *p
= IDENTIFIER_POINTER (id
);
37911 if (strcmp (p
, "simd") == 0)
37913 cp_lexer_consume_token (parser
->lexer
);
37914 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37918 cp_ensure_no_omp_declare_simd (parser
);
37919 if (strcmp (p
, "reduction") == 0)
37921 cp_lexer_consume_token (parser
->lexer
);
37922 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37926 if (!flag_openmp
) /* flag_openmp_simd */
37928 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37931 if (strcmp (p
, "target") == 0)
37933 cp_lexer_consume_token (parser
->lexer
);
37934 cp_parser_omp_declare_target (parser
, pragma_tok
);
37938 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37940 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37945 #pragma omp taskloop taskloop-clause[optseq] new-line
37948 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37951 #define OMP_TASKLOOP_CLAUSE_MASK \
37952 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37968 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37969 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37972 tree clauses
, sb
, ret
;
37974 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37976 strcat (p_name
, " taskloop");
37977 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37981 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37982 const char *p
= IDENTIFIER_POINTER (id
);
37984 if (strcmp (p
, "simd") == 0)
37986 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37987 if (cclauses
== NULL
)
37988 cclauses
= cclauses_buf
;
37990 cp_lexer_consume_token (parser
->lexer
);
37991 if (!flag_openmp
) /* flag_openmp_simd */
37992 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37994 sb
= begin_omp_structured_block ();
37995 save
= cp_parser_begin_omp_structured_block (parser
);
37996 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37998 cp_parser_end_omp_structured_block (parser
, save
);
37999 tree body
= finish_omp_structured_block (sb
);
38002 ret
= make_node (OMP_TASKLOOP
);
38003 TREE_TYPE (ret
) = void_type_node
;
38004 OMP_FOR_BODY (ret
) = body
;
38005 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38006 SET_EXPR_LOCATION (ret
, loc
);
38011 if (!flag_openmp
) /* flag_openmp_simd */
38013 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38017 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38021 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
38022 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38025 sb
= begin_omp_structured_block ();
38026 save
= cp_parser_begin_omp_structured_block (parser
);
38028 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
38031 cp_parser_end_omp_structured_block (parser
, save
);
38032 add_stmt (finish_omp_structured_block (sb
));
38039 # pragma acc routine oacc-routine-clause[optseq] new-line
38040 function-definition
38042 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38045 #define OACC_ROUTINE_CLAUSE_MASK \
38046 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38052 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38053 component, which must resolve to a declared namespace-scope
38054 function. The clauses are either processed directly (for a named
38055 function), or defered until the immediatley following declaration
38059 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
38060 enum pragma_context context
)
38062 gcc_checking_assert (context
== pragma_external
);
38063 /* The checking for "another pragma following this one" in the "no optional
38064 '( name )'" case makes sure that we dont re-enter. */
38065 gcc_checking_assert (parser
->oacc_routine
== NULL
);
38067 cp_oacc_routine_data data
;
38068 data
.error_seen
= false;
38069 data
.fndecl_seen
= false;
38070 data
.tokens
= vNULL
;
38071 data
.clauses
= NULL_TREE
;
38072 data
.loc
= pragma_tok
->location
;
38073 /* It is safe to take the address of a local variable; it will only be
38074 used while this scope is live. */
38075 parser
->oacc_routine
= &data
;
38077 /* Look for optional '( name )'. */
38078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38080 matching_parens parens
;
38081 parens
.consume_open (parser
); /* '(' */
38083 /* We parse the name as an id-expression. If it resolves to
38084 anything other than a non-overloaded function at namespace
38085 scope, it's an error. */
38086 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38087 tree name
= cp_parser_id_expression (parser
,
38088 /*template_keyword_p=*/false,
38089 /*check_dependency_p=*/false,
38090 /*template_p=*/NULL
,
38091 /*declarator_p=*/false,
38092 /*optional_p=*/false);
38093 tree decl
= (identifier_p (name
)
38094 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
38096 if (name
!= error_mark_node
&& decl
== error_mark_node
)
38097 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
38099 if (decl
== error_mark_node
38100 || !parens
.require_close (parser
))
38102 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38103 parser
->oacc_routine
= NULL
;
38108 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38109 "#pragma acc routine",
38110 cp_lexer_peek_token (parser
->lexer
));
38112 if (decl
&& is_overloaded_fn (decl
)
38113 && (TREE_CODE (decl
) != FUNCTION_DECL
38114 || DECL_FUNCTION_TEMPLATE_P (decl
)))
38116 error_at (name_loc
,
38117 "%<#pragma acc routine%> names a set of overloads");
38118 parser
->oacc_routine
= NULL
;
38122 /* Perhaps we should use the same rule as declarations in different
38124 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38126 error_at (name_loc
,
38127 "%qD does not refer to a namespace scope function", decl
);
38128 parser
->oacc_routine
= NULL
;
38132 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38134 error_at (name_loc
, "%qD does not refer to a function", decl
);
38135 parser
->oacc_routine
= NULL
;
38139 cp_finalize_oacc_routine (parser
, decl
, false);
38140 parser
->oacc_routine
= NULL
;
38142 else /* No optional '( name )'. */
38144 /* Store away all pragma tokens. */
38145 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38146 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38147 cp_lexer_consume_token (parser
->lexer
);
38148 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38149 parser
->oacc_routine
->error_seen
= true;
38150 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38151 struct cp_token_cache
*cp
38152 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38153 parser
->oacc_routine
->tokens
.safe_push (cp
);
38155 /* Emit a helpful diagnostic if there's another pragma following this
38157 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38159 cp_ensure_no_oacc_routine (parser
);
38160 data
.tokens
.release ();
38161 /* ..., and then just keep going. */
38165 /* We only have to consider the pragma_external case here. */
38166 cp_parser_declaration (parser
);
38167 if (parser
->oacc_routine
38168 && !parser
->oacc_routine
->fndecl_seen
)
38169 cp_ensure_no_oacc_routine (parser
);
38171 parser
->oacc_routine
= NULL
;
38172 data
.tokens
.release ();
38176 /* Finalize #pragma acc routine clauses after direct declarator has
38180 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38182 struct cp_token_cache
*ce
;
38183 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38185 if (!data
->error_seen
&& data
->fndecl_seen
)
38187 error_at (data
->loc
,
38188 "%<#pragma acc routine%> not immediately followed by "
38189 "a single function declaration or definition");
38190 data
->error_seen
= true;
38192 if (data
->error_seen
)
38195 gcc_checking_assert (data
->tokens
.length () == 1);
38196 ce
= data
->tokens
[0];
38198 cp_parser_push_lexer_for_tokens (parser
, ce
);
38199 parser
->lexer
->in_pragma
= true;
38200 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38202 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38203 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38204 parser
->oacc_routine
->clauses
38205 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38206 "#pragma acc routine", pragma_tok
);
38207 cp_parser_pop_lexer (parser
);
38208 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38214 /* Apply any saved OpenACC routine clauses to a just-parsed
38218 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38220 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38222 /* Keep going if we're in error reporting mode. */
38223 if (parser
->oacc_routine
->error_seen
38224 || fndecl
== error_mark_node
)
38227 if (parser
->oacc_routine
->fndecl_seen
)
38229 error_at (parser
->oacc_routine
->loc
,
38230 "%<#pragma acc routine%> not immediately followed by"
38231 " a single function declaration or definition");
38232 parser
->oacc_routine
= NULL
;
38235 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38237 cp_ensure_no_oacc_routine (parser
);
38241 if (oacc_get_fn_attrib (fndecl
))
38243 error_at (parser
->oacc_routine
->loc
,
38244 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38245 parser
->oacc_routine
= NULL
;
38249 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38251 error_at (parser
->oacc_routine
->loc
,
38253 ? G_("%<#pragma acc routine%> must be applied before use")
38254 : G_("%<#pragma acc routine%> must be applied before "
38256 parser
->oacc_routine
= NULL
;
38260 /* Process the routine's dimension clauses. */
38261 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38262 oacc_replace_fn_attrib (fndecl
, dims
);
38264 /* Add an "omp declare target" attribute. */
38265 DECL_ATTRIBUTES (fndecl
)
38266 = tree_cons (get_identifier ("omp declare target"),
38267 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38269 /* Don't unset parser->oacc_routine here: we may still need it to
38270 diagnose wrong usage. But, remember that we've used this "#pragma acc
38272 parser
->oacc_routine
->fndecl_seen
= true;
38276 /* Main entry point to OpenMP statement pragmas. */
38279 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38282 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38283 omp_clause_mask
mask (0);
38285 switch (cp_parser_pragma_kind (pragma_tok
))
38287 case PRAGMA_OACC_ATOMIC
:
38288 cp_parser_omp_atomic (parser
, pragma_tok
);
38290 case PRAGMA_OACC_CACHE
:
38291 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38293 case PRAGMA_OACC_DATA
:
38294 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38296 case PRAGMA_OACC_ENTER_DATA
:
38297 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38299 case PRAGMA_OACC_EXIT_DATA
:
38300 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38302 case PRAGMA_OACC_HOST_DATA
:
38303 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38305 case PRAGMA_OACC_KERNELS
:
38306 case PRAGMA_OACC_PARALLEL
:
38307 strcpy (p_name
, "#pragma acc");
38308 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38311 case PRAGMA_OACC_LOOP
:
38312 strcpy (p_name
, "#pragma acc");
38313 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38316 case PRAGMA_OACC_UPDATE
:
38317 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38319 case PRAGMA_OACC_WAIT
:
38320 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38322 case PRAGMA_OMP_ATOMIC
:
38323 cp_parser_omp_atomic (parser
, pragma_tok
);
38325 case PRAGMA_OMP_CRITICAL
:
38326 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38328 case PRAGMA_OMP_DISTRIBUTE
:
38329 strcpy (p_name
, "#pragma omp");
38330 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38333 case PRAGMA_OMP_FOR
:
38334 strcpy (p_name
, "#pragma omp");
38335 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38338 case PRAGMA_OMP_MASTER
:
38339 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38341 case PRAGMA_OMP_PARALLEL
:
38342 strcpy (p_name
, "#pragma omp");
38343 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38346 case PRAGMA_OMP_SECTIONS
:
38347 strcpy (p_name
, "#pragma omp");
38348 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38350 case PRAGMA_OMP_SIMD
:
38351 strcpy (p_name
, "#pragma omp");
38352 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38355 case PRAGMA_OMP_SINGLE
:
38356 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38358 case PRAGMA_OMP_TASK
:
38359 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38361 case PRAGMA_OMP_TASKGROUP
:
38362 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38364 case PRAGMA_OMP_TASKLOOP
:
38365 strcpy (p_name
, "#pragma omp");
38366 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38369 case PRAGMA_OMP_TEAMS
:
38370 strcpy (p_name
, "#pragma omp");
38371 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38375 gcc_unreachable ();
38378 protected_set_expr_location (stmt
, pragma_tok
->location
);
38381 /* Transactional Memory parsing routines. */
38383 /* Parse a transaction attribute.
38389 We use this instead of cp_parser_attributes_opt for transactions to avoid
38390 the pedwarn in C++98 mode. */
38393 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38396 tree attr_name
, attr
= NULL
;
38398 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38399 return cp_parser_attributes_opt (parser
);
38401 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38403 cp_lexer_consume_token (parser
->lexer
);
38404 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38407 token
= cp_lexer_peek_token (parser
->lexer
);
38408 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38410 token
= cp_lexer_consume_token (parser
->lexer
);
38412 attr_name
= (token
->type
== CPP_KEYWORD
38413 /* For keywords, use the canonical spelling,
38414 not the parsed identifier. */
38415 ? ridpointers
[(int) token
->keyword
]
38417 attr
= build_tree_list (attr_name
, NULL_TREE
);
38420 cp_parser_error (parser
, "expected identifier");
38422 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38424 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38428 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38430 transaction-statement:
38431 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38433 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38437 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38439 unsigned char old_in
= parser
->in_transaction
;
38440 unsigned char this_in
= 1, new_in
;
38441 enum rid keyword
= token
->keyword
;
38442 tree stmt
, attrs
, noex
;
38444 cp_lexer_consume_token (parser
->lexer
);
38446 if (keyword
== RID_TRANSACTION_RELAXED
38447 || keyword
== RID_SYNCHRONIZED
)
38448 this_in
|= TM_STMT_ATTR_RELAXED
;
38451 attrs
= cp_parser_txn_attribute_opt (parser
);
38453 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38456 /* Parse a noexcept specification. */
38457 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38458 noex
= boolean_true_node
;
38459 else if (keyword
== RID_ATOMIC_CANCEL
)
38461 /* cancel-and-throw is unimplemented. */
38462 sorry ("atomic_cancel");
38466 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38468 /* Keep track if we're in the lexical scope of an outer transaction. */
38469 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38471 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38473 parser
->in_transaction
= new_in
;
38474 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38475 parser
->in_transaction
= old_in
;
38477 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38482 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38484 transaction-expression:
38485 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38486 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38490 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38492 unsigned char old_in
= parser
->in_transaction
;
38493 unsigned char this_in
= 1;
38497 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38499 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38500 || keyword
== RID_TRANSACTION_RELAXED
);
38504 keyword
== RID_TRANSACTION_RELAXED
38505 ? G_("%<__transaction_relaxed%> without transactional memory "
38507 : G_("%<__transaction_atomic%> without transactional memory "
38508 "support enabled"));
38510 token
= cp_parser_require_keyword (parser
, keyword
,
38511 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38512 : RT_TRANSACTION_RELAXED
));
38513 gcc_assert (token
!= NULL
);
38515 if (keyword
== RID_TRANSACTION_RELAXED
)
38516 this_in
|= TM_STMT_ATTR_RELAXED
;
38518 /* Set this early. This might mean that we allow transaction_cancel in
38519 an expression that we find out later actually has to be a constexpr.
38520 However, we expect that cxx_constant_value will be able to deal with
38521 this; also, if the noexcept has no constexpr, then what we parse next
38522 really is a transaction's body. */
38523 parser
->in_transaction
= this_in
;
38525 /* Parse a noexcept specification. */
38526 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38529 if (!noex
|| !noex_expr
38530 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38532 matching_parens parens
;
38533 parens
.require_open (parser
);
38535 expr
= cp_parser_expression (parser
);
38536 expr
= finish_parenthesized_expr (expr
);
38538 parens
.require_close (parser
);
38542 /* The only expression that is available got parsed for the noexcept
38543 already. noexcept is true then. */
38545 noex
= boolean_true_node
;
38548 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38549 parser
->in_transaction
= old_in
;
38551 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38552 return error_mark_node
;
38554 return (flag_tm
? expr
: error_mark_node
);
38557 /* Parse a function-transaction-block.
38559 function-transaction-block:
38560 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38562 __transaction_atomic txn-attribute[opt] function-try-block
38563 __transaction_relaxed ctor-initializer[opt] function-body
38564 __transaction_relaxed function-try-block
38568 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38570 unsigned char old_in
= parser
->in_transaction
;
38571 unsigned char new_in
= 1;
38572 tree compound_stmt
, stmt
, attrs
;
38575 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38576 || keyword
== RID_TRANSACTION_RELAXED
);
38577 token
= cp_parser_require_keyword (parser
, keyword
,
38578 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38579 : RT_TRANSACTION_RELAXED
));
38580 gcc_assert (token
!= NULL
);
38582 if (keyword
== RID_TRANSACTION_RELAXED
)
38583 new_in
|= TM_STMT_ATTR_RELAXED
;
38586 attrs
= cp_parser_txn_attribute_opt (parser
);
38588 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38591 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38593 parser
->in_transaction
= new_in
;
38595 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38596 cp_parser_function_try_block (parser
);
38598 cp_parser_ctor_initializer_opt_and_function_body
38599 (parser
, /*in_function_try_block=*/false);
38601 parser
->in_transaction
= old_in
;
38603 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38606 /* Parse a __transaction_cancel statement.
38609 __transaction_cancel txn-attribute[opt] ;
38610 __transaction_cancel txn-attribute[opt] throw-expression ;
38612 ??? Cancel and throw is not yet implemented. */
38615 cp_parser_transaction_cancel (cp_parser
*parser
)
38618 bool is_outer
= false;
38621 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38622 RT_TRANSACTION_CANCEL
);
38623 gcc_assert (token
!= NULL
);
38625 attrs
= cp_parser_txn_attribute_opt (parser
);
38627 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38629 /* ??? Parse cancel-and-throw here. */
38631 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38635 error_at (token
->location
, "%<__transaction_cancel%> without "
38636 "transactional memory support enabled");
38637 return error_mark_node
;
38639 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38641 error_at (token
->location
, "%<__transaction_cancel%> within a "
38642 "%<__transaction_relaxed%>");
38643 return error_mark_node
;
38647 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38648 && !is_tm_may_cancel_outer (current_function_decl
))
38650 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38651 "within outer %<__transaction_atomic%>");
38652 error_at (token
->location
,
38653 " or a %<transaction_may_cancel_outer%> function");
38654 return error_mark_node
;
38657 else if (parser
->in_transaction
== 0)
38659 error_at (token
->location
, "%<__transaction_cancel%> not within "
38660 "%<__transaction_atomic%>");
38661 return error_mark_node
;
38664 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38672 static GTY (()) cp_parser
*the_parser
;
38675 /* Special handling for the first token or line in the file. The first
38676 thing in the file might be #pragma GCC pch_preprocess, which loads a
38677 PCH file, which is a GC collection point. So we need to handle this
38678 first pragma without benefit of an existing lexer structure.
38680 Always returns one token to the caller in *FIRST_TOKEN. This is
38681 either the true first token of the file, or the first token after
38682 the initial pragma. */
38685 cp_parser_initial_pragma (cp_token
*first_token
)
38689 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38690 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38693 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38694 if (first_token
->type
== CPP_STRING
)
38696 name
= first_token
->u
.value
;
38698 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38699 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38700 error_at (first_token
->location
,
38701 "junk at end of %<#pragma GCC pch_preprocess%>");
38704 error_at (first_token
->location
, "expected string literal");
38706 /* Skip to the end of the pragma. */
38707 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38708 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38710 /* Now actually load the PCH file. */
38712 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38714 /* Read one more token to return to our caller. We have to do this
38715 after reading the PCH file in, since its pointers have to be
38717 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38720 /* Parse a pragma GCC ivdep. */
38723 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38725 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38729 /* Parse a pragma GCC unroll. */
38731 static unsigned short
38732 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38734 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38735 tree expr
= cp_parser_constant_expression (parser
);
38736 unsigned short unroll
;
38737 expr
= maybe_constant_value (expr
);
38738 HOST_WIDE_INT lunroll
= 0;
38739 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38740 || TREE_CODE (expr
) != INTEGER_CST
38741 || (lunroll
= tree_to_shwi (expr
)) < 0
38742 || lunroll
>= USHRT_MAX
)
38744 error_at (location
, "%<#pragma GCC unroll%> requires an"
38745 " assignment-expression that evaluates to a non-negative"
38746 " integral constant less than %u", USHRT_MAX
);
38751 unroll
= (unsigned short)lunroll
;
38755 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38759 /* Normal parsing of a pragma token. Here we can (and must) use the
38763 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38765 cp_token
*pragma_tok
;
38770 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38771 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38772 parser
->lexer
->in_pragma
= true;
38774 id
= cp_parser_pragma_kind (pragma_tok
);
38775 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38776 cp_ensure_no_omp_declare_simd (parser
);
38779 case PRAGMA_GCC_PCH_PREPROCESS
:
38780 error_at (pragma_tok
->location
,
38781 "%<#pragma GCC pch_preprocess%> must be first");
38784 case PRAGMA_OMP_BARRIER
:
38787 case pragma_compound
:
38788 cp_parser_omp_barrier (parser
, pragma_tok
);
38791 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38792 "used in compound statements", "omp barrier");
38799 case PRAGMA_OMP_FLUSH
:
38802 case pragma_compound
:
38803 cp_parser_omp_flush (parser
, pragma_tok
);
38806 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38807 "used in compound statements", "omp flush");
38814 case PRAGMA_OMP_TASKWAIT
:
38817 case pragma_compound
:
38818 cp_parser_omp_taskwait (parser
, pragma_tok
);
38821 error_at (pragma_tok
->location
,
38822 "%<#pragma %s%> may only be used in compound statements",
38830 case PRAGMA_OMP_TASKYIELD
:
38833 case pragma_compound
:
38834 cp_parser_omp_taskyield (parser
, pragma_tok
);
38837 error_at (pragma_tok
->location
,
38838 "%<#pragma %s%> may only be used in compound statements",
38846 case PRAGMA_OMP_CANCEL
:
38849 case pragma_compound
:
38850 cp_parser_omp_cancel (parser
, pragma_tok
);
38853 error_at (pragma_tok
->location
,
38854 "%<#pragma %s%> may only be used in compound statements",
38862 case PRAGMA_OMP_CANCELLATION_POINT
:
38863 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38866 case PRAGMA_OMP_THREADPRIVATE
:
38867 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38870 case PRAGMA_OMP_DECLARE
:
38871 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38873 case PRAGMA_OACC_DECLARE
:
38874 cp_parser_oacc_declare (parser
, pragma_tok
);
38877 case PRAGMA_OACC_ENTER_DATA
:
38878 if (context
== pragma_stmt
)
38880 error_at (pragma_tok
->location
,
38881 "%<#pragma %s%> may only be used in compound statements",
38885 else if (context
!= pragma_compound
)
38887 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38890 case PRAGMA_OACC_EXIT_DATA
:
38891 if (context
== pragma_stmt
)
38893 error_at (pragma_tok
->location
,
38894 "%<#pragma %s%> may only be used in compound statements",
38898 else if (context
!= pragma_compound
)
38900 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38903 case PRAGMA_OACC_ROUTINE
:
38904 if (context
!= pragma_external
)
38906 error_at (pragma_tok
->location
,
38907 "%<#pragma acc routine%> must be at file scope");
38910 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38913 case PRAGMA_OACC_UPDATE
:
38914 if (context
== pragma_stmt
)
38916 error_at (pragma_tok
->location
,
38917 "%<#pragma %s%> may only be used in compound statements",
38921 else if (context
!= pragma_compound
)
38923 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38926 case PRAGMA_OACC_WAIT
:
38927 if (context
== pragma_stmt
)
38929 error_at (pragma_tok
->location
,
38930 "%<#pragma %s%> may only be used in compound statements",
38934 else if (context
!= pragma_compound
)
38936 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38939 case PRAGMA_OACC_ATOMIC
:
38940 case PRAGMA_OACC_CACHE
:
38941 case PRAGMA_OACC_DATA
:
38942 case PRAGMA_OACC_HOST_DATA
:
38943 case PRAGMA_OACC_KERNELS
:
38944 case PRAGMA_OACC_PARALLEL
:
38945 case PRAGMA_OACC_LOOP
:
38946 case PRAGMA_OMP_ATOMIC
:
38947 case PRAGMA_OMP_CRITICAL
:
38948 case PRAGMA_OMP_DISTRIBUTE
:
38949 case PRAGMA_OMP_FOR
:
38950 case PRAGMA_OMP_MASTER
:
38951 case PRAGMA_OMP_PARALLEL
:
38952 case PRAGMA_OMP_SECTIONS
:
38953 case PRAGMA_OMP_SIMD
:
38954 case PRAGMA_OMP_SINGLE
:
38955 case PRAGMA_OMP_TASK
:
38956 case PRAGMA_OMP_TASKGROUP
:
38957 case PRAGMA_OMP_TASKLOOP
:
38958 case PRAGMA_OMP_TEAMS
:
38959 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38961 stmt
= push_omp_privatization_clauses (false);
38962 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38963 pop_omp_privatization_clauses (stmt
);
38966 case PRAGMA_OMP_ORDERED
:
38967 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38969 stmt
= push_omp_privatization_clauses (false);
38970 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38971 pop_omp_privatization_clauses (stmt
);
38974 case PRAGMA_OMP_TARGET
:
38975 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38977 stmt
= push_omp_privatization_clauses (false);
38978 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38979 pop_omp_privatization_clauses (stmt
);
38982 case PRAGMA_OMP_END_DECLARE_TARGET
:
38983 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38986 case PRAGMA_OMP_SECTION
:
38987 error_at (pragma_tok
->location
,
38988 "%<#pragma omp section%> may only be used in "
38989 "%<#pragma omp sections%> construct");
38994 if (context
== pragma_external
)
38996 error_at (pragma_tok
->location
,
38997 "%<#pragma GCC ivdep%> must be inside a function");
39000 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
39001 unsigned short unroll
;
39002 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39003 if (tok
->type
== CPP_PRAGMA
39004 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
39006 tok
= cp_lexer_consume_token (parser
->lexer
);
39007 unroll
= cp_parser_pragma_unroll (parser
, tok
);
39008 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39012 if (tok
->type
!= CPP_KEYWORD
39013 || (tok
->keyword
!= RID_FOR
39014 && tok
->keyword
!= RID_WHILE
39015 && tok
->keyword
!= RID_DO
))
39017 cp_parser_error (parser
, "for, while or do statement expected");
39020 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39024 case PRAGMA_UNROLL
:
39026 if (context
== pragma_external
)
39028 error_at (pragma_tok
->location
,
39029 "%<#pragma GCC unroll%> must be inside a function");
39032 const unsigned short unroll
39033 = cp_parser_pragma_unroll (parser
, pragma_tok
);
39035 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39036 if (tok
->type
== CPP_PRAGMA
39037 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
39039 tok
= cp_lexer_consume_token (parser
->lexer
);
39040 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
39041 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39045 if (tok
->type
!= CPP_KEYWORD
39046 || (tok
->keyword
!= RID_FOR
39047 && tok
->keyword
!= RID_WHILE
39048 && tok
->keyword
!= RID_DO
))
39050 cp_parser_error (parser
, "for, while or do statement expected");
39053 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39058 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
39059 c_invoke_pragma_handler (id
);
39063 cp_parser_error (parser
, "expected declaration specifiers");
39067 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39071 /* The interface the pragma parsers have to the lexer. */
39074 pragma_lex (tree
*value
, location_t
*loc
)
39076 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39077 enum cpp_ttype ret
= tok
->type
;
39079 *value
= tok
->u
.value
;
39081 *loc
= tok
->location
;
39083 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
39085 else if (ret
== CPP_STRING
)
39086 *value
= cp_parser_string_literal (the_parser
, false, false);
39089 if (ret
== CPP_KEYWORD
)
39091 cp_lexer_consume_token (the_parser
->lexer
);
39098 /* External interface. */
39100 /* Parse one entire translation unit. */
39103 c_parse_file (void)
39105 static bool already_called
= false;
39107 if (already_called
)
39108 fatal_error (input_location
,
39109 "inter-module optimizations not implemented for C++");
39110 already_called
= true;
39112 the_parser
= cp_parser_new ();
39113 push_deferring_access_checks (flag_access_control
39114 ? dk_no_deferred
: dk_no_check
);
39115 cp_parser_translation_unit (the_parser
);
39119 /* Create an identifier for a generic parameter type (a synthesized
39120 template parameter implied by `auto' or a concept identifier). */
39122 static GTY(()) int generic_parm_count
;
39124 make_generic_type_name ()
39127 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39128 return get_identifier (buf
);
39131 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39132 (creating a new template parameter list if necessary). Returns the newly
39133 created template type parm. */
39136 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39138 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39140 /* Before committing to modifying any scope, if we're in an
39141 implicit template scope, and we're trying to synthesize a
39142 constrained parameter, try to find a previous parameter with
39143 the same name. This is the same-type rule for abbreviated
39144 function templates.
39146 NOTE: We can generate implicit parameters when tentatively
39147 parsing a nested name specifier, only to reject that parse
39148 later. However, matching the same template-id as part of a
39149 direct-declarator should generate an identical template
39150 parameter, so this rule will merge them. */
39151 if (parser
->implicit_template_scope
&& constr
)
39153 tree t
= parser
->implicit_template_parms
;
39156 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39158 tree d
= TREE_VALUE (t
);
39159 if (TREE_CODE (d
) == PARM_DECL
)
39160 /* Return the TEMPLATE_PARM_INDEX. */
39161 d
= DECL_INITIAL (d
);
39164 t
= TREE_CHAIN (t
);
39168 /* We are either continuing a function template that already contains implicit
39169 template parameters, creating a new fully-implicit function template, or
39170 extending an existing explicit function template with implicit template
39173 cp_binding_level
*const entry_scope
= current_binding_level
;
39175 bool become_template
= false;
39176 cp_binding_level
*parent_scope
= 0;
39178 if (parser
->implicit_template_scope
)
39180 gcc_assert (parser
->implicit_template_parms
);
39182 current_binding_level
= parser
->implicit_template_scope
;
39186 /* Roll back to the existing template parameter scope (in the case of
39187 extending an explicit function template) or introduce a new template
39188 parameter scope ahead of the function parameter scope (or class scope
39189 in the case of out-of-line member definitions). The function scope is
39190 added back after template parameter synthesis below. */
39192 cp_binding_level
*scope
= entry_scope
;
39194 while (scope
->kind
== sk_function_parms
)
39196 parent_scope
= scope
;
39197 scope
= scope
->level_chain
;
39199 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39201 /* If not defining a class, then any class scope is a scope level in
39202 an out-of-line member definition. In this case simply wind back
39203 beyond the first such scope to inject the template parameter list.
39204 Otherwise wind back to the class being defined. The latter can
39205 occur in class member friend declarations such as:
39211 friend void A::foo (auto);
39214 The template parameter list synthesized for the friend declaration
39215 must be injected in the scope of 'B'. This can also occur in
39216 erroneous cases such as:
39222 void B::foo (auto) {}
39225 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39226 but, nevertheless, the template parameter list synthesized for the
39227 declarator should be injected into the scope of 'A' as if the
39228 ill-formed template was specified explicitly. */
39230 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39232 parent_scope
= scope
;
39233 scope
= scope
->level_chain
;
39237 current_binding_level
= scope
;
39239 if (scope
->kind
!= sk_template_parms
39240 || !function_being_declared_is_template_p (parser
))
39242 /* Introduce a new template parameter list for implicit template
39245 become_template
= true;
39247 parser
->implicit_template_scope
39248 = begin_scope (sk_template_parms
, NULL
);
39250 ++processing_template_decl
;
39252 parser
->fully_implicit_function_template_p
= true;
39253 ++parser
->num_template_parameter_lists
;
39257 /* Synthesize implicit template parameters at the end of the explicit
39258 template parameter list. */
39260 gcc_assert (current_template_parms
);
39262 parser
->implicit_template_scope
= scope
;
39264 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39265 parser
->implicit_template_parms
39266 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39270 /* Synthesize a new template parameter and track the current template
39271 parameter chain with implicit_template_parms. */
39273 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39274 tree synth_id
= make_generic_type_name ();
39275 tree synth_tmpl_parm
;
39276 bool non_type
= false;
39278 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39280 = finish_template_type_parm (class_type_node
, synth_id
);
39281 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39283 = finish_constrained_template_template_parm (proto
, synth_id
);
39286 synth_tmpl_parm
= copy_decl (proto
);
39287 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39291 // Attach the constraint to the parm before processing.
39292 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39293 TREE_TYPE (node
) = constr
;
39295 = process_template_parm (parser
->implicit_template_parms
,
39298 /*non_type=*/non_type
,
39299 /*param_pack=*/false);
39301 // Chain the new parameter to the list of implicit parameters.
39302 if (parser
->implicit_template_parms
)
39303 parser
->implicit_template_parms
39304 = TREE_CHAIN (parser
->implicit_template_parms
);
39306 parser
->implicit_template_parms
= new_parm
;
39308 tree new_decl
= get_local_decls ();
39310 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39311 new_decl
= DECL_INITIAL (new_decl
);
39313 /* If creating a fully implicit function template, start the new implicit
39314 template parameter list with this synthesized type, otherwise grow the
39315 current template parameter list. */
39317 if (become_template
)
39319 parent_scope
->level_chain
= current_binding_level
;
39321 tree new_parms
= make_tree_vec (1);
39322 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39323 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39324 new_parms
, current_template_parms
);
39328 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39329 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39330 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39331 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39334 // If the new parameter was constrained, we need to add that to the
39335 // constraints in the template parameter list.
39336 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39338 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39339 reqs
= conjoin_constraints (reqs
, req
);
39340 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39343 current_binding_level
= entry_scope
;
39348 /* Finish the declaration of a fully implicit function template. Such a
39349 template has no explicit template parameter list so has not been through the
39350 normal template head and tail processing. synthesize_implicit_template_parm
39351 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39352 provided if the declaration is a class member such that its template
39353 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39354 form is returned. Otherwise NULL_TREE is returned. */
39357 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39359 gcc_assert (parser
->fully_implicit_function_template_p
);
39361 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39362 && DECL_VIRTUAL_P (member_decl_opt
))
39364 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39365 "implicit templates may not be %<virtual%>");
39366 DECL_VIRTUAL_P (member_decl_opt
) = false;
39369 if (member_decl_opt
)
39370 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39371 end_template_decl ();
39373 parser
->fully_implicit_function_template_p
= false;
39374 parser
->implicit_template_parms
= 0;
39375 parser
->implicit_template_scope
= 0;
39376 --parser
->num_template_parameter_lists
;
39378 return member_decl_opt
;
39381 /* Like finish_fully_implicit_template, but to be used in error
39382 recovery, rearranging scopes so that we restore the state we had
39383 before synthesize_implicit_template_parm inserted the implement
39384 template parms scope. */
39387 abort_fully_implicit_template (cp_parser
*parser
)
39389 cp_binding_level
*return_to_scope
= current_binding_level
;
39391 if (parser
->implicit_template_scope
39392 && return_to_scope
!= parser
->implicit_template_scope
)
39394 cp_binding_level
*child
= return_to_scope
;
39395 for (cp_binding_level
*scope
= child
->level_chain
;
39396 scope
!= parser
->implicit_template_scope
;
39397 scope
= child
->level_chain
)
39399 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
39400 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
39401 current_binding_level
= parser
->implicit_template_scope
;
39404 return_to_scope
= return_to_scope
->level_chain
;
39406 finish_fully_implicit_template (parser
, NULL
);
39408 gcc_assert (current_binding_level
== return_to_scope
);
39411 /* Helper function for diagnostics that have complained about things
39412 being used with 'extern "C"' linkage.
39414 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39417 maybe_show_extern_c_location (void)
39419 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39420 inform (the_parser
->innermost_linkage_specification_location
,
39421 "%<extern \"C\"%> linkage started here");
39424 #include "gt-cp-parser.h"