Skip to content

Commit 69c6f3e

Browse files
committed
Added: UFCS chaining starts on function call
1 parent 92ff88c commit 69c6f3e

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

source/cppfront.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,76 @@ class cppfront
16851685
return;
16861686
}
16871687

1688+
// Check to see if it's just a function call syntax chained with other functions,
1689+
// and if so use this path to convert it to UFCS
1690+
if (// there's a single-token expression followed by (, ., and (
1691+
n.expr->get_token() && // if the base expression is a single token
1692+
std::ssize(n.ops) >= 3 && // and we're of the form:
1693+
n.ops[0].op->type() == lexeme::LeftParen && // token ( expr-list ) . id-expr ( expr-list )
1694+
n.ops[1].op->type() == lexeme::Dot &&
1695+
n.ops[2].op->type() == lexeme::LeftParen
1696+
)
1697+
{
1698+
// If we already replaced this with a capture (which contains the UFCS
1699+
// work already done when the capture was computed), emit the capture
1700+
if (!captured_part.empty()) {
1701+
printer.print_cpp2(captured_part, n.position());
1702+
return;
1703+
}
1704+
1705+
// Otherwise, do the UFCS work...
1706+
1707+
// If method are chained we need to go from the last to the first
1708+
// token(a-expr-list).b(b-expr-list).c(c-expr-list) will be tranformed to:
1709+
// CPP2_UFCS(c, CPP2_UFCS(b, CPP2_UFCS(token, a-expr-list), b-expr-list), c-expr-list )
1710+
for (auto i = std::ssize(n.ops)-1; i > 1; i -= 2)
1711+
{
1712+
// The . has its id_expr
1713+
assert (n.ops[i-1].id_expr);
1714+
1715+
// The ( has its expr_list and op_close
1716+
assert (n.ops[i].expr_list && n.ops[i].op_close);
1717+
1718+
//--------------------------------------------------------------------
1719+
// TODO: When MSVC supports __VA_OPT__ in standard mode without the
1720+
// experimental /Zc:preprocessor switch, use this single line
1721+
// instead of the dual lines below that special-case _0 args
1722+
// AND: Make the similarly noted change in cpp2util.h
1723+
//
1724+
// If there are no additional arguments, use the CPP2_UFCS_0 version
1725+
if (!n.ops[i].expr_list->expressions.empty()) {
1726+
printer.print_cpp2("CPP2_UFCS(", n.position());
1727+
}
1728+
else {
1729+
printer.print_cpp2("CPP2_UFCS_0(", n.position());
1730+
}
1731+
emit(*n.ops[i-1].id_expr);
1732+
printer.print_cpp2(", ", n.position());
1733+
}
1734+
1735+
// emit the first function that starts chaining (the most nested one)
1736+
emit(*n.expr);
1737+
printer.print_cpp2("(", n.position());
1738+
if (!n.ops[0].expr_list->expressions.empty()) {
1739+
emit(*n.ops[0].expr_list);
1740+
}
1741+
printer.print_cpp2(")", n.position());
1742+
1743+
// unroll the nested calls (skipping the first function call)
1744+
// expr-list need to be added in reversed order then CPP2_UFCS macros
1745+
for (auto i = 2; i < std::ssize(n.ops); i += 2) {
1746+
// Then tack on any additional arguments
1747+
if (!n.ops[i].expr_list->expressions.empty()) {
1748+
printer.print_cpp2(", ", n.position());
1749+
emit(*n.ops[i].expr_list);
1750+
}
1751+
printer.print_cpp2(")", n.position());
1752+
}
1753+
1754+
// And we're done. This path has handled this node, so return...
1755+
return;
1756+
}
1757+
16881758
// Otherwise, we're going to have to potentially do some work to change
16891759
// some Cpp2 postfix operators to Cpp1 prefix operators, so let's set up...
16901760
auto prefix = std::vector<text_with_pos>{};

0 commit comments

Comments
 (0)