Skip to content
Open
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
19 changes: 15 additions & 4 deletions src/cp_gen_be.c
Original file line number Diff line number Diff line change
Expand Up @@ -23383,6 +23383,12 @@ when possible.
use_func_notation_cast = FALSE;
unnamed_type_case = TRUE;
} /* if */
if (dip->is_auto_cast) {
/* The cast was written "auto(x)" or "auto{x}", so put it out that way
(the type might not be nameable, e.g., if it is a closure type). */
use_func_notation_cast = TRUE;
unnamed_type_case = FALSE;
} /* if */
/* We've now decided whether or not we want to use a functional-notation
cast. Change that decision in some cases to deal with deficiencies
in some target compilers. */
Expand Down Expand Up @@ -23437,9 +23443,13 @@ when possible.
/* Use a functional-notation cast. */
output_functional_notation_cast:
octl.suppress_template_args = suppress_template_args;
gen_type_reference(init_entity_type, /*suppress_typename_kwd=*/FALSE,
/*is_declaration=*/FALSE,
/*suppress_elab_type_spec=*/TRUE);
if (dip->is_auto_cast) {
write_tok_str("auto");
} else {
gen_type_reference(init_entity_type, /*suppress_typename_kwd=*/FALSE,
/*is_declaration=*/FALSE,
/*suppress_elab_type_spec=*/TRUE);
} /* if */
octl.suppress_template_args = saved_suppress_template_args;
/* In a case like "auto x = T{};", braced-init will be FALSE,
reflecting the use of the "=" in the initializer. However, if T
Expand Down Expand Up @@ -24800,7 +24810,8 @@ this one is such a continuation.
adjust_current_namespace(orig_scope, common_scope);
} /* if */
var->declaration_has_been_put_out = TRUE;
if (var->declared_with_auto_type_specifier && !for_init &&
if (var->declared_with_auto_type_specifier &&
!for_init && !is_condition && !is_iterator &&
is_immediate_class_type(unqual_var_type) &&
unqual_var_type->variant.class_struct_union.extra_info->
is_lambda_closure_class &&
Expand Down
36 changes: 32 additions & 4 deletions src/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -30737,6 +30737,8 @@ previously-scanned braced initializer.
an_init_component_ptr icp;
a_conv_context_set conv_context = CCO_CAST | CCO_EXPLICIT_CAST;
a_boolean error_on_narrowing;
a_boolean is_auto_cast = FALSE;
an_expr_node_ptr auto_cast_operand = NULL;
an_expr_node_ptr expr;

check_assertion(list_init_enabled);
Expand All @@ -30761,6 +30763,7 @@ previously-scanned braced initializer.
cast_operand_to_void(result, type_cast_to);
} else {
if (is_auto_type(type_cast_to)) {
is_auto_cast = TRUE;
if (!auto_cast_enabled) {
expr_pos_warning(ec_auto_cast_is_cpp23, &error_position);
} /* if */
Expand All @@ -30772,6 +30775,7 @@ previously-scanned braced initializer.
if (!is_template_dependent_type(opnd->type)) {
type_cast_to = type_after_auto_cast_transformations(opnd->type);
} /* if */
auto_cast_operand = expr_node_from_operand(opnd);
} else {
expr_pos_error(ec_bad_cast, init_component_pos(icp));
type_cast_to = error_type();
Expand All @@ -30795,8 +30799,22 @@ previously-scanned braced initializer.
/* Record source form information in the associated IL entries if
needed. */
expr->is_brace_notation_cast = TRUE;
if (is_any_reference_type(type_cast_to) && is_array_type(expr->type) &&
node_is_operator(expr, eok_lvalue_adjust)) {
if (is_auto_cast && auto_cast_operand != NULL &&
(expr == auto_cast_operand ||
(node_is(expr, enk_temp_init) &&
node_is(auto_cast_operand, enk_temp_init) &&
skip_constexpr_init_folding(expr->variant.init.dynamic_init) ==
skip_constexpr_init_folding(
auto_cast_operand->variant.init.dynamic_init)))) {
/* The operand is a prvalue of the type cast to, as in "auto{T(x)}",
so the result is just the operand, which keeps its own source
form. (If the operand was folded by a constexpr constructor call,
the result can have its own folded entry for the same
initialization.) */
temp_expr = NULL;
} else 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. */
Expand All @@ -30814,10 +30832,14 @@ previously-scanned braced initializer.
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)) {
if (temp_expr != NULL && 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 (is_auto_cast) {
dip->is_auto_cast = TRUE;
skip_constexpr_init_folding(dip)->is_auto_cast = TRUE;
} /* if */
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;
Expand Down Expand Up @@ -30988,7 +31010,8 @@ freed by this routine.
a_boolean err = FALSE;
a_symbol_ptr ctor_sym = NULL;
a_boolean ctor_case = FALSE, force_dependent = FALSE;
a_boolean could_be_dependent = FALSE, is_auto_cast;
a_boolean could_be_dependent = FALSE;
a_boolean is_auto_cast = FALSE;
a_class_symbol_supplement_ptr cssp = NULL;
an_operand local_bound_function_selector;
a_boolean allow_ms_array = microsoft_bugs && !C_mode();
Expand Down Expand Up @@ -31681,6 +31704,11 @@ freed by this routine.
expr->is_functional_notation_cast = TRUE;
if (expr->kind == (an_expr_node_kind)enk_temp_init) {
dip = expr->variant.init.dynamic_init;
if (is_auto_cast && dip->is_explicit_cast &&
expr != orig_operand_expression) {
dip->is_auto_cast = TRUE;
skip_constexpr_init_folding(dip)->is_auto_cast = TRUE;
} /* if */
} /* if */
} /* if */
set_operand_position(result, start_position, &end_position, start_position);
Expand Down
1 change: 1 addition & 0 deletions src/il_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2434,6 +2434,7 @@ Initialize a dynamic_init entry of the kind specified.
dip->is_explicit_cast = FALSE;
dip->is_compound_literal = FALSE;
dip->is_braced_initializer = FALSE;
dip->is_auto_cast = FALSE;
dip->is_partially_initialized = FALSE;
dip->is_result_for_class_rvalue_question_mark = FALSE;
dip->class_rvalue_initialized_through_master_entry = FALSE;
Expand Down
4 changes: 4 additions & 0 deletions src/il_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -4155,6 +4155,10 @@ typedef struct a_dynamic_init {
a_bit_field is_braced_initializer:1;
/* If TRUE, the source construct that generated this
initialization is a brace-enclosed initializer. */
a_bit_field is_auto_cast:1;
/* If TRUE, is_explicit_cast is also TRUE, and the
cast was written with "auto" as its type (C++23),
as in "auto(x)" or "auto{x}". */
a_bit_field is_partially_initialized:1;
/* TRUE if the initialized entity is an array or class
aggregate not completely initialized by the
Expand Down
3 changes: 3 additions & 0 deletions src/il_display.c
Original file line number Diff line number Diff line change
Expand Up @@ -7370,6 +7370,9 @@ Display the indicated dynamic_init structure.
if (ptr->is_braced_initializer) {
disp_boolean("is_braced_initializer", TRUE);
} /* if */
if (ptr->is_auto_cast) {
disp_boolean("is_auto_cast", TRUE);
} /* if */
if (ptr->is_partially_initialized) {
disp_boolean("is_partially_initialized", TRUE);
} /* if */
Expand Down
25 changes: 25 additions & 0 deletions src/pending_changes/gh-576-closure-braced-cast.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[GH #576]
C++-generating back end: auto casts

A cast written with "auto" (C++23), as in "auto(x)" or "auto{x}", was put
out as a cast to the deduced type. When that was a closure type, the
generated code was often ill-formed: if no variable of that type had been
declared, the type was written as "class __Tn", and if the variable was
const, the cast yielded a const object. For example, with --c++23:

int x = auto{[](int a) { return a; }}(2); // "class __T1{...}(2)"
int f() {
const auto c = [n = 0]() mutable { return ++n; };
return auto{c}(); // Call of a const object.
}

The new is_auto_cast field of a dynamic initialization now records such a
cast, and it is put out as written, with "auto".

Also, for a variable of closure type declared in the condition of an if,
switch, or while statement, or in a range-based for statement, a typedef
was put out inside the declaration, as in

if (auto m = []{ return 1; } typedef decltype(m) __T2;) ...

No typedef is put out for such a variable now.
1 change: 1 addition & 0 deletions tests/tests/changes/.GH576.rto/default.1.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpfe -DTEST_NUMBER=1 --c++23 Test_name.c
108 changes: 108 additions & 0 deletions tests/tests/changes/GH576.sft.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//type:cp
//options:--c++23
//remark:auto casts and closure-type typedefs in generated C++
//
// In generated C++, an auto cast to a closure type for which no typedef had
// been put out, like auto{[](int a) { return a; }} or auto{m} with m declared
// in a for-init, was put out with the unnamed closure type written as if it
// were a class name. An auto cast of a const closure variable used the
// typedef put out for the variable's type, which is const. Also, a typedef
// for the closure type of a variable declared in a condition or range-based
// for statement was put out inside the declaration.

int i = 3;
template<class T> constexpr int size(T&&) { return sizeof(T); }
struct C { C(); C(const C&); int v; };
auto g() { return [](int a) { return a; }; }

static_assert(auto{[](int a) { return a; }}(2) == 2);
static_assert(auto{auto{[] { return 1; }}}() == 1);
static_assert(size(auto{[] {}}) > 0);
int x = auto{[](int a) { return a + i; }}(2);
int y = sizeof(auto{[] {}}) + size(auto{[] {}});
auto h1 = auto{[] { return 1; }};
int z = auto{g()}(2) + auto(g())(3);

int f1() {
int j = 1;
return auto{[&] { return i + j; }}() +
auto{[j]() mutable { return ++j; }}() +
auto{[](auto a) { return a; }}(j) +
auto{[c = C()] { return c.v; }}();
}

template<class T> T f2() { return auto{[] { return T(1); }}(); }
int w = f2<int>();

int f3() {
int n = 0;
for (auto m = [n = 0]() mutable { return ++n; }; auto{m}() < 3; auto(m)()) {
n += m();
}
for (const auto c = [k = 0]() mutable { return ++k; }; n < 9; ++n) {
n += auto{c}();
}
for (auto m = [c = C()] { return c.v; }; n < 12; ++n) {
n += auto{m}() + auto(m)();
}
for (auto m = g(); n < 15; ++n) {
n += auto{m}(1);
}
return n;
}

int f4() {
return [m = [n = 0]() mutable { return ++n; }]() mutable {
return auto{m}() + m();
}();
}

int f6() {
const auto c = [n = 0]() mutable { return ++n; };
return auto{c}() + auto(c)();
}

struct B { int v; B(int); B(const B&); int operator()(int) const; };
B b(1);
int u = auto{b}(1) + auto(b)(2) + auto{B(i)}(3) + auto(B{i})(4);

// The class type is still put out when the operand of the auto cast is a
// prvalue folded by a constexpr constructor call.
struct A { int x; constexpr A(int n) : x(n) {} };
constexpr A ca{3};
int f7() {
return auto{A{2}}.x + auto{A(2)}.x + auto(A{2}).x + auto(A(2)).x +
auto{ca}.x + auto(ca).x;
}
static_assert(auto{auto{A{4}}}.x == 4);
A a1 = auto{A{5}};

struct R {
struct It {
int n;
auto operator*() { return g(); }
It& operator++() { ++n; return *this; }
bool operator!=(It o) const { return n != o.n; }
};
It begin() { return {0}; }
It end() { return {2}; }
};

int f5() {
int n = 0;
if (auto m = [] { return 1; }) {
n += m() + auto{m}();
}
switch (auto m = [] { return 2; }; m()) {
default:
n += m();
}
while (auto m = [] { return 3; }) {
n += m();
break;
}
for (auto m : R{}) {
n += m(1) + auto{m}(2);
}
return n;
}
Loading