2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token
=
56 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant
{
62 /* floating-point literal */
66 /* %<__FUNCTION__%> */
68 /* %<__PRETTY_FUNCTION__%> */
76 /* %<typeid%> operator */
78 /* non-constant compound literals */
86 /* an array reference */
92 /* the address of a label */
106 /* calls to overloaded operators */
110 /* a comma operator */
112 /* a call to a constructor */
114 /* a transaction expression */
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error
{
124 /* is not a class or namespace */
126 /* is not a class, namespace, or enumeration */
130 /* The various kinds of required token */
131 enum required_token
{
133 RT_SEMICOLON
, /* ';' */
134 RT_OPEN_PAREN
, /* '(' */
135 RT_CLOSE_BRACE
, /* '}' */
136 RT_OPEN_BRACE
, /* '{' */
137 RT_CLOSE_SQUARE
, /* ']' */
138 RT_OPEN_SQUARE
, /* '[' */
142 RT_GREATER
, /* '>' */
144 RT_ELLIPSIS
, /* '...' */
148 RT_COLON_SCOPE
, /* ':' or '::' */
149 RT_CLOSE_PAREN
, /* ')' */
150 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
151 RT_PRAGMA_EOL
, /* end of line */
152 RT_NAME
, /* identifier */
154 /* The type is CPP_KEYWORD */
156 RT_DELETE
, /* delete */
157 RT_RETURN
, /* return */
158 RT_WHILE
, /* while */
159 RT_EXTERN
, /* extern */
160 RT_STATIC_ASSERT
, /* static_assert */
161 RT_DECLTYPE
, /* decltype */
162 RT_OPERATOR
, /* operator */
163 RT_CLASS
, /* class */
164 RT_TEMPLATE
, /* template */
165 RT_NAMESPACE
, /* namespace */
166 RT_USING
, /* using */
169 RT_CATCH
, /* catch */
170 RT_THROW
, /* throw */
171 RT_LABEL
, /* __label__ */
172 RT_AT_TRY
, /* @try */
173 RT_AT_SYNCHRONIZED
, /* @synchronized */
174 RT_AT_THROW
, /* @throw */
176 RT_SELECT
, /* selection-statement */
177 RT_ITERATION
, /* iteration-statement */
178 RT_JUMP
, /* jump-statement */
179 RT_CLASS_KEY
, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
194 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
196 saved (parser
->in_type_id_in_expr_p
)
197 { parser
->in_type_id_in_expr_p
= set
; }
198 ~type_id_in_expr_sentinel ()
199 { parser
->in_type_id_in_expr_p
= saved
; }
204 static cp_lexer
*cp_lexer_new_main
206 static cp_lexer
*cp_lexer_new_from_tokens
207 (cp_token_cache
*tokens
);
208 static void cp_lexer_destroy
210 static int cp_lexer_saving_tokens
212 static cp_token
*cp_lexer_token_at
213 (cp_lexer
*, cp_token_position
);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer
*, cp_token
*);
216 static inline cp_token
*cp_lexer_peek_token
218 static cp_token
*cp_lexer_peek_nth_token
219 (cp_lexer
*, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer
*, enum cpp_ttype
);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer
*, enum cpp_ttype
);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer
*, enum rid
);
226 static cp_token
*cp_lexer_consume_token
228 static void cp_lexer_purge_token
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer
*, cp_token_position
);
232 static void cp_lexer_save_tokens
234 static void cp_lexer_commit_tokens
236 static void cp_lexer_rollback_tokens
238 static void cp_lexer_print_token
239 (FILE *, cp_token
*);
240 static inline bool cp_lexer_debugging_p
242 static void cp_lexer_start_debugging
243 (cp_lexer
*) ATTRIBUTE_UNUSED
;
244 static void cp_lexer_stop_debugging
245 (cp_lexer
*) ATTRIBUTE_UNUSED
;
247 static cp_token_cache
*cp_token_cache_new
248 (cp_token
*, cp_token
*);
250 static void cp_parser_initial_pragma
253 static bool cp_parser_omp_declare_reduction_exprs
255 static void cp_finalize_oacc_routine
256 (cp_parser
*, tree
, bool);
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream
;
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand
;
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
278 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
279 cp_token
*start_token
, unsigned num
,
280 cp_token
*curr_token
)
282 unsigned i
, nprinted
;
286 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
292 num
= buffer
->length ();
294 if (start_token
== NULL
)
295 start_token
= buffer
->address ();
297 if (start_token
> buffer
->address ())
299 cp_lexer_print_token (file
, &(*buffer
)[0]);
300 fprintf (file
, " ... ");
305 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
307 if (token
== start_token
)
314 if (token
== curr_token
)
315 fprintf (file
, "[[");
317 cp_lexer_print_token (file
, token
);
319 if (token
== curr_token
)
320 fprintf (file
, "]]");
326 case CPP_CLOSE_BRACE
:
336 if (i
== num
&& i
< buffer
->length ())
338 fprintf (file
, " ... ");
339 cp_lexer_print_token (file
, &buffer
->last ());
342 fprintf (file
, "\n");
346 /* Dump all tokens in BUFFER to stderr. */
349 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
351 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
355 debug (vec
<cp_token
, va_gc
> &ref
)
357 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
361 debug (vec
<cp_token
, va_gc
> *ptr
)
366 fprintf (stderr
, "<nil>\n");
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
374 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
378 fprintf (file
, "%s: ", desc
);
379 print_node_brief (file
, "", t
, 0);
384 /* Dump parser context C to FILE. */
387 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
389 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
391 print_node_brief (file
, "", c
->object_type
, 0);
392 fprintf (file
, "}\n");
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
399 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
402 cp_parser_context
*c
;
404 fprintf (file
, "Parsing context stack:\n");
405 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
407 fprintf (file
, "\t#%u: ", i
);
408 cp_debug_print_context (file
, c
);
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
416 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
419 fprintf (file
, "%s: true\n", desc
);
423 /* Print an unparsed function entry UF to FILE. */
426 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
429 cp_default_arg_entry
*default_arg_fn
;
432 fprintf (file
, "\tFunctions with default args:\n");
434 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
437 fprintf (file
, "\t\tClass type: ");
438 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
439 fprintf (file
, "\t\tDeclaration: ");
440 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
441 fprintf (file
, "\n");
444 fprintf (file
, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
448 print_node_brief (file
, "", fn
, 0);
451 fprintf (file
, "\n");
453 fprintf (file
, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
457 print_node_brief (file
, "", fn
, 0);
460 fprintf (file
, "\n");
464 /* Print the stack of unparsed member functions S to FILE. */
467 cp_debug_print_unparsed_queues (FILE *file
,
468 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
471 cp_unparsed_functions_entry
*uf
;
473 fprintf (file
, "Unparsed functions\n");
474 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
476 fprintf (file
, "#%u:\n", i
);
477 cp_debug_print_unparsed_function (file
, uf
);
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
486 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
488 cp_token
*next_token
, *first_token
, *start_token
;
493 next_token
= parser
->lexer
->next_token
;
494 first_token
= parser
->lexer
->buffer
->address ();
495 start_token
= (next_token
> first_token
+ window_size
/ 2)
496 ? next_token
- window_size
/ 2
498 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
507 cp_debug_parser (FILE *file
, cp_parser
*parser
)
509 const size_t window_size
= 20;
511 expanded_location eloc
;
516 fprintf (file
, "Parser state\n\n");
517 fprintf (file
, "Number of tokens: %u\n",
518 vec_safe_length (parser
->lexer
->buffer
));
519 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
520 cp_debug_print_tree_if_set (file
, "Object scope",
521 parser
->object_scope
);
522 cp_debug_print_tree_if_set (file
, "Qualifying scope",
523 parser
->qualifying_scope
);
524 cp_debug_print_context_stack (file
, parser
->context
);
525 cp_debug_print_flag (file
, "Allow GNU extensions",
526 parser
->allow_gnu_extensions_p
);
527 cp_debug_print_flag (file
, "'>' token is greater-than",
528 parser
->greater_than_is_operator_p
);
529 cp_debug_print_flag (file
, "Default args allowed in current "
530 "parameter list", parser
->default_arg_ok_p
);
531 cp_debug_print_flag (file
, "Parsing integral constant-expression",
532 parser
->integral_constant_expression_p
);
533 cp_debug_print_flag (file
, "Allow non-constant expression in current "
534 "constant-expression",
535 parser
->allow_non_integral_constant_expression_p
);
536 cp_debug_print_flag (file
, "Seen non-constant expression",
537 parser
->non_integral_constant_expression_p
);
538 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
540 parser
->local_variables_forbidden_p
);
541 cp_debug_print_flag (file
, "In unbraced linkage specification",
542 parser
->in_unbraced_linkage_specification_p
);
543 cp_debug_print_flag (file
, "Parsing a declarator",
544 parser
->in_declarator_p
);
545 cp_debug_print_flag (file
, "In template argument list",
546 parser
->in_template_argument_list_p
);
547 cp_debug_print_flag (file
, "Parsing an iteration statement",
548 parser
->in_statement
& IN_ITERATION_STMT
);
549 cp_debug_print_flag (file
, "Parsing a switch statement",
550 parser
->in_statement
& IN_SWITCH_STMT
);
551 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
552 parser
->in_statement
& IN_OMP_BLOCK
);
553 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
554 parser
->in_statement
& IN_OMP_FOR
);
555 cp_debug_print_flag (file
, "Parsing an if statement",
556 parser
->in_statement
& IN_IF_STMT
);
557 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
558 "context", parser
->in_type_id_in_expr_p
);
559 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
560 parser
->implicit_extern_c
);
561 cp_debug_print_flag (file
, "String expressions should be translated "
562 "to execution character set",
563 parser
->translate_strings_p
);
564 cp_debug_print_flag (file
, "Parsing function body outside of a "
565 "local class", parser
->in_function_body
);
566 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
567 parser
->colon_corrects_to_scope_p
);
568 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
569 parser
->colon_doesnt_start_class_def_p
);
570 if (parser
->type_definition_forbidden_message
)
571 fprintf (file
, "Error message for forbidden type definitions: %s\n",
572 parser
->type_definition_forbidden_message
);
573 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
574 fprintf (file
, "Number of class definitions in progress: %u\n",
575 parser
->num_classes_being_defined
);
576 fprintf (file
, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser
->num_template_parameter_lists
);
578 cp_debug_parser_tokens (file
, parser
, window_size
);
579 token
= parser
->lexer
->next_token
;
580 fprintf (file
, "Next token to parse:\n");
581 fprintf (file
, "\tToken: ");
582 cp_lexer_print_token (file
, token
);
583 eloc
= expand_location (token
->location
);
584 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
585 fprintf (file
, "\tLine: %d\n", eloc
.line
);
586 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
590 debug (cp_parser
&ref
)
592 cp_debug_parser (stderr
, &ref
);
596 debug (cp_parser
*ptr
)
601 fprintf (stderr
, "<nil>\n");
604 /* Allocate memory for a new lexer object and return it. */
607 cp_lexer_alloc (void)
611 c_common_no_more_pch ();
613 /* Allocate the memory. */
614 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
616 /* Initially we are not debugging. */
617 lexer
->debugging_p
= false;
619 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
621 /* Create the buffer. */
622 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
632 cp_lexer_new_main (void)
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token
);
642 lexer
= cp_lexer_alloc ();
644 /* Put the first token in the buffer. */
645 lexer
->buffer
->quick_push (token
);
647 /* Get the remaining tokens from the preprocessor. */
648 while (token
.type
!= CPP_EOF
)
650 cp_lexer_get_preprocessor_token (lexer
, &token
);
651 vec_safe_push (lexer
->buffer
, token
);
654 lexer
->last_token
= lexer
->buffer
->address ()
655 + lexer
->buffer
->length ()
657 lexer
->next_token
= lexer
->buffer
->length ()
658 ? lexer
->buffer
->address ()
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
665 gcc_assert (!lexer
->next_token
->purged_p
);
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
673 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
675 cp_token
*first
= cache
->first
;
676 cp_token
*last
= cache
->last
;
677 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
679 /* We do not own the buffer. */
680 lexer
->buffer
= NULL
;
681 lexer
->next_token
= first
== last
? &eof_token
: first
;
682 lexer
->last_token
= last
;
684 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
686 /* Initially we are not debugging. */
687 lexer
->debugging_p
= false;
689 gcc_assert (!lexer
->next_token
->purged_p
);
693 /* Frees all resources associated with LEXER. */
696 cp_lexer_destroy (cp_lexer
*lexer
)
698 vec_free (lexer
->buffer
);
699 lexer
->saved_tokens
.release ();
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
708 #define LEXER_DEBUGGING_ENABLED_P false
710 /* Returns nonzero if debugging information should be output. */
713 cp_lexer_debugging_p (cp_lexer
*lexer
)
715 if (!LEXER_DEBUGGING_ENABLED_P
)
718 return lexer
->debugging_p
;
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
725 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
727 return lexer
->next_token
- previous_p
;
730 static inline cp_token
*
731 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
737 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
739 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer
*lexer
)
745 if (lexer
->next_token
== &eof_token
)
746 return lexer
->last_token
- 1;
748 return cp_lexer_token_position (lexer
, true);
751 static inline cp_token
*
752 cp_lexer_previous_token (cp_lexer
*lexer
)
754 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
756 /* Skip past purged tokens. */
759 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
763 return cp_lexer_token_at (lexer
, tp
);
766 /* nonzero if we are presently saving tokens. */
769 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
771 return lexer
->saved_tokens
.length () != 0;
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
780 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
782 static int is_extern_c
= 0;
784 /* Get a new token from the preprocessor. */
786 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
787 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
788 token
->keyword
= RID_MAX
;
789 token
->purged_p
= false;
790 token
->error_reported
= false;
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c
+= pending_lang_change
;
796 pending_lang_change
= 0;
797 token
->implicit_extern_c
= is_extern_c
> 0;
799 /* Check to see if this token is a keyword. */
800 if (token
->type
== CPP_NAME
)
802 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
804 /* Mark this token as a keyword. */
805 token
->type
= CPP_KEYWORD
;
806 /* Record which keyword. */
807 token
->keyword
= C_RID_CODE (token
->u
.value
);
811 if (warn_cxx11_compat
812 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
813 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
815 /* Warn about the C++0x keyword (but still treat it as
817 warning (OPT_Wc__11_compat
,
818 "identifier %qE is a keyword in C++11",
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
826 token
->keyword
= RID_MAX
;
829 else if (token
->type
== CPP_AT_NAME
)
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token
->type
= CPP_KEYWORD
;
833 switch (C_RID_CODE (token
->u
.value
))
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
843 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
844 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
845 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
846 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
847 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
848 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
849 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
850 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
851 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
856 /* Update the globals input_location and the input file stack from TOKEN. */
858 cp_lexer_set_source_position_from_token (cp_token
*token
)
860 if (token
->type
!= CPP_EOF
)
862 input_location
= token
->location
;
866 /* Update the globals input_location and the input file stack from LEXER. */
868 cp_lexer_set_source_position (cp_lexer
*lexer
)
870 cp_token
*token
= cp_lexer_peek_token (lexer
);
871 cp_lexer_set_source_position_from_token (token
);
874 /* Return a pointer to the next token in the token stream, but do not
877 static inline cp_token
*
878 cp_lexer_peek_token (cp_lexer
*lexer
)
880 if (cp_lexer_debugging_p (lexer
))
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
883 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
884 putc ('\n', cp_lexer_debug_stream
);
886 return lexer
->next_token
;
889 /* Return true if the next token has the indicated TYPE. */
892 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
894 return cp_lexer_peek_token (lexer
)->type
== type
;
897 /* Return true if the next token does not have the indicated TYPE. */
900 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
902 return !cp_lexer_next_token_is (lexer
, type
);
905 /* Return true if the next token is the indicated KEYWORD. */
908 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
910 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
914 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
916 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
920 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
922 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
925 /* Return true if the next token is not the indicated KEYWORD. */
928 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
930 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
933 /* Return true if KEYWORD can start a decl-specifier. */
936 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
943 /* Storage classes. */
949 /* Elaborated type specifiers. */
955 /* Simple type specifiers. */
969 /* GNU extensions. */
972 /* C++0x extensions. */
974 case RID_UNDERLYING_TYPE
:
979 if (keyword
>= RID_FIRST_INT_N
980 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
981 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
987 /* Return true if the next token is a keyword for a decl-specifier. */
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
994 token
= cp_lexer_peek_token (lexer
);
995 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
998 /* Returns TRUE iff the token T begins a decltype type. */
1001 token_is_decltype (cp_token
*t
)
1003 return (t
->keyword
== RID_DECLTYPE
1004 || t
->type
== CPP_DECLTYPE
);
1007 /* Returns TRUE iff the next token begins a decltype type. */
1010 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1012 cp_token
*t
= cp_lexer_peek_token (lexer
);
1013 return token_is_decltype (t
);
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1020 saved_checks_value (struct tree_check
*check_value
)
1022 /* Perform any access checks that were deferred. */
1023 vec
<deferred_access_check
, va_gc
> *checks
;
1024 deferred_access_check
*chk
;
1025 checks
= check_value
->checks
;
1029 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1030 perform_or_defer_access_check (chk
->binfo
,
1032 chk
->diag_decl
, tf_warning_or_error
);
1034 /* Return the stored value. */
1035 return check_value
->value
;
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1045 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1049 /* N is 1-based, not zero-based. */
1052 if (cp_lexer_debugging_p (lexer
))
1053 fprintf (cp_lexer_debug_stream
,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1057 token
= lexer
->next_token
;
1058 gcc_assert (!n
|| token
!= &eof_token
);
1062 if (token
== lexer
->last_token
)
1068 if (!token
->purged_p
)
1072 if (cp_lexer_debugging_p (lexer
))
1074 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1075 putc ('\n', cp_lexer_debug_stream
);
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1085 cp_lexer_consume_token (cp_lexer
* lexer
)
1087 cp_token
*token
= lexer
->next_token
;
1089 gcc_assert (token
!= &eof_token
);
1090 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1094 lexer
->next_token
++;
1095 if (lexer
->next_token
== lexer
->last_token
)
1097 lexer
->next_token
= &eof_token
;
1102 while (lexer
->next_token
->purged_p
);
1104 cp_lexer_set_source_position_from_token (token
);
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer
))
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1110 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1111 putc ('\n', cp_lexer_debug_stream
);
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1122 cp_lexer_purge_token (cp_lexer
*lexer
)
1124 cp_token
*tok
= lexer
->next_token
;
1126 gcc_assert (tok
!= &eof_token
);
1127 tok
->purged_p
= true;
1128 tok
->location
= UNKNOWN_LOCATION
;
1129 tok
->u
.value
= NULL_TREE
;
1130 tok
->keyword
= RID_MAX
;
1135 if (tok
== lexer
->last_token
)
1141 while (tok
->purged_p
);
1142 lexer
->next_token
= tok
;
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1150 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1152 cp_token
*peek
= lexer
->next_token
;
1154 if (peek
== &eof_token
)
1155 peek
= lexer
->last_token
;
1157 gcc_assert (tok
< peek
);
1159 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1161 tok
->purged_p
= true;
1162 tok
->location
= UNKNOWN_LOCATION
;
1163 tok
->u
.value
= NULL_TREE
;
1164 tok
->keyword
= RID_MAX
;
1168 /* Begin saving tokens. All tokens consumed after this point will be
1172 cp_lexer_save_tokens (cp_lexer
* lexer
)
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer
))
1176 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1178 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1181 /* Commit to the portion of the token stream most recently saved. */
1184 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer
))
1188 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1190 lexer
->saved_tokens
.pop ();
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1197 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer
))
1201 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1203 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1211 struct saved_token_sentinel
1216 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1218 len
= lexer
->saved_tokens
.length ();
1219 cp_lexer_save_tokens (lexer
);
1223 cp_lexer_rollback_tokens (lexer
);
1226 ~saved_token_sentinel()
1229 cp_lexer_commit_tokens (lexer
);
1230 gcc_assert (lexer
->saved_tokens
.length () == len
);
1234 /* Print a representation of the TOKEN on the STREAM. */
1237 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names
[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1248 /* C++ parser token types - see "Manifest constants", above. */
1251 "NESTED_NAME_SPECIFIER",
1254 /* For some tokens, print the associated data. */
1255 switch (token
->type
)
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token
->u
.value
))
1264 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1271 case CPP_UTF8STRING
:
1272 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1276 print_generic_expr (stream
, token
->u
.value
);
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token
->type
< ARRAY_SIZE(token_names
))
1283 fputs (token_names
[token
->type
], stream
);
1285 fprintf (stream
, "[%d]", token
->type
);
1291 debug (cp_token
&ref
)
1293 cp_lexer_print_token (stderr
, &ref
);
1294 fprintf (stderr
, "\n");
1298 debug (cp_token
*ptr
)
1303 fprintf (stderr
, "<nil>\n");
1307 /* Start emitting debugging information. */
1310 cp_lexer_start_debugging (cp_lexer
* lexer
)
1312 if (!LEXER_DEBUGGING_ENABLED_P
)
1313 fatal_error (input_location
,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1316 lexer
->debugging_p
= true;
1317 cp_lexer_debug_stream
= stderr
;
1320 /* Stop emitting debugging information. */
1323 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1325 if (!LEXER_DEBUGGING_ENABLED_P
)
1326 fatal_error (input_location
,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1329 lexer
->debugging_p
= false;
1330 cp_lexer_debug_stream
= NULL
;
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1335 static cp_token_cache
*
1336 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1338 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1339 cache
->first
= first
;
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1348 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1350 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser
->omp_declare_simd
= NULL
;
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1362 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1364 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1366 if (fndecl
== error_mark_node
)
1368 parser
->omp_declare_simd
= NULL
;
1371 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1373 cp_ensure_no_omp_declare_simd (parser
);
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1383 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1385 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1387 error_at (parser
->oacc_routine
->loc
,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser
->oacc_routine
= NULL
;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1399 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1401 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator
*make_call_declarator
1412 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1413 static cp_declarator
*make_array_declarator
1414 (cp_declarator
*, tree
);
1415 static cp_declarator
*make_pointer_declarator
1416 (cp_cv_quals
, cp_declarator
*, tree
);
1417 static cp_declarator
*make_reference_declarator
1418 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1419 static cp_declarator
*make_ptrmem_declarator
1420 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1422 /* An erroneous declarator. */
1423 static cp_declarator
*cp_error_declarator
;
1425 /* The obstack on which declarators and related data structures are
1427 static struct obstack declarator_obstack
;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes
)
1434 return obstack_alloc (&declarator_obstack
, bytes
);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator
*
1441 make_declarator (cp_declarator_kind kind
)
1443 cp_declarator
*declarator
;
1445 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1446 declarator
->kind
= kind
;
1447 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1448 declarator
->attributes
= NULL_TREE
;
1449 declarator
->std_attributes
= NULL_TREE
;
1450 declarator
->declarator
= NULL
;
1451 declarator
->parameter_pack_p
= false;
1452 declarator
->id_loc
= UNKNOWN_LOCATION
;
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1463 static cp_declarator
*
1464 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1465 special_function_kind sfk
)
1467 cp_declarator
*declarator
;
1469 /* It is valid to write:
1471 class C { void f(); };
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1479 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1480 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1482 gcc_assert (identifier_p (unqualified_name
)
1483 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1486 declarator
= make_declarator (cdk_id
);
1487 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1488 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1489 declarator
->u
.id
.sfk
= sfk
;
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1503 cp_declarator
*declarator
;
1505 declarator
= make_declarator (cdk_pointer
);
1506 declarator
->declarator
= target
;
1507 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1508 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1511 declarator
->id_loc
= target
->id_loc
;
1512 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1513 target
->parameter_pack_p
= false;
1516 declarator
->parameter_pack_p
= false;
1518 declarator
->std_attributes
= attributes
;
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1528 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1529 bool rvalue_ref
, tree attributes
)
1531 cp_declarator
*declarator
;
1533 declarator
= make_declarator (cdk_reference
);
1534 declarator
->declarator
= target
;
1535 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1536 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1539 declarator
->id_loc
= target
->id_loc
;
1540 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1541 target
->parameter_pack_p
= false;
1544 declarator
->parameter_pack_p
= false;
1546 declarator
->std_attributes
= attributes
;
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1557 cp_declarator
*pointee
,
1560 cp_declarator
*declarator
;
1562 declarator
= make_declarator (cdk_ptrmem
);
1563 declarator
->declarator
= pointee
;
1564 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1565 declarator
->u
.pointer
.class_type
= class_type
;
1569 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1570 pointee
->parameter_pack_p
= false;
1573 declarator
->parameter_pack_p
= false;
1575 declarator
->std_attributes
= attributes
;
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1586 make_call_declarator (cp_declarator
*target
,
1588 cp_cv_quals cv_qualifiers
,
1589 cp_virt_specifiers virt_specifiers
,
1590 cp_ref_qualifier ref_qualifier
,
1592 tree exception_specification
,
1593 tree late_return_type
,
1594 tree requires_clause
)
1596 cp_declarator
*declarator
;
1598 declarator
= make_declarator (cdk_function
);
1599 declarator
->declarator
= target
;
1600 declarator
->u
.function
.parameters
= parms
;
1601 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1602 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1603 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1604 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1605 declarator
->u
.function
.exception_specification
= exception_specification
;
1606 declarator
->u
.function
.late_return_type
= late_return_type
;
1607 declarator
->u
.function
.requires_clause
= requires_clause
;
1610 declarator
->id_loc
= target
->id_loc
;
1611 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1612 target
->parameter_pack_p
= false;
1615 declarator
->parameter_pack_p
= false;
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1624 make_array_declarator (cp_declarator
*element
, tree bounds
)
1626 cp_declarator
*declarator
;
1628 declarator
= make_declarator (cdk_array
);
1629 declarator
->declarator
= element
;
1630 declarator
->u
.array
.bounds
= bounds
;
1633 declarator
->id_loc
= element
->id_loc
;
1634 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1635 element
->parameter_pack_p
= false;
1638 declarator
->parameter_pack_p
= false;
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1646 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1648 if (declarator
&& declarator
->parameter_pack_p
)
1649 /* We already saw an ellipsis. */
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1657 while (declarator
&& !found
)
1659 switch ((int)declarator
->kind
)
1671 declarator
= declarator
->declarator
;
1679 cp_parameter_declarator
*no_parameters
;
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1684 cp_parameter_declarator
*
1685 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1686 cp_declarator
*declarator
,
1687 tree default_argument
,
1689 bool template_parameter_pack_p
= false)
1691 cp_parameter_declarator
*parameter
;
1693 parameter
= ((cp_parameter_declarator
*)
1694 alloc_declarator (sizeof (cp_parameter_declarator
)));
1695 parameter
->next
= NULL
;
1696 if (decl_specifiers
)
1697 parameter
->decl_specifiers
= *decl_specifiers
;
1699 clear_decl_specs (¶meter
->decl_specifiers
);
1700 parameter
->declarator
= declarator
;
1701 parameter
->default_argument
= default_argument
;
1702 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1703 parameter
->loc
= loc
;
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1711 function_declarator_p (const cp_declarator
*declarator
)
1715 if (declarator
->kind
== cdk_function
1716 && declarator
->declarator
->kind
== cdk_id
)
1718 if (declarator
->kind
== cdk_id
1719 || declarator
->kind
== cdk_decomp
1720 || declarator
->kind
== cdk_error
)
1722 declarator
= declarator
->declarator
;
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1788 CP_PARSER_FLAGS_NONE
= 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags
;
1809 /* The different kinds of declarators we want to parse. */
1811 enum cp_parser_declarator_kind
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT
,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED
,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1828 PREC_LOGICAL_OR_EXPRESSION
,
1829 PREC_LOGICAL_AND_EXPRESSION
,
1830 PREC_INCLUSIVE_OR_EXPRESSION
,
1831 PREC_EXCLUSIVE_OR_EXPRESSION
,
1832 PREC_AND_EXPRESSION
,
1833 PREC_EQUALITY_EXPRESSION
,
1834 PREC_RELATIONAL_EXPRESSION
,
1835 PREC_SHIFT_EXPRESSION
,
1836 PREC_ADDITIVE_EXPRESSION
,
1837 PREC_MULTIPLICATIVE_EXPRESSION
,
1839 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1845 struct cp_parser_binary_operations_map_node
1847 /* The token type. */
1848 enum cpp_ttype token_type
;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type
;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec
;
1855 struct cp_parser_expression_stack_entry
1857 /* Left hand side of the binary operation we are currently
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type
;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type
;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec
;
1867 /* Location of the binary operation we are parsing. */
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1879 /* Constructors and destructors. */
1881 static cp_parser_context
*cp_parser_context_new
1882 (cp_parser_context
*);
1884 /* Class variables. */
1886 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1892 static const cp_parser_binary_operations_map_node binops
[] = {
1893 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1894 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1896 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1897 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1898 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1900 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1901 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1903 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1904 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1906 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1907 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1908 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1909 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1911 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1912 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1914 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1916 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1918 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1920 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1922 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1928 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1930 /* Constructors and destructors. */
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1935 static cp_parser_context
*
1936 cp_parser_context_new (cp_parser_context
* next
)
1938 cp_parser_context
*context
;
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list
!= NULL
)
1943 /* Pull the first entry from the free list. */
1944 context
= cp_parser_context_free_list
;
1945 cp_parser_context_free_list
= context
->next
;
1946 memset (context
, 0, sizeof (*context
));
1949 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1951 /* No errors have occurred yet in this context. */
1952 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context
->object_type
= next
->object_type
;
1960 /* Thread the stack. */
1961 context
->next
= next
;
1967 /* Managing the unparsed function queues. */
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1979 push_unparsed_function_queues (cp_parser
*parser
)
1981 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1982 vec_safe_push (parser
->unparsed_queues
, e
);
1986 pop_unparsed_function_queues (cp_parser
*parser
)
1988 release_tree_vector (unparsed_funs_with_definitions
);
1989 parser
->unparsed_queues
->pop ();
1994 /* Constructors and destructors. */
1996 static cp_parser
*cp_parser_new
1999 /* Routines to parse various constructs.
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2011 /* Lexical conventions [gram.lex] */
2013 static cp_expr cp_parser_identifier
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser
*, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2019 static tree cp_parser_userdef_string_literal
2021 static cp_expr cp_parser_userdef_numeric_literal
2024 /* Basic concepts [gram.basic] */
2026 static bool cp_parser_translation_unit
2029 /* Expressions [gram.expr] */
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser
*, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser
*, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser
*, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser
*, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser
*, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser
*, tree
, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2049 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2050 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser
*, tree
, tree
*, tree
*);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2060 static tree cp_parser_new_expression
2062 static vec
<tree
, va_gc
> *cp_parser_new_placement
2064 static tree cp_parser_new_type_id
2065 (cp_parser
*, tree
*);
2066 static cp_declarator
*cp_parser_new_declarator_opt
2068 static cp_declarator
*cp_parser_direct_new_declarator
2070 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2072 static tree cp_parser_delete_expression
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser
*, cp_expr
);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2084 static cp_expr cp_parser_expression
2085 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2090 static cp_expr cp_parser_lambda_expression
2092 static void cp_parser_lambda_introducer
2093 (cp_parser
*, tree
);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser
*, tree
);
2096 static void cp_parser_lambda_body
2097 (cp_parser
*, tree
);
2099 /* Statements [gram.stmt.stmt] */
2101 static void cp_parser_statement
2102 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser
*, tree
);
2105 static tree cp_parser_expression_statement
2106 (cp_parser
*, tree
);
2107 static tree cp_parser_compound_statement
2108 (cp_parser
*, tree
, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser
*, tree
);
2111 static tree cp_parser_selection_statement
2112 (cp_parser
*, bool *, vec
<tree
> *);
2113 static tree cp_parser_condition
2115 static tree cp_parser_iteration_statement
2116 (cp_parser
*, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser
*, tree
*decl
);
2119 static tree cp_parser_for
2120 (cp_parser
*, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser
*, tree
, tree
, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree
, tree
*, tree
*);
2129 static tree cp_parser_range_for_member_function
2131 static tree cp_parser_jump_statement
2133 static void cp_parser_declaration_statement
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser
*, bool *, const token_indent_info
&);
2141 /* Declarations [gram.dcl.dcl] */
2143 static void cp_parser_declaration_seq_opt
2145 static void cp_parser_declaration
2147 static void cp_parser_block_declaration
2148 (cp_parser
*, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser
*, bool, tree
*);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser
*, cp_decl_specifier_seq
*);
2157 static tree cp_parser_type_specifier
2158 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2162 static tree cp_parser_type_name
2163 (cp_parser
*, bool);
2164 static tree cp_parser_type_name
2166 static tree cp_parser_nonclass_name
2167 (cp_parser
* parser
);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser
*, bool, bool);
2170 static tree cp_parser_enum_specifier
2172 static void cp_parser_enumerator_list
2173 (cp_parser
*, tree
);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser
*, tree
);
2176 static tree cp_parser_namespace_name
2178 static void cp_parser_namespace_definition
2180 static void cp_parser_namespace_body
2182 static tree cp_parser_qualified_namespace_specifier
2184 static void cp_parser_namespace_alias_definition
2186 static bool cp_parser_using_declaration
2187 (cp_parser
*, bool);
2188 static void cp_parser_using_directive
2190 static tree cp_parser_alias_declaration
2192 static void cp_parser_asm_definition
2194 static void cp_parser_linkage_specification
2196 static void cp_parser_static_assert
2197 (cp_parser
*, bool);
2198 static tree cp_parser_decltype
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2203 /* Declarators [gram.dcl.decl] */
2205 static tree cp_parser_init_declarator
2206 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2207 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2208 static cp_declarator
*cp_parser_declarator
2209 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2210 static cp_declarator
*cp_parser_direct_declarator
2211 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2220 static tree cp_parser_tx_qualifier_opt
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2224 static tree cp_parser_declarator_id
2225 (cp_parser
*, bool);
2226 static tree cp_parser_type_id
2228 static tree cp_parser_template_type_arg
2230 static tree
cp_parser_trailing_type_id (cp_parser
*);
2231 static tree cp_parser_type_id_1
2232 (cp_parser
*, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2235 static tree cp_parser_parameter_declaration_clause
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser
*, bool *);
2239 static cp_parameter_declarator
*cp_parser_parameter_declaration
2240 (cp_parser
*, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser
*, bool);
2243 static void cp_parser_function_body
2244 (cp_parser
*, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser
*, bool *, bool *);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser
*, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser
*, bool*);
2251 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2252 (cp_parser
*, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser
*, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser
*, tree
);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser
*, tree
);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser
*, tree
);
2265 static tree finish_fully_implicit_template
2266 (cp_parser
*, tree
);
2267 static void abort_fully_implicit_template
2270 /* Classes [gram.class] */
2272 static tree cp_parser_class_name
2273 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2276 static tree cp_parser_class_head
2277 (cp_parser
*, bool *);
2278 static enum tag_types cp_parser_class_key
2280 static void cp_parser_type_parameter_key
2281 (cp_parser
* parser
);
2282 static void cp_parser_member_specification_opt
2284 static void cp_parser_member_declaration
2286 static tree cp_parser_pure_specifier
2288 static tree cp_parser_constant_initializer
2291 /* Derived classes [gram.class.derived] */
2293 static tree cp_parser_base_clause
2295 static tree cp_parser_base_specifier
2298 /* Special member functions [gram.special] */
2300 static tree cp_parser_conversion_function_id
2302 static tree cp_parser_conversion_type_id
2304 static cp_declarator
*cp_parser_conversion_declarator_opt
2306 static void cp_parser_ctor_initializer_opt
2308 static void cp_parser_mem_initializer_list
2310 static tree cp_parser_mem_initializer
2312 static tree cp_parser_mem_initializer_id
2315 /* Overloading [gram.over] */
2317 static cp_expr cp_parser_operator_function_id
2319 static cp_expr cp_parser_operator
2322 /* Templates [gram.temp] */
2324 static void cp_parser_template_declaration
2325 (cp_parser
*, bool);
2326 static tree cp_parser_template_parameter_list
2328 static tree cp_parser_template_parameter
2329 (cp_parser
*, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser
*, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser
*, bool, bool, enum tag_types
, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2336 static tree cp_parser_template_argument_list
2338 static tree cp_parser_template_argument
2340 static void cp_parser_explicit_instantiation
2342 static void cp_parser_explicit_specialization
2345 /* Exception handling [gram.exception] */
2347 static tree cp_parser_try_block
2349 static void cp_parser_function_try_block
2351 static void cp_parser_handler_seq
2353 static void cp_parser_handler
2355 static tree cp_parser_exception_declaration
2357 static tree cp_parser_throw_expression
2359 static tree cp_parser_exception_specification_opt
2361 static tree cp_parser_type_id_list
2364 /* GNU Extensions */
2366 static tree cp_parser_asm_specification_opt
2368 static tree cp_parser_asm_operand_list
2370 static tree cp_parser_asm_clobber_list
2372 static tree cp_parser_asm_label_list
2374 static bool cp_next_tokens_can_be_attribute_p
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2378 static bool cp_next_tokens_can_be_std_attribute_p
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser
*, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser
*, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser
*, size_t);
2386 static tree cp_parser_attributes_opt
2388 static tree cp_parser_gnu_attributes_opt
2390 static tree cp_parser_gnu_attribute_list
2392 static tree cp_parser_std_attribute
2393 (cp_parser
*, tree
);
2394 static tree cp_parser_std_attribute_spec
2396 static tree cp_parser_std_attribute_spec_seq
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser
*, size_t);
2400 static bool cp_parser_extension_opt
2401 (cp_parser
*, int *);
2402 static void cp_parser_label_declaration
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2409 static tree cp_parser_requires_clause_opt
2411 static tree cp_parser_requires_expression
2413 static tree cp_parser_requirement_parameter_list
2415 static tree cp_parser_requirement_body
2417 static tree cp_parser_requirement_list
2419 static tree cp_parser_requirement
2421 static tree cp_parser_simple_requirement
2423 static tree cp_parser_compound_requirement
2425 static tree cp_parser_type_requirement
2427 static tree cp_parser_nested_requirement
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser
*, cp_token
*);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser
*, enum rid
);
2436 static void cp_parser_function_transaction
2437 (cp_parser
*, enum rid
);
2438 static tree cp_parser_transaction_cancel
2441 enum pragma_context
{
2448 static bool cp_parser_pragma
2449 (cp_parser
*, enum pragma_context
, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2455 static tree cp_parser_objc_message_args
2457 static tree cp_parser_objc_message_expression
2459 static cp_expr cp_parser_objc_encode_expression
2461 static tree cp_parser_objc_defs_expression
2463 static tree cp_parser_objc_protocol_expression
2465 static tree cp_parser_objc_selector_expression
2467 static cp_expr cp_parser_objc_expression
2469 static bool cp_parser_objc_selector_p
2471 static tree cp_parser_objc_selector
2473 static tree cp_parser_objc_protocol_refs_opt
2475 static void cp_parser_objc_declaration
2476 (cp_parser
*, tree
);
2477 static tree cp_parser_objc_statement
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser
*, tree
*);
2481 static void cp_parser_objc_at_property_declaration
2483 static void cp_parser_objc_at_synthesize_declaration
2485 static void cp_parser_objc_at_dynamic_declaration
2487 static tree cp_parser_objc_struct_declaration
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser
*, tree
, location_t
);
2496 static tree cp_parser_maybe_treat_template_as_class
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser
*, cp_declarator
*, location_t
);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2502 static cp_expr cp_parser_simple_cast_expression
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser
*, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser
*, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser
*, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser
*, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec
<deferred_access_check
, va_gc
> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser
*, tree
);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2522 static tree cp_parser_save_nsdmi
2524 static tree cp_parser_enclosed_template_argument_list
2526 static void cp_parser_save_default_args
2527 (cp_parser
*, tree
);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser
*, tree
);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser
*, tree
, tree
, tree
);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser
*, tree
);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser
*, tree
);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser
*, enum rid
);
2538 static cp_expr cp_parser_trait_expr
2539 (cp_parser
*, enum rid
);
2540 static bool cp_parser_declares_only_class_p
2542 static void cp_parser_set_storage_class
2543 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq
*decl_specs
,
2548 cp_decl_spec ds
, cp_token
*);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq
*);
2551 static void cp_parser_required_error
2552 (cp_parser
*, required_token
, bool, location_t
);
2553 static cp_token
*cp_parser_require
2554 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2555 static cp_token
*cp_parser_require_keyword
2556 (cp_parser
*, enum rid
, required_token
);
2557 static bool cp_parser_token_starts_function_definition_p
2559 static bool cp_parser_next_token_starts_class_definition_p
2561 static bool cp_parser_next_token_ends_template_argument_p
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser
*, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2569 static void cp_parser_check_class_key
2570 (enum tag_types
, tree type
);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type
, location_t location
);
2573 static bool cp_parser_optional_template_keyword
2575 static void cp_parser_pre_parsed_nested_name_specifier
2577 static bool cp_parser_cache_group
2578 (cp_parser
*, enum cpp_ttype
, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser
*parser
, bool nsdmi
);
2581 static void cp_parser_parse_tentatively
2583 static void cp_parser_commit_to_tentative_parse
2585 static void cp_parser_commit_to_topmost_tentative_parse
2587 static void cp_parser_abort_tentative_parse
2589 static bool cp_parser_parse_definitely
2591 static inline bool cp_parser_parsing_tentatively
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2595 static void cp_parser_error
2596 (cp_parser
*, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2599 static bool cp_parser_simulate_error
2601 static bool cp_parser_check_type_definition
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator
*, tree
, location_t type_location
);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser
*, non_integral_constant
);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser
*, tree
, location_t
);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser
*, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2619 static void cp_parser_skip_to_end_of_block_or_statement
2621 static bool cp_parser_skip_to_closing_brace
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser
*, cp_token
*);
2627 static bool cp_parser_error_occurred
2629 static bool cp_parser_allow_gnu_extensions_p
2631 static bool cp_parser_is_pure_string_literal
2633 static bool cp_parser_is_string_literal
2635 static bool cp_parser_is_keyword
2636 (cp_token
*, enum rid
);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser
*, tree
, location_t location
);
2639 static cp_declarator
* cp_parser_make_indirect_declarator
2640 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2641 static bool cp_parser_compound_literal_p
2643 static bool cp_parser_array_designator_p
2645 static bool cp_parser_init_statement_p
2647 static bool cp_parser_skip_to_closing_square_bracket
2650 /* Concept-related syntactic transformations */
2652 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2653 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2661 ++cp_unevaluated_operand
;
2662 ++c_inhibit_evaluation_warnings
;
2665 cp_unevaluated::~cp_unevaluated ()
2667 --c_inhibit_evaluation_warnings
;
2668 --cp_unevaluated_operand
;
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2674 /* Returns nonzero if we are parsing tentatively. */
2677 cp_parser_parsing_tentatively (cp_parser
* parser
)
2679 return parser
->context
->next
!= NULL
;
2682 /* Returns nonzero if TOKEN is a string literal. */
2685 cp_parser_is_pure_string_literal (cp_token
* token
)
2687 return (token
->type
== CPP_STRING
||
2688 token
->type
== CPP_STRING16
||
2689 token
->type
== CPP_STRING32
||
2690 token
->type
== CPP_WSTRING
||
2691 token
->type
== CPP_UTF8STRING
);
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2698 cp_parser_is_string_literal (cp_token
* token
)
2700 return (cp_parser_is_pure_string_literal (token
) ||
2701 token
->type
== CPP_STRING_USERDEF
||
2702 token
->type
== CPP_STRING16_USERDEF
||
2703 token
->type
== CPP_STRING32_USERDEF
||
2704 token
->type
== CPP_WSTRING_USERDEF
||
2705 token
->type
== CPP_UTF8STRING_USERDEF
);
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2711 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2713 return token
->keyword
== keyword
;
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token
*token
)
2722 if (token
->type
!= CPP_PRAGMA
)
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2740 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2741 location_t
*out_loc
)
2743 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2744 if (token2
->type
!= tok1_kind
)
2746 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2747 if (token3
->type
!= tok1_kind
)
2749 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2750 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2753 /* It must be at the start of the line. */
2754 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2755 if (LOCATION_COLUMN (start_loc
) != 1)
2758 /* We have a conflict marker. Construct a location of the form:
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc
= get_finish (token4
->location
);
2763 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2772 get_matching_symbol (required_token token_desc
)
2779 case RT_CLOSE_BRACE
:
2781 case RT_CLOSE_PAREN
:
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc
)
2795 return CPP_SEMICOLON
;
2797 return CPP_OPEN_PAREN
;
2798 case RT_CLOSE_BRACE
:
2799 return CPP_CLOSE_BRACE
;
2801 return CPP_OPEN_BRACE
;
2802 case RT_CLOSE_SQUARE
:
2803 return CPP_CLOSE_SQUARE
;
2804 case RT_OPEN_SQUARE
:
2805 return CPP_OPEN_SQUARE
;
2810 case RT_CLOSE_PAREN
:
2811 return CPP_CLOSE_PAREN
;
2814 /* Use CPP_EOF as a "no completions possible" code. */
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2839 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2840 required_token missing_token_desc
,
2841 location_t matching_location
)
2843 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token
);
2848 if (token
->type
== CPP_PRAGMA
)
2850 error_at (token
->location
,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser
, token
);
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token
->type
== CPP_LSHIFT
2858 || token
->type
== CPP_RSHIFT
2859 || token
->type
== CPP_EQ_EQ
)
2862 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2864 error_at (loc
, "version control conflict marker in file");
2869 gcc_rich_location
richloc (input_location
);
2871 bool added_matching_location
= false;
2873 if (missing_token_desc
!= RT_NONE
)
2875 /* Potentially supply a fix-it hint, suggesting to add the
2876 missing token immediately after the *previous* token.
2877 This may move the primary location within richloc. */
2878 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2879 location_t prev_token_loc
2880 = cp_lexer_previous_token (parser
->lexer
)->location
;
2881 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2883 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2884 Attempt to consolidate diagnostics by printing it as a
2885 secondary range within the main diagnostic. */
2886 if (matching_location
!= UNKNOWN_LOCATION
)
2887 added_matching_location
2888 = richloc
.add_location_if_nearby (matching_location
);
2891 /* Actually emit the error. */
2892 c_parse_error (gmsgid
,
2893 /* Because c_parser_error does not understand
2894 CPP_KEYWORD, keywords are treated like
2896 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2897 token
->u
.value
, token
->flags
, &richloc
);
2899 if (missing_token_desc
!= RT_NONE
)
2901 /* If we weren't able to consolidate matching_location, then
2902 print it as a secondary diagnostic. */
2903 if (matching_location
!= UNKNOWN_LOCATION
2904 && !added_matching_location
)
2905 inform (matching_location
, "to match this %qs",
2906 get_matching_symbol (missing_token_desc
));
2910 /* If not parsing tentatively, issue a diagnostic of the form
2911 FILE:LINE: MESSAGE before TOKEN
2912 where TOKEN is the next token in the input stream. MESSAGE
2913 (specified by the caller) is usually of the form "expected
2917 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2919 if (!cp_parser_simulate_error (parser
))
2920 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2923 /* Issue an error about name-lookup failing. NAME is the
2924 IDENTIFIER_NODE DECL is the result of
2925 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2926 the thing that we hoped to find. */
2929 cp_parser_name_lookup_error (cp_parser
* parser
,
2932 name_lookup_error desired
,
2933 location_t location
)
2935 /* If name lookup completely failed, tell the user that NAME was not
2937 if (decl
== error_mark_node
)
2939 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2940 error_at (location
, "%<%E::%E%> has not been declared",
2941 parser
->scope
, name
);
2942 else if (parser
->scope
== global_namespace
)
2943 error_at (location
, "%<::%E%> has not been declared", name
);
2944 else if (parser
->object_scope
2945 && !CLASS_TYPE_P (parser
->object_scope
))
2946 error_at (location
, "request for member %qE in non-class type %qT",
2947 name
, parser
->object_scope
);
2948 else if (parser
->object_scope
)
2949 error_at (location
, "%<%T::%E%> has not been declared",
2950 parser
->object_scope
, name
);
2952 error_at (location
, "%qE has not been declared", name
);
2954 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2959 error_at (location
, "%<%E::%E%> is not a type",
2960 parser
->scope
, name
);
2963 error_at (location
, "%<%E::%E%> is not a class or namespace",
2964 parser
->scope
, name
);
2968 "%<%E::%E%> is not a class, namespace, or enumeration",
2969 parser
->scope
, name
);
2976 else if (parser
->scope
== global_namespace
)
2981 error_at (location
, "%<::%E%> is not a type", name
);
2984 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2988 "%<::%E%> is not a class, namespace, or enumeration",
3000 error_at (location
, "%qE is not a type", name
);
3003 error_at (location
, "%qE is not a class or namespace", name
);
3007 "%qE is not a class, namespace, or enumeration", name
);
3015 /* If we are parsing tentatively, remember that an error has occurred
3016 during this tentative parse. Returns true if the error was
3017 simulated; false if a message should be issued by the caller. */
3020 cp_parser_simulate_error (cp_parser
* parser
)
3022 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3024 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3030 /* This function is called when a type is defined. If type
3031 definitions are forbidden at this point, an error message is
3035 cp_parser_check_type_definition (cp_parser
* parser
)
3037 /* If types are forbidden here, issue a message. */
3038 if (parser
->type_definition_forbidden_message
)
3040 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3041 in the message need to be interpreted. */
3042 error (parser
->type_definition_forbidden_message
);
3048 /* This function is called when the DECLARATOR is processed. The TYPE
3049 was a type defined in the decl-specifiers. If it is invalid to
3050 define a type in the decl-specifiers for DECLARATOR, an error is
3051 issued. TYPE_LOCATION is the location of TYPE and is used
3052 for error reporting. */
3055 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3056 tree type
, location_t type_location
)
3058 /* [dcl.fct] forbids type definitions in return types.
3059 Unfortunately, it's not easy to know whether or not we are
3060 processing a return type until after the fact. */
3062 && (declarator
->kind
== cdk_pointer
3063 || declarator
->kind
== cdk_reference
3064 || declarator
->kind
== cdk_ptrmem
))
3065 declarator
= declarator
->declarator
;
3067 && declarator
->kind
== cdk_function
)
3069 error_at (type_location
,
3070 "new types may not be defined in a return type");
3071 inform (type_location
,
3072 "(perhaps a semicolon is missing after the definition of %qT)",
3077 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3078 "<" in any valid C++ program. If the next token is indeed "<",
3079 issue a message warning the user about what appears to be an
3080 invalid attempt to form a template-id. LOCATION is the location
3081 of the type-specifier (TYPE) */
3084 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3086 enum tag_types tag_type
,
3087 location_t location
)
3089 cp_token_position start
= 0;
3091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3093 if (TREE_CODE (type
) == TYPE_DECL
)
3094 type
= TREE_TYPE (type
);
3095 if (TYPE_P (type
) && !template_placeholder_p (type
))
3096 error_at (location
, "%qT is not a template", type
);
3097 else if (identifier_p (type
))
3099 if (tag_type
!= none_type
)
3100 error_at (location
, "%qE is not a class template", type
);
3102 error_at (location
, "%qE is not a template", type
);
3105 error_at (location
, "invalid template-id");
3106 /* Remember the location of the invalid "<". */
3107 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3108 start
= cp_lexer_token_position (parser
->lexer
, true);
3109 /* Consume the "<". */
3110 cp_lexer_consume_token (parser
->lexer
);
3111 /* Parse the template arguments. */
3112 cp_parser_enclosed_template_argument_list (parser
);
3113 /* Permanently remove the invalid template arguments so that
3114 this error message is not issued again. */
3116 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3120 /* If parsing an integral constant-expression, issue an error message
3121 about the fact that THING appeared and return true. Otherwise,
3122 return false. In either case, set
3123 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3126 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3127 non_integral_constant thing
)
3129 parser
->non_integral_constant_expression_p
= true;
3130 if (parser
->integral_constant_expression_p
)
3132 if (!parser
->allow_non_integral_constant_expression_p
)
3134 const char *msg
= NULL
;
3138 pedwarn (input_location
, OPT_Wpedantic
,
3139 "ISO C++ forbids using a floating-point literal "
3140 "in a constant-expression");
3143 error ("a cast to a type other than an integral or "
3144 "enumeration type cannot appear in a "
3145 "constant-expression");
3148 error ("%<typeid%> operator "
3149 "cannot appear in a constant-expression");
3152 error ("non-constant compound literals "
3153 "cannot appear in a constant-expression");
3156 error ("a function call "
3157 "cannot appear in a constant-expression");
3160 error ("an increment "
3161 "cannot appear in a constant-expression");
3164 error ("an decrement "
3165 "cannot appear in a constant-expression");
3168 error ("an array reference "
3169 "cannot appear in a constant-expression");
3171 case NIC_ADDR_LABEL
:
3172 error ("the address of a label "
3173 "cannot appear in a constant-expression");
3175 case NIC_OVERLOADED
:
3176 error ("calls to overloaded operators "
3177 "cannot appear in a constant-expression");
3179 case NIC_ASSIGNMENT
:
3180 error ("an assignment cannot appear in a constant-expression");
3183 error ("a comma operator "
3184 "cannot appear in a constant-expression");
3186 case NIC_CONSTRUCTOR
:
3187 error ("a call to a constructor "
3188 "cannot appear in a constant-expression");
3190 case NIC_TRANSACTION
:
3191 error ("a transaction expression "
3192 "cannot appear in a constant-expression");
3198 msg
= "__FUNCTION__";
3200 case NIC_PRETTY_FUNC
:
3201 msg
= "__PRETTY_FUNCTION__";
3221 case NIC_PREINCREMENT
:
3224 case NIC_PREDECREMENT
:
3237 error ("%qs cannot appear in a constant-expression", msg
);
3244 /* Emit a diagnostic for an invalid type name. This function commits
3245 to the current active tentative parse, if any. (Otherwise, the
3246 problematic construct might be encountered again later, resulting
3247 in duplicate error messages.) LOCATION is the location of ID. */
3250 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3251 location_t location
)
3253 tree decl
, ambiguous_decls
;
3254 cp_parser_commit_to_tentative_parse (parser
);
3255 /* Try to lookup the identifier. */
3256 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3257 /*is_template=*/false,
3258 /*is_namespace=*/false,
3259 /*check_dependency=*/true,
3260 &ambiguous_decls
, location
);
3261 if (ambiguous_decls
)
3262 /* If the lookup was ambiguous, an error will already have
3265 /* If the lookup found a template-name, it means that the user forgot
3266 to specify an argument list. Emit a useful error message. */
3267 if (DECL_TYPE_TEMPLATE_P (decl
))
3270 "invalid use of template-name %qE without an argument list",
3272 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3273 inform (location
, "class template argument deduction is only available "
3274 "with -std=c++17 or -std=gnu++17");
3275 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3277 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3278 error_at (location
, "invalid use of destructor %qD as a type", id
);
3279 else if (TREE_CODE (decl
) == TYPE_DECL
)
3280 /* Something like 'unsigned A a;' */
3281 error_at (location
, "invalid combination of multiple type-specifiers");
3282 else if (!parser
->scope
)
3284 /* Issue an error message. */
3286 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3287 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3290 gcc_rich_location
richloc (location
);
3291 richloc
.add_fixit_replace (hint
.suggestion ());
3293 "%qE does not name a type; did you mean %qs?",
3294 id
, hint
.suggestion ());
3297 error_at (location
, "%qE does not name a type", id
);
3298 /* If we're in a template class, it's possible that the user was
3299 referring to a type from a base class. For example:
3301 template <typename T> struct A { typedef T X; };
3302 template <typename T> struct B : public A<T> { X x; };
3304 The user should have said "typename A<T>::X". */
3305 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3306 inform (location
, "C++11 %<constexpr%> only available with "
3307 "-std=c++11 or -std=gnu++11");
3308 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3309 inform (location
, "C++11 %<noexcept%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (cxx_dialect
< cxx11
3312 && TREE_CODE (id
) == IDENTIFIER_NODE
3313 && id_equal (id
, "thread_local"))
3314 inform (location
, "C++11 %<thread_local%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3317 inform (location
, "%<concept%> only available with -fconcepts");
3318 else if (processing_template_decl
&& current_class_type
3319 && TYPE_BINFO (current_class_type
))
3323 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3327 tree base_type
= BINFO_TYPE (b
);
3328 if (CLASS_TYPE_P (base_type
)
3329 && dependent_type_p (base_type
))
3332 /* Go from a particular instantiation of the
3333 template (which will have an empty TYPE_FIELDs),
3334 to the main version. */
3335 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3336 for (field
= TYPE_FIELDS (base_type
);
3338 field
= DECL_CHAIN (field
))
3339 if (TREE_CODE (field
) == TYPE_DECL
3340 && DECL_NAME (field
) == id
)
3343 "(perhaps %<typename %T::%E%> was intended)",
3344 BINFO_TYPE (b
), id
);
3353 /* Here we diagnose qualified-ids where the scope is actually correct,
3354 but the identifier does not resolve to a valid type name. */
3355 else if (parser
->scope
!= error_mark_node
)
3357 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3360 error_at (location_of (id
),
3361 "%qE in namespace %qE does not name a template type",
3363 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3364 error_at (location_of (id
),
3365 "%qE in namespace %qE does not name a template type",
3366 TREE_OPERAND (id
, 0), parser
->scope
);
3368 error_at (location_of (id
),
3369 "%qE in namespace %qE does not name a type",
3372 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3373 else if (decl
== error_mark_node
)
3374 suggest_alternative_in_explicit_scope (location
, id
,
3377 else if (CLASS_TYPE_P (parser
->scope
)
3378 && constructor_name_p (id
, parser
->scope
))
3381 error_at (location
, "%<%T::%E%> names the constructor, not"
3382 " the type", parser
->scope
, id
);
3383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3384 error_at (location
, "and %qT has no template constructors",
3387 else if (TYPE_P (parser
->scope
)
3388 && dependent_scope_p (parser
->scope
))
3390 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3392 "need %<typename%> before %<%T::%D::%E%> because "
3393 "%<%T::%D%> is a dependent scope",
3394 TYPE_CONTEXT (parser
->scope
),
3395 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3397 TYPE_CONTEXT (parser
->scope
),
3398 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3400 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3401 "%qT is a dependent scope",
3402 parser
->scope
, id
, parser
->scope
);
3404 else if (TYPE_P (parser
->scope
))
3406 if (!COMPLETE_TYPE_P (parser
->scope
))
3407 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3409 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3410 error_at (location_of (id
),
3411 "%qE in %q#T does not name a template type",
3413 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3414 error_at (location_of (id
),
3415 "%qE in %q#T does not name a template type",
3416 TREE_OPERAND (id
, 0), parser
->scope
);
3418 error_at (location_of (id
),
3419 "%qE in %q#T does not name a type",
3422 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3429 /* Check for a common situation where a type-name should be present,
3430 but is not, and issue a sensible error message. Returns true if an
3431 invalid type-name was detected.
3433 The situation handled by this function are variable declarations of the
3434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3435 Usually, `ID' should name a type, but if we got here it means that it
3436 does not. We try to emit the best possible error message depending on
3437 how exactly the id-expression looks like. */
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3443 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3445 /* Avoid duplicate error about ambiguous lookup. */
3446 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3448 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3449 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3453 cp_parser_parse_tentatively (parser
);
3454 id
= cp_parser_id_expression (parser
,
3455 /*template_keyword_p=*/false,
3456 /*check_dependency_p=*/true,
3457 /*template_p=*/NULL
,
3458 /*declarator_p=*/false,
3459 /*optional_p=*/false);
3460 /* If the next token is a (, this is a function with no explicit return
3461 type, i.e. constructor, destructor or conversion op. */
3462 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3463 || TREE_CODE (id
) == TYPE_DECL
)
3465 cp_parser_abort_tentative_parse (parser
);
3468 if (!cp_parser_parse_definitely (parser
))
3471 /* Emit a diagnostic for the invalid type. */
3472 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3474 /* If we aren't in the middle of a declarator (i.e. in a
3475 parameter-declaration-clause), skip to the end of the declaration;
3476 there's no point in trying to process it. */
3477 if (!parser
->in_declarator_p
)
3478 cp_parser_skip_to_end_of_block_or_statement (parser
);
3482 /* Consume tokens up to, and including, the next non-nested closing `)'.
3483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3484 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3485 found an unnested token of that type. */
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3493 unsigned paren_depth
= 0;
3494 unsigned brace_depth
= 0;
3495 unsigned square_depth
= 0;
3497 if (recovering
&& or_ttype
== CPP_EOF
3498 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3503 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3505 /* Have we found what we're looking for before the closing paren? */
3506 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3507 && !brace_depth
&& !paren_depth
&& !square_depth
)
3510 switch (token
->type
)
3513 case CPP_PRAGMA_EOL
:
3514 /* If we've run out of tokens, then there is no closing `)'. */
3517 /* This is good for lambda expression capture-lists. */
3518 case CPP_OPEN_SQUARE
:
3521 case CPP_CLOSE_SQUARE
:
3522 if (!square_depth
--)
3527 /* This matches the processing in skip_to_end_of_statement. */
3532 case CPP_OPEN_BRACE
:
3535 case CPP_CLOSE_BRACE
:
3540 case CPP_OPEN_PAREN
:
3545 case CPP_CLOSE_PAREN
:
3546 if (!brace_depth
&& !paren_depth
--)
3549 cp_lexer_consume_token (parser
->lexer
);
3558 /* Consume the token. */
3559 cp_lexer_consume_token (parser
->lexer
);
3563 /* Consume tokens up to, and including, the next non-nested closing `)'.
3564 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3565 are doing error recovery. Returns -1 if OR_COMMA is true and we
3566 found an unnested token of that type. */
3569 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3574 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3575 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3576 ttype
, consume_paren
);
3579 /* Consume tokens until we reach the end of the current statement.
3580 Normally, that will be just before consuming a `;'. However, if a
3581 non-nested `}' comes first, then we stop before consuming that. */
3584 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3586 unsigned nesting_depth
= 0;
3588 /* Unwind generic function template scope if necessary. */
3589 if (parser
->fully_implicit_function_template_p
)
3590 abort_fully_implicit_template (parser
);
3594 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3596 switch (token
->type
)
3599 case CPP_PRAGMA_EOL
:
3600 /* If we've run out of tokens, stop. */
3604 /* If the next token is a `;', we have reached the end of the
3610 case CPP_CLOSE_BRACE
:
3611 /* If this is a non-nested '}', stop before consuming it.
3612 That way, when confronted with something like:
3616 we stop before consuming the closing '}', even though we
3617 have not yet reached a `;'. */
3618 if (nesting_depth
== 0)
3621 /* If it is the closing '}' for a block that we have
3622 scanned, stop -- but only after consuming the token.
3628 we will stop after the body of the erroneously declared
3629 function, but before consuming the following `typedef'
3631 if (--nesting_depth
== 0)
3633 cp_lexer_consume_token (parser
->lexer
);
3638 case CPP_OPEN_BRACE
:
3646 /* Consume the token. */
3647 cp_lexer_consume_token (parser
->lexer
);
3651 /* This function is called at the end of a statement or declaration.
3652 If the next token is a semicolon, it is consumed; otherwise, error
3653 recovery is attempted. */
3656 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3658 /* Look for the trailing `;'. */
3659 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3661 /* If there is additional (erroneous) input, skip to the end of
3663 cp_parser_skip_to_end_of_statement (parser
);
3664 /* If the next token is now a `;', consume it. */
3665 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3666 cp_lexer_consume_token (parser
->lexer
);
3670 /* Skip tokens until we have consumed an entire block, or until we
3671 have consumed a non-nested `;'. */
3674 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3676 int nesting_depth
= 0;
3678 /* Unwind generic function template scope if necessary. */
3679 if (parser
->fully_implicit_function_template_p
)
3680 abort_fully_implicit_template (parser
);
3682 while (nesting_depth
>= 0)
3684 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3686 switch (token
->type
)
3689 case CPP_PRAGMA_EOL
:
3690 /* If we've run out of tokens, stop. */
3694 /* Stop if this is an unnested ';'. */
3699 case CPP_CLOSE_BRACE
:
3700 /* Stop if this is an unnested '}', or closes the outermost
3703 if (nesting_depth
< 0)
3709 case CPP_OPEN_BRACE
:
3718 /* Consume the token. */
3719 cp_lexer_consume_token (parser
->lexer
);
3723 /* Skip tokens until a non-nested closing curly brace is the next
3724 token, or there are no more tokens. Return true in the first case,
3728 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3730 unsigned nesting_depth
= 0;
3734 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3736 switch (token
->type
)
3739 case CPP_PRAGMA_EOL
:
3740 /* If we've run out of tokens, stop. */
3743 case CPP_CLOSE_BRACE
:
3744 /* If the next token is a non-nested `}', then we have reached
3745 the end of the current block. */
3746 if (nesting_depth
-- == 0)
3750 case CPP_OPEN_BRACE
:
3751 /* If it the next token is a `{', then we are entering a new
3752 block. Consume the entire block. */
3760 /* Consume the token. */
3761 cp_lexer_consume_token (parser
->lexer
);
3765 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3766 parameter is the PRAGMA token, allowing us to purge the entire pragma
3770 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3774 parser
->lexer
->in_pragma
= false;
3777 token
= cp_lexer_consume_token (parser
->lexer
);
3778 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3780 /* Ensure that the pragma is not parsed again. */
3781 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3784 /* Require pragma end of line, resyncing with it as necessary. The
3785 arguments are as for cp_parser_skip_to_pragma_eol. */
3788 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3790 parser
->lexer
->in_pragma
= false;
3791 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3792 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3795 /* This is a simple wrapper around make_typename_type. When the id is
3796 an unresolved identifier node, we can provide a superior diagnostic
3797 using cp_parser_diagnose_invalid_type_name. */
3800 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3801 location_t id_location
)
3804 if (identifier_p (id
))
3806 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3807 /*complain=*/tf_none
);
3808 if (result
== error_mark_node
)
3809 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3812 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3815 /* This is a wrapper around the
3816 make_{pointer,ptrmem,reference}_declarator functions that decides
3817 which one to call based on the CODE and CLASS_TYPE arguments. The
3818 CODE argument should be one of the values returned by
3819 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3820 appertain to the pointer or reference. */
3822 static cp_declarator
*
3823 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3824 cp_cv_quals cv_qualifiers
,
3825 cp_declarator
*target
,
3828 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3829 return cp_error_declarator
;
3831 if (code
== INDIRECT_REF
)
3832 if (class_type
== NULL_TREE
)
3833 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3835 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3836 target
, attributes
);
3837 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3838 return make_reference_declarator (cv_qualifiers
, target
,
3840 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3841 return make_reference_declarator (cv_qualifiers
, target
,
3846 /* Create a new C++ parser. */
3849 cp_parser_new (void)
3855 /* cp_lexer_new_main is called before doing GC allocation because
3856 cp_lexer_new_main might load a PCH file. */
3857 lexer
= cp_lexer_new_main ();
3859 /* Initialize the binops_by_token so that we can get the tree
3860 directly from the token. */
3861 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3862 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3864 parser
= ggc_cleared_alloc
<cp_parser
> ();
3865 parser
->lexer
= lexer
;
3866 parser
->context
= cp_parser_context_new (NULL
);
3868 /* For now, we always accept GNU extensions. */
3869 parser
->allow_gnu_extensions_p
= 1;
3871 /* The `>' token is a greater-than operator, not the end of a
3873 parser
->greater_than_is_operator_p
= true;
3875 parser
->default_arg_ok_p
= true;
3877 /* We are not parsing a constant-expression. */
3878 parser
->integral_constant_expression_p
= false;
3879 parser
->allow_non_integral_constant_expression_p
= false;
3880 parser
->non_integral_constant_expression_p
= false;
3882 /* Local variable names are not forbidden. */
3883 parser
->local_variables_forbidden_p
= false;
3885 /* We are not processing an `extern "C"' declaration. */
3886 parser
->in_unbraced_linkage_specification_p
= false;
3888 /* We are not processing a declarator. */
3889 parser
->in_declarator_p
= false;
3891 /* We are not processing a template-argument-list. */
3892 parser
->in_template_argument_list_p
= false;
3894 /* We are not in an iteration statement. */
3895 parser
->in_statement
= 0;
3897 /* We are not in a switch statement. */
3898 parser
->in_switch_statement_p
= false;
3900 /* We are not parsing a type-id inside an expression. */
3901 parser
->in_type_id_in_expr_p
= false;
3903 /* Declarations aren't implicitly extern "C". */
3904 parser
->implicit_extern_c
= false;
3906 /* String literals should be translated to the execution character set. */
3907 parser
->translate_strings_p
= true;
3909 /* We are not parsing a function body. */
3910 parser
->in_function_body
= false;
3912 /* We can correct until told otherwise. */
3913 parser
->colon_corrects_to_scope_p
= true;
3915 /* The unparsed function queue is empty. */
3916 push_unparsed_function_queues (parser
);
3918 /* There are no classes being defined. */
3919 parser
->num_classes_being_defined
= 0;
3921 /* No template parameters apply. */
3922 parser
->num_template_parameter_lists
= 0;
3924 /* Special parsing data structures. */
3925 parser
->omp_declare_simd
= NULL
;
3926 parser
->oacc_routine
= NULL
;
3928 /* Not declaring an implicit function template. */
3929 parser
->auto_is_implicit_function_template_parm_p
= false;
3930 parser
->fully_implicit_function_template_p
= false;
3931 parser
->implicit_template_parms
= 0;
3932 parser
->implicit_template_scope
= 0;
3934 /* Allow constrained-type-specifiers. */
3935 parser
->prevent_constrained_type_specifiers
= 0;
3937 /* We haven't yet seen an 'extern "C"'. */
3938 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3943 /* Create a cp_lexer structure which will emit the tokens in CACHE
3944 and push it onto the parser's lexer stack. This is used for delayed
3945 parsing of in-class method bodies and default arguments, and should
3946 not be confused with tentative parsing. */
3948 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3950 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3951 lexer
->next
= parser
->lexer
;
3952 parser
->lexer
= lexer
;
3954 /* Move the current source position to that of the first token in the
3956 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3959 /* Pop the top lexer off the parser stack. This is never used for the
3960 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3962 cp_parser_pop_lexer (cp_parser
*parser
)
3964 cp_lexer
*lexer
= parser
->lexer
;
3965 parser
->lexer
= lexer
->next
;
3966 cp_lexer_destroy (lexer
);
3968 /* Put the current source position back where it was before this
3969 lexer was pushed. */
3970 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3973 /* Lexical conventions [gram.lex] */
3975 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3979 cp_parser_identifier (cp_parser
* parser
)
3983 /* Look for the identifier. */
3984 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3985 /* Return the value. */
3987 return cp_expr (token
->u
.value
, token
->location
);
3989 return error_mark_node
;
3992 /* Parse a sequence of adjacent string constants. Returns a
3993 TREE_STRING representing the combined, nul-terminated string
3994 constant. If TRANSLATE is true, translate the string to the
3995 execution character set. If WIDE_OK is true, a wide string is
3998 C++98 [lex.string] says that if a narrow string literal token is
3999 adjacent to a wide string literal token, the behavior is undefined.
4000 However, C99 6.4.5p4 says that this results in a wide string literal.
4001 We follow C99 here, for consistency with the C front end.
4003 This code is largely lifted from lex_string() in c-lex.c.
4005 FUTURE: ObjC++ will need to handle @-strings here. */
4007 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4008 bool lookup_udlit
= true)
4012 struct obstack str_ob
;
4013 cpp_string str
, istr
, *strs
;
4015 enum cpp_ttype type
, curr_type
;
4016 int have_suffix_p
= 0;
4018 tree suffix_id
= NULL_TREE
;
4019 bool curr_tok_is_userdef_p
= false;
4021 tok
= cp_lexer_peek_token (parser
->lexer
);
4022 if (!cp_parser_is_string_literal (tok
))
4024 cp_parser_error (parser
, "expected string-literal");
4025 return error_mark_node
;
4028 location_t loc
= tok
->location
;
4030 if (cpp_userdef_string_p (tok
->type
))
4032 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4033 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4034 curr_tok_is_userdef_p
= true;
4038 string_tree
= tok
->u
.value
;
4039 curr_type
= tok
->type
;
4043 /* Try to avoid the overhead of creating and destroying an obstack
4044 for the common case of just one string. */
4045 if (!cp_parser_is_string_literal
4046 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4048 cp_lexer_consume_token (parser
->lexer
);
4050 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4051 str
.len
= TREE_STRING_LENGTH (string_tree
);
4054 if (curr_tok_is_userdef_p
)
4056 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4058 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4061 curr_type
= tok
->type
;
4067 location_t last_tok_loc
= tok
->location
;
4068 gcc_obstack_init (&str_ob
);
4073 cp_lexer_consume_token (parser
->lexer
);
4075 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4076 str
.len
= TREE_STRING_LENGTH (string_tree
);
4078 if (curr_tok_is_userdef_p
)
4080 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4081 if (have_suffix_p
== 0)
4083 suffix_id
= curr_suffix_id
;
4086 else if (have_suffix_p
== 1
4087 && curr_suffix_id
!= suffix_id
)
4089 error ("inconsistent user-defined literal suffixes"
4090 " %qD and %qD in string literal",
4091 suffix_id
, curr_suffix_id
);
4094 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4097 curr_type
= tok
->type
;
4099 if (type
!= curr_type
)
4101 if (type
== CPP_STRING
)
4103 else if (curr_type
!= CPP_STRING
)
4105 rich_location
rich_loc (line_table
, tok
->location
);
4106 rich_loc
.add_range (last_tok_loc
, false);
4107 error_at (&rich_loc
,
4108 "unsupported non-standard concatenation "
4109 "of string literals");
4113 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4115 last_tok_loc
= tok
->location
;
4117 tok
= cp_lexer_peek_token (parser
->lexer
);
4118 if (cpp_userdef_string_p (tok
->type
))
4120 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4121 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4122 curr_tok_is_userdef_p
= true;
4126 string_tree
= tok
->u
.value
;
4127 curr_type
= tok
->type
;
4128 curr_tok_is_userdef_p
= false;
4131 while (cp_parser_is_string_literal (tok
));
4133 /* A string literal built by concatenation has its caret=start at
4134 the start of the initial string, and its finish at the finish of
4135 the final string literal. */
4136 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4138 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4141 if (type
!= CPP_STRING
&& !wide_ok
)
4143 cp_parser_error (parser
, "a wide string is invalid in this context");
4147 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4148 (parse_in
, strs
, count
, &istr
, type
))
4150 value
= build_string (istr
.len
, (const char *)istr
.text
);
4151 free (CONST_CAST (unsigned char *, istr
.text
));
4157 case CPP_UTF8STRING
:
4158 TREE_TYPE (value
) = char_array_type_node
;
4161 TREE_TYPE (value
) = char16_array_type_node
;
4164 TREE_TYPE (value
) = char32_array_type_node
;
4167 TREE_TYPE (value
) = wchar_array_type_node
;
4171 value
= fix_string_type (value
);
4175 tree literal
= build_userdef_literal (suffix_id
, value
,
4176 OT_NONE
, NULL_TREE
);
4178 value
= cp_parser_userdef_string_literal (literal
);
4184 /* cpp_interpret_string has issued an error. */
4185 value
= error_mark_node
;
4188 obstack_free (&str_ob
, 0);
4190 return cp_expr (value
, loc
);
4193 /* Look up a literal operator with the name and the exact arguments. */
4196 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4199 decl
= lookup_name (name
);
4200 if (!decl
|| !is_overloaded_fn (decl
))
4201 return error_mark_node
;
4203 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4208 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4209 if (parmtypes
!= NULL_TREE
)
4211 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4212 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4214 tree tparm
= TREE_VALUE (parmtypes
);
4215 tree targ
= TREE_TYPE ((*args
)[ix
]);
4216 bool ptr
= TYPE_PTR_P (tparm
);
4217 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4218 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4220 || !same_type_p (TREE_TYPE (tparm
),
4225 && ix
== vec_safe_length (args
)
4226 /* May be this should be sufficient_parms_p instead,
4227 depending on how exactly should user-defined literals
4228 work in presence of default arguments on the literal
4229 operator parameters. */
4230 && parmtypes
== void_list_node
)
4235 return error_mark_node
;
4238 /* Parse a user-defined char constant. Returns a call to a user-defined
4239 literal operator taking the character as an argument. */
4242 cp_parser_userdef_char_literal (cp_parser
*parser
)
4244 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4245 tree literal
= token
->u
.value
;
4246 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4247 tree value
= USERDEF_LITERAL_VALUE (literal
);
4248 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4251 /* Build up a call to the user-defined operator */
4252 /* Lookup the name we got back from the id-expression. */
4253 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4254 vec_safe_push (args
, value
);
4255 decl
= lookup_literal_operator (name
, args
);
4256 if (!decl
|| decl
== error_mark_node
)
4258 error ("unable to find character literal operator %qD with %qT argument",
4259 name
, TREE_TYPE (value
));
4260 release_tree_vector (args
);
4261 return error_mark_node
;
4263 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4264 release_tree_vector (args
);
4268 /* A subroutine of cp_parser_userdef_numeric_literal to
4269 create a char... template parameter pack from a string node. */
4272 make_char_string_pack (tree value
)
4275 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4276 const char *str
= TREE_STRING_POINTER (value
);
4277 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4278 tree argvec
= make_tree_vec (1);
4280 /* Fill in CHARVEC with all of the parameters. */
4281 charvec
= make_tree_vec (len
);
4282 for (i
= 0; i
< len
; ++i
)
4283 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4285 /* Build the argument packs. */
4286 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4288 TREE_VEC_ELT (argvec
, 0) = argpack
;
4293 /* A subroutine of cp_parser_userdef_numeric_literal to
4294 create a char... template parameter pack from a string node. */
4297 make_string_pack (tree value
)
4300 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4301 const unsigned char *str
4302 = (const unsigned char *) TREE_STRING_POINTER (value
);
4303 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4304 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4305 tree argvec
= make_tree_vec (2);
4307 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4308 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4310 /* First template parm is character type. */
4311 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4313 /* Fill in CHARVEC with all of the parameters. */
4314 charvec
= make_tree_vec (len
);
4315 for (int i
= 0; i
< len
; ++i
)
4316 TREE_VEC_ELT (charvec
, i
)
4317 = double_int_to_tree (str_char_type_node
,
4318 double_int::from_buffer (str
+ i
* sz
, sz
));
4320 /* Build the argument packs. */
4321 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4323 TREE_VEC_ELT (argvec
, 1) = argpack
;
4328 /* Parse a user-defined numeric constant. returns a call to a user-defined
4329 literal operator. */
4332 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4334 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4335 tree literal
= token
->u
.value
;
4336 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4337 tree value
= USERDEF_LITERAL_VALUE (literal
);
4338 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4339 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4340 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4342 vec
<tree
, va_gc
> *args
;
4344 /* Look for a literal operator taking the exact type of numeric argument
4345 as the literal value. */
4346 args
= make_tree_vector ();
4347 vec_safe_push (args
, value
);
4348 decl
= lookup_literal_operator (name
, args
);
4349 if (decl
&& decl
!= error_mark_node
)
4351 result
= finish_call_expr (decl
, &args
, false, true,
4352 tf_warning_or_error
);
4354 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4356 warning_at (token
->location
, OPT_Woverflow
,
4357 "integer literal exceeds range of %qT type",
4358 long_long_unsigned_type_node
);
4363 warning_at (token
->location
, OPT_Woverflow
,
4364 "floating literal exceeds range of %qT type",
4365 long_double_type_node
);
4366 else if (overflow
< 0)
4367 warning_at (token
->location
, OPT_Woverflow
,
4368 "floating literal truncated to zero");
4371 release_tree_vector (args
);
4374 release_tree_vector (args
);
4376 /* If the numeric argument didn't work, look for a raw literal
4377 operator taking a const char* argument consisting of the number
4378 in string format. */
4379 args
= make_tree_vector ();
4380 vec_safe_push (args
, num_string
);
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
);
4386 release_tree_vector (args
);
4389 release_tree_vector (args
);
4391 /* If the raw literal didn't work, look for a non-type template
4392 function with parameter pack char.... Call the function with
4393 template parameter characters representing the number. */
4394 args
= make_tree_vector ();
4395 decl
= lookup_literal_operator (name
, args
);
4396 if (decl
&& decl
!= error_mark_node
)
4398 tree tmpl_args
= make_char_string_pack (num_string
);
4399 decl
= lookup_template_function (decl
, tmpl_args
);
4400 result
= finish_call_expr (decl
, &args
, false, true,
4401 tf_warning_or_error
);
4402 release_tree_vector (args
);
4406 release_tree_vector (args
);
4408 /* In C++14 the standard library defines complex number suffixes that
4409 conflict with GNU extensions. Prefer them if <complex> is #included. */
4410 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4411 bool i14
= (cxx_dialect
> cxx11
4412 && (id_equal (suffix_id
, "i")
4413 || id_equal (suffix_id
, "if")
4414 || id_equal (suffix_id
, "il")));
4415 diagnostic_t kind
= DK_ERROR
;
4420 tree cxlit
= lookup_qualified_name (std_node
,
4421 get_identifier ("complex_literals"),
4423 if (cxlit
== error_mark_node
)
4425 /* No <complex>, so pedwarn and use GNU semantics. */
4427 opt
= OPT_Wpedantic
;
4432 = emit_diagnostic (kind
, input_location
, opt
,
4433 "unable to find numeric literal operator %qD", name
);
4436 /* Don't inform either. */;
4439 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4440 "(from <complex>) to enable the C++14 user-defined literal "
4443 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4444 "GNU built-in suffix");
4447 inform (token
->location
, "use -fext-numeric-literals "
4448 "to enable more built-in suffixes");
4450 if (kind
== DK_ERROR
)
4451 value
= error_mark_node
;
4454 /* Use the built-in semantics. */
4456 if (id_equal (suffix_id
, "i"))
4458 if (TREE_CODE (value
) == INTEGER_CST
)
4459 type
= integer_type_node
;
4461 type
= double_type_node
;
4463 else if (id_equal (suffix_id
, "if"))
4464 type
= float_type_node
;
4465 else /* if (id_equal (suffix_id, "il")) */
4466 type
= long_double_type_node
;
4468 value
= build_complex (build_complex_type (type
),
4469 fold_convert (type
, integer_zero_node
),
4470 fold_convert (type
, value
));
4473 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4474 /* Avoid repeated diagnostics. */
4475 token
->u
.value
= value
;
4479 /* Parse a user-defined string constant. Returns a call to a user-defined
4480 literal operator taking a character pointer and the length of the string
4484 cp_parser_userdef_string_literal (tree literal
)
4486 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4487 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4488 tree value
= USERDEF_LITERAL_VALUE (literal
);
4489 int len
= TREE_STRING_LENGTH (value
)
4490 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4492 vec
<tree
, va_gc
> *args
;
4494 /* Build up a call to the user-defined operator. */
4495 /* Lookup the name we got back from the id-expression. */
4496 args
= make_tree_vector ();
4497 vec_safe_push (args
, value
);
4498 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4499 decl
= lookup_literal_operator (name
, args
);
4501 if (decl
&& decl
!= error_mark_node
)
4503 result
= finish_call_expr (decl
, &args
, false, true,
4504 tf_warning_or_error
);
4505 release_tree_vector (args
);
4508 release_tree_vector (args
);
4510 /* Look for a template function with typename parameter CharT
4511 and parameter pack CharT... Call the function with
4512 template parameter characters representing the string. */
4513 args
= make_tree_vector ();
4514 decl
= lookup_literal_operator (name
, args
);
4515 if (decl
&& decl
!= error_mark_node
)
4517 tree tmpl_args
= make_string_pack (value
);
4518 decl
= lookup_template_function (decl
, tmpl_args
);
4519 result
= finish_call_expr (decl
, &args
, false, true,
4520 tf_warning_or_error
);
4521 release_tree_vector (args
);
4524 release_tree_vector (args
);
4526 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4527 name
, TREE_TYPE (value
), size_type_node
);
4528 return error_mark_node
;
4532 /* Basic concepts [gram.basic] */
4534 /* Parse a translation-unit.
4537 declaration-seq [opt]
4539 Returns TRUE if all went well. */
4542 cp_parser_translation_unit (cp_parser
* parser
)
4544 /* The address of the first non-permanent object on the declarator
4546 static void *declarator_obstack_base
;
4550 /* Create the declarator obstack, if necessary. */
4551 if (!cp_error_declarator
)
4553 gcc_obstack_init (&declarator_obstack
);
4554 /* Create the error declarator. */
4555 cp_error_declarator
= make_declarator (cdk_error
);
4556 /* Create the empty parameter list. */
4557 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4559 /* Remember where the base of the declarator obstack lies. */
4560 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4563 cp_parser_declaration_seq_opt (parser
);
4565 /* If there are no tokens left then all went well. */
4566 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4568 /* Get rid of the token array; we don't need it any more. */
4569 cp_lexer_destroy (parser
->lexer
);
4570 parser
->lexer
= NULL
;
4572 /* This file might have been a context that's implicitly extern
4573 "C". If so, pop the lang context. (Only relevant for PCH.) */
4574 if (parser
->implicit_extern_c
)
4576 pop_lang_context ();
4577 parser
->implicit_extern_c
= false;
4581 finish_translation_unit ();
4587 cp_parser_error (parser
, "expected declaration");
4591 /* Make sure the declarator obstack was fully cleaned up. */
4592 gcc_assert (obstack_next_free (&declarator_obstack
)
4593 == declarator_obstack_base
);
4595 /* All went well. */
4599 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4600 decltype context. */
4602 static inline tsubst_flags_t
4603 complain_flags (bool decltype_p
)
4605 tsubst_flags_t complain
= tf_warning_or_error
;
4607 complain
|= tf_decltype
;
4611 /* We're about to parse a collection of statements. If we're currently
4612 parsing tentatively, set up a firewall so that any nested
4613 cp_parser_commit_to_tentative_parse won't affect the current context. */
4615 static cp_token_position
4616 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4618 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4621 cp_parser_parse_tentatively (parser
);
4622 cp_parser_commit_to_topmost_tentative_parse (parser
);
4623 return cp_lexer_token_position (parser
->lexer
, false);
4626 /* We've finished parsing the collection of statements. Wrap up the
4627 firewall and replace the relevant tokens with the parsed form. */
4630 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4636 /* Finish the firewall level. */
4637 cp_parser_parse_definitely (parser
);
4638 /* And remember the result of the parse for when we try again. */
4639 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4640 token
->type
= CPP_PREPARSED_EXPR
;
4641 token
->u
.value
= expr
;
4642 token
->keyword
= RID_MAX
;
4643 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4646 /* Like the above functions, but let the user modify the tokens. Used by
4647 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4648 later parses, so it makes sense to localize the effects of
4649 cp_parser_commit_to_tentative_parse. */
4651 struct tentative_firewall
4656 tentative_firewall (cp_parser
*p
): parser(p
)
4658 /* If we're currently parsing tentatively, start a committed level as a
4659 firewall and then an inner tentative parse. */
4660 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4662 cp_parser_parse_tentatively (parser
);
4663 cp_parser_commit_to_topmost_tentative_parse (parser
);
4664 cp_parser_parse_tentatively (parser
);
4668 ~tentative_firewall()
4672 /* Finish the inner tentative parse and the firewall, propagating any
4673 uncommitted error state to the outer tentative parse. */
4674 bool err
= cp_parser_error_occurred (parser
);
4675 cp_parser_parse_definitely (parser
);
4676 cp_parser_parse_definitely (parser
);
4678 cp_parser_simulate_error (parser
);
4683 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4684 This class is for tracking such a matching pair of symbols.
4685 In particular, it tracks the location of the first token,
4686 so that if the second token is missing, we can highlight the
4687 location of the first token when notifying the user about the
4690 template <typename traits_t
>
4694 /* token_pair's ctor. */
4695 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4697 /* If the next token is the opening symbol for this pair, consume it and
4699 Otherwise, issue an error and return false.
4700 In either case, record the location of the opening token. */
4702 bool require_open (cp_parser
*parser
)
4704 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4705 return cp_parser_require (parser
, traits_t::open_token_type
,
4706 traits_t::required_token_open
);
4709 /* Consume the next token from PARSER, recording its location as
4710 that of the opening token within the pair. */
4712 cp_token
* consume_open (cp_parser
*parser
)
4714 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4715 gcc_assert (tok
->type
== traits_t::open_token_type
);
4716 m_open_loc
= tok
->location
;
4720 /* If the next token is the closing symbol for this pair, consume it
4722 Otherwise, issue an error, highlighting the location of the
4723 corresponding opening token, and return NULL. */
4725 cp_token
*require_close (cp_parser
*parser
) const
4727 return cp_parser_require (parser
, traits_t::close_token_type
,
4728 traits_t::required_token_close
,
4733 location_t m_open_loc
;
4736 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4738 struct matching_paren_traits
4740 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4741 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4742 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4743 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4746 /* "matching_parens" is a token_pair<T> class for tracking matching
4747 pairs of parentheses. */
4749 typedef token_pair
<matching_paren_traits
> matching_parens
;
4751 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4753 struct matching_brace_traits
4755 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4756 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4757 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4758 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4761 /* "matching_braces" is a token_pair<T> class for tracking matching
4764 typedef token_pair
<matching_brace_traits
> matching_braces
;
4767 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4768 enclosing parentheses. */
4771 cp_parser_statement_expr (cp_parser
*parser
)
4773 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4775 /* Consume the '('. */
4776 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4777 matching_parens parens
;
4778 parens
.consume_open (parser
);
4779 /* Start the statement-expression. */
4780 tree expr
= begin_stmt_expr ();
4781 /* Parse the compound-statement. */
4782 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4784 expr
= finish_stmt_expr (expr
, false);
4785 /* Consume the ')'. */
4786 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4787 if (!parens
.require_close (parser
))
4788 cp_parser_skip_to_end_of_statement (parser
);
4790 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4791 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4792 return cp_expr (expr
, combined_loc
);
4795 /* Expressions [gram.expr] */
4797 /* Parse a fold-operator.
4800 - * / % ^ & | = < > << >>
4801 = -= *= /= %= ^= &= |= <<= >>=
4802 == != <= >= && || , .* ->*
4804 This returns the tree code corresponding to the matched operator
4805 as an int. When the current token matches a compound assignment
4806 opertor, the resulting tree code is the negative value of the
4807 non-assignment operator. */
4810 cp_parser_fold_operator (cp_token
*token
)
4812 switch (token
->type
)
4814 case CPP_PLUS
: return PLUS_EXPR
;
4815 case CPP_MINUS
: return MINUS_EXPR
;
4816 case CPP_MULT
: return MULT_EXPR
;
4817 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4818 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4819 case CPP_XOR
: return BIT_XOR_EXPR
;
4820 case CPP_AND
: return BIT_AND_EXPR
;
4821 case CPP_OR
: return BIT_IOR_EXPR
;
4822 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4823 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4825 case CPP_EQ
: return -NOP_EXPR
;
4826 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4827 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4828 case CPP_MULT_EQ
: return -MULT_EXPR
;
4829 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4830 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4831 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4832 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4833 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4834 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4835 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4837 case CPP_EQ_EQ
: return EQ_EXPR
;
4838 case CPP_NOT_EQ
: return NE_EXPR
;
4839 case CPP_LESS
: return LT_EXPR
;
4840 case CPP_GREATER
: return GT_EXPR
;
4841 case CPP_LESS_EQ
: return LE_EXPR
;
4842 case CPP_GREATER_EQ
: return GE_EXPR
;
4844 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4845 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4847 case CPP_COMMA
: return COMPOUND_EXPR
;
4849 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4850 case CPP_DEREF_STAR
: return MEMBER_REF
;
4852 default: return ERROR_MARK
;
4856 /* Returns true if CODE indicates a binary expression, which is not allowed in
4857 the LHS of a fold-expression. More codes will need to be added to use this
4858 function in other contexts. */
4861 is_binary_op (tree_code code
)
4866 case POINTER_PLUS_EXPR
:
4869 case TRUNC_DIV_EXPR
:
4870 case TRUNC_MOD_EXPR
:
4886 case TRUTH_ANDIF_EXPR
:
4887 case TRUTH_ORIF_EXPR
:
4900 /* If the next token is a suitable fold operator, consume it and return as
4901 the function above. */
4904 cp_parser_fold_operator (cp_parser
*parser
)
4906 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4907 int code
= cp_parser_fold_operator (token
);
4908 if (code
!= ERROR_MARK
)
4909 cp_lexer_consume_token (parser
->lexer
);
4913 /* Parse a fold-expression.
4916 ( ... folding-operator cast-expression)
4917 ( cast-expression folding-operator ... )
4918 ( cast-expression folding operator ... folding-operator cast-expression)
4920 Note that the '(' and ')' are matched in primary expression. */
4923 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4928 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4930 cp_lexer_consume_token (parser
->lexer
);
4931 int op
= cp_parser_fold_operator (parser
);
4932 if (op
== ERROR_MARK
)
4934 cp_parser_error (parser
, "expected binary operator");
4935 return error_mark_node
;
4938 tree expr
= cp_parser_cast_expression (parser
, false, false,
4940 if (expr
== error_mark_node
)
4941 return error_mark_node
;
4942 return finish_left_unary_fold_expr (expr
, op
);
4945 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4946 int op
= cp_parser_fold_operator (parser
);
4947 if (op
== ERROR_MARK
)
4949 cp_parser_error (parser
, "expected binary operator");
4950 return error_mark_node
;
4953 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4955 cp_parser_error (parser
, "expected ...");
4956 return error_mark_node
;
4958 cp_lexer_consume_token (parser
->lexer
);
4960 /* The operands of a fold-expression are cast-expressions, so binary or
4961 conditional expressions are not allowed. We check this here to avoid
4962 tentative parsing. */
4963 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4964 /* OK, the expression was parenthesized. */;
4965 else if (is_binary_op (TREE_CODE (expr1
)))
4966 error_at (location_of (expr1
),
4967 "binary expression in operand of fold-expression");
4968 else if (TREE_CODE (expr1
) == COND_EXPR
4969 || (REFERENCE_REF_P (expr1
)
4970 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
4971 error_at (location_of (expr1
),
4972 "conditional expression in operand of fold-expression");
4975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4976 return finish_right_unary_fold_expr (expr1
, op
);
4978 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4980 cp_parser_error (parser
, "mismatched operator in fold-expression");
4981 return error_mark_node
;
4983 cp_lexer_consume_token (parser
->lexer
);
4985 // Binary left or right fold.
4986 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4987 if (expr2
== error_mark_node
)
4988 return error_mark_node
;
4989 return finish_binary_fold_expr (expr1
, expr2
, op
);
4992 /* Parse a primary-expression.
4999 lambda-expression (C++11)
5004 ( compound-statement )
5005 __builtin_va_arg ( assignment-expression , type-id )
5006 __builtin_offsetof ( type-id , offsetof-expression )
5009 __has_nothrow_assign ( type-id )
5010 __has_nothrow_constructor ( type-id )
5011 __has_nothrow_copy ( type-id )
5012 __has_trivial_assign ( type-id )
5013 __has_trivial_constructor ( type-id )
5014 __has_trivial_copy ( type-id )
5015 __has_trivial_destructor ( type-id )
5016 __has_virtual_destructor ( type-id )
5017 __is_abstract ( type-id )
5018 __is_base_of ( type-id , type-id )
5019 __is_class ( type-id )
5020 __is_empty ( type-id )
5021 __is_enum ( type-id )
5022 __is_final ( type-id )
5023 __is_literal_type ( type-id )
5024 __is_pod ( type-id )
5025 __is_polymorphic ( type-id )
5026 __is_std_layout ( type-id )
5027 __is_trivial ( type-id )
5028 __is_union ( type-id )
5030 Objective-C++ Extension:
5038 ADDRESS_P is true iff this expression was immediately preceded by
5039 "&" and therefore might denote a pointer-to-member. CAST_P is true
5040 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5041 true iff this expression is a template argument.
5043 Returns a representation of the expression. Upon return, *IDK
5044 indicates what kind of id-expression (if any) was present. */
5047 cp_parser_primary_expression (cp_parser
*parser
,
5050 bool template_arg_p
,
5054 cp_token
*token
= NULL
;
5056 /* Assume the primary expression is not an id-expression. */
5057 *idk
= CP_ID_KIND_NONE
;
5059 /* Peek at the next token. */
5060 token
= cp_lexer_peek_token (parser
->lexer
);
5061 switch ((int) token
->type
)
5070 user-defined-literal */
5077 case CPP_PREPARSED_EXPR
:
5078 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5079 return cp_parser_userdef_numeric_literal (parser
);
5080 token
= cp_lexer_consume_token (parser
->lexer
);
5081 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5083 error_at (token
->location
,
5084 "fixed-point types not supported in C++");
5085 return error_mark_node
;
5087 /* Floating-point literals are only allowed in an integral
5088 constant expression if they are cast to an integral or
5089 enumeration type. */
5090 if (TREE_CODE (token
->u
.value
) == REAL_CST
5091 && parser
->integral_constant_expression_p
5094 /* CAST_P will be set even in invalid code like "int(2.7 +
5095 ...)". Therefore, we have to check that the next token
5096 is sure to end the cast. */
5099 cp_token
*next_token
;
5101 next_token
= cp_lexer_peek_token (parser
->lexer
);
5102 if (/* The comma at the end of an
5103 enumerator-definition. */
5104 next_token
->type
!= CPP_COMMA
5105 /* The curly brace at the end of an enum-specifier. */
5106 && next_token
->type
!= CPP_CLOSE_BRACE
5107 /* The end of a statement. */
5108 && next_token
->type
!= CPP_SEMICOLON
5109 /* The end of the cast-expression. */
5110 && next_token
->type
!= CPP_CLOSE_PAREN
5111 /* The end of an array bound. */
5112 && next_token
->type
!= CPP_CLOSE_SQUARE
5113 /* The closing ">" in a template-argument-list. */
5114 && (next_token
->type
!= CPP_GREATER
5115 || parser
->greater_than_is_operator_p
)
5116 /* C++0x only: A ">>" treated like two ">" tokens,
5117 in a template-argument-list. */
5118 && (next_token
->type
!= CPP_RSHIFT
5119 || (cxx_dialect
== cxx98
)
5120 || parser
->greater_than_is_operator_p
))
5124 /* If we are within a cast, then the constraint that the
5125 cast is to an integral or enumeration type will be
5126 checked at that point. If we are not within a cast, then
5127 this code is invalid. */
5129 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5131 return cp_expr (token
->u
.value
, token
->location
);
5133 case CPP_CHAR_USERDEF
:
5134 case CPP_CHAR16_USERDEF
:
5135 case CPP_CHAR32_USERDEF
:
5136 case CPP_WCHAR_USERDEF
:
5137 case CPP_UTF8CHAR_USERDEF
:
5138 return cp_parser_userdef_char_literal (parser
);
5144 case CPP_UTF8STRING
:
5145 case CPP_STRING_USERDEF
:
5146 case CPP_STRING16_USERDEF
:
5147 case CPP_STRING32_USERDEF
:
5148 case CPP_WSTRING_USERDEF
:
5149 case CPP_UTF8STRING_USERDEF
:
5150 /* ??? Should wide strings be allowed when parser->translate_strings_p
5151 is false (i.e. in attributes)? If not, we can kill the third
5152 argument to cp_parser_string_literal. */
5153 return cp_parser_string_literal (parser
,
5154 parser
->translate_strings_p
,
5157 case CPP_OPEN_PAREN
:
5158 /* If we see `( { ' then we are looking at the beginning of
5159 a GNU statement-expression. */
5160 if (cp_parser_allow_gnu_extensions_p (parser
)
5161 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5163 /* Statement-expressions are not allowed by the standard. */
5164 pedwarn (token
->location
, OPT_Wpedantic
,
5165 "ISO C++ forbids braced-groups within expressions");
5167 /* And they're not allowed outside of a function-body; you
5168 cannot, for example, write:
5170 int i = ({ int j = 3; j + 1; });
5172 at class or namespace scope. */
5173 if (!parser
->in_function_body
5174 || parser
->in_template_argument_list_p
)
5176 error_at (token
->location
,
5177 "statement-expressions are not allowed outside "
5178 "functions nor in template-argument lists");
5179 cp_parser_skip_to_end_of_block_or_statement (parser
);
5180 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5181 cp_lexer_consume_token (parser
->lexer
);
5182 return error_mark_node
;
5185 return cp_parser_statement_expr (parser
);
5187 /* Otherwise it's a normal parenthesized expression. */
5190 bool saved_greater_than_is_operator_p
;
5192 location_t open_paren_loc
= token
->location
;
5194 /* Consume the `('. */
5195 matching_parens parens
;
5196 parens
.consume_open (parser
);
5197 /* Within a parenthesized expression, a `>' token is always
5198 the greater-than operator. */
5199 saved_greater_than_is_operator_p
5200 = parser
->greater_than_is_operator_p
;
5201 parser
->greater_than_is_operator_p
= true;
5203 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5204 /* Left fold expression. */
5207 /* Parse the parenthesized expression. */
5208 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5210 token
= cp_lexer_peek_token (parser
->lexer
);
5211 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5213 expr
= cp_parser_fold_expression (parser
, expr
);
5214 if (expr
!= error_mark_node
5215 && cxx_dialect
< cxx17
5216 && !in_system_header_at (input_location
))
5217 pedwarn (input_location
, 0, "fold-expressions only available "
5218 "with -std=c++17 or -std=gnu++17");
5221 /* Let the front end know that this expression was
5222 enclosed in parentheses. This matters in case, for
5223 example, the expression is of the form `A::B', since
5224 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5226 expr
= finish_parenthesized_expr (expr
);
5228 /* DR 705: Wrapping an unqualified name in parentheses
5229 suppresses arg-dependent lookup. We want to pass back
5230 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5231 (c++/37862), but none of the others. */
5232 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5233 *idk
= CP_ID_KIND_NONE
;
5235 /* The `>' token might be the end of a template-id or
5236 template-parameter-list now. */
5237 parser
->greater_than_is_operator_p
5238 = saved_greater_than_is_operator_p
;
5240 /* Consume the `)'. */
5241 token
= cp_lexer_peek_token (parser
->lexer
);
5242 location_t close_paren_loc
= token
->location
;
5243 expr
.set_range (open_paren_loc
, close_paren_loc
);
5244 if (!parens
.require_close (parser
)
5245 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5246 cp_parser_skip_to_end_of_statement (parser
);
5251 case CPP_OPEN_SQUARE
:
5253 if (c_dialect_objc ())
5255 /* We might have an Objective-C++ message. */
5256 cp_parser_parse_tentatively (parser
);
5257 tree msg
= cp_parser_objc_message_expression (parser
);
5258 /* If that works out, we're done ... */
5259 if (cp_parser_parse_definitely (parser
))
5261 /* ... else, fall though to see if it's a lambda. */
5263 cp_expr lam
= cp_parser_lambda_expression (parser
);
5264 /* Don't warn about a failed tentative parse. */
5265 if (cp_parser_error_occurred (parser
))
5266 return error_mark_node
;
5267 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5271 case CPP_OBJC_STRING
:
5272 if (c_dialect_objc ())
5273 /* We have an Objective-C++ string literal. */
5274 return cp_parser_objc_expression (parser
);
5275 cp_parser_error (parser
, "expected primary-expression");
5276 return error_mark_node
;
5279 switch (token
->keyword
)
5281 /* These two are the boolean literals. */
5283 cp_lexer_consume_token (parser
->lexer
);
5284 return cp_expr (boolean_true_node
, token
->location
);
5286 cp_lexer_consume_token (parser
->lexer
);
5287 return cp_expr (boolean_false_node
, token
->location
);
5289 /* The `__null' literal. */
5291 cp_lexer_consume_token (parser
->lexer
);
5292 return cp_expr (null_node
, token
->location
);
5294 /* The `nullptr' literal. */
5296 cp_lexer_consume_token (parser
->lexer
);
5297 return cp_expr (nullptr_node
, token
->location
);
5299 /* Recognize the `this' keyword. */
5301 cp_lexer_consume_token (parser
->lexer
);
5302 if (parser
->local_variables_forbidden_p
)
5304 error_at (token
->location
,
5305 "%<this%> may not be used in this context");
5306 return error_mark_node
;
5308 /* Pointers cannot appear in constant-expressions. */
5309 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5310 return error_mark_node
;
5311 return cp_expr (finish_this_expr (), token
->location
);
5313 /* The `operator' keyword can be the beginning of an
5318 case RID_FUNCTION_NAME
:
5319 case RID_PRETTY_FUNCTION_NAME
:
5320 case RID_C99_FUNCTION_NAME
:
5322 non_integral_constant name
;
5324 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5325 __func__ are the names of variables -- but they are
5326 treated specially. Therefore, they are handled here,
5327 rather than relying on the generic id-expression logic
5328 below. Grammatically, these names are id-expressions.
5330 Consume the token. */
5331 token
= cp_lexer_consume_token (parser
->lexer
);
5333 switch (token
->keyword
)
5335 case RID_FUNCTION_NAME
:
5336 name
= NIC_FUNC_NAME
;
5338 case RID_PRETTY_FUNCTION_NAME
:
5339 name
= NIC_PRETTY_FUNC
;
5341 case RID_C99_FUNCTION_NAME
:
5342 name
= NIC_C99_FUNC
;
5348 if (cp_parser_non_integral_constant_expression (parser
, name
))
5349 return error_mark_node
;
5351 /* Look up the name. */
5352 return finish_fname (token
->u
.value
);
5359 source_location type_location
;
5360 location_t start_loc
5361 = cp_lexer_peek_token (parser
->lexer
)->location
;
5362 /* The `__builtin_va_arg' construct is used to handle
5363 `va_arg'. Consume the `__builtin_va_arg' token. */
5364 cp_lexer_consume_token (parser
->lexer
);
5365 /* Look for the opening `('. */
5366 matching_parens parens
;
5367 parens
.require_open (parser
);
5368 /* Now, parse the assignment-expression. */
5369 expression
= cp_parser_assignment_expression (parser
);
5370 /* Look for the `,'. */
5371 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5372 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5373 /* Parse the type-id. */
5375 type_id_in_expr_sentinel
s (parser
);
5376 type
= cp_parser_type_id (parser
);
5378 /* Look for the closing `)'. */
5379 location_t finish_loc
5380 = cp_lexer_peek_token (parser
->lexer
)->location
;
5381 parens
.require_close (parser
);
5382 /* Using `va_arg' in a constant-expression is not
5384 if (cp_parser_non_integral_constant_expression (parser
,
5386 return error_mark_node
;
5387 /* Construct a location of the form:
5388 __builtin_va_arg (v, int)
5389 ~~~~~~~~~~~~~~~~~~~~~^~~~
5390 with the caret at the type, ranging from the start of the
5391 "__builtin_va_arg" token to the close paren. */
5392 location_t combined_loc
5393 = make_location (type_location
, start_loc
, finish_loc
);
5394 return build_x_va_arg (combined_loc
, expression
, type
);
5398 return cp_parser_builtin_offsetof (parser
);
5400 case RID_HAS_NOTHROW_ASSIGN
:
5401 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5402 case RID_HAS_NOTHROW_COPY
:
5403 case RID_HAS_TRIVIAL_ASSIGN
:
5404 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5405 case RID_HAS_TRIVIAL_COPY
:
5406 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5407 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5408 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5409 case RID_IS_ABSTRACT
:
5410 case RID_IS_AGGREGATE
:
5411 case RID_IS_BASE_OF
:
5416 case RID_IS_LITERAL_TYPE
:
5418 case RID_IS_POLYMORPHIC
:
5419 case RID_IS_SAME_AS
:
5420 case RID_IS_STD_LAYOUT
:
5421 case RID_IS_TRIVIAL
:
5422 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5423 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5424 case RID_IS_TRIVIALLY_COPYABLE
:
5426 case RID_IS_ASSIGNABLE
:
5427 case RID_IS_CONSTRUCTIBLE
:
5428 return cp_parser_trait_expr (parser
, token
->keyword
);
5432 return cp_parser_requires_expression (parser
);
5434 /* Objective-C++ expressions. */
5436 case RID_AT_PROTOCOL
:
5437 case RID_AT_SELECTOR
:
5438 return cp_parser_objc_expression (parser
);
5441 if (parser
->in_function_body
5442 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5445 error_at (token
->location
,
5446 "a template declaration cannot appear at block scope");
5447 cp_parser_skip_to_end_of_block_or_statement (parser
);
5448 return error_mark_node
;
5452 cp_parser_error (parser
, "expected primary-expression");
5453 return error_mark_node
;
5456 /* An id-expression can start with either an identifier, a
5457 `::' as the beginning of a qualified-id, or the "operator"
5461 case CPP_TEMPLATE_ID
:
5462 case CPP_NESTED_NAME_SPECIFIER
:
5465 cp_expr id_expression
;
5467 const char *error_msg
;
5470 cp_token
*id_expr_token
;
5472 /* Parse the id-expression. */
5474 = cp_parser_id_expression (parser
,
5475 /*template_keyword_p=*/false,
5476 /*check_dependency_p=*/true,
5478 /*declarator_p=*/false,
5479 /*optional_p=*/false);
5480 if (id_expression
== error_mark_node
)
5481 return error_mark_node
;
5482 id_expr_token
= token
;
5483 token
= cp_lexer_peek_token (parser
->lexer
);
5484 done
= (token
->type
!= CPP_OPEN_SQUARE
5485 && token
->type
!= CPP_OPEN_PAREN
5486 && token
->type
!= CPP_DOT
5487 && token
->type
!= CPP_DEREF
5488 && token
->type
!= CPP_PLUS_PLUS
5489 && token
->type
!= CPP_MINUS_MINUS
);
5490 /* If we have a template-id, then no further lookup is
5491 required. If the template-id was for a template-class, we
5492 will sometimes have a TYPE_DECL at this point. */
5493 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5494 || TREE_CODE (id_expression
) == TYPE_DECL
)
5495 decl
= id_expression
;
5496 /* Look up the name. */
5499 tree ambiguous_decls
;
5501 /* If we already know that this lookup is ambiguous, then
5502 we've already issued an error message; there's no reason
5504 if (id_expr_token
->type
== CPP_NAME
5505 && id_expr_token
->error_reported
)
5507 cp_parser_simulate_error (parser
);
5508 return error_mark_node
;
5511 decl
= cp_parser_lookup_name (parser
, id_expression
,
5514 /*is_namespace=*/false,
5515 /*check_dependency=*/true,
5517 id_expr_token
->location
);
5518 /* If the lookup was ambiguous, an error will already have
5520 if (ambiguous_decls
)
5521 return error_mark_node
;
5523 /* In Objective-C++, we may have an Objective-C 2.0
5524 dot-syntax for classes here. */
5525 if (c_dialect_objc ()
5526 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5527 && TREE_CODE (decl
) == TYPE_DECL
5528 && objc_is_class_name (decl
))
5531 cp_lexer_consume_token (parser
->lexer
);
5532 component
= cp_parser_identifier (parser
);
5533 if (component
== error_mark_node
)
5534 return error_mark_node
;
5536 tree result
= objc_build_class_component_ref (id_expression
,
5538 /* Build a location of the form:
5541 with caret at the start of the component name (at
5542 input_location), ranging from the start of the id_expression
5543 to the end of the component name. */
5544 location_t combined_loc
5545 = make_location (input_location
, id_expression
.get_start (),
5546 get_finish (input_location
));
5547 protected_set_expr_location (result
, combined_loc
);
5551 /* In Objective-C++, an instance variable (ivar) may be preferred
5552 to whatever cp_parser_lookup_name() found.
5553 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5554 rest of c-family, we have to do a little extra work to preserve
5555 any location information in cp_expr "decl". Given that
5556 objc_lookup_ivar is implemented in "c-family" and "objc", we
5557 have a trip through the pure "tree" type, rather than cp_expr.
5558 Naively copying it back to "decl" would implicitly give the
5559 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5560 store an EXPR_LOCATION. Hence we only update "decl" (and
5561 hence its location_t) if we get back a different tree node. */
5562 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5564 if (decl_tree
!= decl
.get_value ())
5565 decl
= cp_expr (decl_tree
);
5567 /* If name lookup gives us a SCOPE_REF, then the
5568 qualifying scope was dependent. */
5569 if (TREE_CODE (decl
) == SCOPE_REF
)
5571 /* At this point, we do not know if DECL is a valid
5572 integral constant expression. We assume that it is
5573 in fact such an expression, so that code like:
5575 template <int N> struct A {
5579 is accepted. At template-instantiation time, we
5580 will check that B<N>::i is actually a constant. */
5583 /* Check to see if DECL is a local variable in a context
5584 where that is forbidden. */
5585 if (parser
->local_variables_forbidden_p
5586 && local_variable_p (decl
))
5588 /* It might be that we only found DECL because we are
5589 trying to be generous with pre-ISO scoping rules.
5590 For example, consider:
5594 for (int i = 0; i < 10; ++i) {}
5595 extern void f(int j = i);
5598 Here, name look up will originally find the out
5599 of scope `i'. We need to issue a warning message,
5600 but then use the global `i'. */
5601 decl
= check_for_out_of_scope_variable (decl
);
5602 if (local_variable_p (decl
))
5604 error_at (id_expr_token
->location
,
5605 "local variable %qD may not appear in this context",
5607 return error_mark_node
;
5612 decl
= (finish_id_expression
5613 (id_expression
, decl
, parser
->scope
,
5615 parser
->integral_constant_expression_p
,
5616 parser
->allow_non_integral_constant_expression_p
,
5617 &parser
->non_integral_constant_expression_p
,
5618 template_p
, done
, address_p
,
5621 id_expression
.get_location ()));
5623 cp_parser_error (parser
, error_msg
);
5624 decl
.set_location (id_expr_token
->location
);
5628 /* Anything else is an error. */
5630 cp_parser_error (parser
, "expected primary-expression");
5631 return error_mark_node
;
5635 static inline cp_expr
5636 cp_parser_primary_expression (cp_parser
*parser
,
5639 bool template_arg_p
,
5642 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5643 /*decltype*/false, idk
);
5646 /* Parse an id-expression.
5653 :: [opt] nested-name-specifier template [opt] unqualified-id
5655 :: operator-function-id
5658 Return a representation of the unqualified portion of the
5659 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5660 a `::' or nested-name-specifier.
5662 Often, if the id-expression was a qualified-id, the caller will
5663 want to make a SCOPE_REF to represent the qualified-id. This
5664 function does not do this in order to avoid wastefully creating
5665 SCOPE_REFs when they are not required.
5667 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5670 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5671 uninstantiated templates.
5673 If *TEMPLATE_P is non-NULL, it is set to true iff the
5674 `template' keyword is used to explicitly indicate that the entity
5675 named is a template.
5677 If DECLARATOR_P is true, the id-expression is appearing as part of
5678 a declarator, rather than as part of an expression. */
5681 cp_parser_id_expression (cp_parser
*parser
,
5682 bool template_keyword_p
,
5683 bool check_dependency_p
,
5688 bool global_scope_p
;
5689 bool nested_name_specifier_p
;
5691 /* Assume the `template' keyword was not used. */
5693 *template_p
= template_keyword_p
;
5695 /* Look for the optional `::' operator. */
5697 = (!template_keyword_p
5698 && (cp_parser_global_scope_opt (parser
,
5699 /*current_scope_valid_p=*/false)
5702 /* Look for the optional nested-name-specifier. */
5703 nested_name_specifier_p
5704 = (cp_parser_nested_name_specifier_opt (parser
,
5705 /*typename_keyword_p=*/false,
5712 /* If there is a nested-name-specifier, then we are looking at
5713 the first qualified-id production. */
5714 if (nested_name_specifier_p
)
5717 tree saved_object_scope
;
5718 tree saved_qualifying_scope
;
5719 cp_expr unqualified_id
;
5722 /* See if the next token is the `template' keyword. */
5724 template_p
= &is_template
;
5725 *template_p
= cp_parser_optional_template_keyword (parser
);
5726 /* Name lookup we do during the processing of the
5727 unqualified-id might obliterate SCOPE. */
5728 saved_scope
= parser
->scope
;
5729 saved_object_scope
= parser
->object_scope
;
5730 saved_qualifying_scope
= parser
->qualifying_scope
;
5731 /* Process the final unqualified-id. */
5732 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5735 /*optional_p=*/false);
5736 /* Restore the SAVED_SCOPE for our caller. */
5737 parser
->scope
= saved_scope
;
5738 parser
->object_scope
= saved_object_scope
;
5739 parser
->qualifying_scope
= saved_qualifying_scope
;
5741 return unqualified_id
;
5743 /* Otherwise, if we are in global scope, then we are looking at one
5744 of the other qualified-id productions. */
5745 else if (global_scope_p
)
5750 /* Peek at the next token. */
5751 token
= cp_lexer_peek_token (parser
->lexer
);
5753 /* If it's an identifier, and the next token is not a "<", then
5754 we can avoid the template-id case. This is an optimization
5755 for this common case. */
5756 if (token
->type
== CPP_NAME
5757 && !cp_parser_nth_token_starts_template_argument_list_p
5759 return cp_parser_identifier (parser
);
5761 cp_parser_parse_tentatively (parser
);
5762 /* Try a template-id. */
5763 id
= cp_parser_template_id (parser
,
5764 /*template_keyword_p=*/false,
5765 /*check_dependency_p=*/true,
5768 /* If that worked, we're done. */
5769 if (cp_parser_parse_definitely (parser
))
5772 /* Peek at the next token. (Changes in the token buffer may
5773 have invalidated the pointer obtained above.) */
5774 token
= cp_lexer_peek_token (parser
->lexer
);
5776 switch (token
->type
)
5779 return cp_parser_identifier (parser
);
5782 if (token
->keyword
== RID_OPERATOR
)
5783 return cp_parser_operator_function_id (parser
);
5787 cp_parser_error (parser
, "expected id-expression");
5788 return error_mark_node
;
5792 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5793 /*check_dependency_p=*/true,
5798 /* Parse an unqualified-id.
5802 operator-function-id
5803 conversion-function-id
5807 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5808 keyword, in a construct like `A::template ...'.
5810 Returns a representation of unqualified-id. For the `identifier'
5811 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5812 production a BIT_NOT_EXPR is returned; the operand of the
5813 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5814 other productions, see the documentation accompanying the
5815 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5816 names are looked up in uninstantiated templates. If DECLARATOR_P
5817 is true, the unqualified-id is appearing as part of a declarator,
5818 rather than as part of an expression. */
5821 cp_parser_unqualified_id (cp_parser
* parser
,
5822 bool template_keyword_p
,
5823 bool check_dependency_p
,
5829 /* Peek at the next token. */
5830 token
= cp_lexer_peek_token (parser
->lexer
);
5832 switch ((int) token
->type
)
5838 /* We don't know yet whether or not this will be a
5840 cp_parser_parse_tentatively (parser
);
5841 /* Try a template-id. */
5842 id
= cp_parser_template_id (parser
, template_keyword_p
,
5846 /* If it worked, we're done. */
5847 if (cp_parser_parse_definitely (parser
))
5849 /* Otherwise, it's an ordinary identifier. */
5850 return cp_parser_identifier (parser
);
5853 case CPP_TEMPLATE_ID
:
5854 return cp_parser_template_id (parser
, template_keyword_p
,
5862 tree qualifying_scope
;
5867 /* Consume the `~' token. */
5868 cp_lexer_consume_token (parser
->lexer
);
5869 /* Parse the class-name. The standard, as written, seems to
5872 template <typename T> struct S { ~S (); };
5873 template <typename T> S<T>::~S() {}
5875 is invalid, since `~' must be followed by a class-name, but
5876 `S<T>' is dependent, and so not known to be a class.
5877 That's not right; we need to look in uninstantiated
5878 templates. A further complication arises from:
5880 template <typename T> void f(T t) {
5884 Here, it is not possible to look up `T' in the scope of `T'
5885 itself. We must look in both the current scope, and the
5886 scope of the containing complete expression.
5888 Yet another issue is:
5897 The standard does not seem to say that the `S' in `~S'
5898 should refer to the type `S' and not the data member
5901 /* DR 244 says that we look up the name after the "~" in the
5902 same scope as we looked up the qualifying name. That idea
5903 isn't fully worked out; it's more complicated than that. */
5904 scope
= parser
->scope
;
5905 object_scope
= parser
->object_scope
;
5906 qualifying_scope
= parser
->qualifying_scope
;
5908 /* Check for invalid scopes. */
5909 if (scope
== error_mark_node
)
5911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5912 cp_lexer_consume_token (parser
->lexer
);
5913 return error_mark_node
;
5915 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5917 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5918 error_at (token
->location
,
5919 "scope %qT before %<~%> is not a class-name",
5921 cp_parser_simulate_error (parser
);
5922 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5923 cp_lexer_consume_token (parser
->lexer
);
5924 return error_mark_node
;
5926 gcc_assert (!scope
|| TYPE_P (scope
));
5928 /* If the name is of the form "X::~X" it's OK even if X is a
5930 token
= cp_lexer_peek_token (parser
->lexer
);
5932 && token
->type
== CPP_NAME
5933 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5935 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5936 || (CLASS_TYPE_P (scope
)
5937 && constructor_name_p (token
->u
.value
, scope
))))
5939 cp_lexer_consume_token (parser
->lexer
);
5940 return build_nt (BIT_NOT_EXPR
, scope
);
5943 /* ~auto means the destructor of whatever the object is. */
5944 if (cp_parser_is_keyword (token
, RID_AUTO
))
5946 if (cxx_dialect
< cxx14
)
5947 pedwarn (input_location
, 0,
5948 "%<~auto%> only available with "
5949 "-std=c++14 or -std=gnu++14");
5950 cp_lexer_consume_token (parser
->lexer
);
5951 return build_nt (BIT_NOT_EXPR
, make_auto ());
5954 /* If there was an explicit qualification (S::~T), first look
5955 in the scope given by the qualification (i.e., S).
5957 Note: in the calls to cp_parser_class_name below we pass
5958 typename_type so that lookup finds the injected-class-name
5959 rather than the constructor. */
5961 type_decl
= NULL_TREE
;
5964 cp_parser_parse_tentatively (parser
);
5965 type_decl
= cp_parser_class_name (parser
,
5966 /*typename_keyword_p=*/false,
5967 /*template_keyword_p=*/false,
5969 /*check_dependency=*/false,
5970 /*class_head_p=*/false,
5972 if (cp_parser_parse_definitely (parser
))
5975 /* In "N::S::~S", look in "N" as well. */
5976 if (!done
&& scope
&& qualifying_scope
)
5978 cp_parser_parse_tentatively (parser
);
5979 parser
->scope
= qualifying_scope
;
5980 parser
->object_scope
= NULL_TREE
;
5981 parser
->qualifying_scope
= NULL_TREE
;
5983 = cp_parser_class_name (parser
,
5984 /*typename_keyword_p=*/false,
5985 /*template_keyword_p=*/false,
5987 /*check_dependency=*/false,
5988 /*class_head_p=*/false,
5990 if (cp_parser_parse_definitely (parser
))
5993 /* In "p->S::~T", look in the scope given by "*p" as well. */
5994 else if (!done
&& object_scope
)
5996 cp_parser_parse_tentatively (parser
);
5997 parser
->scope
= object_scope
;
5998 parser
->object_scope
= NULL_TREE
;
5999 parser
->qualifying_scope
= NULL_TREE
;
6001 = cp_parser_class_name (parser
,
6002 /*typename_keyword_p=*/false,
6003 /*template_keyword_p=*/false,
6005 /*check_dependency=*/false,
6006 /*class_head_p=*/false,
6008 if (cp_parser_parse_definitely (parser
))
6011 /* Look in the surrounding context. */
6014 parser
->scope
= NULL_TREE
;
6015 parser
->object_scope
= NULL_TREE
;
6016 parser
->qualifying_scope
= NULL_TREE
;
6017 if (processing_template_decl
)
6018 cp_parser_parse_tentatively (parser
);
6020 = cp_parser_class_name (parser
,
6021 /*typename_keyword_p=*/false,
6022 /*template_keyword_p=*/false,
6024 /*check_dependency=*/false,
6025 /*class_head_p=*/false,
6027 if (processing_template_decl
6028 && ! cp_parser_parse_definitely (parser
))
6030 /* We couldn't find a type with this name. If we're parsing
6031 tentatively, fail and try something else. */
6032 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6034 cp_parser_simulate_error (parser
);
6035 return error_mark_node
;
6037 /* Otherwise, accept it and check for a match at instantiation
6039 type_decl
= cp_parser_identifier (parser
);
6040 if (type_decl
!= error_mark_node
)
6041 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6045 /* If an error occurred, assume that the name of the
6046 destructor is the same as the name of the qualifying
6047 class. That allows us to keep parsing after running
6048 into ill-formed destructor names. */
6049 if (type_decl
== error_mark_node
&& scope
)
6050 return build_nt (BIT_NOT_EXPR
, scope
);
6051 else if (type_decl
== error_mark_node
)
6052 return error_mark_node
;
6054 /* Check that destructor name and scope match. */
6055 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6057 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6058 error_at (token
->location
,
6059 "declaration of %<~%T%> as member of %qT",
6061 cp_parser_simulate_error (parser
);
6062 return error_mark_node
;
6067 A typedef-name that names a class shall not be used as the
6068 identifier in the declarator for a destructor declaration. */
6070 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6071 && !DECL_SELF_REFERENCE_P (type_decl
)
6072 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6073 error_at (token
->location
,
6074 "typedef-name %qD used as destructor declarator",
6077 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6081 if (token
->keyword
== RID_OPERATOR
)
6085 /* This could be a template-id, so we try that first. */
6086 cp_parser_parse_tentatively (parser
);
6087 /* Try a template-id. */
6088 id
= cp_parser_template_id (parser
, template_keyword_p
,
6089 /*check_dependency_p=*/true,
6092 /* If that worked, we're done. */
6093 if (cp_parser_parse_definitely (parser
))
6095 /* We still don't know whether we're looking at an
6096 operator-function-id or a conversion-function-id. */
6097 cp_parser_parse_tentatively (parser
);
6098 /* Try an operator-function-id. */
6099 id
= cp_parser_operator_function_id (parser
);
6100 /* If that didn't work, try a conversion-function-id. */
6101 if (!cp_parser_parse_definitely (parser
))
6102 id
= cp_parser_conversion_function_id (parser
);
6103 else if (UDLIT_OPER_P (id
))
6106 const char *name
= UDLIT_OP_SUFFIX (id
);
6107 if (name
[0] != '_' && !in_system_header_at (input_location
)
6109 warning (OPT_Wliteral_suffix
,
6110 "literal operator suffixes not preceded by %<_%>"
6111 " are reserved for future standardization");
6121 cp_parser_error (parser
, "expected unqualified-id");
6122 return error_mark_node
;
6126 /* Parse an (optional) nested-name-specifier.
6128 nested-name-specifier: [C++98]
6129 class-or-namespace-name :: nested-name-specifier [opt]
6130 class-or-namespace-name :: template nested-name-specifier [opt]
6132 nested-name-specifier: [C++0x]
6135 nested-name-specifier identifier ::
6136 nested-name-specifier template [opt] simple-template-id ::
6138 PARSER->SCOPE should be set appropriately before this function is
6139 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6140 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6143 Sets PARSER->SCOPE to the class (TYPE) or namespace
6144 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6145 it unchanged if there is no nested-name-specifier. Returns the new
6146 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6148 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6149 part of a declaration and/or decl-specifier. */
6152 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6153 bool typename_keyword_p
,
6154 bool check_dependency_p
,
6156 bool is_declaration
,
6157 bool template_keyword_p
/* = false */)
6159 bool success
= false;
6160 cp_token_position start
= 0;
6163 /* Remember where the nested-name-specifier starts. */
6164 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6166 start
= cp_lexer_token_position (parser
->lexer
, false);
6167 push_deferring_access_checks (dk_deferred
);
6174 tree saved_qualifying_scope
;
6176 /* Spot cases that cannot be the beginning of a
6177 nested-name-specifier. */
6178 token
= cp_lexer_peek_token (parser
->lexer
);
6180 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6181 the already parsed nested-name-specifier. */
6182 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6184 /* Grab the nested-name-specifier and continue the loop. */
6185 cp_parser_pre_parsed_nested_name_specifier (parser
);
6186 /* If we originally encountered this nested-name-specifier
6187 with IS_DECLARATION set to false, we will not have
6188 resolved TYPENAME_TYPEs, so we must do so here. */
6190 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6192 new_scope
= resolve_typename_type (parser
->scope
,
6193 /*only_current_p=*/false);
6194 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6195 parser
->scope
= new_scope
;
6201 /* Spot cases that cannot be the beginning of a
6202 nested-name-specifier. On the second and subsequent times
6203 through the loop, we look for the `template' keyword. */
6204 if (success
&& token
->keyword
== RID_TEMPLATE
)
6206 /* A template-id can start a nested-name-specifier. */
6207 else if (token
->type
== CPP_TEMPLATE_ID
)
6209 /* DR 743: decltype can be used in a nested-name-specifier. */
6210 else if (token_is_decltype (token
))
6214 /* If the next token is not an identifier, then it is
6215 definitely not a type-name or namespace-name. */
6216 if (token
->type
!= CPP_NAME
)
6218 /* If the following token is neither a `<' (to begin a
6219 template-id), nor a `::', then we are not looking at a
6220 nested-name-specifier. */
6221 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6223 if (token
->type
== CPP_COLON
6224 && parser
->colon_corrects_to_scope_p
6225 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6227 gcc_rich_location
richloc (token
->location
);
6228 richloc
.add_fixit_replace ("::");
6230 "found %<:%> in nested-name-specifier, "
6232 token
->type
= CPP_SCOPE
;
6235 if (token
->type
!= CPP_SCOPE
6236 && !cp_parser_nth_token_starts_template_argument_list_p
6241 /* The nested-name-specifier is optional, so we parse
6243 cp_parser_parse_tentatively (parser
);
6245 /* Look for the optional `template' keyword, if this isn't the
6246 first time through the loop. */
6248 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6250 /* Save the old scope since the name lookup we are about to do
6251 might destroy it. */
6252 old_scope
= parser
->scope
;
6253 saved_qualifying_scope
= parser
->qualifying_scope
;
6254 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6255 look up names in "X<T>::I" in order to determine that "Y" is
6256 a template. So, if we have a typename at this point, we make
6257 an effort to look through it. */
6259 && !typename_keyword_p
6261 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6262 parser
->scope
= resolve_typename_type (parser
->scope
,
6263 /*only_current_p=*/false);
6264 /* Parse the qualifying entity. */
6266 = cp_parser_qualifying_entity (parser
,
6272 /* Look for the `::' token. */
6273 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6275 /* If we found what we wanted, we keep going; otherwise, we're
6277 if (!cp_parser_parse_definitely (parser
))
6279 bool error_p
= false;
6281 /* Restore the OLD_SCOPE since it was valid before the
6282 failed attempt at finding the last
6283 class-or-namespace-name. */
6284 parser
->scope
= old_scope
;
6285 parser
->qualifying_scope
= saved_qualifying_scope
;
6287 /* If the next token is a decltype, and the one after that is a
6288 `::', then the decltype has failed to resolve to a class or
6289 enumeration type. Give this error even when parsing
6290 tentatively since it can't possibly be valid--and we're going
6291 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6292 won't get another chance.*/
6293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6294 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6297 token
= cp_lexer_consume_token (parser
->lexer
);
6298 error_at (token
->location
, "decltype evaluates to %qT, "
6299 "which is not a class or enumeration type",
6300 token
->u
.tree_check_value
->value
);
6301 parser
->scope
= error_mark_node
;
6305 cp_lexer_consume_token (parser
->lexer
);
6308 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6309 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6311 /* If we have a non-type template-id followed by ::, it can't
6312 possibly be valid. */
6313 token
= cp_lexer_peek_token (parser
->lexer
);
6314 tree tid
= token
->u
.tree_check_value
->value
;
6315 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6316 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6318 tree tmpl
= NULL_TREE
;
6319 if (is_overloaded_fn (tid
))
6321 tree fns
= get_fns (tid
);
6322 if (OVL_SINGLE_P (fns
))
6323 tmpl
= OVL_FIRST (fns
);
6324 error_at (token
->location
, "function template-id %qD "
6325 "in nested-name-specifier", tid
);
6329 /* Variable template. */
6330 tmpl
= TREE_OPERAND (tid
, 0);
6331 gcc_assert (variable_template_p (tmpl
));
6332 error_at (token
->location
, "variable template-id %qD "
6333 "in nested-name-specifier", tid
);
6336 inform (DECL_SOURCE_LOCATION (tmpl
),
6337 "%qD declared here", tmpl
);
6339 parser
->scope
= error_mark_node
;
6343 cp_lexer_consume_token (parser
->lexer
);
6344 cp_lexer_consume_token (parser
->lexer
);
6348 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6350 /* If the next token is an identifier, and the one after
6351 that is a `::', then any valid interpretation would have
6352 found a class-or-namespace-name. */
6353 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6354 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6356 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6359 token
= cp_lexer_consume_token (parser
->lexer
);
6362 if (!token
->error_reported
)
6365 tree ambiguous_decls
;
6367 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6369 /*is_template=*/false,
6370 /*is_namespace=*/false,
6371 /*check_dependency=*/true,
6374 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6375 error_at (token
->location
,
6376 "%qD used without template parameters",
6378 else if (ambiguous_decls
)
6380 // cp_parser_lookup_name has the same diagnostic,
6381 // thus make sure to emit it at most once.
6382 if (cp_parser_uncommitted_to_tentative_parse_p
6385 error_at (token
->location
,
6386 "reference to %qD is ambiguous",
6388 print_candidates (ambiguous_decls
);
6390 decl
= error_mark_node
;
6394 if (cxx_dialect
!= cxx98
)
6395 cp_parser_name_lookup_error
6396 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6399 cp_parser_name_lookup_error
6400 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6404 parser
->scope
= error_mark_node
;
6406 /* Treat this as a successful nested-name-specifier
6411 If the name found is not a class-name (clause
6412 _class_) or namespace-name (_namespace.def_), the
6413 program is ill-formed. */
6416 cp_lexer_consume_token (parser
->lexer
);
6420 /* We've found one valid nested-name-specifier. */
6422 /* Name lookup always gives us a DECL. */
6423 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6424 new_scope
= TREE_TYPE (new_scope
);
6425 /* Uses of "template" must be followed by actual templates. */
6426 if (template_keyword_p
6427 && !(CLASS_TYPE_P (new_scope
)
6428 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6429 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6430 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6431 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6432 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6433 == TEMPLATE_ID_EXPR
)))
6434 permerror (input_location
, TYPE_P (new_scope
)
6435 ? G_("%qT is not a template")
6436 : G_("%qD is not a template"),
6438 /* If it is a class scope, try to complete it; we are about to
6439 be looking up names inside the class. */
6440 if (TYPE_P (new_scope
)
6441 /* Since checking types for dependency can be expensive,
6442 avoid doing it if the type is already complete. */
6443 && !COMPLETE_TYPE_P (new_scope
)
6444 /* Do not try to complete dependent types. */
6445 && !dependent_type_p (new_scope
))
6447 new_scope
= complete_type (new_scope
);
6448 /* If it is a typedef to current class, use the current
6449 class instead, as the typedef won't have any names inside
6451 if (!COMPLETE_TYPE_P (new_scope
)
6452 && currently_open_class (new_scope
))
6453 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6455 /* Make sure we look in the right scope the next time through
6457 parser
->scope
= new_scope
;
6460 /* If parsing tentatively, replace the sequence of tokens that makes
6461 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6462 token. That way, should we re-parse the token stream, we will
6463 not have to repeat the effort required to do the parse, nor will
6464 we issue duplicate error messages. */
6465 if (success
&& start
)
6469 token
= cp_lexer_token_at (parser
->lexer
, start
);
6470 /* Reset the contents of the START token. */
6471 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6472 /* Retrieve any deferred checks. Do not pop this access checks yet
6473 so the memory will not be reclaimed during token replacing below. */
6474 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6475 token
->u
.tree_check_value
->value
= parser
->scope
;
6476 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6477 token
->u
.tree_check_value
->qualifying_scope
=
6478 parser
->qualifying_scope
;
6479 token
->keyword
= RID_MAX
;
6481 /* Purge all subsequent tokens. */
6482 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6486 pop_to_parent_deferring_access_checks ();
6488 return success
? parser
->scope
: NULL_TREE
;
6491 /* Parse a nested-name-specifier. See
6492 cp_parser_nested_name_specifier_opt for details. This function
6493 behaves identically, except that it will an issue an error if no
6494 nested-name-specifier is present. */
6497 cp_parser_nested_name_specifier (cp_parser
*parser
,
6498 bool typename_keyword_p
,
6499 bool check_dependency_p
,
6501 bool is_declaration
)
6505 /* Look for the nested-name-specifier. */
6506 scope
= cp_parser_nested_name_specifier_opt (parser
,
6511 /* If it was not present, issue an error message. */
6514 cp_parser_error (parser
, "expected nested-name-specifier");
6515 parser
->scope
= NULL_TREE
;
6521 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6522 this is either a class-name or a namespace-name (which corresponds
6523 to the class-or-namespace-name production in the grammar). For
6524 C++0x, it can also be a type-name that refers to an enumeration
6525 type or a simple-template-id.
6527 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6528 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6529 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6530 TYPE_P is TRUE iff the next name should be taken as a class-name,
6531 even the same name is declared to be another entity in the same
6534 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6535 specified by the class-or-namespace-name. If neither is found the
6536 ERROR_MARK_NODE is returned. */
6539 cp_parser_qualifying_entity (cp_parser
*parser
,
6540 bool typename_keyword_p
,
6541 bool template_keyword_p
,
6542 bool check_dependency_p
,
6544 bool is_declaration
)
6547 tree saved_qualifying_scope
;
6548 tree saved_object_scope
;
6551 bool successful_parse_p
;
6553 /* DR 743: decltype can appear in a nested-name-specifier. */
6554 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6556 scope
= cp_parser_decltype (parser
);
6557 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6558 && !MAYBE_CLASS_TYPE_P (scope
))
6560 cp_parser_simulate_error (parser
);
6561 return error_mark_node
;
6563 if (TYPE_NAME (scope
))
6564 scope
= TYPE_NAME (scope
);
6568 /* Before we try to parse the class-name, we must save away the
6569 current PARSER->SCOPE since cp_parser_class_name will destroy
6571 saved_scope
= parser
->scope
;
6572 saved_qualifying_scope
= parser
->qualifying_scope
;
6573 saved_object_scope
= parser
->object_scope
;
6574 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6575 there is no need to look for a namespace-name. */
6576 only_class_p
= template_keyword_p
6577 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6579 cp_parser_parse_tentatively (parser
);
6580 scope
= cp_parser_class_name (parser
,
6583 type_p
? class_type
: none_type
,
6585 /*class_head_p=*/false,
6587 /*enum_ok=*/cxx_dialect
> cxx98
);
6588 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6589 /* If that didn't work, try for a namespace-name. */
6590 if (!only_class_p
&& !successful_parse_p
)
6592 /* Restore the saved scope. */
6593 parser
->scope
= saved_scope
;
6594 parser
->qualifying_scope
= saved_qualifying_scope
;
6595 parser
->object_scope
= saved_object_scope
;
6596 /* If we are not looking at an identifier followed by the scope
6597 resolution operator, then this is not part of a
6598 nested-name-specifier. (Note that this function is only used
6599 to parse the components of a nested-name-specifier.) */
6600 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6601 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6602 return error_mark_node
;
6603 scope
= cp_parser_namespace_name (parser
);
6609 /* Return true if we are looking at a compound-literal, false otherwise. */
6612 cp_parser_compound_literal_p (cp_parser
*parser
)
6614 cp_lexer_save_tokens (parser
->lexer
);
6616 /* Skip tokens until the next token is a closing parenthesis.
6617 If we find the closing `)', and the next token is a `{', then
6618 we are looking at a compound-literal. */
6619 bool compound_literal_p
6620 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6621 /*consume_paren=*/true)
6622 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6624 /* Roll back the tokens we skipped. */
6625 cp_lexer_rollback_tokens (parser
->lexer
);
6627 return compound_literal_p
;
6630 /* Return true if EXPR is the integer constant zero or a complex constant
6631 of zero, without any folding, but ignoring location wrappers. */
6634 literal_integer_zerop (const_tree expr
)
6636 STRIP_ANY_LOCATION_WRAPPER (expr
);
6637 return integer_zerop (expr
);
6640 /* Parse a postfix-expression.
6644 postfix-expression [ expression ]
6645 postfix-expression ( expression-list [opt] )
6646 simple-type-specifier ( expression-list [opt] )
6647 typename :: [opt] nested-name-specifier identifier
6648 ( expression-list [opt] )
6649 typename :: [opt] nested-name-specifier template [opt] template-id
6650 ( expression-list [opt] )
6651 postfix-expression . template [opt] id-expression
6652 postfix-expression -> template [opt] id-expression
6653 postfix-expression . pseudo-destructor-name
6654 postfix-expression -> pseudo-destructor-name
6655 postfix-expression ++
6656 postfix-expression --
6657 dynamic_cast < type-id > ( expression )
6658 static_cast < type-id > ( expression )
6659 reinterpret_cast < type-id > ( expression )
6660 const_cast < type-id > ( expression )
6661 typeid ( expression )
6667 ( type-id ) { initializer-list , [opt] }
6669 This extension is a GNU version of the C99 compound-literal
6670 construct. (The C99 grammar uses `type-name' instead of `type-id',
6671 but they are essentially the same concept.)
6673 If ADDRESS_P is true, the postfix expression is the operand of the
6674 `&' operator. CAST_P is true if this expression is the target of a
6677 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6678 class member access expressions [expr.ref].
6680 Returns a representation of the expression. */
6683 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6684 bool member_access_only_p
, bool decltype_p
,
6685 cp_id_kind
* pidk_return
)
6690 cp_id_kind idk
= CP_ID_KIND_NONE
;
6691 cp_expr postfix_expression
= NULL_TREE
;
6692 bool is_member_access
= false;
6694 /* Peek at the next token. */
6695 token
= cp_lexer_peek_token (parser
->lexer
);
6696 loc
= token
->location
;
6697 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6699 /* Some of the productions are determined by keywords. */
6700 keyword
= token
->keyword
;
6710 const char *saved_message
;
6711 bool saved_in_type_id_in_expr_p
;
6713 /* All of these can be handled in the same way from the point
6714 of view of parsing. Begin by consuming the token
6715 identifying the cast. */
6716 cp_lexer_consume_token (parser
->lexer
);
6718 /* New types cannot be defined in the cast. */
6719 saved_message
= parser
->type_definition_forbidden_message
;
6720 parser
->type_definition_forbidden_message
6721 = G_("types may not be defined in casts");
6723 /* Look for the opening `<'. */
6724 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6725 /* Parse the type to which we are casting. */
6726 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6727 parser
->in_type_id_in_expr_p
= true;
6728 type
= cp_parser_type_id (parser
);
6729 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6730 /* Look for the closing `>'. */
6731 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6732 /* Restore the old message. */
6733 parser
->type_definition_forbidden_message
= saved_message
;
6735 bool saved_greater_than_is_operator_p
6736 = parser
->greater_than_is_operator_p
;
6737 parser
->greater_than_is_operator_p
= true;
6739 /* And the expression which is being cast. */
6740 matching_parens parens
;
6741 parens
.require_open (parser
);
6742 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6743 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6745 location_t end_loc
= close_paren
?
6746 close_paren
->location
: UNKNOWN_LOCATION
;
6748 parser
->greater_than_is_operator_p
6749 = saved_greater_than_is_operator_p
;
6751 /* Only type conversions to integral or enumeration types
6752 can be used in constant-expressions. */
6753 if (!cast_valid_in_integral_constant_expression_p (type
)
6754 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6756 postfix_expression
= error_mark_node
;
6764 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6768 = build_static_cast (type
, expression
, tf_warning_or_error
);
6772 = build_reinterpret_cast (type
, expression
,
6773 tf_warning_or_error
);
6777 = build_const_cast (type
, expression
, tf_warning_or_error
);
6783 /* Construct a location e.g. :
6784 reinterpret_cast <int *> (expr)
6785 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6786 ranging from the start of the "*_cast" token to the final closing
6787 paren, with the caret at the start. */
6788 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6789 postfix_expression
.set_location (cp_cast_loc
);
6796 const char *saved_message
;
6797 bool saved_in_type_id_in_expr_p
;
6799 /* Consume the `typeid' token. */
6800 cp_lexer_consume_token (parser
->lexer
);
6801 /* Look for the `(' token. */
6802 matching_parens parens
;
6803 parens
.require_open (parser
);
6804 /* Types cannot be defined in a `typeid' expression. */
6805 saved_message
= parser
->type_definition_forbidden_message
;
6806 parser
->type_definition_forbidden_message
6807 = G_("types may not be defined in a %<typeid%> expression");
6808 /* We can't be sure yet whether we're looking at a type-id or an
6810 cp_parser_parse_tentatively (parser
);
6811 /* Try a type-id first. */
6812 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6813 parser
->in_type_id_in_expr_p
= true;
6814 type
= cp_parser_type_id (parser
);
6815 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6816 /* Look for the `)' token. Otherwise, we can't be sure that
6817 we're not looking at an expression: consider `typeid (int
6818 (3))', for example. */
6819 cp_token
*close_paren
= parens
.require_close (parser
);
6820 /* If all went well, simply lookup the type-id. */
6821 if (cp_parser_parse_definitely (parser
))
6822 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6823 /* Otherwise, fall back to the expression variant. */
6828 /* Look for an expression. */
6829 expression
= cp_parser_expression (parser
, & idk
);
6830 /* Compute its typeid. */
6831 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6832 /* Look for the `)' token. */
6833 close_paren
= parens
.require_close (parser
);
6835 /* Restore the saved message. */
6836 parser
->type_definition_forbidden_message
= saved_message
;
6837 /* `typeid' may not appear in an integral constant expression. */
6838 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6839 postfix_expression
= error_mark_node
;
6841 /* Construct a location e.g. :
6844 ranging from the start of the "typeid" token to the final closing
6845 paren, with the caret at the start. */
6848 location_t typeid_loc
6849 = make_location (start_loc
, start_loc
, close_paren
->location
);
6850 postfix_expression
.set_location (typeid_loc
);
6851 postfix_expression
.maybe_add_location_wrapper ();
6859 /* The syntax permitted here is the same permitted for an
6860 elaborated-type-specifier. */
6861 ++parser
->prevent_constrained_type_specifiers
;
6862 type
= cp_parser_elaborated_type_specifier (parser
,
6863 /*is_friend=*/false,
6864 /*is_declaration=*/false);
6865 --parser
->prevent_constrained_type_specifiers
;
6866 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6871 case RID_BUILTIN_SHUFFLE
:
6872 case RID_BUILTIN_LAUNDER
:
6874 vec
<tree
, va_gc
> *vec
;
6878 cp_lexer_consume_token (parser
->lexer
);
6879 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6880 /*cast_p=*/false, /*allow_expansion_p=*/true,
6881 /*non_constant_p=*/NULL
);
6884 postfix_expression
= error_mark_node
;
6888 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6894 if (vec
->length () == 1)
6896 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6899 error_at (loc
, "wrong number of arguments to "
6900 "%<__builtin_addressof%>");
6901 postfix_expression
= error_mark_node
;
6905 case RID_BUILTIN_LAUNDER
:
6906 if (vec
->length () == 1)
6907 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6908 tf_warning_or_error
);
6911 error_at (loc
, "wrong number of arguments to "
6912 "%<__builtin_launder%>");
6913 postfix_expression
= error_mark_node
;
6917 case RID_BUILTIN_SHUFFLE
:
6918 if (vec
->length () == 2)
6920 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6921 (*vec
)[1], tf_warning_or_error
);
6922 else if (vec
->length () == 3)
6924 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6925 (*vec
)[2], tf_warning_or_error
);
6928 error_at (loc
, "wrong number of arguments to "
6929 "%<__builtin_shuffle%>");
6930 postfix_expression
= error_mark_node
;
6944 /* If the next thing is a simple-type-specifier, we may be
6945 looking at a functional cast. We could also be looking at
6946 an id-expression. So, we try the functional cast, and if
6947 that doesn't work we fall back to the primary-expression. */
6948 cp_parser_parse_tentatively (parser
);
6949 /* Look for the simple-type-specifier. */
6950 ++parser
->prevent_constrained_type_specifiers
;
6951 type
= cp_parser_simple_type_specifier (parser
,
6952 /*decl_specs=*/NULL
,
6953 CP_PARSER_FLAGS_NONE
);
6954 --parser
->prevent_constrained_type_specifiers
;
6955 /* Parse the cast itself. */
6956 if (!cp_parser_error_occurred (parser
))
6958 = cp_parser_functional_cast (parser
, type
);
6959 /* If that worked, we're done. */
6960 if (cp_parser_parse_definitely (parser
))
6963 /* If the functional-cast didn't work out, try a
6964 compound-literal. */
6965 if (cp_parser_allow_gnu_extensions_p (parser
)
6966 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6968 cp_expr initializer
= NULL_TREE
;
6970 cp_parser_parse_tentatively (parser
);
6972 matching_parens parens
;
6973 parens
.consume_open (parser
);
6975 /* Avoid calling cp_parser_type_id pointlessly, see comment
6976 in cp_parser_cast_expression about c++/29234. */
6977 if (!cp_parser_compound_literal_p (parser
))
6978 cp_parser_simulate_error (parser
);
6981 /* Parse the type. */
6982 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6983 parser
->in_type_id_in_expr_p
= true;
6984 type
= cp_parser_type_id (parser
);
6985 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6986 parens
.require_close (parser
);
6989 /* If things aren't going well, there's no need to
6991 if (!cp_parser_error_occurred (parser
))
6993 bool non_constant_p
;
6994 /* Parse the brace-enclosed initializer list. */
6995 initializer
= cp_parser_braced_list (parser
,
6998 /* If that worked, we're definitely looking at a
6999 compound-literal expression. */
7000 if (cp_parser_parse_definitely (parser
))
7002 /* Warn the user that a compound literal is not
7003 allowed in standard C++. */
7004 pedwarn (input_location
, OPT_Wpedantic
,
7005 "ISO C++ forbids compound-literals");
7006 /* For simplicity, we disallow compound literals in
7007 constant-expressions. We could
7008 allow compound literals of integer type, whose
7009 initializer was a constant, in constant
7010 expressions. Permitting that usage, as a further
7011 extension, would not change the meaning of any
7012 currently accepted programs. (Of course, as
7013 compound literals are not part of ISO C++, the
7014 standard has nothing to say.) */
7015 if (cp_parser_non_integral_constant_expression (parser
,
7018 postfix_expression
= error_mark_node
;
7021 /* Form the representation of the compound-literal. */
7023 = finish_compound_literal (type
, initializer
,
7024 tf_warning_or_error
, fcl_c99
);
7025 postfix_expression
.set_location (initializer
.get_location ());
7030 /* It must be a primary-expression. */
7032 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7033 /*template_arg_p=*/false,
7040 /* Note that we don't need to worry about calling build_cplus_new on a
7041 class-valued CALL_EXPR in decltype when it isn't the end of the
7042 postfix-expression; unary_complex_lvalue will take care of that for
7045 /* Keep looping until the postfix-expression is complete. */
7048 if (idk
== CP_ID_KIND_UNQUALIFIED
7049 && identifier_p (postfix_expression
)
7050 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7051 /* It is not a Koenig lookup function call. */
7053 = unqualified_name_lookup_error (postfix_expression
);
7055 /* Peek at the next token. */
7056 token
= cp_lexer_peek_token (parser
->lexer
);
7058 switch (token
->type
)
7060 case CPP_OPEN_SQUARE
:
7061 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7063 cp_parser_error (parser
,
7064 "two consecutive %<[%> shall "
7065 "only introduce an attribute");
7066 return error_mark_node
;
7069 = cp_parser_postfix_open_square_expression (parser
,
7073 postfix_expression
.set_range (start_loc
,
7074 postfix_expression
.get_location ());
7076 idk
= CP_ID_KIND_NONE
;
7077 is_member_access
= false;
7080 case CPP_OPEN_PAREN
:
7081 /* postfix-expression ( expression-list [opt] ) */
7084 bool is_builtin_constant_p
;
7085 bool saved_integral_constant_expression_p
= false;
7086 bool saved_non_integral_constant_expression_p
= false;
7087 tsubst_flags_t complain
= complain_flags (decltype_p
);
7088 vec
<tree
, va_gc
> *args
;
7089 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7091 is_member_access
= false;
7093 is_builtin_constant_p
7094 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7095 if (is_builtin_constant_p
)
7097 /* The whole point of __builtin_constant_p is to allow
7098 non-constant expressions to appear as arguments. */
7099 saved_integral_constant_expression_p
7100 = parser
->integral_constant_expression_p
;
7101 saved_non_integral_constant_expression_p
7102 = parser
->non_integral_constant_expression_p
;
7103 parser
->integral_constant_expression_p
= false;
7105 args
= (cp_parser_parenthesized_expression_list
7107 /*cast_p=*/false, /*allow_expansion_p=*/true,
7108 /*non_constant_p=*/NULL
,
7109 /*close_paren_loc=*/&close_paren_loc
,
7110 /*wrap_locations_p=*/true));
7111 if (is_builtin_constant_p
)
7113 parser
->integral_constant_expression_p
7114 = saved_integral_constant_expression_p
;
7115 parser
->non_integral_constant_expression_p
7116 = saved_non_integral_constant_expression_p
;
7121 postfix_expression
= error_mark_node
;
7125 /* Function calls are not permitted in
7126 constant-expressions. */
7127 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7128 && cp_parser_non_integral_constant_expression (parser
,
7131 postfix_expression
= error_mark_node
;
7132 release_tree_vector (args
);
7137 if (idk
== CP_ID_KIND_UNQUALIFIED
7138 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7140 if (identifier_p (postfix_expression
))
7142 if (!args
->is_empty ())
7145 if (!any_type_dependent_arguments_p (args
))
7147 = perform_koenig_lookup (postfix_expression
, args
,
7152 = unqualified_fn_lookup_error (postfix_expression
);
7154 /* We do not perform argument-dependent lookup if
7155 normal lookup finds a non-function, in accordance
7156 with the expected resolution of DR 218. */
7157 else if (!args
->is_empty ()
7158 && is_overloaded_fn (postfix_expression
))
7160 tree fn
= get_first_fn (postfix_expression
);
7161 fn
= STRIP_TEMPLATE (fn
);
7163 /* Do not do argument dependent lookup if regular
7164 lookup finds a member function or a block-scope
7165 function declaration. [basic.lookup.argdep]/3 */
7166 if (!DECL_FUNCTION_MEMBER_P (fn
)
7167 && !DECL_LOCAL_FUNCTION_P (fn
))
7170 if (!any_type_dependent_arguments_p (args
))
7172 = perform_koenig_lookup (postfix_expression
, args
,
7178 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
7179 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
7180 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
7181 && vec_safe_length (args
) == 3)
7183 tree arg0
= (*args
)[0];
7184 tree arg1
= (*args
)[1];
7185 tree arg2
= (*args
)[2];
7186 int literal_mask
= ((literal_integer_zerop (arg1
) << 1)
7187 | (literal_integer_zerop (arg2
) << 2));
7188 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
7191 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7193 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7194 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7196 if (processing_template_decl
7197 && (type_dependent_object_expression_p (instance
)
7198 || (!BASELINK_P (fn
)
7199 && TREE_CODE (fn
) != FIELD_DECL
)
7200 || type_dependent_expression_p (fn
)
7201 || any_type_dependent_arguments_p (args
)))
7203 maybe_generic_this_capture (instance
, fn
);
7205 = build_min_nt_call_vec (postfix_expression
, args
);
7206 release_tree_vector (args
);
7210 if (BASELINK_P (fn
))
7213 = (build_new_method_call
7214 (instance
, fn
, &args
, NULL_TREE
,
7215 (idk
== CP_ID_KIND_QUALIFIED
7216 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7223 = finish_call_expr (postfix_expression
, &args
,
7224 /*disallow_virtual=*/false,
7228 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7229 || TREE_CODE (postfix_expression
) == MEMBER_REF
7230 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7231 postfix_expression
= (build_offset_ref_call_from_tree
7232 (postfix_expression
, &args
,
7234 else if (idk
== CP_ID_KIND_QUALIFIED
)
7235 /* A call to a static class member, or a namespace-scope
7238 = finish_call_expr (postfix_expression
, &args
,
7239 /*disallow_virtual=*/true,
7243 /* All other function calls. */
7245 = finish_call_expr (postfix_expression
, &args
,
7246 /*disallow_virtual=*/false,
7250 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7252 location_t combined_loc
= make_location (token
->location
,
7255 postfix_expression
.set_location (combined_loc
);
7258 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7259 idk
= CP_ID_KIND_NONE
;
7261 release_tree_vector (args
);
7267 /* postfix-expression . template [opt] id-expression
7268 postfix-expression . pseudo-destructor-name
7269 postfix-expression -> template [opt] id-expression
7270 postfix-expression -> pseudo-destructor-name */
7272 /* Consume the `.' or `->' operator. */
7273 cp_lexer_consume_token (parser
->lexer
);
7276 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7280 is_member_access
= true;
7284 /* postfix-expression ++ */
7285 /* Consume the `++' token. */
7286 cp_lexer_consume_token (parser
->lexer
);
7287 /* Generate a representation for the complete expression. */
7289 = finish_increment_expr (postfix_expression
,
7290 POSTINCREMENT_EXPR
);
7291 /* Increments may not appear in constant-expressions. */
7292 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7293 postfix_expression
= error_mark_node
;
7294 idk
= CP_ID_KIND_NONE
;
7295 is_member_access
= false;
7298 case CPP_MINUS_MINUS
:
7299 /* postfix-expression -- */
7300 /* Consume the `--' token. */
7301 cp_lexer_consume_token (parser
->lexer
);
7302 /* Generate a representation for the complete expression. */
7304 = finish_increment_expr (postfix_expression
,
7305 POSTDECREMENT_EXPR
);
7306 /* Decrements may not appear in constant-expressions. */
7307 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7308 postfix_expression
= error_mark_node
;
7309 idk
= CP_ID_KIND_NONE
;
7310 is_member_access
= false;
7314 if (pidk_return
!= NULL
)
7315 * pidk_return
= idk
;
7316 if (member_access_only_p
)
7317 return is_member_access
7318 ? postfix_expression
7319 : cp_expr (error_mark_node
);
7321 return postfix_expression
;
7325 /* We should never get here. */
7327 return error_mark_node
;
7330 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7331 by cp_parser_builtin_offsetof. We're looking for
7333 postfix-expression [ expression ]
7334 postfix-expression [ braced-init-list ] (C++11)
7336 FOR_OFFSETOF is set if we're being called in that context, which
7337 changes how we deal with integer constant expressions. */
7340 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7341 tree postfix_expression
,
7345 tree index
= NULL_TREE
;
7346 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7347 bool saved_greater_than_is_operator_p
;
7349 /* Consume the `[' token. */
7350 cp_lexer_consume_token (parser
->lexer
);
7352 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7353 parser
->greater_than_is_operator_p
= true;
7355 /* Parse the index expression. */
7356 /* ??? For offsetof, there is a question of what to allow here. If
7357 offsetof is not being used in an integral constant expression context,
7358 then we *could* get the right answer by computing the value at runtime.
7359 If we are in an integral constant expression context, then we might
7360 could accept any constant expression; hard to say without analysis.
7361 Rather than open the barn door too wide right away, allow only integer
7362 constant expressions here. */
7364 index
= cp_parser_constant_expression (parser
);
7367 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7369 bool expr_nonconst_p
;
7370 cp_lexer_set_source_position (parser
->lexer
);
7371 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7372 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7375 index
= cp_parser_expression (parser
);
7378 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7380 /* Look for the closing `]'. */
7381 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7383 /* Build the ARRAY_REF. */
7384 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7387 /* When not doing offsetof, array references are not permitted in
7388 constant-expressions. */
7390 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7391 postfix_expression
= error_mark_node
;
7393 return postfix_expression
;
7396 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7397 dereference of incomplete type, returns true if error_mark_node should
7398 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7399 and *DEPENDENT_P. */
7402 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7405 /* In a template, be permissive by treating an object expression
7406 of incomplete type as dependent (after a pedwarn). */
7407 diagnostic_t kind
= (processing_template_decl
7408 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7410 switch (TREE_CODE (*postfix_expression
))
7413 case REINTERPRET_CAST_EXPR
:
7414 case CONST_CAST_EXPR
:
7415 case STATIC_CAST_EXPR
:
7416 case DYNAMIC_CAST_EXPR
:
7417 case IMPLICIT_CONV_EXPR
:
7418 case VIEW_CONVERT_EXPR
:
7419 case NON_LVALUE_EXPR
:
7423 /* Don't emit any diagnostic for OVERLOADs. */
7427 /* Avoid clobbering e.g. DECLs. */
7428 if (!EXPR_P (*postfix_expression
))
7433 if (kind
== DK_IGNORED
)
7436 location_t exploc
= location_of (*postfix_expression
);
7437 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7438 if (!MAYBE_CLASS_TYPE_P (*scope
))
7440 if (kind
== DK_ERROR
)
7441 *scope
= *postfix_expression
= error_mark_node
;
7442 else if (processing_template_decl
)
7444 *dependent_p
= true;
7445 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7450 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7451 by cp_parser_builtin_offsetof. We're looking for
7453 postfix-expression . template [opt] id-expression
7454 postfix-expression . pseudo-destructor-name
7455 postfix-expression -> template [opt] id-expression
7456 postfix-expression -> pseudo-destructor-name
7458 FOR_OFFSETOF is set if we're being called in that context. That sorta
7459 limits what of the above we'll actually accept, but nevermind.
7460 TOKEN_TYPE is the "." or "->" token, which will already have been
7461 removed from the stream. */
7464 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7465 enum cpp_ttype token_type
,
7466 cp_expr postfix_expression
,
7467 bool for_offsetof
, cp_id_kind
*idk
,
7468 location_t location
)
7472 bool pseudo_destructor_p
;
7473 tree scope
= NULL_TREE
;
7474 location_t start_loc
= postfix_expression
.get_start ();
7476 /* If this is a `->' operator, dereference the pointer. */
7477 if (token_type
== CPP_DEREF
)
7478 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7479 tf_warning_or_error
);
7480 /* Check to see whether or not the expression is type-dependent and
7481 not the current instantiation. */
7482 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7483 /* The identifier following the `->' or `.' is not qualified. */
7484 parser
->scope
= NULL_TREE
;
7485 parser
->qualifying_scope
= NULL_TREE
;
7486 parser
->object_scope
= NULL_TREE
;
7487 *idk
= CP_ID_KIND_NONE
;
7489 /* Enter the scope corresponding to the type of the object
7490 given by the POSTFIX_EXPRESSION. */
7493 scope
= TREE_TYPE (postfix_expression
);
7494 /* According to the standard, no expression should ever have
7495 reference type. Unfortunately, we do not currently match
7496 the standard in this respect in that our internal representation
7497 of an expression may have reference type even when the standard
7498 says it does not. Therefore, we have to manually obtain the
7499 underlying type here. */
7500 scope
= non_reference (scope
);
7501 /* The type of the POSTFIX_EXPRESSION must be complete. */
7502 /* Unlike the object expression in other contexts, *this is not
7503 required to be of complete type for purposes of class member
7504 access (5.2.5) outside the member function body. */
7505 if (postfix_expression
!= current_class_ref
7506 && scope
!= error_mark_node
7507 && !(processing_template_decl
7508 && current_class_type
7509 && (same_type_ignoring_top_level_qualifiers_p
7510 (scope
, current_class_type
))))
7512 scope
= complete_type (scope
);
7513 if (!COMPLETE_TYPE_P (scope
)
7514 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7516 return error_mark_node
;
7521 /* Let the name lookup machinery know that we are processing a
7522 class member access expression. */
7523 parser
->context
->object_type
= scope
;
7524 /* If something went wrong, we want to be able to discern that case,
7525 as opposed to the case where there was no SCOPE due to the type
7526 of expression being dependent. */
7528 scope
= error_mark_node
;
7529 /* If the SCOPE was erroneous, make the various semantic analysis
7530 functions exit quickly -- and without issuing additional error
7532 if (scope
== error_mark_node
)
7533 postfix_expression
= error_mark_node
;
7538 /* Tell cp_parser_lookup_name that there was an object, even though it's
7540 parser
->context
->object_type
= unknown_type_node
;
7542 /* Assume this expression is not a pseudo-destructor access. */
7543 pseudo_destructor_p
= false;
7545 /* If the SCOPE is a scalar type, then, if this is a valid program,
7546 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7547 is type dependent, it can be pseudo-destructor-name or something else.
7548 Try to parse it as pseudo-destructor-name first. */
7549 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7554 cp_parser_parse_tentatively (parser
);
7555 /* Parse the pseudo-destructor-name. */
7557 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7560 && (cp_parser_error_occurred (parser
)
7561 || !SCALAR_TYPE_P (type
)))
7562 cp_parser_abort_tentative_parse (parser
);
7563 else if (cp_parser_parse_definitely (parser
))
7565 pseudo_destructor_p
= true;
7567 = finish_pseudo_destructor_expr (postfix_expression
,
7572 if (!pseudo_destructor_p
)
7574 /* If the SCOPE is not a scalar type, we are looking at an
7575 ordinary class member access expression, rather than a
7576 pseudo-destructor-name. */
7578 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7579 /* Parse the id-expression. */
7580 name
= (cp_parser_id_expression
7582 cp_parser_optional_template_keyword (parser
),
7583 /*check_dependency_p=*/true,
7585 /*declarator_p=*/false,
7586 /*optional_p=*/false));
7587 /* In general, build a SCOPE_REF if the member name is qualified.
7588 However, if the name was not dependent and has already been
7589 resolved; there is no need to build the SCOPE_REF. For example;
7591 struct X { void f(); };
7592 template <typename T> void f(T* t) { t->X::f(); }
7594 Even though "t" is dependent, "X::f" is not and has been resolved
7595 to a BASELINK; there is no need to include scope information. */
7597 /* But we do need to remember that there was an explicit scope for
7598 virtual function calls. */
7600 *idk
= CP_ID_KIND_QUALIFIED
;
7602 /* If the name is a template-id that names a type, we will get a
7603 TYPE_DECL here. That is invalid code. */
7604 if (TREE_CODE (name
) == TYPE_DECL
)
7606 error_at (token
->location
, "invalid use of %qD", name
);
7607 postfix_expression
= error_mark_node
;
7611 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7613 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7615 error_at (token
->location
, "%<%D::%D%> is not a class member",
7616 parser
->scope
, name
);
7617 postfix_expression
= error_mark_node
;
7620 name
= build_qualified_name (/*type=*/NULL_TREE
,
7624 parser
->scope
= NULL_TREE
;
7625 parser
->qualifying_scope
= NULL_TREE
;
7626 parser
->object_scope
= NULL_TREE
;
7628 if (parser
->scope
&& name
&& BASELINK_P (name
))
7629 adjust_result_of_qualified_name_lookup
7630 (name
, parser
->scope
, scope
);
7632 = finish_class_member_access_expr (postfix_expression
, name
,
7634 tf_warning_or_error
);
7635 /* Build a location e.g.:
7638 where the caret is at the deref token, ranging from
7639 the start of postfix_expression to the end of the access expr. */
7641 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7642 location_t combined_loc
7643 = make_location (input_location
, start_loc
, end_loc
);
7644 protected_set_expr_location (postfix_expression
, combined_loc
);
7648 /* We no longer need to look up names in the scope of the object on
7649 the left-hand side of the `.' or `->' operator. */
7650 parser
->context
->object_type
= NULL_TREE
;
7652 /* Outside of offsetof, these operators may not appear in
7653 constant-expressions. */
7655 && (cp_parser_non_integral_constant_expression
7656 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7657 postfix_expression
= error_mark_node
;
7659 return postfix_expression
;
7662 /* Parse a parenthesized expression-list.
7665 assignment-expression
7666 expression-list, assignment-expression
7671 identifier, expression-list
7673 CAST_P is true if this expression is the target of a cast.
7675 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7678 WRAP_LOCATIONS_P is true if expressions within this list for which
7679 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7680 their source locations.
7682 Returns a vector of trees. Each element is a representation of an
7683 assignment-expression. NULL is returned if the ( and or ) are
7684 missing. An empty, but allocated, vector is returned on no
7685 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7686 if we are parsing an attribute list for an attribute that wants a
7687 plain identifier argument, normal_attr for an attribute that wants
7688 an expression, or non_attr if we aren't parsing an attribute list. If
7689 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7690 not all of the expressions in the list were constant.
7691 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7692 will be written to with the location of the closing parenthesis. If
7693 an error occurs, it may or may not be written to. */
7695 static vec
<tree
, va_gc
> *
7696 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7697 int is_attribute_list
,
7699 bool allow_expansion_p
,
7700 bool *non_constant_p
,
7701 location_t
*close_paren_loc
,
7702 bool wrap_locations_p
)
7704 vec
<tree
, va_gc
> *expression_list
;
7705 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7706 tree identifier
= NULL_TREE
;
7707 bool saved_greater_than_is_operator_p
;
7709 /* Assume all the expressions will be constant. */
7711 *non_constant_p
= false;
7713 matching_parens parens
;
7714 if (!parens
.require_open (parser
))
7717 expression_list
= make_tree_vector ();
7719 /* Within a parenthesized expression, a `>' token is always
7720 the greater-than operator. */
7721 saved_greater_than_is_operator_p
7722 = parser
->greater_than_is_operator_p
;
7723 parser
->greater_than_is_operator_p
= true;
7725 cp_expr
expr (NULL_TREE
);
7727 /* Consume expressions until there are no more. */
7728 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7731 /* At the beginning of attribute lists, check to see if the
7732 next token is an identifier. */
7733 if (is_attribute_list
== id_attr
7734 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7738 /* Consume the identifier. */
7739 token
= cp_lexer_consume_token (parser
->lexer
);
7740 /* Save the identifier. */
7741 identifier
= token
->u
.value
;
7745 bool expr_non_constant_p
;
7747 /* Parse the next assignment-expression. */
7748 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7750 /* A braced-init-list. */
7751 cp_lexer_set_source_position (parser
->lexer
);
7752 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7753 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7754 if (non_constant_p
&& expr_non_constant_p
)
7755 *non_constant_p
= true;
7757 else if (non_constant_p
)
7759 expr
= (cp_parser_constant_expression
7760 (parser
, /*allow_non_constant_p=*/true,
7761 &expr_non_constant_p
));
7762 if (expr_non_constant_p
)
7763 *non_constant_p
= true;
7766 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7770 expr
= instantiate_non_dependent_expr (expr
);
7772 /* If we have an ellipsis, then this is an expression
7774 if (allow_expansion_p
7775 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7777 /* Consume the `...'. */
7778 cp_lexer_consume_token (parser
->lexer
);
7780 /* Build the argument pack. */
7781 expr
= make_pack_expansion (expr
);
7784 if (wrap_locations_p
)
7785 expr
.maybe_add_location_wrapper ();
7787 /* Add it to the list. We add error_mark_node
7788 expressions to the list, so that we can still tell if
7789 the correct form for a parenthesized expression-list
7790 is found. That gives better errors. */
7791 vec_safe_push (expression_list
, expr
.get_value ());
7793 if (expr
== error_mark_node
)
7797 /* After the first item, attribute lists look the same as
7798 expression lists. */
7799 is_attribute_list
= non_attr
;
7802 /* If the next token isn't a `,', then we are done. */
7803 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7806 /* Otherwise, consume the `,' and keep going. */
7807 cp_lexer_consume_token (parser
->lexer
);
7810 if (close_paren_loc
)
7811 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7813 if (!parens
.require_close (parser
))
7818 /* We try and resync to an unnested comma, as that will give the
7819 user better diagnostics. */
7820 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7821 /*recovering=*/true,
7823 /*consume_paren=*/true);
7828 parser
->greater_than_is_operator_p
7829 = saved_greater_than_is_operator_p
;
7834 parser
->greater_than_is_operator_p
7835 = saved_greater_than_is_operator_p
;
7838 vec_safe_insert (expression_list
, 0, identifier
);
7840 return expression_list
;
7843 /* Parse a pseudo-destructor-name.
7845 pseudo-destructor-name:
7846 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7847 :: [opt] nested-name-specifier template template-id :: ~ type-name
7848 :: [opt] nested-name-specifier [opt] ~ type-name
7850 If either of the first two productions is used, sets *SCOPE to the
7851 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7852 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7853 or ERROR_MARK_NODE if the parse fails. */
7856 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7861 bool nested_name_specifier_p
;
7864 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7865 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7866 && !type_dependent_expression_p (object
))
7868 if (cxx_dialect
< cxx14
)
7869 pedwarn (input_location
, 0,
7870 "%<~auto%> only available with "
7871 "-std=c++14 or -std=gnu++14");
7872 cp_lexer_consume_token (parser
->lexer
);
7873 cp_lexer_consume_token (parser
->lexer
);
7875 *type
= TREE_TYPE (object
);
7879 /* Assume that things will not work out. */
7880 *type
= error_mark_node
;
7882 /* Look for the optional `::' operator. */
7883 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7884 /* Look for the optional nested-name-specifier. */
7885 nested_name_specifier_p
7886 = (cp_parser_nested_name_specifier_opt (parser
,
7887 /*typename_keyword_p=*/false,
7888 /*check_dependency_p=*/true,
7890 /*is_declaration=*/false)
7892 /* Now, if we saw a nested-name-specifier, we might be doing the
7893 second production. */
7894 if (nested_name_specifier_p
7895 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7897 /* Consume the `template' keyword. */
7898 cp_lexer_consume_token (parser
->lexer
);
7899 /* Parse the template-id. */
7900 cp_parser_template_id (parser
,
7901 /*template_keyword_p=*/true,
7902 /*check_dependency_p=*/false,
7904 /*is_declaration=*/true);
7905 /* Look for the `::' token. */
7906 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7908 /* If the next token is not a `~', then there might be some
7909 additional qualification. */
7910 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7912 /* At this point, we're looking for "type-name :: ~". The type-name
7913 must not be a class-name, since this is a pseudo-destructor. So,
7914 it must be either an enum-name, or a typedef-name -- both of which
7915 are just identifiers. So, we peek ahead to check that the "::"
7916 and "~" tokens are present; if they are not, then we can avoid
7917 calling type_name. */
7918 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7919 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7920 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7922 cp_parser_error (parser
, "non-scalar type");
7926 /* Look for the type-name. */
7927 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7928 if (*scope
== error_mark_node
)
7931 /* Look for the `::' token. */
7932 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7937 /* Look for the `~'. */
7938 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7940 /* Once we see the ~, this has to be a pseudo-destructor. */
7941 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7942 cp_parser_commit_to_topmost_tentative_parse (parser
);
7944 /* Look for the type-name again. We are not responsible for
7945 checking that it matches the first type-name. */
7946 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7949 /* Parse a unary-expression.
7955 unary-operator cast-expression
7956 sizeof unary-expression
7958 alignof ( type-id ) [C++0x]
7965 __extension__ cast-expression
7966 __alignof__ unary-expression
7967 __alignof__ ( type-id )
7968 alignof unary-expression [C++0x]
7969 __real__ cast-expression
7970 __imag__ cast-expression
7972 sizeof ( type-id ) { initializer-list , [opt] }
7973 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7974 __alignof__ ( type-id ) { initializer-list , [opt] }
7976 ADDRESS_P is true iff the unary-expression is appearing as the
7977 operand of the `&' operator. CAST_P is true if this expression is
7978 the target of a cast.
7980 Returns a representation of the expression. */
7983 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7984 bool address_p
, bool cast_p
, bool decltype_p
)
7987 enum tree_code unary_operator
;
7989 /* Peek at the next token. */
7990 token
= cp_lexer_peek_token (parser
->lexer
);
7991 /* Some keywords give away the kind of expression. */
7992 if (token
->type
== CPP_KEYWORD
)
7994 enum rid keyword
= token
->keyword
;
8003 location_t start_loc
= token
->location
;
8005 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8006 /* Consume the token. */
8007 cp_lexer_consume_token (parser
->lexer
);
8008 /* Parse the operand. */
8009 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8011 if (TYPE_P (operand
))
8012 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
8015 /* ISO C++ defines alignof only with types, not with
8016 expressions. So pedwarn if alignof is used with a non-
8017 type expression. However, __alignof__ is ok. */
8018 if (id_equal (token
->u
.value
, "alignof"))
8019 pedwarn (token
->location
, OPT_Wpedantic
,
8020 "ISO C++ does not allow %<alignof%> "
8023 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8025 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8026 SIZEOF_EXPR with the original operand. */
8027 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8029 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8031 if (!processing_template_decl
&& TYPE_P (operand
))
8033 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8034 build1 (NOP_EXPR
, operand
,
8036 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8039 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8040 TREE_SIDE_EFFECTS (ret
) = 0;
8041 TREE_READONLY (ret
) = 1;
8045 /* Construct a location e.g. :
8048 with start == caret at the start of the "alignof"/"sizeof"
8049 token, with the endpoint at the final closing paren. */
8050 location_t finish_loc
8051 = cp_lexer_previous_token (parser
->lexer
)->location
;
8052 location_t compound_loc
8053 = make_location (start_loc
, start_loc
, finish_loc
);
8055 cp_expr
ret_expr (ret
);
8056 ret_expr
.set_location (compound_loc
);
8057 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8062 return cp_parser_new_expression (parser
);
8065 return cp_parser_delete_expression (parser
);
8069 /* The saved value of the PEDANTIC flag. */
8073 /* Save away the PEDANTIC flag. */
8074 cp_parser_extension_opt (parser
, &saved_pedantic
);
8075 /* Parse the cast-expression. */
8076 expr
= cp_parser_simple_cast_expression (parser
);
8077 /* Restore the PEDANTIC flag. */
8078 pedantic
= saved_pedantic
;
8088 /* Consume the `__real__' or `__imag__' token. */
8089 cp_lexer_consume_token (parser
->lexer
);
8090 /* Parse the cast-expression. */
8091 expression
= cp_parser_simple_cast_expression (parser
);
8092 /* Create the complete representation. */
8093 return build_x_unary_op (token
->location
,
8094 (keyword
== RID_REALPART
8095 ? REALPART_EXPR
: IMAGPART_EXPR
),
8097 tf_warning_or_error
);
8101 case RID_TRANSACTION_ATOMIC
:
8102 case RID_TRANSACTION_RELAXED
:
8103 return cp_parser_transaction_expression (parser
, keyword
);
8108 const char *saved_message
;
8109 bool saved_integral_constant_expression_p
;
8110 bool saved_non_integral_constant_expression_p
;
8111 bool saved_greater_than_is_operator_p
;
8113 location_t start_loc
= token
->location
;
8115 cp_lexer_consume_token (parser
->lexer
);
8116 matching_parens parens
;
8117 parens
.require_open (parser
);
8119 saved_message
= parser
->type_definition_forbidden_message
;
8120 parser
->type_definition_forbidden_message
8121 = G_("types may not be defined in %<noexcept%> expressions");
8123 saved_integral_constant_expression_p
8124 = parser
->integral_constant_expression_p
;
8125 saved_non_integral_constant_expression_p
8126 = parser
->non_integral_constant_expression_p
;
8127 parser
->integral_constant_expression_p
= false;
8129 saved_greater_than_is_operator_p
8130 = parser
->greater_than_is_operator_p
;
8131 parser
->greater_than_is_operator_p
= true;
8133 ++cp_unevaluated_operand
;
8134 ++c_inhibit_evaluation_warnings
;
8135 ++cp_noexcept_operand
;
8136 expr
= cp_parser_expression (parser
);
8137 --cp_noexcept_operand
;
8138 --c_inhibit_evaluation_warnings
;
8139 --cp_unevaluated_operand
;
8141 parser
->greater_than_is_operator_p
8142 = saved_greater_than_is_operator_p
;
8144 parser
->integral_constant_expression_p
8145 = saved_integral_constant_expression_p
;
8146 parser
->non_integral_constant_expression_p
8147 = saved_non_integral_constant_expression_p
;
8149 parser
->type_definition_forbidden_message
= saved_message
;
8151 location_t finish_loc
8152 = cp_lexer_peek_token (parser
->lexer
)->location
;
8153 parens
.require_close (parser
);
8155 /* Construct a location of the form:
8158 with start == caret, finishing at the close-paren. */
8159 location_t noexcept_loc
8160 = make_location (start_loc
, start_loc
, finish_loc
);
8162 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8171 /* Look for the `:: new' and `:: delete', which also signal the
8172 beginning of a new-expression, or delete-expression,
8173 respectively. If the next token is `::', then it might be one of
8175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8179 /* See if the token after the `::' is one of the keywords in
8180 which we're interested. */
8181 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8182 /* If it's `new', we have a new-expression. */
8183 if (keyword
== RID_NEW
)
8184 return cp_parser_new_expression (parser
);
8185 /* Similarly, for `delete'. */
8186 else if (keyword
== RID_DELETE
)
8187 return cp_parser_delete_expression (parser
);
8190 /* Look for a unary operator. */
8191 unary_operator
= cp_parser_unary_operator (token
);
8192 /* The `++' and `--' operators can be handled similarly, even though
8193 they are not technically unary-operators in the grammar. */
8194 if (unary_operator
== ERROR_MARK
)
8196 if (token
->type
== CPP_PLUS_PLUS
)
8197 unary_operator
= PREINCREMENT_EXPR
;
8198 else if (token
->type
== CPP_MINUS_MINUS
)
8199 unary_operator
= PREDECREMENT_EXPR
;
8200 /* Handle the GNU address-of-label extension. */
8201 else if (cp_parser_allow_gnu_extensions_p (parser
)
8202 && token
->type
== CPP_AND_AND
)
8206 location_t start_loc
= token
->location
;
8208 /* Consume the '&&' token. */
8209 cp_lexer_consume_token (parser
->lexer
);
8210 /* Look for the identifier. */
8211 location_t finish_loc
8212 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8213 identifier
= cp_parser_identifier (parser
);
8214 /* Construct a location of the form:
8217 with caret==start at the "&&", finish at the end of the label. */
8218 location_t combined_loc
8219 = make_location (start_loc
, start_loc
, finish_loc
);
8220 /* Create an expression representing the address. */
8221 expression
= finish_label_address_expr (identifier
, combined_loc
);
8222 if (cp_parser_non_integral_constant_expression (parser
,
8224 expression
= error_mark_node
;
8228 if (unary_operator
!= ERROR_MARK
)
8230 cp_expr cast_expression
;
8231 cp_expr expression
= error_mark_node
;
8232 non_integral_constant non_constant_p
= NIC_NONE
;
8233 location_t loc
= token
->location
;
8234 tsubst_flags_t complain
= complain_flags (decltype_p
);
8236 /* Consume the operator token. */
8237 token
= cp_lexer_consume_token (parser
->lexer
);
8238 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8240 /* Parse the cast-expression. */
8242 = cp_parser_cast_expression (parser
,
8243 unary_operator
== ADDR_EXPR
,
8249 OP_TOKEN CAST_EXPRESSION
8250 ^~~~~~~~~~~~~~~~~~~~~~~~~
8251 with start==caret at the operator token, and
8252 extending to the end of the cast_expression. */
8253 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8255 /* Now, build an appropriate representation. */
8256 switch (unary_operator
)
8259 non_constant_p
= NIC_STAR
;
8260 expression
= build_x_indirect_ref (loc
, cast_expression
,
8263 /* TODO: build_x_indirect_ref does not always honor the
8264 location, so ensure it is set. */
8265 expression
.set_location (loc
);
8269 non_constant_p
= NIC_ADDR
;
8272 expression
= build_x_unary_op (loc
, unary_operator
,
8275 /* TODO: build_x_unary_op does not always honor the location,
8276 so ensure it is set. */
8277 expression
.set_location (loc
);
8280 case PREINCREMENT_EXPR
:
8281 case PREDECREMENT_EXPR
:
8282 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8283 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8286 /* Immediately fold negation of a constant, unless the constant is 0
8287 (since -0 == 0) or it would overflow. */
8288 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8289 && CONSTANT_CLASS_P (cast_expression
)
8290 && !integer_zerop (cast_expression
)
8291 && !TREE_OVERFLOW (cast_expression
))
8293 tree folded
= fold_build1 (unary_operator
,
8294 TREE_TYPE (cast_expression
),
8296 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8298 expression
= cp_expr (folded
, loc
);
8303 case UNARY_PLUS_EXPR
:
8304 case TRUTH_NOT_EXPR
:
8305 expression
= finish_unary_op_expr (loc
, unary_operator
,
8306 cast_expression
, complain
);
8313 if (non_constant_p
!= NIC_NONE
8314 && cp_parser_non_integral_constant_expression (parser
,
8316 expression
= error_mark_node
;
8321 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8322 /*member_access_only_p=*/false,
8327 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8328 unary-operator, the corresponding tree code is returned. */
8330 static enum tree_code
8331 cp_parser_unary_operator (cp_token
* token
)
8333 switch (token
->type
)
8336 return INDIRECT_REF
;
8342 return UNARY_PLUS_EXPR
;
8348 return TRUTH_NOT_EXPR
;
8351 return BIT_NOT_EXPR
;
8358 /* Parse a new-expression.
8361 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8362 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8364 Returns a representation of the expression. */
8367 cp_parser_new_expression (cp_parser
* parser
)
8369 bool global_scope_p
;
8370 vec
<tree
, va_gc
> *placement
;
8372 vec
<tree
, va_gc
> *initializer
;
8373 tree nelts
= NULL_TREE
;
8376 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8378 /* Look for the optional `::' operator. */
8380 = (cp_parser_global_scope_opt (parser
,
8381 /*current_scope_valid_p=*/false)
8383 /* Look for the `new' operator. */
8384 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8385 /* There's no easy way to tell a new-placement from the
8386 `( type-id )' construct. */
8387 cp_parser_parse_tentatively (parser
);
8388 /* Look for a new-placement. */
8389 placement
= cp_parser_new_placement (parser
);
8390 /* If that didn't work out, there's no new-placement. */
8391 if (!cp_parser_parse_definitely (parser
))
8393 if (placement
!= NULL
)
8394 release_tree_vector (placement
);
8398 /* If the next token is a `(', then we have a parenthesized
8400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8403 const char *saved_message
= parser
->type_definition_forbidden_message
;
8405 /* Consume the `('. */
8406 matching_parens parens
;
8407 parens
.consume_open (parser
);
8409 /* Parse the type-id. */
8410 parser
->type_definition_forbidden_message
8411 = G_("types may not be defined in a new-expression");
8413 type_id_in_expr_sentinel
s (parser
);
8414 type
= cp_parser_type_id (parser
);
8416 parser
->type_definition_forbidden_message
= saved_message
;
8418 /* Look for the closing `)'. */
8419 parens
.require_close (parser
);
8420 token
= cp_lexer_peek_token (parser
->lexer
);
8421 /* There should not be a direct-new-declarator in this production,
8422 but GCC used to allowed this, so we check and emit a sensible error
8423 message for this case. */
8424 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8426 error_at (token
->location
,
8427 "array bound forbidden after parenthesized type-id");
8428 inform (token
->location
,
8429 "try removing the parentheses around the type-id");
8430 cp_parser_direct_new_declarator (parser
);
8433 /* Otherwise, there must be a new-type-id. */
8435 type
= cp_parser_new_type_id (parser
, &nelts
);
8437 /* If the next token is a `(' or '{', then we have a new-initializer. */
8438 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8439 if (token
->type
== CPP_OPEN_PAREN
8440 || token
->type
== CPP_OPEN_BRACE
)
8441 initializer
= cp_parser_new_initializer (parser
);
8445 /* A new-expression may not appear in an integral constant
8447 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8448 ret
= error_mark_node
;
8449 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8450 of a new-type-id or type-id of a new-expression, the new-expression shall
8451 contain a new-initializer of the form ( assignment-expression )".
8452 Additionally, consistently with the spirit of DR 1467, we want to accept
8453 'new auto { 2 }' too. */
8454 else if ((ret
= type_uses_auto (type
))
8455 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8456 && (vec_safe_length (initializer
) != 1
8457 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8458 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8460 error_at (token
->location
,
8461 "initialization of new-expression for type %<auto%> "
8462 "requires exactly one element");
8463 ret
= error_mark_node
;
8467 /* Construct a location e.g.:
8470 with caret == start at the start of the "new" token, and the end
8471 at the end of the final token we consumed. */
8472 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8473 location_t end_loc
= get_finish (end_tok
->location
);
8474 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8476 /* Create a representation of the new-expression. */
8477 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8478 tf_warning_or_error
);
8479 protected_set_expr_location (ret
, combined_loc
);
8482 if (placement
!= NULL
)
8483 release_tree_vector (placement
);
8484 if (initializer
!= NULL
)
8485 release_tree_vector (initializer
);
8490 /* Parse a new-placement.
8495 Returns the same representation as for an expression-list. */
8497 static vec
<tree
, va_gc
> *
8498 cp_parser_new_placement (cp_parser
* parser
)
8500 vec
<tree
, va_gc
> *expression_list
;
8502 /* Parse the expression-list. */
8503 expression_list
= (cp_parser_parenthesized_expression_list
8504 (parser
, non_attr
, /*cast_p=*/false,
8505 /*allow_expansion_p=*/true,
8506 /*non_constant_p=*/NULL
));
8508 if (expression_list
&& expression_list
->is_empty ())
8509 error ("expected expression-list or type-id");
8511 return expression_list
;
8514 /* Parse a new-type-id.
8517 type-specifier-seq new-declarator [opt]
8519 Returns the TYPE allocated. If the new-type-id indicates an array
8520 type, *NELTS is set to the number of elements in the last array
8521 bound; the TYPE will not include the last array bound. */
8524 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8526 cp_decl_specifier_seq type_specifier_seq
;
8527 cp_declarator
*new_declarator
;
8528 cp_declarator
*declarator
;
8529 cp_declarator
*outer_declarator
;
8530 const char *saved_message
;
8532 /* The type-specifier sequence must not contain type definitions.
8533 (It cannot contain declarations of new types either, but if they
8534 are not definitions we will catch that because they are not
8536 saved_message
= parser
->type_definition_forbidden_message
;
8537 parser
->type_definition_forbidden_message
8538 = G_("types may not be defined in a new-type-id");
8539 /* Parse the type-specifier-seq. */
8540 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8541 /*is_trailing_return=*/false,
8542 &type_specifier_seq
);
8543 /* Restore the old message. */
8544 parser
->type_definition_forbidden_message
= saved_message
;
8546 if (type_specifier_seq
.type
== error_mark_node
)
8547 return error_mark_node
;
8549 /* Parse the new-declarator. */
8550 new_declarator
= cp_parser_new_declarator_opt (parser
);
8552 /* Determine the number of elements in the last array dimension, if
8555 /* Skip down to the last array dimension. */
8556 declarator
= new_declarator
;
8557 outer_declarator
= NULL
;
8558 while (declarator
&& (declarator
->kind
== cdk_pointer
8559 || declarator
->kind
== cdk_ptrmem
))
8561 outer_declarator
= declarator
;
8562 declarator
= declarator
->declarator
;
8565 && declarator
->kind
== cdk_array
8566 && declarator
->declarator
8567 && declarator
->declarator
->kind
== cdk_array
)
8569 outer_declarator
= declarator
;
8570 declarator
= declarator
->declarator
;
8573 if (declarator
&& declarator
->kind
== cdk_array
)
8575 *nelts
= declarator
->u
.array
.bounds
;
8576 if (*nelts
== error_mark_node
)
8577 *nelts
= integer_one_node
;
8579 if (outer_declarator
)
8580 outer_declarator
->declarator
= declarator
->declarator
;
8582 new_declarator
= NULL
;
8585 return groktypename (&type_specifier_seq
, new_declarator
, false);
8588 /* Parse an (optional) new-declarator.
8591 ptr-operator new-declarator [opt]
8592 direct-new-declarator
8594 Returns the declarator. */
8596 static cp_declarator
*
8597 cp_parser_new_declarator_opt (cp_parser
* parser
)
8599 enum tree_code code
;
8600 tree type
, std_attributes
= NULL_TREE
;
8601 cp_cv_quals cv_quals
;
8603 /* We don't know if there's a ptr-operator next, or not. */
8604 cp_parser_parse_tentatively (parser
);
8605 /* Look for a ptr-operator. */
8606 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8607 /* If that worked, look for more new-declarators. */
8608 if (cp_parser_parse_definitely (parser
))
8610 cp_declarator
*declarator
;
8612 /* Parse another optional declarator. */
8613 declarator
= cp_parser_new_declarator_opt (parser
);
8615 declarator
= cp_parser_make_indirect_declarator
8616 (code
, type
, cv_quals
, declarator
, std_attributes
);
8621 /* If the next token is a `[', there is a direct-new-declarator. */
8622 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8623 return cp_parser_direct_new_declarator (parser
);
8628 /* Parse a direct-new-declarator.
8630 direct-new-declarator:
8632 direct-new-declarator [constant-expression]
8636 static cp_declarator
*
8637 cp_parser_direct_new_declarator (cp_parser
* parser
)
8639 cp_declarator
*declarator
= NULL
;
8646 /* Look for the opening `['. */
8647 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8649 token
= cp_lexer_peek_token (parser
->lexer
);
8650 expression
= cp_parser_expression (parser
);
8651 /* The standard requires that the expression have integral
8652 type. DR 74 adds enumeration types. We believe that the
8653 real intent is that these expressions be handled like the
8654 expression in a `switch' condition, which also allows
8655 classes with a single conversion to integral or
8656 enumeration type. */
8657 if (!processing_template_decl
)
8660 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8665 error_at (token
->location
,
8666 "expression in new-declarator must have integral "
8667 "or enumeration type");
8668 expression
= error_mark_node
;
8672 /* Look for the closing `]'. */
8673 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8675 /* Add this bound to the declarator. */
8676 declarator
= make_array_declarator (declarator
, expression
);
8678 /* If the next token is not a `[', then there are no more
8680 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8687 /* Parse a new-initializer.
8690 ( expression-list [opt] )
8693 Returns a representation of the expression-list. */
8695 static vec
<tree
, va_gc
> *
8696 cp_parser_new_initializer (cp_parser
* parser
)
8698 vec
<tree
, va_gc
> *expression_list
;
8700 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8703 bool expr_non_constant_p
;
8704 cp_lexer_set_source_position (parser
->lexer
);
8705 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8706 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8707 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8708 expression_list
= make_tree_vector_single (t
);
8711 expression_list
= (cp_parser_parenthesized_expression_list
8712 (parser
, non_attr
, /*cast_p=*/false,
8713 /*allow_expansion_p=*/true,
8714 /*non_constant_p=*/NULL
));
8716 return expression_list
;
8719 /* Parse a delete-expression.
8722 :: [opt] delete cast-expression
8723 :: [opt] delete [ ] cast-expression
8725 Returns a representation of the expression. */
8728 cp_parser_delete_expression (cp_parser
* parser
)
8730 bool global_scope_p
;
8734 /* Look for the optional `::' operator. */
8736 = (cp_parser_global_scope_opt (parser
,
8737 /*current_scope_valid_p=*/false)
8739 /* Look for the `delete' keyword. */
8740 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8741 /* See if the array syntax is in use. */
8742 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8744 /* Consume the `[' token. */
8745 cp_lexer_consume_token (parser
->lexer
);
8746 /* Look for the `]' token. */
8747 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8748 /* Remember that this is the `[]' construct. */
8754 /* Parse the cast-expression. */
8755 expression
= cp_parser_simple_cast_expression (parser
);
8757 /* A delete-expression may not appear in an integral constant
8759 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8760 return error_mark_node
;
8762 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8763 tf_warning_or_error
);
8766 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8767 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8771 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8773 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8774 switch (token
->type
)
8780 case CPP_CLOSE_SQUARE
:
8781 case CPP_CLOSE_PAREN
:
8782 case CPP_CLOSE_BRACE
:
8783 case CPP_OPEN_BRACE
:
8787 case CPP_DEREF_STAR
:
8795 case CPP_GREATER_EQ
:
8816 case CPP_OPEN_PAREN
:
8817 /* In ((type ()) () the last () isn't a valid cast-expression,
8818 so the whole must be parsed as postfix-expression. */
8819 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8822 case CPP_OPEN_SQUARE
:
8823 /* '[' may start a primary-expression in obj-c++ and in C++11,
8824 as a lambda-expression, eg, '(void)[]{}'. */
8825 if (cxx_dialect
>= cxx11
)
8827 return c_dialect_objc ();
8830 case CPP_MINUS_MINUS
:
8831 /* '++' and '--' may or may not start a cast-expression:
8833 struct T { void operator++(int); };
8834 void f() { (T())++; }
8847 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8848 in the order: const_cast, static_cast, reinterpret_cast.
8850 Don't suggest dynamic_cast.
8852 Return the first legal cast kind found, or NULL otherwise. */
8855 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8859 /* Reuse the parser logic by attempting to build the various kinds of
8860 cast, with "complain" disabled.
8861 Identify the first such cast that is valid. */
8863 /* Don't attempt to run such logic within template processing. */
8864 if (processing_template_decl
)
8867 /* First try const_cast. */
8868 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8869 if (trial
!= error_mark_node
)
8870 return "const_cast";
8872 /* If that fails, try static_cast. */
8873 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8874 if (trial
!= error_mark_node
)
8875 return "static_cast";
8877 /* Finally, try reinterpret_cast. */
8878 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8879 if (trial
!= error_mark_node
)
8880 return "reinterpret_cast";
8882 /* No such cast possible. */
8886 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8887 suggesting how to convert a C-style cast of the form:
8891 to a C++-style cast.
8893 The primary range of RICHLOC is asssumed to be that of the original
8894 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8895 of the parens in the C-style cast. */
8898 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8899 location_t close_paren_loc
, tree orig_expr
,
8902 /* This function is non-trivial, so bail out now if the warning isn't
8903 going to be emitted. */
8904 if (!warn_old_style_cast
)
8907 /* Try to find a legal C++ cast, trying them in order:
8908 const_cast, static_cast, reinterpret_cast. */
8909 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8910 if (!cast_suggestion
)
8913 /* Replace the open paren with "CAST_SUGGESTION<". */
8915 pp_printf (&pp
, "%s<", cast_suggestion
);
8916 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8918 /* Replace the close paren with "> (". */
8919 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8921 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8922 rich_loc
->add_fixit_insert_after (")");
8926 /* Parse a cast-expression.
8930 ( type-id ) cast-expression
8932 ADDRESS_P is true iff the unary-expression is appearing as the
8933 operand of the `&' operator. CAST_P is true if this expression is
8934 the target of a cast.
8936 Returns a representation of the expression. */
8939 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8940 bool decltype_p
, cp_id_kind
* pidk
)
8942 /* If it's a `(', then we might be looking at a cast. */
8943 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8945 tree type
= NULL_TREE
;
8946 cp_expr
expr (NULL_TREE
);
8947 int cast_expression
= 0;
8948 const char *saved_message
;
8950 /* There's no way to know yet whether or not this is a cast.
8951 For example, `(int (3))' is a unary-expression, while `(int)
8952 3' is a cast. So, we resort to parsing tentatively. */
8953 cp_parser_parse_tentatively (parser
);
8954 /* Types may not be defined in a cast. */
8955 saved_message
= parser
->type_definition_forbidden_message
;
8956 parser
->type_definition_forbidden_message
8957 = G_("types may not be defined in casts");
8958 /* Consume the `('. */
8959 matching_parens parens
;
8960 cp_token
*open_paren
= parens
.consume_open (parser
);
8961 location_t open_paren_loc
= open_paren
->location
;
8962 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8964 /* A very tricky bit is that `(struct S) { 3 }' is a
8965 compound-literal (which we permit in C++ as an extension).
8966 But, that construct is not a cast-expression -- it is a
8967 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8968 is legal; if the compound-literal were a cast-expression,
8969 you'd need an extra set of parentheses.) But, if we parse
8970 the type-id, and it happens to be a class-specifier, then we
8971 will commit to the parse at that point, because we cannot
8972 undo the action that is done when creating a new class. So,
8973 then we cannot back up and do a postfix-expression.
8975 Another tricky case is the following (c++/29234):
8977 struct S { void operator () (); };
8984 As a type-id we parse the parenthesized S()() as a function
8985 returning a function, groktypename complains and we cannot
8986 back up in this case either.
8988 Therefore, we scan ahead to the closing `)', and check to see
8989 if the tokens after the `)' can start a cast-expression. Otherwise
8990 we are dealing with an unary-expression, a postfix-expression
8993 Yet another tricky case, in C++11, is the following (c++/54891):
8997 The issue is that usually, besides the case of lambda-expressions,
8998 the parenthesized type-id cannot be followed by '[', and, eg, we
8999 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9000 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9001 we don't commit, we try a cast-expression, then an unary-expression.
9003 Save tokens so that we can put them back. */
9004 cp_lexer_save_tokens (parser
->lexer
);
9006 /* We may be looking at a cast-expression. */
9007 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9008 /*consume_paren=*/true))
9010 = cp_parser_tokens_start_cast_expression (parser
);
9012 /* Roll back the tokens we skipped. */
9013 cp_lexer_rollback_tokens (parser
->lexer
);
9014 /* If we aren't looking at a cast-expression, simulate an error so
9015 that the call to cp_parser_error_occurred below returns true. */
9016 if (!cast_expression
)
9017 cp_parser_simulate_error (parser
);
9020 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9021 parser
->in_type_id_in_expr_p
= true;
9022 /* Look for the type-id. */
9023 type
= cp_parser_type_id (parser
);
9024 /* Look for the closing `)'. */
9025 cp_token
*close_paren
= parens
.require_close (parser
);
9027 close_paren_loc
= close_paren
->location
;
9028 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9031 /* Restore the saved message. */
9032 parser
->type_definition_forbidden_message
= saved_message
;
9034 /* At this point this can only be either a cast or a
9035 parenthesized ctor such as `(T ())' that looks like a cast to
9036 function returning T. */
9037 if (!cp_parser_error_occurred (parser
))
9039 /* Only commit if the cast-expression doesn't start with
9040 '++', '--', or '[' in C++11. */
9041 if (cast_expression
> 0)
9042 cp_parser_commit_to_topmost_tentative_parse (parser
);
9044 expr
= cp_parser_cast_expression (parser
,
9045 /*address_p=*/false,
9047 /*decltype_p=*/false,
9050 if (cp_parser_parse_definitely (parser
))
9052 /* Warn about old-style casts, if so requested. */
9053 if (warn_old_style_cast
9054 && !in_system_header_at (input_location
)
9055 && !VOID_TYPE_P (type
)
9056 && current_lang_name
!= lang_name_c
)
9058 gcc_rich_location
rich_loc (input_location
);
9059 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9061 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9062 "use of old-style cast to %q#T", type
);
9065 /* Only type conversions to integral or enumeration types
9066 can be used in constant-expressions. */
9067 if (!cast_valid_in_integral_constant_expression_p (type
)
9068 && cp_parser_non_integral_constant_expression (parser
,
9070 return error_mark_node
;
9072 /* Perform the cast. */
9076 with start==caret at the open paren, extending to the
9078 location_t cast_loc
= make_location (open_paren_loc
,
9080 expr
.get_finish ());
9081 expr
= build_c_cast (cast_loc
, type
, expr
);
9086 cp_parser_abort_tentative_parse (parser
);
9089 /* If we get here, then it's not a cast, so it must be a
9090 unary-expression. */
9091 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9092 cast_p
, decltype_p
);
9095 /* Parse a binary expression of the general form:
9099 pm-expression .* cast-expression
9100 pm-expression ->* cast-expression
9102 multiplicative-expression:
9104 multiplicative-expression * pm-expression
9105 multiplicative-expression / pm-expression
9106 multiplicative-expression % pm-expression
9108 additive-expression:
9109 multiplicative-expression
9110 additive-expression + multiplicative-expression
9111 additive-expression - multiplicative-expression
9115 shift-expression << additive-expression
9116 shift-expression >> additive-expression
9118 relational-expression:
9120 relational-expression < shift-expression
9121 relational-expression > shift-expression
9122 relational-expression <= shift-expression
9123 relational-expression >= shift-expression
9127 relational-expression:
9128 relational-expression <? shift-expression
9129 relational-expression >? shift-expression
9131 equality-expression:
9132 relational-expression
9133 equality-expression == relational-expression
9134 equality-expression != relational-expression
9138 and-expression & equality-expression
9140 exclusive-or-expression:
9142 exclusive-or-expression ^ and-expression
9144 inclusive-or-expression:
9145 exclusive-or-expression
9146 inclusive-or-expression | exclusive-or-expression
9148 logical-and-expression:
9149 inclusive-or-expression
9150 logical-and-expression && inclusive-or-expression
9152 logical-or-expression:
9153 logical-and-expression
9154 logical-or-expression || logical-and-expression
9156 All these are implemented with a single function like:
9159 simple-cast-expression
9160 binary-expression <token> binary-expression
9162 CAST_P is true if this expression is the target of a cast.
9164 The binops_by_token map is used to get the tree codes for each <token> type.
9165 binary-expressions are associated according to a precedence table. */
9167 #define TOKEN_PRECEDENCE(token) \
9168 (((token->type == CPP_GREATER \
9169 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9170 && !parser->greater_than_is_operator_p) \
9171 ? PREC_NOT_OPERATOR \
9172 : binops_by_token[token->type].prec)
9175 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9176 bool no_toplevel_fold_p
,
9178 enum cp_parser_prec prec
,
9181 cp_parser_expression_stack stack
;
9182 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9183 cp_parser_expression_stack_entry current
;
9186 enum tree_code rhs_type
;
9187 enum cp_parser_prec new_prec
, lookahead_prec
;
9190 /* Parse the first expression. */
9191 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9192 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9193 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9194 cast_p
, decltype_p
, pidk
);
9195 current
.prec
= prec
;
9197 if (cp_parser_error_occurred (parser
))
9198 return error_mark_node
;
9202 /* Get an operator token. */
9203 token
= cp_lexer_peek_token (parser
->lexer
);
9205 if (warn_cxx11_compat
9206 && token
->type
== CPP_RSHIFT
9207 && !parser
->greater_than_is_operator_p
)
9209 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9210 "%<>>%> operator is treated"
9211 " as two right angle brackets in C++11"))
9212 inform (token
->location
,
9213 "suggest parentheses around %<>>%> expression");
9216 new_prec
= TOKEN_PRECEDENCE (token
);
9217 if (new_prec
!= PREC_NOT_OPERATOR
9218 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9219 /* This is a fold-expression; handle it later. */
9220 new_prec
= PREC_NOT_OPERATOR
;
9222 /* Popping an entry off the stack means we completed a subexpression:
9223 - either we found a token which is not an operator (`>' where it is not
9224 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9225 will happen repeatedly;
9226 - or, we found an operator which has lower priority. This is the case
9227 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9229 if (new_prec
<= current
.prec
)
9238 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9239 current
.loc
= token
->location
;
9241 /* We used the operator token. */
9242 cp_lexer_consume_token (parser
->lexer
);
9244 /* For "false && x" or "true || x", x will never be executed;
9245 disable warnings while evaluating it. */
9246 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9247 c_inhibit_evaluation_warnings
+=
9248 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9249 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9250 c_inhibit_evaluation_warnings
+=
9251 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9253 /* Extract another operand. It may be the RHS of this expression
9254 or the LHS of a new, higher priority expression. */
9255 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9256 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9257 rhs
= cp_parser_simple_cast_expression (parser
);
9259 /* Get another operator token. Look up its precedence to avoid
9260 building a useless (immediately popped) stack entry for common
9261 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9262 token
= cp_lexer_peek_token (parser
->lexer
);
9263 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9264 if (lookahead_prec
!= PREC_NOT_OPERATOR
9265 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9266 lookahead_prec
= PREC_NOT_OPERATOR
;
9267 if (lookahead_prec
> new_prec
)
9269 /* ... and prepare to parse the RHS of the new, higher priority
9270 expression. Since precedence levels on the stack are
9271 monotonically increasing, we do not have to care about
9276 current
.lhs_type
= rhs_type
;
9277 current
.prec
= new_prec
;
9278 new_prec
= lookahead_prec
;
9282 lookahead_prec
= new_prec
;
9283 /* If the stack is not empty, we have parsed into LHS the right side
9284 (`4' in the example above) of an expression we had suspended.
9285 We can use the information on the stack to recover the LHS (`3')
9286 from the stack together with the tree code (`MULT_EXPR'), and
9287 the precedence of the higher level subexpression
9288 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9289 which will be used to actually build the additive expression. */
9291 rhs_type
= current
.lhs_type
;
9296 /* Undo the disabling of warnings done above. */
9297 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9298 c_inhibit_evaluation_warnings
-=
9299 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9300 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9301 c_inhibit_evaluation_warnings
-=
9302 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9304 if (warn_logical_not_paren
9305 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9306 && current
.lhs_type
== TRUTH_NOT_EXPR
9307 /* Avoid warning for !!x == y. */
9308 && (TREE_CODE (current
.lhs
) != NE_EXPR
9309 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9310 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9311 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9312 /* Avoid warning for !b == y where b is boolean. */
9313 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9314 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9316 /* Avoid warning for !!b == y where b is boolean. */
9317 && (!DECL_P (current
.lhs
)
9318 || TREE_TYPE (current
.lhs
) == NULL_TREE
9319 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9320 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9321 current
.lhs
, maybe_constant_value (rhs
));
9325 location_t combined_loc
= make_location (current
.loc
,
9326 current
.lhs
.get_start (),
9329 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9330 ERROR_MARK for everything that is not a binary expression.
9331 This makes warn_about_parentheses miss some warnings that
9332 involve unary operators. For unary expressions we should
9333 pass the correct tree_code unless the unary expression was
9334 surrounded by parentheses.
9336 if (no_toplevel_fold_p
9337 && lookahead_prec
<= current
.prec
9340 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9341 current
.lhs
= error_mark_node
;
9345 = build_min (current
.tree_type
,
9346 TREE_CODE_CLASS (current
.tree_type
)
9348 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9349 current
.lhs
.get_value (), rhs
.get_value ());
9350 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9355 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9356 current
.lhs
, current
.lhs_type
,
9357 rhs
, rhs_type
, &overload
,
9358 complain_flags (decltype_p
));
9359 /* TODO: build_x_binary_op doesn't always honor the location. */
9360 current
.lhs
.set_location (combined_loc
);
9362 current
.lhs_type
= current
.tree_type
;
9364 /* If the binary operator required the use of an overloaded operator,
9365 then this expression cannot be an integral constant-expression.
9366 An overloaded operator can be used even if both operands are
9367 otherwise permissible in an integral constant-expression if at
9368 least one of the operands is of enumeration type. */
9371 && cp_parser_non_integral_constant_expression (parser
,
9373 return error_mark_node
;
9380 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9381 bool no_toplevel_fold_p
,
9382 enum cp_parser_prec prec
,
9385 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9386 /*decltype*/false, prec
, pidk
);
9389 /* Parse the `? expression : assignment-expression' part of a
9390 conditional-expression. The LOGICAL_OR_EXPR is the
9391 logical-or-expression that started the conditional-expression.
9392 Returns a representation of the entire conditional-expression.
9394 This routine is used by cp_parser_assignment_expression.
9396 ? expression : assignment-expression
9400 ? : assignment-expression */
9403 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9405 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9406 cp_expr assignment_expr
;
9407 struct cp_token
*token
;
9408 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9410 /* Consume the `?' token. */
9411 cp_lexer_consume_token (parser
->lexer
);
9412 token
= cp_lexer_peek_token (parser
->lexer
);
9413 if (cp_parser_allow_gnu_extensions_p (parser
)
9414 && token
->type
== CPP_COLON
)
9416 pedwarn (token
->location
, OPT_Wpedantic
,
9417 "ISO C++ does not allow ?: with omitted middle operand");
9418 /* Implicit true clause. */
9420 c_inhibit_evaluation_warnings
+=
9421 folded_logical_or_expr
== truthvalue_true_node
;
9422 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9426 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9427 parser
->colon_corrects_to_scope_p
= false;
9428 /* Parse the expression. */
9429 c_inhibit_evaluation_warnings
+=
9430 folded_logical_or_expr
== truthvalue_false_node
;
9431 expr
= cp_parser_expression (parser
);
9432 c_inhibit_evaluation_warnings
+=
9433 ((folded_logical_or_expr
== truthvalue_true_node
)
9434 - (folded_logical_or_expr
== truthvalue_false_node
));
9435 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9438 /* The next token should be a `:'. */
9439 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9440 /* Parse the assignment-expression. */
9441 assignment_expr
= cp_parser_assignment_expression (parser
);
9442 c_inhibit_evaluation_warnings
-=
9443 folded_logical_or_expr
== truthvalue_true_node
;
9446 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9447 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9448 with the caret at the "?", ranging from the start of
9449 the logical_or_expr to the end of the assignment_expr. */
9450 loc
= make_location (loc
,
9451 logical_or_expr
.get_start (),
9452 assignment_expr
.get_finish ());
9454 /* Build the conditional-expression. */
9455 return build_x_conditional_expr (loc
, logical_or_expr
,
9458 tf_warning_or_error
);
9461 /* Parse an assignment-expression.
9463 assignment-expression:
9464 conditional-expression
9465 logical-or-expression assignment-operator assignment_expression
9468 CAST_P is true if this expression is the target of a cast.
9469 DECLTYPE_P is true if this expression is the operand of decltype.
9471 Returns a representation for the expression. */
9474 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9475 bool cast_p
, bool decltype_p
)
9479 /* If the next token is the `throw' keyword, then we're looking at
9480 a throw-expression. */
9481 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9482 expr
= cp_parser_throw_expression (parser
);
9483 /* Otherwise, it must be that we are looking at a
9484 logical-or-expression. */
9487 /* Parse the binary expressions (logical-or-expression). */
9488 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9490 PREC_NOT_OPERATOR
, pidk
);
9491 /* If the next token is a `?' then we're actually looking at a
9492 conditional-expression. */
9493 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9494 return cp_parser_question_colon_clause (parser
, expr
);
9497 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9499 /* If it's an assignment-operator, we're using the second
9501 enum tree_code assignment_operator
9502 = cp_parser_assignment_operator_opt (parser
);
9503 if (assignment_operator
!= ERROR_MARK
)
9505 bool non_constant_p
;
9507 /* Parse the right-hand side of the assignment. */
9508 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9511 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9512 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9514 /* An assignment may not appear in a
9515 constant-expression. */
9516 if (cp_parser_non_integral_constant_expression (parser
,
9518 return error_mark_node
;
9519 /* Build the assignment expression. Its default
9523 is the location of the '=' token as the
9524 caret, ranging from the start of the lhs to the
9526 loc
= make_location (loc
,
9529 expr
= build_x_modify_expr (loc
, expr
,
9530 assignment_operator
,
9532 complain_flags (decltype_p
));
9533 /* TODO: build_x_modify_expr doesn't honor the location,
9534 so we must set it here. */
9535 expr
.set_location (loc
);
9543 /* Parse an (optional) assignment-operator.
9545 assignment-operator: one of
9546 = *= /= %= += -= >>= <<= &= ^= |=
9550 assignment-operator: one of
9553 If the next token is an assignment operator, the corresponding tree
9554 code is returned, and the token is consumed. For example, for
9555 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9556 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9557 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9558 operator, ERROR_MARK is returned. */
9560 static enum tree_code
9561 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9566 /* Peek at the next token. */
9567 token
= cp_lexer_peek_token (parser
->lexer
);
9569 switch (token
->type
)
9580 op
= TRUNC_DIV_EXPR
;
9584 op
= TRUNC_MOD_EXPR
;
9616 /* Nothing else is an assignment operator. */
9620 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9621 if (op
!= ERROR_MARK
9622 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9625 /* If it was an assignment operator, consume it. */
9626 if (op
!= ERROR_MARK
)
9627 cp_lexer_consume_token (parser
->lexer
);
9632 /* Parse an expression.
9635 assignment-expression
9636 expression , assignment-expression
9638 CAST_P is true if this expression is the target of a cast.
9639 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9640 except possibly parenthesized or on the RHS of a comma (N3276).
9642 Returns a representation of the expression. */
9645 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9646 bool cast_p
, bool decltype_p
)
9648 cp_expr expression
= NULL_TREE
;
9649 location_t loc
= UNKNOWN_LOCATION
;
9653 cp_expr assignment_expression
;
9655 /* Parse the next assignment-expression. */
9656 assignment_expression
9657 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9659 /* We don't create a temporary for a call that is the immediate operand
9660 of decltype or on the RHS of a comma. But when we see a comma, we
9661 need to create a temporary for a call on the LHS. */
9662 if (decltype_p
&& !processing_template_decl
9663 && TREE_CODE (assignment_expression
) == CALL_EXPR
9664 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9665 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9666 assignment_expression
9667 = build_cplus_new (TREE_TYPE (assignment_expression
),
9668 assignment_expression
, tf_warning_or_error
);
9670 /* If this is the first assignment-expression, we can just
9673 expression
= assignment_expression
;
9676 /* Create a location with caret at the comma, ranging
9677 from the start of the LHS to the end of the RHS. */
9678 loc
= make_location (loc
,
9679 expression
.get_start (),
9680 assignment_expression
.get_finish ());
9681 expression
= build_x_compound_expr (loc
, expression
,
9682 assignment_expression
,
9683 complain_flags (decltype_p
));
9684 expression
.set_location (loc
);
9686 /* If the next token is not a comma, or we're in a fold-expression, then
9687 we are done with the expression. */
9688 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9689 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9691 /* Consume the `,'. */
9692 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9693 cp_lexer_consume_token (parser
->lexer
);
9694 /* A comma operator cannot appear in a constant-expression. */
9695 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9696 expression
= error_mark_node
;
9702 /* Parse a constant-expression.
9704 constant-expression:
9705 conditional-expression
9707 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9708 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9709 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9710 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9711 only parse a conditional-expression, otherwise parse an
9712 assignment-expression. See below for rationale. */
9715 cp_parser_constant_expression (cp_parser
* parser
,
9716 bool allow_non_constant_p
,
9717 bool *non_constant_p
,
9720 bool saved_integral_constant_expression_p
;
9721 bool saved_allow_non_integral_constant_expression_p
;
9722 bool saved_non_integral_constant_expression_p
;
9725 /* It might seem that we could simply parse the
9726 conditional-expression, and then check to see if it were
9727 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9728 one that the compiler can figure out is constant, possibly after
9729 doing some simplifications or optimizations. The standard has a
9730 precise definition of constant-expression, and we must honor
9731 that, even though it is somewhat more restrictive.
9737 is not a legal declaration, because `(2, 3)' is not a
9738 constant-expression. The `,' operator is forbidden in a
9739 constant-expression. However, GCC's constant-folding machinery
9740 will fold this operation to an INTEGER_CST for `3'. */
9742 /* Save the old settings. */
9743 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9744 saved_allow_non_integral_constant_expression_p
9745 = parser
->allow_non_integral_constant_expression_p
;
9746 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9747 /* We are now parsing a constant-expression. */
9748 parser
->integral_constant_expression_p
= true;
9749 parser
->allow_non_integral_constant_expression_p
9750 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9751 parser
->non_integral_constant_expression_p
= false;
9752 /* Although the grammar says "conditional-expression", when not STRICT_P,
9753 we parse an "assignment-expression", which also permits
9754 "throw-expression" and the use of assignment operators. In the case
9755 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9756 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9757 actually essential that we look for an assignment-expression.
9758 For example, cp_parser_initializer_clauses uses this function to
9759 determine whether a particular assignment-expression is in fact
9763 /* Parse the binary expressions (logical-or-expression). */
9764 expression
= cp_parser_binary_expression (parser
, false, false, false,
9765 PREC_NOT_OPERATOR
, NULL
);
9766 /* If the next token is a `?' then we're actually looking at
9767 a conditional-expression; otherwise we're done. */
9768 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9769 expression
= cp_parser_question_colon_clause (parser
, expression
);
9772 expression
= cp_parser_assignment_expression (parser
);
9773 /* Restore the old settings. */
9774 parser
->integral_constant_expression_p
9775 = saved_integral_constant_expression_p
;
9776 parser
->allow_non_integral_constant_expression_p
9777 = saved_allow_non_integral_constant_expression_p
;
9778 if (cxx_dialect
>= cxx11
)
9780 /* Require an rvalue constant expression here; that's what our
9781 callers expect. Reference constant expressions are handled
9782 separately in e.g. cp_parser_template_argument. */
9783 tree decay
= expression
;
9784 if (TREE_TYPE (expression
)
9785 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9786 decay
= build_address (expression
);
9787 bool is_const
= potential_rvalue_constant_expression (decay
);
9788 parser
->non_integral_constant_expression_p
= !is_const
;
9789 if (!is_const
&& !allow_non_constant_p
)
9790 require_potential_rvalue_constant_expression (decay
);
9792 if (allow_non_constant_p
)
9793 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9794 parser
->non_integral_constant_expression_p
9795 = saved_non_integral_constant_expression_p
;
9800 /* Parse __builtin_offsetof.
9802 offsetof-expression:
9803 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9805 offsetof-member-designator:
9807 | offsetof-member-designator "." id-expression
9808 | offsetof-member-designator "[" expression "]"
9809 | offsetof-member-designator "->" id-expression */
9812 cp_parser_builtin_offsetof (cp_parser
*parser
)
9814 int save_ice_p
, save_non_ice_p
;
9819 location_t finish_loc
;
9821 /* We're about to accept non-integral-constant things, but will
9822 definitely yield an integral constant expression. Save and
9823 restore these values around our local parsing. */
9824 save_ice_p
= parser
->integral_constant_expression_p
;
9825 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9827 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9829 /* Consume the "__builtin_offsetof" token. */
9830 cp_lexer_consume_token (parser
->lexer
);
9831 /* Consume the opening `('. */
9832 matching_parens parens
;
9833 parens
.require_open (parser
);
9834 /* Parse the type-id. */
9835 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9836 type
= cp_parser_type_id (parser
);
9837 /* Look for the `,'. */
9838 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9839 token
= cp_lexer_peek_token (parser
->lexer
);
9841 /* Build the (type *)null that begins the traditional offsetof macro. */
9843 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9844 tf_warning_or_error
);
9846 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9847 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9848 true, &dummy
, token
->location
);
9851 token
= cp_lexer_peek_token (parser
->lexer
);
9852 switch (token
->type
)
9854 case CPP_OPEN_SQUARE
:
9855 /* offsetof-member-designator "[" expression "]" */
9856 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9861 /* offsetof-member-designator "->" identifier */
9862 expr
= grok_array_decl (token
->location
, expr
,
9863 integer_zero_node
, false);
9867 /* offsetof-member-designator "." identifier */
9868 cp_lexer_consume_token (parser
->lexer
);
9869 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9874 case CPP_CLOSE_PAREN
:
9875 /* Consume the ")" token. */
9876 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9877 cp_lexer_consume_token (parser
->lexer
);
9881 /* Error. We know the following require will fail, but
9882 that gives the proper error message. */
9883 parens
.require_close (parser
);
9884 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9885 expr
= error_mark_node
;
9891 /* Make a location of the form:
9892 __builtin_offsetof (struct s, f)
9893 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9894 with caret at the type-id, ranging from the start of the
9895 "_builtin_offsetof" token to the close paren. */
9896 loc
= make_location (loc
, start_loc
, finish_loc
);
9897 /* The result will be an INTEGER_CST, so we need to explicitly
9898 preserve the location. */
9899 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9902 parser
->integral_constant_expression_p
= save_ice_p
;
9903 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9905 expr
= expr
.maybe_add_location_wrapper ();
9909 /* Parse a trait expression.
9911 Returns a representation of the expression, the underlying type
9912 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9915 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9918 tree type1
, type2
= NULL_TREE
;
9919 bool binary
= false;
9920 bool variadic
= false;
9924 case RID_HAS_NOTHROW_ASSIGN
:
9925 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9927 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9928 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9930 case RID_HAS_NOTHROW_COPY
:
9931 kind
= CPTK_HAS_NOTHROW_COPY
;
9933 case RID_HAS_TRIVIAL_ASSIGN
:
9934 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9936 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9937 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9939 case RID_HAS_TRIVIAL_COPY
:
9940 kind
= CPTK_HAS_TRIVIAL_COPY
;
9942 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9943 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9945 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9946 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9948 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9949 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9951 case RID_IS_ABSTRACT
:
9952 kind
= CPTK_IS_ABSTRACT
;
9954 case RID_IS_AGGREGATE
:
9955 kind
= CPTK_IS_AGGREGATE
;
9957 case RID_IS_BASE_OF
:
9958 kind
= CPTK_IS_BASE_OF
;
9962 kind
= CPTK_IS_CLASS
;
9965 kind
= CPTK_IS_EMPTY
;
9968 kind
= CPTK_IS_ENUM
;
9971 kind
= CPTK_IS_FINAL
;
9973 case RID_IS_LITERAL_TYPE
:
9974 kind
= CPTK_IS_LITERAL_TYPE
;
9979 case RID_IS_POLYMORPHIC
:
9980 kind
= CPTK_IS_POLYMORPHIC
;
9982 case RID_IS_SAME_AS
:
9983 kind
= CPTK_IS_SAME_AS
;
9986 case RID_IS_STD_LAYOUT
:
9987 kind
= CPTK_IS_STD_LAYOUT
;
9989 case RID_IS_TRIVIAL
:
9990 kind
= CPTK_IS_TRIVIAL
;
9992 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9993 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9996 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9997 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10000 case RID_IS_TRIVIALLY_COPYABLE
:
10001 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10004 kind
= CPTK_IS_UNION
;
10006 case RID_UNDERLYING_TYPE
:
10007 kind
= CPTK_UNDERLYING_TYPE
;
10012 case RID_DIRECT_BASES
:
10013 kind
= CPTK_DIRECT_BASES
;
10015 case RID_IS_ASSIGNABLE
:
10016 kind
= CPTK_IS_ASSIGNABLE
;
10019 case RID_IS_CONSTRUCTIBLE
:
10020 kind
= CPTK_IS_CONSTRUCTIBLE
;
10024 gcc_unreachable ();
10027 /* Get location of initial token. */
10028 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10030 /* Consume the token. */
10031 cp_lexer_consume_token (parser
->lexer
);
10033 matching_parens parens
;
10034 parens
.require_open (parser
);
10037 type_id_in_expr_sentinel
s (parser
);
10038 type1
= cp_parser_type_id (parser
);
10041 if (type1
== error_mark_node
)
10042 return error_mark_node
;
10046 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10049 type_id_in_expr_sentinel
s (parser
);
10050 type2
= cp_parser_type_id (parser
);
10053 if (type2
== error_mark_node
)
10054 return error_mark_node
;
10058 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10060 cp_lexer_consume_token (parser
->lexer
);
10061 tree elt
= cp_parser_type_id (parser
);
10062 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10064 cp_lexer_consume_token (parser
->lexer
);
10065 elt
= make_pack_expansion (elt
);
10067 if (elt
== error_mark_node
)
10068 return error_mark_node
;
10069 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10073 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10074 parens
.require_close (parser
);
10076 /* Construct a location of the form:
10077 __is_trivially_copyable(_Tp)
10078 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10079 with start == caret, finishing at the close-paren. */
10080 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10082 /* Complete the trait expression, which may mean either processing
10083 the trait expr now or saving it for template instantiation. */
10086 case CPTK_UNDERLYING_TYPE
:
10087 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10089 return cp_expr (finish_bases (type1
, false), trait_loc
);
10090 case CPTK_DIRECT_BASES
:
10091 return cp_expr (finish_bases (type1
, true), trait_loc
);
10093 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10097 /* Parse a lambda expression.
10100 lambda-introducer lambda-declarator [opt] compound-statement
10102 Returns a representation of the expression. */
10105 cp_parser_lambda_expression (cp_parser
* parser
)
10107 tree lambda_expr
= build_lambda_expr ();
10110 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10111 cp_token_position start
= 0;
10113 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10115 if (cp_unevaluated_operand
)
10117 if (!token
->error_reported
)
10119 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10120 "lambda-expression in unevaluated context");
10121 token
->error_reported
= true;
10125 else if (parser
->in_template_argument_list_p
)
10127 if (!token
->error_reported
)
10129 error_at (token
->location
, "lambda-expression in template-argument");
10130 token
->error_reported
= true;
10135 /* We may be in the middle of deferred access check. Disable
10137 push_deferring_access_checks (dk_no_deferred
);
10139 cp_parser_lambda_introducer (parser
, lambda_expr
);
10141 type
= begin_lambda_type (lambda_expr
);
10142 if (type
== error_mark_node
)
10143 return error_mark_node
;
10145 record_lambda_scope (lambda_expr
);
10147 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10148 determine_visibility (TYPE_NAME (type
));
10150 /* Now that we've started the type, add the capture fields for any
10151 explicit captures. */
10152 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10155 /* Inside the class, surrounding template-parameter-lists do not apply. */
10156 unsigned int saved_num_template_parameter_lists
10157 = parser
->num_template_parameter_lists
;
10158 unsigned char in_statement
= parser
->in_statement
;
10159 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10160 bool fully_implicit_function_template_p
10161 = parser
->fully_implicit_function_template_p
;
10162 tree implicit_template_parms
= parser
->implicit_template_parms
;
10163 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10164 bool auto_is_implicit_function_template_parm_p
10165 = parser
->auto_is_implicit_function_template_parm_p
;
10167 parser
->num_template_parameter_lists
= 0;
10168 parser
->in_statement
= 0;
10169 parser
->in_switch_statement_p
= false;
10170 parser
->fully_implicit_function_template_p
= false;
10171 parser
->implicit_template_parms
= 0;
10172 parser
->implicit_template_scope
= 0;
10173 parser
->auto_is_implicit_function_template_parm_p
= false;
10175 /* By virtue of defining a local class, a lambda expression has access to
10176 the private variables of enclosing classes. */
10178 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10180 if (ok
&& cp_parser_error_occurred (parser
))
10185 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10186 && cp_parser_start_tentative_firewall (parser
))
10188 cp_parser_lambda_body (parser
, lambda_expr
);
10190 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10192 if (cp_parser_skip_to_closing_brace (parser
))
10193 cp_lexer_consume_token (parser
->lexer
);
10196 /* The capture list was built up in reverse order; fix that now. */
10197 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10198 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10201 maybe_add_lambda_conv_op (type
);
10203 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10205 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10206 parser
->in_statement
= in_statement
;
10207 parser
->in_switch_statement_p
= in_switch_statement_p
;
10208 parser
->fully_implicit_function_template_p
10209 = fully_implicit_function_template_p
;
10210 parser
->implicit_template_parms
= implicit_template_parms
;
10211 parser
->implicit_template_scope
= implicit_template_scope
;
10212 parser
->auto_is_implicit_function_template_parm_p
10213 = auto_is_implicit_function_template_parm_p
;
10216 /* This field is only used during parsing of the lambda. */
10217 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10219 /* This lambda shouldn't have any proxies left at this point. */
10220 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10221 /* And now that we're done, push proxies for an enclosing lambda. */
10222 insert_pending_capture_proxies ();
10225 lambda_expr
= build_lambda_object (lambda_expr
);
10227 lambda_expr
= error_mark_node
;
10229 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10231 pop_deferring_access_checks ();
10233 return lambda_expr
;
10236 /* Parse the beginning of a lambda expression.
10239 [ lambda-capture [opt] ]
10241 LAMBDA_EXPR is the current representation of the lambda expression. */
10244 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10246 /* Need commas after the first capture. */
10249 /* Eat the leading `['. */
10250 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10252 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10253 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10254 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10255 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10256 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10257 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10259 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10261 cp_lexer_consume_token (parser
->lexer
);
10265 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10267 cp_token
* capture_token
;
10269 tree capture_init_expr
;
10270 cp_id_kind idk
= CP_ID_KIND_NONE
;
10271 bool explicit_init_p
= false;
10273 enum capture_kind_type
10278 enum capture_kind_type capture_kind
= BY_COPY
;
10280 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10282 error ("expected end of capture-list");
10289 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10291 /* Possibly capture `this'. */
10292 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10294 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10295 if (cxx_dialect
< cxx2a
10296 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10297 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10298 "with by-copy capture default");
10299 cp_lexer_consume_token (parser
->lexer
);
10300 add_capture (lambda_expr
,
10301 /*id=*/this_identifier
,
10302 /*initializer=*/finish_this_expr (),
10303 /*by_reference_p=*/true,
10308 /* Possibly capture `*this'. */
10309 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10310 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10312 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10313 if (cxx_dialect
< cxx17
)
10314 pedwarn (loc
, 0, "%<*this%> capture only available with "
10315 "-std=c++17 or -std=gnu++17");
10316 cp_lexer_consume_token (parser
->lexer
);
10317 cp_lexer_consume_token (parser
->lexer
);
10318 add_capture (lambda_expr
,
10319 /*id=*/this_identifier
,
10320 /*initializer=*/finish_this_expr (),
10321 /*by_reference_p=*/false,
10326 /* Remember whether we want to capture as a reference or not. */
10327 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10329 capture_kind
= BY_REFERENCE
;
10330 cp_lexer_consume_token (parser
->lexer
);
10333 /* Get the identifier. */
10334 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10335 capture_id
= cp_parser_identifier (parser
);
10337 if (capture_id
== error_mark_node
)
10338 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10339 delimiters, but I modified this to stop on unnested ']' as well. It
10340 was already changed to stop on unnested '}', so the
10341 "closing_parenthesis" name is no more misleading with my change. */
10343 cp_parser_skip_to_closing_parenthesis (parser
,
10344 /*recovering=*/true,
10346 /*consume_paren=*/true);
10350 /* Find the initializer for this capture. */
10351 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10352 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10353 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10355 bool direct
, non_constant
;
10356 /* An explicit initializer exists. */
10357 if (cxx_dialect
< cxx14
)
10358 pedwarn (input_location
, 0,
10359 "lambda capture initializers "
10360 "only available with -std=c++14 or -std=gnu++14");
10361 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10363 explicit_init_p
= true;
10364 if (capture_init_expr
== NULL_TREE
)
10366 error ("empty initializer for lambda init-capture");
10367 capture_init_expr
= error_mark_node
;
10372 const char* error_msg
;
10374 /* Turn the identifier into an id-expression. */
10376 = cp_parser_lookup_name_simple (parser
, capture_id
,
10377 capture_token
->location
);
10379 if (capture_init_expr
== error_mark_node
)
10381 unqualified_name_lookup_error (capture_id
);
10384 else if (!VAR_P (capture_init_expr
)
10385 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10387 error_at (capture_token
->location
,
10388 "capture of non-variable %qE",
10389 capture_init_expr
);
10390 if (DECL_P (capture_init_expr
))
10391 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10392 "%q#D declared here", capture_init_expr
);
10395 if (VAR_P (capture_init_expr
)
10396 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10398 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10399 "%qD with non-automatic storage duration",
10400 capture_init_expr
))
10401 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10402 "%q#D declared here", capture_init_expr
);
10407 = finish_id_expression
10412 /*integral_constant_expression_p=*/false,
10413 /*allow_non_integral_constant_expression_p=*/false,
10414 /*non_integral_constant_expression_p=*/NULL
,
10415 /*template_p=*/false,
10417 /*address_p=*/false,
10418 /*template_arg_p=*/false,
10420 capture_token
->location
);
10422 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10424 cp_lexer_consume_token (parser
->lexer
);
10425 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10429 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10430 && !explicit_init_p
)
10432 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10433 && capture_kind
== BY_COPY
)
10434 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10435 "of %qD redundant with by-copy capture default",
10437 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10438 && capture_kind
== BY_REFERENCE
)
10439 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10440 "capture of %qD redundant with by-reference capture "
10441 "default", capture_id
);
10444 add_capture (lambda_expr
,
10447 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10450 /* If there is any qualification still in effect, clear it
10451 now; we will be starting fresh with the next capture. */
10452 parser
->scope
= NULL_TREE
;
10453 parser
->qualifying_scope
= NULL_TREE
;
10454 parser
->object_scope
= NULL_TREE
;
10457 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10460 /* Parse the (optional) middle of a lambda expression.
10463 < template-parameter-list [opt] >
10464 ( parameter-declaration-clause [opt] )
10465 attribute-specifier [opt]
10466 decl-specifier-seq [opt]
10467 exception-specification [opt]
10468 lambda-return-type-clause [opt]
10470 LAMBDA_EXPR is the current representation of the lambda expression. */
10473 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10475 /* 5.1.1.4 of the standard says:
10476 If a lambda-expression does not include a lambda-declarator, it is as if
10477 the lambda-declarator were ().
10478 This means an empty parameter list, no attributes, and no exception
10480 tree param_list
= void_list_node
;
10481 tree attributes
= NULL_TREE
;
10482 tree exception_spec
= NULL_TREE
;
10483 tree template_param_list
= NULL_TREE
;
10484 tree tx_qual
= NULL_TREE
;
10485 tree return_type
= NULL_TREE
;
10486 cp_decl_specifier_seq lambda_specs
;
10487 clear_decl_specs (&lambda_specs
);
10489 /* The template-parameter-list is optional, but must begin with
10490 an opening angle if present. */
10491 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10493 if (cxx_dialect
< cxx14
)
10494 pedwarn (parser
->lexer
->next_token
->location
, 0,
10495 "lambda templates are only available with "
10496 "-std=c++14 or -std=gnu++14");
10497 else if (cxx_dialect
< cxx2a
)
10498 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10499 "lambda templates are only available with "
10500 "-std=c++2a or -std=gnu++2a");
10502 cp_lexer_consume_token (parser
->lexer
);
10504 template_param_list
= cp_parser_template_parameter_list (parser
);
10506 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10508 /* We just processed one more parameter list. */
10509 ++parser
->num_template_parameter_lists
;
10512 /* The parameter-declaration-clause is optional (unless
10513 template-parameter-list was given), but must begin with an
10514 opening parenthesis if present. */
10515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10517 matching_parens parens
;
10518 parens
.consume_open (parser
);
10520 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10522 /* Parse parameters. */
10523 param_list
= cp_parser_parameter_declaration_clause (parser
);
10525 /* Default arguments shall not be specified in the
10526 parameter-declaration-clause of a lambda-declarator. */
10527 if (cxx_dialect
< cxx14
)
10528 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10529 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10530 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10531 "default argument specified for lambda parameter");
10533 parens
.require_close (parser
);
10535 attributes
= cp_parser_attributes_opt (parser
);
10537 /* In the decl-specifier-seq of the lambda-declarator, each
10538 decl-specifier shall either be mutable or constexpr. */
10539 int declares_class_or_enum
;
10540 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10541 cp_parser_decl_specifier_seq (parser
,
10542 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10543 &lambda_specs
, &declares_class_or_enum
);
10544 if (lambda_specs
.storage_class
== sc_mutable
)
10546 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10547 if (lambda_specs
.conflicting_specifiers_p
)
10548 error_at (lambda_specs
.locations
[ds_storage_class
],
10549 "duplicate %<mutable%>");
10552 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10554 /* Parse optional exception specification. */
10555 exception_spec
= cp_parser_exception_specification_opt (parser
);
10557 /* Parse optional trailing return type. */
10558 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10560 cp_lexer_consume_token (parser
->lexer
);
10561 return_type
= cp_parser_trailing_type_id (parser
);
10564 /* The function parameters must be in scope all the way until after the
10565 trailing-return-type in case of decltype. */
10566 pop_bindings_and_leave_scope ();
10568 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10569 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10571 /* Create the function call operator.
10573 Messing with declarators like this is no uglier than building up the
10574 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10577 cp_decl_specifier_seq return_type_specs
;
10578 cp_declarator
* declarator
;
10583 clear_decl_specs (&return_type_specs
);
10585 return_type_specs
.type
= return_type
;
10587 /* Maybe we will deduce the return type later. */
10588 return_type_specs
.type
= make_auto ();
10590 if (lambda_specs
.locations
[ds_constexpr
])
10592 if (cxx_dialect
>= cxx17
)
10593 return_type_specs
.locations
[ds_constexpr
]
10594 = lambda_specs
.locations
[ds_constexpr
];
10596 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10597 "lambda only available with -std=c++17 or -std=gnu++17");
10600 p
= obstack_alloc (&declarator_obstack
, 0);
10602 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10604 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10605 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10606 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10607 VIRT_SPEC_UNSPECIFIED
,
10611 /*late_return_type=*/NULL_TREE
,
10612 /*requires_clause*/NULL_TREE
);
10613 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10615 fco
= grokmethod (&return_type_specs
,
10618 if (fco
!= error_mark_node
)
10620 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10621 DECL_ARTIFICIAL (fco
) = 1;
10622 /* Give the object parameter a different name. */
10623 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10625 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10627 if (template_param_list
)
10629 fco
= finish_member_template_decl (fco
);
10630 finish_template_decl (template_param_list
);
10631 --parser
->num_template_parameter_lists
;
10633 else if (parser
->fully_implicit_function_template_p
)
10634 fco
= finish_fully_implicit_template (parser
, fco
);
10636 finish_member_declaration (fco
);
10638 obstack_free (&declarator_obstack
, p
);
10640 return (fco
!= error_mark_node
);
10644 /* Parse the body of a lambda expression, which is simply
10648 but which requires special handling.
10649 LAMBDA_EXPR is the current representation of the lambda expression. */
10652 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10654 bool nested
= (current_function_decl
!= NULL_TREE
);
10655 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10656 bool in_function_body
= parser
->in_function_body
;
10659 push_function_context ();
10661 /* Still increment function_depth so that we don't GC in the
10662 middle of an expression. */
10665 vec
<tree
> omp_privatization_save
;
10666 save_omp_privatization_clauses (omp_privatization_save
);
10667 /* Clear this in case we're in the middle of a default argument. */
10668 parser
->local_variables_forbidden_p
= false;
10669 parser
->in_function_body
= true;
10672 local_specialization_stack
s (lss_copy
);
10673 tree fco
= lambda_function (lambda_expr
);
10674 tree body
= start_lambda_function (fco
, lambda_expr
);
10675 matching_braces braces
;
10677 if (braces
.require_open (parser
))
10679 tree compound_stmt
= begin_compound_stmt (0);
10681 /* Originally C++11 required us to peek for 'return expr'; and
10682 process it specially here to deduce the return type. N3638
10683 removed the need for that. */
10685 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10686 cp_parser_label_declaration (parser
);
10687 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10688 braces
.require_close (parser
);
10690 finish_compound_stmt (compound_stmt
);
10693 finish_lambda_function (body
);
10696 restore_omp_privatization_clauses (omp_privatization_save
);
10697 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10698 parser
->in_function_body
= in_function_body
;
10700 pop_function_context();
10705 /* Statements [gram.stmt.stmt] */
10707 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10710 add_debug_begin_stmt (location_t loc
)
10712 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10714 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10715 /* A concept is never expanded normally. */
10718 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10719 SET_EXPR_LOCATION (stmt
, loc
);
10723 /* Parse a statement.
10727 expression-statement
10729 selection-statement
10730 iteration-statement
10732 declaration-statement
10739 attribute-specifier-seq (opt) expression-statement
10740 attribute-specifier-seq (opt) compound-statement
10741 attribute-specifier-seq (opt) selection-statement
10742 attribute-specifier-seq (opt) iteration-statement
10743 attribute-specifier-seq (opt) jump-statement
10744 declaration-statement
10745 attribute-specifier-seq (opt) try-block
10748 expression-statement
10756 IN_COMPOUND is true when the statement is nested inside a
10757 cp_parser_compound_statement; this matters for certain pragmas.
10759 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10760 is a (possibly labeled) if statement which is not enclosed in braces
10761 and has an else clause. This is used to implement -Wparentheses.
10763 CHAIN is a vector of if-else-if conditions. */
10766 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10767 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10768 location_t
*loc_after_labels
)
10770 tree statement
, std_attrs
= NULL_TREE
;
10772 location_t statement_location
, attrs_location
;
10777 /* There is no statement yet. */
10778 statement
= NULL_TREE
;
10780 saved_token_sentinel
saved_tokens (parser
->lexer
);
10781 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10782 if (c_dialect_objc ())
10783 /* In obj-c++, seeing '[[' might be the either the beginning of
10784 c++11 attributes, or a nested objc-message-expression. So
10785 let's parse the c++11 attributes tentatively. */
10786 cp_parser_parse_tentatively (parser
);
10787 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10788 if (c_dialect_objc ())
10790 if (!cp_parser_parse_definitely (parser
))
10791 std_attrs
= NULL_TREE
;
10794 /* Peek at the next token. */
10795 token
= cp_lexer_peek_token (parser
->lexer
);
10796 /* Remember the location of the first token in the statement. */
10797 statement_location
= token
->location
;
10798 add_debug_begin_stmt (statement_location
);
10799 /* If this is a keyword, then that will often determine what kind of
10800 statement we have. */
10801 if (token
->type
== CPP_KEYWORD
)
10803 enum rid keyword
= token
->keyword
;
10809 /* Looks like a labeled-statement with a case label.
10810 Parse the label, and then use tail recursion to parse
10812 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10813 in_compound
= false;
10818 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10824 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10831 statement
= cp_parser_jump_statement (parser
);
10834 /* Objective-C++ exception-handling constructs. */
10837 case RID_AT_FINALLY
:
10838 case RID_AT_SYNCHRONIZED
:
10840 statement
= cp_parser_objc_statement (parser
);
10844 statement
= cp_parser_try_block (parser
);
10847 case RID_NAMESPACE
:
10848 /* This must be a namespace alias definition. */
10849 cp_parser_declaration_statement (parser
);
10852 case RID_TRANSACTION_ATOMIC
:
10853 case RID_TRANSACTION_RELAXED
:
10854 case RID_SYNCHRONIZED
:
10855 case RID_ATOMIC_NOEXCEPT
:
10856 case RID_ATOMIC_CANCEL
:
10857 statement
= cp_parser_transaction (parser
, token
);
10859 case RID_TRANSACTION_CANCEL
:
10860 statement
= cp_parser_transaction_cancel (parser
);
10864 /* It might be a keyword like `int' that can start a
10865 declaration-statement. */
10869 else if (token
->type
== CPP_NAME
)
10871 /* If the next token is a `:', then we are looking at a
10872 labeled-statement. */
10873 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10874 if (token
->type
== CPP_COLON
)
10876 /* Looks like a labeled-statement with an ordinary label.
10877 Parse the label, and then use tail recursion to parse
10880 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10881 in_compound
= false;
10885 /* Anything that starts with a `{' must be a compound-statement. */
10886 else if (token
->type
== CPP_OPEN_BRACE
)
10887 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10888 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10889 a statement all its own. */
10890 else if (token
->type
== CPP_PRAGMA
)
10892 /* Only certain OpenMP pragmas are attached to statements, and thus
10893 are considered statements themselves. All others are not. In
10894 the context of a compound, accept the pragma as a "statement" and
10895 return so that we can check for a close brace. Otherwise we
10896 require a real statement and must go back and read one. */
10898 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10899 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10903 else if (token
->type
== CPP_EOF
)
10905 cp_parser_error (parser
, "expected statement");
10909 /* Everything else must be a declaration-statement or an
10910 expression-statement. Try for the declaration-statement
10911 first, unless we are looking at a `;', in which case we know that
10912 we have an expression-statement. */
10915 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10917 if (std_attrs
!= NULL_TREE
)
10919 /* Attributes should be parsed as part of the the
10920 declaration, so let's un-parse them. */
10921 saved_tokens
.rollback();
10922 std_attrs
= NULL_TREE
;
10925 cp_parser_parse_tentatively (parser
);
10926 /* Try to parse the declaration-statement. */
10927 cp_parser_declaration_statement (parser
);
10928 /* If that worked, we're done. */
10929 if (cp_parser_parse_definitely (parser
))
10932 /* All preceding labels have been parsed at this point. */
10933 if (loc_after_labels
!= NULL
)
10934 *loc_after_labels
= statement_location
;
10936 /* Look for an expression-statement instead. */
10937 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10939 /* Handle [[fallthrough]];. */
10940 if (attribute_fallthrough_p (std_attrs
))
10942 /* The next token after the fallthrough attribute is ';'. */
10943 if (statement
== NULL_TREE
)
10945 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10946 statement
= build_call_expr_internal_loc (statement_location
,
10948 void_type_node
, 0);
10949 finish_expr_stmt (statement
);
10952 warning_at (statement_location
, OPT_Wattributes
,
10953 "%<fallthrough%> attribute not followed by %<;%>");
10954 std_attrs
= NULL_TREE
;
10958 /* Set the line number for the statement. */
10959 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10960 SET_EXPR_LOCATION (statement
, statement_location
);
10962 /* Allow "[[fallthrough]];", but warn otherwise. */
10963 if (std_attrs
!= NULL_TREE
)
10964 warning_at (attrs_location
,
10966 "attributes at the beginning of statement are ignored");
10969 /* Append ATTR to attribute list ATTRS. */
10972 attr_chainon (tree attrs
, tree attr
)
10974 if (attrs
== error_mark_node
)
10975 return error_mark_node
;
10976 if (attr
== error_mark_node
)
10977 return error_mark_node
;
10978 return chainon (attrs
, attr
);
10981 /* Parse the label for a labeled-statement, i.e.
10984 case constant-expression :
10988 case constant-expression ... constant-expression : statement
10990 When a label is parsed without errors, the label is added to the
10991 parse tree by the finish_* functions, so this function doesn't
10992 have to return the label. */
10995 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10998 tree label
= NULL_TREE
;
10999 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11001 /* The next token should be an identifier. */
11002 token
= cp_lexer_peek_token (parser
->lexer
);
11003 if (token
->type
!= CPP_NAME
11004 && token
->type
!= CPP_KEYWORD
)
11006 cp_parser_error (parser
, "expected labeled-statement");
11010 /* Remember whether this case or a user-defined label is allowed to fall
11012 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11014 parser
->colon_corrects_to_scope_p
= false;
11015 switch (token
->keyword
)
11019 tree expr
, expr_hi
;
11020 cp_token
*ellipsis
;
11022 /* Consume the `case' token. */
11023 cp_lexer_consume_token (parser
->lexer
);
11024 /* Parse the constant-expression. */
11025 expr
= cp_parser_constant_expression (parser
);
11026 if (check_for_bare_parameter_packs (expr
))
11027 expr
= error_mark_node
;
11029 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11030 if (ellipsis
->type
== CPP_ELLIPSIS
)
11032 /* Consume the `...' token. */
11033 cp_lexer_consume_token (parser
->lexer
);
11034 expr_hi
= cp_parser_constant_expression (parser
);
11035 if (check_for_bare_parameter_packs (expr_hi
))
11036 expr_hi
= error_mark_node
;
11038 /* We don't need to emit warnings here, as the common code
11039 will do this for us. */
11042 expr_hi
= NULL_TREE
;
11044 if (parser
->in_switch_statement_p
)
11046 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11047 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11048 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11051 error_at (token
->location
,
11052 "case label %qE not within a switch statement",
11058 /* Consume the `default' token. */
11059 cp_lexer_consume_token (parser
->lexer
);
11061 if (parser
->in_switch_statement_p
)
11063 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11064 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11065 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11068 error_at (token
->location
, "case label not within a switch statement");
11072 /* Anything else must be an ordinary label. */
11073 label
= finish_label_stmt (cp_parser_identifier (parser
));
11074 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11075 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11079 /* Require the `:' token. */
11080 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11082 /* An ordinary label may optionally be followed by attributes.
11083 However, this is only permitted if the attributes are then
11084 followed by a semicolon. This is because, for backward
11085 compatibility, when parsing
11086 lab: __attribute__ ((unused)) int i;
11087 we want the attribute to attach to "i", not "lab". */
11088 if (label
!= NULL_TREE
11089 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11092 cp_parser_parse_tentatively (parser
);
11093 attrs
= cp_parser_gnu_attributes_opt (parser
);
11094 if (attrs
== NULL_TREE
11095 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11096 cp_parser_abort_tentative_parse (parser
);
11097 else if (!cp_parser_parse_definitely (parser
))
11100 attributes
= attr_chainon (attributes
, attrs
);
11103 if (attributes
!= NULL_TREE
)
11104 cplus_decl_attributes (&label
, attributes
, 0);
11106 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11109 /* Parse an expression-statement.
11111 expression-statement:
11114 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11115 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11116 indicates whether this expression-statement is part of an
11117 expression statement. */
11120 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11122 tree statement
= NULL_TREE
;
11123 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11124 location_t loc
= token
->location
;
11126 /* There might be attribute fallthrough. */
11127 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11129 /* If the next token is a ';', then there is no expression
11131 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11133 statement
= cp_parser_expression (parser
);
11134 if (statement
== error_mark_node
11135 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11137 cp_parser_skip_to_end_of_block_or_statement (parser
);
11138 return error_mark_node
;
11142 /* Handle [[fallthrough]];. */
11143 if (attribute_fallthrough_p (attr
))
11145 /* The next token after the fallthrough attribute is ';'. */
11146 if (statement
== NULL_TREE
)
11147 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11148 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11149 void_type_node
, 0);
11151 warning_at (loc
, OPT_Wattributes
,
11152 "%<fallthrough%> attribute not followed by %<;%>");
11156 /* Allow "[[fallthrough]];", but warn otherwise. */
11157 if (attr
!= NULL_TREE
)
11158 warning_at (loc
, OPT_Wattributes
,
11159 "attributes at the beginning of statement are ignored");
11161 /* Give a helpful message for "A<T>::type t;" and the like. */
11162 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11163 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11165 if (TREE_CODE (statement
) == SCOPE_REF
)
11166 error_at (token
->location
, "need %<typename%> before %qE because "
11167 "%qT is a dependent scope",
11168 statement
, TREE_OPERAND (statement
, 0));
11169 else if (is_overloaded_fn (statement
)
11170 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11173 tree fn
= get_first_fn (statement
);
11174 error_at (token
->location
,
11175 "%<%T::%D%> names the constructor, not the type",
11176 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11180 /* Consume the final `;'. */
11181 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11183 if (in_statement_expr
11184 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11185 /* This is the final expression statement of a statement
11187 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11188 else if (statement
)
11189 statement
= finish_expr_stmt (statement
);
11194 /* Parse a compound-statement.
11196 compound-statement:
11197 { statement-seq [opt] }
11201 compound-statement:
11202 { label-declaration-seq [opt] statement-seq [opt] }
11204 label-declaration-seq:
11206 label-declaration-seq label-declaration
11208 Returns a tree representing the statement. */
11211 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11212 int bcs_flags
, bool function_body
)
11214 tree compound_stmt
;
11215 matching_braces braces
;
11217 /* Consume the `{'. */
11218 if (!braces
.require_open (parser
))
11219 return error_mark_node
;
11220 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11221 && !function_body
&& cxx_dialect
< cxx14
)
11222 pedwarn (input_location
, OPT_Wpedantic
,
11223 "compound-statement in %<constexpr%> function");
11224 /* Begin the compound-statement. */
11225 compound_stmt
= begin_compound_stmt (bcs_flags
);
11226 /* If the next keyword is `__label__' we have a label declaration. */
11227 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11228 cp_parser_label_declaration (parser
);
11229 /* Parse an (optional) statement-seq. */
11230 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11231 /* Finish the compound-statement. */
11232 finish_compound_stmt (compound_stmt
);
11233 /* Consume the `}'. */
11234 braces
.require_close (parser
);
11236 return compound_stmt
;
11239 /* Parse an (optional) statement-seq.
11243 statement-seq [opt] statement */
11246 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11248 /* Scan statements until there aren't any more. */
11251 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11253 /* If we are looking at a `}', then we have run out of
11254 statements; the same is true if we have reached the end
11255 of file, or have stumbled upon a stray '@end'. */
11256 if (token
->type
== CPP_CLOSE_BRACE
11257 || token
->type
== CPP_EOF
11258 || token
->type
== CPP_PRAGMA_EOL
11259 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11262 /* If we are in a compound statement and find 'else' then
11263 something went wrong. */
11264 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11266 if (parser
->in_statement
& IN_IF_STMT
)
11270 token
= cp_lexer_consume_token (parser
->lexer
);
11271 error_at (token
->location
, "%<else%> without a previous %<if%>");
11275 /* Parse the statement. */
11276 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11280 /* Return true if we're looking at (init; cond), false otherwise. */
11283 cp_parser_init_statement_p (cp_parser
*parser
)
11285 /* Save tokens so that we can put them back. */
11286 cp_lexer_save_tokens (parser
->lexer
);
11288 /* Look for ';' that is not nested in () or {}. */
11289 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11290 /*recovering=*/false,
11292 /*consume_paren=*/false);
11294 /* Roll back the tokens we skipped. */
11295 cp_lexer_rollback_tokens (parser
->lexer
);
11300 /* Parse a selection-statement.
11302 selection-statement:
11303 if ( init-statement [opt] condition ) statement
11304 if ( init-statement [opt] condition ) statement else statement
11305 switch ( init-statement [opt] condition ) statement
11307 Returns the new IF_STMT or SWITCH_STMT.
11309 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11310 is a (possibly labeled) if statement which is not enclosed in
11311 braces and has an else clause. This is used to implement
11314 CHAIN is a vector of if-else-if conditions. This is used to implement
11315 -Wduplicated-cond. */
11318 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11323 token_indent_info guard_tinfo
;
11328 /* Peek at the next token. */
11329 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11330 guard_tinfo
= get_token_indent_info (token
);
11332 /* See what kind of keyword it is. */
11333 keyword
= token
->keyword
;
11343 if (keyword
== RID_IF
11344 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11348 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11349 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11350 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11351 "with -std=c++17 or -std=gnu++17");
11354 /* Look for the `('. */
11355 matching_parens parens
;
11356 if (!parens
.require_open (parser
))
11358 cp_parser_skip_to_end_of_statement (parser
);
11359 return error_mark_node
;
11362 /* Begin the selection-statement. */
11363 if (keyword
== RID_IF
)
11365 statement
= begin_if_stmt ();
11366 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11369 statement
= begin_switch_stmt ();
11371 /* Parse the optional init-statement. */
11372 if (cp_parser_init_statement_p (parser
))
11375 if (cxx_dialect
< cxx17
)
11376 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11377 "init-statement in selection statements only available "
11378 "with -std=c++17 or -std=gnu++17");
11379 cp_parser_init_statement (parser
, &decl
);
11382 /* Parse the condition. */
11383 condition
= cp_parser_condition (parser
);
11384 /* Look for the `)'. */
11385 if (!parens
.require_close (parser
))
11386 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11387 /*consume_paren=*/true);
11389 if (keyword
== RID_IF
)
11392 unsigned char in_statement
;
11394 /* Add the condition. */
11395 condition
= finish_if_stmt_cond (condition
, statement
);
11397 if (warn_duplicated_cond
)
11398 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11401 /* Parse the then-clause. */
11402 in_statement
= parser
->in_statement
;
11403 parser
->in_statement
|= IN_IF_STMT
;
11405 /* Outside a template, the non-selected branch of a constexpr
11406 if is a 'discarded statement', i.e. unevaluated. */
11407 bool was_discarded
= in_discarded_stmt
;
11408 bool discard_then
= (cx
&& !processing_template_decl
11409 && integer_zerop (condition
));
11412 in_discarded_stmt
= true;
11413 ++c_inhibit_evaluation_warnings
;
11416 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11419 parser
->in_statement
= in_statement
;
11421 finish_then_clause (statement
);
11425 THEN_CLAUSE (statement
) = NULL_TREE
;
11426 in_discarded_stmt
= was_discarded
;
11427 --c_inhibit_evaluation_warnings
;
11430 /* If the next token is `else', parse the else-clause. */
11431 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11434 bool discard_else
= (cx
&& !processing_template_decl
11435 && integer_nonzerop (condition
));
11438 in_discarded_stmt
= true;
11439 ++c_inhibit_evaluation_warnings
;
11443 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11444 /* Consume the `else' keyword. */
11445 cp_lexer_consume_token (parser
->lexer
);
11446 if (warn_duplicated_cond
)
11448 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11452 /* We've got "if (COND) else if (COND2)". Start
11453 the condition chain and add COND as the first
11455 chain
= new vec
<tree
> ();
11456 if (!CONSTANT_CLASS_P (condition
)
11457 && !TREE_SIDE_EFFECTS (condition
))
11459 /* Wrap it in a NOP_EXPR so that we can set the
11460 location of the condition. */
11461 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11463 SET_EXPR_LOCATION (e
, token
->location
);
11464 chain
->safe_push (e
);
11467 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11470 /* This is if-else without subsequent if. Zap the
11471 condition chain; we would have already warned at
11477 begin_else_clause (statement
);
11478 /* Parse the else-clause. */
11479 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11480 guard_tinfo
, chain
);
11482 finish_else_clause (statement
);
11484 /* If we are currently parsing a then-clause, then
11485 IF_P will not be NULL. We set it to true to
11486 indicate that this if statement has an else clause.
11487 This may trigger the Wparentheses warning below
11488 when we get back up to the parent if statement. */
11494 ELSE_CLAUSE (statement
) = NULL_TREE
;
11495 in_discarded_stmt
= was_discarded
;
11496 --c_inhibit_evaluation_warnings
;
11501 /* This if statement does not have an else clause. If
11502 NESTED_IF is true, then the then-clause has an if
11503 statement which does have an else clause. We warn
11504 about the potential ambiguity. */
11506 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11507 "suggest explicit braces to avoid ambiguous"
11509 if (warn_duplicated_cond
)
11511 /* We don't need the condition chain anymore. */
11517 /* Now we're all done with the if-statement. */
11518 finish_if_stmt (statement
);
11522 bool in_switch_statement_p
;
11523 unsigned char in_statement
;
11525 /* Add the condition. */
11526 finish_switch_cond (condition
, statement
);
11528 /* Parse the body of the switch-statement. */
11529 in_switch_statement_p
= parser
->in_switch_statement_p
;
11530 in_statement
= parser
->in_statement
;
11531 parser
->in_switch_statement_p
= true;
11532 parser
->in_statement
|= IN_SWITCH_STMT
;
11533 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11535 parser
->in_switch_statement_p
= in_switch_statement_p
;
11536 parser
->in_statement
= in_statement
;
11538 /* Now we're all done with the switch-statement. */
11539 finish_switch_stmt (statement
);
11547 cp_parser_error (parser
, "expected selection-statement");
11548 return error_mark_node
;
11552 /* Parse a condition.
11556 type-specifier-seq declarator = initializer-clause
11557 type-specifier-seq declarator braced-init-list
11562 type-specifier-seq declarator asm-specification [opt]
11563 attributes [opt] = assignment-expression
11565 Returns the expression that should be tested. */
11568 cp_parser_condition (cp_parser
* parser
)
11570 cp_decl_specifier_seq type_specifiers
;
11571 const char *saved_message
;
11572 int declares_class_or_enum
;
11574 /* Try the declaration first. */
11575 cp_parser_parse_tentatively (parser
);
11576 /* New types are not allowed in the type-specifier-seq for a
11578 saved_message
= parser
->type_definition_forbidden_message
;
11579 parser
->type_definition_forbidden_message
11580 = G_("types may not be defined in conditions");
11581 /* Parse the type-specifier-seq. */
11582 cp_parser_decl_specifier_seq (parser
,
11583 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11585 &declares_class_or_enum
);
11586 /* Restore the saved message. */
11587 parser
->type_definition_forbidden_message
= saved_message
;
11588 /* If all is well, we might be looking at a declaration. */
11589 if (!cp_parser_error_occurred (parser
))
11592 tree asm_specification
;
11594 cp_declarator
*declarator
;
11595 tree initializer
= NULL_TREE
;
11597 /* Parse the declarator. */
11598 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11599 /*ctor_dtor_or_conv_p=*/NULL
,
11600 /*parenthesized_p=*/NULL
,
11601 /*member_p=*/false,
11602 /*friend_p=*/false);
11603 /* Parse the attributes. */
11604 attributes
= cp_parser_attributes_opt (parser
);
11605 /* Parse the asm-specification. */
11606 asm_specification
= cp_parser_asm_specification_opt (parser
);
11607 /* If the next token is not an `=' or '{', then we might still be
11608 looking at an expression. For example:
11612 looks like a decl-specifier-seq and a declarator -- but then
11613 there is no `=', so this is an expression. */
11614 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11615 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11616 cp_parser_simulate_error (parser
);
11618 /* If we did see an `=' or '{', then we are looking at a declaration
11620 if (cp_parser_parse_definitely (parser
))
11623 bool non_constant_p
;
11624 int flags
= LOOKUP_ONLYCONVERTING
;
11626 /* Create the declaration. */
11627 decl
= start_decl (declarator
, &type_specifiers
,
11628 /*initialized_p=*/true,
11629 attributes
, /*prefix_attributes=*/NULL_TREE
,
11632 /* Parse the initializer. */
11633 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11635 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11636 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11641 /* Consume the `='. */
11642 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11643 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11645 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11646 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11648 /* Process the initializer. */
11649 cp_finish_decl (decl
,
11650 initializer
, !non_constant_p
,
11655 pop_scope (pushed_scope
);
11657 return convert_from_reference (decl
);
11660 /* If we didn't even get past the declarator successfully, we are
11661 definitely not looking at a declaration. */
11663 cp_parser_abort_tentative_parse (parser
);
11665 /* Otherwise, we are looking at an expression. */
11666 return cp_parser_expression (parser
);
11669 /* Parses a for-statement or range-for-statement until the closing ')',
11673 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11675 tree init
, scope
, decl
;
11678 /* Begin the for-statement. */
11679 scope
= begin_for_scope (&init
);
11681 /* Parse the initialization. */
11682 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11685 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11687 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11691 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11692 unsigned short unroll
)
11694 /* Normal for loop */
11695 tree condition
= NULL_TREE
;
11696 tree expression
= NULL_TREE
;
11699 stmt
= begin_for_stmt (scope
, init
);
11700 /* The init-statement has already been parsed in
11701 cp_parser_init_statement, so no work is needed here. */
11702 finish_init_stmt (stmt
);
11704 /* If there's a condition, process it. */
11705 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11706 condition
= cp_parser_condition (parser
);
11709 cp_parser_error (parser
, "missing loop condition in loop with "
11710 "%<GCC ivdep%> pragma");
11711 condition
= error_mark_node
;
11715 cp_parser_error (parser
, "missing loop condition in loop with "
11716 "%<GCC unroll%> pragma");
11717 condition
= error_mark_node
;
11719 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11720 /* Look for the `;'. */
11721 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11723 /* If there's an expression, process it. */
11724 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11725 expression
= cp_parser_expression (parser
);
11726 finish_for_expr (expression
, stmt
);
11731 /* Tries to parse a range-based for-statement:
11734 decl-specifier-seq declarator : expression
11736 The decl-specifier-seq declarator and the `:' are already parsed by
11737 cp_parser_init_statement. If processing_template_decl it returns a
11738 newly created RANGE_FOR_STMT; if not, it is converted to a
11739 regular FOR_STMT. */
11742 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11743 bool ivdep
, unsigned short unroll
)
11745 tree stmt
, range_expr
;
11746 auto_vec
<cxx_binding
*, 16> bindings
;
11747 auto_vec
<tree
, 16> names
;
11748 tree decomp_first_name
= NULL_TREE
;
11749 unsigned int decomp_cnt
= 0;
11751 /* Get the range declaration momentarily out of the way so that
11752 the range expression doesn't clash with it. */
11753 if (range_decl
!= error_mark_node
)
11755 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11757 tree v
= DECL_VALUE_EXPR (range_decl
);
11758 /* For decomposition declaration get all of the corresponding
11759 declarations out of the way. */
11760 if (TREE_CODE (v
) == ARRAY_REF
11761 && VAR_P (TREE_OPERAND (v
, 0))
11762 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11764 tree d
= range_decl
;
11765 range_decl
= TREE_OPERAND (v
, 0);
11766 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11767 decomp_first_name
= d
;
11768 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11770 tree name
= DECL_NAME (d
);
11771 names
.safe_push (name
);
11772 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11773 IDENTIFIER_BINDING (name
)
11774 = IDENTIFIER_BINDING (name
)->previous
;
11778 if (names
.is_empty ())
11780 tree name
= DECL_NAME (range_decl
);
11781 names
.safe_push (name
);
11782 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11783 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11787 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11789 bool expr_non_constant_p
;
11790 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11793 range_expr
= cp_parser_expression (parser
);
11795 /* Put the range declaration(s) back into scope. */
11796 for (unsigned int i
= 0; i
< names
.length (); i
++)
11798 cxx_binding
*binding
= bindings
[i
];
11799 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11800 IDENTIFIER_BINDING (names
[i
]) = binding
;
11803 /* If in template, STMT is converted to a normal for-statement
11804 at instantiation. If not, it is done just ahead. */
11805 if (processing_template_decl
)
11807 if (check_for_bare_parameter_packs (range_expr
))
11808 range_expr
= error_mark_node
;
11809 stmt
= begin_range_for_stmt (scope
, init
);
11811 RANGE_FOR_IVDEP (stmt
) = 1;
11813 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11814 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11815 if (!type_dependent_expression_p (range_expr
)
11816 /* do_auto_deduction doesn't mess with template init-lists. */
11817 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11818 do_range_for_auto_deduction (range_decl
, range_expr
);
11822 stmt
= begin_for_stmt (scope
, init
);
11823 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11824 decomp_first_name
, decomp_cnt
, ivdep
,
11830 /* Subroutine of cp_convert_range_for: given the initializer expression,
11831 builds up the range temporary. */
11834 build_range_temp (tree range_expr
)
11836 tree range_type
, range_temp
;
11838 /* Find out the type deduced by the declaration
11839 `auto &&__range = range_expr'. */
11840 range_type
= cp_build_reference_type (make_auto (), true);
11841 range_type
= do_auto_deduction (range_type
, range_expr
,
11842 type_uses_auto (range_type
));
11844 /* Create the __range variable. */
11845 range_temp
= build_decl (input_location
, VAR_DECL
,
11846 get_identifier ("__for_range"), range_type
);
11847 TREE_USED (range_temp
) = 1;
11848 DECL_ARTIFICIAL (range_temp
) = 1;
11853 /* Used by cp_parser_range_for in template context: we aren't going to
11854 do a full conversion yet, but we still need to resolve auto in the
11855 type of the for-range-declaration if present. This is basically
11856 a shortcut version of cp_convert_range_for. */
11859 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11861 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11864 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11865 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11866 iter_type
= (cp_parser_perform_range_for_lookup
11867 (range_temp
, &begin_dummy
, &end_dummy
));
11870 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11872 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11874 tf_warning_or_error
);
11875 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11876 iter_decl
, auto_node
);
11881 /* Converts a range-based for-statement into a normal
11882 for-statement, as per the definition.
11884 for (RANGE_DECL : RANGE_EXPR)
11887 should be equivalent to:
11890 auto &&__range = RANGE_EXPR;
11891 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11895 RANGE_DECL = *__begin;
11900 If RANGE_EXPR is an array:
11901 BEGIN_EXPR = __range
11902 END_EXPR = __range + ARRAY_SIZE(__range)
11903 Else if RANGE_EXPR has a member 'begin' or 'end':
11904 BEGIN_EXPR = __range.begin()
11905 END_EXPR = __range.end()
11907 BEGIN_EXPR = begin(__range)
11908 END_EXPR = end(__range);
11910 If __range has a member 'begin' but not 'end', or vice versa, we must
11911 still use the second alternative (it will surely fail, however).
11912 When calling begin()/end() in the third alternative we must use
11913 argument dependent lookup, but always considering 'std' as an associated
11917 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11918 tree decomp_first_name
, unsigned int decomp_cnt
,
11919 bool ivdep
, unsigned short unroll
)
11922 tree iter_type
, begin_expr
, end_expr
;
11923 tree condition
, expression
;
11925 range_expr
= mark_lvalue_use (range_expr
);
11927 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11928 /* If an error happened previously do nothing or else a lot of
11929 unhelpful errors would be issued. */
11930 begin_expr
= end_expr
= iter_type
= error_mark_node
;
11935 if (VAR_P (range_expr
)
11936 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
11937 /* Can't bind a reference to an array of runtime bound. */
11938 range_temp
= range_expr
;
11941 range_temp
= build_range_temp (range_expr
);
11942 pushdecl (range_temp
);
11943 cp_finish_decl (range_temp
, range_expr
,
11944 /*is_constant_init*/false, NULL_TREE
,
11945 LOOKUP_ONLYCONVERTING
);
11946 range_temp
= convert_from_reference (range_temp
);
11948 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
11949 &begin_expr
, &end_expr
);
11952 /* The new for initialization statement. */
11953 begin
= build_decl (input_location
, VAR_DECL
,
11954 get_identifier ("__for_begin"), iter_type
);
11955 TREE_USED (begin
) = 1;
11956 DECL_ARTIFICIAL (begin
) = 1;
11958 cp_finish_decl (begin
, begin_expr
,
11959 /*is_constant_init*/false, NULL_TREE
,
11960 LOOKUP_ONLYCONVERTING
);
11962 if (cxx_dialect
>= cxx17
)
11963 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
11964 end
= build_decl (input_location
, VAR_DECL
,
11965 get_identifier ("__for_end"), iter_type
);
11966 TREE_USED (end
) = 1;
11967 DECL_ARTIFICIAL (end
) = 1;
11969 cp_finish_decl (end
, end_expr
,
11970 /*is_constant_init*/false, NULL_TREE
,
11971 LOOKUP_ONLYCONVERTING
);
11973 finish_init_stmt (statement
);
11975 /* The new for condition. */
11976 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11979 NULL
, tf_warning_or_error
);
11980 finish_for_cond (condition
, statement
, ivdep
, unroll
);
11982 /* The new increment expression. */
11983 expression
= finish_unary_op_expr (input_location
,
11984 PREINCREMENT_EXPR
, begin
,
11985 tf_warning_or_error
);
11986 finish_for_expr (expression
, statement
);
11988 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11989 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
11991 /* The declaration is initialized with *__begin inside the loop body. */
11992 cp_finish_decl (range_decl
,
11993 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
11994 tf_warning_or_error
),
11995 /*is_constant_init*/false, NULL_TREE
,
11996 LOOKUP_ONLYCONVERTING
);
11997 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
11998 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12003 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12004 We need to solve both at the same time because the method used
12005 depends on the existence of members begin or end.
12006 Returns the type deduced for the iterator expression. */
12009 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12011 if (error_operand_p (range
))
12013 *begin
= *end
= error_mark_node
;
12014 return error_mark_node
;
12017 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12019 error ("range-based %<for%> expression of type %qT "
12020 "has incomplete type", TREE_TYPE (range
));
12021 *begin
= *end
= error_mark_node
;
12022 return error_mark_node
;
12024 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12026 /* If RANGE is an array, we will use pointer arithmetic. */
12027 *begin
= decay_conversion (range
, tf_warning_or_error
);
12028 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12030 array_type_nelts_top (TREE_TYPE (range
)),
12032 return TREE_TYPE (*begin
);
12036 /* If it is not an array, we must do a bit of magic. */
12037 tree id_begin
, id_end
;
12038 tree member_begin
, member_end
;
12040 *begin
= *end
= error_mark_node
;
12042 id_begin
= get_identifier ("begin");
12043 id_end
= get_identifier ("end");
12044 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12045 /*protect=*/2, /*want_type=*/false,
12046 tf_warning_or_error
);
12047 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12048 /*protect=*/2, /*want_type=*/false,
12049 tf_warning_or_error
);
12051 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12053 /* Use the member functions. */
12054 if (member_begin
!= NULL_TREE
)
12055 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12057 error ("range-based %<for%> expression of type %qT has an "
12058 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
12060 if (member_end
!= NULL_TREE
)
12061 *end
= cp_parser_range_for_member_function (range
, id_end
);
12063 error ("range-based %<for%> expression of type %qT has a "
12064 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
12068 /* Use global functions with ADL. */
12069 vec
<tree
, va_gc
> *vec
;
12070 vec
= make_tree_vector ();
12072 vec_safe_push (vec
, range
);
12074 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12075 tf_warning_or_error
);
12076 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12077 tf_warning_or_error
);
12078 member_end
= perform_koenig_lookup (id_end
, vec
,
12079 tf_warning_or_error
);
12080 *end
= finish_call_expr (member_end
, &vec
, false, true,
12081 tf_warning_or_error
);
12083 release_tree_vector (vec
);
12086 /* Last common checks. */
12087 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12089 /* If one of the expressions is an error do no more checks. */
12090 *begin
= *end
= error_mark_node
;
12091 return error_mark_node
;
12093 else if (type_dependent_expression_p (*begin
)
12094 || type_dependent_expression_p (*end
))
12095 /* Can happen, when, eg, in a template context, Koenig lookup
12096 can't resolve begin/end (c++/58503). */
12100 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12101 /* The unqualified type of the __begin and __end temporaries should
12102 be the same, as required by the multiple auto declaration. */
12103 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12105 if (cxx_dialect
>= cxx17
12106 && (build_x_binary_op (input_location
, NE_EXPR
,
12107 *begin
, ERROR_MARK
,
12110 != error_mark_node
))
12111 /* P0184R0 allows __begin and __end to have different types,
12112 but make sure they are comparable so we can give a better
12115 error ("inconsistent begin/end types in range-based %<for%> "
12116 "statement: %qT and %qT",
12117 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12124 /* Helper function for cp_parser_perform_range_for_lookup.
12125 Builds a tree for RANGE.IDENTIFIER(). */
12128 cp_parser_range_for_member_function (tree range
, tree identifier
)
12131 vec
<tree
, va_gc
> *vec
;
12133 member
= finish_class_member_access_expr (range
, identifier
,
12134 false, tf_warning_or_error
);
12135 if (member
== error_mark_node
)
12136 return error_mark_node
;
12138 vec
= make_tree_vector ();
12139 res
= finish_call_expr (member
, &vec
,
12140 /*disallow_virtual=*/false,
12141 /*koenig_p=*/false,
12142 tf_warning_or_error
);
12143 release_tree_vector (vec
);
12147 /* Parse an iteration-statement.
12149 iteration-statement:
12150 while ( condition ) statement
12151 do statement while ( expression ) ;
12152 for ( init-statement condition [opt] ; expression [opt] )
12155 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12158 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12159 unsigned short unroll
)
12164 unsigned char in_statement
;
12165 token_indent_info guard_tinfo
;
12167 /* Peek at the next token. */
12168 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12170 return error_mark_node
;
12172 guard_tinfo
= get_token_indent_info (token
);
12174 /* Remember whether or not we are already within an iteration
12176 in_statement
= parser
->in_statement
;
12178 /* See what kind of keyword it is. */
12179 keyword
= token
->keyword
;
12186 /* Begin the while-statement. */
12187 statement
= begin_while_stmt ();
12188 /* Look for the `('. */
12189 matching_parens parens
;
12190 parens
.require_open (parser
);
12191 /* Parse the condition. */
12192 condition
= cp_parser_condition (parser
);
12193 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12194 /* Look for the `)'. */
12195 parens
.require_close (parser
);
12196 /* Parse the dependent statement. */
12197 parser
->in_statement
= IN_ITERATION_STMT
;
12198 bool prev
= note_iteration_stmt_body_start ();
12199 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12200 note_iteration_stmt_body_end (prev
);
12201 parser
->in_statement
= in_statement
;
12202 /* We're done with the while-statement. */
12203 finish_while_stmt (statement
);
12211 /* Begin the do-statement. */
12212 statement
= begin_do_stmt ();
12213 /* Parse the body of the do-statement. */
12214 parser
->in_statement
= IN_ITERATION_STMT
;
12215 bool prev
= note_iteration_stmt_body_start ();
12216 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12217 note_iteration_stmt_body_end (prev
);
12218 parser
->in_statement
= in_statement
;
12219 finish_do_body (statement
);
12220 /* Look for the `while' keyword. */
12221 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12222 /* Look for the `('. */
12223 matching_parens parens
;
12224 parens
.require_open (parser
);
12225 /* Parse the expression. */
12226 expression
= cp_parser_expression (parser
);
12227 /* We're done with the do-statement. */
12228 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12229 /* Look for the `)'. */
12230 parens
.require_close (parser
);
12231 /* Look for the `;'. */
12232 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12238 /* Look for the `('. */
12239 matching_parens parens
;
12240 parens
.require_open (parser
);
12242 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12244 /* Look for the `)'. */
12245 parens
.require_close (parser
);
12247 /* Parse the body of the for-statement. */
12248 parser
->in_statement
= IN_ITERATION_STMT
;
12249 bool prev
= note_iteration_stmt_body_start ();
12250 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12251 note_iteration_stmt_body_end (prev
);
12252 parser
->in_statement
= in_statement
;
12254 /* We're done with the for-statement. */
12255 finish_for_stmt (statement
);
12260 cp_parser_error (parser
, "expected iteration-statement");
12261 statement
= error_mark_node
;
12268 /* Parse a init-statement or the declarator of a range-based-for.
12269 Returns true if a range-based-for declaration is seen.
12272 expression-statement
12273 simple-declaration */
12276 cp_parser_init_statement (cp_parser
* parser
, tree
*decl
)
12278 /* If the next token is a `;', then we have an empty
12279 expression-statement. Grammatically, this is also a
12280 simple-declaration, but an invalid one, because it does not
12281 declare anything. Therefore, if we did not handle this case
12282 specially, we would issue an error message about an invalid
12284 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12286 bool is_range_for
= false;
12287 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12289 /* A colon is used in range-based for. */
12290 parser
->colon_corrects_to_scope_p
= false;
12292 /* We're going to speculatively look for a declaration, falling back
12293 to an expression, if necessary. */
12294 cp_parser_parse_tentatively (parser
);
12295 /* Parse the declaration. */
12296 cp_parser_simple_declaration (parser
,
12297 /*function_definition_allowed_p=*/false,
12299 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12302 /* It is a range-for, consume the ':' */
12303 cp_lexer_consume_token (parser
->lexer
);
12304 is_range_for
= true;
12305 if (cxx_dialect
< cxx11
)
12307 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12308 "range-based %<for%> loops only available with "
12309 "-std=c++11 or -std=gnu++11");
12310 *decl
= error_mark_node
;
12314 /* The ';' is not consumed yet because we told
12315 cp_parser_simple_declaration not to. */
12316 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12318 if (cp_parser_parse_definitely (parser
))
12319 return is_range_for
;
12320 /* If the tentative parse failed, then we shall need to look for an
12321 expression-statement. */
12323 /* If we are here, it is an expression-statement. */
12324 cp_parser_expression_statement (parser
, NULL_TREE
);
12328 /* Parse a jump-statement.
12333 return expression [opt] ;
12334 return braced-init-list ;
12340 goto * expression ;
12342 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12345 cp_parser_jump_statement (cp_parser
* parser
)
12347 tree statement
= error_mark_node
;
12350 unsigned char in_statement
;
12352 /* Peek at the next token. */
12353 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12355 return error_mark_node
;
12357 /* See what kind of keyword it is. */
12358 keyword
= token
->keyword
;
12362 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12363 switch (in_statement
)
12366 error_at (token
->location
, "break statement not within loop or switch");
12369 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12370 || in_statement
== IN_ITERATION_STMT
);
12371 statement
= finish_break_stmt ();
12372 if (in_statement
== IN_ITERATION_STMT
)
12373 break_maybe_infinite_loop ();
12376 error_at (token
->location
, "invalid exit from OpenMP structured block");
12379 error_at (token
->location
, "break statement used with OpenMP for loop");
12382 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12386 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12389 error_at (token
->location
, "continue statement not within a loop");
12391 /* Fall through. */
12392 case IN_ITERATION_STMT
:
12394 statement
= finish_continue_stmt ();
12397 error_at (token
->location
, "invalid exit from OpenMP structured block");
12400 gcc_unreachable ();
12402 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12408 bool expr_non_constant_p
;
12410 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12412 cp_lexer_set_source_position (parser
->lexer
);
12413 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12414 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12416 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12417 expr
= cp_parser_expression (parser
);
12419 /* If the next token is a `;', then there is no
12422 /* Build the return-statement. */
12423 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12424 /* Don't deduce from a discarded return statement. */;
12426 statement
= finish_return_stmt (expr
);
12427 /* Look for the final `;'. */
12428 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12433 if (parser
->in_function_body
12434 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12436 error ("%<goto%> in %<constexpr%> function");
12437 cp_function_chain
->invalid_constexpr
= true;
12440 /* Create the goto-statement. */
12441 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12443 /* Issue a warning about this use of a GNU extension. */
12444 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12445 /* Consume the '*' token. */
12446 cp_lexer_consume_token (parser
->lexer
);
12447 /* Parse the dependent expression. */
12448 finish_goto_stmt (cp_parser_expression (parser
));
12451 finish_goto_stmt (cp_parser_identifier (parser
));
12452 /* Look for the final `;'. */
12453 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12457 cp_parser_error (parser
, "expected jump-statement");
12464 /* Parse a declaration-statement.
12466 declaration-statement:
12467 block-declaration */
12470 cp_parser_declaration_statement (cp_parser
* parser
)
12474 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12475 p
= obstack_alloc (&declarator_obstack
, 0);
12477 /* Parse the block-declaration. */
12478 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12480 /* Free any declarators allocated. */
12481 obstack_free (&declarator_obstack
, p
);
12484 /* Some dependent statements (like `if (cond) statement'), are
12485 implicitly in their own scope. In other words, if the statement is
12486 a single statement (as opposed to a compound-statement), it is
12487 none-the-less treated as if it were enclosed in braces. Any
12488 declarations appearing in the dependent statement are out of scope
12489 after control passes that point. This function parses a statement,
12490 but ensures that is in its own scope, even if it is not a
12491 compound-statement.
12493 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12494 is a (possibly labeled) if statement which is not enclosed in
12495 braces and has an else clause. This is used to implement
12498 CHAIN is a vector of if-else-if conditions. This is used to implement
12501 Returns the new statement. */
12504 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12505 const token_indent_info
&guard_tinfo
,
12509 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12510 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12511 token_indent_info body_tinfo
12512 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12517 /* Mark if () ; with a special NOP_EXPR. */
12518 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12520 cp_lexer_consume_token (parser
->lexer
);
12521 statement
= add_stmt (build_empty_stmt (body_loc
));
12523 if (guard_tinfo
.keyword
== RID_IF
12524 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12525 warning_at (body_loc
, OPT_Wempty_body
,
12526 "suggest braces around empty body in an %<if%> statement");
12527 else if (guard_tinfo
.keyword
== RID_ELSE
)
12528 warning_at (body_loc
, OPT_Wempty_body
,
12529 "suggest braces around empty body in an %<else%> statement");
12531 /* if a compound is opened, we simply parse the statement directly. */
12532 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12533 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12534 /* If the token is not a `{', then we must take special action. */
12537 /* Create a compound-statement. */
12538 statement
= begin_compound_stmt (0);
12539 /* Parse the dependent-statement. */
12540 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12541 &body_loc_after_labels
);
12542 /* Finish the dummy compound-statement. */
12543 finish_compound_stmt (statement
);
12546 token_indent_info next_tinfo
12547 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12548 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12550 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12551 && next_tinfo
.type
!= CPP_SEMICOLON
)
12552 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12553 guard_tinfo
.location
, guard_tinfo
.keyword
);
12555 /* Return the statement. */
12559 /* For some dependent statements (like `while (cond) statement'), we
12560 have already created a scope. Therefore, even if the dependent
12561 statement is a compound-statement, we do not want to create another
12565 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12566 const token_indent_info
&guard_tinfo
)
12568 /* If the token is a `{', then we must take special action. */
12569 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12571 token_indent_info body_tinfo
12572 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12573 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12575 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12576 &loc_after_labels
);
12577 token_indent_info next_tinfo
12578 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12579 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12581 if (loc_after_labels
!= UNKNOWN_LOCATION
12582 && next_tinfo
.type
!= CPP_SEMICOLON
)
12583 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12584 guard_tinfo
.location
,
12585 guard_tinfo
.keyword
);
12589 /* Avoid calling cp_parser_compound_statement, so that we
12590 don't create a new scope. Do everything else by hand. */
12591 matching_braces braces
;
12592 braces
.require_open (parser
);
12593 /* If the next keyword is `__label__' we have a label declaration. */
12594 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12595 cp_parser_label_declaration (parser
);
12596 /* Parse an (optional) statement-seq. */
12597 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12598 braces
.require_close (parser
);
12602 /* Declarations [gram.dcl.dcl] */
12604 /* Parse an optional declaration-sequence.
12608 declaration-seq declaration */
12611 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12617 token
= cp_lexer_peek_token (parser
->lexer
);
12619 if (token
->type
== CPP_CLOSE_BRACE
12620 || token
->type
== CPP_EOF
12621 || token
->type
== CPP_PRAGMA_EOL
)
12624 if (token
->type
== CPP_SEMICOLON
)
12626 /* A declaration consisting of a single semicolon is
12627 invalid. Allow it unless we're being pedantic. */
12628 cp_lexer_consume_token (parser
->lexer
);
12629 if (!in_system_header_at (input_location
))
12630 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12634 /* If we're entering or exiting a region that's implicitly
12635 extern "C", modify the lang context appropriately. */
12636 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12638 push_lang_context (lang_name_c
);
12639 parser
->implicit_extern_c
= true;
12641 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12643 pop_lang_context ();
12644 parser
->implicit_extern_c
= false;
12647 if (token
->type
== CPP_PRAGMA
)
12649 /* A top-level declaration can consist solely of a #pragma.
12650 A nested declaration cannot, so this is done here and not
12651 in cp_parser_declaration. (A #pragma at block scope is
12652 handled in cp_parser_statement.) */
12653 cp_parser_pragma (parser
, pragma_external
, NULL
);
12657 /* Parse the declaration itself. */
12658 cp_parser_declaration (parser
);
12662 /* Parse a declaration.
12666 function-definition
12667 template-declaration
12668 explicit-instantiation
12669 explicit-specialization
12670 linkage-specification
12671 namespace-definition
12679 __extension__ declaration */
12682 cp_parser_declaration (cp_parser
* parser
)
12686 int saved_pedantic
;
12688 tree attributes
= NULL_TREE
;
12690 /* Check for the `__extension__' keyword. */
12691 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12693 /* Parse the qualified declaration. */
12694 cp_parser_declaration (parser
);
12695 /* Restore the PEDANTIC flag. */
12696 pedantic
= saved_pedantic
;
12701 /* Try to figure out what kind of declaration is present. */
12702 token1
= *cp_lexer_peek_token (parser
->lexer
);
12704 if (token1
.type
!= CPP_EOF
)
12705 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12708 token2
.type
= CPP_EOF
;
12709 token2
.keyword
= RID_MAX
;
12712 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12713 p
= obstack_alloc (&declarator_obstack
, 0);
12715 /* If the next token is `extern' and the following token is a string
12716 literal, then we have a linkage specification. */
12717 if (token1
.keyword
== RID_EXTERN
12718 && cp_parser_is_pure_string_literal (&token2
))
12719 cp_parser_linkage_specification (parser
);
12720 /* If the next token is `template', then we have either a template
12721 declaration, an explicit instantiation, or an explicit
12723 else if (token1
.keyword
== RID_TEMPLATE
)
12725 /* `template <>' indicates a template specialization. */
12726 if (token2
.type
== CPP_LESS
12727 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12728 cp_parser_explicit_specialization (parser
);
12729 /* `template <' indicates a template declaration. */
12730 else if (token2
.type
== CPP_LESS
)
12731 cp_parser_template_declaration (parser
, /*member_p=*/false);
12732 /* Anything else must be an explicit instantiation. */
12734 cp_parser_explicit_instantiation (parser
);
12736 /* If the next token is `export', then we have a template
12738 else if (token1
.keyword
== RID_EXPORT
)
12739 cp_parser_template_declaration (parser
, /*member_p=*/false);
12740 /* If the next token is `extern', 'static' or 'inline' and the one
12741 after that is `template', we have a GNU extended explicit
12742 instantiation directive. */
12743 else if (cp_parser_allow_gnu_extensions_p (parser
)
12744 && (token1
.keyword
== RID_EXTERN
12745 || token1
.keyword
== RID_STATIC
12746 || token1
.keyword
== RID_INLINE
)
12747 && token2
.keyword
== RID_TEMPLATE
)
12748 cp_parser_explicit_instantiation (parser
);
12749 /* If the next token is `namespace', check for a named or unnamed
12750 namespace definition. */
12751 else if (token1
.keyword
== RID_NAMESPACE
12752 && (/* A named namespace definition. */
12753 (token2
.type
== CPP_NAME
12754 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12756 || (token2
.type
== CPP_OPEN_SQUARE
12757 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12758 == CPP_OPEN_SQUARE
)
12759 /* An unnamed namespace definition. */
12760 || token2
.type
== CPP_OPEN_BRACE
12761 || token2
.keyword
== RID_ATTRIBUTE
))
12762 cp_parser_namespace_definition (parser
);
12763 /* An inline (associated) namespace definition. */
12764 else if (token1
.keyword
== RID_INLINE
12765 && token2
.keyword
== RID_NAMESPACE
)
12766 cp_parser_namespace_definition (parser
);
12767 /* Objective-C++ declaration/definition. */
12768 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12769 cp_parser_objc_declaration (parser
, NULL_TREE
);
12770 else if (c_dialect_objc ()
12771 && token1
.keyword
== RID_ATTRIBUTE
12772 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12773 cp_parser_objc_declaration (parser
, attributes
);
12774 /* At this point we may have a template declared by a concept
12776 else if (flag_concepts
12777 && cp_parser_template_declaration_after_export (parser
,
12778 /*member_p=*/false))
12781 /* Try to parse a block-declaration, or a function-definition. */
12782 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12784 /* Free any declarators allocated. */
12785 obstack_free (&declarator_obstack
, p
);
12788 /* Parse a block-declaration.
12793 namespace-alias-definition
12800 __extension__ block-declaration
12805 static_assert-declaration
12807 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12808 part of a declaration-statement. */
12811 cp_parser_block_declaration (cp_parser
*parser
,
12815 int saved_pedantic
;
12817 /* Check for the `__extension__' keyword. */
12818 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12820 /* Parse the qualified declaration. */
12821 cp_parser_block_declaration (parser
, statement_p
);
12822 /* Restore the PEDANTIC flag. */
12823 pedantic
= saved_pedantic
;
12828 /* Peek at the next token to figure out which kind of declaration is
12830 token1
= cp_lexer_peek_token (parser
->lexer
);
12832 /* If the next keyword is `asm', we have an asm-definition. */
12833 if (token1
->keyword
== RID_ASM
)
12836 cp_parser_commit_to_tentative_parse (parser
);
12837 cp_parser_asm_definition (parser
);
12839 /* If the next keyword is `namespace', we have a
12840 namespace-alias-definition. */
12841 else if (token1
->keyword
== RID_NAMESPACE
)
12842 cp_parser_namespace_alias_definition (parser
);
12843 /* If the next keyword is `using', we have a
12844 using-declaration, a using-directive, or an alias-declaration. */
12845 else if (token1
->keyword
== RID_USING
)
12850 cp_parser_commit_to_tentative_parse (parser
);
12851 /* If the token after `using' is `namespace', then we have a
12852 using-directive. */
12853 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12854 if (token2
->keyword
== RID_NAMESPACE
)
12855 cp_parser_using_directive (parser
);
12856 /* If the second token after 'using' is '=', then we have an
12857 alias-declaration. */
12858 else if (cxx_dialect
>= cxx11
12859 && token2
->type
== CPP_NAME
12860 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12861 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12862 cp_parser_alias_declaration (parser
);
12863 /* Otherwise, it's a using-declaration. */
12865 cp_parser_using_declaration (parser
,
12866 /*access_declaration_p=*/false);
12868 /* If the next keyword is `__label__' we have a misplaced label
12870 else if (token1
->keyword
== RID_LABEL
)
12872 cp_lexer_consume_token (parser
->lexer
);
12873 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12874 cp_parser_skip_to_end_of_statement (parser
);
12875 /* If the next token is now a `;', consume it. */
12876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12877 cp_lexer_consume_token (parser
->lexer
);
12879 /* If the next token is `static_assert' we have a static assertion. */
12880 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12881 cp_parser_static_assert (parser
, /*member_p=*/false);
12882 /* Anything else must be a simple-declaration. */
12884 cp_parser_simple_declaration (parser
, !statement_p
,
12885 /*maybe_range_for_decl*/NULL
);
12888 /* Parse a simple-declaration.
12890 simple-declaration:
12891 decl-specifier-seq [opt] init-declarator-list [opt] ;
12892 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12893 brace-or-equal-initializer ;
12895 init-declarator-list:
12897 init-declarator-list , init-declarator
12899 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12900 function-definition as a simple-declaration.
12902 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12903 parsed declaration if it is an uninitialized single declarator not followed
12904 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12905 if present, will not be consumed. */
12908 cp_parser_simple_declaration (cp_parser
* parser
,
12909 bool function_definition_allowed_p
,
12910 tree
*maybe_range_for_decl
)
12912 cp_decl_specifier_seq decl_specifiers
;
12913 int declares_class_or_enum
;
12914 bool saw_declarator
;
12915 location_t comma_loc
= UNKNOWN_LOCATION
;
12916 location_t init_loc
= UNKNOWN_LOCATION
;
12918 if (maybe_range_for_decl
)
12919 *maybe_range_for_decl
= NULL_TREE
;
12921 /* Defer access checks until we know what is being declared; the
12922 checks for names appearing in the decl-specifier-seq should be
12923 done as if we were in the scope of the thing being declared. */
12924 push_deferring_access_checks (dk_deferred
);
12926 /* Parse the decl-specifier-seq. We have to keep track of whether
12927 or not the decl-specifier-seq declares a named class or
12928 enumeration type, since that is the only case in which the
12929 init-declarator-list is allowed to be empty.
12933 In a simple-declaration, the optional init-declarator-list can be
12934 omitted only when declaring a class or enumeration, that is when
12935 the decl-specifier-seq contains either a class-specifier, an
12936 elaborated-type-specifier, or an enum-specifier. */
12937 cp_parser_decl_specifier_seq (parser
,
12938 CP_PARSER_FLAGS_OPTIONAL
,
12940 &declares_class_or_enum
);
12941 /* We no longer need to defer access checks. */
12942 stop_deferring_access_checks ();
12944 /* In a block scope, a valid declaration must always have a
12945 decl-specifier-seq. By not trying to parse declarators, we can
12946 resolve the declaration/expression ambiguity more quickly. */
12947 if (!function_definition_allowed_p
12948 && !decl_specifiers
.any_specifiers_p
)
12950 cp_parser_error (parser
, "expected declaration");
12954 /* If the next two tokens are both identifiers, the code is
12955 erroneous. The usual cause of this situation is code like:
12959 where "T" should name a type -- but does not. */
12960 if (!decl_specifiers
.any_type_specifiers_p
12961 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12963 /* If parsing tentatively, we should commit; we really are
12964 looking at a declaration. */
12965 cp_parser_commit_to_tentative_parse (parser
);
12970 /* If we have seen at least one decl-specifier, and the next token
12971 is not a parenthesis, then we must be looking at a declaration.
12972 (After "int (" we might be looking at a functional cast.) */
12973 if (decl_specifiers
.any_specifiers_p
12974 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12975 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12976 && !cp_parser_error_occurred (parser
))
12977 cp_parser_commit_to_tentative_parse (parser
);
12979 /* Look for C++17 decomposition declaration. */
12980 for (size_t n
= 1; ; n
++)
12981 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
12982 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
12984 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
12985 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
12986 && decl_specifiers
.any_specifiers_p
)
12989 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
12990 maybe_range_for_decl
,
12993 /* The next token should be either a `,' or a `;'. */
12994 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12995 /* If it's a `;', we are done. */
12996 if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
12998 /* Anything else is an error. */
13001 /* If we have already issued an error message we don't need
13002 to issue another one. */
13003 if ((decl
!= error_mark_node
13004 && DECL_INITIAL (decl
) != error_mark_node
)
13005 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13006 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13007 /* Skip tokens until we reach the end of the statement. */
13008 cp_parser_skip_to_end_of_statement (parser
);
13009 /* If the next token is now a `;', consume it. */
13010 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13011 cp_lexer_consume_token (parser
->lexer
);
13019 bool auto_specifier_p
;
13020 /* NULL_TREE if both variable and function declaration are allowed,
13021 error_mark_node if function declaration are not allowed and
13022 a FUNCTION_DECL that should be diagnosed if it is followed by
13023 variable declarations. */
13024 tree auto_function_declaration
;
13026 last_type
= NULL_TREE
;
13028 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13029 auto_function_declaration
= NULL_TREE
;
13031 /* Keep going until we hit the `;' at the end of the simple
13033 saw_declarator
= false;
13034 while (cp_lexer_next_token_is_not (parser
->lexer
,
13038 bool function_definition_p
;
13040 tree auto_result
= NULL_TREE
;
13042 if (saw_declarator
)
13044 /* If we are processing next declarator, comma is expected */
13045 token
= cp_lexer_peek_token (parser
->lexer
);
13046 gcc_assert (token
->type
== CPP_COMMA
);
13047 cp_lexer_consume_token (parser
->lexer
);
13048 if (maybe_range_for_decl
)
13050 *maybe_range_for_decl
= error_mark_node
;
13051 if (comma_loc
== UNKNOWN_LOCATION
)
13052 comma_loc
= token
->location
;
13056 saw_declarator
= true;
13058 /* Parse the init-declarator. */
13059 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13061 function_definition_allowed_p
,
13062 /*member_p=*/false,
13063 declares_class_or_enum
,
13064 &function_definition_p
,
13065 maybe_range_for_decl
,
13068 /* If an error occurred while parsing tentatively, exit quickly.
13069 (That usually happens when in the body of a function; each
13070 statement is treated as a declaration-statement until proven
13072 if (cp_parser_error_occurred (parser
))
13075 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13077 /* If the init-declarator-list contains more than one
13078 init-declarator, they shall all form declarations of
13080 if (auto_function_declaration
== NULL_TREE
)
13081 auto_function_declaration
13082 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13083 else if (TREE_CODE (decl
) == FUNCTION_DECL
13084 || auto_function_declaration
!= error_mark_node
)
13086 error_at (decl_specifiers
.locations
[ds_type_spec
],
13087 "non-variable %qD in declaration with more than one "
13088 "declarator with placeholder type",
13089 TREE_CODE (decl
) == FUNCTION_DECL
13090 ? decl
: auto_function_declaration
);
13091 auto_function_declaration
= error_mark_node
;
13096 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13099 && last_type
!= error_mark_node
13100 && !same_type_p (auto_result
, last_type
))
13102 /* If the list of declarators contains more than one declarator,
13103 the type of each declared variable is determined as described
13104 above. If the type deduced for the template parameter U is not
13105 the same in each deduction, the program is ill-formed. */
13106 error_at (decl_specifiers
.locations
[ds_type_spec
],
13107 "inconsistent deduction for %qT: %qT and then %qT",
13108 decl_specifiers
.type
, last_type
, auto_result
);
13109 last_type
= error_mark_node
;
13112 last_type
= auto_result
;
13115 /* Handle function definitions specially. */
13116 if (function_definition_p
)
13118 /* If the next token is a `,', then we are probably
13119 processing something like:
13123 which is erroneous. */
13124 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13126 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13127 error_at (token
->location
,
13129 " declarations and function-definitions is forbidden");
13131 /* Otherwise, we're done with the list of declarators. */
13134 pop_deferring_access_checks ();
13138 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13139 *maybe_range_for_decl
= decl
;
13140 /* The next token should be either a `,' or a `;'. */
13141 token
= cp_lexer_peek_token (parser
->lexer
);
13142 /* If it's a `,', there are more declarators to come. */
13143 if (token
->type
== CPP_COMMA
)
13144 /* will be consumed next time around */;
13145 /* If it's a `;', we are done. */
13146 else if (token
->type
== CPP_SEMICOLON
)
13148 else if (maybe_range_for_decl
)
13150 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13151 permerror (decl_specifiers
.locations
[ds_type_spec
],
13152 "types may not be defined in a for-range-declaration");
13155 /* Anything else is an error. */
13158 /* If we have already issued an error message we don't need
13159 to issue another one. */
13160 if ((decl
!= error_mark_node
13161 && DECL_INITIAL (decl
) != error_mark_node
)
13162 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13163 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13164 /* Skip tokens until we reach the end of the statement. */
13165 cp_parser_skip_to_end_of_statement (parser
);
13166 /* If the next token is now a `;', consume it. */
13167 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13168 cp_lexer_consume_token (parser
->lexer
);
13171 /* After the first time around, a function-definition is not
13172 allowed -- even if it was OK at first. For example:
13177 function_definition_allowed_p
= false;
13180 /* Issue an error message if no declarators are present, and the
13181 decl-specifier-seq does not itself declare a class or
13182 enumeration: [dcl.dcl]/3. */
13183 if (!saw_declarator
)
13185 if (cp_parser_declares_only_class_p (parser
))
13187 if (!declares_class_or_enum
13188 && decl_specifiers
.type
13189 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13190 /* Ensure an error is issued anyway when finish_decltype_type,
13191 called via cp_parser_decl_specifier_seq, returns a class or
13192 an enumeration (c++/51786). */
13193 decl_specifiers
.type
= NULL_TREE
;
13194 shadow_tag (&decl_specifiers
);
13196 /* Perform any deferred access checks. */
13197 perform_deferred_access_checks (tf_warning_or_error
);
13200 /* Consume the `;'. */
13202 if (!maybe_range_for_decl
)
13203 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13204 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13206 if (init_loc
!= UNKNOWN_LOCATION
)
13207 error_at (init_loc
, "initializer in range-based %<for%> loop");
13208 if (comma_loc
!= UNKNOWN_LOCATION
)
13209 error_at (comma_loc
,
13210 "multiple declarations in range-based %<for%> loop");
13214 pop_deferring_access_checks ();
13217 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13218 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13222 cp_parser_decomposition_declaration (cp_parser
*parser
,
13223 cp_decl_specifier_seq
*decl_specifiers
,
13224 tree
*maybe_range_for_decl
,
13225 location_t
*init_loc
)
13227 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13228 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13229 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13231 /* Parse the identifier-list. */
13232 auto_vec
<cp_expr
, 10> v
;
13233 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13236 cp_expr e
= cp_parser_identifier (parser
);
13237 if (e
.get_value () == error_mark_node
)
13240 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13242 cp_lexer_consume_token (parser
->lexer
);
13245 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13246 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13248 end_loc
= UNKNOWN_LOCATION
;
13249 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13252 cp_lexer_consume_token (parser
->lexer
);
13255 cp_parser_skip_to_end_of_statement (parser
);
13256 return error_mark_node
;
13260 if (cxx_dialect
< cxx17
)
13261 pedwarn (loc
, 0, "structured bindings only available with "
13262 "-std=c++17 or -std=gnu++17");
13265 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13266 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13267 declarator
->id_loc
= loc
;
13268 if (ref_qual
!= REF_QUAL_NONE
)
13269 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13270 ref_qual
== REF_QUAL_RVALUE
,
13272 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13273 NULL_TREE
, decl_specifiers
->attributes
,
13275 tree orig_decl
= decl
;
13279 cp_decl_specifier_seq decl_specs
;
13280 clear_decl_specs (&decl_specs
);
13281 decl_specs
.type
= make_auto ();
13283 FOR_EACH_VEC_ELT (v
, i
, e
)
13286 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13288 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13289 declarator
->id_loc
= e
.get_location ();
13290 tree elt_pushed_scope
;
13291 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13292 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13293 if (decl2
== error_mark_node
)
13294 decl
= error_mark_node
;
13295 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13297 /* Ensure we've diagnosed redeclaration if we aren't creating
13299 gcc_assert (errorcount
);
13300 decl
= error_mark_node
;
13304 if (elt_pushed_scope
)
13305 pop_scope (elt_pushed_scope
);
13310 error_at (loc
, "empty structured binding declaration");
13311 decl
= error_mark_node
;
13314 if (maybe_range_for_decl
== NULL
13315 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13317 bool non_constant_p
= false, is_direct_init
= false;
13318 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13319 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13321 if (initializer
== NULL_TREE
13322 || (TREE_CODE (initializer
) == TREE_LIST
13323 && TREE_CHAIN (initializer
))
13325 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13326 && CONSTRUCTOR_NELTS (initializer
) != 1))
13328 error_at (loc
, "invalid initializer for structured binding "
13330 initializer
= error_mark_node
;
13333 if (decl
!= error_mark_node
)
13335 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13336 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13337 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13338 cp_finish_decomp (decl
, prev
, v
.length ());
13341 else if (decl
!= error_mark_node
)
13343 *maybe_range_for_decl
= prev
;
13344 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13345 the underlying DECL. */
13346 cp_finish_decomp (decl
, prev
, v
.length ());
13350 pop_scope (pushed_scope
);
13352 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13354 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13355 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13361 /* Parse a decl-specifier-seq.
13363 decl-specifier-seq:
13364 decl-specifier-seq [opt] decl-specifier
13365 decl-specifier attribute-specifier-seq [opt] (C++11)
13368 storage-class-specifier
13379 Concepts Extension:
13384 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13386 The parser flags FLAGS is used to control type-specifier parsing.
13388 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13391 1: one of the decl-specifiers is an elaborated-type-specifier
13392 (i.e., a type declaration)
13393 2: one of the decl-specifiers is an enum-specifier or a
13394 class-specifier (i.e., a type definition)
13399 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13400 cp_parser_flags flags
,
13401 cp_decl_specifier_seq
*decl_specs
,
13402 int* declares_class_or_enum
)
13404 bool constructor_possible_p
= !parser
->in_declarator_p
;
13405 bool found_decl_spec
= false;
13406 cp_token
*start_token
= NULL
;
13409 /* Clear DECL_SPECS. */
13410 clear_decl_specs (decl_specs
);
13412 /* Assume no class or enumeration type is declared. */
13413 *declares_class_or_enum
= 0;
13415 /* Keep reading specifiers until there are no more to read. */
13418 bool constructor_p
;
13422 /* Peek at the next token. */
13423 token
= cp_lexer_peek_token (parser
->lexer
);
13425 /* Save the first token of the decl spec list for error
13428 start_token
= token
;
13429 /* Handle attributes. */
13430 if (cp_next_tokens_can_be_attribute_p (parser
))
13432 /* Parse the attributes. */
13433 tree attrs
= cp_parser_attributes_opt (parser
);
13435 /* In a sequence of declaration specifiers, c++11 attributes
13436 appertain to the type that precede them. In that case
13439 The attribute-specifier-seq affects the type only for
13440 the declaration it appears in, not other declarations
13441 involving the same type.
13443 But for now let's force the user to position the
13444 attribute either at the beginning of the declaration or
13445 after the declarator-id, which would clearly mean that it
13446 applies to the declarator. */
13447 if (cxx11_attribute_p (attrs
))
13449 if (!found_decl_spec
)
13450 /* The c++11 attribute is at the beginning of the
13451 declaration. It appertains to the entity being
13455 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13457 /* This is an attribute following a
13458 class-specifier. */
13459 if (decl_specs
->type_definition_p
)
13460 warn_misplaced_attr_for_class_type (token
->location
,
13466 decl_specs
->std_attributes
13467 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13468 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13469 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13475 decl_specs
->attributes
13476 = attr_chainon (decl_specs
->attributes
, attrs
);
13477 if (decl_specs
->locations
[ds_attribute
] == 0)
13478 decl_specs
->locations
[ds_attribute
] = token
->location
;
13481 /* Assume we will find a decl-specifier keyword. */
13482 found_decl_spec
= true;
13483 /* If the next token is an appropriate keyword, we can simply
13484 add it to the list. */
13485 switch (token
->keyword
)
13491 if (!at_class_scope_p ())
13493 gcc_rich_location
richloc (token
->location
);
13494 richloc
.add_fixit_remove ();
13495 error_at (&richloc
, "%<friend%> used outside of class");
13496 cp_lexer_purge_token (parser
->lexer
);
13501 /* Consume the token. */
13502 cp_lexer_consume_token (parser
->lexer
);
13506 case RID_CONSTEXPR
:
13508 cp_lexer_consume_token (parser
->lexer
);
13513 cp_lexer_consume_token (parser
->lexer
);
13516 /* function-specifier:
13523 cp_parser_function_specifier_opt (parser
, decl_specs
);
13530 /* Consume the token. */
13531 cp_lexer_consume_token (parser
->lexer
);
13532 /* A constructor declarator cannot appear in a typedef. */
13533 constructor_possible_p
= false;
13534 /* The "typedef" keyword can only occur in a declaration; we
13535 may as well commit at this point. */
13536 cp_parser_commit_to_tentative_parse (parser
);
13538 if (decl_specs
->storage_class
!= sc_none
)
13539 decl_specs
->conflicting_specifiers_p
= true;
13542 /* storage-class-specifier:
13552 if (cxx_dialect
== cxx98
)
13554 /* Consume the token. */
13555 cp_lexer_consume_token (parser
->lexer
);
13557 /* Complain about `auto' as a storage specifier, if
13558 we're complaining about C++0x compatibility. */
13559 gcc_rich_location
richloc (token
->location
);
13560 richloc
.add_fixit_remove ();
13561 warning_at (&richloc
, OPT_Wc__11_compat
,
13562 "%<auto%> changes meaning in C++11; "
13563 "please remove it");
13565 /* Set the storage class anyway. */
13566 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13570 /* C++0x auto type-specifier. */
13571 found_decl_spec
= false;
13578 /* Consume the token. */
13579 cp_lexer_consume_token (parser
->lexer
);
13580 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13584 /* Consume the token. */
13586 cp_lexer_consume_token (parser
->lexer
);
13590 /* We did not yet find a decl-specifier yet. */
13591 found_decl_spec
= false;
13595 if (found_decl_spec
13596 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13597 && token
->keyword
!= RID_CONSTEXPR
)
13598 error ("decl-specifier invalid in condition");
13600 if (found_decl_spec
13601 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13602 && token
->keyword
!= RID_MUTABLE
13603 && token
->keyword
!= RID_CONSTEXPR
)
13604 error_at (token
->location
, "%qD invalid in lambda",
13605 ridpointers
[token
->keyword
]);
13608 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13610 /* Constructors are a special case. The `S' in `S()' is not a
13611 decl-specifier; it is the beginning of the declarator. */
13613 = (!found_decl_spec
13614 && constructor_possible_p
13615 && (cp_parser_constructor_declarator_p
13616 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13618 /* If we don't have a DECL_SPEC yet, then we must be looking at
13619 a type-specifier. */
13620 if (!found_decl_spec
&& !constructor_p
)
13622 int decl_spec_declares_class_or_enum
;
13623 bool is_cv_qualifier
;
13627 = cp_parser_type_specifier (parser
, flags
,
13629 /*is_declaration=*/true,
13630 &decl_spec_declares_class_or_enum
,
13632 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13634 /* If this type-specifier referenced a user-defined type
13635 (a typedef, class-name, etc.), then we can't allow any
13636 more such type-specifiers henceforth.
13640 The longest sequence of decl-specifiers that could
13641 possibly be a type name is taken as the
13642 decl-specifier-seq of a declaration. The sequence shall
13643 be self-consistent as described below.
13647 As a general rule, at most one type-specifier is allowed
13648 in the complete decl-specifier-seq of a declaration. The
13649 only exceptions are the following:
13651 -- const or volatile can be combined with any other
13654 -- signed or unsigned can be combined with char, long,
13662 void g (const int Pc);
13664 Here, Pc is *not* part of the decl-specifier seq; it's
13665 the declarator. Therefore, once we see a type-specifier
13666 (other than a cv-qualifier), we forbid any additional
13667 user-defined types. We *do* still allow things like `int
13668 int' to be considered a decl-specifier-seq, and issue the
13669 error message later. */
13670 if (type_spec
&& !is_cv_qualifier
)
13671 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13672 /* A constructor declarator cannot follow a type-specifier. */
13675 constructor_possible_p
= false;
13676 found_decl_spec
= true;
13677 if (!is_cv_qualifier
)
13678 decl_specs
->any_type_specifiers_p
= true;
13682 /* If we still do not have a DECL_SPEC, then there are no more
13683 decl-specifiers. */
13684 if (!found_decl_spec
)
13687 decl_specs
->any_specifiers_p
= true;
13688 /* After we see one decl-specifier, further decl-specifiers are
13689 always optional. */
13690 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13693 /* Don't allow a friend specifier with a class definition. */
13694 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13695 && (*declares_class_or_enum
& 2))
13696 error_at (decl_specs
->locations
[ds_friend
],
13697 "class definition may not be declared a friend");
13700 /* Parse an (optional) storage-class-specifier.
13702 storage-class-specifier:
13711 storage-class-specifier:
13714 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13717 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13719 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13722 if (cxx_dialect
!= cxx98
)
13724 /* Fall through for C++98. */
13725 gcc_fallthrough ();
13732 /* Consume the token. */
13733 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13740 /* Parse an (optional) function-specifier.
13742 function-specifier:
13747 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13748 Updates DECL_SPECS, if it is non-NULL. */
13751 cp_parser_function_specifier_opt (cp_parser
* parser
,
13752 cp_decl_specifier_seq
*decl_specs
)
13754 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13755 switch (token
->keyword
)
13758 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13762 /* 14.5.2.3 [temp.mem]
13764 A member function template shall not be virtual. */
13765 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13766 && current_class_type
)
13767 error_at (token
->location
, "templates may not be %<virtual%>");
13769 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13773 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13780 /* Consume the token. */
13781 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13784 /* Parse a linkage-specification.
13786 linkage-specification:
13787 extern string-literal { declaration-seq [opt] }
13788 extern string-literal declaration */
13791 cp_parser_linkage_specification (cp_parser
* parser
)
13795 /* Look for the `extern' keyword. */
13796 cp_token
*extern_token
13797 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13799 /* Look for the string-literal. */
13800 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13801 linkage
= cp_parser_string_literal (parser
, false, false);
13803 /* Transform the literal into an identifier. If the literal is a
13804 wide-character string, or contains embedded NULs, then we can't
13805 handle it as the user wants. */
13806 if (strlen (TREE_STRING_POINTER (linkage
))
13807 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13809 cp_parser_error (parser
, "invalid linkage-specification");
13810 /* Assume C++ linkage. */
13811 linkage
= lang_name_cplusplus
;
13814 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13816 /* We're now using the new linkage. */
13817 push_lang_context (linkage
);
13819 /* Preserve the location of the the innermost linkage specification,
13820 tracking the locations of nested specifications via a local. */
13821 location_t saved_location
13822 = parser
->innermost_linkage_specification_location
;
13823 /* Construct a location ranging from the start of the "extern" to
13824 the end of the string-literal, with the caret at the start, e.g.:
13828 parser
->innermost_linkage_specification_location
13829 = make_location (extern_token
->location
,
13830 extern_token
->location
,
13831 get_finish (string_token
->location
));
13833 /* If the next token is a `{', then we're using the first
13835 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13837 cp_ensure_no_omp_declare_simd (parser
);
13838 cp_ensure_no_oacc_routine (parser
);
13840 /* Consume the `{' token. */
13841 matching_braces braces
;
13842 braces
.consume_open (parser
)->location
;
13843 /* Parse the declarations. */
13844 cp_parser_declaration_seq_opt (parser
);
13845 /* Look for the closing `}'. */
13846 braces
.require_close (parser
);
13848 /* Otherwise, there's just one declaration. */
13851 bool saved_in_unbraced_linkage_specification_p
;
13853 saved_in_unbraced_linkage_specification_p
13854 = parser
->in_unbraced_linkage_specification_p
;
13855 parser
->in_unbraced_linkage_specification_p
= true;
13856 cp_parser_declaration (parser
);
13857 parser
->in_unbraced_linkage_specification_p
13858 = saved_in_unbraced_linkage_specification_p
;
13861 /* We're done with the linkage-specification. */
13862 pop_lang_context ();
13864 /* Restore location of parent linkage specification, if any. */
13865 parser
->innermost_linkage_specification_location
= saved_location
;
13868 /* Parse a static_assert-declaration.
13870 static_assert-declaration:
13871 static_assert ( constant-expression , string-literal ) ;
13872 static_assert ( constant-expression ) ; (C++17)
13874 If MEMBER_P, this static_assert is a class member. */
13877 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13880 location_t token_loc
;
13884 /* Peek at the `static_assert' token so we can keep track of exactly
13885 where the static assertion started. */
13886 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13888 /* Look for the `static_assert' keyword. */
13889 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13893 /* We know we are in a static assertion; commit to any tentative
13895 if (cp_parser_parsing_tentatively (parser
))
13896 cp_parser_commit_to_tentative_parse (parser
);
13898 /* Parse the `(' starting the static assertion condition. */
13899 matching_parens parens
;
13900 parens
.require_open (parser
);
13902 /* Parse the constant-expression. Allow a non-constant expression
13903 here in order to give better diagnostics in finish_static_assert. */
13905 cp_parser_constant_expression (parser
,
13906 /*allow_non_constant_p=*/true,
13907 /*non_constant_p=*/&dummy
);
13909 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13911 if (cxx_dialect
< cxx17
)
13912 pedwarn (input_location
, OPT_Wpedantic
,
13913 "static_assert without a message "
13914 "only available with -std=c++17 or -std=gnu++17");
13916 cp_lexer_consume_token (parser
->lexer
);
13917 message
= build_string (1, "");
13918 TREE_TYPE (message
) = char_array_type_node
;
13919 fix_string_type (message
);
13923 /* Parse the separating `,'. */
13924 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13926 /* Parse the string-literal message. */
13927 message
= cp_parser_string_literal (parser
,
13928 /*translate=*/false,
13931 /* A `)' completes the static assertion. */
13932 if (!parens
.require_close (parser
))
13933 cp_parser_skip_to_closing_parenthesis (parser
,
13934 /*recovering=*/true,
13935 /*or_comma=*/false,
13936 /*consume_paren=*/true);
13939 /* A semicolon terminates the declaration. */
13940 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13942 /* Get the location for the static assertion. Use that of the
13943 condition if available, otherwise, use that of the "static_assert"
13945 location_t assert_loc
= condition
.get_location ();
13946 if (assert_loc
== UNKNOWN_LOCATION
)
13947 assert_loc
= token_loc
;
13949 /* Complete the static assertion, which may mean either processing
13950 the static assert now or saving it for template instantiation. */
13951 finish_static_assert (condition
, message
, assert_loc
, member_p
);
13954 /* Parse the expression in decltype ( expression ). */
13957 cp_parser_decltype_expr (cp_parser
*parser
,
13958 bool &id_expression_or_member_access_p
)
13960 cp_token
*id_expr_start_token
;
13963 /* Since we're going to preserve any side-effects from this parse, set up a
13964 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13965 in the expression. */
13966 tentative_firewall
firewall (parser
);
13968 /* First, try parsing an id-expression. */
13969 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
13970 cp_parser_parse_tentatively (parser
);
13971 expr
= cp_parser_id_expression (parser
,
13972 /*template_keyword_p=*/false,
13973 /*check_dependency_p=*/true,
13974 /*template_p=*/NULL
,
13975 /*declarator_p=*/false,
13976 /*optional_p=*/false);
13978 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
13980 bool non_integral_constant_expression_p
= false;
13981 tree id_expression
= expr
;
13983 const char *error_msg
;
13985 if (identifier_p (expr
))
13986 /* Lookup the name we got back from the id-expression. */
13987 expr
= cp_parser_lookup_name_simple (parser
, expr
,
13988 id_expr_start_token
->location
);
13990 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
13991 /* A template without args is not a complete id-expression. */
13992 expr
= error_mark_node
;
13995 && expr
!= error_mark_node
13996 && TREE_CODE (expr
) != TYPE_DECL
13997 && (TREE_CODE (expr
) != BIT_NOT_EXPR
13998 || !TYPE_P (TREE_OPERAND (expr
, 0)))
13999 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14001 /* Complete lookup of the id-expression. */
14002 expr
= (finish_id_expression
14003 (id_expression
, expr
, parser
->scope
, &idk
,
14004 /*integral_constant_expression_p=*/false,
14005 /*allow_non_integral_constant_expression_p=*/true,
14006 &non_integral_constant_expression_p
,
14007 /*template_p=*/false,
14009 /*address_p=*/false,
14010 /*template_arg_p=*/false,
14012 id_expr_start_token
->location
));
14014 if (expr
== error_mark_node
)
14015 /* We found an id-expression, but it was something that we
14016 should not have found. This is an error, not something
14017 we can recover from, so note that we found an
14018 id-expression and we'll recover as gracefully as
14020 id_expression_or_member_access_p
= true;
14024 && expr
!= error_mark_node
14025 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14026 /* We have an id-expression. */
14027 id_expression_or_member_access_p
= true;
14030 if (!id_expression_or_member_access_p
)
14032 /* Abort the id-expression parse. */
14033 cp_parser_abort_tentative_parse (parser
);
14035 /* Parsing tentatively, again. */
14036 cp_parser_parse_tentatively (parser
);
14038 /* Parse a class member access. */
14039 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14040 /*cast_p=*/false, /*decltype*/true,
14041 /*member_access_only_p=*/true, NULL
);
14044 && expr
!= error_mark_node
14045 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14046 /* We have an id-expression. */
14047 id_expression_or_member_access_p
= true;
14050 if (id_expression_or_member_access_p
)
14051 /* We have parsed the complete id-expression or member access. */
14052 cp_parser_parse_definitely (parser
);
14055 /* Abort our attempt to parse an id-expression or member access
14057 cp_parser_abort_tentative_parse (parser
);
14059 /* Commit to the tentative_firewall so we get syntax errors. */
14060 cp_parser_commit_to_tentative_parse (parser
);
14062 /* Parse a full expression. */
14063 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14064 /*decltype_p=*/true);
14070 /* Parse a `decltype' type. Returns the type.
14072 simple-type-specifier:
14073 decltype ( expression )
14075 decltype ( auto ) */
14078 cp_parser_decltype (cp_parser
*parser
)
14080 bool id_expression_or_member_access_p
= false;
14081 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14083 if (start_token
->type
== CPP_DECLTYPE
)
14085 /* Already parsed. */
14086 cp_lexer_consume_token (parser
->lexer
);
14087 return saved_checks_value (start_token
->u
.tree_check_value
);
14090 /* Look for the `decltype' token. */
14091 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14092 return error_mark_node
;
14094 /* Parse the opening `('. */
14095 matching_parens parens
;
14096 if (!parens
.require_open (parser
))
14097 return error_mark_node
;
14099 push_deferring_access_checks (dk_deferred
);
14101 tree expr
= NULL_TREE
;
14103 if (cxx_dialect
>= cxx14
14104 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14105 /* decltype (auto) */
14106 cp_lexer_consume_token (parser
->lexer
);
14109 /* decltype (expression) */
14111 /* Types cannot be defined in a `decltype' expression. Save away the
14112 old message and set the new one. */
14113 const char *saved_message
= parser
->type_definition_forbidden_message
;
14114 parser
->type_definition_forbidden_message
14115 = G_("types may not be defined in %<decltype%> expressions");
14117 /* The restrictions on constant-expressions do not apply inside
14118 decltype expressions. */
14119 bool saved_integral_constant_expression_p
14120 = parser
->integral_constant_expression_p
;
14121 bool saved_non_integral_constant_expression_p
14122 = parser
->non_integral_constant_expression_p
;
14123 parser
->integral_constant_expression_p
= false;
14125 /* Within a parenthesized expression, a `>' token is always
14126 the greater-than operator. */
14127 bool saved_greater_than_is_operator_p
14128 = parser
->greater_than_is_operator_p
;
14129 parser
->greater_than_is_operator_p
= true;
14131 /* Do not actually evaluate the expression. */
14132 ++cp_unevaluated_operand
;
14134 /* Do not warn about problems with the expression. */
14135 ++c_inhibit_evaluation_warnings
;
14137 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14139 /* Go back to evaluating expressions. */
14140 --cp_unevaluated_operand
;
14141 --c_inhibit_evaluation_warnings
;
14143 /* The `>' token might be the end of a template-id or
14144 template-parameter-list now. */
14145 parser
->greater_than_is_operator_p
14146 = saved_greater_than_is_operator_p
;
14148 /* Restore the old message and the integral constant expression
14150 parser
->type_definition_forbidden_message
= saved_message
;
14151 parser
->integral_constant_expression_p
14152 = saved_integral_constant_expression_p
;
14153 parser
->non_integral_constant_expression_p
14154 = saved_non_integral_constant_expression_p
;
14157 /* Parse to the closing `)'. */
14158 if (!parens
.require_close (parser
))
14160 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14161 /*consume_paren=*/true);
14162 pop_deferring_access_checks ();
14163 return error_mark_node
;
14169 expr
= make_decltype_auto ();
14170 AUTO_IS_DECLTYPE (expr
) = true;
14173 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14174 tf_warning_or_error
);
14176 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14178 start_token
->type
= CPP_DECLTYPE
;
14179 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14180 start_token
->u
.tree_check_value
->value
= expr
;
14181 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14182 start_token
->keyword
= RID_MAX
;
14183 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14185 pop_to_parent_deferring_access_checks ();
14190 /* Special member functions [gram.special] */
14192 /* Parse a conversion-function-id.
14194 conversion-function-id:
14195 operator conversion-type-id
14197 Returns an IDENTIFIER_NODE representing the operator. */
14200 cp_parser_conversion_function_id (cp_parser
* parser
)
14204 tree saved_qualifying_scope
;
14205 tree saved_object_scope
;
14206 tree pushed_scope
= NULL_TREE
;
14208 /* Look for the `operator' token. */
14209 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14210 return error_mark_node
;
14211 /* When we parse the conversion-type-id, the current scope will be
14212 reset. However, we need that information in able to look up the
14213 conversion function later, so we save it here. */
14214 saved_scope
= parser
->scope
;
14215 saved_qualifying_scope
= parser
->qualifying_scope
;
14216 saved_object_scope
= parser
->object_scope
;
14217 /* We must enter the scope of the class so that the names of
14218 entities declared within the class are available in the
14219 conversion-type-id. For example, consider:
14226 S::operator I() { ... }
14228 In order to see that `I' is a type-name in the definition, we
14229 must be in the scope of `S'. */
14231 pushed_scope
= push_scope (saved_scope
);
14232 /* Parse the conversion-type-id. */
14233 type
= cp_parser_conversion_type_id (parser
);
14234 /* Leave the scope of the class, if any. */
14236 pop_scope (pushed_scope
);
14237 /* Restore the saved scope. */
14238 parser
->scope
= saved_scope
;
14239 parser
->qualifying_scope
= saved_qualifying_scope
;
14240 parser
->object_scope
= saved_object_scope
;
14241 /* If the TYPE is invalid, indicate failure. */
14242 if (type
== error_mark_node
)
14243 return error_mark_node
;
14244 return make_conv_op_name (type
);
14247 /* Parse a conversion-type-id:
14249 conversion-type-id:
14250 type-specifier-seq conversion-declarator [opt]
14252 Returns the TYPE specified. */
14255 cp_parser_conversion_type_id (cp_parser
* parser
)
14258 cp_decl_specifier_seq type_specifiers
;
14259 cp_declarator
*declarator
;
14260 tree type_specified
;
14261 const char *saved_message
;
14263 /* Parse the attributes. */
14264 attributes
= cp_parser_attributes_opt (parser
);
14266 saved_message
= parser
->type_definition_forbidden_message
;
14267 parser
->type_definition_forbidden_message
14268 = G_("types may not be defined in a conversion-type-id");
14270 /* Parse the type-specifiers. */
14271 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14272 /*is_trailing_return=*/false,
14275 parser
->type_definition_forbidden_message
= saved_message
;
14277 /* If that didn't work, stop. */
14278 if (type_specifiers
.type
== error_mark_node
)
14279 return error_mark_node
;
14280 /* Parse the conversion-declarator. */
14281 declarator
= cp_parser_conversion_declarator_opt (parser
);
14283 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14284 /*initialized=*/0, &attributes
);
14286 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14288 /* Don't give this error when parsing tentatively. This happens to
14289 work because we always parse this definitively once. */
14290 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14291 && type_uses_auto (type_specified
))
14293 if (cxx_dialect
< cxx14
)
14295 error ("invalid use of %<auto%> in conversion operator");
14296 return error_mark_node
;
14298 else if (template_parm_scope_p ())
14299 warning (0, "use of %<auto%> in member template "
14300 "conversion operator can never be deduced");
14303 return type_specified
;
14306 /* Parse an (optional) conversion-declarator.
14308 conversion-declarator:
14309 ptr-operator conversion-declarator [opt]
14313 static cp_declarator
*
14314 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14316 enum tree_code code
;
14317 tree class_type
, std_attributes
= NULL_TREE
;
14318 cp_cv_quals cv_quals
;
14320 /* We don't know if there's a ptr-operator next, or not. */
14321 cp_parser_parse_tentatively (parser
);
14322 /* Try the ptr-operator. */
14323 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14325 /* If it worked, look for more conversion-declarators. */
14326 if (cp_parser_parse_definitely (parser
))
14328 cp_declarator
*declarator
;
14330 /* Parse another optional declarator. */
14331 declarator
= cp_parser_conversion_declarator_opt (parser
);
14333 declarator
= cp_parser_make_indirect_declarator
14334 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14342 /* Parse an (optional) ctor-initializer.
14345 : mem-initializer-list */
14348 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14350 /* If the next token is not a `:', then there is no
14351 ctor-initializer. */
14352 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14354 /* Do default initialization of any bases and members. */
14355 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14356 finish_mem_initializers (NULL_TREE
);
14360 /* Consume the `:' token. */
14361 cp_lexer_consume_token (parser
->lexer
);
14362 /* And the mem-initializer-list. */
14363 cp_parser_mem_initializer_list (parser
);
14366 /* Parse a mem-initializer-list.
14368 mem-initializer-list:
14369 mem-initializer ... [opt]
14370 mem-initializer ... [opt] , mem-initializer-list */
14373 cp_parser_mem_initializer_list (cp_parser
* parser
)
14375 tree mem_initializer_list
= NULL_TREE
;
14376 tree target_ctor
= error_mark_node
;
14377 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14379 /* Let the semantic analysis code know that we are starting the
14380 mem-initializer-list. */
14381 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14382 error_at (token
->location
,
14383 "only constructors take member initializers");
14385 /* Loop through the list. */
14388 tree mem_initializer
;
14390 token
= cp_lexer_peek_token (parser
->lexer
);
14391 /* Parse the mem-initializer. */
14392 mem_initializer
= cp_parser_mem_initializer (parser
);
14393 /* If the next token is a `...', we're expanding member initializers. */
14394 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14396 /* Consume the `...'. */
14397 cp_lexer_consume_token (parser
->lexer
);
14399 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14400 can be expanded but members cannot. */
14401 if (mem_initializer
!= error_mark_node
14402 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14404 error_at (token
->location
,
14405 "cannot expand initializer for member %qD",
14406 TREE_PURPOSE (mem_initializer
));
14407 mem_initializer
= error_mark_node
;
14410 /* Construct the pack expansion type. */
14411 if (mem_initializer
!= error_mark_node
)
14412 mem_initializer
= make_pack_expansion (mem_initializer
);
14414 if (target_ctor
!= error_mark_node
14415 && mem_initializer
!= error_mark_node
)
14417 error ("mem-initializer for %qD follows constructor delegation",
14418 TREE_PURPOSE (mem_initializer
));
14419 mem_initializer
= error_mark_node
;
14421 /* Look for a target constructor. */
14422 if (mem_initializer
!= error_mark_node
14423 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14424 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14426 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14427 if (mem_initializer_list
)
14429 error ("constructor delegation follows mem-initializer for %qD",
14430 TREE_PURPOSE (mem_initializer_list
));
14431 mem_initializer
= error_mark_node
;
14433 target_ctor
= mem_initializer
;
14435 /* Add it to the list, unless it was erroneous. */
14436 if (mem_initializer
!= error_mark_node
)
14438 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14439 mem_initializer_list
= mem_initializer
;
14441 /* If the next token is not a `,', we're done. */
14442 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14444 /* Consume the `,' token. */
14445 cp_lexer_consume_token (parser
->lexer
);
14448 /* Perform semantic analysis. */
14449 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14450 finish_mem_initializers (mem_initializer_list
);
14453 /* Parse a mem-initializer.
14456 mem-initializer-id ( expression-list [opt] )
14457 mem-initializer-id braced-init-list
14462 ( expression-list [opt] )
14464 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14465 class) or FIELD_DECL (for a non-static data member) to initialize;
14466 the TREE_VALUE is the expression-list. An empty initialization
14467 list is represented by void_list_node. */
14470 cp_parser_mem_initializer (cp_parser
* parser
)
14472 tree mem_initializer_id
;
14473 tree expression_list
;
14475 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14477 /* Find out what is being initialized. */
14478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14480 permerror (token
->location
,
14481 "anachronistic old-style base class initializer");
14482 mem_initializer_id
= NULL_TREE
;
14486 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14487 if (mem_initializer_id
== error_mark_node
)
14488 return mem_initializer_id
;
14490 member
= expand_member_init (mem_initializer_id
);
14491 if (member
&& !DECL_P (member
))
14492 in_base_initializer
= 1;
14494 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14496 bool expr_non_constant_p
;
14497 cp_lexer_set_source_position (parser
->lexer
);
14498 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14499 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14500 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14501 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14505 vec
<tree
, va_gc
> *vec
;
14506 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14508 /*allow_expansion_p=*/true,
14509 /*non_constant_p=*/NULL
);
14511 return error_mark_node
;
14512 expression_list
= build_tree_list_vec (vec
);
14513 release_tree_vector (vec
);
14516 if (expression_list
== error_mark_node
)
14517 return error_mark_node
;
14518 if (!expression_list
)
14519 expression_list
= void_type_node
;
14521 in_base_initializer
= 0;
14523 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14526 /* Parse a mem-initializer-id.
14528 mem-initializer-id:
14529 :: [opt] nested-name-specifier [opt] class-name
14530 decltype-specifier (C++11)
14533 Returns a TYPE indicating the class to be initialized for the first
14534 production (and the second in C++11). Returns an IDENTIFIER_NODE
14535 indicating the data member to be initialized for the last production. */
14538 cp_parser_mem_initializer_id (cp_parser
* parser
)
14540 bool global_scope_p
;
14541 bool nested_name_specifier_p
;
14542 bool template_p
= false;
14545 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14547 /* `typename' is not allowed in this context ([temp.res]). */
14548 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14550 error_at (token
->location
,
14551 "keyword %<typename%> not allowed in this context (a qualified "
14552 "member initializer is implicitly a type)");
14553 cp_lexer_consume_token (parser
->lexer
);
14555 /* Look for the optional `::' operator. */
14557 = (cp_parser_global_scope_opt (parser
,
14558 /*current_scope_valid_p=*/false)
14560 /* Look for the optional nested-name-specifier. The simplest way to
14565 The keyword `typename' is not permitted in a base-specifier or
14566 mem-initializer; in these contexts a qualified name that
14567 depends on a template-parameter is implicitly assumed to be a
14570 is to assume that we have seen the `typename' keyword at this
14572 nested_name_specifier_p
14573 = (cp_parser_nested_name_specifier_opt (parser
,
14574 /*typename_keyword_p=*/true,
14575 /*check_dependency_p=*/true,
14577 /*is_declaration=*/true)
14579 if (nested_name_specifier_p
)
14580 template_p
= cp_parser_optional_template_keyword (parser
);
14581 /* If there is a `::' operator or a nested-name-specifier, then we
14582 are definitely looking for a class-name. */
14583 if (global_scope_p
|| nested_name_specifier_p
)
14584 return cp_parser_class_name (parser
,
14585 /*typename_keyword_p=*/true,
14586 /*template_keyword_p=*/template_p
,
14588 /*check_dependency_p=*/true,
14589 /*class_head_p=*/false,
14590 /*is_declaration=*/true);
14591 /* Otherwise, we could also be looking for an ordinary identifier. */
14592 cp_parser_parse_tentatively (parser
);
14593 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14594 /* Try a decltype-specifier. */
14595 id
= cp_parser_decltype (parser
);
14597 /* Otherwise, try a class-name. */
14598 id
= cp_parser_class_name (parser
,
14599 /*typename_keyword_p=*/true,
14600 /*template_keyword_p=*/false,
14602 /*check_dependency_p=*/true,
14603 /*class_head_p=*/false,
14604 /*is_declaration=*/true);
14605 /* If we found one, we're done. */
14606 if (cp_parser_parse_definitely (parser
))
14608 /* Otherwise, look for an ordinary identifier. */
14609 return cp_parser_identifier (parser
);
14612 /* Overloading [gram.over] */
14614 /* Parse an operator-function-id.
14616 operator-function-id:
14619 Returns an IDENTIFIER_NODE for the operator which is a
14620 human-readable spelling of the identifier, e.g., `operator +'. */
14623 cp_parser_operator_function_id (cp_parser
* parser
)
14625 /* Look for the `operator' keyword. */
14626 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14627 return error_mark_node
;
14628 /* And then the name of the operator itself. */
14629 return cp_parser_operator (parser
);
14632 /* Return an identifier node for a user-defined literal operator.
14633 The suffix identifier is chained to the operator name identifier. */
14636 cp_literal_operator_id (const char* name
)
14639 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14640 + strlen (name
) + 10);
14641 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14642 identifier
= get_identifier (buffer
);
14647 /* Parse an operator.
14650 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14651 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14652 || ++ -- , ->* -> () []
14659 Returns an IDENTIFIER_NODE for the operator which is a
14660 human-readable spelling of the identifier, e.g., `operator +'. */
14663 cp_parser_operator (cp_parser
* parser
)
14665 tree id
= NULL_TREE
;
14669 /* Peek at the next token. */
14670 token
= cp_lexer_peek_token (parser
->lexer
);
14672 location_t start_loc
= token
->location
;
14674 /* Figure out which operator we have. */
14675 enum tree_code op
= ERROR_MARK
;
14676 bool assop
= false;
14677 bool consumed
= false;
14678 switch (token
->type
)
14682 /* The keyword should be either `new' or `delete'. */
14683 if (token
->keyword
== RID_NEW
)
14685 else if (token
->keyword
== RID_DELETE
)
14690 /* Consume the `new' or `delete' token. */
14691 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14693 /* Peek at the next token. */
14694 token
= cp_lexer_peek_token (parser
->lexer
);
14695 /* If it's a `[' token then this is the array variant of the
14697 if (token
->type
== CPP_OPEN_SQUARE
)
14699 /* Consume the `[' token. */
14700 cp_lexer_consume_token (parser
->lexer
);
14701 /* Look for the `]' token. */
14702 if (cp_token
*close_token
14703 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14704 end_loc
= close_token
->location
;
14705 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14707 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14725 op
= TRUNC_DIV_EXPR
;
14729 op
= TRUNC_MOD_EXPR
;
14749 op
= TRUTH_NOT_EXPR
;
14782 op
= TRUNC_DIV_EXPR
;
14787 op
= TRUNC_MOD_EXPR
;
14813 case CPP_LSHIFT_EQ
:
14818 case CPP_RSHIFT_EQ
:
14835 case CPP_GREATER_EQ
:
14840 op
= TRUTH_ANDIF_EXPR
;
14844 op
= TRUTH_ORIF_EXPR
;
14847 case CPP_PLUS_PLUS
:
14848 op
= POSTINCREMENT_EXPR
;
14851 case CPP_MINUS_MINUS
:
14852 op
= PREDECREMENT_EXPR
;
14856 op
= COMPOUND_EXPR
;
14859 case CPP_DEREF_STAR
:
14864 op
= COMPONENT_REF
;
14867 case CPP_OPEN_PAREN
:
14869 /* Consume the `('. */
14870 matching_parens parens
;
14871 parens
.consume_open (parser
);
14872 /* Look for the matching `)'. */
14873 parens
.require_close (parser
);
14879 case CPP_OPEN_SQUARE
:
14880 /* Consume the `['. */
14881 cp_lexer_consume_token (parser
->lexer
);
14882 /* Look for the matching `]'. */
14883 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14888 case CPP_UTF8STRING
:
14889 case CPP_UTF8STRING_USERDEF
:
14896 case CPP_STRING_USERDEF
:
14897 case CPP_WSTRING_USERDEF
:
14898 case CPP_STRING16_USERDEF
:
14899 case CPP_STRING32_USERDEF
:
14901 tree str
, string_tree
;
14904 if (cxx_dialect
== cxx98
)
14905 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14907 /* Consume the string. */
14908 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14909 /*wide_ok=*/true, /*lookup_udlit=*/false);
14910 if (str
== error_mark_node
)
14911 return error_mark_node
;
14912 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
14914 string_tree
= USERDEF_LITERAL_VALUE (str
);
14915 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
14920 /* Look for the suffix identifier. */
14921 token
= cp_lexer_peek_token (parser
->lexer
);
14922 if (token
->type
== CPP_NAME
)
14923 id
= cp_parser_identifier (parser
);
14924 else if (token
->type
== CPP_KEYWORD
)
14926 error ("unexpected keyword;"
14927 " remove space between quotes and suffix identifier");
14928 return error_mark_node
;
14932 error ("expected suffix identifier");
14933 return error_mark_node
;
14936 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14937 (TREE_TYPE (TREE_TYPE (string_tree
))));
14938 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14941 error ("expected empty string after %<operator%> keyword");
14942 return error_mark_node
;
14944 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14947 error ("invalid encoding prefix in literal operator");
14948 return error_mark_node
;
14950 if (id
!= error_mark_node
)
14952 const char *name
= IDENTIFIER_POINTER (id
);
14953 id
= cp_literal_operator_id (name
);
14959 /* Anything else is an error. */
14963 /* If we have selected an identifier, we need to consume the
14965 if (op
!= ERROR_MARK
)
14967 id
= ovl_op_identifier (assop
, op
);
14969 cp_lexer_consume_token (parser
->lexer
);
14971 /* Otherwise, no valid operator name was present. */
14974 cp_parser_error (parser
, "expected operator");
14975 id
= error_mark_node
;
14978 return cp_expr (id
, start_loc
);
14981 /* Parse a template-declaration.
14983 template-declaration:
14984 export [opt] template < template-parameter-list > declaration
14986 If MEMBER_P is TRUE, this template-declaration occurs within a
14989 The grammar rule given by the standard isn't correct. What
14990 is really meant is:
14992 template-declaration:
14993 export [opt] template-parameter-list-seq
14994 decl-specifier-seq [opt] init-declarator [opt] ;
14995 export [opt] template-parameter-list-seq
14996 function-definition
14998 template-parameter-list-seq:
14999 template-parameter-list-seq [opt]
15000 template < template-parameter-list >
15002 Concept Extensions:
15004 template-parameter-list-seq:
15005 template < template-parameter-list > requires-clause [opt]
15008 requires logical-or-expression */
15011 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15013 /* Check for `export'. */
15014 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15016 /* Consume the `export' token. */
15017 cp_lexer_consume_token (parser
->lexer
);
15018 /* Warn that we do not support `export'. */
15019 warning (0, "keyword %<export%> not implemented, and will be ignored");
15022 cp_parser_template_declaration_after_export (parser
, member_p
);
15025 /* Parse a template-parameter-list.
15027 template-parameter-list:
15029 template-parameter-list , template-parameter
15031 Returns a TREE_LIST. Each node represents a template parameter.
15032 The nodes are connected via their TREE_CHAINs. */
15035 cp_parser_template_parameter_list (cp_parser
* parser
)
15037 tree parameter_list
= NULL_TREE
;
15039 begin_template_parm_list ();
15041 /* The loop below parses the template parms. We first need to know
15042 the total number of template parms to be able to compute proper
15043 canonical types of each dependent type. So after the loop, when
15044 we know the total number of template parms,
15045 end_template_parm_list computes the proper canonical types and
15046 fixes up the dependent types accordingly. */
15051 bool is_parameter_pack
;
15052 location_t parm_loc
;
15054 /* Parse the template-parameter. */
15055 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15056 parameter
= cp_parser_template_parameter (parser
,
15058 &is_parameter_pack
);
15059 /* Add it to the list. */
15060 if (parameter
!= error_mark_node
)
15061 parameter_list
= process_template_parm (parameter_list
,
15065 is_parameter_pack
);
15068 tree err_parm
= build_tree_list (parameter
, parameter
);
15069 parameter_list
= chainon (parameter_list
, err_parm
);
15072 /* If the next token is not a `,', we're done. */
15073 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15075 /* Otherwise, consume the `,' token. */
15076 cp_lexer_consume_token (parser
->lexer
);
15079 return end_template_parm_list (parameter_list
);
15082 /* Parse a introduction-list.
15085 introduced-parameter
15086 introduction-list , introduced-parameter
15088 introduced-parameter:
15089 ...[opt] identifier
15091 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15092 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15093 WILDCARD_DECL will also have DECL_NAME set and token location in
15094 DECL_SOURCE_LOCATION. */
15097 cp_parser_introduction_list (cp_parser
*parser
)
15099 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15103 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15105 cp_lexer_consume_token (parser
->lexer
);
15107 /* Build placeholder. */
15108 tree parm
= build_nt (WILDCARD_DECL
);
15109 DECL_SOURCE_LOCATION (parm
)
15110 = cp_lexer_peek_token (parser
->lexer
)->location
;
15111 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15112 WILDCARD_PACK_P (parm
) = is_pack
;
15113 vec_safe_push (introduction_vec
, parm
);
15115 /* If the next token is not a `,', we're done. */
15116 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15118 /* Otherwise, consume the `,' token. */
15119 cp_lexer_consume_token (parser
->lexer
);
15122 /* Convert the vec into a TREE_VEC. */
15123 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15126 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15127 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15129 release_tree_vector (introduction_vec
);
15130 return introduction_list
;
15133 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15134 is an abstract declarator. */
15136 static inline cp_declarator
*
15137 get_id_declarator (cp_declarator
*declarator
)
15139 cp_declarator
*d
= declarator
;
15140 while (d
&& d
->kind
!= cdk_id
)
15145 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15146 is an abstract declarator. */
15149 get_unqualified_id (cp_declarator
*declarator
)
15151 declarator
= get_id_declarator (declarator
);
15153 return declarator
->u
.id
.unqualified_name
;
15158 /* Returns true if DECL represents a constrained-parameter. */
15161 is_constrained_parameter (tree decl
)
15164 && TREE_CODE (decl
) == TYPE_DECL
15165 && CONSTRAINED_PARM_CONCEPT (decl
)
15166 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15169 /* Returns true if PARM declares a constrained-parameter. */
15172 is_constrained_parameter (cp_parameter_declarator
*parm
)
15174 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15177 /* Check that the type parameter is only a declarator-id, and that its
15178 type is not cv-qualified. */
15181 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15182 cp_parameter_declarator
*parm
)
15184 if (!parm
->declarator
)
15187 if (parm
->declarator
->kind
!= cdk_id
)
15189 cp_parser_error (parser
, "invalid constrained type parameter");
15193 /* Don't allow cv-qualified type parameters. */
15194 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15195 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15197 cp_parser_error (parser
, "cv-qualified type parameter");
15204 /* Finish parsing/processing a template type parameter and checking
15205 various restrictions. */
15208 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15210 cp_parameter_declarator
* parmdecl
)
15212 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15213 return finish_template_type_parm (class_type_node
, id
);
15215 return error_mark_node
;
15219 finish_constrained_template_template_parm (tree proto
, tree id
)
15221 /* FIXME: This should probably be copied, and we may need to adjust
15222 the template parameter depths. */
15223 tree saved_parms
= current_template_parms
;
15224 begin_template_parm_list ();
15225 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15226 end_template_parm_list ();
15228 tree parm
= finish_template_template_parm (class_type_node
, id
);
15229 current_template_parms
= saved_parms
;
15234 /* Finish parsing/processing a template template parameter by borrowing
15235 the template parameter list from the prototype parameter. */
15238 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15241 cp_parameter_declarator
*parmdecl
)
15243 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15244 return error_mark_node
;
15245 return finish_constrained_template_template_parm (proto
, id
);
15248 /* Create a new non-type template parameter from the given PARM
15252 constrained_non_type_template_parm (bool *is_non_type
,
15253 cp_parameter_declarator
*parm
)
15255 *is_non_type
= true;
15256 cp_declarator
*decl
= parm
->declarator
;
15257 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15258 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15259 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15262 /* Build a constrained template parameter based on the PARMDECL
15263 declarator. The type of PARMDECL is the constrained type, which
15264 refers to the prototype template parameter that ultimately
15265 specifies the type of the declared parameter. */
15268 finish_constrained_parameter (cp_parser
*parser
,
15269 cp_parameter_declarator
*parmdecl
,
15271 bool *is_parameter_pack
)
15273 tree decl
= parmdecl
->decl_specifiers
.type
;
15274 tree id
= get_unqualified_id (parmdecl
->declarator
);
15275 tree def
= parmdecl
->default_argument
;
15276 tree proto
= DECL_INITIAL (decl
);
15278 /* A template parameter constrained by a variadic concept shall also
15279 be declared as a template parameter pack. */
15280 bool is_variadic
= template_parameter_pack_p (proto
);
15281 if (is_variadic
&& !*is_parameter_pack
)
15282 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15284 /* Build the parameter. Return an error if the declarator was invalid. */
15286 if (TREE_CODE (proto
) == TYPE_DECL
)
15287 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15288 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15289 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15292 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15293 if (parm
== error_mark_node
)
15294 return error_mark_node
;
15296 /* Finish the parameter decl and create a node attaching the
15297 default argument and constraint. */
15298 parm
= build_tree_list (def
, parm
);
15299 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15304 /* Returns true if the parsed type actually represents the declaration
15305 of a type template-parameter. */
15308 declares_constrained_type_template_parameter (tree type
)
15310 return (is_constrained_parameter (type
)
15311 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15315 /* Returns true if the parsed type actually represents the declaration of
15316 a template template-parameter. */
15319 declares_constrained_template_template_parameter (tree type
)
15321 return (is_constrained_parameter (type
)
15322 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15325 /* Parse a default argument for a type template-parameter.
15326 Note that diagnostics are handled in cp_parser_template_parameter. */
15329 cp_parser_default_type_template_argument (cp_parser
*parser
)
15331 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15333 /* Consume the `=' token. */
15334 cp_lexer_consume_token (parser
->lexer
);
15336 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15338 /* Parse the default-argument. */
15339 push_deferring_access_checks (dk_no_deferred
);
15340 tree default_argument
= cp_parser_type_id (parser
);
15341 pop_deferring_access_checks ();
15343 if (flag_concepts
&& type_uses_auto (default_argument
))
15345 error_at (token
->location
,
15346 "invalid use of %<auto%> in default template argument");
15347 return error_mark_node
;
15350 return default_argument
;
15353 /* Parse a default argument for a template template-parameter. */
15356 cp_parser_default_template_template_argument (cp_parser
*parser
)
15358 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15362 /* Consume the `='. */
15363 cp_lexer_consume_token (parser
->lexer
);
15364 /* Parse the id-expression. */
15365 push_deferring_access_checks (dk_no_deferred
);
15366 /* save token before parsing the id-expression, for error
15368 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15369 tree default_argument
15370 = cp_parser_id_expression (parser
,
15371 /*template_keyword_p=*/false,
15372 /*check_dependency_p=*/true,
15373 /*template_p=*/&is_template
,
15374 /*declarator_p=*/false,
15375 /*optional_p=*/false);
15376 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15377 /* If the id-expression was a template-id that refers to
15378 a template-class, we already have the declaration here,
15379 so no further lookup is needed. */
15382 /* Look up the name. */
15384 = cp_parser_lookup_name (parser
, default_argument
,
15386 /*is_template=*/is_template
,
15387 /*is_namespace=*/false,
15388 /*check_dependency=*/true,
15389 /*ambiguous_decls=*/NULL
,
15391 /* See if the default argument is valid. */
15392 default_argument
= check_template_template_default_arg (default_argument
);
15393 pop_deferring_access_checks ();
15394 return default_argument
;
15397 /* Parse a template-parameter.
15399 template-parameter:
15401 parameter-declaration
15403 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15404 the parameter. The TREE_PURPOSE is the default value, if any.
15405 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15406 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15407 set to true iff this parameter is a parameter pack. */
15410 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15411 bool *is_parameter_pack
)
15414 cp_parameter_declarator
*parameter_declarator
;
15417 /* Assume it is a type parameter or a template parameter. */
15418 *is_non_type
= false;
15419 /* Assume it not a parameter pack. */
15420 *is_parameter_pack
= false;
15421 /* Peek at the next token. */
15422 token
= cp_lexer_peek_token (parser
->lexer
);
15423 /* If it is `template', we have a type-parameter. */
15424 if (token
->keyword
== RID_TEMPLATE
)
15425 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15426 /* If it is `class' or `typename' we do not know yet whether it is a
15427 type parameter or a non-type parameter. Consider:
15429 template <typename T, typename T::X X> ...
15433 template <class C, class D*> ...
15435 Here, the first parameter is a type parameter, and the second is
15436 a non-type parameter. We can tell by looking at the token after
15437 the identifier -- if it is a `,', `=', or `>' then we have a type
15439 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15441 /* Peek at the token after `class' or `typename'. */
15442 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15443 /* If it's an ellipsis, we have a template type parameter
15445 if (token
->type
== CPP_ELLIPSIS
)
15446 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15447 /* If it's an identifier, skip it. */
15448 if (token
->type
== CPP_NAME
)
15449 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15450 /* Now, see if the token looks like the end of a template
15452 if (token
->type
== CPP_COMMA
15453 || token
->type
== CPP_EQ
15454 || token
->type
== CPP_GREATER
)
15455 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15458 /* Otherwise, it is a non-type parameter or a constrained parameter.
15462 When parsing a default template-argument for a non-type
15463 template-parameter, the first non-nested `>' is taken as the end
15464 of the template parameter-list rather than a greater-than
15466 parameter_declarator
15467 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15468 /*parenthesized_p=*/NULL
);
15470 if (!parameter_declarator
)
15471 return error_mark_node
;
15473 /* If the parameter declaration is marked as a parameter pack, set
15474 *IS_PARAMETER_PACK to notify the caller. */
15475 if (parameter_declarator
->template_parameter_pack_p
)
15476 *is_parameter_pack
= true;
15478 if (parameter_declarator
->default_argument
)
15480 /* Can happen in some cases of erroneous input (c++/34892). */
15481 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15482 /* Consume the `...' for better error recovery. */
15483 cp_lexer_consume_token (parser
->lexer
);
15486 // The parameter may have been constrained.
15487 if (is_constrained_parameter (parameter_declarator
))
15488 return finish_constrained_parameter (parser
,
15489 parameter_declarator
,
15491 is_parameter_pack
);
15493 // Now we're sure that the parameter is a non-type parameter.
15494 *is_non_type
= true;
15496 parm
= grokdeclarator (parameter_declarator
->declarator
,
15497 ¶meter_declarator
->decl_specifiers
,
15498 TPARM
, /*initialized=*/0,
15499 /*attrlist=*/NULL
);
15500 if (parm
== error_mark_node
)
15501 return error_mark_node
;
15503 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15506 /* Parse a type-parameter.
15509 class identifier [opt]
15510 class identifier [opt] = type-id
15511 typename identifier [opt]
15512 typename identifier [opt] = type-id
15513 template < template-parameter-list > class identifier [opt]
15514 template < template-parameter-list > class identifier [opt]
15517 GNU Extension (variadic templates):
15520 class ... identifier [opt]
15521 typename ... identifier [opt]
15523 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15524 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15525 the declaration of the parameter.
15527 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15530 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15535 /* Look for a keyword to tell us what kind of parameter this is. */
15536 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15538 return error_mark_node
;
15540 switch (token
->keyword
)
15546 tree default_argument
;
15548 /* If the next token is an ellipsis, we have a template
15550 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15552 /* Consume the `...' token. */
15553 cp_lexer_consume_token (parser
->lexer
);
15554 maybe_warn_variadic_templates ();
15556 *is_parameter_pack
= true;
15559 /* If the next token is an identifier, then it names the
15561 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15562 identifier
= cp_parser_identifier (parser
);
15564 identifier
= NULL_TREE
;
15566 /* Create the parameter. */
15567 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15569 /* If the next token is an `=', we have a default argument. */
15570 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15573 = cp_parser_default_type_template_argument (parser
);
15575 /* Template parameter packs cannot have default
15577 if (*is_parameter_pack
)
15580 error_at (token
->location
,
15581 "template parameter pack %qD cannot have a "
15582 "default argument", identifier
);
15584 error_at (token
->location
,
15585 "template parameter packs cannot have "
15586 "default arguments");
15587 default_argument
= NULL_TREE
;
15589 else if (check_for_bare_parameter_packs (default_argument
))
15590 default_argument
= error_mark_node
;
15593 default_argument
= NULL_TREE
;
15595 /* Create the combined representation of the parameter and the
15596 default argument. */
15597 parameter
= build_tree_list (default_argument
, parameter
);
15604 tree default_argument
;
15606 /* Look for the `<'. */
15607 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15608 /* Parse the template-parameter-list. */
15609 cp_parser_template_parameter_list (parser
);
15610 /* Look for the `>'. */
15611 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15613 // If template requirements are present, parse them.
15616 tree reqs
= get_shorthand_constraints (current_template_parms
);
15617 if (tree r
= cp_parser_requires_clause_opt (parser
))
15618 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15619 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15622 /* Look for the `class' or 'typename' keywords. */
15623 cp_parser_type_parameter_key (parser
);
15624 /* If the next token is an ellipsis, we have a template
15626 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15628 /* Consume the `...' token. */
15629 cp_lexer_consume_token (parser
->lexer
);
15630 maybe_warn_variadic_templates ();
15632 *is_parameter_pack
= true;
15634 /* If the next token is an `=', then there is a
15635 default-argument. If the next token is a `>', we are at
15636 the end of the parameter-list. If the next token is a `,',
15637 then we are at the end of this parameter. */
15638 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15639 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15640 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15642 identifier
= cp_parser_identifier (parser
);
15643 /* Treat invalid names as if the parameter were nameless. */
15644 if (identifier
== error_mark_node
)
15645 identifier
= NULL_TREE
;
15648 identifier
= NULL_TREE
;
15650 /* Create the template parameter. */
15651 parameter
= finish_template_template_parm (class_type_node
,
15654 /* If the next token is an `=', then there is a
15655 default-argument. */
15656 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15659 = cp_parser_default_template_template_argument (parser
);
15661 /* Template parameter packs cannot have default
15663 if (*is_parameter_pack
)
15666 error_at (token
->location
,
15667 "template parameter pack %qD cannot "
15668 "have a default argument",
15671 error_at (token
->location
, "template parameter packs cannot "
15672 "have default arguments");
15673 default_argument
= NULL_TREE
;
15677 default_argument
= NULL_TREE
;
15679 /* Create the combined representation of the parameter and the
15680 default argument. */
15681 parameter
= build_tree_list (default_argument
, parameter
);
15686 gcc_unreachable ();
15693 /* Parse a template-id.
15696 template-name < template-argument-list [opt] >
15698 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15699 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15700 returned. Otherwise, if the template-name names a function, or set
15701 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15702 names a class, returns a TYPE_DECL for the specialization.
15704 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15705 uninstantiated templates. */
15708 cp_parser_template_id (cp_parser
*parser
,
15709 bool template_keyword_p
,
15710 bool check_dependency_p
,
15711 enum tag_types tag_type
,
15712 bool is_declaration
)
15717 cp_token_position start_of_id
= 0;
15718 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15719 bool is_identifier
;
15721 /* If the next token corresponds to a template-id, there is no need
15723 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15724 if (token
->type
== CPP_TEMPLATE_ID
)
15726 cp_lexer_consume_token (parser
->lexer
);
15727 return saved_checks_value (token
->u
.tree_check_value
);
15730 /* Avoid performing name lookup if there is no possibility of
15731 finding a template-id. */
15732 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15733 || (token
->type
== CPP_NAME
15734 && !cp_parser_nth_token_starts_template_argument_list_p
15737 cp_parser_error (parser
, "expected template-id");
15738 return error_mark_node
;
15741 /* Remember where the template-id starts. */
15742 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15743 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15745 push_deferring_access_checks (dk_deferred
);
15747 /* Parse the template-name. */
15748 is_identifier
= false;
15749 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15750 check_dependency_p
,
15754 if (templ
== error_mark_node
|| is_identifier
)
15756 pop_deferring_access_checks ();
15760 /* Since we're going to preserve any side-effects from this parse, set up a
15761 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15762 in the template arguments. */
15763 tentative_firewall
firewall (parser
);
15765 /* If we find the sequence `[:' after a template-name, it's probably
15766 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15767 parse correctly the argument list. */
15768 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15769 == CPP_OPEN_SQUARE
)
15770 && next_token
->flags
& DIGRAPH
15771 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15773 && !(next_token_2
->flags
& PREV_WHITE
))
15775 cp_parser_parse_tentatively (parser
);
15776 /* Change `:' into `::'. */
15777 next_token_2
->type
= CPP_SCOPE
;
15778 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15780 cp_lexer_consume_token (parser
->lexer
);
15782 /* Parse the arguments. */
15783 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15784 if (!cp_parser_parse_definitely (parser
))
15786 /* If we couldn't parse an argument list, then we revert our changes
15787 and return simply an error. Maybe this is not a template-id
15789 next_token_2
->type
= CPP_COLON
;
15790 cp_parser_error (parser
, "expected %<<%>");
15791 pop_deferring_access_checks ();
15792 return error_mark_node
;
15794 /* Otherwise, emit an error about the invalid digraph, but continue
15795 parsing because we got our argument list. */
15796 if (permerror (next_token
->location
,
15797 "%<<::%> cannot begin a template-argument list"))
15799 static bool hint
= false;
15800 inform (next_token
->location
,
15801 "%<<:%> is an alternate spelling for %<[%>."
15802 " Insert whitespace between %<<%> and %<::%>");
15803 if (!hint
&& !flag_permissive
)
15805 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15806 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15807 "accept your code)");
15814 /* Look for the `<' that starts the template-argument-list. */
15815 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15817 pop_deferring_access_checks ();
15818 return error_mark_node
;
15820 /* Parse the arguments. */
15821 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15824 /* Set the location to be of the form:
15825 template-name < template-argument-list [opt] >
15826 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15827 with caret == start at the start of the template-name,
15828 ranging until the closing '>'. */
15829 location_t finish_loc
15830 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15831 location_t combined_loc
15832 = make_location (token
->location
, token
->location
, finish_loc
);
15834 /* Build a representation of the specialization. */
15835 if (identifier_p (templ
))
15836 template_id
= build_min_nt_loc (combined_loc
,
15839 else if (DECL_TYPE_TEMPLATE_P (templ
)
15840 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15842 bool entering_scope
;
15843 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15844 template (rather than some instantiation thereof) only if
15845 is not nested within some other construct. For example, in
15846 "template <typename T> void f(T) { A<T>::", A<T> is just an
15847 instantiation of A. */
15848 entering_scope
= (template_parm_scope_p ()
15849 && cp_lexer_next_token_is (parser
->lexer
,
15852 = finish_template_type (templ
, arguments
, entering_scope
);
15854 /* A template-like identifier may be a partial concept id. */
15855 else if (flag_concepts
15856 && (template_id
= (cp_parser_maybe_partial_concept_id
15857 (parser
, templ
, arguments
))))
15858 return template_id
;
15859 else if (variable_template_p (templ
))
15861 template_id
= lookup_template_variable (templ
, arguments
);
15862 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15863 SET_EXPR_LOCATION (template_id
, combined_loc
);
15867 /* If it's not a class-template or a template-template, it should be
15868 a function-template. */
15869 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15870 || TREE_CODE (templ
) == OVERLOAD
15871 || BASELINK_P (templ
)));
15873 template_id
= lookup_template_function (templ
, arguments
);
15874 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15875 SET_EXPR_LOCATION (template_id
, combined_loc
);
15878 /* If parsing tentatively, replace the sequence of tokens that makes
15879 up the template-id with a CPP_TEMPLATE_ID token. That way,
15880 should we re-parse the token stream, we will not have to repeat
15881 the effort required to do the parse, nor will we issue duplicate
15882 error messages about problems during instantiation of the
15885 /* Don't do this if we had a parse error in a declarator; re-parsing
15886 might succeed if a name changes meaning (60361). */
15887 && !(cp_parser_error_occurred (parser
)
15888 && cp_parser_parsing_tentatively (parser
)
15889 && parser
->in_declarator_p
))
15891 /* Reset the contents of the START_OF_ID token. */
15892 token
->type
= CPP_TEMPLATE_ID
;
15893 token
->location
= combined_loc
;
15895 /* We must mark the lookup as kept, so we don't throw it away on
15896 the first parse. */
15897 if (is_overloaded_fn (template_id
))
15898 lookup_keep (get_fns (template_id
), true);
15900 /* Retrieve any deferred checks. Do not pop this access checks yet
15901 so the memory will not be reclaimed during token replacing below. */
15902 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15903 token
->u
.tree_check_value
->value
= template_id
;
15904 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15905 token
->keyword
= RID_MAX
;
15907 /* Purge all subsequent tokens. */
15908 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
15910 /* ??? Can we actually assume that, if template_id ==
15911 error_mark_node, we will have issued a diagnostic to the
15912 user, as opposed to simply marking the tentative parse as
15914 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
15915 error_at (token
->location
, "parse error in template argument list");
15918 pop_to_parent_deferring_access_checks ();
15919 return template_id
;
15922 /* Parse a template-name.
15927 The standard should actually say:
15931 operator-function-id
15933 A defect report has been filed about this issue.
15935 A conversion-function-id cannot be a template name because they cannot
15936 be part of a template-id. In fact, looking at this code:
15938 a.operator K<int>()
15940 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15941 It is impossible to call a templated conversion-function-id with an
15942 explicit argument list, since the only allowed template parameter is
15943 the type to which it is converting.
15945 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15946 `template' keyword, in a construction like:
15950 In that case `f' is taken to be a template-name, even though there
15951 is no way of knowing for sure.
15953 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15954 name refers to a set of overloaded functions, at least one of which
15955 is a template, or an IDENTIFIER_NODE with the name of the template,
15956 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15957 names are looked up inside uninstantiated templates. */
15960 cp_parser_template_name (cp_parser
* parser
,
15961 bool template_keyword_p
,
15962 bool check_dependency_p
,
15963 bool is_declaration
,
15964 enum tag_types tag_type
,
15965 bool *is_identifier
)
15969 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15971 /* If the next token is `operator', then we have either an
15972 operator-function-id or a conversion-function-id. */
15973 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
15975 /* We don't know whether we're looking at an
15976 operator-function-id or a conversion-function-id. */
15977 cp_parser_parse_tentatively (parser
);
15978 /* Try an operator-function-id. */
15979 identifier
= cp_parser_operator_function_id (parser
);
15980 /* If that didn't work, try a conversion-function-id. */
15981 if (!cp_parser_parse_definitely (parser
))
15983 cp_parser_error (parser
, "expected template-name");
15984 return error_mark_node
;
15987 /* Look for the identifier. */
15989 identifier
= cp_parser_identifier (parser
);
15991 /* If we didn't find an identifier, we don't have a template-id. */
15992 if (identifier
== error_mark_node
)
15993 return error_mark_node
;
15995 /* If the name immediately followed the `template' keyword, then it
15996 is a template-name. However, if the next token is not `<', then
15997 we do not treat it as a template-name, since it is not being used
15998 as part of a template-id. This enables us to handle constructs
16001 template <typename T> struct S { S(); };
16002 template <typename T> S<T>::S();
16004 correctly. We would treat `S' as a template -- if it were `S<T>'
16005 -- but we do not if there is no `<'. */
16007 if (processing_template_decl
16008 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16010 /* In a declaration, in a dependent context, we pretend that the
16011 "template" keyword was present in order to improve error
16012 recovery. For example, given:
16014 template <typename T> void f(T::X<int>);
16016 we want to treat "X<int>" as a template-id. */
16018 && !template_keyword_p
16019 && parser
->scope
&& TYPE_P (parser
->scope
)
16020 && check_dependency_p
16021 && dependent_scope_p (parser
->scope
)
16022 /* Do not do this for dtors (or ctors), since they never
16023 need the template keyword before their name. */
16024 && !constructor_name_p (identifier
, parser
->scope
))
16026 cp_token_position start
= 0;
16028 /* Explain what went wrong. */
16029 error_at (token
->location
, "non-template %qD used as template",
16031 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16032 parser
->scope
, identifier
);
16033 /* If parsing tentatively, find the location of the "<" token. */
16034 if (cp_parser_simulate_error (parser
))
16035 start
= cp_lexer_token_position (parser
->lexer
, true);
16036 /* Parse the template arguments so that we can issue error
16037 messages about them. */
16038 cp_lexer_consume_token (parser
->lexer
);
16039 cp_parser_enclosed_template_argument_list (parser
);
16040 /* Skip tokens until we find a good place from which to
16041 continue parsing. */
16042 cp_parser_skip_to_closing_parenthesis (parser
,
16043 /*recovering=*/true,
16045 /*consume_paren=*/false);
16046 /* If parsing tentatively, permanently remove the
16047 template argument list. That will prevent duplicate
16048 error messages from being issued about the missing
16049 "template" keyword. */
16051 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16053 *is_identifier
= true;
16054 parser
->context
->object_type
= NULL_TREE
;
16058 /* If the "template" keyword is present, then there is generally
16059 no point in doing name-lookup, so we just return IDENTIFIER.
16060 But, if the qualifying scope is non-dependent then we can
16061 (and must) do name-lookup normally. */
16062 if (template_keyword_p
)
16064 tree scope
= (parser
->scope
? parser
->scope
16065 : parser
->context
->object_type
);
16066 if (scope
&& TYPE_P (scope
)
16067 && (!CLASS_TYPE_P (scope
)
16068 || (check_dependency_p
&& dependent_type_p (scope
))))
16070 /* We're optimizing away the call to cp_parser_lookup_name, but
16071 we still need to do this. */
16072 parser
->context
->object_type
= NULL_TREE
;
16078 /* Look up the name. */
16079 decl
= cp_parser_lookup_name (parser
, identifier
,
16081 /*is_template=*/true,
16082 /*is_namespace=*/false,
16083 check_dependency_p
,
16084 /*ambiguous_decls=*/NULL
,
16087 decl
= strip_using_decl (decl
);
16089 /* If DECL is a template, then the name was a template-name. */
16090 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16092 if (TREE_DEPRECATED (decl
)
16093 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16094 warn_deprecated_use (decl
, NULL_TREE
);
16098 /* The standard does not explicitly indicate whether a name that
16099 names a set of overloaded declarations, some of which are
16100 templates, is a template-name. However, such a name should
16101 be a template-name; otherwise, there is no way to form a
16102 template-id for the overloaded templates. */
16103 bool found
= false;
16105 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16106 !found
&& iter
; ++iter
)
16107 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16112 /* The name does not name a template. */
16113 cp_parser_error (parser
, "expected template-name");
16114 return error_mark_node
;
16118 /* If DECL is dependent, and refers to a function, then just return
16119 its name; we will look it up again during template instantiation. */
16120 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16122 tree scope
= ovl_scope (decl
);
16123 if (TYPE_P (scope
) && dependent_type_p (scope
))
16130 /* Parse a template-argument-list.
16132 template-argument-list:
16133 template-argument ... [opt]
16134 template-argument-list , template-argument ... [opt]
16136 Returns a TREE_VEC containing the arguments. */
16139 cp_parser_template_argument_list (cp_parser
* parser
)
16141 tree fixed_args
[10];
16142 unsigned n_args
= 0;
16143 unsigned alloced
= 10;
16144 tree
*arg_ary
= fixed_args
;
16146 bool saved_in_template_argument_list_p
;
16148 bool saved_non_ice_p
;
16150 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16151 parser
->in_template_argument_list_p
= true;
16152 /* Even if the template-id appears in an integral
16153 constant-expression, the contents of the argument list do
16155 saved_ice_p
= parser
->integral_constant_expression_p
;
16156 parser
->integral_constant_expression_p
= false;
16157 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16158 parser
->non_integral_constant_expression_p
= false;
16160 /* Parse the arguments. */
16166 /* Consume the comma. */
16167 cp_lexer_consume_token (parser
->lexer
);
16169 /* Parse the template-argument. */
16170 argument
= cp_parser_template_argument (parser
);
16172 /* If the next token is an ellipsis, we're expanding a template
16174 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16176 if (argument
== error_mark_node
)
16178 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16179 error_at (token
->location
,
16180 "expected parameter pack before %<...%>");
16182 /* Consume the `...' token. */
16183 cp_lexer_consume_token (parser
->lexer
);
16185 /* Make the argument into a TYPE_PACK_EXPANSION or
16186 EXPR_PACK_EXPANSION. */
16187 argument
= make_pack_expansion (argument
);
16190 if (n_args
== alloced
)
16194 if (arg_ary
== fixed_args
)
16196 arg_ary
= XNEWVEC (tree
, alloced
);
16197 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16200 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16202 arg_ary
[n_args
++] = argument
;
16204 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16206 vec
= make_tree_vec (n_args
);
16209 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16211 if (arg_ary
!= fixed_args
)
16213 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16214 parser
->integral_constant_expression_p
= saved_ice_p
;
16215 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16217 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16221 /* Parse a template-argument.
16224 assignment-expression
16228 The representation is that of an assignment-expression, type-id, or
16229 id-expression -- except that the qualified id-expression is
16230 evaluated, so that the value returned is either a DECL or an
16233 Although the standard says "assignment-expression", it forbids
16234 throw-expressions or assignments in the template argument.
16235 Therefore, we use "conditional-expression" instead. */
16238 cp_parser_template_argument (cp_parser
* parser
)
16243 bool maybe_type_id
= false;
16244 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16245 location_t loc
= 0;
16248 /* There's really no way to know what we're looking at, so we just
16249 try each alternative in order.
16253 In a template-argument, an ambiguity between a type-id and an
16254 expression is resolved to a type-id, regardless of the form of
16255 the corresponding template-parameter.
16257 Therefore, we try a type-id first. */
16258 cp_parser_parse_tentatively (parser
);
16259 argument
= cp_parser_template_type_arg (parser
);
16260 /* If there was no error parsing the type-id but the next token is a
16261 '>>', our behavior depends on which dialect of C++ we're
16262 parsing. In C++98, we probably found a typo for '> >'. But there
16263 are type-id which are also valid expressions. For instance:
16265 struct X { int operator >> (int); };
16266 template <int V> struct Foo {};
16269 Here 'X()' is a valid type-id of a function type, but the user just
16270 wanted to write the expression "X() >> 5". Thus, we remember that we
16271 found a valid type-id, but we still try to parse the argument as an
16272 expression to see what happens.
16274 In C++0x, the '>>' will be considered two separate '>'
16276 if (!cp_parser_error_occurred (parser
)
16277 && cxx_dialect
== cxx98
16278 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16280 maybe_type_id
= true;
16281 cp_parser_abort_tentative_parse (parser
);
16285 /* If the next token isn't a `,' or a `>', then this argument wasn't
16286 really finished. This means that the argument is not a valid
16288 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16289 cp_parser_error (parser
, "expected template-argument");
16290 /* If that worked, we're done. */
16291 if (cp_parser_parse_definitely (parser
))
16294 /* We're still not sure what the argument will be. */
16295 cp_parser_parse_tentatively (parser
);
16296 /* Try a template. */
16297 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16298 argument
= cp_parser_id_expression (parser
,
16299 /*template_keyword_p=*/false,
16300 /*check_dependency_p=*/true,
16302 /*declarator_p=*/false,
16303 /*optional_p=*/false);
16304 /* If the next token isn't a `,' or a `>', then this argument wasn't
16305 really finished. */
16306 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16307 cp_parser_error (parser
, "expected template-argument");
16308 if (!cp_parser_error_occurred (parser
))
16310 /* Figure out what is being referred to. If the id-expression
16311 was for a class template specialization, then we will have a
16312 TYPE_DECL at this point. There is no need to do name lookup
16313 at this point in that case. */
16314 if (TREE_CODE (argument
) != TYPE_DECL
)
16315 argument
= cp_parser_lookup_name (parser
, argument
,
16317 /*is_template=*/template_p
,
16318 /*is_namespace=*/false,
16319 /*check_dependency=*/true,
16320 /*ambiguous_decls=*/NULL
,
16321 argument_start_token
->location
);
16322 /* Handle a constrained-type-specifier for a non-type template
16324 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16326 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16327 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16328 cp_parser_error (parser
, "expected template-name");
16330 if (cp_parser_parse_definitely (parser
))
16332 if (TREE_DEPRECATED (argument
))
16333 warn_deprecated_use (argument
, NULL_TREE
);
16336 /* It must be a non-type argument. In C++17 any constant-expression is
16338 if (cxx_dialect
> cxx14
)
16341 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16343 -- an integral constant-expression of integral or enumeration
16346 -- the name of a non-type template-parameter; or
16348 -- the name of an object or function with external linkage...
16350 -- the address of an object or function with external linkage...
16352 -- a pointer to member... */
16353 /* Look for a non-type template parameter. */
16354 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16356 cp_parser_parse_tentatively (parser
);
16357 argument
= cp_parser_primary_expression (parser
,
16358 /*address_p=*/false,
16360 /*template_arg_p=*/true,
16362 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16363 || !cp_parser_next_token_ends_template_argument_p (parser
))
16364 cp_parser_simulate_error (parser
);
16365 if (cp_parser_parse_definitely (parser
))
16369 /* If the next token is "&", the argument must be the address of an
16370 object or function with external linkage. */
16371 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16374 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16375 cp_lexer_consume_token (parser
->lexer
);
16377 /* See if we might have an id-expression. */
16378 token
= cp_lexer_peek_token (parser
->lexer
);
16379 if (token
->type
== CPP_NAME
16380 || token
->keyword
== RID_OPERATOR
16381 || token
->type
== CPP_SCOPE
16382 || token
->type
== CPP_TEMPLATE_ID
16383 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16385 cp_parser_parse_tentatively (parser
);
16386 argument
= cp_parser_primary_expression (parser
,
16389 /*template_arg_p=*/true,
16391 if (cp_parser_error_occurred (parser
)
16392 || !cp_parser_next_token_ends_template_argument_p (parser
))
16393 cp_parser_abort_tentative_parse (parser
);
16398 if (INDIRECT_REF_P (argument
))
16400 /* Strip the dereference temporarily. */
16401 gcc_assert (REFERENCE_REF_P (argument
));
16402 argument
= TREE_OPERAND (argument
, 0);
16405 /* If we're in a template, we represent a qualified-id referring
16406 to a static data member as a SCOPE_REF even if the scope isn't
16407 dependent so that we can check access control later. */
16409 if (TREE_CODE (probe
) == SCOPE_REF
)
16410 probe
= TREE_OPERAND (probe
, 1);
16413 /* A variable without external linkage might still be a
16414 valid constant-expression, so no error is issued here
16415 if the external-linkage check fails. */
16416 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16417 cp_parser_simulate_error (parser
);
16419 else if (is_overloaded_fn (argument
))
16420 /* All overloaded functions are allowed; if the external
16421 linkage test does not pass, an error will be issued
16425 && (TREE_CODE (argument
) == OFFSET_REF
16426 || TREE_CODE (argument
) == SCOPE_REF
))
16427 /* A pointer-to-member. */
16429 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16432 cp_parser_simulate_error (parser
);
16434 if (cp_parser_parse_definitely (parser
))
16437 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16438 tf_warning_or_error
);
16440 argument
= convert_from_reference (argument
);
16445 /* If the argument started with "&", there are no other valid
16446 alternatives at this point. */
16449 cp_parser_error (parser
, "invalid non-type template argument");
16450 return error_mark_node
;
16454 /* If the argument wasn't successfully parsed as a type-id followed
16455 by '>>', the argument can only be a constant expression now.
16456 Otherwise, we try parsing the constant-expression tentatively,
16457 because the argument could really be a type-id. */
16459 cp_parser_parse_tentatively (parser
);
16461 if (cxx_dialect
<= cxx14
)
16462 argument
= cp_parser_constant_expression (parser
);
16465 /* With C++17 generalized non-type template arguments we need to handle
16466 lvalue constant expressions, too. */
16467 argument
= cp_parser_assignment_expression (parser
);
16468 require_potential_constant_expression (argument
);
16471 if (!maybe_type_id
)
16473 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16474 cp_parser_error (parser
, "expected template-argument");
16475 if (cp_parser_parse_definitely (parser
))
16477 /* We did our best to parse the argument as a non type-id, but that
16478 was the only alternative that matched (albeit with a '>' after
16479 it). We can assume it's just a typo from the user, and a
16480 diagnostic will then be issued. */
16481 return cp_parser_template_type_arg (parser
);
16484 /* Parse an explicit-instantiation.
16486 explicit-instantiation:
16487 template declaration
16489 Although the standard says `declaration', what it really means is:
16491 explicit-instantiation:
16492 template decl-specifier-seq [opt] declarator [opt] ;
16494 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16495 supposed to be allowed. A defect report has been filed about this
16500 explicit-instantiation:
16501 storage-class-specifier template
16502 decl-specifier-seq [opt] declarator [opt] ;
16503 function-specifier template
16504 decl-specifier-seq [opt] declarator [opt] ; */
16507 cp_parser_explicit_instantiation (cp_parser
* parser
)
16509 int declares_class_or_enum
;
16510 cp_decl_specifier_seq decl_specifiers
;
16511 tree extension_specifier
= NULL_TREE
;
16513 timevar_push (TV_TEMPLATE_INST
);
16515 /* Look for an (optional) storage-class-specifier or
16516 function-specifier. */
16517 if (cp_parser_allow_gnu_extensions_p (parser
))
16519 extension_specifier
16520 = cp_parser_storage_class_specifier_opt (parser
);
16521 if (!extension_specifier
)
16522 extension_specifier
16523 = cp_parser_function_specifier_opt (parser
,
16524 /*decl_specs=*/NULL
);
16527 /* Look for the `template' keyword. */
16528 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16529 /* Let the front end know that we are processing an explicit
16531 begin_explicit_instantiation ();
16532 /* [temp.explicit] says that we are supposed to ignore access
16533 control while processing explicit instantiation directives. */
16534 push_deferring_access_checks (dk_no_check
);
16535 /* Parse a decl-specifier-seq. */
16536 cp_parser_decl_specifier_seq (parser
,
16537 CP_PARSER_FLAGS_OPTIONAL
,
16539 &declares_class_or_enum
);
16540 /* If there was exactly one decl-specifier, and it declared a class,
16541 and there's no declarator, then we have an explicit type
16543 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16547 type
= check_tag_decl (&decl_specifiers
,
16548 /*explicit_type_instantiation_p=*/true);
16549 /* Turn access control back on for names used during
16550 template instantiation. */
16551 pop_deferring_access_checks ();
16553 do_type_instantiation (type
, extension_specifier
,
16554 /*complain=*/tf_error
);
16558 cp_declarator
*declarator
;
16561 /* Parse the declarator. */
16563 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16564 /*ctor_dtor_or_conv_p=*/NULL
,
16565 /*parenthesized_p=*/NULL
,
16566 /*member_p=*/false,
16567 /*friend_p=*/false);
16568 if (declares_class_or_enum
& 2)
16569 cp_parser_check_for_definition_in_return_type (declarator
,
16570 decl_specifiers
.type
,
16571 decl_specifiers
.locations
[ds_type_spec
]);
16572 if (declarator
!= cp_error_declarator
)
16574 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16575 permerror (decl_specifiers
.locations
[ds_inline
],
16576 "explicit instantiation shall not use"
16577 " %<inline%> specifier");
16578 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16579 permerror (decl_specifiers
.locations
[ds_constexpr
],
16580 "explicit instantiation shall not use"
16581 " %<constexpr%> specifier");
16583 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16584 NORMAL
, 0, &decl_specifiers
.attributes
);
16585 /* Turn access control back on for names used during
16586 template instantiation. */
16587 pop_deferring_access_checks ();
16588 /* Do the explicit instantiation. */
16589 do_decl_instantiation (decl
, extension_specifier
);
16593 pop_deferring_access_checks ();
16594 /* Skip the body of the explicit instantiation. */
16595 cp_parser_skip_to_end_of_statement (parser
);
16598 /* We're done with the instantiation. */
16599 end_explicit_instantiation ();
16601 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16603 timevar_pop (TV_TEMPLATE_INST
);
16606 /* Parse an explicit-specialization.
16608 explicit-specialization:
16609 template < > declaration
16611 Although the standard says `declaration', what it really means is:
16613 explicit-specialization:
16614 template <> decl-specifier [opt] init-declarator [opt] ;
16615 template <> function-definition
16616 template <> explicit-specialization
16617 template <> template-declaration */
16620 cp_parser_explicit_specialization (cp_parser
* parser
)
16622 bool need_lang_pop
;
16623 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16625 /* Look for the `template' keyword. */
16626 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16627 /* Look for the `<'. */
16628 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16629 /* Look for the `>'. */
16630 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16631 /* We have processed another parameter list. */
16632 ++parser
->num_template_parameter_lists
;
16635 A template ... explicit specialization ... shall not have C
16637 if (current_lang_name
== lang_name_c
)
16639 error_at (token
->location
, "template specialization with C linkage");
16640 maybe_show_extern_c_location ();
16641 /* Give it C++ linkage to avoid confusing other parts of the
16643 push_lang_context (lang_name_cplusplus
);
16644 need_lang_pop
= true;
16647 need_lang_pop
= false;
16648 /* Let the front end know that we are beginning a specialization. */
16649 if (!begin_specialization ())
16651 end_specialization ();
16655 /* If the next keyword is `template', we need to figure out whether
16656 or not we're looking a template-declaration. */
16657 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16659 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16660 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16661 cp_parser_template_declaration_after_export (parser
,
16662 /*member_p=*/false);
16664 cp_parser_explicit_specialization (parser
);
16667 /* Parse the dependent declaration. */
16668 cp_parser_single_declaration (parser
,
16670 /*member_p=*/false,
16671 /*explicit_specialization_p=*/true,
16672 /*friend_p=*/NULL
);
16673 /* We're done with the specialization. */
16674 end_specialization ();
16675 /* For the erroneous case of a template with C linkage, we pushed an
16676 implicit C++ linkage scope; exit that scope now. */
16678 pop_lang_context ();
16679 /* We're done with this parameter list. */
16680 --parser
->num_template_parameter_lists
;
16683 /* Parse a type-specifier.
16686 simple-type-specifier
16689 elaborated-type-specifier
16697 Returns a representation of the type-specifier. For a
16698 class-specifier, enum-specifier, or elaborated-type-specifier, a
16699 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16701 The parser flags FLAGS is used to control type-specifier parsing.
16703 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16704 in a decl-specifier-seq.
16706 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16707 class-specifier, enum-specifier, or elaborated-type-specifier, then
16708 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16709 if a type is declared; 2 if it is defined. Otherwise, it is set to
16712 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16713 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16714 is set to FALSE. */
16717 cp_parser_type_specifier (cp_parser
* parser
,
16718 cp_parser_flags flags
,
16719 cp_decl_specifier_seq
*decl_specs
,
16720 bool is_declaration
,
16721 int* declares_class_or_enum
,
16722 bool* is_cv_qualifier
)
16724 tree type_spec
= NULL_TREE
;
16727 cp_decl_spec ds
= ds_last
;
16729 /* Assume this type-specifier does not declare a new type. */
16730 if (declares_class_or_enum
)
16731 *declares_class_or_enum
= 0;
16732 /* And that it does not specify a cv-qualifier. */
16733 if (is_cv_qualifier
)
16734 *is_cv_qualifier
= false;
16735 /* Peek at the next token. */
16736 token
= cp_lexer_peek_token (parser
->lexer
);
16738 /* If we're looking at a keyword, we can use that to guide the
16739 production we choose. */
16740 keyword
= token
->keyword
;
16744 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16745 goto elaborated_type_specifier
;
16747 /* Look for the enum-specifier. */
16748 type_spec
= cp_parser_enum_specifier (parser
);
16749 /* If that worked, we're done. */
16752 if (declares_class_or_enum
)
16753 *declares_class_or_enum
= 2;
16755 cp_parser_set_decl_spec_type (decl_specs
,
16758 /*type_definition_p=*/true);
16762 goto elaborated_type_specifier
;
16764 /* Any of these indicate either a class-specifier, or an
16765 elaborated-type-specifier. */
16769 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16770 goto elaborated_type_specifier
;
16772 /* Parse tentatively so that we can back up if we don't find a
16773 class-specifier. */
16774 cp_parser_parse_tentatively (parser
);
16775 /* Look for the class-specifier. */
16776 type_spec
= cp_parser_class_specifier (parser
);
16777 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16778 /* If that worked, we're done. */
16779 if (cp_parser_parse_definitely (parser
))
16781 if (declares_class_or_enum
)
16782 *declares_class_or_enum
= 2;
16784 cp_parser_set_decl_spec_type (decl_specs
,
16787 /*type_definition_p=*/true);
16791 /* Fall through. */
16792 elaborated_type_specifier
:
16793 /* We're declaring (not defining) a class or enum. */
16794 if (declares_class_or_enum
)
16795 *declares_class_or_enum
= 1;
16797 /* Fall through. */
16799 /* Look for an elaborated-type-specifier. */
16801 = (cp_parser_elaborated_type_specifier
16803 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16806 cp_parser_set_decl_spec_type (decl_specs
,
16809 /*type_definition_p=*/false);
16814 if (is_cv_qualifier
)
16815 *is_cv_qualifier
= true;
16820 if (is_cv_qualifier
)
16821 *is_cv_qualifier
= true;
16826 if (is_cv_qualifier
)
16827 *is_cv_qualifier
= true;
16831 /* The `__complex__' keyword is a GNU extension. */
16839 /* Handle simple keywords. */
16844 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16845 decl_specs
->any_specifiers_p
= true;
16847 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16850 /* If we do not already have a type-specifier, assume we are looking
16851 at a simple-type-specifier. */
16852 type_spec
= cp_parser_simple_type_specifier (parser
,
16856 /* If we didn't find a type-specifier, and a type-specifier was not
16857 optional in this context, issue an error message. */
16858 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16860 cp_parser_error (parser
, "expected type specifier");
16861 return error_mark_node
;
16867 /* Parse a simple-type-specifier.
16869 simple-type-specifier:
16870 :: [opt] nested-name-specifier [opt] type-name
16871 :: [opt] nested-name-specifier template template-id
16886 simple-type-specifier:
16888 decltype ( expression )
16891 __underlying_type ( type-id )
16895 nested-name-specifier(opt) template-name
16899 simple-type-specifier:
16901 __typeof__ unary-expression
16902 __typeof__ ( type-id )
16903 __typeof__ ( type-id ) { initializer-list , [opt] }
16905 Concepts Extension:
16907 simple-type-specifier:
16908 constrained-type-specifier
16910 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16911 appropriately updated. */
16914 cp_parser_simple_type_specifier (cp_parser
* parser
,
16915 cp_decl_specifier_seq
*decl_specs
,
16916 cp_parser_flags flags
)
16918 tree type
= NULL_TREE
;
16922 /* Peek at the next token. */
16923 token
= cp_lexer_peek_token (parser
->lexer
);
16925 /* If we're looking at a keyword, things are easy. */
16926 switch (token
->keyword
)
16930 decl_specs
->explicit_char_p
= true;
16931 type
= char_type_node
;
16934 type
= char16_type_node
;
16937 type
= char32_type_node
;
16940 type
= wchar_type_node
;
16943 type
= boolean_type_node
;
16946 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
16947 type
= short_integer_type_node
;
16951 decl_specs
->explicit_int_p
= true;
16952 type
= integer_type_node
;
16958 idx
= token
->keyword
- RID_INT_N_0
;
16959 if (! int_n_enabled_p
[idx
])
16963 decl_specs
->explicit_intN_p
= true;
16964 decl_specs
->int_n_idx
= idx
;
16966 type
= int_n_trees
[idx
].signed_type
;
16970 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
16971 type
= long_integer_type_node
;
16974 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
16975 type
= integer_type_node
;
16978 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
16979 type
= unsigned_type_node
;
16982 type
= float_type_node
;
16985 type
= double_type_node
;
16988 type
= void_type_node
;
16992 maybe_warn_cpp0x (CPP0X_AUTO
);
16993 if (parser
->auto_is_implicit_function_template_parm_p
)
16995 /* The 'auto' might be the placeholder return type for a function decl
16996 with trailing return type. */
16997 bool have_trailing_return_fn_decl
= false;
16999 cp_parser_parse_tentatively (parser
);
17000 cp_lexer_consume_token (parser
->lexer
);
17001 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17002 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17003 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17004 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17006 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17008 cp_lexer_consume_token (parser
->lexer
);
17009 cp_parser_skip_to_closing_parenthesis (parser
,
17010 /*recovering*/false,
17012 /*consume_paren*/true);
17016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17018 have_trailing_return_fn_decl
= true;
17022 cp_lexer_consume_token (parser
->lexer
);
17024 cp_parser_abort_tentative_parse (parser
);
17026 if (have_trailing_return_fn_decl
)
17028 type
= make_auto ();
17032 if (cxx_dialect
>= cxx14
)
17034 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17035 type
= TREE_TYPE (type
);
17038 type
= error_mark_node
;
17040 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17042 if (cxx_dialect
< cxx14
)
17043 error_at (token
->location
,
17044 "use of %<auto%> in lambda parameter declaration "
17045 "only available with "
17046 "-std=c++14 or -std=gnu++14");
17048 else if (cxx_dialect
< cxx14
)
17049 error_at (token
->location
,
17050 "use of %<auto%> in parameter declaration "
17051 "only available with "
17052 "-std=c++14 or -std=gnu++14");
17053 else if (!flag_concepts
)
17054 pedwarn (token
->location
, 0,
17055 "use of %<auto%> in parameter declaration "
17056 "only available with -fconcepts");
17059 type
= make_auto ();
17063 /* Since DR 743, decltype can either be a simple-type-specifier by
17064 itself or begin a nested-name-specifier. Parsing it will replace
17065 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17066 handling below decide what to do. */
17067 cp_parser_decltype (parser
);
17068 cp_lexer_set_token_position (parser
->lexer
, token
);
17072 /* Consume the `typeof' token. */
17073 cp_lexer_consume_token (parser
->lexer
);
17074 /* Parse the operand to `typeof'. */
17075 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17076 /* If it is not already a TYPE, take its type. */
17077 if (!TYPE_P (type
))
17078 type
= finish_typeof (type
);
17081 cp_parser_set_decl_spec_type (decl_specs
, type
,
17083 /*type_definition_p=*/false);
17087 case RID_UNDERLYING_TYPE
:
17088 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17090 cp_parser_set_decl_spec_type (decl_specs
, type
,
17092 /*type_definition_p=*/false);
17097 case RID_DIRECT_BASES
:
17098 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17100 cp_parser_set_decl_spec_type (decl_specs
, type
,
17102 /*type_definition_p=*/false);
17108 /* If token is an already-parsed decltype not followed by ::,
17109 it's a simple-type-specifier. */
17110 if (token
->type
== CPP_DECLTYPE
17111 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17113 type
= saved_checks_value (token
->u
.tree_check_value
);
17116 cp_parser_set_decl_spec_type (decl_specs
, type
,
17118 /*type_definition_p=*/false);
17119 /* Remember that we are handling a decltype in order to
17120 implement the resolution of DR 1510 when the argument
17121 isn't instantiation dependent. */
17122 decl_specs
->decltype_p
= true;
17124 cp_lexer_consume_token (parser
->lexer
);
17128 /* If the type-specifier was for a built-in type, we're done. */
17131 /* Record the type. */
17133 && (token
->keyword
!= RID_SIGNED
17134 && token
->keyword
!= RID_UNSIGNED
17135 && token
->keyword
!= RID_SHORT
17136 && token
->keyword
!= RID_LONG
))
17137 cp_parser_set_decl_spec_type (decl_specs
,
17140 /*type_definition_p=*/false);
17142 decl_specs
->any_specifiers_p
= true;
17144 /* Consume the token. */
17145 cp_lexer_consume_token (parser
->lexer
);
17147 if (type
== error_mark_node
)
17148 return error_mark_node
;
17150 /* There is no valid C++ program where a non-template type is
17151 followed by a "<". That usually indicates that the user thought
17152 that the type was a template. */
17153 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17156 return TYPE_NAME (type
);
17159 /* The type-specifier must be a user-defined type. */
17160 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17165 /* Don't gobble tokens or issue error messages if this is an
17166 optional type-specifier. */
17167 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17168 cp_parser_parse_tentatively (parser
);
17170 token
= cp_lexer_peek_token (parser
->lexer
);
17172 /* Look for the optional `::' operator. */
17174 = (cp_parser_global_scope_opt (parser
,
17175 /*current_scope_valid_p=*/false)
17177 /* Look for the nested-name specifier. */
17179 = (cp_parser_nested_name_specifier_opt (parser
,
17180 /*typename_keyword_p=*/false,
17181 /*check_dependency_p=*/true,
17183 /*is_declaration=*/false)
17185 /* If we have seen a nested-name-specifier, and the next token
17186 is `template', then we are using the template-id production. */
17188 && cp_parser_optional_template_keyword (parser
))
17190 /* Look for the template-id. */
17191 type
= cp_parser_template_id (parser
,
17192 /*template_keyword_p=*/true,
17193 /*check_dependency_p=*/true,
17195 /*is_declaration=*/false);
17196 /* If the template-id did not name a type, we are out of
17198 if (TREE_CODE (type
) != TYPE_DECL
)
17200 cp_parser_error (parser
, "expected template-id for type");
17204 /* Otherwise, look for a type-name. */
17206 type
= cp_parser_type_name (parser
);
17207 /* Keep track of all name-lookups performed in class scopes. */
17211 && TREE_CODE (type
) == TYPE_DECL
17212 && identifier_p (DECL_NAME (type
)))
17213 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17214 /* If it didn't work out, we don't have a TYPE. */
17215 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17216 && !cp_parser_parse_definitely (parser
))
17218 if (!type
&& cxx_dialect
>= cxx17
)
17220 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17221 cp_parser_parse_tentatively (parser
);
17223 cp_parser_global_scope_opt (parser
,
17224 /*current_scope_valid_p=*/false);
17225 cp_parser_nested_name_specifier_opt (parser
,
17226 /*typename_keyword_p=*/false,
17227 /*check_dependency_p=*/true,
17229 /*is_declaration=*/false);
17230 tree name
= cp_parser_identifier (parser
);
17231 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17232 && parser
->scope
!= error_mark_node
)
17234 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17236 /*is_template=*/false,
17237 /*is_namespace=*/false,
17238 /*check_dependency=*/true,
17239 /*ambiguous_decls=*/NULL
,
17241 if (tmpl
&& tmpl
!= error_mark_node
17242 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17243 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17244 type
= make_template_placeholder (tmpl
);
17247 type
= error_mark_node
;
17248 if (!cp_parser_simulate_error (parser
))
17249 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17250 NLE_TYPE
, token
->location
);
17254 type
= error_mark_node
;
17256 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17257 && !cp_parser_parse_definitely (parser
))
17260 if (type
&& decl_specs
)
17261 cp_parser_set_decl_spec_type (decl_specs
, type
,
17263 /*type_definition_p=*/false);
17266 /* If we didn't get a type-name, issue an error message. */
17267 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17269 cp_parser_error (parser
, "expected type-name");
17270 return error_mark_node
;
17273 if (type
&& type
!= error_mark_node
)
17275 /* See if TYPE is an Objective-C type, and if so, parse and
17276 accept any protocol references following it. Do this before
17277 the cp_parser_check_for_invalid_template_id() call, because
17278 Objective-C types can be followed by '<...>' which would
17279 enclose protocol names rather than template arguments, and so
17280 everything is fine. */
17281 if (c_dialect_objc () && !parser
->scope
17282 && (objc_is_id (type
) || objc_is_class_name (type
)))
17284 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17285 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17287 /* Clobber the "unqualified" type previously entered into
17288 DECL_SPECS with the new, improved protocol-qualified version. */
17290 decl_specs
->type
= qual_type
;
17295 /* There is no valid C++ program where a non-template type is
17296 followed by a "<". That usually indicates that the user
17297 thought that the type was a template. */
17298 cp_parser_check_for_invalid_template_id (parser
, type
,
17306 /* Parse a type-name.
17312 simple-template-id [in c++0x]
17329 Returns a TYPE_DECL for the type. */
17332 cp_parser_type_name (cp_parser
* parser
)
17334 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17339 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17343 /* We can't know yet whether it is a class-name or not. */
17344 cp_parser_parse_tentatively (parser
);
17345 /* Try a class-name. */
17346 type_decl
= cp_parser_class_name (parser
,
17347 typename_keyword_p
,
17348 /*template_keyword_p=*/false,
17350 /*check_dependency_p=*/true,
17351 /*class_head_p=*/false,
17352 /*is_declaration=*/false);
17353 /* If it's not a class-name, keep looking. */
17354 if (!cp_parser_parse_definitely (parser
))
17356 if (cxx_dialect
< cxx11
)
17357 /* It must be a typedef-name or an enum-name. */
17358 return cp_parser_nonclass_name (parser
);
17360 cp_parser_parse_tentatively (parser
);
17361 /* It is either a simple-template-id representing an
17362 instantiation of an alias template... */
17363 type_decl
= cp_parser_template_id (parser
,
17364 /*template_keyword_p=*/false,
17365 /*check_dependency_p=*/true,
17367 /*is_declaration=*/false);
17368 /* Note that this must be an instantiation of an alias template
17369 because [temp.names]/6 says:
17371 A template-id that names an alias template specialization
17374 Whereas [temp.names]/7 says:
17376 A simple-template-id that names a class template
17377 specialization is a class-name.
17379 With concepts, this could also be a partial-concept-id that
17380 declares a non-type template parameter. */
17381 if (type_decl
!= NULL_TREE
17382 && TREE_CODE (type_decl
) == TYPE_DECL
17383 && TYPE_DECL_ALIAS_P (type_decl
))
17384 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17385 else if (is_constrained_parameter (type_decl
))
17386 /* Don't do anything. */ ;
17388 cp_parser_simulate_error (parser
);
17390 if (!cp_parser_parse_definitely (parser
))
17391 /* ... Or a typedef-name or an enum-name. */
17392 return cp_parser_nonclass_name (parser
);
17398 /* Check if DECL and ARGS can form a constrained-type-specifier.
17399 If ARGS is non-null, we try to form a concept check of the
17400 form DECL<?, ARGS> where ? is a wildcard that matches any
17401 kind of template argument. If ARGS is NULL, then we try to
17402 form a concept check of the form DECL<?>. */
17405 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17406 tree decl
, tree args
)
17408 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17410 /* If we a constrained-type-specifier cannot be deduced. */
17411 if (parser
->prevent_constrained_type_specifiers
)
17414 /* A constrained type specifier can only be found in an
17415 overload set or as a reference to a template declaration.
17417 FIXME: This might be masking a bug. It's possible that
17418 that the deduction below is causing template specializations
17419 to be formed with the wildcard as an argument. */
17420 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17423 /* Try to build a call expression that evaluates the
17424 concept. This can fail if the overload set refers
17425 only to non-templates. */
17426 tree placeholder
= build_nt (WILDCARD_DECL
);
17427 tree check
= build_concept_check (decl
, placeholder
, args
);
17428 if (check
== error_mark_node
)
17431 /* Deduce the checked constraint and the prototype parameter.
17433 FIXME: In certain cases, failure to deduce should be a
17434 diagnosable error. */
17437 if (!deduce_constrained_parameter (check
, conc
, proto
))
17440 /* In template parameter scope, this results in a constrained
17441 parameter. Return a descriptor of that parm. */
17442 if (processing_template_parmlist
)
17443 return build_constrained_parameter (conc
, proto
, args
);
17445 /* In a parameter-declaration-clause, constrained-type
17446 specifiers result in invented template parameters. */
17447 if (parser
->auto_is_implicit_function_template_parm_p
)
17449 tree x
= build_constrained_parameter (conc
, proto
, args
);
17450 return synthesize_implicit_template_parm (parser
, x
);
17454 /* Otherwise, we're in a context where the constrained
17455 type name is deduced and the constraint applies
17456 after deduction. */
17457 return make_constrained_auto (conc
, args
);
17463 /* If DECL refers to a concept, return a TYPE_DECL representing
17464 the result of using the constrained type specifier in the
17465 current context. DECL refers to a concept if
17467 - it is an overload set containing a function concept taking a single
17470 - it is a variable concept taking a single type argument. */
17473 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17476 && (TREE_CODE (decl
) == OVERLOAD
17477 || BASELINK_P (decl
)
17478 || variable_concept_p (decl
)))
17479 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17484 /* Check if DECL and ARGS form a partial-concept-id. If so,
17485 assign ID to the resulting constrained placeholder.
17487 Returns true if the partial-concept-id designates a placeholder
17488 and false otherwise. Note that *id is set to NULL_TREE in
17492 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17494 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17497 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17509 Returns a TYPE_DECL for the type. */
17512 cp_parser_nonclass_name (cp_parser
* parser
)
17517 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17518 identifier
= cp_parser_identifier (parser
);
17519 if (identifier
== error_mark_node
)
17520 return error_mark_node
;
17522 /* Look up the type-name. */
17523 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17525 type_decl
= strip_using_decl (type_decl
);
17527 /* If we found an overload set, then it may refer to a concept-name. */
17528 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17531 if (TREE_CODE (type_decl
) != TYPE_DECL
17532 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17534 /* See if this is an Objective-C type. */
17535 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17536 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17538 type_decl
= TYPE_NAME (type
);
17541 /* Issue an error if we did not find a type-name. */
17542 if (TREE_CODE (type_decl
) != TYPE_DECL
17543 /* In Objective-C, we have the complication that class names are
17544 normally type names and start declarations (eg, the
17545 "NSObject" in "NSObject *object;"), but can be used in an
17546 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17547 is an expression. So, a classname followed by a dot is not a
17548 valid type-name. */
17549 || (objc_is_class_name (TREE_TYPE (type_decl
))
17550 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17552 if (!cp_parser_simulate_error (parser
))
17553 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17554 NLE_TYPE
, token
->location
);
17555 return error_mark_node
;
17557 /* Remember that the name was used in the definition of the
17558 current class so that we can check later to see if the
17559 meaning would have been different after the class was
17560 entirely defined. */
17561 else if (type_decl
!= error_mark_node
17563 maybe_note_name_used_in_class (identifier
, type_decl
);
17568 /* Parse an elaborated-type-specifier. Note that the grammar given
17569 here incorporates the resolution to DR68.
17571 elaborated-type-specifier:
17572 class-key :: [opt] nested-name-specifier [opt] identifier
17573 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17574 enum-key :: [opt] nested-name-specifier [opt] identifier
17575 typename :: [opt] nested-name-specifier identifier
17576 typename :: [opt] nested-name-specifier template [opt]
17581 elaborated-type-specifier:
17582 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17583 class-key attributes :: [opt] nested-name-specifier [opt]
17584 template [opt] template-id
17585 enum attributes :: [opt] nested-name-specifier [opt] identifier
17587 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17588 declared `friend'. If IS_DECLARATION is TRUE, then this
17589 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17590 something is being declared.
17592 Returns the TYPE specified. */
17595 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17597 bool is_declaration
)
17599 enum tag_types tag_type
;
17601 tree type
= NULL_TREE
;
17602 tree attributes
= NULL_TREE
;
17604 cp_token
*token
= NULL
;
17606 /* See if we're looking at the `enum' keyword. */
17607 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17609 /* Consume the `enum' token. */
17610 cp_lexer_consume_token (parser
->lexer
);
17611 /* Remember that it's an enumeration type. */
17612 tag_type
= enum_type
;
17613 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17614 enums) is used here. */
17615 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17616 if (cp_parser_is_keyword (token
, RID_CLASS
)
17617 || cp_parser_is_keyword (token
, RID_STRUCT
))
17619 gcc_rich_location
richloc (token
->location
);
17620 richloc
.add_range (input_location
, false);
17621 richloc
.add_fixit_remove ();
17622 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17623 "a scoped enum must not use the %qD keyword",
17625 /* Consume the `struct' or `class' and parse it anyway. */
17626 cp_lexer_consume_token (parser
->lexer
);
17628 /* Parse the attributes. */
17629 attributes
= cp_parser_attributes_opt (parser
);
17631 /* Or, it might be `typename'. */
17632 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17635 /* Consume the `typename' token. */
17636 cp_lexer_consume_token (parser
->lexer
);
17637 /* Remember that it's a `typename' type. */
17638 tag_type
= typename_type
;
17640 /* Otherwise it must be a class-key. */
17643 tag_type
= cp_parser_class_key (parser
);
17644 if (tag_type
== none_type
)
17645 return error_mark_node
;
17646 /* Parse the attributes. */
17647 attributes
= cp_parser_attributes_opt (parser
);
17650 /* Look for the `::' operator. */
17651 globalscope
= cp_parser_global_scope_opt (parser
,
17652 /*current_scope_valid_p=*/false);
17653 /* Look for the nested-name-specifier. */
17654 tree nested_name_specifier
;
17655 if (tag_type
== typename_type
&& !globalscope
)
17657 nested_name_specifier
17658 = cp_parser_nested_name_specifier (parser
,
17659 /*typename_keyword_p=*/true,
17660 /*check_dependency_p=*/true,
17663 if (!nested_name_specifier
)
17664 return error_mark_node
;
17667 /* Even though `typename' is not present, the proposed resolution
17668 to Core Issue 180 says that in `class A<T>::B', `B' should be
17669 considered a type-name, even if `A<T>' is dependent. */
17670 nested_name_specifier
17671 = cp_parser_nested_name_specifier_opt (parser
,
17672 /*typename_keyword_p=*/true,
17673 /*check_dependency_p=*/true,
17676 /* For everything but enumeration types, consider a template-id.
17677 For an enumeration type, consider only a plain identifier. */
17678 if (tag_type
!= enum_type
)
17680 bool template_p
= false;
17683 /* Allow the `template' keyword. */
17684 template_p
= cp_parser_optional_template_keyword (parser
);
17685 /* If we didn't see `template', we don't know if there's a
17686 template-id or not. */
17688 cp_parser_parse_tentatively (parser
);
17689 /* Parse the template-id. */
17690 token
= cp_lexer_peek_token (parser
->lexer
);
17691 decl
= cp_parser_template_id (parser
, template_p
,
17692 /*check_dependency_p=*/true,
17695 /* If we didn't find a template-id, look for an ordinary
17697 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17699 /* We can get here when cp_parser_template_id, called by
17700 cp_parser_class_name with tag_type == none_type, succeeds
17701 and caches a BASELINK. Then, when called again here,
17702 instead of failing and returning an error_mark_node
17703 returns it (see template/typename17.C in C++11).
17704 ??? Could we diagnose this earlier? */
17705 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17707 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17708 type
= error_mark_node
;
17710 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17711 in effect, then we must assume that, upon instantiation, the
17712 template will correspond to a class. */
17713 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17714 && tag_type
== typename_type
)
17715 type
= make_typename_type (parser
->scope
, decl
,
17717 /*complain=*/tf_error
);
17718 /* If the `typename' keyword is in effect and DECL is not a type
17719 decl, then type is non existent. */
17720 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17722 else if (TREE_CODE (decl
) == TYPE_DECL
)
17724 type
= check_elaborated_type_specifier (tag_type
, decl
,
17725 /*allow_template_p=*/true);
17727 /* If the next token is a semicolon, this must be a specialization,
17728 instantiation, or friend declaration. Check the scope while we
17729 still know whether or not we had a nested-name-specifier. */
17730 if (type
!= error_mark_node
17731 && !nested_name_specifier
&& !is_friend
17732 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17733 check_unqualified_spec_or_inst (type
, token
->location
);
17735 else if (decl
== error_mark_node
)
17736 type
= error_mark_node
;
17741 token
= cp_lexer_peek_token (parser
->lexer
);
17742 identifier
= cp_parser_identifier (parser
);
17744 if (identifier
== error_mark_node
)
17746 parser
->scope
= NULL_TREE
;
17747 return error_mark_node
;
17750 /* For a `typename', we needn't call xref_tag. */
17751 if (tag_type
== typename_type
17752 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17753 return cp_parser_make_typename_type (parser
, identifier
,
17756 /* Template parameter lists apply only if we are not within a
17757 function parameter list. */
17758 bool template_parm_lists_apply
17759 = parser
->num_template_parameter_lists
;
17760 if (template_parm_lists_apply
)
17761 for (cp_binding_level
*s
= current_binding_level
;
17762 s
&& s
->kind
!= sk_template_parms
;
17763 s
= s
->level_chain
)
17764 if (s
->kind
== sk_function_parms
)
17765 template_parm_lists_apply
= false;
17767 /* Look up a qualified name in the usual way. */
17771 tree ambiguous_decls
;
17773 decl
= cp_parser_lookup_name (parser
, identifier
,
17775 /*is_template=*/false,
17776 /*is_namespace=*/false,
17777 /*check_dependency=*/true,
17781 /* If the lookup was ambiguous, an error will already have been
17783 if (ambiguous_decls
)
17784 return error_mark_node
;
17786 /* If we are parsing friend declaration, DECL may be a
17787 TEMPLATE_DECL tree node here. However, we need to check
17788 whether this TEMPLATE_DECL results in valid code. Consider
17789 the following example:
17792 template <class T> class C {};
17795 template <class T> friend class N::C; // #1, valid code
17797 template <class T> class Y {
17798 friend class N::C; // #2, invalid code
17801 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17802 name lookup of `N::C'. We see that friend declaration must
17803 be template for the code to be valid. Note that
17804 processing_template_decl does not work here since it is
17805 always 1 for the above two cases. */
17807 decl
= (cp_parser_maybe_treat_template_as_class
17808 (decl
, /*tag_name_p=*/is_friend
17809 && template_parm_lists_apply
));
17811 if (TREE_CODE (decl
) != TYPE_DECL
)
17813 cp_parser_diagnose_invalid_type_name (parser
,
17816 return error_mark_node
;
17819 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17821 bool allow_template
= (template_parm_lists_apply
17822 || DECL_SELF_REFERENCE_P (decl
));
17823 type
= check_elaborated_type_specifier (tag_type
, decl
,
17826 if (type
== error_mark_node
)
17827 return error_mark_node
;
17830 /* Forward declarations of nested types, such as
17835 are invalid unless all components preceding the final '::'
17836 are complete. If all enclosing types are complete, these
17837 declarations become merely pointless.
17839 Invalid forward declarations of nested types are errors
17840 caught elsewhere in parsing. Those that are pointless arrive
17843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17844 && !is_friend
&& !processing_explicit_instantiation
)
17845 warning (0, "declaration %qD does not declare anything", decl
);
17847 type
= TREE_TYPE (decl
);
17851 /* An elaborated-type-specifier sometimes introduces a new type and
17852 sometimes names an existing type. Normally, the rule is that it
17853 introduces a new type only if there is not an existing type of
17854 the same name already in scope. For example, given:
17857 void f() { struct S s; }
17859 the `struct S' in the body of `f' is the same `struct S' as in
17860 the global scope; the existing definition is used. However, if
17861 there were no global declaration, this would introduce a new
17862 local class named `S'.
17864 An exception to this rule applies to the following code:
17866 namespace N { struct S; }
17868 Here, the elaborated-type-specifier names a new type
17869 unconditionally; even if there is already an `S' in the
17870 containing scope this declaration names a new type.
17871 This exception only applies if the elaborated-type-specifier
17872 forms the complete declaration:
17876 A declaration consisting solely of `class-key identifier ;' is
17877 either a redeclaration of the name in the current scope or a
17878 forward declaration of the identifier as a class name. It
17879 introduces the name into the current scope.
17881 We are in this situation precisely when the next token is a `;'.
17883 An exception to the exception is that a `friend' declaration does
17884 *not* name a new type; i.e., given:
17886 struct S { friend struct T; };
17888 `T' is not a new type in the scope of `S'.
17890 Also, `new struct S' or `sizeof (struct S)' never results in the
17891 definition of a new type; a new type can only be declared in a
17892 declaration context. */
17898 /* Friends have special name lookup rules. */
17899 ts
= ts_within_enclosing_non_class
;
17900 else if (is_declaration
17901 && cp_lexer_next_token_is (parser
->lexer
,
17903 /* This is a `class-key identifier ;' */
17909 (template_parm_lists_apply
17910 && (cp_parser_next_token_starts_class_definition_p (parser
)
17911 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
17912 /* An unqualified name was used to reference this type, so
17913 there were no qualifying templates. */
17914 if (template_parm_lists_apply
17915 && !cp_parser_check_template_parameters (parser
,
17916 /*num_templates=*/0,
17918 /*declarator=*/NULL
))
17919 return error_mark_node
;
17920 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
17924 if (type
== error_mark_node
)
17925 return error_mark_node
;
17927 /* Allow attributes on forward declarations of classes. */
17930 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17931 warning (OPT_Wattributes
,
17932 "attributes ignored on uninstantiated type");
17933 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
17934 && ! processing_explicit_instantiation
)
17935 warning (OPT_Wattributes
,
17936 "attributes ignored on template instantiation");
17937 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
17938 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
17940 warning (OPT_Wattributes
,
17941 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17944 if (tag_type
!= enum_type
)
17946 /* Indicate whether this class was declared as a `class' or as a
17948 if (CLASS_TYPE_P (type
))
17949 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
17950 cp_parser_check_class_key (tag_type
, type
);
17953 /* A "<" cannot follow an elaborated type specifier. If that
17954 happens, the user was probably trying to form a template-id. */
17955 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
17961 /* Parse an enum-specifier.
17964 enum-head { enumerator-list [opt] }
17965 enum-head { enumerator-list , } [C++0x]
17968 enum-key identifier [opt] enum-base [opt]
17969 enum-key nested-name-specifier identifier enum-base [opt]
17974 enum struct [C++0x]
17977 : type-specifier-seq
17979 opaque-enum-specifier:
17980 enum-key identifier enum-base [opt] ;
17983 enum-key attributes[opt] identifier [opt] enum-base [opt]
17984 { enumerator-list [opt] }attributes[opt]
17985 enum-key attributes[opt] identifier [opt] enum-base [opt]
17986 { enumerator-list, }attributes[opt] [C++0x]
17988 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17989 if the token stream isn't an enum-specifier after all. */
17992 cp_parser_enum_specifier (cp_parser
* parser
)
17995 tree type
= NULL_TREE
;
17997 tree nested_name_specifier
= NULL_TREE
;
17999 bool scoped_enum_p
= false;
18000 bool has_underlying_type
= false;
18001 bool nested_being_defined
= false;
18002 bool new_value_list
= false;
18003 bool is_new_type
= false;
18004 bool is_unnamed
= false;
18005 tree underlying_type
= NULL_TREE
;
18006 cp_token
*type_start_token
= NULL
;
18007 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18009 parser
->colon_corrects_to_scope_p
= false;
18011 /* Parse tentatively so that we can back up if we don't find a
18013 cp_parser_parse_tentatively (parser
);
18015 /* Caller guarantees that the current token is 'enum', an identifier
18016 possibly follows, and the token after that is an opening brace.
18017 If we don't have an identifier, fabricate an anonymous name for
18018 the enumeration being defined. */
18019 cp_lexer_consume_token (parser
->lexer
);
18021 /* Parse the "class" or "struct", which indicates a scoped
18022 enumeration type in C++0x. */
18023 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18024 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18026 if (cxx_dialect
< cxx11
)
18027 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18029 /* Consume the `struct' or `class' token. */
18030 cp_lexer_consume_token (parser
->lexer
);
18032 scoped_enum_p
= true;
18035 attributes
= cp_parser_attributes_opt (parser
);
18037 /* Clear the qualification. */
18038 parser
->scope
= NULL_TREE
;
18039 parser
->qualifying_scope
= NULL_TREE
;
18040 parser
->object_scope
= NULL_TREE
;
18042 /* Figure out in what scope the declaration is being placed. */
18043 prev_scope
= current_scope ();
18045 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18047 push_deferring_access_checks (dk_no_check
);
18048 nested_name_specifier
18049 = cp_parser_nested_name_specifier_opt (parser
,
18050 /*typename_keyword_p=*/true,
18051 /*check_dependency_p=*/false,
18053 /*is_declaration=*/false);
18055 if (nested_name_specifier
)
18059 identifier
= cp_parser_identifier (parser
);
18060 name
= cp_parser_lookup_name (parser
, identifier
,
18062 /*is_template=*/false,
18063 /*is_namespace=*/false,
18064 /*check_dependency=*/true,
18065 /*ambiguous_decls=*/NULL
,
18067 if (name
&& name
!= error_mark_node
)
18069 type
= TREE_TYPE (name
);
18070 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18072 /* Are template enums allowed in ISO? */
18073 if (template_parm_scope_p ())
18074 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18075 "%qD is an enumeration template", name
);
18076 /* ignore a typename reference, for it will be solved by name
18081 else if (nested_name_specifier
== error_mark_node
)
18082 /* We already issued an error. */;
18085 error_at (type_start_token
->location
,
18086 "%qD does not name an enumeration in %qT",
18087 identifier
, nested_name_specifier
);
18088 nested_name_specifier
= error_mark_node
;
18093 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18094 identifier
= cp_parser_identifier (parser
);
18097 identifier
= make_anon_name ();
18100 error_at (type_start_token
->location
,
18101 "unnamed scoped enum is not allowed");
18104 pop_deferring_access_checks ();
18106 /* Check for the `:' that denotes a specified underlying type in C++0x.
18107 Note that a ':' could also indicate a bitfield width, however. */
18108 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18110 cp_decl_specifier_seq type_specifiers
;
18112 /* Consume the `:'. */
18113 cp_lexer_consume_token (parser
->lexer
);
18115 /* Parse the type-specifier-seq. */
18116 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18117 /*is_trailing_return=*/false,
18120 /* At this point this is surely not elaborated type specifier. */
18121 if (!cp_parser_parse_definitely (parser
))
18124 if (cxx_dialect
< cxx11
)
18125 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18127 has_underlying_type
= true;
18129 /* If that didn't work, stop. */
18130 if (type_specifiers
.type
!= error_mark_node
)
18132 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18133 /*initialized=*/0, NULL
);
18134 if (underlying_type
== error_mark_node
18135 || check_for_bare_parameter_packs (underlying_type
))
18136 underlying_type
= NULL_TREE
;
18140 /* Look for the `{' but don't consume it yet. */
18141 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18143 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18145 cp_parser_error (parser
, "expected %<{%>");
18146 if (has_underlying_type
)
18152 /* An opaque-enum-specifier must have a ';' here. */
18153 if ((scoped_enum_p
|| underlying_type
)
18154 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18156 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18157 if (has_underlying_type
)
18165 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18168 if (nested_name_specifier
)
18170 if (CLASS_TYPE_P (nested_name_specifier
))
18172 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18173 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18174 push_scope (nested_name_specifier
);
18176 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18178 push_nested_namespace (nested_name_specifier
);
18182 /* Issue an error message if type-definitions are forbidden here. */
18183 if (!cp_parser_check_type_definition (parser
))
18184 type
= error_mark_node
;
18186 /* Create the new type. We do this before consuming the opening
18187 brace so the enum will be recorded as being on the line of its
18188 tag (or the 'enum' keyword, if there is no tag). */
18189 type
= start_enum (identifier
, type
, underlying_type
,
18190 attributes
, scoped_enum_p
, &is_new_type
);
18192 /* If the next token is not '{' it is an opaque-enum-specifier or an
18193 elaborated-type-specifier. */
18194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18196 timevar_push (TV_PARSE_ENUM
);
18197 if (nested_name_specifier
18198 && nested_name_specifier
!= error_mark_node
)
18200 /* The following catches invalid code such as:
18201 enum class S<int>::E { A, B, C }; */
18202 if (!processing_specialization
18203 && CLASS_TYPE_P (nested_name_specifier
)
18204 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18205 error_at (type_start_token
->location
, "cannot add an enumerator "
18206 "list to a template instantiation");
18208 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18210 error_at (type_start_token
->location
,
18211 "%<%T::%E%> has not been declared",
18212 TYPE_CONTEXT (nested_name_specifier
),
18213 nested_name_specifier
);
18214 type
= error_mark_node
;
18216 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18217 && !CLASS_TYPE_P (nested_name_specifier
))
18219 error_at (type_start_token
->location
, "nested name specifier "
18220 "%qT for enum declaration does not name a class "
18221 "or namespace", nested_name_specifier
);
18222 type
= error_mark_node
;
18224 /* If that scope does not contain the scope in which the
18225 class was originally declared, the program is invalid. */
18226 else if (prev_scope
&& !is_ancestor (prev_scope
,
18227 nested_name_specifier
))
18229 if (at_namespace_scope_p ())
18230 error_at (type_start_token
->location
,
18231 "declaration of %qD in namespace %qD which does not "
18233 type
, prev_scope
, nested_name_specifier
);
18235 error_at (type_start_token
->location
,
18236 "declaration of %qD in %qD which does not "
18238 type
, prev_scope
, nested_name_specifier
);
18239 type
= error_mark_node
;
18241 /* If that scope is the scope where the declaration is being placed
18242 the program is invalid. */
18243 else if (CLASS_TYPE_P (nested_name_specifier
)
18244 && CLASS_TYPE_P (prev_scope
)
18245 && same_type_p (nested_name_specifier
, prev_scope
))
18247 permerror (type_start_token
->location
,
18248 "extra qualification not allowed");
18249 nested_name_specifier
= NULL_TREE
;
18254 begin_scope (sk_scoped_enum
, type
);
18256 /* Consume the opening brace. */
18257 matching_braces braces
;
18258 braces
.consume_open (parser
);
18260 if (type
== error_mark_node
)
18261 ; /* Nothing to add */
18262 else if (OPAQUE_ENUM_P (type
)
18263 || (cxx_dialect
> cxx98
&& processing_specialization
))
18265 new_value_list
= true;
18266 SET_OPAQUE_ENUM_P (type
, false);
18267 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18271 error_at (type_start_token
->location
,
18272 "multiple definition of %q#T", type
);
18273 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18274 "previous definition here");
18275 type
= error_mark_node
;
18278 if (type
== error_mark_node
)
18279 cp_parser_skip_to_end_of_block_or_statement (parser
);
18280 /* If the next token is not '}', then there are some enumerators. */
18281 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18283 if (is_unnamed
&& !scoped_enum_p
)
18284 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18285 "ISO C++ forbids empty unnamed enum");
18288 cp_parser_enumerator_list (parser
, type
);
18290 /* Consume the final '}'. */
18291 braces
.require_close (parser
);
18295 timevar_pop (TV_PARSE_ENUM
);
18299 /* If a ';' follows, then it is an opaque-enum-specifier
18300 and additional restrictions apply. */
18301 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18304 error_at (type_start_token
->location
,
18305 "opaque-enum-specifier without name");
18306 else if (nested_name_specifier
)
18307 error_at (type_start_token
->location
,
18308 "opaque-enum-specifier must use a simple identifier");
18312 /* Look for trailing attributes to apply to this enumeration, and
18313 apply them if appropriate. */
18314 if (cp_parser_allow_gnu_extensions_p (parser
))
18316 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18317 cplus_decl_attributes (&type
,
18319 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18322 /* Finish up the enumeration. */
18323 if (type
!= error_mark_node
)
18325 if (new_value_list
)
18326 finish_enum_value_list (type
);
18328 finish_enum (type
);
18331 if (nested_name_specifier
)
18333 if (CLASS_TYPE_P (nested_name_specifier
))
18335 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18336 pop_scope (nested_name_specifier
);
18338 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18340 pop_nested_namespace (nested_name_specifier
);
18344 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18348 /* Parse an enumerator-list. The enumerators all have the indicated
18352 enumerator-definition
18353 enumerator-list , enumerator-definition */
18356 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18360 /* Parse an enumerator-definition. */
18361 cp_parser_enumerator_definition (parser
, type
);
18363 /* If the next token is not a ',', we've reached the end of
18365 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18367 /* Otherwise, consume the `,' and keep going. */
18368 cp_lexer_consume_token (parser
->lexer
);
18369 /* If the next token is a `}', there is a trailing comma. */
18370 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18372 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18373 pedwarn (input_location
, OPT_Wpedantic
,
18374 "comma at end of enumerator list");
18380 /* Parse an enumerator-definition. The enumerator has the indicated
18383 enumerator-definition:
18385 enumerator = constant-expression
18392 enumerator-definition:
18393 enumerator attributes [opt]
18394 enumerator attributes [opt] = constant-expression */
18397 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18403 /* Save the input location because we are interested in the location
18404 of the identifier and not the location of the explicit value. */
18405 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18407 /* Look for the identifier. */
18408 identifier
= cp_parser_identifier (parser
);
18409 if (identifier
== error_mark_node
)
18412 /* Parse any specified attributes. */
18413 tree attrs
= cp_parser_attributes_opt (parser
);
18415 /* If the next token is an '=', then there is an explicit value. */
18416 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18418 /* Consume the `=' token. */
18419 cp_lexer_consume_token (parser
->lexer
);
18420 /* Parse the value. */
18421 value
= cp_parser_constant_expression (parser
);
18426 /* If we are processing a template, make sure the initializer of the
18427 enumerator doesn't contain any bare template parameter pack. */
18428 if (check_for_bare_parameter_packs (value
))
18429 value
= error_mark_node
;
18431 /* Create the enumerator. */
18432 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18435 /* Parse a namespace-name.
18438 original-namespace-name
18441 Returns the NAMESPACE_DECL for the namespace. */
18444 cp_parser_namespace_name (cp_parser
* parser
)
18447 tree namespace_decl
;
18449 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18451 /* Get the name of the namespace. */
18452 identifier
= cp_parser_identifier (parser
);
18453 if (identifier
== error_mark_node
)
18454 return error_mark_node
;
18456 /* Look up the identifier in the currently active scope. Look only
18457 for namespaces, due to:
18459 [basic.lookup.udir]
18461 When looking up a namespace-name in a using-directive or alias
18462 definition, only namespace names are considered.
18466 [basic.lookup.qual]
18468 During the lookup of a name preceding the :: scope resolution
18469 operator, object, function, and enumerator names are ignored.
18471 (Note that cp_parser_qualifying_entity only calls this
18472 function if the token after the name is the scope resolution
18474 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18476 /*is_template=*/false,
18477 /*is_namespace=*/true,
18478 /*check_dependency=*/true,
18479 /*ambiguous_decls=*/NULL
,
18481 /* If it's not a namespace, issue an error. */
18482 if (namespace_decl
== error_mark_node
18483 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18485 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18487 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18488 if (namespace_decl
== error_mark_node
18489 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18490 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18493 cp_parser_error (parser
, "expected namespace-name");
18494 namespace_decl
= error_mark_node
;
18497 return namespace_decl
;
18500 /* Parse a namespace-definition.
18502 namespace-definition:
18503 named-namespace-definition
18504 unnamed-namespace-definition
18506 named-namespace-definition:
18507 original-namespace-definition
18508 extension-namespace-definition
18510 original-namespace-definition:
18511 namespace identifier { namespace-body }
18513 extension-namespace-definition:
18514 namespace original-namespace-name { namespace-body }
18516 unnamed-namespace-definition:
18517 namespace { namespace-body } */
18520 cp_parser_namespace_definition (cp_parser
* parser
)
18523 int nested_definition_count
= 0;
18525 cp_ensure_no_omp_declare_simd (parser
);
18526 cp_ensure_no_oacc_routine (parser
);
18528 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18532 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18533 cp_lexer_consume_token (parser
->lexer
);
18536 /* Look for the `namespace' keyword. */
18538 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18540 /* Parse any specified attributes before the identifier. */
18541 tree attribs
= cp_parser_attributes_opt (parser
);
18545 identifier
= NULL_TREE
;
18547 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18549 identifier
= cp_parser_identifier (parser
);
18551 /* Parse any attributes specified after the identifier. */
18552 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18555 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18558 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18559 pedwarn (input_location
, OPT_Wpedantic
,
18560 "nested namespace definitions only available with "
18561 "-std=c++17 or -std=gnu++17");
18563 /* Nested namespace names can create new namespaces (unlike
18564 other qualified-ids). */
18565 if (int count
= identifier
? push_namespace (identifier
) : 0)
18566 nested_definition_count
+= count
;
18568 cp_parser_error (parser
, "nested namespace name required");
18569 cp_lexer_consume_token (parser
->lexer
);
18572 if (nested_definition_count
&& !identifier
)
18573 cp_parser_error (parser
, "namespace name required");
18575 if (nested_definition_count
&& attribs
)
18576 error_at (token
->location
,
18577 "a nested namespace definition cannot have attributes");
18578 if (nested_definition_count
&& is_inline
)
18579 error_at (token
->location
,
18580 "a nested namespace definition cannot be inline");
18582 /* Start the namespace. */
18583 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18585 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18587 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18589 /* Look for the `{' to validate starting the namespace. */
18590 matching_braces braces
;
18591 if (braces
.require_open (parser
))
18593 /* Parse the body of the namespace. */
18594 cp_parser_namespace_body (parser
);
18596 /* Look for the final `}'. */
18597 braces
.require_close (parser
);
18600 if (has_visibility
)
18601 pop_visibility (1);
18603 /* Pop the nested namespace definitions. */
18604 while (nested_definition_count
--)
18608 /* Parse a namespace-body.
18611 declaration-seq [opt] */
18614 cp_parser_namespace_body (cp_parser
* parser
)
18616 cp_parser_declaration_seq_opt (parser
);
18619 /* Parse a namespace-alias-definition.
18621 namespace-alias-definition:
18622 namespace identifier = qualified-namespace-specifier ; */
18625 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18628 tree namespace_specifier
;
18630 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18632 /* Look for the `namespace' keyword. */
18633 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18634 /* Look for the identifier. */
18635 identifier
= cp_parser_identifier (parser
);
18636 if (identifier
== error_mark_node
)
18638 /* Look for the `=' token. */
18639 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18640 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18642 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18643 /* Skip the definition. */
18644 cp_lexer_consume_token (parser
->lexer
);
18645 if (cp_parser_skip_to_closing_brace (parser
))
18646 cp_lexer_consume_token (parser
->lexer
);
18649 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18650 /* Look for the qualified-namespace-specifier. */
18651 namespace_specifier
18652 = cp_parser_qualified_namespace_specifier (parser
);
18653 /* Look for the `;' token. */
18654 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18656 /* Register the alias in the symbol table. */
18657 do_namespace_alias (identifier
, namespace_specifier
);
18660 /* Parse a qualified-namespace-specifier.
18662 qualified-namespace-specifier:
18663 :: [opt] nested-name-specifier [opt] namespace-name
18665 Returns a NAMESPACE_DECL corresponding to the specified
18669 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18671 /* Look for the optional `::'. */
18672 cp_parser_global_scope_opt (parser
,
18673 /*current_scope_valid_p=*/false);
18675 /* Look for the optional nested-name-specifier. */
18676 cp_parser_nested_name_specifier_opt (parser
,
18677 /*typename_keyword_p=*/false,
18678 /*check_dependency_p=*/true,
18680 /*is_declaration=*/true);
18682 return cp_parser_namespace_name (parser
);
18685 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18686 access declaration.
18689 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18690 using :: unqualified-id ;
18692 access-declaration:
18698 cp_parser_using_declaration (cp_parser
* parser
,
18699 bool access_declaration_p
)
18702 bool typename_p
= false;
18703 bool global_scope_p
;
18707 int oldcount
= errorcount
;
18708 cp_token
*diag_token
= NULL
;
18710 if (access_declaration_p
)
18712 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18713 cp_parser_parse_tentatively (parser
);
18717 /* Look for the `using' keyword. */
18718 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18721 /* Peek at the next token. */
18722 token
= cp_lexer_peek_token (parser
->lexer
);
18723 /* See if it's `typename'. */
18724 if (token
->keyword
== RID_TYPENAME
)
18726 /* Remember that we've seen it. */
18728 /* Consume the `typename' token. */
18729 cp_lexer_consume_token (parser
->lexer
);
18733 /* Look for the optional global scope qualification. */
18735 = (cp_parser_global_scope_opt (parser
,
18736 /*current_scope_valid_p=*/false)
18739 /* If we saw `typename', or didn't see `::', then there must be a
18740 nested-name-specifier present. */
18741 if (typename_p
|| !global_scope_p
)
18743 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18744 /*check_dependency_p=*/true,
18746 /*is_declaration=*/true);
18747 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18749 cp_parser_skip_to_end_of_block_or_statement (parser
);
18753 /* Otherwise, we could be in either of the two productions. In that
18754 case, treat the nested-name-specifier as optional. */
18756 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18757 /*typename_keyword_p=*/false,
18758 /*check_dependency_p=*/true,
18760 /*is_declaration=*/true);
18762 qscope
= global_namespace
;
18763 else if (UNSCOPED_ENUM_P (qscope
))
18764 qscope
= CP_TYPE_CONTEXT (qscope
);
18766 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18767 /* Something has already gone wrong; there's no need to parse
18768 further. Since an error has occurred, the return value of
18769 cp_parser_parse_definitely will be false, as required. */
18770 return cp_parser_parse_definitely (parser
);
18772 token
= cp_lexer_peek_token (parser
->lexer
);
18773 /* Parse the unqualified-id. */
18774 identifier
= cp_parser_unqualified_id (parser
,
18775 /*template_keyword_p=*/false,
18776 /*check_dependency_p=*/true,
18777 /*declarator_p=*/true,
18778 /*optional_p=*/false);
18780 if (access_declaration_p
)
18782 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18783 cp_parser_simulate_error (parser
);
18784 if (!cp_parser_parse_definitely (parser
))
18787 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18789 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18790 if (cxx_dialect
< cxx17
18791 && !in_system_header_at (ell
->location
))
18792 pedwarn (ell
->location
, 0,
18793 "pack expansion in using-declaration only available "
18794 "with -std=c++17 or -std=gnu++17");
18795 qscope
= make_pack_expansion (qscope
);
18798 /* The function we call to handle a using-declaration is different
18799 depending on what scope we are in. */
18800 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18802 else if (!identifier_p (identifier
)
18803 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18804 /* [namespace.udecl]
18806 A using declaration shall not name a template-id. */
18807 error_at (token
->location
,
18808 "a template-id may not appear in a using-declaration");
18811 if (at_class_scope_p ())
18813 /* Create the USING_DECL. */
18814 decl
= do_class_using_decl (qscope
, identifier
);
18816 if (decl
&& typename_p
)
18817 USING_DECL_TYPENAME_P (decl
) = 1;
18819 if (check_for_bare_parameter_packs (decl
))
18821 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18825 /* Add it to the list of members in this class. */
18826 finish_member_declaration (decl
);
18830 decl
= cp_parser_lookup_name_simple (parser
,
18833 if (decl
== error_mark_node
)
18834 cp_parser_name_lookup_error (parser
, identifier
,
18837 else if (check_for_bare_parameter_packs (decl
))
18839 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18842 else if (!at_namespace_scope_p ())
18843 finish_local_using_decl (decl
, qscope
, identifier
);
18845 finish_namespace_using_decl (decl
, qscope
, identifier
);
18849 if (!access_declaration_p
18850 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18852 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18853 if (cxx_dialect
< cxx17
)
18854 pedwarn (comma
->location
, 0,
18855 "comma-separated list in using-declaration only available "
18856 "with -std=c++17 or -std=gnu++17");
18860 /* Look for the final `;'. */
18861 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18863 if (access_declaration_p
&& errorcount
== oldcount
)
18864 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18865 "access declarations are deprecated "
18866 "in favour of using-declarations; "
18867 "suggestion: add the %<using%> keyword");
18872 /* Parse an alias-declaration.
18875 using identifier attribute-specifier-seq [opt] = type-id */
18878 cp_parser_alias_declaration (cp_parser
* parser
)
18880 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18881 location_t id_location
;
18882 cp_declarator
*declarator
;
18883 cp_decl_specifier_seq decl_specs
;
18885 const char *saved_message
= NULL
;
18887 /* Look for the `using' keyword. */
18888 cp_token
*using_token
18889 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18890 if (using_token
== NULL
)
18891 return error_mark_node
;
18893 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18894 id
= cp_parser_identifier (parser
);
18895 if (id
== error_mark_node
)
18896 return error_mark_node
;
18898 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18899 attributes
= cp_parser_attributes_opt (parser
);
18900 if (attributes
== error_mark_node
)
18901 return error_mark_node
;
18903 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18905 if (cp_parser_error_occurred (parser
))
18906 return error_mark_node
;
18908 cp_parser_commit_to_tentative_parse (parser
);
18910 /* Now we are going to parse the type-id of the declaration. */
18915 "A type-specifier-seq shall not define a class or enumeration
18916 unless it appears in the type-id of an alias-declaration (7.1.3) that
18917 is not the declaration of a template-declaration."
18919 In other words, if we currently are in an alias template, the
18920 type-id should not define a type.
18922 So let's set parser->type_definition_forbidden_message in that
18923 case; cp_parser_check_type_definition (called by
18924 cp_parser_class_specifier) will then emit an error if a type is
18925 defined in the type-id. */
18926 if (parser
->num_template_parameter_lists
)
18928 saved_message
= parser
->type_definition_forbidden_message
;
18929 parser
->type_definition_forbidden_message
=
18930 G_("types may not be defined in alias template declarations");
18933 type
= cp_parser_type_id (parser
);
18935 /* Restore the error message if need be. */
18936 if (parser
->num_template_parameter_lists
)
18937 parser
->type_definition_forbidden_message
= saved_message
;
18939 if (type
== error_mark_node
18940 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
18942 cp_parser_skip_to_end_of_block_or_statement (parser
);
18943 return error_mark_node
;
18946 /* A typedef-name can also be introduced by an alias-declaration. The
18947 identifier following the using keyword becomes a typedef-name. It has
18948 the same semantics as if it were introduced by the typedef
18949 specifier. In particular, it does not define a new type and it shall
18950 not appear in the type-id. */
18952 clear_decl_specs (&decl_specs
);
18953 decl_specs
.type
= type
;
18954 if (attributes
!= NULL_TREE
)
18956 decl_specs
.attributes
= attributes
;
18957 set_and_check_decl_spec_loc (&decl_specs
,
18961 set_and_check_decl_spec_loc (&decl_specs
,
18964 set_and_check_decl_spec_loc (&decl_specs
,
18968 if (parser
->num_template_parameter_lists
18969 && !cp_parser_check_template_parameters (parser
,
18970 /*num_templates=*/0,
18972 /*declarator=*/NULL
))
18973 return error_mark_node
;
18975 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
18976 declarator
->id_loc
= id_location
;
18978 member_p
= at_class_scope_p ();
18980 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
18981 NULL_TREE
, attributes
);
18983 decl
= start_decl (declarator
, &decl_specs
, 0,
18984 attributes
, NULL_TREE
, &pushed_scope
);
18985 if (decl
== error_mark_node
)
18988 // Attach constraints to the alias declaration.
18989 if (flag_concepts
&& current_template_parms
)
18991 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
18992 tree constr
= build_constraints (reqs
, NULL_TREE
);
18993 set_constraints (decl
, constr
);
18996 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
18999 pop_scope (pushed_scope
);
19001 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19002 added into the symbol table; otherwise, return the TYPE_DECL. */
19003 if (DECL_LANG_SPECIFIC (decl
)
19004 && DECL_TEMPLATE_INFO (decl
)
19005 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19007 decl
= DECL_TI_TEMPLATE (decl
);
19009 check_member_template (decl
);
19015 /* Parse a using-directive.
19018 using namespace :: [opt] nested-name-specifier [opt]
19019 namespace-name ; */
19022 cp_parser_using_directive (cp_parser
* parser
)
19024 tree namespace_decl
;
19027 /* Look for the `using' keyword. */
19028 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19029 /* And the `namespace' keyword. */
19030 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19031 /* Look for the optional `::' operator. */
19032 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19033 /* And the optional nested-name-specifier. */
19034 cp_parser_nested_name_specifier_opt (parser
,
19035 /*typename_keyword_p=*/false,
19036 /*check_dependency_p=*/true,
19038 /*is_declaration=*/true);
19039 /* Get the namespace being used. */
19040 namespace_decl
= cp_parser_namespace_name (parser
);
19041 /* And any specified attributes. */
19042 attribs
= cp_parser_attributes_opt (parser
);
19044 /* Update the symbol table. */
19045 if (namespace_bindings_p ())
19046 finish_namespace_using_directive (namespace_decl
, attribs
);
19048 finish_local_using_directive (namespace_decl
, attribs
);
19050 /* Look for the final `;'. */
19051 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19054 /* Parse an asm-definition.
19057 asm ( string-literal ) ;
19062 asm volatile [opt] ( string-literal ) ;
19063 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19064 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19065 : asm-operand-list [opt] ) ;
19066 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19067 : asm-operand-list [opt]
19068 : asm-clobber-list [opt] ) ;
19069 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19070 : asm-clobber-list [opt]
19071 : asm-goto-list ) ; */
19074 cp_parser_asm_definition (cp_parser
* parser
)
19077 tree outputs
= NULL_TREE
;
19078 tree inputs
= NULL_TREE
;
19079 tree clobbers
= NULL_TREE
;
19080 tree labels
= NULL_TREE
;
19082 bool volatile_p
= false;
19083 bool extended_p
= false;
19084 bool invalid_inputs_p
= false;
19085 bool invalid_outputs_p
= false;
19086 bool goto_p
= false;
19087 required_token missing
= RT_NONE
;
19089 /* Look for the `asm' keyword. */
19090 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19092 if (parser
->in_function_body
19093 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19095 error ("%<asm%> in %<constexpr%> function");
19096 cp_function_chain
->invalid_constexpr
= true;
19099 /* See if the next token is `volatile'. */
19100 if (cp_parser_allow_gnu_extensions_p (parser
)
19101 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19103 /* Remember that we saw the `volatile' keyword. */
19105 /* Consume the token. */
19106 cp_lexer_consume_token (parser
->lexer
);
19108 if (cp_parser_allow_gnu_extensions_p (parser
)
19109 && parser
->in_function_body
19110 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19112 /* Remember that we saw the `goto' keyword. */
19114 /* Consume the token. */
19115 cp_lexer_consume_token (parser
->lexer
);
19117 /* Look for the opening `('. */
19118 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19120 /* Look for the string. */
19121 string
= cp_parser_string_literal (parser
, false, false);
19122 if (string
== error_mark_node
)
19124 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19125 /*consume_paren=*/true);
19129 /* If we're allowing GNU extensions, check for the extended assembly
19130 syntax. Unfortunately, the `:' tokens need not be separated by
19131 a space in C, and so, for compatibility, we tolerate that here
19132 too. Doing that means that we have to treat the `::' operator as
19134 if (cp_parser_allow_gnu_extensions_p (parser
)
19135 && parser
->in_function_body
19136 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19137 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19139 bool inputs_p
= false;
19140 bool clobbers_p
= false;
19141 bool labels_p
= false;
19143 /* The extended syntax was used. */
19146 /* Look for outputs. */
19147 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19149 /* Consume the `:'. */
19150 cp_lexer_consume_token (parser
->lexer
);
19151 /* Parse the output-operands. */
19152 if (cp_lexer_next_token_is_not (parser
->lexer
,
19154 && cp_lexer_next_token_is_not (parser
->lexer
,
19156 && cp_lexer_next_token_is_not (parser
->lexer
,
19160 outputs
= cp_parser_asm_operand_list (parser
);
19161 if (outputs
== error_mark_node
)
19162 invalid_outputs_p
= true;
19165 /* If the next token is `::', there are no outputs, and the
19166 next token is the beginning of the inputs. */
19167 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19168 /* The inputs are coming next. */
19171 /* Look for inputs. */
19173 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19175 /* Consume the `:' or `::'. */
19176 cp_lexer_consume_token (parser
->lexer
);
19177 /* Parse the output-operands. */
19178 if (cp_lexer_next_token_is_not (parser
->lexer
,
19180 && cp_lexer_next_token_is_not (parser
->lexer
,
19182 && cp_lexer_next_token_is_not (parser
->lexer
,
19185 inputs
= cp_parser_asm_operand_list (parser
);
19186 if (inputs
== error_mark_node
)
19187 invalid_inputs_p
= true;
19190 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19191 /* The clobbers are coming next. */
19194 /* Look for clobbers. */
19196 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19199 /* Consume the `:' or `::'. */
19200 cp_lexer_consume_token (parser
->lexer
);
19201 /* Parse the clobbers. */
19202 if (cp_lexer_next_token_is_not (parser
->lexer
,
19204 && cp_lexer_next_token_is_not (parser
->lexer
,
19206 clobbers
= cp_parser_asm_clobber_list (parser
);
19209 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19210 /* The labels are coming next. */
19213 /* Look for labels. */
19215 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19218 /* Consume the `:' or `::'. */
19219 cp_lexer_consume_token (parser
->lexer
);
19220 /* Parse the labels. */
19221 labels
= cp_parser_asm_label_list (parser
);
19224 if (goto_p
&& !labels_p
)
19225 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19228 missing
= RT_COLON_SCOPE
;
19230 /* Look for the closing `)'. */
19231 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19232 missing
? missing
: RT_CLOSE_PAREN
))
19233 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19234 /*consume_paren=*/true);
19235 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19237 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19239 /* Create the ASM_EXPR. */
19240 if (parser
->in_function_body
)
19242 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19243 inputs
, clobbers
, labels
);
19244 /* If the extended syntax was not used, mark the ASM_EXPR. */
19247 tree temp
= asm_stmt
;
19248 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19249 temp
= TREE_OPERAND (temp
, 0);
19251 ASM_INPUT_P (temp
) = 1;
19255 symtab
->finalize_toplevel_asm (string
);
19259 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19260 type that comes from the decl-specifier-seq. */
19263 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19265 for (cp_declarator
*d
= declarator
; d
;)
19275 if (TYPE_PTRMEMFUNC_P (type
))
19276 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19277 type
= TREE_TYPE (type
);
19285 /* Declarators [gram.dcl.decl] */
19287 /* Parse an init-declarator.
19290 declarator initializer [opt]
19295 declarator asm-specification [opt] attributes [opt] initializer [opt]
19297 function-definition:
19298 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19300 decl-specifier-seq [opt] declarator function-try-block
19304 function-definition:
19305 __extension__ function-definition
19309 function-definition:
19310 decl-specifier-seq [opt] declarator function-transaction-block
19312 The DECL_SPECIFIERS apply to this declarator. Returns a
19313 representation of the entity declared. If MEMBER_P is TRUE, then
19314 this declarator appears in a class scope. The new DECL created by
19315 this declarator is returned.
19317 The CHECKS are access checks that should be performed once we know
19318 what entity is being declared (and, therefore, what classes have
19321 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19322 for a function-definition here as well. If the declarator is a
19323 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19324 be TRUE upon return. By that point, the function-definition will
19325 have been completely parsed.
19327 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19330 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19331 parsed declaration if it is an uninitialized single declarator not followed
19332 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19333 if present, will not be consumed. If returned, this declarator will be
19334 created with SD_INITIALIZED but will not call cp_finish_decl.
19336 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19337 and there is an initializer, the pointed location_t is set to the
19338 location of the '=' or `(', or '{' in C++11 token introducing the
19342 cp_parser_init_declarator (cp_parser
* parser
,
19343 cp_decl_specifier_seq
*decl_specifiers
,
19344 vec
<deferred_access_check
, va_gc
> *checks
,
19345 bool function_definition_allowed_p
,
19347 int declares_class_or_enum
,
19348 bool* function_definition_p
,
19349 tree
* maybe_range_for_decl
,
19350 location_t
* init_loc
,
19353 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19354 *attributes_start_token
= NULL
;
19355 cp_declarator
*declarator
;
19356 tree prefix_attributes
;
19357 tree attributes
= NULL
;
19358 tree asm_specification
;
19360 tree decl
= NULL_TREE
;
19362 int is_initialized
;
19363 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19364 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19366 enum cpp_ttype initialization_kind
;
19367 bool is_direct_init
= false;
19368 bool is_non_constant_init
;
19369 int ctor_dtor_or_conv_p
;
19370 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19371 tree pushed_scope
= NULL_TREE
;
19372 bool range_for_decl_p
= false;
19373 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19374 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19376 /* Gather the attributes that were provided with the
19377 decl-specifiers. */
19378 prefix_attributes
= decl_specifiers
->attributes
;
19380 /* Assume that this is not the declarator for a function
19382 if (function_definition_p
)
19383 *function_definition_p
= false;
19385 /* Default arguments are only permitted for function parameters. */
19386 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19387 parser
->default_arg_ok_p
= false;
19389 /* Defer access checks while parsing the declarator; we cannot know
19390 what names are accessible until we know what is being
19392 resume_deferring_access_checks ();
19394 token
= cp_lexer_peek_token (parser
->lexer
);
19396 /* Parse the declarator. */
19398 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19399 &ctor_dtor_or_conv_p
,
19400 /*parenthesized_p=*/NULL
,
19401 member_p
, friend_p
);
19402 /* Gather up the deferred checks. */
19403 stop_deferring_access_checks ();
19405 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19407 /* If the DECLARATOR was erroneous, there's no need to go
19409 if (declarator
== cp_error_declarator
)
19410 return error_mark_node
;
19412 /* Check that the number of template-parameter-lists is OK. */
19413 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19415 return error_mark_node
;
19417 if (declares_class_or_enum
& 2)
19418 cp_parser_check_for_definition_in_return_type (declarator
,
19419 decl_specifiers
->type
,
19420 decl_specifiers
->locations
[ds_type_spec
]);
19422 /* Figure out what scope the entity declared by the DECLARATOR is
19423 located in. `grokdeclarator' sometimes changes the scope, so
19424 we compute it now. */
19425 scope
= get_scope_of_declarator (declarator
);
19427 /* Perform any lookups in the declared type which were thought to be
19428 dependent, but are not in the scope of the declarator. */
19429 decl_specifiers
->type
19430 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19432 /* If we're allowing GNU extensions, look for an
19433 asm-specification. */
19434 if (cp_parser_allow_gnu_extensions_p (parser
))
19436 /* Look for an asm-specification. */
19437 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19438 asm_specification
= cp_parser_asm_specification_opt (parser
);
19441 asm_specification
= NULL_TREE
;
19443 /* Look for attributes. */
19444 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19445 attributes
= cp_parser_attributes_opt (parser
);
19447 /* Peek at the next token. */
19448 token
= cp_lexer_peek_token (parser
->lexer
);
19450 bool bogus_implicit_tmpl
= false;
19452 if (function_declarator_p (declarator
))
19454 /* Handle C++17 deduction guides. */
19455 if (!decl_specifiers
->type
19456 && ctor_dtor_or_conv_p
<= 0
19457 && cxx_dialect
>= cxx17
)
19459 cp_declarator
*id
= get_id_declarator (declarator
);
19460 tree name
= id
->u
.id
.unqualified_name
;
19461 parser
->scope
= id
->u
.id
.qualifying_scope
;
19462 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19464 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19465 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19467 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19468 id
->u
.id
.sfk
= sfk_deduction_guide
;
19469 ctor_dtor_or_conv_p
= 1;
19473 /* Check to see if the token indicates the start of a
19474 function-definition. */
19475 if (cp_parser_token_starts_function_definition_p (token
))
19477 if (!function_definition_allowed_p
)
19479 /* If a function-definition should not appear here, issue an
19481 cp_parser_error (parser
,
19482 "a function-definition is not allowed here");
19483 return error_mark_node
;
19486 location_t func_brace_location
19487 = cp_lexer_peek_token (parser
->lexer
)->location
;
19489 /* Neither attributes nor an asm-specification are allowed
19490 on a function-definition. */
19491 if (asm_specification
)
19492 error_at (asm_spec_start_token
->location
,
19493 "an asm-specification is not allowed "
19494 "on a function-definition");
19496 error_at (attributes_start_token
->location
,
19497 "attributes are not allowed "
19498 "on a function-definition");
19499 /* This is a function-definition. */
19500 *function_definition_p
= true;
19502 /* Parse the function definition. */
19504 decl
= cp_parser_save_member_function_body (parser
,
19507 prefix_attributes
);
19510 (cp_parser_function_definition_from_specifiers_and_declarator
19511 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19513 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19515 /* This is where the prologue starts... */
19516 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19517 = func_brace_location
;
19523 else if (parser
->fully_implicit_function_template_p
)
19525 /* A non-template declaration involving a function parameter list
19526 containing an implicit template parameter will be made into a
19527 template. If the resulting declaration is not going to be an
19528 actual function then finish the template scope here to prevent it.
19529 An error message will be issued once we have a decl to talk about.
19531 FIXME probably we should do type deduction rather than create an
19532 implicit template, but the standard currently doesn't allow it. */
19533 bogus_implicit_tmpl
= true;
19534 finish_fully_implicit_template (parser
, NULL_TREE
);
19539 Only in function declarations for constructors, destructors, type
19540 conversions, and deduction guides can the decl-specifier-seq be omitted.
19542 We explicitly postpone this check past the point where we handle
19543 function-definitions because we tolerate function-definitions
19544 that are missing their return types in some modes. */
19545 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19547 cp_parser_error (parser
,
19548 "expected constructor, destructor, or type conversion");
19549 return error_mark_node
;
19552 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19553 if (token
->type
== CPP_EQ
19554 || token
->type
== CPP_OPEN_PAREN
19555 || token
->type
== CPP_OPEN_BRACE
)
19557 is_initialized
= SD_INITIALIZED
;
19558 initialization_kind
= token
->type
;
19559 if (maybe_range_for_decl
)
19560 *maybe_range_for_decl
= error_mark_node
;
19561 tmp_init_loc
= token
->location
;
19562 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19563 *init_loc
= tmp_init_loc
;
19565 if (token
->type
== CPP_EQ
19566 && function_declarator_p (declarator
))
19568 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19569 if (t2
->keyword
== RID_DEFAULT
)
19570 is_initialized
= SD_DEFAULTED
;
19571 else if (t2
->keyword
== RID_DELETE
)
19572 is_initialized
= SD_DELETED
;
19577 /* If the init-declarator isn't initialized and isn't followed by a
19578 `,' or `;', it's not a valid init-declarator. */
19579 if (token
->type
!= CPP_COMMA
19580 && token
->type
!= CPP_SEMICOLON
)
19582 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19583 range_for_decl_p
= true;
19586 if (!maybe_range_for_decl
)
19587 cp_parser_error (parser
, "expected initializer");
19588 return error_mark_node
;
19591 is_initialized
= SD_UNINITIALIZED
;
19592 initialization_kind
= CPP_EOF
;
19595 /* Because start_decl has side-effects, we should only call it if we
19596 know we're going ahead. By this point, we know that we cannot
19597 possibly be looking at any other construct. */
19598 cp_parser_commit_to_tentative_parse (parser
);
19600 /* Enter the newly declared entry in the symbol table. If we're
19601 processing a declaration in a class-specifier, we wait until
19602 after processing the initializer. */
19605 if (parser
->in_unbraced_linkage_specification_p
)
19606 decl_specifiers
->storage_class
= sc_extern
;
19607 decl
= start_decl (declarator
, decl_specifiers
,
19608 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19609 attributes
, prefix_attributes
, &pushed_scope
);
19610 cp_finalize_omp_declare_simd (parser
, decl
);
19611 cp_finalize_oacc_routine (parser
, decl
, false);
19612 /* Adjust location of decl if declarator->id_loc is more appropriate:
19613 set, and decl wasn't merged with another decl, in which case its
19614 location would be different from input_location, and more accurate. */
19616 && declarator
->id_loc
!= UNKNOWN_LOCATION
19617 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19618 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19621 /* Enter the SCOPE. That way unqualified names appearing in the
19622 initializer will be looked up in SCOPE. */
19623 pushed_scope
= push_scope (scope
);
19625 /* Perform deferred access control checks, now that we know in which
19626 SCOPE the declared entity resides. */
19627 if (!member_p
&& decl
)
19629 tree saved_current_function_decl
= NULL_TREE
;
19631 /* If the entity being declared is a function, pretend that we
19632 are in its scope. If it is a `friend', it may have access to
19633 things that would not otherwise be accessible. */
19634 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19636 saved_current_function_decl
= current_function_decl
;
19637 current_function_decl
= decl
;
19640 /* Perform access checks for template parameters. */
19641 cp_parser_perform_template_parameter_access_checks (checks
);
19643 /* Perform the access control checks for the declarator and the
19644 decl-specifiers. */
19645 perform_deferred_access_checks (tf_warning_or_error
);
19647 /* Restore the saved value. */
19648 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19649 current_function_decl
= saved_current_function_decl
;
19652 /* Parse the initializer. */
19653 initializer
= NULL_TREE
;
19654 is_direct_init
= false;
19655 is_non_constant_init
= true;
19656 if (is_initialized
)
19658 if (function_declarator_p (declarator
))
19660 if (initialization_kind
== CPP_EQ
)
19661 initializer
= cp_parser_pure_specifier (parser
);
19664 /* If the declaration was erroneous, we don't really
19665 know what the user intended, so just silently
19666 consume the initializer. */
19667 if (decl
!= error_mark_node
)
19668 error_at (tmp_init_loc
, "initializer provided for function");
19669 cp_parser_skip_to_closing_parenthesis (parser
,
19670 /*recovering=*/true,
19671 /*or_comma=*/false,
19672 /*consume_paren=*/true);
19677 /* We want to record the extra mangling scope for in-class
19678 initializers of class members and initializers of static data
19679 member templates. The former involves deferring
19680 parsing of the initializer until end of class as with default
19681 arguments. So right here we only handle the latter. */
19682 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19683 start_lambda_scope (decl
);
19684 initializer
= cp_parser_initializer (parser
,
19686 &is_non_constant_init
);
19687 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19688 finish_lambda_scope ();
19689 if (initializer
== error_mark_node
)
19690 cp_parser_skip_to_end_of_statement (parser
);
19694 /* The old parser allows attributes to appear after a parenthesized
19695 initializer. Mark Mitchell proposed removing this functionality
19696 on the GCC mailing lists on 2002-08-13. This parser accepts the
19697 attributes -- but ignores them. Made a permerror in GCC 8. */
19698 if (cp_parser_allow_gnu_extensions_p (parser
)
19699 && initialization_kind
== CPP_OPEN_PAREN
19700 && cp_parser_attributes_opt (parser
)
19701 && permerror (input_location
,
19702 "attributes after parenthesized initializer ignored"))
19705 if (flag_permissive
&& !hint
)
19708 inform (input_location
,
19709 "this flexibility is deprecated and will be removed");
19713 /* And now complain about a non-function implicit template. */
19714 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19715 error_at (DECL_SOURCE_LOCATION (decl
),
19716 "non-function %qD declared as implicit template", decl
);
19718 /* For an in-class declaration, use `grokfield' to create the
19724 pop_scope (pushed_scope
);
19725 pushed_scope
= NULL_TREE
;
19727 decl
= grokfield (declarator
, decl_specifiers
,
19728 initializer
, !is_non_constant_init
,
19729 /*asmspec=*/NULL_TREE
,
19730 attr_chainon (attributes
, prefix_attributes
));
19731 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19732 cp_parser_save_default_args (parser
, decl
);
19733 cp_finalize_omp_declare_simd (parser
, decl
);
19734 cp_finalize_oacc_routine (parser
, decl
, false);
19737 /* Finish processing the declaration. But, skip member
19739 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19741 cp_finish_decl (decl
,
19742 initializer
, !is_non_constant_init
,
19744 /* If the initializer is in parentheses, then this is
19745 a direct-initialization, which means that an
19746 `explicit' constructor is OK. Otherwise, an
19747 `explicit' constructor cannot be used. */
19748 ((is_direct_init
|| !is_initialized
)
19749 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19751 else if ((cxx_dialect
!= cxx98
) && friend_p
19752 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19753 /* Core issue #226 (C++0x only): A default template-argument
19754 shall not be specified in a friend class template
19756 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19757 /*is_partial=*/false, /*is_friend_decl=*/1);
19759 if (!friend_p
&& pushed_scope
)
19760 pop_scope (pushed_scope
);
19762 if (function_declarator_p (declarator
)
19763 && parser
->fully_implicit_function_template_p
)
19766 decl
= finish_fully_implicit_template (parser
, decl
);
19768 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19771 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19772 && type_uses_auto (decl_specifiers
->type
))
19773 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19778 /* Parse a declarator.
19782 ptr-operator declarator
19784 abstract-declarator:
19785 ptr-operator abstract-declarator [opt]
19786 direct-abstract-declarator
19791 attributes [opt] direct-declarator
19792 attributes [opt] ptr-operator declarator
19794 abstract-declarator:
19795 attributes [opt] ptr-operator abstract-declarator [opt]
19796 attributes [opt] direct-abstract-declarator
19798 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19799 detect constructors, destructors, deduction guides, or conversion operators.
19800 It is set to -1 if the declarator is a name, and +1 if it is a
19801 function. Otherwise it is set to zero. Usually you just want to
19802 test for >0, but internally the negative value is used.
19804 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19805 a decl-specifier-seq unless it declares a constructor, destructor,
19806 or conversion. It might seem that we could check this condition in
19807 semantic analysis, rather than parsing, but that makes it difficult
19808 to handle something like `f()'. We want to notice that there are
19809 no decl-specifiers, and therefore realize that this is an
19810 expression, not a declaration.)
19812 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19813 the declarator is a direct-declarator of the form "(...)".
19815 MEMBER_P is true iff this declarator is a member-declarator.
19817 FRIEND_P is true iff this declarator is a friend. */
19819 static cp_declarator
*
19820 cp_parser_declarator (cp_parser
* parser
,
19821 cp_parser_declarator_kind dcl_kind
,
19822 int* ctor_dtor_or_conv_p
,
19823 bool* parenthesized_p
,
19824 bool member_p
, bool friend_p
)
19826 cp_declarator
*declarator
;
19827 enum tree_code code
;
19828 cp_cv_quals cv_quals
;
19830 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19832 /* Assume this is not a constructor, destructor, or type-conversion
19834 if (ctor_dtor_or_conv_p
)
19835 *ctor_dtor_or_conv_p
= 0;
19837 if (cp_parser_allow_gnu_extensions_p (parser
))
19838 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19840 /* Check for the ptr-operator production. */
19841 cp_parser_parse_tentatively (parser
);
19842 /* Parse the ptr-operator. */
19843 code
= cp_parser_ptr_operator (parser
,
19848 /* If that worked, then we have a ptr-operator. */
19849 if (cp_parser_parse_definitely (parser
))
19851 /* If a ptr-operator was found, then this declarator was not
19853 if (parenthesized_p
)
19854 *parenthesized_p
= true;
19855 /* The dependent declarator is optional if we are parsing an
19856 abstract-declarator. */
19857 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19858 cp_parser_parse_tentatively (parser
);
19860 /* Parse the dependent declarator. */
19861 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19862 /*ctor_dtor_or_conv_p=*/NULL
,
19863 /*parenthesized_p=*/NULL
,
19864 /*member_p=*/false,
19867 /* If we are parsing an abstract-declarator, we must handle the
19868 case where the dependent declarator is absent. */
19869 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19870 && !cp_parser_parse_definitely (parser
))
19873 declarator
= cp_parser_make_indirect_declarator
19874 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19876 /* Everything else is a direct-declarator. */
19879 if (parenthesized_p
)
19880 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19882 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19883 ctor_dtor_or_conv_p
,
19884 member_p
, friend_p
);
19887 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19888 declarator
->attributes
= gnu_attributes
;
19892 /* Parse a direct-declarator or direct-abstract-declarator.
19896 direct-declarator ( parameter-declaration-clause )
19897 cv-qualifier-seq [opt]
19898 ref-qualifier [opt]
19899 exception-specification [opt]
19900 direct-declarator [ constant-expression [opt] ]
19903 direct-abstract-declarator:
19904 direct-abstract-declarator [opt]
19905 ( parameter-declaration-clause )
19906 cv-qualifier-seq [opt]
19907 ref-qualifier [opt]
19908 exception-specification [opt]
19909 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19910 ( abstract-declarator )
19912 Returns a representation of the declarator. DCL_KIND is
19913 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19914 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19915 we are parsing a direct-declarator. It is
19916 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19917 of ambiguity we prefer an abstract declarator, as per
19918 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19919 as for cp_parser_declarator. */
19921 static cp_declarator
*
19922 cp_parser_direct_declarator (cp_parser
* parser
,
19923 cp_parser_declarator_kind dcl_kind
,
19924 int* ctor_dtor_or_conv_p
,
19925 bool member_p
, bool friend_p
)
19928 cp_declarator
*declarator
= NULL
;
19929 tree scope
= NULL_TREE
;
19930 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19931 bool saved_in_declarator_p
= parser
->in_declarator_p
;
19933 tree pushed_scope
= NULL_TREE
;
19934 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
19938 /* Peek at the next token. */
19939 token
= cp_lexer_peek_token (parser
->lexer
);
19940 if (token
->type
== CPP_OPEN_PAREN
)
19942 /* This is either a parameter-declaration-clause, or a
19943 parenthesized declarator. When we know we are parsing a
19944 named declarator, it must be a parenthesized declarator
19945 if FIRST is true. For instance, `(int)' is a
19946 parameter-declaration-clause, with an omitted
19947 direct-abstract-declarator. But `((*))', is a
19948 parenthesized abstract declarator. Finally, when T is a
19949 template parameter `(T)' is a
19950 parameter-declaration-clause, and not a parenthesized
19953 We first try and parse a parameter-declaration-clause,
19954 and then try a nested declarator (if FIRST is true).
19956 It is not an error for it not to be a
19957 parameter-declaration-clause, even when FIRST is
19963 The first is the declaration of a function while the
19964 second is the definition of a variable, including its
19967 Having seen only the parenthesis, we cannot know which of
19968 these two alternatives should be selected. Even more
19969 complex are examples like:
19974 The former is a function-declaration; the latter is a
19975 variable initialization.
19977 Thus again, we try a parameter-declaration-clause, and if
19978 that fails, we back out and return. */
19980 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19983 bool is_declarator
= false;
19987 /* In a member-declarator, the only valid interpretation
19988 of a parenthesis is the start of a
19989 parameter-declaration-clause. (It is invalid to
19990 initialize a static data member with a parenthesized
19991 initializer; only the "=" form of initialization is
19994 cp_parser_parse_tentatively (parser
);
19996 /* Consume the `('. */
19997 matching_parens parens
;
19998 parens
.consume_open (parser
);
20001 /* If this is going to be an abstract declarator, we're
20002 in a declarator and we can't have default args. */
20003 parser
->default_arg_ok_p
= false;
20004 parser
->in_declarator_p
= true;
20007 begin_scope (sk_function_parms
, NULL_TREE
);
20009 /* Parse the parameter-declaration-clause. */
20010 params
= cp_parser_parameter_declaration_clause (parser
);
20012 /* Consume the `)'. */
20013 parens
.require_close (parser
);
20015 /* If all went well, parse the cv-qualifier-seq,
20016 ref-qualifier and the exception-specification. */
20017 if (member_p
|| cp_parser_parse_definitely (parser
))
20019 cp_cv_quals cv_quals
;
20020 cp_virt_specifiers virt_specifiers
;
20021 cp_ref_qualifier ref_qual
;
20022 tree exception_specification
;
20025 bool memfn
= (member_p
|| (pushed_scope
20026 && CLASS_TYPE_P (pushed_scope
)));
20028 is_declarator
= true;
20030 if (ctor_dtor_or_conv_p
)
20031 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20034 /* Parse the cv-qualifier-seq. */
20035 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20036 /* Parse the ref-qualifier. */
20037 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20038 /* Parse the tx-qualifier. */
20039 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20040 /* And the exception-specification. */
20041 exception_specification
20042 = cp_parser_exception_specification_opt (parser
);
20044 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20046 /* In here, we handle cases where attribute is used after
20047 the function declaration. For example:
20048 void func (int x) __attribute__((vector(..))); */
20049 tree gnu_attrs
= NULL_TREE
;
20050 tree requires_clause
= NULL_TREE
;
20051 late_return
= (cp_parser_late_return_type_opt
20052 (parser
, declarator
, requires_clause
,
20053 memfn
? cv_quals
: -1));
20055 /* Parse the virt-specifier-seq. */
20056 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20058 /* Create the function-declarator. */
20059 declarator
= make_call_declarator (declarator
,
20065 exception_specification
,
20068 declarator
->std_attributes
= attrs
;
20069 declarator
->attributes
= gnu_attrs
;
20070 /* Any subsequent parameter lists are to do with
20071 return type, so are not those of the declared
20073 parser
->default_arg_ok_p
= false;
20076 /* Remove the function parms from scope. */
20077 pop_bindings_and_leave_scope ();
20080 /* Repeat the main loop. */
20084 /* If this is the first, we can try a parenthesized
20088 bool saved_in_type_id_in_expr_p
;
20090 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20091 parser
->in_declarator_p
= saved_in_declarator_p
;
20093 open_paren
= token
;
20094 /* Consume the `('. */
20095 matching_parens parens
;
20096 parens
.consume_open (parser
);
20097 /* Parse the nested declarator. */
20098 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20099 parser
->in_type_id_in_expr_p
= true;
20101 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20102 /*parenthesized_p=*/NULL
,
20103 member_p
, friend_p
);
20104 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20106 /* Expect a `)'. */
20107 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20108 if (!parens
.require_close (parser
))
20109 declarator
= cp_error_declarator
;
20110 if (declarator
== cp_error_declarator
)
20113 goto handle_declarator
;
20115 /* Otherwise, we must be done. */
20119 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20120 && token
->type
== CPP_OPEN_SQUARE
20121 && !cp_next_tokens_can_be_attribute_p (parser
))
20123 /* Parse an array-declarator. */
20124 tree bounds
, attrs
;
20126 if (ctor_dtor_or_conv_p
)
20127 *ctor_dtor_or_conv_p
= 0;
20131 parser
->default_arg_ok_p
= false;
20132 parser
->in_declarator_p
= true;
20133 /* Consume the `['. */
20134 cp_lexer_consume_token (parser
->lexer
);
20135 /* Peek at the next token. */
20136 token
= cp_lexer_peek_token (parser
->lexer
);
20137 /* If the next token is `]', then there is no
20138 constant-expression. */
20139 if (token
->type
!= CPP_CLOSE_SQUARE
)
20141 bool non_constant_p
;
20143 = cp_parser_constant_expression (parser
,
20144 /*allow_non_constant=*/true,
20146 if (!non_constant_p
)
20148 else if (error_operand_p (bounds
))
20149 /* Already gave an error. */;
20150 else if (!parser
->in_function_body
20151 || current_binding_level
->kind
== sk_function_parms
)
20153 /* Normally, the array bound must be an integral constant
20154 expression. However, as an extension, we allow VLAs
20155 in function scopes as long as they aren't part of a
20156 parameter declaration. */
20157 cp_parser_error (parser
,
20158 "array bound is not an integer constant");
20159 bounds
= error_mark_node
;
20161 else if (processing_template_decl
20162 && !type_dependent_expression_p (bounds
))
20164 /* Remember this wasn't a constant-expression. */
20165 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20166 TREE_SIDE_EFFECTS (bounds
) = 1;
20170 bounds
= NULL_TREE
;
20171 /* Look for the closing `]'. */
20172 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20174 declarator
= cp_error_declarator
;
20178 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20179 declarator
= make_array_declarator (declarator
, bounds
);
20180 declarator
->std_attributes
= attrs
;
20182 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20185 tree qualifying_scope
;
20186 tree unqualified_name
;
20188 special_function_kind sfk
;
20190 bool pack_expansion_p
= false;
20191 cp_token
*declarator_id_start_token
;
20193 /* Parse a declarator-id */
20194 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20197 cp_parser_parse_tentatively (parser
);
20199 /* If we see an ellipsis, we should be looking at a
20201 if (token
->type
== CPP_ELLIPSIS
)
20203 /* Consume the `...' */
20204 cp_lexer_consume_token (parser
->lexer
);
20206 pack_expansion_p
= true;
20210 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20212 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20213 qualifying_scope
= parser
->scope
;
20218 if (!unqualified_name
&& pack_expansion_p
)
20220 /* Check whether an error occurred. */
20221 okay
= !cp_parser_error_occurred (parser
);
20223 /* We already consumed the ellipsis to mark a
20224 parameter pack, but we have no way to report it,
20225 so abort the tentative parse. We will be exiting
20226 immediately anyway. */
20227 cp_parser_abort_tentative_parse (parser
);
20230 okay
= cp_parser_parse_definitely (parser
);
20233 unqualified_name
= error_mark_node
;
20234 else if (unqualified_name
20235 && (qualifying_scope
20236 || (!identifier_p (unqualified_name
))))
20238 cp_parser_error (parser
, "expected unqualified-id");
20239 unqualified_name
= error_mark_node
;
20243 if (!unqualified_name
)
20245 if (unqualified_name
== error_mark_node
)
20247 declarator
= cp_error_declarator
;
20248 pack_expansion_p
= false;
20249 declarator
->parameter_pack_p
= false;
20253 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20255 if (qualifying_scope
&& at_namespace_scope_p ()
20256 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20258 /* In the declaration of a member of a template class
20259 outside of the class itself, the SCOPE will sometimes
20260 be a TYPENAME_TYPE. For example, given:
20262 template <typename T>
20263 int S<T>::R::i = 3;
20265 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20266 this context, we must resolve S<T>::R to an ordinary
20267 type, rather than a typename type.
20269 The reason we normally avoid resolving TYPENAME_TYPEs
20270 is that a specialization of `S' might render
20271 `S<T>::R' not a type. However, if `S' is
20272 specialized, then this `i' will not be used, so there
20273 is no harm in resolving the types here. */
20276 /* Resolve the TYPENAME_TYPE. */
20277 type
= resolve_typename_type (qualifying_scope
,
20278 /*only_current_p=*/false);
20279 /* If that failed, the declarator is invalid. */
20280 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20282 if (typedef_variant_p (type
))
20283 error_at (declarator_id_start_token
->location
,
20284 "cannot define member of dependent typedef "
20287 error_at (declarator_id_start_token
->location
,
20288 "%<%T::%E%> is not a type",
20289 TYPE_CONTEXT (qualifying_scope
),
20290 TYPE_IDENTIFIER (qualifying_scope
));
20292 qualifying_scope
= type
;
20297 if (unqualified_name
)
20301 if (qualifying_scope
20302 && CLASS_TYPE_P (qualifying_scope
))
20303 class_type
= qualifying_scope
;
20305 class_type
= current_class_type
;
20307 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20309 tree name_type
= TREE_TYPE (unqualified_name
);
20311 if (!class_type
|| !same_type_p (name_type
, class_type
))
20313 /* We do not attempt to print the declarator
20314 here because we do not have enough
20315 information about its original syntactic
20317 cp_parser_error (parser
, "invalid declarator");
20318 declarator
= cp_error_declarator
;
20321 else if (qualifying_scope
20322 && CLASSTYPE_USE_TEMPLATE (name_type
))
20324 error_at (declarator_id_start_token
->location
,
20325 "invalid use of constructor as a template");
20326 inform (declarator_id_start_token
->location
,
20327 "use %<%T::%D%> instead of %<%T::%D%> to "
20328 "name the constructor in a qualified name",
20330 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20331 class_type
, name_type
);
20332 declarator
= cp_error_declarator
;
20335 unqualified_name
= constructor_name (class_type
);
20340 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20341 sfk
= sfk_destructor
;
20342 else if (identifier_p (unqualified_name
)
20343 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20344 sfk
= sfk_conversion
;
20345 else if (/* There's no way to declare a constructor
20346 for an unnamed type, even if the type
20347 got a name for linkage purposes. */
20348 !TYPE_WAS_UNNAMED (class_type
)
20349 /* Handle correctly (c++/19200):
20363 friend void N::S();
20365 && (!friend_p
|| class_type
== qualifying_scope
)
20366 && constructor_name_p (unqualified_name
,
20368 sfk
= sfk_constructor
;
20369 else if (is_overloaded_fn (unqualified_name
)
20370 && DECL_CONSTRUCTOR_P (get_first_fn
20371 (unqualified_name
)))
20372 sfk
= sfk_constructor
;
20374 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20375 *ctor_dtor_or_conv_p
= -1;
20378 declarator
= make_id_declarator (qualifying_scope
,
20381 declarator
->std_attributes
= attrs
;
20382 declarator
->id_loc
= token
->location
;
20383 declarator
->parameter_pack_p
= pack_expansion_p
;
20385 if (pack_expansion_p
)
20386 maybe_warn_variadic_templates ();
20389 handle_declarator
:;
20390 scope
= get_scope_of_declarator (declarator
);
20393 /* Any names that appear after the declarator-id for a
20394 member are looked up in the containing scope. */
20395 if (at_function_scope_p ())
20397 /* But declarations with qualified-ids can't appear in a
20399 cp_parser_error (parser
, "qualified-id in declaration");
20400 declarator
= cp_error_declarator
;
20403 pushed_scope
= push_scope (scope
);
20405 parser
->in_declarator_p
= true;
20406 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20407 || (declarator
&& declarator
->kind
== cdk_id
))
20408 /* Default args are only allowed on function
20410 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20412 parser
->default_arg_ok_p
= false;
20421 /* For an abstract declarator, we might wind up with nothing at this
20422 point. That's an error; the declarator is not optional. */
20424 cp_parser_error (parser
, "expected declarator");
20425 else if (open_paren
)
20427 /* Record overly parenthesized declarator so we can give a
20428 diagnostic about confusing decl/expr disambiguation. */
20429 if (declarator
->kind
== cdk_array
)
20431 /* If the open and close parens are on different lines, this
20432 is probably a formatting thing, so ignore. */
20433 expanded_location open
= expand_location (open_paren
->location
);
20434 expanded_location close
= expand_location (close_paren
->location
);
20435 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20439 declarator
->parenthesized
= open_paren
->location
;
20442 /* If we entered a scope, we must exit it now. */
20444 pop_scope (pushed_scope
);
20446 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20447 parser
->in_declarator_p
= saved_in_declarator_p
;
20452 /* Parse a ptr-operator.
20455 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20456 * cv-qualifier-seq [opt]
20458 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20459 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20464 & cv-qualifier-seq [opt]
20466 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20467 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20468 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20469 filled in with the TYPE containing the member. *CV_QUALS is
20470 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20471 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20472 Note that the tree codes returned by this function have nothing
20473 to do with the types of trees that will be eventually be created
20474 to represent the pointer or reference type being parsed. They are
20475 just constants with suggestive names. */
20476 static enum tree_code
20477 cp_parser_ptr_operator (cp_parser
* parser
,
20479 cp_cv_quals
*cv_quals
,
20482 enum tree_code code
= ERROR_MARK
;
20484 tree attrs
= NULL_TREE
;
20486 /* Assume that it's not a pointer-to-member. */
20488 /* And that there are no cv-qualifiers. */
20489 *cv_quals
= TYPE_UNQUALIFIED
;
20491 /* Peek at the next token. */
20492 token
= cp_lexer_peek_token (parser
->lexer
);
20494 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20495 if (token
->type
== CPP_MULT
)
20496 code
= INDIRECT_REF
;
20497 else if (token
->type
== CPP_AND
)
20499 else if ((cxx_dialect
!= cxx98
) &&
20500 token
->type
== CPP_AND_AND
) /* C++0x only */
20501 code
= NON_LVALUE_EXPR
;
20503 if (code
!= ERROR_MARK
)
20505 /* Consume the `*', `&' or `&&'. */
20506 cp_lexer_consume_token (parser
->lexer
);
20508 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20509 `&', if we are allowing GNU extensions. (The only qualifier
20510 that can legally appear after `&' is `restrict', but that is
20511 enforced during semantic analysis. */
20512 if (code
== INDIRECT_REF
20513 || cp_parser_allow_gnu_extensions_p (parser
))
20514 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20516 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20517 if (attributes
!= NULL
)
20518 *attributes
= attrs
;
20522 /* Try the pointer-to-member case. */
20523 cp_parser_parse_tentatively (parser
);
20524 /* Look for the optional `::' operator. */
20525 cp_parser_global_scope_opt (parser
,
20526 /*current_scope_valid_p=*/false);
20527 /* Look for the nested-name specifier. */
20528 token
= cp_lexer_peek_token (parser
->lexer
);
20529 cp_parser_nested_name_specifier (parser
,
20530 /*typename_keyword_p=*/false,
20531 /*check_dependency_p=*/true,
20533 /*is_declaration=*/false);
20534 /* If we found it, and the next token is a `*', then we are
20535 indeed looking at a pointer-to-member operator. */
20536 if (!cp_parser_error_occurred (parser
)
20537 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20539 /* Indicate that the `*' operator was used. */
20540 code
= INDIRECT_REF
;
20542 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20543 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20544 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20545 error_at (token
->location
, "cannot form pointer to member of "
20546 "non-class %q#T", parser
->scope
);
20549 /* The type of which the member is a member is given by the
20551 *type
= parser
->scope
;
20552 /* The next name will not be qualified. */
20553 parser
->scope
= NULL_TREE
;
20554 parser
->qualifying_scope
= NULL_TREE
;
20555 parser
->object_scope
= NULL_TREE
;
20556 /* Look for optional c++11 attributes. */
20557 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20558 if (attributes
!= NULL
)
20559 *attributes
= attrs
;
20560 /* Look for the optional cv-qualifier-seq. */
20561 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20564 /* If that didn't work we don't have a ptr-operator. */
20565 if (!cp_parser_parse_definitely (parser
))
20566 cp_parser_error (parser
, "expected ptr-operator");
20572 /* Parse an (optional) cv-qualifier-seq.
20575 cv-qualifier cv-qualifier-seq [opt]
20586 Returns a bitmask representing the cv-qualifiers. */
20589 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20591 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20596 cp_cv_quals cv_qualifier
;
20598 /* Peek at the next token. */
20599 token
= cp_lexer_peek_token (parser
->lexer
);
20600 /* See if it's a cv-qualifier. */
20601 switch (token
->keyword
)
20604 cv_qualifier
= TYPE_QUAL_CONST
;
20608 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20612 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20616 cv_qualifier
= TYPE_UNQUALIFIED
;
20623 if (cv_quals
& cv_qualifier
)
20625 gcc_rich_location
richloc (token
->location
);
20626 richloc
.add_fixit_remove ();
20627 error_at (&richloc
, "duplicate cv-qualifier");
20628 cp_lexer_purge_token (parser
->lexer
);
20632 cp_lexer_consume_token (parser
->lexer
);
20633 cv_quals
|= cv_qualifier
;
20640 /* Parse an (optional) ref-qualifier
20646 Returns cp_ref_qualifier representing ref-qualifier. */
20648 static cp_ref_qualifier
20649 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20651 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20653 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20654 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20659 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20660 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20662 switch (token
->type
)
20665 curr_ref_qual
= REF_QUAL_LVALUE
;
20669 curr_ref_qual
= REF_QUAL_RVALUE
;
20673 curr_ref_qual
= REF_QUAL_NONE
;
20677 if (!curr_ref_qual
)
20681 error_at (token
->location
, "multiple ref-qualifiers");
20682 cp_lexer_purge_token (parser
->lexer
);
20686 ref_qual
= curr_ref_qual
;
20687 cp_lexer_consume_token (parser
->lexer
);
20694 /* Parse an optional tx-qualifier.
20698 transaction_safe_dynamic */
20701 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20703 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20704 if (token
->type
== CPP_NAME
)
20706 tree name
= token
->u
.value
;
20707 const char *p
= IDENTIFIER_POINTER (name
);
20708 const int len
= strlen ("transaction_safe");
20709 if (!strncmp (p
, "transaction_safe", len
))
20713 || !strcmp (p
, "_dynamic"))
20715 cp_lexer_consume_token (parser
->lexer
);
20718 error ("%qE requires %<-fgnu-tm%>", name
);
20729 /* Parse an (optional) virt-specifier-seq.
20731 virt-specifier-seq:
20732 virt-specifier virt-specifier-seq [opt]
20738 Returns a bitmask representing the virt-specifiers. */
20740 static cp_virt_specifiers
20741 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20743 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20748 cp_virt_specifiers virt_specifier
;
20750 /* Peek at the next token. */
20751 token
= cp_lexer_peek_token (parser
->lexer
);
20752 /* See if it's a virt-specifier-qualifier. */
20753 if (token
->type
!= CPP_NAME
)
20755 if (id_equal (token
->u
.value
, "override"))
20757 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20758 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20760 else if (id_equal (token
->u
.value
, "final"))
20762 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20763 virt_specifier
= VIRT_SPEC_FINAL
;
20765 else if (id_equal (token
->u
.value
, "__final"))
20767 virt_specifier
= VIRT_SPEC_FINAL
;
20772 if (virt_specifiers
& virt_specifier
)
20774 gcc_rich_location
richloc (token
->location
);
20775 richloc
.add_fixit_remove ();
20776 error_at (&richloc
, "duplicate virt-specifier");
20777 cp_lexer_purge_token (parser
->lexer
);
20781 cp_lexer_consume_token (parser
->lexer
);
20782 virt_specifiers
|= virt_specifier
;
20785 return virt_specifiers
;
20788 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20789 is in scope even though it isn't real. */
20792 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20796 if (current_class_ptr
)
20798 /* We don't clear this between NSDMIs. Is it already what we want? */
20799 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20800 if (DECL_P (current_class_ptr
)
20801 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20802 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20803 && cp_type_quals (type
) == quals
)
20807 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20808 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20809 current_class_ptr
= NULL_TREE
;
20811 = cp_build_fold_indirect_ref (this_parm
);
20812 current_class_ptr
= this_parm
;
20815 /* Return true iff our current scope is a non-static data member
20819 parsing_nsdmi (void)
20821 /* We recognize NSDMI context by the context-less 'this' pointer set up
20822 by the function above. */
20823 if (current_class_ptr
20824 && TREE_CODE (current_class_ptr
) == PARM_DECL
20825 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20830 /* Parse a late-specified return type, if any. This is not a separate
20831 non-terminal, but part of a function declarator, which looks like
20833 -> trailing-type-specifier-seq abstract-declarator(opt)
20835 Returns the type indicated by the type-id.
20837 In addition to this, parse any queued up #pragma omp declare simd
20838 clauses, and #pragma acc routine clauses.
20840 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20844 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20845 tree
& requires_clause
, cp_cv_quals quals
)
20848 tree type
= NULL_TREE
;
20849 bool declare_simd_p
= (parser
->omp_declare_simd
20851 && declarator
->kind
== cdk_id
);
20853 bool oacc_routine_p
= (parser
->oacc_routine
20855 && declarator
->kind
== cdk_id
);
20857 /* Peek at the next token. */
20858 token
= cp_lexer_peek_token (parser
->lexer
);
20859 /* A late-specified return type is indicated by an initial '->'. */
20860 if (token
->type
!= CPP_DEREF
20861 && token
->keyword
!= RID_REQUIRES
20862 && !(token
->type
== CPP_NAME
20863 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20864 && !(declare_simd_p
|| oacc_routine_p
))
20867 tree save_ccp
= current_class_ptr
;
20868 tree save_ccr
= current_class_ref
;
20871 /* DR 1207: 'this' is in scope in the trailing return type. */
20872 inject_this_parameter (current_class_type
, quals
);
20875 if (token
->type
== CPP_DEREF
)
20877 /* Consume the ->. */
20878 cp_lexer_consume_token (parser
->lexer
);
20880 type
= cp_parser_trailing_type_id (parser
);
20883 /* Function declarations may be followed by a trailing
20884 requires-clause. */
20885 requires_clause
= cp_parser_requires_clause_opt (parser
);
20887 if (declare_simd_p
)
20888 declarator
->attributes
20889 = cp_parser_late_parsing_omp_declare_simd (parser
,
20890 declarator
->attributes
);
20891 if (oacc_routine_p
)
20892 declarator
->attributes
20893 = cp_parser_late_parsing_oacc_routine (parser
,
20894 declarator
->attributes
);
20898 current_class_ptr
= save_ccp
;
20899 current_class_ref
= save_ccr
;
20905 /* Parse a declarator-id.
20909 :: [opt] nested-name-specifier [opt] type-name
20911 In the `id-expression' case, the value returned is as for
20912 cp_parser_id_expression if the id-expression was an unqualified-id.
20913 If the id-expression was a qualified-id, then a SCOPE_REF is
20914 returned. The first operand is the scope (either a NAMESPACE_DECL
20915 or TREE_TYPE), but the second is still just a representation of an
20919 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
20922 /* The expression must be an id-expression. Assume that qualified
20923 names are the names of types so that:
20926 int S<T>::R::i = 3;
20928 will work; we must treat `S<T>::R' as the name of a type.
20929 Similarly, assume that qualified names are templates, where
20933 int S<T>::R<T>::i = 3;
20936 id
= cp_parser_id_expression (parser
,
20937 /*template_keyword_p=*/false,
20938 /*check_dependency_p=*/false,
20939 /*template_p=*/NULL
,
20940 /*declarator_p=*/true,
20942 if (id
&& BASELINK_P (id
))
20943 id
= BASELINK_FUNCTIONS (id
);
20947 /* Parse a type-id.
20950 type-specifier-seq abstract-declarator [opt]
20952 Returns the TYPE specified. */
20955 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
20956 bool is_trailing_return
)
20958 cp_decl_specifier_seq type_specifier_seq
;
20959 cp_declarator
*abstract_declarator
;
20961 /* Parse the type-specifier-seq. */
20962 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
20963 is_trailing_return
,
20964 &type_specifier_seq
);
20965 if (is_template_arg
&& type_specifier_seq
.type
20966 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
20967 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
20968 /* A bare template name as a template argument is a template template
20969 argument, not a placeholder, so fail parsing it as a type argument. */
20971 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
20972 cp_parser_simulate_error (parser
);
20973 return error_mark_node
;
20975 if (type_specifier_seq
.type
== error_mark_node
)
20976 return error_mark_node
;
20978 /* There might or might not be an abstract declarator. */
20979 cp_parser_parse_tentatively (parser
);
20980 /* Look for the declarator. */
20981 abstract_declarator
20982 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
20983 /*parenthesized_p=*/NULL
,
20984 /*member_p=*/false,
20985 /*friend_p=*/false);
20986 /* Check to see if there really was a declarator. */
20987 if (!cp_parser_parse_definitely (parser
))
20988 abstract_declarator
= NULL
;
20990 if (type_specifier_seq
.type
20991 /* The concepts TS allows 'auto' as a type-id. */
20992 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
20993 /* None of the valid uses of 'auto' in C++14 involve the type-id
20994 nonterminal, but it is valid in a trailing-return-type. */
20995 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
20996 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
20998 /* A type-id with type 'auto' is only ok if the abstract declarator
20999 is a function declarator with a late-specified return type.
21001 A type-id with 'auto' is also valid in a trailing-return-type
21002 in a compound-requirement. */
21003 if (abstract_declarator
21004 && abstract_declarator
->kind
== cdk_function
21005 && abstract_declarator
->u
.function
.late_return_type
)
21007 else if (parser
->in_result_type_constraint_p
)
21011 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21012 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21014 error_at (loc
, "missing template arguments after %qT",
21016 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21020 error_at (loc
, "invalid use of %qT", auto_node
);
21021 return error_mark_node
;
21025 return groktypename (&type_specifier_seq
, abstract_declarator
,
21030 cp_parser_type_id (cp_parser
*parser
)
21032 return cp_parser_type_id_1 (parser
, false, false);
21036 cp_parser_template_type_arg (cp_parser
*parser
)
21039 const char *saved_message
= parser
->type_definition_forbidden_message
;
21040 parser
->type_definition_forbidden_message
21041 = G_("types may not be defined in template arguments");
21042 r
= cp_parser_type_id_1 (parser
, true, false);
21043 parser
->type_definition_forbidden_message
= saved_message
;
21044 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21046 error ("invalid use of %<auto%> in template argument");
21047 r
= error_mark_node
;
21053 cp_parser_trailing_type_id (cp_parser
*parser
)
21055 return cp_parser_type_id_1 (parser
, false, true);
21058 /* Parse a type-specifier-seq.
21060 type-specifier-seq:
21061 type-specifier type-specifier-seq [opt]
21065 type-specifier-seq:
21066 attributes type-specifier-seq [opt]
21068 If IS_DECLARATION is true, we are at the start of a "condition" or
21069 exception-declaration, so we might be followed by a declarator-id.
21071 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21072 i.e. we've just seen "->".
21074 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21077 cp_parser_type_specifier_seq (cp_parser
* parser
,
21078 bool is_declaration
,
21079 bool is_trailing_return
,
21080 cp_decl_specifier_seq
*type_specifier_seq
)
21082 bool seen_type_specifier
= false;
21083 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21084 cp_token
*start_token
= NULL
;
21086 /* Clear the TYPE_SPECIFIER_SEQ. */
21087 clear_decl_specs (type_specifier_seq
);
21089 /* In the context of a trailing return type, enum E { } is an
21090 elaborated-type-specifier followed by a function-body, not an
21092 if (is_trailing_return
)
21093 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21095 /* Parse the type-specifiers and attributes. */
21098 tree type_specifier
;
21099 bool is_cv_qualifier
;
21101 /* Check for attributes first. */
21102 if (cp_next_tokens_can_be_attribute_p (parser
))
21104 type_specifier_seq
->attributes
21105 = attr_chainon (type_specifier_seq
->attributes
,
21106 cp_parser_attributes_opt (parser
));
21110 /* record the token of the beginning of the type specifier seq,
21111 for error reporting purposes*/
21113 start_token
= cp_lexer_peek_token (parser
->lexer
);
21115 /* Look for the type-specifier. */
21116 type_specifier
= cp_parser_type_specifier (parser
,
21118 type_specifier_seq
,
21119 /*is_declaration=*/false,
21122 if (!type_specifier
)
21124 /* If the first type-specifier could not be found, this is not a
21125 type-specifier-seq at all. */
21126 if (!seen_type_specifier
)
21128 /* Set in_declarator_p to avoid skipping to the semicolon. */
21129 int in_decl
= parser
->in_declarator_p
;
21130 parser
->in_declarator_p
= true;
21132 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21133 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21134 cp_parser_error (parser
, "expected type-specifier");
21136 parser
->in_declarator_p
= in_decl
;
21138 type_specifier_seq
->type
= error_mark_node
;
21141 /* If subsequent type-specifiers could not be found, the
21142 type-specifier-seq is complete. */
21146 seen_type_specifier
= true;
21147 /* The standard says that a condition can be:
21149 type-specifier-seq declarator = assignment-expression
21156 we should treat the "S" as a declarator, not as a
21157 type-specifier. The standard doesn't say that explicitly for
21158 type-specifier-seq, but it does say that for
21159 decl-specifier-seq in an ordinary declaration. Perhaps it
21160 would be clearer just to allow a decl-specifier-seq here, and
21161 then add a semantic restriction that if any decl-specifiers
21162 that are not type-specifiers appear, the program is invalid. */
21163 if (is_declaration
&& !is_cv_qualifier
)
21164 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21168 /* Return whether the function currently being declared has an associated
21169 template parameter list. */
21172 function_being_declared_is_template_p (cp_parser
* parser
)
21174 if (!current_template_parms
|| processing_template_parmlist
)
21177 if (parser
->implicit_template_scope
)
21180 if (at_class_scope_p ()
21181 && TYPE_BEING_DEFINED (current_class_type
))
21182 return parser
->num_template_parameter_lists
!= 0;
21184 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21185 (current_class_type
));
21188 /* Parse a parameter-declaration-clause.
21190 parameter-declaration-clause:
21191 parameter-declaration-list [opt] ... [opt]
21192 parameter-declaration-list , ...
21194 Returns a representation for the parameter declarations. A return
21195 value of NULL indicates a parameter-declaration-clause consisting
21196 only of an ellipsis. */
21199 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21206 temp_override
<bool> cleanup
21207 (parser
->auto_is_implicit_function_template_parm_p
);
21209 if (!processing_specialization
21210 && !processing_template_parmlist
21211 && !processing_explicit_instantiation
21212 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21213 actual function or a random abstract declarator. */
21214 && parser
->default_arg_ok_p
)
21215 if (!current_function_decl
21216 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21217 parser
->auto_is_implicit_function_template_parm_p
= true;
21219 /* Peek at the next token. */
21220 token
= cp_lexer_peek_token (parser
->lexer
);
21221 /* Check for trivial parameter-declaration-clauses. */
21222 if (token
->type
== CPP_ELLIPSIS
)
21224 /* Consume the `...' token. */
21225 cp_lexer_consume_token (parser
->lexer
);
21228 else if (token
->type
== CPP_CLOSE_PAREN
)
21229 /* There are no parameters. */
21231 #ifndef NO_IMPLICIT_EXTERN_C
21232 if (in_system_header_at (input_location
)
21233 && current_class_type
== NULL
21234 && current_lang_name
== lang_name_c
)
21238 return void_list_node
;
21240 /* Check for `(void)', too, which is a special case. */
21241 else if (token
->keyword
== RID_VOID
21242 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21243 == CPP_CLOSE_PAREN
))
21245 /* Consume the `void' token. */
21246 cp_lexer_consume_token (parser
->lexer
);
21247 /* There are no parameters. */
21248 return void_list_node
;
21251 /* Parse the parameter-declaration-list. */
21252 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
21253 /* If a parse error occurred while parsing the
21254 parameter-declaration-list, then the entire
21255 parameter-declaration-clause is erroneous. */
21259 /* Peek at the next token. */
21260 token
= cp_lexer_peek_token (parser
->lexer
);
21261 /* If it's a `,', the clause should terminate with an ellipsis. */
21262 if (token
->type
== CPP_COMMA
)
21264 /* Consume the `,'. */
21265 cp_lexer_consume_token (parser
->lexer
);
21266 /* Expect an ellipsis. */
21268 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21270 /* It might also be `...' if the optional trailing `,' was
21272 else if (token
->type
== CPP_ELLIPSIS
)
21274 /* Consume the `...' token. */
21275 cp_lexer_consume_token (parser
->lexer
);
21276 /* And remember that we saw it. */
21280 ellipsis_p
= false;
21282 /* Finish the parameter list. */
21284 parameters
= chainon (parameters
, void_list_node
);
21289 /* Parse a parameter-declaration-list.
21291 parameter-declaration-list:
21292 parameter-declaration
21293 parameter-declaration-list , parameter-declaration
21295 Returns a representation of the parameter-declaration-list, as for
21296 cp_parser_parameter_declaration_clause. However, the
21297 `void_list_node' is never appended to the list. Upon return,
21298 *IS_ERROR will be true iff an error occurred. */
21301 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
21303 tree parameters
= NULL_TREE
;
21304 tree
*tail
= ¶meters
;
21305 bool saved_in_unbraced_linkage_specification_p
;
21308 /* Assume all will go well. */
21310 /* The special considerations that apply to a function within an
21311 unbraced linkage specifications do not apply to the parameters
21312 to the function. */
21313 saved_in_unbraced_linkage_specification_p
21314 = parser
->in_unbraced_linkage_specification_p
;
21315 parser
->in_unbraced_linkage_specification_p
= false;
21317 /* Look for more parameters. */
21320 cp_parameter_declarator
*parameter
;
21321 tree decl
= error_mark_node
;
21322 bool parenthesized_p
= false;
21323 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
21324 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21325 (current_template_parms
)) : 0);
21327 /* Parse the parameter. */
21329 = cp_parser_parameter_declaration (parser
,
21330 /*template_parm_p=*/false,
21333 /* We don't know yet if the enclosing context is deprecated, so wait
21334 and warn in grokparms if appropriate. */
21335 deprecated_state
= DEPRECATED_SUPPRESS
;
21339 /* If a function parameter pack was specified and an implicit template
21340 parameter was introduced during cp_parser_parameter_declaration,
21341 change any implicit parameters introduced into packs. */
21342 if (parser
->implicit_template_parms
21343 && parameter
->declarator
21344 && parameter
->declarator
->parameter_pack_p
)
21346 int latest_template_parm_idx
= TREE_VEC_LENGTH
21347 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21349 if (latest_template_parm_idx
!= template_parm_idx
)
21350 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
21351 (parameter
->decl_specifiers
.type
,
21352 template_parm_idx
, latest_template_parm_idx
);
21355 decl
= grokdeclarator (parameter
->declarator
,
21356 ¶meter
->decl_specifiers
,
21358 parameter
->default_argument
!= NULL_TREE
,
21359 ¶meter
->decl_specifiers
.attributes
);
21360 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21361 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21364 deprecated_state
= DEPRECATED_NORMAL
;
21366 /* If a parse error occurred parsing the parameter declaration,
21367 then the entire parameter-declaration-list is erroneous. */
21368 if (decl
== error_mark_node
)
21371 parameters
= error_mark_node
;
21375 if (parameter
->decl_specifiers
.attributes
)
21376 cplus_decl_attributes (&decl
,
21377 parameter
->decl_specifiers
.attributes
,
21379 if (DECL_NAME (decl
))
21380 decl
= pushdecl (decl
);
21382 if (decl
!= error_mark_node
)
21384 retrofit_lang_decl (decl
);
21385 DECL_PARM_INDEX (decl
) = ++index
;
21386 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21389 /* Add the new parameter to the list. */
21390 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21391 tail
= &TREE_CHAIN (*tail
);
21393 /* Peek at the next token. */
21394 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21395 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21396 /* These are for Objective-C++ */
21397 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21398 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21399 /* The parameter-declaration-list is complete. */
21401 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21405 /* Peek at the next token. */
21406 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21407 /* If it's an ellipsis, then the list is complete. */
21408 if (token
->type
== CPP_ELLIPSIS
)
21410 /* Otherwise, there must be more parameters. Consume the
21412 cp_lexer_consume_token (parser
->lexer
);
21413 /* When parsing something like:
21415 int i(float f, double d)
21417 we can tell after seeing the declaration for "f" that we
21418 are not looking at an initialization of a variable "i",
21419 but rather at the declaration of a function "i".
21421 Due to the fact that the parsing of template arguments
21422 (as specified to a template-id) requires backtracking we
21423 cannot use this technique when inside a template argument
21425 if (!parser
->in_template_argument_list_p
21426 && !parser
->in_type_id_in_expr_p
21427 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21428 /* However, a parameter-declaration of the form
21429 "float(f)" (which is a valid declaration of a
21430 parameter "f") can also be interpreted as an
21431 expression (the conversion of "f" to "float"). */
21432 && !parenthesized_p
)
21433 cp_parser_commit_to_tentative_parse (parser
);
21437 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21438 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21439 cp_parser_skip_to_closing_parenthesis (parser
,
21440 /*recovering=*/true,
21441 /*or_comma=*/false,
21442 /*consume_paren=*/false);
21447 parser
->in_unbraced_linkage_specification_p
21448 = saved_in_unbraced_linkage_specification_p
;
21450 /* Reset implicit_template_scope if we are about to leave the function
21451 parameter list that introduced it. Note that for out-of-line member
21452 definitions, there will be one or more class scopes before we get to
21453 the template parameter scope. */
21455 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21456 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21458 while (maybe_its
->kind
== sk_class
)
21459 maybe_its
= maybe_its
->level_chain
;
21460 if (maybe_its
== its
)
21462 parser
->implicit_template_parms
= 0;
21463 parser
->implicit_template_scope
= 0;
21470 /* Parse a parameter declaration.
21472 parameter-declaration:
21473 decl-specifier-seq ... [opt] declarator
21474 decl-specifier-seq declarator = assignment-expression
21475 decl-specifier-seq ... [opt] abstract-declarator [opt]
21476 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21478 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21479 declares a template parameter. (In that case, a non-nested `>'
21480 token encountered during the parsing of the assignment-expression
21481 is not interpreted as a greater-than operator.)
21483 Returns a representation of the parameter, or NULL if an error
21484 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21485 true iff the declarator is of the form "(p)". */
21487 static cp_parameter_declarator
*
21488 cp_parser_parameter_declaration (cp_parser
*parser
,
21489 bool template_parm_p
,
21490 bool *parenthesized_p
)
21492 int declares_class_or_enum
;
21493 cp_decl_specifier_seq decl_specifiers
;
21494 cp_declarator
*declarator
;
21495 tree default_argument
;
21496 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21497 const char *saved_message
;
21498 bool template_parameter_pack_p
= false;
21500 /* In a template parameter, `>' is not an operator.
21504 When parsing a default template-argument for a non-type
21505 template-parameter, the first non-nested `>' is taken as the end
21506 of the template parameter-list rather than a greater-than
21509 /* Type definitions may not appear in parameter types. */
21510 saved_message
= parser
->type_definition_forbidden_message
;
21511 parser
->type_definition_forbidden_message
21512 = G_("types may not be defined in parameter types");
21514 /* Parse the declaration-specifiers. */
21515 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21516 cp_parser_decl_specifier_seq (parser
,
21517 CP_PARSER_FLAGS_NONE
,
21519 &declares_class_or_enum
);
21521 /* Complain about missing 'typename' or other invalid type names. */
21522 if (!decl_specifiers
.any_type_specifiers_p
21523 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21524 decl_specifiers
.type
= error_mark_node
;
21526 /* If an error occurred, there's no reason to attempt to parse the
21527 rest of the declaration. */
21528 if (cp_parser_error_occurred (parser
))
21530 parser
->type_definition_forbidden_message
= saved_message
;
21534 /* Peek at the next token. */
21535 token
= cp_lexer_peek_token (parser
->lexer
);
21537 /* If the next token is a `)', `,', `=', `>', or `...', then there
21538 is no declarator. However, when variadic templates are enabled,
21539 there may be a declarator following `...'. */
21540 if (token
->type
== CPP_CLOSE_PAREN
21541 || token
->type
== CPP_COMMA
21542 || token
->type
== CPP_EQ
21543 || token
->type
== CPP_GREATER
)
21546 if (parenthesized_p
)
21547 *parenthesized_p
= false;
21549 /* Otherwise, there should be a declarator. */
21552 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21553 parser
->default_arg_ok_p
= false;
21555 /* After seeing a decl-specifier-seq, if the next token is not a
21556 "(", there is no possibility that the code is a valid
21557 expression. Therefore, if parsing tentatively, we commit at
21559 if (!parser
->in_template_argument_list_p
21560 /* In an expression context, having seen:
21564 we cannot be sure whether we are looking at a
21565 function-type (taking a "char" as a parameter) or a cast
21566 of some object of type "char" to "int". */
21567 && !parser
->in_type_id_in_expr_p
21568 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21569 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21570 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21571 cp_parser_commit_to_tentative_parse (parser
);
21572 /* Parse the declarator. */
21573 declarator_token_start
= token
;
21574 declarator
= cp_parser_declarator (parser
,
21575 CP_PARSER_DECLARATOR_EITHER
,
21576 /*ctor_dtor_or_conv_p=*/NULL
,
21578 /*member_p=*/false,
21579 /*friend_p=*/false);
21580 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21581 /* After the declarator, allow more attributes. */
21582 decl_specifiers
.attributes
21583 = attr_chainon (decl_specifiers
.attributes
,
21584 cp_parser_attributes_opt (parser
));
21586 /* If the declarator is a template parameter pack, remember that and
21587 clear the flag in the declarator itself so we don't get errors
21588 from grokdeclarator. */
21589 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21591 declarator
->parameter_pack_p
= false;
21592 template_parameter_pack_p
= true;
21596 /* If the next token is an ellipsis, and we have not seen a declarator
21597 name, and if either the type of the declarator contains parameter
21598 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21599 for, eg, abbreviated integral type names), then we actually have a
21600 parameter pack expansion expression. Otherwise, leave the ellipsis
21601 for a C-style variadic function. */
21602 token
= cp_lexer_peek_token (parser
->lexer
);
21603 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21605 tree type
= decl_specifiers
.type
;
21607 if (type
&& DECL_P (type
))
21608 type
= TREE_TYPE (type
);
21611 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21612 && (template_parm_p
|| uses_parameter_packs (type
)))
21613 || (!type
&& template_parm_p
))
21614 && declarator_can_be_parameter_pack (declarator
))
21616 /* Consume the `...'. */
21617 cp_lexer_consume_token (parser
->lexer
);
21618 maybe_warn_variadic_templates ();
21620 /* Build a pack expansion type */
21621 if (template_parm_p
)
21622 template_parameter_pack_p
= true;
21623 else if (declarator
)
21624 declarator
->parameter_pack_p
= true;
21626 decl_specifiers
.type
= make_pack_expansion (type
);
21630 /* The restriction on defining new types applies only to the type
21631 of the parameter, not to the default argument. */
21632 parser
->type_definition_forbidden_message
= saved_message
;
21634 /* If the next token is `=', then process a default argument. */
21635 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21637 tree type
= decl_specifiers
.type
;
21638 token
= cp_lexer_peek_token (parser
->lexer
);
21639 /* If we are defining a class, then the tokens that make up the
21640 default argument must be saved and processed later. */
21641 if (!template_parm_p
&& at_class_scope_p ()
21642 && TYPE_BEING_DEFINED (current_class_type
)
21643 && !LAMBDA_TYPE_P (current_class_type
))
21644 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21646 // A constrained-type-specifier may declare a type template-parameter.
21647 else if (declares_constrained_type_template_parameter (type
))
21649 = cp_parser_default_type_template_argument (parser
);
21651 // A constrained-type-specifier may declare a template-template-parameter.
21652 else if (declares_constrained_template_template_parameter (type
))
21654 = cp_parser_default_template_template_argument (parser
);
21656 /* Outside of a class definition, we can just parse the
21657 assignment-expression. */
21660 = cp_parser_default_argument (parser
, template_parm_p
);
21662 if (!parser
->default_arg_ok_p
)
21664 permerror (token
->location
,
21665 "default arguments are only "
21666 "permitted for function parameters");
21668 else if ((declarator
&& declarator
->parameter_pack_p
)
21669 || template_parameter_pack_p
21670 || (decl_specifiers
.type
21671 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21673 /* Find the name of the parameter pack. */
21674 cp_declarator
*id_declarator
= declarator
;
21675 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21676 id_declarator
= id_declarator
->declarator
;
21678 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21679 error_at (declarator_token_start
->location
,
21681 ? G_("template parameter pack %qD "
21682 "cannot have a default argument")
21683 : G_("parameter pack %qD cannot have "
21684 "a default argument"),
21685 id_declarator
->u
.id
.unqualified_name
);
21687 error_at (declarator_token_start
->location
,
21689 ? G_("template parameter pack cannot have "
21690 "a default argument")
21691 : G_("parameter pack cannot have a "
21692 "default argument"));
21694 default_argument
= NULL_TREE
;
21698 default_argument
= NULL_TREE
;
21700 /* Generate a location for the parameter, ranging from the start of the
21701 initial token to the end of the final token (using input_location for
21702 the latter, set up by cp_lexer_set_source_position_from_token when
21705 If we have a identifier, then use it for the caret location, e.g.
21707 extern int callee (int one, int (*two)(int, int), float three);
21708 ~~~~~~^~~~~~~~~~~~~~
21710 otherwise, reuse the start location for the caret location e.g.:
21712 extern int callee (int one, int (*)(int, int), float three);
21716 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21717 ? declarator
->id_loc
21718 : decl_spec_token_start
->location
);
21719 location_t param_loc
= make_location (caret_loc
,
21720 decl_spec_token_start
->location
,
21723 return make_parameter_declarator (&decl_specifiers
,
21727 template_parameter_pack_p
);
21730 /* Parse a default argument and return it.
21732 TEMPLATE_PARM_P is true if this is a default argument for a
21733 non-type template parameter. */
21735 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21737 tree default_argument
= NULL_TREE
;
21738 bool saved_greater_than_is_operator_p
;
21739 bool saved_local_variables_forbidden_p
;
21740 bool non_constant_p
, is_direct_init
;
21742 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21744 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21745 parser
->greater_than_is_operator_p
= !template_parm_p
;
21746 /* Local variable names (and the `this' keyword) may not
21747 appear in a default argument. */
21748 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21749 parser
->local_variables_forbidden_p
= true;
21750 /* Parse the assignment-expression. */
21751 if (template_parm_p
)
21752 push_deferring_access_checks (dk_no_deferred
);
21753 tree saved_class_ptr
= NULL_TREE
;
21754 tree saved_class_ref
= NULL_TREE
;
21755 /* The "this" pointer is not valid in a default argument. */
21758 saved_class_ptr
= current_class_ptr
;
21759 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21760 saved_class_ref
= current_class_ref
;
21761 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21764 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21765 /* Restore the "this" pointer. */
21768 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21769 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21771 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21772 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21773 if (template_parm_p
)
21774 pop_deferring_access_checks ();
21775 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21776 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21778 return default_argument
;
21781 /* Parse a function-body.
21784 compound_statement */
21787 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21789 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21790 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21794 /* Parse a ctor-initializer-opt followed by a function-body. Return
21795 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21796 is true we are parsing a function-try-block. */
21799 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21800 bool in_function_try_block
)
21803 const bool check_body_p
=
21804 DECL_CONSTRUCTOR_P (current_function_decl
)
21805 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21808 /* Begin the function body. */
21809 body
= begin_function_body ();
21810 /* Parse the optional ctor-initializer. */
21811 cp_parser_ctor_initializer_opt (parser
);
21813 /* If we're parsing a constexpr constructor definition, we need
21814 to check that the constructor body is indeed empty. However,
21815 before we get to cp_parser_function_body lot of junk has been
21816 generated, so we can't just check that we have an empty block.
21817 Rather we take a snapshot of the outermost block, and check whether
21818 cp_parser_function_body changed its state. */
21821 list
= cur_stmt_list
;
21822 if (STATEMENT_LIST_TAIL (list
))
21823 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21825 /* Parse the function-body. */
21826 cp_parser_function_body (parser
, in_function_try_block
);
21828 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21829 /* Finish the function body. */
21830 finish_function_body (body
);
21833 /* Parse an initializer.
21836 = initializer-clause
21837 ( expression-list )
21839 Returns an expression representing the initializer. If no
21840 initializer is present, NULL_TREE is returned.
21842 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21843 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21844 set to TRUE if there is no initializer present. If there is an
21845 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21846 is set to true; otherwise it is set to false. */
21849 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21850 bool* non_constant_p
)
21855 /* Peek at the next token. */
21856 token
= cp_lexer_peek_token (parser
->lexer
);
21858 /* Let our caller know whether or not this initializer was
21860 *is_direct_init
= (token
->type
!= CPP_EQ
);
21861 /* Assume that the initializer is constant. */
21862 *non_constant_p
= false;
21864 if (token
->type
== CPP_EQ
)
21866 /* Consume the `='. */
21867 cp_lexer_consume_token (parser
->lexer
);
21868 /* Parse the initializer-clause. */
21869 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21871 else if (token
->type
== CPP_OPEN_PAREN
)
21873 vec
<tree
, va_gc
> *vec
;
21874 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21876 /*allow_expansion_p=*/true,
21879 return error_mark_node
;
21880 init
= build_tree_list_vec (vec
);
21881 release_tree_vector (vec
);
21883 else if (token
->type
== CPP_OPEN_BRACE
)
21885 cp_lexer_set_source_position (parser
->lexer
);
21886 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21887 init
= cp_parser_braced_list (parser
, non_constant_p
);
21888 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21892 /* Anything else is an error. */
21893 cp_parser_error (parser
, "expected initializer");
21894 init
= error_mark_node
;
21897 if (check_for_bare_parameter_packs (init
))
21898 init
= error_mark_node
;
21903 /* Parse an initializer-clause.
21905 initializer-clause:
21906 assignment-expression
21909 Returns an expression representing the initializer.
21911 If the `assignment-expression' production is used the value
21912 returned is simply a representation for the expression.
21914 Otherwise, calls cp_parser_braced_list. */
21917 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
21919 cp_expr initializer
;
21921 /* Assume the expression is constant. */
21922 *non_constant_p
= false;
21924 /* If it is not a `{', then we are looking at an
21925 assignment-expression. */
21926 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
21929 = cp_parser_constant_expression (parser
,
21930 /*allow_non_constant_p=*/true,
21934 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
21936 return initializer
;
21939 /* Parse a brace-enclosed initializer list.
21942 { initializer-list , [opt] }
21943 { designated-initializer-list , [opt] }
21946 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21947 the elements of the initializer-list (or NULL, if the last
21948 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21949 NULL_TREE. There is no way to detect whether or not the optional
21950 trailing `,' was provided. NON_CONSTANT_P is as for
21951 cp_parser_initializer. */
21954 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
21957 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21959 /* Consume the `{' token. */
21960 matching_braces braces
;
21961 braces
.require_open (parser
);
21962 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21963 initializer
= make_node (CONSTRUCTOR
);
21964 /* If it's not a `}', then there is a non-trivial initializer. */
21965 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
21967 /* Parse the initializer list. */
21968 CONSTRUCTOR_ELTS (initializer
)
21969 = cp_parser_initializer_list (parser
, non_constant_p
);
21970 /* A trailing `,' token is allowed. */
21971 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21972 cp_lexer_consume_token (parser
->lexer
);
21975 *non_constant_p
= false;
21976 /* Now, there should be a trailing `}'. */
21977 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
21978 braces
.require_close (parser
);
21979 TREE_TYPE (initializer
) = init_list_type_node
;
21981 cp_expr
result (initializer
);
21982 /* Build a location of the form:
21985 with caret==start at the open brace, finish at the close brace. */
21986 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
21987 result
.set_location (combined_loc
);
21991 /* Consume tokens up to, and including, the next non-nested closing `]'.
21992 Returns true iff we found a closing `]'. */
21995 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
21997 unsigned square_depth
= 0;
22001 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22003 switch (token
->type
)
22006 case CPP_PRAGMA_EOL
:
22007 /* If we've run out of tokens, then there is no closing `]'. */
22010 case CPP_OPEN_SQUARE
:
22014 case CPP_CLOSE_SQUARE
:
22015 if (!square_depth
--)
22017 cp_lexer_consume_token (parser
->lexer
);
22026 /* Consume the token. */
22027 cp_lexer_consume_token (parser
->lexer
);
22031 /* Return true if we are looking at an array-designator, false otherwise. */
22034 cp_parser_array_designator_p (cp_parser
*parser
)
22036 /* Consume the `['. */
22037 cp_lexer_consume_token (parser
->lexer
);
22039 cp_lexer_save_tokens (parser
->lexer
);
22041 /* Skip tokens until the next token is a closing square bracket.
22042 If we find the closing `]', and the next token is a `=', then
22043 we are looking at an array designator. */
22044 bool array_designator_p
22045 = (cp_parser_skip_to_closing_square_bracket (parser
)
22046 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22048 /* Roll back the tokens we skipped. */
22049 cp_lexer_rollback_tokens (parser
->lexer
);
22051 return array_designator_p
;
22054 /* Parse an initializer-list.
22057 initializer-clause ... [opt]
22058 initializer-list , initializer-clause ... [opt]
22062 designated-initializer-list:
22063 designated-initializer-clause
22064 designated-initializer-list , designated-initializer-clause
22066 designated-initializer-clause:
22067 designator brace-or-equal-initializer
22075 designation initializer-clause ...[opt]
22076 initializer-list , designation initializer-clause ...[opt]
22081 [ constant-expression ] =
22083 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22084 for the initializer. If the INDEX of the elt is non-NULL, it is the
22085 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22086 as for cp_parser_initializer. */
22088 static vec
<constructor_elt
, va_gc
> *
22089 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22091 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22092 bool first_p
= true;
22093 tree first_designator
= NULL_TREE
;
22095 /* Assume all of the expressions are constant. */
22096 *non_constant_p
= false;
22098 /* Parse the rest of the list. */
22104 bool clause_non_constant_p
;
22105 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22107 /* Handle the C++2A syntax, '. id ='. */
22108 if ((cxx_dialect
>= cxx2a
22109 || cp_parser_allow_gnu_extensions_p (parser
))
22110 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22111 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22112 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22113 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22114 == CPP_OPEN_BRACE
)))
22116 if (cxx_dialect
< cxx2a
)
22117 pedwarn (loc
, OPT_Wpedantic
,
22118 "C++ designated initializers only available with "
22119 "-std=c++2a or -std=gnu++2a");
22120 /* Consume the `.'. */
22121 cp_lexer_consume_token (parser
->lexer
);
22122 /* Consume the identifier. */
22123 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22124 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22125 /* Consume the `='. */
22126 cp_lexer_consume_token (parser
->lexer
);
22128 /* Also, if the next token is an identifier and the following one is a
22129 colon, we are looking at the GNU designated-initializer
22131 else if (cp_parser_allow_gnu_extensions_p (parser
)
22132 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22133 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22136 /* Warn the user that they are using an extension. */
22137 pedwarn (loc
, OPT_Wpedantic
,
22138 "ISO C++ does not allow GNU designated initializers");
22139 /* Consume the identifier. */
22140 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22141 /* Consume the `:'. */
22142 cp_lexer_consume_token (parser
->lexer
);
22144 /* Also handle C99 array designators, '[ const ] ='. */
22145 else if (cp_parser_allow_gnu_extensions_p (parser
)
22146 && !c_dialect_objc ()
22147 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22149 /* In C++11, [ could start a lambda-introducer. */
22150 bool non_const
= false;
22152 cp_parser_parse_tentatively (parser
);
22154 if (!cp_parser_array_designator_p (parser
))
22156 cp_parser_simulate_error (parser
);
22157 designator
= NULL_TREE
;
22161 designator
= cp_parser_constant_expression (parser
, true,
22163 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22164 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22167 if (!cp_parser_parse_definitely (parser
))
22168 designator
= NULL_TREE
;
22170 && (!require_potential_rvalue_constant_expression
22172 designator
= NULL_TREE
;
22174 /* Warn the user that they are using an extension. */
22175 pedwarn (loc
, OPT_Wpedantic
,
22176 "ISO C++ does not allow C99 designated initializers");
22179 designator
= NULL_TREE
;
22183 first_designator
= designator
;
22186 else if (cxx_dialect
>= cxx2a
22187 && first_designator
!= error_mark_node
22188 && (!first_designator
!= !designator
))
22190 error_at (loc
, "either all initializer clauses should be designated "
22191 "or none of them should be");
22192 first_designator
= error_mark_node
;
22194 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22195 first_designator
= designator
;
22197 /* Parse the initializer. */
22198 initializer
= cp_parser_initializer_clause (parser
,
22199 &clause_non_constant_p
);
22200 /* If any clause is non-constant, so is the entire initializer. */
22201 if (clause_non_constant_p
)
22202 *non_constant_p
= true;
22204 /* If we have an ellipsis, this is an initializer pack
22206 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22208 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22210 /* Consume the `...'. */
22211 cp_lexer_consume_token (parser
->lexer
);
22213 if (designator
&& cxx_dialect
>= cxx2a
)
22215 "%<...%> not allowed in designated initializer list");
22217 /* Turn the initializer into an initializer expansion. */
22218 initializer
= make_pack_expansion (initializer
);
22221 /* Add it to the vector. */
22222 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22224 /* If the next token is not a comma, we have reached the end of
22226 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22229 /* Peek at the next token. */
22230 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22231 /* If the next token is a `}', then we're still done. An
22232 initializer-clause can have a trailing `,' after the
22233 initializer-list and before the closing `}'. */
22234 if (token
->type
== CPP_CLOSE_BRACE
)
22237 /* Consume the `,' token. */
22238 cp_lexer_consume_token (parser
->lexer
);
22241 /* The same identifier shall not appear in multiple designators
22242 of a designated-initializer-list. */
22243 if (first_designator
)
22246 tree designator
, val
;
22247 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22248 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22250 if (IDENTIFIER_MARKED (designator
))
22252 error_at (EXPR_LOC_OR_LOC (val
, input_location
),
22253 "%<.%s%> designator used multiple times in "
22254 "the same initializer list",
22255 IDENTIFIER_POINTER (designator
));
22256 (*v
)[i
].index
= NULL_TREE
;
22259 IDENTIFIER_MARKED (designator
) = 1;
22261 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22262 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22263 IDENTIFIER_MARKED (designator
) = 0;
22269 /* Classes [gram.class] */
22271 /* Parse a class-name.
22277 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22278 to indicate that names looked up in dependent types should be
22279 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22280 keyword has been used to indicate that the name that appears next
22281 is a template. TAG_TYPE indicates the explicit tag given before
22282 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22283 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22284 is the class being defined in a class-head. If ENUM_OK is TRUE,
22285 enum-names are also accepted.
22287 Returns the TYPE_DECL representing the class. */
22290 cp_parser_class_name (cp_parser
*parser
,
22291 bool typename_keyword_p
,
22292 bool template_keyword_p
,
22293 enum tag_types tag_type
,
22294 bool check_dependency_p
,
22296 bool is_declaration
,
22303 tree identifier
= NULL_TREE
;
22305 /* All class-names start with an identifier. */
22306 token
= cp_lexer_peek_token (parser
->lexer
);
22307 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22309 cp_parser_error (parser
, "expected class-name");
22310 return error_mark_node
;
22313 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22314 to a template-id, so we save it here. */
22315 scope
= parser
->scope
;
22316 if (scope
== error_mark_node
)
22317 return error_mark_node
;
22319 /* Any name names a type if we're following the `typename' keyword
22320 in a qualified name where the enclosing scope is type-dependent. */
22321 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22322 && dependent_type_p (scope
));
22323 /* Handle the common case (an identifier, but not a template-id)
22325 if (token
->type
== CPP_NAME
22326 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22328 cp_token
*identifier_token
;
22331 /* Look for the identifier. */
22332 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22333 ambiguous_p
= identifier_token
->error_reported
;
22334 identifier
= cp_parser_identifier (parser
);
22335 /* If the next token isn't an identifier, we are certainly not
22336 looking at a class-name. */
22337 if (identifier
== error_mark_node
)
22338 decl
= error_mark_node
;
22339 /* If we know this is a type-name, there's no need to look it
22341 else if (typename_p
)
22345 tree ambiguous_decls
;
22346 /* If we already know that this lookup is ambiguous, then
22347 we've already issued an error message; there's no reason
22351 cp_parser_simulate_error (parser
);
22352 return error_mark_node
;
22354 /* If the next token is a `::', then the name must be a type
22357 [basic.lookup.qual]
22359 During the lookup for a name preceding the :: scope
22360 resolution operator, object, function, and enumerator
22361 names are ignored. */
22362 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22363 tag_type
= scope_type
;
22364 /* Look up the name. */
22365 decl
= cp_parser_lookup_name (parser
, identifier
,
22367 /*is_template=*/false,
22368 /*is_namespace=*/false,
22369 check_dependency_p
,
22371 identifier_token
->location
);
22372 if (ambiguous_decls
)
22374 if (cp_parser_parsing_tentatively (parser
))
22375 cp_parser_simulate_error (parser
);
22376 return error_mark_node
;
22382 /* Try a template-id. */
22383 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22384 check_dependency_p
,
22387 if (decl
== error_mark_node
)
22388 return error_mark_node
;
22391 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22393 /* If this is a typename, create a TYPENAME_TYPE. */
22394 if (typename_p
&& decl
!= error_mark_node
)
22396 decl
= make_typename_type (scope
, decl
, typename_type
,
22397 /*complain=*/tf_error
);
22398 if (decl
!= error_mark_node
)
22399 decl
= TYPE_NAME (decl
);
22402 decl
= strip_using_decl (decl
);
22404 /* Check to see that it is really the name of a class. */
22405 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22406 && identifier_p (TREE_OPERAND (decl
, 0))
22407 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22408 /* Situations like this:
22410 template <typename T> struct A {
22411 typename T::template X<int>::I i;
22414 are problematic. Is `T::template X<int>' a class-name? The
22415 standard does not seem to be definitive, but there is no other
22416 valid interpretation of the following `::'. Therefore, those
22417 names are considered class-names. */
22419 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22420 if (decl
!= error_mark_node
)
22421 decl
= TYPE_NAME (decl
);
22423 else if (TREE_CODE (decl
) != TYPE_DECL
22424 || TREE_TYPE (decl
) == error_mark_node
22425 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22426 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22427 /* In Objective-C 2.0, a classname followed by '.' starts a
22428 dot-syntax expression, and it's not a type-name. */
22429 || (c_dialect_objc ()
22430 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22431 && objc_is_class_name (decl
)))
22432 decl
= error_mark_node
;
22434 if (decl
== error_mark_node
)
22435 cp_parser_error (parser
, "expected class-name");
22436 else if (identifier
&& !parser
->scope
)
22437 maybe_note_name_used_in_class (identifier
, decl
);
22442 /* Parse a class-specifier.
22445 class-head { member-specification [opt] }
22447 Returns the TREE_TYPE representing the class. */
22450 cp_parser_class_specifier_1 (cp_parser
* parser
)
22453 tree attributes
= NULL_TREE
;
22454 bool nested_name_specifier_p
;
22455 unsigned saved_num_template_parameter_lists
;
22456 bool saved_in_function_body
;
22457 unsigned char in_statement
;
22458 bool in_switch_statement_p
;
22459 bool saved_in_unbraced_linkage_specification_p
;
22460 tree old_scope
= NULL_TREE
;
22461 tree scope
= NULL_TREE
;
22462 cp_token
*closing_brace
;
22464 push_deferring_access_checks (dk_no_deferred
);
22466 /* Parse the class-head. */
22467 type
= cp_parser_class_head (parser
,
22468 &nested_name_specifier_p
);
22469 /* If the class-head was a semantic disaster, skip the entire body
22473 cp_parser_skip_to_end_of_block_or_statement (parser
);
22474 pop_deferring_access_checks ();
22475 return error_mark_node
;
22478 /* Look for the `{'. */
22479 matching_braces braces
;
22480 if (!braces
.require_open (parser
))
22482 pop_deferring_access_checks ();
22483 return error_mark_node
;
22486 cp_ensure_no_omp_declare_simd (parser
);
22487 cp_ensure_no_oacc_routine (parser
);
22489 /* Issue an error message if type-definitions are forbidden here. */
22490 cp_parser_check_type_definition (parser
);
22491 /* Remember that we are defining one more class. */
22492 ++parser
->num_classes_being_defined
;
22493 /* Inside the class, surrounding template-parameter-lists do not
22495 saved_num_template_parameter_lists
22496 = parser
->num_template_parameter_lists
;
22497 parser
->num_template_parameter_lists
= 0;
22498 /* We are not in a function body. */
22499 saved_in_function_body
= parser
->in_function_body
;
22500 parser
->in_function_body
= false;
22501 /* Or in a loop. */
22502 in_statement
= parser
->in_statement
;
22503 parser
->in_statement
= 0;
22504 /* Or in a switch. */
22505 in_switch_statement_p
= parser
->in_switch_statement_p
;
22506 parser
->in_switch_statement_p
= false;
22507 /* We are not immediately inside an extern "lang" block. */
22508 saved_in_unbraced_linkage_specification_p
22509 = parser
->in_unbraced_linkage_specification_p
;
22510 parser
->in_unbraced_linkage_specification_p
= false;
22512 // Associate constraints with the type.
22514 type
= associate_classtype_constraints (type
);
22516 /* Start the class. */
22517 if (nested_name_specifier_p
)
22519 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22520 old_scope
= push_inner_scope (scope
);
22522 type
= begin_class_definition (type
);
22524 if (type
== error_mark_node
)
22525 /* If the type is erroneous, skip the entire body of the class. */
22526 cp_parser_skip_to_closing_brace (parser
);
22528 /* Parse the member-specification. */
22529 cp_parser_member_specification_opt (parser
);
22531 /* Look for the trailing `}'. */
22532 closing_brace
= braces
.require_close (parser
);
22533 /* Look for trailing attributes to apply to this class. */
22534 if (cp_parser_allow_gnu_extensions_p (parser
))
22535 attributes
= cp_parser_gnu_attributes_opt (parser
);
22536 if (type
!= error_mark_node
)
22537 type
= finish_struct (type
, attributes
);
22538 if (nested_name_specifier_p
)
22539 pop_inner_scope (old_scope
, scope
);
22541 /* We've finished a type definition. Check for the common syntax
22542 error of forgetting a semicolon after the definition. We need to
22543 be careful, as we can't just check for not-a-semicolon and be done
22544 with it; the user might have typed:
22546 class X { } c = ...;
22547 class X { } *p = ...;
22549 and so forth. Instead, enumerate all the possible tokens that
22550 might follow this production; if we don't see one of them, then
22551 complain and silently insert the semicolon. */
22553 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22554 bool want_semicolon
= true;
22556 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22557 /* Don't try to parse c++11 attributes here. As per the
22558 grammar, that should be a task for
22559 cp_parser_decl_specifier_seq. */
22560 want_semicolon
= false;
22562 switch (token
->type
)
22565 case CPP_SEMICOLON
:
22568 case CPP_OPEN_PAREN
:
22569 case CPP_CLOSE_PAREN
:
22571 want_semicolon
= false;
22574 /* While it's legal for type qualifiers and storage class
22575 specifiers to follow type definitions in the grammar, only
22576 compiler testsuites contain code like that. Assume that if
22577 we see such code, then what we're really seeing is a case
22581 const <type> var = ...;
22586 static <type> func (...) ...
22588 i.e. the qualifier or specifier applies to the next
22589 declaration. To do so, however, we need to look ahead one
22590 more token to see if *that* token is a type specifier.
22592 This code could be improved to handle:
22595 static const <type> var = ...; */
22597 if (keyword_is_decl_specifier (token
->keyword
))
22599 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22601 /* Handling user-defined types here would be nice, but very
22604 = (lookahead
->type
== CPP_KEYWORD
22605 && keyword_begins_type_specifier (lookahead
->keyword
));
22612 /* If we don't have a type, then something is very wrong and we
22613 shouldn't try to do anything clever. Likewise for not seeing the
22615 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22617 /* Locate the closing brace. */
22618 cp_token_position prev
22619 = cp_lexer_previous_token_position (parser
->lexer
);
22620 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22621 location_t loc
= prev_token
->location
;
22623 /* We want to suggest insertion of a ';' immediately *after* the
22624 closing brace, so, if we can, offset the location by 1 column. */
22625 location_t next_loc
= loc
;
22626 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22627 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22629 rich_location
richloc (line_table
, next_loc
);
22631 /* If we successfully offset the location, suggest the fix-it. */
22632 if (next_loc
!= loc
)
22633 richloc
.add_fixit_insert_before (next_loc
, ";");
22635 if (CLASSTYPE_DECLARED_CLASS (type
))
22636 error_at (&richloc
,
22637 "expected %<;%> after class definition");
22638 else if (TREE_CODE (type
) == RECORD_TYPE
)
22639 error_at (&richloc
,
22640 "expected %<;%> after struct definition");
22641 else if (TREE_CODE (type
) == UNION_TYPE
)
22642 error_at (&richloc
,
22643 "expected %<;%> after union definition");
22645 gcc_unreachable ();
22647 /* Unget one token and smash it to look as though we encountered
22648 a semicolon in the input stream. */
22649 cp_lexer_set_token_position (parser
->lexer
, prev
);
22650 token
= cp_lexer_peek_token (parser
->lexer
);
22651 token
->type
= CPP_SEMICOLON
;
22652 token
->keyword
= RID_MAX
;
22656 /* If this class is not itself within the scope of another class,
22657 then we need to parse the bodies of all of the queued function
22658 definitions. Note that the queued functions defined in a class
22659 are not always processed immediately following the
22660 class-specifier for that class. Consider:
22663 struct B { void f() { sizeof (A); } };
22666 If `f' were processed before the processing of `A' were
22667 completed, there would be no way to compute the size of `A'.
22668 Note that the nesting we are interested in here is lexical --
22669 not the semantic nesting given by TYPE_CONTEXT. In particular,
22672 struct A { struct B; };
22673 struct A::B { void f() { } };
22675 there is no need to delay the parsing of `A::B::f'. */
22676 if (--parser
->num_classes_being_defined
== 0)
22679 tree class_type
= NULL_TREE
;
22680 tree pushed_scope
= NULL_TREE
;
22682 cp_default_arg_entry
*e
;
22683 tree save_ccp
, save_ccr
;
22685 if (any_erroneous_template_args_p (type
))
22687 /* Skip default arguments, NSDMIs, etc, in order to improve
22688 error recovery (c++/71169, c++/71832). */
22689 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22690 vec_safe_truncate (unparsed_nsdmis
, 0);
22691 vec_safe_truncate (unparsed_classes
, 0);
22692 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22695 /* In a first pass, parse default arguments to the functions.
22696 Then, in a second pass, parse the bodies of the functions.
22697 This two-phased approach handles cases like:
22705 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22708 /* If there are default arguments that have not yet been processed,
22709 take care of them now. */
22710 if (class_type
!= e
->class_type
)
22713 pop_scope (pushed_scope
);
22714 class_type
= e
->class_type
;
22715 pushed_scope
= push_scope (class_type
);
22717 /* Make sure that any template parameters are in scope. */
22718 maybe_begin_member_template_processing (decl
);
22719 /* Parse the default argument expressions. */
22720 cp_parser_late_parsing_default_args (parser
, decl
);
22721 /* Remove any template parameters from the symbol table. */
22722 maybe_end_member_template_processing ();
22724 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22725 /* Now parse any NSDMIs. */
22726 save_ccp
= current_class_ptr
;
22727 save_ccr
= current_class_ref
;
22728 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22730 if (class_type
!= DECL_CONTEXT (decl
))
22733 pop_scope (pushed_scope
);
22734 class_type
= DECL_CONTEXT (decl
);
22735 pushed_scope
= push_scope (class_type
);
22737 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22738 cp_parser_late_parsing_nsdmi (parser
, decl
);
22740 vec_safe_truncate (unparsed_nsdmis
, 0);
22741 current_class_ptr
= save_ccp
;
22742 current_class_ref
= save_ccr
;
22744 pop_scope (pushed_scope
);
22746 /* Now do some post-NSDMI bookkeeping. */
22747 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22748 after_nsdmi_defaulted_late_checks (class_type
);
22749 vec_safe_truncate (unparsed_classes
, 0);
22750 after_nsdmi_defaulted_late_checks (type
);
22752 /* Now parse the body of the functions. */
22755 /* OpenMP UDRs need to be parsed before all other functions. */
22756 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22757 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22758 cp_parser_late_parsing_for_member (parser
, decl
);
22759 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22760 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22761 cp_parser_late_parsing_for_member (parser
, decl
);
22764 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22765 cp_parser_late_parsing_for_member (parser
, decl
);
22766 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22769 vec_safe_push (unparsed_classes
, type
);
22771 /* Put back any saved access checks. */
22772 pop_deferring_access_checks ();
22774 /* Restore saved state. */
22775 parser
->in_switch_statement_p
= in_switch_statement_p
;
22776 parser
->in_statement
= in_statement
;
22777 parser
->in_function_body
= saved_in_function_body
;
22778 parser
->num_template_parameter_lists
22779 = saved_num_template_parameter_lists
;
22780 parser
->in_unbraced_linkage_specification_p
22781 = saved_in_unbraced_linkage_specification_p
;
22787 cp_parser_class_specifier (cp_parser
* parser
)
22790 timevar_push (TV_PARSE_STRUCT
);
22791 ret
= cp_parser_class_specifier_1 (parser
);
22792 timevar_pop (TV_PARSE_STRUCT
);
22796 /* Parse a class-head.
22799 class-key identifier [opt] base-clause [opt]
22800 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22801 class-key nested-name-specifier [opt] template-id
22804 class-virt-specifier:
22808 class-key attributes identifier [opt] base-clause [opt]
22809 class-key attributes nested-name-specifier identifier base-clause [opt]
22810 class-key attributes nested-name-specifier [opt] template-id
22813 Upon return BASES is initialized to the list of base classes (or
22814 NULL, if there are none) in the same form returned by
22815 cp_parser_base_clause.
22817 Returns the TYPE of the indicated class. Sets
22818 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22819 involving a nested-name-specifier was used, and FALSE otherwise.
22821 Returns error_mark_node if this is not a class-head.
22823 Returns NULL_TREE if the class-head is syntactically valid, but
22824 semantically invalid in a way that means we should skip the entire
22825 body of the class. */
22828 cp_parser_class_head (cp_parser
* parser
,
22829 bool* nested_name_specifier_p
)
22831 tree nested_name_specifier
;
22832 enum tag_types class_key
;
22833 tree id
= NULL_TREE
;
22834 tree type
= NULL_TREE
;
22837 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22838 bool template_id_p
= false;
22839 bool qualified_p
= false;
22840 bool invalid_nested_name_p
= false;
22841 bool invalid_explicit_specialization_p
= false;
22842 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22843 tree pushed_scope
= NULL_TREE
;
22844 unsigned num_templates
;
22845 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22846 /* Assume no nested-name-specifier will be present. */
22847 *nested_name_specifier_p
= false;
22848 /* Assume no template parameter lists will be used in defining the
22851 parser
->colon_corrects_to_scope_p
= false;
22853 /* Look for the class-key. */
22854 class_key
= cp_parser_class_key (parser
);
22855 if (class_key
== none_type
)
22856 return error_mark_node
;
22858 location_t class_head_start_location
= input_location
;
22860 /* Parse the attributes. */
22861 attributes
= cp_parser_attributes_opt (parser
);
22863 /* If the next token is `::', that is invalid -- but sometimes
22864 people do try to write:
22868 Handle this gracefully by accepting the extra qualifier, and then
22869 issuing an error about it later if this really is a
22870 class-head. If it turns out just to be an elaborated type
22871 specifier, remain silent. */
22872 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22873 qualified_p
= true;
22875 push_deferring_access_checks (dk_no_check
);
22877 /* Determine the name of the class. Begin by looking for an
22878 optional nested-name-specifier. */
22879 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22880 nested_name_specifier
22881 = cp_parser_nested_name_specifier_opt (parser
,
22882 /*typename_keyword_p=*/false,
22883 /*check_dependency_p=*/false,
22885 /*is_declaration=*/false);
22886 /* If there was a nested-name-specifier, then there *must* be an
22889 cp_token
*bad_template_keyword
= NULL
;
22891 if (nested_name_specifier
)
22893 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22894 /* Although the grammar says `identifier', it really means
22895 `class-name' or `template-name'. You are only allowed to
22896 define a class that has already been declared with this
22899 The proposed resolution for Core Issue 180 says that wherever
22900 you see `class T::X' you should treat `X' as a type-name.
22902 It is OK to define an inaccessible class; for example:
22904 class A { class B; };
22907 We do not know if we will see a class-name, or a
22908 template-name. We look for a class-name first, in case the
22909 class-name is a template-id; if we looked for the
22910 template-name first we would stop after the template-name. */
22911 cp_parser_parse_tentatively (parser
);
22912 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
22913 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
22914 type
= cp_parser_class_name (parser
,
22915 /*typename_keyword_p=*/false,
22916 /*template_keyword_p=*/false,
22918 /*check_dependency_p=*/false,
22919 /*class_head_p=*/true,
22920 /*is_declaration=*/false);
22921 /* If that didn't work, ignore the nested-name-specifier. */
22922 if (!cp_parser_parse_definitely (parser
))
22924 invalid_nested_name_p
= true;
22925 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22926 id
= cp_parser_identifier (parser
);
22927 if (id
== error_mark_node
)
22930 /* If we could not find a corresponding TYPE, treat this
22931 declaration like an unqualified declaration. */
22932 if (type
== error_mark_node
)
22933 nested_name_specifier
= NULL_TREE
;
22934 /* Otherwise, count the number of templates used in TYPE and its
22935 containing scopes. */
22940 for (scope
= TREE_TYPE (type
);
22941 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
22942 scope
= get_containing_scope (scope
))
22944 && CLASS_TYPE_P (scope
)
22945 && CLASSTYPE_TEMPLATE_INFO (scope
)
22946 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
22947 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
22948 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
22952 /* Otherwise, the identifier is optional. */
22955 /* We don't know whether what comes next is a template-id,
22956 an identifier, or nothing at all. */
22957 cp_parser_parse_tentatively (parser
);
22958 /* Check for a template-id. */
22959 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22960 id
= cp_parser_template_id (parser
,
22961 /*template_keyword_p=*/false,
22962 /*check_dependency_p=*/true,
22964 /*is_declaration=*/true);
22965 /* If that didn't work, it could still be an identifier. */
22966 if (!cp_parser_parse_definitely (parser
))
22968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
22970 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22971 id
= cp_parser_identifier (parser
);
22978 template_id_p
= true;
22983 pop_deferring_access_checks ();
22987 cp_parser_check_for_invalid_template_id (parser
, id
,
22989 type_start_token
->location
);
22991 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
22993 /* If it's not a `:' or a `{' then we can't really be looking at a
22994 class-head, since a class-head only appears as part of a
22995 class-specifier. We have to detect this situation before calling
22996 xref_tag, since that has irreversible side-effects. */
22997 if (!cp_parser_next_token_starts_class_definition_p (parser
))
22999 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23000 type
= error_mark_node
;
23004 /* At this point, we're going ahead with the class-specifier, even
23005 if some other problem occurs. */
23006 cp_parser_commit_to_tentative_parse (parser
);
23007 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23009 cp_parser_error (parser
,
23010 "cannot specify %<override%> for a class");
23011 type
= error_mark_node
;
23014 /* Issue the error about the overly-qualified name now. */
23017 cp_parser_error (parser
,
23018 "global qualification of class name is invalid");
23019 type
= error_mark_node
;
23022 else if (invalid_nested_name_p
)
23024 cp_parser_error (parser
,
23025 "qualified name does not name a class");
23026 type
= error_mark_node
;
23029 else if (nested_name_specifier
)
23033 if (bad_template_keyword
)
23034 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23035 keyword template shall not appear at the top level. */
23036 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23037 "keyword %<template%> not allowed in class-head-name");
23039 /* Reject typedef-names in class heads. */
23040 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23042 error_at (type_start_token
->location
,
23043 "invalid class name in declaration of %qD",
23049 /* Figure out in what scope the declaration is being placed. */
23050 scope
= current_scope ();
23051 /* If that scope does not contain the scope in which the
23052 class was originally declared, the program is invalid. */
23053 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23055 if (at_namespace_scope_p ())
23056 error_at (type_start_token
->location
,
23057 "declaration of %qD in namespace %qD which does not "
23059 type
, scope
, nested_name_specifier
);
23061 error_at (type_start_token
->location
,
23062 "declaration of %qD in %qD which does not enclose %qD",
23063 type
, scope
, nested_name_specifier
);
23069 A declarator-id shall not be qualified except for the
23070 definition of a ... nested class outside of its class
23071 ... [or] the definition or explicit instantiation of a
23072 class member of a namespace outside of its namespace. */
23073 if (scope
== nested_name_specifier
)
23075 permerror (nested_name_specifier_token_start
->location
,
23076 "extra qualification not allowed");
23077 nested_name_specifier
= NULL_TREE
;
23081 /* An explicit-specialization must be preceded by "template <>". If
23082 it is not, try to recover gracefully. */
23083 if (at_namespace_scope_p ()
23084 && parser
->num_template_parameter_lists
== 0
23085 && !processing_template_parmlist
23088 /* Build a location of this form:
23089 struct typename <ARGS>
23090 ^~~~~~~~~~~~~~~~~~~~~~
23091 with caret==start at the start token, and
23092 finishing at the end of the type. */
23093 location_t reported_loc
23094 = make_location (class_head_start_location
,
23095 class_head_start_location
,
23096 get_finish (type_start_token
->location
));
23097 rich_location
richloc (line_table
, reported_loc
);
23098 richloc
.add_fixit_insert_before (class_head_start_location
,
23100 error_at (&richloc
,
23101 "an explicit specialization must be preceded by"
23102 " %<template <>%>");
23103 invalid_explicit_specialization_p
= true;
23104 /* Take the same action that would have been taken by
23105 cp_parser_explicit_specialization. */
23106 ++parser
->num_template_parameter_lists
;
23107 begin_specialization ();
23109 /* There must be no "return" statements between this point and the
23110 end of this function; set "type "to the correct return value and
23111 use "goto done;" to return. */
23112 /* Make sure that the right number of template parameters were
23114 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23115 type_start_token
->location
,
23116 /*declarator=*/NULL
))
23118 /* If something went wrong, there is no point in even trying to
23119 process the class-definition. */
23124 /* Look up the type. */
23127 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23128 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23129 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23131 error_at (type_start_token
->location
,
23132 "function template %qD redeclared as a class template", id
);
23133 type
= error_mark_node
;
23137 type
= TREE_TYPE (id
);
23138 type
= maybe_process_partial_specialization (type
);
23140 /* Check the scope while we still know whether or not we had a
23141 nested-name-specifier. */
23142 if (type
!= error_mark_node
)
23143 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23145 if (nested_name_specifier
)
23146 pushed_scope
= push_scope (nested_name_specifier
);
23148 else if (nested_name_specifier
)
23154 template <typename T> struct S { struct T };
23155 template <typename T> struct S<T>::T { };
23157 we will get a TYPENAME_TYPE when processing the definition of
23158 `S::T'. We need to resolve it to the actual type before we
23159 try to define it. */
23160 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23162 class_type
= resolve_typename_type (TREE_TYPE (type
),
23163 /*only_current_p=*/false);
23164 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23165 type
= TYPE_NAME (class_type
);
23168 cp_parser_error (parser
, "could not resolve typename type");
23169 type
= error_mark_node
;
23173 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23174 == error_mark_node
)
23180 class_type
= current_class_type
;
23181 /* Enter the scope indicated by the nested-name-specifier. */
23182 pushed_scope
= push_scope (nested_name_specifier
);
23183 /* Get the canonical version of this type. */
23184 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23185 /* Call push_template_decl if it seems like we should be defining a
23186 template either from the template headers or the type we're
23187 defining, so that we diagnose both extra and missing headers. */
23188 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23189 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23190 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23192 type
= push_template_decl (type
);
23193 if (type
== error_mark_node
)
23200 type
= TREE_TYPE (type
);
23201 *nested_name_specifier_p
= true;
23203 else /* The name is not a nested name. */
23205 /* If the class was unnamed, create a dummy name. */
23207 id
= make_anon_name ();
23208 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23209 ? ts_within_enclosing_non_class
23211 type
= xref_tag (class_key
, id
, tag_scope
,
23212 parser
->num_template_parameter_lists
);
23215 /* Indicate whether this class was declared as a `class' or as a
23217 if (TREE_CODE (type
) == RECORD_TYPE
)
23218 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23219 cp_parser_check_class_key (class_key
, type
);
23221 /* If this type was already complete, and we see another definition,
23222 that's an error. */
23223 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23225 error_at (type_start_token
->location
, "redefinition of %q#T",
23227 inform (location_of (type
), "previous definition of %q#T",
23232 else if (type
== error_mark_node
)
23237 /* Apply attributes now, before any use of the class as a template
23238 argument in its base list. */
23239 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23240 fixup_attribute_variants (type
);
23243 /* We will have entered the scope containing the class; the names of
23244 base classes should be looked up in that context. For example:
23246 struct A { struct B {}; struct C; };
23247 struct A::C : B {};
23251 /* Get the list of base-classes, if there is one. */
23252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23254 /* PR59482: enter the class scope so that base-specifiers are looked
23258 bases
= cp_parser_base_clause (parser
);
23259 /* PR59482: get out of the previously pushed class scope so that the
23260 subsequent pops pop the right thing. */
23267 /* If we're really defining a class, process the base classes.
23268 If they're invalid, fail. */
23269 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23270 xref_basetypes (type
, bases
);
23273 /* Leave the scope given by the nested-name-specifier. We will
23274 enter the class scope itself while processing the members. */
23276 pop_scope (pushed_scope
);
23278 if (invalid_explicit_specialization_p
)
23280 end_specialization ();
23281 --parser
->num_template_parameter_lists
;
23285 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23286 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23287 CLASSTYPE_FINAL (type
) = 1;
23289 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23293 /* Parse a class-key.
23300 Returns the kind of class-key specified, or none_type to indicate
23303 static enum tag_types
23304 cp_parser_class_key (cp_parser
* parser
)
23307 enum tag_types tag_type
;
23309 /* Look for the class-key. */
23310 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23314 /* Check to see if the TOKEN is a class-key. */
23315 tag_type
= cp_parser_token_is_class_key (token
);
23317 cp_parser_error (parser
, "expected class-key");
23321 /* Parse a type-parameter-key.
23323 type-parameter-key:
23329 cp_parser_type_parameter_key (cp_parser
* parser
)
23331 /* Look for the type-parameter-key. */
23332 enum tag_types tag_type
= none_type
;
23333 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23334 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23336 cp_lexer_consume_token (parser
->lexer
);
23337 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23338 /* typename is not allowed in a template template parameter
23339 by the standard until C++17. */
23340 pedwarn (token
->location
, OPT_Wpedantic
,
23341 "ISO C++ forbids typename key in template template parameter;"
23342 " use -std=c++17 or -std=gnu++17");
23345 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23350 /* Parse an (optional) member-specification.
23352 member-specification:
23353 member-declaration member-specification [opt]
23354 access-specifier : member-specification [opt] */
23357 cp_parser_member_specification_opt (cp_parser
* parser
)
23364 /* Peek at the next token. */
23365 token
= cp_lexer_peek_token (parser
->lexer
);
23366 /* If it's a `}', or EOF then we've seen all the members. */
23367 if (token
->type
== CPP_CLOSE_BRACE
23368 || token
->type
== CPP_EOF
23369 || token
->type
== CPP_PRAGMA_EOL
)
23372 /* See if this token is a keyword. */
23373 keyword
= token
->keyword
;
23377 case RID_PROTECTED
:
23379 /* Consume the access-specifier. */
23380 cp_lexer_consume_token (parser
->lexer
);
23381 /* Remember which access-specifier is active. */
23382 current_access_specifier
= token
->u
.value
;
23383 /* Look for the `:'. */
23384 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23388 /* Accept #pragmas at class scope. */
23389 if (token
->type
== CPP_PRAGMA
)
23391 cp_parser_pragma (parser
, pragma_member
, NULL
);
23395 /* Otherwise, the next construction must be a
23396 member-declaration. */
23397 cp_parser_member_declaration (parser
);
23402 /* Parse a member-declaration.
23404 member-declaration:
23405 decl-specifier-seq [opt] member-declarator-list [opt] ;
23406 function-definition ; [opt]
23407 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23409 template-declaration
23412 member-declarator-list:
23414 member-declarator-list , member-declarator
23417 declarator pure-specifier [opt]
23418 declarator constant-initializer [opt]
23419 identifier [opt] : constant-expression
23423 member-declaration:
23424 __extension__ member-declaration
23427 declarator attributes [opt] pure-specifier [opt]
23428 declarator attributes [opt] constant-initializer [opt]
23429 identifier [opt] attributes [opt] : constant-expression
23433 member-declaration:
23434 static_assert-declaration */
23437 cp_parser_member_declaration (cp_parser
* parser
)
23439 cp_decl_specifier_seq decl_specifiers
;
23440 tree prefix_attributes
;
23442 int declares_class_or_enum
;
23444 cp_token
*token
= NULL
;
23445 cp_token
*decl_spec_token_start
= NULL
;
23446 cp_token
*initializer_token_start
= NULL
;
23447 int saved_pedantic
;
23448 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23450 /* Check for the `__extension__' keyword. */
23451 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23454 cp_parser_member_declaration (parser
);
23455 /* Restore the old value of the PEDANTIC flag. */
23456 pedantic
= saved_pedantic
;
23461 /* Check for a template-declaration. */
23462 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23464 /* An explicit specialization here is an error condition, and we
23465 expect the specialization handler to detect and report this. */
23466 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23467 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23468 cp_parser_explicit_specialization (parser
);
23470 cp_parser_template_declaration (parser
, /*member_p=*/true);
23474 /* Check for a template introduction. */
23475 else if (cp_parser_template_declaration_after_export (parser
, true))
23478 /* Check for a using-declaration. */
23479 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23481 if (cxx_dialect
< cxx11
)
23483 /* Parse the using-declaration. */
23484 cp_parser_using_declaration (parser
,
23485 /*access_declaration_p=*/false);
23491 bool alias_decl_expected
;
23492 cp_parser_parse_tentatively (parser
);
23493 decl
= cp_parser_alias_declaration (parser
);
23494 /* Note that if we actually see the '=' token after the
23495 identifier, cp_parser_alias_declaration commits the
23496 tentative parse. In that case, we really expect an
23497 alias-declaration. Otherwise, we expect a using
23499 alias_decl_expected
=
23500 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23501 cp_parser_parse_definitely (parser
);
23503 if (alias_decl_expected
)
23504 finish_member_declaration (decl
);
23506 cp_parser_using_declaration (parser
,
23507 /*access_declaration_p=*/false);
23512 /* Check for @defs. */
23513 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23516 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23517 ivar
= ivar_chains
;
23521 ivar
= TREE_CHAIN (member
);
23522 TREE_CHAIN (member
) = NULL_TREE
;
23523 finish_member_declaration (member
);
23528 /* If the next token is `static_assert' we have a static assertion. */
23529 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23531 cp_parser_static_assert (parser
, /*member_p=*/true);
23535 parser
->colon_corrects_to_scope_p
= false;
23537 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23540 /* Parse the decl-specifier-seq. */
23541 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23542 cp_parser_decl_specifier_seq (parser
,
23543 CP_PARSER_FLAGS_OPTIONAL
,
23545 &declares_class_or_enum
);
23546 /* Check for an invalid type-name. */
23547 if (!decl_specifiers
.any_type_specifiers_p
23548 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23550 /* If there is no declarator, then the decl-specifier-seq should
23552 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23554 /* If there was no decl-specifier-seq, and the next token is a
23555 `;', then we have something like:
23561 Each member-declaration shall declare at least one member
23562 name of the class. */
23563 if (!decl_specifiers
.any_specifiers_p
)
23565 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23566 if (!in_system_header_at (token
->location
))
23568 gcc_rich_location
richloc (token
->location
);
23569 richloc
.add_fixit_remove ();
23570 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23577 /* See if this declaration is a friend. */
23578 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23579 /* If there were decl-specifiers, check to see if there was
23580 a class-declaration. */
23581 type
= check_tag_decl (&decl_specifiers
,
23582 /*explicit_type_instantiation_p=*/false);
23583 /* Nested classes have already been added to the class, but
23584 a `friend' needs to be explicitly registered. */
23587 /* If the `friend' keyword was present, the friend must
23588 be introduced with a class-key. */
23589 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23590 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23591 "in C++03 a class-key must be used "
23592 "when declaring a friend");
23595 template <typename T> struct A {
23596 friend struct A<T>::B;
23599 A<T>::B will be represented by a TYPENAME_TYPE, and
23600 therefore not recognized by check_tag_decl. */
23603 type
= decl_specifiers
.type
;
23604 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23605 type
= TREE_TYPE (type
);
23607 if (!type
|| !TYPE_P (type
))
23608 error_at (decl_spec_token_start
->location
,
23609 "friend declaration does not name a class or "
23612 make_friend_class (current_class_type
, type
,
23613 /*complain=*/true);
23615 /* If there is no TYPE, an error message will already have
23617 else if (!type
|| type
== error_mark_node
)
23619 /* An anonymous aggregate has to be handled specially; such
23620 a declaration really declares a data member (with a
23621 particular type), as opposed to a nested class. */
23622 else if (ANON_AGGR_TYPE_P (type
))
23625 if (decl_specifiers
.storage_class
!= sc_none
)
23626 error_at (decl_spec_token_start
->location
,
23627 "a storage class on an anonymous aggregate "
23628 "in class scope is not allowed");
23630 /* Remove constructors and such from TYPE, now that we
23631 know it is an anonymous aggregate. */
23632 fixup_anonymous_aggr (type
);
23633 /* And make the corresponding data member. */
23634 decl
= build_decl (decl_spec_token_start
->location
,
23635 FIELD_DECL
, NULL_TREE
, type
);
23636 /* Add it to the class. */
23637 finish_member_declaration (decl
);
23640 cp_parser_check_access_in_redeclaration
23642 decl_spec_token_start
->location
);
23647 bool assume_semicolon
= false;
23649 /* Clear attributes from the decl_specifiers but keep them
23650 around as prefix attributes that apply them to the entity
23652 prefix_attributes
= decl_specifiers
.attributes
;
23653 decl_specifiers
.attributes
= NULL_TREE
;
23655 /* See if these declarations will be friends. */
23656 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23658 /* Keep going until we hit the `;' at the end of the
23660 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23662 tree attributes
= NULL_TREE
;
23663 tree first_attribute
;
23665 bool named_bitfld
= false;
23667 /* Peek at the next token. */
23668 token
= cp_lexer_peek_token (parser
->lexer
);
23670 /* The following code wants to know early if it is a bit-field
23671 or some other declaration. Attributes can appear before
23672 the `:' token. Skip over them without consuming any tokens
23673 to peek if they are followed by `:'. */
23674 if (cp_next_tokens_can_be_attribute_p (parser
)
23675 || (token
->type
== CPP_NAME
23676 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23677 && (named_bitfld
= true)))
23680 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23681 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23684 /* Check for a bitfield declaration. */
23685 if (token
->type
== CPP_COLON
23686 || (token
->type
== CPP_NAME
23687 && token
== cp_lexer_peek_token (parser
->lexer
)
23688 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23689 && (named_bitfld
= true)))
23693 tree late_attributes
= NULL_TREE
;
23696 identifier
= cp_parser_identifier (parser
);
23698 identifier
= NULL_TREE
;
23700 /* Look for attributes that apply to the bitfield. */
23701 attributes
= cp_parser_attributes_opt (parser
);
23703 /* Consume the `:' token. */
23704 cp_lexer_consume_token (parser
->lexer
);
23706 /* Get the width of the bitfield. */
23707 width
= cp_parser_constant_expression (parser
, false, NULL
,
23708 cxx_dialect
>= cxx11
);
23710 /* In C++2A and as extension for C++11 and above we allow
23711 default member initializers for bit-fields. */
23712 initializer
= NULL_TREE
;
23713 if (cxx_dialect
>= cxx11
23714 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23715 || cp_lexer_next_token_is (parser
->lexer
,
23719 = cp_lexer_peek_token (parser
->lexer
)->location
;
23720 if (cxx_dialect
< cxx2a
23721 && !in_system_header_at (loc
)
23722 && identifier
!= NULL_TREE
)
23724 "default member initializers for bit-fields "
23725 "only available with -std=c++2a or "
23728 initializer
= cp_parser_save_nsdmi (parser
);
23729 if (identifier
== NULL_TREE
)
23731 error_at (loc
, "default member initializer for "
23732 "unnamed bit-field");
23733 initializer
= NULL_TREE
;
23738 /* Look for attributes that apply to the bitfield after
23739 the `:' token and width. This is where GCC used to
23740 parse attributes in the past, pedwarn if there is
23741 a std attribute. */
23742 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23743 pedwarn (input_location
, OPT_Wpedantic
,
23744 "ISO C++ allows bit-field attributes only "
23745 "before the %<:%> token");
23747 late_attributes
= cp_parser_attributes_opt (parser
);
23750 attributes
= attr_chainon (attributes
, late_attributes
);
23752 /* Remember which attributes are prefix attributes and
23754 first_attribute
= attributes
;
23755 /* Combine the attributes. */
23756 attributes
= attr_chainon (prefix_attributes
, attributes
);
23758 /* Create the bitfield declaration. */
23759 decl
= grokbitfield (identifier
23760 ? make_id_declarator (NULL_TREE
,
23765 width
, initializer
,
23770 cp_declarator
*declarator
;
23771 tree asm_specification
;
23772 int ctor_dtor_or_conv_p
;
23774 /* Parse the declarator. */
23776 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23777 &ctor_dtor_or_conv_p
,
23778 /*parenthesized_p=*/NULL
,
23782 /* If something went wrong parsing the declarator, make sure
23783 that we at least consume some tokens. */
23784 if (declarator
== cp_error_declarator
)
23786 /* Skip to the end of the statement. */
23787 cp_parser_skip_to_end_of_statement (parser
);
23788 /* If the next token is not a semicolon, that is
23789 probably because we just skipped over the body of
23790 a function. So, we consume a semicolon if
23791 present, but do not issue an error message if it
23793 if (cp_lexer_next_token_is (parser
->lexer
,
23795 cp_lexer_consume_token (parser
->lexer
);
23799 if (declares_class_or_enum
& 2)
23800 cp_parser_check_for_definition_in_return_type
23801 (declarator
, decl_specifiers
.type
,
23802 decl_specifiers
.locations
[ds_type_spec
]);
23804 /* Look for an asm-specification. */
23805 asm_specification
= cp_parser_asm_specification_opt (parser
);
23806 /* Look for attributes that apply to the declaration. */
23807 attributes
= cp_parser_attributes_opt (parser
);
23808 /* Remember which attributes are prefix attributes and
23810 first_attribute
= attributes
;
23811 /* Combine the attributes. */
23812 attributes
= attr_chainon (prefix_attributes
, attributes
);
23814 /* If it's an `=', then we have a constant-initializer or a
23815 pure-specifier. It is not correct to parse the
23816 initializer before registering the member declaration
23817 since the member declaration should be in scope while
23818 its initializer is processed. However, the rest of the
23819 front end does not yet provide an interface that allows
23820 us to handle this correctly. */
23821 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23825 A pure-specifier shall be used only in the declaration of
23826 a virtual function.
23828 A member-declarator can contain a constant-initializer
23829 only if it declares a static member of integral or
23832 Therefore, if the DECLARATOR is for a function, we look
23833 for a pure-specifier; otherwise, we look for a
23834 constant-initializer. When we call `grokfield', it will
23835 perform more stringent semantics checks. */
23836 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23837 if (function_declarator_p (declarator
)
23838 || (decl_specifiers
.type
23839 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23840 && declarator
->kind
== cdk_id
23841 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23842 == FUNCTION_TYPE
)))
23843 initializer
= cp_parser_pure_specifier (parser
);
23844 else if (decl_specifiers
.storage_class
!= sc_static
)
23845 initializer
= cp_parser_save_nsdmi (parser
);
23846 else if (cxx_dialect
>= cxx11
)
23849 /* Don't require a constant rvalue in C++11, since we
23850 might want a reference constant. We'll enforce
23851 constancy later. */
23852 cp_lexer_consume_token (parser
->lexer
);
23853 /* Parse the initializer. */
23854 initializer
= cp_parser_initializer_clause (parser
,
23858 /* Parse the initializer. */
23859 initializer
= cp_parser_constant_initializer (parser
);
23861 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23862 && !function_declarator_p (declarator
))
23865 if (decl_specifiers
.storage_class
!= sc_static
)
23866 initializer
= cp_parser_save_nsdmi (parser
);
23868 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23870 /* Otherwise, there is no initializer. */
23872 initializer
= NULL_TREE
;
23874 /* See if we are probably looking at a function
23875 definition. We are certainly not looking at a
23876 member-declarator. Calling `grokfield' has
23877 side-effects, so we must not do it unless we are sure
23878 that we are looking at a member-declarator. */
23879 if (cp_parser_token_starts_function_definition_p
23880 (cp_lexer_peek_token (parser
->lexer
)))
23882 /* The grammar does not allow a pure-specifier to be
23883 used when a member function is defined. (It is
23884 possible that this fact is an oversight in the
23885 standard, since a pure function may be defined
23886 outside of the class-specifier. */
23887 if (initializer
&& initializer_token_start
)
23888 error_at (initializer_token_start
->location
,
23889 "pure-specifier on function-definition");
23890 decl
= cp_parser_save_member_function_body (parser
,
23894 if (parser
->fully_implicit_function_template_p
)
23895 decl
= finish_fully_implicit_template (parser
, decl
);
23896 /* If the member was not a friend, declare it here. */
23898 finish_member_declaration (decl
);
23899 /* Peek at the next token. */
23900 token
= cp_lexer_peek_token (parser
->lexer
);
23901 /* If the next token is a semicolon, consume it. */
23902 if (token
->type
== CPP_SEMICOLON
)
23904 location_t semicolon_loc
23905 = cp_lexer_consume_token (parser
->lexer
)->location
;
23906 gcc_rich_location
richloc (semicolon_loc
);
23907 richloc
.add_fixit_remove ();
23908 warning_at (&richloc
, OPT_Wextra_semi
,
23909 "extra %<;%> after in-class "
23910 "function definition");
23915 if (declarator
->kind
== cdk_function
)
23916 declarator
->id_loc
= token
->location
;
23917 /* Create the declaration. */
23918 decl
= grokfield (declarator
, &decl_specifiers
,
23919 initializer
, /*init_const_expr_p=*/true,
23920 asm_specification
, attributes
);
23921 if (parser
->fully_implicit_function_template_p
)
23924 finish_fully_implicit_template (parser
, 0);
23926 decl
= finish_fully_implicit_template (parser
, decl
);
23930 cp_finalize_omp_declare_simd (parser
, decl
);
23931 cp_finalize_oacc_routine (parser
, decl
, false);
23933 /* Reset PREFIX_ATTRIBUTES. */
23934 if (attributes
!= error_mark_node
)
23936 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
23937 attributes
= TREE_CHAIN (attributes
);
23939 TREE_CHAIN (attributes
) = NULL_TREE
;
23942 /* If there is any qualification still in effect, clear it
23943 now; we will be starting fresh with the next declarator. */
23944 parser
->scope
= NULL_TREE
;
23945 parser
->qualifying_scope
= NULL_TREE
;
23946 parser
->object_scope
= NULL_TREE
;
23947 /* If it's a `,', then there are more declarators. */
23948 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23950 cp_lexer_consume_token (parser
->lexer
);
23951 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23953 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23954 gcc_rich_location
richloc (token
->location
);
23955 richloc
.add_fixit_remove ();
23956 error_at (&richloc
, "stray %<,%> at end of "
23957 "member declaration");
23960 /* If the next token isn't a `;', then we have a parse error. */
23961 else if (cp_lexer_next_token_is_not (parser
->lexer
,
23964 /* The next token might be a ways away from where the
23965 actual semicolon is missing. Find the previous token
23966 and use that for our error position. */
23967 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
23968 gcc_rich_location
richloc (token
->location
);
23969 richloc
.add_fixit_insert_after (";");
23970 error_at (&richloc
, "expected %<;%> at end of "
23971 "member declaration");
23973 /* Assume that the user meant to provide a semicolon. If
23974 we were to cp_parser_skip_to_end_of_statement, we might
23975 skip to a semicolon inside a member function definition
23976 and issue nonsensical error messages. */
23977 assume_semicolon
= true;
23982 /* Add DECL to the list of members. */
23984 /* Explicitly include, eg, NSDMIs, for better error
23985 recovery (c++/58650). */
23986 || !DECL_DECLARES_FUNCTION_P (decl
))
23987 finish_member_declaration (decl
);
23989 if (TREE_CODE (decl
) == FUNCTION_DECL
)
23990 cp_parser_save_default_args (parser
, decl
);
23991 else if (TREE_CODE (decl
) == FIELD_DECL
23992 && DECL_INITIAL (decl
))
23993 /* Add DECL to the queue of NSDMI to be parsed later. */
23994 vec_safe_push (unparsed_nsdmis
, decl
);
23997 if (assume_semicolon
)
24002 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24004 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24007 /* Parse a pure-specifier.
24012 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24013 Otherwise, ERROR_MARK_NODE is returned. */
24016 cp_parser_pure_specifier (cp_parser
* parser
)
24020 /* Look for the `=' token. */
24021 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24022 return error_mark_node
;
24023 /* Look for the `0' token. */
24024 token
= cp_lexer_peek_token (parser
->lexer
);
24026 if (token
->type
== CPP_EOF
24027 || token
->type
== CPP_PRAGMA_EOL
)
24028 return error_mark_node
;
24030 cp_lexer_consume_token (parser
->lexer
);
24032 /* Accept = default or = delete in c++0x mode. */
24033 if (token
->keyword
== RID_DEFAULT
24034 || token
->keyword
== RID_DELETE
)
24036 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24037 return token
->u
.value
;
24040 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24041 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24043 cp_parser_error (parser
,
24044 "invalid pure specifier (only %<= 0%> is allowed)");
24045 cp_parser_skip_to_end_of_statement (parser
);
24046 return error_mark_node
;
24048 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24050 error_at (token
->location
, "templates may not be %<virtual%>");
24051 return error_mark_node
;
24054 return integer_zero_node
;
24057 /* Parse a constant-initializer.
24059 constant-initializer:
24060 = constant-expression
24062 Returns a representation of the constant-expression. */
24065 cp_parser_constant_initializer (cp_parser
* parser
)
24067 /* Look for the `=' token. */
24068 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24069 return error_mark_node
;
24071 /* It is invalid to write:
24073 struct S { static const int i = { 7 }; };
24076 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24078 cp_parser_error (parser
,
24079 "a brace-enclosed initializer is not allowed here");
24080 /* Consume the opening brace. */
24081 matching_braces braces
;
24082 braces
.consume_open (parser
);
24083 /* Skip the initializer. */
24084 cp_parser_skip_to_closing_brace (parser
);
24085 /* Look for the trailing `}'. */
24086 braces
.require_close (parser
);
24088 return error_mark_node
;
24091 return cp_parser_constant_expression (parser
);
24094 /* Derived classes [gram.class.derived] */
24096 /* Parse a base-clause.
24099 : base-specifier-list
24101 base-specifier-list:
24102 base-specifier ... [opt]
24103 base-specifier-list , base-specifier ... [opt]
24105 Returns a TREE_LIST representing the base-classes, in the order in
24106 which they were declared. The representation of each node is as
24107 described by cp_parser_base_specifier.
24109 In the case that no bases are specified, this function will return
24110 NULL_TREE, not ERROR_MARK_NODE. */
24113 cp_parser_base_clause (cp_parser
* parser
)
24115 tree bases
= NULL_TREE
;
24117 /* Look for the `:' that begins the list. */
24118 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24120 /* Scan the base-specifier-list. */
24125 bool pack_expansion_p
= false;
24127 /* Look for the base-specifier. */
24128 base
= cp_parser_base_specifier (parser
);
24129 /* Look for the (optional) ellipsis. */
24130 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24132 /* Consume the `...'. */
24133 cp_lexer_consume_token (parser
->lexer
);
24135 pack_expansion_p
= true;
24138 /* Add BASE to the front of the list. */
24139 if (base
&& base
!= error_mark_node
)
24141 if (pack_expansion_p
)
24142 /* Make this a pack expansion type. */
24143 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24145 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24147 TREE_CHAIN (base
) = bases
;
24151 /* Peek at the next token. */
24152 token
= cp_lexer_peek_token (parser
->lexer
);
24153 /* If it's not a comma, then the list is complete. */
24154 if (token
->type
!= CPP_COMMA
)
24156 /* Consume the `,'. */
24157 cp_lexer_consume_token (parser
->lexer
);
24160 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24161 base class had a qualified name. However, the next name that
24162 appears is certainly not qualified. */
24163 parser
->scope
= NULL_TREE
;
24164 parser
->qualifying_scope
= NULL_TREE
;
24165 parser
->object_scope
= NULL_TREE
;
24167 return nreverse (bases
);
24170 /* Parse a base-specifier.
24173 :: [opt] nested-name-specifier [opt] class-name
24174 virtual access-specifier [opt] :: [opt] nested-name-specifier
24176 access-specifier virtual [opt] :: [opt] nested-name-specifier
24179 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24180 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24181 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24182 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24185 cp_parser_base_specifier (cp_parser
* parser
)
24189 bool virtual_p
= false;
24190 bool duplicate_virtual_error_issued_p
= false;
24191 bool duplicate_access_error_issued_p
= false;
24192 bool class_scope_p
, template_p
;
24193 tree access
= access_default_node
;
24196 /* Process the optional `virtual' and `access-specifier'. */
24199 /* Peek at the next token. */
24200 token
= cp_lexer_peek_token (parser
->lexer
);
24201 /* Process `virtual'. */
24202 switch (token
->keyword
)
24205 /* If `virtual' appears more than once, issue an error. */
24206 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24208 cp_parser_error (parser
,
24209 "%<virtual%> specified more than once in base-specifier");
24210 duplicate_virtual_error_issued_p
= true;
24215 /* Consume the `virtual' token. */
24216 cp_lexer_consume_token (parser
->lexer
);
24221 case RID_PROTECTED
:
24223 /* If more than one access specifier appears, issue an
24225 if (access
!= access_default_node
24226 && !duplicate_access_error_issued_p
)
24228 cp_parser_error (parser
,
24229 "more than one access specifier in base-specifier");
24230 duplicate_access_error_issued_p
= true;
24233 access
= ridpointers
[(int) token
->keyword
];
24235 /* Consume the access-specifier. */
24236 cp_lexer_consume_token (parser
->lexer
);
24245 /* It is not uncommon to see programs mechanically, erroneously, use
24246 the 'typename' keyword to denote (dependent) qualified types
24247 as base classes. */
24248 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24250 token
= cp_lexer_peek_token (parser
->lexer
);
24251 if (!processing_template_decl
)
24252 error_at (token
->location
,
24253 "keyword %<typename%> not allowed outside of templates");
24255 error_at (token
->location
,
24256 "keyword %<typename%> not allowed in this context "
24257 "(the base class is implicitly a type)");
24258 cp_lexer_consume_token (parser
->lexer
);
24261 /* Look for the optional `::' operator. */
24262 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24263 /* Look for the nested-name-specifier. The simplest way to
24268 The keyword `typename' is not permitted in a base-specifier or
24269 mem-initializer; in these contexts a qualified name that
24270 depends on a template-parameter is implicitly assumed to be a
24273 is to pretend that we have seen the `typename' keyword at this
24275 cp_parser_nested_name_specifier_opt (parser
,
24276 /*typename_keyword_p=*/true,
24277 /*check_dependency_p=*/true,
24279 /*is_declaration=*/true);
24280 /* If the base class is given by a qualified name, assume that names
24281 we see are type names or templates, as appropriate. */
24282 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24283 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24286 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24287 /* DR 950 allows decltype as a base-specifier. */
24288 type
= cp_parser_decltype (parser
);
24291 /* Otherwise, look for the class-name. */
24292 type
= cp_parser_class_name (parser
,
24296 /*check_dependency_p=*/true,
24297 /*class_head_p=*/false,
24298 /*is_declaration=*/true);
24299 type
= TREE_TYPE (type
);
24302 if (type
== error_mark_node
)
24303 return error_mark_node
;
24305 return finish_base_specifier (type
, access
, virtual_p
);
24308 /* Exception handling [gram.exception] */
24310 /* Parse an (optional) noexcept-specification.
24312 noexcept-specification:
24313 noexcept ( constant-expression ) [opt]
24315 If no noexcept-specification is present, returns NULL_TREE.
24316 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24317 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24318 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24319 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24320 in which case a boolean condition is returned instead. */
24323 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24324 bool require_constexpr
,
24325 bool* consumed_expr
,
24329 const char *saved_message
;
24331 /* Peek at the next token. */
24332 token
= cp_lexer_peek_token (parser
->lexer
);
24334 /* Is it a noexcept-specification? */
24335 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24338 cp_lexer_consume_token (parser
->lexer
);
24340 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24342 matching_parens parens
;
24343 parens
.consume_open (parser
);
24345 if (require_constexpr
)
24347 /* Types may not be defined in an exception-specification. */
24348 saved_message
= parser
->type_definition_forbidden_message
;
24349 parser
->type_definition_forbidden_message
24350 = G_("types may not be defined in an exception-specification");
24352 expr
= cp_parser_constant_expression (parser
);
24354 /* Restore the saved message. */
24355 parser
->type_definition_forbidden_message
= saved_message
;
24359 expr
= cp_parser_expression (parser
);
24360 *consumed_expr
= true;
24363 parens
.require_close (parser
);
24367 expr
= boolean_true_node
;
24368 if (!require_constexpr
)
24369 *consumed_expr
= false;
24372 /* We cannot build a noexcept-spec right away because this will check
24373 that expr is a constexpr. */
24375 return build_noexcept_spec (expr
, tf_warning_or_error
);
24383 /* Parse an (optional) exception-specification.
24385 exception-specification:
24386 throw ( type-id-list [opt] )
24388 Returns a TREE_LIST representing the exception-specification. The
24389 TREE_VALUE of each node is a type. */
24392 cp_parser_exception_specification_opt (cp_parser
* parser
)
24396 const char *saved_message
;
24398 /* Peek at the next token. */
24399 token
= cp_lexer_peek_token (parser
->lexer
);
24401 /* Is it a noexcept-specification? */
24402 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24404 if (type_id_list
!= NULL_TREE
)
24405 return type_id_list
;
24407 /* If it's not `throw', then there's no exception-specification. */
24408 if (!cp_parser_is_keyword (token
, RID_THROW
))
24411 location_t loc
= token
->location
;
24413 /* Consume the `throw'. */
24414 cp_lexer_consume_token (parser
->lexer
);
24416 /* Look for the `('. */
24417 matching_parens parens
;
24418 parens
.require_open (parser
);
24420 /* Peek at the next token. */
24421 token
= cp_lexer_peek_token (parser
->lexer
);
24422 /* If it's not a `)', then there is a type-id-list. */
24423 if (token
->type
!= CPP_CLOSE_PAREN
)
24425 /* Types may not be defined in an exception-specification. */
24426 saved_message
= parser
->type_definition_forbidden_message
;
24427 parser
->type_definition_forbidden_message
24428 = G_("types may not be defined in an exception-specification");
24429 /* Parse the type-id-list. */
24430 type_id_list
= cp_parser_type_id_list (parser
);
24431 /* Restore the saved message. */
24432 parser
->type_definition_forbidden_message
= saved_message
;
24434 if (cxx_dialect
>= cxx17
)
24436 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24438 type_id_list
= NULL_TREE
;
24440 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24441 warning_at (loc
, OPT_Wdeprecated
,
24442 "dynamic exception specifications are deprecated in "
24445 /* In C++17, throw() is equivalent to noexcept (true). throw()
24446 is deprecated in C++11 and above as well, but is still widely used,
24447 so don't warn about it yet. */
24448 else if (cxx_dialect
>= cxx17
)
24449 type_id_list
= noexcept_true_spec
;
24451 type_id_list
= empty_except_spec
;
24453 /* Look for the `)'. */
24454 parens
.require_close (parser
);
24456 return type_id_list
;
24459 /* Parse an (optional) type-id-list.
24463 type-id-list , type-id ... [opt]
24465 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24466 in the order that the types were presented. */
24469 cp_parser_type_id_list (cp_parser
* parser
)
24471 tree types
= NULL_TREE
;
24478 token
= cp_lexer_peek_token (parser
->lexer
);
24480 /* Get the next type-id. */
24481 type
= cp_parser_type_id (parser
);
24482 /* Check for invalid 'auto'. */
24483 if (flag_concepts
&& type_uses_auto (type
))
24485 error_at (token
->location
,
24486 "invalid use of %<auto%> in exception-specification");
24487 type
= error_mark_node
;
24489 /* Parse the optional ellipsis. */
24490 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24492 /* Consume the `...'. */
24493 cp_lexer_consume_token (parser
->lexer
);
24495 /* Turn the type into a pack expansion expression. */
24496 type
= make_pack_expansion (type
);
24498 /* Add it to the list. */
24499 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24500 /* Peek at the next token. */
24501 token
= cp_lexer_peek_token (parser
->lexer
);
24502 /* If it is not a `,', we are done. */
24503 if (token
->type
!= CPP_COMMA
)
24505 /* Consume the `,'. */
24506 cp_lexer_consume_token (parser
->lexer
);
24509 return nreverse (types
);
24512 /* Parse a try-block.
24515 try compound-statement handler-seq */
24518 cp_parser_try_block (cp_parser
* parser
)
24522 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24523 if (parser
->in_function_body
24524 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24525 error ("%<try%> in %<constexpr%> function");
24527 try_block
= begin_try_block ();
24528 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24529 finish_try_block (try_block
);
24530 cp_parser_handler_seq (parser
);
24531 finish_handler_sequence (try_block
);
24536 /* Parse a function-try-block.
24538 function-try-block:
24539 try ctor-initializer [opt] function-body handler-seq */
24542 cp_parser_function_try_block (cp_parser
* parser
)
24544 tree compound_stmt
;
24547 /* Look for the `try' keyword. */
24548 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24550 /* Let the rest of the front end know where we are. */
24551 try_block
= begin_function_try_block (&compound_stmt
);
24552 /* Parse the function-body. */
24553 cp_parser_ctor_initializer_opt_and_function_body
24554 (parser
, /*in_function_try_block=*/true);
24555 /* We're done with the `try' part. */
24556 finish_function_try_block (try_block
);
24557 /* Parse the handlers. */
24558 cp_parser_handler_seq (parser
);
24559 /* We're done with the handlers. */
24560 finish_function_handler_sequence (try_block
, compound_stmt
);
24563 /* Parse a handler-seq.
24566 handler handler-seq [opt] */
24569 cp_parser_handler_seq (cp_parser
* parser
)
24575 /* Parse the handler. */
24576 cp_parser_handler (parser
);
24577 /* Peek at the next token. */
24578 token
= cp_lexer_peek_token (parser
->lexer
);
24579 /* If it's not `catch' then there are no more handlers. */
24580 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24585 /* Parse a handler.
24588 catch ( exception-declaration ) compound-statement */
24591 cp_parser_handler (cp_parser
* parser
)
24596 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24597 handler
= begin_handler ();
24598 matching_parens parens
;
24599 parens
.require_open (parser
);
24600 declaration
= cp_parser_exception_declaration (parser
);
24601 finish_handler_parms (declaration
, handler
);
24602 parens
.require_close (parser
);
24603 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24604 finish_handler (handler
);
24607 /* Parse an exception-declaration.
24609 exception-declaration:
24610 type-specifier-seq declarator
24611 type-specifier-seq abstract-declarator
24615 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24616 ellipsis variant is used. */
24619 cp_parser_exception_declaration (cp_parser
* parser
)
24621 cp_decl_specifier_seq type_specifiers
;
24622 cp_declarator
*declarator
;
24623 const char *saved_message
;
24625 /* If it's an ellipsis, it's easy to handle. */
24626 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24628 /* Consume the `...' token. */
24629 cp_lexer_consume_token (parser
->lexer
);
24633 /* Types may not be defined in exception-declarations. */
24634 saved_message
= parser
->type_definition_forbidden_message
;
24635 parser
->type_definition_forbidden_message
24636 = G_("types may not be defined in exception-declarations");
24638 /* Parse the type-specifier-seq. */
24639 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24640 /*is_trailing_return=*/false,
24642 /* If it's a `)', then there is no declarator. */
24643 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24646 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24647 /*ctor_dtor_or_conv_p=*/NULL
,
24648 /*parenthesized_p=*/NULL
,
24649 /*member_p=*/false,
24650 /*friend_p=*/false);
24652 /* Restore the saved message. */
24653 parser
->type_definition_forbidden_message
= saved_message
;
24655 if (!type_specifiers
.any_specifiers_p
)
24656 return error_mark_node
;
24658 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24661 /* Parse a throw-expression.
24664 throw assignment-expression [opt]
24666 Returns a THROW_EXPR representing the throw-expression. */
24669 cp_parser_throw_expression (cp_parser
* parser
)
24674 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24675 token
= cp_lexer_peek_token (parser
->lexer
);
24676 /* Figure out whether or not there is an assignment-expression
24677 following the "throw" keyword. */
24678 if (token
->type
== CPP_COMMA
24679 || token
->type
== CPP_SEMICOLON
24680 || token
->type
== CPP_CLOSE_PAREN
24681 || token
->type
== CPP_CLOSE_SQUARE
24682 || token
->type
== CPP_CLOSE_BRACE
24683 || token
->type
== CPP_COLON
)
24684 expression
= NULL_TREE
;
24686 expression
= cp_parser_assignment_expression (parser
);
24688 return build_throw (expression
);
24691 /* GNU Extensions */
24693 /* Parse an (optional) asm-specification.
24696 asm ( string-literal )
24698 If the asm-specification is present, returns a STRING_CST
24699 corresponding to the string-literal. Otherwise, returns
24703 cp_parser_asm_specification_opt (cp_parser
* parser
)
24706 tree asm_specification
;
24708 /* Peek at the next token. */
24709 token
= cp_lexer_peek_token (parser
->lexer
);
24710 /* If the next token isn't the `asm' keyword, then there's no
24711 asm-specification. */
24712 if (!cp_parser_is_keyword (token
, RID_ASM
))
24715 /* Consume the `asm' token. */
24716 cp_lexer_consume_token (parser
->lexer
);
24717 /* Look for the `('. */
24718 matching_parens parens
;
24719 parens
.require_open (parser
);
24721 /* Look for the string-literal. */
24722 asm_specification
= cp_parser_string_literal (parser
, false, false);
24724 /* Look for the `)'. */
24725 parens
.require_close (parser
);
24727 return asm_specification
;
24730 /* Parse an asm-operand-list.
24734 asm-operand-list , asm-operand
24737 string-literal ( expression )
24738 [ string-literal ] string-literal ( expression )
24740 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24741 each node is the expression. The TREE_PURPOSE is itself a
24742 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24743 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24744 is a STRING_CST for the string literal before the parenthesis. Returns
24745 ERROR_MARK_NODE if any of the operands are invalid. */
24748 cp_parser_asm_operand_list (cp_parser
* parser
)
24750 tree asm_operands
= NULL_TREE
;
24751 bool invalid_operands
= false;
24755 tree string_literal
;
24759 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24761 /* Consume the `[' token. */
24762 cp_lexer_consume_token (parser
->lexer
);
24763 /* Read the operand name. */
24764 name
= cp_parser_identifier (parser
);
24765 if (name
!= error_mark_node
)
24766 name
= build_string (IDENTIFIER_LENGTH (name
),
24767 IDENTIFIER_POINTER (name
));
24768 /* Look for the closing `]'. */
24769 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24773 /* Look for the string-literal. */
24774 string_literal
= cp_parser_string_literal (parser
, false, false);
24776 /* Look for the `('. */
24777 matching_parens parens
;
24778 parens
.require_open (parser
);
24779 /* Parse the expression. */
24780 expression
= cp_parser_expression (parser
);
24781 /* Look for the `)'. */
24782 parens
.require_close (parser
);
24784 if (name
== error_mark_node
24785 || string_literal
== error_mark_node
24786 || expression
== error_mark_node
)
24787 invalid_operands
= true;
24789 /* Add this operand to the list. */
24790 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24793 /* If the next token is not a `,', there are no more
24795 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24797 /* Consume the `,'. */
24798 cp_lexer_consume_token (parser
->lexer
);
24801 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24804 /* Parse an asm-clobber-list.
24808 asm-clobber-list , string-literal
24810 Returns a TREE_LIST, indicating the clobbers in the order that they
24811 appeared. The TREE_VALUE of each node is a STRING_CST. */
24814 cp_parser_asm_clobber_list (cp_parser
* parser
)
24816 tree clobbers
= NULL_TREE
;
24820 tree string_literal
;
24822 /* Look for the string literal. */
24823 string_literal
= cp_parser_string_literal (parser
, false, false);
24824 /* Add it to the list. */
24825 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24826 /* If the next token is not a `,', then the list is
24828 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24830 /* Consume the `,' token. */
24831 cp_lexer_consume_token (parser
->lexer
);
24837 /* Parse an asm-label-list.
24841 asm-label-list , identifier
24843 Returns a TREE_LIST, indicating the labels in the order that they
24844 appeared. The TREE_VALUE of each node is a label. */
24847 cp_parser_asm_label_list (cp_parser
* parser
)
24849 tree labels
= NULL_TREE
;
24853 tree identifier
, label
, name
;
24855 /* Look for the identifier. */
24856 identifier
= cp_parser_identifier (parser
);
24857 if (!error_operand_p (identifier
))
24859 label
= lookup_label (identifier
);
24860 if (TREE_CODE (label
) == LABEL_DECL
)
24862 TREE_USED (label
) = 1;
24863 check_goto (label
);
24864 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24865 IDENTIFIER_POINTER (identifier
));
24866 labels
= tree_cons (name
, label
, labels
);
24869 /* If the next token is not a `,', then the list is
24871 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24873 /* Consume the `,' token. */
24874 cp_lexer_consume_token (parser
->lexer
);
24877 return nreverse (labels
);
24880 /* Return TRUE iff the next tokens in the stream are possibly the
24881 beginning of a GNU extension attribute. */
24884 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24886 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24889 /* Return TRUE iff the next tokens in the stream are possibly the
24890 beginning of a standard C++-11 attribute specifier. */
24893 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24895 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24898 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24899 beginning of a standard C++-11 attribute specifier. */
24902 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24904 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24906 return (cxx_dialect
>= cxx11
24907 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24908 || (token
->type
== CPP_OPEN_SQUARE
24909 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24910 && token
->type
== CPP_OPEN_SQUARE
)));
24913 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24914 beginning of a GNU extension attribute. */
24917 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
24919 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24921 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
24924 /* Return true iff the next tokens can be the beginning of either a
24925 GNU attribute list, or a standard C++11 attribute sequence. */
24928 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
24930 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
24931 || cp_next_tokens_can_be_std_attribute_p (parser
));
24934 /* Return true iff the next Nth tokens can be the beginning of either
24935 a GNU attribute list, or a standard C++11 attribute sequence. */
24938 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
24940 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
24941 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
24944 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24945 of GNU attributes, or return NULL. */
24948 cp_parser_attributes_opt (cp_parser
*parser
)
24950 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
24951 return cp_parser_gnu_attributes_opt (parser
);
24952 return cp_parser_std_attribute_spec_seq (parser
);
24955 /* Parse an (optional) series of attributes.
24958 attributes attribute
24961 __attribute__ (( attribute-list [opt] ))
24963 The return value is as for cp_parser_gnu_attribute_list. */
24966 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
24968 tree attributes
= NULL_TREE
;
24970 temp_override
<bool> cleanup
24971 (parser
->auto_is_implicit_function_template_parm_p
, false);
24976 tree attribute_list
;
24979 /* Peek at the next token. */
24980 token
= cp_lexer_peek_token (parser
->lexer
);
24981 /* If it's not `__attribute__', then we're done. */
24982 if (token
->keyword
!= RID_ATTRIBUTE
)
24985 /* Consume the `__attribute__' keyword. */
24986 cp_lexer_consume_token (parser
->lexer
);
24987 /* Look for the two `(' tokens. */
24988 matching_parens outer_parens
;
24989 outer_parens
.require_open (parser
);
24990 matching_parens inner_parens
;
24991 inner_parens
.require_open (parser
);
24993 /* Peek at the next token. */
24994 token
= cp_lexer_peek_token (parser
->lexer
);
24995 if (token
->type
!= CPP_CLOSE_PAREN
)
24996 /* Parse the attribute-list. */
24997 attribute_list
= cp_parser_gnu_attribute_list (parser
);
24999 /* If the next token is a `)', then there is no attribute
25001 attribute_list
= NULL
;
25003 /* Look for the two `)' tokens. */
25004 if (!inner_parens
.require_close (parser
))
25006 if (!outer_parens
.require_close (parser
))
25009 cp_parser_skip_to_end_of_statement (parser
);
25011 /* Add these new attributes to the list. */
25012 attributes
= attr_chainon (attributes
, attribute_list
);
25018 /* Parse a GNU attribute-list.
25022 attribute-list , attribute
25026 identifier ( identifier )
25027 identifier ( identifier , expression-list )
25028 identifier ( expression-list )
25030 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25031 to an attribute. The TREE_PURPOSE of each node is the identifier
25032 indicating which attribute is in use. The TREE_VALUE represents
25033 the arguments, if any. */
25036 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25038 tree attribute_list
= NULL_TREE
;
25039 bool save_translate_strings_p
= parser
->translate_strings_p
;
25041 parser
->translate_strings_p
= false;
25048 /* Look for the identifier. We also allow keywords here; for
25049 example `__attribute__ ((const))' is legal. */
25050 token
= cp_lexer_peek_token (parser
->lexer
);
25051 if (token
->type
== CPP_NAME
25052 || token
->type
== CPP_KEYWORD
)
25054 tree arguments
= NULL_TREE
;
25056 /* Consume the token, but save it since we need it for the
25057 SIMD enabled function parsing. */
25058 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25060 /* Save away the identifier that indicates which attribute
25062 identifier
= (token
->type
== CPP_KEYWORD
)
25063 /* For keywords, use the canonical spelling, not the
25064 parsed identifier. */
25065 ? ridpointers
[(int) token
->keyword
]
25066 : id_token
->u
.value
;
25068 identifier
= canonicalize_attr_name (identifier
);
25069 attribute
= build_tree_list (identifier
, NULL_TREE
);
25071 /* Peek at the next token. */
25072 token
= cp_lexer_peek_token (parser
->lexer
);
25073 /* If it's an `(', then parse the attribute arguments. */
25074 if (token
->type
== CPP_OPEN_PAREN
)
25076 vec
<tree
, va_gc
> *vec
;
25077 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25078 ? id_attr
: normal_attr
);
25079 vec
= cp_parser_parenthesized_expression_list
25080 (parser
, attr_flag
, /*cast_p=*/false,
25081 /*allow_expansion_p=*/false,
25082 /*non_constant_p=*/NULL
);
25084 arguments
= error_mark_node
;
25087 arguments
= build_tree_list_vec (vec
);
25088 release_tree_vector (vec
);
25090 /* Save the arguments away. */
25091 TREE_VALUE (attribute
) = arguments
;
25094 if (arguments
!= error_mark_node
)
25096 /* Add this attribute to the list. */
25097 TREE_CHAIN (attribute
) = attribute_list
;
25098 attribute_list
= attribute
;
25101 token
= cp_lexer_peek_token (parser
->lexer
);
25103 /* Now, look for more attributes. If the next token isn't a
25104 `,', we're done. */
25105 if (token
->type
!= CPP_COMMA
)
25108 /* Consume the comma and keep going. */
25109 cp_lexer_consume_token (parser
->lexer
);
25111 parser
->translate_strings_p
= save_translate_strings_p
;
25113 /* We built up the list in reverse order. */
25114 return nreverse (attribute_list
);
25117 /* Parse a standard C++11 attribute.
25119 The returned representation is a TREE_LIST which TREE_PURPOSE is
25120 the scoped name of the attribute, and the TREE_VALUE is its
25123 Note that the scoped name of the attribute is itself a TREE_LIST
25124 which TREE_PURPOSE is the namespace of the attribute, and
25125 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25126 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25127 and which TREE_PURPOSE is directly the attribute name.
25129 Clients of the attribute code should use get_attribute_namespace
25130 and get_attribute_name to get the actual namespace and name of
25131 attributes, regardless of their being GNU or C++11 attributes.
25134 attribute-token attribute-argument-clause [opt]
25138 attribute-scoped-token
25140 attribute-scoped-token:
25141 attribute-namespace :: identifier
25143 attribute-namespace:
25146 attribute-argument-clause:
25147 ( balanced-token-seq )
25149 balanced-token-seq:
25150 balanced-token [opt]
25151 balanced-token-seq balanced-token
25154 ( balanced-token-seq )
25155 [ balanced-token-seq ]
25156 { balanced-token-seq }. */
25159 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25161 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25164 temp_override
<bool> cleanup
25165 (parser
->auto_is_implicit_function_template_parm_p
, false);
25167 /* First, parse name of the attribute, a.k.a attribute-token. */
25169 token
= cp_lexer_peek_token (parser
->lexer
);
25170 if (token
->type
== CPP_NAME
)
25171 attr_id
= token
->u
.value
;
25172 else if (token
->type
== CPP_KEYWORD
)
25173 attr_id
= ridpointers
[(int) token
->keyword
];
25174 else if (token
->flags
& NAMED_OP
)
25175 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25177 if (attr_id
== NULL_TREE
)
25180 cp_lexer_consume_token (parser
->lexer
);
25182 token
= cp_lexer_peek_token (parser
->lexer
);
25183 if (token
->type
== CPP_SCOPE
)
25185 /* We are seeing a scoped attribute token. */
25187 cp_lexer_consume_token (parser
->lexer
);
25189 error_at (token
->location
, "attribute using prefix used together "
25190 "with scoped attribute token");
25193 token
= cp_lexer_consume_token (parser
->lexer
);
25194 if (token
->type
== CPP_NAME
)
25195 attr_id
= token
->u
.value
;
25196 else if (token
->type
== CPP_KEYWORD
)
25197 attr_id
= ridpointers
[(int) token
->keyword
];
25198 else if (token
->flags
& NAMED_OP
)
25199 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25202 error_at (token
->location
,
25203 "expected an identifier for the attribute name");
25204 return error_mark_node
;
25207 attr_id
= canonicalize_attr_name (attr_id
);
25208 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25210 token
= cp_lexer_peek_token (parser
->lexer
);
25213 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25217 attr_id
= canonicalize_attr_name (attr_id
);
25218 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25220 /* C++11 noreturn attribute is equivalent to GNU's. */
25221 if (is_attribute_p ("noreturn", attr_id
))
25222 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25223 /* C++14 deprecated attribute is equivalent to GNU's. */
25224 else if (is_attribute_p ("deprecated", attr_id
))
25225 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25226 /* C++17 fallthrough attribute is equivalent to GNU's. */
25227 else if (is_attribute_p ("fallthrough", attr_id
))
25228 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25229 /* Transactional Memory TS optimize_for_synchronized attribute is
25230 equivalent to GNU transaction_callable. */
25231 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25232 TREE_PURPOSE (attribute
)
25233 = get_identifier ("transaction_callable");
25234 /* Transactional Memory attributes are GNU attributes. */
25235 else if (tm_attr_to_mask (attr_id
))
25236 TREE_PURPOSE (attribute
) = attr_id
;
25239 /* Now parse the optional argument clause of the attribute. */
25241 if (token
->type
!= CPP_OPEN_PAREN
)
25245 vec
<tree
, va_gc
> *vec
;
25246 int attr_flag
= normal_attr
;
25248 if (attr_ns
== get_identifier ("gnu")
25249 && attribute_takes_identifier_p (attr_id
))
25250 /* A GNU attribute that takes an identifier in parameter. */
25251 attr_flag
= id_attr
;
25253 vec
= cp_parser_parenthesized_expression_list
25254 (parser
, attr_flag
, /*cast_p=*/false,
25255 /*allow_expansion_p=*/true,
25256 /*non_constant_p=*/NULL
);
25258 arguments
= error_mark_node
;
25261 arguments
= build_tree_list_vec (vec
);
25262 release_tree_vector (vec
);
25265 if (arguments
== error_mark_node
)
25266 attribute
= error_mark_node
;
25268 TREE_VALUE (attribute
) = arguments
;
25274 /* Check that the attribute ATTRIBUTE appears at most once in the
25275 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25276 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25277 isn't implemented yet in GCC. */
25280 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25284 tree name
= get_attribute_name (attribute
);
25285 if (is_attribute_p ("noreturn", name
)
25286 && lookup_attribute ("noreturn", attributes
))
25287 error ("attribute %<noreturn%> can appear at most once "
25288 "in an attribute-list");
25289 else if (is_attribute_p ("deprecated", name
)
25290 && lookup_attribute ("deprecated", attributes
))
25291 error ("attribute %<deprecated%> can appear at most once "
25292 "in an attribute-list");
25296 /* Parse a list of standard C++-11 attributes.
25300 attribute-list , attribute[opt]
25302 attribute-list , attribute ...
25306 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25308 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25309 cp_token
*token
= NULL
;
25313 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25314 if (attribute
== error_mark_node
)
25316 if (attribute
!= NULL_TREE
)
25318 cp_parser_check_std_attribute (attributes
, attribute
);
25319 TREE_CHAIN (attribute
) = attributes
;
25320 attributes
= attribute
;
25322 token
= cp_lexer_peek_token (parser
->lexer
);
25323 if (token
->type
== CPP_ELLIPSIS
)
25325 cp_lexer_consume_token (parser
->lexer
);
25326 if (attribute
== NULL_TREE
)
25327 error_at (token
->location
,
25328 "expected attribute before %<...%>");
25331 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25332 if (pack
== error_mark_node
)
25333 return error_mark_node
;
25334 TREE_VALUE (attribute
) = pack
;
25336 token
= cp_lexer_peek_token (parser
->lexer
);
25338 if (token
->type
!= CPP_COMMA
)
25340 cp_lexer_consume_token (parser
->lexer
);
25342 attributes
= nreverse (attributes
);
25346 /* Parse a standard C++-11 attribute specifier.
25348 attribute-specifier:
25349 [ [ attribute-using-prefix [opt] attribute-list ] ]
25350 alignment-specifier
25352 attribute-using-prefix:
25353 using attribute-namespace :
25355 alignment-specifier:
25356 alignas ( type-id ... [opt] )
25357 alignas ( alignment-expression ... [opt] ). */
25360 cp_parser_std_attribute_spec (cp_parser
*parser
)
25362 tree attributes
= NULL_TREE
;
25363 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25365 if (token
->type
== CPP_OPEN_SQUARE
25366 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25368 tree attr_ns
= NULL_TREE
;
25370 cp_lexer_consume_token (parser
->lexer
);
25371 cp_lexer_consume_token (parser
->lexer
);
25373 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25375 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25376 if (token
->type
== CPP_NAME
)
25377 attr_ns
= token
->u
.value
;
25378 else if (token
->type
== CPP_KEYWORD
)
25379 attr_ns
= ridpointers
[(int) token
->keyword
];
25380 else if (token
->flags
& NAMED_OP
)
25381 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25384 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25386 if (cxx_dialect
< cxx17
25387 && !in_system_header_at (input_location
))
25388 pedwarn (input_location
, 0,
25389 "attribute using prefix only available "
25390 "with -std=c++17 or -std=gnu++17");
25392 cp_lexer_consume_token (parser
->lexer
);
25393 cp_lexer_consume_token (parser
->lexer
);
25394 cp_lexer_consume_token (parser
->lexer
);
25397 attr_ns
= NULL_TREE
;
25400 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25402 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25403 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25404 cp_parser_skip_to_end_of_statement (parser
);
25406 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25407 when we are sure that we have actually parsed them. */
25408 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25414 /* Look for an alignment-specifier. */
25416 token
= cp_lexer_peek_token (parser
->lexer
);
25418 if (token
->type
!= CPP_KEYWORD
25419 || token
->keyword
!= RID_ALIGNAS
)
25422 cp_lexer_consume_token (parser
->lexer
);
25423 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25425 matching_parens parens
;
25426 if (!parens
.require_open (parser
))
25427 return error_mark_node
;
25429 cp_parser_parse_tentatively (parser
);
25430 alignas_expr
= cp_parser_type_id (parser
);
25432 if (!cp_parser_parse_definitely (parser
))
25434 alignas_expr
= cp_parser_assignment_expression (parser
);
25435 if (alignas_expr
== error_mark_node
)
25436 cp_parser_skip_to_end_of_statement (parser
);
25437 if (alignas_expr
== NULL_TREE
25438 || alignas_expr
== error_mark_node
)
25439 return alignas_expr
;
25442 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25443 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25445 /* Handle alignas (pack...). */
25446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25448 cp_lexer_consume_token (parser
->lexer
);
25449 alignas_expr
= make_pack_expansion (alignas_expr
);
25452 /* Something went wrong, so don't build the attribute. */
25453 if (alignas_expr
== error_mark_node
)
25454 return error_mark_node
;
25456 if (!parens
.require_close (parser
))
25457 return error_mark_node
;
25459 /* Build the C++-11 representation of an 'aligned'
25462 build_tree_list (build_tree_list (get_identifier ("gnu"),
25463 get_identifier ("aligned")),
25470 /* Parse a standard C++-11 attribute-specifier-seq.
25472 attribute-specifier-seq:
25473 attribute-specifier-seq [opt] attribute-specifier
25477 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25479 tree attr_specs
= NULL_TREE
;
25480 tree attr_last
= NULL_TREE
;
25484 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25485 if (attr_spec
== NULL_TREE
)
25487 if (attr_spec
== error_mark_node
)
25488 return error_mark_node
;
25491 TREE_CHAIN (attr_last
) = attr_spec
;
25493 attr_specs
= attr_last
= attr_spec
;
25494 attr_last
= tree_last (attr_last
);
25500 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25501 return index of the first token after balanced-token, or N on failure. */
25504 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25507 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25509 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25512 case CPP_PRAGMA_EOL
:
25513 /* Ran out of tokens. */
25515 case CPP_OPEN_PAREN
:
25518 case CPP_OPEN_BRACE
:
25521 case CPP_OPEN_SQUARE
:
25524 case CPP_CLOSE_PAREN
:
25527 case CPP_CLOSE_BRACE
:
25530 case CPP_CLOSE_SQUARE
:
25536 while (nparens
|| nbraces
|| nsquares
);
25540 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25541 return index of the first token after the GNU attribute tokens, or N on
25545 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25549 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25550 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25551 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25554 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25557 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25564 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25565 next token), return index of the first token after the standard C++11
25566 attribute tokens, or N on failure. */
25569 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25573 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25574 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25576 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25579 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25583 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25584 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25586 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25597 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25598 as the next token), return index of the first token after the attribute
25599 tokens, or N on failure. */
25602 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25604 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25605 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25606 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25609 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25610 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25611 current value of the PEDANTIC flag, regardless of whether or not
25612 the `__extension__' keyword is present. The caller is responsible
25613 for restoring the value of the PEDANTIC flag. */
25616 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25618 /* Save the old value of the PEDANTIC flag. */
25619 *saved_pedantic
= pedantic
;
25621 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25623 /* Consume the `__extension__' token. */
25624 cp_lexer_consume_token (parser
->lexer
);
25625 /* We're not being pedantic while the `__extension__' keyword is
25635 /* Parse a label declaration.
25638 __label__ label-declarator-seq ;
25640 label-declarator-seq:
25641 identifier , label-declarator-seq
25645 cp_parser_label_declaration (cp_parser
* parser
)
25647 /* Look for the `__label__' keyword. */
25648 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25654 /* Look for an identifier. */
25655 identifier
= cp_parser_identifier (parser
);
25656 /* If we failed, stop. */
25657 if (identifier
== error_mark_node
)
25659 /* Declare it as a label. */
25660 finish_label_decl (identifier
);
25661 /* If the next token is a `;', stop. */
25662 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25664 /* Look for the `,' separating the label declarations. */
25665 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25668 /* Look for the final `;'. */
25669 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25672 // -------------------------------------------------------------------------- //
25675 // Parse a requires clause.
25677 // requires-clause:
25678 // 'requires' logical-or-expression
25680 // The required logical-or-expression must be a constant expression. Note
25681 // that we don't check that the expression is constepxr here. We defer until
25682 // we analyze constraints and then, we only check atomic constraints.
25684 cp_parser_requires_clause (cp_parser
*parser
)
25686 // Parse the requires clause so that it is not automatically folded.
25687 ++processing_template_decl
;
25688 tree expr
= cp_parser_binary_expression (parser
, false, false,
25689 PREC_NOT_OPERATOR
, NULL
);
25690 if (check_for_bare_parameter_packs (expr
))
25691 expr
= error_mark_node
;
25692 --processing_template_decl
;
25696 // Optionally parse a requires clause:
25698 cp_parser_requires_clause_opt (cp_parser
*parser
)
25700 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25701 if (tok
->keyword
!= RID_REQUIRES
)
25703 if (!flag_concepts
&& tok
->type
== CPP_NAME
25704 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25706 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25707 "%<requires%> only available with -fconcepts");
25708 /* Parse and discard the requires-clause. */
25709 cp_lexer_consume_token (parser
->lexer
);
25710 cp_parser_requires_clause (parser
);
25714 cp_lexer_consume_token (parser
->lexer
);
25715 return cp_parser_requires_clause (parser
);
25719 /*---------------------------------------------------------------------------
25720 Requires expressions
25721 ---------------------------------------------------------------------------*/
25723 /* Parse a requires expression
25725 requirement-expression:
25726 'requires' requirement-parameter-list [opt] requirement-body */
25728 cp_parser_requires_expression (cp_parser
*parser
)
25730 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25731 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25733 /* A requires-expression shall appear only within a concept
25734 definition or a requires-clause.
25736 TODO: Implement this diagnostic correctly. */
25737 if (!processing_template_decl
)
25739 error_at (loc
, "a requires expression cannot appear outside a template");
25740 cp_parser_skip_to_end_of_statement (parser
);
25741 return error_mark_node
;
25746 /* Local parameters are delared as variables within the scope
25747 of the expression. They are not visible past the end of
25748 the expression. Expressions within the requires-expression
25749 are unevaluated. */
25750 struct scope_sentinel
25754 ++cp_unevaluated_operand
;
25755 begin_scope (sk_block
, NULL_TREE
);
25760 pop_bindings_and_leave_scope ();
25761 --cp_unevaluated_operand
;
25765 /* Parse the optional parameter list. */
25766 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25768 parms
= cp_parser_requirement_parameter_list (parser
);
25769 if (parms
== error_mark_node
)
25770 return error_mark_node
;
25775 /* Parse the requirement body. */
25776 reqs
= cp_parser_requirement_body (parser
);
25777 if (reqs
== error_mark_node
)
25778 return error_mark_node
;
25781 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25783 grokparms (parms
, &parms
);
25784 return finish_requires_expr (parms
, reqs
);
25787 /* Parse a parameterized requirement.
25789 requirement-parameter-list:
25790 '(' parameter-declaration-clause ')' */
25792 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25794 matching_parens parens
;
25795 if (!parens
.require_open (parser
))
25796 return error_mark_node
;
25798 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25800 if (!parens
.require_close (parser
))
25801 return error_mark_node
;
25806 /* Parse the body of a requirement.
25809 '{' requirement-list '}' */
25811 cp_parser_requirement_body (cp_parser
*parser
)
25813 matching_braces braces
;
25814 if (!braces
.require_open (parser
))
25815 return error_mark_node
;
25817 tree reqs
= cp_parser_requirement_list (parser
);
25819 if (!braces
.require_close (parser
))
25820 return error_mark_node
;
25825 /* Parse a list of requirements.
25829 requirement-list ';' requirement[opt] */
25831 cp_parser_requirement_list (cp_parser
*parser
)
25833 tree result
= NULL_TREE
;
25836 tree req
= cp_parser_requirement (parser
);
25837 if (req
== error_mark_node
)
25838 return error_mark_node
;
25840 result
= tree_cons (NULL_TREE
, req
, result
);
25842 /* If we see a semi-colon, consume it. */
25843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25844 cp_lexer_consume_token (parser
->lexer
);
25846 /* Stop processing at the end of the list. */
25847 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25851 /* Reverse the order of requirements so they are analyzed in
25852 declaration order. */
25853 return nreverse (result
);
25856 /* Parse a syntactic requirement or type requirement.
25860 compound-requirement
25862 nested-requirement */
25864 cp_parser_requirement (cp_parser
*parser
)
25866 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25867 return cp_parser_compound_requirement (parser
);
25868 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25869 return cp_parser_type_requirement (parser
);
25870 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25871 return cp_parser_nested_requirement (parser
);
25873 return cp_parser_simple_requirement (parser
);
25876 /* Parse a simple requirement.
25878 simple-requirement:
25881 cp_parser_simple_requirement (cp_parser
*parser
)
25883 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25884 if (!expr
|| expr
== error_mark_node
)
25885 return error_mark_node
;
25887 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25888 return error_mark_node
;
25890 return finish_simple_requirement (expr
);
25893 /* Parse a type requirement
25896 nested-name-specifier [opt] required-type-name ';'
25898 required-type-name:
25900 'template' [opt] simple-template-id */
25902 cp_parser_type_requirement (cp_parser
*parser
)
25904 cp_lexer_consume_token (parser
->lexer
);
25906 // Save the scope before parsing name specifiers.
25907 tree saved_scope
= parser
->scope
;
25908 tree saved_object_scope
= parser
->object_scope
;
25909 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25910 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25911 cp_parser_nested_name_specifier_opt (parser
,
25912 /*typename_keyword_p=*/true,
25913 /*check_dependency_p=*/false,
25915 /*is_declaration=*/false);
25918 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25920 cp_lexer_consume_token (parser
->lexer
);
25921 type
= cp_parser_template_id (parser
,
25922 /*template_keyword_p=*/true,
25923 /*check_dependency=*/false,
25924 /*tag_type=*/none_type
,
25925 /*is_declaration=*/false);
25926 type
= make_typename_type (parser
->scope
, type
, typename_type
,
25927 /*complain=*/tf_error
);
25930 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
25932 if (TREE_CODE (type
) == TYPE_DECL
)
25933 type
= TREE_TYPE (type
);
25935 parser
->scope
= saved_scope
;
25936 parser
->object_scope
= saved_object_scope
;
25937 parser
->qualifying_scope
= saved_qualifying_scope
;
25939 if (type
== error_mark_node
)
25940 cp_parser_skip_to_end_of_statement (parser
);
25942 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25943 return error_mark_node
;
25944 if (type
== error_mark_node
)
25945 return error_mark_node
;
25947 return finish_type_requirement (type
);
25950 /* Parse a compound requirement
25952 compound-requirement:
25953 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25955 cp_parser_compound_requirement (cp_parser
*parser
)
25957 /* Parse an expression enclosed in '{ }'s. */
25958 matching_braces braces
;
25959 if (!braces
.require_open (parser
))
25960 return error_mark_node
;
25962 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25963 if (!expr
|| expr
== error_mark_node
)
25964 return error_mark_node
;
25966 if (!braces
.require_close (parser
))
25967 return error_mark_node
;
25969 /* Parse the optional noexcept. */
25970 bool noexcept_p
= false;
25971 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
25973 cp_lexer_consume_token (parser
->lexer
);
25977 /* Parse the optional trailing return type. */
25978 tree type
= NULL_TREE
;
25979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
25981 cp_lexer_consume_token (parser
->lexer
);
25982 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
25983 parser
->in_result_type_constraint_p
= true;
25984 type
= cp_parser_trailing_type_id (parser
);
25985 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
25986 if (type
== error_mark_node
)
25987 return error_mark_node
;
25990 return finish_compound_requirement (expr
, type
, noexcept_p
);
25993 /* Parse a nested requirement. This is the same as a requires clause.
25995 nested-requirement:
25998 cp_parser_nested_requirement (cp_parser
*parser
)
26000 cp_lexer_consume_token (parser
->lexer
);
26001 tree req
= cp_parser_requires_clause (parser
);
26002 if (req
== error_mark_node
)
26003 return error_mark_node
;
26004 return finish_nested_requirement (req
);
26007 /* Support Functions */
26009 /* Return the appropriate prefer_type argument for lookup_name_real based on
26010 tag_type and template_mem_access. */
26013 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26015 /* DR 141: When looking in the current enclosing context for a template-name
26016 after -> or ., only consider class templates. */
26017 if (template_mem_access
)
26021 case none_type
: return 0; // No preference.
26022 case scope_type
: return 1; // Type or namespace.
26023 default: return 2; // Type only.
26027 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26028 NAME should have one of the representations used for an
26029 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26030 is returned. If PARSER->SCOPE is a dependent type, then a
26031 SCOPE_REF is returned.
26033 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26034 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26035 was formed. Abstractly, such entities should not be passed to this
26036 function, because they do not need to be looked up, but it is
26037 simpler to check for this special case here, rather than at the
26040 In cases not explicitly covered above, this function returns a
26041 DECL, OVERLOAD, or baselink representing the result of the lookup.
26042 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26045 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26046 (e.g., "struct") that was used. In that case bindings that do not
26047 refer to types are ignored.
26049 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26052 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26055 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26058 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26059 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26060 NULL_TREE otherwise. */
26063 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26064 enum tag_types tag_type
,
26067 bool check_dependency
,
26068 tree
*ambiguous_decls
,
26069 location_t name_location
)
26072 tree object_type
= parser
->context
->object_type
;
26074 /* Assume that the lookup will be unambiguous. */
26075 if (ambiguous_decls
)
26076 *ambiguous_decls
= NULL_TREE
;
26078 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26079 no longer valid. Note that if we are parsing tentatively, and
26080 the parse fails, OBJECT_TYPE will be automatically restored. */
26081 parser
->context
->object_type
= NULL_TREE
;
26083 if (name
== error_mark_node
)
26084 return error_mark_node
;
26086 /* A template-id has already been resolved; there is no lookup to
26088 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26090 if (BASELINK_P (name
))
26092 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26093 == TEMPLATE_ID_EXPR
);
26097 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26098 it should already have been checked to make sure that the name
26099 used matches the type being destroyed. */
26100 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26104 /* Figure out to which type this destructor applies. */
26106 type
= parser
->scope
;
26107 else if (object_type
)
26108 type
= object_type
;
26110 type
= current_class_type
;
26111 /* If that's not a class type, there is no destructor. */
26112 if (!type
|| !CLASS_TYPE_P (type
))
26113 return error_mark_node
;
26115 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26116 lazily_declare_fn (sfk_destructor
, type
);
26118 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26121 return error_mark_node
;
26124 /* By this point, the NAME should be an ordinary identifier. If
26125 the id-expression was a qualified name, the qualifying scope is
26126 stored in PARSER->SCOPE at this point. */
26127 gcc_assert (identifier_p (name
));
26129 /* Perform the lookup. */
26134 if (parser
->scope
== error_mark_node
)
26135 return error_mark_node
;
26137 /* If the SCOPE is dependent, the lookup must be deferred until
26138 the template is instantiated -- unless we are explicitly
26139 looking up names in uninstantiated templates. Even then, we
26140 cannot look up the name if the scope is not a class type; it
26141 might, for example, be a template type parameter. */
26142 dependent_p
= (TYPE_P (parser
->scope
)
26143 && dependent_scope_p (parser
->scope
));
26144 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26146 /* Defer lookup. */
26147 decl
= error_mark_node
;
26150 tree pushed_scope
= NULL_TREE
;
26152 /* If PARSER->SCOPE is a dependent type, then it must be a
26153 class type, and we must not be checking dependencies;
26154 otherwise, we would have processed this lookup above. So
26155 that PARSER->SCOPE is not considered a dependent base by
26156 lookup_member, we must enter the scope here. */
26158 pushed_scope
= push_scope (parser
->scope
);
26160 /* If the PARSER->SCOPE is a template specialization, it
26161 may be instantiated during name lookup. In that case,
26162 errors may be issued. Even if we rollback the current
26163 tentative parse, those errors are valid. */
26164 decl
= lookup_qualified_name (parser
->scope
, name
,
26165 prefer_type_arg (tag_type
),
26166 /*complain=*/true);
26168 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26169 lookup result and the nested-name-specifier nominates a class C:
26170 * if the name specified after the nested-name-specifier, when
26171 looked up in C, is the injected-class-name of C (Clause 9), or
26172 * if the name specified after the nested-name-specifier is the
26173 same as the identifier or the simple-template-id's template-
26174 name in the last component of the nested-name-specifier,
26175 the name is instead considered to name the constructor of
26176 class C. [ Note: for example, the constructor is not an
26177 acceptable lookup result in an elaborated-type-specifier so
26178 the constructor would not be used in place of the
26179 injected-class-name. --end note ] Such a constructor name
26180 shall be used only in the declarator-id of a declaration that
26181 names a constructor or in a using-declaration. */
26182 if (tag_type
== none_type
26183 && DECL_SELF_REFERENCE_P (decl
)
26184 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26185 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26186 prefer_type_arg (tag_type
),
26187 /*complain=*/true);
26189 /* If we have a single function from a using decl, pull it out. */
26190 if (TREE_CODE (decl
) == OVERLOAD
26191 && !really_overloaded_fn (decl
))
26192 decl
= OVL_FUNCTION (decl
);
26195 pop_scope (pushed_scope
);
26198 /* If the scope is a dependent type and either we deferred lookup or
26199 we did lookup but didn't find the name, rememeber the name. */
26200 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26201 && dependent_type_p (parser
->scope
))
26207 /* The resolution to Core Issue 180 says that `struct
26208 A::B' should be considered a type-name, even if `A'
26210 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26211 /*complain=*/tf_error
);
26212 if (type
!= error_mark_node
)
26213 decl
= TYPE_NAME (type
);
26215 else if (is_template
26216 && (cp_parser_next_token_ends_template_argument_p (parser
)
26217 || cp_lexer_next_token_is (parser
->lexer
,
26219 decl
= make_unbound_class_template (parser
->scope
,
26221 /*complain=*/tf_error
);
26223 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26224 parser
->scope
, name
,
26227 parser
->qualifying_scope
= parser
->scope
;
26228 parser
->object_scope
= NULL_TREE
;
26230 else if (object_type
)
26232 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26233 OBJECT_TYPE is not a class. */
26234 if (CLASS_TYPE_P (object_type
))
26235 /* If the OBJECT_TYPE is a template specialization, it may
26236 be instantiated during name lookup. In that case, errors
26237 may be issued. Even if we rollback the current tentative
26238 parse, those errors are valid. */
26239 decl
= lookup_member (object_type
,
26242 prefer_type_arg (tag_type
),
26243 tf_warning_or_error
);
26248 /* Look it up in the enclosing context. DR 141: When looking for a
26249 template-name after -> or ., only consider class templates. */
26250 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26252 /*block_p=*/true, is_namespace
, 0);
26253 if (object_type
== unknown_type_node
)
26254 /* The object is type-dependent, so we can't look anything up; we used
26255 this to get the DR 141 behavior. */
26256 object_type
= NULL_TREE
;
26257 parser
->object_scope
= object_type
;
26258 parser
->qualifying_scope
= NULL_TREE
;
26262 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26264 /*block_p=*/true, is_namespace
, 0);
26265 parser
->qualifying_scope
= NULL_TREE
;
26266 parser
->object_scope
= NULL_TREE
;
26269 /* If the lookup failed, let our caller know. */
26270 if (!decl
|| decl
== error_mark_node
)
26271 return error_mark_node
;
26273 /* Pull out the template from an injected-class-name (or multiple). */
26275 decl
= maybe_get_template_decl_from_type_decl (decl
);
26277 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26278 if (TREE_CODE (decl
) == TREE_LIST
)
26280 if (ambiguous_decls
)
26281 *ambiguous_decls
= decl
;
26282 /* The error message we have to print is too complicated for
26283 cp_parser_error, so we incorporate its actions directly. */
26284 if (!cp_parser_simulate_error (parser
))
26286 error_at (name_location
, "reference to %qD is ambiguous",
26288 print_candidates (decl
);
26290 return error_mark_node
;
26293 gcc_assert (DECL_P (decl
)
26294 || TREE_CODE (decl
) == OVERLOAD
26295 || TREE_CODE (decl
) == SCOPE_REF
26296 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26297 || BASELINK_P (decl
));
26299 /* If we have resolved the name of a member declaration, check to
26300 see if the declaration is accessible. When the name resolves to
26301 set of overloaded functions, accessibility is checked when
26302 overload resolution is done.
26304 During an explicit instantiation, access is not checked at all,
26305 as per [temp.explicit]. */
26307 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26309 maybe_record_typedef_use (decl
);
26311 return cp_expr (decl
, name_location
);
26314 /* Like cp_parser_lookup_name, but for use in the typical case where
26315 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26316 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26319 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26321 return cp_parser_lookup_name (parser
, name
,
26323 /*is_template=*/false,
26324 /*is_namespace=*/false,
26325 /*check_dependency=*/true,
26326 /*ambiguous_decls=*/NULL
,
26330 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26331 the current context, return the TYPE_DECL. If TAG_NAME_P is
26332 true, the DECL indicates the class being defined in a class-head,
26333 or declared in an elaborated-type-specifier.
26335 Otherwise, return DECL. */
26338 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26340 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26341 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26344 template <typename T> struct B;
26347 template <typename T> struct A::B {};
26349 Similarly, in an elaborated-type-specifier:
26351 namespace N { struct X{}; }
26354 template <typename T> friend struct N::X;
26357 However, if the DECL refers to a class type, and we are in
26358 the scope of the class, then the name lookup automatically
26359 finds the TYPE_DECL created by build_self_reference rather
26360 than a TEMPLATE_DECL. For example, in:
26362 template <class T> struct S {
26366 there is no need to handle such case. */
26368 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26369 return DECL_TEMPLATE_RESULT (decl
);
26374 /* If too many, or too few, template-parameter lists apply to the
26375 declarator, issue an error message. Returns TRUE if all went well,
26376 and FALSE otherwise. */
26379 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26380 cp_declarator
*declarator
,
26381 location_t declarator_location
)
26383 switch (declarator
->kind
)
26387 unsigned num_templates
= 0;
26388 tree scope
= declarator
->u
.id
.qualifying_scope
;
26391 num_templates
= num_template_headers_for_class (scope
);
26392 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26393 == TEMPLATE_ID_EXPR
)
26394 /* If the DECLARATOR has the form `X<y>' then it uses one
26395 additional level of template parameters. */
26398 return cp_parser_check_template_parameters
26399 (parser
, num_templates
, declarator_location
, declarator
);
26405 case cdk_reference
:
26407 return (cp_parser_check_declarator_template_parameters
26408 (parser
, declarator
->declarator
, declarator_location
));
26415 gcc_unreachable ();
26420 /* NUM_TEMPLATES were used in the current declaration. If that is
26421 invalid, return FALSE and issue an error messages. Otherwise,
26422 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26423 declarator and we can print more accurate diagnostics. */
26426 cp_parser_check_template_parameters (cp_parser
* parser
,
26427 unsigned num_templates
,
26428 location_t location
,
26429 cp_declarator
*declarator
)
26431 /* If there are the same number of template classes and parameter
26432 lists, that's OK. */
26433 if (parser
->num_template_parameter_lists
== num_templates
)
26435 /* If there are more, but only one more, then we are referring to a
26436 member template. That's OK too. */
26437 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
26439 /* If there are more template classes than parameter lists, we have
26442 template <class T> void S<T>::R<T>::f (); */
26443 if (parser
->num_template_parameter_lists
< num_templates
)
26445 if (declarator
&& !current_function_decl
)
26446 error_at (location
, "specializing member %<%T::%E%> "
26447 "requires %<template<>%> syntax",
26448 declarator
->u
.id
.qualifying_scope
,
26449 declarator
->u
.id
.unqualified_name
);
26450 else if (declarator
)
26451 error_at (location
, "invalid declaration of %<%T::%E%>",
26452 declarator
->u
.id
.qualifying_scope
,
26453 declarator
->u
.id
.unqualified_name
);
26455 error_at (location
, "too few template-parameter-lists");
26458 /* Otherwise, there are too many template parameter lists. We have
26461 template <class T> template <class U> void S::f(); */
26462 error_at (location
, "too many template-parameter-lists");
26466 /* Parse an optional `::' token indicating that the following name is
26467 from the global namespace. If so, PARSER->SCOPE is set to the
26468 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26469 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26470 Returns the new value of PARSER->SCOPE, if the `::' token is
26471 present, and NULL_TREE otherwise. */
26474 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26478 /* Peek at the next token. */
26479 token
= cp_lexer_peek_token (parser
->lexer
);
26480 /* If we're looking at a `::' token then we're starting from the
26481 global namespace, not our current location. */
26482 if (token
->type
== CPP_SCOPE
)
26484 /* Consume the `::' token. */
26485 cp_lexer_consume_token (parser
->lexer
);
26486 /* Set the SCOPE so that we know where to start the lookup. */
26487 parser
->scope
= global_namespace
;
26488 parser
->qualifying_scope
= global_namespace
;
26489 parser
->object_scope
= NULL_TREE
;
26491 return parser
->scope
;
26493 else if (!current_scope_valid_p
)
26495 parser
->scope
= NULL_TREE
;
26496 parser
->qualifying_scope
= NULL_TREE
;
26497 parser
->object_scope
= NULL_TREE
;
26503 /* Returns TRUE if the upcoming token sequence is the start of a
26504 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26505 declarator is preceded by the `friend' specifier. */
26508 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26510 bool constructor_p
;
26511 bool outside_class_specifier_p
;
26512 tree nested_name_specifier
;
26513 cp_token
*next_token
;
26515 /* The common case is that this is not a constructor declarator, so
26516 try to avoid doing lots of work if at all possible. It's not
26517 valid declare a constructor at function scope. */
26518 if (parser
->in_function_body
)
26520 /* And only certain tokens can begin a constructor declarator. */
26521 next_token
= cp_lexer_peek_token (parser
->lexer
);
26522 if (next_token
->type
!= CPP_NAME
26523 && next_token
->type
!= CPP_SCOPE
26524 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26525 && next_token
->type
!= CPP_TEMPLATE_ID
)
26528 /* Parse tentatively; we are going to roll back all of the tokens
26530 cp_parser_parse_tentatively (parser
);
26531 /* Assume that we are looking at a constructor declarator. */
26532 constructor_p
= true;
26534 /* Look for the optional `::' operator. */
26535 cp_parser_global_scope_opt (parser
,
26536 /*current_scope_valid_p=*/false);
26537 /* Look for the nested-name-specifier. */
26538 nested_name_specifier
26539 = (cp_parser_nested_name_specifier_opt (parser
,
26540 /*typename_keyword_p=*/false,
26541 /*check_dependency_p=*/false,
26543 /*is_declaration=*/false));
26545 outside_class_specifier_p
= (!at_class_scope_p ()
26546 || !TYPE_BEING_DEFINED (current_class_type
)
26549 /* Outside of a class-specifier, there must be a
26550 nested-name-specifier. Except in C++17 mode, where we
26551 might be declaring a guiding declaration. */
26552 if (!nested_name_specifier
&& outside_class_specifier_p
26553 && cxx_dialect
< cxx17
)
26554 constructor_p
= false;
26555 else if (nested_name_specifier
== error_mark_node
)
26556 constructor_p
= false;
26558 /* If we have a class scope, this is easy; DR 147 says that S::S always
26559 names the constructor, and no other qualified name could. */
26560 if (constructor_p
&& nested_name_specifier
26561 && CLASS_TYPE_P (nested_name_specifier
))
26563 tree id
= cp_parser_unqualified_id (parser
,
26564 /*template_keyword_p=*/false,
26565 /*check_dependency_p=*/false,
26566 /*declarator_p=*/true,
26567 /*optional_p=*/false);
26568 if (is_overloaded_fn (id
))
26569 id
= DECL_NAME (get_first_fn (id
));
26570 if (!constructor_name_p (id
, nested_name_specifier
))
26571 constructor_p
= false;
26573 /* If we still think that this might be a constructor-declarator,
26574 look for a class-name. */
26575 else if (constructor_p
)
26579 template <typename T> struct S {
26583 we must recognize that the nested `S' names a class. */
26584 if (cxx_dialect
>= cxx17
)
26585 cp_parser_parse_tentatively (parser
);
26588 type_decl
= cp_parser_class_name (parser
,
26589 /*typename_keyword_p=*/false,
26590 /*template_keyword_p=*/false,
26592 /*check_dependency_p=*/false,
26593 /*class_head_p=*/false,
26594 /*is_declaration=*/false);
26596 if (cxx_dialect
>= cxx17
26597 && !cp_parser_parse_definitely (parser
))
26599 type_decl
= NULL_TREE
;
26600 tree tmpl
= cp_parser_template_name (parser
,
26601 /*template_keyword*/false,
26602 /*check_dependency_p*/false,
26603 /*is_declaration*/false,
26605 /*is_identifier*/NULL
);
26606 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26607 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26608 /* It's a deduction guide, return true. */;
26610 cp_parser_simulate_error (parser
);
26613 /* If there was no class-name, then this is not a constructor.
26614 Otherwise, if we are in a class-specifier and we aren't
26615 handling a friend declaration, check that its type matches
26616 current_class_type (c++/38313). Note: error_mark_node
26617 is left alone for error recovery purposes. */
26618 constructor_p
= (!cp_parser_error_occurred (parser
)
26619 && (outside_class_specifier_p
26620 || type_decl
== NULL_TREE
26621 || type_decl
== error_mark_node
26622 || same_type_p (current_class_type
,
26623 TREE_TYPE (type_decl
))));
26625 /* If we're still considering a constructor, we have to see a `(',
26626 to begin the parameter-declaration-clause, followed by either a
26627 `)', an `...', or a decl-specifier. We need to check for a
26628 type-specifier to avoid being fooled into thinking that:
26632 is a constructor. (It is actually a function named `f' that
26633 takes one parameter (of type `int') and returns a value of type
26636 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26637 constructor_p
= false;
26640 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26641 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26642 /* A parameter declaration begins with a decl-specifier,
26643 which is either the "attribute" keyword, a storage class
26644 specifier, or (usually) a type-specifier. */
26645 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26648 tree pushed_scope
= NULL_TREE
;
26649 unsigned saved_num_template_parameter_lists
;
26651 /* Names appearing in the type-specifier should be looked up
26652 in the scope of the class. */
26653 if (current_class_type
)
26655 else if (type_decl
)
26657 type
= TREE_TYPE (type_decl
);
26658 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26660 type
= resolve_typename_type (type
,
26661 /*only_current_p=*/false);
26662 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26664 cp_parser_abort_tentative_parse (parser
);
26668 pushed_scope
= push_scope (type
);
26671 /* Inside the constructor parameter list, surrounding
26672 template-parameter-lists do not apply. */
26673 saved_num_template_parameter_lists
26674 = parser
->num_template_parameter_lists
;
26675 parser
->num_template_parameter_lists
= 0;
26677 /* Look for the type-specifier. */
26678 cp_parser_type_specifier (parser
,
26679 CP_PARSER_FLAGS_NONE
,
26680 /*decl_specs=*/NULL
,
26681 /*is_declarator=*/true,
26682 /*declares_class_or_enum=*/NULL
,
26683 /*is_cv_qualifier=*/NULL
);
26685 parser
->num_template_parameter_lists
26686 = saved_num_template_parameter_lists
;
26688 /* Leave the scope of the class. */
26690 pop_scope (pushed_scope
);
26692 constructor_p
= !cp_parser_error_occurred (parser
);
26696 /* We did not really want to consume any tokens. */
26697 cp_parser_abort_tentative_parse (parser
);
26699 return constructor_p
;
26702 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26703 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26704 they must be performed once we are in the scope of the function.
26706 Returns the function defined. */
26709 cp_parser_function_definition_from_specifiers_and_declarator
26710 (cp_parser
* parser
,
26711 cp_decl_specifier_seq
*decl_specifiers
,
26713 const cp_declarator
*declarator
)
26718 /* Begin the function-definition. */
26719 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26721 /* The things we're about to see are not directly qualified by any
26722 template headers we've seen thus far. */
26723 reset_specialization ();
26725 /* If there were names looked up in the decl-specifier-seq that we
26726 did not check, check them now. We must wait until we are in the
26727 scope of the function to perform the checks, since the function
26728 might be a friend. */
26729 perform_deferred_access_checks (tf_warning_or_error
);
26733 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26734 parser
->omp_declare_simd
= NULL
;
26735 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26736 parser
->oacc_routine
= NULL
;
26741 /* Skip the entire function. */
26742 cp_parser_skip_to_end_of_block_or_statement (parser
);
26743 fn
= error_mark_node
;
26745 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26747 /* Seen already, skip it. An error message has already been output. */
26748 cp_parser_skip_to_end_of_block_or_statement (parser
);
26749 fn
= current_function_decl
;
26750 current_function_decl
= NULL_TREE
;
26751 /* If this is a function from a class, pop the nested class. */
26752 if (current_class_name
)
26753 pop_nested_class ();
26758 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26759 tv
= TV_PARSE_INLINE
;
26761 tv
= TV_PARSE_FUNC
;
26763 fn
= cp_parser_function_definition_after_declarator (parser
,
26764 /*inline_p=*/false);
26771 /* Parse the part of a function-definition that follows the
26772 declarator. INLINE_P is TRUE iff this function is an inline
26773 function defined within a class-specifier.
26775 Returns the function defined. */
26778 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26782 bool saved_in_unbraced_linkage_specification_p
;
26783 bool saved_in_function_body
;
26784 unsigned saved_num_template_parameter_lists
;
26786 bool fully_implicit_function_template_p
26787 = parser
->fully_implicit_function_template_p
;
26788 parser
->fully_implicit_function_template_p
= false;
26789 tree implicit_template_parms
26790 = parser
->implicit_template_parms
;
26791 parser
->implicit_template_parms
= 0;
26792 cp_binding_level
* implicit_template_scope
26793 = parser
->implicit_template_scope
;
26794 parser
->implicit_template_scope
= 0;
26796 saved_in_function_body
= parser
->in_function_body
;
26797 parser
->in_function_body
= true;
26798 /* If the next token is `return', then the code may be trying to
26799 make use of the "named return value" extension that G++ used to
26801 token
= cp_lexer_peek_token (parser
->lexer
);
26802 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26804 /* Consume the `return' keyword. */
26805 cp_lexer_consume_token (parser
->lexer
);
26806 /* Look for the identifier that indicates what value is to be
26808 cp_parser_identifier (parser
);
26809 /* Issue an error message. */
26810 error_at (token
->location
,
26811 "named return values are no longer supported");
26812 /* Skip tokens until we reach the start of the function body. */
26815 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26816 if (token
->type
== CPP_OPEN_BRACE
26817 || token
->type
== CPP_EOF
26818 || token
->type
== CPP_PRAGMA_EOL
)
26820 cp_lexer_consume_token (parser
->lexer
);
26823 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26824 anything declared inside `f'. */
26825 saved_in_unbraced_linkage_specification_p
26826 = parser
->in_unbraced_linkage_specification_p
;
26827 parser
->in_unbraced_linkage_specification_p
= false;
26828 /* Inside the function, surrounding template-parameter-lists do not
26830 saved_num_template_parameter_lists
26831 = parser
->num_template_parameter_lists
;
26832 parser
->num_template_parameter_lists
= 0;
26834 /* If the next token is `try', `__transaction_atomic', or
26835 `__transaction_relaxed`, then we are looking at either function-try-block
26836 or function-transaction-block. Note that all of these include the
26838 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26839 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26840 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26841 RID_TRANSACTION_RELAXED
))
26842 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26843 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26844 cp_parser_function_try_block (parser
);
26846 cp_parser_ctor_initializer_opt_and_function_body
26847 (parser
, /*in_function_try_block=*/false);
26849 /* Finish the function. */
26850 fn
= finish_function (inline_p
);
26851 /* Generate code for it, if necessary. */
26852 expand_or_defer_fn (fn
);
26853 /* Restore the saved values. */
26854 parser
->in_unbraced_linkage_specification_p
26855 = saved_in_unbraced_linkage_specification_p
;
26856 parser
->num_template_parameter_lists
26857 = saved_num_template_parameter_lists
;
26858 parser
->in_function_body
= saved_in_function_body
;
26860 parser
->fully_implicit_function_template_p
26861 = fully_implicit_function_template_p
;
26862 parser
->implicit_template_parms
26863 = implicit_template_parms
;
26864 parser
->implicit_template_scope
26865 = implicit_template_scope
;
26867 if (parser
->fully_implicit_function_template_p
)
26868 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26873 /* Parse a template-declaration body (following argument list). */
26876 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26877 tree parameter_list
,
26880 tree decl
= NULL_TREE
;
26881 bool friend_p
= false;
26883 /* We just processed one more parameter list. */
26884 ++parser
->num_template_parameter_lists
;
26886 /* Get the deferred access checks from the parameter list. These
26887 will be checked once we know what is being declared, as for a
26888 member template the checks must be performed in the scope of the
26889 class containing the member. */
26890 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26892 /* Tentatively parse for a new template parameter list, which can either be
26893 the template keyword or a template introduction. */
26894 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26896 else if (cxx_dialect
>= cxx11
26897 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26898 decl
= cp_parser_alias_declaration (parser
);
26901 /* There are no access checks when parsing a template, as we do not
26902 know if a specialization will be a friend. */
26903 push_deferring_access_checks (dk_no_check
);
26904 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26905 decl
= cp_parser_single_declaration (parser
,
26908 /*explicit_specialization_p=*/false,
26910 pop_deferring_access_checks ();
26912 /* If this is a member template declaration, let the front
26914 if (member_p
&& !friend_p
&& decl
)
26916 if (TREE_CODE (decl
) == TYPE_DECL
)
26917 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
26919 decl
= finish_member_template_decl (decl
);
26921 else if (friend_p
&& decl
26922 && DECL_DECLARES_TYPE_P (decl
))
26923 make_friend_class (current_class_type
, TREE_TYPE (decl
),
26924 /*complain=*/true);
26926 /* We are done with the current parameter list. */
26927 --parser
->num_template_parameter_lists
;
26929 pop_deferring_access_checks ();
26932 finish_template_decl (parameter_list
);
26934 /* Check the template arguments for a literal operator template. */
26936 && DECL_DECLARES_FUNCTION_P (decl
)
26937 && UDLIT_OPER_P (DECL_NAME (decl
)))
26940 if (parameter_list
== NULL_TREE
)
26944 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
26945 if (num_parms
== 1)
26947 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
26948 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26949 if (TREE_TYPE (parm
) != char_type_node
26950 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26953 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
26955 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
26956 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
26957 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
26958 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
26959 if (parm
== error_mark_node
26960 || TREE_TYPE (parm
) != TREE_TYPE (type
)
26961 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
26969 if (cxx_dialect
>= cxx14
)
26970 error ("literal operator template %qD has invalid parameter list."
26971 " Expected non-type template argument pack <char...>"
26972 " or <typename CharT, CharT...>",
26975 error ("literal operator template %qD has invalid parameter list."
26976 " Expected non-type template argument pack <char...>",
26981 /* Register member declarations. */
26982 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
26983 finish_member_declaration (decl
);
26984 /* If DECL is a function template, we must return to parse it later.
26985 (Even though there is no definition, there might be default
26986 arguments that need handling.) */
26987 if (member_p
&& decl
26988 && DECL_DECLARES_FUNCTION_P (decl
))
26989 vec_safe_push (unparsed_funs_with_definitions
, decl
);
26992 /* Parse a template introduction header for a template-declaration. Returns
26993 false if tentative parse fails. */
26996 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
26998 cp_parser_parse_tentatively (parser
);
27000 tree saved_scope
= parser
->scope
;
27001 tree saved_object_scope
= parser
->object_scope
;
27002 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27004 /* Look for the optional `::' operator. */
27005 cp_parser_global_scope_opt (parser
,
27006 /*current_scope_valid_p=*/false);
27007 /* Look for the nested-name-specifier. */
27008 cp_parser_nested_name_specifier_opt (parser
,
27009 /*typename_keyword_p=*/false,
27010 /*check_dependency_p=*/true,
27012 /*is_declaration=*/false);
27014 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27015 tree concept_name
= cp_parser_identifier (parser
);
27017 /* Look up the concept for which we will be matching
27018 template parameters. */
27019 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27021 parser
->scope
= saved_scope
;
27022 parser
->object_scope
= saved_object_scope
;
27023 parser
->qualifying_scope
= saved_qualifying_scope
;
27025 if (concept_name
== error_mark_node
)
27026 cp_parser_simulate_error (parser
);
27028 /* Look for opening brace for introduction. */
27029 matching_braces braces
;
27030 braces
.require_open (parser
);
27032 if (!cp_parser_parse_definitely (parser
))
27035 push_deferring_access_checks (dk_deferred
);
27037 /* Build vector of placeholder parameters and grab
27038 matching identifiers. */
27039 tree introduction_list
= cp_parser_introduction_list (parser
);
27041 /* The introduction-list shall not be empty. */
27042 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27045 error ("empty introduction-list");
27049 /* Look for closing brace for introduction. */
27050 if (!braces
.require_close (parser
))
27053 if (tmpl_decl
== error_mark_node
)
27055 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27060 /* Build and associate the constraint. */
27061 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27062 if (parms
&& parms
!= error_mark_node
)
27064 cp_parser_template_declaration_after_parameters (parser
, parms
,
27069 error_at (token
->location
, "no matching concept for template-introduction");
27073 /* Parse a normal template-declaration following the template keyword. */
27076 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27078 tree parameter_list
;
27079 bool need_lang_pop
;
27080 location_t location
= input_location
;
27082 /* Look for the `<' token. */
27083 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27085 if (at_class_scope_p () && current_function_decl
)
27087 /* 14.5.2.2 [temp.mem]
27089 A local class shall not have member templates. */
27090 error_at (location
,
27091 "invalid declaration of member template in local class");
27092 cp_parser_skip_to_end_of_block_or_statement (parser
);
27097 A template ... shall not have C linkage. */
27098 if (current_lang_name
== lang_name_c
)
27100 error_at (location
, "template with C linkage");
27101 maybe_show_extern_c_location ();
27102 /* Give it C++ linkage to avoid confusing other parts of the
27104 push_lang_context (lang_name_cplusplus
);
27105 need_lang_pop
= true;
27108 need_lang_pop
= false;
27110 /* We cannot perform access checks on the template parameter
27111 declarations until we know what is being declared, just as we
27112 cannot check the decl-specifier list. */
27113 push_deferring_access_checks (dk_deferred
);
27115 /* If the next token is `>', then we have an invalid
27116 specialization. Rather than complain about an invalid template
27117 parameter, issue an error message here. */
27118 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27120 cp_parser_error (parser
, "invalid explicit specialization");
27121 begin_specialization ();
27122 parameter_list
= NULL_TREE
;
27126 /* Parse the template parameters. */
27127 parameter_list
= cp_parser_template_parameter_list (parser
);
27130 /* Look for the `>'. */
27131 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27133 /* Manage template requirements */
27136 tree reqs
= get_shorthand_constraints (current_template_parms
);
27137 if (tree r
= cp_parser_requires_clause_opt (parser
))
27138 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27139 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27142 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27145 /* For the erroneous case of a template with C linkage, we pushed an
27146 implicit C++ linkage scope; exit that scope now. */
27148 pop_lang_context ();
27151 /* Parse a template-declaration, assuming that the `export' (and
27152 `extern') keywords, if present, has already been scanned. MEMBER_P
27153 is as for cp_parser_template_declaration. */
27156 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27158 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27160 cp_lexer_consume_token (parser
->lexer
);
27161 cp_parser_explicit_template_declaration (parser
, member_p
);
27164 else if (flag_concepts
)
27165 return cp_parser_template_introduction (parser
, member_p
);
27170 /* Perform the deferred access checks from a template-parameter-list.
27171 CHECKS is a TREE_LIST of access checks, as returned by
27172 get_deferred_access_checks. */
27175 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27177 ++processing_template_parmlist
;
27178 perform_access_checks (checks
, tf_warning_or_error
);
27179 --processing_template_parmlist
;
27182 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27183 `function-definition' sequence that follows a template header.
27184 If MEMBER_P is true, this declaration appears in a class scope.
27186 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27187 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27190 cp_parser_single_declaration (cp_parser
* parser
,
27191 vec
<deferred_access_check
, va_gc
> *checks
,
27193 bool explicit_specialization_p
,
27196 int declares_class_or_enum
;
27197 tree decl
= NULL_TREE
;
27198 cp_decl_specifier_seq decl_specifiers
;
27199 bool function_definition_p
= false;
27200 cp_token
*decl_spec_token_start
;
27202 /* This function is only used when processing a template
27204 gcc_assert (innermost_scope_kind () == sk_template_parms
27205 || innermost_scope_kind () == sk_template_spec
);
27207 /* Defer access checks until we know what is being declared. */
27208 push_deferring_access_checks (dk_deferred
);
27210 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27212 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27213 cp_parser_decl_specifier_seq (parser
,
27214 CP_PARSER_FLAGS_OPTIONAL
,
27216 &declares_class_or_enum
);
27218 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27220 /* There are no template typedefs. */
27221 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27223 error_at (decl_spec_token_start
->location
,
27224 "template declaration of %<typedef%>");
27225 decl
= error_mark_node
;
27228 /* Gather up the access checks that occurred the
27229 decl-specifier-seq. */
27230 stop_deferring_access_checks ();
27232 /* Check for the declaration of a template class. */
27233 if (declares_class_or_enum
)
27235 if (cp_parser_declares_only_class_p (parser
)
27236 || (declares_class_or_enum
& 2))
27238 // If this is a declaration, but not a definition, associate
27239 // any constraints with the type declaration. Constraints
27240 // are associated with definitions in cp_parser_class_specifier.
27241 if (declares_class_or_enum
== 1)
27242 associate_classtype_constraints (decl_specifiers
.type
);
27244 decl
= shadow_tag (&decl_specifiers
);
27249 friend template <typename T> struct A<T>::B;
27252 A<T>::B will be represented by a TYPENAME_TYPE, and
27253 therefore not recognized by shadow_tag. */
27254 if (friend_p
&& *friend_p
27256 && decl_specifiers
.type
27257 && TYPE_P (decl_specifiers
.type
))
27258 decl
= decl_specifiers
.type
;
27260 if (decl
&& decl
!= error_mark_node
)
27261 decl
= TYPE_NAME (decl
);
27263 decl
= error_mark_node
;
27265 /* Perform access checks for template parameters. */
27266 cp_parser_perform_template_parameter_access_checks (checks
);
27268 /* Give a helpful diagnostic for
27269 template <class T> struct A { } a;
27270 if we aren't already recovering from an error. */
27271 if (!cp_parser_declares_only_class_p (parser
)
27274 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27275 "a class template declaration must not declare "
27277 cp_parser_skip_to_end_of_block_or_statement (parser
);
27283 /* Complain about missing 'typename' or other invalid type names. */
27284 if (!decl_specifiers
.any_type_specifiers_p
27285 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27287 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27288 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27289 the rest of this declaration. */
27290 decl
= error_mark_node
;
27294 /* If it's not a template class, try for a template function. If
27295 the next token is a `;', then this declaration does not declare
27296 anything. But, if there were errors in the decl-specifiers, then
27297 the error might well have come from an attempted class-specifier.
27298 In that case, there's no need to warn about a missing declarator. */
27300 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27301 || decl_specifiers
.type
!= error_mark_node
))
27303 decl
= cp_parser_init_declarator (parser
,
27306 /*function_definition_allowed_p=*/true,
27308 declares_class_or_enum
,
27309 &function_definition_p
,
27312 /* 7.1.1-1 [dcl.stc]
27314 A storage-class-specifier shall not be specified in an explicit
27315 specialization... */
27317 && explicit_specialization_p
27318 && decl_specifiers
.storage_class
!= sc_none
)
27320 error_at (decl_spec_token_start
->location
,
27321 "explicit template specialization cannot have a storage class");
27322 decl
= error_mark_node
;
27325 if (decl
&& VAR_P (decl
))
27326 check_template_variable (decl
);
27329 /* Look for a trailing `;' after the declaration. */
27330 if (!function_definition_p
27331 && (decl
== error_mark_node
27332 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27333 cp_parser_skip_to_end_of_block_or_statement (parser
);
27336 pop_deferring_access_checks ();
27338 /* Clear any current qualification; whatever comes next is the start
27339 of something new. */
27340 parser
->scope
= NULL_TREE
;
27341 parser
->qualifying_scope
= NULL_TREE
;
27342 parser
->object_scope
= NULL_TREE
;
27347 /* Parse a cast-expression that is not the operand of a unary "&". */
27350 cp_parser_simple_cast_expression (cp_parser
*parser
)
27352 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27353 /*cast_p=*/false, /*decltype*/false, NULL
);
27356 /* Parse a functional cast to TYPE. Returns an expression
27357 representing the cast. */
27360 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27362 vec
<tree
, va_gc
> *vec
;
27363 tree expression_list
;
27367 location_t start_loc
= input_location
;
27370 type
= error_mark_node
;
27372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27374 cp_lexer_set_source_position (parser
->lexer
);
27375 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27376 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27377 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27378 if (TREE_CODE (type
) == TYPE_DECL
)
27379 type
= TREE_TYPE (type
);
27381 cast
= finish_compound_literal (type
, expression_list
,
27382 tf_warning_or_error
, fcl_functional
);
27383 /* Create a location of the form:
27386 with caret == start at the start of the type name,
27387 finishing at the closing brace. */
27388 location_t finish_loc
27389 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27390 location_t combined_loc
= make_location (start_loc
, start_loc
,
27392 cast
.set_location (combined_loc
);
27397 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27399 /*allow_expansion_p=*/true,
27400 /*non_constant_p=*/NULL
);
27402 expression_list
= error_mark_node
;
27405 expression_list
= build_tree_list_vec (vec
);
27406 release_tree_vector (vec
);
27409 cast
= build_functional_cast (type
, expression_list
,
27410 tf_warning_or_error
);
27411 /* [expr.const]/1: In an integral constant expression "only type
27412 conversions to integral or enumeration type can be used". */
27413 if (TREE_CODE (type
) == TYPE_DECL
)
27414 type
= TREE_TYPE (type
);
27415 if (cast
!= error_mark_node
27416 && !cast_valid_in_integral_constant_expression_p (type
)
27417 && cp_parser_non_integral_constant_expression (parser
,
27419 return error_mark_node
;
27421 /* Create a location of the form:
27424 with caret == start at the start of the type name,
27425 finishing at the closing paren. */
27426 location_t finish_loc
27427 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27428 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27429 cast
.set_location (combined_loc
);
27433 /* Save the tokens that make up the body of a member function defined
27434 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27435 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27436 specifiers applied to the declaration. Returns the FUNCTION_DECL
27437 for the member function. */
27440 cp_parser_save_member_function_body (cp_parser
* parser
,
27441 cp_decl_specifier_seq
*decl_specifiers
,
27442 cp_declarator
*declarator
,
27448 bool function_try_block
= false;
27450 /* Create the FUNCTION_DECL. */
27451 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27452 cp_finalize_omp_declare_simd (parser
, fn
);
27453 cp_finalize_oacc_routine (parser
, fn
, true);
27454 /* If something went badly wrong, bail out now. */
27455 if (fn
== error_mark_node
)
27457 /* If there's a function-body, skip it. */
27458 if (cp_parser_token_starts_function_definition_p
27459 (cp_lexer_peek_token (parser
->lexer
)))
27460 cp_parser_skip_to_end_of_block_or_statement (parser
);
27461 return error_mark_node
;
27464 /* Remember it, if there default args to post process. */
27465 cp_parser_save_default_args (parser
, fn
);
27467 /* Save away the tokens that make up the body of the
27469 first
= parser
->lexer
->next_token
;
27471 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27472 cp_lexer_consume_token (parser
->lexer
);
27473 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27474 RID_TRANSACTION_ATOMIC
))
27476 cp_lexer_consume_token (parser
->lexer
);
27477 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27479 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27480 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27481 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27482 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27483 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27485 cp_lexer_consume_token (parser
->lexer
);
27486 cp_lexer_consume_token (parser
->lexer
);
27487 cp_lexer_consume_token (parser
->lexer
);
27488 cp_lexer_consume_token (parser
->lexer
);
27489 cp_lexer_consume_token (parser
->lexer
);
27492 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27493 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27495 cp_lexer_consume_token (parser
->lexer
);
27496 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27501 /* Handle function try blocks. */
27502 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27504 cp_lexer_consume_token (parser
->lexer
);
27505 function_try_block
= true;
27507 /* We can have braced-init-list mem-initializers before the fn body. */
27508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27510 cp_lexer_consume_token (parser
->lexer
);
27511 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27513 /* cache_group will stop after an un-nested { } pair, too. */
27514 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27517 /* variadic mem-inits have ... after the ')'. */
27518 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27519 cp_lexer_consume_token (parser
->lexer
);
27522 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27523 /* Handle function try blocks. */
27524 if (function_try_block
)
27525 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27526 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27527 last
= parser
->lexer
->next_token
;
27529 /* Save away the inline definition; we will process it when the
27530 class is complete. */
27531 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27532 DECL_PENDING_INLINE_P (fn
) = 1;
27534 /* We need to know that this was defined in the class, so that
27535 friend templates are handled correctly. */
27536 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27538 /* Add FN to the queue of functions to be parsed later. */
27539 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27544 /* Save the tokens that make up the in-class initializer for a non-static
27545 data member. Returns a DEFAULT_ARG. */
27548 cp_parser_save_nsdmi (cp_parser
* parser
)
27550 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27553 /* Parse a template-argument-list, as well as the trailing ">" (but
27554 not the opening "<"). See cp_parser_template_argument_list for the
27558 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27562 tree saved_qualifying_scope
;
27563 tree saved_object_scope
;
27564 bool saved_greater_than_is_operator_p
;
27565 int saved_unevaluated_operand
;
27566 int saved_inhibit_evaluation_warnings
;
27570 When parsing a template-id, the first non-nested `>' is taken as
27571 the end of the template-argument-list rather than a greater-than
27573 saved_greater_than_is_operator_p
27574 = parser
->greater_than_is_operator_p
;
27575 parser
->greater_than_is_operator_p
= false;
27576 /* Parsing the argument list may modify SCOPE, so we save it
27578 saved_scope
= parser
->scope
;
27579 saved_qualifying_scope
= parser
->qualifying_scope
;
27580 saved_object_scope
= parser
->object_scope
;
27581 /* We need to evaluate the template arguments, even though this
27582 template-id may be nested within a "sizeof". */
27583 saved_unevaluated_operand
= cp_unevaluated_operand
;
27584 cp_unevaluated_operand
= 0;
27585 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27586 c_inhibit_evaluation_warnings
= 0;
27587 /* Parse the template-argument-list itself. */
27588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27589 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27590 arguments
= NULL_TREE
;
27592 arguments
= cp_parser_template_argument_list (parser
);
27593 /* Look for the `>' that ends the template-argument-list. If we find
27594 a '>>' instead, it's probably just a typo. */
27595 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27597 if (cxx_dialect
!= cxx98
)
27599 /* In C++0x, a `>>' in a template argument list or cast
27600 expression is considered to be two separate `>'
27601 tokens. So, change the current token to a `>', but don't
27602 consume it: it will be consumed later when the outer
27603 template argument list (or cast expression) is parsed.
27604 Note that this replacement of `>' for `>>' is necessary
27605 even if we are parsing tentatively: in the tentative
27606 case, after calling
27607 cp_parser_enclosed_template_argument_list we will always
27608 throw away all of the template arguments and the first
27609 closing `>', either because the template argument list
27610 was erroneous or because we are replacing those tokens
27611 with a CPP_TEMPLATE_ID token. The second `>' (which will
27612 not have been thrown away) is needed either to close an
27613 outer template argument list or to complete a new-style
27615 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27616 token
->type
= CPP_GREATER
;
27618 else if (!saved_greater_than_is_operator_p
)
27620 /* If we're in a nested template argument list, the '>>' has
27621 to be a typo for '> >'. We emit the error message, but we
27622 continue parsing and we push a '>' as next token, so that
27623 the argument list will be parsed correctly. Note that the
27624 global source location is still on the token before the
27625 '>>', so we need to say explicitly where we want it. */
27626 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27627 gcc_rich_location
richloc (token
->location
);
27628 richloc
.add_fixit_replace ("> >");
27629 error_at (&richloc
, "%<>>%> should be %<> >%> "
27630 "within a nested template argument list");
27632 token
->type
= CPP_GREATER
;
27636 /* If this is not a nested template argument list, the '>>'
27637 is a typo for '>'. Emit an error message and continue.
27638 Same deal about the token location, but here we can get it
27639 right by consuming the '>>' before issuing the diagnostic. */
27640 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27641 error_at (token
->location
,
27642 "spurious %<>>%>, use %<>%> to terminate "
27643 "a template argument list");
27647 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27648 /* The `>' token might be a greater-than operator again now. */
27649 parser
->greater_than_is_operator_p
27650 = saved_greater_than_is_operator_p
;
27651 /* Restore the SAVED_SCOPE. */
27652 parser
->scope
= saved_scope
;
27653 parser
->qualifying_scope
= saved_qualifying_scope
;
27654 parser
->object_scope
= saved_object_scope
;
27655 cp_unevaluated_operand
= saved_unevaluated_operand
;
27656 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27661 /* MEMBER_FUNCTION is a member function, or a friend. If default
27662 arguments, or the body of the function have not yet been parsed,
27666 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27668 timevar_push (TV_PARSE_INMETH
);
27669 /* If this member is a template, get the underlying
27671 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27672 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27674 /* There should not be any class definitions in progress at this
27675 point; the bodies of members are only parsed outside of all class
27677 gcc_assert (parser
->num_classes_being_defined
== 0);
27678 /* While we're parsing the member functions we might encounter more
27679 classes. We want to handle them right away, but we don't want
27680 them getting mixed up with functions that are currently in the
27682 push_unparsed_function_queues (parser
);
27684 /* Make sure that any template parameters are in scope. */
27685 maybe_begin_member_template_processing (member_function
);
27687 /* If the body of the function has not yet been parsed, parse it
27689 if (DECL_PENDING_INLINE_P (member_function
))
27691 tree function_scope
;
27692 cp_token_cache
*tokens
;
27694 /* The function is no longer pending; we are processing it. */
27695 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27696 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27697 DECL_PENDING_INLINE_P (member_function
) = 0;
27699 /* If this is a local class, enter the scope of the containing
27701 function_scope
= current_function_decl
;
27702 if (function_scope
)
27703 push_function_context ();
27705 /* Push the body of the function onto the lexer stack. */
27706 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27708 /* Let the front end know that we going to be defining this
27710 start_preparsed_function (member_function
, NULL_TREE
,
27711 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27713 /* Don't do access checking if it is a templated function. */
27714 if (processing_template_decl
)
27715 push_deferring_access_checks (dk_no_check
);
27717 /* #pragma omp declare reduction needs special parsing. */
27718 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27720 parser
->lexer
->in_pragma
= true;
27721 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27722 finish_function (/*inline_p=*/true);
27723 cp_check_omp_declare_reduction (member_function
);
27726 /* Now, parse the body of the function. */
27727 cp_parser_function_definition_after_declarator (parser
,
27728 /*inline_p=*/true);
27730 if (processing_template_decl
)
27731 pop_deferring_access_checks ();
27733 /* Leave the scope of the containing function. */
27734 if (function_scope
)
27735 pop_function_context ();
27736 cp_parser_pop_lexer (parser
);
27739 /* Remove any template parameters from the symbol table. */
27740 maybe_end_member_template_processing ();
27742 /* Restore the queue. */
27743 pop_unparsed_function_queues (parser
);
27744 timevar_pop (TV_PARSE_INMETH
);
27747 /* If DECL contains any default args, remember it on the unparsed
27748 functions queue. */
27751 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27755 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27757 probe
= TREE_CHAIN (probe
))
27758 if (TREE_PURPOSE (probe
))
27760 cp_default_arg_entry entry
= {current_class_type
, decl
};
27761 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27766 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27767 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27768 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27769 from the parameter-type-list. */
27772 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27773 tree default_arg
, tree parmtype
)
27775 cp_token_cache
*tokens
;
27779 if (default_arg
== error_mark_node
)
27780 return error_mark_node
;
27782 /* Push the saved tokens for the default argument onto the parser's
27784 tokens
= DEFARG_TOKENS (default_arg
);
27785 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27787 start_lambda_scope (decl
);
27789 /* Parse the default argument. */
27790 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27791 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27792 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27794 finish_lambda_scope ();
27796 if (parsed_arg
== error_mark_node
)
27797 cp_parser_skip_to_end_of_statement (parser
);
27799 if (!processing_template_decl
)
27801 /* In a non-template class, check conversions now. In a template,
27802 we'll wait and instantiate these as needed. */
27803 if (TREE_CODE (decl
) == PARM_DECL
)
27804 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27805 tf_warning_or_error
);
27806 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27807 parsed_arg
= error_mark_node
;
27809 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27812 /* If the token stream has not been completely used up, then
27813 there was extra junk after the end of the default
27815 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27817 if (TREE_CODE (decl
) == PARM_DECL
)
27818 cp_parser_error (parser
, "expected %<,%>");
27820 cp_parser_error (parser
, "expected %<;%>");
27823 /* Revert to the main lexer. */
27824 cp_parser_pop_lexer (parser
);
27829 /* FIELD is a non-static data member with an initializer which we saved for
27830 later; parse it now. */
27833 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27837 maybe_begin_member_template_processing (field
);
27839 push_unparsed_function_queues (parser
);
27840 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27841 DECL_INITIAL (field
),
27843 pop_unparsed_function_queues (parser
);
27845 maybe_end_member_template_processing ();
27847 DECL_INITIAL (field
) = def
;
27850 /* FN is a FUNCTION_DECL which may contains a parameter with an
27851 unparsed DEFAULT_ARG. Parse the default args now. This function
27852 assumes that the current scope is the scope in which the default
27853 argument should be processed. */
27856 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27858 bool saved_local_variables_forbidden_p
;
27859 tree parm
, parmdecl
;
27861 /* While we're parsing the default args, we might (due to the
27862 statement expression extension) encounter more classes. We want
27863 to handle them right away, but we don't want them getting mixed
27864 up with default args that are currently in the queue. */
27865 push_unparsed_function_queues (parser
);
27867 /* Local variable names (and the `this' keyword) may not appear
27868 in a default argument. */
27869 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27870 parser
->local_variables_forbidden_p
= true;
27872 push_defarg_context (fn
);
27874 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27875 parmdecl
= DECL_ARGUMENTS (fn
);
27876 parm
&& parm
!= void_list_node
;
27877 parm
= TREE_CHAIN (parm
),
27878 parmdecl
= DECL_CHAIN (parmdecl
))
27880 tree default_arg
= TREE_PURPOSE (parm
);
27882 vec
<tree
, va_gc
> *insts
;
27889 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27890 /* This can happen for a friend declaration for a function
27891 already declared with default arguments. */
27895 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27897 TREE_VALUE (parm
));
27898 TREE_PURPOSE (parm
) = parsed_arg
;
27900 /* Update any instantiations we've already created. */
27901 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27902 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27903 TREE_PURPOSE (copy
) = parsed_arg
;
27906 pop_defarg_context ();
27908 /* Make sure no default arg is missing. */
27909 check_default_args (fn
);
27911 /* Restore the state of local_variables_forbidden_p. */
27912 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
27914 /* Restore the queue. */
27915 pop_unparsed_function_queues (parser
);
27918 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27920 sizeof ... ( identifier )
27922 where the 'sizeof' token has already been consumed. */
27925 cp_parser_sizeof_pack (cp_parser
*parser
)
27927 /* Consume the `...'. */
27928 cp_lexer_consume_token (parser
->lexer
);
27929 maybe_warn_variadic_templates ();
27931 matching_parens parens
;
27932 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
27934 parens
.consume_open (parser
);
27936 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
27937 "%<sizeof...%> argument must be surrounded by parentheses");
27939 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27940 tree name
= cp_parser_identifier (parser
);
27941 if (name
== error_mark_node
)
27942 return error_mark_node
;
27943 /* The name is not qualified. */
27944 parser
->scope
= NULL_TREE
;
27945 parser
->qualifying_scope
= NULL_TREE
;
27946 parser
->object_scope
= NULL_TREE
;
27947 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27948 if (expr
== error_mark_node
)
27949 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
27951 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
27952 expr
= TREE_TYPE (expr
);
27953 else if (TREE_CODE (expr
) == CONST_DECL
)
27954 expr
= DECL_INITIAL (expr
);
27955 expr
= make_pack_expansion (expr
);
27956 PACK_EXPANSION_SIZEOF_P (expr
) = true;
27959 parens
.require_close (parser
);
27964 /* Parse the operand of `sizeof' (or a similar operator). Returns
27965 either a TYPE or an expression, depending on the form of the
27966 input. The KEYWORD indicates which kind of expression we have
27970 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
27972 tree expr
= NULL_TREE
;
27973 const char *saved_message
;
27975 bool saved_integral_constant_expression_p
;
27976 bool saved_non_integral_constant_expression_p
;
27978 /* If it's a `...', then we are computing the length of a parameter
27980 if (keyword
== RID_SIZEOF
27981 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27982 return cp_parser_sizeof_pack (parser
);
27984 /* Types cannot be defined in a `sizeof' expression. Save away the
27986 saved_message
= parser
->type_definition_forbidden_message
;
27987 /* And create the new one. */
27988 tmp
= concat ("types may not be defined in %<",
27989 IDENTIFIER_POINTER (ridpointers
[keyword
]),
27990 "%> expressions", NULL
);
27991 parser
->type_definition_forbidden_message
= tmp
;
27993 /* The restrictions on constant-expressions do not apply inside
27994 sizeof expressions. */
27995 saved_integral_constant_expression_p
27996 = parser
->integral_constant_expression_p
;
27997 saved_non_integral_constant_expression_p
27998 = parser
->non_integral_constant_expression_p
;
27999 parser
->integral_constant_expression_p
= false;
28001 /* Do not actually evaluate the expression. */
28002 ++cp_unevaluated_operand
;
28003 ++c_inhibit_evaluation_warnings
;
28004 /* If it's a `(', then we might be looking at the type-id
28006 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28008 tree type
= NULL_TREE
;
28010 /* We can't be sure yet whether we're looking at a type-id or an
28012 cp_parser_parse_tentatively (parser
);
28014 matching_parens parens
;
28015 parens
.consume_open (parser
);
28017 /* Note: as a GNU Extension, compound literals are considered
28018 postfix-expressions as they are in C99, so they are valid
28019 arguments to sizeof. See comment in cp_parser_cast_expression
28021 if (cp_parser_compound_literal_p (parser
))
28022 cp_parser_simulate_error (parser
);
28025 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28026 parser
->in_type_id_in_expr_p
= true;
28027 /* Look for the type-id. */
28028 type
= cp_parser_type_id (parser
);
28029 /* Look for the closing `)'. */
28030 parens
.require_close (parser
);
28031 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28034 /* If all went well, then we're done. */
28035 if (cp_parser_parse_definitely (parser
))
28037 cp_decl_specifier_seq decl_specs
;
28039 /* Build a trivial decl-specifier-seq. */
28040 clear_decl_specs (&decl_specs
);
28041 decl_specs
.type
= type
;
28043 /* Call grokdeclarator to figure out what type this is. */
28044 expr
= grokdeclarator (NULL
,
28048 /*attrlist=*/NULL
);
28052 /* If the type-id production did not work out, then we must be
28053 looking at the unary-expression production. */
28055 expr
= cp_parser_unary_expression (parser
);
28057 /* Go back to evaluating expressions. */
28058 --cp_unevaluated_operand
;
28059 --c_inhibit_evaluation_warnings
;
28061 /* Free the message we created. */
28063 /* And restore the old one. */
28064 parser
->type_definition_forbidden_message
= saved_message
;
28065 parser
->integral_constant_expression_p
28066 = saved_integral_constant_expression_p
;
28067 parser
->non_integral_constant_expression_p
28068 = saved_non_integral_constant_expression_p
;
28073 /* If the current declaration has no declarator, return true. */
28076 cp_parser_declares_only_class_p (cp_parser
*parser
)
28078 /* If the next token is a `;' or a `,' then there is no
28080 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28081 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28084 /* Update the DECL_SPECS to reflect the storage class indicated by
28088 cp_parser_set_storage_class (cp_parser
*parser
,
28089 cp_decl_specifier_seq
*decl_specs
,
28093 cp_storage_class storage_class
;
28095 if (parser
->in_unbraced_linkage_specification_p
)
28097 error_at (token
->location
, "invalid use of %qD in linkage specification",
28098 ridpointers
[keyword
]);
28101 else if (decl_specs
->storage_class
!= sc_none
)
28103 decl_specs
->conflicting_specifiers_p
= true;
28107 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28108 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28109 && decl_specs
->gnu_thread_keyword_p
)
28111 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28112 "%<__thread%> before %qD", ridpointers
[keyword
]);
28118 storage_class
= sc_auto
;
28121 storage_class
= sc_register
;
28124 storage_class
= sc_static
;
28127 storage_class
= sc_extern
;
28130 storage_class
= sc_mutable
;
28133 gcc_unreachable ();
28135 decl_specs
->storage_class
= storage_class
;
28136 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28138 /* A storage class specifier cannot be applied alongside a typedef
28139 specifier. If there is a typedef specifier present then set
28140 conflicting_specifiers_p which will trigger an error later
28141 on in grokdeclarator. */
28142 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28143 decl_specs
->conflicting_specifiers_p
= true;
28146 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28147 is true, the type is a class or enum definition. */
28150 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28153 bool type_definition_p
)
28155 decl_specs
->any_specifiers_p
= true;
28157 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28158 (with, for example, in "typedef int wchar_t;") we remember that
28159 this is what happened. In system headers, we ignore these
28160 declarations so that G++ can work with system headers that are not
28162 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28163 && !type_definition_p
28164 && (type_spec
== boolean_type_node
28165 || type_spec
== char16_type_node
28166 || type_spec
== char32_type_node
28167 || type_spec
== wchar_type_node
)
28168 && (decl_specs
->type
28169 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28170 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28171 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28172 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28174 decl_specs
->redefined_builtin_type
= type_spec
;
28175 set_and_check_decl_spec_loc (decl_specs
,
28176 ds_redefined_builtin_type_spec
,
28178 if (!decl_specs
->type
)
28180 decl_specs
->type
= type_spec
;
28181 decl_specs
->type_definition_p
= false;
28182 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28185 else if (decl_specs
->type
)
28186 decl_specs
->multiple_types_p
= true;
28189 decl_specs
->type
= type_spec
;
28190 decl_specs
->type_definition_p
= type_definition_p
;
28191 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28192 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28196 /* True iff TOKEN is the GNU keyword __thread. */
28199 token_is__thread (cp_token
*token
)
28201 gcc_assert (token
->keyword
== RID_THREAD
);
28202 return id_equal (token
->u
.value
, "__thread");
28205 /* Set the location for a declarator specifier and check if it is
28208 DECL_SPECS is the sequence of declarator specifiers onto which to
28211 DS is the single declarator specifier to set which location is to
28212 be set onto the existing sequence of declarators.
28214 LOCATION is the location for the declarator specifier to
28218 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28219 cp_decl_spec ds
, cp_token
*token
)
28221 gcc_assert (ds
< ds_last
);
28223 if (decl_specs
== NULL
)
28226 source_location location
= token
->location
;
28228 if (decl_specs
->locations
[ds
] == 0)
28230 decl_specs
->locations
[ds
] = location
;
28231 if (ds
== ds_thread
)
28232 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28238 if (decl_specs
->locations
[ds_long_long
] != 0)
28239 error_at (location
,
28240 "%<long long long%> is too long for GCC");
28243 decl_specs
->locations
[ds_long_long
] = location
;
28244 pedwarn_cxx98 (location
,
28246 "ISO C++ 1998 does not support %<long long%>");
28249 else if (ds
== ds_thread
)
28251 bool gnu
= token_is__thread (token
);
28252 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28253 error_at (location
,
28254 "both %<__thread%> and %<thread_local%> specified");
28257 gcc_rich_location
richloc (location
);
28258 richloc
.add_fixit_remove ();
28259 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28264 static const char *const decl_spec_names
[] = {
28281 gcc_rich_location
richloc (location
);
28282 richloc
.add_fixit_remove ();
28283 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28288 /* Return true iff the declarator specifier DS is present in the
28289 sequence of declarator specifiers DECL_SPECS. */
28292 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28295 gcc_assert (ds
< ds_last
);
28297 if (decl_specs
== NULL
)
28300 return decl_specs
->locations
[ds
] != 0;
28303 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28304 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28307 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28309 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28312 /* Issue an error message indicating that TOKEN_DESC was expected.
28313 If KEYWORD is true, it indicated this function is called by
28314 cp_parser_require_keword and the required token can only be
28315 a indicated keyword.
28317 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28318 within any error as the location of an "opening" token matching
28319 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28320 RT_CLOSE_PAREN). */
28323 cp_parser_required_error (cp_parser
*parser
,
28324 required_token token_desc
,
28326 location_t matching_location
)
28328 if (cp_parser_simulate_error (parser
))
28331 const char *gmsgid
= NULL
;
28332 switch (token_desc
)
28335 gmsgid
= G_("expected %<new%>");
28338 gmsgid
= G_("expected %<delete%>");
28341 gmsgid
= G_("expected %<return%>");
28344 gmsgid
= G_("expected %<while%>");
28347 gmsgid
= G_("expected %<extern%>");
28349 case RT_STATIC_ASSERT
:
28350 gmsgid
= G_("expected %<static_assert%>");
28353 gmsgid
= G_("expected %<decltype%>");
28356 gmsgid
= G_("expected %<operator%>");
28359 gmsgid
= G_("expected %<class%>");
28362 gmsgid
= G_("expected %<template%>");
28365 gmsgid
= G_("expected %<namespace%>");
28368 gmsgid
= G_("expected %<using%>");
28371 gmsgid
= G_("expected %<asm%>");
28374 gmsgid
= G_("expected %<try%>");
28377 gmsgid
= G_("expected %<catch%>");
28380 gmsgid
= G_("expected %<throw%>");
28383 gmsgid
= G_("expected %<__label__%>");
28386 gmsgid
= G_("expected %<@try%>");
28388 case RT_AT_SYNCHRONIZED
:
28389 gmsgid
= G_("expected %<@synchronized%>");
28392 gmsgid
= G_("expected %<@throw%>");
28394 case RT_TRANSACTION_ATOMIC
:
28395 gmsgid
= G_("expected %<__transaction_atomic%>");
28397 case RT_TRANSACTION_RELAXED
:
28398 gmsgid
= G_("expected %<__transaction_relaxed%>");
28404 if (!gmsgid
&& !keyword
)
28406 switch (token_desc
)
28409 gmsgid
= G_("expected %<;%>");
28411 case RT_OPEN_PAREN
:
28412 gmsgid
= G_("expected %<(%>");
28414 case RT_CLOSE_BRACE
:
28415 gmsgid
= G_("expected %<}%>");
28417 case RT_OPEN_BRACE
:
28418 gmsgid
= G_("expected %<{%>");
28420 case RT_CLOSE_SQUARE
:
28421 gmsgid
= G_("expected %<]%>");
28423 case RT_OPEN_SQUARE
:
28424 gmsgid
= G_("expected %<[%>");
28427 gmsgid
= G_("expected %<,%>");
28430 gmsgid
= G_("expected %<::%>");
28433 gmsgid
= G_("expected %<<%>");
28436 gmsgid
= G_("expected %<>%>");
28439 gmsgid
= G_("expected %<=%>");
28442 gmsgid
= G_("expected %<...%>");
28445 gmsgid
= G_("expected %<*%>");
28448 gmsgid
= G_("expected %<~%>");
28451 gmsgid
= G_("expected %<:%>");
28453 case RT_COLON_SCOPE
:
28454 gmsgid
= G_("expected %<:%> or %<::%>");
28456 case RT_CLOSE_PAREN
:
28457 gmsgid
= G_("expected %<)%>");
28459 case RT_COMMA_CLOSE_PAREN
:
28460 gmsgid
= G_("expected %<,%> or %<)%>");
28462 case RT_PRAGMA_EOL
:
28463 gmsgid
= G_("expected end of line");
28466 gmsgid
= G_("expected identifier");
28469 gmsgid
= G_("expected selection-statement");
28472 gmsgid
= G_("expected iteration-statement");
28475 gmsgid
= G_("expected jump-statement");
28478 gmsgid
= G_("expected class-key");
28480 case RT_CLASS_TYPENAME_TEMPLATE
:
28481 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28484 gcc_unreachable ();
28489 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28493 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28494 issue an error message indicating that TOKEN_DESC was expected.
28496 Returns the token consumed, if the token had the appropriate type.
28497 Otherwise, returns NULL.
28499 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28500 within any error as the location of an "opening" token matching
28501 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28502 RT_CLOSE_PAREN). */
28505 cp_parser_require (cp_parser
* parser
,
28506 enum cpp_ttype type
,
28507 required_token token_desc
,
28508 location_t matching_location
)
28510 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28511 return cp_lexer_consume_token (parser
->lexer
);
28514 /* Output the MESSAGE -- unless we're parsing tentatively. */
28515 if (!cp_parser_simulate_error (parser
))
28516 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28517 matching_location
);
28522 /* An error message is produced if the next token is not '>'.
28523 All further tokens are skipped until the desired token is
28524 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28527 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28529 /* Current level of '< ... >'. */
28530 unsigned level
= 0;
28531 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28532 unsigned nesting_depth
= 0;
28534 /* Are we ready, yet? If not, issue error message. */
28535 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28538 /* Skip tokens until the desired token is found. */
28541 /* Peek at the next token. */
28542 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28545 if (!nesting_depth
)
28550 if (cxx_dialect
== cxx98
)
28551 /* C++0x views the `>>' operator as two `>' tokens, but
28554 else if (!nesting_depth
&& level
-- == 0)
28556 /* We've hit a `>>' where the first `>' closes the
28557 template argument list, and the second `>' is
28558 spurious. Just consume the `>>' and stop; we've
28559 already produced at least one error. */
28560 cp_lexer_consume_token (parser
->lexer
);
28563 /* Fall through for C++0x, so we handle the second `>' in
28565 gcc_fallthrough ();
28568 if (!nesting_depth
&& level
-- == 0)
28570 /* We've reached the token we want, consume it and stop. */
28571 cp_lexer_consume_token (parser
->lexer
);
28576 case CPP_OPEN_PAREN
:
28577 case CPP_OPEN_SQUARE
:
28581 case CPP_CLOSE_PAREN
:
28582 case CPP_CLOSE_SQUARE
:
28583 if (nesting_depth
-- == 0)
28588 case CPP_PRAGMA_EOL
:
28589 case CPP_SEMICOLON
:
28590 case CPP_OPEN_BRACE
:
28591 case CPP_CLOSE_BRACE
:
28592 /* The '>' was probably forgotten, don't look further. */
28599 /* Consume this token. */
28600 cp_lexer_consume_token (parser
->lexer
);
28604 /* If the next token is the indicated keyword, consume it. Otherwise,
28605 issue an error message indicating that TOKEN_DESC was expected.
28607 Returns the token consumed, if the token had the appropriate type.
28608 Otherwise, returns NULL. */
28611 cp_parser_require_keyword (cp_parser
* parser
,
28613 required_token token_desc
)
28615 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28617 if (token
&& token
->keyword
!= keyword
)
28619 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28627 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28628 function-definition. */
28631 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28633 return (/* An ordinary function-body begins with an `{'. */
28634 token
->type
== CPP_OPEN_BRACE
28635 /* A ctor-initializer begins with a `:'. */
28636 || token
->type
== CPP_COLON
28637 /* A function-try-block begins with `try'. */
28638 || token
->keyword
== RID_TRY
28639 /* A function-transaction-block begins with `__transaction_atomic'
28640 or `__transaction_relaxed'. */
28641 || token
->keyword
== RID_TRANSACTION_ATOMIC
28642 || token
->keyword
== RID_TRANSACTION_RELAXED
28643 /* The named return value extension begins with `return'. */
28644 || token
->keyword
== RID_RETURN
);
28647 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28651 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28655 token
= cp_lexer_peek_token (parser
->lexer
);
28656 return (token
->type
== CPP_OPEN_BRACE
28657 || (token
->type
== CPP_COLON
28658 && !parser
->colon_doesnt_start_class_def_p
));
28661 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28662 C++0x) ending a template-argument. */
28665 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28669 token
= cp_lexer_peek_token (parser
->lexer
);
28670 return (token
->type
== CPP_COMMA
28671 || token
->type
== CPP_GREATER
28672 || token
->type
== CPP_ELLIPSIS
28673 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28676 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28677 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28680 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28685 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28686 if (token
->type
== CPP_LESS
)
28688 /* Check for the sequence `<::' in the original code. It would be lexed as
28689 `[:', where `[' is a digraph, and there is no whitespace before
28691 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28694 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28695 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28701 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28702 or none_type otherwise. */
28704 static enum tag_types
28705 cp_parser_token_is_class_key (cp_token
* token
)
28707 switch (token
->keyword
)
28712 return record_type
;
28721 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28722 or none_type otherwise or if the token is null. */
28724 static enum tag_types
28725 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28730 switch (token
->keyword
)
28735 return typename_type
;
28742 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28745 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28747 if (type
== error_mark_node
)
28749 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28751 if (permerror (input_location
, "%qs tag used in naming %q#T",
28752 class_key
== union_type
? "union"
28753 : class_key
== record_type
? "struct" : "class",
28755 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28756 "%q#T was previously declared here", type
);
28760 /* Issue an error message if DECL is redeclared with different
28761 access than its original declaration [class.access.spec/3].
28762 This applies to nested classes, nested class templates and
28763 enumerations [class.mem/1]. */
28766 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28769 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28770 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28773 if ((TREE_PRIVATE (decl
)
28774 != (current_access_specifier
== access_private_node
))
28775 || (TREE_PROTECTED (decl
)
28776 != (current_access_specifier
== access_protected_node
)))
28777 error_at (location
, "%qD redeclared with different access", decl
);
28780 /* Look for the `template' keyword, as a syntactic disambiguator.
28781 Return TRUE iff it is present, in which case it will be
28785 cp_parser_optional_template_keyword (cp_parser
*parser
)
28787 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28789 /* In C++98 the `template' keyword can only be used within templates;
28790 outside templates the parser can always figure out what is a
28791 template and what is not. In C++11, per the resolution of DR 468,
28792 `template' is allowed in cases where it is not strictly necessary. */
28793 if (!processing_template_decl
28794 && pedantic
&& cxx_dialect
== cxx98
)
28796 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28797 pedwarn (token
->location
, OPT_Wpedantic
,
28798 "in C++98 %<template%> (as a disambiguator) is only "
28799 "allowed within templates");
28800 /* If this part of the token stream is rescanned, the same
28801 error message would be generated. So, we purge the token
28802 from the stream. */
28803 cp_lexer_purge_token (parser
->lexer
);
28808 /* Consume the `template' keyword. */
28809 cp_lexer_consume_token (parser
->lexer
);
28816 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28817 set PARSER->SCOPE, and perform other related actions. */
28820 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28822 struct tree_check
*check_value
;
28824 /* Get the stored value. */
28825 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28826 /* Set the scope from the stored value. */
28827 parser
->scope
= saved_checks_value (check_value
);
28828 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28829 parser
->object_scope
= NULL_TREE
;
28832 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28833 encounter the end of a block before what we were looking for. */
28836 cp_parser_cache_group (cp_parser
*parser
,
28837 enum cpp_ttype end
,
28842 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28844 /* Abort a parenthesized expression if we encounter a semicolon. */
28845 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28846 && token
->type
== CPP_SEMICOLON
)
28848 /* If we've reached the end of the file, stop. */
28849 if (token
->type
== CPP_EOF
28850 || (end
!= CPP_PRAGMA_EOL
28851 && token
->type
== CPP_PRAGMA_EOL
))
28853 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28854 /* We've hit the end of an enclosing block, so there's been some
28855 kind of syntax error. */
28858 /* Consume the token. */
28859 cp_lexer_consume_token (parser
->lexer
);
28860 /* See if it starts a new group. */
28861 if (token
->type
== CPP_OPEN_BRACE
)
28863 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28864 /* In theory this should probably check end == '}', but
28865 cp_parser_save_member_function_body needs it to exit
28866 after either '}' or ')' when called with ')'. */
28870 else if (token
->type
== CPP_OPEN_PAREN
)
28872 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28873 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28876 else if (token
->type
== CPP_PRAGMA
)
28877 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28878 else if (token
->type
== end
)
28883 /* Like above, for caching a default argument or NSDMI. Both of these are
28884 terminated by a non-nested comma, but it can be unclear whether or not a
28885 comma is nested in a template argument list unless we do more parsing.
28886 In order to handle this ambiguity, when we encounter a ',' after a '<'
28887 we try to parse what follows as a parameter-declaration-list (in the
28888 case of a default argument) or a member-declarator (in the case of an
28889 NSDMI). If that succeeds, then we stop caching. */
28892 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28894 unsigned depth
= 0;
28895 int maybe_template_id
= 0;
28896 cp_token
*first_token
;
28898 tree default_argument
;
28900 /* Add tokens until we have processed the entire default
28901 argument. We add the range [first_token, token). */
28902 first_token
= cp_lexer_peek_token (parser
->lexer
);
28903 if (first_token
->type
== CPP_OPEN_BRACE
)
28905 /* For list-initialization, this is straightforward. */
28906 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
28907 token
= cp_lexer_peek_token (parser
->lexer
);
28913 /* Peek at the next token. */
28914 token
= cp_lexer_peek_token (parser
->lexer
);
28915 /* What we do depends on what token we have. */
28916 switch (token
->type
)
28918 /* In valid code, a default argument must be
28919 immediately followed by a `,' `)', or `...'. */
28921 if (depth
== 0 && maybe_template_id
)
28923 /* If we've seen a '<', we might be in a
28924 template-argument-list. Until Core issue 325 is
28925 resolved, we don't know how this situation ought
28926 to be handled, so try to DTRT. We check whether
28927 what comes after the comma is a valid parameter
28928 declaration list. If it is, then the comma ends
28929 the default argument; otherwise the default
28930 argument continues. */
28931 bool error
= false;
28934 /* Set ITALP so cp_parser_parameter_declaration_list
28935 doesn't decide to commit to this parse. */
28936 bool saved_italp
= parser
->in_template_argument_list_p
;
28937 parser
->in_template_argument_list_p
= true;
28939 cp_parser_parse_tentatively (parser
);
28943 /* Parse declarators until we reach a non-comma or
28944 somthing that cannot be an initializer.
28945 Just checking whether we're looking at a single
28946 declarator is insufficient. Consider:
28947 int var = tuple<T,U>::x;
28948 The template parameter 'U' looks exactly like a
28952 int ctor_dtor_or_conv_p
;
28953 cp_lexer_consume_token (parser
->lexer
);
28954 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28955 &ctor_dtor_or_conv_p
,
28956 /*parenthesized_p=*/NULL
,
28958 /*friend_p=*/false);
28959 peek
= cp_lexer_peek_token (parser
->lexer
);
28960 if (cp_parser_error_occurred (parser
))
28963 while (peek
->type
== CPP_COMMA
);
28964 /* If we met an '=' or ';' then the original comma
28965 was the end of the NSDMI. Otherwise assume
28966 we're still in the NSDMI. */
28967 error
= (peek
->type
!= CPP_EQ
28968 && peek
->type
!= CPP_SEMICOLON
);
28972 cp_lexer_consume_token (parser
->lexer
);
28973 begin_scope (sk_function_parms
, NULL_TREE
);
28974 cp_parser_parameter_declaration_list (parser
, &error
);
28975 pop_bindings_and_leave_scope ();
28977 if (!cp_parser_error_occurred (parser
) && !error
)
28979 cp_parser_abort_tentative_parse (parser
);
28981 parser
->in_template_argument_list_p
= saved_italp
;
28985 case CPP_CLOSE_PAREN
:
28987 /* If we run into a non-nested `;', `}', or `]',
28988 then the code is invalid -- but the default
28989 argument is certainly over. */
28990 case CPP_SEMICOLON
:
28991 case CPP_CLOSE_BRACE
:
28992 case CPP_CLOSE_SQUARE
:
28994 /* Handle correctly int n = sizeof ... ( p ); */
28995 && token
->type
!= CPP_ELLIPSIS
)
28997 /* Update DEPTH, if necessary. */
28998 else if (token
->type
== CPP_CLOSE_PAREN
28999 || token
->type
== CPP_CLOSE_BRACE
29000 || token
->type
== CPP_CLOSE_SQUARE
)
29004 case CPP_OPEN_PAREN
:
29005 case CPP_OPEN_SQUARE
:
29006 case CPP_OPEN_BRACE
:
29012 /* This might be the comparison operator, or it might
29013 start a template argument list. */
29014 ++maybe_template_id
;
29018 if (cxx_dialect
== cxx98
)
29020 /* Fall through for C++0x, which treats the `>>'
29021 operator like two `>' tokens in certain
29023 gcc_fallthrough ();
29028 /* This might be an operator, or it might close a
29029 template argument list. But if a previous '<'
29030 started a template argument list, this will have
29031 closed it, so we can't be in one anymore. */
29032 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29033 if (maybe_template_id
< 0)
29034 maybe_template_id
= 0;
29038 /* If we run out of tokens, issue an error message. */
29040 case CPP_PRAGMA_EOL
:
29041 error_at (token
->location
, "file ends in default argument");
29042 return error_mark_node
;
29046 /* In these cases, we should look for template-ids.
29047 For example, if the default argument is
29048 `X<int, double>()', we need to do name lookup to
29049 figure out whether or not `X' is a template; if
29050 so, the `,' does not end the default argument.
29052 That is not yet done. */
29059 /* If we've reached the end, stop. */
29063 /* Add the token to the token block. */
29064 token
= cp_lexer_consume_token (parser
->lexer
);
29067 /* Create a DEFAULT_ARG to represent the unparsed default
29069 default_argument
= make_node (DEFAULT_ARG
);
29070 DEFARG_TOKENS (default_argument
)
29071 = cp_token_cache_new (first_token
, token
);
29072 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29074 return default_argument
;
29077 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29080 defarg_location (tree default_argument
)
29082 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29083 location_t start
= tokens
->first
->location
;
29084 location_t end
= tokens
->last
->location
;
29085 return make_location (start
, start
, end
);
29088 /* Begin parsing tentatively. We always save tokens while parsing
29089 tentatively so that if the tentative parsing fails we can restore the
29093 cp_parser_parse_tentatively (cp_parser
* parser
)
29095 /* Enter a new parsing context. */
29096 parser
->context
= cp_parser_context_new (parser
->context
);
29097 /* Begin saving tokens. */
29098 cp_lexer_save_tokens (parser
->lexer
);
29099 /* In order to avoid repetitive access control error messages,
29100 access checks are queued up until we are no longer parsing
29102 push_deferring_access_checks (dk_deferred
);
29105 /* Commit to the currently active tentative parse. */
29108 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29110 cp_parser_context
*context
;
29113 /* Mark all of the levels as committed. */
29114 lexer
= parser
->lexer
;
29115 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29117 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29119 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29120 while (!cp_lexer_saving_tokens (lexer
))
29121 lexer
= lexer
->next
;
29122 cp_lexer_commit_tokens (lexer
);
29126 /* Commit to the topmost currently active tentative parse.
29128 Note that this function shouldn't be called when there are
29129 irreversible side-effects while in a tentative state. For
29130 example, we shouldn't create a permanent entry in the symbol
29131 table, or issue an error message that might not apply if the
29132 tentative parse is aborted. */
29135 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29137 cp_parser_context
*context
= parser
->context
;
29138 cp_lexer
*lexer
= parser
->lexer
;
29142 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29144 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29146 while (!cp_lexer_saving_tokens (lexer
))
29147 lexer
= lexer
->next
;
29148 cp_lexer_commit_tokens (lexer
);
29152 /* Abort the currently active tentative parse. All consumed tokens
29153 will be rolled back, and no diagnostics will be issued. */
29156 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29158 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29159 || errorcount
> 0);
29160 cp_parser_simulate_error (parser
);
29161 /* Now, pretend that we want to see if the construct was
29162 successfully parsed. */
29163 cp_parser_parse_definitely (parser
);
29166 /* Stop parsing tentatively. If a parse error has occurred, restore the
29167 token stream. Otherwise, commit to the tokens we have consumed.
29168 Returns true if no error occurred; false otherwise. */
29171 cp_parser_parse_definitely (cp_parser
* parser
)
29173 bool error_occurred
;
29174 cp_parser_context
*context
;
29176 /* Remember whether or not an error occurred, since we are about to
29177 destroy that information. */
29178 error_occurred
= cp_parser_error_occurred (parser
);
29179 /* Remove the topmost context from the stack. */
29180 context
= parser
->context
;
29181 parser
->context
= context
->next
;
29182 /* If no parse errors occurred, commit to the tentative parse. */
29183 if (!error_occurred
)
29185 /* Commit to the tokens read tentatively, unless that was
29187 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29188 cp_lexer_commit_tokens (parser
->lexer
);
29190 pop_to_parent_deferring_access_checks ();
29192 /* Otherwise, if errors occurred, roll back our state so that things
29193 are just as they were before we began the tentative parse. */
29196 cp_lexer_rollback_tokens (parser
->lexer
);
29197 pop_deferring_access_checks ();
29199 /* Add the context to the front of the free list. */
29200 context
->next
= cp_parser_context_free_list
;
29201 cp_parser_context_free_list
= context
;
29203 return !error_occurred
;
29206 /* Returns true if we are parsing tentatively and are not committed to
29207 this tentative parse. */
29210 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29212 return (cp_parser_parsing_tentatively (parser
)
29213 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29216 /* Returns nonzero iff an error has occurred during the most recent
29217 tentative parse. */
29220 cp_parser_error_occurred (cp_parser
* parser
)
29222 return (cp_parser_parsing_tentatively (parser
)
29223 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29226 /* Returns nonzero if GNU extensions are allowed. */
29229 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29231 return parser
->allow_gnu_extensions_p
;
29234 /* Objective-C++ Productions */
29237 /* Parse an Objective-C expression, which feeds into a primary-expression
29241 objc-message-expression
29242 objc-string-literal
29243 objc-encode-expression
29244 objc-protocol-expression
29245 objc-selector-expression
29247 Returns a tree representation of the expression. */
29250 cp_parser_objc_expression (cp_parser
* parser
)
29252 /* Try to figure out what kind of declaration is present. */
29253 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29257 case CPP_OPEN_SQUARE
:
29258 return cp_parser_objc_message_expression (parser
);
29260 case CPP_OBJC_STRING
:
29261 kwd
= cp_lexer_consume_token (parser
->lexer
);
29262 return objc_build_string_object (kwd
->u
.value
);
29265 switch (kwd
->keyword
)
29267 case RID_AT_ENCODE
:
29268 return cp_parser_objc_encode_expression (parser
);
29270 case RID_AT_PROTOCOL
:
29271 return cp_parser_objc_protocol_expression (parser
);
29273 case RID_AT_SELECTOR
:
29274 return cp_parser_objc_selector_expression (parser
);
29281 error_at (kwd
->location
,
29282 "misplaced %<@%D%> Objective-C++ construct",
29284 cp_parser_skip_to_end_of_block_or_statement (parser
);
29287 return error_mark_node
;
29290 /* Parse an Objective-C message expression.
29292 objc-message-expression:
29293 [ objc-message-receiver objc-message-args ]
29295 Returns a representation of an Objective-C message. */
29298 cp_parser_objc_message_expression (cp_parser
* parser
)
29300 tree receiver
, messageargs
;
29302 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29303 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29304 receiver
= cp_parser_objc_message_receiver (parser
);
29305 messageargs
= cp_parser_objc_message_args (parser
);
29306 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29307 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29309 tree result
= objc_build_message_expr (receiver
, messageargs
);
29311 /* Construct a location e.g.
29314 ranging from the '[' to the ']', with the caret at the start. */
29315 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29316 protected_set_expr_location (result
, combined_loc
);
29321 /* Parse an objc-message-receiver.
29323 objc-message-receiver:
29325 simple-type-specifier
29327 Returns a representation of the type or expression. */
29330 cp_parser_objc_message_receiver (cp_parser
* parser
)
29334 /* An Objective-C message receiver may be either (1) a type
29335 or (2) an expression. */
29336 cp_parser_parse_tentatively (parser
);
29337 rcv
= cp_parser_expression (parser
);
29339 /* If that worked out, fine. */
29340 if (cp_parser_parse_definitely (parser
))
29343 cp_parser_parse_tentatively (parser
);
29344 rcv
= cp_parser_simple_type_specifier (parser
,
29345 /*decl_specs=*/NULL
,
29346 CP_PARSER_FLAGS_NONE
);
29348 if (cp_parser_parse_definitely (parser
))
29349 return objc_get_class_reference (rcv
);
29351 cp_parser_error (parser
, "objective-c++ message receiver expected");
29352 return error_mark_node
;
29355 /* Parse the arguments and selectors comprising an Objective-C message.
29360 objc-selector-args , objc-comma-args
29362 objc-selector-args:
29363 objc-selector [opt] : assignment-expression
29364 objc-selector-args objc-selector [opt] : assignment-expression
29367 assignment-expression
29368 objc-comma-args , assignment-expression
29370 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29371 selector arguments and TREE_VALUE containing a list of comma
29375 cp_parser_objc_message_args (cp_parser
* parser
)
29377 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29378 bool maybe_unary_selector_p
= true;
29379 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29381 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29383 tree selector
= NULL_TREE
, arg
;
29385 if (token
->type
!= CPP_COLON
)
29386 selector
= cp_parser_objc_selector (parser
);
29388 /* Detect if we have a unary selector. */
29389 if (maybe_unary_selector_p
29390 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29391 return build_tree_list (selector
, NULL_TREE
);
29393 maybe_unary_selector_p
= false;
29394 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29395 arg
= cp_parser_assignment_expression (parser
);
29398 = chainon (sel_args
,
29399 build_tree_list (selector
, arg
));
29401 token
= cp_lexer_peek_token (parser
->lexer
);
29404 /* Handle non-selector arguments, if any. */
29405 while (token
->type
== CPP_COMMA
)
29409 cp_lexer_consume_token (parser
->lexer
);
29410 arg
= cp_parser_assignment_expression (parser
);
29413 = chainon (addl_args
,
29414 build_tree_list (NULL_TREE
, arg
));
29416 token
= cp_lexer_peek_token (parser
->lexer
);
29419 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29421 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29422 return build_tree_list (error_mark_node
, error_mark_node
);
29425 return build_tree_list (sel_args
, addl_args
);
29428 /* Parse an Objective-C encode expression.
29430 objc-encode-expression:
29431 @encode objc-typename
29433 Returns an encoded representation of the type argument. */
29436 cp_parser_objc_encode_expression (cp_parser
* parser
)
29440 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29442 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29443 matching_parens parens
;
29444 parens
.require_open (parser
);
29445 token
= cp_lexer_peek_token (parser
->lexer
);
29446 type
= complete_type (cp_parser_type_id (parser
));
29447 parens
.require_close (parser
);
29451 error_at (token
->location
,
29452 "%<@encode%> must specify a type as an argument");
29453 return error_mark_node
;
29456 /* This happens if we find @encode(T) (where T is a template
29457 typename or something dependent on a template typename) when
29458 parsing a template. In that case, we can't compile it
29459 immediately, but we rather create an AT_ENCODE_EXPR which will
29460 need to be instantiated when the template is used.
29462 if (dependent_type_p (type
))
29464 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29465 TREE_READONLY (value
) = 1;
29470 /* Build a location of the form:
29473 with caret==start at the @ token, finishing at the close paren. */
29474 location_t combined_loc
29475 = make_location (start_loc
, start_loc
,
29476 cp_lexer_previous_token (parser
->lexer
)->location
);
29478 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29481 /* Parse an Objective-C @defs expression. */
29484 cp_parser_objc_defs_expression (cp_parser
*parser
)
29488 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29489 matching_parens parens
;
29490 parens
.require_open (parser
);
29491 name
= cp_parser_identifier (parser
);
29492 parens
.require_close (parser
);
29494 return objc_get_class_ivars (name
);
29497 /* Parse an Objective-C protocol expression.
29499 objc-protocol-expression:
29500 @protocol ( identifier )
29502 Returns a representation of the protocol expression. */
29505 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29508 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29510 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29511 matching_parens parens
;
29512 parens
.require_open (parser
);
29513 proto
= cp_parser_identifier (parser
);
29514 parens
.require_close (parser
);
29516 /* Build a location of the form:
29519 with caret==start at the @ token, finishing at the close paren. */
29520 location_t combined_loc
29521 = make_location (start_loc
, start_loc
,
29522 cp_lexer_previous_token (parser
->lexer
)->location
);
29523 tree result
= objc_build_protocol_expr (proto
);
29524 protected_set_expr_location (result
, combined_loc
);
29528 /* Parse an Objective-C selector expression.
29530 objc-selector-expression:
29531 @selector ( objc-method-signature )
29533 objc-method-signature:
29539 objc-selector-seq objc-selector :
29541 Returns a representation of the method selector. */
29544 cp_parser_objc_selector_expression (cp_parser
* parser
)
29546 tree sel_seq
= NULL_TREE
;
29547 bool maybe_unary_selector_p
= true;
29549 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29551 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29552 matching_parens parens
;
29553 parens
.require_open (parser
);
29554 token
= cp_lexer_peek_token (parser
->lexer
);
29556 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29557 || token
->type
== CPP_SCOPE
)
29559 tree selector
= NULL_TREE
;
29561 if (token
->type
!= CPP_COLON
29562 || token
->type
== CPP_SCOPE
)
29563 selector
= cp_parser_objc_selector (parser
);
29565 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29566 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29568 /* Detect if we have a unary selector. */
29569 if (maybe_unary_selector_p
)
29571 sel_seq
= selector
;
29572 goto finish_selector
;
29576 cp_parser_error (parser
, "expected %<:%>");
29579 maybe_unary_selector_p
= false;
29580 token
= cp_lexer_consume_token (parser
->lexer
);
29582 if (token
->type
== CPP_SCOPE
)
29585 = chainon (sel_seq
,
29586 build_tree_list (selector
, NULL_TREE
));
29588 = chainon (sel_seq
,
29589 build_tree_list (NULL_TREE
, NULL_TREE
));
29593 = chainon (sel_seq
,
29594 build_tree_list (selector
, NULL_TREE
));
29596 token
= cp_lexer_peek_token (parser
->lexer
);
29600 parens
.require_close (parser
);
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 (loc
, loc
,
29609 cp_lexer_previous_token (parser
->lexer
)->location
);
29610 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29611 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29612 protected_set_expr_location (result
, combined_loc
);
29616 /* Parse a list of identifiers.
29618 objc-identifier-list:
29620 objc-identifier-list , identifier
29622 Returns a TREE_LIST of identifier nodes. */
29625 cp_parser_objc_identifier_list (cp_parser
* parser
)
29631 identifier
= cp_parser_identifier (parser
);
29632 if (identifier
== error_mark_node
)
29633 return error_mark_node
;
29635 list
= build_tree_list (NULL_TREE
, identifier
);
29636 sep
= cp_lexer_peek_token (parser
->lexer
);
29638 while (sep
->type
== CPP_COMMA
)
29640 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29641 identifier
= cp_parser_identifier (parser
);
29642 if (identifier
== error_mark_node
)
29645 list
= chainon (list
, build_tree_list (NULL_TREE
,
29647 sep
= cp_lexer_peek_token (parser
->lexer
);
29653 /* Parse an Objective-C alias declaration.
29655 objc-alias-declaration:
29656 @compatibility_alias identifier identifier ;
29658 This function registers the alias mapping with the Objective-C front end.
29659 It returns nothing. */
29662 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29666 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29667 alias
= cp_parser_identifier (parser
);
29668 orig
= cp_parser_identifier (parser
);
29669 objc_declare_alias (alias
, orig
);
29670 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29673 /* Parse an Objective-C class forward-declaration.
29675 objc-class-declaration:
29676 @class objc-identifier-list ;
29678 The function registers the forward declarations with the Objective-C
29679 front end. It returns nothing. */
29682 cp_parser_objc_class_declaration (cp_parser
* parser
)
29684 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29689 id
= cp_parser_identifier (parser
);
29690 if (id
== error_mark_node
)
29693 objc_declare_class (id
);
29695 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29696 cp_lexer_consume_token (parser
->lexer
);
29700 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29703 /* Parse a list of Objective-C protocol references.
29705 objc-protocol-refs-opt:
29706 objc-protocol-refs [opt]
29708 objc-protocol-refs:
29709 < objc-identifier-list >
29711 Returns a TREE_LIST of identifiers, if any. */
29714 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29716 tree protorefs
= NULL_TREE
;
29718 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29720 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29721 protorefs
= cp_parser_objc_identifier_list (parser
);
29722 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29728 /* Parse a Objective-C visibility specification. */
29731 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29733 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29735 switch (vis
->keyword
)
29737 case RID_AT_PRIVATE
:
29738 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29740 case RID_AT_PROTECTED
:
29741 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29743 case RID_AT_PUBLIC
:
29744 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29746 case RID_AT_PACKAGE
:
29747 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29753 /* Eat '@private'/'@protected'/'@public'. */
29754 cp_lexer_consume_token (parser
->lexer
);
29757 /* Parse an Objective-C method type. Return 'true' if it is a class
29758 (+) method, and 'false' if it is an instance (-) method. */
29761 cp_parser_objc_method_type (cp_parser
* parser
)
29763 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29769 /* Parse an Objective-C protocol qualifier. */
29772 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29774 tree quals
= NULL_TREE
, node
;
29775 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29777 node
= token
->u
.value
;
29779 while (node
&& identifier_p (node
)
29780 && (node
== ridpointers
[(int) RID_IN
]
29781 || node
== ridpointers
[(int) RID_OUT
]
29782 || node
== ridpointers
[(int) RID_INOUT
]
29783 || node
== ridpointers
[(int) RID_BYCOPY
]
29784 || node
== ridpointers
[(int) RID_BYREF
]
29785 || node
== ridpointers
[(int) RID_ONEWAY
]))
29787 quals
= tree_cons (NULL_TREE
, node
, quals
);
29788 cp_lexer_consume_token (parser
->lexer
);
29789 token
= cp_lexer_peek_token (parser
->lexer
);
29790 node
= token
->u
.value
;
29796 /* Parse an Objective-C typename. */
29799 cp_parser_objc_typename (cp_parser
* parser
)
29801 tree type_name
= NULL_TREE
;
29803 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29805 tree proto_quals
, cp_type
= NULL_TREE
;
29807 matching_parens parens
;
29808 parens
.consume_open (parser
); /* Eat '('. */
29809 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29811 /* An ObjC type name may consist of just protocol qualifiers, in which
29812 case the type shall default to 'id'. */
29813 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29815 cp_type
= cp_parser_type_id (parser
);
29817 /* If the type could not be parsed, an error has already
29818 been produced. For error recovery, behave as if it had
29819 not been specified, which will use the default type
29821 if (cp_type
== error_mark_node
)
29823 cp_type
= NULL_TREE
;
29824 /* We need to skip to the closing parenthesis as
29825 cp_parser_type_id() does not seem to do it for
29827 cp_parser_skip_to_closing_parenthesis (parser
,
29828 /*recovering=*/true,
29829 /*or_comma=*/false,
29830 /*consume_paren=*/false);
29834 parens
.require_close (parser
);
29835 type_name
= build_tree_list (proto_quals
, cp_type
);
29841 /* Check to see if TYPE refers to an Objective-C selector name. */
29844 cp_parser_objc_selector_p (enum cpp_ttype type
)
29846 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29847 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29848 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29849 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29850 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29853 /* Parse an Objective-C selector. */
29856 cp_parser_objc_selector (cp_parser
* parser
)
29858 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29860 if (!cp_parser_objc_selector_p (token
->type
))
29862 error_at (token
->location
, "invalid Objective-C++ selector name");
29863 return error_mark_node
;
29866 /* C++ operator names are allowed to appear in ObjC selectors. */
29867 switch (token
->type
)
29869 case CPP_AND_AND
: return get_identifier ("and");
29870 case CPP_AND_EQ
: return get_identifier ("and_eq");
29871 case CPP_AND
: return get_identifier ("bitand");
29872 case CPP_OR
: return get_identifier ("bitor");
29873 case CPP_COMPL
: return get_identifier ("compl");
29874 case CPP_NOT
: return get_identifier ("not");
29875 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29876 case CPP_OR_OR
: return get_identifier ("or");
29877 case CPP_OR_EQ
: return get_identifier ("or_eq");
29878 case CPP_XOR
: return get_identifier ("xor");
29879 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29880 default: return token
->u
.value
;
29884 /* Parse an Objective-C params list. */
29887 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29889 tree params
= NULL_TREE
;
29890 bool maybe_unary_selector_p
= true;
29891 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29893 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29895 tree selector
= NULL_TREE
, type_name
, identifier
;
29896 tree parm_attr
= NULL_TREE
;
29898 if (token
->keyword
== RID_ATTRIBUTE
)
29901 if (token
->type
!= CPP_COLON
)
29902 selector
= cp_parser_objc_selector (parser
);
29904 /* Detect if we have a unary selector. */
29905 if (maybe_unary_selector_p
29906 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29908 params
= selector
; /* Might be followed by attributes. */
29912 maybe_unary_selector_p
= false;
29913 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29915 /* Something went quite wrong. There should be a colon
29916 here, but there is not. Stop parsing parameters. */
29919 type_name
= cp_parser_objc_typename (parser
);
29920 /* New ObjC allows attributes on parameters too. */
29921 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
29922 parm_attr
= cp_parser_attributes_opt (parser
);
29923 identifier
= cp_parser_identifier (parser
);
29927 objc_build_keyword_decl (selector
,
29932 token
= cp_lexer_peek_token (parser
->lexer
);
29935 if (params
== NULL_TREE
)
29937 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29938 return error_mark_node
;
29941 /* We allow tail attributes for the method. */
29942 if (token
->keyword
== RID_ATTRIBUTE
)
29944 *attributes
= cp_parser_attributes_opt (parser
);
29945 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29946 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29948 cp_parser_error (parser
,
29949 "method attributes must be specified at the end");
29950 return error_mark_node
;
29953 if (params
== NULL_TREE
)
29955 cp_parser_error (parser
, "objective-c++ method declaration is expected");
29956 return error_mark_node
;
29961 /* Parse the non-keyword Objective-C params. */
29964 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
29967 tree params
= make_node (TREE_LIST
);
29968 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29969 *ellipsisp
= false; /* Initially, assume no ellipsis. */
29971 while (token
->type
== CPP_COMMA
)
29973 cp_parameter_declarator
*parmdecl
;
29976 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29977 token
= cp_lexer_peek_token (parser
->lexer
);
29979 if (token
->type
== CPP_ELLIPSIS
)
29981 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
29983 token
= cp_lexer_peek_token (parser
->lexer
);
29987 /* TODO: parse attributes for tail parameters. */
29988 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
29989 parm
= grokdeclarator (parmdecl
->declarator
,
29990 &parmdecl
->decl_specifiers
,
29991 PARM
, /*initialized=*/0,
29992 /*attrlist=*/NULL
);
29994 chainon (params
, build_tree_list (NULL_TREE
, parm
));
29995 token
= cp_lexer_peek_token (parser
->lexer
);
29998 /* We allow tail attributes for the method. */
29999 if (token
->keyword
== RID_ATTRIBUTE
)
30001 if (*attributes
== NULL_TREE
)
30003 *attributes
= cp_parser_attributes_opt (parser
);
30004 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30005 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30009 /* We have an error, but parse the attributes, so that we can
30011 *attributes
= cp_parser_attributes_opt (parser
);
30013 cp_parser_error (parser
,
30014 "method attributes must be specified at the end");
30015 return error_mark_node
;
30021 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30024 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30026 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30028 /* If the next token is `extern' and the following token is a string
30029 literal, then we have a linkage specification. */
30030 if (token
->keyword
== RID_EXTERN
30031 && cp_parser_is_pure_string_literal
30032 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30033 cp_parser_linkage_specification (parser
);
30034 /* Handle #pragma, if any. */
30035 else if (token
->type
== CPP_PRAGMA
)
30036 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30037 /* Allow stray semicolons. */
30038 else if (token
->type
== CPP_SEMICOLON
)
30039 cp_lexer_consume_token (parser
->lexer
);
30040 /* Mark methods as optional or required, when building protocols. */
30041 else if (token
->keyword
== RID_AT_OPTIONAL
)
30043 cp_lexer_consume_token (parser
->lexer
);
30044 objc_set_method_opt (true);
30046 else if (token
->keyword
== RID_AT_REQUIRED
)
30048 cp_lexer_consume_token (parser
->lexer
);
30049 objc_set_method_opt (false);
30051 else if (token
->keyword
== RID_NAMESPACE
)
30052 cp_parser_namespace_definition (parser
);
30053 /* Other stray characters must generate errors. */
30054 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30056 cp_lexer_consume_token (parser
->lexer
);
30057 error ("stray %qs between Objective-C++ methods",
30058 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30060 /* Finally, try to parse a block-declaration, or a function-definition. */
30062 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30065 /* Parse a method signature. */
30068 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30070 tree rettype
, kwdparms
, optparms
;
30071 bool ellipsis
= false;
30072 bool is_class_method
;
30074 is_class_method
= cp_parser_objc_method_type (parser
);
30075 rettype
= cp_parser_objc_typename (parser
);
30076 *attributes
= NULL_TREE
;
30077 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30078 if (kwdparms
== error_mark_node
)
30079 return error_mark_node
;
30080 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30081 if (optparms
== error_mark_node
)
30082 return error_mark_node
;
30084 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30088 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30091 cp_lexer_save_tokens (parser
->lexer
);
30092 tattr
= cp_parser_attributes_opt (parser
);
30093 gcc_assert (tattr
) ;
30095 /* If the attributes are followed by a method introducer, this is not allowed.
30096 Dump the attributes and flag the situation. */
30097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30098 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30101 /* Otherwise, the attributes introduce some interstitial code, possibly so
30102 rewind to allow that check. */
30103 cp_lexer_rollback_tokens (parser
->lexer
);
30107 /* Parse an Objective-C method prototype list. */
30110 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30112 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30114 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30116 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30118 tree attributes
, sig
;
30119 bool is_class_method
;
30120 if (token
->type
== CPP_PLUS
)
30121 is_class_method
= true;
30123 is_class_method
= false;
30124 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30125 if (sig
== error_mark_node
)
30127 cp_parser_skip_to_end_of_block_or_statement (parser
);
30128 token
= cp_lexer_peek_token (parser
->lexer
);
30131 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30132 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30134 else if (token
->keyword
== RID_AT_PROPERTY
)
30135 cp_parser_objc_at_property_declaration (parser
);
30136 else if (token
->keyword
== RID_ATTRIBUTE
30137 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30138 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30140 "prefix attributes are ignored for methods");
30142 /* Allow for interspersed non-ObjC++ code. */
30143 cp_parser_objc_interstitial_code (parser
);
30145 token
= cp_lexer_peek_token (parser
->lexer
);
30148 if (token
->type
!= CPP_EOF
)
30149 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30151 cp_parser_error (parser
, "expected %<@end%>");
30153 objc_finish_interface ();
30156 /* Parse an Objective-C method definition list. */
30159 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30161 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30163 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30167 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30170 tree sig
, attribute
;
30171 bool is_class_method
;
30172 if (token
->type
== CPP_PLUS
)
30173 is_class_method
= true;
30175 is_class_method
= false;
30176 push_deferring_access_checks (dk_deferred
);
30177 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30178 if (sig
== error_mark_node
)
30180 cp_parser_skip_to_end_of_block_or_statement (parser
);
30181 token
= cp_lexer_peek_token (parser
->lexer
);
30184 objc_start_method_definition (is_class_method
, sig
, attribute
,
30187 /* For historical reasons, we accept an optional semicolon. */
30188 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30189 cp_lexer_consume_token (parser
->lexer
);
30191 ptk
= cp_lexer_peek_token (parser
->lexer
);
30192 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30193 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30195 perform_deferred_access_checks (tf_warning_or_error
);
30196 stop_deferring_access_checks ();
30197 meth
= cp_parser_function_definition_after_declarator (parser
,
30199 pop_deferring_access_checks ();
30200 objc_finish_method_definition (meth
);
30203 /* The following case will be removed once @synthesize is
30204 completely implemented. */
30205 else if (token
->keyword
== RID_AT_PROPERTY
)
30206 cp_parser_objc_at_property_declaration (parser
);
30207 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30208 cp_parser_objc_at_synthesize_declaration (parser
);
30209 else if (token
->keyword
== RID_AT_DYNAMIC
)
30210 cp_parser_objc_at_dynamic_declaration (parser
);
30211 else if (token
->keyword
== RID_ATTRIBUTE
30212 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30213 warning_at (token
->location
, OPT_Wattributes
,
30214 "prefix attributes are ignored for methods");
30216 /* Allow for interspersed non-ObjC++ code. */
30217 cp_parser_objc_interstitial_code (parser
);
30219 token
= cp_lexer_peek_token (parser
->lexer
);
30222 if (token
->type
!= CPP_EOF
)
30223 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30225 cp_parser_error (parser
, "expected %<@end%>");
30227 objc_finish_implementation ();
30230 /* Parse Objective-C ivars. */
30233 cp_parser_objc_class_ivars (cp_parser
* parser
)
30235 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30237 if (token
->type
!= CPP_OPEN_BRACE
)
30238 return; /* No ivars specified. */
30240 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30241 token
= cp_lexer_peek_token (parser
->lexer
);
30243 while (token
->type
!= CPP_CLOSE_BRACE
30244 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30246 cp_decl_specifier_seq declspecs
;
30247 int decl_class_or_enum_p
;
30248 tree prefix_attributes
;
30250 cp_parser_objc_visibility_spec (parser
);
30252 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30255 cp_parser_decl_specifier_seq (parser
,
30256 CP_PARSER_FLAGS_OPTIONAL
,
30258 &decl_class_or_enum_p
);
30260 /* auto, register, static, extern, mutable. */
30261 if (declspecs
.storage_class
!= sc_none
)
30263 cp_parser_error (parser
, "invalid type for instance variable");
30264 declspecs
.storage_class
= sc_none
;
30267 /* thread_local. */
30268 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30270 cp_parser_error (parser
, "invalid type for instance variable");
30271 declspecs
.locations
[ds_thread
] = 0;
30275 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30277 cp_parser_error (parser
, "invalid type for instance variable");
30278 declspecs
.locations
[ds_typedef
] = 0;
30281 prefix_attributes
= declspecs
.attributes
;
30282 declspecs
.attributes
= NULL_TREE
;
30284 /* Keep going until we hit the `;' at the end of the
30286 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30288 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30289 cp_declarator
*declarator
= NULL
;
30290 int ctor_dtor_or_conv_p
;
30292 /* Check for a (possibly unnamed) bitfield declaration. */
30293 token
= cp_lexer_peek_token (parser
->lexer
);
30294 if (token
->type
== CPP_COLON
)
30297 if (token
->type
== CPP_NAME
30298 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30301 /* Get the name of the bitfield. */
30302 declarator
= make_id_declarator (NULL_TREE
,
30303 cp_parser_identifier (parser
),
30307 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30308 /* Get the width of the bitfield. */
30310 = cp_parser_constant_expression (parser
);
30314 /* Parse the declarator. */
30316 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30317 &ctor_dtor_or_conv_p
,
30318 /*parenthesized_p=*/NULL
,
30319 /*member_p=*/false,
30320 /*friend_p=*/false);
30323 /* Look for attributes that apply to the ivar. */
30324 attributes
= cp_parser_attributes_opt (parser
);
30325 /* Remember which attributes are prefix attributes and
30327 first_attribute
= attributes
;
30328 /* Combine the attributes. */
30329 attributes
= attr_chainon (prefix_attributes
, attributes
);
30332 /* Create the bitfield declaration. */
30333 decl
= grokbitfield (declarator
, &declspecs
,
30334 width
, NULL_TREE
, attributes
);
30336 decl
= grokfield (declarator
, &declspecs
,
30337 NULL_TREE
, /*init_const_expr_p=*/false,
30338 NULL_TREE
, attributes
);
30340 /* Add the instance variable. */
30341 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30342 objc_add_instance_variable (decl
);
30344 /* Reset PREFIX_ATTRIBUTES. */
30345 if (attributes
!= error_mark_node
)
30347 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30348 attributes
= TREE_CHAIN (attributes
);
30350 TREE_CHAIN (attributes
) = NULL_TREE
;
30353 token
= cp_lexer_peek_token (parser
->lexer
);
30355 if (token
->type
== CPP_COMMA
)
30357 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30363 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30364 token
= cp_lexer_peek_token (parser
->lexer
);
30367 if (token
->keyword
== RID_AT_END
)
30368 cp_parser_error (parser
, "expected %<}%>");
30370 /* Do not consume the RID_AT_END, so it will be read again as terminating
30371 the @interface of @implementation. */
30372 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30373 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30375 /* For historical reasons, we accept an optional semicolon. */
30376 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30377 cp_lexer_consume_token (parser
->lexer
);
30380 /* Parse an Objective-C protocol declaration. */
30383 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30385 tree proto
, protorefs
;
30388 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30389 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30391 tok
= cp_lexer_peek_token (parser
->lexer
);
30392 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30393 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30397 /* See if we have a forward declaration or a definition. */
30398 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30400 /* Try a forward declaration first. */
30401 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30407 id
= cp_parser_identifier (parser
);
30408 if (id
== error_mark_node
)
30411 objc_declare_protocol (id
, attributes
);
30413 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30414 cp_lexer_consume_token (parser
->lexer
);
30418 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30421 /* Ok, we got a full-fledged definition (or at least should). */
30424 proto
= cp_parser_identifier (parser
);
30425 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30426 objc_start_protocol (proto
, protorefs
, attributes
);
30427 cp_parser_objc_method_prototype_list (parser
);
30431 /* Parse an Objective-C superclass or category. */
30434 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30437 tree
*categ
, bool *is_class_extension
)
30439 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30441 *super
= *categ
= NULL_TREE
;
30442 *is_class_extension
= false;
30443 if (next
->type
== CPP_COLON
)
30445 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30446 *super
= cp_parser_identifier (parser
);
30448 else if (next
->type
== CPP_OPEN_PAREN
)
30450 matching_parens parens
;
30451 parens
.consume_open (parser
); /* Eat '('. */
30453 /* If there is no category name, and this is an @interface, we
30454 have a class extension. */
30455 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30457 *categ
= NULL_TREE
;
30458 *is_class_extension
= true;
30461 *categ
= cp_parser_identifier (parser
);
30463 parens
.require_close (parser
);
30467 /* Parse an Objective-C class interface. */
30470 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30472 tree name
, super
, categ
, protos
;
30473 bool is_class_extension
;
30475 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30476 name
= cp_parser_identifier (parser
);
30477 if (name
== error_mark_node
)
30479 /* It's hard to recover because even if valid @interface stuff
30480 is to follow, we can't compile it (or validate it) if we
30481 don't even know which class it refers to. Let's assume this
30482 was a stray '@interface' token in the stream and skip it.
30486 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30487 &is_class_extension
);
30488 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30490 /* We have either a class or a category on our hands. */
30491 if (categ
|| is_class_extension
)
30492 objc_start_category_interface (name
, categ
, protos
, attributes
);
30495 objc_start_class_interface (name
, super
, protos
, attributes
);
30496 /* Handle instance variable declarations, if any. */
30497 cp_parser_objc_class_ivars (parser
);
30498 objc_continue_interface ();
30501 cp_parser_objc_method_prototype_list (parser
);
30504 /* Parse an Objective-C class implementation. */
30507 cp_parser_objc_class_implementation (cp_parser
* parser
)
30509 tree name
, super
, categ
;
30510 bool is_class_extension
;
30512 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30513 name
= cp_parser_identifier (parser
);
30514 if (name
== error_mark_node
)
30516 /* It's hard to recover because even if valid @implementation
30517 stuff is to follow, we can't compile it (or validate it) if
30518 we don't even know which class it refers to. Let's assume
30519 this was a stray '@implementation' token in the stream and
30524 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30525 &is_class_extension
);
30527 /* We have either a class or a category on our hands. */
30529 objc_start_category_implementation (name
, categ
);
30532 objc_start_class_implementation (name
, super
);
30533 /* Handle instance variable declarations, if any. */
30534 cp_parser_objc_class_ivars (parser
);
30535 objc_continue_implementation ();
30538 cp_parser_objc_method_definition_list (parser
);
30541 /* Consume the @end token and finish off the implementation. */
30544 cp_parser_objc_end_implementation (cp_parser
* parser
)
30546 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30547 objc_finish_implementation ();
30550 /* Parse an Objective-C declaration. */
30553 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30555 /* Try to figure out what kind of declaration is present. */
30556 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30559 switch (kwd
->keyword
)
30564 error_at (kwd
->location
, "attributes may not be specified before"
30565 " the %<@%D%> Objective-C++ keyword",
30569 case RID_AT_IMPLEMENTATION
:
30570 warning_at (kwd
->location
, OPT_Wattributes
,
30571 "prefix attributes are ignored before %<@%D%>",
30578 switch (kwd
->keyword
)
30581 cp_parser_objc_alias_declaration (parser
);
30584 cp_parser_objc_class_declaration (parser
);
30586 case RID_AT_PROTOCOL
:
30587 cp_parser_objc_protocol_declaration (parser
, attributes
);
30589 case RID_AT_INTERFACE
:
30590 cp_parser_objc_class_interface (parser
, attributes
);
30592 case RID_AT_IMPLEMENTATION
:
30593 cp_parser_objc_class_implementation (parser
);
30596 cp_parser_objc_end_implementation (parser
);
30599 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30601 cp_parser_skip_to_end_of_block_or_statement (parser
);
30605 /* Parse an Objective-C try-catch-finally statement.
30607 objc-try-catch-finally-stmt:
30608 @try compound-statement objc-catch-clause-seq [opt]
30609 objc-finally-clause [opt]
30611 objc-catch-clause-seq:
30612 objc-catch-clause objc-catch-clause-seq [opt]
30615 @catch ( objc-exception-declaration ) compound-statement
30617 objc-finally-clause:
30618 @finally compound-statement
30620 objc-exception-declaration:
30621 parameter-declaration
30624 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30628 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30629 for C. Keep them in sync. */
30632 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30634 location_t location
;
30637 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30638 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30639 objc_maybe_warn_exceptions (location
);
30640 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30641 node, lest it get absorbed into the surrounding block. */
30642 stmt
= push_stmt_list ();
30643 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30644 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30646 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30648 cp_parameter_declarator
*parm
;
30649 tree parameter_declaration
= error_mark_node
;
30650 bool seen_open_paren
= false;
30651 matching_parens parens
;
30653 cp_lexer_consume_token (parser
->lexer
);
30654 if (parens
.require_open (parser
))
30655 seen_open_paren
= true;
30656 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30658 /* We have "@catch (...)" (where the '...' are literally
30659 what is in the code). Skip the '...'.
30660 parameter_declaration is set to NULL_TREE, and
30661 objc_being_catch_clauses() knows that that means
30663 cp_lexer_consume_token (parser
->lexer
);
30664 parameter_declaration
= NULL_TREE
;
30668 /* We have "@catch (NSException *exception)" or something
30669 like that. Parse the parameter declaration. */
30670 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30672 parameter_declaration
= error_mark_node
;
30674 parameter_declaration
= grokdeclarator (parm
->declarator
,
30675 &parm
->decl_specifiers
,
30676 PARM
, /*initialized=*/0,
30677 /*attrlist=*/NULL
);
30679 if (seen_open_paren
)
30680 parens
.require_close (parser
);
30683 /* If there was no open parenthesis, we are recovering from
30684 an error, and we are trying to figure out what mistake
30685 the user has made. */
30687 /* If there is an immediate closing parenthesis, the user
30688 probably forgot the opening one (ie, they typed "@catch
30689 NSException *e)". Parse the closing parenthesis and keep
30691 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30692 cp_lexer_consume_token (parser
->lexer
);
30694 /* If these is no immediate closing parenthesis, the user
30695 probably doesn't know that parenthesis are required at
30696 all (ie, they typed "@catch NSException *e"). So, just
30697 forget about the closing parenthesis and keep going. */
30699 objc_begin_catch_clause (parameter_declaration
);
30700 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30701 objc_finish_catch_clause ();
30703 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30705 cp_lexer_consume_token (parser
->lexer
);
30706 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30707 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30708 node, lest it get absorbed into the surrounding block. */
30709 stmt
= push_stmt_list ();
30710 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30711 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30714 return objc_finish_try_stmt ();
30717 /* Parse an Objective-C synchronized statement.
30719 objc-synchronized-stmt:
30720 @synchronized ( expression ) compound-statement
30722 Returns NULL_TREE. */
30725 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30727 location_t location
;
30730 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30732 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30733 objc_maybe_warn_exceptions (location
);
30734 matching_parens parens
;
30735 parens
.require_open (parser
);
30736 lock
= cp_parser_expression (parser
);
30737 parens
.require_close (parser
);
30739 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30740 node, lest it get absorbed into the surrounding block. */
30741 stmt
= push_stmt_list ();
30742 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30744 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30747 /* Parse an Objective-C throw statement.
30750 @throw assignment-expression [opt] ;
30752 Returns a constructed '@throw' statement. */
30755 cp_parser_objc_throw_statement (cp_parser
*parser
)
30757 tree expr
= NULL_TREE
;
30758 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30760 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30762 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30763 expr
= cp_parser_expression (parser
);
30765 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30767 return objc_build_throw_stmt (loc
, expr
);
30770 /* Parse an Objective-C statement. */
30773 cp_parser_objc_statement (cp_parser
* parser
)
30775 /* Try to figure out what kind of declaration is present. */
30776 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30778 switch (kwd
->keyword
)
30781 return cp_parser_objc_try_catch_finally_statement (parser
);
30782 case RID_AT_SYNCHRONIZED
:
30783 return cp_parser_objc_synchronized_statement (parser
);
30785 return cp_parser_objc_throw_statement (parser
);
30787 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30789 cp_parser_skip_to_end_of_block_or_statement (parser
);
30792 return error_mark_node
;
30795 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30796 look ahead to see if an objc keyword follows the attributes. This
30797 is to detect the use of prefix attributes on ObjC @interface and
30801 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30803 cp_lexer_save_tokens (parser
->lexer
);
30804 *attrib
= cp_parser_attributes_opt (parser
);
30805 gcc_assert (*attrib
);
30806 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30808 cp_lexer_commit_tokens (parser
->lexer
);
30811 cp_lexer_rollback_tokens (parser
->lexer
);
30815 /* This routine is a minimal replacement for
30816 c_parser_struct_declaration () used when parsing the list of
30817 types/names or ObjC++ properties. For example, when parsing the
30820 @property (readonly) int a, b, c;
30822 this function is responsible for parsing "int a, int b, int c" and
30823 returning the declarations as CHAIN of DECLs.
30825 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30826 similar parsing. */
30828 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30830 tree decls
= NULL_TREE
;
30831 cp_decl_specifier_seq declspecs
;
30832 int decl_class_or_enum_p
;
30833 tree prefix_attributes
;
30835 cp_parser_decl_specifier_seq (parser
,
30836 CP_PARSER_FLAGS_NONE
,
30838 &decl_class_or_enum_p
);
30840 if (declspecs
.type
== error_mark_node
)
30841 return error_mark_node
;
30843 /* auto, register, static, extern, mutable. */
30844 if (declspecs
.storage_class
!= sc_none
)
30846 cp_parser_error (parser
, "invalid type for property");
30847 declspecs
.storage_class
= sc_none
;
30850 /* thread_local. */
30851 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30853 cp_parser_error (parser
, "invalid type for property");
30854 declspecs
.locations
[ds_thread
] = 0;
30858 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30860 cp_parser_error (parser
, "invalid type for property");
30861 declspecs
.locations
[ds_typedef
] = 0;
30864 prefix_attributes
= declspecs
.attributes
;
30865 declspecs
.attributes
= NULL_TREE
;
30867 /* Keep going until we hit the `;' at the end of the declaration. */
30868 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30870 tree attributes
, first_attribute
, decl
;
30871 cp_declarator
*declarator
;
30874 /* Parse the declarator. */
30875 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30876 NULL
, NULL
, false, false);
30878 /* Look for attributes that apply to the ivar. */
30879 attributes
= cp_parser_attributes_opt (parser
);
30880 /* Remember which attributes are prefix attributes and
30882 first_attribute
= attributes
;
30883 /* Combine the attributes. */
30884 attributes
= attr_chainon (prefix_attributes
, attributes
);
30886 decl
= grokfield (declarator
, &declspecs
,
30887 NULL_TREE
, /*init_const_expr_p=*/false,
30888 NULL_TREE
, attributes
);
30890 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30891 return error_mark_node
;
30893 /* Reset PREFIX_ATTRIBUTES. */
30894 if (attributes
!= error_mark_node
)
30896 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30897 attributes
= TREE_CHAIN (attributes
);
30899 TREE_CHAIN (attributes
) = NULL_TREE
;
30902 DECL_CHAIN (decl
) = decls
;
30905 token
= cp_lexer_peek_token (parser
->lexer
);
30906 if (token
->type
== CPP_COMMA
)
30908 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30917 /* Parse an Objective-C @property declaration. The syntax is:
30919 objc-property-declaration:
30920 '@property' objc-property-attributes[opt] struct-declaration ;
30922 objc-property-attributes:
30923 '(' objc-property-attribute-list ')'
30925 objc-property-attribute-list:
30926 objc-property-attribute
30927 objc-property-attribute-list, objc-property-attribute
30929 objc-property-attribute
30930 'getter' = identifier
30931 'setter' = identifier
30940 @property NSString *name;
30941 @property (readonly) id object;
30942 @property (retain, nonatomic, getter=getTheName) id name;
30943 @property int a, b, c;
30945 PS: This function is identical to
30946 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30948 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
30950 /* The following variables hold the attributes of the properties as
30951 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30952 seen. When we see an attribute, we set them to 'true' (if they
30953 are boolean properties) or to the identifier (if they have an
30954 argument, ie, for getter and setter). Note that here we only
30955 parse the list of attributes, check the syntax and accumulate the
30956 attributes that we find. objc_add_property_declaration() will
30957 then process the information. */
30958 bool property_assign
= false;
30959 bool property_copy
= false;
30960 tree property_getter_ident
= NULL_TREE
;
30961 bool property_nonatomic
= false;
30962 bool property_readonly
= false;
30963 bool property_readwrite
= false;
30964 bool property_retain
= false;
30965 tree property_setter_ident
= NULL_TREE
;
30967 /* 'properties' is the list of properties that we read. Usually a
30968 single one, but maybe more (eg, in "@property int a, b, c;" there
30973 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30975 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
30977 /* Parse the optional attribute list... */
30978 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30981 matching_parens parens
;
30982 parens
.consume_open (parser
);
30986 bool syntax_error
= false;
30987 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30990 if (token
->type
!= CPP_NAME
)
30992 cp_parser_error (parser
, "expected identifier");
30995 keyword
= C_RID_CODE (token
->u
.value
);
30996 cp_lexer_consume_token (parser
->lexer
);
30999 case RID_ASSIGN
: property_assign
= true; break;
31000 case RID_COPY
: property_copy
= true; break;
31001 case RID_NONATOMIC
: property_nonatomic
= true; break;
31002 case RID_READONLY
: property_readonly
= true; break;
31003 case RID_READWRITE
: property_readwrite
= true; break;
31004 case RID_RETAIN
: property_retain
= true; break;
31008 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31010 if (keyword
== RID_GETTER
)
31011 cp_parser_error (parser
,
31012 "missing %<=%> (after %<getter%> attribute)");
31014 cp_parser_error (parser
,
31015 "missing %<=%> (after %<setter%> attribute)");
31016 syntax_error
= true;
31019 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31020 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31022 cp_parser_error (parser
, "expected identifier");
31023 syntax_error
= true;
31026 if (keyword
== RID_SETTER
)
31028 if (property_setter_ident
!= NULL_TREE
)
31030 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31031 cp_lexer_consume_token (parser
->lexer
);
31034 property_setter_ident
= cp_parser_objc_selector (parser
);
31035 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31036 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31038 cp_lexer_consume_token (parser
->lexer
);
31042 if (property_getter_ident
!= NULL_TREE
)
31044 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31045 cp_lexer_consume_token (parser
->lexer
);
31048 property_getter_ident
= cp_parser_objc_selector (parser
);
31052 cp_parser_error (parser
, "unknown property attribute");
31053 syntax_error
= true;
31060 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31061 cp_lexer_consume_token (parser
->lexer
);
31066 /* FIXME: "@property (setter, assign);" will generate a spurious
31067 "error: expected ‘)’ before ‘,’ token". This is because
31068 cp_parser_require, unlike the C counterpart, will produce an
31069 error even if we are in error recovery. */
31070 if (!parens
.require_close (parser
))
31072 cp_parser_skip_to_closing_parenthesis (parser
,
31073 /*recovering=*/true,
31074 /*or_comma=*/false,
31075 /*consume_paren=*/true);
31079 /* ... and the property declaration(s). */
31080 properties
= cp_parser_objc_struct_declaration (parser
);
31082 if (properties
== error_mark_node
)
31084 cp_parser_skip_to_end_of_statement (parser
);
31085 /* If the next token is now a `;', consume it. */
31086 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31087 cp_lexer_consume_token (parser
->lexer
);
31091 if (properties
== NULL_TREE
)
31092 cp_parser_error (parser
, "expected identifier");
31095 /* Comma-separated properties are chained together in
31096 reverse order; add them one by one. */
31097 properties
= nreverse (properties
);
31099 for (; properties
; properties
= TREE_CHAIN (properties
))
31100 objc_add_property_declaration (loc
, copy_node (properties
),
31101 property_readonly
, property_readwrite
,
31102 property_assign
, property_retain
,
31103 property_copy
, property_nonatomic
,
31104 property_getter_ident
, property_setter_ident
);
31107 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31110 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31112 objc-synthesize-declaration:
31113 @synthesize objc-synthesize-identifier-list ;
31115 objc-synthesize-identifier-list:
31116 objc-synthesize-identifier
31117 objc-synthesize-identifier-list, objc-synthesize-identifier
31119 objc-synthesize-identifier
31121 identifier = identifier
31124 @synthesize MyProperty;
31125 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31127 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31128 for C. Keep them in sync.
31131 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31133 tree list
= NULL_TREE
;
31135 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31137 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31140 tree property
, ivar
;
31141 property
= cp_parser_identifier (parser
);
31142 if (property
== error_mark_node
)
31144 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31147 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31149 cp_lexer_consume_token (parser
->lexer
);
31150 ivar
= cp_parser_identifier (parser
);
31151 if (ivar
== error_mark_node
)
31153 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31159 list
= chainon (list
, build_tree_list (ivar
, property
));
31160 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31161 cp_lexer_consume_token (parser
->lexer
);
31165 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31166 objc_add_synthesize_declaration (loc
, list
);
31169 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31171 objc-dynamic-declaration:
31172 @dynamic identifier-list ;
31175 @dynamic MyProperty;
31176 @dynamic MyProperty, AnotherProperty;
31178 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31179 for C. Keep them in sync.
31182 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31184 tree list
= NULL_TREE
;
31186 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31188 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31192 property
= cp_parser_identifier (parser
);
31193 if (property
== error_mark_node
)
31195 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31198 list
= chainon (list
, build_tree_list (NULL
, property
));
31199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31200 cp_lexer_consume_token (parser
->lexer
);
31204 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31205 objc_add_dynamic_declaration (loc
, list
);
31209 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31211 /* Returns name of the next clause.
31212 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31213 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31214 returned and the token is consumed. */
31216 static pragma_omp_clause
31217 cp_parser_omp_clause_name (cp_parser
*parser
)
31219 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31221 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31222 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31223 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31224 result
= PRAGMA_OMP_CLAUSE_IF
;
31225 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31226 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31227 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31228 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31229 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31230 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31231 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31232 result
= PRAGMA_OMP_CLAUSE_FOR
;
31233 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31235 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31236 const char *p
= IDENTIFIER_POINTER (id
);
31241 if (!strcmp ("aligned", p
))
31242 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31243 else if (!strcmp ("async", p
))
31244 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31247 if (!strcmp ("collapse", p
))
31248 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31249 else if (!strcmp ("copy", p
))
31250 result
= PRAGMA_OACC_CLAUSE_COPY
;
31251 else if (!strcmp ("copyin", p
))
31252 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31253 else if (!strcmp ("copyout", p
))
31254 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31255 else if (!strcmp ("copyprivate", p
))
31256 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31257 else if (!strcmp ("create", p
))
31258 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31261 if (!strcmp ("defaultmap", p
))
31262 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31263 else if (!strcmp ("depend", p
))
31264 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31265 else if (!strcmp ("device", p
))
31266 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31267 else if (!strcmp ("deviceptr", p
))
31268 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31269 else if (!strcmp ("device_resident", p
))
31270 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31271 else if (!strcmp ("dist_schedule", p
))
31272 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31275 if (!strcmp ("final", p
))
31276 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31277 else if (!strcmp ("firstprivate", p
))
31278 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31279 else if (!strcmp ("from", p
))
31280 result
= PRAGMA_OMP_CLAUSE_FROM
;
31283 if (!strcmp ("gang", p
))
31284 result
= PRAGMA_OACC_CLAUSE_GANG
;
31285 else if (!strcmp ("grainsize", p
))
31286 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31289 if (!strcmp ("hint", p
))
31290 result
= PRAGMA_OMP_CLAUSE_HINT
;
31291 else if (!strcmp ("host", p
))
31292 result
= PRAGMA_OACC_CLAUSE_HOST
;
31295 if (!strcmp ("inbranch", p
))
31296 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31297 else if (!strcmp ("independent", p
))
31298 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31299 else if (!strcmp ("is_device_ptr", p
))
31300 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31303 if (!strcmp ("lastprivate", p
))
31304 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31305 else if (!strcmp ("linear", p
))
31306 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31307 else if (!strcmp ("link", p
))
31308 result
= PRAGMA_OMP_CLAUSE_LINK
;
31311 if (!strcmp ("map", p
))
31312 result
= PRAGMA_OMP_CLAUSE_MAP
;
31313 else if (!strcmp ("mergeable", p
))
31314 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31317 if (!strcmp ("nogroup", p
))
31318 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31319 else if (!strcmp ("notinbranch", p
))
31320 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31321 else if (!strcmp ("nowait", p
))
31322 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31323 else if (!strcmp ("num_gangs", p
))
31324 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31325 else if (!strcmp ("num_tasks", p
))
31326 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31327 else if (!strcmp ("num_teams", p
))
31328 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31329 else if (!strcmp ("num_threads", p
))
31330 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31331 else if (!strcmp ("num_workers", p
))
31332 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31335 if (!strcmp ("ordered", p
))
31336 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31339 if (!strcmp ("parallel", p
))
31340 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31341 else if (!strcmp ("present", p
))
31342 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31343 else if (!strcmp ("present_or_copy", p
)
31344 || !strcmp ("pcopy", p
))
31345 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
31346 else if (!strcmp ("present_or_copyin", p
)
31347 || !strcmp ("pcopyin", p
))
31348 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
31349 else if (!strcmp ("present_or_copyout", p
)
31350 || !strcmp ("pcopyout", p
))
31351 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
31352 else if (!strcmp ("present_or_create", p
)
31353 || !strcmp ("pcreate", p
))
31354 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
31355 else if (!strcmp ("priority", p
))
31356 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31357 else if (!strcmp ("proc_bind", p
))
31358 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31361 if (!strcmp ("reduction", p
))
31362 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31365 if (!strcmp ("safelen", p
))
31366 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31367 else if (!strcmp ("schedule", p
))
31368 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31369 else if (!strcmp ("sections", p
))
31370 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31371 else if (!strcmp ("self", p
))
31372 result
= PRAGMA_OACC_CLAUSE_SELF
;
31373 else if (!strcmp ("seq", p
))
31374 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31375 else if (!strcmp ("shared", p
))
31376 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31377 else if (!strcmp ("simd", p
))
31378 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31379 else if (!strcmp ("simdlen", p
))
31380 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31383 if (!strcmp ("taskgroup", p
))
31384 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31385 else if (!strcmp ("thread_limit", p
))
31386 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31387 else if (!strcmp ("threads", p
))
31388 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31389 else if (!strcmp ("tile", p
))
31390 result
= PRAGMA_OACC_CLAUSE_TILE
;
31391 else if (!strcmp ("to", p
))
31392 result
= PRAGMA_OMP_CLAUSE_TO
;
31395 if (!strcmp ("uniform", p
))
31396 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31397 else if (!strcmp ("untied", p
))
31398 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31399 else if (!strcmp ("use_device", p
))
31400 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31401 else if (!strcmp ("use_device_ptr", p
))
31402 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31405 if (!strcmp ("vector", p
))
31406 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31407 else if (!strcmp ("vector_length", p
))
31408 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31411 if (!strcmp ("wait", p
))
31412 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31413 else if (!strcmp ("worker", p
))
31414 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31419 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31420 cp_lexer_consume_token (parser
->lexer
);
31425 /* Validate that a clause of the given type does not already exist. */
31428 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31429 const char *name
, location_t location
)
31433 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31434 if (OMP_CLAUSE_CODE (c
) == code
)
31436 error_at (location
, "too many %qs clauses", name
);
31444 variable-list , identifier
31446 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31447 colon). An opening parenthesis will have been consumed by the caller.
31449 If KIND is nonzero, create the appropriate node and install the decl
31450 in OMP_CLAUSE_DECL and add the node to the head of the list.
31452 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31453 return the list created.
31455 COLON can be NULL if only closing parenthesis should end the list,
31456 or pointer to bool which will receive false if the list is terminated
31457 by closing parenthesis or true if the list is terminated by colon. */
31460 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31461 tree list
, bool *colon
)
31464 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31467 parser
->colon_corrects_to_scope_p
= false;
31474 token
= cp_lexer_peek_token (parser
->lexer
);
31476 && current_class_ptr
31477 && cp_parser_is_keyword (token
, RID_THIS
))
31479 decl
= finish_this_expr ();
31480 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31481 || CONVERT_EXPR_P (decl
))
31482 decl
= TREE_OPERAND (decl
, 0);
31483 cp_lexer_consume_token (parser
->lexer
);
31487 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31488 /*check_dependency_p=*/true,
31489 /*template_p=*/NULL
,
31490 /*declarator_p=*/false,
31491 /*optional_p=*/false);
31492 if (name
== error_mark_node
)
31495 if (identifier_p (name
))
31496 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31499 if (decl
== error_mark_node
)
31500 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31503 if (decl
== error_mark_node
)
31505 else if (kind
!= 0)
31509 case OMP_CLAUSE__CACHE_
:
31510 /* The OpenACC cache directive explicitly only allows "array
31511 elements or subarrays". */
31512 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31514 error_at (token
->location
, "expected %<[%>");
31515 decl
= error_mark_node
;
31519 case OMP_CLAUSE_MAP
:
31520 case OMP_CLAUSE_FROM
:
31521 case OMP_CLAUSE_TO
:
31522 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31525 = cp_lexer_peek_token (parser
->lexer
)->location
;
31526 cp_id_kind idk
= CP_ID_KIND_NONE
;
31527 cp_lexer_consume_token (parser
->lexer
);
31528 decl
= convert_from_reference (decl
);
31530 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31535 case OMP_CLAUSE_DEPEND
:
31536 case OMP_CLAUSE_REDUCTION
:
31537 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31539 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31541 parser
->colon_corrects_to_scope_p
= false;
31542 cp_lexer_consume_token (parser
->lexer
);
31543 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31544 low_bound
= cp_parser_expression (parser
);
31546 parser
->colon_corrects_to_scope_p
31547 = saved_colon_corrects_to_scope_p
;
31548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31549 length
= integer_one_node
;
31552 /* Look for `:'. */
31553 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31555 if (!cp_lexer_next_token_is (parser
->lexer
,
31557 length
= cp_parser_expression (parser
);
31559 /* Look for the closing `]'. */
31560 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31564 decl
= tree_cons (low_bound
, length
, decl
);
31571 tree u
= build_omp_clause (token
->location
, kind
);
31572 OMP_CLAUSE_DECL (u
) = decl
;
31573 OMP_CLAUSE_CHAIN (u
) = list
;
31577 list
= tree_cons (decl
, NULL_TREE
, list
);
31580 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31582 cp_lexer_consume_token (parser
->lexer
);
31586 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31588 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31591 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31595 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31599 /* Try to resync to an unnested comma. Copied from
31600 cp_parser_parenthesized_expression_list. */
31603 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31604 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31605 /*recovering=*/true,
31607 /*consume_paren=*/true);
31615 /* Similarly, but expect leading and trailing parenthesis. This is a very
31616 common case for omp clauses. */
31619 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31621 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31622 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31627 copy ( variable-list )
31628 copyin ( variable-list )
31629 copyout ( variable-list )
31630 create ( variable-list )
31631 delete ( variable-list )
31632 present ( variable-list )
31633 present_or_copy ( variable-list )
31634 pcopy ( variable-list )
31635 present_or_copyin ( variable-list )
31636 pcopyin ( variable-list )
31637 present_or_copyout ( variable-list )
31638 pcopyout ( variable-list )
31639 present_or_create ( variable-list )
31640 pcreate ( variable-list ) */
31643 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31646 enum gomp_map_kind kind
;
31649 case PRAGMA_OACC_CLAUSE_COPY
:
31650 kind
= GOMP_MAP_FORCE_TOFROM
;
31652 case PRAGMA_OACC_CLAUSE_COPYIN
:
31653 kind
= GOMP_MAP_FORCE_TO
;
31655 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31656 kind
= GOMP_MAP_FORCE_FROM
;
31658 case PRAGMA_OACC_CLAUSE_CREATE
:
31659 kind
= GOMP_MAP_FORCE_ALLOC
;
31661 case PRAGMA_OACC_CLAUSE_DELETE
:
31662 kind
= GOMP_MAP_DELETE
;
31664 case PRAGMA_OACC_CLAUSE_DEVICE
:
31665 kind
= GOMP_MAP_FORCE_TO
;
31667 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31668 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31670 case PRAGMA_OACC_CLAUSE_HOST
:
31671 case PRAGMA_OACC_CLAUSE_SELF
:
31672 kind
= GOMP_MAP_FORCE_FROM
;
31674 case PRAGMA_OACC_CLAUSE_LINK
:
31675 kind
= GOMP_MAP_LINK
;
31677 case PRAGMA_OACC_CLAUSE_PRESENT
:
31678 kind
= GOMP_MAP_FORCE_PRESENT
;
31680 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31681 kind
= GOMP_MAP_TOFROM
;
31683 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31684 kind
= GOMP_MAP_TO
;
31686 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31687 kind
= GOMP_MAP_FROM
;
31689 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31690 kind
= GOMP_MAP_ALLOC
;
31693 gcc_unreachable ();
31696 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31698 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31699 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31705 deviceptr ( variable-list ) */
31708 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31710 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31713 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31714 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31715 variable-list must only allow for pointer variables. */
31716 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31717 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31719 tree v
= TREE_PURPOSE (t
);
31720 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31721 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31722 OMP_CLAUSE_DECL (u
) = v
;
31723 OMP_CLAUSE_CHAIN (u
) = list
;
31737 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31738 enum omp_clause_code code
,
31739 tree list
, location_t location
)
31741 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31742 tree c
= build_omp_clause (location
, code
);
31743 OMP_CLAUSE_CHAIN (c
) = list
;
31748 num_gangs ( expression )
31749 num_workers ( expression )
31750 vector_length ( expression ) */
31753 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31754 const char *str
, tree list
)
31756 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31758 matching_parens parens
;
31759 if (!parens
.require_open (parser
))
31762 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31764 if (t
== error_mark_node
31765 || !parens
.require_close (parser
))
31767 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31768 /*or_comma=*/false,
31769 /*consume_paren=*/true);
31773 check_no_duplicate_clause (list
, code
, str
, loc
);
31775 tree c
= build_omp_clause (loc
, code
);
31776 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31777 OMP_CLAUSE_CHAIN (c
) = list
;
31783 gang [( gang-arg-list )]
31784 worker [( [num:] int-expr )]
31785 vector [( [length:] int-expr )]
31787 where gang-arg is one of:
31792 and size-expr may be:
31799 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31800 const char *str
, tree list
)
31802 const char *id
= "num";
31803 cp_lexer
*lexer
= parser
->lexer
;
31804 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31805 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31807 if (kind
== OMP_CLAUSE_VECTOR
)
31810 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31812 matching_parens parens
;
31813 parens
.consume_open (parser
);
31817 cp_token
*next
= cp_lexer_peek_token (lexer
);
31820 /* Gang static argument. */
31821 if (kind
== OMP_CLAUSE_GANG
31822 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31824 cp_lexer_consume_token (lexer
);
31826 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31827 goto cleanup_error
;
31830 if (ops
[idx
] != NULL
)
31832 cp_parser_error (parser
, "too many %<static%> arguments");
31833 goto cleanup_error
;
31836 /* Check for the '*' argument. */
31837 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31838 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31839 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31842 cp_lexer_consume_token (lexer
);
31843 ops
[idx
] = integer_minus_one_node
;
31845 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31847 cp_lexer_consume_token (lexer
);
31853 /* Worker num: argument and vector length: arguments. */
31854 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31855 && id_equal (next
->u
.value
, id
)
31856 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31858 cp_lexer_consume_token (lexer
); /* id */
31859 cp_lexer_consume_token (lexer
); /* ':' */
31862 /* Now collect the actual argument. */
31863 if (ops
[idx
] != NULL_TREE
)
31865 cp_parser_error (parser
, "unexpected argument");
31866 goto cleanup_error
;
31869 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31871 if (expr
== error_mark_node
)
31872 goto cleanup_error
;
31874 mark_exp_read (expr
);
31877 if (kind
== OMP_CLAUSE_GANG
31878 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31880 cp_lexer_consume_token (lexer
);
31887 if (!parens
.require_close (parser
))
31888 goto cleanup_error
;
31891 check_no_duplicate_clause (list
, kind
, str
, loc
);
31893 c
= build_omp_clause (loc
, kind
);
31896 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31898 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31899 OMP_CLAUSE_CHAIN (c
) = list
;
31904 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
31909 tile ( size-expr-list ) */
31912 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
31914 tree c
, expr
= error_mark_node
;
31915 tree tile
= NULL_TREE
;
31917 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31918 so, but the spec authors never considered such a case and have
31919 differing opinions on what it might mean, including 'not
31921 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
31922 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
31925 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31930 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
31933 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
31934 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31935 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
31937 cp_lexer_consume_token (parser
->lexer
);
31938 expr
= integer_zero_node
;
31941 expr
= cp_parser_constant_expression (parser
);
31943 tile
= tree_cons (NULL_TREE
, expr
, tile
);
31945 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
31947 /* Consume the trailing ')'. */
31948 cp_lexer_consume_token (parser
->lexer
);
31950 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
31951 tile
= nreverse (tile
);
31952 OMP_CLAUSE_TILE_LIST (c
) = tile
;
31953 OMP_CLAUSE_CHAIN (c
) = list
;
31958 Parse wait clause or directive parameters. */
31961 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
31963 vec
<tree
, va_gc
> *args
;
31966 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
31968 /*allow_expansion_p=*/true,
31969 /*non_constant_p=*/NULL
);
31971 if (args
== NULL
|| args
->length () == 0)
31973 cp_parser_error (parser
, "expected integer expression before ')'");
31975 release_tree_vector (args
);
31979 args_tree
= build_tree_list_vec (args
);
31981 release_tree_vector (args
);
31983 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
31985 tree targ
= TREE_VALUE (t
);
31987 if (targ
!= error_mark_node
)
31989 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
31990 error ("%<wait%> expression must be integral");
31993 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
31995 targ
= mark_rvalue_use (targ
);
31996 OMP_CLAUSE_DECL (c
) = targ
;
31997 OMP_CLAUSE_CHAIN (c
) = list
;
32007 wait ( int-expr-list ) */
32010 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32012 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32014 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32017 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32023 collapse ( constant-expression ) */
32026 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32032 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32033 matching_parens parens
;
32034 if (!parens
.require_open (parser
))
32037 num
= cp_parser_constant_expression (parser
);
32039 if (!parens
.require_close (parser
))
32040 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32041 /*or_comma=*/false,
32042 /*consume_paren=*/true);
32044 if (num
== error_mark_node
)
32046 num
= fold_non_dependent_expr (num
);
32047 if (!tree_fits_shwi_p (num
)
32048 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32049 || (n
= tree_to_shwi (num
)) <= 0
32052 error_at (loc
, "collapse argument needs positive constant integer expression");
32056 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32057 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32058 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32059 OMP_CLAUSE_CHAIN (c
) = list
;
32060 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32066 default ( none | shared )
32069 default ( none | present ) */
32072 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32073 location_t location
, bool is_oacc
)
32075 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32078 matching_parens parens
;
32079 if (!parens
.require_open (parser
))
32081 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32083 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32084 const char *p
= IDENTIFIER_POINTER (id
);
32089 if (strcmp ("none", p
) != 0)
32091 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32095 if (strcmp ("present", p
) != 0 || !is_oacc
)
32097 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32101 if (strcmp ("shared", p
) != 0 || is_oacc
)
32103 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32110 cp_lexer_consume_token (parser
->lexer
);
32116 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32118 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32121 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32122 || !parens
.require_close (parser
))
32123 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32124 /*or_comma=*/false,
32125 /*consume_paren=*/true);
32127 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32130 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32131 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32132 OMP_CLAUSE_CHAIN (c
) = list
;
32133 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32139 final ( expression ) */
32142 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32146 matching_parens parens
;
32147 if (!parens
.require_open (parser
))
32150 t
= cp_parser_condition (parser
);
32152 if (t
== error_mark_node
32153 || !parens
.require_close (parser
))
32154 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32155 /*or_comma=*/false,
32156 /*consume_paren=*/true);
32158 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32160 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32161 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32162 OMP_CLAUSE_CHAIN (c
) = list
;
32171 if ( directive-name-modifier : expression )
32173 directive-name-modifier:
32174 parallel | task | taskloop | target data | target | target update
32175 | target enter data | target exit data */
32178 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32182 enum tree_code if_modifier
= ERROR_MARK
;
32184 matching_parens parens
;
32185 if (!parens
.require_open (parser
))
32188 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32190 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32191 const char *p
= IDENTIFIER_POINTER (id
);
32194 if (strcmp ("parallel", p
) == 0)
32195 if_modifier
= OMP_PARALLEL
;
32196 else if (strcmp ("task", p
) == 0)
32197 if_modifier
= OMP_TASK
;
32198 else if (strcmp ("taskloop", p
) == 0)
32199 if_modifier
= OMP_TASKLOOP
;
32200 else if (strcmp ("target", p
) == 0)
32202 if_modifier
= OMP_TARGET
;
32203 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32205 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32206 p
= IDENTIFIER_POINTER (id
);
32207 if (strcmp ("data", p
) == 0)
32208 if_modifier
= OMP_TARGET_DATA
;
32209 else if (strcmp ("update", p
) == 0)
32210 if_modifier
= OMP_TARGET_UPDATE
;
32211 else if (strcmp ("enter", p
) == 0)
32212 if_modifier
= OMP_TARGET_ENTER_DATA
;
32213 else if (strcmp ("exit", p
) == 0)
32214 if_modifier
= OMP_TARGET_EXIT_DATA
;
32215 if (if_modifier
!= OMP_TARGET
)
32220 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32221 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32223 if_modifier
= ERROR_MARK
;
32225 if (if_modifier
== OMP_TARGET_ENTER_DATA
32226 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32228 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32230 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32231 p
= IDENTIFIER_POINTER (id
);
32232 if (strcmp ("data", p
) == 0)
32238 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32239 error_at (loc
, "expected %<data%>");
32240 if_modifier
= ERROR_MARK
;
32245 if (if_modifier
!= ERROR_MARK
)
32247 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32250 cp_lexer_consume_token (parser
->lexer
);
32257 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32258 error_at (loc
, "expected %<:%>");
32260 if_modifier
= ERROR_MARK
;
32265 t
= cp_parser_condition (parser
);
32267 if (t
== error_mark_node
32268 || !parens
.require_close (parser
))
32269 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32270 /*or_comma=*/false,
32271 /*consume_paren=*/true);
32273 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32274 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32276 if (if_modifier
!= ERROR_MARK
32277 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32279 const char *p
= NULL
;
32280 switch (if_modifier
)
32282 case OMP_PARALLEL
: p
= "parallel"; break;
32283 case OMP_TASK
: p
= "task"; break;
32284 case OMP_TASKLOOP
: p
= "taskloop"; break;
32285 case OMP_TARGET_DATA
: p
= "target data"; break;
32286 case OMP_TARGET
: p
= "target"; break;
32287 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32288 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32289 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32290 default: gcc_unreachable ();
32292 error_at (location
, "too many %<if%> clauses with %qs modifier",
32296 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32299 error_at (location
, "too many %<if%> clauses");
32301 error_at (location
, "too many %<if%> clauses without modifier");
32304 else if (if_modifier
== ERROR_MARK
32305 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32307 error_at (location
, "if any %<if%> clause has modifier, then all "
32308 "%<if%> clauses have to use modifier");
32313 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32314 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32315 OMP_CLAUSE_IF_EXPR (c
) = t
;
32316 OMP_CLAUSE_CHAIN (c
) = list
;
32325 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32326 tree list
, location_t location
)
32330 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32333 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32334 OMP_CLAUSE_CHAIN (c
) = list
;
32342 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32343 tree list
, location_t location
)
32347 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32349 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32350 OMP_CLAUSE_CHAIN (c
) = list
;
32355 num_threads ( expression ) */
32358 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32359 location_t location
)
32363 matching_parens parens
;
32364 if (!parens
.require_open (parser
))
32367 t
= cp_parser_expression (parser
);
32369 if (t
== error_mark_node
32370 || !parens
.require_close (parser
))
32371 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32372 /*or_comma=*/false,
32373 /*consume_paren=*/true);
32375 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32376 "num_threads", location
);
32378 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32379 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32380 OMP_CLAUSE_CHAIN (c
) = list
;
32386 num_tasks ( expression ) */
32389 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32390 location_t location
)
32394 matching_parens parens
;
32395 if (!parens
.require_open (parser
))
32398 t
= cp_parser_expression (parser
);
32400 if (t
== error_mark_node
32401 || !parens
.require_close (parser
))
32402 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32403 /*or_comma=*/false,
32404 /*consume_paren=*/true);
32406 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32407 "num_tasks", location
);
32409 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32410 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32411 OMP_CLAUSE_CHAIN (c
) = list
;
32417 grainsize ( expression ) */
32420 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32421 location_t location
)
32425 matching_parens parens
;
32426 if (!parens
.require_open (parser
))
32429 t
= cp_parser_expression (parser
);
32431 if (t
== error_mark_node
32432 || !parens
.require_close (parser
))
32433 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32434 /*or_comma=*/false,
32435 /*consume_paren=*/true);
32437 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32438 "grainsize", location
);
32440 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32441 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32442 OMP_CLAUSE_CHAIN (c
) = list
;
32448 priority ( expression ) */
32451 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32452 location_t location
)
32456 matching_parens parens
;
32457 if (!parens
.require_open (parser
))
32460 t
= cp_parser_expression (parser
);
32462 if (t
== error_mark_node
32463 || !parens
.require_close (parser
))
32464 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32465 /*or_comma=*/false,
32466 /*consume_paren=*/true);
32468 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32469 "priority", location
);
32471 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32472 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32473 OMP_CLAUSE_CHAIN (c
) = list
;
32479 hint ( expression ) */
32482 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32483 location_t location
)
32487 matching_parens parens
;
32488 if (!parens
.require_open (parser
))
32491 t
= cp_parser_expression (parser
);
32493 if (t
== error_mark_node
32494 || !parens
.require_close (parser
))
32495 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32496 /*or_comma=*/false,
32497 /*consume_paren=*/true);
32499 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32501 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32502 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32503 OMP_CLAUSE_CHAIN (c
) = list
;
32509 defaultmap ( tofrom : scalar ) */
32512 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32513 location_t location
)
32518 matching_parens parens
;
32519 if (!parens
.require_open (parser
))
32522 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32524 cp_parser_error (parser
, "expected %<tofrom%>");
32527 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32528 p
= IDENTIFIER_POINTER (id
);
32529 if (strcmp (p
, "tofrom") != 0)
32531 cp_parser_error (parser
, "expected %<tofrom%>");
32534 cp_lexer_consume_token (parser
->lexer
);
32535 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32538 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32540 cp_parser_error (parser
, "expected %<scalar%>");
32543 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32544 p
= IDENTIFIER_POINTER (id
);
32545 if (strcmp (p
, "scalar") != 0)
32547 cp_parser_error (parser
, "expected %<scalar%>");
32550 cp_lexer_consume_token (parser
->lexer
);
32551 if (!parens
.require_close (parser
))
32554 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32557 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32558 OMP_CLAUSE_CHAIN (c
) = list
;
32562 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32563 /*or_comma=*/false,
32564 /*consume_paren=*/true);
32572 ordered ( constant-expression ) */
32575 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32576 tree list
, location_t location
)
32578 tree c
, num
= NULL_TREE
;
32581 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32582 "ordered", location
);
32584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32586 matching_parens parens
;
32587 parens
.consume_open (parser
);
32589 num
= cp_parser_constant_expression (parser
);
32591 if (!parens
.require_close (parser
))
32592 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32593 /*or_comma=*/false,
32594 /*consume_paren=*/true);
32596 if (num
== error_mark_node
)
32598 num
= fold_non_dependent_expr (num
);
32599 if (!tree_fits_shwi_p (num
)
32600 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32601 || (n
= tree_to_shwi (num
)) <= 0
32604 error_at (location
,
32605 "ordered argument needs positive constant integer "
32611 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32612 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32613 OMP_CLAUSE_CHAIN (c
) = list
;
32618 reduction ( reduction-operator : variable-list )
32620 reduction-operator:
32621 One of: + * - & ^ | && ||
32625 reduction-operator:
32626 One of: + * - & ^ | && || min max
32630 reduction-operator:
32631 One of: + * - & ^ | && ||
32635 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32637 enum tree_code code
= ERROR_MARK
;
32638 tree nlist
, c
, id
= NULL_TREE
;
32640 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32643 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32645 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32646 case CPP_MULT
: code
= MULT_EXPR
; break;
32647 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32648 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32649 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32650 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32651 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32652 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32656 if (code
!= ERROR_MARK
)
32657 cp_lexer_consume_token (parser
->lexer
);
32660 bool saved_colon_corrects_to_scope_p
;
32661 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32662 parser
->colon_corrects_to_scope_p
= false;
32663 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32664 /*check_dependency_p=*/true,
32665 /*template_p=*/NULL
,
32666 /*declarator_p=*/false,
32667 /*optional_p=*/false);
32668 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32669 if (identifier_p (id
))
32671 const char *p
= IDENTIFIER_POINTER (id
);
32673 if (strcmp (p
, "min") == 0)
32675 else if (strcmp (p
, "max") == 0)
32677 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32679 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32681 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32683 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32684 code
= BIT_AND_EXPR
;
32685 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32686 code
= BIT_IOR_EXPR
;
32687 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32688 code
= BIT_XOR_EXPR
;
32689 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32690 code
= TRUTH_ANDIF_EXPR
;
32691 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32692 code
= TRUTH_ORIF_EXPR
;
32693 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32694 tree scope
= parser
->scope
;
32696 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32697 parser
->scope
= NULL_TREE
;
32698 parser
->qualifying_scope
= NULL_TREE
;
32699 parser
->object_scope
= NULL_TREE
;
32703 error ("invalid reduction-identifier");
32705 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32706 /*or_comma=*/false,
32707 /*consume_paren=*/true);
32712 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32715 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32717 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32719 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32720 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32727 schedule ( schedule-kind )
32728 schedule ( schedule-kind , expression )
32731 static | dynamic | guided | runtime | auto
32734 schedule ( schedule-modifier : schedule-kind )
32735 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32743 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32746 int modifiers
= 0, nmodifiers
= 0;
32748 matching_parens parens
;
32749 if (!parens
.require_open (parser
))
32752 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32754 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32756 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32757 const char *p
= IDENTIFIER_POINTER (id
);
32758 if (strcmp ("simd", p
) == 0)
32759 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32760 else if (strcmp ("monotonic", p
) == 0)
32761 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32762 else if (strcmp ("nonmonotonic", p
) == 0)
32763 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32766 cp_lexer_consume_token (parser
->lexer
);
32767 if (nmodifiers
++ == 0
32768 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32769 cp_lexer_consume_token (parser
->lexer
);
32772 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32777 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32779 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32780 const char *p
= IDENTIFIER_POINTER (id
);
32785 if (strcmp ("dynamic", p
) != 0)
32787 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32791 if (strcmp ("guided", p
) != 0)
32793 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32797 if (strcmp ("runtime", p
) != 0)
32799 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32806 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32807 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32808 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32809 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32812 cp_lexer_consume_token (parser
->lexer
);
32814 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32815 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32816 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32817 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32819 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32824 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32827 cp_lexer_consume_token (parser
->lexer
);
32829 token
= cp_lexer_peek_token (parser
->lexer
);
32830 t
= cp_parser_assignment_expression (parser
);
32832 if (t
== error_mark_node
)
32834 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32835 error_at (token
->location
, "schedule %<runtime%> does not take "
32836 "a %<chunk_size%> parameter");
32837 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32838 error_at (token
->location
, "schedule %<auto%> does not take "
32839 "a %<chunk_size%> parameter");
32841 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32843 if (!parens
.require_close (parser
))
32846 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32849 OMP_CLAUSE_SCHEDULE_KIND (c
)
32850 = (enum omp_clause_schedule_kind
)
32851 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32853 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32854 OMP_CLAUSE_CHAIN (c
) = list
;
32858 cp_parser_error (parser
, "invalid schedule kind");
32860 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32861 /*or_comma=*/false,
32862 /*consume_paren=*/true);
32870 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32871 tree list
, location_t location
)
32875 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32877 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32878 OMP_CLAUSE_CHAIN (c
) = list
;
32887 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32888 tree list
, location_t location
)
32890 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32891 tree c
= build_omp_clause (location
, code
);
32892 OMP_CLAUSE_CHAIN (c
) = list
;
32903 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32904 enum omp_clause_code code
,
32905 tree list
, location_t location
)
32907 tree c
= build_omp_clause (location
, code
);
32908 OMP_CLAUSE_CHAIN (c
) = list
;
32916 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
32917 tree list
, location_t location
)
32919 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
32920 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
32921 OMP_CLAUSE_CHAIN (c
) = list
;
32930 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
32931 enum omp_clause_code code
,
32932 tree list
, location_t location
)
32934 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32935 tree c
= build_omp_clause (location
, code
);
32936 OMP_CLAUSE_CHAIN (c
) = list
;
32941 num_teams ( expression ) */
32944 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
32945 location_t location
)
32949 matching_parens parens
;
32950 if (!parens
.require_open (parser
))
32953 t
= cp_parser_expression (parser
);
32955 if (t
== error_mark_node
32956 || !parens
.require_close (parser
))
32957 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32958 /*or_comma=*/false,
32959 /*consume_paren=*/true);
32961 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
32962 "num_teams", location
);
32964 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
32965 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
32966 OMP_CLAUSE_CHAIN (c
) = list
;
32972 thread_limit ( expression ) */
32975 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
32976 location_t location
)
32980 matching_parens parens
;
32981 if (!parens
.require_open (parser
))
32984 t
= cp_parser_expression (parser
);
32986 if (t
== error_mark_node
32987 || !parens
.require_close (parser
))
32988 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32989 /*or_comma=*/false,
32990 /*consume_paren=*/true);
32992 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
32993 "thread_limit", location
);
32995 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
32996 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
32997 OMP_CLAUSE_CHAIN (c
) = list
;
33003 aligned ( variable-list )
33004 aligned ( variable-list : constant-expression ) */
33007 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33009 tree nlist
, c
, alignment
= NULL_TREE
;
33012 matching_parens parens
;
33013 if (!parens
.require_open (parser
))
33016 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33021 alignment
= cp_parser_constant_expression (parser
);
33023 if (!parens
.require_close (parser
))
33024 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33025 /*or_comma=*/false,
33026 /*consume_paren=*/true);
33028 if (alignment
== error_mark_node
)
33029 alignment
= NULL_TREE
;
33032 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33033 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33039 linear ( variable-list )
33040 linear ( variable-list : expression )
33043 linear ( modifier ( variable-list ) )
33044 linear ( modifier ( variable-list ) : expression ) */
33047 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33050 tree nlist
, c
, step
= integer_one_node
;
33052 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33054 matching_parens parens
;
33055 if (!parens
.require_open (parser
))
33058 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33060 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33061 const char *p
= IDENTIFIER_POINTER (id
);
33063 if (strcmp ("ref", p
) == 0)
33064 kind
= OMP_CLAUSE_LINEAR_REF
;
33065 else if (strcmp ("val", p
) == 0)
33066 kind
= OMP_CLAUSE_LINEAR_VAL
;
33067 else if (strcmp ("uval", p
) == 0)
33068 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33069 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33070 cp_lexer_consume_token (parser
->lexer
);
33072 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33075 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33076 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33080 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33081 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33083 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33084 else if (!parens
.require_close (parser
))
33085 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33086 /*or_comma=*/false,
33087 /*consume_paren=*/true);
33094 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33095 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33097 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33098 cp_parser_parse_tentatively (parser
);
33099 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33100 /*check_dependency_p=*/true,
33101 /*template_p=*/NULL
,
33102 /*declarator_p=*/false,
33103 /*optional_p=*/false);
33104 if (step
!= error_mark_node
)
33105 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33106 if (step
== error_mark_node
)
33109 cp_parser_abort_tentative_parse (parser
);
33111 else if (!cp_parser_parse_definitely (parser
))
33115 step
= cp_parser_expression (parser
);
33117 if (!parens
.require_close (parser
))
33118 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33119 /*or_comma=*/false,
33120 /*consume_paren=*/true);
33122 if (step
== error_mark_node
)
33126 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33128 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33129 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33136 safelen ( constant-expression ) */
33139 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33140 location_t location
)
33144 matching_parens parens
;
33145 if (!parens
.require_open (parser
))
33148 t
= cp_parser_constant_expression (parser
);
33150 if (t
== error_mark_node
33151 || !parens
.require_close (parser
))
33152 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33153 /*or_comma=*/false,
33154 /*consume_paren=*/true);
33156 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33158 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33159 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33160 OMP_CLAUSE_CHAIN (c
) = list
;
33166 simdlen ( constant-expression ) */
33169 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33170 location_t location
)
33174 matching_parens parens
;
33175 if (!parens
.require_open (parser
))
33178 t
= cp_parser_constant_expression (parser
);
33180 if (t
== error_mark_node
33181 || !parens
.require_close (parser
))
33182 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33183 /*or_comma=*/false,
33184 /*consume_paren=*/true);
33186 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33188 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33189 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33190 OMP_CLAUSE_CHAIN (c
) = list
;
33197 identifier [+/- integer]
33198 vec , identifier [+/- integer]
33202 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33207 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33209 cp_parser_error (parser
, "expected identifier");
33213 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33215 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33216 tree t
, identifier
= cp_parser_identifier (parser
);
33217 tree addend
= NULL
;
33219 if (identifier
== error_mark_node
)
33220 t
= error_mark_node
;
33223 t
= cp_parser_lookup_name_simple
33224 (parser
, identifier
,
33225 cp_lexer_peek_token (parser
->lexer
)->location
);
33226 if (t
== error_mark_node
)
33227 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33232 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33234 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33236 addend
= integer_zero_node
;
33237 goto add_to_vector
;
33239 cp_lexer_consume_token (parser
->lexer
);
33241 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33243 cp_parser_error (parser
, "expected integer");
33247 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33248 if (TREE_CODE (addend
) != INTEGER_CST
)
33250 cp_parser_error (parser
, "expected integer");
33253 cp_lexer_consume_token (parser
->lexer
);
33256 if (t
!= error_mark_node
)
33258 vec
= tree_cons (addend
, t
, vec
);
33260 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33263 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33266 cp_lexer_consume_token (parser
->lexer
);
33269 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33271 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33272 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33273 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33274 OMP_CLAUSE_CHAIN (u
) = list
;
33281 depend ( depend-kind : variable-list )
33289 depend ( sink : vec ) */
33292 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33295 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33297 matching_parens parens
;
33298 if (!parens
.require_open (parser
))
33301 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33303 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33304 const char *p
= IDENTIFIER_POINTER (id
);
33306 if (strcmp ("in", p
) == 0)
33307 kind
= OMP_CLAUSE_DEPEND_IN
;
33308 else if (strcmp ("inout", p
) == 0)
33309 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33310 else if (strcmp ("out", p
) == 0)
33311 kind
= OMP_CLAUSE_DEPEND_OUT
;
33312 else if (strcmp ("source", p
) == 0)
33313 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33314 else if (strcmp ("sink", p
) == 0)
33315 kind
= OMP_CLAUSE_DEPEND_SINK
;
33322 cp_lexer_consume_token (parser
->lexer
);
33324 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33326 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33327 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33328 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33329 OMP_CLAUSE_CHAIN (c
) = list
;
33330 if (!parens
.require_close (parser
))
33331 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33332 /*or_comma=*/false,
33333 /*consume_paren=*/true);
33337 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33340 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33341 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33344 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33347 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33348 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33353 cp_parser_error (parser
, "invalid depend kind");
33355 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33356 /*or_comma=*/false,
33357 /*consume_paren=*/true);
33362 map ( map-kind : variable-list )
33363 map ( variable-list )
33366 alloc | to | from | tofrom
33370 alloc | to | from | tofrom | release | delete
33372 map ( always [,] map-kind: variable-list ) */
33375 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33378 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33379 bool always
= false;
33381 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33384 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33386 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33387 const char *p
= IDENTIFIER_POINTER (id
);
33389 if (strcmp ("always", p
) == 0)
33392 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33394 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33395 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33397 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33401 cp_lexer_consume_token (parser
->lexer
);
33403 cp_lexer_consume_token (parser
->lexer
);
33408 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33409 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33411 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33412 const char *p
= IDENTIFIER_POINTER (id
);
33414 if (strcmp ("alloc", p
) == 0)
33415 kind
= GOMP_MAP_ALLOC
;
33416 else if (strcmp ("to", p
) == 0)
33417 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33418 else if (strcmp ("from", p
) == 0)
33419 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33420 else if (strcmp ("tofrom", p
) == 0)
33421 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33422 else if (strcmp ("release", p
) == 0)
33423 kind
= GOMP_MAP_RELEASE
;
33426 cp_parser_error (parser
, "invalid map kind");
33427 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33428 /*or_comma=*/false,
33429 /*consume_paren=*/true);
33432 cp_lexer_consume_token (parser
->lexer
);
33433 cp_lexer_consume_token (parser
->lexer
);
33435 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33436 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33438 kind
= GOMP_MAP_DELETE
;
33439 cp_lexer_consume_token (parser
->lexer
);
33440 cp_lexer_consume_token (parser
->lexer
);
33443 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33446 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33447 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33453 device ( expression ) */
33456 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33457 location_t location
)
33461 matching_parens parens
;
33462 if (!parens
.require_open (parser
))
33465 t
= cp_parser_expression (parser
);
33467 if (t
== error_mark_node
33468 || !parens
.require_close (parser
))
33469 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33470 /*or_comma=*/false,
33471 /*consume_paren=*/true);
33473 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33474 "device", location
);
33476 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33477 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33478 OMP_CLAUSE_CHAIN (c
) = list
;
33484 dist_schedule ( static )
33485 dist_schedule ( static , expression ) */
33488 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33489 location_t location
)
33493 matching_parens parens
;
33494 if (!parens
.require_open (parser
))
33497 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33499 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33501 cp_lexer_consume_token (parser
->lexer
);
33503 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33505 cp_lexer_consume_token (parser
->lexer
);
33507 t
= cp_parser_assignment_expression (parser
);
33509 if (t
== error_mark_node
)
33511 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33513 if (!parens
.require_close (parser
))
33516 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33519 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33521 OMP_CLAUSE_CHAIN (c
) = list
;
33525 cp_parser_error (parser
, "invalid dist_schedule kind");
33527 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33528 /*or_comma=*/false,
33529 /*consume_paren=*/true);
33534 proc_bind ( proc-bind-kind )
33537 master | close | spread */
33540 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33541 location_t location
)
33544 enum omp_clause_proc_bind_kind kind
;
33546 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33549 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33551 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33552 const char *p
= IDENTIFIER_POINTER (id
);
33554 if (strcmp ("master", p
) == 0)
33555 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33556 else if (strcmp ("close", p
) == 0)
33557 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33558 else if (strcmp ("spread", p
) == 0)
33559 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33566 cp_lexer_consume_token (parser
->lexer
);
33567 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33570 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33571 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33573 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33574 OMP_CLAUSE_CHAIN (c
) = list
;
33578 cp_parser_error (parser
, "invalid depend kind");
33580 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33581 /*or_comma=*/false,
33582 /*consume_paren=*/true);
33587 async [( int-expr )] */
33590 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33593 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33595 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33597 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33599 matching_parens parens
;
33600 parens
.consume_open (parser
);
33602 t
= cp_parser_expression (parser
);
33603 if (t
== error_mark_node
33604 || !parens
.require_close (parser
))
33605 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33606 /*or_comma=*/false,
33607 /*consume_paren=*/true);
33610 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33612 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33613 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33614 OMP_CLAUSE_CHAIN (c
) = list
;
33620 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33621 is a bitmask in MASK. Return the list of clauses found. */
33624 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33625 const char *where
, cp_token
*pragma_tok
,
33626 bool finish_p
= true)
33628 tree clauses
= NULL
;
33631 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33634 pragma_omp_clause c_kind
;
33635 omp_clause_code code
;
33636 const char *c_name
;
33637 tree prev
= clauses
;
33639 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33640 cp_lexer_consume_token (parser
->lexer
);
33642 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33643 c_kind
= cp_parser_omp_clause_name (parser
);
33647 case PRAGMA_OACC_CLAUSE_ASYNC
:
33648 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33651 case PRAGMA_OACC_CLAUSE_AUTO
:
33652 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33656 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33657 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33658 c_name
= "collapse";
33660 case PRAGMA_OACC_CLAUSE_COPY
:
33661 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33664 case PRAGMA_OACC_CLAUSE_COPYIN
:
33665 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33668 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33669 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33670 c_name
= "copyout";
33672 case PRAGMA_OACC_CLAUSE_CREATE
:
33673 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33676 case PRAGMA_OACC_CLAUSE_DELETE
:
33677 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33680 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33681 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33682 c_name
= "default";
33684 case PRAGMA_OACC_CLAUSE_DEVICE
:
33685 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33688 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33689 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33690 c_name
= "deviceptr";
33692 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33693 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33694 c_name
= "device_resident";
33696 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33697 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33699 c_name
= "firstprivate";
33701 case PRAGMA_OACC_CLAUSE_GANG
:
33703 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33706 case PRAGMA_OACC_CLAUSE_HOST
:
33707 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33710 case PRAGMA_OACC_CLAUSE_IF
:
33711 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33714 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33715 clauses
= cp_parser_oacc_simple_clause (parser
,
33716 OMP_CLAUSE_INDEPENDENT
,
33718 c_name
= "independent";
33720 case PRAGMA_OACC_CLAUSE_LINK
:
33721 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33724 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33725 code
= OMP_CLAUSE_NUM_GANGS
;
33726 c_name
= "num_gangs";
33727 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33730 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33731 c_name
= "num_workers";
33732 code
= OMP_CLAUSE_NUM_WORKERS
;
33733 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33736 case PRAGMA_OACC_CLAUSE_PRESENT
:
33737 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33738 c_name
= "present";
33740 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33741 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33742 c_name
= "present_or_copy";
33744 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33745 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33746 c_name
= "present_or_copyin";
33748 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33749 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33750 c_name
= "present_or_copyout";
33752 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33753 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33754 c_name
= "present_or_create";
33756 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33757 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33759 c_name
= "private";
33761 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33762 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33763 c_name
= "reduction";
33765 case PRAGMA_OACC_CLAUSE_SELF
:
33766 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33769 case PRAGMA_OACC_CLAUSE_SEQ
:
33770 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33774 case PRAGMA_OACC_CLAUSE_TILE
:
33775 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33778 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33779 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33781 c_name
= "use_device";
33783 case PRAGMA_OACC_CLAUSE_VECTOR
:
33785 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33788 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33789 c_name
= "vector_length";
33790 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33791 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33794 case PRAGMA_OACC_CLAUSE_WAIT
:
33795 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33798 case PRAGMA_OACC_CLAUSE_WORKER
:
33800 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33804 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33810 if (((mask
>> c_kind
) & 1) == 0)
33812 /* Remove the invalid clause(s) from the list to avoid
33813 confusing the rest of the compiler. */
33815 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33820 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33823 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33828 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33829 is a bitmask in MASK. Return the list of clauses found; the result
33830 of clause default goes in *pdefault. */
33833 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33834 const char *where
, cp_token
*pragma_tok
,
33835 bool finish_p
= true)
33837 tree clauses
= NULL
;
33839 cp_token
*token
= NULL
;
33841 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33843 pragma_omp_clause c_kind
;
33844 const char *c_name
;
33845 tree prev
= clauses
;
33847 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33848 cp_lexer_consume_token (parser
->lexer
);
33850 token
= cp_lexer_peek_token (parser
->lexer
);
33851 c_kind
= cp_parser_omp_clause_name (parser
);
33855 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33856 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33858 c_name
= "collapse";
33860 case PRAGMA_OMP_CLAUSE_COPYIN
:
33861 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33864 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33865 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33867 c_name
= "copyprivate";
33869 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33870 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33871 token
->location
, false);
33872 c_name
= "default";
33874 case PRAGMA_OMP_CLAUSE_FINAL
:
33875 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33878 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33879 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33881 c_name
= "firstprivate";
33883 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33884 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33886 c_name
= "grainsize";
33888 case PRAGMA_OMP_CLAUSE_HINT
:
33889 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33893 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33894 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33896 c_name
= "defaultmap";
33898 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33899 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33901 c_name
= "use_device_ptr";
33903 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33904 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
33906 c_name
= "is_device_ptr";
33908 case PRAGMA_OMP_CLAUSE_IF
:
33909 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
33913 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
33914 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33916 c_name
= "lastprivate";
33918 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
33919 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
33921 c_name
= "mergeable";
33923 case PRAGMA_OMP_CLAUSE_NOWAIT
:
33924 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
33927 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
33928 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
33930 c_name
= "num_tasks";
33932 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
33933 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
33935 c_name
= "num_threads";
33937 case PRAGMA_OMP_CLAUSE_ORDERED
:
33938 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
33940 c_name
= "ordered";
33942 case PRAGMA_OMP_CLAUSE_PRIORITY
:
33943 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
33945 c_name
= "priority";
33947 case PRAGMA_OMP_CLAUSE_PRIVATE
:
33948 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33950 c_name
= "private";
33952 case PRAGMA_OMP_CLAUSE_REDUCTION
:
33953 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33954 c_name
= "reduction";
33956 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
33957 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
33959 c_name
= "schedule";
33961 case PRAGMA_OMP_CLAUSE_SHARED
:
33962 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
33966 case PRAGMA_OMP_CLAUSE_UNTIED
:
33967 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
33971 case PRAGMA_OMP_CLAUSE_INBRANCH
:
33972 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
33973 clauses
, token
->location
);
33974 c_name
= "inbranch";
33976 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
33977 clauses
= cp_parser_omp_clause_branch (parser
,
33978 OMP_CLAUSE_NOTINBRANCH
,
33979 clauses
, token
->location
);
33980 c_name
= "notinbranch";
33982 case PRAGMA_OMP_CLAUSE_PARALLEL
:
33983 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
33984 clauses
, token
->location
);
33985 c_name
= "parallel";
33989 error_at (token
->location
, "%qs must be the first clause of %qs",
33994 case PRAGMA_OMP_CLAUSE_FOR
:
33995 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
33996 clauses
, token
->location
);
33999 goto clause_not_first
;
34001 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34002 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34003 clauses
, token
->location
);
34004 c_name
= "sections";
34006 goto clause_not_first
;
34008 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34009 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34010 clauses
, token
->location
);
34011 c_name
= "taskgroup";
34013 goto clause_not_first
;
34015 case PRAGMA_OMP_CLAUSE_LINK
:
34016 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34019 case PRAGMA_OMP_CLAUSE_TO
:
34020 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34021 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34024 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34027 case PRAGMA_OMP_CLAUSE_FROM
:
34028 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34031 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34032 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34034 c_name
= "uniform";
34036 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34037 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34039 c_name
= "num_teams";
34041 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34042 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34044 c_name
= "thread_limit";
34046 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34047 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34048 c_name
= "aligned";
34050 case PRAGMA_OMP_CLAUSE_LINEAR
:
34052 bool declare_simd
= false;
34053 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34054 declare_simd
= true;
34055 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34059 case PRAGMA_OMP_CLAUSE_DEPEND
:
34060 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34064 case PRAGMA_OMP_CLAUSE_MAP
:
34065 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34068 case PRAGMA_OMP_CLAUSE_DEVICE
:
34069 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34073 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34074 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34076 c_name
= "dist_schedule";
34078 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34079 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34081 c_name
= "proc_bind";
34083 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34084 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34086 c_name
= "safelen";
34088 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34089 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34091 c_name
= "simdlen";
34093 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34094 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34096 c_name
= "nogroup";
34098 case PRAGMA_OMP_CLAUSE_THREADS
:
34100 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34101 clauses
, token
->location
);
34102 c_name
= "threads";
34104 case PRAGMA_OMP_CLAUSE_SIMD
:
34106 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34107 clauses
, token
->location
);
34111 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34117 if (((mask
>> c_kind
) & 1) == 0)
34119 /* Remove the invalid clause(s) from the list to avoid
34120 confusing the rest of the compiler. */
34122 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34126 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34129 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34130 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34132 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34141 In practice, we're also interested in adding the statement to an
34142 outer node. So it is convenient if we work around the fact that
34143 cp_parser_statement calls add_stmt. */
34146 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34148 unsigned save
= parser
->in_statement
;
34150 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34151 This preserves the "not within loop or switch" style error messages
34152 for nonsense cases like
34158 if (parser
->in_statement
)
34159 parser
->in_statement
= IN_OMP_BLOCK
;
34165 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34167 parser
->in_statement
= save
;
34171 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34173 tree stmt
= begin_omp_structured_block ();
34174 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34176 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34178 cp_parser_end_omp_structured_block (parser
, save
);
34179 return finish_omp_structured_block (stmt
);
34183 # pragma omp atomic new-line
34187 x binop= expr | x++ | ++x | x-- | --x
34189 +, *, -, /, &, ^, |, <<, >>
34191 where x is an lvalue expression with scalar type.
34194 # pragma omp atomic new-line
34197 # pragma omp atomic read new-line
34200 # pragma omp atomic write new-line
34203 # pragma omp atomic update new-line
34206 # pragma omp atomic capture new-line
34209 # pragma omp atomic capture new-line
34217 expression-stmt | x = x binop expr
34219 v = expression-stmt
34221 { v = x; update-stmt; } | { update-stmt; v = x; }
34225 expression-stmt | x = x binop expr | x = expr binop x
34229 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34231 where x and v are lvalue expressions with scalar type. */
34234 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34236 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34237 tree rhs1
= NULL_TREE
, orig_lhs
;
34238 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34239 bool structured_block
= false;
34240 bool seq_cst
= false;
34242 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34244 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34245 const char *p
= IDENTIFIER_POINTER (id
);
34247 if (!strcmp (p
, "seq_cst"))
34250 cp_lexer_consume_token (parser
->lexer
);
34251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34252 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34253 cp_lexer_consume_token (parser
->lexer
);
34256 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34258 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34259 const char *p
= IDENTIFIER_POINTER (id
);
34261 if (!strcmp (p
, "read"))
34262 code
= OMP_ATOMIC_READ
;
34263 else if (!strcmp (p
, "write"))
34265 else if (!strcmp (p
, "update"))
34267 else if (!strcmp (p
, "capture"))
34268 code
= OMP_ATOMIC_CAPTURE_NEW
;
34272 cp_lexer_consume_token (parser
->lexer
);
34276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34277 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34278 cp_lexer_consume_token (parser
->lexer
);
34280 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34282 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34283 const char *p
= IDENTIFIER_POINTER (id
);
34285 if (!strcmp (p
, "seq_cst"))
34288 cp_lexer_consume_token (parser
->lexer
);
34292 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34296 case OMP_ATOMIC_READ
:
34297 case NOP_EXPR
: /* atomic write */
34298 v
= cp_parser_unary_expression (parser
);
34299 if (v
== error_mark_node
)
34301 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34303 if (code
== NOP_EXPR
)
34304 lhs
= cp_parser_expression (parser
);
34306 lhs
= cp_parser_unary_expression (parser
);
34307 if (lhs
== error_mark_node
)
34309 if (code
== NOP_EXPR
)
34311 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34319 case OMP_ATOMIC_CAPTURE_NEW
:
34320 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34322 cp_lexer_consume_token (parser
->lexer
);
34323 structured_block
= true;
34327 v
= cp_parser_unary_expression (parser
);
34328 if (v
== error_mark_node
)
34330 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34338 lhs
= cp_parser_unary_expression (parser
);
34340 switch (TREE_CODE (lhs
))
34345 case POSTINCREMENT_EXPR
:
34346 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34347 code
= OMP_ATOMIC_CAPTURE_OLD
;
34349 case PREINCREMENT_EXPR
:
34350 lhs
= TREE_OPERAND (lhs
, 0);
34351 opcode
= PLUS_EXPR
;
34352 rhs
= integer_one_node
;
34355 case POSTDECREMENT_EXPR
:
34356 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34357 code
= OMP_ATOMIC_CAPTURE_OLD
;
34359 case PREDECREMENT_EXPR
:
34360 lhs
= TREE_OPERAND (lhs
, 0);
34361 opcode
= MINUS_EXPR
;
34362 rhs
= integer_one_node
;
34365 case COMPOUND_EXPR
:
34366 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34367 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34368 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34369 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34370 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34371 (TREE_OPERAND (lhs
, 1), 0), 0)))
34373 /* Undo effects of boolean_increment for post {in,de}crement. */
34374 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34377 if (TREE_CODE (lhs
) == MODIFY_EXPR
34378 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34380 /* Undo effects of boolean_increment. */
34381 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34383 /* This is pre or post increment. */
34384 rhs
= TREE_OPERAND (lhs
, 1);
34385 lhs
= TREE_OPERAND (lhs
, 0);
34387 if (code
== OMP_ATOMIC_CAPTURE_NEW
34388 && !structured_block
34389 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34390 code
= OMP_ATOMIC_CAPTURE_OLD
;
34396 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34399 opcode
= MULT_EXPR
;
34402 opcode
= TRUNC_DIV_EXPR
;
34405 opcode
= PLUS_EXPR
;
34408 opcode
= MINUS_EXPR
;
34410 case CPP_LSHIFT_EQ
:
34411 opcode
= LSHIFT_EXPR
;
34413 case CPP_RSHIFT_EQ
:
34414 opcode
= RSHIFT_EXPR
;
34417 opcode
= BIT_AND_EXPR
;
34420 opcode
= BIT_IOR_EXPR
;
34423 opcode
= BIT_XOR_EXPR
;
34426 enum cp_parser_prec oprec
;
34428 cp_lexer_consume_token (parser
->lexer
);
34429 cp_parser_parse_tentatively (parser
);
34430 rhs1
= cp_parser_simple_cast_expression (parser
);
34431 if (rhs1
== error_mark_node
)
34433 cp_parser_abort_tentative_parse (parser
);
34434 cp_parser_simple_cast_expression (parser
);
34437 token
= cp_lexer_peek_token (parser
->lexer
);
34438 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34440 cp_parser_abort_tentative_parse (parser
);
34441 cp_parser_parse_tentatively (parser
);
34442 rhs
= cp_parser_binary_expression (parser
, false, true,
34443 PREC_NOT_OPERATOR
, NULL
);
34444 if (rhs
== error_mark_node
)
34446 cp_parser_abort_tentative_parse (parser
);
34447 cp_parser_binary_expression (parser
, false, true,
34448 PREC_NOT_OPERATOR
, NULL
);
34451 switch (TREE_CODE (rhs
))
34454 case TRUNC_DIV_EXPR
:
34463 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34465 if (cp_parser_parse_definitely (parser
))
34467 opcode
= TREE_CODE (rhs
);
34468 rhs1
= TREE_OPERAND (rhs
, 0);
34469 rhs
= TREE_OPERAND (rhs
, 1);
34479 cp_parser_abort_tentative_parse (parser
);
34480 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34482 rhs
= cp_parser_expression (parser
);
34483 if (rhs
== error_mark_node
)
34489 cp_parser_error (parser
,
34490 "invalid form of %<#pragma omp atomic%>");
34493 if (!cp_parser_parse_definitely (parser
))
34495 switch (token
->type
)
34497 case CPP_SEMICOLON
:
34498 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34500 code
= OMP_ATOMIC_CAPTURE_OLD
;
34505 cp_lexer_consume_token (parser
->lexer
);
34508 else if (structured_block
)
34515 cp_parser_error (parser
,
34516 "invalid form of %<#pragma omp atomic%>");
34519 opcode
= MULT_EXPR
;
34522 opcode
= TRUNC_DIV_EXPR
;
34525 opcode
= PLUS_EXPR
;
34528 opcode
= MINUS_EXPR
;
34531 opcode
= LSHIFT_EXPR
;
34534 opcode
= RSHIFT_EXPR
;
34537 opcode
= BIT_AND_EXPR
;
34540 opcode
= BIT_IOR_EXPR
;
34543 opcode
= BIT_XOR_EXPR
;
34546 cp_parser_error (parser
,
34547 "invalid operator for %<#pragma omp atomic%>");
34550 oprec
= TOKEN_PRECEDENCE (token
);
34551 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34552 if (commutative_tree_code (opcode
))
34553 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34554 cp_lexer_consume_token (parser
->lexer
);
34555 rhs
= cp_parser_binary_expression (parser
, false, false,
34557 if (rhs
== error_mark_node
)
34562 cp_parser_error (parser
,
34563 "invalid operator for %<#pragma omp atomic%>");
34566 cp_lexer_consume_token (parser
->lexer
);
34568 rhs
= cp_parser_expression (parser
);
34569 if (rhs
== error_mark_node
)
34574 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34576 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34578 v
= cp_parser_unary_expression (parser
);
34579 if (v
== error_mark_node
)
34581 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34583 lhs1
= cp_parser_unary_expression (parser
);
34584 if (lhs1
== error_mark_node
)
34587 if (structured_block
)
34589 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34590 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34593 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34594 if (!structured_block
)
34595 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34599 cp_parser_skip_to_end_of_block_or_statement (parser
);
34600 if (structured_block
)
34602 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34603 cp_lexer_consume_token (parser
->lexer
);
34604 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34606 cp_parser_skip_to_end_of_block_or_statement (parser
);
34607 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34608 cp_lexer_consume_token (parser
->lexer
);
34615 # pragma omp barrier new-line */
34618 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34620 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34621 finish_omp_barrier ();
34625 # pragma omp critical [(name)] new-line
34629 # pragma omp critical [(name) [hint(expression)]] new-line
34630 structured-block */
34632 #define OMP_CRITICAL_CLAUSE_MASK \
34633 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34636 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34638 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34640 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34642 matching_parens parens
;
34643 parens
.consume_open (parser
);
34645 name
= cp_parser_identifier (parser
);
34647 if (name
== error_mark_node
34648 || !parens
.require_close (parser
))
34649 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34650 /*or_comma=*/false,
34651 /*consume_paren=*/true);
34652 if (name
== error_mark_node
)
34655 clauses
= cp_parser_omp_all_clauses (parser
,
34656 OMP_CRITICAL_CLAUSE_MASK
,
34657 "#pragma omp critical", pragma_tok
);
34660 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34662 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34663 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34667 # pragma omp flush flush-vars[opt] new-line
34670 ( variable-list ) */
34673 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34675 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34676 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34677 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34679 finish_omp_flush ();
34682 /* Helper function, to parse omp for increment expression. */
34685 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34687 tree cond
= cp_parser_binary_expression (parser
, false, true,
34688 PREC_NOT_OPERATOR
, NULL
);
34689 if (cond
== error_mark_node
34690 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34692 cp_parser_skip_to_end_of_statement (parser
);
34693 return error_mark_node
;
34696 switch (TREE_CODE (cond
))
34704 /* Fall through: OpenMP disallows NE_EXPR. */
34705 gcc_fallthrough ();
34707 return error_mark_node
;
34710 /* If decl is an iterator, preserve LHS and RHS of the relational
34711 expr until finish_omp_for. */
34713 && (type_dependent_expression_p (decl
)
34714 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34717 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34719 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34720 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34721 /*overload=*/NULL
, tf_warning_or_error
);
34724 /* Helper function, to parse omp for increment expression. */
34727 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34729 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34735 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34737 op
= (token
->type
== CPP_PLUS_PLUS
34738 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34739 cp_lexer_consume_token (parser
->lexer
);
34740 lhs
= cp_parser_simple_cast_expression (parser
);
34742 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34743 return error_mark_node
;
34744 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34747 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34749 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34750 return error_mark_node
;
34752 token
= cp_lexer_peek_token (parser
->lexer
);
34753 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34755 op
= (token
->type
== CPP_PLUS_PLUS
34756 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34757 cp_lexer_consume_token (parser
->lexer
);
34758 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34761 op
= cp_parser_assignment_operator_opt (parser
);
34762 if (op
== ERROR_MARK
)
34763 return error_mark_node
;
34765 if (op
!= NOP_EXPR
)
34767 rhs
= cp_parser_assignment_expression (parser
);
34768 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34769 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34772 lhs
= cp_parser_binary_expression (parser
, false, false,
34773 PREC_ADDITIVE_EXPRESSION
, NULL
);
34774 token
= cp_lexer_peek_token (parser
->lexer
);
34775 decl_first
= (lhs
== decl
34776 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34779 if (token
->type
!= CPP_PLUS
34780 && token
->type
!= CPP_MINUS
)
34781 return error_mark_node
;
34785 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34786 cp_lexer_consume_token (parser
->lexer
);
34787 rhs
= cp_parser_binary_expression (parser
, false, false,
34788 PREC_ADDITIVE_EXPRESSION
, NULL
);
34789 token
= cp_lexer_peek_token (parser
->lexer
);
34790 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34792 if (lhs
== NULL_TREE
)
34794 if (op
== PLUS_EXPR
)
34797 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34798 tf_warning_or_error
);
34801 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34802 ERROR_MARK
, NULL
, tf_warning_or_error
);
34805 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34810 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34811 || op
== MINUS_EXPR
)
34812 return error_mark_node
;
34813 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34816 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34818 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34821 /* Parse the initialization statement of an OpenMP for loop.
34823 Return true if the resulting construct should have an
34824 OMP_CLAUSE_PRIVATE added to it. */
34827 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34828 tree
&this_pre_body
,
34829 vec
<tree
, va_gc
> *for_block
,
34835 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34838 tree add_private_clause
= NULL_TREE
;
34840 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34844 integer-type var = lb
34845 random-access-iterator-type var = lb
34846 pointer-type var = lb
34848 cp_decl_specifier_seq type_specifiers
;
34850 /* First, try to parse as an initialized declaration. See
34851 cp_parser_condition, from whence the bulk of this is copied. */
34853 cp_parser_parse_tentatively (parser
);
34854 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34855 /*is_trailing_return=*/false,
34857 if (cp_parser_parse_definitely (parser
))
34859 /* If parsing a type specifier seq succeeded, then this
34860 MUST be a initialized declaration. */
34861 tree asm_specification
, attributes
;
34862 cp_declarator
*declarator
;
34864 declarator
= cp_parser_declarator (parser
,
34865 CP_PARSER_DECLARATOR_NAMED
,
34866 /*ctor_dtor_or_conv_p=*/NULL
,
34867 /*parenthesized_p=*/NULL
,
34868 /*member_p=*/false,
34869 /*friend_p=*/false);
34870 attributes
= cp_parser_attributes_opt (parser
);
34871 asm_specification
= cp_parser_asm_specification_opt (parser
);
34873 if (declarator
== cp_error_declarator
)
34874 cp_parser_skip_to_end_of_statement (parser
);
34878 tree pushed_scope
, auto_node
;
34880 decl
= start_decl (declarator
, &type_specifiers
,
34881 SD_INITIALIZED
, attributes
,
34882 /*prefix_attributes=*/NULL_TREE
,
34885 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34886 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34888 if (cp_lexer_next_token_is (parser
->lexer
,
34890 error ("parenthesized initialization is not allowed in "
34891 "OpenMP %<for%> loop");
34893 /* Trigger an error. */
34894 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34896 init
= error_mark_node
;
34897 cp_parser_skip_to_end_of_statement (parser
);
34899 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34900 || type_dependent_expression_p (decl
)
34903 bool is_direct_init
, is_non_constant_init
;
34905 init
= cp_parser_initializer (parser
,
34907 &is_non_constant_init
);
34912 = do_auto_deduction (TREE_TYPE (decl
), init
,
34915 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
34916 && !type_dependent_expression_p (decl
))
34920 cp_finish_decl (decl
, init
, !is_non_constant_init
,
34922 LOOKUP_ONLYCONVERTING
);
34924 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
34926 vec_safe_push (for_block
, this_pre_body
);
34931 init
= pop_stmt_list (this_pre_body
);
34932 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
34934 tree_stmt_iterator i
= tsi_start (init
);
34935 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34936 while (!tsi_end_p (i
))
34938 tree t
= tsi_stmt (i
);
34939 if (TREE_CODE (t
) == DECL_EXPR
34940 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
34943 vec_safe_push (for_block
, t
);
34948 if (tsi_one_before_end_p (i
))
34950 tree t
= tsi_stmt (i
);
34952 free_stmt_list (init
);
34957 this_pre_body
= NULL_TREE
;
34962 cp_lexer_consume_token (parser
->lexer
);
34963 init
= cp_parser_assignment_expression (parser
);
34966 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
34967 init
= error_mark_node
;
34969 cp_finish_decl (decl
, NULL_TREE
,
34970 /*init_const_expr_p=*/false,
34972 LOOKUP_ONLYCONVERTING
);
34976 pop_scope (pushed_scope
);
34982 /* If parsing a type specifier sequence failed, then
34983 this MUST be a simple expression. */
34984 cp_parser_parse_tentatively (parser
);
34985 decl
= cp_parser_primary_expression (parser
, false, false,
34987 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
34988 if (!cp_parser_error_occurred (parser
)
34990 && (TREE_CODE (decl
) == COMPONENT_REF
34991 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
34993 cp_parser_abort_tentative_parse (parser
);
34994 cp_parser_parse_tentatively (parser
);
34995 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34996 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
34997 /*check_dependency_p=*/true,
34998 /*template_p=*/NULL
,
34999 /*declarator_p=*/false,
35000 /*optional_p=*/false);
35001 if (name
!= error_mark_node
35002 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35004 decl
= cp_parser_lookup_name_simple (parser
, name
,
35006 if (TREE_CODE (decl
) == FIELD_DECL
)
35007 add_private_clause
= omp_privatize_field (decl
, false);
35009 cp_parser_abort_tentative_parse (parser
);
35010 cp_parser_parse_tentatively (parser
);
35011 decl
= cp_parser_primary_expression (parser
, false, false,
35014 if (!cp_parser_error_occurred (parser
)
35017 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35021 cp_parser_parse_definitely (parser
);
35022 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35023 rhs
= cp_parser_assignment_expression (parser
);
35025 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35028 tf_warning_or_error
));
35029 if (!add_private_clause
)
35030 add_private_clause
= decl
;
35035 cp_parser_abort_tentative_parse (parser
);
35036 init
= cp_parser_expression (parser
);
35039 if (TREE_CODE (init
) == MODIFY_EXPR
35040 || TREE_CODE (init
) == MODOP_EXPR
)
35041 real_decl
= TREE_OPERAND (init
, 0);
35045 return add_private_clause
;
35048 /* Parse the restricted form of the for statement allowed by OpenMP. */
35051 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35052 tree
*cclauses
, bool *if_p
)
35054 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35055 tree real_decl
, initv
, condv
, incrv
, declv
;
35056 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35057 location_t loc_first
;
35058 bool collapse_err
= false;
35059 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35060 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35061 auto_vec
<tree
, 4> orig_inits
;
35062 bool tiling
= false;
35064 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35065 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35066 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35067 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35070 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35072 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35073 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35076 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35079 if (ordered
&& ordered
< collapse
)
35081 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35082 "%<ordered%> clause parameter is less than %<collapse%>");
35083 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35084 = build_int_cst (NULL_TREE
, collapse
);
35085 ordered
= collapse
;
35089 for (tree
*pc
= &clauses
; *pc
; )
35090 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35092 error_at (OMP_CLAUSE_LOCATION (*pc
),
35093 "%<linear%> clause may not be specified together "
35094 "with %<ordered%> clause with a parameter");
35095 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35098 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35101 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35102 count
= ordered
? ordered
: collapse
;
35104 declv
= make_tree_vec (count
);
35105 initv
= make_tree_vec (count
);
35106 condv
= make_tree_vec (count
);
35107 incrv
= make_tree_vec (count
);
35109 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35111 for (i
= 0; i
< count
; i
++)
35113 int bracecount
= 0;
35114 tree add_private_clause
= NULL_TREE
;
35117 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35120 cp_parser_error (parser
, "for statement expected");
35123 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35125 matching_parens parens
;
35126 if (!parens
.require_open (parser
))
35129 init
= orig_init
= decl
= real_decl
= NULL
;
35130 this_pre_body
= push_stmt_list ();
35133 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35134 init
, orig_init
, decl
, real_decl
);
35136 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35139 this_pre_body
= pop_stmt_list (this_pre_body
);
35143 pre_body
= push_stmt_list ();
35145 add_stmt (this_pre_body
);
35146 pre_body
= pop_stmt_list (pre_body
);
35149 pre_body
= this_pre_body
;
35154 if (cclauses
!= NULL
35155 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35156 && real_decl
!= NULL_TREE
)
35159 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35160 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35161 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35163 error_at (loc
, "iteration variable %qD"
35164 " should not be firstprivate", real_decl
);
35165 *c
= OMP_CLAUSE_CHAIN (*c
);
35167 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35168 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35170 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35172 *c
= OMP_CLAUSE_CHAIN (*c
);
35173 if (code
== OMP_SIMD
)
35175 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35176 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35180 OMP_CLAUSE_CHAIN (l
) = clauses
;
35183 add_private_clause
= NULL_TREE
;
35187 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35188 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35189 add_private_clause
= NULL_TREE
;
35190 c
= &OMP_CLAUSE_CHAIN (*c
);
35194 if (add_private_clause
)
35197 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35199 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35200 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35201 && OMP_CLAUSE_DECL (c
) == decl
)
35203 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35204 && OMP_CLAUSE_DECL (c
) == decl
)
35205 error_at (loc
, "iteration variable %qD "
35206 "should not be firstprivate",
35208 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35209 && OMP_CLAUSE_DECL (c
) == decl
)
35210 error_at (loc
, "iteration variable %qD should not be reduction",
35215 if (code
!= OMP_SIMD
)
35216 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35217 else if (collapse
== 1)
35218 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35220 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35221 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35222 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35225 OMP_CLAUSE_CHAIN (c
) = clauses
;
35227 /* For linear, signal that we need to fill up
35228 the so far unknown linear step. */
35229 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35230 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35236 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35237 cond
= cp_parser_omp_for_cond (parser
, decl
);
35238 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35241 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35243 /* If decl is an iterator, preserve the operator on decl
35244 until finish_omp_for. */
35246 && ((processing_template_decl
35247 && (TREE_TYPE (real_decl
) == NULL_TREE
35248 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
35249 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35250 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35252 incr
= cp_parser_expression (parser
);
35253 if (!EXPR_HAS_LOCATION (incr
))
35254 protected_set_expr_location (incr
, input_location
);
35257 if (!parens
.require_close (parser
))
35258 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35259 /*or_comma=*/false,
35260 /*consume_paren=*/true);
35262 TREE_VEC_ELT (declv
, i
) = decl
;
35263 TREE_VEC_ELT (initv
, i
) = init
;
35264 TREE_VEC_ELT (condv
, i
) = cond
;
35265 TREE_VEC_ELT (incrv
, i
) = incr
;
35268 orig_inits
.safe_grow_cleared (i
+ 1);
35269 orig_inits
[i
] = orig_init
;
35272 if (i
== count
- 1)
35275 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35276 in between the collapsed for loops to be still considered perfectly
35277 nested. Hopefully the final version clarifies this.
35278 For now handle (multiple) {'s and empty statements. */
35279 cp_parser_parse_tentatively (parser
);
35282 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35284 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35286 cp_lexer_consume_token (parser
->lexer
);
35289 else if (bracecount
35290 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35291 cp_lexer_consume_token (parser
->lexer
);
35294 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35295 error_at (loc
, "not enough for loops to collapse");
35296 collapse_err
= true;
35297 cp_parser_abort_tentative_parse (parser
);
35305 cp_parser_parse_definitely (parser
);
35306 nbraces
+= bracecount
;
35313 /* Note that we saved the original contents of this flag when we entered
35314 the structured block, and so we don't need to re-save it here. */
35315 parser
->in_statement
= IN_OMP_FOR
;
35317 /* Note that the grammar doesn't call for a structured block here,
35318 though the loop as a whole is a structured block. */
35319 body
= push_stmt_list ();
35320 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35321 body
= pop_stmt_list (body
);
35323 if (declv
== NULL_TREE
)
35326 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35327 body
, pre_body
, &orig_inits
, clauses
);
35331 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35333 cp_lexer_consume_token (parser
->lexer
);
35336 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35337 cp_lexer_consume_token (parser
->lexer
);
35342 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35343 "collapsed loops not perfectly nested");
35345 collapse_err
= true;
35346 cp_parser_statement_seq_opt (parser
, NULL
);
35347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35352 while (!for_block
->is_empty ())
35354 tree t
= for_block
->pop ();
35355 if (TREE_CODE (t
) == STATEMENT_LIST
)
35356 add_stmt (pop_stmt_list (t
));
35360 release_tree_vector (for_block
);
35365 /* Helper function for OpenMP parsing, split clauses and call
35366 finish_omp_clauses on each of the set of clauses afterwards. */
35369 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35370 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35373 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35374 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35376 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35380 #pragma omp simd simd-clause[optseq] new-line
35383 #define OMP_SIMD_CLAUSE_MASK \
35384 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35394 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35395 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35398 tree clauses
, sb
, ret
;
35400 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35402 strcat (p_name
, " simd");
35403 mask
|= OMP_SIMD_CLAUSE_MASK
;
35405 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35409 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35410 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35411 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35412 OMP_CLAUSE_ORDERED
);
35413 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35415 error_at (OMP_CLAUSE_LOCATION (c
),
35416 "%<ordered%> clause with parameter may not be specified "
35417 "on %qs construct", p_name
);
35418 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35422 sb
= begin_omp_structured_block ();
35423 save
= cp_parser_begin_omp_structured_block (parser
);
35425 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35427 cp_parser_end_omp_structured_block (parser
, save
);
35428 add_stmt (finish_omp_structured_block (sb
));
35434 #pragma omp for for-clause[optseq] new-line
35438 #pragma omp for simd for-simd-clause[optseq] new-line
35441 #define OMP_FOR_CLAUSE_MASK \
35442 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35453 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35454 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35457 tree clauses
, sb
, ret
;
35459 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35461 strcat (p_name
, " for");
35462 mask
|= OMP_FOR_CLAUSE_MASK
;
35463 /* parallel for{, simd} disallows nowait clause, but for
35464 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35465 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35466 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35467 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35468 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35469 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35473 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35474 const char *p
= IDENTIFIER_POINTER (id
);
35476 if (strcmp (p
, "simd") == 0)
35478 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35479 if (cclauses
== NULL
)
35480 cclauses
= cclauses_buf
;
35482 cp_lexer_consume_token (parser
->lexer
);
35483 if (!flag_openmp
) /* flag_openmp_simd */
35484 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35486 sb
= begin_omp_structured_block ();
35487 save
= cp_parser_begin_omp_structured_block (parser
);
35488 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35490 cp_parser_end_omp_structured_block (parser
, save
);
35491 tree body
= finish_omp_structured_block (sb
);
35494 ret
= make_node (OMP_FOR
);
35495 TREE_TYPE (ret
) = void_type_node
;
35496 OMP_FOR_BODY (ret
) = body
;
35497 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35498 SET_EXPR_LOCATION (ret
, loc
);
35503 if (!flag_openmp
) /* flag_openmp_simd */
35505 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35509 /* Composite distribute parallel for disallows linear clause. */
35510 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35511 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35513 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35517 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35518 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35521 sb
= begin_omp_structured_block ();
35522 save
= cp_parser_begin_omp_structured_block (parser
);
35524 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35526 cp_parser_end_omp_structured_block (parser
, save
);
35527 add_stmt (finish_omp_structured_block (sb
));
35533 # pragma omp master new-line
35534 structured-block */
35537 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35539 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35540 return c_finish_omp_master (input_location
,
35541 cp_parser_omp_structured_block (parser
, if_p
));
35545 # pragma omp ordered new-line
35549 # pragma omp ordered ordered-clauses new-line
35550 structured-block */
35552 #define OMP_ORDERED_CLAUSE_MASK \
35553 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35556 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35557 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35560 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35561 enum pragma_context context
, bool *if_p
)
35563 location_t loc
= pragma_tok
->location
;
35565 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35567 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35568 const char *p
= IDENTIFIER_POINTER (id
);
35570 if (strcmp (p
, "depend") == 0)
35572 if (!flag_openmp
) /* flag_openmp_simd */
35574 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35577 if (context
== pragma_stmt
)
35579 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35580 "%<depend%> clause may only be used in compound "
35582 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35586 = cp_parser_omp_all_clauses (parser
,
35587 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35588 "#pragma omp ordered", pragma_tok
);
35589 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35595 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35596 "#pragma omp ordered", pragma_tok
);
35598 if (!flag_openmp
/* flag_openmp_simd */
35599 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35602 c_finish_omp_ordered (loc
, clauses
,
35603 cp_parser_omp_structured_block (parser
, if_p
));
35610 { section-sequence }
35613 section-directive[opt] structured-block
35614 section-sequence section-directive structured-block */
35617 cp_parser_omp_sections_scope (cp_parser
*parser
)
35619 tree stmt
, substmt
;
35620 bool error_suppress
= false;
35623 matching_braces braces
;
35624 if (!braces
.require_open (parser
))
35627 stmt
= push_stmt_list ();
35629 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35630 != PRAGMA_OMP_SECTION
)
35632 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35633 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35634 add_stmt (substmt
);
35639 tok
= cp_lexer_peek_token (parser
->lexer
);
35640 if (tok
->type
== CPP_CLOSE_BRACE
)
35642 if (tok
->type
== CPP_EOF
)
35645 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35647 cp_lexer_consume_token (parser
->lexer
);
35648 cp_parser_require_pragma_eol (parser
, tok
);
35649 error_suppress
= false;
35651 else if (!error_suppress
)
35653 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35654 error_suppress
= true;
35657 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35658 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35659 add_stmt (substmt
);
35661 braces
.require_close (parser
);
35663 substmt
= pop_stmt_list (stmt
);
35665 stmt
= make_node (OMP_SECTIONS
);
35666 TREE_TYPE (stmt
) = void_type_node
;
35667 OMP_SECTIONS_BODY (stmt
) = substmt
;
35674 # pragma omp sections sections-clause[optseq] newline
35677 #define OMP_SECTIONS_CLAUSE_MASK \
35678 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35685 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35686 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35689 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35691 strcat (p_name
, " sections");
35692 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35694 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35696 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35700 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35701 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35704 ret
= cp_parser_omp_sections_scope (parser
);
35706 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35712 # pragma omp parallel parallel-clause[optseq] new-line
35714 # pragma omp parallel for parallel-for-clause[optseq] new-line
35716 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35720 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35721 structured-block */
35723 #define OMP_PARALLEL_CLAUSE_MASK \
35724 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35735 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35736 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35739 tree stmt
, clauses
, block
;
35741 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35743 strcat (p_name
, " parallel");
35744 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35745 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35746 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35747 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35748 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35750 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35752 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35753 if (cclauses
== NULL
)
35754 cclauses
= cclauses_buf
;
35756 cp_lexer_consume_token (parser
->lexer
);
35757 if (!flag_openmp
) /* flag_openmp_simd */
35758 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35760 block
= begin_omp_parallel ();
35761 save
= cp_parser_begin_omp_structured_block (parser
);
35762 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35764 cp_parser_end_omp_structured_block (parser
, save
);
35765 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35767 if (ret
== NULL_TREE
)
35769 OMP_PARALLEL_COMBINED (stmt
) = 1;
35772 /* When combined with distribute, parallel has to be followed by for.
35773 #pragma omp target parallel is allowed though. */
35775 && (mask
& (OMP_CLAUSE_MASK_1
35776 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35778 error_at (loc
, "expected %<for%> after %qs", p_name
);
35779 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35782 else if (!flag_openmp
) /* flag_openmp_simd */
35784 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35787 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35789 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35790 const char *p
= IDENTIFIER_POINTER (id
);
35791 if (strcmp (p
, "sections") == 0)
35793 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35794 cclauses
= cclauses_buf
;
35796 cp_lexer_consume_token (parser
->lexer
);
35797 block
= begin_omp_parallel ();
35798 save
= cp_parser_begin_omp_structured_block (parser
);
35799 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35800 cp_parser_end_omp_structured_block (parser
, save
);
35801 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35803 OMP_PARALLEL_COMBINED (stmt
) = 1;
35808 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35812 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35813 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35816 block
= begin_omp_parallel ();
35817 save
= cp_parser_begin_omp_structured_block (parser
);
35818 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35819 cp_parser_end_omp_structured_block (parser
, save
);
35820 stmt
= finish_omp_parallel (clauses
, block
);
35825 # pragma omp single single-clause[optseq] new-line
35826 structured-block */
35828 #define OMP_SINGLE_CLAUSE_MASK \
35829 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35835 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35837 tree stmt
= make_node (OMP_SINGLE
);
35838 TREE_TYPE (stmt
) = void_type_node
;
35840 OMP_SINGLE_CLAUSES (stmt
)
35841 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35842 "#pragma omp single", pragma_tok
);
35843 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35845 return add_stmt (stmt
);
35849 # pragma omp task task-clause[optseq] new-line
35850 structured-block */
35852 #define OMP_TASK_CLAUSE_MASK \
35853 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35865 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35867 tree clauses
, block
;
35870 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35871 "#pragma omp task", pragma_tok
);
35872 block
= begin_omp_task ();
35873 save
= cp_parser_begin_omp_structured_block (parser
);
35874 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35875 cp_parser_end_omp_structured_block (parser
, save
);
35876 return finish_omp_task (clauses
, block
);
35880 # pragma omp taskwait new-line */
35883 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35885 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35886 finish_omp_taskwait ();
35890 # pragma omp taskyield new-line */
35893 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35895 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35896 finish_omp_taskyield ();
35900 # pragma omp taskgroup new-line
35901 structured-block */
35904 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35906 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35907 return c_finish_omp_taskgroup (input_location
,
35908 cp_parser_omp_structured_block (parser
,
35914 # pragma omp threadprivate (variable-list) */
35917 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
35921 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35922 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35924 finish_omp_threadprivate (vars
);
35928 # pragma omp cancel cancel-clause[optseq] new-line */
35930 #define OMP_CANCEL_CLAUSE_MASK \
35931 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35938 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
35940 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
35941 "#pragma omp cancel", pragma_tok
);
35942 finish_omp_cancel (clauses
);
35946 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35948 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35949 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35955 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
35956 enum pragma_context context
)
35959 bool point_seen
= false;
35961 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35963 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35964 const char *p
= IDENTIFIER_POINTER (id
);
35966 if (strcmp (p
, "point") == 0)
35968 cp_lexer_consume_token (parser
->lexer
);
35974 cp_parser_error (parser
, "expected %<point%>");
35975 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35979 if (context
!= pragma_compound
)
35981 if (context
== pragma_stmt
)
35982 error_at (pragma_tok
->location
,
35983 "%<#pragma %s%> may only be used in compound statements",
35984 "omp cancellation point");
35986 cp_parser_error (parser
, "expected declaration specifiers");
35987 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35991 clauses
= cp_parser_omp_all_clauses (parser
,
35992 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
35993 "#pragma omp cancellation point",
35995 finish_omp_cancellation_point (clauses
);
35999 #pragma omp distribute distribute-clause[optseq] new-line
36002 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36003 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36010 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
36011 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36014 tree clauses
, sb
, ret
;
36016 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36018 strcat (p_name
, " distribute");
36019 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
36021 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36023 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36024 const char *p
= IDENTIFIER_POINTER (id
);
36026 bool parallel
= false;
36028 if (strcmp (p
, "simd") == 0)
36031 parallel
= strcmp (p
, "parallel") == 0;
36032 if (parallel
|| simd
)
36034 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36035 if (cclauses
== NULL
)
36036 cclauses
= cclauses_buf
;
36037 cp_lexer_consume_token (parser
->lexer
);
36038 if (!flag_openmp
) /* flag_openmp_simd */
36041 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36044 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36047 sb
= begin_omp_structured_block ();
36048 save
= cp_parser_begin_omp_structured_block (parser
);
36050 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36053 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36055 cp_parser_end_omp_structured_block (parser
, save
);
36056 tree body
= finish_omp_structured_block (sb
);
36059 ret
= make_node (OMP_DISTRIBUTE
);
36060 TREE_TYPE (ret
) = void_type_node
;
36061 OMP_FOR_BODY (ret
) = body
;
36062 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36063 SET_EXPR_LOCATION (ret
, loc
);
36068 if (!flag_openmp
) /* flag_openmp_simd */
36070 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36074 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36078 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36079 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36082 sb
= begin_omp_structured_block ();
36083 save
= cp_parser_begin_omp_structured_block (parser
);
36085 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36087 cp_parser_end_omp_structured_block (parser
, save
);
36088 add_stmt (finish_omp_structured_block (sb
));
36094 # pragma omp teams teams-clause[optseq] new-line
36095 structured-block */
36097 #define OMP_TEAMS_CLAUSE_MASK \
36098 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36107 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36108 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36111 tree clauses
, sb
, ret
;
36113 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36115 strcat (p_name
, " teams");
36116 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36118 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36120 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36121 const char *p
= IDENTIFIER_POINTER (id
);
36122 if (strcmp (p
, "distribute") == 0)
36124 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36125 if (cclauses
== NULL
)
36126 cclauses
= cclauses_buf
;
36128 cp_lexer_consume_token (parser
->lexer
);
36129 if (!flag_openmp
) /* flag_openmp_simd */
36130 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36132 sb
= begin_omp_structured_block ();
36133 save
= cp_parser_begin_omp_structured_block (parser
);
36134 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36136 cp_parser_end_omp_structured_block (parser
, save
);
36137 tree body
= finish_omp_structured_block (sb
);
36140 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36141 ret
= make_node (OMP_TEAMS
);
36142 TREE_TYPE (ret
) = void_type_node
;
36143 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36144 OMP_TEAMS_BODY (ret
) = body
;
36145 OMP_TEAMS_COMBINED (ret
) = 1;
36146 SET_EXPR_LOCATION (ret
, loc
);
36147 return add_stmt (ret
);
36150 if (!flag_openmp
) /* flag_openmp_simd */
36152 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36156 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36160 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36161 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36164 tree stmt
= make_node (OMP_TEAMS
);
36165 TREE_TYPE (stmt
) = void_type_node
;
36166 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36167 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36168 SET_EXPR_LOCATION (stmt
, loc
);
36170 return add_stmt (stmt
);
36174 # pragma omp target data target-data-clause[optseq] new-line
36175 structured-block */
36177 #define OMP_TARGET_DATA_CLAUSE_MASK \
36178 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36184 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36187 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36188 "#pragma omp target data", pragma_tok
);
36190 for (tree
*pc
= &clauses
; *pc
;)
36192 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36193 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36196 case GOMP_MAP_ALWAYS_TO
:
36197 case GOMP_MAP_FROM
:
36198 case GOMP_MAP_ALWAYS_FROM
:
36199 case GOMP_MAP_TOFROM
:
36200 case GOMP_MAP_ALWAYS_TOFROM
:
36201 case GOMP_MAP_ALLOC
:
36204 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36205 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36206 case GOMP_MAP_ALWAYS_POINTER
:
36210 error_at (OMP_CLAUSE_LOCATION (*pc
),
36211 "%<#pragma omp target data%> with map-type other "
36212 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36213 "on %<map%> clause");
36214 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36217 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36223 error_at (pragma_tok
->location
,
36224 "%<#pragma omp target data%> must contain at least "
36225 "one %<map%> clause");
36229 tree stmt
= make_node (OMP_TARGET_DATA
);
36230 TREE_TYPE (stmt
) = void_type_node
;
36231 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36233 keep_next_level (true);
36234 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36236 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36237 return add_stmt (stmt
);
36241 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36242 structured-block */
36244 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36245 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36252 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36253 enum pragma_context context
)
36255 bool data_seen
= false;
36256 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36258 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36259 const char *p
= IDENTIFIER_POINTER (id
);
36261 if (strcmp (p
, "data") == 0)
36263 cp_lexer_consume_token (parser
->lexer
);
36269 cp_parser_error (parser
, "expected %<data%>");
36270 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36274 if (context
== pragma_stmt
)
36276 error_at (pragma_tok
->location
,
36277 "%<#pragma %s%> may only be used in compound statements",
36278 "omp target enter data");
36279 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36284 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36285 "#pragma omp target enter data", pragma_tok
);
36287 for (tree
*pc
= &clauses
; *pc
;)
36289 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36290 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36293 case GOMP_MAP_ALWAYS_TO
:
36294 case GOMP_MAP_ALLOC
:
36297 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36298 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36299 case GOMP_MAP_ALWAYS_POINTER
:
36303 error_at (OMP_CLAUSE_LOCATION (*pc
),
36304 "%<#pragma omp target enter data%> with map-type other "
36305 "than %<to%> or %<alloc%> on %<map%> clause");
36306 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36309 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36315 error_at (pragma_tok
->location
,
36316 "%<#pragma omp target enter data%> must contain at least "
36317 "one %<map%> clause");
36321 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36322 TREE_TYPE (stmt
) = void_type_node
;
36323 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36324 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36325 return add_stmt (stmt
);
36329 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36330 structured-block */
36332 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36333 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36340 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36341 enum pragma_context context
)
36343 bool data_seen
= false;
36344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36346 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36347 const char *p
= IDENTIFIER_POINTER (id
);
36349 if (strcmp (p
, "data") == 0)
36351 cp_lexer_consume_token (parser
->lexer
);
36357 cp_parser_error (parser
, "expected %<data%>");
36358 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36362 if (context
== pragma_stmt
)
36364 error_at (pragma_tok
->location
,
36365 "%<#pragma %s%> may only be used in compound statements",
36366 "omp target exit data");
36367 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36372 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36373 "#pragma omp target exit data", pragma_tok
);
36375 for (tree
*pc
= &clauses
; *pc
;)
36377 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36378 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36380 case GOMP_MAP_FROM
:
36381 case GOMP_MAP_ALWAYS_FROM
:
36382 case GOMP_MAP_RELEASE
:
36383 case GOMP_MAP_DELETE
:
36386 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36387 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36388 case GOMP_MAP_ALWAYS_POINTER
:
36392 error_at (OMP_CLAUSE_LOCATION (*pc
),
36393 "%<#pragma omp target exit data%> with map-type other "
36394 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36396 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36399 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36405 error_at (pragma_tok
->location
,
36406 "%<#pragma omp target exit data%> must contain at least "
36407 "one %<map%> clause");
36411 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36412 TREE_TYPE (stmt
) = void_type_node
;
36413 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36414 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36415 return add_stmt (stmt
);
36419 # pragma omp target update target-update-clause[optseq] new-line */
36421 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36422 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36430 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36431 enum pragma_context context
)
36433 if (context
== pragma_stmt
)
36435 error_at (pragma_tok
->location
,
36436 "%<#pragma %s%> may only be used in compound statements",
36437 "omp target update");
36438 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36443 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36444 "#pragma omp target update", pragma_tok
);
36445 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36446 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36448 error_at (pragma_tok
->location
,
36449 "%<#pragma omp target update%> must contain at least one "
36450 "%<from%> or %<to%> clauses");
36454 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36455 TREE_TYPE (stmt
) = void_type_node
;
36456 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36457 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36463 # pragma omp target target-clause[optseq] new-line
36464 structured-block */
36466 #define OMP_TARGET_CLAUSE_MASK \
36467 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36478 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36479 enum pragma_context context
, bool *if_p
)
36481 tree
*pc
= NULL
, stmt
;
36483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36485 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36486 const char *p
= IDENTIFIER_POINTER (id
);
36487 enum tree_code ccode
= ERROR_MARK
;
36489 if (strcmp (p
, "teams") == 0)
36491 else if (strcmp (p
, "parallel") == 0)
36492 ccode
= OMP_PARALLEL
;
36493 else if (strcmp (p
, "simd") == 0)
36495 if (ccode
!= ERROR_MARK
)
36497 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36498 char p_name
[sizeof ("#pragma omp target teams distribute "
36499 "parallel for simd")];
36501 cp_lexer_consume_token (parser
->lexer
);
36502 strcpy (p_name
, "#pragma omp target");
36503 if (!flag_openmp
) /* flag_openmp_simd */
36509 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36510 OMP_TARGET_CLAUSE_MASK
,
36514 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36515 OMP_TARGET_CLAUSE_MASK
,
36519 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36520 OMP_TARGET_CLAUSE_MASK
,
36524 gcc_unreachable ();
36526 return stmt
!= NULL_TREE
;
36528 keep_next_level (true);
36529 tree sb
= begin_omp_structured_block (), ret
;
36530 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36534 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36535 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36539 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36540 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36544 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36545 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36549 gcc_unreachable ();
36551 cp_parser_end_omp_structured_block (parser
, save
);
36552 tree body
= finish_omp_structured_block (sb
);
36553 if (ret
== NULL_TREE
)
36555 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36557 /* For combined target teams, ensure the num_teams and
36558 thread_limit clause expressions are evaluated on the host,
36559 before entering the target construct. */
36561 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36562 c
; c
= OMP_CLAUSE_CHAIN (c
))
36563 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36564 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36565 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36567 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36568 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36569 if (expr
== error_mark_node
)
36571 tree tmp
= TARGET_EXPR_SLOT (expr
);
36573 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36574 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36575 OMP_CLAUSE_FIRSTPRIVATE
);
36576 OMP_CLAUSE_DECL (tc
) = tmp
;
36577 OMP_CLAUSE_CHAIN (tc
)
36578 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36579 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36582 tree stmt
= make_node (OMP_TARGET
);
36583 TREE_TYPE (stmt
) = void_type_node
;
36584 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36585 OMP_TARGET_BODY (stmt
) = body
;
36586 OMP_TARGET_COMBINED (stmt
) = 1;
36587 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36589 pc
= &OMP_TARGET_CLAUSES (stmt
);
36590 goto check_clauses
;
36592 else if (!flag_openmp
) /* flag_openmp_simd */
36594 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36597 else if (strcmp (p
, "data") == 0)
36599 cp_lexer_consume_token (parser
->lexer
);
36600 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36603 else if (strcmp (p
, "enter") == 0)
36605 cp_lexer_consume_token (parser
->lexer
);
36606 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36609 else if (strcmp (p
, "exit") == 0)
36611 cp_lexer_consume_token (parser
->lexer
);
36612 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36615 else if (strcmp (p
, "update") == 0)
36617 cp_lexer_consume_token (parser
->lexer
);
36618 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36621 if (!flag_openmp
) /* flag_openmp_simd */
36623 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36627 stmt
= make_node (OMP_TARGET
);
36628 TREE_TYPE (stmt
) = void_type_node
;
36630 OMP_TARGET_CLAUSES (stmt
)
36631 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36632 "#pragma omp target", pragma_tok
);
36633 pc
= &OMP_TARGET_CLAUSES (stmt
);
36634 keep_next_level (true);
36635 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36637 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36643 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36644 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36647 case GOMP_MAP_ALWAYS_TO
:
36648 case GOMP_MAP_FROM
:
36649 case GOMP_MAP_ALWAYS_FROM
:
36650 case GOMP_MAP_TOFROM
:
36651 case GOMP_MAP_ALWAYS_TOFROM
:
36652 case GOMP_MAP_ALLOC
:
36653 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36654 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36655 case GOMP_MAP_ALWAYS_POINTER
:
36658 error_at (OMP_CLAUSE_LOCATION (*pc
),
36659 "%<#pragma omp target%> with map-type other "
36660 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36661 "on %<map%> clause");
36662 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36665 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36671 # pragma acc cache (variable-list) new-line
36675 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36677 tree stmt
, clauses
;
36679 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36680 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36682 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36684 stmt
= make_node (OACC_CACHE
);
36685 TREE_TYPE (stmt
) = void_type_node
;
36686 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36687 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36694 # pragma acc data oacc-data-clause[optseq] new-line
36695 structured-block */
36697 #define OACC_DATA_CLAUSE_MASK \
36698 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36711 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36713 tree stmt
, clauses
, block
;
36716 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36717 "#pragma acc data", pragma_tok
);
36719 block
= begin_omp_parallel ();
36720 save
= cp_parser_begin_omp_structured_block (parser
);
36721 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36722 cp_parser_end_omp_structured_block (parser
, save
);
36723 stmt
= finish_oacc_data (clauses
, block
);
36728 # pragma acc host_data <clauses> new-line
36729 structured-block */
36731 #define OACC_HOST_DATA_CLAUSE_MASK \
36732 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36735 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36737 tree stmt
, clauses
, block
;
36740 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36741 "#pragma acc host_data", pragma_tok
);
36743 block
= begin_omp_parallel ();
36744 save
= cp_parser_begin_omp_structured_block (parser
);
36745 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36746 cp_parser_end_omp_structured_block (parser
, save
);
36747 stmt
= finish_oacc_host_data (clauses
, block
);
36752 # pragma acc declare oacc-data-clause[optseq] new-line
36755 #define OACC_DECLARE_CLAUSE_MASK \
36756 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36770 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36772 tree clauses
, stmt
;
36773 bool error
= false;
36775 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36776 "#pragma acc declare", pragma_tok
, true);
36779 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36781 error_at (pragma_tok
->location
,
36782 "no valid clauses specified in %<#pragma acc declare%>");
36786 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36788 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36789 tree decl
= OMP_CLAUSE_DECL (t
);
36790 if (!DECL_P (decl
))
36792 error_at (loc
, "array section in %<#pragma acc declare%>");
36796 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36797 switch (OMP_CLAUSE_MAP_KIND (t
))
36799 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36800 case GOMP_MAP_FORCE_ALLOC
:
36801 case GOMP_MAP_FORCE_TO
:
36802 case GOMP_MAP_FORCE_DEVICEPTR
:
36803 case GOMP_MAP_DEVICE_RESIDENT
:
36806 case GOMP_MAP_LINK
:
36807 if (!global_bindings_p ()
36808 && (TREE_STATIC (decl
)
36809 || !DECL_EXTERNAL (decl
)))
36812 "%qD must be a global variable in "
36813 "%<#pragma acc declare link%>",
36821 if (global_bindings_p ())
36823 error_at (loc
, "invalid OpenACC clause at file scope");
36827 if (DECL_EXTERNAL (decl
))
36830 "invalid use of %<extern%> variable %qD "
36831 "in %<#pragma acc declare%>", decl
);
36835 else if (TREE_PUBLIC (decl
))
36838 "invalid use of %<global%> variable %qD "
36839 "in %<#pragma acc declare%>", decl
);
36846 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36847 || lookup_attribute ("omp declare target link",
36848 DECL_ATTRIBUTES (decl
)))
36850 error_at (loc
, "variable %qD used more than once with "
36851 "%<#pragma acc declare%>", decl
);
36860 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36861 id
= get_identifier ("omp declare target link");
36863 id
= get_identifier ("omp declare target");
36865 DECL_ATTRIBUTES (decl
)
36866 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36867 if (global_bindings_p ())
36869 symtab_node
*node
= symtab_node::get (decl
);
36872 node
->offloadable
= 1;
36873 if (ENABLE_OFFLOADING
)
36875 g
->have_offload
= true;
36876 if (is_a
<varpool_node
*> (node
))
36877 vec_safe_push (offload_vars
, decl
);
36884 if (error
|| global_bindings_p ())
36887 stmt
= make_node (OACC_DECLARE
);
36888 TREE_TYPE (stmt
) = void_type_node
;
36889 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36890 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36898 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36902 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36904 LOC is the location of the #pragma token.
36907 #define OACC_ENTER_DATA_CLAUSE_MASK \
36908 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36916 #define OACC_EXIT_DATA_CLAUSE_MASK \
36917 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36924 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36927 location_t loc
= pragma_tok
->location
;
36928 tree stmt
, clauses
;
36929 const char *p
= "";
36931 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36932 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
36934 if (strcmp (p
, "data") != 0)
36936 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
36937 enter
? "enter" : "exit");
36938 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36942 cp_lexer_consume_token (parser
->lexer
);
36945 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
36946 "#pragma acc enter data", pragma_tok
);
36948 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
36949 "#pragma acc exit data", pragma_tok
);
36951 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36953 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
36954 enter
? "enter" : "exit");
36958 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
36959 TREE_TYPE (stmt
) = void_type_node
;
36960 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
36961 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36967 # pragma acc loop oacc-loop-clause[optseq] new-line
36968 structured-block */
36970 #define OACC_LOOP_CLAUSE_MASK \
36971 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36983 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
36984 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
36986 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
36988 strcat (p_name
, " loop");
36989 mask
|= OACC_LOOP_CLAUSE_MASK
;
36991 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36995 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
36997 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
36999 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
37002 tree block
= begin_omp_structured_block ();
37003 int save
= cp_parser_begin_omp_structured_block (parser
);
37004 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
37005 cp_parser_end_omp_structured_block (parser
, save
);
37006 add_stmt (finish_omp_structured_block (block
));
37012 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37017 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37021 #define OACC_KERNELS_CLAUSE_MASK \
37022 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37040 #define OACC_PARALLEL_CLAUSE_MASK \
37041 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37063 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37064 char *p_name
, bool *if_p
)
37066 omp_clause_mask mask
;
37067 enum tree_code code
;
37068 switch (cp_parser_pragma_kind (pragma_tok
))
37070 case PRAGMA_OACC_KERNELS
:
37071 strcat (p_name
, " kernels");
37072 mask
= OACC_KERNELS_CLAUSE_MASK
;
37073 code
= OACC_KERNELS
;
37075 case PRAGMA_OACC_PARALLEL
:
37076 strcat (p_name
, " parallel");
37077 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37078 code
= OACC_PARALLEL
;
37081 gcc_unreachable ();
37084 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37087 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37088 if (strcmp (p
, "loop") == 0)
37090 cp_lexer_consume_token (parser
->lexer
);
37091 tree block
= begin_omp_parallel ();
37093 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37095 return finish_omp_construct (code
, block
, clauses
);
37099 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37101 tree block
= begin_omp_parallel ();
37102 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37103 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37104 cp_parser_end_omp_structured_block (parser
, save
);
37105 return finish_omp_construct (code
, block
, clauses
);
37109 # pragma acc update oacc-update-clause[optseq] new-line
37112 #define OACC_UPDATE_CLAUSE_MASK \
37113 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37121 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37123 tree stmt
, clauses
;
37125 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37126 "#pragma acc update", pragma_tok
);
37128 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37130 error_at (pragma_tok
->location
,
37131 "%<#pragma acc update%> must contain at least one "
37132 "%<device%> or %<host%> or %<self%> clause");
37136 stmt
= make_node (OACC_UPDATE
);
37137 TREE_TYPE (stmt
) = void_type_node
;
37138 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37139 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37145 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37147 LOC is the location of the #pragma token.
37150 #define OACC_WAIT_CLAUSE_MASK \
37151 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37154 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37156 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37157 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37159 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37160 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37162 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37163 "#pragma acc wait", pragma_tok
);
37165 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37166 stmt
= finish_expr_stmt (stmt
);
37172 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37174 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37175 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37183 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37184 enum pragma_context context
)
37186 bool first_p
= parser
->omp_declare_simd
== NULL
;
37187 cp_omp_declare_simd_data data
;
37190 data
.error_seen
= false;
37191 data
.fndecl_seen
= false;
37192 data
.tokens
= vNULL
;
37193 data
.clauses
= NULL_TREE
;
37194 /* It is safe to take the address of a local variable; it will only be
37195 used while this scope is live. */
37196 parser
->omp_declare_simd
= &data
;
37199 /* Store away all pragma tokens. */
37200 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37201 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37202 cp_lexer_consume_token (parser
->lexer
);
37203 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37204 parser
->omp_declare_simd
->error_seen
= true;
37205 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37206 struct cp_token_cache
*cp
37207 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37208 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37212 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37213 cp_parser_pragma (parser
, context
, NULL
);
37216 case pragma_external
:
37217 cp_parser_declaration (parser
);
37219 case pragma_member
:
37220 cp_parser_member_declaration (parser
);
37222 case pragma_objc_icode
:
37223 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37226 cp_parser_declaration_statement (parser
);
37229 if (parser
->omp_declare_simd
37230 && !parser
->omp_declare_simd
->error_seen
37231 && !parser
->omp_declare_simd
->fndecl_seen
)
37232 error_at (pragma_tok
->location
,
37233 "%<#pragma omp declare simd%> not immediately followed by "
37234 "function declaration or definition");
37235 data
.tokens
.release ();
37236 parser
->omp_declare_simd
= NULL
;
37240 /* Finalize #pragma omp declare simd clauses after direct declarator has
37241 been parsed, and put that into "omp declare simd" attribute. */
37244 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37246 struct cp_token_cache
*ce
;
37247 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37250 if (!data
->error_seen
&& data
->fndecl_seen
)
37252 error ("%<#pragma omp declare simd%> not immediately followed by "
37253 "a single function declaration or definition");
37254 data
->error_seen
= true;
37256 if (data
->error_seen
)
37259 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37263 cp_parser_push_lexer_for_tokens (parser
, ce
);
37264 parser
->lexer
->in_pragma
= true;
37265 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37266 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37267 cp_lexer_consume_token (parser
->lexer
);
37268 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37269 "#pragma omp declare simd", pragma_tok
);
37270 cp_parser_pop_lexer (parser
);
37272 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37273 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37274 TREE_CHAIN (c
) = attrs
;
37275 if (processing_template_decl
)
37276 ATTR_IS_DEPENDENT (c
) = 1;
37280 data
->fndecl_seen
= true;
37286 # pragma omp declare target new-line
37287 declarations and definitions
37288 # pragma omp end declare target new-line
37291 # pragma omp declare target ( extended-list ) new-line
37293 # pragma omp declare target declare-target-clauses[seq] new-line */
37295 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37296 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37300 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37302 tree clauses
= NULL_TREE
;
37303 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37305 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37306 "#pragma omp declare target", pragma_tok
);
37307 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37309 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37311 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37312 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37316 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37317 scope_chain
->omp_declare_target_attribute
++;
37320 if (scope_chain
->omp_declare_target_attribute
)
37321 error_at (pragma_tok
->location
,
37322 "%<#pragma omp declare target%> with clauses in between "
37323 "%<#pragma omp declare target%> without clauses and "
37324 "%<#pragma omp end declare target%>");
37325 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37327 tree t
= OMP_CLAUSE_DECL (c
), id
;
37328 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37329 tree at2
= lookup_attribute ("omp declare target link",
37330 DECL_ATTRIBUTES (t
));
37331 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37333 id
= get_identifier ("omp declare target link");
37334 std::swap (at1
, at2
);
37337 id
= get_identifier ("omp declare target");
37340 error_at (OMP_CLAUSE_LOCATION (c
),
37341 "%qD specified both in declare target %<link%> and %<to%>"
37347 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37348 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37351 symtab_node
*node
= symtab_node::get (t
);
37354 node
->offloadable
= 1;
37355 if (ENABLE_OFFLOADING
)
37357 g
->have_offload
= true;
37358 if (is_a
<varpool_node
*> (node
))
37359 vec_safe_push (offload_vars
, t
);
37367 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37369 const char *p
= "";
37370 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37372 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37373 p
= IDENTIFIER_POINTER (id
);
37375 if (strcmp (p
, "declare") == 0)
37377 cp_lexer_consume_token (parser
->lexer
);
37379 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37381 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37382 p
= IDENTIFIER_POINTER (id
);
37384 if (strcmp (p
, "target") == 0)
37385 cp_lexer_consume_token (parser
->lexer
);
37388 cp_parser_error (parser
, "expected %<target%>");
37389 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37395 cp_parser_error (parser
, "expected %<declare%>");
37396 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37399 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37400 if (!scope_chain
->omp_declare_target_attribute
)
37401 error_at (pragma_tok
->location
,
37402 "%<#pragma omp end declare target%> without corresponding "
37403 "%<#pragma omp declare target%>");
37405 scope_chain
->omp_declare_target_attribute
--;
37408 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37409 expression and optional initializer clause of
37410 #pragma omp declare reduction. We store the expression(s) as
37411 either 3, 6 or 7 special statements inside of the artificial function's
37412 body. The first two statements are DECL_EXPRs for the artificial
37413 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37414 expression that uses those variables.
37415 If there was any INITIALIZER clause, this is followed by further statements,
37416 the fourth and fifth statements are DECL_EXPRs for the artificial
37417 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37418 constructor variant (first token after open paren is not omp_priv),
37419 then the sixth statement is a statement with the function call expression
37420 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37421 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37422 to initialize the OMP_PRIV artificial variable and there is seventh
37423 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37426 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37428 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37429 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
37430 type
= TREE_TYPE (type
);
37431 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37432 DECL_ARTIFICIAL (omp_out
) = 1;
37433 pushdecl (omp_out
);
37434 add_decl_expr (omp_out
);
37435 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37436 DECL_ARTIFICIAL (omp_in
) = 1;
37438 add_decl_expr (omp_in
);
37440 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37442 keep_next_level (true);
37443 tree block
= begin_omp_structured_block ();
37444 combiner
= cp_parser_expression (parser
);
37445 finish_expr_stmt (combiner
);
37446 block
= finish_omp_structured_block (block
);
37449 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37452 const char *p
= "";
37453 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37455 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37456 p
= IDENTIFIER_POINTER (id
);
37459 if (strcmp (p
, "initializer") == 0)
37461 cp_lexer_consume_token (parser
->lexer
);
37462 matching_parens parens
;
37463 if (!parens
.require_open (parser
))
37467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37469 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37470 p
= IDENTIFIER_POINTER (id
);
37473 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37474 DECL_ARTIFICIAL (omp_priv
) = 1;
37475 pushdecl (omp_priv
);
37476 add_decl_expr (omp_priv
);
37477 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37478 DECL_ARTIFICIAL (omp_orig
) = 1;
37479 pushdecl (omp_orig
);
37480 add_decl_expr (omp_orig
);
37482 keep_next_level (true);
37483 block
= begin_omp_structured_block ();
37486 if (strcmp (p
, "omp_priv") == 0)
37488 bool is_direct_init
, is_non_constant_init
;
37490 cp_lexer_consume_token (parser
->lexer
);
37491 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37492 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37493 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37494 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37496 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37497 == CPP_CLOSE_PAREN
))
37499 finish_omp_structured_block (block
);
37500 error ("invalid initializer clause");
37503 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37504 &is_non_constant_init
);
37505 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37506 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37510 cp_parser_parse_tentatively (parser
);
37511 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37512 /*check_dependency_p=*/true,
37513 /*template_p=*/NULL
,
37514 /*declarator_p=*/false,
37515 /*optional_p=*/false);
37516 vec
<tree
, va_gc
> *args
;
37517 if (fn_name
== error_mark_node
37518 || cp_parser_error_occurred (parser
)
37519 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37520 || ((args
= cp_parser_parenthesized_expression_list
37521 (parser
, non_attr
, /*cast_p=*/false,
37522 /*allow_expansion_p=*/true,
37523 /*non_constant_p=*/NULL
)),
37524 cp_parser_error_occurred (parser
)))
37526 finish_omp_structured_block (block
);
37527 cp_parser_abort_tentative_parse (parser
);
37528 cp_parser_error (parser
, "expected id-expression (arguments)");
37533 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37534 if (arg
== omp_priv
37535 || (TREE_CODE (arg
) == ADDR_EXPR
37536 && TREE_OPERAND (arg
, 0) == omp_priv
))
37538 cp_parser_abort_tentative_parse (parser
);
37539 if (arg
== NULL_TREE
)
37540 error ("one of the initializer call arguments should be %<omp_priv%>"
37541 " or %<&omp_priv%>");
37542 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37544 finish_expr_stmt (initializer
);
37547 block
= finish_omp_structured_block (block
);
37548 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37552 add_decl_expr (omp_orig
);
37554 if (!parens
.require_close (parser
))
37558 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37559 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37566 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37567 initializer-clause[opt] new-line
37569 initializer-clause:
37570 initializer (omp_priv initializer)
37571 initializer (function-name (argument-list)) */
37574 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37575 enum pragma_context
)
37577 auto_vec
<tree
> types
;
37578 enum tree_code reduc_code
= ERROR_MARK
;
37579 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37581 cp_token
*first_token
;
37582 cp_token_cache
*cp
;
37586 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37587 p
= obstack_alloc (&declarator_obstack
, 0);
37589 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37592 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37595 reduc_code
= PLUS_EXPR
;
37598 reduc_code
= MULT_EXPR
;
37601 reduc_code
= MINUS_EXPR
;
37604 reduc_code
= BIT_AND_EXPR
;
37607 reduc_code
= BIT_XOR_EXPR
;
37610 reduc_code
= BIT_IOR_EXPR
;
37613 reduc_code
= TRUTH_ANDIF_EXPR
;
37616 reduc_code
= TRUTH_ORIF_EXPR
;
37619 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37622 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37623 "%<|%>, %<&&%>, %<||%> or identifier");
37627 if (reduc_code
!= ERROR_MARK
)
37628 cp_lexer_consume_token (parser
->lexer
);
37630 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37631 if (reduc_id
== error_mark_node
)
37634 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37637 /* Types may not be defined in declare reduction type list. */
37638 const char *saved_message
;
37639 saved_message
= parser
->type_definition_forbidden_message
;
37640 parser
->type_definition_forbidden_message
37641 = G_("types may not be defined in declare reduction type list");
37642 bool saved_colon_corrects_to_scope_p
;
37643 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37644 parser
->colon_corrects_to_scope_p
= false;
37645 bool saved_colon_doesnt_start_class_def_p
;
37646 saved_colon_doesnt_start_class_def_p
37647 = parser
->colon_doesnt_start_class_def_p
;
37648 parser
->colon_doesnt_start_class_def_p
= true;
37652 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37653 type
= cp_parser_type_id (parser
);
37654 if (type
== error_mark_node
)
37656 else if (ARITHMETIC_TYPE_P (type
)
37657 && (orig_reduc_id
== NULL_TREE
37658 || (TREE_CODE (type
) != COMPLEX_TYPE
37659 && (id_equal (orig_reduc_id
, "min")
37660 || id_equal (orig_reduc_id
, "max")))))
37661 error_at (loc
, "predeclared arithmetic type %qT in "
37662 "%<#pragma omp declare reduction%>", type
);
37663 else if (TREE_CODE (type
) == FUNCTION_TYPE
37664 || TREE_CODE (type
) == METHOD_TYPE
37665 || TREE_CODE (type
) == ARRAY_TYPE
)
37666 error_at (loc
, "function or array type %qT in "
37667 "%<#pragma omp declare reduction%>", type
);
37668 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
37669 error_at (loc
, "reference type %qT in "
37670 "%<#pragma omp declare reduction%>", type
);
37671 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37672 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37673 "%<#pragma omp declare reduction%>", type
);
37675 types
.safe_push (type
);
37677 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37678 cp_lexer_consume_token (parser
->lexer
);
37683 /* Restore the saved message. */
37684 parser
->type_definition_forbidden_message
= saved_message
;
37685 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37686 parser
->colon_doesnt_start_class_def_p
37687 = saved_colon_doesnt_start_class_def_p
;
37689 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37690 || types
.is_empty ())
37693 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37697 first_token
= cp_lexer_peek_token (parser
->lexer
);
37700 FOR_EACH_VEC_ELT (types
, i
, type
)
37703 = build_function_type_list (void_type_node
,
37704 cp_build_reference_type (type
, false),
37706 tree this_reduc_id
= reduc_id
;
37707 if (!dependent_type_p (type
))
37708 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37709 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37710 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37711 DECL_ARTIFICIAL (fndecl
) = 1;
37712 DECL_EXTERNAL (fndecl
) = 1;
37713 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37714 DECL_IGNORED_P (fndecl
) = 1;
37715 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37716 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37717 DECL_ATTRIBUTES (fndecl
)
37718 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37719 DECL_ATTRIBUTES (fndecl
));
37720 if (processing_template_decl
)
37721 fndecl
= push_template_decl (fndecl
);
37722 bool block_scope
= false;
37723 tree block
= NULL_TREE
;
37724 if (current_function_decl
)
37726 block_scope
= true;
37727 DECL_CONTEXT (fndecl
) = global_namespace
;
37728 if (!processing_template_decl
)
37731 else if (current_class_type
)
37735 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37736 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37737 cp_lexer_consume_token (parser
->lexer
);
37738 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37740 cp
= cp_token_cache_new (first_token
,
37741 cp_lexer_peek_nth_token (parser
->lexer
,
37744 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37745 finish_member_declaration (fndecl
);
37746 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37747 DECL_PENDING_INLINE_P (fndecl
) = 1;
37748 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37753 DECL_CONTEXT (fndecl
) = current_namespace
;
37757 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37759 block
= begin_omp_structured_block ();
37762 cp_parser_push_lexer_for_tokens (parser
, cp
);
37763 parser
->lexer
->in_pragma
= true;
37765 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37768 finish_function (/*inline_p=*/false);
37770 DECL_CONTEXT (fndecl
) = current_function_decl
;
37772 cp_parser_pop_lexer (parser
);
37776 cp_parser_pop_lexer (parser
);
37778 finish_function (/*inline_p=*/false);
37781 DECL_CONTEXT (fndecl
) = current_function_decl
;
37782 block
= finish_omp_structured_block (block
);
37783 if (TREE_CODE (block
) == BIND_EXPR
)
37784 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37785 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37786 DECL_SAVED_TREE (fndecl
) = block
;
37787 if (processing_template_decl
)
37788 add_decl_expr (fndecl
);
37790 cp_check_omp_declare_reduction (fndecl
);
37791 if (cp
== NULL
&& types
.length () > 1)
37792 cp
= cp_token_cache_new (first_token
,
37793 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37794 if (errs
!= errorcount
)
37798 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37801 /* Free any declarators allocated. */
37802 obstack_free (&declarator_obstack
, p
);
37806 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37807 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37808 initializer-clause[opt] new-line
37809 #pragma omp declare target new-line */
37812 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37813 enum pragma_context context
)
37815 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37817 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37818 const char *p
= IDENTIFIER_POINTER (id
);
37820 if (strcmp (p
, "simd") == 0)
37822 cp_lexer_consume_token (parser
->lexer
);
37823 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37827 cp_ensure_no_omp_declare_simd (parser
);
37828 if (strcmp (p
, "reduction") == 0)
37830 cp_lexer_consume_token (parser
->lexer
);
37831 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37835 if (!flag_openmp
) /* flag_openmp_simd */
37837 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37840 if (strcmp (p
, "target") == 0)
37842 cp_lexer_consume_token (parser
->lexer
);
37843 cp_parser_omp_declare_target (parser
, pragma_tok
);
37847 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37849 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37854 #pragma omp taskloop taskloop-clause[optseq] new-line
37857 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37860 #define OMP_TASKLOOP_CLAUSE_MASK \
37861 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37877 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37878 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37881 tree clauses
, sb
, ret
;
37883 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37885 strcat (p_name
, " taskloop");
37886 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37888 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37890 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37891 const char *p
= IDENTIFIER_POINTER (id
);
37893 if (strcmp (p
, "simd") == 0)
37895 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37896 if (cclauses
== NULL
)
37897 cclauses
= cclauses_buf
;
37899 cp_lexer_consume_token (parser
->lexer
);
37900 if (!flag_openmp
) /* flag_openmp_simd */
37901 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37903 sb
= begin_omp_structured_block ();
37904 save
= cp_parser_begin_omp_structured_block (parser
);
37905 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37907 cp_parser_end_omp_structured_block (parser
, save
);
37908 tree body
= finish_omp_structured_block (sb
);
37911 ret
= make_node (OMP_TASKLOOP
);
37912 TREE_TYPE (ret
) = void_type_node
;
37913 OMP_FOR_BODY (ret
) = body
;
37914 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37915 SET_EXPR_LOCATION (ret
, loc
);
37920 if (!flag_openmp
) /* flag_openmp_simd */
37922 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37926 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37930 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
37931 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
37934 sb
= begin_omp_structured_block ();
37935 save
= cp_parser_begin_omp_structured_block (parser
);
37937 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
37940 cp_parser_end_omp_structured_block (parser
, save
);
37941 add_stmt (finish_omp_structured_block (sb
));
37948 # pragma acc routine oacc-routine-clause[optseq] new-line
37949 function-definition
37951 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37954 #define OACC_ROUTINE_CLAUSE_MASK \
37955 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37961 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37962 component, which must resolve to a declared namespace-scope
37963 function. The clauses are either processed directly (for a named
37964 function), or defered until the immediatley following declaration
37968 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
37969 enum pragma_context context
)
37971 gcc_checking_assert (context
== pragma_external
);
37972 /* The checking for "another pragma following this one" in the "no optional
37973 '( name )'" case makes sure that we dont re-enter. */
37974 gcc_checking_assert (parser
->oacc_routine
== NULL
);
37976 cp_oacc_routine_data data
;
37977 data
.error_seen
= false;
37978 data
.fndecl_seen
= false;
37979 data
.tokens
= vNULL
;
37980 data
.clauses
= NULL_TREE
;
37981 data
.loc
= pragma_tok
->location
;
37982 /* It is safe to take the address of a local variable; it will only be
37983 used while this scope is live. */
37984 parser
->oacc_routine
= &data
;
37986 /* Look for optional '( name )'. */
37987 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37989 matching_parens parens
;
37990 parens
.consume_open (parser
); /* '(' */
37992 /* We parse the name as an id-expression. If it resolves to
37993 anything other than a non-overloaded function at namespace
37994 scope, it's an error. */
37995 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37996 tree name
= cp_parser_id_expression (parser
,
37997 /*template_keyword_p=*/false,
37998 /*check_dependency_p=*/false,
37999 /*template_p=*/NULL
,
38000 /*declarator_p=*/false,
38001 /*optional_p=*/false);
38002 tree decl
= (identifier_p (name
)
38003 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
38005 if (name
!= error_mark_node
&& decl
== error_mark_node
)
38006 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
38008 if (decl
== error_mark_node
38009 || !parens
.require_close (parser
))
38011 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38012 parser
->oacc_routine
= NULL
;
38017 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38018 "#pragma acc routine",
38019 cp_lexer_peek_token (parser
->lexer
));
38021 if (decl
&& is_overloaded_fn (decl
)
38022 && (TREE_CODE (decl
) != FUNCTION_DECL
38023 || DECL_FUNCTION_TEMPLATE_P (decl
)))
38025 error_at (name_loc
,
38026 "%<#pragma acc routine%> names a set of overloads");
38027 parser
->oacc_routine
= NULL
;
38031 /* Perhaps we should use the same rule as declarations in different
38033 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38035 error_at (name_loc
,
38036 "%qD does not refer to a namespace scope function", decl
);
38037 parser
->oacc_routine
= NULL
;
38041 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38043 error_at (name_loc
, "%qD does not refer to a function", decl
);
38044 parser
->oacc_routine
= NULL
;
38048 cp_finalize_oacc_routine (parser
, decl
, false);
38049 parser
->oacc_routine
= NULL
;
38051 else /* No optional '( name )'. */
38053 /* Store away all pragma tokens. */
38054 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38055 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38056 cp_lexer_consume_token (parser
->lexer
);
38057 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38058 parser
->oacc_routine
->error_seen
= true;
38059 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38060 struct cp_token_cache
*cp
38061 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38062 parser
->oacc_routine
->tokens
.safe_push (cp
);
38064 /* Emit a helpful diagnostic if there's another pragma following this
38066 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38068 cp_ensure_no_oacc_routine (parser
);
38069 data
.tokens
.release ();
38070 /* ..., and then just keep going. */
38074 /* We only have to consider the pragma_external case here. */
38075 cp_parser_declaration (parser
);
38076 if (parser
->oacc_routine
38077 && !parser
->oacc_routine
->fndecl_seen
)
38078 cp_ensure_no_oacc_routine (parser
);
38080 parser
->oacc_routine
= NULL
;
38081 data
.tokens
.release ();
38085 /* Finalize #pragma acc routine clauses after direct declarator has
38089 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38091 struct cp_token_cache
*ce
;
38092 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38094 if (!data
->error_seen
&& data
->fndecl_seen
)
38096 error_at (data
->loc
,
38097 "%<#pragma acc routine%> not immediately followed by "
38098 "a single function declaration or definition");
38099 data
->error_seen
= true;
38101 if (data
->error_seen
)
38104 gcc_checking_assert (data
->tokens
.length () == 1);
38105 ce
= data
->tokens
[0];
38107 cp_parser_push_lexer_for_tokens (parser
, ce
);
38108 parser
->lexer
->in_pragma
= true;
38109 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38111 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38112 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38113 parser
->oacc_routine
->clauses
38114 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38115 "#pragma acc routine", pragma_tok
);
38116 cp_parser_pop_lexer (parser
);
38117 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38123 /* Apply any saved OpenACC routine clauses to a just-parsed
38127 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38129 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38131 /* Keep going if we're in error reporting mode. */
38132 if (parser
->oacc_routine
->error_seen
38133 || fndecl
== error_mark_node
)
38136 if (parser
->oacc_routine
->fndecl_seen
)
38138 error_at (parser
->oacc_routine
->loc
,
38139 "%<#pragma acc routine%> not immediately followed by"
38140 " a single function declaration or definition");
38141 parser
->oacc_routine
= NULL
;
38144 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38146 cp_ensure_no_oacc_routine (parser
);
38150 if (oacc_get_fn_attrib (fndecl
))
38152 error_at (parser
->oacc_routine
->loc
,
38153 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38154 parser
->oacc_routine
= NULL
;
38158 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38160 error_at (parser
->oacc_routine
->loc
,
38162 ? G_("%<#pragma acc routine%> must be applied before use")
38163 : G_("%<#pragma acc routine%> must be applied before "
38165 parser
->oacc_routine
= NULL
;
38169 /* Process the routine's dimension clauses. */
38170 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38171 oacc_replace_fn_attrib (fndecl
, dims
);
38173 /* Add an "omp declare target" attribute. */
38174 DECL_ATTRIBUTES (fndecl
)
38175 = tree_cons (get_identifier ("omp declare target"),
38176 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38178 /* Don't unset parser->oacc_routine here: we may still need it to
38179 diagnose wrong usage. But, remember that we've used this "#pragma acc
38181 parser
->oacc_routine
->fndecl_seen
= true;
38185 /* Main entry point to OpenMP statement pragmas. */
38188 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38191 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38192 omp_clause_mask
mask (0);
38194 switch (cp_parser_pragma_kind (pragma_tok
))
38196 case PRAGMA_OACC_ATOMIC
:
38197 cp_parser_omp_atomic (parser
, pragma_tok
);
38199 case PRAGMA_OACC_CACHE
:
38200 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38202 case PRAGMA_OACC_DATA
:
38203 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38205 case PRAGMA_OACC_ENTER_DATA
:
38206 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38208 case PRAGMA_OACC_EXIT_DATA
:
38209 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38211 case PRAGMA_OACC_HOST_DATA
:
38212 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38214 case PRAGMA_OACC_KERNELS
:
38215 case PRAGMA_OACC_PARALLEL
:
38216 strcpy (p_name
, "#pragma acc");
38217 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38220 case PRAGMA_OACC_LOOP
:
38221 strcpy (p_name
, "#pragma acc");
38222 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38225 case PRAGMA_OACC_UPDATE
:
38226 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38228 case PRAGMA_OACC_WAIT
:
38229 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38231 case PRAGMA_OMP_ATOMIC
:
38232 cp_parser_omp_atomic (parser
, pragma_tok
);
38234 case PRAGMA_OMP_CRITICAL
:
38235 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38237 case PRAGMA_OMP_DISTRIBUTE
:
38238 strcpy (p_name
, "#pragma omp");
38239 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38242 case PRAGMA_OMP_FOR
:
38243 strcpy (p_name
, "#pragma omp");
38244 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38247 case PRAGMA_OMP_MASTER
:
38248 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38250 case PRAGMA_OMP_PARALLEL
:
38251 strcpy (p_name
, "#pragma omp");
38252 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38255 case PRAGMA_OMP_SECTIONS
:
38256 strcpy (p_name
, "#pragma omp");
38257 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38259 case PRAGMA_OMP_SIMD
:
38260 strcpy (p_name
, "#pragma omp");
38261 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38264 case PRAGMA_OMP_SINGLE
:
38265 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38267 case PRAGMA_OMP_TASK
:
38268 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38270 case PRAGMA_OMP_TASKGROUP
:
38271 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38273 case PRAGMA_OMP_TASKLOOP
:
38274 strcpy (p_name
, "#pragma omp");
38275 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38278 case PRAGMA_OMP_TEAMS
:
38279 strcpy (p_name
, "#pragma omp");
38280 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38284 gcc_unreachable ();
38287 protected_set_expr_location (stmt
, pragma_tok
->location
);
38290 /* Transactional Memory parsing routines. */
38292 /* Parse a transaction attribute.
38298 We use this instead of cp_parser_attributes_opt for transactions to avoid
38299 the pedwarn in C++98 mode. */
38302 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38305 tree attr_name
, attr
= NULL
;
38307 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38308 return cp_parser_attributes_opt (parser
);
38310 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38312 cp_lexer_consume_token (parser
->lexer
);
38313 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38316 token
= cp_lexer_peek_token (parser
->lexer
);
38317 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38319 token
= cp_lexer_consume_token (parser
->lexer
);
38321 attr_name
= (token
->type
== CPP_KEYWORD
38322 /* For keywords, use the canonical spelling,
38323 not the parsed identifier. */
38324 ? ridpointers
[(int) token
->keyword
]
38326 attr
= build_tree_list (attr_name
, NULL_TREE
);
38329 cp_parser_error (parser
, "expected identifier");
38331 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38333 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38337 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38339 transaction-statement:
38340 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38342 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38346 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38348 unsigned char old_in
= parser
->in_transaction
;
38349 unsigned char this_in
= 1, new_in
;
38350 enum rid keyword
= token
->keyword
;
38351 tree stmt
, attrs
, noex
;
38353 cp_lexer_consume_token (parser
->lexer
);
38355 if (keyword
== RID_TRANSACTION_RELAXED
38356 || keyword
== RID_SYNCHRONIZED
)
38357 this_in
|= TM_STMT_ATTR_RELAXED
;
38360 attrs
= cp_parser_txn_attribute_opt (parser
);
38362 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38365 /* Parse a noexcept specification. */
38366 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38367 noex
= boolean_true_node
;
38368 else if (keyword
== RID_ATOMIC_CANCEL
)
38370 /* cancel-and-throw is unimplemented. */
38371 sorry ("atomic_cancel");
38375 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38377 /* Keep track if we're in the lexical scope of an outer transaction. */
38378 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38380 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38382 parser
->in_transaction
= new_in
;
38383 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38384 parser
->in_transaction
= old_in
;
38386 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38391 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38393 transaction-expression:
38394 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38395 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38399 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38401 unsigned char old_in
= parser
->in_transaction
;
38402 unsigned char this_in
= 1;
38406 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38408 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38409 || keyword
== RID_TRANSACTION_RELAXED
);
38413 keyword
== RID_TRANSACTION_RELAXED
38414 ? G_("%<__transaction_relaxed%> without transactional memory "
38416 : G_("%<__transaction_atomic%> without transactional memory "
38417 "support enabled"));
38419 token
= cp_parser_require_keyword (parser
, keyword
,
38420 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38421 : RT_TRANSACTION_RELAXED
));
38422 gcc_assert (token
!= NULL
);
38424 if (keyword
== RID_TRANSACTION_RELAXED
)
38425 this_in
|= TM_STMT_ATTR_RELAXED
;
38427 /* Set this early. This might mean that we allow transaction_cancel in
38428 an expression that we find out later actually has to be a constexpr.
38429 However, we expect that cxx_constant_value will be able to deal with
38430 this; also, if the noexcept has no constexpr, then what we parse next
38431 really is a transaction's body. */
38432 parser
->in_transaction
= this_in
;
38434 /* Parse a noexcept specification. */
38435 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38438 if (!noex
|| !noex_expr
38439 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38441 matching_parens parens
;
38442 parens
.require_open (parser
);
38444 expr
= cp_parser_expression (parser
);
38445 expr
= finish_parenthesized_expr (expr
);
38447 parens
.require_close (parser
);
38451 /* The only expression that is available got parsed for the noexcept
38452 already. noexcept is true then. */
38454 noex
= boolean_true_node
;
38457 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38458 parser
->in_transaction
= old_in
;
38460 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38461 return error_mark_node
;
38463 return (flag_tm
? expr
: error_mark_node
);
38466 /* Parse a function-transaction-block.
38468 function-transaction-block:
38469 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38471 __transaction_atomic txn-attribute[opt] function-try-block
38472 __transaction_relaxed ctor-initializer[opt] function-body
38473 __transaction_relaxed function-try-block
38477 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38479 unsigned char old_in
= parser
->in_transaction
;
38480 unsigned char new_in
= 1;
38481 tree compound_stmt
, stmt
, attrs
;
38484 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38485 || keyword
== RID_TRANSACTION_RELAXED
);
38486 token
= cp_parser_require_keyword (parser
, keyword
,
38487 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38488 : RT_TRANSACTION_RELAXED
));
38489 gcc_assert (token
!= NULL
);
38491 if (keyword
== RID_TRANSACTION_RELAXED
)
38492 new_in
|= TM_STMT_ATTR_RELAXED
;
38495 attrs
= cp_parser_txn_attribute_opt (parser
);
38497 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38500 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38502 parser
->in_transaction
= new_in
;
38504 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38505 cp_parser_function_try_block (parser
);
38507 cp_parser_ctor_initializer_opt_and_function_body
38508 (parser
, /*in_function_try_block=*/false);
38510 parser
->in_transaction
= old_in
;
38512 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38515 /* Parse a __transaction_cancel statement.
38518 __transaction_cancel txn-attribute[opt] ;
38519 __transaction_cancel txn-attribute[opt] throw-expression ;
38521 ??? Cancel and throw is not yet implemented. */
38524 cp_parser_transaction_cancel (cp_parser
*parser
)
38527 bool is_outer
= false;
38530 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38531 RT_TRANSACTION_CANCEL
);
38532 gcc_assert (token
!= NULL
);
38534 attrs
= cp_parser_txn_attribute_opt (parser
);
38536 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38538 /* ??? Parse cancel-and-throw here. */
38540 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38544 error_at (token
->location
, "%<__transaction_cancel%> without "
38545 "transactional memory support enabled");
38546 return error_mark_node
;
38548 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38550 error_at (token
->location
, "%<__transaction_cancel%> within a "
38551 "%<__transaction_relaxed%>");
38552 return error_mark_node
;
38556 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38557 && !is_tm_may_cancel_outer (current_function_decl
))
38559 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38560 "within outer %<__transaction_atomic%>");
38561 error_at (token
->location
,
38562 " or a %<transaction_may_cancel_outer%> function");
38563 return error_mark_node
;
38566 else if (parser
->in_transaction
== 0)
38568 error_at (token
->location
, "%<__transaction_cancel%> not within "
38569 "%<__transaction_atomic%>");
38570 return error_mark_node
;
38573 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38581 static GTY (()) cp_parser
*the_parser
;
38584 /* Special handling for the first token or line in the file. The first
38585 thing in the file might be #pragma GCC pch_preprocess, which loads a
38586 PCH file, which is a GC collection point. So we need to handle this
38587 first pragma without benefit of an existing lexer structure.
38589 Always returns one token to the caller in *FIRST_TOKEN. This is
38590 either the true first token of the file, or the first token after
38591 the initial pragma. */
38594 cp_parser_initial_pragma (cp_token
*first_token
)
38598 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38599 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38602 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38603 if (first_token
->type
== CPP_STRING
)
38605 name
= first_token
->u
.value
;
38607 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38608 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38609 error_at (first_token
->location
,
38610 "junk at end of %<#pragma GCC pch_preprocess%>");
38613 error_at (first_token
->location
, "expected string literal");
38615 /* Skip to the end of the pragma. */
38616 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38617 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38619 /* Now actually load the PCH file. */
38621 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38623 /* Read one more token to return to our caller. We have to do this
38624 after reading the PCH file in, since its pointers have to be
38626 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38629 /* Parse a pragma GCC ivdep. */
38632 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38634 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38638 /* Parse a pragma GCC unroll. */
38640 static unsigned short
38641 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38643 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38644 tree expr
= cp_parser_constant_expression (parser
);
38645 unsigned short unroll
;
38646 expr
= maybe_constant_value (expr
);
38647 HOST_WIDE_INT lunroll
= 0;
38648 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38649 || TREE_CODE (expr
) != INTEGER_CST
38650 || (lunroll
= tree_to_shwi (expr
)) < 0
38651 || lunroll
>= USHRT_MAX
)
38653 error_at (location
, "%<#pragma GCC unroll%> requires an"
38654 " assignment-expression that evaluates to a non-negative"
38655 " integral constant less than %u", USHRT_MAX
);
38660 unroll
= (unsigned short)lunroll
;
38664 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38668 /* Normal parsing of a pragma token. Here we can (and must) use the
38672 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38674 cp_token
*pragma_tok
;
38679 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38680 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38681 parser
->lexer
->in_pragma
= true;
38683 id
= cp_parser_pragma_kind (pragma_tok
);
38684 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38685 cp_ensure_no_omp_declare_simd (parser
);
38688 case PRAGMA_GCC_PCH_PREPROCESS
:
38689 error_at (pragma_tok
->location
,
38690 "%<#pragma GCC pch_preprocess%> must be first");
38693 case PRAGMA_OMP_BARRIER
:
38696 case pragma_compound
:
38697 cp_parser_omp_barrier (parser
, pragma_tok
);
38700 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38701 "used in compound statements", "omp barrier");
38708 case PRAGMA_OMP_FLUSH
:
38711 case pragma_compound
:
38712 cp_parser_omp_flush (parser
, pragma_tok
);
38715 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38716 "used in compound statements", "omp flush");
38723 case PRAGMA_OMP_TASKWAIT
:
38726 case pragma_compound
:
38727 cp_parser_omp_taskwait (parser
, pragma_tok
);
38730 error_at (pragma_tok
->location
,
38731 "%<#pragma %s%> may only be used in compound statements",
38739 case PRAGMA_OMP_TASKYIELD
:
38742 case pragma_compound
:
38743 cp_parser_omp_taskyield (parser
, pragma_tok
);
38746 error_at (pragma_tok
->location
,
38747 "%<#pragma %s%> may only be used in compound statements",
38755 case PRAGMA_OMP_CANCEL
:
38758 case pragma_compound
:
38759 cp_parser_omp_cancel (parser
, pragma_tok
);
38762 error_at (pragma_tok
->location
,
38763 "%<#pragma %s%> may only be used in compound statements",
38771 case PRAGMA_OMP_CANCELLATION_POINT
:
38772 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38775 case PRAGMA_OMP_THREADPRIVATE
:
38776 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38779 case PRAGMA_OMP_DECLARE
:
38780 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38782 case PRAGMA_OACC_DECLARE
:
38783 cp_parser_oacc_declare (parser
, pragma_tok
);
38786 case PRAGMA_OACC_ENTER_DATA
:
38787 if (context
== pragma_stmt
)
38789 error_at (pragma_tok
->location
,
38790 "%<#pragma %s%> may only be used in compound statements",
38794 else if (context
!= pragma_compound
)
38796 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38799 case PRAGMA_OACC_EXIT_DATA
:
38800 if (context
== pragma_stmt
)
38802 error_at (pragma_tok
->location
,
38803 "%<#pragma %s%> may only be used in compound statements",
38807 else if (context
!= pragma_compound
)
38809 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38812 case PRAGMA_OACC_ROUTINE
:
38813 if (context
!= pragma_external
)
38815 error_at (pragma_tok
->location
,
38816 "%<#pragma acc routine%> must be at file scope");
38819 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38822 case PRAGMA_OACC_UPDATE
:
38823 if (context
== pragma_stmt
)
38825 error_at (pragma_tok
->location
,
38826 "%<#pragma %s%> may only be used in compound statements",
38830 else if (context
!= pragma_compound
)
38832 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38835 case PRAGMA_OACC_WAIT
:
38836 if (context
== pragma_stmt
)
38838 error_at (pragma_tok
->location
,
38839 "%<#pragma %s%> may only be used in compound statements",
38843 else if (context
!= pragma_compound
)
38845 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38848 case PRAGMA_OACC_ATOMIC
:
38849 case PRAGMA_OACC_CACHE
:
38850 case PRAGMA_OACC_DATA
:
38851 case PRAGMA_OACC_HOST_DATA
:
38852 case PRAGMA_OACC_KERNELS
:
38853 case PRAGMA_OACC_PARALLEL
:
38854 case PRAGMA_OACC_LOOP
:
38855 case PRAGMA_OMP_ATOMIC
:
38856 case PRAGMA_OMP_CRITICAL
:
38857 case PRAGMA_OMP_DISTRIBUTE
:
38858 case PRAGMA_OMP_FOR
:
38859 case PRAGMA_OMP_MASTER
:
38860 case PRAGMA_OMP_PARALLEL
:
38861 case PRAGMA_OMP_SECTIONS
:
38862 case PRAGMA_OMP_SIMD
:
38863 case PRAGMA_OMP_SINGLE
:
38864 case PRAGMA_OMP_TASK
:
38865 case PRAGMA_OMP_TASKGROUP
:
38866 case PRAGMA_OMP_TASKLOOP
:
38867 case PRAGMA_OMP_TEAMS
:
38868 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38870 stmt
= push_omp_privatization_clauses (false);
38871 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38872 pop_omp_privatization_clauses (stmt
);
38875 case PRAGMA_OMP_ORDERED
:
38876 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38878 stmt
= push_omp_privatization_clauses (false);
38879 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38880 pop_omp_privatization_clauses (stmt
);
38883 case PRAGMA_OMP_TARGET
:
38884 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38886 stmt
= push_omp_privatization_clauses (false);
38887 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38888 pop_omp_privatization_clauses (stmt
);
38891 case PRAGMA_OMP_END_DECLARE_TARGET
:
38892 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38895 case PRAGMA_OMP_SECTION
:
38896 error_at (pragma_tok
->location
,
38897 "%<#pragma omp section%> may only be used in "
38898 "%<#pragma omp sections%> construct");
38903 if (context
== pragma_external
)
38905 error_at (pragma_tok
->location
,
38906 "%<#pragma GCC ivdep%> must be inside a function");
38909 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
38910 unsigned short unroll
;
38911 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38912 if (tok
->type
== CPP_PRAGMA
38913 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
38915 tok
= cp_lexer_consume_token (parser
->lexer
);
38916 unroll
= cp_parser_pragma_unroll (parser
, tok
);
38917 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38921 if (tok
->type
!= CPP_KEYWORD
38922 || (tok
->keyword
!= RID_FOR
38923 && tok
->keyword
!= RID_WHILE
38924 && tok
->keyword
!= RID_DO
))
38926 cp_parser_error (parser
, "for, while or do statement expected");
38929 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38933 case PRAGMA_UNROLL
:
38935 if (context
== pragma_external
)
38937 error_at (pragma_tok
->location
,
38938 "%<#pragma GCC unroll%> must be inside a function");
38941 const unsigned short unroll
38942 = cp_parser_pragma_unroll (parser
, pragma_tok
);
38944 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38945 if (tok
->type
== CPP_PRAGMA
38946 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
38948 tok
= cp_lexer_consume_token (parser
->lexer
);
38949 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
38950 tok
= cp_lexer_peek_token (the_parser
->lexer
);
38954 if (tok
->type
!= CPP_KEYWORD
38955 || (tok
->keyword
!= RID_FOR
38956 && tok
->keyword
!= RID_WHILE
38957 && tok
->keyword
!= RID_DO
))
38959 cp_parser_error (parser
, "for, while or do statement expected");
38962 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
38967 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
38968 c_invoke_pragma_handler (id
);
38972 cp_parser_error (parser
, "expected declaration specifiers");
38976 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38980 /* The interface the pragma parsers have to the lexer. */
38983 pragma_lex (tree
*value
, location_t
*loc
)
38985 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
38986 enum cpp_ttype ret
= tok
->type
;
38988 *value
= tok
->u
.value
;
38990 *loc
= tok
->location
;
38992 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
38994 else if (ret
== CPP_STRING
)
38995 *value
= cp_parser_string_literal (the_parser
, false, false);
38998 if (ret
== CPP_KEYWORD
)
39000 cp_lexer_consume_token (the_parser
->lexer
);
39007 /* External interface. */
39009 /* Parse one entire translation unit. */
39012 c_parse_file (void)
39014 static bool already_called
= false;
39016 if (already_called
)
39017 fatal_error (input_location
,
39018 "inter-module optimizations not implemented for C++");
39019 already_called
= true;
39021 the_parser
= cp_parser_new ();
39022 push_deferring_access_checks (flag_access_control
39023 ? dk_no_deferred
: dk_no_check
);
39024 cp_parser_translation_unit (the_parser
);
39028 /* Create an identifier for a generic parameter type (a synthesized
39029 template parameter implied by `auto' or a concept identifier). */
39031 static GTY(()) int generic_parm_count
;
39033 make_generic_type_name ()
39036 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39037 return get_identifier (buf
);
39040 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39041 (creating a new template parameter list if necessary). Returns the newly
39042 created template type parm. */
39045 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39047 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39049 /* Before committing to modifying any scope, if we're in an
39050 implicit template scope, and we're trying to synthesize a
39051 constrained parameter, try to find a previous parameter with
39052 the same name. This is the same-type rule for abbreviated
39053 function templates.
39055 NOTE: We can generate implicit parameters when tentatively
39056 parsing a nested name specifier, only to reject that parse
39057 later. However, matching the same template-id as part of a
39058 direct-declarator should generate an identical template
39059 parameter, so this rule will merge them. */
39060 if (parser
->implicit_template_scope
&& constr
)
39062 tree t
= parser
->implicit_template_parms
;
39065 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39067 tree d
= TREE_VALUE (t
);
39068 if (TREE_CODE (d
) == PARM_DECL
)
39069 /* Return the TEMPLATE_PARM_INDEX. */
39070 d
= DECL_INITIAL (d
);
39073 t
= TREE_CHAIN (t
);
39077 /* We are either continuing a function template that already contains implicit
39078 template parameters, creating a new fully-implicit function template, or
39079 extending an existing explicit function template with implicit template
39082 cp_binding_level
*const entry_scope
= current_binding_level
;
39084 bool become_template
= false;
39085 cp_binding_level
*parent_scope
= 0;
39087 if (parser
->implicit_template_scope
)
39089 gcc_assert (parser
->implicit_template_parms
);
39091 current_binding_level
= parser
->implicit_template_scope
;
39095 /* Roll back to the existing template parameter scope (in the case of
39096 extending an explicit function template) or introduce a new template
39097 parameter scope ahead of the function parameter scope (or class scope
39098 in the case of out-of-line member definitions). The function scope is
39099 added back after template parameter synthesis below. */
39101 cp_binding_level
*scope
= entry_scope
;
39103 while (scope
->kind
== sk_function_parms
)
39105 parent_scope
= scope
;
39106 scope
= scope
->level_chain
;
39108 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39110 /* If not defining a class, then any class scope is a scope level in
39111 an out-of-line member definition. In this case simply wind back
39112 beyond the first such scope to inject the template parameter list.
39113 Otherwise wind back to the class being defined. The latter can
39114 occur in class member friend declarations such as:
39120 friend void A::foo (auto);
39123 The template parameter list synthesized for the friend declaration
39124 must be injected in the scope of 'B'. This can also occur in
39125 erroneous cases such as:
39131 void B::foo (auto) {}
39134 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39135 but, nevertheless, the template parameter list synthesized for the
39136 declarator should be injected into the scope of 'A' as if the
39137 ill-formed template was specified explicitly. */
39139 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39141 parent_scope
= scope
;
39142 scope
= scope
->level_chain
;
39146 current_binding_level
= scope
;
39148 if (scope
->kind
!= sk_template_parms
39149 || !function_being_declared_is_template_p (parser
))
39151 /* Introduce a new template parameter list for implicit template
39154 become_template
= true;
39156 parser
->implicit_template_scope
39157 = begin_scope (sk_template_parms
, NULL
);
39159 ++processing_template_decl
;
39161 parser
->fully_implicit_function_template_p
= true;
39162 ++parser
->num_template_parameter_lists
;
39166 /* Synthesize implicit template parameters at the end of the explicit
39167 template parameter list. */
39169 gcc_assert (current_template_parms
);
39171 parser
->implicit_template_scope
= scope
;
39173 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39174 parser
->implicit_template_parms
39175 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39179 /* Synthesize a new template parameter and track the current template
39180 parameter chain with implicit_template_parms. */
39182 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39183 tree synth_id
= make_generic_type_name ();
39184 tree synth_tmpl_parm
;
39185 bool non_type
= false;
39187 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39189 = finish_template_type_parm (class_type_node
, synth_id
);
39190 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39192 = finish_constrained_template_template_parm (proto
, synth_id
);
39195 synth_tmpl_parm
= copy_decl (proto
);
39196 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39200 // Attach the constraint to the parm before processing.
39201 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39202 TREE_TYPE (node
) = constr
;
39204 = process_template_parm (parser
->implicit_template_parms
,
39207 /*non_type=*/non_type
,
39208 /*param_pack=*/false);
39210 // Chain the new parameter to the list of implicit parameters.
39211 if (parser
->implicit_template_parms
)
39212 parser
->implicit_template_parms
39213 = TREE_CHAIN (parser
->implicit_template_parms
);
39215 parser
->implicit_template_parms
= new_parm
;
39217 tree new_decl
= get_local_decls ();
39219 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39220 new_decl
= DECL_INITIAL (new_decl
);
39222 /* If creating a fully implicit function template, start the new implicit
39223 template parameter list with this synthesized type, otherwise grow the
39224 current template parameter list. */
39226 if (become_template
)
39228 parent_scope
->level_chain
= current_binding_level
;
39230 tree new_parms
= make_tree_vec (1);
39231 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39232 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39233 new_parms
, current_template_parms
);
39237 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39238 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39239 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39240 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39243 // If the new parameter was constrained, we need to add that to the
39244 // constraints in the template parameter list.
39245 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39247 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39248 reqs
= conjoin_constraints (reqs
, req
);
39249 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39252 current_binding_level
= entry_scope
;
39257 /* Finish the declaration of a fully implicit function template. Such a
39258 template has no explicit template parameter list so has not been through the
39259 normal template head and tail processing. synthesize_implicit_template_parm
39260 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39261 provided if the declaration is a class member such that its template
39262 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39263 form is returned. Otherwise NULL_TREE is returned. */
39266 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39268 gcc_assert (parser
->fully_implicit_function_template_p
);
39270 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39271 && DECL_VIRTUAL_P (member_decl_opt
))
39273 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39274 "implicit templates may not be %<virtual%>");
39275 DECL_VIRTUAL_P (member_decl_opt
) = false;
39278 if (member_decl_opt
)
39279 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39280 end_template_decl ();
39282 parser
->fully_implicit_function_template_p
= false;
39283 parser
->implicit_template_parms
= 0;
39284 parser
->implicit_template_scope
= 0;
39285 --parser
->num_template_parameter_lists
;
39287 return member_decl_opt
;
39290 /* Like finish_fully_implicit_template, but to be used in error
39291 recovery, rearranging scopes so that we restore the state we had
39292 before synthesize_implicit_template_parm inserted the implement
39293 template parms scope. */
39296 abort_fully_implicit_template (cp_parser
*parser
)
39298 cp_binding_level
*return_to_scope
= current_binding_level
;
39300 if (parser
->implicit_template_scope
39301 && return_to_scope
!= parser
->implicit_template_scope
)
39303 cp_binding_level
*child
= return_to_scope
;
39304 for (cp_binding_level
*scope
= child
->level_chain
;
39305 scope
!= parser
->implicit_template_scope
;
39306 scope
= child
->level_chain
)
39308 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
39309 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
39310 current_binding_level
= parser
->implicit_template_scope
;
39313 return_to_scope
= return_to_scope
->level_chain
;
39315 finish_fully_implicit_template (parser
, NULL
);
39317 gcc_assert (current_binding_level
== return_to_scope
);
39320 /* Helper function for diagnostics that have complained about things
39321 being used with 'extern "C"' linkage.
39323 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39326 maybe_show_extern_c_location (void)
39328 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39329 inform (the_parser
->innermost_linkage_specification_location
,
39330 "%<extern \"C\"%> linkage started here");
39333 #include "gt-cp-parser.h"