Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/cp_gen_be.c
Original file line number Diff line number Diff line change
Expand Up @@ -8539,6 +8539,20 @@ which constant is the value.
/* Figure out the kind of aggregate so we can track the type as we
work through constants. */
type = skip_typerefs(type);
if (is_any_reference_type(type)) {
a_type_ptr referenced_type = skip_typerefs(type_pointed_to(type));
if (type_is(referenced_type, tk_array) ||
is_immediate_vector_type(referenced_type) ||
#if C99_IL_EXTENSIONS_SUPPORTED
type_is(referenced_type, tk_complex) ||
#endif /* C99_IL_EXTENSIONS_SUPPORTED */
is_immediate_class_type(referenced_type)) {
/* The aggregate initializes the object bound to the reference, as in
"R{1, 2}" with R being "const int(&)[2]" (or a reference to a
vector, complex, or class type). */
type = referenced_type;
} /* if */
} /* if */
switch (type->kind) {
case tk_array:
#if GNU_VECTOR_TYPES_ALLOWED
Expand Down Expand Up @@ -23545,9 +23559,11 @@ when possible.
/* Constant (simple or aggregate). */
con = dip->variant.constant.ptr;
if (is_reference_type(init_entity_type) &&
!con->explicit_cast_applied &&
!((assoc_expr != NULL && is_glvalue_node(assoc_expr)) ||
is_reference_type(con->type))) {
/* A reference initialized by a prvalue. */
/* A reference initialized by a prvalue (other than by a cast like
"R{1, 2}", which is put out with the reference type). */
a_type_ptr referred_to_type = type_pointed_to(init_entity_type);
if (is_array_type(referred_to_type) ||
(is_class_struct_union_type(referred_to_type) &&
Expand Down Expand Up @@ -24068,7 +24084,12 @@ Output the initializer, if any, for the indicated variable.
} /* if */
} else {
dip = expr->variant.init.dynamic_init;
init_type = expr->type;
/* As in gen_temp_init, use the type of a braced cast as written
(e.g., a reference type, as in "R{1, 2}"). */
init_type = expr->variant.init.source.type;
if (init_type == NULL) {
init_type = expr->type;
} /* if */
goto handle_dynamic_init;
} /* if */
} else if (con->explicit_parentheses_on_aggregate &&
Expand Down
34 changes: 31 additions & 3 deletions src/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -30795,18 +30795,46 @@ previously-scanned braced initializer.
} /* if */
expr = expr_node_from_operand(result);
if (expr != NULL) {
an_expr_node_ptr temp_expr = expr;
/* Record source form information in the associated IL entries if
needed. */
expr->is_brace_notation_cast = TRUE;
if (expr->kind == (an_expr_node_kind)enk_temp_init) {
a_dynamic_init_ptr dip = expr->variant.init.dynamic_init;
if (is_any_reference_type(type_cast_to) && is_array_type(expr->type) &&
node_is_operator(expr, eok_lvalue_adjust)) {
/* A reference bound to an array temporary, as in "R{1, 2}" with R
being "int(&&)[2]", is "*" applied to the decayed temporary (under
the eok_lvalue_adjust). Record the cast on the temporary. */
an_expr_node_ptr opnd = expr->variant.operation.operands;
if (node_is_operator(opnd, eok_indirect)) {
opnd = opnd->variant.operation.operands;
if (node_is_operator(opnd, eok_array_to_pointer)) {
temp_expr = opnd->variant.operation.operands;
} /* if */
} /* if */
} else if (is_any_reference_type(type_cast_to) &&
node_is_operator(expr, eok_lvalue)) {
/* In a template, a reference bound to a temporary of dependent type,
as in "R<T>{x, x}" with R<T> being "const S<T>&", is the temporary
under an eok_lvalue. Record the cast on the temporary. */
temp_expr = expr->variant.operation.operands;
} /* if */
if (node_is(temp_expr, enk_temp_init)) {
a_dynamic_init_ptr dip = temp_expr->variant.init.dynamic_init;
dip->is_explicit_cast = TRUE;
dip->is_braced_initializer = TRUE;
if (dip->kind == (a_dynamic_init_kind)dik_constant ||
dip->kind == (a_dynamic_init_kind)dik_nonconstant_aggregate) {
dip->variant.constant.ptr->explicit_cast_applied = TRUE;
} /* if */
expr->variant.init.source.type = type_cast_to;
temp_expr->variant.init.source.type = type_cast_to;
} /* if */
if (is_rvalue_reference_type(type_cast_to) &&
!is_function_type(type_pointed_to(type_cast_to)) &&
is_expression_operand(result) && is_an_lvalue(result)) {
/* The result of a cast to an rvalue reference to an object type is
an xvalue, but binding the reference produced an lvalue (e.g., for
an array temporary, as in "R{1, 2}" with R being "int(&&)[2]"). */
conv_rvalue_reference_result_to_xvalue(result);
} /* if */
} /* if */
#if EXTRA_SOURCE_POSITIONS_IN_IL
Expand Down
17 changes: 17 additions & 0 deletions src/overload.c
Original file line number Diff line number Diff line change
Expand Up @@ -28173,6 +28173,7 @@ will be an lvalue instead of the usual prvalue.
(CCO_CAST | CCO_FUNC_NOTATION_CAST |
CCO_SUPPRESS_USER_CONVERSIONS_IN_OVL_RES);
a_boolean rmake_lvalue_temp = FALSE;
a_boolean rmake_xvalue_temp = FALSE;
a_type_ptr underlying_type = type_pointed_to(dest_type);
if (!is_class_struct_union_type(underlying_type) &&
is_lvalue_reference_type(dest_type) &&
Expand All @@ -28183,6 +28184,19 @@ will be an lvalue instead of the usual prvalue.
sense and is probably unobservable, and it avoids a problem
in reproducing the source when the cp_gen_be is used. */
rmake_lvalue_temp = TRUE;
} else if (is_rvalue_reference_type(dest_type) &&
list != NULL && next_elem(list) != NULL &&
(is_vector_type(underlying_type)
#if C99_IL_EXTENSIONS_SUPPORTED
|| is_complex_type(underlying_type)
#endif /* C99_IL_EXTENSIONS_SUPPORTED */
)) {
/* Likewise for an rvalue reference to a GNU vector or complex type
initialized from a list of elements, as in "R{1, 2, 3, 4}" with R
being "V&&", except that the temporary is then made an xvalue so
the reference can bind to it directly. */
rmake_lvalue_temp = TRUE;
rmake_xvalue_temp = TRUE;
} /* if */
if (is_direct_init &&
(clang_version_is(any_version) || gnu_version_is(<90000) ||
Expand Down Expand Up @@ -28259,6 +28273,9 @@ will be an lvalue instead of the usual prvalue.
dest_type = make_reference_type_of_same_kind(underlying_type,
dest_type);
} /* if */
if (rmake_xvalue_temp) {
conv_rvalue_reference_result_to_xvalue(&operand);
} /* if */
prep_reference_initializer_operand(&operand, dest_type,
(a_conv_descr *)NULL,
leave_as_object,
Expand Down
47 changes: 47 additions & 0 deletions src/pending_changes/gh-572-array-ref-braced-cast.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[GH #572]
Braced casts to references to aggregates

The C++-generating back end dropped the type of a braced functional-notation
cast to a reference to an array, so the generated code had a different meaning
or was rejected. For such a cast to a reference to a const array or to a
class, and for a braced initializer of such a reference, it issued an internal
error (an assertion failure in gen_initializer_constant, "ran out of fields").
Furthermore, when such a cast to a reference to an array was the constant
initializer of a variable, it was mistakenly rendered as a compound literal.
For example, with --c++17:

struct S { int a, b; };
using RA = int(&&)[2];
using CA = const int(&)[2];
template<class T> int size(T&&) { return sizeof(T); }
int n1 = size(RA{1, 2}); // Previously rendered as "size({1, 2})".
int n2 = size(CA{1, 2}); // Internal error.
const S &r{1, 2}; // Internal error.
CA c = CA{1, 2}; // Previously rendered as "(const int [2]){1, 2}".

These cases are now all fixed.

Similarly, in configurations where ALL_TEMPLATE_INFO_IN_IL is TRUE, the type
of a braced cast to a reference to a class of dependent type was dropped.
For example,

template<class T> struct S2 { T x, y; };
template<class T> using R = const S2<T>&;
template<class T> void f(T x) {
auto &&r = R<T>{x, x}; // Previously rendered as "auto &&r = {x, x};".
}

This case is now correctly rendered as well.

Likewise, in GNU modes, a braced cast to an rvalue reference to a vector or
complex type, as in "R{1, 2, 3}" with R being "V&&" (V being a vector type),
was rendered with an extra pair of braces ("R{{1, 2, 3}}"), which g++ rejects.
It is now rendered as written.

Finally, the front end treated a braced functional-notation cast to an rvalue
reference to an array as an lvalue rather than an xvalue. For example, with
"RA" as above:

int h(const int (&)[2]) = delete;
int h(const int (&&)[2]);
int n3 = h(RA{1, 2}); // Previously selected the deleted function.
1 change: 0 additions & 1 deletion tests/expectations/edg_x86_32_cp/cwg.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
cwg2242/test.mft.cpp:fn:-DTEST_NUMBER=1:CATASTROPHE:
cwg2845b.sft.cpp:cp:-DTEST_NUMBER=1:FAIL:
cwg2941.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:ABORT:
2 changes: 0 additions & 2 deletions tests/expectations/edg_x86_32_cp/imported.log
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,6 @@ gnu/cpp/cpp0x/initlist25/test.mft.cpp:rp:-DTEST_NUMBER=1 --c++11:FAIL(COMPILE):
gnu/cpp/cpp0x/initlist48.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
gnu/cpp/cpp0x/initlist53.sft.cpp:rp:-DTEST_NUMBER=1 --c++11:FAIL(COMPILE):
gnu/cpp/cpp0x/initlist54.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
gnu/cpp/cpp0x/initlist89.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:ABORT:
gnu/cpp/cpp0x/lambda/lambda-eh2.sft.cpp:rp:-DTEST_NUMBER=1 --c++11:FAIL(COMPILE):
gnu/cpp/cpp0x/lambda/lambda-err3.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
gnu/cpp/cpp0x/lambda/lambda-ice16.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
Expand Down Expand Up @@ -859,7 +858,6 @@ gnu/cpp/cpp23/attr-assume11.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
gnu/cpp/cpp23/explicit-obj-default1.sft.cpp:rp:-DTEST_NUMBER=1 --c++23:FAIL(COMPILE):
gnu/cpp/cpp23/ext-floating14.sft.cpp:rp:-DTEST_NUMBER=1 --c++23:FAIL(COMPILE):
gnu/cpp/cpp23/size_t-literals.sft.cpp:fp:-DTEST_NUMBER=1 --c++23:FAIL:
gnu/cpp/cpp2a/array-conv12.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:ABORT:
gnu/cpp/cpp2a/concepts-alias6.sft.cpp:fn:-DTEST_NUMBER=1 --c++20:PASS:
gnu/cpp/cpp2a/concepts-cmath.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
gnu/cpp/cpp2a/concepts-explicit-inst3.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
Expand Down
1 change: 0 additions & 1 deletion tests/expectations/edg_x86_64_cp/cwg.log
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
cwg2242/test.mft.cpp:fn:-DTEST_NUMBER=1:CATASTROPHE:
cwg2845b.sft.cpp:cp:-DTEST_NUMBER=1:FAIL:
cwg2941.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:ABORT:
2 changes: 0 additions & 2 deletions tests/expectations/edg_x86_64_cp/imported.log
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ gnu/cpp/cpp0x/gen-attrs-5.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
gnu/cpp/cpp0x/gen-attrs-74.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
gnu/cpp/cpp0x/initlist-value4.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
gnu/cpp/cpp0x/initlist123.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
gnu/cpp/cpp0x/initlist89.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:ABORT:
gnu/cpp/cpp0x/lambda/lambda-err3.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
gnu/cpp/cpp0x/lambda/lambda-ice16.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
gnu/cpp/cpp0x/lambda/lambda-ice27.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
Expand Down Expand Up @@ -129,7 +128,6 @@ gnu/cpp/cpp1z/class-deduction70.sft.cpp:fn:-DTEST_NUMBER=1:PASS:
gnu/cpp/cpp1z/constexpr-if15.sft.cpp:fn:-DTEST_NUMBER=1 --c++17:PASS:
gnu/cpp/cpp1z/fold6.sft.cpp:fn:-DTEST_NUMBER=1 --c++17:PASS:
gnu/cpp/cpp23/attr-assume11.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
gnu/cpp/cpp2a/array-conv12.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:ABORT:
gnu/cpp/cpp2a/concepts-alias6.sft.cpp:fn:-DTEST_NUMBER=1 --c++20:PASS:
gnu/cpp/cpp2a/concepts-explicit-inst3.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
gnu/cpp/cpp2a/concepts-lambda15.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
Expand Down
1 change: 1 addition & 0 deletions tests/tests/changes/.GH572.rto/default.1.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=1 --c++11 Test_name.c
1 change: 1 addition & 0 deletions tests/tests/changes/.GH572.rto/default.2.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=2 --c++17 Test_name.c
1 change: 1 addition & 0 deletions tests/tests/changes/.GH572_a.rto/default.1.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=1 --c++17 --gnu_version 150200 Test_name.c
82 changes: 82 additions & 0 deletions tests/tests/changes/GH572.sft.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//type:fp
//options:--c++11:--c++17
//remark:Braced casts to references to aggregates
//
// In generated C++, a braced functional-notation cast to a reference to an
// array, like R{1, 2} with "using R = int(&&)[2]", lost its type (becoming
// just "{1, 2}"), and one to a reference to a const array or to a class
// caused an internal error, as did a braced initializer for such a
// reference. Such a cast to a reference to an array that was the constant
// initializer of a variable became a compound literal (a GNU extension).
// Also, a braced cast to an rvalue reference to an array was an lvalue
// rather than an xvalue. In configurations where ALL_TEMPLATE_INFO_IN_IL
// is TRUE, a braced cast to a reference to a class of dependent type in a
// template also lost its type.

struct S { int a, b; };
using RA = int(&&)[2];
using CA = const int(&)[2];
using CRA = const int(&&)[2];
using RS = S&&;
using CS = const S&;
using RSA = S(&&)[2];
template<class T> constexpr int size(T&&) { return sizeof(T); }
template<class T, class U> struct same { static const bool value = false; };
template<class T> struct same<T, T> { static const bool value = true; };
int i, j;

static_assert(size(RA{1, 2}) == 2 * sizeof(int), "");
static_assert(size(CA{1, 2}) == 2 * sizeof(int), "");
static_assert(size(CRA{1, 2}) == 2 * sizeof(int), "");
static_assert(size(RS{1, 2}) == sizeof(S), "");
static_assert(size(CS{1, 2}) == sizeof(S), "");
static_assert(size(RSA{{1, 2}, {3, 4}}) == 2 * sizeof(S), "");
static_assert(sizeof(decltype((RA{1, 2}))) == 2 * sizeof(int), "");
static_assert(same<decltype((RA{1, 2})), RA>::value, "");
static_assert(same<decltype((RA{i, j})), RA>::value, "");
static_assert(same<decltype((CA{1, 2})), CA>::value, "");
static_assert(same<decltype((CRA{1, 2})), CRA>::value, "");
static_assert(same<decltype((RS{1, 2})), RS>::value, "");
static_assert(same<decltype((RSA{{1, 2}, {3, 4}})), RSA>::value, "");
constexpr int h(const int (&)[2]) { return 1; }
constexpr int h(const int (&&)[2]) { return 2; }
static_assert(h(RA{1, 2}) == 2 && h(CA{1, 2}) == 1, "");
template<class T> constexpr bool is_lvalue(T&&) { return same<T, T&>::value; }
static_assert(!is_lvalue(RA{1, 2}) && is_lvalue(CA{1, 2}), "");
template<class T> constexpr bool cast_is_lvalue() {
return is_lvalue(T{1, 2});
}
static_assert(!cast_is_lvalue<RA>() && cast_is_lvalue<CA>(), "");

const int (&r1)[2]{1, 2};
const S &r2{1, 2};
auto &&r3 = RA{1, 2};
CA r4 = CA{1, 2};
const S &r5 = RS{1, 2};
#if __cplusplus >= 201402L
decltype(auto) r6 = RA{1, 2};
decltype(auto) r7 = CA{1, 2};
decltype(auto) r8 = RS{1, 2};
static_assert(same<decltype(r6), RA>::value, "");
static_assert(same<decltype(r7), CA>::value, "");
static_assert(same<decltype(r8), RS>::value, "");
#endif
void g(const int (&)[2]);
void f() {
auto &&a = RA{1, 2};
auto &&b = CA{i, j};
int n = size(RA{i, j}) + size(CS{i, j}) + size(RA{});
g(CA{1, 2});
g(RA{i, j});
(void)a; (void)b; (void)n;
}

template<class T> struct ST { T x, y; };
template<class T> using RT = const ST<T>&;
template<class T> using RRT = ST<T>&&;
template<class T> int ft(T x) {
auto &&r = RT<T>{x, x};
const ST<T> &s = RRT<T>{x, x};
return r.x + s.y + RT<T>{x, x}.x + size(RT<T>{x, x});
}
int nt = ft(1);
54 changes: 54 additions & 0 deletions tests/tests/changes/GH572_a.sft.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//type:fp
//options:--c++17 --gnu_version 150200
//remark:Braced casts to references to GNU vector and complex types
//
// In generated C++, a braced functional-notation cast to a reference to
// const of a GNU vector or complex type, like R{1, 2, 3, 4} with
// "using R = const V&", and a braced initializer for such a reference, caused
// an internal error. One to an rvalue reference to such a type, like
// RR{1, 2, 3, 4} with "using RR = V&&", got an extra pair of braces
// ("RR{{1, 2, 3, 4}}"), which g++ rejects.

using V = int __attribute__((vector_size(16)));
using RV = const V&;
using RRV = V&&;
using C = _Complex double;
using RC = const C&;
using RRC = C&&;
template<class T> int size(T&&) { return sizeof(T); }
template<class T, class U> struct same { static const bool value = false; };
template<class T> struct same<T, T> { static const bool value = true; };
template<class T> constexpr bool is_lvalue(T&&) { return same<T, T&>::value; }
int i;

RV v1 = RV{1, 2, 3, 4};
const V &v2{1, 2, 3, 4};
auto &&v3 = RV{1, 2, 3, 4};
auto &&v4 = RRV{1, 2, 3, 4};
V &&v5{1, 2, 3, 4};
RC c1 = RC{1.0, 2.0};
const C &c2{1.0, 2.0};
auto &&c3 = RRC{1.0, 2.0};
static_assert(same<decltype((RV{1, 2, 3, 4})), RV>::value, "");
static_assert(same<decltype((RRV{1, 2, 3, 4})), RRV>::value, "");
static_assert(same<decltype((RRC{1.0, 2.0})), RRC>::value, "");
static_assert(is_lvalue(RV{1, 2, 3, 4}) && !is_lvalue(RRV{1, 2, 3, 4}), "");
int f() {
auto &&a = RV{i, 2, 3, 4};
const V &b{1, i, 3, 4};
RC c = RC{1.0, 2.0};
auto &&d = RRV{1, 2, i, 4};
V &&e{1, 2, 3, i};
RRC g = RRC{1.0, 2.0};
return a[0] + b[1] + (int)__real__ c + d[2] + e[3] + (int)__imag__ g +
size(RV{1, 2, 3, i}) + size(RRV{i, 2, 3, 4});
}
template<class T> int g(T x) {
using TV = T __attribute__((vector_size(16)));
using TR = const TV&;
using TRR = TV&&;
auto &&a = TR{x, x, x, x};
auto &&b = TRR{x, x, x, x};
return a[0] + b[1];
}
int n = g(1);
8 changes: 0 additions & 8 deletions tests/tests/cwg/.cwg2941.rto/edg_x86_32_cp.1.1.txt

This file was deleted.

8 changes: 0 additions & 8 deletions tests/tests/cwg/.cwg2941.rto/edg_x86_64_cp.1.1.txt

This file was deleted.

This file was deleted.

Loading
Loading