1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817
|
/*
* Copyright 2020 Cerebras Systems. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY CEREBRAS SYSTEMS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CEREBRAS SYSTEMS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation
* are those of the authors and should not be interpreted as
* representing official policies, either expressed or implied, of
* Cerebras Systems.
*/
#include <ctype.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include "template_cpp.h"
#include "isl_config.h"
/* The textual representation of this tuple kind.
*
* By default, the textual representation is just the name.
*/
std::string TupleKind::to_string() const
{
return name;
}
/* Return the parameters of this tuple kind.
*
* By default, there are no parameters.
*/
std::vector<std::string> TupleKind::params() const
{
return { };
}
/* Apply the substitution "subs" to this tuple kind and return the result.
* "self" is a shared pointer to this.
*
* If the name of this tuple kind appears in the substitution,
* then return the corresponding tuple kind pointer.
* Otherwise, return "self".
*/
TupleKindPtr TupleKind::apply(const Substitution &subs,
const TupleKindPtr &self) const
{
if (subs.count(name) != 0)
return subs.at(name);
return self;
}
/* Apply the substitution "subs" to "tuple" and return the result.
*/
static TupleKindPtr apply(const TupleKindPtr tuple, const Substitution &subs)
{
return tuple->apply(subs, tuple);
}
/* Return the left child of this tuple kind.
*
* Since this is not a pair, there is no left child.
*/
TupleKindPtr TupleKind::left() const
{
return TupleKindPtr();
}
/* Return the right child of this tuple kind.
*
* Since this is not a pair, there is no right child.
*/
TupleKindPtr TupleKind::right() const
{
return TupleKindPtr();
}
/* Helper class used to construct a pointer to a tuple kind
* that refers to a non-template type.
*/
struct Fixed {
};
/* Construct a pointer to a tuple kind that refers to a non-template type.
*
* Use an empty string as name. Since this is a non-template type,
* the kind name will never appear in the generated code.
*/
TupleKindPtr::TupleKindPtr(Fixed) : Base(std::make_shared<TupleKind>(""))
{
}
/* Tuple pointers for non-template types.
*/
static TupleKindPtr Ctx{Fixed()};
static TupleKindPtr Integer{Fixed()};
static TupleKindPtr Str{Fixed()};
static TupleKindPtr Res{Fixed()};
/* Special tuple pointers.
* Anonymous appears in the generated code but cannot be unified
* with anything else since it is a predefined template argument.
* Leaf can only be unified with something that is not a pair and
* does not appear in the generated code.
*/
static TupleKindPtr Anonymous("Anonymous");
static TupleKindPtr Leaf("Leaf");
/* Placeholder tuple pointers that refer to (part of) the domain or range.
*/
static TupleKindPtr Domain("Domain");
static TupleKindPtr Domain2("Domain2");
static TupleKindPtr Domain3("Domain3");
static TupleKindPtr Range("Range");
static TupleKindPtr Range2("Range2");
static TupleKindPtr Range3("Range3");
/* A representation of a proper tuple kind that is used as a template
* parameter or a template argument.
*/
struct ProperTupleKind : public TupleKind {
ProperTupleKind(const std::string &name) : TupleKind(name) {}
virtual std::vector<std::string> params() const override;
};
/* Return the parameters of this tuple kind.
*
* Return the name of this tuple kind, unless it is the special Anonymous
* predefined template argument.
*/
std::vector<std::string> ProperTupleKind::params() const
{
if (Anonymous.get() == this)
return { };
return { name };
}
/* Construct a pointer to a tuple kind that refers
* to a proper tuple kind with the given name.
*/
TupleKindPtr::TupleKindPtr(const std::string &name) :
Base(std::make_shared<ProperTupleKind>(name))
{
}
/* A tuple kind that represents an anonymous pair of nested tuple kinds.
*/
struct Pair : public TupleKind {
Pair(const TupleKindPtr &tuple1, const TupleKindPtr &tuple2) :
TupleKind(""), tuple1(tuple1), tuple2(tuple2) {}
virtual std::string to_string() const override;
virtual std::vector<std::string> params() const override;
virtual TupleKindPtr apply(const Substitution &match,
const TupleKindPtr &self) const override;
virtual TupleKindPtr left() const override;
virtual TupleKindPtr right() const override;
const TupleKindPtr tuple1;
const TupleKindPtr tuple2;
};
/* The textual representation of this tuple kind.
*
* The textual representation of a pair is of the form "pair<tuple1, tuple2>".
*/
std::string Pair::to_string() const
{
return std::string("pair<") + tuple1->to_string() + ", " +
tuple2->to_string() + ">";
}
/* Add the elements of "vec2" that do not already appear in "vec1"
* at the end of "vec1".
*
* The two vectors are assumed not to have any repeated elements.
* The updated vector will then also not have repeated elements.
*/
static void combine(std::vector<std::string> &vec1,
const std::vector<std::string> &vec2)
{
for (const auto &s : vec2)
if (std::find(vec1.begin(), vec1.end(), s) == vec1.end())
vec1.emplace_back(s);
}
/* Return the parameters of this tuple kind.
*
* Combine the parameters of the two nested tuple kinds.
*/
std::vector<std::string> Pair::params() const
{
auto names1 = tuple1->params();
auto names2 = tuple2->params();
combine(names1, names2);
return names1;
}
/* Apply the substitution "subs" to this tuple kind and return the result.
* "self" is a shared pointer to this.
*
* Construct a new tuple kind consisting of the result of applying
* the substitution to the two nested tuple kinds.
*/
TupleKindPtr Pair::apply(const Substitution &subs, const TupleKindPtr &self)
const
{
return TupleKindPtr(::apply(tuple1, subs), ::apply(tuple2, subs));
}
/* Return the left child of this tuple kind.
*/
TupleKindPtr Pair::left() const
{
return tuple1;
}
/* Return the right child of this tuple kind.
*/
TupleKindPtr Pair::right() const
{
return tuple2;
}
/* Construct a pointer to a tuple kind that refers
* to the given pair of nested tuple kinds.
*/
TupleKindPtr::TupleKindPtr(const TupleKindPtr &left, const TupleKindPtr &right)
: Base(std::make_shared<Pair>(left, right))
{
}
/* Is this a kind of object representing an anonymous function?
*/
bool Kind::is_anon() const
{
return size() != 0 && back() == Anonymous;
}
/* Is this a kind of object with a single tuple?
*/
bool Kind::is_set() const
{
return size() == 1;
}
/* Is this a kind of object with a single, anonymous tuple?
*/
bool Kind::is_anon_set() const
{
return is_set() && is_anon();
}
/* Return the parameters of this kind.
*
* Collect the parameters of the tuple kinds in the sequence.
*/
std::vector<std::string> Kind::params() const
{
std::vector<std::string> params;
for (const auto &tuple : *this)
combine(params, tuple->params());
return params;
}
/* Apply the substitution "subs" to this kind and return the result.
*
* Apply the substitution to each of the tuple kinds in the sequence.
*/
Kind Kind::apply(const Substitution &subs) const
{
Kind applied;
for (const auto &tuple : *this)
applied.emplace_back(::apply(tuple, subs));
return applied;
}
/* A signature of a method in terms of kinds,
* consisting of a return kind and a sequence of argument kinds.
*/
struct Signature {
Kind ret;
std::vector<Kind> args;
std::vector<std::string> params() const;
Signature apply(const Substitution &match) const;
};
/* Return the parameters of this signature.
*
* Collect the parameters of the argument kinds and the return kind.
*/
std::vector<std::string> Signature::params() const
{
std::vector<std::string> params;
for (const auto &arg : args)
combine(params, arg.params());
combine(params, ret.params());
return params;
}
/* Apply the substitution "subs" to this kind and return the result.
*
* Apply the substitution to the argument kinds and the return kind.
*/
Signature Signature::apply(const Substitution &subs) const
{
std::vector<Kind> applied_args;
for (const auto &arg : args)
applied_args.emplace_back(arg.apply(subs));
return { ret.apply(subs), applied_args };
}
/* Return a renaming substitution that renames the elements of "params"
* using names starting with "prefix".
*/
static Substitution param_renamer(const std::vector<std::string> ¶ms,
const std::string &prefix)
{
Substitution renamer;
int n = 0;
for (const auto &name : params) {
auto suffix = std::to_string(++n);
auto arg_name = prefix + suffix;
auto arg = TupleKindPtr(arg_name);
if (name == Leaf->name)
generator::die("Leaf cannot be renamed");
renamer.emplace(name, arg);
}
return renamer;
}
/* Does the vector "v" contain the element "el"?
*/
static bool contains(const std::vector<std::string> &v, const std::string &el)
{
return find(v.begin(), v.end(), el) != v.end();
}
/* Return the shared elements of "v1" and "v2", preserving the order
* of those elements in "v1".
*/
static std::vector<std::string> intersect(const std::vector<std::string> &v1,
const std::vector<std::string> &v2)
{
std::vector<std::string> intersection;
for (const auto &el : v1)
if (contains(v2, el))
intersection.push_back(el);
return intersection;
}
/* Return a renaming substitution that renames
* any parameters that appears in both "sig" and "kind".
*/
static Substitution shared_param_renamer(const Signature &sig, const Kind &kind)
{
return param_renamer(intersect(sig.params(), kind.params()), "Arg");
}
/* Signatures for unary operations.
* Functions have at least one tuple.
*/
static Signature un_params = { { }, { { } } };
static Signature un_set = { { Domain }, { { Domain } } };
static Signature un_map = { { Domain, Range }, { { Domain, Range } } };
static std::vector<Signature> un_op = { un_params, un_set, un_map };
static std::vector<Signature> fn_un_op = { un_set, un_map };
/* Signatures for binary operations, with the second argument
* possibly referring to part of the first argument.
* Functions have at least one tuple.
*/
static Signature bin_params = { { }, { { }, { } } };
static Signature bin_set = { { Domain }, { { Domain }, { Domain } } };
static Signature bin_map =
{ { Domain, Range }, { { Domain, Range }, { Domain, Range } } };
static std::vector<Signature> bin_op = { bin_params, bin_set, bin_map };
static std::vector<Signature> fn_bin_op = { bin_set, bin_map };
static Signature bin_set_params = { { Domain }, { { Domain }, { } } };
static Signature bin_map_params =
{ { Domain, Range }, { { Domain, Range }, { } } };
static Signature bin_map_domain =
{ { Domain, Range }, { { Domain, Range }, { Domain } } };
static Signature bin_map_range =
{ { Domain, Range }, { { Domain, Range }, { Range } } };
/* Signatures for binary operations, where the second argument
* is an identifier (with an anonymous tuple).
*/
static Signature bin_params_anon = { { }, { { }, { Anonymous } } };
static Signature bin_set_anon = { { Domain }, { { Domain }, { Anonymous } } };
static Signature bin_map_anon =
{ { Domain, Range }, { { Domain, Range }, { Anonymous } } };
static std::vector<Signature> bin_op_anon =
{ bin_params_anon, bin_set_anon, bin_map_anon };
/* Signatures for ternary operations, where the last two arguments are integers.
*/
static Signature ter_params_int_int =
{ { }, { { }, { Integer }, { Integer } } };
static Signature ter_set_int_int =
{ { Domain }, { { Domain }, { Integer }, { Integer } } };
static Signature ter_map_int_int =
{ { Domain, Range }, { { Domain, Range }, { Integer }, { Integer } } };
static std::vector<Signature> ter_int_int =
{ ter_params_int_int, ter_set_int_int, ter_map_int_int };
/* Signatures for ternary operations.
* Functions have at least one tuple.
*/
static Signature ter_set =
{ { Domain }, { { Domain }, { Domain }, { Domain } } };
static Signature ter_map =
{ { Domain, Range },
{ { Domain, Range }, { Domain, Range }, { Domain, Range } } };
static std::vector<Signature> fn_ter_op = { ter_set, ter_map };
/* Signatures for naming a leaf tuple using an identifier (with an anonymous
* tuple).
*/
static Signature update_set = { { Domain2 }, { { Leaf }, { Anonymous } } };
static Signature update_domain =
{ { Domain2, Range }, { { Leaf, Range }, { Anonymous } } };
static Signature update_range =
{ { Domain, Range2 }, { { Domain, Leaf }, { Anonymous } } };
/* Signatures for the functions "min" and "max", which can be either
* unary or binary operations.
*/
static std::vector<Signature> min_max = { un_set, bin_set, un_map, bin_map };
/* Signatures for adding an unnamed tuple to an object with zero or one tuple.
*/
static Signature to_set = { { Domain }, { { }, { Integer } } };
static Signature add_range = { { Domain, Range }, { { Domain }, { Integer } } };
/* Signatures for adding a named tuple to an object with zero or one tuple.
*/
static Signature to_set_named =
{ { Domain }, { { }, { Anonymous }, { Integer } } };
static Signature add_range_named =
{ { Domain, Range }, { { Domain }, { Anonymous }, { Integer } } };
/* Signatures for methods applying a map to a set, a function or
* part of a map.
*/
static Signature set_forward = { { Range }, { { Domain }, { Domain, Range } } };
static Signature domain_forward =
{ { Domain2, Range }, { { Domain, Range }, { Domain, Domain2 } } };
static Signature range_forward =
{ { Domain, Range2 }, { { Domain, Range }, { Range, Range2 } } };
/* Signatures for methods plugging in a function into a set, a function or
* part of a map.
*/
static Signature set_backward =
{ { Domain2 }, { { Domain }, { Domain2, Domain } } };
static Signature domain_backward =
{ { Domain2, Range }, { { Domain, Range }, { Domain2, Domain } } };
static Signature range_backward =
{ { Domain, Range2 }, { { Domain, Range }, { Range2, Range } } };
static Signature domain_wrapped_domain_backward =
{ { { Domain3, Domain2 }, Range },
{ { { Domain, Domain2 }, Range }, { Domain3, Domain } } };
/* Signatures for methods binding a set, a function,
* or (part of) a map to parameters or an object of the same kind.
*/
static Signature bind_set = { { }, { { Domain }, { Domain } } };
static Signature bind_domain = { { Range }, { { Domain, Range }, { Domain } } };
static Signature bind_range = { { Domain }, { { Domain, Range }, { Range } } };
static Signature bind_domain_wrapped_domain =
{ { Range2, Range }, { { { Domain2, Range2 }, Range }, { Domain2 } } };
/* Signatures for functions that take a callback accepting
* objects of the same kind (but a different type).
*
* The return and argument kinds of the callback appear
* at the position of the callback.
*/
static Signature each_params = { { Res }, { { }, { Res }, { } } };
static Signature each_set = { { Res }, { { Domain }, { Res }, { Domain } } };
static Signature each_map =
{ { Res }, { { Domain, Range }, { Res }, { Domain, Range } } };
static std::vector<Signature> each = { each_params, each_set, each_map };
/* Signature for creating a map from a range,
* where the domain is given by an extra argument.
*/
static Signature map_from_range_and_domain =
{ { Domain, Range }, { { Range }, { Domain } } };
/* Signature for creating a map from a domain,
* where the range is given by an extra argument.
*/
static Signature map_from_domain_and_range =
{ { Domain, Range }, { { Domain }, { Range } } };
/* Signatures for creating an anonymous set from a parameter set
* or a map from a domain, where the range is anonymous.
*/
static Signature anonymous_set_from_params = { { Anonymous }, { { } } };
static Signature anonymous_map_from_domain =
{ { Domain, Anonymous }, { { Domain } } };
static std::vector<Signature> anonymous_from_domain =
{ anonymous_set_from_params, anonymous_map_from_domain };
/* Signature for creating a set from a parameter set,
* where the domain is given by an extra argument.
*/
static Signature set_from_params = { { Domain }, { { }, { Domain } } };
/* Signatures for creating an anonymous function from a domain,
* where the second argument is an identifier (with an anonymous tuple).
*/
static Signature anonymous_set_from_params_bin_anon =
{ { Anonymous }, { { }, { Anonymous } } };
static Signature anonymous_map_from_domain_bin_anon =
{ { Domain, Anonymous }, { { Domain }, { Anonymous } } };
static std::vector<Signature> anonymous_from_domain_bin_anon = {
anonymous_set_from_params_bin_anon,
anonymous_map_from_domain_bin_anon
};
/* Signature for creating a map from a domain,
* where the range tuple is equal to the domain tuple.
*/
static Signature set_to_map = { { Domain, Domain }, { { Domain } } };
/* Signatures for obtaining the range or the domain of a map.
* In case of a transformation, the domain and range are the same.
*/
static Signature domain = { { Domain }, { { Domain, Range } } };
static Signature range = { { Range }, { { Domain, Range } } };
static Signature transformation_domain = { { Domain }, { { Domain, Domain } } };
/* Signatures for obtaining the parameter domain of a set or map.
*/
static Signature set_params = { { }, { { Domain } } };
static Signature map_params = { { }, { { Domain, Range } } };
/* Signatures for obtaining the domain of a function.
*/
static std::vector<Signature> fn_domain = { domain, set_params };
/* Signatures for interchanging (wrapped) domain and range.
*/
static Signature map_reverse = { { Range, Domain }, { { Domain, Range } } };
static Signature map_range_reverse =
{ { Domain, { Range2, Range } }, { { Domain, { Range, Range2 } } } };
/* Signatures for constructing products.
*/
static Signature set_product =
{ { { Domain, Range } }, { { Domain }, { Range } } };
static Signature map_product =
{ { { Domain, Domain2 }, { Range, Range2 } },
{ { Domain, Range }, { Domain2, Range2 } } };
static Signature domain_product =
{ { { Domain, Domain2 }, Range },
{ { Domain, Range }, { Domain2, Range } } };
static Signature range_product =
{ { Domain, { Range, Range2 } },
{ { Domain, Range }, { Domain, Range2 } } };
/* Signatures for obtaining factors from a product.
*/
static Signature domain_factor_domain =
{ { Domain, Range }, { { { Domain, Domain2 }, Range } } };
static Signature domain_factor_range =
{ { Domain2, Range }, { { { Domain, Domain2 }, Range } } };
static Signature range_factor_domain =
{ { Domain, Range }, { { Domain, { Range, Range2 } } } };
static Signature range_factor_range =
{ { Domain, Range2 }, { { Domain, { Range, Range2 } } } };
/* Signatures for (un)currying.
*/
static Signature curry =
{ { Domain, { Range, Range2 } },
{ { { Domain, Range }, Range2 } } };
static Signature uncurry =
{ { { Domain, Range }, Range2 },
{ { Domain, { Range, Range2 } } } };
/* Signatures for (un)wrapping.
*/
static Signature wrap = { { { Domain, Range } }, { { Domain, Range } } };
static Signature unwrap = { { Domain, Range }, { { { Domain, Range } } } };
/* Signatures for constructing objects that map to the domain or range
* of a map.
*/
static Signature domain_map =
{ { { Domain, Range }, Domain }, { { Domain, Range } } };
static Signature range_map =
{ { { Domain, Range }, Range }, { { Domain, Range } } };
/* Signature for applying a comparison between the domain and the range
* of a map.
*/
static Signature map_cmp =
{ { Domain, Domain }, { { Domain, Domain }, { Domain, Range } } };
/* Signature for creating a set corresponding to the domains
* of two functions.
*/
static Signature set_join =
{ { Domain }, { { Domain, Range }, { Domain, Range } } };
/* Signatures for flattening the domain or range of a map,
* replacing it with either an anonymous tuple or a tuple with a given name.
*/
static Signature anonymize_nested_domain =
{ { Anonymous, Range2 }, { { { Domain, Range }, Range2 } } };
static Signature anonymize_nested_range =
{ { Domain, Anonymous }, { { Domain, { Range, Range2 } } } };
static Signature replace_nested_domain =
{ { Domain2, Range2 },
{ { { Domain, Range }, Range2 }, { Anonymous} } };
static Signature replace_nested_range =
{ { Domain, Range3 }, { { Domain, { Range, Range2 } }, { Anonymous} } };
static std::vector<Signature> flatten_domain =
{ anonymize_nested_domain, replace_nested_domain };
static std::vector<Signature> flatten_range =
{ anonymize_nested_range, replace_nested_range };
/* Signatures for "set_at" methods.
*/
static Signature set_at_set =
{ { Domain }, { { Domain }, { Integer }, { Anonymous } } };
static Signature set_at_map =
{ { Domain, Range },
{ { Domain, Range }, { Integer }, { Domain, Anonymous } } };
static std::vector<Signature> set_at = { set_at_set, set_at_map };
/* Signatures for "list" methods, extracting a list
* from a multi-expression.
*/
static Signature to_list_set = { { Anonymous }, { { Domain } } };
static Signature to_list_map = { { Domain, Anonymous }, { { Domain, Range } } };
/* Signatures for functions constructing an object from only an isl::ctx.
*/
static Signature ctx_params = { { }, { { Ctx } } };
static Signature ctx_set = { { Domain }, { { Ctx } } };
static Signature ctx_map = { { Domain, Range }, { { Ctx } } };
/* Helper structure for sorting the keys of static_methods and
* special_member_methods such that the larger keys appear first.
* In particular, a key should appear before any key that appears
* as a substring in the key.
* Note that this sorting is currently only important
* for special_member_methods.
*/
struct larger_infix {
bool operator()(const std::string &x, const std::string &y) const {
if (x.length() > y. length())
return true;
return x < y;
}
};
/* A map from part of a type name to a sequence of signatures.
*/
typedef std::map<std::string, std::vector<Signature>, larger_infix> infix_map;
/* A map from a method name to a map from part of a type name
* to a sequence of signatures.
*/
typedef std::map<std::string, infix_map> infix_map_map;
/* Signatures for static methods.
*
* The "unit" static method is only available in a 0-tuple space.
*
* The "empty" static method creates union objects with the relevant
* number of tuples.
*
* The "universe" static methods create objects from the corresponding spaces.
*/
static const infix_map_map static_methods {
{ "unit",
{ { "space", { ctx_params } } }
},
{ "empty",
{
{ "union_set", { ctx_params, ctx_set } },
{ "union_map", { ctx_map } },
{ "union_pw_multi_aff", { ctx_set, ctx_map } },
}
},
{ "universe",
{
{ "set", { un_params, un_set } },
{ "map", { un_map } },
}
},
};
/* Signatures for unary operations that either take something in a set space
* and return something in the same space or take something in a map space
* and return something in the range of that space.
*/
static std::vector<Signature> range_op = { un_set, range };
/* Signatures for binary operations where the second argument
* is a (multi-)value.
*/
static std::vector<Signature> bin_val = { bin_set, bin_map_range };
/* The (default) signatures for methods with a given name.
* Some of these are overridden by special_member_methods.
*/
static const std::unordered_map<std::string, std::vector<Signature>>
member_methods {
{ "add", bin_op },
{ "add_constant", bin_val },
{ "add_named_tuple", { to_set_named, add_range_named } },
{ "add_param", bin_op_anon },
{ "add_unnamed_tuple", { to_set, add_range } },
{ "apply", { set_forward, range_forward } },
{ "apply_domain", { domain_forward } },
{ "apply_range", { range_forward } },
{ "as", un_op },
{ "as_map", { un_map } },
{ "as_union_map", { un_map } },
{ "as_set", { un_set } },
{ "bind", { bind_set, bind_range } },
{ "bind_domain", { bind_domain } },
{ "bind_range", { bind_range } },
{ "bind_domain_wrapped_domain",
{ bind_domain_wrapped_domain } },
{ "ceil", fn_un_op },
{ "coalesce", un_op },
{ "cond", fn_ter_op },
{ "constant_multi_val", range_op },
{ "curry", { curry } },
{ "deltas", { transformation_domain } },
{ "detect_equalities", un_op },
{ "domain", fn_domain },
{ "domain_factor_domain",
{ domain_factor_domain } },
{ "domain_factor_range",
{ domain_factor_range } },
{ "domain_map", { domain_map } },
{ "domain_product", { domain_product } },
{ "drop", ter_int_int },
{ "eq_at", { map_cmp } },
{ "every", each },
{ "extract", bin_op },
{ "flatten_domain", flatten_domain },
{ "flatten_range", flatten_range },
{ "floor", fn_un_op },
{ "foreach", each },
{ "ge_set", { set_join } },
{ "gt_set", { set_join } },
{ "gist", bin_op },
{ "gist_domain", { bin_map_domain } },
{ "identity", { un_map, set_to_map } },
{ "identity_on_domain", { set_to_map } },
{ "indicator_function", anonymous_from_domain },
{ "insert_domain", { map_from_range_and_domain } },
{ "intersect", bin_op },
{ "intersect_params", { bin_set_params, bin_map_params } },
{ "intersect_domain", { bin_map_domain } },
{ "intersect_range", { bin_map_range } },
{ "le_set", { set_join } },
{ "lt_set", { set_join } },
{ "lex_le_at", { map_cmp } },
{ "lex_lt_at", { map_cmp } },
{ "lex_ge_at", { map_cmp } },
{ "lex_gt_at", { map_cmp } },
{ "lexmin", fn_un_op },
{ "lexmax", fn_un_op },
{ "list", { to_list_set, to_list_map } },
{ "lower_bound", fn_bin_op },
{ "map_from_set", { set_to_map } },
{ "max", min_max },
{ "max_multi_val", range_op },
{ "min", min_max },
{ "min_multi_val", range_op },
{ "mod", bin_val },
{ "on_domain", { map_from_domain_and_range } },
{ "neg", fn_un_op },
{ "offset", fn_un_op },
{ "param_on_domain", anonymous_from_domain_bin_anon },
{ "params", { set_params, map_params } },
{ "plain_multi_val_if_fixed",
{ un_set } },
{ "preimage", { set_backward } },
{ "preimage_domain", { domain_backward } },
{ "preimage_domain_wrapped_domain",
{ domain_wrapped_domain_backward } },
{ "preimage_range", { range_backward } },
{ "product", { set_product, map_product } },
{ "project_out_param", bin_op_anon },
{ "project_out_all_params",
un_op },
{ "pullback", { domain_backward, bind_domain } },
{ "range", { range } },
{ "range_factor_domain",
{ range_factor_domain } },
{ "range_factor_range", { range_factor_range } },
{ "range_lattice_tile", { un_map } },
{ "range_map", { range_map } },
{ "range_product", { range_product } },
{ "range_reverse", { map_range_reverse } },
{ "range_simple_fixed_box_hull",
{ un_map } },
{ "reverse", { map_reverse } },
{ "scale", bin_val },
{ "scale_down", bin_val },
{ "set_at", set_at },
{ "set_domain_tuple", { update_domain } },
{ "set_range_tuple", { update_set, update_range } },
{ "simple_fixed_box_hull",
{ un_set } },
{ "sub", fn_bin_op },
{ "subtract", bin_op },
{ "subtract_domain", { bin_map_domain } },
{ "subtract_range", { bin_map_range } },
{ "translation", { set_to_map } },
{ "to", un_op },
{ "unbind_params", { set_from_params } },
{ "unbind_params_insert_domain",
{ map_from_range_and_domain } },
{ "uncurry", { uncurry } },
{ "union_add", fn_bin_op },
{ "unite", bin_op },
{ "universe", un_op },
{ "unwrap", { unwrap } },
{ "upper_bound", fn_bin_op },
{ "wrap", { wrap } },
{ "zero", fn_un_op },
{ "zero_on_domain", { anonymous_map_from_domain } },
};
/* Signatures for methods of types containing a given substring
* that override the default signatures, where larger substrings
* appear first.
*
* In particular, "gist" is usually a regular binary operation,
* but for any type derived from "aff", the argument refers
* to the domain of the function.
*
* The "size" method can usually simply be inherited from
* the corresponding plain C++ type, but for a "fixed_box",
* the size lives in the space of the box or its range.
*
* The "space" method is usually a regular unary operation
* that returns the single space of the elements in the object,
* with the same number of tuples.
* However, a "union" object may contain elements from many spaces and
* therefore its space only refers to the symbolic constants and
* has zero tuples, except if it is also a "multi_union" object,
* in which case it has a fixed range space and the space of the object
* has a single tuple.
* Note that since "space' is also the name of a template class,
* the default space method is handled by print_type_named_member_method.
*/
static const infix_map_map special_member_methods {
{ "gist",
{ { "aff", { bin_set_params, bin_map_domain } } }
},
{ "size",
{ { "fixed_box", range_op } },
},
{ "space",
{
{ "multi_union", range_op },
{ "union", { un_params, set_params, map_params } },
}
},
};
/* Generic kinds for objects with zero, one or two tuples,
* the last of which may be anonymous.
*/
static Kind params{};
static Kind set_type{ Domain };
static Kind set_anon{ Anonymous };
static Kind map_type{ Domain, Range };
static Kind map_anon{ Domain, Anonymous };
/* The initial sequence of specialization kinds for base types.
* The specialization kinds for other types are derived
* from the corresponding base types.
*
* In particular, this sequence specifies how many tuples
* a given type can have and whether it is anonymous.
*
* "space" can have any number of tuples.
* "set" and "point" can have zero or one tuple.
* "map" can only have two tuples.
* "aff" can have one or two tuples, the last of which is anonymous.
* "fixed_box" can represent a (proper) set) or a map.
* "val" and "id" are treated as anonymous sets so that
* they can form the basis of "multi_val" and "multi_id".
*/
static const std::unordered_map<std::string, std::vector<Kind>> base_kinds {
{ "space", { params, set_type, map_type } },
{ "set", { params, set_type } },
{ "point", { params, set_type } },
{ "map", { map_type } },
{ "aff", { set_anon, map_anon } },
{ "fixed_box", { set_type, map_type } },
{ "val", { set_anon } },
{ "id", { set_anon } },
};
/* Prefixes introduced by type constructors.
*/
static const std::unordered_set<std::string> type_prefixes {
"basic",
"multi",
"pw",
"union",
};
/* If "type" has a "_list" suffix, then return "type" with this suffix removed.
* Otherwise, simply return "type".
*/
static std::string drop_list(const std::string &type)
{
size_t pos = type.rfind('_');
if (pos == std::string::npos)
return type;
if (type.substr(pos + 1) == "list")
return type.substr(0, pos);
return type;
}
/* Given the name of a plain C++ type, return the base type
* from which it was derived using type constructors.
*
* In particular, drop any "list" suffix and
* drop any prefixes from type_prefixes, stopping
* as soon as a base type is found for which kinds have been registered
* in base_kinds.
*/
static std::string base_type(const std::string &type)
{
auto base = type;
size_t pos;
base = drop_list(base);
while (base_kinds.count(base) == 0 &&
(pos = base.find('_')) != std::string::npos &&
type_prefixes.count(base.substr(0, pos)) != 0) {
base = base.substr(pos + 1);
}
return base;
}
/* A mapping from anonymous kinds to named kinds.
*/
static std::map<Kind, Kind> anon_to_named {
{ set_anon, set_type },
{ map_anon, map_type },
};
/* Given a sequence of anonymous kinds, replace them
* by the corresponding named kinds.
*/
static std::vector<Kind> add_name(const std::vector<Kind> &tuples)
{
std::vector<Kind> named;
for (const auto &tuple : tuples)
named.emplace_back(anon_to_named.at(tuple));
return named;
}
/* Add a template class called "name", of which the methods are described
* by "clazz" and where the corresponding base type has kinds "base_kinds".
*
* If this template class is a multi-expression, then it was derived
* from an anonymous function type. Replace the final Anonymous
* tuple kind by a placeholder in this case.
*/
void template_cpp_generator::add_template_class(const isl_class &clazz,
const std::string &name, const std::vector<Kind> &base_kinds)
{
auto isl_namespace = cpp_type_printer().isl_namespace();
auto super = isl_namespace + name;
auto class_tuples = base_kinds;
if (name.find("multi_") != std::string::npos)
class_tuples = add_name(class_tuples);
template_classes.emplace(name,
template_class{name, super, clazz, class_tuples});
}
/* Construct a templated C++ bindings generator from
* the exported types and functions and the set of all declared functions.
*
* On top of the initialization of the shared parts
* of C++ bindings generators, add a template class
* for each plain C++ class for which template kinds
* have been defined.
* In particular, determine the base type from which the plain C++ class
* was derived using type constructors and check if any template kinds
* have been registered for this base type.
*/
template_cpp_generator::template_cpp_generator(clang::SourceManager &SM,
std::set<clang::RecordDecl *> &exported_types,
std::set<clang::FunctionDecl *> exported_functions,
std::set<clang::FunctionDecl *> functions) :
cpp_generator(SM, exported_types, exported_functions,
functions)
{
for (const auto &kvp : classes) {
const auto &clazz = kvp.second;
std::string name = type2cpp(clazz);
std::string base = base_type(name);
if (base_kinds.count(base) == 0)
continue;
add_template_class(clazz, name, base_kinds.at(base));
}
}
/* Call "fn" on each template class.
*/
void template_cpp_generator::foreach_template_class(
const std::function<void(const template_class &)> &fn) const
{
for (const auto &kvp : template_classes)
fn(kvp.second);
}
/* Print forward declarations for all template classes to "os".
*
* For template classes that represent an anonymous function
* that can also have a domain tuple, provide an <name>_on alias
* that adds the fixed Anonymous tuple kind.
*/
void template_cpp_generator::print_forward_declarations(std::ostream &os)
{
foreach_template_class([&os] (const template_class &template_class) {
auto name = template_class.class_name;
os << "\n";
os << "template <typename...>\n";
os << "struct " << name << ";\n";
if (!template_class.is_anon())
return;
if (template_class.is_anon_set())
return;
os << "\n";
os << "template <typename...Ts>\n";
os << "using " << name << "_on = "
<< name << "<Ts..., Anonymous>;\n";
});
}
/* Print friend declarations for all template classes to "os".
*/
void template_cpp_generator::print_friends(std::ostream &os)
{
foreach_template_class([&os] (const template_class &template_class) {
os << " template <typename...>\n";
os << " friend struct " << template_class.class_name << ";\n";
});
}
/* Print a template parameter or argument.
* In case of a std::string, it's a template parameter
* that needs to be declared.
*/
static void print_template_arg(std::ostream &os, const std::string &arg)
{
os << "typename " << arg;
}
/* Print a template parameter or argument.
* In case of a TupleKindPtr, it's a template argument.
*/
static void print_template_arg(std::ostream &os, const TupleKindPtr &kind)
{
os << kind->to_string();
}
/* Print a sequence of template parameters (std::string) or
* arguments (TupleKindPtr) "args", without the enclosing angle brackets.
*/
template <typename List>
static void print_pure_template_args(std::ostream &os, const List &args)
{
for (size_t i = 0; i < args.size(); ++i) {
if (i != 0)
os << ", ";
print_template_arg(os, args[i]);
}
}
/* Print a sequence of template parameters (std::string) or
* arguments (TupleKindPtr) "args".
*/
template <typename List>
static void print_template_args(std::ostream &os, const List &args)
{
os << "<";
print_pure_template_args(os, args);
os << ">";
}
/* Print a declaration of the template parameters "params".
*/
static void print_template(std::ostream &os,
const std::vector<std::string> ¶ms)
{
os << "template ";
print_template_args(os, params);
os << "\n";
}
/* Print a declaration of the template parameters "params",
* if there are any.
*/
static void print_non_empty_template(std::ostream &os,
const std::vector<std::string> ¶ms)
{
if (params.size() > 0)
print_template(os, params);
}
/* Print a bare template type, i.e., without namespace,
* consisting of the type "type" and the kind "kind" to "os".
*
* In particular, print "type" followed by the template arguments
* as specified by "kind".
*/
static void print_bare_template_type(std::ostream &os, const std::string &type,
const Kind &kind)
{
os << type;
print_template_args(os, kind);
}
/* A specific instance of "template_class", with tuple kinds given by "kind".
*/
struct specialization {
struct template_class &template_class;
Kind kind;
const std::string &base_name() const;
const std::string &class_name() const;
};
/* The name of the plain C++ interface class
* from which this template class (instance) derives.
*/
const std::string &specialization::base_name() const
{
return template_class.super_name;
}
/* The name of the template class.
*/
const std::string &specialization::class_name() const
{
return template_class.class_name;
}
/* Helper class for printing the specializations of template classes
* that is used to print both the class declarations and the class definitions.
*
* "os" is the stream onto which the classes should be printed.
* "generator" is the templated C++ interface generator printing the classes.
*/
struct specialization_printer {
specialization_printer(std::ostream &os,
template_cpp_generator &generator) :
os(os), generator(generator) {}
virtual void print_class(const specialization &instance) const = 0;
void print_classes() const;
std::ostream &os;
template_cpp_generator &generator;
};
/* Print all specializations of all template classes.
*
* Each class has a predefined set of initial specializations,
* but while such a specialization is being printed,
* the need for other specializations may arise and
* these are added at the end of the list of specializations.
* That is, class_tuples.size() may change during the execution
* of the loop.
*
* For each specialization of a template class, call
* the print_class virtual method.
*/
void specialization_printer::print_classes() const
{
for (auto &kvp : generator.template_classes) {
auto &template_class = kvp.second;
const auto &class_tuples = template_class.class_tuples;
for (size_t i = 0; i < class_tuples.size(); ++i)
print_class({ template_class, class_tuples[i] });
}
}
/* A helper class for printing method declarations and definitions
* of a template class specialization.
*
* "instance" is the template class specialization for which methods
* are printed.
* "generator" is the templated C++ interface generator printing the classes.
*/
struct template_cpp_generator::class_printer :
public cpp_generator::class_printer {
class_printer(const specialization &instance,
const specialization_printer &instance_printer,
bool is_declaration);
void print_return_type(const Method &method, const Kind &kind)
const;
void print_method_template_arguments(const Signature &sig);
void print_method_header(const Method &method, const Signature &sig);
bool print_special_method(const Method &method,
const infix_map_map &special_methods);
void print_static_method(const Method &method);
void print_constructor(const Method &method);
bool is_return_kind(const Method &method, const Kind &return_kind);
void add_specialization(const Kind &kind);
bool print_matching_method(const Method &method, const Signature &sig,
const Kind &match_arg);
bool print_matching_method(const Method &method, const Signature &sig);
void print_matching_method(const Method &method,
const std::vector<Signature> &signatures);
void print_at_method(const Method &method);
bool print_special_member_method(const Method &method);
bool print_type_named_member_method(const Method &method);
bool print_member_method_with_name(const Method &method,
const std::string &name);
void print_member_method(const Method &method);
void print_any_method(const Method &method);
virtual void print_method(const Method &method) override;
virtual void print_method(const ConversionMethod &method) override;
virtual void print_method_sig(const Method &method,
const Signature &sig, bool deleted) = 0;
virtual bool want_descendent_overloads(const function_set &methods)
override;
void print_all_methods();
const specialization &instance;
template_cpp_generator &generator;
};
/* Construct a class_printer from the template class specialization
* for which methods are printed and
* the printer of the template class.
*
* The template class printer is only used to obtain the output stream and
* the templated C++ interface generator printing the classes.
*/
template_cpp_generator::class_printer::class_printer(
const specialization &instance,
const specialization_printer &instance_printer,
bool is_declaration) :
cpp_generator::class_printer(instance_printer.os,
instance.template_class.clazz, instance_printer.generator,
is_declaration),
instance(instance), generator(instance_printer.generator)
{
}
/* An abstract template type printer, where the way of obtaining
* the argument kind is specified by the subclasses.
*/
struct template_cpp_type_printer : public cpp_type_printer {
template_cpp_type_printer() {}
std::string base(const std::string &type, const Kind &kind) const;
virtual Kind kind(int arg) const = 0;
virtual std::string qualified(int arg, const std::string &cpp_type)
const override;
};
/* Print a template type consisting of the type "type" and the kind "kind",
* including the "typed::" namespace specifier.
*/
std::string template_cpp_type_printer::base(const std::string &type,
const Kind &kind) const
{
std::ostringstream ss;
ss << "typed::";
print_bare_template_type(ss, type, kind);
return ss.str();
}
/* Return the qualified form of the given C++ isl type name appearing
* in argument position "arg" (-1 for return type).
*
* isl::ctx is not templated, so if "cpp_type" is "ctx",
* then print a non-templated version.
* Otherwise, look up the kind of the argument and print
* the corresponding template type.
*/
std::string template_cpp_type_printer::qualified(int arg,
const std::string &cpp_type) const
{
if (cpp_type == "ctx")
return cpp_type_printer::qualified(arg, cpp_type);
return base(cpp_type, kind(arg));
}
/* A template type printer for printing types with a fixed kind.
*
* "fixed_kind" is the fixed kind.
*/
struct template_cpp_kind_type_printer : public template_cpp_type_printer {
template_cpp_kind_type_printer(const Kind &kind) :
template_cpp_type_printer(), fixed_kind(kind) {}
virtual Kind kind(int arg) const override;
const Kind &fixed_kind;
};
/* Return the kind of the argument at position "arg",
* where position -1 refers to the return type.
*
* Always use the fixed kind.
*/
Kind template_cpp_kind_type_printer::kind(int arg) const
{
return fixed_kind;
}
/* A template type printer for printing a method with a given signature.
*
* "sig" is the signature of the method being printed.
*/
struct template_cpp_arg_type_printer : public template_cpp_type_printer {
template_cpp_arg_type_printer(const Signature &sig) :
template_cpp_type_printer(), sig(sig) {}
virtual Kind kind(int arg) const override;
const Signature &sig;
};
/* Return the kind of the argument at position "arg",
* where position -1 refers to the return type.
*
* Look up the kind in the signature.
*/
Kind template_cpp_arg_type_printer::kind(int arg) const
{
int n_args = sig.args.size();
if (arg < 0)
return sig.ret;
if (arg >= n_args)
generator::die("argument out of bounds");
return sig.args[arg];
}
/* A template type printer for printing a method with a given signature
* as part of a template class specialization of a given kind.
*
* "class_kind" is the template class specialization kind.
*/
struct template_method_type_printer : public template_cpp_arg_type_printer {
template_method_type_printer(const Signature &sig,
const Kind &class_kind) :
template_cpp_arg_type_printer(sig),
class_kind(class_kind) {}
virtual std::string class_type(const std::string &cpp_name)
const override;
const Kind &class_kind;
};
/* Print the class type "cpp_name".
*
* Print the templated version using the template class specialization kind.
*/
std::string template_method_type_printer::class_type(
const std::string &cpp_name) const
{
return base(cpp_name, class_kind);
}
/* Print the templated return type of "method" of the kind "return_kind".
*
* Construct a type printer with "return_kind" as fixed kind and
* use it to print the return type.
*/
void template_cpp_generator::class_printer::print_return_type(
const Method &method, const Kind &return_kind) const
{
template_cpp_kind_type_printer printer(return_kind);
os << printer.return_type(method);
}
/* Remove the initial "n" elements from "v".
*/
template <typename T>
static void drop_initial(std::vector<T> &v, size_t n)
{
v.erase(v.begin(), v.begin() + n);
}
/* If a method with signature "sig" requires additional template parameters
* compared to those of the class, then print a declaration for them.
* If this->declarations is set, then this will be part of a method declaration,
* requiring extra indentation.
*
* Construct the sequence of all required template parameters
* with those of the template class appearing first.
* If this sequence has any parameters not induced by the template class itself,
* then print a declaration for these extra parameters.
*/
void template_cpp_generator::class_printer::print_method_template_arguments(
const Signature &sig)
{
std::vector<std::string> class_params, method_params;
class_params = instance.kind.params();
method_params = class_params;
combine(method_params, sig.params());
if (class_params.size() == method_params.size())
return;
drop_initial(method_params, class_params.size());
if (declarations)
os << " ";
print_template(os, method_params);
}
/* Print the header for "method" with signature "sig".
*
* First print any additional template parameters that may be required and
* then print a regular method header, using a template type printer.
*/
void template_cpp_generator::class_printer::print_method_header(
const Method &method, const Signature &sig)
{
template_method_type_printer type_printer(sig, instance.kind);
print_method_template_arguments(sig);
cpp_generator::class_printer::print_method_header(method,
type_printer);
}
/* Given a group of methods with the same name,
* should extra methods be added that take as arguments
* those types that can be converted to the original argument type
* through a unary constructor?
*
* Since type deduction does not consider implicit conversions,
* these extra methods should always be printed.
*/
bool template_cpp_generator::class_printer::want_descendent_overloads(
const function_set &methods)
{
return true;
}
/* Print all constructors and methods that forward
* to the corresponding methods in the plain C++ interface class.
*/
void template_cpp_generator::class_printer::print_all_methods()
{
print_constructors();
print_methods();
}
/* A helper class for printing method declarations
* of a template class specialization.
*/
struct template_cpp_generator::method_decl_printer :
public template_cpp_generator::class_printer {
method_decl_printer(const specialization &instance,
const struct specialization_printer &instance_printer) :
class_printer(instance, instance_printer, true) {}
virtual void print_method_sig(const Method &method,
const Signature &sig, bool deleted) override;
virtual void print_get_method(FunctionDecl *fd) override;
};
/* Print a declaration of the method "method" with signature "sig".
* Mark is "delete" if "deleted" is set.
*/
void template_cpp_generator::method_decl_printer::print_method_sig(
const Method &method, const Signature &sig, bool deleted)
{
print_method_header(method, sig);
if (deleted)
os << " = delete";
os << ";\n";
}
/* Return the total number of arguments in the signature for "method",
* taking into account a possible callback argument.
*
* In particular, if the method has a callback argument,
* then the return kind of the callback appears at the position
* of the callback and the kinds of the arguments (except
* the user pointer argument) appear in the following positions.
*/
static int total_params(const Method &method)
{
int n = method.num_params();
if (method.callback) {
auto callback_type = method.callback->getType();
auto callback = generator::extract_prototype(callback_type);
n += callback->getNumArgs() - 1;
}
return n;
}
/* Return a signature for "method" that matches "instance".
*/
static Signature instance_sig(const Method &method,
const specialization &instance)
{
std::vector<Kind> args(total_params(method));
args[0] = instance.kind;
return { instance.kind, args };
}
/* Print a declaration for the "get" method "fd",
* using a name that includes the "get_" prefix.
*
* These methods are only included in the plain interface.
* Explicitly delete them from the templated interface.
*/
void template_cpp_generator::method_decl_printer::print_get_method(
FunctionDecl *fd)
{
Method method(clazz, fd, clazz.base_method_name(fd));
print_method_sig(method, instance_sig(method, instance), true);
}
/* A helper class for printing method definitions
* of a template class specialization.
*/
struct template_cpp_generator::method_impl_printer :
public template_cpp_generator::class_printer {
method_impl_printer(const specialization &instance,
const struct specialization_printer &instance_printer) :
class_printer(instance, instance_printer, false) {}
void print_callback_method_body(const Method &method,
const Signature &sig);
void print_method_body(const Method &method, const Signature &sig);
void print_constructor_body(const Method &method, const Signature &sig);
virtual void print_method_sig(const Method &method,
const Signature &sig, bool deleted) override;
virtual void print_get_method(FunctionDecl *fd) override;
};
/* Print a definition of the constructor "method" with signature "sig".
*
* Simply pass all arguments to the constructor of the corresponding
* plain type.
*/
void template_cpp_generator::method_impl_printer::print_constructor_body(
const Method &method, const Signature &sig)
{
const auto &base_name = instance.base_name();
os << " : " << base_name;
method.print_cpp_arg_list(os, [&] (int i) {
os << method.fd->getParamDecl(i)->getName().str();
});
os << "\n";
os << "{\n";
os << "}\n";
}
/* Print the arguments of the callback function "callback" to "os",
* calling "print_arg" with the type and the name of the arguments,
* where the type is obtained from "type_printer" with argument positions
* shifted by "shift".
*/
static void print_callback_args(std::ostream &os,
const FunctionProtoType *callback, const cpp_type_printer &type_printer,
int shift,
const std::function<void(const std::string &type,
const std::string &name)> &print_arg)
{
auto n_arg = callback->getNumArgs() - 1;
Method::print_arg_list(os, 0, n_arg, [&] (int i) {
auto type = callback->getArgType(i);
auto name = "arg" + std::to_string(i);
auto cpptype = type_printer.param(shift + i, type);
print_arg(cpptype, name);
});
}
/* Print a lambda for passing to the plain method corresponding to "method"
* with signature "sig".
*
* The method is assumed to have only the callback as argument,
* which means the arguments of the callback are shifted by 2
* with respect to the arguments of the signature
* (one for the position of the callback argument plus
* one for the return kind of the callback).
*
* The lambda takes arguments with plain isl types and
* calls the callback of "method" with templated arguments.
*/
static void print_callback_lambda(std::ostream &os, const Method &method,
const Signature &sig)
{
auto callback_type = method.callback->getType();
auto callback_name = method.callback->getName().str();
auto callback = generator::extract_prototype(callback_type);
if (method.num_params() != 2)
generator::die("callback is assumed to be single argument");
os << " auto lambda = [&] ";
print_callback_args(os, callback, cpp_type_printer(), 2,
[&] (const std::string &type, const std::string &name) {
os << type << " " << name;
});
os << " {\n";
os << " return " << callback_name;
print_callback_args(os, callback, template_cpp_arg_type_printer(sig), 2,
[&] (const std::string &type, const std::string &name) {
os << type << "(" << name << ")";
});
os << ";\n";
os << " };\n";
}
/* Print a definition of the member method "method", which is known
* to have a callback argument, with signature "sig".
*
* First print a lambda for passing to the corresponding plain method and
* calling the callback of "method" with templated arguments.
* Then call the plain method, replacing the original callback
* by the lambda.
*
* The return value is assumed to be isl_bool or isl_stat
* so that no conversion to a template type is required.
*/
void template_cpp_generator::method_impl_printer::print_callback_method_body(
const Method &method, const Signature &sig)
{
const auto &base_name = instance.base_name();
auto return_type = method.fd->getReturnType();
if (!is_isl_bool(return_type) && !is_isl_stat(return_type))
die("only isl_bool and isl_stat return types are supported");
os << "{\n";
print_callback_lambda(os, method, sig);
os << " return ";
os << base_name << "::" << method.name;
method.print_cpp_arg_list(os, [&] (int i) {
auto param = method.fd->getParamDecl(i);
if (param == method.callback)
os << "lambda";
else
os << param->getName().str();
});
os << ";\n";
os << "}\n";
}
/* Print a definition of the member or static method "method"
* with signature "sig".
*
* The body calls the corresponding method of the base class
* in the plain interface and
* then casts the result to the templated result type.
*/
void template_cpp_generator::method_impl_printer::print_method_body(
const Method &method, const Signature &sig)
{
const auto &base_name = instance.base_name();
os << "{\n";
os << " auto res = ";
os << base_name << "::" << method.name;
method.print_cpp_arg_list(os, [&] (int i) {
os << method.fd->getParamDecl(i)->getName().str();
});
os << ";\n";
os << " return ";
print_return_type(method, sig.ret);
os << "(res);\n";
os << "}\n";
}
/* Print a definition of the method "method" with signature "sig",
* if "deleted" is not set.
*
* If "deleted" is set, then the corresponding declaration
* is marked "delete" and no definition needs to be printed.
*
* Otherwise print the method header, preceded by the template parameters,
* if needed.
* The body depends on whether the method is a constructor or
* takes a callback.
*/
void template_cpp_generator::method_impl_printer::print_method_sig(
const Method &method, const Signature &sig, bool deleted)
{
if (deleted)
return;
os << "\n";
print_non_empty_template(os, instance.kind.params());
print_method_header(method, sig);
os << "\n";
if (method.kind == Method::Kind::constructor)
print_constructor_body(method, sig);
else if (method.callback)
print_callback_method_body(method, sig);
else
print_method_body(method, sig);
}
/* Print a definition for the "get" method "fd" in class "clazz",
* using a name that includes the "get_" prefix, to "os".
*
* The declarations of these methods are explicitly delete'd
* so no definition needs to be printed.
*/
void template_cpp_generator::method_impl_printer::print_get_method(
FunctionDecl *fd)
{
}
/* Print a declaration or definition of the static method "method",
* if it has a signature specified by static_methods.
*/
void template_cpp_generator::class_printer::print_static_method(
const Method &method)
{
print_special_method(method, static_methods);
}
/* Signatures for constructors of multi-expressions
* from a space and a list.
*/
static Signature from_list_set = { { Domain }, { { Domain }, { Anonymous } } };
static Signature from_list_map =
{ { Domain, Range }, { { Domain, Range }, { Domain, Anonymous } } };
/* Signatures for constructors from a string.
*/
static Signature params_from_str = { { }, { { Ctx }, { Str } } };
static Signature set_from_str = { { Domain }, { { Ctx }, { Str } } };
static Signature map_from_str = { { Domain, Range }, { { Ctx }, { Str } } };
static std::vector<Signature> from_str =
{ params_from_str, set_from_str, map_from_str };
/* Signature for a constructor from an integer.
*/
static Signature int_from_si = { { Anonymous }, { { Ctx }, { Integer } } };
/* Signatures for constructors of lists from the initial number
* of elements.
*/
static Signature alloc_params = { { }, { { Ctx }, { Integer } } };
static Signature alloc_set = { { Domain }, { { Ctx }, { Integer } } };
static Signature alloc_map = { { Domain, Range }, { { Ctx }, { Integer } } };
/* Signatures for constructors and methods named after some other class.
*
* Two forms of constructors are handled
* - conversion from another object
* - construction of a multi-expression from a space and a list
*
* Methods named after some other class also come in two forms
* - extraction of information such as the space or a list
* - construction of a multi-expression from a space and a list
*
* In both cases, the first form is a unary operation and
* the second has an extra argument with a kind that is equal
* to that of the first argument, except that the final tuple is anonymous.
*/
static std::vector<Signature> constructor_sig = {
un_params,
un_set,
un_map,
from_list_set,
from_list_map,
};
/* Signatures for constructors derived from methods
* with the given names that override the default signatures.
*/
static const std::unordered_map<std::string, std::vector<Signature>>
special_constructors {
{ "alloc", { alloc_params, alloc_set, alloc_map } },
{ "int_from_si", { int_from_si } },
{ "read_from_str", from_str },
};
/* Print a declaration or definition of the constructor "method".
*/
void template_cpp_generator::class_printer::print_constructor(
const Method &method)
{
if (special_constructors.count(method.name) != 0) {
const auto &sigs = special_constructors.at(method.name);
return print_matching_method(method, sigs);
}
print_matching_method(method, constructor_sig);
}
/* Does this template class represent an anonymous function?
*
* If any specialization represents an anonymous function,
* then every specialization does, so simply check
* the first specialization.
*/
bool template_class::is_anon() const
{
return class_tuples[0].is_anon();
}
/* Does this template class represent an anonymous value?
*
* That is, is there only a single specialization that moreover
* has a single, anonymous tuple?
*/
bool template_class::is_anon_set() const
{
return class_tuples.size() == 1 && class_tuples[0].is_anon_set();
}
/* Update the substitution "sub" to map "general" to "specific"
* if "specific" is a special case of "general" consistent with "sub",
* given that "general" is not a pair and can be assigned "specific".
* Return true if successful.
* Otherwise, return false.
*
* Check whether "general" is already assigned something in "sub".
* If so, it must be assigned "specific".
* Otherwise, there is a conflict.
*/
static bool update_sub_base(Substitution &sub, const TupleKindPtr &general,
const TupleKindPtr &specific)
{
auto name = general->name;
if (sub.count(name) != 0 && sub.at(name) != specific)
return false;
sub.emplace(name, specific);
return true;
}
/* Update the substitution "sub" to map "general" to "specific"
* if "specific" is a special case of "general" consistent with "sub".
* Return true if successful.
* Otherwise, return false.
*
* If "general" is a pair and "specific" is not,
* then "specific" cannot be a special case.
* If both are pairs, then update the substitution based
* on both sides.
* If "general" is Anonymous, then "specific" must be Anonymous as well.
* If "general" is Leaf, then "specific" cannot be a pair.
*
* Otherwise, assign "specific" to "general", if possible.
*/
static bool update_sub(Substitution &sub, const TupleKindPtr &general,
const TupleKindPtr &specific)
{
if (general->left() && !specific->left())
return false;
if (general->left())
return update_sub(sub, general->left(), specific->left()) &&
update_sub(sub, general->right(), specific->right());
if (general == Anonymous && specific != Anonymous)
return false;
if (general == Leaf && specific->left())
return false;
return update_sub_base(sub, general, specific);
}
/* Check if "specific" is a special case of "general" and,
* if so, return true along with a substitution
* that maps "general" to "specific".
* Otherwise return false.
*
* This can only happen if the number of tuple kinds is the same.
* If so, start with an empty substitution and update it
* for each pair of tuple kinds, checking that each update succeeds.
*/
static std::pair<bool, Substitution> specializer(const Kind &general,
const Kind &specific)
{
Substitution specializer;
if (general.size() != specific.size())
return { false, Substitution() };
for (size_t i = 0; i < general.size(); ++i) {
auto general_tuple = general[i];
if (!update_sub(specializer, general[i], specific[i]))
return { false, Substitution() };
}
return { true, specializer };
}
/* Is "kind1" equivalent to "kind2"?
* That is, is each a special case of the other?
*/
static bool equivalent(const Kind &kind1, const Kind &kind2)
{
return specializer(kind1, kind2).first &&
specializer(kind2, kind1).first;
}
/* Add the specialization "kind" to the sequence of specializations,
* provided there is no equivalent specialization already in there.
*/
void template_class::add_specialization(const Kind &kind)
{
for (const auto &special : class_tuples)
if (equivalent(special, kind))
return;
class_tuples.emplace_back(kind);
}
/* A type printer that prints the plain interface type,
* without namespace.
*/
struct plain_cpp_type_printer : public cpp_type_printer {
plain_cpp_type_printer() {}
virtual std::string qualified(int arg, const std::string &cpp_type)
const override;
};
/* Return the qualified form of the given C++ isl type name appearing
* in argument position "arg" (-1 for return type).
*
* For printing the plain type without namespace, no modifications
* are required.
*/
std::string plain_cpp_type_printer::qualified(int arg,
const std::string &cpp_type) const
{
return cpp_type;
}
/* Return a string representation of the plain type "type".
*
* For the plain printer, the argument position is irrelevant,
* so simply pass in -1.
*/
static std::string plain_type(QualType type)
{
return plain_cpp_type_printer().param(-1, type);
}
/* Return a string representation of the plain return type of "method".
*/
static std::string plain_return_type(const Method &method)
{
return plain_type(method.fd->getReturnType());
}
/* Return that part of the signature "sig" that should match
* the template class specialization for the given method.
*
* In particular, if the method is a regular member method,
* then the instance should match the first argument.
* Otherwise, it should match the return kind.
*/
static const Kind &matching_kind(const Method &method, const Signature &sig)
{
if (method.kind == Method::Kind::member_method)
return sig.args[0];
else
return sig.ret;
}
/* Is it possible for "template_class" to have the given kind?
*
* If the template class represents an anonymous function,
* then so must the given kind.
* There should also be specialization with the same number of tuple kinds.
*/
static bool has_kind(const template_class &template_class, const Kind &kind)
{
if (template_class.is_anon() && !kind.is_anon())
return false;
for (const auto &class_tuple : template_class.class_tuples)
if (class_tuple.size() == kind.size())
return true;
return false;
}
/* Is "return_kind" a possible kind for the return type of "method"?
*
* If the return type is not a template class,
* then "return_kind" should not have any template parameters.
* Otherwise, "return_kind" should be a valid kind for the template class.
*/
bool template_cpp_generator::class_printer::is_return_kind(
const Method &method, const Kind &return_kind)
{
const auto &template_classes = generator.template_classes;
auto return_type = plain_return_type(method);
if (template_classes.count(return_type) == 0)
return return_kind.params().size() == 0;
return has_kind(template_classes.at(return_type), return_kind);
}
/* Is "kind" a placeholder that can be assigned something else
* in a substitution?
*
* Anonymous can only be mapped to itself. This is taken care of
* by assign().
* Leaf can only be assigned a placeholder, but there is no need
* to handle this specifically since Leaf can still be assigned
* to the placeholder.
*/
static bool assignable(const TupleKindPtr &kind)
{
return kind != Anonymous && kind != Leaf;
}
/* Return a substitution that maps "kind1" to "kind2", if possible.
* Otherwise return an empty substitution.
*
* Check if "kind1" can be assigned anything or
* if "kind1" and "kind2" are identical.
* The latter case handles mapping Anonymous to itself.
*/
static Substitution assign(const TupleKindPtr &kind1, const TupleKindPtr &kind2)
{
Substitution res;
if (assignable(kind1) || kind1 == kind2)
res.emplace(kind1->name, kind2);
return res;
}
/* Return a substitution that first applies "first" and then "second".
*
* The result consists of "second" and of "second" applied to "first".
*/
static Substitution compose(const Substitution &first,
const Substitution &second)
{
Substitution res = second;
for (const auto &kvp : first)
res.emplace(kvp.first, apply(kvp.second, second));
return res;
}
static Substitution compute_unifier(const TupleKindPtr &kind1,
const TupleKindPtr &kind2);
/* Try and extend "unifier" with a unifier for "kind1" and "kind2".
* Return the resulting unifier if successful.
* Otherwise, return an empty substitution.
*
* First apply "unifier" to "kind1" and "kind2".
* Then compute a unifier for the resulting tuple kinds and
* combine it with "unifier".
*/
static Substitution combine_unifiers(const TupleKindPtr &kind1,
const TupleKindPtr &kind2, const Substitution &unifier)
{
auto k1 = apply(kind1, unifier);
auto k2 = apply(kind2, unifier);
auto u = compute_unifier(k1, k2);
if (u.size() == 0)
return Substitution();
return compose(unifier, u);
}
/* Try and compute a unifier of "kind1" and "kind2",
* i.e., a substitution that produces the same result when
* applied to both "kind1" and "kind2",
* for the case where both "kind1" and "kind2" are pairs.
* Return this unifier if it was found.
* Return an empty substitution if no unifier can be found.
*
* First compute a unifier for the left parts of the pairs and,
* if successful, combine it with a unifier for the right parts.
*/
static Substitution compute_pair_unifier(const TupleKindPtr &kind1,
const TupleKindPtr &kind2)
{
auto unifier_left = compute_unifier(kind1->left(), kind2->left());
if (unifier_left.size() == 0)
return Substitution();
return combine_unifiers(kind1->right(), kind2->right(), unifier_left);
}
/* Try and compute a unifier of "kind1" and "kind2",
* i.e., a substitution that produces the same result when
* applied to both "kind1" and "kind2".
* Return this unifier if it was found.
* Return an empty substitution if no unifier can be found.
*
* If one of the tuple kinds is a pair then assign it
* to the other tuple kind, if possible.
* If neither is a pair, then try and assign one to the other.
* Otherwise, let compute_pair_unifier compute a unifier.
*
* Note that an assignment is added to the unifier even
* if "kind1" and "kind2" are identical.
* This ensures that a successful substitution is never empty.
*/
static Substitution compute_unifier(const TupleKindPtr &kind1,
const TupleKindPtr &kind2)
{
if (kind1->left() && !kind2->left())
return assign(kind2, kind1);
if (!kind1->left() && kind2->left())
return assign(kind1, kind2);
if (!kind1->left() && !kind2->left()) {
if (assignable(kind1))
return assign(kind1, kind2);
else
return assign(kind2, kind1);
}
return compute_pair_unifier(kind1, kind2);
}
/* Try and compute a unifier of "kind1" and "kind2",
* i.e., a substitution that produces the same result when
* applied to both "kind1" and "kind2".
* Return this unifier if it was found.
* Return an empty substitution if no unifier can be found.
*
* Start with an empty substitution and compute a unifier for
* each pair of tuple kinds, combining the results.
* If no combined unifier can be found or
* if the numbers of tuple kinds are different, then return
* an empty substitution.
* This assumes that the number of tuples is greater than zero,
* as otherwise an empty substitution would be returned as well.
*/
static Substitution compute_unifier(const Kind &kind1, const Kind &kind2)
{
Substitution unifier;
if (kind1.size() != kind2.size())
return Substitution();
for (size_t i = 0; i < kind1.size(); ++i)
unifier = combine_unifiers(kind1[i], kind2[i], unifier);
return unifier;
}
/* Try and construct a Kind that is a specialization of both "general" and
* "specific", where "specific" is known _not_ to be a specialization
* of "general" and not to contain any Leaf.
*
* First check whether "general" is a specialization of "specific".
* If so, simply return "general".
* Otherwise, rename the placeholders in the two kinds apart and
* try and compute a unifier.
* If this succeeds, then return the result of applying the unifier.
*/
static std::pair<bool, Kind> unify(const Kind &general, const Kind &specific)
{
if (specializer(specific, general).first) {
return { true, general };
} else {
auto rename = param_renamer(specific.params(), "T");
auto renamed = specific.apply(rename);
auto unifier = compute_unifier(general, renamed);
if (unifier.size() == 0)
return { false, { } };
return { true, general.apply(unifier) };
}
}
/* Try and add a template class specialization corresponding to "kind".
* The new specialization needs to be a specialization of both
* the current specialization and "kind".
*
* The current template class specialization is known not to be a special case
* of "kind".
*
* Try and unify the two kinds and, if this succeeds, add the result
* to this list of template class specializations.
*/
void template_cpp_generator::class_printer::add_specialization(
const Kind &kind)
{
auto maybe_unified = unify(kind, instance.kind);
if (!maybe_unified.first)
return;
instance.template_class.add_specialization(maybe_unified.second);
}
/* Print a declaration or definition of the method "method"
* if the template class specialization matches "match_arg".
* Return true if so.
* "sig" is the complete signature, of which "match_arg" refers
* to the first argument or the return type.
*
* Since "sig" may have parameters with the same names as
* those in instance.kind, rename them apart first.
*
* If the template class specialization is a special case of
* (the renamed) "match_arg"
* then apply the specializer to the complete (renamed) signature,
* check that the return kind is allowed and, if so,
* print the declaration or definition using the specialized signature.
*
* If the template class specialization is not a special case of "match_arg"
* then add a further specialization to the list of specializations
* of the template class.
*/
bool template_cpp_generator::class_printer::print_matching_method(
const Method &method, const Signature &sig, const Kind &match_arg)
{
auto rename = shared_param_renamer(sig, instance.kind);
auto renamed_arg = match_arg.apply(rename);
auto maybe_specializer = specializer(renamed_arg, instance.kind);
if (maybe_specializer.first) {
const auto &specializer = maybe_specializer.second;
auto specialized_sig = sig.apply(rename).apply(specializer);
if (!is_return_kind(method, specialized_sig.ret))
return false;
print_method_sig(method, specialized_sig, false);
} else {
add_specialization(match_arg);
}
return maybe_specializer.first;
}
/* Is the first argument of "method" of type "isl_ctx *"?
*/
static bool first_arg_is_ctx(const Method &method)
{
return generator::first_arg_is_isl_ctx(method.fd);
}
/* Is the first signature argument set to { Ctx }?
*/
static bool first_kind_is_ctx(const Signature &sig)
{
return sig.args[0].size() > 0 && sig.args[0][0] == Ctx;
}
/* Print a declaration or definition of the member method "method"
* if it matches the signature "sig".
* Return true if so.
*
* First determine the part of the signature that needs to match
* the template class specialization and
* check that it has the same number of template arguments.
* Also check that the number of arguments of the signature
* matches that of the method.
* If there is at least one argument, then check that the first method argument
* is an isl_ctx if and only if the first signature argument is Ctx.
*
* If these tests succeed, proceed with the actual matching.
*/
bool template_cpp_generator::class_printer::print_matching_method(
const Method &method, const Signature &sig)
{
auto match_arg = matching_kind(method, sig);
int n_args = sig.args.size();
if (match_arg.size() != instance.kind.size())
return false;
if (n_args != total_params(method))
return false;
if (n_args > 0 && first_arg_is_ctx(method) != first_kind_is_ctx(sig))
return false;
return print_matching_method(method, sig, match_arg);
}
/* Print a declaration or definition of the member method "method"
* for each matching signature in "signatures".
*
* If there is no matching signature in "signatures",
* then explicitly delete the method (using a signature based on
* the specialization) so that it is not inherited from the base class.
*/
void template_cpp_generator::class_printer::print_matching_method(
const Method &method, const std::vector<Signature> &signatures)
{
auto any = false;
for (const auto &sig : signatures)
if (print_matching_method(method, sig))
any = true;
if (!any)
print_method_sig(method, instance_sig(method, instance), true);
}
/* Signatures for "at" methods applied to a multi-expression,
* which make the final tuple anonymous.
*/
static Signature select_set = { { Anonymous }, { { Domain }, { Integer } } };
static Signature select_map =
{ { Domain, Anonymous }, { { Domain, Range }, { Integer } } };
static std::vector<Signature> at_select = { select_set, select_map };
/* Signatures for other "at" methods applied to a list,
* which do not modify the tuple kind.
*/
static Signature bin_set_int = { { Domain }, { { Domain }, { Integer } } };
static Signature bin_map_int =
{ { Domain, Range }, { { Domain, Range }, { Integer } } };
static std::vector<Signature> at_keep = { bin_set_int, bin_map_int };
/* Print a declaration or definition of the "at" member method "method".
*
* There are two types of methods called "at".
* One type extracts an element from a multi-expression and
* the other extracts an element from a list.
*
* In the first case, the return type is an anonymous function
* while the object type is not. In this case, the return kind
* should have a final Anonymous tuple.
* Otherwise, the return kind should be the same as the object kind.
*/
void template_cpp_generator::class_printer::print_at_method(
const Method &method)
{
auto anon = instance.template_class.is_anon();
auto return_type = plain_return_type(method);
auto return_class = generator.template_classes.at(return_type);
if (!anon && return_class.is_anon())
return print_matching_method(method, at_select);
else
return print_matching_method(method, at_keep);
}
/* Does the string "s" contain "sub" as a substring?
*/
static bool contains(const std::string &s, const std::string &sub)
{
return s.find(sub) != std::string::npos;
}
/* Print a declaration or definition of the member method "method",
* if it has a special signature in "special_methods".
* Return true if this is the case.
*
* Check if any special signatures are specified for this method and
* if the class name matches any of those with special signatures.
* If so, pick the one with the best match, i.e., the first match
* since the largest keys appear first.
*/
bool template_cpp_generator::class_printer::print_special_method(
const Method &method, const infix_map_map &special_methods)
{
if (special_methods.count(method.name) == 0)
return false;
for (const auto &kvp : special_methods.at(method.name)) {
if (!contains(instance.template_class.class_name, kvp.first))
continue;
print_matching_method(method, kvp.second);
return true;
}
return false;
}
/* Print a declaration or definition of the member method "method",
* if it has a special signature specified by special_member_methods.
* Return true if this is the case.
*/
bool template_cpp_generator::class_printer::print_special_member_method(
const Method &method)
{
return print_special_method(method, special_member_methods);
}
/* Print a declaration or definition of the member method "method",
* if it is named after a template class. Return true if this is the case.
*/
bool template_cpp_generator::class_printer::print_type_named_member_method(
const Method &method)
{
if (generator.template_classes.count(method.name) == 0)
return false;
print_matching_method(method, constructor_sig);
return true;
}
/* Print a declaration or definition of the member method "method"
* using a signature associated to method name "name", if there is any.
* Return true if this is the case.
*/
bool template_cpp_generator::class_printer::print_member_method_with_name(
const Method &method, const std::string &name)
{
if (member_methods.count(name) == 0)
return false;
print_matching_method(method, member_methods.at(name));
return true;
}
/* If "sub" appears inside "str", then remove the first occurrence and
* return the result. Otherwise, simply return "str".
*/
static std::string drop_occurrence(const std::string &str,
const std::string &sub)
{
auto res = str;
auto pos = str.find(sub);
if (pos != std::string::npos)
res.erase(pos, sub.length());
return res;
}
/* If "sub" appears in "str" next to an underscore, then remove the combination.
* Otherwise, simply return "str".
*/
static std::string drop_underscore_occurrence(const std::string &str,
const std::string &sub)
{
auto res = drop_occurrence(str, sub + "_");
if (res != str)
return res;
return drop_occurrence(res, std::string("_") + sub);
}
/* Return the name of "method", with the name of the return type,
* along with an underscore, removed, if this combination appears in the name.
* Otherwise, simply return the name.
*/
const std::string name_without_return(const Method &method)
{
auto return_infix = plain_return_type(method);
return drop_underscore_occurrence(method.name, return_infix);
}
/* If this method has a callback, then remove the type
* of the first argument of the callback from the name of the method.
* Otherwise, simply return the name of the method.
*/
const std::string callback_name(const Method &method)
{
if (!method.callback)
return method.name;
auto type = method.callback->getType();
auto callback = cpp_generator::extract_prototype(type);
auto arg_type = plain_type(callback->getArgType(0));
return generator::drop_suffix(method.name, "_" + arg_type);
}
/* Print a declaration or definition of the member method "method".
*
* If the method is called "at", then it requires special treatment.
* Otherwise, check if the signature is overridden for this class or
* if the method is named after some other type.
* Otherwise look for an appropriate signature using different variations
* of the method name. First try the method name itself,
* then the method name with the return type removed and
* finally the method name with the callback argument type removed.
*/
void template_cpp_generator::class_printer::print_member_method(
const Method &method)
{
if (method.name == "at")
return print_at_method(method);
if (print_special_member_method(method))
return;
if (print_type_named_member_method(method))
return;
if (print_member_method_with_name(method, method.name))
return;
if (print_member_method_with_name(method, name_without_return(method)))
return;
if (print_member_method_with_name(method, callback_name(method)))
return;
}
/* Print a declaration or definition of "method" based on its type.
*/
void template_cpp_generator::class_printer::print_any_method(
const Method &method)
{
switch (method.kind) {
case Method::Kind::static_method:
print_static_method(method);
break;
case Method::Kind::constructor:
print_constructor(method);
break;
case Method::Kind::member_method:
print_member_method(method);
break;
}
}
/* Print a declaration or definition of "method".
*
* Mark the method as not requiring copies of the arguments.
*/
void template_cpp_generator::class_printer::print_method(const Method &method)
{
print_any_method(NoCopyMethod(method));
}
/* Print a declaration or definition of "method".
*
* Note that a ConversionMethod is already marked
* as not requiring copies of the arguments.
*/
void template_cpp_generator::class_printer::print_method(
const ConversionMethod &method)
{
print_any_method(method);
}
/* Helper class for printing the declarations for
* template class specializations.
*/
struct template_cpp_generator::class_decl_printer :
public specialization_printer
{
class_decl_printer(std::ostream &os,
template_cpp_generator &generator) :
specialization_printer(os, generator) {}
void print_arg_subclass_constructor(const specialization &instance,
const std::vector<std::string> ¶ms) const;
void print_super_constructor(const specialization &instance) const;
virtual void print_class(const specialization &instance) const override;
};
/* Print the declaration and definition of a constructor
* for the template class specialization "instance" taking
* an instance with more specialized template arguments,
* where "params" holds the template parameters of "instance".
* It is assumed that there is at least one template parameter as otherwise
* there are no template arguments to be specialized and
* no constructor needs to be printed.
*
* In particular, the constructor takes an object of the same instance where
* for each template parameter, the corresponding template argument
* of the input object is a subclass of the template argument
* of the constructed object.
*
* Pick fresh names for all template parameters and
* add a constructor with these fresh names as extra template parameters and
* a constraint requiring that each of them is a subclass
* of the corresponding class template parameter.
* The plain C++ interface object of the constructed object is initialized with
* the plain C++ interface object of the constructor argument.
*/
void template_cpp_generator::class_decl_printer::print_arg_subclass_constructor(
const specialization &instance,
const std::vector<std::string> ¶ms) const
{
const auto &class_name = instance.class_name();
auto rename = param_renamer(params, "Arg");
auto derived = instance.kind.apply(rename);
os << " template ";
os << "<";
print_pure_template_args(os, derived.params());
os << ",\n";
os << " typename std::enable_if<\n";
for (size_t i = 0; i < params.size(); ++i) {
if (i != 0)
os << " &&\n";
os << " std::is_base_of<"
<< params[i] << ", "
<< rename.at(params[i])->params()[0] << ">{}";
}
os << ",\n";
os << " bool>::type = true>";
os << "\n";
os << " " << class_name << "(const ";
print_bare_template_type(os, class_name, derived);
os << " &obj) : " << instance.base_name() << "(obj) {}\n";
}
/* Print the declaration and definition of a constructor
* for the template class specialization "instance" taking
* an instance of the base class.
*
* If the instance kind is that of an anonymous set
* (i.e., it has a single tuple that is set to Anonymous),
* then allow the constructor to be called externally.
* This is mostly useful for being able to use isl::val and
* isl::typed::val<Anonymous> interchangeably and similarly for isl::id.
*
* If the instance is of any other kind, then make this constructor private
* to avoid objects of the plain interface being converted automatically.
* Also make sure that it does not apply to any type derived
* from the base class. In particular, this makes sure it does
* not apply to any other specializations of this template class as
* otherwise any conflict in specializations would simply point
* to the private constructor.
*
* A factory method is added to be able to perform the conversion explicitly,
* with an explicit specification of the template arguments.
*/
void template_cpp_generator::class_decl_printer::print_super_constructor(
const specialization &instance) const
{
bool hide = !instance.kind.is_anon_set();
const auto &base_name = instance.base_name();
const auto &arg_name = hide ? "base" : base_name;
if (hide) {
os << " private:\n";
os << " template <typename base,\n";
os << " typename std::enable_if<\n";
os << " std::is_same<base, " << base_name
<< ">{}, bool>::type = true>\n";
}
os << " " << instance.class_name()
<< "(const " << arg_name << " &obj) : "
<< base_name << "(obj) {}\n";
if (hide)
os << " public:\n";
os << " static " << instance.class_name() << " from"
<< "(const " << base_name << " &obj) {\n";
os << " return " << instance.class_name() << "(obj);\n";
os << " }\n";
}
/* Print a "declaration" for the given template class specialization.
* In particular, print the class definition and the method declarations.
*
* The template parameters are the distinct variable names
* in the instance kind.
*
* Each instance of the template class derives from the corresponding
* plain C++ interface class.
*
* All (other) template classes are made friends of this template class
* to allow them to call the private constructor taking an object
* of the plain interface.
*
* Besides the constructors and methods that forward
* to the corresponding methods in the plain C++ interface class,
* some extra constructors are defined.
* The default zero-argument constructor is useful for declaring
* a variable that only gets assigned a value at a later stage.
* The constructor taking an instance with more specialized
* template arguments is useful for lifting the class hierarchy
* of the template arguments to the template class.
* The constructor taking an instance of the base class
* is useful for (explicitly) constructing a template type
* from a plain type.
*/
void template_cpp_generator::class_decl_printer::print_class(
const specialization &instance) const
{
const auto &class_name = instance.class_name();
auto params = instance.kind.params();
os << "\n";
print_template(os, params);
os << "struct ";
print_bare_template_type(os, class_name, instance.kind);
os << " : public " << instance.base_name() << " {\n";
generator.print_friends(os);
os << "\n";
os << " " << class_name << "() = default;\n";
if (params.size() != 0)
print_arg_subclass_constructor(instance, params);
print_super_constructor(instance);
method_decl_printer(instance, *this).print_all_methods();
os << "};\n";
}
/* Helper class for printing the definitions of template class specializations.
*/
struct template_cpp_generator::class_impl_printer :
public specialization_printer
{
class_impl_printer(std::ostream &os,
template_cpp_generator &generator) :
specialization_printer(os, generator) {}
virtual void print_class(const specialization &instance) const override;
};
/* Print a definition for the given template class specialization.
*
* In particular, print definitions
* for the constructors and methods that forward
* to the corresponding methods in the plain C++ interface class.
* The extra constructors declared in the class definition
* are defined inline.
*/
void template_cpp_generator::class_impl_printer::print_class(
const specialization &instance) const
{
method_impl_printer(instance, *this).print_all_methods();
}
/* Generate a templated cpp interface
* based on the extracted types and functions.
*
* First print forward declarations for all template classes,
* then the declarations of the classes, and at the end all
* method implementations.
*/
void template_cpp_generator::generate()
{
ostream &os = std::cout;
os << "\n";
print_forward_declarations(os);
class_decl_printer(os, *this).print_classes();
class_impl_printer(os, *this).print_classes();
}
|