1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2022 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C++ front end.
24 It contains routines to build C++ expressions given their operands,
25 including computing the types of the result, C and C++ specific error
26 checks, and some optimization. */
30 #include "coretypes.h"
32 #include "stor-layout.h"
35 #include "gcc-rich-location.h"
39 process_init_constructor (tree type
, tree init
, int nested
, int flags
,
40 tsubst_flags_t complain
);
43 /* Print an error message stemming from an attempt to use
44 BASETYPE as a base class for TYPE. */
47 error_not_base_type (tree basetype
, tree type
)
49 if (TREE_CODE (basetype
) == FUNCTION_DECL
)
50 basetype
= DECL_CONTEXT (basetype
);
51 error ("type %qT is not a base type for type %qT", basetype
, type
);
52 return error_mark_node
;
56 binfo_or_else (tree base
, tree type
)
58 tree binfo
= lookup_base (type
, base
, ba_unique
,
59 NULL
, tf_warning_or_error
);
61 if (binfo
== error_mark_node
)
64 error_not_base_type (base
, type
);
68 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
69 value may not be changed thereafter. */
72 cxx_readonly_error (location_t loc
, tree arg
, enum lvalue_use errstring
)
75 /* This macro is used to emit diagnostics to ensure that all format
76 strings are complete sentences, visible to gettext and checked at
79 #define ERROR_FOR_ASSIGNMENT(LOC, AS, ASM, IN, DE, ARG) \
84 error_at (LOC, AS, ARG); \
87 error_at (LOC, ASM, ARG); \
90 error_at (LOC, IN, ARG); \
93 error_at (LOC, DE, ARG); \
100 /* Handle C++-specific things first. */
103 && DECL_LANG_SPECIFIC (arg
)
104 && DECL_IN_AGGR_P (arg
)
105 && !TREE_STATIC (arg
))
106 ERROR_FOR_ASSIGNMENT (loc
,
107 G_("assignment of constant field %qD"),
108 G_("constant field %qD used as %<asm%> output"),
109 G_("increment of constant field %qD"),
110 G_("decrement of constant field %qD"),
112 else if (INDIRECT_REF_P (arg
)
113 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (arg
, 0)))
114 && (VAR_P (TREE_OPERAND (arg
, 0))
115 || TREE_CODE (TREE_OPERAND (arg
, 0)) == PARM_DECL
))
116 ERROR_FOR_ASSIGNMENT (loc
,
117 G_("assignment of read-only reference %qD"),
118 G_("read-only reference %qD used as %<asm%> output"),
119 G_("increment of read-only reference %qD"),
120 G_("decrement of read-only reference %qD"),
121 TREE_OPERAND (arg
, 0));
123 readonly_error (loc
, arg
, errstring
);
126 /* If TYPE has abstract virtual functions, issue an error about trying
127 to create an object of that type. DECL is the object declared, or
128 NULL_TREE if the declaration is unavailable, in which case USE specifies
129 the kind of invalid use. Returns 1 if an error occurred; zero if
133 abstract_virtuals_error_sfinae (tree decl
, tree type
, abstract_class_use use
,
134 tsubst_flags_t complain
)
136 vec
<tree
, va_gc
> *pure
;
138 if (TREE_CODE (type
) == ARRAY_TYPE
)
142 type
= strip_array_types (type
);
145 /* This function applies only to classes. Any other entity can never
147 if (!CLASS_TYPE_P (type
))
149 type
= TYPE_MAIN_VARIANT (type
);
152 /* Instantiation here seems to be required by the standard,
153 but breaks e.g. boost::bind. FIXME! */
154 /* In SFINAE, non-N3276 context, force instantiation. */
155 if (!(complain
& (tf_error
|tf_decltype
)))
156 complete_type (type
);
159 if (!TYPE_SIZE (type
))
160 /* TYPE is being defined, and during that time
161 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
164 pure
= CLASSTYPE_PURE_VIRTUALS (type
);
168 if (!(complain
& tf_error
))
171 auto_diagnostic_group d
;
175 error ("cannot declare variable %q+D to be of abstract "
176 "type %qT", decl
, type
);
177 else if (TREE_CODE (decl
) == PARM_DECL
)
179 if (DECL_NAME (decl
))
180 error ("cannot declare parameter %q+D to be of abstract type %qT",
183 error ("cannot declare parameter to be of abstract type %qT",
186 else if (TREE_CODE (decl
) == FIELD_DECL
)
187 error ("cannot declare field %q+D to be of abstract type %qT",
189 else if (TREE_CODE (decl
) == FUNCTION_DECL
190 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
191 error ("invalid abstract return type for member function %q+#D", decl
);
192 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
193 error ("invalid abstract return type for function %q+#D", decl
);
194 else if (identifier_p (decl
))
195 /* Here we do not have location information. */
196 error ("invalid abstract type %qT for %qE", type
, decl
);
198 error ("invalid abstract type for %q+D", decl
);
203 error ("creating array of %qT, which is an abstract class type", type
);
206 error ("invalid cast to abstract class type %qT", type
);
209 error ("invalid new-expression of abstract class type %qT", type
);
212 error ("invalid abstract return type %qT", type
);
215 error ("invalid abstract parameter type %qT", type
);
218 error ("expression of abstract class type %qT cannot "
219 "be used in throw-expression", type
);
222 error ("cannot declare %<catch%> parameter to be of abstract "
223 "class type %qT", type
);
226 error ("cannot allocate an object of abstract type %qT", type
);
229 /* Only go through this once. */
235 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
236 " because the following virtual functions are pure within %qT:",
239 FOR_EACH_VEC_ELT (*pure
, ix
, fn
)
240 if (! DECL_CLONED_FUNCTION_P (fn
)
241 || DECL_COMPLETE_DESTRUCTOR_P (fn
))
242 inform (DECL_SOURCE_LOCATION (fn
), " %#qD", fn
);
244 /* Now truncate the vector. This leaves it non-null, so we know
245 there are pure virtuals, but empty so we don't list them out
254 abstract_virtuals_error_sfinae (tree decl
, tree type
, tsubst_flags_t complain
)
256 return abstract_virtuals_error_sfinae (decl
, type
, ACU_UNKNOWN
, complain
);
260 abstract_virtuals_error_sfinae (abstract_class_use use
, tree type
,
261 tsubst_flags_t complain
)
263 return abstract_virtuals_error_sfinae (NULL_TREE
, type
, use
, complain
);
267 /* Wrapper for the above function in the common case of wanting errors. */
270 abstract_virtuals_error (tree decl
, tree type
)
272 return abstract_virtuals_error_sfinae (decl
, type
, tf_warning_or_error
);
276 abstract_virtuals_error (abstract_class_use use
, tree type
)
278 return abstract_virtuals_error_sfinae (use
, type
, tf_warning_or_error
);
281 /* Print an inform about the declaration of the incomplete type TYPE. */
284 cxx_incomplete_type_inform (const_tree type
)
286 if (!TYPE_MAIN_DECL (type
))
289 location_t loc
= DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
));
290 tree ptype
= strip_top_quals (CONST_CAST_TREE (type
));
292 if (current_class_type
293 && TYPE_BEING_DEFINED (current_class_type
)
294 && same_type_p (ptype
, current_class_type
))
295 inform (loc
, "definition of %q#T is not complete until "
296 "the closing brace", ptype
);
297 else if (!TYPE_TEMPLATE_INFO (ptype
))
298 inform (loc
, "forward declaration of %q#T", ptype
);
300 inform (loc
, "declaration of %q#T", ptype
);
303 /* Print an error message for invalid use of an incomplete type.
304 VALUE is the expression that was used (or 0 if that isn't known)
305 and TYPE is the type that was invalid. DIAG_KIND indicates the
306 type of diagnostic (see diagnostic.def). */
309 cxx_incomplete_type_diagnostic (location_t loc
, const_tree value
,
310 const_tree type
, diagnostic_t diag_kind
)
312 bool is_decl
= false, complained
= false;
314 gcc_assert (diag_kind
== DK_WARNING
315 || diag_kind
== DK_PEDWARN
316 || diag_kind
== DK_ERROR
);
318 /* Avoid duplicate error message. */
319 if (TREE_CODE (type
) == ERROR_MARK
)
324 STRIP_ANY_LOCATION_WRAPPER (value
);
327 || TREE_CODE (value
) == PARM_DECL
328 || TREE_CODE (value
) == FIELD_DECL
)
330 complained
= emit_diagnostic (diag_kind
, DECL_SOURCE_LOCATION (value
), 0,
331 "%qD has incomplete type", value
);
336 /* We must print an error message. Be clever about what it says. */
338 switch (TREE_CODE (type
))
344 complained
= emit_diagnostic (diag_kind
, loc
, 0,
345 "invalid use of incomplete type %q#T",
348 cxx_incomplete_type_inform (type
);
352 emit_diagnostic (diag_kind
, loc
, 0,
353 "invalid use of %qT", type
);
357 if (TYPE_DOMAIN (type
))
359 type
= TREE_TYPE (type
);
362 emit_diagnostic (diag_kind
, loc
, 0,
363 "invalid use of array with unspecified bounds");
369 tree member
= TREE_OPERAND (value
, 1);
370 if (is_overloaded_fn (member
))
371 member
= get_first_fn (member
);
373 if (DECL_FUNCTION_MEMBER_P (member
)
374 && ! flag_ms_extensions
)
376 gcc_rich_location
richloc (loc
);
377 /* If "member" has no arguments (other than "this"), then
378 add a fix-it hint. */
379 if (type_num_arguments (TREE_TYPE (member
)) == 1)
380 richloc
.add_fixit_insert_after ("()");
381 emit_diagnostic (diag_kind
, &richloc
, 0,
382 "invalid use of member function %qD "
383 "(did you forget the %<()%> ?)", member
);
386 emit_diagnostic (diag_kind
, loc
, 0,
387 "invalid use of member %qD "
388 "(did you forget the %<&%> ?)", member
);
392 case TEMPLATE_TYPE_PARM
:
395 if (CLASS_PLACEHOLDER_TEMPLATE (type
))
396 emit_diagnostic (diag_kind
, loc
, 0,
397 "invalid use of placeholder %qT", type
);
399 emit_diagnostic (diag_kind
, loc
, 0,
400 "invalid use of %qT", type
);
403 emit_diagnostic (diag_kind
, loc
, 0,
404 "invalid use of template type parameter %qT", type
);
407 case BOUND_TEMPLATE_TEMPLATE_PARM
:
408 emit_diagnostic (diag_kind
, loc
, 0,
409 "invalid use of template template parameter %qT",
413 case TYPE_PACK_EXPANSION
:
414 emit_diagnostic (diag_kind
, loc
, 0,
415 "invalid use of pack expansion %qT", type
);
420 emit_diagnostic (diag_kind
, loc
, 0,
421 "invalid use of dependent type %qT", type
);
425 if (type
== init_list_type_node
)
427 emit_diagnostic (diag_kind
, loc
, 0,
428 "invalid use of brace-enclosed initializer list");
431 gcc_assert (type
== unknown_type_node
);
432 if (value
&& TREE_CODE (value
) == COMPONENT_REF
)
434 else if (value
&& TREE_CODE (value
) == ADDR_EXPR
)
435 emit_diagnostic (diag_kind
, loc
, 0,
436 "address of overloaded function with no contextual "
438 else if (value
&& TREE_CODE (value
) == OVERLOAD
)
439 emit_diagnostic (diag_kind
, loc
, 0,
440 "overloaded function with no contextual type information");
442 emit_diagnostic (diag_kind
, loc
, 0,
443 "insufficient contextual information to determine type");
451 /* Print an error message for invalid use of an incomplete type.
452 VALUE is the expression that was used (or 0 if that isn't known)
453 and TYPE is the type that was invalid. */
456 cxx_incomplete_type_error (location_t loc
, const_tree value
, const_tree type
)
458 cxx_incomplete_type_diagnostic (loc
, value
, type
, DK_ERROR
);
462 /* We've just initialized subobject SUB; also insert a TARGET_EXPR with an
463 EH-only cleanup for SUB. Because of EH region nesting issues, we need to
464 make the cleanup conditional on a flag that we will clear once the object is
465 fully initialized, so push a new flag onto FLAGS. */
468 maybe_push_temp_cleanup (tree sub
, vec
<tree
,va_gc
> **flags
)
471 = cxx_maybe_build_cleanup (sub
, tf_warning_or_error
))
473 tree tx
= get_target_expr (boolean_true_node
);
474 tree flag
= TARGET_EXPR_SLOT (tx
);
475 CLEANUP_EH_ONLY (tx
) = true;
476 TARGET_EXPR_CLEANUP (tx
) = build3 (COND_EXPR
, void_type_node
,
477 flag
, cleanup
, void_node
);
479 vec_safe_push (*flags
, flag
);
483 /* The recursive part of split_nonconstant_init. DEST is an lvalue
484 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
485 Return true if the whole of the value was initialized by the
486 generated statements. */
489 split_nonconstant_init_1 (tree dest
, tree init
, vec
<tree
,va_gc
> **flags
)
491 unsigned HOST_WIDE_INT idx
, tidx
= HOST_WIDE_INT_M1U
;
492 tree field_index
, value
;
493 tree type
= TREE_TYPE (dest
);
494 tree inner_type
= NULL
;
495 bool array_type_p
= false;
496 bool complete_p
= true;
497 HOST_WIDE_INT num_split_elts
= 0;
499 switch (TREE_CODE (type
))
502 inner_type
= TREE_TYPE (type
);
504 if ((TREE_SIDE_EFFECTS (init
)
505 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
506 || vla_type_p (type
))
508 if (!TYPE_DOMAIN (type
)
509 && TREE_CODE (init
) == CONSTRUCTOR
510 && CONSTRUCTOR_NELTS (init
))
512 /* Flexible array. */
513 cp_complete_array_type (&type
, init
, /*default*/true);
514 dest
= build1 (VIEW_CONVERT_EXPR
, type
, dest
);
517 /* For an array, we only need/want a single cleanup region rather
518 than one per element. build_vec_init will handle it. */
519 tree code
= build_vec_init (dest
, NULL_TREE
, init
, false, 1,
520 tf_warning_or_error
, flags
);
528 case QUAL_UNION_TYPE
:
529 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init
), idx
,
532 /* The current implementation of this algorithm assumes that
533 the field was set for all the elements. This is usually done
534 by process_init_constructor. */
535 gcc_assert (field_index
);
538 inner_type
= TREE_TYPE (field_index
);
542 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
543 NULL_TREE
, NULL_TREE
);
545 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
548 if (TREE_CODE (value
) == CONSTRUCTOR
)
550 if (!split_nonconstant_init_1 (sub
, value
, flags
)
551 /* For flexible array member with initializer we
552 can't remove the initializer, because only the
553 initializer determines how many elements the
554 flexible array member has. */
556 && TREE_CODE (inner_type
) == ARRAY_TYPE
557 && TYPE_DOMAIN (inner_type
) == NULL
558 && TREE_CODE (TREE_TYPE (value
)) == ARRAY_TYPE
559 && COMPLETE_TYPE_P (TREE_TYPE (value
))
560 && !integer_zerop (TYPE_SIZE (TREE_TYPE (value
)))
561 && idx
== CONSTRUCTOR_NELTS (init
) - 1
562 && TYPE_HAS_TRIVIAL_DESTRUCTOR
563 (strip_array_types (inner_type
))))
567 /* Mark element for removal. */
568 CONSTRUCTOR_ELT (init
, idx
)->index
= NULL_TREE
;
574 else if (TREE_CODE (value
) == VEC_INIT_EXPR
)
576 add_stmt (expand_vec_init_expr (sub
, value
, tf_warning_or_error
,
579 /* Mark element for removal. */
580 CONSTRUCTOR_ELT (init
, idx
)->index
= NULL_TREE
;
585 else if (!initializer_constant_valid_p (value
, inner_type
))
589 /* Mark element for removal. */
590 CONSTRUCTOR_ELT (init
, idx
)->index
= NULL_TREE
;
594 if (TREE_CODE (field_index
) == RANGE_EXPR
)
596 /* Use build_vec_init to initialize a range. */
597 tree low
= TREE_OPERAND (field_index
, 0);
598 tree hi
= TREE_OPERAND (field_index
, 1);
599 sub
= build4 (ARRAY_REF
, inner_type
, dest
, low
,
600 NULL_TREE
, NULL_TREE
);
601 sub
= cp_build_addr_expr (sub
, tf_warning_or_error
);
602 tree max
= size_binop (MINUS_EXPR
, hi
, low
);
603 code
= build_vec_init (sub
, max
, value
, false, 0,
604 tf_warning_or_error
);
606 if (tree_fits_shwi_p (max
))
607 num_split_elts
+= tree_to_shwi (max
);
611 /* We may need to add a copy constructor call if
612 the field has [[no_unique_address]]. */
613 if (unsafe_return_slot_p (sub
))
615 /* But not if the initializer is an implicit ctor call
616 we just built in digest_init. */
617 if (TREE_CODE (value
) == TARGET_EXPR
618 && TARGET_EXPR_LIST_INIT_P (value
)
619 && make_safe_copy_elision (sub
, value
))
622 tree name
= (DECL_FIELD_IS_BASE (field_index
)
623 ? base_ctor_identifier
624 : complete_ctor_identifier
);
625 releasing_vec args
= make_tree_vector_single (value
);
626 code
= build_special_member_call
627 (sub
, name
, &args
, inner_type
,
628 LOOKUP_NORMAL
, tf_warning_or_error
);
633 code
= build2 (INIT_EXPR
, inner_type
, sub
, value
);
635 code
= build_stmt (input_location
, EXPR_STMT
, code
);
637 maybe_push_temp_cleanup (sub
, flags
);
643 if (num_split_elts
== 1)
644 CONSTRUCTOR_ELTS (init
)->ordered_remove (tidx
);
645 else if (num_split_elts
> 1)
647 /* Perform the delayed ordered removal of non-constant elements
649 for (idx
= tidx
; idx
< CONSTRUCTOR_NELTS (init
); ++idx
)
650 if (CONSTRUCTOR_ELT (init
, idx
)->index
== NULL_TREE
)
654 *CONSTRUCTOR_ELT (init
, tidx
) = *CONSTRUCTOR_ELT (init
, idx
);
657 vec_safe_truncate (CONSTRUCTOR_ELTS (init
), tidx
);
662 if (!initializer_constant_valid_p (init
, type
))
665 tree cons
= copy_node (init
);
666 CONSTRUCTOR_ELTS (init
) = NULL
;
667 code
= build2 (MODIFY_EXPR
, type
, dest
, cons
);
668 code
= build_stmt (input_location
, EXPR_STMT
, code
);
670 num_split_elts
+= CONSTRUCTOR_NELTS (init
);
678 /* The rest of the initializer is now a constant. */
679 TREE_CONSTANT (init
) = 1;
680 TREE_SIDE_EFFECTS (init
) = 0;
682 /* We didn't split out anything. */
683 if (num_split_elts
== 0)
686 return complete_p
&& complete_ctor_at_level_p (TREE_TYPE (init
),
687 num_split_elts
, inner_type
);
690 /* A subroutine of store_init_value. Splits non-constant static
691 initializer INIT into a constant part and generates code to
692 perform the non-constant part of the initialization to DEST.
693 Returns the code for the runtime init. */
696 split_nonconstant_init (tree dest
, tree init
)
700 if (TREE_CODE (init
) == TARGET_EXPR
)
701 init
= TARGET_EXPR_INITIAL (init
);
702 if (TREE_CODE (init
) == CONSTRUCTOR
)
704 /* Subobject initializers are not full-expressions. */
705 auto fe
= (make_temp_override
706 (current_stmt_tree ()->stmts_are_full_exprs_p
, 0));
708 init
= cp_fully_fold_init (init
);
709 code
= push_stmt_list ();
711 /* If the complete object is an array, build_vec_init's cleanup is
712 enough. Otherwise, collect flags for disabling subobject
713 cleanups once the complete object is fully constructed. */
714 vec
<tree
, va_gc
> *flags
= nullptr;
715 if (TREE_CODE (TREE_TYPE (dest
)) != ARRAY_TYPE
)
716 flags
= make_tree_vector ();
718 if (split_nonconstant_init_1 (dest
, init
, &flags
))
723 /* See maybe_push_temp_cleanup. */
725 tree i
= boolean_false_node
;
726 if (TREE_CODE (f
) == TREE_LIST
)
728 /* To disable a build_vec_init cleanup, set
729 iterator = maxindex. */
730 d
= TREE_PURPOSE (f
);
734 add_stmt (build2 (MODIFY_EXPR
, TREE_TYPE (d
), d
, i
));
736 release_tree_vector (flags
);
738 code
= pop_stmt_list (code
);
739 if (VAR_P (dest
) && !is_local_temp (dest
))
741 DECL_INITIAL (dest
) = init
;
742 TREE_READONLY (dest
) = 0;
746 tree ie
= build2 (INIT_EXPR
, void_type_node
, dest
, init
);
747 code
= add_stmt_to_compound (ie
, code
);
750 else if (TREE_CODE (init
) == STRING_CST
751 && array_of_runtime_bound_p (TREE_TYPE (dest
)))
752 code
= build_vec_init (dest
, NULL_TREE
, init
, /*value-init*/false,
753 /*from array*/1, tf_warning_or_error
);
755 code
= build2 (INIT_EXPR
, TREE_TYPE (dest
), dest
, init
);
760 /* Perform appropriate conversions on the initial value of a variable,
761 store it in the declaration DECL,
762 and print any error messages that are appropriate.
763 If the init is invalid, store an ERROR_MARK.
765 C++: Note that INIT might be a TREE_LIST, which would mean that it is
766 a base class initializer for some aggregate type, hopefully compatible
767 with DECL. If INIT is a single element, and DECL is an aggregate
768 type, we silently convert INIT into a TREE_LIST, allowing a constructor
771 If INIT is a TREE_LIST and there is no constructor, turn INIT
772 into a CONSTRUCTOR and use standard initialization techniques.
773 Perhaps a warning should be generated?
775 Returns code to be executed if initialization could not be performed
776 for static variable. In that case, caller must emit the code. */
779 store_init_value (tree decl
, tree init
, vec
<tree
, va_gc
>** cleanups
, int flags
)
783 /* If variable's type was invalidly declared, just ignore it. */
785 type
= TREE_TYPE (decl
);
786 if (TREE_CODE (type
) == ERROR_MARK
)
789 if (MAYBE_CLASS_TYPE_P (type
))
791 if (TREE_CODE (init
) == TREE_LIST
)
793 error ("constructor syntax used, but no constructor declared "
794 "for type %qT", type
);
795 init
= build_constructor_from_list (init_list_type_node
, nreverse (init
));
799 /* End of special C++ code. */
801 if (flags
& LOOKUP_ALREADY_DIGESTED
)
805 if (TREE_STATIC (decl
))
806 flags
|= LOOKUP_ALLOW_FLEXARRAY_INIT
;
807 /* Digest the specified initializer into an expression. */
808 value
= digest_init_flags (type
, init
, flags
, tf_warning_or_error
);
811 /* Look for braced array initializers for character arrays and
812 recursively convert them into STRING_CSTs. */
813 value
= braced_lists_to_strings (type
, value
);
815 current_ref_temp_count
= 0;
816 value
= extend_ref_init_temps (decl
, value
, cleanups
);
818 /* In C++11 constant expression is a semantic, not syntactic, property.
819 In C++98, make sure that what we thought was a constant expression at
820 template definition time is still constant and otherwise perform this
821 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
822 if (decl_maybe_constant_var_p (decl
) || TREE_STATIC (decl
))
826 if (DECL_DECLARED_CONSTEXPR_P (decl
)
827 || (DECL_IN_AGGR_P (decl
)
828 && DECL_INITIALIZED_IN_CLASS_P (decl
)))
830 value
= fold_non_dependent_expr (value
, tf_warning_or_error
,
831 /*manifestly_const_eval=*/true,
833 /* Diagnose a non-constant initializer for constexpr variable or
834 non-inline in-class-initialized static data member. */
835 if (!require_constant_expression (value
))
836 value
= error_mark_node
;
837 else if (processing_template_decl
)
838 /* In a template we might not have done the necessary
839 transformations to make value actually constant,
840 e.g. extend_ref_init_temps. */
841 value
= maybe_constant_init (value
, decl
, true);
843 value
= cxx_constant_init (value
, decl
);
846 value
= fold_non_dependent_init (value
, tf_warning_or_error
,
847 /*manifestly_const_eval=*/true, decl
);
848 if (TREE_CODE (value
) == CONSTRUCTOR
&& cp_has_mutable_p (type
))
849 /* Poison this CONSTRUCTOR so it can't be copied to another
850 constexpr variable. */
851 CONSTRUCTOR_MUTABLE_POISON (value
) = true;
852 const_init
= (reduced_constant_expression_p (value
)
853 || error_operand_p (value
));
854 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = const_init
;
855 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
856 if (!TYPE_REF_P (type
))
857 TREE_CONSTANT (decl
) = const_init
&& decl_maybe_constant_var_p (decl
);
860 /* [dcl.constinit]/2 "If a variable declared with the constinit
861 specifier has dynamic initialization, the program is
863 if (DECL_DECLARED_CONSTINIT_P (decl
))
865 error_at (location_of (decl
),
866 "%<constinit%> variable %qD does not have a constant "
867 "initializer", decl
);
868 if (require_constant_expression (value
))
869 cxx_constant_init (value
, decl
);
870 value
= error_mark_node
;
876 /* Don't fold initializers of automatic variables in constexpr functions,
877 that might fold away something that needs to be diagnosed at constexpr
879 if (!current_function_decl
880 || !DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
881 || TREE_STATIC (decl
))
882 value
= cp_fully_fold_init (value
);
884 /* Handle aggregate NSDMI in non-constant initializers, too. */
885 value
= replace_placeholders (value
, decl
);
887 /* A COMPOUND_LITERAL_P CONSTRUCTOR is the syntactic form; by the time we get
888 here it should have been digested into an actual value for the type. */
889 gcc_checking_assert (TREE_CODE (value
) != CONSTRUCTOR
890 || processing_template_decl
891 || !TREE_HAS_CONSTRUCTOR (value
));
893 /* If the initializer is not a constant, fill in DECL_INITIAL with
894 the bits that are constant, and then return an expression that
895 will perform the dynamic initialization. */
896 if (value
!= error_mark_node
897 && !processing_template_decl
898 && (TREE_SIDE_EFFECTS (value
)
900 || ! reduced_constant_expression_p (value
)))
901 return split_nonconstant_init (decl
, value
);
903 /* DECL may change value; purge caches. */
904 clear_cv_and_fold_caches ();
906 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
907 is an automatic variable, the middle end will turn this into a
908 dynamic initialization later. */
909 DECL_INITIAL (decl
) = value
;
914 /* Give diagnostic about narrowing conversions within { }, or as part of
915 a converted constant expression. If CONST_ONLY, only check
919 check_narrowing (tree type
, tree init
, tsubst_flags_t complain
,
920 bool const_only
/*= false*/)
922 tree ftype
= unlowered_expr_type (init
);
926 if (((!warn_narrowing
|| !(complain
& tf_warning
))
927 && cxx_dialect
== cxx98
)
928 || !ARITHMETIC_TYPE_P (type
)
929 /* Don't emit bogus warnings with e.g. value-dependent trees. */
930 || instantiation_dependent_expression_p (init
))
933 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
934 && TREE_CODE (type
) == COMPLEX_TYPE
)
936 tree elttype
= TREE_TYPE (type
);
937 if (CONSTRUCTOR_NELTS (init
) > 0)
938 ok
&= check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 0)->value
,
940 if (CONSTRUCTOR_NELTS (init
) > 1)
941 ok
&= check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 1)->value
,
946 /* Even non-dependent expressions can still have template
947 codes like CAST_EXPR, so use *_non_dependent_expr to cope. */
948 init
= fold_non_dependent_expr (init
, complain
, /*manifest*/true);
949 if (init
== error_mark_node
)
952 /* If we were asked to only check constants, return early. */
953 if (const_only
&& !TREE_CONSTANT (init
))
956 if (CP_INTEGRAL_TYPE_P (type
)
957 && TREE_CODE (ftype
) == REAL_TYPE
)
959 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
960 && CP_INTEGRAL_TYPE_P (type
))
962 if (TREE_CODE (ftype
) == ENUMERAL_TYPE
)
963 /* Check for narrowing based on the values of the enumeration. */
964 ftype
= ENUM_UNDERLYING_TYPE (ftype
);
965 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type
),
966 TYPE_MAX_VALUE (ftype
))
967 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype
),
968 TYPE_MIN_VALUE (type
)))
969 && (TREE_CODE (init
) != INTEGER_CST
970 || !int_fits_type_p (init
, type
)))
973 /* [dcl.init.list]#7.2: "from long double to double or float, or from
975 else if (TREE_CODE (ftype
) == REAL_TYPE
976 && TREE_CODE (type
) == REAL_TYPE
)
978 if ((same_type_p (ftype
, long_double_type_node
)
979 && (same_type_p (type
, double_type_node
)
980 || same_type_p (type
, float_type_node
)))
981 || (same_type_p (ftype
, double_type_node
)
982 && same_type_p (type
, float_type_node
))
983 || (TYPE_PRECISION (type
) < TYPE_PRECISION (ftype
)))
985 if (TREE_CODE (init
) == REAL_CST
)
987 /* Issue 703: Loss of precision is OK as long as the value is
988 within the representable range of the new type. */
990 d
= TREE_REAL_CST (init
);
991 real_convert (&r
, TYPE_MODE (type
), &d
);
999 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
1000 && TREE_CODE (type
) == REAL_TYPE
)
1003 if (TREE_CODE (init
) == INTEGER_CST
)
1005 d
= real_value_from_int_cst (0, init
);
1006 if (exact_real_truncate (TYPE_MODE (type
), &d
))
1010 else if (TREE_CODE (type
) == BOOLEAN_TYPE
1011 && (TYPE_PTR_P (ftype
) || TYPE_PTRMEM_P (ftype
)))
1012 /* C++20 P1957R2: converting from a pointer type or a pointer-to-member
1013 type to bool should be considered narrowing. This is a DR so is not
1014 limited to C++20 only. */
1017 bool almost_ok
= ok
;
1018 if (!ok
&& !CONSTANT_CLASS_P (init
) && (complain
& tf_warning_or_error
))
1020 tree folded
= cp_fully_fold (init
);
1021 if (TREE_CONSTANT (folded
) && check_narrowing (type
, folded
, tf_none
))
1027 location_t loc
= cp_expr_loc_or_input_loc (init
);
1028 if (cxx_dialect
== cxx98
)
1030 if (complain
& tf_warning
)
1031 warning_at (loc
, OPT_Wnarrowing
, "narrowing conversion of %qE "
1032 "from %qH to %qI is ill-formed in C++11",
1036 else if (!CONSTANT_CLASS_P (init
))
1038 if (complain
& tf_warning_or_error
)
1040 auto_diagnostic_group d
;
1041 if ((!almost_ok
|| pedantic
)
1042 && pedwarn (loc
, OPT_Wnarrowing
,
1043 "narrowing conversion of %qE from %qH to %qI",
1046 inform (loc
, " the expression has a constant value but is not "
1047 "a C++ constant-expression");
1051 else if (complain
& tf_error
)
1053 int savederrorcount
= errorcount
;
1054 global_dc
->pedantic_errors
= 1;
1055 auto s
= make_temp_override (global_dc
->dc_warn_system_headers
, true);
1056 pedwarn (loc
, OPT_Wnarrowing
,
1057 "narrowing conversion of %qE from %qH to %qI",
1059 if (errorcount
== savederrorcount
)
1061 global_dc
->pedantic_errors
= flag_pedantic_errors
;
1068 /* True iff TYPE is a C++20 "ordinary" character type. */
1071 ordinary_char_type_p (tree type
)
1073 type
= TYPE_MAIN_VARIANT (type
);
1074 return (type
== char_type_node
1075 || type
== signed_char_type_node
1076 || type
== unsigned_char_type_node
);
1079 /* True iff the string literal INIT has a type suitable for initializing array
1083 array_string_literal_compatible_p (tree type
, tree init
)
1085 tree to_char_type
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
1086 tree from_char_type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init
)));
1088 if (to_char_type
== from_char_type
)
1090 /* The array element type does not match the initializing string
1091 literal element type; this is only allowed when both types are
1092 ordinary character type. There are no string literals of
1093 signed or unsigned char type in the language, but we can get
1094 them internally from converting braced-init-lists to
1096 if (ordinary_char_type_p (to_char_type
)
1097 && ordinary_char_type_p (from_char_type
))
1102 /* Process the initializer INIT for a variable of type TYPE, emitting
1103 diagnostics for invalid initializers and converting the initializer as
1106 For aggregate types, it assumes that reshape_init has already run, thus the
1107 initializer will have the right shape (brace elision has been undone).
1109 NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1110 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */
1113 digest_init_r (tree type
, tree init
, int nested
, int flags
,
1114 tsubst_flags_t complain
)
1116 enum tree_code code
= TREE_CODE (type
);
1118 if (error_operand_p (init
))
1119 return error_mark_node
;
1123 /* We must strip the outermost array type when completing the type,
1124 because the its bounds might be incomplete at the moment. */
1125 if (!complete_type_or_maybe_complain (code
== ARRAY_TYPE
1126 ? TREE_TYPE (type
) : type
, NULL_TREE
,
1128 return error_mark_node
;
1130 location_t loc
= cp_expr_loc_or_input_loc (init
);
1132 tree stripped_init
= init
;
1134 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1135 && CONSTRUCTOR_IS_PAREN_INIT (init
))
1136 flags
|= LOOKUP_AGGREGATE_PAREN_INIT
;
1138 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1139 (g++.old-deja/g++.law/casts2.C). */
1140 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
1141 stripped_init
= TREE_OPERAND (init
, 0);
1143 stripped_init
= tree_strip_any_location_wrapper (stripped_init
);
1145 /* Initialization of an array of chars from a string constant. The initializer
1146 can be optionally enclosed in braces, but reshape_init has already removed
1147 them if they were present. */
1148 if (code
== ARRAY_TYPE
)
1150 if (nested
&& !TYPE_DOMAIN (type
))
1151 /* C++ flexible array members have a null domain. */
1153 if (flags
& LOOKUP_ALLOW_FLEXARRAY_INIT
)
1154 pedwarn (loc
, OPT_Wpedantic
,
1155 "initialization of a flexible array member");
1158 if (complain
& tf_error
)
1159 error_at (loc
, "non-static initialization of"
1160 " a flexible array member");
1161 return error_mark_node
;
1165 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
1166 if (char_type_p (typ1
)
1167 && TREE_CODE (stripped_init
) == STRING_CST
)
1169 if (!array_string_literal_compatible_p (type
, init
))
1171 if (complain
& tf_error
)
1172 error_at (loc
, "cannot initialize array of %qT from "
1173 "a string literal with type array of %qT",
1175 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init
))));
1176 return error_mark_node
;
1179 if (nested
== 2 && !TYPE_DOMAIN (type
))
1181 if (complain
& tf_error
)
1182 error_at (loc
, "initialization of flexible array member "
1183 "in a nested context");
1184 return error_mark_node
;
1187 if (type
!= TREE_TYPE (init
)
1188 && !variably_modified_type_p (type
, NULL_TREE
))
1190 init
= copy_node (init
);
1191 TREE_TYPE (init
) = type
;
1192 /* If we have a location wrapper, then also copy the wrapped
1193 node, and update the copy's type. */
1194 if (location_wrapper_p (init
))
1196 stripped_init
= copy_node (stripped_init
);
1197 TREE_OPERAND (init
, 0) = stripped_init
;
1198 TREE_TYPE (stripped_init
) = type
;
1201 if (TYPE_DOMAIN (type
) && TREE_CONSTANT (TYPE_SIZE (type
)))
1203 /* Not a flexible array member. */
1204 int size
= TREE_INT_CST_LOW (TYPE_SIZE (type
));
1205 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
1206 /* In C it is ok to subtract 1 from the length of the string
1207 because it's ok to ignore the terminating null char that is
1208 counted in the length of the constant, but in C++ this would
1210 if (size
< TREE_STRING_LENGTH (stripped_init
))
1212 permerror (loc
, "initializer-string for %qT is too long",
1215 init
= build_string (size
,
1216 TREE_STRING_POINTER (stripped_init
));
1217 TREE_TYPE (init
) = type
;
1224 /* Handle scalar types (including conversions) and references. */
1225 if ((code
!= COMPLEX_TYPE
|| BRACE_ENCLOSED_INITIALIZER_P (stripped_init
))
1226 && (SCALAR_TYPE_P (type
) || code
== REFERENCE_TYPE
))
1228 /* Narrowing is OK when initializing an aggregate from
1229 a parenthesized list. */
1230 if (nested
&& !(flags
& LOOKUP_AGGREGATE_PAREN_INIT
))
1231 flags
|= LOOKUP_NO_NARROWING
;
1232 init
= convert_for_initialization (0, type
, init
, flags
,
1233 ICR_INIT
, NULL_TREE
, 0,
1239 /* Come here only for aggregates: records, arrays, unions, complex numbers
1241 gcc_assert (code
== ARRAY_TYPE
1242 || VECTOR_TYPE_P (type
)
1243 || code
== RECORD_TYPE
1244 || code
== UNION_TYPE
1245 || code
== OPAQUE_TYPE
1246 || code
== COMPLEX_TYPE
);
1248 /* "If T is a class type and the initializer list has a single
1249 element of type cv U, where U is T or a class derived from T,
1250 the object is initialized from that element." */
1251 if (cxx_dialect
>= cxx11
1252 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init
)
1253 && !CONSTRUCTOR_IS_DESIGNATED_INIT (stripped_init
)
1254 && CONSTRUCTOR_NELTS (stripped_init
) == 1
1255 && ((CLASS_TYPE_P (type
) && !CLASSTYPE_NON_AGGREGATE (type
))
1256 || VECTOR_TYPE_P (type
)))
1258 tree elt
= CONSTRUCTOR_ELT (stripped_init
, 0)->value
;
1259 if (reference_related_p (type
, TREE_TYPE (elt
)))
1261 /* In C++17, aggregates can have bases, thus participate in
1262 aggregate initialization. In the following case:
1264 struct B { int c; };
1268 there's an extra set of braces, so the D temporary initializes
1269 the first element of d, which is the B base subobject. The base
1270 of type B is copy-initialized from the D temporary, causing
1272 tree field
= next_initializable_field (TYPE_FIELDS (type
));
1273 if (field
&& DECL_FIELD_IS_BASE (field
))
1275 if (warning_at (loc
, 0, "initializing a base class of type %qT "
1276 "results in object slicing", TREE_TYPE (field
)))
1277 inform (loc
, "remove %<{ }%> around initializer");
1279 else if (flag_checking
)
1280 /* We should have fixed this in reshape_init. */
1285 if (SIMPLE_TARGET_EXPR_P (stripped_init
))
1286 stripped_init
= TARGET_EXPR_INITIAL (stripped_init
);
1288 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init
)
1289 && !TYPE_NON_AGGREGATE_CLASS (type
))
1290 return process_init_constructor (type
, stripped_init
, nested
, flags
,
1294 if (COMPOUND_LITERAL_P (stripped_init
) && code
== ARRAY_TYPE
)
1296 if (complain
& tf_error
)
1297 error_at (loc
, "cannot initialize aggregate of type %qT with "
1298 "a compound literal", type
);
1300 return error_mark_node
;
1303 if (code
== ARRAY_TYPE
1304 && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init
))
1306 /* Allow the result of build_array_copy and of
1307 build_value_init_noctor. */
1308 if ((TREE_CODE (stripped_init
) == VEC_INIT_EXPR
1309 || TREE_CODE (stripped_init
) == CONSTRUCTOR
)
1310 && (same_type_ignoring_top_level_qualifiers_p
1311 (type
, TREE_TYPE (init
))))
1314 if (complain
& tf_error
)
1315 error_at (loc
, "array must be initialized with a brace-enclosed"
1317 return error_mark_node
;
1320 return convert_for_initialization (NULL_TREE
, type
, init
,
1322 ICR_INIT
, NULL_TREE
, 0,
1328 digest_init (tree type
, tree init
, tsubst_flags_t complain
)
1330 return digest_init_r (type
, init
, 0, LOOKUP_IMPLICIT
, complain
);
1334 digest_init_flags (tree type
, tree init
, int flags
, tsubst_flags_t complain
)
1336 return digest_init_r (type
, init
, 0, flags
, complain
);
1339 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1341 digest_nsdmi_init (tree decl
, tree init
, tsubst_flags_t complain
)
1343 gcc_assert (TREE_CODE (decl
) == FIELD_DECL
);
1345 tree type
= TREE_TYPE (decl
);
1346 if (DECL_BIT_FIELD_TYPE (decl
))
1347 type
= DECL_BIT_FIELD_TYPE (decl
);
1348 int flags
= LOOKUP_IMPLICIT
;
1349 if (DIRECT_LIST_INIT_P (init
))
1351 flags
= LOOKUP_NORMAL
;
1352 complain
|= tf_no_cleanup
;
1354 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1355 && CP_AGGREGATE_TYPE_P (type
))
1356 init
= reshape_init (type
, init
, complain
);
1357 init
= digest_init_flags (type
, init
, flags
, complain
);
1361 /* Set of flags used within process_init_constructor to describe the
1363 #define PICFLAG_ERRONEOUS 1
1364 #define PICFLAG_NOT_ALL_CONSTANT 2
1365 #define PICFLAG_NOT_ALL_SIMPLE 4
1366 #define PICFLAG_SIDE_EFFECTS 8
1367 #define PICFLAG_VEC_INIT 16
1369 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1373 picflag_from_initializer (tree init
)
1375 if (init
== error_mark_node
)
1376 return PICFLAG_ERRONEOUS
;
1377 else if (!TREE_CONSTANT (init
))
1379 if (TREE_SIDE_EFFECTS (init
))
1380 return PICFLAG_SIDE_EFFECTS
;
1382 return PICFLAG_NOT_ALL_CONSTANT
;
1384 else if (!initializer_constant_valid_p (init
, TREE_TYPE (init
)))
1385 return PICFLAG_NOT_ALL_SIMPLE
;
1389 /* Adjust INIT for going into a CONSTRUCTOR. */
1392 massage_init_elt (tree type
, tree init
, int nested
, int flags
,
1393 tsubst_flags_t complain
)
1395 int new_flags
= LOOKUP_IMPLICIT
;
1396 if (flags
& LOOKUP_ALLOW_FLEXARRAY_INIT
)
1397 new_flags
|= LOOKUP_ALLOW_FLEXARRAY_INIT
;
1398 if (flags
& LOOKUP_AGGREGATE_PAREN_INIT
)
1399 new_flags
|= LOOKUP_AGGREGATE_PAREN_INIT
;
1400 init
= digest_init_r (type
, init
, nested
? 2 : 1, new_flags
, complain
);
1401 /* When we defer constant folding within a statement, we may want to
1402 defer this folding as well. */
1403 tree t
= fold_non_dependent_init (init
, complain
);
1404 if (TREE_CONSTANT (t
))
1409 /* Subroutine of process_init_constructor, which will process an initializer
1410 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1411 which describe the initializers. */
1414 process_init_constructor_array (tree type
, tree init
, int nested
, int flags
,
1415 tsubst_flags_t complain
)
1417 unsigned HOST_WIDE_INT i
, len
= 0;
1419 bool unbounded
= false;
1420 constructor_elt
*ce
;
1421 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (init
);
1423 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1424 || VECTOR_TYPE_P (type
));
1426 if (TREE_CODE (type
) == ARRAY_TYPE
)
1428 /* C++ flexible array members have a null domain. */
1429 tree domain
= TYPE_DOMAIN (type
);
1430 if (domain
&& TREE_CONSTANT (TYPE_MAX_VALUE (domain
)))
1431 len
= wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain
))
1432 - wi::to_offset (TYPE_MIN_VALUE (domain
)) + 1,
1433 TYPE_PRECISION (TREE_TYPE (domain
)),
1434 TYPE_SIGN (TREE_TYPE (domain
))).to_uhwi ();
1436 unbounded
= true; /* Take as many as there are. */
1438 if (nested
== 2 && !domain
&& !vec_safe_is_empty (v
))
1440 if (complain
& tf_error
)
1441 error_at (cp_expr_loc_or_input_loc (init
),
1442 "initialization of flexible array member "
1443 "in a nested context");
1444 return PICFLAG_ERRONEOUS
;
1448 /* Vectors are like simple fixed-size arrays. */
1449 unbounded
= !TYPE_VECTOR_SUBPARTS (type
).is_constant (&len
);
1451 /* There must not be more initializers than needed. */
1452 if (!unbounded
&& vec_safe_length (v
) > len
)
1454 if (complain
& tf_error
)
1455 error ("too many initializers for %qT", type
);
1457 return PICFLAG_ERRONEOUS
;
1460 FOR_EACH_VEC_SAFE_ELT (v
, i
, ce
)
1463 ce
->index
= size_int (i
);
1464 else if (!check_array_designated_initializer (ce
, i
))
1465 ce
->index
= error_mark_node
;
1466 gcc_assert (ce
->value
);
1468 = massage_init_elt (TREE_TYPE (type
), ce
->value
, nested
, flags
,
1472 (ce
->value
== error_mark_node
1473 || (same_type_ignoring_top_level_qualifiers_p
1474 (strip_array_types (TREE_TYPE (type
)),
1475 strip_array_types (TREE_TYPE (ce
->value
)))));
1477 picflags
|= picflag_from_initializer (ce
->value
);
1480 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1481 we must add initializers ourselves. */
1483 for (; i
< len
; ++i
)
1487 if (type_build_ctor_call (TREE_TYPE (type
)))
1489 /* If this type needs constructors run for default-initialization,
1490 we can't rely on the back end to do it for us, so make the
1491 initialization explicit by list-initializing from T{}. */
1492 next
= build_constructor (init_list_type_node
, NULL
);
1493 next
= massage_init_elt (TREE_TYPE (type
), next
, nested
, flags
,
1495 if (initializer_zerop (next
))
1496 /* The default zero-initialization is fine for us; don't
1497 add anything to the CONSTRUCTOR. */
1500 else if (!zero_init_p (TREE_TYPE (type
)))
1501 next
= build_zero_init (TREE_TYPE (type
),
1502 /*nelts=*/NULL_TREE
,
1503 /*static_storage_p=*/false);
1505 /* The default zero-initialization is fine for us; don't
1506 add anything to the CONSTRUCTOR. */
1511 if (next
!= error_mark_node
1512 && ! seen_error () // Improves error-recovery on anew5.C.
1513 && (initializer_constant_valid_p (next
, TREE_TYPE (next
))
1514 != null_pointer_node
))
1516 /* Use VEC_INIT_EXPR for non-constant initialization of
1517 trailing elements with no explicit initializers. */
1518 picflags
|= PICFLAG_VEC_INIT
;
1522 picflags
|= picflag_from_initializer (next
);
1525 tree range
= build2 (RANGE_EXPR
, size_type_node
,
1526 build_int_cst (size_type_node
, i
),
1527 build_int_cst (size_type_node
, len
- 1));
1528 CONSTRUCTOR_APPEND_ELT (v
, range
, next
);
1532 CONSTRUCTOR_APPEND_ELT (v
, size_int (i
), next
);
1535 /* Don't bother checking all the other elements. */
1539 CONSTRUCTOR_ELTS (init
) = v
;
1543 /* Subroutine of process_init_constructor, which will process an initializer
1544 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1545 the initializers. */
1548 process_init_constructor_record (tree type
, tree init
, int nested
, int flags
,
1549 tsubst_flags_t complain
)
1551 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1555 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
);
1556 gcc_assert (!CLASSTYPE_VBASECLASSES (type
));
1557 gcc_assert (!TYPE_BINFO (type
)
1558 || cxx_dialect
>= cxx17
1559 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1560 gcc_assert (!TYPE_POLYMORPHIC_P (type
));
1564 unsigned HOST_WIDE_INT idx
= 0;
1565 int designator_skip
= -1;
1566 /* Generally, we will always have an index for each initializer (which is
1567 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1568 reshape_init. So we need to handle both cases. */
1569 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1573 if (TREE_CODE (field
) != FIELD_DECL
1574 || (DECL_ARTIFICIAL (field
)
1575 && !(cxx_dialect
>= cxx17
&& DECL_FIELD_IS_BASE (field
))))
1578 if (DECL_UNNAMED_BIT_FIELD (field
))
1581 /* If this is a bitfield, first convert to the declared type. */
1582 tree fldtype
= TREE_TYPE (field
);
1583 if (DECL_BIT_FIELD_TYPE (field
))
1584 fldtype
= DECL_BIT_FIELD_TYPE (field
);
1585 if (fldtype
== error_mark_node
)
1586 return PICFLAG_ERRONEOUS
;
1589 if (idx
< CONSTRUCTOR_NELTS (init
))
1591 constructor_elt
*ce
= &(*CONSTRUCTOR_ELTS (init
))[idx
];
1594 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1595 latter case can happen in templates where lookup has to be
1597 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1598 || identifier_p (ce
->index
));
1599 if (ce
->index
== field
|| ce
->index
== DECL_NAME (field
))
1604 if (designator_skip
== -1)
1605 designator_skip
= 1;
1610 designator_skip
= 0;
1616 gcc_assert (ce
->value
);
1617 next
= massage_init_elt (fldtype
, next
, nested
, flags
, complain
);
1621 if (next
== error_mark_node
)
1622 /* We skip initializers for empty bases/fields, so skipping an invalid
1623 one could make us accept invalid code. */
1624 return PICFLAG_ERRONEOUS
;
1626 /* Already handled above. */;
1627 else if (DECL_INITIAL (field
))
1631 /* We're using an NSDMI past a field with implicit
1632 zero-init. Go back and make it explicit. */
1634 vec_safe_truncate (v
, 0);
1637 /* C++14 aggregate NSDMI. */
1638 next
= get_nsdmi (field
, /*ctor*/false, complain
);
1639 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init
)
1640 && find_placeholders (next
))
1641 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init
) = 1;
1643 else if (type_build_ctor_call (fldtype
))
1645 /* If this type needs constructors run for
1646 default-initialization, we can't rely on the back end to do it
1647 for us, so build up TARGET_EXPRs. If the type in question is
1648 a class, just build one up; if it's an array, recurse. */
1649 next
= build_constructor (init_list_type_node
, NULL
);
1650 next
= massage_init_elt (fldtype
, next
, nested
, flags
, complain
);
1652 /* Warn when some struct elements are implicitly initialized. */
1653 if ((complain
& tf_warning
)
1654 && !cp_unevaluated_operand
1655 && !EMPTY_CONSTRUCTOR_P (init
))
1656 warning (OPT_Wmissing_field_initializers
,
1657 "missing initializer for member %qD", field
);
1661 if (TYPE_REF_P (fldtype
))
1663 if (complain
& tf_error
)
1664 error ("member %qD is uninitialized reference", field
);
1666 return PICFLAG_ERRONEOUS
;
1668 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype
))
1670 if (complain
& tf_error
)
1671 error ("member %qD with uninitialized reference fields", field
);
1673 return PICFLAG_ERRONEOUS
;
1675 /* Do nothing for flexible array members since they need not have any
1676 elements. Don't worry about 'skipped' because a flexarray has to
1677 be the last field. */
1678 else if (TREE_CODE (fldtype
) == ARRAY_TYPE
&& !TYPE_DOMAIN (fldtype
))
1681 /* Warn when some struct elements are implicitly initialized
1683 if ((complain
& tf_warning
)
1684 && !cp_unevaluated_operand
1685 && !EMPTY_CONSTRUCTOR_P (init
))
1686 warning (OPT_Wmissing_field_initializers
,
1687 "missing initializer for member %qD", field
);
1689 if (!zero_init_p (fldtype
) || skipped
< 0)
1691 if (TYPE_REF_P (fldtype
))
1692 next
= build_zero_cst (fldtype
);
1694 next
= build_zero_init (fldtype
, /*nelts=*/NULL_TREE
,
1695 /*static_storage_p=*/false);
1699 /* The default zero-initialization is fine for us; don't
1700 add anything to the CONSTRUCTOR. */
1706 if (is_empty_field (field
)
1707 && !TREE_SIDE_EFFECTS (next
))
1708 /* Don't add trivial initialization of an empty base/field to the
1709 constructor, as they might not be ordered the way the back-end
1713 /* If this is a bitfield, now convert to the lowered type. */
1714 if (fldtype
!= TREE_TYPE (field
))
1715 next
= cp_convert_and_check (TREE_TYPE (field
), next
, complain
);
1716 picflags
|= picflag_from_initializer (next
);
1717 CONSTRUCTOR_APPEND_ELT (v
, field
, next
);
1720 if (idx
< CONSTRUCTOR_NELTS (init
))
1722 if (complain
& tf_error
)
1724 constructor_elt
*ce
= &(*CONSTRUCTOR_ELTS (init
))[idx
];
1725 /* For better diagnostics, try to find out if it is really
1726 the case of too many initializers or if designators are
1727 in incorrect order. */
1728 if (designator_skip
== 1 && ce
->index
)
1730 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1731 || identifier_p (ce
->index
));
1732 for (field
= TYPE_FIELDS (type
);
1733 field
; field
= DECL_CHAIN (field
))
1735 if (TREE_CODE (field
) != FIELD_DECL
1736 || (DECL_ARTIFICIAL (field
)
1737 && !(cxx_dialect
>= cxx17
1738 && DECL_FIELD_IS_BASE (field
))))
1741 if (DECL_UNNAMED_BIT_FIELD (field
))
1744 if (ce
->index
== field
|| ce
->index
== DECL_NAME (field
))
1749 error ("designator order for field %qD does not match declaration "
1750 "order in %qT", field
, type
);
1752 error ("too many initializers for %qT", type
);
1755 return PICFLAG_ERRONEOUS
;
1758 CONSTRUCTOR_ELTS (init
) = v
;
1762 /* Subroutine of process_init_constructor, which will process a single
1763 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1764 which describe the initializer. */
1767 process_init_constructor_union (tree type
, tree init
, int nested
, int flags
,
1768 tsubst_flags_t complain
)
1770 constructor_elt
*ce
;
1773 /* If the initializer was empty, use the union's NSDMI if it has one.
1774 Otherwise use default zero initialization. */
1775 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1777 for (tree field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1779 if (TREE_CODE (field
) == FIELD_DECL
1780 && DECL_INITIAL (field
) != NULL_TREE
)
1782 tree val
= get_nsdmi (field
, /*in_ctor=*/false, complain
);
1783 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init
)
1784 && find_placeholders (val
))
1785 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init
) = 1;
1786 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init
), field
, val
);
1791 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1795 len
= CONSTRUCTOR_ELTS (init
)->length ();
1798 if (!(complain
& tf_error
))
1799 return PICFLAG_ERRONEOUS
;
1800 error ("too many initializers for %qT", type
);
1801 CONSTRUCTOR_ELTS (init
)->block_remove (1, len
-1);
1804 ce
= &(*CONSTRUCTOR_ELTS (init
))[0];
1806 /* If this element specifies a field, initialize via that field. */
1809 if (TREE_CODE (ce
->index
) == FIELD_DECL
)
1811 else if (identifier_p (ce
->index
))
1813 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1814 tree name
= ce
->index
;
1816 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1817 if (DECL_NAME (field
) == name
)
1821 if (complain
& tf_error
)
1822 error ("no field %qD found in union being initialized",
1824 ce
->value
= error_mark_node
;
1830 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
1831 || TREE_CODE (ce
->index
) == RANGE_EXPR
);
1832 if (complain
& tf_error
)
1833 error ("index value instead of field name in union initializer");
1834 ce
->value
= error_mark_node
;
1839 /* Find the first named field. ANSI decided in September 1990
1840 that only named fields count here. */
1841 tree field
= TYPE_FIELDS (type
);
1842 while (field
&& (!DECL_NAME (field
) || TREE_CODE (field
) != FIELD_DECL
))
1843 field
= TREE_CHAIN (field
);
1844 if (field
== NULL_TREE
)
1846 if (complain
& tf_error
)
1847 error ("too many initializers for %qT", type
);
1848 ce
->value
= error_mark_node
;
1853 if (ce
->value
&& ce
->value
!= error_mark_node
)
1854 ce
->value
= massage_init_elt (TREE_TYPE (ce
->index
), ce
->value
, nested
,
1857 return picflag_from_initializer (ce
->value
);
1860 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1861 constructor is a brace-enclosed initializer, and will be modified in-place.
1863 Each element is converted to the right type through digest_init, and
1864 missing initializers are added following the language rules (zero-padding,
1867 After the execution, the initializer will have TREE_CONSTANT if all elts are
1868 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1869 constants that the assembler and linker can compute them.
1871 The function returns the initializer itself, or error_mark_node in case
1875 process_init_constructor (tree type
, tree init
, int nested
, int flags
,
1876 tsubst_flags_t complain
)
1880 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
1882 if (TREE_CODE (type
) == ARRAY_TYPE
|| VECTOR_TYPE_P (type
))
1883 picflags
= process_init_constructor_array (type
, init
, nested
, flags
,
1885 else if (TREE_CODE (type
) == RECORD_TYPE
)
1886 picflags
= process_init_constructor_record (type
, init
, nested
, flags
,
1888 else if (TREE_CODE (type
) == UNION_TYPE
)
1889 picflags
= process_init_constructor_union (type
, init
, nested
, flags
,
1894 if (picflags
& PICFLAG_ERRONEOUS
)
1895 return error_mark_node
;
1897 TREE_TYPE (init
) = type
;
1898 if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
) == NULL_TREE
)
1899 cp_complete_array_type (&TREE_TYPE (init
), init
, /*do_default=*/0);
1900 if (picflags
& PICFLAG_SIDE_EFFECTS
)
1902 TREE_CONSTANT (init
) = false;
1903 TREE_SIDE_EFFECTS (init
) = true;
1905 else if (picflags
& PICFLAG_NOT_ALL_CONSTANT
)
1907 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1908 TREE_CONSTANT (init
) = false;
1909 TREE_SIDE_EFFECTS (init
) = false;
1913 TREE_CONSTANT (init
) = 1;
1914 TREE_SIDE_EFFECTS (init
) = false;
1915 if (!(picflags
& PICFLAG_NOT_ALL_SIMPLE
))
1916 TREE_STATIC (init
) = 1;
1918 if (picflags
& PICFLAG_VEC_INIT
)
1920 /* Defer default-initialization of array elements with no corresponding
1921 initializer-clause until later so we can use a loop. */
1922 TREE_TYPE (init
) = init_list_type_node
;
1923 init
= build_vec_init_expr (type
, init
, complain
);
1928 /* Given a structure or union value DATUM, construct and return
1929 the structure or union component which results from narrowing
1930 that value to the base specified in BASETYPE. For example, given the
1933 class L { int ii; };
1934 class A : L { ... };
1935 class B : L { ... };
1936 class C : A, B { ... };
1944 x.A::ii refers to the ii member of the L part of
1945 the A part of the C object named by X. In this case,
1946 DATUM would be x, and BASETYPE would be A.
1948 I used to think that this was nonconformant, that the standard specified
1949 that first we look up ii in A, then convert x to an L& and pull out the
1950 ii part. But in fact, it does say that we convert x to an A&; A here
1951 is known as the "naming class". (jason 2000-12-19)
1953 BINFO_P points to a variable initialized either to NULL_TREE or to the
1954 binfo for the specific base subobject we want to convert to. */
1957 build_scoped_ref (tree datum
, tree basetype
, tree
* binfo_p
)
1961 if (datum
== error_mark_node
)
1962 return error_mark_node
;
1966 binfo
= lookup_base (TREE_TYPE (datum
), basetype
, ba_check
,
1967 NULL
, tf_warning_or_error
);
1969 if (!binfo
|| binfo
== error_mark_node
)
1971 *binfo_p
= NULL_TREE
;
1973 error_not_base_type (basetype
, TREE_TYPE (datum
));
1974 return error_mark_node
;
1978 return build_base_path (PLUS_EXPR
, datum
, binfo
, 1,
1979 tf_warning_or_error
);
1982 /* Build a reference to an object specified by the C++ `->' operator.
1983 Usually this just involves dereferencing the object, but if the
1984 `->' operator is overloaded, then such overloads must be
1985 performed until an object which does not have the `->' operator
1986 overloaded is found. An error is reported when circular pointer
1987 delegation is detected. */
1990 build_x_arrow (location_t loc
, tree expr
, tsubst_flags_t complain
)
1992 tree orig_expr
= expr
;
1993 tree type
= TREE_TYPE (expr
);
1994 tree last_rval
= NULL_TREE
;
1995 vec
<tree
, va_gc
> *types_memoized
= NULL
;
1997 if (type
== error_mark_node
)
1998 return error_mark_node
;
2000 if (processing_template_decl
)
2002 tree ttype
= NULL_TREE
;
2003 if (type
&& TYPE_PTR_P (type
))
2004 ttype
= TREE_TYPE (type
);
2005 if (ttype
&& !dependent_scope_p (ttype
))
2006 /* Pointer to current instantiation, don't treat as dependent. */;
2007 else if (type_dependent_expression_p (expr
))
2009 expr
= build_min_nt_loc (loc
, ARROW_EXPR
, expr
);
2010 TREE_TYPE (expr
) = ttype
;
2013 expr
= build_non_dependent_expr (expr
);
2016 if (MAYBE_CLASS_TYPE_P (type
))
2018 struct tinst_level
*actual_inst
= current_instantiation ();
2021 while ((expr
= build_new_op (loc
, COMPONENT_REF
,
2022 LOOKUP_NORMAL
, expr
, NULL_TREE
, NULL_TREE
,
2023 NULL_TREE
, &fn
, complain
)))
2025 if (expr
== error_mark_node
)
2026 return error_mark_node
;
2028 /* This provides a better instantiation backtrace in case of
2030 if (fn
&& DECL_USE_TEMPLATE (fn
))
2031 push_tinst_level_loc (fn
,
2032 (current_instantiation () != actual_inst
)
2033 ? DECL_SOURCE_LOCATION (fn
)
2037 if (vec_member (TREE_TYPE (expr
), types_memoized
))
2039 if (complain
& tf_error
)
2040 error ("circular pointer delegation detected");
2041 return error_mark_node
;
2044 vec_safe_push (types_memoized
, TREE_TYPE (expr
));
2048 while (current_instantiation () != actual_inst
)
2051 if (last_rval
== NULL_TREE
)
2053 if (complain
& tf_error
)
2054 error ("base operand of %<->%> has non-pointer type %qT", type
);
2055 return error_mark_node
;
2058 if (TYPE_REF_P (TREE_TYPE (last_rval
)))
2059 last_rval
= convert_from_reference (last_rval
);
2063 last_rval
= decay_conversion (expr
, complain
);
2064 if (last_rval
== error_mark_node
)
2065 return error_mark_node
;
2068 if (TYPE_PTR_P (TREE_TYPE (last_rval
)))
2070 if (processing_template_decl
)
2072 expr
= build_min (ARROW_EXPR
, TREE_TYPE (TREE_TYPE (last_rval
)),
2074 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (last_rval
);
2078 return cp_build_indirect_ref (loc
, last_rval
, RO_ARROW
, complain
);
2081 if (complain
& tf_error
)
2084 error ("result of %<operator->()%> yields non-pointer result");
2086 error ("base operand of %<->%> is not a pointer");
2088 return error_mark_node
;
2091 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
2092 already been checked out to be of aggregate type. */
2095 build_m_component_ref (tree datum
, tree component
, tsubst_flags_t complain
)
2103 datum
= mark_lvalue_use (datum
);
2104 component
= mark_rvalue_use (component
);
2106 if (error_operand_p (datum
) || error_operand_p (component
))
2107 return error_mark_node
;
2109 ptrmem_type
= TREE_TYPE (component
);
2110 if (!TYPE_PTRMEM_P (ptrmem_type
))
2112 if (complain
& tf_error
)
2113 error ("%qE cannot be used as a member pointer, since it is of "
2114 "type %qT", component
, ptrmem_type
);
2115 return error_mark_node
;
2118 objtype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
2119 if (! MAYBE_CLASS_TYPE_P (objtype
))
2121 if (complain
& tf_error
)
2122 error ("cannot apply member pointer %qE to %qE, which is of "
2123 "non-class type %qT", component
, datum
, objtype
);
2124 return error_mark_node
;
2127 type
= TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type
);
2128 ctype
= complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type
));
2130 if (!COMPLETE_TYPE_P (ctype
))
2132 if (!same_type_p (ctype
, objtype
))
2138 binfo
= lookup_base (objtype
, ctype
, ba_check
, NULL
, complain
);
2143 if (complain
& tf_error
)
2144 error ("pointer to member type %qT incompatible with object "
2145 "type %qT", type
, objtype
);
2146 return error_mark_node
;
2148 else if (binfo
== error_mark_node
)
2149 return error_mark_node
;
2152 if (TYPE_PTRDATAMEM_P (ptrmem_type
))
2154 bool is_lval
= real_lvalue_p (datum
);
2157 /* Compute the type of the field, as described in [expr.ref].
2158 There's no such thing as a mutable pointer-to-member, so
2159 things are not as complex as they are for references to
2160 non-static data members. */
2161 type
= cp_build_qualified_type (type
,
2162 (cp_type_quals (type
)
2163 | cp_type_quals (TREE_TYPE (datum
))));
2165 datum
= build_address (datum
);
2167 /* Convert object to the correct base. */
2170 datum
= build_base_path (PLUS_EXPR
, datum
, binfo
, 1, complain
);
2171 if (datum
== error_mark_node
)
2172 return error_mark_node
;
2175 /* Build an expression for "object + offset" where offset is the
2176 value stored in the pointer-to-data-member. */
2177 ptype
= build_pointer_type (type
);
2178 datum
= fold_build_pointer_plus (fold_convert (ptype
, datum
), component
);
2179 datum
= cp_build_fold_indirect_ref (datum
);
2180 if (datum
== error_mark_node
)
2181 return error_mark_node
;
2183 /* If the object expression was an rvalue, return an rvalue. */
2185 datum
= move (datum
);
2190 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
2191 program is ill-formed if the second operand is a pointer to member
2192 function with ref-qualifier & (for C++20: unless its cv-qualifier-seq
2193 is const). In a .* expression whose object expression is an lvalue,
2194 the program is ill-formed if the second operand is a pointer to member
2195 function with ref-qualifier &&. */
2196 if (FUNCTION_REF_QUALIFIED (type
))
2198 bool lval
= lvalue_p (datum
);
2199 if (lval
&& FUNCTION_RVALUE_QUALIFIED (type
))
2201 if (complain
& tf_error
)
2202 error ("pointer-to-member-function type %qT requires an rvalue",
2204 return error_mark_node
;
2206 else if (!lval
&& !FUNCTION_RVALUE_QUALIFIED (type
))
2208 if ((type_memfn_quals (type
)
2209 & (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
))
2212 if (complain
& tf_error
)
2213 error ("pointer-to-member-function type %qT requires "
2214 "an lvalue", ptrmem_type
);
2215 return error_mark_node
;
2217 else if (cxx_dialect
< cxx20
)
2219 if (complain
& tf_warning_or_error
)
2220 pedwarn (input_location
, OPT_Wpedantic
,
2221 "pointer-to-member-function type %qT requires "
2222 "an lvalue before C++20", ptrmem_type
);
2224 return error_mark_node
;
2228 return build2 (OFFSET_REF
, type
, datum
, component
);
2232 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
2235 build_functional_cast_1 (location_t loc
, tree exp
, tree parms
,
2236 tsubst_flags_t complain
)
2238 /* This is either a call to a constructor,
2239 or a C cast in C++'s `functional' notation. */
2241 /* The type to which we are casting. */
2244 if (error_operand_p (exp
) || parms
== error_mark_node
)
2245 return error_mark_node
;
2247 if (TREE_CODE (exp
) == TYPE_DECL
)
2249 type
= TREE_TYPE (exp
);
2251 if (DECL_ARTIFICIAL (exp
))
2252 cp_handle_deprecated_or_unavailable (type
);
2257 /* We need to check this explicitly, since value-initialization of
2258 arrays is allowed in other situations. */
2259 if (TREE_CODE (type
) == ARRAY_TYPE
)
2261 if (complain
& tf_error
)
2262 error_at (loc
, "functional cast to array type %qT", type
);
2263 return error_mark_node
;
2266 if (tree anode
= type_uses_auto (type
))
2269 if (CLASS_PLACEHOLDER_TEMPLATE (anode
))
2271 /* C++23 auto(x). */
2272 else if (!AUTO_IS_DECLTYPE (anode
)
2273 && list_length (parms
) == 1)
2275 init
= TREE_VALUE (parms
);
2276 if (cxx_dialect
< cxx23
)
2277 pedwarn (loc
, OPT_Wc__23_extensions
,
2278 "%<auto(x)%> only available with "
2279 "%<-std=c++2b%> or %<-std=gnu++2b%>");
2283 if (complain
& tf_error
)
2284 error_at (loc
, "invalid use of %qT", anode
);
2285 return error_mark_node
;
2287 type
= do_auto_deduction (type
, init
, anode
, complain
,
2289 if (type
== error_mark_node
)
2290 return error_mark_node
;
2293 if (processing_template_decl
)
2297 /* Diagnose this even in a template. We could also try harder
2298 to give all the usual errors when the type and args are
2300 if (TYPE_REF_P (type
) && !parms
)
2302 if (complain
& tf_error
)
2303 error_at (loc
, "invalid value-initialization of reference type");
2304 return error_mark_node
;
2307 t
= build_min (CAST_EXPR
, type
, parms
);
2308 /* We don't know if it will or will not have side effects. */
2309 TREE_SIDE_EFFECTS (t
) = 1;
2313 if (! MAYBE_CLASS_TYPE_P (type
))
2315 if (parms
== NULL_TREE
)
2317 if (VOID_TYPE_P (type
))
2319 return build_value_init (cv_unqualified (type
), complain
);
2322 /* This must build a C cast. */
2323 parms
= build_x_compound_expr_from_list (parms
, ELK_FUNC_CAST
, complain
);
2324 return cp_build_c_cast (loc
, type
, parms
, complain
);
2327 /* Prepare to evaluate as a call to a constructor. If this expression
2328 is actually used, for example,
2330 return X (arg1, arg2, ...);
2332 then the slot being initialized will be filled in. */
2334 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
2335 return error_mark_node
;
2336 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
2337 return error_mark_node
;
2341 If the expression list is a single-expression, the type
2342 conversion is equivalent (in definedness, and if defined in
2343 meaning) to the corresponding cast expression. */
2344 if (parms
&& TREE_CHAIN (parms
) == NULL_TREE
)
2345 return cp_build_c_cast (loc
, type
, TREE_VALUE (parms
), complain
);
2349 The expression T(), where T is a simple-type-specifier for a
2350 non-array complete object type or the (possibly cv-qualified)
2351 void type, creates an rvalue of the specified type, which is
2352 value-initialized. */
2354 if (parms
== NULL_TREE
)
2356 exp
= build_value_init (type
, complain
);
2357 exp
= get_target_expr_sfinae (exp
, complain
);
2361 /* Call the constructor. */
2362 releasing_vec parmvec
;
2363 for (; parms
!= NULL_TREE
; parms
= TREE_CHAIN (parms
))
2364 vec_safe_push (parmvec
, TREE_VALUE (parms
));
2365 exp
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
2366 &parmvec
, type
, LOOKUP_NORMAL
, complain
);
2368 if (exp
== error_mark_node
)
2369 return error_mark_node
;
2371 return build_cplus_new (type
, exp
, complain
);
2375 build_functional_cast (location_t loc
, tree exp
, tree parms
,
2376 tsubst_flags_t complain
)
2378 tree result
= build_functional_cast_1 (loc
, exp
, parms
, complain
);
2379 protected_set_expr_location (result
, loc
);
2384 /* Add new exception specifier SPEC, to the LIST we currently have.
2385 If it's already in LIST then do nothing.
2386 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2387 know what we're doing. */
2390 add_exception_specifier (tree list
, tree spec
, tsubst_flags_t complain
)
2395 diagnostic_t diag_type
= DK_UNSPECIFIED
; /* none */
2397 if (spec
== error_mark_node
)
2400 gcc_assert (spec
&& (!list
|| TREE_VALUE (list
)));
2402 /* [except.spec] 1, type in an exception specifier shall not be
2403 incomplete, or pointer or ref to incomplete other than pointer
2405 is_ptr
= TYPE_PTR_P (core
);
2406 if (is_ptr
|| TYPE_REF_P (core
))
2407 core
= TREE_TYPE (core
);
2410 else if (VOID_TYPE_P (core
))
2412 else if (TREE_CODE (core
) == TEMPLATE_TYPE_PARM
)
2414 else if (processing_template_decl
)
2416 else if (!verify_type_context (input_location
, TCTX_EXCEPTIONS
, core
,
2417 !(complain
& tf_error
)))
2418 return error_mark_node
;
2422 /* 15.4/1 says that types in an exception specifier must be complete,
2423 but it seems more reasonable to only require this on definitions
2424 and calls. So just give a pedwarn at this point; we will give an
2425 error later if we hit one of those two cases. */
2426 if (!COMPLETE_TYPE_P (complete_type (core
)))
2427 diag_type
= DK_PEDWARN
; /* pedwarn */
2434 for (probe
= list
; probe
; probe
= TREE_CHAIN (probe
))
2435 if (same_type_p (TREE_VALUE (probe
), spec
))
2438 list
= tree_cons (NULL_TREE
, spec
, list
);
2441 diag_type
= DK_ERROR
; /* error */
2443 if (diag_type
!= DK_UNSPECIFIED
2444 && (complain
& tf_warning_or_error
))
2445 cxx_incomplete_type_diagnostic (NULL_TREE
, core
, diag_type
);
2450 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2453 nothrow_spec_p_uninst (const_tree spec
)
2455 if (DEFERRED_NOEXCEPT_SPEC_P (spec
))
2457 return nothrow_spec_p (spec
);
2460 /* Combine the two exceptions specifier lists LIST and ADD, and return
2464 merge_exception_specifiers (tree list
, tree add
)
2466 tree noex
, orig_list
;
2468 if (list
== error_mark_node
|| add
== error_mark_node
)
2469 return error_mark_node
;
2471 /* No exception-specifier or noexcept(false) are less strict than
2472 anything else. Prefer the newer variant (LIST). */
2473 if (!list
|| list
== noexcept_false_spec
)
2475 else if (!add
|| add
== noexcept_false_spec
)
2478 /* noexcept(true) and throw() are stricter than anything else.
2479 As above, prefer the more recent one (LIST). */
2480 if (nothrow_spec_p_uninst (add
))
2483 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2484 if (UNEVALUATED_NOEXCEPT_SPEC_P (add
)
2485 && UNEVALUATED_NOEXCEPT_SPEC_P (list
))
2487 /* We should have instantiated other deferred noexcept specs by now. */
2488 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add
));
2490 if (nothrow_spec_p_uninst (list
))
2492 noex
= TREE_PURPOSE (list
);
2493 gcc_checking_assert (!TREE_PURPOSE (add
)
2494 || errorcount
|| !flag_exceptions
2495 || cp_tree_equal (noex
, TREE_PURPOSE (add
)));
2497 /* Combine the dynamic-exception-specifiers, if any. */
2499 for (; add
&& TREE_VALUE (add
); add
= TREE_CHAIN (add
))
2501 tree spec
= TREE_VALUE (add
);
2504 for (probe
= orig_list
; probe
&& TREE_VALUE (probe
);
2505 probe
= TREE_CHAIN (probe
))
2506 if (same_type_p (TREE_VALUE (probe
), spec
))
2510 spec
= build_tree_list (NULL_TREE
, spec
);
2511 TREE_CHAIN (spec
) = list
;
2516 /* Keep the noexcept-specifier at the beginning of the list. */
2517 if (noex
!= TREE_PURPOSE (list
))
2518 list
= tree_cons (noex
, TREE_VALUE (list
), TREE_CHAIN (list
));
2523 /* Subroutine of build_call. Ensure that each of the types in the
2524 exception specification is complete. Technically, 15.4/1 says that
2525 they need to be complete when we see a declaration of the function,
2526 but we should be able to get away with only requiring this when the
2527 function is defined or called. See also add_exception_specifier. */
2530 require_complete_eh_spec_types (tree fntype
, tree decl
)
2533 /* Don't complain about calls to op new. */
2534 if (decl
&& DECL_ARTIFICIAL (decl
))
2536 for (raises
= TYPE_RAISES_EXCEPTIONS (fntype
); raises
;
2537 raises
= TREE_CHAIN (raises
))
2539 tree type
= TREE_VALUE (raises
);
2540 if (type
&& !COMPLETE_TYPE_P (type
))
2544 ("call to function %qD which throws incomplete type %q#T",
2547 error ("call to function which throws incomplete type %q#T",