zero-initialization does not simply mean filling the storage with
zero bytes. FIELD_SIZE, if non-NULL, is the bit size of the field,
subfields with bit positions at or above that bit size shouldn't
- be added. */
+ be added. Note that this only works when the result is assigned
+ to a base COMPONENT_REF; if we only have a pointer to the base subobject,
+ expand_assignment will end up clearing the full size of TYPE. */
static tree
build_zero_init_1 (tree type, tree nelts, bool static_storage_p,
init = convert (type, nullptr_node);
else if (SCALAR_TYPE_P (type))
init = convert (type, integer_zero_node);
- else if (CLASS_TYPE_P (type))
+ else if (RECORD_OR_UNION_CODE_P (TREE_CODE (type)))
{
tree field;
VEC(constructor_elt,gc) *v = NULL;
have an upper bound of -1. */
if (!tree_int_cst_equal (max_index, integer_minus_one_node))
{
- constructor_elt *ce;
-
- v = VEC_alloc (constructor_elt, gc, 1);
- ce = VEC_quick_push (constructor_elt, v, NULL);
+ constructor_elt ce;
/* If this is a one element array, we just use a regular init. */
if (tree_int_cst_equal (size_zero_node, max_index))
- ce->index = size_zero_node;
+ ce.index = size_zero_node;
else
- ce->index = build2 (RANGE_EXPR, sizetype, size_zero_node,
- max_index);
+ ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node,
+ max_index);
- ce->value = build_zero_init_1 (TREE_TYPE (type),
- /*nelts=*/NULL_TREE,
- static_storage_p, NULL_TREE);
+ ce.value = build_zero_init_1 (TREE_TYPE (type),
+ /*nelts=*/NULL_TREE,
+ static_storage_p, NULL_TREE);
+ if (ce.value)
+ {
+ v = VEC_alloc (constructor_elt, gc, 1);
+ *VEC_quick_push (constructor_elt, v, NULL) = ce;
+ }
}
/* Build a constructor to contain the initializations. */
constructor. */
/* The AGGR_INIT_EXPR tweaking below breaks in templates. */
- gcc_assert (!processing_template_decl || SCALAR_TYPE_P (type));
+ gcc_assert (!processing_template_decl
+ || (SCALAR_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE));
if (CLASS_TYPE_P (type))
{
tree ctor = build_special_member_call
(NULL_TREE, complete_ctor_identifier,
NULL, type, LOOKUP_NORMAL, complain);
+ ctor = build_aggr_init_expr (type, ctor, complain);
if (ctor != error_mark_node)
- {
- ctor = build_aggr_init_expr (type, ctor, complain);
- AGGR_INIT_ZERO_FIRST (ctor) = 1;
- }
+ AGGR_INIT_ZERO_FIRST (ctor) = 1;
return ctor;
}
}
tree
build_value_init_noctor (tree type, tsubst_flags_t complain)
{
+ if (!COMPLETE_TYPE_P (type))
+ {
+ if (complain & tf_error)
+ error ("value-initialization of incomplete type %qT", type);
+ return error_mark_node;
+ }
/* FIXME the class and array cases should just use digest_init once it is
SFINAE-enabled. */
if (CLASS_TYPE_P (type))
have an upper bound of -1. */
if (!tree_int_cst_equal (max_index, integer_minus_one_node))
{
- constructor_elt *ce;
-
- v = VEC_alloc (constructor_elt, gc, 1);
- ce = VEC_quick_push (constructor_elt, v, NULL);
+ constructor_elt ce;
/* If this is a one element array, we just use a regular init. */
if (tree_int_cst_equal (size_zero_node, max_index))
- ce->index = size_zero_node;
+ ce.index = size_zero_node;
else
- ce->index = build2 (RANGE_EXPR, sizetype, size_zero_node,
- max_index);
+ ce.index = build2 (RANGE_EXPR, sizetype, size_zero_node,
+ max_index);
- ce->value = build_value_init (TREE_TYPE (type), complain);
+ ce.value = build_value_init (TREE_TYPE (type), complain);
- if (ce->value == error_mark_node)
- return error_mark_node;
+ if (ce.value)
+ {
+ if (ce.value == error_mark_node)
+ return error_mark_node;
+
+ v = VEC_alloc (constructor_elt, gc, 1);
+ *VEC_quick_push (constructor_elt, v, NULL) = ce;
- /* We shouldn't have gotten here for anything that would need
- non-trivial initialization, and gimplify_init_ctor_preeval
- would need to be fixed to allow it. */
- gcc_assert (TREE_CODE (ce->value) != TARGET_EXPR
- && TREE_CODE (ce->value) != AGGR_INIT_EXPR);
+ /* We shouldn't have gotten here for anything that would need
+ non-trivial initialization, and gimplify_init_ctor_preeval
+ would need to be fixed to allow it. */
+ gcc_assert (TREE_CODE (ce.value) != TARGET_EXPR
+ && TREE_CODE (ce.value) != AGGR_INIT_EXPR);
+ }
}
/* Build a constructor to contain the initializations. */
return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
}
+/* Initialize current class with INIT, a TREE_LIST of
+ arguments for a target constructor. If TREE_LIST is void_type_node,
+ an empty initializer list was given. */
+
+static void
+perform_target_ctor (tree init)
+{
+ tree decl = current_class_ref;
+ tree type = current_class_type;
+
+ finish_expr_stmt (build_aggr_init (decl, init, LOOKUP_NORMAL,
+ tf_warning_or_error));
+ if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
+ {
+ tree expr = build_delete (type, decl, sfk_complete_destructor,
+ LOOKUP_NORMAL
+ |LOOKUP_NONVIRTUAL
+ |LOOKUP_DESTRUCTOR,
+ 0, tf_warning_or_error);
+ if (expr != error_mark_node)
+ finish_eh_cleanup (expr);
+ }
+}
+
/* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
arguments. If TREE_LIST is void_type_node, an empty initializer
list was given; if NULL_TREE no initializer was given. */
}
}
+ if (init == error_mark_node)
+ return;
+
/* Effective C++ rule 12 requires that all data members be
initialized. */
if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE)
if (!COMPLETE_TYPE_P (current_class_type))
return;
+ if (mem_inits
+ && TYPE_P (TREE_PURPOSE (mem_inits))
+ && same_type_p (TREE_PURPOSE (mem_inits), current_class_type))
+ {
+ /* Delegating constructor. */
+ gcc_assert (TREE_CHAIN (mem_inits) == NULL_TREE);
+ perform_target_ctor (TREE_VALUE (mem_inits));
+ return;
+ }
+
if (DECL_DEFAULTED_FN (current_function_decl))
flags |= LOOKUP_DEFAULTED;
tree virtual_binfo;
int i;
- if (current_template_parms)
- return basetype;
+ if (same_type_p (basetype, current_class_type)
+ || current_template_parms)
+ return basetype;
class_binfo = TYPE_BINFO (current_class_type);
direct_binfo = NULL_TREE;
else
parms = make_tree_vector_single (init);
- if (true_exp == exp)
- ctor_name = complete_ctor_identifier;
- else
- ctor_name = base_ctor_identifier;
+ if (exp == current_class_ref && current_function_decl
+ && DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
+ {
+ /* Delegating constructor. */
+ tree complete;
+ tree base;
+ tree elt; unsigned i;
+
+ /* Unshare the arguments for the second call. */
+ VEC(tree,gc) *parms2 = make_tree_vector ();
+ FOR_EACH_VEC_ELT (tree, parms, i, elt)
+ {
+ elt = break_out_target_exprs (elt);
+ VEC_safe_push (tree, gc, parms2, elt);
+ }
+ complete = build_special_member_call (exp, complete_ctor_identifier,
+ &parms2, binfo, flags,
+ complain);
+ complete = fold_build_cleanup_point_expr (void_type_node, complete);
+ release_tree_vector (parms2);
- rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
- complain);
+ base = build_special_member_call (exp, base_ctor_identifier,
+ &parms, binfo, flags,
+ complain);
+ base = fold_build_cleanup_point_expr (void_type_node, base);
+ rval = build3 (COND_EXPR, void_type_node,
+ build2 (EQ_EXPR, boolean_type_node,
+ current_in_charge_parm, integer_zero_node),
+ base,
+ complete);
+ }
+ else
+ {
+ if (true_exp == exp)
+ ctor_name = complete_ctor_identifier;
+ else
+ ctor_name = base_ctor_identifier;
+ rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
+ complain);
+ }
if (parms != NULL)
release_tree_vector (parms);
that's value-initialization. */
if (init == void_type_node)
{
- /* If no user-provided ctor, we need to zero out the object. */
- if (!type_has_user_provided_constructor (type))
+ /* If the type has data but no user-provided ctor, we need to zero
+ out the object. */
+ if (!type_has_user_provided_constructor (type)
+ && !is_really_empty_class (type))
{
tree field_size = NULL_TREE;
if (exp != true_exp && CLASSTYPE_AS_BASE (type) != type)
if (type == error_mark_node)
return error_mark_node;
- if (nelts == NULL_TREE && VEC_length (tree, *init) == 1)
+ if (nelts == NULL_TREE && VEC_length (tree, *init) == 1
+ /* Don't do auto deduction where it might affect mangling. */
+ && (!processing_template_decl || at_function_scope_p ()))
{
tree auto_node = type_uses_auto (type);
if (auto_node)
orig_placement = make_tree_vector_copy (*placement);
orig_nelts = nelts;
- orig_init = make_tree_vector_copy (*init);
+ if (*init)
+ orig_init = make_tree_vector_copy (*init);
make_args_non_dependent (*placement);
if (nelts)
if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
maxindex = array_type_nelts (atype);
- if (maxindex == NULL_TREE || maxindex == error_mark_node
- || integer_all_onesp (maxindex))
+ if (maxindex == NULL_TREE || maxindex == error_mark_node)
return error_mark_node;
if (explicit_value_init_p)
else
{
if (do_static_init)
- CONSTRUCTOR_APPEND_ELT (new_vec, field,
- build_zero_init (TREE_TYPE (e),
- NULL_TREE, true));
+ {
+ tree value = build_zero_init (TREE_TYPE (e), NULL_TREE,
+ true);
+ if (value)
+ CONSTRUCTOR_APPEND_ELT (new_vec, field, value);
+ }
saw_non_const = true;
}
}
if (TREE_CODE (type) == ARRAY_TYPE)
m = cp_build_binary_op (input_location,
MULT_EXPR, m,
- array_type_nelts_total (type),
+ /* Force signed arithmetic. */
+ convert (TREE_TYPE (m),
+ array_type_nelts_total (type)),
complain);
finish_cleanup_try_block (try_block);