Skip to content

Commit 9721b18

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 d8c1a50 commit 9721b18

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
@@ -5013,6 +5013,13 @@ class cppfront
50135013
}
50145014
}
50155015

5016+
// CTAD
5017+
struct ctad_details {
5018+
std::string args;
5019+
std::string types;
5020+
};
5021+
auto ctads = std::vector<ctad_details>{};
5022+
50165023
// Type definition
50175024

50185025
auto separator = std::string{":"};
@@ -5037,6 +5044,52 @@ class cppfront
50375044

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

51265179
printer.print_cpp2("};\n", compound_stmt->close_brace);
5180+
5181+
if (!ctads.empty()) {
5182+
printer.print_cpp2("\n", n.position());
5183+
}
5184+
5185+
for (const auto& ctad : ctads) {
5186+
printer.print_cpp2("template", n.position());
5187+
emit(*n.template_parameters, false, true);
5188+
printer.print_cpp2(" ", n.position());
5189+
5190+
printer.print_cpp2(n.name()->to_string(true), n.position());
5191+
printer.print_cpp2("(", n.position());
5192+
5193+
printer.print_cpp2(ctad.args, n.position());
5194+
printer.print_cpp2(") -> ", n.position());
5195+
printer.print_cpp2(n.name()->to_string(true) + "<", n.position());
5196+
printer.print_cpp2(ctad.types, n.position());
5197+
printer.print_cpp2(">;\n", n.position());
5198+
}
51275199
}
51285200
}
51295201

0 commit comments

Comments
 (0)