Skip to content

Commit f3310da

Browse files
Generate braced casts to references to aggregates as written in generated C++ [GH #572]
1 parent 123522f commit f3310da

16 files changed

Lines changed: 90 additions & 58 deletions

File tree

‎src/cp_gen_be.c‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8539,6 +8539,15 @@ which constant is the value.
85398539
/* Figure out the kind of aggregate so we can track the type as we
85408540
work through constants. */
85418541
type = skip_typerefs(type);
8542+
if (type_is(type, tk_pointer) && type->variant.pointer.is_reference) {
8543+
a_type_ptr referenced_type = skip_typerefs(type_pointed_to(type));
8544+
if (type_is(referenced_type, tk_array) ||
8545+
is_immediate_class_type(referenced_type)) {
8546+
/* The aggregate initializes the object bound to the reference, as in
8547+
"R{1, 2}" with R being "const int(&)[2]". */
8548+
type = referenced_type;
8549+
} /* if */
8550+
} /* if */
85428551
switch (type->kind) {
85438552
case tk_array:
85448553
#if GNU_VECTOR_TYPES_ALLOWED
@@ -23545,9 +23554,11 @@ when possible.
2354523554
/* Constant (simple or aggregate). */
2354623555
con = dip->variant.constant.ptr;
2354723556
if (is_reference_type(init_entity_type) &&
23557+
!con->explicit_cast_applied &&
2354823558
!((assoc_expr != NULL && is_glvalue_node(assoc_expr)) ||
2354923559
is_reference_type(con->type))) {
23550-
/* A reference initialized by a prvalue. */
23560+
/* A reference initialized by a prvalue (other than by a cast like
23561+
"R{1, 2}", which is put out with the reference type). */
2355123562
a_type_ptr referred_to_type = type_pointed_to(init_entity_type);
2355223563
if (is_array_type(referred_to_type) ||
2355323564
(is_class_struct_union_type(referred_to_type) &&

‎src/expr.c‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30795,18 +30795,34 @@ previously-scanned braced initializer.
3079530795
} /* if */
3079630796
expr = expr_node_from_operand(result);
3079730797
if (expr != NULL) {
30798+
an_expr_node_ptr temp_expr = expr;
3079830799
/* Record source form information in the associated IL entries if
3079930800
needed. */
3080030801
expr->is_brace_notation_cast = TRUE;
30801-
if (expr->kind == (an_expr_node_kind)enk_temp_init) {
30802-
a_dynamic_init_ptr dip = expr->variant.init.dynamic_init;
30802+
if (is_any_reference_type(type_cast_to) && is_array_type(expr->type) &&
30803+
is_operation_node(expr) &&
30804+
node_operator_is(expr, eok_lvalue_adjust)) {
30805+
/* A reference bound to an array temporary, as in "R{1, 2}" with R
30806+
being "int(&&)[2]", is "*" applied to the decayed temporary (under
30807+
the eok_lvalue_adjust). Record the cast on the temporary. */
30808+
an_expr_node_ptr opnd = expr->variant.operation.operands;
30809+
if (is_operation_node(opnd) && node_operator_is(opnd, eok_indirect)) {
30810+
opnd = opnd->variant.operation.operands;
30811+
if (is_operation_node(opnd) &&
30812+
node_operator_is(opnd, eok_array_to_pointer)) {
30813+
temp_expr = opnd->variant.operation.operands;
30814+
} /* if */
30815+
} /* if */
30816+
} /* if */
30817+
if (node_is(temp_expr, enk_temp_init)) {
30818+
a_dynamic_init_ptr dip = temp_expr->variant.init.dynamic_init;
3080330819
dip->is_explicit_cast = TRUE;
3080430820
dip->is_braced_initializer = TRUE;
3080530821
if (dip->kind == (a_dynamic_init_kind)dik_constant ||
3080630822
dip->kind == (a_dynamic_init_kind)dik_nonconstant_aggregate) {
3080730823
dip->variant.constant.ptr->explicit_cast_applied = TRUE;
3080830824
} /* if */
30809-
expr->variant.init.source.type = type_cast_to;
30825+
temp_expr->variant.init.source.type = type_cast_to;
3081030826
} /* if */
3081130827
} /* if */
3081230828
#if EXTRA_SOURCE_POSITIONS_IN_IL
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[GH #572]
2+
C++-generating back end: braced casts to references to aggregates
3+
4+
The C++-generating back end dropped the type of a braced functional-notation
5+
cast to a reference to an array, so the generated code had a different
6+
meaning or was rejected, and it issued an internal error for such a cast to a
7+
reference to a const array or to a class, and for a braced initializer of
8+
such a reference. For example, with --c++17:
9+
10+
struct S { int a, b; };
11+
using RA = int(&&)[2];
12+
using CA = const int(&)[2];
13+
template<class T> int size(T&&) { return sizeof(T); }
14+
int n1 = size(RA{1, 2}); // Was generated as "size({1, 2})".
15+
int n2 = size(CA{1, 2}); // Internal error.
16+
const S &r{1, 2}; // Internal error.
17+
18+
Such casts and initializers are now generated as written.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
cwg2242/test.mft.cpp:fn:-DTEST_NUMBER=1:CATASTROPHE:
22
cwg2845b.sft.cpp:cp:-DTEST_NUMBER=1:FAIL:
3-
cwg2941.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:ABORT:

‎tests/expectations/edg_x86_32_cp/imported.log‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,6 @@ gnu/cpp/cpp0x/initlist25/test.mft.cpp:rp:-DTEST_NUMBER=1 --c++11:FAIL(COMPILE):
780780
gnu/cpp/cpp0x/initlist48.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
781781
gnu/cpp/cpp0x/initlist53.sft.cpp:rp:-DTEST_NUMBER=1 --c++11:FAIL(COMPILE):
782782
gnu/cpp/cpp0x/initlist54.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
783-
gnu/cpp/cpp0x/initlist89.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:ABORT:
784783
gnu/cpp/cpp0x/lambda/lambda-eh2.sft.cpp:rp:-DTEST_NUMBER=1 --c++11:FAIL(COMPILE):
785784
gnu/cpp/cpp0x/lambda/lambda-err3.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
786785
gnu/cpp/cpp0x/lambda/lambda-ice16.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
@@ -859,7 +858,6 @@ gnu/cpp/cpp23/attr-assume11.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
859858
gnu/cpp/cpp23/explicit-obj-default1.sft.cpp:rp:-DTEST_NUMBER=1 --c++23:FAIL(COMPILE):
860859
gnu/cpp/cpp23/ext-floating14.sft.cpp:rp:-DTEST_NUMBER=1 --c++23:FAIL(COMPILE):
861860
gnu/cpp/cpp23/size_t-literals.sft.cpp:fp:-DTEST_NUMBER=1 --c++23:FAIL:
862-
gnu/cpp/cpp2a/array-conv12.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:ABORT:
863861
gnu/cpp/cpp2a/concepts-alias6.sft.cpp:fn:-DTEST_NUMBER=1 --c++20:PASS:
864862
gnu/cpp/cpp2a/concepts-cmath.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
865863
gnu/cpp/cpp2a/concepts-explicit-inst3.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
cwg2242/test.mft.cpp:fn:-DTEST_NUMBER=1:CATASTROPHE:
22
cwg2845b.sft.cpp:cp:-DTEST_NUMBER=1:FAIL:
3-
cwg2941.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:ABORT:

‎tests/expectations/edg_x86_64_cp/imported.log‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ gnu/cpp/cpp0x/gen-attrs-5.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
101101
gnu/cpp/cpp0x/gen-attrs-74.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
102102
gnu/cpp/cpp0x/initlist-value4.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
103103
gnu/cpp/cpp0x/initlist123.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
104-
gnu/cpp/cpp0x/initlist89.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:ABORT:
105104
gnu/cpp/cpp0x/lambda/lambda-err3.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
106105
gnu/cpp/cpp0x/lambda/lambda-ice16.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
107106
gnu/cpp/cpp0x/lambda/lambda-ice27.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
@@ -129,7 +128,6 @@ gnu/cpp/cpp1z/class-deduction70.sft.cpp:fn:-DTEST_NUMBER=1:PASS:
129128
gnu/cpp/cpp1z/constexpr-if15.sft.cpp:fn:-DTEST_NUMBER=1 --c++17:PASS:
130129
gnu/cpp/cpp1z/fold6.sft.cpp:fn:-DTEST_NUMBER=1 --c++17:PASS:
131130
gnu/cpp/cpp23/attr-assume11.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS:
132-
gnu/cpp/cpp2a/array-conv12.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:ABORT:
133131
gnu/cpp/cpp2a/concepts-alias6.sft.cpp:fn:-DTEST_NUMBER=1 --c++20:PASS:
134132
gnu/cpp/cpp2a/concepts-explicit-inst3.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
135133
gnu/cpp/cpp2a/concepts-lambda15.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=1 --c++11 Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=2 --c++17 Test_name.c

‎tests/tests/changes/GH572.sft.cpp‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//type:fp
2+
//options:--c++11:--c++17
3+
//remark:Braced casts to references to aggregates in generated C++
4+
//
5+
// In generated C++, a braced functional-notation cast to a reference to an
6+
// array, like R{1, 2} with "using R = int(&&)[2]", lost its type (becoming
7+
// just "{1, 2}"), and one to a reference to a const array or to a class
8+
// caused an internal error, as did a braced initializer for such a
9+
// reference.
10+
11+
struct S { int a, b; };
12+
using RA = int(&&)[2];
13+
using CA = const int(&)[2];
14+
using CRA = const int(&&)[2];
15+
using RS = S&&;
16+
using CS = const S&;
17+
using RSA = S(&&)[2];
18+
template<class T> constexpr int size(T&&) { return sizeof(T); }
19+
int i, j;
20+
21+
static_assert(size(RA{1, 2}) == 2 * sizeof(int), "");
22+
static_assert(size(CA{1, 2}) == 2 * sizeof(int), "");
23+
static_assert(size(CRA{1, 2}) == 2 * sizeof(int), "");
24+
static_assert(size(RS{1, 2}) == sizeof(S), "");
25+
static_assert(size(CS{1, 2}) == sizeof(S), "");
26+
static_assert(size(RSA{{1, 2}, {3, 4}}) == 2 * sizeof(S), "");
27+
static_assert(sizeof(decltype((RA{1, 2}))) == 2 * sizeof(int), "");
28+
29+
const int (&r1)[2]{1, 2};
30+
const S &r2{1, 2};
31+
void g(const int (&)[2]);
32+
void f() {
33+
auto &&a = RA{1, 2};
34+
auto &&b = CA{i, j};
35+
int n = size(RA{i, j}) + size(CS{i, j}) + size(RA{});
36+
g(CA{1, 2});
37+
g(RA{i, j});
38+
(void)a; (void)b; (void)n;
39+
}

0 commit comments

Comments
 (0)