Skip to content

UFCS: function call starts UFCS chaining #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions regression-tests/mixed-ufcs-function-call-starts-chaining.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <vector>
#include <memory>

template <typename T, typename D>
struct scope_exit {
scope_exit(T v, D d)
: value(v)
, deleter(d)
{
}

~scope_exit() {
deleter(value);
}

operator T&() { return value; }

scope_exit(const scope_exit&) = delete;
scope_exit& operator=(const scope_exit&) = delete;
scope_exit(scope_exit&& rhs) noexcept : value(std::move(rhs.value)), deleter(std::move(rhs.deleter)) {}
scope_exit& operator=(scope_exit&& rhs) noexcept {
value = std::move(rhs.value);
deleter = std::move(rhs.deleter);
return *this;
}

private:
T value;
D deleter;
};

template <typename T, typename D>
auto on_scope_exit(T&& v, D&& d) {
return scope_exit(std::forward<T>(v),std::forward<D>(d));
}

main: () -> int = {

fopen("variable2.txt", "w").on_scope_exit(:(e:_) = {
e.fprintf("you can handle smart_ptrs without changing behaviour of UFCS");
e.fclose();
});

m := fopen("manual.txt", "w");
m.fprintf("Manual handling still works");
m.fclose();

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ auto test_generic(auto const& x) -> void;

auto test_generic(auto const& x) -> void{
std::cout
<< std::setw(30) << typeid(x).name()
<< std::setw(30) << CPP2_UFCS_0(name, typeid(x))
<< " value is "
<< [&] () -> std::string { auto&& __expr = x;
if (cpp2::is<int>(__expr)) { if constexpr( requires{"integer " + std::to_string(cpp2::as<int>(x));} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("integer " + std::to_string(cpp2::as<int>(x)))),std::string> ) return "integer " + std::to_string(cpp2::as<int>(x)); else return std::string{}; else return std::string{}; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ auto print_an_int(auto const& x) -> void;

auto print_an_int(auto const& x) -> void{
std::cout
<< std::setw(30) << typeid(x).name()
<< std::setw(30) << CPP2_UFCS_0(name, typeid(x))
<< " value is "
<< [&] () -> std::string { auto&& __expr = x;
if (cpp2::is<int>(__expr)) { if constexpr( requires{std::to_string(cpp2::as<int>(x));} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF((std::to_string(cpp2::as<int>(x)))),std::string> ) return std::to_string(cpp2::as<int>(x)); else return std::string{}; else return std::string{}; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ auto test_generic(auto const& x) -> void;

auto test_generic(auto const& x) -> void{
std::cout
<< "\n" << typeid(x).name() << "\n ..."
<< "\n" << CPP2_UFCS_0(name, typeid(x)) << "\n ..."
<< [&] () -> std::string { auto&& __expr = x;
if (cpp2::is<std::string>(__expr)) { if constexpr( requires{" matches std::string";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF((" matches std::string")),std::string> ) return " matches std::string"; else return std::string{}; else return std::string{}; }
else if (cpp2::is<std::variant<int,std::string>>(__expr)) { if constexpr( requires{" matches std::variant<int, std::string>";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF((" matches std::variant<int, std::string>")),std::string> ) return " matches std::variant<int, std::string>"; else return std::string{}; else return std::string{}; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ auto test_generic(auto const& x) -> void;

auto test_generic(auto const& x) -> void{
std::cout
<< "\n" << typeid(x).name() << "\n ..."
<< "\n" << CPP2_UFCS_0(name, typeid(x)) << "\n ..."
<< [&] () -> std::string { auto&& __expr = x;
if (cpp2::is<void>(__expr)) { if constexpr( requires{" VOYDE AND EMPTIE";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF((" VOYDE AND EMPTIE")),std::string> ) return " VOYDE AND EMPTIE"; else return std::string{}; else return std::string{}; }
else return " no match"; }
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/test-results/pure2-type-safety-1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ auto print(cpp2::in<std::string> msg, cpp2::in<bool> b) -> void;
}

auto test_generic(auto const& x) -> void{
std::string msg { typeid(x).name() };
std::string msg { CPP2_UFCS_0(name, typeid(x)) };
msg += " is int? ";
print( msg, cpp2::is<int>(x));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ auto test_generic(auto const& x) -> void;

auto test_generic(auto const& x) -> void{
std::cout
<< std::setw(30) << typeid(x).name()
<< std::setw(30) << CPP2_UFCS_0(name, typeid(x))
<< " value is "
<< [&] () -> std::string { auto&& __expr = x;
if (cpp2::is<int>(__expr)) { if constexpr( requires{std::to_string(cpp2::as<int>(x));} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF((std::to_string(cpp2::as<int>(x)))),std::string> ) return std::to_string(cpp2::as<int>(x)); else return std::string{}; else return std::string{}; }
Expand Down
68 changes: 68 additions & 0 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,74 @@ class cppfront
return;
}

// Check to see if it's just a function call syntax chained with other functions,
// and if so use this path to convert it to UFCS
if (// there's a single-token expression followed by (, ., and (
n.expr->get_token() && // if the base expression is a single token
std::ssize(n.ops) >= 3 && // and we're of the form:
n.ops[0].op->type() == lexeme::LeftParen && // token ( expr-list ) . id-expr ( expr-list )
n.ops[1].op->type() == lexeme::Dot &&
n.ops[2].op->type() == lexeme::LeftParen
)
{
// If we already replaced this with a capture (which contains the UFCS
// work already done when the capture was computed), emit the capture
if (!captured_part.empty()) {
printer.print_cpp2(captured_part, n.position());
return;
}

// Otherwise, do the UFCS work...

// If method are chained we need to go from the last to the first
// token(a-expr-list).b(b-expr-list).c(c-expr-list) will be tranformed to:
// CPP2_UFCS(c, CPP2_UFCS(b, CPP2_UFCS(token, a-expr-list), b-expr-list), c-expr-list )
for (auto i = std::ssize(n.ops)-1; i > 1; i -= 2)
{
// The . has its id_expr
assert (n.ops[i-1].id_expr);

// The ( has its expr_list and op_close
assert (n.ops[i].expr_list && n.ops[i].op_close);

//--------------------------------------------------------------------
// TODO: When MSVC supports __VA_OPT__ in standard mode without the
// experimental /Zc:preprocessor switch, use this single line
// instead of the dual lines below that special-case _0 args
// AND: Make the similarly noted change in cpp2util.h
//
// If there are no additional arguments, use the CPP2_UFCS_0 version
if (!n.ops[i].expr_list->expressions.empty()) {
printer.print_cpp2("CPP2_UFCS(", n.position());
}
else {
printer.print_cpp2("CPP2_UFCS_0(", n.position());
}
emit(*n.ops[i-1].id_expr);
printer.print_cpp2(", ", n.position());
}

// emit the first function that starts chaining (the most nested one)
emit(*n.expr);
if (!n.ops[0].expr_list->expressions.empty()) {
emit(*n.ops[0].expr_list);
}

// unroll the nested calls (skipping the first function call)
// expr-list need to be added in reversed order then CPP2_UFCS macros
for (auto i = 2; i < std::ssize(n.ops); i += 2) {
// Then tack on any additional arguments
if (!n.ops[i].expr_list->expressions.empty()) {
printer.print_cpp2(", ", n.position());
emit(*n.ops[i].expr_list);
}
printer.print_cpp2(")", n.position());
}

// And we're done. This path has handled this node, so return...
return;
}

// Otherwise, we're going to have to potentially do some work to change
// some Cpp2 postfix operators to Cpp1 prefix operators, so let's set up...
auto prefix = std::vector<text_with_pos>{};
Expand Down