From 955042bb71d0319076f9a5b6c5f412058bd2713e Mon Sep 17 00:00:00 2001 From: Filip Sajdak Date: Sun, 21 May 2023 18:47:26 +0200 Subject: [PATCH] Add ctad for template udt Currently after compiling the following code: ```cpp value: type = { public data1: T; public data2: Y; public data3: Z; operator=: (out this, out t: T, inout y: Y, z: Z) = { t = 42; data1 = t; data2 = y; data3 = z; } operator=: (out this, t: * const T, y: *Y, move z: Z) = { data1 = t*; data2 = y*; data3 = z; } } main: () = { i : int; s := std::string("lvalue string"); v: value = (out i, s, :std::string = "temporary string"); std::cout << "(v.data1)$, (v.data2)$, (v.data3)$" << std::endl; s = "will be moved"; w: value = (v.data1&, v.data2&, move s); std::cout << "(w.data1)$, (w.data2)$, (w.data3)$" << std::endl; } ``` We will get the following error: ``` ../tests/ctad.cpp2... ok (all Cpp2, passes safety checks) ../tests/ctad.cpp2:22:11: error: no viable constructor or deduction guide for deduction of template arguments of 'value' value v {&i, s, std::string{"temporary string"}}; ^ ../tests/ctad.cpp2:12:13: note: candidate template ignored: couldn't infer template argument 'T' public: value(cpp2::in t, cpp2::in y, Z&& z); ^ ../tests/ctad.cpp2:6:13: note: candidate template ignored: could not match 'cpp2::out' against 'cpp2::deferred_init *' public: value(cpp2::out t, Y& y, cpp2::in z); ^ ../tests/ctad.cpp2:1:52: note: candidate function template not viable: requires 1 argument, but 3 were provided template class value { ^ 1 error generated. ``` After this change cppfront generates Class template argument deduction (CTAD) for template class that has constructors with the same argument type list as template arguments (order matters). cppfront will add the following cpp1 code to `Cpp2 type definitions and function declarations` section: ```cpp template value(cpp2::deferred_init*, Y&, Z const &) -> value; template value(T const* const &, Y* const &, Z&&) -> value; ``` And will make the code compile and run successfuly: ``` 42, lvalue string, temporary string 42, lvalue string, will be moved ``` --- source/cppfront.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/source/cppfront.cpp b/source/cppfront.cpp index 13a3e94c8..95e72faca 100644 --- a/source/cppfront.cpp +++ b/source/cppfront.cpp @@ -5027,6 +5027,13 @@ class cppfront { printer.print_cpp2( " final", n.position() ); } + + // CTAD + struct ctad_details { + std::string args; + std::string types; + }; + auto ctads = std::vector{}; // Type definition @@ -5052,6 +5059,52 @@ class cppfront if (decl->is_constructor()) { found_constructor = true; + + if (n.template_parameters) { + if (auto func = std::get_if(&decl->type)) { + const auto& params = (*func)->parameters->parameters; + auto can_generate_ctad = std::equal( + std::cbegin(params)+1, std::cend(params), + std::cbegin(n.template_parameters->parameters), std::cend(n.template_parameters->parameters), + [](const auto& arg, const auto& type) -> bool { + if (const auto* arg_type = std::get_if(&arg->declaration->type)) { + assert(type->name()); + return (*arg_type)->get_token() && (*arg_type)->get_token()->as_string_view() == type->name()->as_string_view(); + } else { + return false; + } + } + ); + if (can_generate_ctad) { + ctads.emplace_back(); + std::for_each(std::cbegin(params)+1, std::cend(params), [&](const auto& arg) { + if (const auto* type = std::get_if(&arg->declaration->type)) { + auto& arg_t = *type; + assert(arg_t->get_token()); + auto type_name = arg_t->get_token()->to_string(true); + auto arg_type = print_to_string(*arg_t); + switch (arg->pass) { + case passing_style::in : arg_type += " const &"; break; + case passing_style::out : arg_type = "cpp2::deferred_init<" + arg_type + ">*"; break; + case passing_style::inout : arg_type += "&"; break; + case passing_style::move : + case passing_style::forward: arg_type += "&&"; break; + case passing_style::copy : break; + default: assert(!"ICE: invalid argument passing style"); + } + + if (ctads.back().args.empty()) { + ctads.back().args = arg_type; + ctads.back().types = type_name; + } else { + ctads.back().args += ", " + arg_type; + ctads.back().types += ", " + type_name; + } + } + }); + } + } + } } if (decl->is_constructor_with_that()) { found_that_constructor = true; @@ -5145,6 +5198,25 @@ class cppfront } printer.print_cpp2("};\n", compound_stmt->close_brace); + + if (!ctads.empty()) { + printer.print_cpp2("\n", n.position()); + } + + for (const auto& ctad : ctads) { + printer.print_cpp2("template", n.position()); + emit(*n.template_parameters, false, true); + printer.print_cpp2(" ", n.position()); + + printer.print_cpp2(n.name()->to_string(true), n.position()); + printer.print_cpp2("(", n.position()); + + printer.print_cpp2(ctad.args, n.position()); + printer.print_cpp2(") -> ", n.position()); + printer.print_cpp2(n.name()->to_string(true) + "<", n.position()); + printer.print_cpp2(ctad.types, n.position()); + printer.print_cpp2(">;\n", n.position()); + } } }