1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2021 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 /* The recursive part of split_nonconstant_init. DEST is an lvalue
463 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
464 Return true if the whole of the value was initialized by the
465 generated statements. */
468 split_nonconstant_init_1 (tree dest
, tree init
, bool nested
)
470 unsigned HOST_WIDE_INT idx
, tidx
= HOST_WIDE_INT_M1U
;
471 tree field_index
, value
;
472 tree type
= TREE_TYPE (dest
);
473 tree inner_type
= NULL
;
474 bool array_type_p
= false;
475 bool complete_p
= true;
476 HOST_WIDE_INT num_split_elts
= 0;
478 switch (TREE_CODE (type
))
481 inner_type
= TREE_TYPE (type
);
483 if ((TREE_SIDE_EFFECTS (init
)
484 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
485 || vla_type_p (type
))
487 /* For an array, we only need/want a single cleanup region rather
488 than one per element. */
489 tree code
= build_vec_init (dest
, NULL_TREE
, init
, false, 1,
490 tf_warning_or_error
);
493 /* Also clean up the whole array if something later in an enclosing
495 if (tree cleanup
= cxx_maybe_build_cleanup (dest
,
496 tf_warning_or_error
))
497 finish_eh_cleanup (cleanup
);
504 case QUAL_UNION_TYPE
:
505 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init
), idx
,
508 /* The current implementation of this algorithm assumes that
509 the field was set for all the elements. This is usually done
510 by process_init_constructor. */
511 gcc_assert (field_index
);
514 inner_type
= TREE_TYPE (field_index
);
516 if (TREE_CODE (value
) == CONSTRUCTOR
)
521 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
522 NULL_TREE
, NULL_TREE
);
524 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
527 if (!split_nonconstant_init_1 (sub
, value
, true))
531 /* Mark element for removal. */
532 CONSTRUCTOR_ELT (init
, idx
)->index
= NULL_TREE
;
538 else if (!initializer_constant_valid_p (value
, inner_type
))
543 /* Mark element for removal. */
544 CONSTRUCTOR_ELT (init
, idx
)->index
= NULL_TREE
;
548 if (TREE_CODE (field_index
) == RANGE_EXPR
)
550 /* Use build_vec_init to initialize a range. */
551 tree low
= TREE_OPERAND (field_index
, 0);
552 tree hi
= TREE_OPERAND (field_index
, 1);
553 sub
= build4 (ARRAY_REF
, inner_type
, dest
, low
,
554 NULL_TREE
, NULL_TREE
);
555 sub
= cp_build_addr_expr (sub
, tf_warning_or_error
);
556 tree max
= size_binop (MINUS_EXPR
, hi
, low
);
557 code
= build_vec_init (sub
, max
, value
, false, 0,
558 tf_warning_or_error
);
560 if (tree_fits_shwi_p (max
))
561 num_split_elts
+= tree_to_shwi (max
);
566 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
567 NULL_TREE
, NULL_TREE
);
569 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
572 /* We may need to add a copy constructor call if
573 the field has [[no_unique_address]]. */
574 if (unsafe_return_slot_p (sub
))
576 /* But not if the initializer is an implicit ctor call
577 we just built in digest_init. */
578 if (TREE_CODE (value
) == TARGET_EXPR
579 && TARGET_EXPR_LIST_INIT_P (value
)
580 && make_safe_copy_elision (sub
, value
))
583 tree name
= (DECL_FIELD_IS_BASE (field_index
)
584 ? base_ctor_identifier
585 : complete_ctor_identifier
);
586 releasing_vec args
= make_tree_vector_single (value
);
587 code
= build_special_member_call
588 (sub
, name
, &args
, inner_type
,
589 LOOKUP_NORMAL
, tf_warning_or_error
);
594 code
= build2 (INIT_EXPR
, inner_type
, sub
, value
);
596 code
= build_stmt (input_location
, EXPR_STMT
, code
);
597 code
= maybe_cleanup_point_expr_void (code
);
600 = cxx_maybe_build_cleanup (sub
, tf_warning_or_error
))
601 finish_eh_cleanup (cleanup
);
607 if (num_split_elts
== 1)
608 CONSTRUCTOR_ELTS (init
)->ordered_remove (tidx
);
609 else if (num_split_elts
> 1)
611 /* Perform the delayed ordered removal of non-constant elements
613 for (idx
= tidx
; idx
< CONSTRUCTOR_NELTS (init
); ++idx
)
614 if (CONSTRUCTOR_ELT (init
, idx
)->index
== NULL_TREE
)
618 *CONSTRUCTOR_ELT (init
, tidx
) = *CONSTRUCTOR_ELT (init
, idx
);
621 vec_safe_truncate (CONSTRUCTOR_ELTS (init
), tidx
);
626 if (!initializer_constant_valid_p (init
, type
))
629 tree cons
= copy_node (init
);
630 CONSTRUCTOR_ELTS (init
) = NULL
;
631 code
= build2 (MODIFY_EXPR
, type
, dest
, cons
);
632 code
= build_stmt (input_location
, EXPR_STMT
, code
);
634 num_split_elts
+= CONSTRUCTOR_NELTS (init
);
642 /* The rest of the initializer is now a constant. */
643 TREE_CONSTANT (init
) = 1;
644 TREE_SIDE_EFFECTS (init
) = 0;
646 /* We didn't split out anything. */
647 if (num_split_elts
== 0)
650 return complete_p
&& complete_ctor_at_level_p (TREE_TYPE (init
),
651 num_split_elts
, inner_type
);
654 /* A subroutine of store_init_value. Splits non-constant static
655 initializer INIT into a constant part and generates code to
656 perform the non-constant part of the initialization to DEST.
657 Returns the code for the runtime init. */
660 split_nonconstant_init (tree dest
, tree init
)
664 if (TREE_CODE (init
) == TARGET_EXPR
)
665 init
= TARGET_EXPR_INITIAL (init
);
666 if (TREE_CODE (init
) == CONSTRUCTOR
)
668 init
= cp_fully_fold_init (init
);
669 code
= push_stmt_list ();
670 if (split_nonconstant_init_1 (dest
, init
, false))
672 code
= pop_stmt_list (code
);
673 if (VAR_P (dest
) && !is_local_temp (dest
))
675 DECL_INITIAL (dest
) = init
;
676 TREE_READONLY (dest
) = 0;
680 tree ie
= build2 (INIT_EXPR
, void_type_node
, dest
, init
);
681 code
= add_stmt_to_compound (ie
, code
);
684 else if (TREE_CODE (init
) == STRING_CST
685 && array_of_runtime_bound_p (TREE_TYPE (dest
)))
686 code
= build_vec_init (dest
, NULL_TREE
, init
, /*value-init*/false,
687 /*from array*/1, tf_warning_or_error
);
689 code
= build2 (INIT_EXPR
, TREE_TYPE (dest
), dest
, init
);
694 /* Perform appropriate conversions on the initial value of a variable,
695 store it in the declaration DECL,
696 and print any error messages that are appropriate.
697 If the init is invalid, store an ERROR_MARK.
699 C++: Note that INIT might be a TREE_LIST, which would mean that it is
700 a base class initializer for some aggregate type, hopefully compatible
701 with DECL. If INIT is a single element, and DECL is an aggregate
702 type, we silently convert INIT into a TREE_LIST, allowing a constructor
705 If INIT is a TREE_LIST and there is no constructor, turn INIT
706 into a CONSTRUCTOR and use standard initialization techniques.
707 Perhaps a warning should be generated?
709 Returns code to be executed if initialization could not be performed
710 for static variable. In that case, caller must emit the code. */
713 store_init_value (tree decl
, tree init
, vec
<tree
, va_gc
>** cleanups
, int flags
)
717 /* If variable's type was invalidly declared, just ignore it. */
719 type
= TREE_TYPE (decl
);
720 if (TREE_CODE (type
) == ERROR_MARK
)
723 if (MAYBE_CLASS_TYPE_P (type
))
725 if (TREE_CODE (init
) == TREE_LIST
)
727 error ("constructor syntax used, but no constructor declared "
728 "for type %qT", type
);
729 init
= build_constructor_from_list (init_list_type_node
, nreverse (init
));
733 /* End of special C++ code. */
735 if (flags
& LOOKUP_ALREADY_DIGESTED
)
739 if (TREE_STATIC (decl
))
740 flags
|= LOOKUP_ALLOW_FLEXARRAY_INIT
;
741 /* Digest the specified initializer into an expression. */
742 value
= digest_init_flags (type
, init
, flags
, tf_warning_or_error
);
745 /* Look for braced array initializers for character arrays and
746 recursively convert them into STRING_CSTs. */
747 value
= braced_lists_to_strings (type
, value
);
749 current_ref_temp_count
= 0;
750 value
= extend_ref_init_temps (decl
, value
, cleanups
);
752 /* In C++11 constant expression is a semantic, not syntactic, property.
753 In C++98, make sure that what we thought was a constant expression at
754 template definition time is still constant and otherwise perform this
755 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
756 if (decl_maybe_constant_var_p (decl
) || TREE_STATIC (decl
))
760 if (DECL_DECLARED_CONSTEXPR_P (decl
)
761 || (DECL_IN_AGGR_P (decl
)
762 && DECL_INITIALIZED_IN_CLASS_P (decl
)))
764 value
= fold_non_dependent_expr (value
, tf_warning_or_error
,
765 /*manifestly_const_eval=*/true,
767 /* Diagnose a non-constant initializer for constexpr variable or
768 non-inline in-class-initialized static data member. */
769 if (!require_constant_expression (value
))
770 value
= error_mark_node
;
771 else if (processing_template_decl
)
772 /* In a template we might not have done the necessary
773 transformations to make value actually constant,
774 e.g. extend_ref_init_temps. */
775 value
= maybe_constant_init (value
, decl
, true);
777 value
= cxx_constant_init (value
, decl
);
780 value
= fold_non_dependent_init (value
, tf_warning_or_error
,
781 /*manifestly_const_eval=*/true, decl
);
782 if (TREE_CODE (value
) == CONSTRUCTOR
&& cp_has_mutable_p (type
))
783 /* Poison this CONSTRUCTOR so it can't be copied to another
784 constexpr variable. */
785 CONSTRUCTOR_MUTABLE_POISON (value
) = true;
786 const_init
= (reduced_constant_expression_p (value
)
787 || error_operand_p (value
));
788 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = const_init
;
789 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
790 if (!TYPE_REF_P (type
))
791 TREE_CONSTANT (decl
) = const_init
&& decl_maybe_constant_var_p (decl
);
794 /* [dcl.constinit]/2 "If a variable declared with the constinit
795 specifier has dynamic initialization, the program is
797 if (DECL_DECLARED_CONSTINIT_P (decl
))
799 error_at (location_of (decl
),
800 "%<constinit%> variable %qD does not have a constant "
801 "initializer", decl
);
802 if (require_constant_expression (value
))
803 cxx_constant_init (value
, decl
);
804 value
= error_mark_node
;
810 /* Don't fold initializers of automatic variables in constexpr functions,
811 that might fold away something that needs to be diagnosed at constexpr
813 if (!current_function_decl
814 || !DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
815 || TREE_STATIC (decl
))
816 value
= cp_fully_fold_init (value
);
818 /* Handle aggregate NSDMI in non-constant initializers, too. */
819 value
= replace_placeholders (value
, decl
);
821 /* If the initializer is not a constant, fill in DECL_INITIAL with
822 the bits that are constant, and then return an expression that
823 will perform the dynamic initialization. */
824 if (value
!= error_mark_node
825 && !processing_template_decl
826 && (TREE_SIDE_EFFECTS (value
)
828 || ! reduced_constant_expression_p (value
)))
829 return split_nonconstant_init (decl
, value
);
831 /* DECL may change value; purge caches. */
832 clear_cv_and_fold_caches ();
834 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
835 is an automatic variable, the middle end will turn this into a
836 dynamic initialization later. */
837 DECL_INITIAL (decl
) = value
;
842 /* Give diagnostic about narrowing conversions within { }, or as part of
843 a converted constant expression. If CONST_ONLY, only check
847 check_narrowing (tree type
, tree init
, tsubst_flags_t complain
,
848 bool const_only
/*= false*/)
850 tree ftype
= unlowered_expr_type (init
);
854 if (((!warn_narrowing
|| !(complain
& tf_warning
))
855 && cxx_dialect
== cxx98
)
856 || !ARITHMETIC_TYPE_P (type
)
857 /* Don't emit bogus warnings with e.g. value-dependent trees. */
858 || instantiation_dependent_expression_p (init
))
861 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
862 && TREE_CODE (type
) == COMPLEX_TYPE
)
864 tree elttype
= TREE_TYPE (type
);
865 if (CONSTRUCTOR_NELTS (init
) > 0)
866 ok
&= check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 0)->value
,
868 if (CONSTRUCTOR_NELTS (init
) > 1)
869 ok
&= check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 1)->value
,
874 /* Even non-dependent expressions can still have template
875 codes like CAST_EXPR, so use *_non_dependent_expr to cope. */
876 init
= fold_non_dependent_expr (init
, complain
);
877 if (init
== error_mark_node
)
880 /* If we were asked to only check constants, return early. */
881 if (const_only
&& !TREE_CONSTANT (init
))
884 if (CP_INTEGRAL_TYPE_P (type
)
885 && TREE_CODE (ftype
) == REAL_TYPE
)
887 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
888 && CP_INTEGRAL_TYPE_P (type
))
890 if (TREE_CODE (ftype
) == ENUMERAL_TYPE
)
891 /* Check for narrowing based on the values of the enumeration. */
892 ftype
= ENUM_UNDERLYING_TYPE (ftype
);
893 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type
),
894 TYPE_MAX_VALUE (ftype
))
895 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype
),
896 TYPE_MIN_VALUE (type
)))
897 && (TREE_CODE (init
) != INTEGER_CST
898 || !int_fits_type_p (init
, type
)))
901 /* [dcl.init.list]#7.2: "from long double to double or float, or from
903 else if (TREE_CODE (ftype
) == REAL_TYPE
904 && TREE_CODE (type
) == REAL_TYPE
)
906 if ((same_type_p (ftype
, long_double_type_node
)
907 && (same_type_p (type
, double_type_node
)
908 || same_type_p (type
, float_type_node
)))
909 || (same_type_p (ftype
, double_type_node
)
910 && same_type_p (type
, float_type_node
))
911 || (TYPE_PRECISION (type
) < TYPE_PRECISION (ftype
)))
913 if (TREE_CODE (init
) == REAL_CST
)
915 /* Issue 703: Loss of precision is OK as long as the value is
916 within the representable range of the new type. */
918 d
= TREE_REAL_CST (init
);
919 real_convert (&r
, TYPE_MODE (type
), &d
);
927 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
928 && TREE_CODE (type
) == REAL_TYPE
)
931 if (TREE_CODE (init
) == INTEGER_CST
)
933 d
= real_value_from_int_cst (0, init
);
934 if (exact_real_truncate (TYPE_MODE (type
), &d
))
938 else if (TREE_CODE (type
) == BOOLEAN_TYPE
939 && (TYPE_PTR_P (ftype
) || TYPE_PTRMEM_P (ftype
)))
940 /* C++20 P1957R2: converting from a pointer type or a pointer-to-member
941 type to bool should be considered narrowing. This is a DR so is not
942 limited to C++20 only. */
946 if (!ok
&& !CONSTANT_CLASS_P (init
) && (complain
& tf_warning_or_error
))
948 tree folded
= cp_fully_fold (init
);
949 if (TREE_CONSTANT (folded
) && check_narrowing (type
, folded
, tf_none
))
955 location_t loc
= cp_expr_loc_or_input_loc (init
);
956 if (cxx_dialect
== cxx98
)
958 if (complain
& tf_warning
)
959 warning_at (loc
, OPT_Wnarrowing
, "narrowing conversion of %qE "
960 "from %qH to %qI is ill-formed in C++11",
964 else if (!CONSTANT_CLASS_P (init
))
966 if (complain
& tf_warning_or_error
)
968 auto_diagnostic_group d
;
969 if ((!almost_ok
|| pedantic
)
970 && pedwarn (loc
, OPT_Wnarrowing
,
971 "narrowing conversion of %qE from %qH to %qI",
974 inform (loc
, " the expression has a constant value but is not "
975 "a C++ constant-expression");
979 else if (complain
& tf_error
)
981 int savederrorcount
= errorcount
;
982 global_dc
->pedantic_errors
= 1;
983 pedwarn (loc
, OPT_Wnarrowing
,
984 "narrowing conversion of %qE from %qH to %qI",
986 if (errorcount
== savederrorcount
)
988 global_dc
->pedantic_errors
= flag_pedantic_errors
;
995 /* True iff TYPE is a C++20 "ordinary" character type. */
998 ordinary_char_type_p (tree type
)
1000 type
= TYPE_MAIN_VARIANT (type
);
1001 return (type
== char_type_node
1002 || type
== signed_char_type_node
1003 || type
== unsigned_char_type_node
);
1006 /* Process the initializer INIT for a variable of type TYPE, emitting
1007 diagnostics for invalid initializers and converting the initializer as
1010 For aggregate types, it assumes that reshape_init has already run, thus the
1011 initializer will have the right shape (brace elision has been undone).
1013 NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1014 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */
1017 digest_init_r (tree type
, tree init
, int nested
, int flags
,
1018 tsubst_flags_t complain
)
1020 enum tree_code code
= TREE_CODE (type
);
1022 if (error_operand_p (init
))
1023 return error_mark_node
;
1027 /* We must strip the outermost array type when completing the type,
1028 because the its bounds might be incomplete at the moment. */
1029 if (!complete_type_or_maybe_complain (code
== ARRAY_TYPE
1030 ? TREE_TYPE (type
) : type
, NULL_TREE
,
1032 return error_mark_node
;
1034 location_t loc
= cp_expr_loc_or_input_loc (init
);
1036 tree stripped_init
= init
;
1038 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1039 && CONSTRUCTOR_IS_PAREN_INIT (init
))
1040 flags
|= LOOKUP_AGGREGATE_PAREN_INIT
;
1042 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1043 (g++.old-deja/g++.law/casts2.C). */
1044 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
1045 stripped_init
= TREE_OPERAND (init
, 0);
1047 stripped_init
= tree_strip_any_location_wrapper (stripped_init
);
1049 /* Initialization of an array of chars from a string constant. The initializer
1050 can be optionally enclosed in braces, but reshape_init has already removed
1051 them if they were present. */
1052 if (code
== ARRAY_TYPE
)
1054 if (nested
&& !TYPE_DOMAIN (type
))
1055 /* C++ flexible array members have a null domain. */
1057 if (flags
& LOOKUP_ALLOW_FLEXARRAY_INIT
)
1058 pedwarn (loc
, OPT_Wpedantic
,
1059 "initialization of a flexible array member");
1062 if (complain
& tf_error
)
1063 error_at (loc
, "non-static initialization of"
1064 " a flexible array member");
1065 return error_mark_node
;
1069 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
1070 if (char_type_p (typ1
)
1071 && TREE_CODE (stripped_init
) == STRING_CST
)
1073 tree char_type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init
)));
1074 bool incompat_string_cst
= false;
1076 if (typ1
!= char_type
)
1078 /* The array element type does not match the initializing string
1079 literal element type; this is only allowed when both types are
1080 ordinary character type. There are no string literals of
1081 signed or unsigned char type in the language, but we can get
1082 them internally from converting braced-init-lists to
1084 if (ordinary_char_type_p (typ1
)
1085 && ordinary_char_type_p (char_type
))
1088 incompat_string_cst
= true;
1091 if (incompat_string_cst
)
1093 if (complain
& tf_error
)
1094 error_at (loc
, "cannot initialize array of %qT from "
1095 "a string literal with type array of %qT",
1097 return error_mark_node
;
1100 if (nested
== 2 && !TYPE_DOMAIN (type
))
1102 if (complain
& tf_error
)
1103 error_at (loc
, "initialization of flexible array member "
1104 "in a nested context");
1105 return error_mark_node
;
1108 if (type
!= TREE_TYPE (init
)
1109 && !variably_modified_type_p (type
, NULL_TREE
))
1111 init
= copy_node (init
);
1112 TREE_TYPE (init
) = type
;
1113 /* If we have a location wrapper, then also copy the wrapped
1114 node, and update the copy's type. */
1115 if (location_wrapper_p (init
))
1117 stripped_init
= copy_node (stripped_init
);
1118 TREE_OPERAND (init
, 0) = stripped_init
;
1119 TREE_TYPE (stripped_init
) = type
;
1122 if (TYPE_DOMAIN (type
) && TREE_CONSTANT (TYPE_SIZE (type
)))
1124 /* Not a flexible array member. */
1125 int size
= TREE_INT_CST_LOW (TYPE_SIZE (type
));
1126 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
1127 /* In C it is ok to subtract 1 from the length of the string
1128 because it's ok to ignore the terminating null char that is
1129 counted in the length of the constant, but in C++ this would
1131 if (size
< TREE_STRING_LENGTH (stripped_init
))
1133 permerror (loc
, "initializer-string for %qT is too long",
1136 init
= build_string (size
,
1137 TREE_STRING_POINTER (stripped_init
));
1138 TREE_TYPE (init
) = type
;
1145 /* Handle scalar types (including conversions) and references. */
1146 if ((code
!= COMPLEX_TYPE
|| BRACE_ENCLOSED_INITIALIZER_P (stripped_init
))
1147 && (SCALAR_TYPE_P (type
) || code
== REFERENCE_TYPE
))
1149 /* Narrowing is OK when initializing an aggregate from
1150 a parenthesized list. */
1151 if (nested
&& !(flags
& LOOKUP_AGGREGATE_PAREN_INIT
))
1152 flags
|= LOOKUP_NO_NARROWING
;
1153 init
= convert_for_initialization (0, type
, init
, flags
,
1154 ICR_INIT
, NULL_TREE
, 0,
1160 /* Come here only for aggregates: records, arrays, unions, complex numbers
1162 gcc_assert (code
== ARRAY_TYPE
1163 || VECTOR_TYPE_P (type
)
1164 || code
== RECORD_TYPE
1165 || code
== UNION_TYPE
1166 || code
== OPAQUE_TYPE
1167 || code
== COMPLEX_TYPE
);
1169 /* "If T is a class type and the initializer list has a single
1170 element of type cv U, where U is T or a class derived from T,
1171 the object is initialized from that element." */
1172 if (cxx_dialect
>= cxx11
1173 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init
)
1174 && CONSTRUCTOR_NELTS (stripped_init
) == 1
1175 && ((CLASS_TYPE_P (type
) && !CLASSTYPE_NON_AGGREGATE (type
))
1176 || VECTOR_TYPE_P (type
)))
1178 tree elt
= CONSTRUCTOR_ELT (stripped_init
, 0)->value
;
1179 if (reference_related_p (type
, TREE_TYPE (elt
)))
1181 /* In C++17, aggregates can have bases, thus participate in
1182 aggregate initialization. In the following case:
1184 struct B { int c; };
1188 there's an extra set of braces, so the D temporary initializes
1189 the first element of d, which is the B base subobject. The base
1190 of type B is copy-initialized from the D temporary, causing
1192 tree field
= next_initializable_field (TYPE_FIELDS (type
));
1193 if (field
&& DECL_FIELD_IS_BASE (field
))
1195 if (warning_at (loc
, 0, "initializing a base class of type %qT "
1196 "results in object slicing", TREE_TYPE (field
)))
1197 inform (loc
, "remove %<{ }%> around initializer");
1199 else if (flag_checking
)
1200 /* We should have fixed this in reshape_init. */
1205 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init
)
1206 && !TYPE_NON_AGGREGATE_CLASS (type
))
1207 return process_init_constructor (type
, stripped_init
, nested
, flags
,
1211 if (COMPOUND_LITERAL_P (stripped_init
) && code
== ARRAY_TYPE
)
1213 if (complain
& tf_error
)
1214 error_at (loc
, "cannot initialize aggregate of type %qT with "
1215 "a compound literal", type
);
1217 return error_mark_node
;
1220 if (code
== ARRAY_TYPE
1221 && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init
))
1223 /* Allow the result of build_array_copy and of
1224 build_value_init_noctor. */
1225 if ((TREE_CODE (stripped_init
) == VEC_INIT_EXPR
1226 || TREE_CODE (stripped_init
) == CONSTRUCTOR
)
1227 && (same_type_ignoring_top_level_qualifiers_p
1228 (type
, TREE_TYPE (init
))))
1231 if (complain
& tf_error
)
1232 error_at (loc
, "array must be initialized with a brace-enclosed"
1234 return error_mark_node
;
1237 return convert_for_initialization (NULL_TREE
, type
, init
,
1239 ICR_INIT
, NULL_TREE
, 0,
1245 digest_init (tree type
, tree init
, tsubst_flags_t complain
)
1247 return digest_init_r (type
, init
, 0, LOOKUP_IMPLICIT
, complain
);
1251 digest_init_flags (tree type
, tree init
, int flags
, tsubst_flags_t complain
)
1253 return digest_init_r (type
, init
, 0, flags
, complain
);
1256 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1258 digest_nsdmi_init (tree decl
, tree init
, tsubst_flags_t complain
)
1260 gcc_assert (TREE_CODE (decl
) == FIELD_DECL
);
1262 tree type
= TREE_TYPE (decl
);
1263 if (DECL_BIT_FIELD_TYPE (decl
))
1264 type
= DECL_BIT_FIELD_TYPE (decl
);
1265 int flags
= LOOKUP_IMPLICIT
;
1266 if (DIRECT_LIST_INIT_P (init
))
1268 flags
= LOOKUP_NORMAL
;
1269 complain
|= tf_no_cleanup
;
1271 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1272 && CP_AGGREGATE_TYPE_P (type
))
1273 init
= reshape_init (type
, init
, complain
);
1274 init
= digest_init_flags (type
, init
, flags
, complain
);
1278 /* Set of flags used within process_init_constructor to describe the
1280 #define PICFLAG_ERRONEOUS 1
1281 #define PICFLAG_NOT_ALL_CONSTANT 2
1282 #define PICFLAG_NOT_ALL_SIMPLE 4
1283 #define PICFLAG_SIDE_EFFECTS 8
1285 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1289 picflag_from_initializer (tree init
)
1291 if (init
== error_mark_node
)
1292 return PICFLAG_ERRONEOUS
;
1293 else if (!TREE_CONSTANT (init
))
1295 if (TREE_SIDE_EFFECTS (init
))
1296 return PICFLAG_SIDE_EFFECTS
;
1298 return PICFLAG_NOT_ALL_CONSTANT
;
1300 else if (!initializer_constant_valid_p (init
, TREE_TYPE (init
)))
1301 return PICFLAG_NOT_ALL_SIMPLE
;
1305 /* Adjust INIT for going into a CONSTRUCTOR. */
1308 massage_init_elt (tree type
, tree init
, int nested
, int flags
,
1309 tsubst_flags_t complain
)
1311 int new_flags
= LOOKUP_IMPLICIT
;
1312 if (flags
& LOOKUP_ALLOW_FLEXARRAY_INIT
)
1313 new_flags
|= LOOKUP_ALLOW_FLEXARRAY_INIT
;
1314 if (flags
& LOOKUP_AGGREGATE_PAREN_INIT
)
1315 new_flags
|= LOOKUP_AGGREGATE_PAREN_INIT
;
1316 init
= digest_init_r (type
, init
, nested
? 2 : 1, new_flags
, complain
);
1317 /* Strip a simple TARGET_EXPR when we know this is an initializer. */
1318 if (SIMPLE_TARGET_EXPR_P (init
))
1319 init
= TARGET_EXPR_INITIAL (init
);
1320 /* When we defer constant folding within a statement, we may want to
1321 defer this folding as well. */
1322 tree t
= fold_non_dependent_init (init
, complain
);
1323 if (TREE_CONSTANT (t
))
1328 /* Subroutine of process_init_constructor, which will process an initializer
1329 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1330 which describe the initializers. */
1333 process_init_constructor_array (tree type
, tree init
, int nested
, int flags
,
1334 tsubst_flags_t complain
)
1336 unsigned HOST_WIDE_INT i
, len
= 0;
1338 bool unbounded
= false;
1339 constructor_elt
*ce
;
1340 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (init
);
1342 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1343 || VECTOR_TYPE_P (type
));
1345 if (TREE_CODE (type
) == ARRAY_TYPE
)
1347 /* C++ flexible array members have a null domain. */
1348 tree domain
= TYPE_DOMAIN (type
);
1349 if (domain
&& TREE_CONSTANT (TYPE_MAX_VALUE (domain
)))
1350 len
= wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain
))
1351 - wi::to_offset (TYPE_MIN_VALUE (domain
)) + 1,
1352 TYPE_PRECISION (TREE_TYPE (domain
)),
1353 TYPE_SIGN (TREE_TYPE (domain
))).to_uhwi ();
1355 unbounded
= true; /* Take as many as there are. */
1357 if (nested
== 2 && !domain
&& !vec_safe_is_empty (v
))
1359 if (complain
& tf_error
)
1360 error_at (cp_expr_loc_or_input_loc (init
),
1361 "initialization of flexible array member "
1362 "in a nested context");
1363 return PICFLAG_ERRONEOUS
;
1367 /* Vectors are like simple fixed-size arrays. */
1368 unbounded
= !TYPE_VECTOR_SUBPARTS (type
).is_constant (&len
);
1370 /* There must not be more initializers than needed. */
1371 if (!unbounded
&& vec_safe_length (v
) > len
)
1373 if (complain
& tf_error
)
1374 error ("too many initializers for %qT", type
);
1376 return PICFLAG_ERRONEOUS
;
1379 FOR_EACH_VEC_SAFE_ELT (v
, i
, ce
)
1382 ce
->index
= size_int (i
);
1383 else if (!check_array_designated_initializer (ce
, i
))
1384 ce
->index
= error_mark_node
;
1385 gcc_assert (ce
->value
);
1387 = massage_init_elt (TREE_TYPE (type
), ce
->value
, nested
, flags
,
1391 (ce
->value
== error_mark_node
1392 || (same_type_ignoring_top_level_qualifiers_p
1393 (strip_array_types (TREE_TYPE (type
)),
1394 strip_array_types (TREE_TYPE (ce
->value
)))));
1396 picflags
|= picflag_from_initializer (ce
->value
);
1399 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1400 we must add initializers ourselves. */
1402 for (; i
< len
; ++i
)
1406 if (type_build_ctor_call (TREE_TYPE (type
)))
1408 /* If this type needs constructors run for default-initialization,
1409 we can't rely on the back end to do it for us, so make the
1410 initialization explicit by list-initializing from T{}. */
1411 next
= build_constructor (init_list_type_node
, NULL
);
1412 next
= massage_init_elt (TREE_TYPE (type
), next
, nested
, flags
,
1414 if (initializer_zerop (next
))
1415 /* The default zero-initialization is fine for us; don't
1416 add anything to the CONSTRUCTOR. */
1419 else if (!zero_init_p (TREE_TYPE (type
)))
1420 next
= build_zero_init (TREE_TYPE (type
),
1421 /*nelts=*/NULL_TREE
,
1422 /*static_storage_p=*/false);
1424 /* The default zero-initialization is fine for us; don't
1425 add anything to the CONSTRUCTOR. */
1430 picflags
|= picflag_from_initializer (next
);
1432 && (initializer_constant_valid_p (next
, TREE_TYPE (next
))
1433 == null_pointer_node
))
1435 tree range
= build2 (RANGE_EXPR
, size_type_node
,
1436 build_int_cst (size_type_node
, i
),
1437 build_int_cst (size_type_node
, len
- 1));
1438 CONSTRUCTOR_APPEND_ELT (v
, range
, next
);
1442 CONSTRUCTOR_APPEND_ELT (v
, size_int (i
), next
);
1445 /* Don't bother checking all the other elements. */
1449 CONSTRUCTOR_ELTS (init
) = v
;
1453 /* Subroutine of process_init_constructor, which will process an initializer
1454 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1455 the initializers. */
1458 process_init_constructor_record (tree type
, tree init
, int nested
, int flags
,
1459 tsubst_flags_t complain
)
1461 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1465 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
);
1466 gcc_assert (!CLASSTYPE_VBASECLASSES (type
));
1467 gcc_assert (!TYPE_BINFO (type
)
1468 || cxx_dialect
>= cxx17
1469 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1470 gcc_assert (!TYPE_POLYMORPHIC_P (type
));
1474 unsigned HOST_WIDE_INT idx
= 0;
1475 int designator_skip
= -1;
1476 /* Generally, we will always have an index for each initializer (which is
1477 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1478 reshape_init. So we need to handle both cases. */
1479 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1483 if (TREE_CODE (field
) != FIELD_DECL
1484 || (DECL_ARTIFICIAL (field
)
1485 && !(cxx_dialect
>= cxx17
&& DECL_FIELD_IS_BASE (field
))))
1488 if (DECL_UNNAMED_BIT_FIELD (field
))
1491 /* If this is a bitfield, first convert to the declared type. */
1492 tree fldtype
= TREE_TYPE (field
);
1493 if (DECL_BIT_FIELD_TYPE (field
))
1494 fldtype
= DECL_BIT_FIELD_TYPE (field
);
1495 if (fldtype
== error_mark_node
)
1496 return PICFLAG_ERRONEOUS
;
1499 if (idx
< CONSTRUCTOR_NELTS (init
))
1501 constructor_elt
*ce
= &(*CONSTRUCTOR_ELTS (init
))[idx
];
1504 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1505 latter case can happen in templates where lookup has to be
1507 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1508 || identifier_p (ce
->index
));
1509 if (ce
->index
== field
|| ce
->index
== DECL_NAME (field
))
1511 else if (ANON_AGGR_TYPE_P (fldtype
)
1512 && search_anon_aggr (fldtype
,
1513 TREE_CODE (ce
->index
) == FIELD_DECL
1514 ? DECL_NAME (ce
->index
)
1516 /* If the element is an anonymous union object and the
1517 initializer list is a designated-initializer-list, the
1518 anonymous union object is initialized by the
1519 designated-initializer-list { D }, where D is the
1520 designated-initializer-clause naming a member of the
1521 anonymous union object. */
1522 next
= build_constructor_single (init_list_type_node
,
1523 ce
->index
, ce
->value
);
1527 if (designator_skip
== -1)
1528 designator_skip
= 1;
1533 designator_skip
= 0;
1539 gcc_assert (ce
->value
);
1540 next
= massage_init_elt (fldtype
, next
, nested
, flags
, complain
);
1544 if (next
== error_mark_node
)
1545 /* We skip initializers for empty bases/fields, so skipping an invalid
1546 one could make us accept invalid code. */
1547 return PICFLAG_ERRONEOUS
;
1549 /* Already handled above. */;
1550 else if (DECL_INITIAL (field
))
1554 /* We're using an NSDMI past a field with implicit
1555 zero-init. Go back and make it explicit. */
1557 vec_safe_truncate (v
, 0);
1560 /* C++14 aggregate NSDMI. */
1561 next
= get_nsdmi (field
, /*ctor*/false, complain
);
1562 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init
)
1563 && find_placeholders (next
))
1564 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init
) = 1;
1566 else if (type_build_ctor_call (fldtype
))
1568 /* If this type needs constructors run for
1569 default-initialization, we can't rely on the back end to do it
1570 for us, so build up TARGET_EXPRs. If the type in question is
1571 a class, just build one up; if it's an array, recurse. */
1572 next
= build_constructor (init_list_type_node
, NULL
);
1573 next
= massage_init_elt (fldtype
, next
, nested
, flags
, complain
);
1575 /* Warn when some struct elements are implicitly initialized. */
1576 if ((complain
& tf_warning
)
1577 && !cp_unevaluated_operand
1578 && !EMPTY_CONSTRUCTOR_P (init
))
1579 warning (OPT_Wmissing_field_initializers
,
1580 "missing initializer for member %qD", field
);
1584 if (TYPE_REF_P (fldtype
))
1586 if (complain
& tf_error
)
1587 error ("member %qD is uninitialized reference", field
);
1589 return PICFLAG_ERRONEOUS
;
1591 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype
))
1593 if (complain
& tf_error
)
1594 error ("member %qD with uninitialized reference fields", field
);
1596 return PICFLAG_ERRONEOUS
;
1598 /* Do nothing for flexible array members since they need not have any
1599 elements. Don't worry about 'skipped' because a flexarray has to
1600 be the last field. */
1601 else if (TREE_CODE (fldtype
) == ARRAY_TYPE
&& !TYPE_DOMAIN (fldtype
))
1604 /* Warn when some struct elements are implicitly initialized
1606 if ((complain
& tf_warning
)
1607 && !cp_unevaluated_operand
1608 && !EMPTY_CONSTRUCTOR_P (init
))
1609 warning (OPT_Wmissing_field_initializers
,
1610 "missing initializer for member %qD", field
);
1612 if (!zero_init_p (fldtype
)
1614 next
= build_zero_init (fldtype
, /*nelts=*/NULL_TREE
,
1615 /*static_storage_p=*/false);
1618 /* The default zero-initialization is fine for us; don't
1619 add anything to the CONSTRUCTOR. */
1625 if (DECL_SIZE (field
) && integer_zerop (DECL_SIZE (field
))
1626 && !TREE_SIDE_EFFECTS (next
))
1627 /* Don't add trivial initialization of an empty base/field to the
1628 constructor, as they might not be ordered the way the back-end
1632 /* If this is a bitfield, now convert to the lowered type. */
1633 if (fldtype
!= TREE_TYPE (field
))
1634 next
= cp_convert_and_check (TREE_TYPE (field
), next
, complain
);
1635 picflags
|= picflag_from_initializer (next
);
1636 CONSTRUCTOR_APPEND_ELT (v
, field
, next
);
1639 if (idx
< CONSTRUCTOR_NELTS (init
))
1641 if (complain
& tf_error
)
1643 constructor_elt
*ce
= &(*CONSTRUCTOR_ELTS (init
))[idx
];
1644 /* For better diagnostics, try to find out if it is really
1645 the case of too many initializers or if designators are
1646 in incorrect order. */
1647 if (designator_skip
== 1 && ce
->index
)
1649 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1650 || identifier_p (ce
->index
));
1651 for (field
= TYPE_FIELDS (type
);
1652 field
; field
= DECL_CHAIN (field
))
1654 if (TREE_CODE (field
) != FIELD_DECL
1655 || (DECL_ARTIFICIAL (field
)
1656 && !(cxx_dialect
>= cxx17
1657 && DECL_FIELD_IS_BASE (field
))))
1660 if (DECL_UNNAMED_BIT_FIELD (field
))
1663 if (ce
->index
== field
|| ce
->index
== DECL_NAME (field
))
1665 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1668 = search_anon_aggr (TREE_TYPE (field
),
1669 TREE_CODE (ce
->index
) == FIELD_DECL
1670 ? DECL_NAME (ce
->index
)
1681 error ("designator order for field %qD does not match declaration "
1682 "order in %qT", field
, type
);
1684 error ("too many initializers for %qT", type
);
1687 return PICFLAG_ERRONEOUS
;
1690 CONSTRUCTOR_ELTS (init
) = v
;
1694 /* Subroutine of process_init_constructor, which will process a single
1695 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1696 which describe the initializer. */
1699 process_init_constructor_union (tree type
, tree init
, int nested
, int flags
,
1700 tsubst_flags_t complain
)
1702 constructor_elt
*ce
;
1705 /* If the initializer was empty, use the union's NSDMI if it has one.
1706 Otherwise use default zero initialization. */
1707 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1709 for (tree field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1711 if (TREE_CODE (field
) == FIELD_DECL
1712 && DECL_INITIAL (field
) != NULL_TREE
)
1714 tree val
= get_nsdmi (field
, /*in_ctor=*/false, complain
);
1715 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init
)
1716 && find_placeholders (val
))
1717 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init
) = 1;
1718 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init
), field
, val
);
1723 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1727 len
= CONSTRUCTOR_ELTS (init
)->length ();
1730 if (!(complain
& tf_error
))
1731 return PICFLAG_ERRONEOUS
;
1732 error ("too many initializers for %qT", type
);
1733 CONSTRUCTOR_ELTS (init
)->block_remove (1, len
-1);
1736 ce
= &(*CONSTRUCTOR_ELTS (init
))[0];
1738 /* If this element specifies a field, initialize via that field. */
1741 if (TREE_CODE (ce
->index
) == FIELD_DECL
)
1743 else if (identifier_p (ce
->index
))
1745 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1746 tree name
= ce
->index
;
1748 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1749 if (DECL_NAME (field
) == name
)
1753 if (complain
& tf_error
)
1754 error ("no field %qD found in union being initialized",
1756 ce
->value
= error_mark_node
;
1762 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
1763 || TREE_CODE (ce
->index
) == RANGE_EXPR
);
1764 if (complain
& tf_error
)
1765 error ("index value instead of field name in union initializer");
1766 ce
->value
= error_mark_node
;
1771 /* Find the first named field. ANSI decided in September 1990
1772 that only named fields count here. */
1773 tree field
= TYPE_FIELDS (type
);
1774 while (field
&& (!DECL_NAME (field
) || TREE_CODE (field
) != FIELD_DECL
))
1775 field
= TREE_CHAIN (field
);
1776 if (field
== NULL_TREE
)
1778 if (complain
& tf_error
)
1779 error ("too many initializers for %qT", type
);
1780 ce
->value
= error_mark_node
;
1785 if (ce
->value
&& ce
->value
!= error_mark_node
)
1786 ce
->value
= massage_init_elt (TREE_TYPE (ce
->index
), ce
->value
, nested
,
1789 return picflag_from_initializer (ce
->value
);
1792 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1793 constructor is a brace-enclosed initializer, and will be modified in-place.
1795 Each element is converted to the right type through digest_init, and
1796 missing initializers are added following the language rules (zero-padding,
1799 After the execution, the initializer will have TREE_CONSTANT if all elts are
1800 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1801 constants that the assembler and linker can compute them.
1803 The function returns the initializer itself, or error_mark_node in case
1807 process_init_constructor (tree type
, tree init
, int nested
, int flags
,
1808 tsubst_flags_t complain
)
1812 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
1814 if (TREE_CODE (type
) == ARRAY_TYPE
|| VECTOR_TYPE_P (type
))
1815 picflags
= process_init_constructor_array (type
, init
, nested
, flags
,
1817 else if (TREE_CODE (type
) == RECORD_TYPE
)
1818 picflags
= process_init_constructor_record (type
, init
, nested
, flags
,
1820 else if (TREE_CODE (type
) == UNION_TYPE
)
1821 picflags
= process_init_constructor_union (type
, init
, nested
, flags
,
1826 if (picflags
& PICFLAG_ERRONEOUS
)
1827 return error_mark_node
;
1829 TREE_TYPE (init
) = type
;
1830 if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
) == NULL_TREE
)
1831 cp_complete_array_type (&TREE_TYPE (init
), init
, /*do_default=*/0);
1832 if (picflags
& PICFLAG_SIDE_EFFECTS
)
1834 TREE_CONSTANT (init
) = false;
1835 TREE_SIDE_EFFECTS (init
) = true;
1837 else if (picflags
& PICFLAG_NOT_ALL_CONSTANT
)
1839 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1840 TREE_CONSTANT (init
) = false;
1841 TREE_SIDE_EFFECTS (init
) = false;
1845 TREE_CONSTANT (init
) = 1;
1846 TREE_SIDE_EFFECTS (init
) = false;
1847 if (!(picflags
& PICFLAG_NOT_ALL_SIMPLE
))
1848 TREE_STATIC (init
) = 1;
1853 /* Given a structure or union value DATUM, construct and return
1854 the structure or union component which results from narrowing
1855 that value to the base specified in BASETYPE. For example, given the
1858 class L { int ii; };
1859 class A : L { ... };
1860 class B : L { ... };
1861 class C : A, B { ... };
1869 x.A::ii refers to the ii member of the L part of
1870 the A part of the C object named by X. In this case,
1871 DATUM would be x, and BASETYPE would be A.
1873 I used to think that this was nonconformant, that the standard specified
1874 that first we look up ii in A, then convert x to an L& and pull out the
1875 ii part. But in fact, it does say that we convert x to an A&; A here
1876 is known as the "naming class". (jason 2000-12-19)
1878 BINFO_P points to a variable initialized either to NULL_TREE or to the
1879 binfo for the specific base subobject we want to convert to. */
1882 build_scoped_ref (tree datum
, tree basetype
, tree
* binfo_p
)
1886 if (datum
== error_mark_node
)
1887 return error_mark_node
;
1891 binfo
= lookup_base (TREE_TYPE (datum
), basetype
, ba_check
,
1892 NULL
, tf_warning_or_error
);
1894 if (!binfo
|| binfo
== error_mark_node
)
1896 *binfo_p
= NULL_TREE
;
1898 error_not_base_type (basetype
, TREE_TYPE (datum
));
1899 return error_mark_node
;
1903 return build_base_path (PLUS_EXPR
, datum
, binfo
, 1,
1904 tf_warning_or_error
);
1907 /* Build a reference to an object specified by the C++ `->' operator.
1908 Usually this just involves dereferencing the object, but if the
1909 `->' operator is overloaded, then such overloads must be
1910 performed until an object which does not have the `->' operator
1911 overloaded is found. An error is reported when circular pointer
1912 delegation is detected. */
1915 build_x_arrow (location_t loc
, tree expr
, tsubst_flags_t complain
)
1917 tree orig_expr
= expr
;
1918 tree type
= TREE_TYPE (expr
);
1919 tree last_rval
= NULL_TREE
;
1920 vec
<tree
, va_gc
> *types_memoized
= NULL
;
1922 if (type
== error_mark_node
)
1923 return error_mark_node
;
1925 if (processing_template_decl
)
1927 if (type
&& TYPE_PTR_P (type
)
1928 && !dependent_scope_p (TREE_TYPE (type
)))
1929 /* Pointer to current instantiation, don't treat as dependent. */;
1930 else if (type_dependent_expression_p (expr
))
1931 return build_min_nt_loc (loc
, ARROW_EXPR
, expr
);
1932 expr
= build_non_dependent_expr (expr
);
1935 if (MAYBE_CLASS_TYPE_P (type
))
1937 struct tinst_level
*actual_inst
= current_instantiation ();
1940 while ((expr
= build_new_op (loc
, COMPONENT_REF
,
1941 LOOKUP_NORMAL
, expr
, NULL_TREE
, NULL_TREE
,
1944 if (expr
== error_mark_node
)
1945 return error_mark_node
;
1947 /* This provides a better instantiation backtrace in case of
1949 if (fn
&& DECL_USE_TEMPLATE (fn
))
1950 push_tinst_level_loc (fn
,
1951 (current_instantiation () != actual_inst
)
1952 ? DECL_SOURCE_LOCATION (fn
)
1956 if (vec_member (TREE_TYPE (expr
), types_memoized
))
1958 if (complain
& tf_error
)
1959 error ("circular pointer delegation detected");
1960 return error_mark_node
;
1963 vec_safe_push (types_memoized
, TREE_TYPE (expr
));
1967 while (current_instantiation () != actual_inst
)
1970 if (last_rval
== NULL_TREE
)
1972 if (complain
& tf_error
)
1973 error ("base operand of %<->%> has non-pointer type %qT", type
);
1974 return error_mark_node
;
1977 if (TYPE_REF_P (TREE_TYPE (last_rval
)))
1978 last_rval
= convert_from_reference (last_rval
);
1982 last_rval
= decay_conversion (expr
, complain
);
1983 if (last_rval
== error_mark_node
)
1984 return error_mark_node
;
1987 if (TYPE_PTR_P (TREE_TYPE (last_rval
)))
1989 if (processing_template_decl
)
1991 expr
= build_min (ARROW_EXPR
, TREE_TYPE (TREE_TYPE (last_rval
)),
1993 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (last_rval
);
1997 return cp_build_indirect_ref (loc
, last_rval
, RO_ARROW
, complain
);
2000 if (complain
& tf_error
)
2003 error ("result of %<operator->()%> yields non-pointer result");
2005 error ("base operand of %<->%> is not a pointer");
2007 return error_mark_node
;
2010 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
2011 already been checked out to be of aggregate type. */
2014 build_m_component_ref (tree datum
, tree component
, tsubst_flags_t complain
)
2022 datum
= mark_lvalue_use (datum
);
2023 component
= mark_rvalue_use (component
);
2025 if (error_operand_p (datum
) || error_operand_p (component
))
2026 return error_mark_node
;
2028 ptrmem_type
= TREE_TYPE (component
);
2029 if (!TYPE_PTRMEM_P (ptrmem_type
))
2031 if (complain
& tf_error
)
2032 error ("%qE cannot be used as a member pointer, since it is of "
2033 "type %qT", component
, ptrmem_type
);
2034 return error_mark_node
;
2037 objtype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
2038 if (! MAYBE_CLASS_TYPE_P (objtype
))
2040 if (complain
& tf_error
)
2041 error ("cannot apply member pointer %qE to %qE, which is of "
2042 "non-class type %qT", component
, datum
, objtype
);
2043 return error_mark_node
;
2046 type
= TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type
);
2047 ctype
= complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type
));
2049 if (!COMPLETE_TYPE_P (ctype
))
2051 if (!same_type_p (ctype
, objtype
))
2057 binfo
= lookup_base (objtype
, ctype
, ba_check
, NULL
, complain
);
2062 if (complain
& tf_error
)
2063 error ("pointer to member type %qT incompatible with object "
2064 "type %qT", type
, objtype
);
2065 return error_mark_node
;
2067 else if (binfo
== error_mark_node
)
2068 return error_mark_node
;
2071 if (TYPE_PTRDATAMEM_P (ptrmem_type
))
2073 bool is_lval
= real_lvalue_p (datum
);
2076 /* Compute the type of the field, as described in [expr.ref].
2077 There's no such thing as a mutable pointer-to-member, so
2078 things are not as complex as they are for references to
2079 non-static data members. */
2080 type
= cp_build_qualified_type (type
,
2081 (cp_type_quals (type
)
2082 | cp_type_quals (TREE_TYPE (datum
))));
2084 datum
= build_address (datum
);
2086 /* Convert object to the correct base. */
2089 datum
= build_base_path (PLUS_EXPR
, datum
, binfo
, 1, complain
);
2090 if (datum
== error_mark_node
)
2091 return error_mark_node
;
2094 /* Build an expression for "object + offset" where offset is the
2095 value stored in the pointer-to-data-member. */
2096 ptype
= build_pointer_type (type
);
2097 datum
= fold_build_pointer_plus (fold_convert (ptype
, datum
), component
);
2098 datum
= cp_build_fold_indirect_ref (datum
);
2099 if (datum
== error_mark_node
)
2100 return error_mark_node
;
2102 /* If the object expression was an rvalue, return an rvalue. */
2104 datum
= move (datum
);
2109 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
2110 program is ill-formed if the second operand is a pointer to member
2111 function with ref-qualifier & (for C++20: unless its cv-qualifier-seq
2112 is const). In a .* expression whose object expression is an lvalue,
2113 the program is ill-formed if the second operand is a pointer to member
2114 function with ref-qualifier &&. */
2115 if (FUNCTION_REF_QUALIFIED (type
))
2117 bool lval
= lvalue_p (datum
);
2118 if (lval
&& FUNCTION_RVALUE_QUALIFIED (type
))
2120 if (complain
& tf_error
)
2121 error ("pointer-to-member-function type %qT requires an rvalue",
2123 return error_mark_node
;
2125 else if (!lval
&& !FUNCTION_RVALUE_QUALIFIED (type
))
2127 if ((type_memfn_quals (type
)
2128 & (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
))
2131 if (complain
& tf_error
)
2132 error ("pointer-to-member-function type %qT requires "
2133 "an lvalue", ptrmem_type
);
2134 return error_mark_node
;
2136 else if (cxx_dialect
< cxx20
)
2138 if (complain
& tf_warning_or_error
)
2139 pedwarn (input_location
, OPT_Wpedantic
,
2140 "pointer-to-member-function type %qT requires "
2141 "an lvalue before C++20", ptrmem_type
);
2143 return error_mark_node
;
2147 return build2 (OFFSET_REF
, type
, datum
, component
);
2151 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
2154 build_functional_cast_1 (location_t loc
, tree exp
, tree parms
,
2155 tsubst_flags_t complain
)
2157 /* This is either a call to a constructor,
2158 or a C cast in C++'s `functional' notation. */
2160 /* The type to which we are casting. */
2163 if (error_operand_p (exp
) || parms
== error_mark_node
)
2164 return error_mark_node
;
2166 if (TREE_CODE (exp
) == TYPE_DECL
)
2168 type
= TREE_TYPE (exp
);
2170 if (DECL_ARTIFICIAL (exp
))
2171 cp_warn_deprecated_use (type
);
2176 /* We need to check this explicitly, since value-initialization of
2177 arrays is allowed in other situations. */
2178 if (TREE_CODE (type
) == ARRAY_TYPE
)
2180 if (complain
& tf_error
)
2181 error_at (loc
, "functional cast to array type %qT", type
);
2182 return error_mark_node
;
2185 if (tree anode
= type_uses_auto (type
))
2187 if (!CLASS_PLACEHOLDER_TEMPLATE (anode
))
2189 if (complain
& tf_error
)
2190 error_at (loc
, "invalid use of %qT", anode
);
2191 return error_mark_node
;
2195 /* Even if there are no parameters, we might be able to deduce from
2196 default template arguments. Pass TF_NONE so that we don't
2197 generate redundant diagnostics. */
2198 type
= do_auto_deduction (type
, parms
, anode
, tf_none
,
2200 if (type
== error_mark_node
)
2202 if (complain
& tf_error
)
2203 error_at (loc
, "cannot deduce template arguments "
2204 "for %qT from %<()%>", anode
);
2205 return error_mark_node
;
2209 type
= do_auto_deduction (type
, parms
, anode
, complain
,
2213 if (processing_template_decl
)
2217 /* Diagnose this even in a template. We could also try harder
2218 to give all the usual errors when the type and args are
2220 if (TYPE_REF_P (type
) && !parms
)
2222 if (complain
& tf_error
)
2223 error_at (loc
, "invalid value-initialization of reference type");
2224 return error_mark_node
;
2227 t
= build_min (CAST_EXPR
, type
, parms
);
2228 /* We don't know if it will or will not have side effects. */
2229 TREE_SIDE_EFFECTS (t
) = 1;
2233 if (! MAYBE_CLASS_TYPE_P (type
))
2235 if (parms
== NULL_TREE
)
2237 if (VOID_TYPE_P (type
))
2239 return build_value_init (cv_unqualified (type
), complain
);
2242 /* This must build a C cast. */
2243 parms
= build_x_compound_expr_from_list (parms
, ELK_FUNC_CAST
, complain
);
2244 return cp_build_c_cast (loc
, type
, parms
, complain
);
2247 /* Prepare to evaluate as a call to a constructor. If this expression
2248 is actually used, for example,
2250 return X (arg1, arg2, ...);
2252 then the slot being initialized will be filled in. */
2254 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
2255 return error_mark_node
;
2256 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
2257 return error_mark_node
;
2261 If the expression list is a single-expression, the type
2262 conversion is equivalent (in definedness, and if defined in
2263 meaning) to the corresponding cast expression. */
2264 if (parms
&& TREE_CHAIN (parms
) == NULL_TREE
)
2265 return cp_build_c_cast (loc
, type
, TREE_VALUE (parms
), complain
);
2269 The expression T(), where T is a simple-type-specifier for a
2270 non-array complete object type or the (possibly cv-qualified)
2271 void type, creates an rvalue of the specified type, which is
2272 value-initialized. */
2274 if (parms
== NULL_TREE
)
2276 exp
= build_value_init (type
, complain
);
2277 exp
= get_target_expr_sfinae (exp
, complain
);
2281 /* Call the constructor. */
2282 releasing_vec parmvec
;
2283 for (; parms
!= NULL_TREE
; parms
= TREE_CHAIN (parms
))
2284 vec_safe_push (parmvec
, TREE_VALUE (parms
));
2285 exp
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
2286 &parmvec
, type
, LOOKUP_NORMAL
, complain
);
2288 if (exp
== error_mark_node
)
2289 return error_mark_node
;
2291 return build_cplus_new (type
, exp
, complain
);
2295 build_functional_cast (location_t loc
, tree exp
, tree parms
,
2296 tsubst_flags_t complain
)
2298 tree result
= build_functional_cast_1 (loc
, exp
, parms
, complain
);
2299 protected_set_expr_location (result
, loc
);
2304 /* Add new exception specifier SPEC, to the LIST we currently have.
2305 If it's already in LIST then do nothing.
2306 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2307 know what we're doing. */
2310 add_exception_specifier (tree list
, tree spec
, tsubst_flags_t complain
)
2315 diagnostic_t diag_type
= DK_UNSPECIFIED
; /* none */
2317 if (spec
== error_mark_node
)
2320 gcc_assert (spec
&& (!list
|| TREE_VALUE (list
)));
2322 /* [except.spec] 1, type in an exception specifier shall not be
2323 incomplete, or pointer or ref to incomplete other than pointer
2325 is_ptr
= TYPE_PTR_P (core
);
2326 if (is_ptr
|| TYPE_REF_P (core
))
2327 core
= TREE_TYPE (core
);
2330 else if (VOID_TYPE_P (core
))
2332 else if (TREE_CODE (core
) == TEMPLATE_TYPE_PARM
)
2334 else if (processing_template_decl
)
2336 else if (!verify_type_context (input_location
, TCTX_EXCEPTIONS
, core
,
2337 !(complain
& tf_error
)))
2338 return error_mark_node
;
2342 /* 15.4/1 says that types in an exception specifier must be complete,
2343 but it seems more reasonable to only require this on definitions
2344 and calls. So just give a pedwarn at this point; we will give an
2345 error later if we hit one of those two cases. */
2346 if (!COMPLETE_TYPE_P (complete_type (core
)))
2347 diag_type
= DK_PEDWARN
; /* pedwarn */
2354 for (probe
= list
; probe
; probe
= TREE_CHAIN (probe
))
2355 if (same_type_p (TREE_VALUE (probe
), spec
))
2358 list
= tree_cons (NULL_TREE
, spec
, list
);
2361 diag_type
= DK_ERROR
; /* error */
2363 if (diag_type
!= DK_UNSPECIFIED
2364 && (complain
& tf_warning_or_error
))
2365 cxx_incomplete_type_diagnostic (NULL_TREE
, core
, diag_type
);
2370 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2373 nothrow_spec_p_uninst (const_tree spec
)
2375 if (DEFERRED_NOEXCEPT_SPEC_P (spec
))
2377 return nothrow_spec_p (spec
);
2380 /* Combine the two exceptions specifier lists LIST and ADD, and return
2384 merge_exception_specifiers (tree list
, tree add
)
2386 tree noex
, orig_list
;
2388 if (list
== error_mark_node
|| add
== error_mark_node
)
2389 return error_mark_node
;
2391 /* No exception-specifier or noexcept(false) are less strict than
2392 anything else. Prefer the newer variant (LIST). */
2393 if (!list
|| list
== noexcept_false_spec
)
2395 else if (!add
|| add
== noexcept_false_spec
)
2398 /* noexcept(true) and throw() are stricter than anything else.
2399 As above, prefer the more recent one (LIST). */
2400 if (nothrow_spec_p_uninst (add
))
2403 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2404 if (UNEVALUATED_NOEXCEPT_SPEC_P (add
)
2405 && UNEVALUATED_NOEXCEPT_SPEC_P (list
))
2407 /* We should have instantiated other deferred noexcept specs by now. */
2408 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add
));
2410 if (nothrow_spec_p_uninst (list
))
2412 noex
= TREE_PURPOSE (list
);
2413 gcc_checking_assert (!TREE_PURPOSE (add
)
2414 || errorcount
|| !flag_exceptions
2415 || cp_tree_equal (noex
, TREE_PURPOSE (add
)));
2417 /* Combine the dynamic-exception-specifiers, if any. */
2419 for (; add
&& TREE_VALUE (add
); add
= TREE_CHAIN (add
))
2421 tree spec
= TREE_VALUE (add
);
2424 for (probe
= orig_list
; probe
&& TREE_VALUE (probe
);
2425 probe
= TREE_CHAIN (probe
))
2426 if (same_type_p (TREE_VALUE (probe
), spec
))
2430 spec
= build_tree_list (NULL_TREE
, spec
);
2431 TREE_CHAIN (spec
) = list
;
2436 /* Keep the noexcept-specifier at the beginning of the list. */
2437 if (noex
!= TREE_PURPOSE (list
))
2438 list
= tree_cons (noex
, TREE_VALUE (list
), TREE_CHAIN (list
));
2443 /* Subroutine of build_call. Ensure that each of the types in the
2444 exception specification is complete. Technically, 15.4/1 says that
2445 they need to be complete when we see a declaration of the function,
2446 but we should be able to get away with only requiring this when the
2447 function is defined or called. See also add_exception_specifier. */
2450 require_complete_eh_spec_types (tree fntype
, tree decl
)
2453 /* Don't complain about calls to op new. */
2454 if (decl
&& DECL_ARTIFICIAL (decl
))
2456 for (raises
= TYPE_RAISES_EXCEPTIONS (fntype
); raises
;
2457 raises
= TREE_CHAIN (raises
))
2459 tree type
= TREE_VALUE (raises
);
2460 if (type
&& !COMPLETE_TYPE_P (type
))
2464 ("call to function %qD which throws incomplete type %q#T",
2467 error ("call to function which throws incomplete type %q#T",