diff options
author | Jason Merrill <jason@redhat.com> | 2022-12-06 18:10:48 -0500 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2022-12-08 13:43:25 -0500 |
commit | bd0485f20f4794f9787237706a6308473a8e9415 (patch) | |
tree | fce5865dde25c38a2f1d403cf3ba3154b3d0e5f1 /gcc/cp | |
parent | d081807d8d70e3e87eae41e1560e54d503f4d465 (diff) | |
download | gcc-bd0485f20f4794f9787237706a6308473a8e9415.zip gcc-bd0485f20f4794f9787237706a6308473a8e9415.tar.gz gcc-bd0485f20f4794f9787237706a6308473a8e9415.tar.bz2 |
c++: build initializer_list<string> in a loop [PR105838]
The previous patch avoided building an initializer_list<string> at all when
building a vector<string>, but in situations where that isn't possible, we
could still build the initializer_list with a loop over a constant array.
This is represented using a VEC_INIT_EXPR, which required adjusting a couple
of places that expected the initializer array to have the same type as the
target array and fixing build_vec_init not to undo our efforts.
PR c++/105838
gcc/cp/ChangeLog:
* call.cc (convert_like_internal) [ck_list]: Use
maybe_init_list_as_array.
* constexpr.cc (cxx_eval_vec_init_1): Init might have
a different type.
* tree.cc (build_vec_init_elt): Likewise.
* init.cc (build_vec_init): Handle from_array from a
TARGET_EXPR. Retain TARGET_EXPR of a different type.
gcc/testsuite/ChangeLog:
* g++.dg/tree-ssa/initlist-opt2.C: New test.
Diffstat (limited to 'gcc/cp')
-rw-r--r-- | gcc/cp/call.cc | 11 | ||||
-rw-r--r-- | gcc/cp/constexpr.cc | 6 | ||||
-rw-r--r-- | gcc/cp/init.cc | 13 | ||||
-rw-r--r-- | gcc/cp/tree.cc | 2 |
4 files changed, 24 insertions, 8 deletions
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 33b5e7f8..14aa96d 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -8501,7 +8501,16 @@ convert_like_internal (conversion *convs, tree expr, tree fn, int argnum, unsigned len = CONSTRUCTOR_NELTS (expr); tree array; - if (len) + if (tree init = maybe_init_list_as_array (elttype, expr)) + { + elttype = cp_build_qualified_type + (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST); + array = build_array_of_n_type (elttype, len); + array = build_vec_init_expr (array, init, complain); + array = get_target_expr (array); + array = cp_build_addr_expr (array, complain); + } + else if (len) { tree val; unsigned ix; diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index e43d928..3f7892a 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -5255,12 +5255,12 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, else { /* Copying an element. */ - gcc_assert (same_type_ignoring_top_level_qualifiers_p - (atype, TREE_TYPE (init))); eltinit = cp_build_array_ref (input_location, init, idx, complain); if (!lvalue_p (init)) eltinit = move (eltinit); - eltinit = force_rvalue (eltinit, complain); + eltinit = (perform_implicit_conversion_flags + (elttype, eltinit, complain, + LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING)); eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval, non_constant_p, overflow_p); } diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index 428fac5..1ccde7c 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -4420,7 +4420,9 @@ build_vec_init (tree base, tree maxindex, tree init, /* Look through the TARGET_EXPR around a compound literal. */ if (init && TREE_CODE (init) == TARGET_EXPR && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR - && from_array != 2) + && from_array != 2 + && (same_type_ignoring_top_level_qualifiers_p + (TREE_TYPE (init), atype))) init = TARGET_EXPR_INITIAL (init); if (tree vi = get_vec_init_expr (init)) @@ -4546,7 +4548,14 @@ build_vec_init (tree base, tree maxindex, tree init, { if (lvalue_kind (init) & clk_rvalueref) xvalue = true; - base2 = decay_conversion (init, complain); + if (TREE_CODE (init) == TARGET_EXPR) + { + /* Avoid error in decay_conversion. */ + base2 = decay_conversion (TARGET_EXPR_SLOT (init), complain); + base2 = cp_build_compound_expr (init, base2, tf_none); + } + else + base2 = decay_conversion (init, complain); if (base2 == error_mark_node) return error_mark_node; itype = TREE_TYPE (base2); diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index 33bde16..a600178 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -785,8 +785,6 @@ build_vec_init_elt (tree type, tree init, tsubst_flags_t complain) releasing_vec argvec; if (init && !BRACE_ENCLOSED_INITIALIZER_P (init)) { - gcc_assert (same_type_ignoring_top_level_qualifiers_p - (type, TREE_TYPE (init))); tree init_type = strip_array_types (TREE_TYPE (init)); tree dummy = build_dummy_object (init_type); if (!lvalue_p (init)) |