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 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016
|
#include "llvm/ADT/STLExtras.h"
#include "swift/AST/ASTMangler.h"
#include "swift/Basic/Defer.h"
#include "swift/Sema/IDETypeChecking.h"
#include <swift/APIDigester/ModuleAnalyzerNodes.h>
#include <algorithm>
using namespace swift;
using namespace ide;
using namespace api;
namespace fs = llvm::sys::fs;
namespace path = llvm::sys::path;
namespace {
static PrintOptions getTypePrintOpts(CheckerOptions CheckerOpts) {
PrintOptions Opts;
Opts.SynthesizeSugarOnTypes = true;
Opts.UseOriginallyDefinedInModuleNames = true;
Opts.PrintInverseRequirements = true; // Only inverses are relevant for ABI stability
if (!CheckerOpts.Migrator) {
// We should always print fully qualified type names for checking either
// API or ABI stability.
Opts.FullyQualifiedTypes = true;
}
return Opts;
}
} // End of anonymous namespace.
struct swift::ide::api::SDKNodeInitInfo {
SDKContext &Ctx;
DeclKind DKind;
AccessorKind AccKind;
SourceLoc Loc;
#define KEY_STRING(X, Y) StringRef X;
#include "swift/IDE/DigesterEnums.def"
#define KEY_BOOL(X, Y) bool X = false;
#include "swift/IDE/DigesterEnums.def"
#define KEY_UINT(X, Y) std::optional<uint8_t> X;
#include "swift/IDE/DigesterEnums.def"
#define KEY_STRING_ARR(X, Y) std::vector<StringRef> X;
#include "swift/IDE/DigesterEnums.def"
swift::ReferenceOwnership ReferenceOwnership = ReferenceOwnership::Strong;
std::vector<DeclAttrKind> DeclAttrs;
std::vector<TypeAttrKind> TypeAttrs;
SDKNodeInitInfo(SDKContext &Ctx) : Ctx(Ctx) {}
SDKNodeInitInfo(SDKContext &Ctx, Decl *D);
SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD);
SDKNodeInitInfo(SDKContext &Ctx, OperatorDecl *D);
SDKNodeInitInfo(SDKContext &Ctx, ImportDecl *ID);
SDKNodeInitInfo(SDKContext &Ctx, ProtocolConformanceRef Conform);
SDKNodeInitInfo(SDKContext &Ctx, Type Ty, TypeInitInfo Info = TypeInitInfo());
SDKNode* createSDKNode(SDKNodeKind Kind);
};
bool swift::ide::api::hasValidParentPtr(SDKNodeKind kind) {
switch(kind) {
case SDKNodeKind::Conformance:
case SDKNodeKind::DeclAccessor:
return false;
default:
return true;
}
}
SDKContext::SDKContext(CheckerOptions Opts): Diags(SourceMgr), Opts(Opts) {}
DiagnosticEngine &SDKContext::getDiags(SourceLoc Loc) {
// If the location is invalid, we just use the locally created DiagEngine.
if (Loc.isInvalid())
return Diags;
// If the Loc is valid, it may belong to any of the SourceManagers owned by
// the ASTContexts we created, thus we should go through the ASTContxts to find
// the right DiagnosticEngine to use.
for (auto &CI: CIs) {
if (CI->getSourceMgr().isOwning(Loc))
return CI->getDiags();
}
llvm_unreachable("cannot find diagnostic engine to use");
}
void SDKContext::addDiagConsumer(DiagnosticConsumer &Consumer) {
// we may emit diagnostics via any of the diagnostic engine, so add the consumer
// to all of them.
Diags.addConsumer(Consumer);
for (auto &CI: CIs) {
CI->getDiags().addConsumer(Consumer);
}
}
void SDKNodeRoot::registerDescendant(SDKNode *D) {
// Operator doesn't have usr
if (isa<SDKNodeDeclOperator>(D))
return;
// Import doesn't have usr
if (isa<SDKNodeDeclImport>(D))
return;
if (auto DD = dyn_cast<SDKNodeDecl>(D)) {
assert(!DD->getUsr().empty());
DescendantDeclTable[DD->getUsr()].insert(DD);
}
}
SDKNode::SDKNode(SDKNodeInitInfo Info, SDKNodeKind Kind): Ctx(Info.Ctx),
Name(Info.Name), PrintedName(Info.PrintedName), TheKind(unsigned(Kind)) {}
SDKNodeRoot::SDKNodeRoot(SDKNodeInitInfo Info): SDKNode(Info, SDKNodeKind::Root),
ToolArgs(Info.ToolArgs),
JsonFormatVer(Info.JsonFormatVer.has_value() ? *Info.JsonFormatVer : DIGESTER_JSON_DEFAULT_VERSION) {}
SDKNodeDecl::SDKNodeDecl(SDKNodeInitInfo Info, SDKNodeKind Kind)
: SDKNode(Info, Kind), DKind(Info.DKind), Usr(Info.Usr),
MangledName(Info.MangledName), Loc(Info.Loc),
Location(Info.Location), ModuleName(Info.ModuleName),
DeclAttributes(Info.DeclAttrs),
SPIGroups(Info.SPIGroups),
IsImplicit(Info.IsImplicit),
IsStatic(Info.IsStatic), IsDeprecated(Info.IsDeprecated),
IsProtocolReq(Info.IsProtocolReq),
IsOverriding(Info.IsOverriding),
IsOpen(Info.IsOpen),
IsInternal(Info.IsInternal), IsABIPlaceholder(Info.IsABIPlaceholder),
IsFromExtension(Info.IsFromExtension),
ReferenceOwnership(uint8_t(Info.ReferenceOwnership)),
GenericSig(Info.GenericSig),
SugaredGenericSig(Info.SugaredGenericSig),
FixedBinaryOrder(Info.FixedBinaryOrder),
introVersions({Info.IntromacOS, Info.IntroiOS, Info.IntrotvOS,
Info.IntrowatchOS, Info.Introswift}),
ObjCName(Info.ObjCName) {}
SDKNodeType::SDKNodeType(SDKNodeInitInfo Info, SDKNodeKind Kind):
SDKNode(Info, Kind), TypeAttributes(Info.TypeAttrs),
HasDefaultArg(Info.HasDefaultArg),
ParamValueOwnership(Info.ParamValueOwnership) {}
SDKNodeTypeNominal::SDKNodeTypeNominal(SDKNodeInitInfo Info):
SDKNodeType(Info, SDKNodeKind::TypeNominal), USR(Info.Usr),
MangledName(Info.MangledName) {}
SDKNodeTypeFunc::SDKNodeTypeFunc(SDKNodeInitInfo Info):
SDKNodeType(Info, SDKNodeKind::TypeFunc) {}
SDKNodeTypeAlias::SDKNodeTypeAlias(SDKNodeInitInfo Info):
SDKNodeType(Info, SDKNodeKind::TypeAlias) {}
SDKNodeDeclType::SDKNodeDeclType(SDKNodeInitInfo Info):
SDKNodeDecl(Info, SDKNodeKind::DeclType), SuperclassUsr(Info.SuperclassUsr),
SuperclassNames(Info.SuperclassNames),
EnumRawTypeName(Info.EnumRawTypeName),
IsExternal(Info.IsExternal),
IsEnumExhaustive(Info.IsEnumExhaustive),
HasMissingDesignatedInitializers(Info.HasMissingDesignatedInitializers),
InheritsConvenienceInitializers(Info.InheritsConvenienceInitializers) {}
SDKNodeConformance::SDKNodeConformance(SDKNodeInitInfo Info):
SDKNode(Info, SDKNodeKind::Conformance),
Usr(Info.Usr), MangledName(Info.MangledName),
IsABIPlaceholder(Info.IsABIPlaceholder) {}
SDKNodeTypeWitness::SDKNodeTypeWitness(SDKNodeInitInfo Info):
SDKNode(Info, SDKNodeKind::TypeWitness) {}
SDKNodeDeclOperator::SDKNodeDeclOperator(SDKNodeInitInfo Info):
SDKNodeDecl(Info, SDKNodeKind::DeclOperator) {}
SDKNodeDeclTypeAlias::SDKNodeDeclTypeAlias(SDKNodeInitInfo Info):
SDKNodeDecl(Info, SDKNodeKind::DeclTypeAlias) {}
SDKNodeDeclVar::SDKNodeDeclVar(SDKNodeInitInfo Info):
SDKNodeDecl(Info, SDKNodeKind::DeclVar), IsLet(Info.IsLet),
HasStorage(Info.HasStorage) {}
SDKNodeDeclMacro::SDKNodeDeclMacro(SDKNodeInitInfo Info):
SDKNodeDecl(Info, SDKNodeKind::DeclMacro) {}
SDKNodeDeclAbstractFunc::SDKNodeDeclAbstractFunc(SDKNodeInitInfo Info,
SDKNodeKind Kind): SDKNodeDecl(Info, Kind), IsThrowing(Info.IsThrowing),
ReqNewWitnessTableEntry(Info.ReqNewWitnessTableEntry),
SelfIndex(Info.SelfIndex) {}
SDKNodeDeclFunction::SDKNodeDeclFunction(SDKNodeInitInfo Info):
SDKNodeDeclAbstractFunc(Info, SDKNodeKind::DeclFunction),
FuncSelfKind(Info.FuncSelfKind) {}
SDKNodeDeclConstructor::SDKNodeDeclConstructor(SDKNodeInitInfo Info):
SDKNodeDeclAbstractFunc(Info, SDKNodeKind::DeclConstructor), InitKind(Info.InitKind) {}
SDKNodeDeclAccessor::SDKNodeDeclAccessor(SDKNodeInitInfo Info):
SDKNodeDeclAbstractFunc(Info, SDKNodeKind::DeclAccessor),
AccKind(Info.AccKind) {}
SDKNodeDeclImport::SDKNodeDeclImport(SDKNodeInitInfo Info):
SDKNodeDecl(Info, SDKNodeKind::DeclImport) {}
SDKNodeDeclAssociatedType::SDKNodeDeclAssociatedType(SDKNodeInitInfo Info):
SDKNodeDecl(Info, SDKNodeKind::DeclAssociatedType) {}
SDKNodeDeclSubscript::SDKNodeDeclSubscript(SDKNodeInitInfo Info):
SDKNodeDeclAbstractFunc(Info, SDKNodeKind::DeclSubscript),
HasStorage(Info.HasStorage) {}
StringRef SDKNodeDecl::getHeaderName() const {
if (Location.empty())
return StringRef();
return llvm::sys::path::filename(Location.split(":").first);
}
static SDKNodeDeclAccessor *getAccessorInternal(ArrayRef<SDKNode*> Accessors,
AccessorKind Kind) {
for (auto *AC: Accessors) {
if (cast<SDKNodeDeclAccessor>(AC)->getAccessorKind() == Kind) {
return cast<SDKNodeDeclAccessor>(AC);
}
}
return nullptr;
}
SDKNodeDeclAccessor *SDKNodeDeclVar::getAccessor(AccessorKind Kind) const {
return getAccessorInternal(Accessors, Kind);
}
SDKNodeDeclAccessor *SDKNodeDeclSubscript::getAccessor(AccessorKind Kind) const {
return getAccessorInternal(Accessors, Kind);
}
SDKNodeType *SDKNodeDeclVar::getType() const {
return cast<SDKNodeType>(childAt(0));
}
SDKNodeType *SDKNodeDeclMacro::getType() const {
return cast<SDKNodeType>(childAt(0));
}
NodePtr UpdatedNodesMap::findUpdateCounterpart(const SDKNode *Node) const {
assert(Node->isAnnotatedAs(NodeAnnotation::Updated) && "Not update operation.");
auto FoundPair = std::find_if(MapImpl.begin(), MapImpl.end(),
[&](std::pair<NodePtr, NodePtr> Pair) {
return Pair.second == Node || Pair.first == Node;
});
assert(FoundPair != MapImpl.end() && "Cannot find update counterpart.");
return Node == FoundPair->first ? FoundPair->second : FoundPair->first;
}
#define NODE_KIND_RANGE(ID, FIRST, LAST) \
bool SDKNode##ID::classof(const SDKNode *N) { \
return N->getKind() >= SDKNodeKind::FIRST && \
N->getKind() <= SDKNodeKind::LAST; \
}
#include "swift/IDE/DigesterEnums.def"
unsigned SDKNode::getChildIndex(const SDKNode* Child) const {
auto It = std::find(Children.begin(), Children.end(), Child);
assert(It != Children.end() && "cannot find the child");
return It - Children.begin();
}
SDKNode* SDKNode::getOnlyChild() const {
assert(Children.size() == 1 && "more that one child.");
return *Children.begin();
}
SDKNodeRoot *SDKNode::getRootNode() const {
for (auto *Root = const_cast<SDKNode*>(this); Root;) {
if (auto Result = dyn_cast<SDKNodeRoot>(Root))
return Result;
if (auto *Conf = dyn_cast<SDKNodeConformance>(Root)) {
Root = Conf->getNominalTypeDecl();
} else if (auto *Acc = dyn_cast<SDKNodeDeclAccessor>(Root)) {
Root = Acc->getStorage();
} else {
Root = Root->getParent();
}
}
llvm_unreachable("Unhandled SDKNodeKind in switch.");
}
uint8_t SDKNode::getJsonFormatVersion() const {
return getRootNode()->getJsonFormatVersion();
}
void SDKNode::addChild(SDKNode *Child) {
Child->Parent = this;
Children.push_back(Child);
if (auto *Root = dyn_cast<SDKNodeRoot>(this)) {
struct DeclCollector: public SDKNodeVisitor {
SDKNodeRoot &Root;
DeclCollector(SDKNodeRoot &Root): Root(Root) {}
void visit(NodePtr Node) override {
Root.registerDescendant(Node);
}
} Collector(*Root);
SDKNode::preorderVisit(Child, Collector);
}
}
NodePtr SDKNode::childAt(unsigned I) const {
assert(I < getChildrenCount());
return getChildren()[I];
}
void SDKNode::removeChild(NodePtr C) {
Children.erase(std::find(Children.begin(), Children.end(), C));
}
void SDKNode::annotate(NodeAnnotation Anno, StringRef Comment) {
assert(!Comment.empty());
if(isAnnotatedAs(Anno))
return;
annotate(Anno);
AnnotateComments[Anno] = Comment;
}
void SDKNode::removeAnnotate(NodeAnnotation Anno) {
assert(isAnnotatedAs(Anno));
Annotations.erase(Anno);
AnnotateComments.erase(Anno);
assert(!isAnnotatedAs(Anno));
assert(AnnotateComments.count(Anno) == 0);
}
StringRef SDKNode::getAnnotateComment(NodeAnnotation Anno) const {
return AnnotateComments.find(Anno)->second;
}
ArrayRef<NodeAnnotation> SDKNode::
getAnnotations(std::vector<NodeAnnotation> &Scratch) const {
for (auto Ann : Annotations)
Scratch.push_back(Ann);
return llvm::ArrayRef(Scratch);
}
bool SDKNode::isAnnotatedAs(NodeAnnotation Anno) const {
return Annotations.find(Anno) != Annotations.end();;
}
void SDKNode::preorderVisit(NodePtr Root, SDKNodeVisitor &Visitor) {
Visitor.visit(Root);
Visitor.Ancestors.push_back(Root);
for (auto Child : Root->Children)
preorderVisit(Child, Visitor);
Visitor.Ancestors.pop_back();
}
void SDKNode::postorderVisit(NodePtr Root, SDKNodeVisitor &Visitor) {
Visitor.Ancestors.push_back(Root);
for (auto Child : Root->Children)
postorderVisit(Child, Visitor);
Visitor.Ancestors.pop_back();
Visitor.visit(Root);
}
SDKNodeVectorViewer::VectorIt
SDKNodeVectorViewer::getNext(VectorIt Start) {
for (auto It = Start; It != Collection.end(); ++ It)
if (Selector(*It))
return It;
return Collection.end();
}
SDKNodeVectorViewer::ViewerIterator&
SDKNodeVectorViewer::ViewerIterator::operator++() {
P = Viewer.getNext(P + 1);
return *this;
}
SDKNodeVectorViewer::ViewerIterator SDKNodeVectorViewer::begin() {
return ViewerIterator(*this, getNext(Collection.begin()));
}
SDKNodeVectorViewer::ViewerIterator SDKNodeVectorViewer::end() {
return ViewerIterator(*this, Collection.end());
}
KnownTypeKind SDKNodeType::getTypeKind() const {
#define KNOWN_TYPE(NAME) if (getName() == #NAME) return KnownTypeKind::NAME;
#include "swift/IDE/DigesterEnums.def"
return KnownTypeKind::Unknown;
}
ArrayRef<TypeAttrKind> SDKNodeType::getTypeAttributes() const {
return llvm::ArrayRef(TypeAttributes.data(), TypeAttributes.size());
}
void SDKNodeType::addTypeAttribute(TypeAttrKind AttrKind) {
TypeAttributes.push_back(AttrKind);
}
bool SDKNodeType::hasTypeAttribute(TypeAttrKind DAKind) const {
return std::find(TypeAttributes.begin(), TypeAttributes.end(), DAKind) !=
TypeAttributes.end();
}
StringRef SDKNodeType::getParamValueOwnership() const {
return ParamValueOwnership.empty() ? "Default" : ParamValueOwnership;
}
StringRef SDKNodeType::getTypeRoleDescription() const {
assert(isTopLevelType());
auto P = getParent();
switch(P->getKind()) {
case SDKNodeKind::Root:
case SDKNodeKind::TypeNominal:
case SDKNodeKind::TypeFunc:
case SDKNodeKind::TypeAlias:
case SDKNodeKind::DeclType:
case SDKNodeKind::DeclOperator:
case SDKNodeKind::Conformance:
case SDKNodeKind::DeclImport:
llvm_unreachable("Type Parent is wrong");
case SDKNodeKind::DeclFunction:
case SDKNodeKind::DeclConstructor:
case SDKNodeKind::DeclAccessor:
case SDKNodeKind::DeclSubscript:
return SDKNodeDeclAbstractFunc::getTypeRoleDescription(Ctx,
P->getChildIndex(this));
case SDKNodeKind::DeclVar:
case SDKNodeKind::DeclMacro:
return "declared";
case SDKNodeKind::DeclTypeAlias:
return "underlying";
case SDKNodeKind::DeclAssociatedType:
return "default";
case SDKNodeKind::TypeWitness:
return "type witness type";
}
llvm_unreachable("Unhandled SDKNodeKind in switch");
}
SDKNode *SDKNodeRoot::getInstance(SDKContext &Ctx, ArrayRef<ModuleDecl*> modules) {
SDKNodeInitInfo Info(Ctx);
StringRef name = Ctx.buffer("NO_MODULE");
if (modules.size() == 1) {
// use the module name
name = Ctx.buffer(modules[0]->getName().str());
} else if (!modules.empty()) {
name = Ctx.buffer("MULTI_MODULES");
}
Info.Name = name;
Info.PrintedName = name;
llvm::transform(Ctx.getOpts().ToolArgs, std::back_inserter(Info.ToolArgs),
[&](std::string s) { return Ctx.buffer(s); });
Info.JsonFormatVer = DIGESTER_JSON_VERSION;
return Info.createSDKNode(SDKNodeKind::Root);
}
StringRef SDKNodeDecl::getScreenInfo() const {
auto ModuleName = getModuleName();
auto HeaderName = getHeaderName();
auto &Ctx = getSDKContext();
llvm::SmallString<64> SS;
llvm::raw_svector_ostream OS(SS);
if (Ctx.getOpts().CompilerStyle) {
// Compiler style we don't need source info
OS << (Ctx.checkingABI() ? "ABI breakage" : "API breakage");
} else {
// Print more source info.
if (Ctx.getOpts().PrintModule)
OS << ModuleName;
if (!HeaderName.empty())
OS << "(" << HeaderName << ")";
}
if (!OS.str().empty())
OS << ": ";
bool IsExtension = false;
if (auto *TD = dyn_cast<SDKNodeDeclType>(this)) {
IsExtension = TD->isExternal();
}
// There is no particular reasons why we don't use lower-cased keyword names
// in non-CompilerStyle mode. This is to be backward compatible so clients
// don't need to update existing known breakages.
OS << getDeclKindStr(IsExtension? DeclKind::Extension : getDeclKind(),
getSDKContext().getOpts().CompilerStyle);
OS << " " << getFullyQualifiedName();
return Ctx.buffer(OS.str());
}
void SDKNodeDecl::printFullyQualifiedName(llvm::raw_ostream &OS) const {
if (auto *ACC = dyn_cast<SDKNodeDeclAccessor>(this)) {
ACC->getStorage()->printFullyQualifiedName(OS);
OS << "." << getPrintedName();
return;
}
std::vector<NodePtr> Parent;
for (auto *P = getParent(); isa<SDKNodeDecl>(P); P = P->getParent())
Parent.push_back(P);
for (auto It = Parent.rbegin(); It != Parent.rend(); ++ It)
OS << (*It)->getPrintedName() << ".";
OS << getPrintedName();
}
StringRef SDKNodeDecl::getFullyQualifiedName() const {
llvm::SmallString<32> Buffer;
llvm::raw_svector_ostream OS(Buffer);
printFullyQualifiedName(OS);
return getSDKContext().buffer(OS.str());
}
bool SDKNodeDecl::isNonOptionalProtocolRequirement() const {
return isProtocolRequirement() && !hasDeclAttribute(DeclAttrKind::Optional);
}
bool SDKNodeDecl::hasDeclAttribute(DeclAttrKind DAKind) const {
return std::find(DeclAttributes.begin(), DeclAttributes.end(), DAKind) !=
DeclAttributes.end();
}
ArrayRef<DeclAttrKind> SDKNodeDecl::getDeclAttributes() const {
return llvm::ArrayRef(DeclAttributes.data(), DeclAttributes.size());
}
bool SDKNodeDecl::hasAttributeChange(const SDKNodeDecl &Another) const {
std::set<DeclAttrKind> Left(getDeclAttributes().begin(),
getDeclAttributes().end());
std::set<DeclAttrKind> Right(Another.getDeclAttributes().begin(),
Another.getDeclAttributes().end());
return Left != Right;
}
bool SDKNodeType::hasAttributeChange(const SDKNodeType &Another) const {
std::set<TypeAttrKind> Left(getTypeAttributes().begin(),
getTypeAttributes().end());
std::set<TypeAttrKind> Right(Another.getTypeAttributes().begin(),
Another.getTypeAttributes().end());
return Left != Right;
}
SDKNodeDecl *SDKNodeType::getClosestParentDecl() const {
auto *Result = getParent();
for (; !isa<SDKNodeDecl>(Result); Result = Result->getParent());
return Result->getAs<SDKNodeDecl>();
}
void SDKNodeDeclType::addConformance(SDKNode *Conf) {
cast<SDKNodeConformance>(Conf)->TypeDecl = this;
Conformances.push_back(Conf);
}
void SDKNodeDeclSubscript::addAccessor(SDKNode *AC) {
cast<SDKNodeDeclAccessor>(AC)->Owner = this;
Accessors.push_back(AC);
}
void SDKNodeDeclVar::addAccessor(SDKNode *AC) {
cast<SDKNodeDeclAccessor>(AC)->Owner = this;
Accessors.push_back(AC);
}
SDKNodeType *SDKNodeTypeWitness::getUnderlyingType() const {
return getOnlyChild()->getAs<SDKNodeType>();
}
StringRef SDKNodeTypeWitness::getWitnessedTypeName() const {
return Ctx.buffer((llvm::Twine(getParent()->getAs<SDKNodeConformance>()->
getName()) + "." + getName()).str());
}
std::optional<SDKNodeDeclType *> SDKNodeDeclType::getSuperclass() const {
if (SuperclassUsr.empty())
return std::nullopt;
auto Descendants = getRootNode()->getDescendantsByUsr(SuperclassUsr);
if (!Descendants.empty()) {
return Descendants.front()->getAs<SDKNodeDeclType>();
}
return std::nullopt;
}
/// Finding the node through all children, including the inherited ones,
/// whose printed name matches with the given name.
std::optional<SDKNodeDecl *>
SDKNodeDeclType::lookupChildByPrintedName(StringRef Name) const {
for (auto C : getChildren()) {
if (C->getPrintedName() == Name)
return C->getAs<SDKNodeDecl>();
}
// Finding from the inheritance chain.
if (auto Super = getSuperclass()) {
return (*Super)->lookupChildByPrintedName(Name);
}
return std::nullopt;
}
SDKNodeType *SDKNodeDeclType::getRawValueType() const {
if (isConformingTo(KnownProtocolKind::RawRepresentable)) {
if (auto RV = lookupChildByPrintedName("rawValue")) {
return (*(*RV)->getChildBegin())->getAs<SDKNodeType>();
}
}
return nullptr;
}
bool SDKNodeDeclType::isConformingTo(swift::ide::api::KnownProtocolKind Kind) const {
switch (Kind) {
#define KNOWN_PROTOCOL(NAME) \
case KnownProtocolKind::NAME: \
return std::find_if(Conformances.begin(), Conformances.end(), \
[](SDKNode *Conf) { return Conf->getName() == #NAME; }) != \
Conformances.end();
#include "swift/IDE/DigesterEnums.def"
}
llvm_unreachable("covered switch");
}
StringRef SDKNodeDeclAbstractFunc::getTypeRoleDescription(SDKContext &Ctx,
unsigned Index) {
if (Index == 0) {
return Ctx.buffer("return");
} else {
llvm::SmallString<4> Buffer;
Buffer += "parameter ";
Buffer += std::to_string(Index - 1);
return Ctx.buffer(Buffer.str());
}
}
#define NODE_KIND(X, NAME) \
bool SDKNode##X::classof(const SDKNode *N) { \
return N->getKind() == SDKNodeKind::X; \
}
#include "swift/IDE/DigesterEnums.def"
static std::optional<KeyKind> parseKeyKind(StringRef Content) {
return llvm::StringSwitch<std::optional<KeyKind>>(Content)
#define KEY(NAME) .Case(#NAME, KeyKind::KK_##NAME)
#include "swift/IDE/DigesterEnums.def"
.Default(std::nullopt);
}
static StringRef getKeyContent(SDKContext &Ctx, KeyKind Kind) {
switch (Kind) {
#define KEY(NAME) case KeyKind::KK_##NAME: return Ctx.buffer(#NAME);
#include "swift/IDE/DigesterEnums.def"
}
llvm_unreachable("Unhandled KeyKind in switch.");
}
SDKNode* SDKNode::constructSDKNode(SDKContext &Ctx,
llvm::yaml::MappingNode *Node) {
static auto GetScalarString = [&](llvm::yaml::Node *N) -> StringRef {
SmallString<64> Buffer;
return Ctx.buffer(cast<llvm::yaml::ScalarNode>(N)->getValue(Buffer));
};
static auto getAsInt = [&](llvm::yaml::Node *N) -> int {
return std::stoi(cast<llvm::yaml::ScalarNode>(N)->getRawValue().str());
};
static auto getAsBool = [&](llvm::yaml::Node *N) -> bool {
auto txt = cast<llvm::yaml::ScalarNode>(N)->getRawValue();
assert(txt.starts_with("false") || txt.starts_with("true"));
return txt.starts_with("true");
};
SDKNodeKind Kind;
SDKNodeInitInfo Info(Ctx);
NodeVector Children;
NodeVector Conformances;
NodeVector Accessors;
SDKNode *Result = nullptr;
for (auto &Pair : *Node) {
auto keyString = GetScalarString(Pair.getKey());
if (auto keyKind = parseKeyKind(keyString)) {
switch(*keyKind) {
case KeyKind::KK_kind:
if (auto parsedKind = parseSDKNodeKind(GetScalarString(Pair.getValue()))) {
Kind = *parsedKind;
} else {
Ctx.diagnose(Pair.getValue(), diag::sdk_node_unrecognized_node_kind,
GetScalarString(Pair.getValue()));
}
break;
#define KEY_UINT(X, Y) \
case KeyKind::KK_##Y: Info.X = getAsInt(Pair.getValue()); break;
#include "swift/IDE/DigesterEnums.def"
#define KEY_STRING(X, Y) \
case KeyKind::KK_##Y: Info.X = GetScalarString(Pair.getValue()); break;
#include "swift/IDE/DigesterEnums.def"
#define KEY_BOOL(X, Y) case KeyKind::KK_##Y: Info.X = getAsBool(Pair.getValue()); break;
#include "swift/IDE/DigesterEnums.def"
case KeyKind::KK_children:
for (auto &Mapping : *cast<llvm::yaml::SequenceNode>(Pair.getValue())) {
Children.push_back(constructSDKNode(Ctx,
cast<llvm::yaml::MappingNode>(&Mapping)));
}
break;
case KeyKind::KK_conformances:
for (auto &Mapping : *cast<llvm::yaml::SequenceNode>(Pair.getValue())) {
Conformances.push_back(constructSDKNode(Ctx,
cast<llvm::yaml::MappingNode>(&Mapping)));
}
break;
#define KEY_STRING_ARR(X, Y) \
case KeyKind::KK_##Y: \
assert(Info.X.empty()); \
for (auto &Name : *cast<llvm::yaml::SequenceNode>(Pair.getValue())) { \
Info.X.push_back(GetScalarString(&Name)); \
} \
break;
#include "swift/IDE/DigesterEnums.def"
case KeyKind::KK_ownership:
Info.ReferenceOwnership =
swift::ReferenceOwnership(getAsInt(Pair.getValue()));
assert(Info.ReferenceOwnership != swift::ReferenceOwnership::Strong &&
"Strong is implied.");
break;
case KeyKind::KK_typeAttributes: {
auto *Seq = cast<llvm::yaml::SequenceNode>(Pair.getValue());
for (auto &N : *Seq) {
auto Result = llvm::StringSwitch<std::optional<TypeAttrKind>>(
GetScalarString(&N))
#define TYPE_ATTR(X, C) .Case(#X, TypeAttrKind::C)
#include "swift/AST/TypeAttr.def"
.Default(std::nullopt);
if (!Result)
Ctx.diagnose(&N, diag::sdk_node_unrecognized_type_attr_kind,
GetScalarString(&N));
else
Info.TypeAttrs.push_back(*Result);
}
break;
}
case KeyKind::KK_declAttributes: {
auto *Seq = cast<llvm::yaml::SequenceNode>(Pair.getValue());
for (auto &N : *Seq) {
auto Result = llvm::StringSwitch<std::optional<DeclAttrKind>>(
GetScalarString(&N))
#define DECL_ATTR(_, NAME, ...) .Case(#NAME, DeclAttrKind::NAME)
#include "swift/AST/DeclAttr.def"
.Default(std::nullopt);
if (!Result)
Ctx.diagnose(&N, diag::sdk_node_unrecognized_decl_attr_kind,
GetScalarString(&N));
else
Info.DeclAttrs.push_back(*Result);
}
break;
}
case KeyKind::KK_accessors: {
for (auto &Mapping : *cast<llvm::yaml::SequenceNode>(Pair.getValue())) {
Accessors.push_back(constructSDKNode(Ctx,
cast<llvm::yaml::MappingNode>(&Mapping)));
}
break;
}
case KeyKind::KK_accessorKind: {
AccessorKind unknownKind = (AccessorKind)((uint8_t)(AccessorKind::Last) + 1);
Info.AccKind = llvm::StringSwitch<AccessorKind>(
GetScalarString(Pair.getValue()))
#define ACCESSOR(ID)
#define SINGLETON_ACCESSOR(ID, KEYWORD) .Case(#KEYWORD, AccessorKind::ID)
#include "swift/AST/AccessorKinds.def"
.Default(unknownKind);
if (Info.AccKind == unknownKind) {
Ctx.diagnose(Pair.getValue(), diag::sdk_node_unrecognized_accessor_kind,
GetScalarString(Pair.getValue()));
}
break;
}
case KeyKind::KK_declKind: {
auto dKind = llvm::StringSwitch<std::optional<DeclKind>>(
GetScalarString(Pair.getValue()))
#define DECL(X, PARENT) .Case(#X, DeclKind::X)
#include "swift/AST/DeclNodes.def"
.Default(std::nullopt);
if (dKind)
Info.DKind = *dKind;
else
Ctx.diagnose(Pair.getValue(), diag::sdk_node_unrecognized_decl_kind,
GetScalarString(Pair.getValue()));
break;
}
}
} else if (keyString == ABIRootKey) {
Result = constructSDKNode(Ctx,
cast<llvm::yaml::MappingNode>(Pair.getValue()));
} else if (keyString == ConstValuesKey) {
// We don't need to consume the const values from the compiler-side
Pair.skip();
} else {
Ctx.diagnose(Pair.getKey(), diag::sdk_node_unrecognized_key,
keyString);
Pair.skip();
}
};
if (Result)
return Result;
Result = Info.createSDKNode(Kind);
for (auto C : Children) {
Result->addChild(C);
}
for (auto *Conf: Conformances) {
cast<SDKNodeDeclType>(Result)->addConformance(Conf);
}
for (auto *Acc: Accessors) {
if (auto *SD = dyn_cast<SDKNodeDeclSubscript>(Result)) {
SD->addAccessor(Acc);
} else if (auto *VD = dyn_cast<SDKNodeDeclVar>(Result)) {
VD->addAccessor(Acc);
}
}
return Result;
}
bool SDKNode::hasSameChildren(const SDKNode &Other) const {
if (Children.size() != Other.Children.size())
return false;
for (unsigned I = 0; I < Children.size(); ++ I) {
if (*Children[I] != *Other.Children[I])
return false;
}
return true;
}
void swift::ide::api::nodeSetDifference(ArrayRef<SDKNode*> Left,
ArrayRef<SDKNode*> Right,
NodeVector &LeftMinusRight,
NodeVector &RightMinusLeft) {
llvm::SmallPtrSet<NodePtr, 16> LeftToRemove;
llvm::SmallPtrSet<NodePtr, 16> RightToRemove;
for (auto LC: Left) {
for (auto RC: Right) {
if (!RightToRemove.count(RC) && *LC == *RC) {
LeftToRemove.insert(LC);
RightToRemove.insert(RC);
break;
}
}
}
std::for_each(Left.begin(), Left.end(), [&] (SDKNode *N) {
if (!LeftToRemove.count(N))
LeftMinusRight.push_back(N);
});
std::for_each(Right.begin(), Right.end(), [&] (SDKNode *N) {
if (!RightToRemove.count(N))
RightMinusLeft.push_back(N);
});
}
static bool hasSameContents(ArrayRef<SDKNode*> Left,
ArrayRef<SDKNode*> Right) {
NodeVector LeftMinusRight, RightMinusLeft;
nodeSetDifference(Left, Right, LeftMinusRight, RightMinusLeft);
return LeftMinusRight.empty() && RightMinusLeft.empty();
}
static bool hasSameParameterFlags(const SDKNodeType *Left, const SDKNodeType *Right) {
if (Left->hasDefaultArgument() != Right->hasDefaultArgument())
return false;
if (Left->getParamValueOwnership() != Right->getParamValueOwnership())
return false;
return true;
}
// Return whether a decl has been moved in/out to an extension
static std::optional<bool> isFromExtensionChanged(const SDKNode &L,
const SDKNode &R) {
assert(L.getKind() == R.getKind());
// Version 8 starts to include whether a decl is from an extension.
if (L.getJsonFormatVersion() + R.getJsonFormatVersion() < 2 * 8) {
return std::nullopt;
}
auto *Left = dyn_cast<SDKNodeDecl>(&L);
auto *Right = dyn_cast<SDKNodeDecl>(&R);
if (!Left) {
return std::nullopt;
}
if (Left->isFromExtension() == Right->isFromExtension()) {
return std::nullopt;
} else {
return Right->isFromExtension();
}
}
static bool isSDKNodeEqual(SDKContext &Ctx, const SDKNode &L, const SDKNode &R) {
auto *LeftAlias = dyn_cast<SDKNodeTypeAlias>(&L);
auto *RightAlias = dyn_cast<SDKNodeTypeAlias>(&R);
if (LeftAlias || RightAlias) {
auto Left = L.getAs<SDKNodeType>();
auto Right = R.getAs<SDKNodeType>();
// First compare the parameter attributes.
if (!hasSameParameterFlags(Left, Right))
return false;
// Comparing the underlying types if any of the inputs are alias.
Left = LeftAlias ? LeftAlias->getUnderlyingType() : Left;
Right = RightAlias ? RightAlias->getUnderlyingType() : Right;
return *Left == *Right;
}
if (L.getKind() != R.getKind())
return false;
switch(L.getKind()) {
case SDKNodeKind::TypeAlias:
llvm_unreachable("Should be handled above.");
case SDKNodeKind::TypeNominal:
case SDKNodeKind::TypeFunc: {
auto Left = L.getAs<SDKNodeType>();
auto Right = R.getAs<SDKNodeType>();
if (Left->hasAttributeChange(*Right))
return false;
if (!hasSameParameterFlags(Left, Right))
return false;
if (Left->getPrintedName() == Right->getPrintedName())
return true;
if (Ctx.checkingABI()) {
// For abi checking where we don't have sugar types at all, the printed
// name difference is enough to indicate these two types differ.
return false;
} else {
return Left->getName() == Right->getName() &&
Left->hasSameChildren(*Right);
}
}
case SDKNodeKind::DeclFunction: {
auto Left = L.getAs<SDKNodeDeclFunction>();
auto Right = R.getAs<SDKNodeDeclFunction>();
if (Left->getSelfAccessKind() != Right->getSelfAccessKind())
return false;
LLVM_FALLTHROUGH;
}
case SDKNodeKind::DeclConstructor: {
auto Left = L.getAs<SDKNodeDeclAbstractFunc>();
auto Right = R.getAs<SDKNodeDeclAbstractFunc>();
if (Left->isThrowing() ^ Right->isThrowing())
return false;
if (Left->reqNewWitnessTableEntry() != Right->reqNewWitnessTableEntry())
return false;
LLVM_FALLTHROUGH;
}
case SDKNodeKind::DeclAccessor: {
if (auto *LA = dyn_cast<SDKNodeDeclAccessor>(&L)) {
if (auto *RA = dyn_cast<SDKNodeDeclAccessor>(&R)) {
if (LA->getAccessorKind() != RA->getAccessorKind())
return false;
}
}
LLVM_FALLTHROUGH;
}
case SDKNodeKind::DeclVar: {
if (auto *LV = dyn_cast<SDKNodeDeclVar>(&L)) {
if (auto *RV = dyn_cast<SDKNodeDeclVar>(&R)) {
if (Ctx.checkingABI()) {
if (LV->hasStorage() != RV->hasStorage())
return false;
}
if (!hasSameContents(LV->getAllAccessors(), RV->getAllAccessors()))
return false;
}
}
LLVM_FALLTHROUGH;
}
case SDKNodeKind::DeclType: {
auto *Left = dyn_cast<SDKNodeDeclType>(&L);
auto *Right = dyn_cast<SDKNodeDeclType>(&R);
if (Left && Right) {
if (!hasSameContents(Left->getConformances(), Right->getConformances())) {
return false;
}
if (Left->getSuperClassName() != Right->getSuperClassName()) {
return false;
}
if (Left->getDeclKind() != Right->getDeclKind()) {
return false;
}
}
LLVM_FALLTHROUGH;
}
case SDKNodeKind::DeclAssociatedType:
case SDKNodeKind::DeclSubscript: {
if (auto *Left = dyn_cast<SDKNodeDeclSubscript>(&L)) {
if (auto *Right = dyn_cast<SDKNodeDeclSubscript>(&R)) {
if (!hasSameContents(Left->getAllAccessors(), Right->getAllAccessors()))
return false;
}
}
LLVM_FALLTHROUGH;
}
case SDKNodeKind::DeclOperator:
case SDKNodeKind::DeclTypeAlias: {
auto Left = L.getAs<SDKNodeDecl>();
auto Right = R.getAs<SDKNodeDecl>();
if (Left->isStatic() ^ Right->isStatic())
return false;
if (Left->getReferenceOwnership() != Right->getReferenceOwnership())
return false;
if (Left->hasAttributeChange(*Right))
return false;
if (Left->getGenericSignature() != Right->getGenericSignature())
return false;
if (Left->isOpen() != Right->isOpen())
return false;
if (Left->isInternal() != Right->isInternal())
return false;
if (Left->getObjCName() != Right->getObjCName())
return false;
if (Left->hasFixedBinaryOrder() != Right->hasFixedBinaryOrder())
return false;
if (Left->hasFixedBinaryOrder()) {
if (Left->getFixedBinaryOrder() != Right->getFixedBinaryOrder())
return false;
}
if (Left->getUsr() != Right->getUsr())
return false;
LLVM_FALLTHROUGH;
}
case SDKNodeKind::DeclMacro:
case SDKNodeKind::Conformance:
case SDKNodeKind::TypeWitness:
case SDKNodeKind::DeclImport:
case SDKNodeKind::Root: {
if (isFromExtensionChanged(L, R))
return false;
return L.getPrintedName() == R.getPrintedName() &&
L.hasSameChildren(R);
}
}
llvm_unreachable("Unhandled SDKNodeKind in switch.");
}
bool SDKContext::isEqual(const SDKNode &Left, const SDKNode &Right) {
return isSDKNodeEqual(*this, Left, Right);
}
AccessLevel SDKContext::getAccessLevel(const ValueDecl *VD) const {
return checkingABI() ? VD->getEffectiveAccess() : VD->getFormalAccess();
}
bool SDKNode::operator==(const SDKNode &Other) const {
return Ctx.isEqual(*this, Other);
}
// The pretty printer of a tree of SDKNode
class SDKNodeDumpVisitor : public SDKNodeVisitor {
void dumpSpace(int Num) {
while (Num != 0) {
llvm::outs() << "\t";
Num --;
}
}
void visit(NodePtr Node) override {
dumpSpace(depth());
llvm::outs() << "[" << Node->getKind() << "]" << Node->getName() << "\n";
}
public:
SDKNodeDumpVisitor() {};
};
static StringRef getPrintedName(SDKContext &Ctx, Type Ty,
bool IsImplicitlyUnwrappedOptional = false) {
std::string S;
llvm::raw_string_ostream OS(S);
PrintOptions PO = getTypePrintOpts(Ctx.getOpts());
PO.SkipAttributes = true;
if (IsImplicitlyUnwrappedOptional)
PO.PrintOptionalAsImplicitlyUnwrapped = true;
Ty.print(OS, PO);
return Ctx.buffer(OS.str());
}
static StringRef getTypeName(SDKContext &Ctx, Type Ty,
bool IsImplicitlyUnwrappedOptional) {
if (Ty->getKind() == TypeKind::Paren) {
return Ctx.buffer("Paren");
}
if (Ty->isVoid()) {
return Ctx.buffer("Void");
}
if (auto *NAT = dyn_cast<TypeAliasType>(Ty.getPointer())) {
return NAT->getDecl()->getNameStr();
}
if (auto existential = Ty->getAs<ExistentialType>()) {
return getTypeName(Ctx, existential->getConstraintType(),
IsImplicitlyUnwrappedOptional);
}
if (Ty->getAnyNominal()) {
if (IsImplicitlyUnwrappedOptional) {
assert(Ty->getOptionalObjectType());
return StringRef("ImplicitlyUnwrappedOptional");
}
return Ty->getAnyNominal()->getNameStr();
}
#define TYPE(id, parent) \
if (Ty->getKind() == TypeKind::id) { \
return Ctx.buffer(#id); \
}
#include "swift/AST/TypeNodes.def"
llvm_unreachable("Unhandled type name.");
}
static StringRef calculateUsr(SDKContext &Ctx, ValueDecl *VD) {
llvm::SmallString<64> SS;
llvm::raw_svector_ostream OS(SS);
if (!ide::printValueDeclUSR(VD, OS)) {
return Ctx.buffer(SS.str());
}
return StringRef();
}
static StringRef calculateMangledName(SDKContext &Ctx, ValueDecl *VD) {
if (isFromClang(VD)) {
// Don't mangle clang symbols.
return StringRef();
}
if (auto *attr = VD->getAttrs().getAttribute<SILGenNameAttr>()) {
return Ctx.buffer(attr->Name);
}
Mangle::ASTMangler NewMangler;
return Ctx.buffer(NewMangler.mangleAnyDecl(VD, true,
/*bool respectOriginallyDefinedIn*/true));
}
static StringRef calculateLocation(SDKContext &SDKCtx, Decl *D) {
if (SDKCtx.getOpts().AvoidLocation)
return StringRef();
auto &Ctx = D->getASTContext();
auto &Importer = static_cast<ClangImporter &>(*Ctx.getClangModuleLoader());
clang::SourceManager &SM = Importer.getClangPreprocessor().getSourceManager();
if (ClangNode CN = D->getClangNode()) {
clang::SourceLocation Loc = CN.getLocation();
Loc = SM.getFileLoc(Loc);
if (Loc.isValid())
return SDKCtx.buffer(Loc.printToString(SM));
}
return StringRef();
}
static bool isFunctionTypeNoEscape(Type Ty) {
if (auto *AFT = Ty->getAs<AnyFunctionType>()) {
return AFT->getExtInfo().isNoEscape();
}
return false;
}
static StringRef getSimpleName(ValueDecl *VD) {
if (VD->hasName()) {
auto name = VD->getBaseName();
switch (name.getKind()) {
case DeclBaseName::Kind::Subscript:
return "subscript";
case DeclBaseName::Kind::Constructor:
return "init";
case DeclBaseName::Kind::Destructor:
return "deinit";
case DeclBaseName::Kind::Normal:
return llvm::StringSwitch<StringRef>(name.getIdentifier().str())
.Case("subscript", "`subscript`")
.Case("init", "`init`")
.Case("deinit", "`deinit`")
.Default(name.getIdentifier().str());
}
}
if (auto *AD = dyn_cast<AccessorDecl>(VD)) {
switch(AD->getAccessorKind()) {
#define ACCESSOR(ID) \
case AccessorKind::ID: return #ID;
#include "swift/AST/AccessorKinds.def"
}
}
return "_";
}
static StringRef getPrintedName(SDKContext &Ctx, ValueDecl *VD) {
if (isa<AbstractFunctionDecl>(VD) || isa<SubscriptDecl>(VD)) {
llvm::SmallString<32> Result;
Result.append(getSimpleName(VD));
Result.append("(");
for (auto Arg : VD->getName().getArgumentNames()) {
Result.append(Arg.empty() ? "_" : Arg.str());
Result.append(":");
}
Result.append(")");
return Ctx.buffer(Result.str());
}
return getSimpleName(VD);
}
static bool isFuncThrowing(ValueDecl *VD) {
if (auto AF = dyn_cast<AbstractFunctionDecl>(VD)) {
return AF->hasThrows();
}
return false;
}
static std::optional<uint8_t> getSelfIndex(ValueDecl *VD) {
if (auto AF = dyn_cast<AbstractFunctionDecl>(VD)) {
if (AF->isImportAsInstanceMember())
return AF->getSelfIndex();
}
return std::nullopt;
}
static ReferenceOwnership getReferenceOwnership(ValueDecl *VD) {
if (auto OA = VD->getAttrs().getAttribute<ReferenceOwnershipAttr>()) {
return OA->get();
}
return ReferenceOwnership::Strong;
}
// Get a requirement with all types canonicalized.
Requirement getCanonicalRequirement(Requirement &Req) {
auto kind = Req.getKind();
if (kind == RequirementKind::Layout) {
return Requirement(kind, Req.getFirstType()->getCanonicalType(),
Req.getLayoutConstraint());
} else {
return Requirement(kind, Req.getFirstType()->getCanonicalType(),
Req.getSecondType()->getCanonicalType());
}
}
// Get an inverse requirement with the subject type canonicalized.
InverseRequirement getCanonicalInverseRequirement(InverseRequirement &Req) {
return {Req.subject->getCanonicalType(), Req.protocol, Req.loc};
}
static
StringRef printGenericSignature(SDKContext &Ctx,
ArrayRef<Requirement> AllReqs,
ArrayRef<InverseRequirement> Inverses,
bool Canonical) {
llvm::SmallString<32> Result;
llvm::raw_svector_ostream OS(Result);
if (AllReqs.empty())
return StringRef();
OS << "<";
bool First = true;
PrintOptions Opts = getTypePrintOpts(Ctx.getOpts());
for (auto Req: AllReqs) {
if (!First) {
OS << ", ";
} else {
First = false;
}
if (Canonical)
getCanonicalRequirement(Req).print(OS, Opts);
else
Req.print(OS, Opts);
}
for (auto Inv: Inverses) {
if (!First) {
OS << ", ";
} else {
First = false;
}
if (Canonical)
getCanonicalInverseRequirement(Inv).print(OS, Opts);
else
Inv.print(OS, Opts);
}
OS << ">";
return Ctx.buffer(OS.str());
}
static StringRef printGenericSignature(SDKContext &Ctx, Decl *D, bool Canonical) {
llvm::SmallString<32> Result;
llvm::raw_svector_ostream OS(Result);
if (auto *PD = dyn_cast<ProtocolDecl>(D)) {
SmallVector<Requirement, 2> reqs;
SmallVector<InverseRequirement, 2> inverses;
PD->getRequirementSignature().getRequirementsWithInverses(PD, reqs, inverses);
return printGenericSignature(Ctx, reqs, inverses, Canonical);
}
PrintOptions Opts = getTypePrintOpts(Ctx.getOpts());
if (auto *GC = D->getAsGenericContext()) {
if (auto Sig = GC->getGenericSignature()) {
if (Canonical)
Sig.getCanonicalSignature()->print(OS, Opts);
else
Sig->print(OS, Opts);
return Ctx.buffer(OS.str());
}
}
return StringRef();
}
static
StringRef printGenericSignature(SDKContext &Ctx, ProtocolConformance *Conf, bool Canonical) {
return printGenericSignature(Ctx, Conf->getConditionalRequirements(), {}, Canonical);
}
static std::optional<uint8_t>
getSimilarMemberCount(NominalTypeDecl *NTD, ValueDecl *VD,
llvm::function_ref<bool(Decl *)> Check) {
if (!Check(VD))
return std::nullopt;
auto Members = NTD->getMembers();
auto End = std::find(Members.begin(), Members.end(), VD);
assert(End != Members.end());
return std::count_if(Members.begin(), End, Check);
}
std::optional<uint8_t> SDKContext::getFixedBinaryOrder(ValueDecl *VD) const {
// We don't need fixed binary order when checking API stability.
if (!checkingABI())
return std::nullopt;
auto *NTD = dyn_cast_or_null<NominalTypeDecl>(VD->getDeclContext()->
getAsDecl());
if (!NTD || isa<ProtocolDecl>(NTD) || NTD->isResilient())
return std::nullopt;
// The relative order of stored properties matters for non-resilient type.
auto isStored = [](Decl *M) {
if (auto *STD = dyn_cast<AbstractStorageDecl>(M)) {
return STD->hasStorage() && !STD->isStatic();
}
return false;
};
switch (NTD->getKind()) {
case DeclKind::Enum: {
return getSimilarMemberCount(NTD, VD, [](Decl *M) {
return isa<EnumElementDecl>(M);
});
}
case DeclKind::Class:
case DeclKind::Struct: {
return getSimilarMemberCount(NTD, VD, isStored);
}
default:
llvm_unreachable("bad nominal type kind.");
}
}
// check for if it has @available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
static bool isABIPlaceHolder(Decl *D) {
llvm::SmallSet<PlatformKind, 4> Platforms;
for (auto *ATT: D->getAttrs()) {
if (auto *AVA = dyn_cast<AvailableAttr>(ATT)) {
if (AVA->Platform != PlatformKind::none && AVA->Introduced &&
AVA->Introduced->getMajor() == 9999) {
Platforms.insert(AVA->Platform);
}
}
}
return Platforms.size() == 4;
}
static bool isABIPlaceholderRecursive(Decl *D) {
for (auto *CD = D; CD; CD = CD->getDeclContext()->getAsDecl()) {
if (isABIPlaceHolder(CD))
return true;
}
return false;
}
StringRef SDKContext::getPlatformIntroVersion(Decl *D, PlatformKind Kind) {
if (!D)
return StringRef();
for (auto *ATT: D->getAttrs()) {
if (auto *AVA = dyn_cast<AvailableAttr>(ATT)) {
if (AVA->Platform == Kind && AVA->Introduced) {
return buffer(AVA->Introduced->getAsString());
}
}
}
return StringRef();
}
StringRef SDKContext::getLanguageIntroVersion(Decl *D) {
if (!D)
return StringRef();
for (auto *ATT: D->getAttrs()) {
if (auto *AVA = dyn_cast<AvailableAttr>(ATT)) {
if (AVA->isLanguageVersionSpecific() && AVA->Introduced) {
return buffer(AVA->Introduced->getAsString());
}
}
}
return getLanguageIntroVersion(D->getDeclContext()->getAsDecl());
}
StringRef SDKContext::getObjcName(Decl *D) {
if (auto *OC = D->getAttrs().getAttribute<ObjCAttr>()) {
if (OC->getName().has_value()) {
SmallString<32> Buffer;
return buffer(OC->getName()->getString(Buffer));
}
}
return StringRef();
}
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Type Ty, TypeInitInfo Info) :
Ctx(Ctx), Name(getTypeName(Ctx, Ty, Info.IsImplicitlyUnwrappedOptional)),
PrintedName(getPrintedName(Ctx, Ty, Info.IsImplicitlyUnwrappedOptional)),
ParamValueOwnership(Info.ValueOwnership),
HasDefaultArg(Info.hasDefaultArgument) {
if (isFunctionTypeNoEscape(Ty))
TypeAttrs.push_back(TypeAttrKind::NoEscape);
// If this is a nominal type, get its Usr.
if (auto *ND = Ty->getAnyNominal()) {
Usr = calculateUsr(Ctx, ND);
MangledName = calculateMangledName(Ctx, ND);
}
}
static std::vector<DeclAttrKind> collectDeclAttributes(Decl *D) {
std::vector<DeclAttrKind> Results;
for (auto *Attr: D->getAttrs())
Results.push_back(Attr->getKind());
if (auto *VD = dyn_cast<ValueDecl>(D)) {
#define HANDLE(COND, KIND_NAME) \
if (VD->COND && !llvm::is_contained(Results, DeclAttrKind::KIND_NAME)) \
Results.emplace_back(DeclAttrKind::KIND_NAME);
// These attributes may be semantically applicable to the current decl but absent from
// the actual AST. Populating them to the nodes ensure we don't have false positives.
HANDLE(isObjC(), ObjC)
HANDLE(isFinal(), Final)
HANDLE(isDynamic(), Dynamic)
#undef HANDLE
}
return Results;
}
CtorInitializerKind SDKNodeDeclConstructor::getInitKind() const {
#define CASE(KIND) if (InitKind == #KIND) return CtorInitializerKind::KIND;
CASE(Designated)
CASE(Convenience)
CASE(ConvenienceFactory)
CASE(Factory)
#undef CASE
llvm_unreachable("unhandled init kind");
}
StringRef SDKContext::getInitKind(Decl *D) {
if (auto *CD = dyn_cast<ConstructorDecl>(D)) {
switch(CD->getInitKind()) {
#define CASE(KIND) case CtorInitializerKind::KIND: return #KIND;
CASE(Designated)
CASE(Convenience)
CASE(ConvenienceFactory)
CASE(Factory)
#undef CASE
}
}
return StringRef();
}
static bool isDeclaredInExtension(Decl *D) {
if (auto *DC = D->getDeclContext()->getAsDecl()) {
return isa<ExtensionDecl>(DC);
}
return false;
}
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Decl *D):
Ctx(Ctx), DKind(D->getKind()), Loc(D->getLoc()),
Location(calculateLocation(Ctx, D)),
ModuleName(D->getModuleContext()->getName().str()),
GenericSig(printGenericSignature(Ctx, D, /*Canonical*/Ctx.checkingABI())),
SugaredGenericSig(Ctx.checkingABI()?
printGenericSignature(Ctx, D, /*Canonical*/false):
StringRef()),
IntromacOS(Ctx.getPlatformIntroVersion(D, PlatformKind::macOS)),
IntroiOS(Ctx.getPlatformIntroVersion(D, PlatformKind::iOS)),
IntrotvOS(Ctx.getPlatformIntroVersion(D, PlatformKind::tvOS)),
IntrowatchOS(Ctx.getPlatformIntroVersion(D, PlatformKind::watchOS)),
Introswift(Ctx.getLanguageIntroVersion(D)),
ObjCName(Ctx.getObjcName(D)),
InitKind(Ctx.getInitKind(D)),
IsImplicit(D->isImplicit()),
IsDeprecated(D->getAttrs().isDeprecated(D->getASTContext())),
IsABIPlaceholder(isABIPlaceholderRecursive(D)),
IsFromExtension(isDeclaredInExtension(D)),
DeclAttrs(collectDeclAttributes(D)) {
// Keep track of SPI group names
for (auto id: D->getSPIGroups()) {
SPIGroups.push_back(id.str());
}
}
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, OperatorDecl *OD):
SDKNodeInitInfo(Ctx, cast<Decl>(OD)) {
Name = OD->getName().str();
PrintedName = OD->getName().str();
}
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ImportDecl *ID):
SDKNodeInitInfo(Ctx, cast<Decl>(ID)) {
std::string content;
llvm::raw_string_ostream OS(content);
ID->getModulePath().print(OS);
Name = PrintedName = Ctx.buffer(content);
}
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ProtocolConformanceRef Conform):
SDKNodeInitInfo(Ctx, Conform.getRequirement()) {
// The conformance can be conditional. The generic signature keeps track of
// the requirements.
if (Conform.isConcrete()) {
auto *Concrete = Conform.getConcrete();
GenericSig = printGenericSignature(Ctx, Concrete, Ctx.checkingABI());
SugaredGenericSig = Ctx.checkingABI() ?
printGenericSignature(Ctx, Concrete, false): StringRef();
// Whether this conformance is ABI placeholder depends on the decl context
// of this conformance.
IsABIPlaceholder = isABIPlaceholderRecursive(Concrete->getDeclContext()->
getAsDecl());
}
}
static bool isProtocolRequirement(ValueDecl *VD) {
if (isa<ProtocolDecl>(VD->getDeclContext()) && VD->isProtocolRequirement())
return true;
// If the VD is an accessor of the property declaration that is a protocol
// requirement, we consider this accessor as a protocol requirement too.
if (auto *AD = dyn_cast<AccessorDecl>(VD)) {
if (auto *ST = AD->getStorage()) {
return isProtocolRequirement(ST);
}
}
return false;
}
static bool requireWitnessTableEntry(ValueDecl *VD) {
if (auto *FD = dyn_cast<AbstractFunctionDecl>(VD)) {
return FD->requiresNewWitnessTableEntry();
}
return false;
}
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD)
: SDKNodeInitInfo(Ctx, cast<Decl>(VD)) {
Name = getSimpleName(VD);
PrintedName = getPrintedName(Ctx, VD);
Usr = calculateUsr(Ctx, VD);
MangledName = calculateMangledName(Ctx, VD);
IsThrowing = isFuncThrowing(VD);
IsStatic = VD->isStatic();
IsOverriding = VD->getOverriddenDecl();
IsProtocolReq = isProtocolRequirement(VD);
IsOpen = Ctx.getAccessLevel(VD) == AccessLevel::Open;
IsInternal = Ctx.getAccessLevel(VD) < AccessLevel::Public;
SelfIndex = getSelfIndex(VD);
FixedBinaryOrder = Ctx.getFixedBinaryOrder(VD);
ReferenceOwnership = getReferenceOwnership(VD);
ReqNewWitnessTableEntry = IsProtocolReq && requireWitnessTableEntry(VD);
// Calculate usr for its super class.
if (auto *CD = dyn_cast_or_null<ClassDecl>(VD)) {
if (auto *Super = CD->getSuperclassDecl()) {
SuperclassUsr = calculateUsr(Ctx, Super);
for (auto T = CD->getSuperclass(); T; T = T->getSuperclass()) {
SuperclassNames.push_back(getPrintedName(Ctx, T->getCanonicalType()));
}
}
HasMissingDesignatedInitializers = CD->hasMissingDesignatedInitializers();
InheritsConvenienceInitializers = CD->inheritsSuperclassInitializers();
}
if (auto *FD = dyn_cast<FuncDecl>(VD)) {
switch(FD->getSelfAccessKind()) {
case SelfAccessKind::Mutating:
FuncSelfKind = "Mutating";
break;
case SelfAccessKind::LegacyConsuming:
// FIXME: Stay consistent with earlier digests that had underscores here.
FuncSelfKind = "__Consuming";
break;
case SelfAccessKind::NonMutating:
FuncSelfKind = "NonMutating";
break;
case SelfAccessKind::Consuming:
FuncSelfKind = "Consuming";
break;
case SelfAccessKind::Borrowing:
FuncSelfKind = "Borrowing";
break;
}
}
// Get enum raw type name if this is an enum.
if (auto *ED = dyn_cast<EnumDecl>(VD)) {
IsEnumExhaustive = ED->isFormallyExhaustive(nullptr);
if (auto RT = ED->getRawType()) {
if (auto *D = RT->getNominalOrBoundGenericNominal()) {
EnumRawTypeName = D->getName().str();
}
}
}
if (auto *VAD = dyn_cast<VarDecl>(VD)) {
IsLet = VAD->isLet();
}
if (auto *VAR = dyn_cast<AbstractStorageDecl>(VD)) {
HasStorage = VAR->hasStorage();
}
if (auto *ACC = dyn_cast<AccessorDecl>(VD)) {
AccKind = ACC->getAccessorKind();
}
}
SDKNode *SDKNodeInitInfo::createSDKNode(SDKNodeKind Kind) {
switch(Kind) {
#define NODE_KIND(X, NAME) \
case SDKNodeKind::X: \
return static_cast<SDKNode*>(new (Ctx.allocator().Allocate<SDKNode##X>()) \
SDKNode##X(*this)); \
break;
#include "swift/IDE/DigesterEnums.def"
}
llvm_unreachable("covered switch");
}
// Recursively construct a node that represents a type, for instance,
// representing the return value type of a function decl.
SDKNode *swift::ide::api::
SwiftDeclCollector::constructTypeNode(Type T, TypeInitInfo Info) {
if (Ctx.checkingABI()) {
T = T->getCanonicalType();
if (T->hasOpaqueArchetype()) {
// When the type contains an opaque result type and we're in the ABI mode,
// we should substitute the opaque result type to its underlying type.
// Notice this only works if the opaque result type is from an inlinable
// function where the function body is present in the swift module file,
// thus allowing us to know the concrete type.
ReplaceOpaqueTypesWithUnderlyingTypes replacer(
/*inContext=*/nullptr, ResilienceExpansion::Maximal,
/*isWholeModuleContext=*/false);
T = T.subst(replacer, replacer, SubstFlags::SubstituteOpaqueArchetypes)
->getCanonicalType();
}
}
if (auto NAT = dyn_cast<TypeAliasType>(T.getPointer())) {
SDKNode* Root = SDKNodeInitInfo(Ctx, T, Info).createSDKNode(SDKNodeKind::TypeAlias);
Root->addChild(constructTypeNode(NAT->getSinglyDesugaredType()));
return Root;
}
if (auto Fun = T->getAs<AnyFunctionType>()) {
SDKNode* Root = SDKNodeInitInfo(Ctx, T, Info).createSDKNode(SDKNodeKind::TypeFunc);
// Still, return type first
Root->addChild(constructTypeNode(Fun->getResult()));
auto Input =
AnyFunctionType::composeTuple(Fun->getASTContext(), Fun->getParams(),
ParameterFlagHandling::IgnoreNonEmpty);
Root->addChild(constructTypeNode(Input));
return Root;
}
SDKNode* Root = SDKNodeInitInfo(Ctx, T, Info).createSDKNode(SDKNodeKind::TypeNominal);
// Keep paren type as a stand-alone level.
if (auto *PT = dyn_cast<ParenType>(T.getPointer())) {
Root->addChild(constructTypeNode(PT->getSinglyDesugaredType()));
return Root;
}
// Handle the case where Type has sub-types.
if (auto BGT = T->getAs<BoundGenericType>()) {
for (auto Arg : BGT->getGenericArgs()) {
Root->addChild(constructTypeNode(Arg));
}
} else if (auto Tup = T->getAs<TupleType>()) {
for (auto Elt : Tup->getElementTypes())
Root->addChild(constructTypeNode(Elt));
} else if (auto MTT = T->getAs<AnyMetatypeType>()) {
Root->addChild(constructTypeNode(MTT->getInstanceType()));
} else if (auto ATT = T->getAs<ArchetypeType>()) {
for (auto Pro : ATT->getConformsTo()) {
Root->addChild(constructTypeNode(Pro->getDeclaredInterfaceType()));
}
}
return Root;
}
std::vector<SDKNode*> swift::ide::api::
SwiftDeclCollector::createParameterNodes(ParameterList *PL) {
std::vector<SDKNode*> Result;
for (auto param: *PL) {
TypeInitInfo Info;
Info.IsImplicitlyUnwrappedOptional = param->isImplicitlyUnwrappedOptional();
Info.hasDefaultArgument = param->getDefaultArgumentKind() !=
DefaultArgumentKind::None;
switch (param->getValueOwnership()) {
#define CASE(KIND) case ValueOwnership::KIND: Info.ValueOwnership = #KIND; break;
CASE(Owned)
CASE(InOut)
CASE(Shared)
case ValueOwnership::Default: break;
#undef CASE
}
Result.push_back(constructTypeNode(param->getInterfaceType(), Info));
}
return Result;
}
// Construct a node for a function decl. The first child of the function decl
// is guaranteed to be the return value type of this function.
// We sometimes skip the first parameter because it can be metatype of dynamic
// this if the function is a member function.
SDKNode *swift::ide::api::
SwiftDeclCollector::constructFunctionNode(FuncDecl* FD,
SDKNodeKind Kind) {
auto Func = SDKNodeInitInfo(Ctx, FD).createSDKNode(Kind);
TypeInitInfo Info;
Info.IsImplicitlyUnwrappedOptional = FD->isImplicitlyUnwrappedOptional();
Func->addChild(constructTypeNode(FD->getResultInterfaceType(), Info));
for (auto *Node : createParameterNodes(FD->getParameters()))
Func->addChild(Node);
return Func;
}
SDKNode* swift::ide::api::
SwiftDeclCollector::constructInitNode(ConstructorDecl *CD) {
auto Func = SDKNodeInitInfo(Ctx, CD).createSDKNode(SDKNodeKind::DeclConstructor);
Func->addChild(constructTypeNode(CD->getResultInterfaceType()));
for (auto *Node : createParameterNodes(CD->getParameters()))
Func->addChild(Node);
return Func;
}
bool swift::ide::api::
SDKContext::shouldIgnore(Decl *D, const Decl* Parent) const {
// Exclude all clang nodes if we're comparing Swift decls specifically.
// FIXME: isFromClang also excludes Swift decls with @objc. We should allow those.
if (Opts.SwiftOnly && isFromClang(D)) {
return true;
}
if (auto *ACC = dyn_cast<AccessorDecl>(D)) {
// Only include accessors if they are part of var and subscript decl.
if (!isa<AbstractStorageDecl>(Parent)) {
return true;
}
// Only include getter/setter if we are checking source compatibility.
if (!checkingABI()) {
switch (ACC->getAccessorKind()) {
case AccessorKind::Get:
case AccessorKind::DistributedGet:
case AccessorKind::Set:
break;
default:
return true;
}
}
}
if (checkingABI()) {
if (auto *VD = dyn_cast<ValueDecl>(D)) {
// Private vars with fixed binary orders can have ABI-impact, so we should
// allowlist them if we're checking ABI.
if (getFixedBinaryOrder(VD).has_value())
return false;
// Typealias should have no impact on ABI.
if (isa<TypeAliasDecl>(VD))
return true;
}
// Exclude decls with @_alwaysEmitIntoClient if we are checking ABI.
// These decls are considered effectively public because they are usable
// from inline, so we have to manually exclude them here.
if (D->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>())
return true;
} else {
if (D->isPrivateStdlibDecl(false))
return true;
}
if (AvailableAttr::isUnavailable(D))
return true;
if (auto VD = dyn_cast<ValueDecl>(D)) {
switch (getAccessLevel(VD)) {
case AccessLevel::Internal:
case AccessLevel::Private:
case AccessLevel::FilePrivate:
return true;
case AccessLevel::Package:
case AccessLevel::Public:
case AccessLevel::Open:
break;
}
}
if (auto *ClangD = D->getClangDecl()) {
if (isa<clang::ObjCIvarDecl>(ClangD))
return true;
if (isa<clang::FieldDecl>(ClangD))
return true;
if (ClangD->hasAttr<clang::SwiftPrivateAttr>())
return true;
// If this decl is a synthesized member from a conformed clang protocol, we
// should ignore this member to reduce redundancy.
if (Parent &&
!isa<swift::ProtocolDecl>(Parent) &&
isa<clang::ObjCProtocolDecl>(ClangD->getDeclContext()))
return true;
}
return false;
}
SDKNode *swift::ide::api::
SwiftDeclCollector::constructTypeDeclNode(NominalTypeDecl *NTD) {
auto TypeNode = SDKNodeInitInfo(Ctx, NTD).createSDKNode(SDKNodeKind::DeclType);
addConformancesToTypeDecl(cast<SDKNodeDeclType>(TypeNode), NTD);
addMembersToRoot(TypeNode, NTD);
for (auto Ext : NTD->getExtensions()) {
HandledExtensions.insert(Ext);
addMembersToRoot(TypeNode, Ext);
}
return TypeNode;
}
/// Create a node for stand-alone extensions. In the sdk dump, we don't have
/// a specific node for extension. Members in extensions are inlined to the
/// extended types. If the extended types are from a different module, we have to
/// synthesize this type node to include those extension members, since these
/// extension members are legit members of the module.
SDKNode *swift::ide::api::
SwiftDeclCollector::constructExternalExtensionNode(NominalTypeDecl *NTD,
ArrayRef<ExtensionDecl*> AllExts) {
SDKNodeInitInfo initInfo(Ctx, NTD);
initInfo.IsExternal = true;
auto *TypeNode = initInfo.createSDKNode(SDKNodeKind::DeclType);
addConformancesToTypeDecl(cast<SDKNodeDeclType>(TypeNode), NTD);
bool anyConformancesAdded = false;
// The members of the extensions are the only members of this synthesized type.
for (auto *Ext: AllExts) {
HandledExtensions.insert(Ext);
addMembersToRoot(TypeNode, Ext);
// Keep track if we've declared any conformances in this extension.
// FIXME: This is too conservative. We only _really_ care if this extension
// declares a conformance to any public protocols outside the module
// where the extended type originated. Eventually this should be
// updated to filter extensions that declare conformances to internal
// protocols that either don't inherit from any protocols or only
// inherit from other internal protocols. It should also consider
// conditional conformances with internal requirements that are still
// part of the ABI.
if (!Ext->getInherited().empty())
anyConformancesAdded = true;
}
// If none of the extensions added any public members or conformances, don't
// synthesize the type node.
if (TypeNode->getChildrenCount() == 0 && !anyConformancesAdded)
return nullptr;
return TypeNode;
}
SDKNode *swift::ide::api::
SwiftDeclCollector::constructVarNode(ValueDecl *VD) {
auto Var = cast<SDKNodeDeclVar>(SDKNodeInitInfo(Ctx, VD).createSDKNode(SDKNodeKind::DeclVar));
TypeInitInfo Info;
Info.IsImplicitlyUnwrappedOptional = VD->isImplicitlyUnwrappedOptional();
Var->addChild(constructTypeNode(VD->getInterfaceType(), Info));
if (auto VAD = dyn_cast<AbstractStorageDecl>(VD)) {
llvm::SmallVector<AccessorDecl*, 4> scratch;
for(auto *AC: VAD->getOpaqueAccessors(scratch)) {
if (!Ctx.shouldIgnore(AC, VAD)) {
Var->addAccessor(constructFunctionNode(AC, SDKNodeKind::DeclAccessor));
}
}
}
return Var;
}
SDKNode *swift::ide::api::
SwiftDeclCollector::constructTypeAliasNode(TypeAliasDecl *TAD) {
auto Alias = SDKNodeInitInfo(Ctx, TAD).createSDKNode(SDKNodeKind::DeclTypeAlias);
Alias->addChild(constructTypeNode(TAD->getUnderlyingType()));
return Alias;
}
SDKNode *swift::ide::api::
SwiftDeclCollector::constructMacroNode(MacroDecl *MD) {
auto Macro = SDKNodeInitInfo(Ctx, MD).createSDKNode(SDKNodeKind::DeclMacro);
Macro->addChild(constructTypeNode(MD->getInterfaceType(), TypeInitInfo()));
return Macro;
}
SDKNode *swift::ide::api::
SwiftDeclCollector::constructAssociatedTypeNode(AssociatedTypeDecl *ATD) {
auto Asso = SDKNodeInitInfo(Ctx, ATD).
createSDKNode(SDKNodeKind::DeclAssociatedType);
if (auto DT = ATD->getDefaultDefinitionType()) {
Asso->addChild(constructTypeNode(DT));
}
return Asso;
}
SDKNode *swift::ide::api::
SwiftDeclCollector::constructSubscriptDeclNode(SubscriptDecl *SD) {
auto *Subs = cast<SDKNodeDeclSubscript>(SDKNodeInitInfo(Ctx, SD).
createSDKNode(SDKNodeKind::DeclSubscript));
Subs->addChild(constructTypeNode(SD->getElementInterfaceType()));
for (auto *Node: createParameterNodes(SD->getIndices())) {
Subs->addChild(Node);
}
llvm::SmallVector<AccessorDecl*, 4> scratch;
for(auto *AC: SD->getOpaqueAccessors(scratch)) {
if (!Ctx.shouldIgnore(AC, SD)) {
Subs->addAccessor(constructFunctionNode(AC, SDKNodeKind::DeclAccessor));
}
}
return Subs;
}
void swift::ide::api::
SwiftDeclCollector::addMembersToRoot(SDKNode *Root, IterableDeclContext *Context) {
for (auto *Member : Context->getMembers()) {
if (Ctx.shouldIgnore(Member, Context->getDecl()))
continue;
if (auto Func = dyn_cast<FuncDecl>(Member)) {
Root->addChild(constructFunctionNode(Func, SDKNodeKind::DeclFunction));
} else if (auto CD = dyn_cast<ConstructorDecl>(Member)) {
Root->addChild(constructInitNode(CD));
} else if (auto VD = dyn_cast<VarDecl>(Member)) {
Root->addChild(constructVarNode(VD));
} else if (auto TAD = dyn_cast<TypeAliasDecl>(Member)) {
Root->addChild(constructTypeAliasNode(TAD));
} else if (auto EED = dyn_cast<EnumElementDecl>(Member)) {
Root->addChild(constructVarNode(EED));
} else if (auto NTD = dyn_cast<NominalTypeDecl>(Member)) {
Root->addChild(constructTypeDeclNode(NTD));
} else if (auto ATD = dyn_cast<AssociatedTypeDecl>(Member)) {
Root->addChild(constructAssociatedTypeNode(ATD));
} else if (auto SD = dyn_cast<SubscriptDecl>(Member)) {
Root->addChild(constructSubscriptDeclNode(SD));
} else if (isa<PatternBindingDecl>(Member)) {
// All containing variables should have been handled.
} else if (isa<EnumCaseDecl>(Member)) {
// All containing variables should have been handled.
} else if (isa<IfConfigDecl>(Member)) {
// All containing members should have been handled.
} else if (isa<PoundDiagnosticDecl>(Member)) {
// All containing members should have been handled.
} else if (isa<DestructorDecl>(Member)) {
// deinit has no impact.
} else if (isa<MissingMemberDecl>(Member)) {
// avoid adding MissingMemberDecl
} else {
llvm::errs() << "Unhandled decl:\n";
Member->dump(llvm::errs());
}
}
}
SDKNode *swift::ide::api::
SwiftDeclCollector::constructTypeWitnessNode(AssociatedTypeDecl *Assoc,
Type Ty) {
auto *Witness = SDKNodeInitInfo(Ctx, Assoc).createSDKNode(SDKNodeKind::TypeWitness);
Witness->addChild(constructTypeNode(Ty));
return Witness;
}
SDKNode *swift::ide::api::
SwiftDeclCollector::constructConformanceNode(ProtocolConformance *Conform) {
if (Ctx.checkingABI())
Conform = Conform->getCanonicalConformance();
auto ConfNode = cast<SDKNodeConformance>(SDKNodeInitInfo(Ctx,
ProtocolConformanceRef(Conform)).createSDKNode(SDKNodeKind::Conformance));
Conform->forEachTypeWitness(
[&](AssociatedTypeDecl *assoc, Type ty, TypeDecl *typeDecl) -> bool {
ConfNode->addChild(constructTypeWitnessNode(assoc, ty));
return false;
});
return ConfNode;
}
void swift::ide::api::
SwiftDeclCollector::addConformancesToTypeDecl(SDKNodeDeclType *Root,
NominalTypeDecl *NTD) {
if (auto *PD = dyn_cast<ProtocolDecl>(NTD)) {
for (auto *inherited : PD->getAllInheritedProtocols()) {
if (!Ctx.shouldIgnore(inherited)) {
ProtocolConformanceRef Conf(inherited);
auto ConfNode = SDKNodeInitInfo(Ctx, Conf)
.createSDKNode(SDKNodeKind::Conformance);
Root->addConformance(ConfNode);
}
}
} else {
// Avoid adding the same conformance twice.
SmallPtrSet<ProtocolConformance*, 4> Seen;
for (auto &Conf: NTD->getAllConformances()) {
if (!Ctx.shouldIgnore(Conf->getProtocol()) && !Seen.count(Conf))
Root->addConformance(constructConformanceNode(Conf));
Seen.insert(Conf);
}
}
}
void SwiftDeclCollector::printTopLevelNames() {
for (auto &Node : RootNode->getChildren()) {
llvm::outs() << Node->getKind() << ": " << Node->getName() << '\n';
}
}
SwiftDeclCollector::SwiftDeclCollector(SDKContext &Ctx,
ArrayRef<ModuleDecl*> Modules) : Ctx(Ctx),
RootNode(SDKNodeRoot::getInstance(Ctx, Modules)) {
if (Modules.empty()) {
return;
}
assert(!Modules.empty());
for (auto M: Modules) {
llvm::SmallVector<Decl*, 512> Decls;
swift::getTopLevelDeclsForDisplay(M, Decls);
for (auto D : Decls) {
if (Ctx.shouldIgnore(D))
continue;
if (KnownDecls.count(D))
continue;
KnownDecls.insert(D);
if (auto VD = dyn_cast<ValueDecl>(D))
foundDecl(VD, DeclVisibilityKind::DynamicLookup,
DynamicLookupInfo::AnyObject);
else
processDecl(D);
}
}
// Now sort the macros before processing so that we can have deterministic
// output.
llvm::array_pod_sort(ClangMacros.begin(), ClangMacros.end(),
[](ValueDecl * const *lhs,
ValueDecl * const *rhs) -> int {
return (*lhs)->getBaseName().userFacingName().compare(
(*rhs)->getBaseName().userFacingName());
});
for (auto *VD : ClangMacros)
processValueDecl(VD);
// Collect extensions to types from other modules and synthesize type nodes
// for them.
llvm::MapVector<NominalTypeDecl*, llvm::SmallVector<ExtensionDecl*, 4>> ExtensionMap;
for (auto *D: KnownDecls) {
if (auto *Ext = dyn_cast<ExtensionDecl>(D)) {
if (HandledExtensions.find(Ext) == HandledExtensions.end()) {
if (auto *NTD = Ext->getExtendedNominal()) {
// Check if the extension is from other modules.
if (!llvm::is_contained(Modules, NTD->getModuleContext())) {
ExtensionMap[NTD].push_back(Ext);
}
}
}
}
}
for (auto Pair: ExtensionMap) {
if (auto child = constructExternalExtensionNode(Pair.first, Pair.second))
RootNode->addChild(child);
}
}
SDKNode *SwiftDeclCollector::constructOperatorDeclNode(OperatorDecl *OD) {
return SDKNodeInitInfo(Ctx, OD).createSDKNode(SDKNodeKind::DeclOperator);
}
void SwiftDeclCollector::processDecl(Decl *D) {
assert(!isa<ValueDecl>(D));
if (auto *OD = dyn_cast<OperatorDecl>(D)) {
RootNode->addChild(constructOperatorDeclNode(OD));
}
if (auto *IM = dyn_cast<ImportDecl>(D)) {
RootNode->addChild(SDKNodeInitInfo(Ctx, IM)
.createSDKNode(SDKNodeKind::DeclImport));
}
}
void SwiftDeclCollector::processValueDecl(ValueDecl *VD) {
if (auto FD = dyn_cast<FuncDecl>(VD)) {
RootNode->addChild(constructFunctionNode(FD, SDKNodeKind::DeclFunction));
} else if (auto NTD = dyn_cast<NominalTypeDecl>(VD)) {
RootNode->addChild(constructTypeDeclNode(NTD));
} else if (auto VAD = dyn_cast<VarDecl>(VD)) {
RootNode->addChild(constructVarNode(VAD));
} else if (auto TAD = dyn_cast<TypeAliasDecl>(VD)) {
RootNode->addChild(constructTypeAliasNode(TAD));
} else if (auto MD = dyn_cast<MacroDecl>(VD)) {
RootNode->addChild(constructMacroNode(MD));
} else {
llvm_unreachable("unhandled value decl");
}
}
void SwiftDeclCollector::foundDecl(ValueDecl *VD, DeclVisibilityKind Reason,
DynamicLookupInfo) {
if (VD->getClangMacro()) {
// Collect macros, we will sort them afterwards.
ClangMacros.push_back(VD);
return;
}
processValueDecl(VD);
}
void SDKNode::output(json::Output &out, KeyKind Key, bool Value) {
if (Value)
out.mapRequired(getKeyContent(Ctx, Key).data(), Value);
}
void SDKNode::output(json::Output &out, KeyKind Key, StringRef Value) {
if (!Value.empty())
out.mapRequired(getKeyContent(Ctx, Key).data(), Value);
}
void SDKNode::jsonize(json::Output &out) {
auto Kind = getKind();
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_kind).data(), Kind);
output(out, KeyKind::KK_name, Name);
output(out, KeyKind::KK_printedName, PrintedName);
out.mapOptional(getKeyContent(Ctx, KeyKind::KK_children).data(), Children);
}
void SDKNodeRoot::jsonize(json::Output &out) {
SDKNode::jsonize(out);
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_json_format_version).data(), JsonFormatVer);
if (!Ctx.getOpts().AvoidToolArgs)
out.mapOptional(getKeyContent(Ctx, KeyKind::KK_tool_arguments).data(), ToolArgs);
}
std::optional<StringRef> SDKNodeRoot::getSingleModuleName() const {
auto rawName = getName();
if (rawName == "MULTI_MODULE" || rawName == "NO_MODULE")
return std::nullopt;
return rawName;
}
void SDKNodeConformance::jsonize(json::Output &out) {
SDKNode::jsonize(out);
output(out, KeyKind::KK_usr, Usr);
output(out, KeyKind::KK_mangledName, MangledName);
output(out, KeyKind::KK_isABIPlaceholder, IsABIPlaceholder);
}
void SDKNodeDecl::jsonize(json::Output &out) {
SDKNode::jsonize(out);
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_declKind).data(), DKind);
output(out, KeyKind::KK_usr, Usr);
output(out, KeyKind::KK_mangledName, MangledName);
output(out, KeyKind::KK_location, Location);
output(out, KeyKind::KK_moduleName, ModuleName);
output(out, KeyKind::KK_genericSig, GenericSig);
output(out, KeyKind::KK_sugared_genericSig, SugaredGenericSig);
output(out, KeyKind::KK_static, IsStatic);
output(out, KeyKind::KK_deprecated,IsDeprecated);
output(out, KeyKind::KK_protocolReq, IsProtocolReq);
output(out, KeyKind::KK_overriding, IsOverriding);
output(out, KeyKind::KK_implicit, IsImplicit);
output(out, KeyKind::KK_isOpen, IsOpen);
output(out, KeyKind::KK_isInternal, IsInternal);
output(out, KeyKind::KK_isABIPlaceholder, IsABIPlaceholder);
output(out, KeyKind::KK_intro_Macosx, introVersions.macos);
output(out, KeyKind::KK_intro_iOS, introVersions.ios);
output(out, KeyKind::KK_intro_tvOS, introVersions.tvos);
output(out, KeyKind::KK_intro_watchOS, introVersions.watchos);
output(out, KeyKind::KK_intro_swift, introVersions.swift);
output(out, KeyKind::KK_objc_name, ObjCName);
out.mapOptional(getKeyContent(Ctx, KeyKind::KK_declAttributes).data(), DeclAttributes);
out.mapOptional(getKeyContent(Ctx, KeyKind::KK_fixedbinaryorder).data(), FixedBinaryOrder);
// Strong reference is implied, no need for serialization.
if (getReferenceOwnership() != ReferenceOwnership::Strong) {
uint8_t Raw = uint8_t(getReferenceOwnership());
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_ownership).data(), Raw);
}
output(out, KeyKind::KK_isFromExtension, IsFromExtension);
out.mapOptional(getKeyContent(Ctx, KeyKind::KK_spi_group_names).data(), SPIGroups);
}
void SDKNodeDeclAbstractFunc::jsonize(json::Output &out) {
SDKNodeDecl::jsonize(out);
output(out, KeyKind::KK_throwing, IsThrowing);
output(out, KeyKind::KK_reqNewWitnessTableEntry, ReqNewWitnessTableEntry);
out.mapOptional(getKeyContent(Ctx, KeyKind::KK_selfIndex).data(), SelfIndex);
}
void SDKNodeDeclFunction::jsonize(json::Output &out) {
SDKNodeDeclAbstractFunc::jsonize(out);
output(out, KeyKind::KK_funcSelfKind, FuncSelfKind);
}
void SDKNodeDeclConstructor::jsonize(json::Output &out) {
SDKNodeDeclAbstractFunc::jsonize(out);
output(out, KeyKind::KK_init_kind, InitKind);
}
void SDKNodeDeclType::jsonize(json::Output &out) {
SDKNodeDecl::jsonize(out);
output(out, KeyKind::KK_superclassUsr, SuperclassUsr);
output(out, KeyKind::KK_enumRawTypeName, EnumRawTypeName);
output(out, KeyKind::KK_isExternal, IsExternal);
output(out, KeyKind::KK_isEnumExhaustive, IsEnumExhaustive);
output(out, KeyKind::KK_hasMissingDesignatedInitializers,
HasMissingDesignatedInitializers);
output(out, KeyKind::KK_inheritsConvenienceInitializers,
InheritsConvenienceInitializers);
out.mapOptional(getKeyContent(Ctx, KeyKind::KK_superclassNames).data(), SuperclassNames);
out.mapOptional(getKeyContent(Ctx, KeyKind::KK_conformances).data(), Conformances);
}
void SDKNodeType::jsonize(json::Output &out) {
SDKNode::jsonize(out);
out.mapOptional(getKeyContent(Ctx, KeyKind::KK_typeAttributes).data(), TypeAttributes);
output(out, KeyKind::KK_hasDefaultArg, HasDefaultArg);
output(out, KeyKind::KK_paramValueOwnership, ParamValueOwnership);
}
void SDKNodeTypeNominal::jsonize(json::Output &out) {
SDKNodeType::jsonize(out);
output(out, KeyKind::KK_usr, USR);
}
void SDKNodeDeclSubscript::jsonize(json::Output &out) {
SDKNodeDeclAbstractFunc::jsonize(out);
output(out, KeyKind::KK_hasStorage, HasStorage);
out.mapOptional(getKeyContent(Ctx, KeyKind::KK_accessors).data(), Accessors);
}
void SDKNodeDeclVar::jsonize(json::Output &out) {
SDKNodeDecl::jsonize(out);
output(out, KeyKind::KK_isLet, IsLet);
output(out, KeyKind::KK_hasStorage, HasStorage);
out.mapOptional(getKeyContent(Ctx, KeyKind::KK_accessors).data(), Accessors);
}
void SDKNodeDeclAccessor::jsonize(json::Output &out) {
SDKNodeDeclAbstractFunc::jsonize(out);
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_accessorKind).data(), AccKind);
}
namespace swift {
namespace json {
// In the namespace of swift::json, we define several functions so that the
// JSON serializer will know how to interpret and dump types defined in this
// file.
template<>
struct ScalarEnumerationTraits<TypeAttrKind> {
static void enumeration(Output &out, TypeAttrKind &value) {
// NOTE: For historical reasons. TypeAttribute uses the spelling, but
// DeclAttribute uses the kind name.
#define TYPE_ATTR(X, C) out.enumCase(value, #X, TypeAttrKind::C);
#include "swift/AST/TypeAttr.def"
}
};
template<>
struct ScalarEnumerationTraits<DeclAttrKind> {
static void enumeration(Output &out, DeclAttrKind &value) {
// NOTE: For historical reasons. TypeAttribute uses the spelling, but
// DeclAttribute uses the kind name.
#define DECL_ATTR(_, Name, ...) out.enumCase(value, #Name, DeclAttrKind::Name);
#include "swift/AST/DeclAttr.def"
}
};
template<>
struct ScalarEnumerationTraits<DeclKind> {
static void enumeration(Output &out, DeclKind &value) {
#define DECL(X, PARENT) out.enumCase(value, #X, DeclKind::X);
#include "swift/AST/DeclNodes.def"
}
};
template<>
struct ScalarEnumerationTraits<AccessorKind> {
static void enumeration(Output &out, AccessorKind &value) {
#define ACCESSOR(ID)
#define SINGLETON_ACCESSOR(ID, KEYWORD) \
out.enumCase(value, #KEYWORD, AccessorKind::ID);
#include "swift/AST/AccessorKinds.def"
}
};
template<>
struct ObjectTraits<SDKNode *> {
static void mapping(Output &out, SDKNode *&value) {
value->jsonize(out);
}
};
template<>
struct ArrayTraits<ArrayRef<SDKNode*>> {
static size_t size(Output &out, ArrayRef<SDKNode *> &seq) {
return seq.size();
}
static SDKNode *&element(Output &, ArrayRef<SDKNode *> &seq,
size_t index) {
return const_cast<SDKNode *&>(seq[index]);
}
};
template<>
struct ArrayTraits<ArrayRef<TypeAttrKind>> {
static size_t size(Output &out, ArrayRef<TypeAttrKind> &seq) {
return seq.size();
}
static TypeAttrKind& element(Output &, ArrayRef<TypeAttrKind> &seq,
size_t index) {
return const_cast<TypeAttrKind&>(seq[index]);
}
};
template<>
struct ArrayTraits<ArrayRef<DeclAttrKind>> {
static size_t size(Output &out, ArrayRef<DeclAttrKind> &seq) {
return seq.size();
}
static DeclAttrKind& element(Output &, ArrayRef<DeclAttrKind> &seq,
size_t index) {
return const_cast<DeclAttrKind&>(seq[index]);
}
};
template<>
struct ArrayTraits<ArrayRef<StringRef>> {
static size_t size(Output &out, ArrayRef<StringRef> &seq) {
return seq.size();
}
static StringRef& element(Output &, ArrayRef<StringRef> &seq,
size_t index) {
return const_cast<StringRef&>(seq[index]);
}
};
} // namespace json
} // namespace swift
namespace {// Anonymous namespace.
// Deserialize an SDKNode tree.
std::pair<std::unique_ptr<llvm::MemoryBuffer>, SDKNode*>
static parseJsonEmit(SDKContext &Ctx, StringRef FileName) {
namespace yaml = llvm::yaml;
// Load the input file.
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
vfs::getFileOrSTDIN(*Ctx.getSourceMgr().getFileSystem(), FileName,
/*FileSize*/-1, /*RequiresNullTerminator*/true,
/*IsVolatile*/false, /*RetryCount*/30);
if (!FileBufOrErr) {
llvm_unreachable("Failed to read JSON file");
}
StringRef Buffer = FileBufOrErr->get()->getBuffer();
yaml::Stream Stream(llvm::MemoryBufferRef(Buffer, FileName),
Ctx.getSourceMgr().getLLVMSourceMgr());
SDKNode *Result = nullptr;
for (auto DI = Stream.begin(); DI != Stream.end(); ++ DI) {
assert(DI != Stream.end() && "Failed to read a document");
yaml::Node *N = DI->getRoot();
assert(N && "Failed to find a root");
Result = SDKNode::constructSDKNode(Ctx, cast<yaml::MappingNode>(N));
}
return {std::move(FileBufOrErr.get()), Result};
}
enum class ConstKind: uint8_t {
StringLiteral = 0,
IntegerLiteral,
FloatLiteral,
BooleanLiteral,
Array,
Dictionary,
};
struct ConstExprInfo {
StringRef filePath;
ConstKind kind;
unsigned offset = 0;
unsigned length = 0;
StringRef value;
StringRef referencedD;
ConstExprInfo(StringRef filePath, ConstKind kind, unsigned offset,
unsigned length, StringRef value, StringRef referencedD):
filePath(filePath), kind(kind), offset(offset), length(length), value(value),
referencedD(referencedD) {}
ConstExprInfo() = default;
};
class ConstExtractor: public ASTWalker {
SDKContext &SCtx;
SourceManager &SM;
std::vector<ConstExprInfo> allConsts;
void record(Expr *E, ConstKind kind, StringRef Value, StringRef ReferencedD) {
auto startLoc = E->getStartLoc();
// Asserts?
if (startLoc.isInvalid())
return;
auto endLoc = E->getEndLoc();
assert(endLoc.isValid());
endLoc = Lexer::getLocForEndOfToken(SM, endLoc);
auto bufferId = SM.findBufferContainingLoc(startLoc);
auto length = SM.getByteDistance(startLoc, endLoc);
auto file = SM.getIdentifierForBuffer(bufferId);
auto offset = SM.getLocOffsetInBuffer(startLoc, bufferId);
allConsts.emplace_back(file, kind, offset, length, Value, ReferencedD);
}
void record(Expr *E, Expr *ValueProvider, StringRef ReferencedD = "") {
std::string content;
llvm::raw_string_ostream os(content);
ValueProvider->printConstExprValue(&os, nullptr);
assert(!content.empty());
auto buffered = SCtx.buffer(content);
switch(ValueProvider->getKind()) {
#define CASE(X) case ExprKind::X: record(E, ConstKind::X, buffered, ReferencedD); break;
CASE(StringLiteral)
CASE(IntegerLiteral)
CASE(FloatLiteral)
CASE(BooleanLiteral)
CASE(Dictionary)
CASE(Array)
#undef CASE
default:
return;
}
}
StringRef getDeclName(Decl *D) {
if (auto *VD = dyn_cast<ValueDecl>(D)) {
std::string content;
llvm::raw_string_ostream os(content);
VD->getName().print(os);
return SCtx.buffer(content);
}
return StringRef();
}
bool handleSimpleReference(Expr *E) {
assert(E);
Decl *ReferencedDecl = nullptr;
if (auto *MRE = dyn_cast<MemberRefExpr>(E)) {
ReferencedDecl = MRE->getDecl().getDecl();
} else if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
ReferencedDecl = DRE->getDecl();
} else {
return false;
}
assert(ReferencedDecl);
if (auto *VAR = dyn_cast<VarDecl>(ReferencedDecl)) {
if (!VAR->getAttrs().hasAttribute<CompileTimeConstAttr>()) {
return false;
}
if (auto *PD = VAR->getParentPatternBinding()) {
if (auto *init = PD->getInit(PD->getPatternEntryIndexForVarDecl(VAR))) {
record(E, init, getDeclName(ReferencedDecl));
return true;
}
}
}
return false;
}
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::ArgumentsAndExpansion;
}
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
if (E->isSemanticallyConstExpr()) {
record(E, E);
return Action::SkipNode(E);
}
if (handleSimpleReference(E)) {
return Action::SkipNode(E);
}
return Action::Continue(E);
}
public:
ConstExtractor(SDKContext &SCtx, ASTContext &Ctx): SCtx(SCtx),
SM(Ctx.SourceMgr) {}
void extract(ModuleDecl *MD) { MD->walk(*this); }
std::vector<ConstExprInfo> &getAllConstValues() { return allConsts; }
};
} // End of anonymous namespace
template <> struct swift::json::ObjectTraits<ConstExprInfo> {
static void mapping(Output &out, ConstExprInfo &info) {
out.mapRequired("filePath", info.filePath);
StringRef kind;
switch(info.kind) {
#define CASE(X) case ConstKind::X: kind = #X; break;
CASE(StringLiteral)
CASE(IntegerLiteral)
CASE(FloatLiteral)
CASE(BooleanLiteral)
CASE(Dictionary)
CASE(Array)
#undef CASE
}
out.mapRequired("kind", kind);
out.mapRequired("offset", info.offset);
out.mapRequired("length", info.length);
out.mapRequired("value", info.value);
if (!info.referencedD.empty())
out.mapRequired("decl", info.referencedD);
}
};
struct swift::ide::api::PayLoad {
std::vector<ConstExprInfo> *allContsValues = nullptr;
};
// Construct all roots vector from a given file where a forest was
// previously dumped.
void SwiftDeclCollector::deSerialize(StringRef Filename) {
auto Pair = parseJsonEmit(Ctx, Filename);
RootNode = std::move(Pair.second);
}
// Serialize the content of all roots to a given file using JSON format.
void SwiftDeclCollector::serialize(llvm::raw_ostream &os, SDKNode *Root,
PayLoad OtherInfo) {
std::error_code EC;
json::Output yout(os);
assert(Root->getKind() == SDKNodeKind::Root);
SDKNodeRoot &root = *static_cast<SDKNodeRoot*>(Root);
yout.beginObject();
yout.mapRequired(ABIRootKey, root);
if (auto *constValues = OtherInfo.allContsValues) {
yout.mapRequired(ConstValuesKey, *constValues);
}
yout.endObject();
}
// Serialize the content of all roots to a given file using JSON format.
void SwiftDeclCollector::serialize(llvm::raw_ostream &os) {
SwiftDeclCollector::serialize(os, RootNode, PayLoad());
}
SDKNodeRoot *
swift::ide::api::getEmptySDKNodeRoot(SDKContext &SDKCtx) {
SwiftDeclCollector Collector(SDKCtx);
return Collector.getSDKRoot();
}
SDKNodeRoot*
swift::ide::api::getSDKNodeRoot(SDKContext &SDKCtx,
const CompilerInvocation &InitInvoke,
const llvm::StringSet<> &ModuleNames) {
CheckerOptions Opts = SDKCtx.getOpts();
CompilerInvocation Invocation(InitInvoke);
CompilerInstance &CI = SDKCtx.newCompilerInstance();
// Display diagnostics to stderr.
PrintingDiagnosticConsumer PrintDiags(llvm::errs());
if (llvm::errs().has_colors())
PrintDiags.forceColors();
CI.addDiagnosticConsumer(&PrintDiags);
// The PrintDiags is only responsible compiler errors, we should remove the
// consumer immediately after importing is done.
SWIFT_DEFER { CI.getDiags().removeConsumer(PrintDiags); };
std::string InstanceSetupError;
if (CI.setup(Invocation, InstanceSetupError)) {
llvm::errs() << InstanceSetupError << '\n';
return nullptr;
}
auto &Ctx = CI.getASTContext();
// Load standard library so that Clang importer can use it.
auto *Stdlib = Ctx.getStdlibModule(/*loadIfAbsent=*/true);
if (!Stdlib) {
llvm::errs() << "Failed to load Swift stdlib\n";
return nullptr;
}
std::vector<ModuleDecl *> Modules;
for (auto &Entry : ModuleNames) {
StringRef Name = Entry.getKey();
if (Opts.Verbose)
llvm::errs() << "Loading module: " << Name << "...\n";
auto *M = Ctx.getModuleByName(Name);
if (!M || M->failedToLoad() || Ctx.Diags.hadAnyError()) {
llvm::errs() << "Failed to load module: " << Name << '\n';
if (Opts.AbortOnModuleLoadFailure)
return nullptr;
} else {
Modules.push_back(M);
}
}
if (Opts.Verbose)
llvm::errs() << "Scanning symbols...\n";
SwiftDeclCollector Collector(SDKCtx, Modules);
return Collector.getSDKRoot();
}
void swift::ide::api::dumpSDKRoot(SDKNodeRoot *Root, PayLoad load,
llvm::raw_ostream &os) {
assert(Root);
auto Opts = Root->getSDKContext().getOpts();
if (Opts.Verbose)
llvm::errs() << "Dumping SDK...\n";
SwiftDeclCollector::serialize(os, Root, load);
}
void swift::ide::api::dumpSDKRoot(SDKNodeRoot *Root, llvm::raw_ostream &os) {
dumpSDKRoot(Root, PayLoad(), os);
}
int swift::ide::api::dumpSDKContent(const CompilerInvocation &InitInvoke,
const llvm::StringSet<> &ModuleNames,
llvm::raw_ostream &os, CheckerOptions Opts) {
SDKContext SDKCtx(Opts);
SDKNodeRoot *Root = getSDKNodeRoot(SDKCtx, InitInvoke, ModuleNames);
if (!Root)
return 1;
dumpSDKRoot(Root, os);
return 0;
}
int swift::ide::api::deserializeSDKDump(StringRef dumpPath, StringRef OutputPath,
CheckerOptions Opts) {
std::error_code EC;
llvm::raw_fd_ostream FS(OutputPath, EC, llvm::sys::fs::OF_None);
if (!fs::exists(dumpPath)) {
llvm::errs() << dumpPath << " does not exist\n";
return 1;
}
PrintingDiagnosticConsumer PDC;
SDKContext Ctx(Opts);
Ctx.addDiagConsumer(PDC);
SwiftDeclCollector Collector(Ctx);
Collector.deSerialize(dumpPath);
Collector.serialize(FS);
return 0;
}
void swift::ide::api::dumpModuleContent(ModuleDecl *MD, llvm::raw_ostream &os,
bool ABI, bool Empty) {
CheckerOptions opts;
opts.ABI = ABI;
opts.SwiftOnly = true;
opts.AvoidLocation = true;
opts.AvoidToolArgs = true;
opts.Migrator = false;
opts.SkipOSCheck = false;
opts.SkipRemoveDeprecatedCheck = false;
opts.Verbose = false;
SDKContext ctx(opts);
llvm::SmallVector<ModuleDecl*, 4> modules;
if (!Empty) {
modules.push_back(MD);
}
SwiftDeclCollector collector(ctx, modules);
ConstExtractor extractor(ctx, MD->getASTContext());
PayLoad payload;
SWIFT_DEFER {
payload.allContsValues = &extractor.getAllConstValues();
dumpSDKRoot(collector.getSDKRoot(), payload, os);
};
if (Empty) {
return;
}
extractor.extract(MD);
}
int swift::ide::api::findDeclUsr(StringRef dumpPath, CheckerOptions Opts) {
std::error_code EC;
if (!fs::exists(dumpPath)) {
llvm::errs() << dumpPath << " does not exist\n";
return 1;
}
PrintingDiagnosticConsumer PDC;
SDKContext Ctx(Opts);
Ctx.addDiagConsumer(PDC);
SwiftDeclCollector Collector(Ctx);
Collector.deSerialize(dumpPath);
struct FinderByLocation: SDKNodeVisitor {
StringRef Location;
FinderByLocation(StringRef Location): Location(Location) {}
void visit(SDKNode* Node) override {
if (auto *D = dyn_cast<SDKNodeDecl>(Node)) {
if (D->getLocation().contains(Location) && !D->getUsr().empty()) {
llvm::outs() << D->getFullyQualifiedName() << ": " << D->getUsr() << "\n";
}
}
}
};
if (!Opts.LocationFilter.empty()) {
FinderByLocation Finder(Opts.LocationFilter);
Collector.visitAllRoots(Finder);
}
return 0;
}
void swift::ide::api::SDKNodeDeclType::diagnose(SDKNode *Right) {
SDKNodeDecl::diagnose(Right);
auto *R = dyn_cast<SDKNodeDeclType>(Right);
if (!R)
return;
auto Loc = R->getLoc();
if (getDeclKind() != R->getDeclKind()) {
emitDiag(Loc, diag::decl_kind_changed, getDeclKindStr(R->getDeclKind(),
getSDKContext().getOpts().CompilerStyle));
return;
}
assert(getDeclKind() == R->getDeclKind());
auto DKind = getDeclKind();
switch (DKind) {
case DeclKind::Class: {
auto LSuperClass = getSuperClassName();
auto RSuperClass = R->getSuperClassName();
if (!LSuperClass.empty() && LSuperClass != RSuperClass) {
if (RSuperClass.empty()) {
emitDiag(Loc, diag::super_class_removed, LSuperClass);
} else if (!llvm::is_contained(R->getClassInheritanceChain(), LSuperClass)) {
emitDiag(Loc, diag::super_class_changed, LSuperClass, RSuperClass);
}
}
// Check for @_hasMissingDesignatedInitializers and
// @_inheritsConvenienceInitializers changes.
if (isOpen() && R->isOpen()) {
// It's not safe to add new, invisible designated inits to open
// classes.
if (!hasMissingDesignatedInitializers() &&
R->hasMissingDesignatedInitializers())
R->emitDiag(R->getLoc(), diag::added_invisible_designated_init);
}
// It's not safe to stop inheriting convenience inits, it changes
// the set of initializers that are available.
if (!Ctx.checkingABI() &&
inheritsConvenienceInitializers() &&
!R->inheritsConvenienceInitializers())
R->emitDiag(R->getLoc(), diag::not_inheriting_convenience_inits);
break;
}
default:
break;
}
}
void swift::ide::api::SDKNodeDeclAbstractFunc::diagnose(SDKNode *Right) {
SDKNodeDecl::diagnose(Right);
auto *R = dyn_cast<SDKNodeDeclAbstractFunc>(Right);
if (!R)
return;
auto Loc = R->getLoc();
if (!isThrowing() && R->isThrowing()) {
emitDiag(Loc, diag::decl_new_attr, Ctx.buffer("throwing"));
}
if (Ctx.checkingABI()) {
if (reqNewWitnessTableEntry() != R->reqNewWitnessTableEntry()) {
emitDiag(Loc, diag::decl_new_witness_table_entry, reqNewWitnessTableEntry());
}
// Diagnose moving a non-final class member to an extension.
if (hasValidParentPtr(getKind())) {
while(auto *parent = dyn_cast<SDKNodeDecl>(getParent())) {
if (parent->getDeclKind() != DeclKind::Class) {
break;
}
if (hasDeclAttribute(DeclAttrKind::Final)) {
break;
}
auto result = isFromExtensionChanged(*this, *Right);
if (result.has_value() && *result) {
emitDiag(Loc, diag::class_member_moved_to_extension);
}
break;
}
}
}
}
void swift::ide::api::SDKNodeDeclFunction::diagnose(SDKNode *Right) {
SDKNodeDeclAbstractFunc::diagnose(Right);
auto *R = dyn_cast<SDKNodeDeclFunction>(Right);
if (!R)
return;
auto Loc = R->getLoc();
if (getSelfAccessKind() != R->getSelfAccessKind()) {
emitDiag(Loc, diag::func_self_access_change, getSelfAccessKind(),
R->getSelfAccessKind());
}
if (Ctx.checkingABI()) {
if (hasFixedBinaryOrder() != R->hasFixedBinaryOrder()) {
emitDiag(Loc, diag::func_has_fixed_order_change, hasFixedBinaryOrder());
}
}
}
static StringRef getAttrName(DeclAttrKind Kind) {
switch (Kind) {
#define DECL_ATTR(NAME, CLASS, ...) \
case DeclAttrKind::CLASS: \
return DeclAttribute::isDeclModifier(DeclAttrKind::CLASS) ? #NAME \
: "@" #NAME;
#include "swift/AST/DeclAttr.def"
}
llvm_unreachable("covered switch");
}
static bool shouldDiagnoseAddingAttribute(SDKNodeDecl *D, DeclAttrKind Kind) {
return true;
}
static bool shouldDiagnoseRemovingAttribute(SDKNodeDecl *D, DeclAttrKind Kind) {
return true;
}
static bool isOwnershipEquivalent(ReferenceOwnership Left,
ReferenceOwnership Right) {
if (Left == Right)
return true;
if (Left == ReferenceOwnership::Unowned && Right == ReferenceOwnership::Weak)
return true;
if (Left == ReferenceOwnership::Weak && Right == ReferenceOwnership::Unowned)
return true;
return false;
}
void swift::ide::api::detectRename(SDKNode *L, SDKNode *R) {
if (L->getKind() == R->getKind() && isa<SDKNodeDecl>(L) &&
L->getPrintedName() != R->getPrintedName()) {
L->annotate(NodeAnnotation::Rename);
L->annotate(NodeAnnotation::RenameOldName, L->getPrintedName());
L->annotate(NodeAnnotation::RenameNewName, R->getPrintedName());
}
}
void swift::ide::api::SDKNodeDecl::diagnose(SDKNode *Right) {
SDKNode::diagnose(Right);
auto *RD = dyn_cast<SDKNodeDecl>(Right);
if (!RD)
return;
detectRename(this, RD);
auto Loc = RD->getLoc();
if (isOpen() && !RD->isOpen()) {
emitDiag(Loc, diag::no_longer_open);
}
// Diagnose static attribute change.
if (isStatic() ^ RD->isStatic()) {
emitDiag(Loc, diag::decl_new_attr, Ctx.buffer(isStatic() ? "not static" :
"static"));
}
// Diagnose ownership change.
if (!isOwnershipEquivalent(getReferenceOwnership(),
RD->getReferenceOwnership())) {
auto getOwnershipDescription = [&](swift::ReferenceOwnership O) {
if (O == ReferenceOwnership::Strong)
return Ctx.buffer("strong");
return keywordOf(O);
};
emitDiag(Loc, diag::decl_attr_change,
getOwnershipDescription(getReferenceOwnership()),
getOwnershipDescription(RD->getReferenceOwnership()));
}
// Diagnose generic signature change
if (getGenericSignature() != RD->getGenericSignature()) {
// Prefer sugared signature in diagnostics to be more user-friendly.
if (Ctx.commonVersionAtLeast(2) &&
getSugaredGenericSignature() != RD->getSugaredGenericSignature()) {
emitDiag(Loc, diag::generic_sig_change,
getSugaredGenericSignature(), RD->getSugaredGenericSignature());
} else {
emitDiag(Loc, diag::generic_sig_change,
getGenericSignature(), RD->getGenericSignature());
}
}
// ObjC name changes are considered breakage
if (getObjCName() != RD->getObjCName()) {
if (Ctx.commonVersionAtLeast(4)) {
emitDiag(Loc, diag::objc_name_change, getObjCName(), RD->getObjCName());
}
}
if (isOptional() != RD->isOptional()) {
if (Ctx.checkingABI()) {
// Both adding/removing optional is ABI-breaking.
emitDiag(Loc, diag::optional_req_changed, isOptional());
} else if (isOptional()) {
// Removing optional is source-breaking.
emitDiag(Loc, diag::optional_req_changed, isOptional());
}
}
// Diagnose removing attributes.
for (auto Kind: getDeclAttributes()) {
if (!RD->hasDeclAttribute(Kind)) {
if ((Ctx.checkingABI() ? DeclAttribute::isRemovingBreakingABI(Kind) :
DeclAttribute::isRemovingBreakingAPI(Kind)) &&
shouldDiagnoseRemovingAttribute(this, Kind)) {
emitDiag(Loc, diag::decl_new_attr,
Ctx.buffer((llvm::Twine("without ") + getAttrName(Kind)).str()));
}
}
}
// Diagnose adding attributes.
for (auto Kind: RD->getDeclAttributes()) {
if (!hasDeclAttribute(Kind)) {
if ((Ctx.checkingABI() ? DeclAttribute::isAddingBreakingABI(Kind) :
DeclAttribute::isAddingBreakingAPI(Kind)) &&
shouldDiagnoseAddingAttribute(this, Kind)) {
emitDiag(Loc, diag::decl_new_attr,
Ctx.buffer((llvm::Twine("with ") + getAttrName(Kind)).str()));
}
}
}
if (Ctx.checkingABI()) {
if (hasFixedBinaryOrder() && RD->hasFixedBinaryOrder() &&
getFixedBinaryOrder() != RD->getFixedBinaryOrder()) {
emitDiag(Loc, diag::decl_reorder, getFixedBinaryOrder(),
RD->getFixedBinaryOrder());
}
if (getUsr() != RD->getUsr()) {
auto left = demangleUSR(getUsr());
auto right = demangleUSR(RD->getUsr());
if (left != right) {
emitDiag(Loc, diag::demangled_name_changed, left, right);
}
}
}
}
void swift::ide::api::SDKNodeDeclOperator::diagnose(SDKNode *Right) {
SDKNodeDecl::diagnose(Right);
auto *RO = dyn_cast<SDKNodeDeclOperator>(Right);
if (!RO)
return;
auto Loc = RO->getLoc();
if (getDeclKind() != RO->getDeclKind()) {
emitDiag(Loc, diag::decl_kind_changed, getDeclKindStr(RO->getDeclKind(),
getSDKContext().getOpts().CompilerStyle));
}
}
void swift::ide::api::SDKNodeDeclVar::diagnose(SDKNode *Right) {
SDKNodeDecl::diagnose(Right);
auto *RV = dyn_cast<SDKNodeDeclVar>(Right);
if (!RV)
return;
auto Loc = RV->getLoc();
if (Ctx.checkingABI()) {
if (hasFixedBinaryOrder() != RV->hasFixedBinaryOrder()) {
emitDiag(Loc, diag::var_has_fixed_order_change, hasFixedBinaryOrder());
}
}
}
static bool shouldDiagnoseType(SDKNodeType *T) {
return T->isTopLevelType();
}
void swift::ide::api::SDKNodeType::diagnose(SDKNode *Right) {
SDKNode::diagnose(Right);
auto *RT = dyn_cast<SDKNodeType>(Right);
if (!RT || !shouldDiagnoseType(this))
return;
assert(isTopLevelType());
// Diagnose type witness changes when diagnosing ABI breakages.
if (auto *Wit = dyn_cast<SDKNodeTypeWitness>(getParent())) {
auto *Conform = Wit->getParent()->getAs<SDKNodeConformance>();
if (Ctx.checkingABI() && getPrintedName() != RT->getPrintedName()) {
auto *LD = Conform->getNominalTypeDecl();
LD->emitDiag(SourceLoc(), diag::type_witness_change,
Wit->getWitnessedTypeName(),
getPrintedName(), RT->getPrintedName());
}
return;
}
StringRef Descriptor = getTypeRoleDescription();
assert(isa<SDKNodeDecl>(getParent()));
auto LParent = cast<SDKNodeDecl>(getParent());
assert(LParent->getKind() == RT->getParent()->getAs<SDKNodeDecl>()->getKind());
auto Loc = RT->getParent()->getAs<SDKNodeDecl>()->getLoc();
if (getPrintedName() != RT->getPrintedName()) {
LParent->emitDiag(Loc, diag::decl_type_change,
Descriptor, getPrintedName(), RT->getPrintedName());
}
if (hasDefaultArgument() && !RT->hasDefaultArgument()) {
LParent->emitDiag(Loc, diag::default_arg_removed, Descriptor);
}
if (getParamValueOwnership() != RT->getParamValueOwnership()) {
LParent->emitDiag(Loc, diag::param_ownership_change,
getTypeRoleDescription(),
getParamValueOwnership(),
RT->getParamValueOwnership());
}
}
void swift::ide::api::SDKNodeTypeFunc::diagnose(SDKNode *Right) {
SDKNodeType::diagnose(Right);
auto *RT = dyn_cast<SDKNodeTypeFunc>(Right);
if (!RT || !shouldDiagnoseType(this))
return;
assert(isTopLevelType());
auto Loc = RT->getParent()->getAs<SDKNodeDecl>()->getLoc();
if (Ctx.checkingABI() && isEscaping() != RT->isEscaping()) {
getParent()->getAs<SDKNodeDecl>()->emitDiag(Loc,
diag::func_type_escaping_changed,
getTypeRoleDescription(),
isEscaping());
}
}
|