Skip to content

Commit f186f11

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 72004cd commit f186f11

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
@@ -4989,6 +4989,13 @@ class cppfront
49894989
{
49904990
printer.print_cpp2( " final", n.position() );
49914991
}
4992+
4993+
// CTAD
4994+
struct ctad_details {
4995+
std::string args;
4996+
std::string types;
4997+
};
4998+
auto ctads = std::vector<ctad_details>{};
49924999

49935000
// Type definition
49945001

@@ -5014,6 +5021,52 @@ class cppfront
50145021

50155022
if (decl->is_constructor()) {
50165023
found_constructor = true;
5024+
5025+
if (n.template_parameters) {
5026+
if (auto func = std::get_if<declaration_node::a_function>(&decl->type)) {
5027+
const auto& params = (*func)->parameters->parameters;
5028+
auto can_generate_ctad = std::equal(
5029+
std::cbegin(params)+1, std::cend(params),
5030+
std::cbegin(n.template_parameters->parameters), std::cend(n.template_parameters->parameters),
5031+
[](const auto& arg, const auto& type) -> bool {
5032+
if (const auto* arg_type = std::get_if<declaration_node::an_object>(&arg->declaration->type)) {
5033+
assert(type->name());
5034+
return (*arg_type)->get_token() && (*arg_type)->get_token()->as_string_view() == type->name()->as_string_view();
5035+
} else {
5036+
return false;
5037+
}
5038+
}
5039+
);
5040+
if (can_generate_ctad) {
5041+
ctads.emplace_back();
5042+
std::for_each(std::cbegin(params)+1, std::cend(params), [&](const auto& arg) {
5043+
if (const auto* type = std::get_if<declaration_node::an_object>(&arg->declaration->type)) {
5044+
auto& arg_t = *type;
5045+
assert(arg_t->get_token());
5046+
auto type_name = arg_t->get_token()->to_string(true);
5047+
auto arg_type = print_to_string(*arg_t);
5048+
switch (arg->pass) {
5049+
case passing_style::in : arg_type += " const &"; break;
5050+
case passing_style::out : arg_type = "cpp2::deferred_init<" + arg_type + ">*"; break;
5051+
case passing_style::inout : arg_type += "&"; break;
5052+
case passing_style::move :
5053+
case passing_style::forward: arg_type += "&&"; break;
5054+
case passing_style::copy : break;
5055+
default: assert(!"ICE: invalid argument passing style");
5056+
}
5057+
5058+
if (ctads.back().args.empty()) {
5059+
ctads.back().args = arg_type;
5060+
ctads.back().types = type_name;
5061+
} else {
5062+
ctads.back().args += ", " + arg_type;
5063+
ctads.back().types += ", " + type_name;
5064+
}
5065+
}
5066+
});
5067+
}
5068+
}
5069+
}
50175070
}
50185071
if (decl->is_constructor_with_that()) {
50195072
found_that_constructor = true;
@@ -5107,6 +5160,25 @@ class cppfront
51075160
}
51085161

51095162
printer.print_cpp2("};\n", compound_stmt->close_brace);
5163+
5164+
if (!ctads.empty()) {
5165+
printer.print_cpp2("\n", n.position());
5166+
}
5167+
5168+
for (const auto& ctad : ctads) {
5169+
printer.print_cpp2("template", n.position());
5170+
emit(*n.template_parameters, false, true);
5171+
printer.print_cpp2(" ", n.position());
5172+
5173+
printer.print_cpp2(n.name()->to_string(true), n.position());
5174+
printer.print_cpp2("(", n.position());
5175+
5176+
printer.print_cpp2(ctad.args, n.position());
5177+
printer.print_cpp2(") -> ", n.position());
5178+
printer.print_cpp2(n.name()->to_string(true) + "<", n.position());
5179+
printer.print_cpp2(ctad.types, n.position());
5180+
printer.print_cpp2(">;\n", n.position());
5181+
}
51105182
}
51115183
}
51125184

0 commit comments

Comments
 (0)