@@ -1661,6 +1661,70 @@ class cppfront
1661
1661
return ;
1662
1662
}
1663
1663
1664
+ // Check to see if it's just a function call syntax chained with other functions,
1665
+ // and if so use this path to convert it to UFCS
1666
+ if (// there's a single-token expression followed by (, ., and (
1667
+ n.expr ->get_token () && // if the base expression is a single token
1668
+ std::ssize (n.ops ) >= 3 && // and we're of the form:
1669
+ n.ops [0 ].op ->type () == lexeme::LeftParen && // token ( expr-list ) . id-expr ( expr-list )
1670
+ n.ops [1 ].op ->type () == lexeme::Dot &&
1671
+ n.ops [2 ].op ->type () == lexeme::LeftParen
1672
+ )
1673
+ {
1674
+ // If we already replaced this with a capture (which contains the UFCS
1675
+ // work already done when the capture was computed), emit the capture
1676
+ if (!captured_part.empty ()) {
1677
+ printer.print_cpp2 (captured_part, n.position ());
1678
+ return ;
1679
+ }
1680
+
1681
+ // Otherwise, do the UFCS work...
1682
+
1683
+ // If method are chained we need to go from the last to the first
1684
+ // token(a-expr-list).b(b-expr-list).c(c-expr-list) will be tranformed to:
1685
+ // CPP2_UFCS(c, CPP2_UFCS(b, CPP2_UFCS(token, a-expr-list), b-expr-list), c-expr-list )
1686
+ for (auto i = std::ssize (n.ops )-1 ; i > 1 ; i -= 2 )
1687
+ {
1688
+ // The . has its id_expr
1689
+ assert (n.ops [i-1 ].id_expr );
1690
+
1691
+ // The ( has its expr_list and op_close
1692
+ assert (n.ops [i].expr_list && n.ops [i].op_close );
1693
+
1694
+ // If there are no additional arguments, use the CPP2_UFCS_0 version
1695
+ if (!n.ops [i].expr_list ->expressions .empty ()) {
1696
+ printer.print_cpp2 (" CPP2_UFCS(" , n.position ());
1697
+ }
1698
+ else {
1699
+ printer.print_cpp2 (" CPP2_UFCS_0(" , n.position ());
1700
+ }
1701
+ emit (*n.ops [i-1 ].id_expr );
1702
+ printer.print_cpp2 (" , " , n.position ());
1703
+ }
1704
+
1705
+ // emit the first function that starts chaining (the most nested one)
1706
+ emit (*n.expr );
1707
+ printer.print_cpp2 (" (" , n.position ());
1708
+ if (!n.ops [0 ].expr_list ->expressions .empty ()) {
1709
+ emit (*n.ops [0 ].expr_list );
1710
+ }
1711
+ printer.print_cpp2 (" )" , n.position ());
1712
+
1713
+ // unroll the nested calls (skipping the first function call)
1714
+ // expr-list need to be added in reversed order then CPP2_UFCS macros
1715
+ for (auto i = 2 ; i < std::ssize (n.ops ); i += 2 ) {
1716
+ // Then tack on any additional arguments
1717
+ if (!n.ops [i].expr_list ->expressions .empty ()) {
1718
+ printer.print_cpp2 (" , " , n.position ());
1719
+ emit (*n.ops [i].expr_list );
1720
+ }
1721
+ printer.print_cpp2 (" )" , n.position ());
1722
+ }
1723
+
1724
+ // And we're done. This path has handled this node, so return...
1725
+ return ;
1726
+ }
1727
+
1664
1728
// Otherwise, we're going to have to potentially do some work to change
1665
1729
// some Cpp2 postfix operators to Cpp1 prefix operators, so let's set up...
1666
1730
auto prefix = std::vector<text_with_pos>{};
0 commit comments