Skip to content

Commit 95688de

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 65fcd0f commit 95688de

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
@@ -4950,6 +4950,13 @@ class cppfront
49504950
}
49514951
}
49524952

4953+
// CTAD
4954+
struct ctad_details {
4955+
std::string args;
4956+
std::string types;
4957+
};
4958+
auto ctads = std::vector<ctad_details>{};
4959+
49534960
// Type definition
49544961

49554962
auto separator = std::string{":"};
@@ -4974,6 +4981,52 @@ class cppfront
49744981

49754982
if (decl->is_constructor()) {
49764983
found_constructor = true;
4984+
4985+
if (n.template_parameters) {
4986+
if (auto func = std::get_if<declaration_node::a_function>(&decl->type)) {
4987+
const auto& params = (*func)->parameters->parameters;
4988+
auto can_generate_ctad = std::equal(
4989+
std::cbegin(params)+1, std::cend(params),
4990+
std::cbegin(n.template_parameters->parameters), std::cend(n.template_parameters->parameters),
4991+
[](const auto& arg, const auto& type) -> bool {
4992+
if (const auto* arg_type = std::get_if<declaration_node::an_object>(&arg->declaration->type)) {
4993+
assert(type->name());
4994+
return (*arg_type)->get_token() && (*arg_type)->get_token()->as_string_view() == type->name()->as_string_view();
4995+
} else {
4996+
return false;
4997+
}
4998+
}
4999+
);
5000+
if (can_generate_ctad) {
5001+
ctads.emplace_back();
5002+
std::for_each(std::cbegin(params)+1, std::cend(params), [&](const auto& arg) {
5003+
if (const auto* type = std::get_if<declaration_node::an_object>(&arg->declaration->type)) {
5004+
auto& arg_t = *type;
5005+
assert(arg_t->get_token());
5006+
auto type_name = arg_t->get_token()->to_string(true);
5007+
auto arg_type = print_to_string(*arg_t);
5008+
switch (arg->pass) {
5009+
case passing_style::in : arg_type += " const &"; break;
5010+
case passing_style::out : arg_type = "cpp2::deferred_init<" + arg_type + ">*"; break;
5011+
case passing_style::inout : arg_type += "&"; break;
5012+
case passing_style::move :
5013+
case passing_style::forward: arg_type += "&&"; break;
5014+
case passing_style::copy : break;
5015+
default: assert(!"ICE: invalid argument passing style");
5016+
}
5017+
5018+
if (ctads.back().args.empty()) {
5019+
ctads.back().args = arg_type;
5020+
ctads.back().types = type_name;
5021+
} else {
5022+
ctads.back().args += ", " + arg_type;
5023+
ctads.back().types += ", " + type_name;
5024+
}
5025+
}
5026+
});
5027+
}
5028+
}
5029+
}
49775030
}
49785031
if (decl->is_constructor_with_that()) {
49795032
found_that_constructor = true;
@@ -5061,6 +5114,25 @@ class cppfront
50615114
}
50625115

50635116
printer.print_cpp2("};\n", compound_stmt->close_brace);
5117+
5118+
if (!ctads.empty()) {
5119+
printer.print_cpp2("\n", n.position());
5120+
}
5121+
5122+
for (const auto& ctad : ctads) {
5123+
printer.print_cpp2("template", n.position());
5124+
emit(*n.template_parameters, false, true);
5125+
printer.print_cpp2(" ", n.position());
5126+
5127+
printer.print_cpp2(n.name()->to_string(true), n.position());
5128+
printer.print_cpp2("(", n.position());
5129+
5130+
printer.print_cpp2(ctad.args, n.position());
5131+
printer.print_cpp2(") -> ", n.position());
5132+
printer.print_cpp2(n.name()->to_string(true) + "<", n.position());
5133+
printer.print_cpp2(ctad.types, n.position());
5134+
printer.print_cpp2(">;\n", n.position());
5135+
}
50645136
}
50655137
}
50665138

0 commit comments

Comments
 (0)