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
60 changes: 56 additions & 4 deletions src/cp_gen_be.c
Original file line number Diff line number Diff line change
Expand Up @@ -23128,6 +23128,45 @@ to add extra parentheses to disambiguate.
} /* expr_may_look_like_type */


static a_boolean is_single_operand_cast_to_unnamed_closure(
a_dynamic_init_ptr dip,
a_type_ptr type)
/*
dip is the dynamic initialization for an explicit cast to type. Return TRUE
if type is a closure type that has no name in the generated code (because no
typedef was put out for it; see gen_variable_decl) and the cast has a single
operand.
*/
{
a_boolean result = FALSE;

type = skip_non_naming_typerefs(type);
if (is_immediate_class_type(type) &&
class_type_supp(type)->is_lambda_closure_class &&
!type->has_been_declared) {
an_expr_node_ptr args;
switch (dip->kind) {
case dik_expression:
case dik_lambda:
result = TRUE;
break;
case dik_bitwise_copy:
result = (dip->variant.bitwise_copy.source != NULL);
break;
case dik_constructor:
args = dip->variant.constructor.args;
result = (args != NULL && !args->generated_default_arg &&
!args->is_pack_expansion &&
(args->next == NULL || args->next->generated_default_arg));
break;
default:
break;
} /* switch */
} /* if */
return result;
} /* is_single_operand_cast_to_unnamed_closure */


static void handle_braced_init_list_ctor(an_expr_node_ptr arg)
/*
arg is the first constructor argument in a braced dik_constructor dynamic
Expand Down Expand Up @@ -23212,6 +23251,7 @@ when possible.
an_expr_node_ptr expr = NULL;
int closing_parens_needed = 0;
a_boolean unnamed_type_case = FALSE;
a_boolean unnamed_closure_case = FALSE;
a_boolean braced_init;
a_boolean is_value_init;
a_type_ptr bare_init_entity_type;
Expand Down Expand Up @@ -23353,6 +23393,13 @@ when possible.
use_func_notation_cast = FALSE;
unnamed_type_case = TRUE;
} /* if */
if (is_single_operand_cast_to_unnamed_closure(dip, init_entity_type)) {
/* The closure type has no name here, so the cast must have been
written "auto{x}" or "auto(x)", and it is put out that way. */
use_func_notation_cast = TRUE;
unnamed_type_case = FALSE;
unnamed_closure_case = TRUE;
} /* 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 @@ -23407,9 +23454,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 (unnamed_closure_case) {
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 @@ -24772,7 +24823,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
41 changes: 41 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,41 @@
[GH #576]
C++-generating back end: casts to unnamed closure types

The C++-generating back end can name a closure type only through a typedef
that it puts out after the declaration of a variable of that type. A cast to
a closure type for which there was no such typedef (which can be written
only with "auto", in C++23) was put out with the closure type written as if
it were a class name, which made the generated code ill-formed. For
example, with --c++23:

int x = auto{[](int a) { return a; }}(2);
auto g() { return [](int a) { return a; }; }
int y = auto{g()}(2);
int f() {
for (auto m = [n = 0]() mutable { return ++n; }; ; ) return auto{m}();
}

was generated as

int x = class __T1{[](int a) { return a; } }(2);
auto g() { return [](int a) { return a; } ; }
int y = class __T2{g()}(2);
int f() {
for (auto m = [n = 0]() mutable { return ++n; } ; ;) {
return class __T3{m}(); }
}

Such a cast is now 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, the typedef
was put out inside the declaration. For example,

if (auto m = [] { return 1; }) return m();

was generated as

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

No typedef is put out for such a variable now (as was already the case for a
variable declared in a for-init).
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 @@
fe_only -DTEST_NUMBER=1 --c++23 Test_name.c
87 changes: 87 additions & 0 deletions tests/tests/changes/GH576.sft.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//type:fp
//options:--c++23
//remark:Casts to unnamed closure types and closure typedefs in generated C++
//
// In generated C++, a 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. 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();
}();
}

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