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
|
//===--- SwiftSourceDocInfo.cpp -------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "SourceKit/Support/FileSystemProvider.h"
#include "SourceKit/Support/ImmutableTextBuffer.h"
#include "SourceKit/Support/Logging.h"
#include "SourceKit/Support/UIdent.h"
#include "SwiftASTManager.h"
#include "SwiftEditorDiagConsumer.h"
#include "SwiftLangSupport.h"
#include "swift/AST/ASTDemangler.h"
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/Decl.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/LookupKinds.h"
#include "swift/AST/ModuleNameLookup.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/SwiftNameTranslation.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
#include "swift/IDE/CodeCompletion.h"
#include "swift/IDE/CommentConversion.h"
#include "swift/IDE/IDERequests.h"
#include "swift/IDE/ModuleInterfacePrinting.h"
#include "swift/Refactoring/Refactoring.h"
#include "swift/IDE/SourceEntityWalker.h"
#include "swift/IDE/Utils.h"
#include "swift/Markup/XMLUtils.h"
#include "swift/Sema/IDETypeChecking.h"
#include "swift/SymbolGraphGen/SymbolGraphGen.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclObjC.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/Module.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Index/USRGeneration.h"
#include "clang/Lex/Lexer.h"
#include "llvm/Support/MemoryBuffer.h"
#include <numeric>
using namespace SourceKit;
using namespace swift;
using namespace swift::ide;
namespace {
class AnnotatedDeclarationPrinter : public XMLEscapingPrinter {
public:
AnnotatedDeclarationPrinter(raw_ostream &OS)
:XMLEscapingPrinter(OS) { }
private:
void printTypeRef(
Type T, const TypeDecl *TD, Identifier Name,
PrintNameContext NameContext = PrintNameContext::Normal) override {
printXML("<Type usr=\"");
SwiftLangSupport::printUSR(TD, OS);
printXML("\">");
StreamPrinter::printTypeRef(T, TD, Name, NameContext);
printXML("</Type>");
}
};
} // end anonymous namespace
static StringRef getTagForDecl(const Decl *D, bool isRef) {
auto UID = SwiftLangSupport::getUIDForDecl(D, isRef);
static const char *prefix = "source.lang.swift.";
assert(UID.getName().starts_with(prefix));
return UID.getName().drop_front(strlen(prefix));
}
static StringRef ExternalParamNameTag = "decl.var.parameter.argument_label";
static StringRef LocalParamNameTag = "decl.var.parameter.name";
static StringRef GenericParamNameTag = "decl.generic_type_param.name";
static StringRef SyntaxKeywordTag = "syntaxtype.keyword";
static StringRef getTagForParameter(PrintStructureKind context) {
switch (context) {
case PrintStructureKind::FunctionParameter:
return "decl.var.parameter";
case PrintStructureKind::FunctionReturnType:
return "decl.function.returntype";
case PrintStructureKind::FunctionType:
return "";
case PrintStructureKind::TupleType:
return "tuple";
case PrintStructureKind::TupleElement:
return "tuple.element";
case PrintStructureKind::GenericParameter:
return "decl.generic_type_param";
case PrintStructureKind::GenericRequirement:
return "decl.generic_type_requirement";
case PrintStructureKind::BuiltinAttribute:
return "syntaxtype.attribute.builtin";
case PrintStructureKind::NumberLiteral:
return "syntaxtype.number";
case PrintStructureKind::StringLiteral:
return "syntaxtype.string";
case PrintStructureKind::DefaultArgumentClause:
case PrintStructureKind::DeclGenericParameterClause:
case PrintStructureKind::DeclGenericRequirementClause:
case PrintStructureKind::EffectsSpecifiers:
case PrintStructureKind::DeclResultTypeClause:
case PrintStructureKind::FunctionParameterList:
case PrintStructureKind::FunctionParameterType:
// These kinds are ignored by 'isIgnoredPrintStructureKind()'
llvm_unreachable("ignored structure kind");
}
llvm_unreachable("unexpected structure kind");
}
static StringRef getDeclNameTagForDecl(const Decl *D) {
switch (D->getKind()) {
case DeclKind::Param:
// When we're examining the parameter itself, it is the local name that is
// the name of the variable.
return LocalParamNameTag;
case DeclKind::GenericTypeParam:
return ""; // Handled by printName.
case DeclKind::Constructor:
case DeclKind::Destructor:
case DeclKind::Subscript:
// The names 'init'/'deinit'/'subscript' are actually keywords.
return SyntaxKeywordTag;
default:
return "decl.name";
}
}
namespace {
/// A typesafe union of contexts that the printer can be inside.
/// Currently: Decl, PrintStructureKind
class PrintContext {
// Use the low bit to determine the type; store the enum value shifted left
// to leave the low bit free.
const uintptr_t value;
static constexpr unsigned declTag = 0;
static constexpr unsigned PrintStructureKindTag = 1;
static constexpr unsigned typeTag = 2;
static constexpr unsigned tagMask = 3;
static constexpr unsigned tagShift = 2;
bool hasTag(unsigned tag) const { return (value & tagMask) == tag; }
public:
PrintContext(const Decl *D) : value(uintptr_t(D)) {
static_assert(llvm::PointerLikeTypeTraits<Decl *>::NumLowBitsAvailable >=
tagShift,
"missing spare bit in Decl *");
}
PrintContext(PrintStructureKind K)
: value((uintptr_t(K) << tagShift) | PrintStructureKindTag) {}
PrintContext(TypeLoc unused) : value(typeTag) {}
/// Get the context as a Decl, or nullptr.
const Decl *getDecl() const {
return hasTag(declTag) ? (const Decl *)value : nullptr;
}
/// Get the context as a PrintStructureKind, or None.
std::optional<PrintStructureKind> getPrintStructureKind() const {
if (!hasTag(PrintStructureKindTag))
return std::nullopt;
return PrintStructureKind(value >> tagShift);
}
/// Whether this is a PrintStructureKind context of the given \p kind.
bool is(PrintStructureKind kind) const {
auto storedKind = getPrintStructureKind();
return storedKind && *storedKind == kind;
}
bool isType() const { return hasTag(typeTag); }
};
/// An ASTPrinter for annotating declarations with XML tags that describe the
/// key substructure of the declaration for CursorInfo/DocInfo.
///
/// Prints declarations with decl- and type-specific tags derived from the
/// UIDs used for decl/refs. For example (including newlines purely for ease of
/// reading):
///
/// \verbatim
/// <decl.function.free>
/// func <decl.name>foo</decl.name>
/// (
/// <decl.var.parameter>
/// <decl.var.parameter.name>x</decl.var.parameter.name>:
/// <ref.struct usr="Si">Int</ref.struct>
/// </decl.var.parameter>
/// ) -> <decl.function.returntype>
/// <ref.struct usr="Si">Int</ref.struct></decl.function.returntype>
/// </decl.function.free>
/// \endverbatim
class FullyAnnotatedDeclarationPrinter final : public XMLEscapingPrinter {
public:
FullyAnnotatedDeclarationPrinter(raw_ostream &OS) : XMLEscapingPrinter(OS) {}
private:
// MARK: The ASTPrinter callback interface.
void printDeclPre(const Decl *D,
std::optional<BracketOptions> Bracket) override {
contextStack.emplace_back(PrintContext(D));
openTag(getTagForDecl(D, /*isRef=*/false));
}
void printDeclPost(const Decl *D,
std::optional<BracketOptions> Bracket) override {
assert(contextStack.back().getDecl() == D && "unmatched printDeclPre");
contextStack.pop_back();
closeTag(getTagForDecl(D, /*isRef=*/false));
}
void printDeclLoc(const Decl *D) override {
auto tag = getDeclNameTagForDecl(D);
if (!tag.empty())
openTag(tag);
}
void printDeclNameEndLoc(const Decl *D) override {
auto tag = getDeclNameTagForDecl(D);
if (!tag.empty())
closeTag(tag);
}
void printTypePre(const TypeLoc &TL) override {
auto tag = getTypeTagForCurrentContext();
contextStack.emplace_back(PrintContext(TL));
if (!tag.empty())
openTag(tag);
}
void printTypePost(const TypeLoc &TL) override {
assert(contextStack.back().isType());
contextStack.pop_back();
auto tag = getTypeTagForCurrentContext();
if (!tag.empty())
closeTag(tag);
}
bool isIgnoredPrintStructureKind(PrintStructureKind kind) {
switch (kind) {
case PrintStructureKind::DefaultArgumentClause:
case PrintStructureKind::DeclGenericParameterClause:
case PrintStructureKind::DeclGenericRequirementClause:
case PrintStructureKind::EffectsSpecifiers:
case PrintStructureKind::DeclResultTypeClause:
case PrintStructureKind::FunctionParameterList:
case PrintStructureKind::FunctionParameterType:
return true;
default:
return false;
}
}
void printStructurePre(PrintStructureKind kind, const Decl *D) override {
if (isIgnoredPrintStructureKind(kind))
return;
if (kind == PrintStructureKind::TupleElement ||
kind == PrintStructureKind::TupleType)
fixupTuple(kind);
contextStack.emplace_back(PrintContext(kind));
auto tag = getTagForParameter(kind);
if (tag.empty())
return;
if (D && kind == PrintStructureKind::GenericParameter) {
assert(isa<ValueDecl>(D) && "unexpected non-value decl for param");
openTagWithUSRForDecl(tag, cast<ValueDecl>(D));
} else {
openTag(tag);
}
}
void printStructurePost(PrintStructureKind kind, const Decl *D) override {
if (isIgnoredPrintStructureKind(kind))
return;
if (kind == PrintStructureKind::TupleElement ||
kind == PrintStructureKind::TupleType) {
auto prev = contextStack.pop_back_val();
(void)prev;
fixupTuple(kind);
assert(prev.is(kind) && "unmatched printStructurePre");
} else {
assert(contextStack.back().is(kind) && "unmatched printStructurePre");
contextStack.pop_back();
}
auto tag = getTagForParameter(kind);
if (!tag.empty())
closeTag(tag);
}
void printNamePre(PrintNameContext context) override {
auto tag = getTagForPrintNameContext(context);
if (!tag.empty())
openTag(tag);
}
void printNamePost(PrintNameContext context) override {
auto tag = getTagForPrintNameContext(context);
if (!tag.empty())
closeTag(tag);
}
void printTypeRef(
Type T, const TypeDecl *TD, Identifier name,
PrintNameContext NameContext = PrintNameContext::Normal) override {
auto tag = getTagForDecl(TD, /*isRef=*/true);
openTagWithUSRForDecl(tag, TD);
insideRef = true;
XMLEscapingPrinter::printTypeRef(T, TD, name, NameContext);
insideRef = false;
closeTag(tag);
}
// MARK: Convenience functions for printing.
void openTag(StringRef tag) { OS << "<" << tag << ">"; }
void closeTag(StringRef tag) { OS << "</" << tag << ">"; }
void openTagWithUSRForDecl(StringRef tag, const ValueDecl *VD) {
OS << "<" << tag << " usr=\"";
SwiftLangSupport::printUSR(VD, OS);
OS << "\">";
}
// MARK: Misc.
StringRef getTypeTagForCurrentContext() const {
if (contextStack.empty())
return "";
static StringRef parameterTypeTag = "decl.var.parameter.type";
static StringRef genericParamTypeTag = "decl.generic_type_param.constraint";
auto context = contextStack.back();
if (context.is(PrintStructureKind::FunctionParameter))
return parameterTypeTag;
if (context.is(PrintStructureKind::GenericParameter))
return genericParamTypeTag;
if (context.is(PrintStructureKind::TupleElement))
return "tuple.element.type";
if (context.getPrintStructureKind().has_value() || context.isType())
return "";
assert(context.getDecl() && "unexpected context kind");
switch (context.getDecl()->getKind()) {
case DeclKind::Param:
return parameterTypeTag;
case DeclKind::GenericTypeParam:
return genericParamTypeTag;
case DeclKind::Var:
return "decl.var.type";
case DeclKind::Subscript:
case DeclKind::Func:
default:
return "";
}
}
StringRef getTagForPrintNameContext(PrintNameContext context) {
if (insideRef)
return "";
bool insideParam =
!contextStack.empty() &&
contextStack.back().is(PrintStructureKind::FunctionParameter);
switch (context) {
case PrintNameContext::FunctionParameterExternal:
return ExternalParamNameTag;
case PrintNameContext::FunctionParameterLocal:
return LocalParamNameTag;
case PrintNameContext::TupleElement:
if (insideParam)
return ExternalParamNameTag;
return "tuple.element.argument_label";
case PrintNameContext::Keyword:
case PrintNameContext::IntroducerKeyword:
return SyntaxKeywordTag;
case PrintNameContext::GenericParameter:
return GenericParamNameTag;
case PrintNameContext::Attribute:
return "syntaxtype.attribute.name";
default:
return "";
}
}
/// 'Fix' a tuple or tuple element structure kind to be a function parameter
/// or function type if we are currently inside a function type. This
/// simplifies functions that need to differentiate a tuple from the input
/// part of a function type.
void fixupTuple(PrintStructureKind &kind) {
assert(kind == PrintStructureKind::TupleElement ||
kind == PrintStructureKind::TupleType);
// Skip over 'type's in the context stack.
for (auto I = contextStack.rbegin(), E = contextStack.rend(); I != E; ++I) {
if (I->is(PrintStructureKind::FunctionType)) {
if (kind == PrintStructureKind::TupleElement)
kind = PrintStructureKind::FunctionParameter;
else
kind = PrintStructureKind::FunctionType;
break;
} else if (!I->isType()) {
break;
}
}
}
private:
/// A stack of contexts being printed, used to determine the context for
/// subsequent ASTPrinter callbacks.
llvm::SmallVector<PrintContext, 3> contextStack;
bool insideRef = false;
};
} // end anonymous namespace
static Type findBaseTypeForReplacingArchetype(const ValueDecl *VD, const Type Ty) {
if (Ty.isNull())
return Type();
// Find the nominal type decl related to VD.
if (!VD->getDeclContext()->isTypeContext())
return Type();
return Ty->getRValueType()->getInOutObjectType()->getMetatypeInstanceType();
}
static void printAnnotatedDeclaration(const ValueDecl *VD,
const Type BaseTy,
raw_ostream &OS) {
AnnotatedDeclarationPrinter Printer(OS);
PrintOptions PO = PrintOptions::printQuickHelpDeclaration();
if (BaseTy) {
PO.setBaseType(BaseTy);
PO.PrintAsMember = true;
}
// If it's implicit, try to find an overridden ValueDecl that's not implicit.
// This will ensure we can properly annotate TypeRepr with a usr
// in AnnotatedDeclarationPrinter.
while (VD->isImplicit() && VD->getOverriddenDecl())
VD = VD->getOverriddenDecl();
// VD may be a compiler synthesized member, constructor, or shorthand argument
// so always print it even if it's implicit.
//
// FIXME: Update PrintOptions::printQuickHelpDeclaration to print implicit
// decls by default. That causes issues due to newlines being printed before
// implicit OpaqueTypeDecls at time of writing.
PO.TreatAsExplicitDeclList.push_back(VD);
// Wrap this up in XML, as that's what we'll use for documentation comments.
OS<<"<Declaration>";
VD->print(Printer, PO);
OS<<"</Declaration>";
}
void SwiftLangSupport::printFullyAnnotatedDeclaration(const ValueDecl *VD,
Type BaseTy,
raw_ostream &OS) {
FullyAnnotatedDeclarationPrinter Printer(OS);
PrintOptions PO = PrintOptions::printQuickHelpDeclaration();
if (BaseTy) {
PO.setBaseType(BaseTy);
PO.PrintAsMember = true;
}
// If it's implicit, try to find an overridden ValueDecl that's not implicit.
// This will ensure we can properly annotate TypeRepr with a usr
// in AnnotatedDeclarationPrinter.
while (VD->isImplicit() && VD->getOverriddenDecl())
VD = VD->getOverriddenDecl();
// VD may be a compiler synthesized member, constructor, or shorthand argument
// so always print it even if it's implicit.
//
// FIXME: Update PrintOptions::printQuickHelpDeclaration to print implicit
// decls by default. That causes issues due to newlines being printed before
// implicit OpaqueTypeDecls at time of writing.
PO.TreatAsExplicitDeclList.push_back(VD);
VD->print(Printer, PO);
}
void SwiftLangSupport::printFullyAnnotatedDeclaration(const ExtensionDecl *ED,
raw_ostream &OS) {
FullyAnnotatedDeclarationPrinter Printer(OS);
PrintOptions PO = PrintOptions::printQuickHelpDeclaration();
ED->print(Printer, PO);
}
void SwiftLangSupport::printFullyAnnotatedSynthesizedDeclaration(
const swift::ValueDecl *VD, TypeOrExtensionDecl Target,
llvm::raw_ostream &OS) {
FullyAnnotatedDeclarationPrinter Printer(OS);
PrintOptions PO = PrintOptions::printQuickHelpDeclaration();
PO.initForSynthesizedExtension(Target);
PO.PrintAsMember = true;
VD->print(Printer, PO);
}
void SwiftLangSupport::printFullyAnnotatedSynthesizedDeclaration(
const swift::ExtensionDecl *ED, TypeOrExtensionDecl Target,
llvm::raw_ostream &OS) {
FullyAnnotatedDeclarationPrinter Printer(OS);
PrintOptions PO = PrintOptions::printQuickHelpDeclaration();
PO.initForSynthesizedExtension(Target);
ED->print(Printer, PO);
}
template <typename FnTy>
static void walkRelatedDecls(const ValueDecl *VD, const FnTy &Fn) {
if (isa<ParamDecl>(VD))
return; // Parameters don't have interesting related declarations.
auto &ctx = VD->getASTContext();
llvm::SmallDenseMap<DeclName, unsigned, 16> NamesSeen;
++NamesSeen[VD->getName()];
auto *DC = VD->getDeclContext();
bool typeLookup = DC->isTypeContext();
SmallVector<ValueDecl *, 4> results;
if (typeLookup) {
auto type = DC->getDeclaredInterfaceType();
if (!type->is<ErrorType>()) {
DC->lookupQualified(type, DeclNameRef(VD->getBaseName()),
VD->getLoc(), NL_QualifiedDefault,
results);
}
} else {
namelookup::lookupInModule(DC->getModuleScopeContext(),
VD->getBaseName(), results,
NLKind::UnqualifiedLookup,
namelookup::ResolutionKind::Overloadable,
DC->getModuleScopeContext(),
VD->getLoc(),
NL_UnqualifiedDefault);
}
SmallVector<ValueDecl *, 8> RelatedDecls;
for (auto result : results) {
if (result->getAttrs().isUnavailable(ctx))
continue;
if (result != VD) {
++NamesSeen[result->getName()];
RelatedDecls.push_back(result);
}
}
// Now provide the results along with whether the name is duplicate or not.
for (auto result : RelatedDecls)
Fn(result, typeLookup, NamesSeen[result->getName()] > 1);
}
//===----------------------------------------------------------------------===//
// SwiftLangSupport::getCursorInfo
//===----------------------------------------------------------------------===//
static StringRef getSourceToken(unsigned Offset,
ImmutableTextSnapshotRef Snap) {
auto MemBuf = Snap->getBuffer()->getInternalBuffer();
// FIXME: Invalid offset shouldn't reach here.
if (Offset >= MemBuf->getBufferSize())
return StringRef();
SourceManager SM;
auto MemBufRef = llvm::MemoryBuffer::getMemBuffer(MemBuf->getBuffer(),
MemBuf->getBufferIdentifier());
auto BufId = SM.addNewSourceBuffer(std::move(MemBufRef));
SourceLoc Loc = SM.getLocForOffset(BufId, Offset);
return Lexer::getTokenAtLocation(SM, Loc).getText();
}
static std::optional<unsigned>
mapOffsetToOlderSnapshot(unsigned Offset, ImmutableTextSnapshotRef NewSnap,
ImmutableTextSnapshotRef OldSnap) {
SmallVector<ReplaceImmutableTextUpdateRef, 16> Updates;
OldSnap->foreachReplaceUntil(NewSnap,
[&](ReplaceImmutableTextUpdateRef Upd)->bool {
Updates.push_back(Upd);
return true;
});
// Walk the updates backwards and "undo" them.
for (auto I = Updates.rbegin(), E = Updates.rend(); I != E; ++I) {
auto Upd = *I;
if (Upd->getByteOffset() <= Offset &&
Offset < Upd->getByteOffset() + Upd->getText().size())
return std::nullopt; // Offset is part of newly inserted text.
if (Upd->getByteOffset() <= Offset) {
Offset += Upd->getLength(); // "bring back" what was removed.
Offset -= Upd->getText().size(); // "remove" what was added.
}
}
return Offset;
}
static std::optional<unsigned>
mapOffsetToNewerSnapshot(unsigned Offset, ImmutableTextSnapshotRef OldSnap,
ImmutableTextSnapshotRef NewSnap) {
bool Completed = OldSnap->foreachReplaceUntil(NewSnap,
[&](ReplaceImmutableTextUpdateRef Upd)->bool {
if (Upd->getByteOffset() <= Offset &&
Offset < Upd->getByteOffset() + Upd->getLength())
return false; // Offset is part of removed text.
if (Upd->getByteOffset() <= Offset) {
Offset += Upd->getText().size();
Offset -= Upd->getLength();
}
return true;
});
if (Completed)
return Offset;
return std::nullopt;
}
/// Tries to remap the location from a previous snapshot to the latest one and
/// then sets the location's line and column.
static void mapLocToLatestSnapshot(
SwiftLangSupport &Lang, LocationInfo &Location,
ArrayRef<ImmutableTextSnapshotRef> PreviousASTSnaps) {
auto EditorDoc = Lang.getEditorDocuments()->findByPath(Location.Filename,
/*IsRealpath=*/true);
if (!EditorDoc)
return;
ImmutableTextSnapshotRef LatestSnap = EditorDoc->getLatestSnapshot();
if (!LatestSnap)
return;
for (auto &PrevSnap : PreviousASTSnaps) {
if (PrevSnap->isFromSameBuffer(LatestSnap)) {
if (PrevSnap->getStamp() == LatestSnap->getStamp())
break;
auto OptBegin = mapOffsetToNewerSnapshot(Location.Offset,
PrevSnap, LatestSnap);
if (!OptBegin.has_value()) {
Location.Filename = StringRef();
return;
}
auto OptEnd = mapOffsetToNewerSnapshot(Location.Offset +
Location.Length,
PrevSnap, LatestSnap);
if (!OptEnd.has_value()) {
Location.Filename = StringRef();
return;
}
Location.Offset = *OptBegin;
Location.Length = *OptEnd - *OptBegin;
}
}
std::tie(Location.Line, Location.Column) =
LatestSnap->getBuffer()->getLineAndColumn(Location.Offset);
}
/// Returns true for error.
static bool passCursorInfoForModule(ModuleEntity Mod,
SwiftInterfaceGenMap &IFaceGenContexts,
const CompilerInvocation &Invok,
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) {
std::string FullName = Mod.getFullName();
SmallVector<CursorSymbolInfo, 1> Symbols;
SmallVector<StringRef, 4> ModuleGroups;
CursorSymbolInfo &Symbol = Symbols.emplace_back();
Symbol.Kind = SwiftLangSupport::getUIDForModuleRef();
Symbol.Name = Mod.getName();
Symbol.ModuleName = FullName;
if (auto IFaceGenRef = IFaceGenContexts.find(Symbol.ModuleName, Invok))
Symbol.ModuleInterfaceName = IFaceGenRef->getDocumentName();
Symbol.IsSystem = Mod.isNonUserModule();
if (auto MD = Mod.getAsSwiftModule()) {
ide::collectModuleGroups(const_cast<ModuleDecl *>(MD), ModuleGroups);
Symbol.ModuleGroupArray = llvm::ArrayRef(ModuleGroups);
}
CursorInfoData Data;
Data.Symbols = Symbols;
Receiver(RequestResult<CursorInfoData>::fromResult(Data));
return false;
}
static void addRefactorings(
SmallVectorImpl<RefactoringInfo> &intoLangInfos,
const SmallVectorImpl<RefactorAvailabilityInfo> &availableInfos) {
for (auto info : availableInfos) {
auto uid = SwiftLangSupport::getUIDForRefactoringKind(info.Kind);
bool hasRefactoringWithSameUID =
llvm::any_of(intoLangInfos, [&](RefactoringInfo &existing) {
return uid == existing.Kind;
});
if (hasRefactoringWithSameUID)
continue;
intoLangInfos.emplace_back(
uid, ide::getDescriptiveRefactoringKindName(info.Kind),
ide::getDescriptiveRenameUnavailableReason(info.AvailableKind));
}
}
static std::optional<unsigned> getParamParentNameOffset(const ValueDecl *VD,
SourceLoc Cursor) {
if (Cursor.isInvalid())
return std::nullopt;
SourceLoc Loc;
if (auto PD = dyn_cast<ParamDecl>(VD)) {
// Avoid returning parent loc for internal-only names.
if (PD->getArgumentNameLoc().isValid() && PD->getArgumentNameLoc() != Cursor)
return std::nullopt;
auto *DC = PD->getDeclContext();
switch (DC->getContextKind()) {
case DeclContextKind::SubscriptDecl:
Loc = cast<SubscriptDecl>(DC)->getNameLoc();
break;
case DeclContextKind::AbstractFunctionDecl:
Loc = cast<AbstractFunctionDecl>(DC)->getNameLoc();
break;
default:
break;
}
}
if (Loc.isInvalid())
return std::nullopt;
auto &SM = VD->getASTContext().SourceMgr;
return SM.getLocOffsetInBuffer(Loc, SM.findBufferContainingLoc(Loc));
}
static StringRef getModuleName(const ValueDecl *VD,
llvm::BumpPtrAllocator &Allocator) {
ASTContext &Ctx = VD->getASTContext();
ClangImporter *Importer =
static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
if (auto ClangNode = VD->getClangNode()) {
if (const auto *ClangMod = Importer->getClangOwningModule(ClangNode))
return StringRef(ClangMod->getFullModuleName()).copy(Allocator);
return "";
}
ModuleDecl *MD = VD->getModuleContext();
// If the decl is from a cross-import overlay module, report the
// overlay's declaring module as the owning module.
if (ModuleDecl *Declaring = MD->getDeclaringModuleIfCrossImportOverlay())
MD = Declaring;
return MD->getNameStr();
}
struct DeclInfo {
ValueDecl *VD;
/// If not null, a solution-specific interface type of `VD`. This allows
/// us to produce results for declarations where the soluion has not been
/// applied to the AST, eg. because the declaration has an ambiguous type
/// or because it occurs inside a closure that has an error, which prevents
/// the constraint system solution to be applied to the AST.
Type SolutionSpecificInterfaceType;
Type ContainerType;
bool IsRef;
bool IsDynamic;
ArrayRef<NominalTypeDecl *> ReceiverTypes;
/// If VD is a synthesized property wrapper backing storage (_foo) or
/// projected value ($foo) of a property (foo), the property instead.
/// Otherwise, VD.
const ValueDecl *OriginalProperty = nullptr;
bool Unavailable = true;
Type BaseType;
/// Whether the \c VD is in a synthesized extension of \c BaseType
bool InSynthesizedExtension = false;
DeclInfo(ValueDecl *VD, Type SolutionSpecificInterfaceType,
Type ContainerType, bool IsRef, bool IsDynamic,
ArrayRef<NominalTypeDecl *> ReceiverTypes,
const CompilerInvocation &Invoc)
: VD(VD), SolutionSpecificInterfaceType(SolutionSpecificInterfaceType),
ContainerType(ContainerType), IsRef(IsRef), IsDynamic(IsDynamic),
ReceiverTypes(ReceiverTypes) {
if (VD == nullptr)
return;
// The synthesized properties $foo and _foo aren't unavailable even if
// the original property foo is, so check them rather than the original
// property.
Unavailable = AvailableAttr::isUnavailable(VD);
// No point computing the rest since they won't be used anyway.
if (Unavailable)
return;
OriginalProperty = VD;
if (auto *VarD = dyn_cast<VarDecl>(VD)) {
if (auto *Wrapped = VarD->getOriginalWrappedProperty())
OriginalProperty = Wrapped;
}
BaseType = findBaseTypeForReplacingArchetype(VD, ContainerType);
if (BaseType) {
if (auto *Target = BaseType->getAnyNominal()) {
SynthesizedExtensionAnalyzer Analyzer(
Target, PrintOptions::printModuleInterface(
Invoc.getFrontendOptions().PrintFullConvention));
InSynthesizedExtension = Analyzer.isInSynthesizedExtension(VD);
}
}
}
};
static StringRef copyAndClearString(llvm::BumpPtrAllocator &Allocator,
SmallVectorImpl<char> &Str) {
auto Ref = StringRef(Str.data(), Str.size()).copy(Allocator);
Str.clear();
return Ref;
}
template <typename T>
static ArrayRef<T> copyAndClearArray(llvm::BumpPtrAllocator &Allocator,
SmallVectorImpl<T> &Array) {
auto Ref = copyArray(Allocator, llvm::ArrayRef(Array));
Array.clear();
return Ref;
}
static void setLocationInfoForClangNode(ClangNode ClangNode,
ClangImporter *Importer,
LocationInfo &Location) {
clang::ASTContext &ClangCtx = Importer->getClangASTContext();
clang::SourceManager &ClangSM = ClangCtx.getSourceManager();
clang::SourceRange SR = ClangNode.getLocation();
if (auto MD =
dyn_cast_or_null<clang::ObjCMethodDecl>(ClangNode.getAsDecl())) {
SR = clang::SourceRange(MD->getSelectorStartLoc(),
MD->getDeclaratorEndLoc());
}
clang::CharSourceRange CharRange =
clang::Lexer::makeFileCharRange(clang::CharSourceRange::getTokenRange(SR),
ClangSM, ClangCtx.getLangOpts());
if (CharRange.isInvalid())
return;
std::pair<clang::FileID, unsigned> Decomp =
ClangSM.getDecomposedLoc(CharRange.getBegin());
if (!Decomp.first.isInvalid()) {
if (auto FE = ClangSM.getFileEntryForID(Decomp.first)) {
Location.Filename = FE->getName();
std::pair<clang::FileID, unsigned> EndDecomp =
ClangSM.getDecomposedLoc(CharRange.getEnd());
Location.Offset = Decomp.second;
Location.Length = EndDecomp.second - Decomp.second;
Location.Line = ClangSM.getLineNumber(Decomp.first, Decomp.second);
Location.Column = ClangSM.getColumnNumber(Decomp.first, Decomp.second);
}
}
}
static unsigned getCharLength(SourceManager &SM, SourceRange TokenRange) {
SourceLoc CharEndLoc = Lexer::getLocForEndOfToken(SM, TokenRange.End);
return SM.getByteDistance(TokenRange.Start, CharEndLoc);
}
static void setLocationInfo(const ValueDecl *VD,
LocationInfo &Location) {
ASTContext &Ctx = VD->getASTContext();
SourceManager &SM = Ctx.SourceMgr;
auto ClangNode = VD->getClangNode();
auto Loc = VD->getLoc(/*SerializedOK=*/true);
if (Loc.isValid()) {
auto getSignatureRange =
[&](const ValueDecl *VD) -> std::optional<unsigned> {
if (auto FD = dyn_cast<AbstractFunctionDecl>(VD)) {
SourceRange R = FD->getSignatureSourceRange();
if (R.isValid())
return getCharLength(SM, R);
}
return std::nullopt;
};
unsigned NameLen;
if (auto SigLen = getSignatureRange(VD)) {
NameLen = SigLen.value();
} else if (VD->hasName()) {
NameLen = VD->getBaseName().userFacingName().size();
} else {
NameLen = getCharLength(SM, Loc);
}
auto [DeclBufID, DeclLoc] =
VD->getModuleContext()->getOriginalLocation(Loc);
Location.Filename = SM.getIdentifierForBuffer(DeclBufID);
Location.Offset = SM.getLocOffsetInBuffer(DeclLoc, DeclBufID);
Location.Length = NameLen;
std::tie(Location.Line, Location.Column) =
SM.getLineAndColumnInBuffer(DeclLoc, DeclBufID);
if (auto GeneratedSourceInfo = SM.getGeneratedSourceInfo(DeclBufID)) {
if (GeneratedSourceInfo->kind ==
GeneratedSourceInfo::ReplacedFunctionBody) {
// The location was in a temporary source buffer that just contains the
// function body and which we created while reusing the ASTContext for
// the rest of the file. Map the location back to the original file.
unsigned OriginalBufID = SM.findBufferContainingLoc(
GeneratedSourceInfo->originalSourceRange.getStart());
auto OriginalStartOffset = SM.getLocOffsetInBuffer(
GeneratedSourceInfo->originalSourceRange.getStart(), OriginalBufID);
auto GeneratedStartOffset = SM.getLocOffsetInBuffer(
GeneratedSourceInfo->generatedSourceRange.getStart(), DeclBufID);
Location.Offset += OriginalStartOffset - GeneratedStartOffset;
std::tie(Location.Line, Location.Column) =
SM.getPresumedLineAndColumnForLoc(DeclLoc, DeclBufID);
}
}
} else if (ClangNode) {
ClangImporter *Importer =
static_cast<ClangImporter*>(Ctx.getClangModuleLoader());
setLocationInfoForClangNode(ClangNode, Importer, Location);
}
}
static llvm::Error
fillSymbolInfo(CursorSymbolInfo &Symbol, const DeclInfo &DInfo,
SourceLoc CursorLoc, bool AddSymbolGraph, SwiftLangSupport &Lang,
const CompilerInvocation &Invoc,
ArrayRef<ImmutableTextSnapshotRef> PreviousSnaps,
llvm::BumpPtrAllocator &Allocator) {
// Override the type of `DInfo.VD` while retrieving symbol information.
// Ideally, we would pass a customizable `getInterfaceType` function to all
// functions that operate on VD but doing so is not viable - USR generation,
// AST printing, symbol graph and probably more have been designed with the
// assumption that VD->getInterfaceType() returns the fixed interface type
// of the declaration.
Type OriginalInterfaceType = DInfo.VD->getInterfaceType();
if (DInfo.SolutionSpecificInterfaceType) {
DInfo.VD->setInterfaceType(DInfo.SolutionSpecificInterfaceType);
}
SWIFT_DEFER { DInfo.VD->setInterfaceType(OriginalInterfaceType); };
SmallString<256> Buffer;
SmallVector<StringRef, 4> Strings;
llvm::raw_svector_ostream OS(Buffer);
Symbol.DeclarationLang = SwiftLangSupport::getUIDForDeclLanguage(DInfo.VD);
Symbol.Kind = SwiftLangSupport::getUIDForDecl(DInfo.VD, DInfo.IsRef);
SwiftLangSupport::printDisplayName(DInfo.VD, OS);
Symbol.Name = copyAndClearString(Allocator, Buffer);
SwiftLangSupport::printUSR(DInfo.OriginalProperty, OS);
if (DInfo.InSynthesizedExtension) {
OS << LangSupport::SynthesizedUSRSeparator;
SwiftLangSupport::printUSR(DInfo.BaseType->getAnyNominal(), OS);
}
Symbol.USR = copyAndClearString(Allocator, Buffer);
{
PrintOptions Options;
Options.PrintTypeAliasUnderlyingType = true;
DInfo.VD->getInterfaceType().print(OS, Options);
}
Symbol.TypeName = copyAndClearString(Allocator, Buffer);
// ParameterizedProtocolType should always be wrapped in ExistentialType and
// cannot be mangled on its own.
// But ParameterizedProtocolType can currently occur in 'typealias'
// declarations. rdar://99176683
// To avoid crashing in USR generation, return an error for now.
if (auto Ty = DInfo.VD->getInterfaceType()) {
while (auto MetaTy = Ty->getAs<MetatypeType>()) {
Ty = MetaTy->getInstanceType();
}
if (Ty && Ty->getCanonicalType()->is<ParameterizedProtocolType>()) {
return llvm::createStringError(
llvm::inconvertibleErrorCode(),
"Cannot mangle USR for ParameterizedProtocolType without 'any'.");
}
}
SwiftLangSupport::printDeclTypeUSR(DInfo.VD, OS);
Symbol.TypeUSR = copyAndClearString(Allocator, Buffer);
if (DInfo.ContainerType && !DInfo.ContainerType->hasArchetype()) {
SwiftLangSupport::printTypeUSR(DInfo.ContainerType, OS);
}
Symbol.ContainerTypeUSR = copyAndClearString(Allocator, Buffer);
ide::getRawDocumentationComment(DInfo.OriginalProperty, OS);
Symbol.DocComment = copyAndClearString(Allocator, Buffer);
ide::getDocumentationCommentAsXML(DInfo.OriginalProperty, OS);
Symbol.DocCommentAsXML = copyAndClearString(Allocator, Buffer);
{
auto *Group = DInfo.InSynthesizedExtension ? DInfo.BaseType->getAnyNominal()
: DInfo.VD;
if (auto Name = Group->getGroupName())
Symbol.GroupName = Name.value();
}
ide::getLocalizationKey(DInfo.VD, OS);
Symbol.LocalizationKey = copyAndClearString(Allocator, Buffer);
printAnnotatedDeclaration(DInfo.VD, DInfo.BaseType, OS);
Symbol.AnnotatedDeclaration = copyAndClearString(Allocator, Buffer);
SwiftLangSupport::printFullyAnnotatedDeclaration(DInfo.VD, DInfo.BaseType,
OS);
Symbol.FullyAnnotatedDeclaration = copyAndClearString(Allocator, Buffer);
if (AddSymbolGraph) {
SmallVector<symbolgraphgen::PathComponent, 4> PathComponents;
SmallVector<symbolgraphgen::FragmentInfo, 8> FragmentInfos;
symbolgraphgen::SymbolGraphOptions Options;
Options.Target = Invoc.getLangOptions().Target;
Options.MinimumAccessLevel = AccessLevel::Private;
Options.IncludeSPISymbols = true;
Options.IncludeClangDocs = true;
Options.PrintPrivateStdlibSymbols = true;
symbolgraphgen::printSymbolGraphForDecl(DInfo.VD, DInfo.BaseType,
DInfo.InSynthesizedExtension,
Options, OS, PathComponents,
FragmentInfos);
Symbol.SymbolGraph = copyAndClearString(Allocator, Buffer);
SmallVector<ParentInfo, 4> Parents;
for (auto &Component : PathComponents) {
SwiftLangSupport::printUSR(Component.VD, OS);
Parents.emplace_back(Component.Title.str().copy(Allocator),
Component.Kind,
copyAndClearString(Allocator, Buffer));
};
Symbol.ParentContexts = copyArray(Allocator, llvm::ArrayRef(Parents));
SmallVector<ReferencedDeclInfo, 8> ReferencedDecls;
for (auto &FI: FragmentInfos) {
SmallVector<ParentInfo, 4> FIParents;
for (auto &Component: FI.ParentContexts) {
SwiftLangSupport::printUSR(Component.VD, OS);
FIParents.emplace_back(Component.Title.str().copy(Allocator),
Component.Kind,
copyAndClearString(Allocator, Buffer));
}
ASTContext &Ctx = FI.VD->getASTContext();
StringRef Filename = "";
if (auto Loc = FI.VD->getLoc(/*SerializedOK=*/true)) {
Filename = Ctx.SourceMgr.getDisplayNameForLoc(Loc);
} else if (auto ClangNode = FI.VD->getClangNode()) {
auto Loc = ClangNode.getLocation();
if (Loc.isValid()) {
Filename = Ctx.getClangModuleLoader()->getClangASTContext()
.getSourceManager()
.getFilename(Loc);
}
}
SwiftLangSupport::printUSR(FI.VD, OS);
ReferencedDecls.emplace_back(
copyAndClearString(Allocator, Buffer),
SwiftLangSupport::getUIDForDeclLanguage(FI.VD),
swift::getAccessLevelSpelling(FI.VD->getFormalAccess()), Filename,
getModuleName(FI.VD, Allocator),
FI.VD->getModuleContext()->isNonUserModule(), FI.VD->isSPI(),
copyArray(Allocator, llvm::ArrayRef(FIParents)));
}
Symbol.ReferencedSymbols =
copyArray(Allocator, llvm::ArrayRef(ReferencedDecls));
}
Symbol.ModuleName = getModuleName(DInfo.VD, Allocator);
if (auto IFaceGenRef =
Lang.getIFaceGenContexts().find(Symbol.ModuleName, Invoc))
Symbol.ModuleInterfaceName = IFaceGenRef->getDocumentName();
setLocationInfo(DInfo.OriginalProperty, Symbol.Location);
if (!Symbol.Location.Filename.empty()) {
mapLocToLatestSnapshot(Lang, Symbol.Location, PreviousSnaps);
if (Symbol.Location.Filename.empty()) {
return llvm::createStringError(
llvm::inconvertibleErrorCode(),
"Failed to remap declaration to latest snapshot.");
}
}
ide::walkOverriddenDecls(
DInfo.VD,
[&](llvm::PointerUnion<const ValueDecl *, const clang::NamedDecl *> D) {
// Could have junk in from previous failing USR print
Buffer.clear();
if (auto VD = D.dyn_cast<const ValueDecl *>()) {
if (SwiftLangSupport::printUSR(VD, OS))
return;
} else {
if (clang::index::generateUSRForDecl(
D.get<const clang::NamedDecl *>(), Buffer))
return;
}
Strings.push_back(copyAndClearString(Allocator, Buffer));
});
Symbol.OverrideUSRs = copyAndClearArray(Allocator, Strings);
walkRelatedDecls(DInfo.VD, [&](const ValueDecl *RelatedDecl,
bool UseOriginalBase, bool DuplicateName) {
OS << "<RelatedName usr=\"";
SwiftLangSupport::printUSR(RelatedDecl, OS);
OS << "\">";
if (isa<AbstractFunctionDecl>(RelatedDecl) && DuplicateName) {
// Related decls are generally overloads, so print parameter types to
// differentiate them.
PrintOptions PO;
PO.SkipAttributes = true;
PO.PrintStaticKeyword = false;
PO.PrintSelfAccessKindKeyword = false;
PO.SkipIntroducerKeywords = true;
PO.ArgAndParamPrinting =
PrintOptions::ArgAndParamPrintingMode::ArgumentOnly;
XMLEscapingPrinter Printer(OS);
if (UseOriginalBase && DInfo.BaseType) {
PO.setBaseType(DInfo.BaseType);
PO.PrintAsMember = true;
}
RelatedDecl->print(Printer, PO);
} else {
SmallString<128> RelatedBuffer;
llvm::raw_svector_ostream RelatedOS(RelatedBuffer);
SwiftLangSupport::printDisplayName(RelatedDecl, RelatedOS);
swift::markup::appendWithXMLEscaping(OS, RelatedBuffer);
}
OS << "</RelatedName>";
Strings.push_back(copyAndClearString(Allocator, Buffer));
});
Symbol.AnnotatedRelatedDeclarations = copyAndClearArray(Allocator, Strings);
for (auto *ReceiverTy : DInfo.ReceiverTypes) {
if (!SwiftLangSupport::printUSR(ReceiverTy, OS))
Strings.push_back(copyAndClearString(Allocator, Buffer));
}
Symbol.ReceiverUSRs = copyAndClearArray(Allocator, Strings);
Symbol.IsSystem = DInfo.VD->getModuleContext()->isNonUserModule();
Symbol.IsDynamic = DInfo.IsDynamic;
Symbol.IsSynthesized = DInfo.VD->isImplicit();
Symbol.ParentNameOffset = getParamParentNameOffset(DInfo.VD, CursorLoc);
return llvm::Error::success();
}
// If \p E is a literal, returns the declaration that should be reported by
// cursor info for that initializer.
static ValueDecl *getCursorInfoDeclForLiteral(Expr *E) {
if (auto *CollectionLit = dyn_cast<CollectionExpr>(E)) {
return CollectionLit->getInitializer().getDecl();
}
LiteralExpr* LitExpr = dyn_cast<LiteralExpr>(E);
if (!LitExpr) {
return nullptr;
}
bool IsObjectLiteral = isa<ObjectLiteralExpr>(E);
if (!IsObjectLiteral && LitExpr->getInitializer().getDecl()) {
return LitExpr->getInitializer().getDecl();
}
// We shouldn’t report the builtin initializer to the user because it’s
// underscored and not visible. Instead, return the type of the literal.
if (IsObjectLiteral || isa<BuiltinLiteralExpr>(E)) {
Type Ty = E->getType();
if (!Ty) {
return nullptr;
}
auto NominalTy = Ty->getAs<NominalOrBoundGenericNominalType>();
if (!NominalTy) {
return nullptr;
}
return NominalTy->getDecl();
}
return nullptr;
}
static bool addCursorInfoForLiteral(
CursorInfoData &Data, Expr *LitExpr, SwiftLangSupport &Lang,
const CompilerInvocation &CompInvoc, SourceLoc CursorLoc,
ArrayRef<ImmutableTextSnapshotRef> PreviousSnaps) {
if (!LitExpr) {
return false;
}
ValueDecl *Decl = getCursorInfoDeclForLiteral(LitExpr);
if (!Decl) {
return false;
}
auto &Symbol = Data.Symbols.emplace_back();
DeclInfo Info(Decl, nullptr, nullptr, true, false, {}, CompInvoc);
auto Err = fillSymbolInfo(Symbol, Info, CursorLoc, false, Lang, CompInvoc,
PreviousSnaps, Data.Allocator);
bool Success = true;
llvm::handleAllErrors(std::move(Err), [&](const llvm::StringError &E) {
Data.InternalDiagnostic = copyCString(E.getMessage(), Data.Allocator);
Data.Symbols.pop_back();
Success = false;
});
return Success;
}
static bool
addCursorInfoForDecl(CursorInfoData &Data, ResolvedValueRefCursorInfoPtr Info,
bool AddRefactorings, bool AddSymbolGraph,
SwiftLangSupport &Lang, const CompilerInvocation &Invoc,
std::string &Diagnostic,
ArrayRef<ImmutableTextSnapshotRef> PreviousSnaps) {
DeclInfo OrigInfo(Info->getValueD(), Info->getSolutionSpecificInterfaceType(),
Info->getContainerType(), Info->isRef(), Info->isDynamic(),
Info->getReceiverTypes(), Invoc);
DeclInfo CtorTypeInfo(Info->getCtorTyRef(), Type(), Type(), true, false,
ArrayRef<NominalTypeDecl *>(), Invoc);
DeclInfo &MainInfo = CtorTypeInfo.VD ? CtorTypeInfo : OrigInfo;
if (MainInfo.Unavailable) {
Diagnostic = "Unavailable in the current compilation context.";
return false;
}
CursorSymbolInfo &MainSymbol = Data.Symbols.emplace_back();
// The primary result for constructor calls, eg. `MyType()` should be
// the type itself, rather than the constructor. The constructor will be
// added as a secondary result.
if (auto Err =
fillSymbolInfo(MainSymbol, MainInfo, Info->getLoc(), AddSymbolGraph,
Lang, Invoc, PreviousSnaps, Data.Allocator)) {
llvm::handleAllErrors(std::move(Err), [&](const llvm::StringError &E) {
Diagnostic = E.message();
});
return false;
}
if (MainInfo.VD != OrigInfo.VD && !OrigInfo.Unavailable) {
CursorSymbolInfo &CtorSymbol = Data.Symbols.emplace_back();
if (auto Err =
fillSymbolInfo(CtorSymbol, OrigInfo, Info->getLoc(), AddSymbolGraph,
Lang, Invoc, PreviousSnaps, Data.Allocator)) {
// Ignore but make sure to remove the partially-filled symbol
llvm::handleAllErrors(std::move(Err), [](const llvm::StringError &E) {});
Data.Symbols.pop_back();
}
}
// Add in shadowed declarations if on a decl. For references just go to the
// actual declaration.
if (!Info->isRef()) {
for (auto D : Info->getShorthandShadowedDecls()) {
CursorSymbolInfo &SymbolInfo = Data.Symbols.emplace_back();
DeclInfo DInfo(D, Type(), Type(), /*IsRef=*/true, /*IsDynamic=*/false,
ArrayRef<NominalTypeDecl *>(), Invoc);
if (auto Err =
fillSymbolInfo(SymbolInfo, DInfo, Info->getLoc(), AddSymbolGraph,
Lang, Invoc, PreviousSnaps, Data.Allocator)) {
// Ignore but make sure to remove the partially-filled symbol
llvm::handleAllErrors(std::move(Err),
[](const llvm::StringError &E) {});
Data.Symbols.pop_back();
}
}
}
if (AddRefactorings) {
addRefactorings(Data.AvailableActions,
collectRefactorings(Info, /*ExcludeRename=*/false));
}
return true;
}
/// Returns true on success, false on error (and sets `Diagnostic` accordingly).
static bool passCursorInfoForDecl(
ResolvedValueRefCursorInfoPtr Info, bool AddRefactorings,
bool AddSymbolGraph, ArrayRef<RefactoringInfo> KnownRefactoringInfo,
SwiftLangSupport &Lang, const CompilerInvocation &Invoc,
std::string &Diagnostic, ArrayRef<ImmutableTextSnapshotRef> PreviousSnaps,
bool DidReuseAST,
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) {
CursorInfoData Data;
bool success =
addCursorInfoForDecl(Data, Info, AddRefactorings, AddSymbolGraph, Lang,
Invoc, Diagnostic, PreviousSnaps);
if (!success) {
return false;
}
Data.AvailableActions.append(KnownRefactoringInfo.begin(),
KnownRefactoringInfo.end());
Data.DidReuseAST = DidReuseAST;
Receiver(RequestResult<CursorInfoData>::fromResult(Data));
return true;
}
static clang::DeclarationName
getClangDeclarationName(const clang::NamedDecl *ND, NameTranslatingInfo &Info) {
auto &Ctx = ND->getASTContext();
auto OrigName = ND->getDeclName();
assert(SwiftLangSupport::getNameKindForUID(Info.NameKind) == NameKind::ObjC);
if (Info.BaseName.empty() == Info.ArgNames.empty()) {
// cannot have both.
return clang::DeclarationName();
}
if (!Info.BaseName.empty()) {
return clang::DeclarationName(&Ctx.Idents.get(Info.BaseName));
} else {
switch (OrigName.getNameKind()) {
case clang::DeclarationName::ObjCZeroArgSelector:
case clang::DeclarationName::ObjCOneArgSelector:
case clang::DeclarationName::ObjCMultiArgSelector:
break;
default:
return clang::DeclarationName();
}
auto OrigSel = OrigName.getObjCSelector();
unsigned NumPieces = OrigSel.isUnarySelector() ? 1 : OrigSel.getNumArgs();
if (Info.ArgNames.size() > NumPieces)
return clang::DeclarationName();
ArrayRef<StringRef> Args = llvm::ArrayRef(Info.ArgNames);
std::vector<clang::IdentifierInfo *> Pieces;
for (unsigned i = 0; i < NumPieces; ++i) {
if (i >= Info.ArgNames.size() || Info.ArgNames[i].empty()) {
Pieces.push_back(OrigSel.getIdentifierInfoForSlot(i));
} else {
StringRef T = Args[i];
Pieces.push_back(&Ctx.Idents.get(T.endswith(":") ? T.drop_back() : T));
}
}
return clang::DeclarationName(
Ctx.Selectors.getSelector(OrigSel.getNumArgs(), Pieces.data()));
}
}
static DeclName getSwiftDeclName(const ValueDecl *VD,
NameTranslatingInfo &Info) {
auto &Ctx = VD->getDeclContext()->getASTContext();
assert(SwiftLangSupport::getNameKindForUID(Info.NameKind) == NameKind::Swift);
const DeclName OrigName = VD->getName();
DeclBaseName BaseName = Info.BaseName.empty()
? OrigName.getBaseName()
: DeclBaseName(
Info.BaseName == "init"
? DeclBaseName::createConstructor()
: Ctx.getIdentifier(Info.BaseName));
auto OrigArgs = OrigName.getArgumentNames();
SmallVector<Identifier, 8> Args(OrigArgs.begin(), OrigArgs.end());
if (Info.ArgNames.size() > OrigArgs.size())
return DeclName();
for (unsigned i = 0; i < OrigArgs.size(); ++i) {
if (i < Info.ArgNames.size() && !Info.ArgNames[i].empty()) {
StringRef Arg = Info.ArgNames[i];
Args[i] = Ctx.getIdentifier(Arg == "_" ? StringRef() : Arg);
}
}
return DeclName(Ctx, BaseName, llvm::ArrayRef(Args));
}
/// Returns true on success, false on error (and sets `Diagnostic` accordingly).
static bool passNameInfoForDecl(
const ResolvedValueRefCursorInfo &CursorInfo, NameTranslatingInfo &Info,
std::string &Diagnostic,
std::function<void(const RequestResult<NameTranslatingInfo> &)> Receiver) {
auto *VD = CursorInfo.getValueD();
// If the given name is not a function name, and the cursor points to
// a constructor call, we use the type declaration instead of the init
// declaration to translate the name.
if (Info.ArgNames.empty() && !Info.IsZeroArgSelector) {
if (auto *TD = CursorInfo.getCtorTyRef()) {
VD = TD;
}
}
switch (SwiftLangSupport::getNameKindForUID(Info.NameKind)) {
case NameKind::Swift: {
NameTranslatingInfo Result;
auto DeclName = getSwiftDeclName(VD, Info);
if (!DeclName) {
Diagnostic = "Unable to resolve Swift declaration name.";
return false;
}
auto ResultPair =
swift::objc_translation::getObjCNameForSwiftDecl(VD, DeclName);
Identifier Name = ResultPair.first;
if (!Name.empty()) {
Result.NameKind = SwiftLangSupport::getUIDForNameKind(NameKind::ObjC);
Result.BaseName = Name.str();
Receiver(RequestResult<NameTranslatingInfo>::fromResult(Result));
} else if (ObjCSelector Selector = ResultPair.second) {
Result.NameKind = SwiftLangSupport::getUIDForNameKind(NameKind::ObjC);
SmallString<64> Buffer;
StringRef Total = Selector.getString(Buffer);
SmallVector<StringRef, 4> Pieces;
Total.split(Pieces, ":");
if (Selector.getNumArgs()) {
assert(Pieces.back().empty());
Pieces.pop_back();
} else {
Result.IsZeroArgSelector = true;
}
Result.ArgNames.insert(Result.ArgNames.begin(), Pieces.begin(), Pieces.end());
Receiver(RequestResult<NameTranslatingInfo>::fromResult(Result));
} else {
Diagnostic = "Unable to resolve name info.";
return false;
}
return true;
}
case NameKind::ObjC: {
ClangImporter *Importer = static_cast<ClangImporter *>(VD->getDeclContext()->
getASTContext().getClangModuleLoader());
const clang::NamedDecl *Named = nullptr;
auto *BaseDecl = VD;
while (!Named && BaseDecl) {
Named = dyn_cast_or_null<clang::NamedDecl>(BaseDecl->getClangDecl());
BaseDecl = BaseDecl->getOverriddenDecl();
}
if (!Named) {
Diagnostic = "Unable to resolve a named declaration.";
return false;
}
auto ObjCName = getClangDeclarationName(Named, Info);
if (!ObjCName) {
Diagnostic = "Unable to resolve ObjC declaration name.";
return false;
}
DeclName Name = Importer->importName(Named, ObjCName);
NameTranslatingInfo Result;
Result.NameKind = SwiftLangSupport::getUIDForNameKind(NameKind::Swift);
Result.BaseName = Name.getBaseName().userFacingName();
llvm::transform(Name.getArgumentNames(),
std::back_inserter(Result.ArgNames),
[](Identifier Id) { return Id.str(); });
Receiver(RequestResult<NameTranslatingInfo>::fromResult(Result));
return true;
}
}
}
class CursorRangeInfoConsumer : public SwiftASTConsumer {
protected:
SwiftLangSupport ⟪
SwiftInvocationRef ASTInvok;
std::string PrimaryFilePath;
std::string InputBufferName;
unsigned Offset;
unsigned Length;
private:
const bool TryExistingAST;
SmallVector<ImmutableTextSnapshotRef, 4> PreviousASTSnaps;
protected:
bool CancelOnSubsequentRequest;
protected:
ArrayRef<ImmutableTextSnapshotRef> getPreviousASTSnaps() {
return llvm::ArrayRef(PreviousASTSnaps);
}
public:
CursorRangeInfoConsumer(StringRef PrimaryFilePath, StringRef InputBufferName,
unsigned Offset, unsigned Length,
SwiftLangSupport &Lang, SwiftInvocationRef ASTInvok,
bool TryExistingAST, bool CancelOnSubsequentRequest)
: Lang(Lang), ASTInvok(ASTInvok), PrimaryFilePath(PrimaryFilePath.str()),
InputBufferName(InputBufferName.str()), Offset(Offset), Length(Length),
TryExistingAST(TryExistingAST),
CancelOnSubsequentRequest(CancelOnSubsequentRequest) {}
bool canUseASTWithSnapshots(ArrayRef<ImmutableTextSnapshotRef> Snapshots) override {
if (!TryExistingAST) {
LOG_INFO_FUNC(High, "will resolve using up-to-date AST");
return false;
}
// If there is an existing AST and the offset can be mapped back to the
// document snapshot that was used to create it, then use that AST.
// The downside is that we may return stale information, but we get the
// benefit of increased responsiveness, since the request will not be
// blocked waiting on the AST to be fully typechecked.
ImmutableTextSnapshotRef InputSnap;
if (auto EditorDoc = Lang.getEditorDocuments()->findByPath(
PrimaryFilePath, /*IsRealpath=*/true))
InputSnap = EditorDoc->getLatestSnapshot();
if (!InputSnap)
return false;
auto mappedBackOffset = [&]() -> std::optional<unsigned> {
for (auto &Snap : Snapshots) {
if (Snap->isFromSameBuffer(InputSnap)) {
if (Snap->getStamp() == InputSnap->getStamp())
return Offset;
auto OptOffset = mapOffsetToOlderSnapshot(Offset, InputSnap, Snap);
if (!OptOffset.has_value())
return std::nullopt;
// Check that the new and old offset still point to the same token.
StringRef NewTok = getSourceToken(Offset, InputSnap);
if (NewTok.empty())
return std::nullopt;
if (NewTok == getSourceToken(OptOffset.value(), Snap))
return OptOffset;
return std::nullopt;
}
}
return std::nullopt;
};
auto OldOffsetOpt = mappedBackOffset();
if (OldOffsetOpt.has_value()) {
Offset = *OldOffsetOpt;
PreviousASTSnaps.append(Snapshots.begin(), Snapshots.end());
LOG_INFO_FUNC(High, "will try existing AST");
return true;
}
LOG_INFO_FUNC(High, "will resolve using up-to-date AST");
return false;
}
};
SourceFile *SourceKit::retrieveInputFile(StringRef inputBufferName,
const CompilerInstance &CI,
bool haveRealPath) {
// Don't bother looking up if we have the same file as the primary file or
// we weren't given a separate input file
if (inputBufferName.empty() ||
CI.getPrimarySourceFile()->getFilename() == inputBufferName)
return CI.getPrimarySourceFile();
// Otherwise, try to find the given buffer identifier
const SourceManager &SM = CI.getSourceMgr();
std::optional<unsigned> inputBufferID =
SM.getIDForBufferIdentifier(inputBufferName);
if (!inputBufferID) {
// If that failed, try again with symlinks resolved (unless we've already
// done that)
if (haveRealPath)
return nullptr;
std::string realPath =
SwiftLangSupport::resolvePathSymlinks(inputBufferName);
return retrieveInputFile(realPath, CI, /*haveRealPath=*/true);
}
return CI.getMainModule()->getSourceFileContainingLocation(
SM.getRangeForBuffer(*inputBufferID).getStart());
}
static void resolveCursor(
SwiftLangSupport &Lang, StringRef PrimaryFile, StringRef InputBufferName,
unsigned Offset, unsigned Length, bool Actionables, bool SymbolGraph,
SwiftInvocationRef Invok, bool TryExistingAST,
bool CancelOnSubsequentRequest,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fileSystem,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) {
assert(Invok);
assert(fileSystem);
class CursorInfoConsumer : public CursorRangeInfoConsumer {
bool Actionables;
bool SymbolGraph;
SourceKitCancellationToken CancellationToken;
std::function<void(const RequestResult<CursorInfoData> &)> Receiver;
public:
CursorInfoConsumer(
StringRef PrimaryFile, StringRef InputBufferName, unsigned Offset,
unsigned Length, bool Actionables, bool SymbolGraph,
SwiftLangSupport &Lang, SwiftInvocationRef ASTInvok,
bool TryExistingAST, bool CancelOnSubsequentRequest,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<CursorInfoData> &)> Receiver)
: CursorRangeInfoConsumer(PrimaryFile, InputBufferName, Offset, Length,
Lang, ASTInvok, TryExistingAST,
CancelOnSubsequentRequest),
Actionables(Actionables), SymbolGraph(SymbolGraph),
CancellationToken(CancellationToken), Receiver(std::move(Receiver)) {}
void handlePrimaryAST(ASTUnitRef AstUnit) override {
auto &CompIns = AstUnit->getCompilerInstance();
SourceFile *SF = retrieveInputFile(InputBufferName, CompIns);
if (!SF) {
Receiver(RequestResult<CursorInfoData>::fromError(
"Unable to find input file"));
return;
}
SourceManager &SM = CompIns.getSourceMgr();
unsigned BufferID = SF->getBufferID().value();
SourceLoc Loc =
Lexer::getLocForStartOfToken(SM, BufferID, Offset);
if (Loc.isInvalid()) {
Receiver(RequestResult<CursorInfoData>::fromError(
"Unable to find initial lookup location"));
return;
}
// Sanitize length.
if (Length) {
SourceLoc TokEnd = Lexer::getLocForEndOfToken(SM, Loc);
SourceLoc EndLoc = SM.getLocForOffset(BufferID, Offset + Length);
// If TokEnd is not before the given EndLoc, the EndLoc contains no
// more stuff than this token, so set the length to 0.
if (SM.isBeforeInBuffer(EndLoc, TokEnd) || TokEnd == EndLoc)
Length = 0;
}
// Retrieve relevant actions on the code under selection.
llvm::SmallVector<RefactoringInfo, 8> Actions;
if (Actionables && Length) {
RangeConfig Range;
Range.BufferID = BufferID;
auto Pair = SM.getLineAndColumnInBuffer(Loc);
Range.Line = Pair.first;
Range.Column = Pair.second;
Range.Length = Length;
bool CollectRangeStartRefactorings = false;
addRefactorings(
Actions,
collectRefactorings(SF, Range, CollectRangeStartRefactorings, {}));
if (!CollectRangeStartRefactorings) {
// If Length is given then this request is only for refactorings,
// return straight away unless we need cursor based refactorings as
// well.
CursorInfoData Data;
Data.AvailableActions = Actions;
Receiver(RequestResult<CursorInfoData>::fromResult(Data));
return;
}
// Fall through to collect cursor based refactorings
}
ResolvedCursorInfoPtr CursorInfo =
evaluateOrDefault(CompIns.getASTContext().evaluator,
CursorInfoRequest{CursorInfoOwner(SF, Loc)},
new ResolvedCursorInfo());
CompilerInvocation CompInvok;
ASTInvok->applyTo(CompInvok);
switch (CursorInfo->getKind()) {
case CursorInfoKind::ModuleRef: {
auto ModuleRefInfo = cast<ResolvedModuleRefCursorInfo>(CursorInfo);
passCursorInfoForModule(ModuleRefInfo->getMod(),
Lang.getIFaceGenContexts(), CompInvok,
Receiver);
return;
}
case CursorInfoKind::ValueRef: {
std::string Diagnostic;
bool Success = passCursorInfoForDecl(
cast<ResolvedValueRefCursorInfo>(CursorInfo), Actionables,
SymbolGraph, Actions, Lang, CompInvok, Diagnostic,
getPreviousASTSnaps(),
/*DidReuseAST=*/!getPreviousASTSnaps().empty(), Receiver);
if (!Success) {
if (!getPreviousASTSnaps().empty()) {
// Attempt again using the up-to-date AST.
resolveCursor(Lang, PrimaryFilePath, InputBufferName, Offset,
Length, Actionables, SymbolGraph, ASTInvok,
/*TryExistingAST=*/false, CancelOnSubsequentRequest,
SM.getFileSystem(), CancellationToken, Receiver);
} else {
CursorInfoData Info;
Info.InternalDiagnostic = Diagnostic;
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
}
}
return;
}
case CursorInfoKind::ExprStart:
case CursorInfoKind::StmtStart: {
// If code under cursor is literal expression returns Expr,
// otherwise nullptr.
auto tryGetLiteralExpr = [](auto CI) -> Expr * {
auto *ExprInfo = dyn_cast<ResolvedExprStartCursorInfo>(CI);
if (!ExprInfo || !ExprInfo->getTrailingExpr()) {
return nullptr;
}
Expr *E = ExprInfo->getTrailingExpr();
if (dyn_cast<LiteralExpr>(E) || dyn_cast<CollectionExpr>(E)) {
return E;
}
return nullptr;
};
CursorInfoData Data;
if (Actionables) {
addRefactorings(
Actions, collectRefactorings(CursorInfo, /*ExcludeRename=*/true));
Data.AvailableActions = Actions;
}
// Handle literal expression.
if (auto *LitExpr = tryGetLiteralExpr(CursorInfo)) {
addCursorInfoForLiteral(Data, LitExpr, Lang, CompInvok,
CursorInfo->getLoc(), getPreviousASTSnaps());
}
if (Data.isEmpty()) {
Data.InternalDiagnostic =
"Resolved to incomplete expression or statement.";
}
Receiver(RequestResult<CursorInfoData>::fromResult(Data));
return;
}
case CursorInfoKind::Invalid:
CursorInfoData Data;
if (Actionables) {
Data.AvailableActions = Actions;
} else {
Data.InternalDiagnostic = "Unable to resolve cursor info.";
}
Receiver(RequestResult<CursorInfoData>::fromResult(Data));
return;
}
}
void cancelled() override {
Receiver(RequestResult<CursorInfoData>::cancelled());
}
void failed(StringRef Error) override {
LOG_WARN_FUNC("cursor info failed: " << Error);
Receiver(RequestResult<CursorInfoData>::fromError(Error));
}
};
auto Consumer = std::make_shared<CursorInfoConsumer>(
PrimaryFile, InputBufferName, Offset, Length, Actionables, SymbolGraph,
Lang, Invok, TryExistingAST, CancelOnSubsequentRequest, CancellationToken,
Receiver);
/// FIXME: When request cancellation is implemented and Xcode adopts it,
/// don't use 'OncePerASTToken'.
static const char OncePerASTToken = 0;
static const char OncePerASTTokenWithActionables = 0;
const void *Once = nullptr;
if (CancelOnSubsequentRequest)
Once = Actionables ? &OncePerASTTokenWithActionables : &OncePerASTToken;
Lang.getASTManager()->processASTAsync(Invok, std::move(Consumer), Once,
CancellationToken, fileSystem);
}
static void computeDiagnostics(
SwiftLangSupport &Lang, SwiftInvocationRef Invok,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<DiagnosticsResult> &)> Receiver) {
class DiagnosticsConsumer : public SwiftASTConsumer {
std::function<void(const RequestResult<DiagnosticsResult> &)> Receiver;
public:
DiagnosticsConsumer(
std::function<void(const RequestResult<DiagnosticsResult> &)> Receiver)
: Receiver(Receiver) {}
void handlePrimaryAST(ASTUnitRef AstUnit) override {
unsigned BufferID = *AstUnit->getPrimarySourceFile().getBufferID();
auto &DiagConsumer = AstUnit->getEditorDiagConsumer();
auto Diagnostics = DiagConsumer.getDiagnosticsForBuffer(BufferID);
Receiver(RequestResult<DiagnosticsResult>::fromResult(Diagnostics));
}
void cancelled() override {
Receiver(RequestResult<DiagnosticsResult>::cancelled());
}
};
auto Consumer = std::make_shared<DiagnosticsConsumer>(std::move(Receiver));
Lang.getASTManager()->processASTAsync(Invok, std::move(Consumer),
/*OncePerASTToken=*/nullptr,
CancellationToken, FileSystem);
}
static void resolveName(
SwiftLangSupport &Lang, StringRef PrimaryFilePath,
StringRef InputBufferName, unsigned Offset, SwiftInvocationRef Invok,
bool TryExistingAST, NameTranslatingInfo &Input,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<NameTranslatingInfo> &)> Receiver) {
assert(Invok);
class NameInfoConsumer : public CursorRangeInfoConsumer {
NameTranslatingInfo Input;
SourceKitCancellationToken CancellationToken;
std::function<void(const RequestResult<NameTranslatingInfo> &)> Receiver;
public:
NameInfoConsumer(
StringRef PrimaryFilePath, StringRef InputBufferName, unsigned Offset,
SwiftLangSupport &Lang, SwiftInvocationRef ASTInvok,
bool TryExistingAST, NameTranslatingInfo Input,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<NameTranslatingInfo> &)>
Receiver)
: CursorRangeInfoConsumer(PrimaryFilePath, InputBufferName, Offset, 0,
Lang, ASTInvok, TryExistingAST,
/*CancelOnSubsequentRequest=*/false),
Input(std::move(Input)), CancellationToken(CancellationToken),
Receiver(std::move(Receiver)) {}
void handlePrimaryAST(ASTUnitRef AstUnit) override {
auto &CompIns = AstUnit->getCompilerInstance();
SourceFile *SF = retrieveInputFile(InputBufferName, CompIns);
if (!SF) {
Receiver(RequestResult<NameTranslatingInfo>::fromError(
"Unable to find input file"));
return;
}
SourceLoc Loc = Lexer::getLocForStartOfToken(CompIns.getSourceMgr(),
*SF->getBufferID(), Offset);
if (Loc.isInvalid()) {
Receiver(RequestResult<NameTranslatingInfo>::fromError(
"Unable to resolve the start of the token."));
return;
}
ResolvedCursorInfoPtr CursorInfo =
evaluateOrDefault(CompIns.getASTContext().evaluator,
CursorInfoRequest{CursorInfoOwner(SF, Loc)},
new ResolvedCursorInfo());
if (CursorInfo->isInvalid()) {
NameTranslatingInfo Info;
Info.InternalDiagnostic = "Unable to resolve cursor info.";
Receiver(RequestResult<NameTranslatingInfo>::fromResult(Info));
return;
}
CompilerInvocation CompInvok;
ASTInvok->applyTo(CompInvok);
switch (CursorInfo->getKind()) {
case CursorInfoKind::ModuleRef:
return;
case CursorInfoKind::ValueRef: {
std::string Diagnostic;
bool Success =
passNameInfoForDecl(*cast<ResolvedValueRefCursorInfo>(CursorInfo),
Input, Diagnostic, Receiver);
if (!Success) {
if (!getPreviousASTSnaps().empty()) {
// Attempt again using the up-to-date AST.
resolveName(
Lang, PrimaryFilePath, InputBufferName, Offset, ASTInvok,
/*TryExistingAST=*/false, Input, CancellationToken, Receiver);
} else {
NameTranslatingInfo Info;
Info.InternalDiagnostic = Diagnostic;
Receiver(RequestResult<NameTranslatingInfo>::fromResult(Info));
}
}
return;
}
case CursorInfoKind::ExprStart:
case CursorInfoKind::StmtStart: {
NameTranslatingInfo Info;
Info.InternalDiagnostic =
"Resolved to incomplete expression or statement.";
Receiver(RequestResult<NameTranslatingInfo>::fromResult(Info));
return;
}
case CursorInfoKind::Invalid:
llvm_unreachable("bad sema token kind.");
}
}
void cancelled() override {
Receiver(RequestResult<NameTranslatingInfo>::cancelled());
}
void failed(StringRef Error) override {
LOG_WARN_FUNC("name info failed: " << Error);
Receiver(RequestResult<NameTranslatingInfo>::fromError(Error));
}
};
auto Consumer = std::make_shared<NameInfoConsumer>(
PrimaryFilePath, InputBufferName, Offset, Lang, Invok, TryExistingAST,
Input, CancellationToken, Receiver);
Lang.getASTManager()->processASTAsync(
Invok, std::move(Consumer), /*OncePerASTToken=*/nullptr,
CancellationToken, llvm::vfs::getRealFileSystem());
}
static void
resolveRange(SwiftLangSupport &Lang, StringRef PrimaryFilePath,
StringRef InputBufferName, unsigned Offset, unsigned Length,
SwiftInvocationRef Invok, bool TryExistingAST,
bool CancelOnSubsequentRequest,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<RangeInfo> &)> Receiver) {
assert(Invok);
class RangeInfoConsumer : public CursorRangeInfoConsumer {
SourceKitCancellationToken CancellationToken;
std::function<void(const RequestResult<RangeInfo> &)> Receiver;
public:
RangeInfoConsumer(
StringRef PrimaryFilePath, StringRef InputBufferName, unsigned Offset,
unsigned Length, SwiftLangSupport &Lang, SwiftInvocationRef ASTInvok,
bool TryExistingAST, bool CancelOnSubsequentRequest,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<RangeInfo> &)> Receiver)
: CursorRangeInfoConsumer(PrimaryFilePath, InputBufferName, Offset,
Length, Lang, ASTInvok, TryExistingAST,
CancelOnSubsequentRequest),
CancellationToken(CancellationToken), Receiver(std::move(Receiver)) {}
void handlePrimaryAST(ASTUnitRef AstUnit) override {
// FIXME: Implement tracing
auto &CompIns = AstUnit->getCompilerInstance();
SourceFile *SF = retrieveInputFile(InputBufferName, CompIns);
if (!SF) {
Receiver(
RequestResult<RangeInfo>::fromError("Unable to find input file"));
return;
}
ResolvedRangeInfo Info = evaluateOrDefault(
CompIns.getASTContext().evaluator,
RangeInfoRequest(RangeInfoOwner({SF, Offset, Length})),
ResolvedRangeInfo());
CompilerInvocation CompInvok;
ASTInvok->applyTo(CompInvok);
RangeInfo Result;
Result.RangeKind = Lang.getUIDForRangeKind(Info.Kind);
if (Info.Kind == RangeKind::Invalid) {
Result.RangeContent = "";
} else {
assert(Info.ContentRange.isValid());
Result.RangeContent = Info.ContentRange.str();
}
switch (Info.Kind) {
case RangeKind::SingleExpression: {
SmallString<64> SS;
llvm::raw_svector_ostream OS(SS);
Info.ExitInfo.ReturnType->print(OS);
Result.ExprType = OS.str();
Receiver(RequestResult<RangeInfo>::fromResult(Result));
return;
}
case RangeKind::SingleDecl:
case RangeKind::MultiTypeMemberDecl:
case RangeKind::MultiStatement:
case RangeKind::SingleStatement: {
Receiver(RequestResult<RangeInfo>::fromResult(Result));
return;
}
case RangeKind::PartOfExpression:
case RangeKind::Invalid:
if (!getPreviousASTSnaps().empty()) {
// Attempt again using the up-to-date AST.
resolveRange(Lang, PrimaryFilePath, InputBufferName, Offset, Length,
ASTInvok,
/*TryExistingAST=*/false, CancelOnSubsequentRequest,
CancellationToken, Receiver);
} else {
Receiver(RequestResult<RangeInfo>::fromResult(Result));
}
return;
}
}
void cancelled() override {
Receiver(RequestResult<RangeInfo>::cancelled());
}
void failed(StringRef Error) override {
LOG_WARN_FUNC("range info failed: " << Error);
Receiver(RequestResult<RangeInfo>::fromError(Error));
}
};
auto Consumer = std::make_shared<RangeInfoConsumer>(
PrimaryFilePath, InputBufferName, Offset, Length, Lang, Invok,
TryExistingAST, CancelOnSubsequentRequest, CancellationToken, Receiver);
/// FIXME: When request cancellation is implemented and Xcode adopts it,
/// don't use 'OncePerASTToken'.
static const char OncePerASTToken = 0;
const void *Once = CancelOnSubsequentRequest ? &OncePerASTToken : nullptr;
Lang.getASTManager()->processASTAsync(Invok, std::move(Consumer), Once,
CancellationToken,
llvm::vfs::getRealFileSystem());
}
static void deliverCursorInfoResults(
std::function<void(const RequestResult<CursorInfoData> &)> Receiver,
CancellableResult<CursorInfoResults> Results, SwiftLangSupport &Lang,
const CompilerInvocation &Invoc, bool AddRefactorings,
bool AddSymbolGraph) {
switch (Results.getKind()) {
case CancellableResultKind::Success: {
// TODO: Implement delivery of other result types as more cursor info kinds
// are migrated to be completion-like.
CursorInfoData Data;
for (auto ResolvedCursorInfo : Results->ResolvedCursorInfos) {
if (auto Result = dyn_cast_or_null<ResolvedValueRefCursorInfo>(
ResolvedCursorInfo)) {
std::string Diagnostic; // Unused
addCursorInfoForDecl(Data, Result, AddRefactorings, AddSymbolGraph,
Lang, Invoc, Diagnostic, /*PreviousSnaps=*/{});
}
}
Data.DidReuseAST = Results->DidReuseAST;
if (!Data.Symbols.empty()) {
Receiver(RequestResult<CursorInfoData>::fromResult(Data));
}
break;
}
case CancellableResultKind::Failure:
Receiver(RequestResult<CursorInfoData>::fromError(Results.getError()));
break;
case CancellableResultKind::Cancelled:
Receiver(RequestResult<CursorInfoData>::cancelled());
break;
}
}
void SwiftLangSupport::getCursorInfo(
StringRef PrimaryFilePath, StringRef InputBufferName, unsigned Offset,
unsigned Length, bool Actionables, bool SymbolGraph,
bool CancelOnSubsequentRequest, ArrayRef<const char *> Args,
std::optional<VFSOptions> vfsOptions,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) {
std::string error;
auto fileSystem = getFileSystem(vfsOptions, PrimaryFilePath, error);
if (!fileSystem)
return Receiver(RequestResult<CursorInfoData>::fromError(error));
if (auto IFaceGenRef = IFaceGenContexts.get(PrimaryFilePath)) {
IFaceGenRef->accessASTAsync([this, IFaceGenRef, Offset, Actionables,
SymbolGraph, Receiver] {
SwiftInterfaceGenContext::ResolvedEntity Entity;
Entity = IFaceGenRef->resolveEntityForOffset(Offset);
if (Entity.isResolved()) {
CompilerInvocation Invok;
IFaceGenRef->applyTo(Invok);
if (Entity.Mod) {
passCursorInfoForModule(Entity.Mod, IFaceGenContexts, Invok,
Receiver);
} else {
std::string Diagnostic; // Unused.
ResolvedValueRefCursorInfoPtr Info = new ResolvedValueRefCursorInfo(
/*SourcFile=*/nullptr, SourceLoc(),
const_cast<ValueDecl *>(Entity.Dcl),
/*CtorTyRef=*/nullptr,
/*ExtTyRef=*/nullptr, Entity.IsRef,
/*SolutionSpecificInterfaceType=*/Type(),
/*ContainerType=*/Type(),
/*CustomAttrRef=*/std::nullopt,
/*IsKeywordArgument=*/false,
/*IsDynamic=*/false,
/*ReceiverTypes=*/{},
/*ShorthandShadowedDecls=*/{});
passCursorInfoForDecl(
Info, Actionables, SymbolGraph, {}, *this, Invok, Diagnostic,
/*PreviousSnaps=*/{},
/*DidReuseAST=*/false, Receiver);
}
} else {
CursorInfoData Info;
Info.InternalDiagnostic =
"Unable to resolve entity from generated interface.";
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
}
});
return;
}
std::string Error;
SwiftInvocationRef Invok =
ASTMgr->getTypecheckInvocation(Args, PrimaryFilePath, fileSystem, Error);
if (!Error.empty()) {
LOG_WARN_FUNC("error creating ASTInvocation: " << Error);
}
if (!Invok) {
Receiver(RequestResult<CursorInfoData>::fromError(Error));
return;
}
// Solver based cursor info cannot handle generated buffers or range based
// cursor info.
std::shared_ptr<llvm::MemoryBuffer> InputBuffer;
if (InputBufferName.empty() && Length == 0) {
std::string InputFileError;
InputBuffer =
std::shared_ptr<llvm::MemoryBuffer>(getASTManager()->getMemoryBuffer(
PrimaryFilePath, fileSystem, InputFileError));
}
// Receiver is async, so be careful about captured values. This is all
// fairly hacky in order to run AST based cursor info before solver based.
// ie. we shouldn't need to apply the compiler invocation again or copy any
// arguments. We could possibly sink this down into `resolveCursor`. Or
// improve the solver based so we don't need to run the old.
auto ASTBasedReceiver = [this, CancellationToken, Invok, InputBuffer,
fileSystem, Receiver, Offset, Actionables,
SymbolGraph](
const RequestResult<CursorInfoData> &Res) {
if (Res.isCancelled()) {
// If the AST-based result got cancelled, we don’t want to start
// solver-based cursor info anymore.
Receiver(Res);
return;
}
// AST based completion *always* produces a result
bool NoResults = Res.isError();
if (Res.isValue()) {
NoResults |= Res.value().Symbols.empty();
bool AllResultsHaveErrorType =
llvm::all_of(Res.value().Symbols, [](const CursorSymbolInfo &Symbol) {
// USR of the AST's error type if no type could be resolved.
return Symbol.TypeUSR == "$sXeD";
});
NoResults |= AllResultsHaveErrorType;
}
if (!NoResults || !InputBuffer) {
Receiver(Res);
return;
}
CompilerInvocation CompInvok;
Invok->applyTo(CompInvok);
SmallVector<const char *, 0> Args;
Args.reserve(Invok->getArgs().size());
for (const std::string &Arg : Invok->getArgs()) {
Args.push_back(Arg.c_str());
}
bool SolverProducedResults = false;
auto SolverBasedReceiver = [&](const RequestResult<CursorInfoData> &Res) {
SolverProducedResults = true;
Receiver(Res);
};
performWithParamsToCompletionLikeOperation(
InputBuffer.get(), Offset,
/*InsertCodeCompletionToken=*/false, Args, fileSystem,
CancellationToken,
[&](CancellableResult<CompletionLikeOperationParams> ParmsResult) {
ParmsResult.mapAsync<CursorInfoResults>(
[&](auto &Params, auto DeliverTransformed) {
getIDEInspectionInstance()->cursorInfo(
Params.Invocation, Args, fileSystem,
Params.completionBuffer, Offset, Params.DiagC,
Params.CancellationFlag, DeliverTransformed);
},
[&](auto Result) {
deliverCursorInfoResults(SolverBasedReceiver, Result, *this,
CompInvok, Actionables, SymbolGraph);
});
});
// If the solver based cursor info produced no results, fallback to the
// original AST based.
if (!SolverProducedResults) {
Receiver(Res);
}
};
resolveCursor(*this, PrimaryFilePath, InputBufferName, Offset, Length,
Actionables, SymbolGraph, Invok, /*TryExistingAST=*/true,
CancelOnSubsequentRequest, fileSystem, CancellationToken,
ASTBasedReceiver);
}
void SwiftLangSupport::getDiagnostics(
StringRef PrimaryFilePath, ArrayRef<const char *> Args,
std::optional<VFSOptions> VfsOptions,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<DiagnosticsResult> &)> Receiver) {
std::string FileSystemError;
auto FileSystem = getFileSystem(VfsOptions, PrimaryFilePath, FileSystemError);
if (!FileSystem) {
Receiver(RequestResult<DiagnosticsResult>::fromError(FileSystemError));
return;
}
std::string InvocationError;
SwiftInvocationRef Invok = ASTMgr->getTypecheckInvocation(
Args, PrimaryFilePath, FileSystem, InvocationError);
if (!InvocationError.empty()) {
LOG_WARN_FUNC("error creating ASTInvocation: " << InvocationError);
}
if (!Invok) {
Receiver(RequestResult<DiagnosticsResult>::fromError(InvocationError));
return;
}
computeDiagnostics(*this, Invok, FileSystem, CancellationToken, Receiver);
}
void SwiftLangSupport::getRangeInfo(
StringRef PrimaryFilePath, StringRef InputBufferName, unsigned Offset,
unsigned Length, bool CancelOnSubsequentRequest,
ArrayRef<const char *> Args, SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<RangeInfo> &)> Receiver) {
if (IFaceGenContexts.get(InputBufferName)) {
// FIXME: return range info for generated interfaces.
Receiver(RequestResult<RangeInfo>::fromError(
"Range info for generated interfaces is not implemented."));
return;
}
std::string Error;
SwiftInvocationRef Invok =
ASTMgr->getTypecheckInvocation(Args, PrimaryFilePath, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver(RequestResult<RangeInfo>::fromError(Error));
return;
}
if (Length == 0) {
Receiver(RequestResult<RangeInfo>::fromError("Invalid range length."));
return;
}
resolveRange(*this, PrimaryFilePath, InputBufferName, Offset, Length, Invok,
/*TryExistingAST=*/true, CancelOnSubsequentRequest,
CancellationToken, Receiver);
}
void SwiftLangSupport::getNameInfo(
StringRef PrimaryFilePath, StringRef InputBufferName, unsigned Offset,
NameTranslatingInfo &Input, ArrayRef<const char *> Args,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<NameTranslatingInfo> &)> Receiver) {
if (auto IFaceGenRef = IFaceGenContexts.get(PrimaryFilePath)) {
IFaceGenRef->accessASTAsync([IFaceGenRef, Offset, Input, Receiver] {
SwiftInterfaceGenContext::ResolvedEntity Entity;
Entity = IFaceGenRef->resolveEntityForOffset(Offset);
if (Entity.isResolved()) {
CompilerInvocation Invok;
IFaceGenRef->applyTo(Invok);
if (Entity.Mod) {
// Module is ignored
} else {
// FIXME: Should pass the main module for the interface but currently
// it's not necessary.
}
} else {
NameTranslatingInfo Info;
Info.InternalDiagnostic =
"Unable to resolve entity from generated interface.";
Receiver(RequestResult<NameTranslatingInfo>::fromResult(Info));
}
});
return;
}
std::string Error;
SwiftInvocationRef Invok =
ASTMgr->getTypecheckInvocation(Args, PrimaryFilePath, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver(RequestResult<NameTranslatingInfo>::fromError(Error));
return;
}
resolveName(*this, PrimaryFilePath, InputBufferName, Offset, Invok,
/*TryExistingAST=*/true, Input, CancellationToken, Receiver);
}
static void resolveCursorFromUSR(
SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
SwiftInvocationRef Invok, bool TryExistingAST,
bool CancelOnSubsequentRequest,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fileSystem,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) {
assert(Invok);
class CursorInfoConsumer : public SwiftASTConsumer {
std::string InputFile;
StringRef USR;
SwiftLangSupport ⟪
SwiftInvocationRef ASTInvok;
const bool TryExistingAST;
bool CancelOnSubsequentRequest;
SourceKitCancellationToken CancellationToken;
std::function<void(const RequestResult<CursorInfoData> &)> Receiver;
SmallVector<ImmutableTextSnapshotRef, 4> PreviousASTSnaps;
public:
CursorInfoConsumer(
StringRef InputFile, StringRef USR, SwiftLangSupport &Lang,
SwiftInvocationRef ASTInvok, bool TryExistingAST,
bool CancelOnSubsequentRequest,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<CursorInfoData> &)> Receiver)
: InputFile(InputFile), USR(USR), Lang(Lang),
ASTInvok(std::move(ASTInvok)), TryExistingAST(TryExistingAST),
CancelOnSubsequentRequest(CancelOnSubsequentRequest),
CancellationToken(CancellationToken), Receiver(std::move(Receiver)) {}
bool canUseASTWithSnapshots(
ArrayRef<ImmutableTextSnapshotRef> Snapshots) override {
if (!TryExistingAST) {
LOG_INFO_FUNC(High, "will resolve using up-to-date AST");
return false;
}
if (!Snapshots.empty()) {
PreviousASTSnaps.append(Snapshots.begin(), Snapshots.end());
LOG_INFO_FUNC(High, "will try existing AST");
return true;
}
LOG_INFO_FUNC(High, "will resolve using up-to-date AST");
return false;
}
void handlePrimaryAST(ASTUnitRef AstUnit) override {
auto &CompIns = AstUnit->getCompilerInstance();
if (USR.starts_with("c:")) {
LOG_WARN_FUNC("lookup for C/C++/ObjC USRs not implemented");
CursorInfoData Info;
Info.InternalDiagnostic = "Lookup for C/C++/ObjC USRs not implemented.";
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
return;
}
auto &context = CompIns.getASTContext();
TypeDecl *D = Demangle::getTypeDeclForUSR(context, USR);
if (!D) {
CursorInfoData Info;
Info.InternalDiagnostic = "Unable to resolve type from USR.";
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
return;
}
CompilerInvocation CompInvok;
ASTInvok->applyTo(CompInvok);
if (auto *M = dyn_cast<ModuleDecl>(D)) {
passCursorInfoForModule(M, Lang.getIFaceGenContexts(), CompInvok,
Receiver);
} else {
auto *DC = D->getDeclContext();
Type ContainerType;
if (DC->isTypeContext()) {
auto ContainerType = DC->getSelfInterfaceType();
ContainerType =
D->getInnermostDeclContext()->mapTypeIntoContext(ContainerType);
}
ResolvedValueRefCursorInfoPtr Info = new ResolvedValueRefCursorInfo(
/*SourceFile=*/nullptr, SourceLoc(), D,
/*CtorTyRef=*/nullptr,
/*ExtTyRef=*/nullptr,
/*IsRef=*/false,
/*SolutionSpecificInterfaceType=*/Type(), ContainerType,
/*CustomAttrRef=*/std::nullopt,
/*IsKeywordArgument=*/false,
/*IsDynamic=*/false,
/*ReceiverTypes=*/{},
/*ShorthandShadowedDecls=*/{});
Type selfTy;
std::string Diagnostic;
bool Success = passCursorInfoForDecl(
Info, /*AddRefactorings*/ false,
/*AddSymbolGraph*/ false, {}, Lang, CompInvok, Diagnostic,
PreviousASTSnaps,
/*DidReuseAST=*/false, Receiver);
if (!Success) {
if (!PreviousASTSnaps.empty()) {
// Attempt again using the up-to-date AST.
resolveCursorFromUSR(Lang, InputFile, USR, ASTInvok,
/*TryExistingAST=*/false,
CancelOnSubsequentRequest,
CompIns.getSourceMgr().getFileSystem(),
CancellationToken, Receiver);
} else {
CursorInfoData Info;
Info.InternalDiagnostic = Diagnostic;
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
}
}
}
}
void cancelled() override {
Receiver(RequestResult<CursorInfoData>::cancelled());
}
void failed(StringRef Error) override {
LOG_WARN_FUNC("cursor info failed: " << Error);
Receiver(RequestResult<CursorInfoData>::fromError(Error));
}
};
auto Consumer = std::make_shared<CursorInfoConsumer>(
InputFile, USR, Lang, Invok, TryExistingAST, CancelOnSubsequentRequest,
CancellationToken, Receiver);
/// FIXME: When request cancellation is implemented and Xcode adopts it,
/// don't use 'OncePerASTToken'.
static const char OncePerASTToken = 0;
const void *Once = CancelOnSubsequentRequest ? &OncePerASTToken : nullptr;
Lang.getASTManager()->processASTAsync(Invok, std::move(Consumer), Once,
CancellationToken, fileSystem);
}
void SwiftLangSupport::getCursorInfoFromUSR(
StringRef PrimaryFilePath, StringRef InputBufferName, StringRef USR,
bool CancelOnSubsequentRequest, ArrayRef<const char *> Args,
std::optional<VFSOptions> vfsOptions,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) {
std::string error;
auto fileSystem = getFileSystem(vfsOptions, PrimaryFilePath, error);
if (!fileSystem)
return Receiver(RequestResult<CursorInfoData>::fromError(error));
if (auto IFaceGenRef = IFaceGenContexts.get(PrimaryFilePath)) {
LOG_WARN_FUNC("Info from usr for generated interface not implemented yet.");
CursorInfoData Info;
Info.InternalDiagnostic = "Info for generated interfaces not implemented.";
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
return;
}
std::string Error;
SwiftInvocationRef Invok =
ASTMgr->getTypecheckInvocation(Args, PrimaryFilePath, fileSystem, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver(RequestResult<CursorInfoData>::fromError(Error));
return;
}
resolveCursorFromUSR(*this, InputBufferName, USR, Invok,
/*TryExistingAST=*/true, CancelOnSubsequentRequest,
fileSystem, CancellationToken, Receiver);
}
//===----------------------------------------------------------------------===//
// SwiftLangSupport::findUSRRange
//===----------------------------------------------------------------------===//
std::optional<std::pair<unsigned, unsigned>>
SwiftLangSupport::findUSRRange(StringRef DocumentName, StringRef USR) {
if (auto IFaceGenRef = IFaceGenContexts.get(DocumentName))
return IFaceGenRef->findUSRRange(USR);
// Only works for a module interface document currently.
// FIXME: Report it as failed request.
return std::nullopt;
}
//===----------------------------------------------------------------------===//
// SwiftLangSupport::findRelatedIdentifiersInFile
//===----------------------------------------------------------------------===//
void SwiftLangSupport::findRelatedIdentifiersInFile(
StringRef PrimaryFilePath, StringRef InputBufferName, unsigned Offset,
bool IncludeNonEditableBaseNames, bool CancelOnSubsequentRequest,
ArrayRef<const char *> Args, SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<RelatedIdentsResult> &)> Receiver) {
std::string Error;
SwiftInvocationRef Invok =
ASTMgr->getTypecheckInvocation(Args, PrimaryFilePath, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver(RequestResult<RelatedIdentsResult>::fromError(Error));
return;
}
class RelatedIdConsumer : public SwiftASTConsumer {
std::string InputFile;
unsigned Offset;
bool IncludeNonEditableBaseNames;
std::function<void(const RequestResult<RelatedIdentsResult> &)> Receiver;
SwiftInvocationRef Invok;
#if SWIFT_BUILD_SWIFT_SYNTAX
// FIXME: Don't silently eat errors here.
RelatedIdentsResult getRelatedIdents(SourceFile *SrcFile,
CompilerInstance &CompInst) {
unsigned BufferID = SrcFile->getBufferID().value();
SourceLoc Loc = Lexer::getLocForStartOfToken(CompInst.getSourceMgr(),
BufferID, Offset);
if (Loc.isInvalid())
return RelatedIdentsResult::empty();
SourceManager &SrcMgr = CompInst.getASTContext().SourceMgr;
ResolvedCursorInfoPtr CursorInfo =
evaluateOrDefault(CompInst.getASTContext().evaluator,
CursorInfoRequest{CursorInfoOwner(SrcFile, Loc)},
new ResolvedCursorInfo());
auto ValueRefCursorInfo =
dyn_cast<ResolvedValueRefCursorInfo>(CursorInfo);
if (!ValueRefCursorInfo)
return RelatedIdentsResult::empty();
if (ValueRefCursorInfo->isKeywordArgument())
return RelatedIdentsResult::empty();
ValueDecl *VD = ValueRefCursorInfo->typeOrValue();
if (!VD)
return RelatedIdentsResult::empty(); // This was a module reference.
// Only accept pointing to an identifier.
if (!IncludeNonEditableBaseNames && !ValueRefCursorInfo->isRef() &&
(isa<ConstructorDecl>(VD) || isa<DestructorDecl>(VD) ||
isa<SubscriptDecl>(VD)))
return RelatedIdentsResult::empty();
std::optional<RenameInfo> Info = getRenameInfo(CursorInfo);
if (!Info) {
return RelatedIdentsResult::empty();
}
RenameLocs Locs = localRenameLocs(SrcFile, Info->VD);
std::optional<std::string> OldName;
if (!Locs.getLocations().empty()) {
OldName = Locs.getLocations().front().OldName.str();
}
#ifndef NDEBUG
for (auto loc : Locs.getLocations()) {
assert(loc.OldName == OldName &&
"Found related identfiers with different names?");
}
#endif
// Ignore any errors produced by `resolveRenameLocations` since, if some
// symbol failed to resolve, we still want to return all the other
// symbols. This makes related idents more fault-tolerant.
DiagnosticEngine Diags(SrcMgr);
std::vector<ResolvedAndRenameLoc> ResolvedAndRenameLocs =
resolveRenameLocations(Locs.getLocations(), /*NewName=*/StringRef(),
*SrcFile, Diags);
SmallVector<RelatedIdentInfo, 8> Ranges;
for (auto Loc : ResolvedAndRenameLocs) {
if (Loc.resolved.range.isInvalid()) {
continue;
}
unsigned Offset = SrcMgr.getLocOffsetInBuffer(
Loc.resolved.range.getStart(), BufferID);
Ranges.push_back(
{Offset, Loc.resolved.range.getByteLength(), Loc.renameLoc.Usage});
}
return RelatedIdentsResult(Ranges, OldName);
}
#endif
public:
RelatedIdConsumer(
StringRef InputFile, unsigned Offset, bool IncludeNonEditableBaseNames,
std::function<void(const RequestResult<RelatedIdentsResult> &)>
Receiver,
SwiftInvocationRef Invok)
: InputFile(InputFile.str()), Offset(Offset),
IncludeNonEditableBaseNames(IncludeNonEditableBaseNames),
Receiver(std::move(Receiver)), Invok(Invok) {}
void handlePrimaryAST(ASTUnitRef AstUnit) override {
using ResultType = RequestResult<RelatedIdentsResult>;
#if !SWIFT_BUILD_SWIFT_SYNTAX
ResultType::fromError(
"relatedidents is not supported because sourcekitd was built without "
"swift-syntax");
return;
#else
auto &CompInst = AstUnit->getCompilerInstance();
auto *SrcFile = retrieveInputFile(InputFile, CompInst);
if (!SrcFile) {
Receiver(RequestResult<RelatedIdentsResult>::fromError(
"Unable to find input file"));
return;
}
RelatedIdentsResult Result = getRelatedIdents(SrcFile, CompInst);
Receiver(ResultType::fromResult(Result));
#endif
}
void cancelled() override {
Receiver(RequestResult<RelatedIdentsResult>::cancelled());
}
void failed(StringRef Error) override {
LOG_WARN_FUNC("related idents failed: " << Error);
Receiver(RequestResult<RelatedIdentsResult>::fromError(Error));
}
static CaseStmt *getCaseStmtOfCanonicalVar(Decl *D) {
assert(D);
if (auto *VD = dyn_cast<VarDecl>(D)) {
if (auto *Canonical = VD->getCanonicalVarDecl()) {
return dyn_cast_or_null<CaseStmt>(Canonical->getRecursiveParentPatternStmt());
}
}
return nullptr;
}
};
auto Consumer = std::make_shared<RelatedIdConsumer>(
InputBufferName, Offset, IncludeNonEditableBaseNames, Receiver, Invok);
/// FIXME: When request cancellation is implemented and Xcode adopts it,
/// don't use 'OncePerASTToken'.
static const char OncePerASTToken = 0;
const void *Once = CancelOnSubsequentRequest ? &OncePerASTToken : nullptr;
ASTMgr->processASTAsync(Invok, std::move(Consumer), Once, CancellationToken,
llvm::vfs::getRealFileSystem());
}
//===----------------------------------------------------------------------===//
// SwiftLangSupport::findActiveRegionsInFile
//===----------------------------------------------------------------------===//
void SwiftLangSupport::findActiveRegionsInFile(
StringRef PrimaryFilePath, StringRef InputBufferName,
ArrayRef<const char *> Args, SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<ActiveRegionsInfo> &)> Receiver) {
std::string Error;
SwiftInvocationRef Invok =
ASTMgr->getTypecheckInvocation(Args, PrimaryFilePath, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver(RequestResult<ActiveRegionsInfo>::fromError(Error));
return;
}
class IfConfigConsumer : public SwiftASTConsumer {
std::string InputFile;
std::function<void(const RequestResult<ActiveRegionsInfo> &)> Receiver;
SwiftInvocationRef Invok;
public:
IfConfigConsumer(
StringRef InputFile,
std::function<void(const RequestResult<ActiveRegionsInfo> &)> Receiver,
SwiftInvocationRef Invok)
: InputFile(InputFile.str()), Receiver(std::move(Receiver)),
Invok(Invok) {}
void handlePrimaryAST(ASTUnitRef AstUnit) override {
auto *SF = retrieveInputFile(InputFile, AstUnit->getCompilerInstance());
if (!SF) {
Receiver(RequestResult<ActiveRegionsInfo>::fromError(
"Unable to find input file"));
return;
}
auto &SM = SF->getASTContext().SourceMgr;
auto BufferID = *SF->getBufferID();
SmallVector<IfConfigInfo> Configs;
for (auto &range : SF->getIfConfigClauseRanges()) {
bool isActive = false;
switch (range.getKind()) {
case IfConfigClauseRangeInfo::ActiveClause:
isActive = true;
break;
case IfConfigClauseRangeInfo::InactiveClause:
isActive = false;
break;
case IfConfigClauseRangeInfo::EndDirective:
continue;
}
auto offset = SM.getLocOffsetInBuffer(range.getStartLoc(), BufferID);
Configs.emplace_back(offset, isActive);
}
ActiveRegionsInfo Info;
Info.Configs = Configs;
Receiver(RequestResult<ActiveRegionsInfo>::fromResult(Info));
}
void cancelled() override {
Receiver(RequestResult<ActiveRegionsInfo>::cancelled());
}
void failed(StringRef Error) override {
LOG_WARN_FUNC("inactive ranges failed: " << Error);
Receiver(RequestResult<ActiveRegionsInfo>::fromError(Error));
}
};
auto Consumer =
std::make_shared<IfConfigConsumer>(InputBufferName, Receiver, Invok);
ASTMgr->processASTAsync(Invok, std::move(Consumer),
/*OncePerASTToken=*/nullptr, CancellationToken,
llvm::vfs::getRealFileSystem());
}
//===----------------------------------------------------------------------===//
// SwiftLangSupport::semanticRefactoring
//===----------------------------------------------------------------------===//
static RefactoringKind getIDERefactoringKind(SemanticRefactoringInfo Info) {
switch(Info.Kind) {
case SemanticRefactoringKind::None: return RefactoringKind::None;
#define SEMANTIC_REFACTORING(KIND, NAME, ID) \
case SemanticRefactoringKind::KIND: return RefactoringKind::KIND;
#include "swift/Refactoring/RefactoringKinds.def"
}
}
void SwiftLangSupport::semanticRefactoring(
StringRef PrimaryFilePath, SemanticRefactoringInfo Info,
ArrayRef<const char *> Args, SourceKitCancellationToken CancellationToken,
CategorizedEditsReceiver Receiver) {
std::string Error;
SwiftInvocationRef Invok =
ASTMgr->getTypecheckInvocation(Args, PrimaryFilePath, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver(RequestResult<ArrayRef<CategorizedEdits>>::fromError(Error));
return;
}
assert(Invok);
class SemaRefactoringConsumer : public SwiftASTConsumer {
SemanticRefactoringInfo Info;
CategorizedEditsReceiver Receiver;
public:
SemaRefactoringConsumer(SemanticRefactoringInfo Info,
CategorizedEditsReceiver Receiver) : Info(Info),
Receiver(std::move(Receiver)) {}
void handlePrimaryAST(ASTUnitRef AstUnit) override {
RequestRefactoringEditConsumer EditConsumer(Receiver);
auto &CompIns = AstUnit->getCompilerInstance();
RefactoringOptions Opts(getIDERefactoringKind(Info));
SourceFile *SF = retrieveInputFile(Info.InputBufferName, CompIns);
if (!SF) {
Receiver(RequestResult<ArrayRef<CategorizedEdits>>::fromError(
"Unable to find input file"));
return;
}
Opts.Range.BufferID = *SF->getBufferID();
Opts.Range.Line = Info.Line;
Opts.Range.Column = Info.Column;
Opts.Range.Length = Info.Length;
Opts.PreferredName = Info.PreferredName.str();
refactorSwiftModule(CompIns.getMainModule(), Opts, EditConsumer,
EditConsumer);
}
void cancelled() override {
Receiver(RequestResult<ArrayRef<CategorizedEdits>>::cancelled());
}
void failed(StringRef Error) override {
Receiver(RequestResult<ArrayRef<CategorizedEdits>>::fromError(Error));
}
};
auto Consumer = std::make_shared<SemaRefactoringConsumer>(Info, Receiver);
/// FIXME: When request cancellation is implemented and Xcode adopts it,
/// don't use 'OncePerASTToken'.
static const char OncePerASTToken = 0;
getASTManager()->processASTAsync(Invok, std::move(Consumer), &OncePerASTToken,
CancellationToken,
llvm::vfs::getRealFileSystem());
}
void SwiftLangSupport::collectExpressionTypes(
StringRef PrimaryFilePath, StringRef InputBufferName,
ArrayRef<const char *> Args, ArrayRef<const char *> ExpectedProtocols,
bool FullyQualified, bool CanonicalType,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<ExpressionTypesInFile> &)>
Receiver) {
std::string Error;
SwiftInvocationRef Invok =
ASTMgr->getTypecheckInvocation(Args, PrimaryFilePath, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver(RequestResult<ExpressionTypesInFile>::fromError(Error));
return;
}
assert(Invok);
class ExpressionTypeCollector: public SwiftASTConsumer {
std::function<void(const RequestResult<ExpressionTypesInFile> &)> Receiver;
std::string InputFile;
std::vector<const char *> ExpectedProtocols;
bool FullyQualified;
bool CanonicalType;
public:
ExpressionTypeCollector(
std::function<void(const RequestResult<ExpressionTypesInFile> &)>
Receiver,
StringRef InputFile, ArrayRef<const char *> ExpectedProtocols,
bool FullyQualified, bool CanonicalType)
: Receiver(std::move(Receiver)), InputFile(InputFile.str()),
ExpectedProtocols(ExpectedProtocols.vec()),
FullyQualified(FullyQualified), CanonicalType(CanonicalType) {}
void handlePrimaryAST(ASTUnitRef AstUnit) override {
SourceFile *SF =
retrieveInputFile(InputFile, AstUnit->getCompilerInstance());
if (!SF) {
Receiver(RequestResult<ExpressionTypesInFile>::fromError(
"Unable to find input file"));
return;
}
std::vector<ExpressionTypeInfo> Scratch;
llvm::SmallString<256> TypeBuffer;
llvm::raw_svector_ostream OS(TypeBuffer);
ExpressionTypesInFile Result;
for (auto Item :
collectExpressionType(*SF, ExpectedProtocols, Scratch,
FullyQualified, CanonicalType, OS)) {
Result.Results.push_back({Item.offset, Item.length, Item.typeOffset, {}});
for (auto P: Item.protocols) {
Result.Results.back().ProtocolOffsets.push_back(P.first);
}
}
Result.TypeBuffer = OS.str();
Receiver(RequestResult<ExpressionTypesInFile>::fromResult(Result));
}
void cancelled() override {
Receiver(RequestResult<ExpressionTypesInFile>::cancelled());
}
void failed(StringRef Error) override {
Receiver(RequestResult<ExpressionTypesInFile>::fromError(Error));
}
};
auto Collector = std::make_shared<ExpressionTypeCollector>(
Receiver, InputBufferName, ExpectedProtocols, FullyQualified,
CanonicalType);
/// FIXME: When request cancellation is implemented and Xcode adopts it,
/// don't use 'OncePerASTToken'.
static const char OncePerASTToken = 0;
getASTManager()->processASTAsync(Invok, std::move(Collector),
&OncePerASTToken, CancellationToken,
llvm::vfs::getRealFileSystem());
}
void SwiftLangSupport::collectVariableTypes(
StringRef PrimaryFilePath, StringRef InputBufferName,
ArrayRef<const char *> Args, std::optional<unsigned> Offset,
std::optional<unsigned> Length, bool FullyQualified,
SourceKitCancellationToken CancellationToken,
std::function<void(const RequestResult<VariableTypesInFile> &)> Receiver) {
std::string Error;
SwiftInvocationRef Invok =
ASTMgr->getTypecheckInvocation(Args, PrimaryFilePath, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver(RequestResult<VariableTypesInFile>::fromError(Error));
return;
}
assert(Invok);
class VariableTypeCollectorASTConsumer : public SwiftASTConsumer {
private:
std::function<void(const RequestResult<VariableTypesInFile> &)> Receiver;
std::string InputFile;
std::optional<unsigned> Offset;
std::optional<unsigned> Length;
bool FullyQualified;
public:
VariableTypeCollectorASTConsumer(
std::function<void(const RequestResult<VariableTypesInFile> &)>
Receiver,
StringRef InputFile, std::optional<unsigned> Offset,
std::optional<unsigned> Length, bool FullyQualified)
: Receiver(std::move(Receiver)), InputFile(InputFile), Offset(Offset),
Length(Length), FullyQualified(FullyQualified) {}
void handlePrimaryAST(ASTUnitRef AstUnit) override {
auto &CompInst = AstUnit->getCompilerInstance();
SourceFile *SF = retrieveInputFile(InputFile, CompInst);
if (!SF) {
Receiver(RequestResult<VariableTypesInFile>::fromError(
"Unable to find input file"));
return;
}
// Construct the range for which variable types are to be queried. If
// offset/length are unset, the (default) range will be used, which
// corresponds to the entire document.
SourceRange Range;
if (Offset.has_value() && Length.has_value()) {
auto &SM = CompInst.getSourceMgr();
unsigned BufferID = SF->getBufferID().value();
SourceLoc Start = Lexer::getLocForStartOfToken(SM, BufferID, *Offset);
SourceLoc End =
Lexer::getLocForStartOfToken(SM, BufferID, *Offset + *Length);
Range = SourceRange(Start, End);
}
std::vector<VariableTypeInfo> Infos;
std::string TypeBuffer;
llvm::raw_string_ostream OS(TypeBuffer);
VariableTypesInFile Result;
collectVariableType(*SF, Range, FullyQualified, Infos, OS);
for (auto Info : Infos) {
Result.Results.push_back({Info.Offset, Info.Length, Info.TypeOffset, Info.HasExplicitType});
}
Result.TypeBuffer = OS.str();
Receiver(RequestResult<VariableTypesInFile>::fromResult(Result));
}
void cancelled() override {
Receiver(RequestResult<VariableTypesInFile>::cancelled());
}
void failed(StringRef Error) override {
Receiver(RequestResult<VariableTypesInFile>::fromError(Error));
}
};
auto Collector = std::make_shared<VariableTypeCollectorASTConsumer>(
Receiver, InputBufferName, Offset, Length, FullyQualified);
/// FIXME: When request cancellation is implemented and Xcode adopts it,
/// don't use 'OncePerASTToken'.
static const char OncePerASTToken = 0;
getASTManager()->processASTAsync(Invok, std::move(Collector),
&OncePerASTToken, CancellationToken,
llvm::vfs::getRealFileSystem());
}
|