Skip to content

Commit 41f6749

Browse files
committed
Add ctad for template udt
Currently after compiling the following code: ```cpp value: <T: type, Y: type, Z: type> 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 const*> t, cpp2::in<Y*> y, Z&& z); ^ ../tests/ctad.cpp2:6:13: note: candidate template ignored: could not match 'cpp2::out<T>' against 'cpp2::deferred_init<int> *' public: value(cpp2::out<T> t, Y& y, cpp2::in<Z> z); ^ ../tests/ctad.cpp2:1:52: note: candidate function template not viable: requires 1 argument, but 3 were provided template<typename T, typename Y, typename Z> 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<typename T, typename Y, typename Z> value(cpp2::deferred_init<T>*, Y&, Z const &) -> value<T, Y, Z>; template<typename T, typename Y, typename Z> value(T const* const &, Y* const &, Z&&) -> value<T, Y, Z>; ``` And will make the code compile and run successfuly: ``` 42, lvalue string, temporary string 42, lvalue string, will be moved ```
1 parent e85612d commit 41f6749

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

source/cppfront.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5014,6 +5014,13 @@ class cppfront
50145014
{
50155015
printer.print_cpp2( " final", n.position() );
50165016
}
5017+
5018+
// CTAD
5019+
struct ctad_details {
5020+
std::string args;
5021+
std::string types;
5022+
};
5023+
auto ctads = std::vector<ctad_details>{};
50175024

50185025
// Type definition
50195026

@@ -5039,6 +5046,52 @@ class cppfront
50395046

50405047
if (decl->is_constructor()) {
50415048
found_constructor = true;
5049+
5050+
if (n.template_parameters) {
5051+
if (auto func = std::get_if<declaration_node::a_function>(&decl->type)) {
5052+
const auto& params = (*func)->parameters->parameters;
5053+
auto can_generate_ctad = std::equal(
5054+
std::cbegin(params)+1, std::cend(params),
5055+
std::cbegin(n.template_parameters->parameters), std::cend(n.template_parameters->parameters),
5056+
[](const auto& arg, const auto& type) -> bool {
5057+
if (const auto* arg_type = std::get_if<declaration_node::an_object>(&arg->declaration->type)) {
5058+
assert(type->name());
5059+
return (*arg_type)->get_token() && (*arg_type)->get_token()->as_string_view() == type->name()->as_string_view();
5060+
} else {
5061+
return false;
5062+
}
5063+
}
5064+
);
5065+
if (can_generate_ctad) {
5066+
ctads.emplace_back();
5067+
std::for_each(std::cbegin(params)+1, std::cend(params), [&](const auto& arg) {
5068+
if (const auto* type = std::get_if<declaration_node::an_object>(&arg->declaration->type)) {
5069+
auto& arg_t = *type;
5070+
assert(arg_t->get_token());
5071+
auto type_name = arg_t->get_token()->to_string(true);
5072+
auto arg_type = print_to_string(*arg_t);
5073+
switch (arg->pass) {
5074+
case passing_style::in : arg_type += " const &"; break;
5075+
case passing_style::out : arg_type = "cpp2::deferred_init<" + arg_type + ">*"; break;
5076+
case passing_style::inout : arg_type += "&"; break;
5077+
case passing_style::move :
5078+
case passing_style::forward: arg_type += "&&"; break;
5079+
case passing_style::copy : break;
5080+
default: assert(!"ICE: invalid argument passing style");
5081+
}
5082+
5083+
if (ctads.back().args.empty()) {
5084+
ctads.back().args = arg_type;
5085+
ctads.back().types = type_name;
5086+
} else {
5087+
ctads.back().args += ", " + arg_type;
5088+
ctads.back().types += ", " + type_name;
5089+
}
5090+
}
5091+
});
5092+
}
5093+
}
5094+
}
50425095
}
50435096
if (decl->is_constructor_with_that()) {
50445097
found_that_constructor = true;
@@ -5132,6 +5185,25 @@ class cppfront
51325185
}
51335186

51345187
printer.print_cpp2("};\n", compound_stmt->close_brace);
5188+
5189+
if (!ctads.empty()) {
5190+
printer.print_cpp2("\n", n.position());
5191+
}
5192+
5193+
for (const auto& ctad : ctads) {
5194+
printer.print_cpp2("template", n.position());
5195+
emit(*n.template_parameters, false, true);
5196+
printer.print_cpp2(" ", n.position());
5197+
5198+
printer.print_cpp2(n.name()->to_string(true), n.position());
5199+
printer.print_cpp2("(", n.position());
5200+
5201+
printer.print_cpp2(ctad.args, n.position());
5202+
printer.print_cpp2(") -> ", n.position());
5203+
printer.print_cpp2(n.name()->to_string(true) + "<", n.position());
5204+
printer.print_cpp2(ctad.types, n.position());
5205+
printer.print_cpp2(">;\n", n.position());
5206+
}
51355207
}
51365208
}
51375209

0 commit comments

Comments
 (0)