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
|
//===--- ASTWalker.cpp - AST Traversal ------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements a recursive traversal of every node in an AST.
//
// It's important to update this traversal whenever the AST is
// changed, whether by adding a new node class or adding a new child
// to an existing node. Many walker implementations rely on being
// invoked with every node in the AST.
//
// Please follow these general rules when implementing traversal for
// a node:
//
// - Every node should be walked. If a node has both syntactic and
// semantic components, you should make sure you visit every node
// in both.
//
// - Nodes should only be walked once. So if a node has both
// syntactic and semantic components, but the type-checker builds
// the semantic components directly on top of the syntactic
// components, walking the semantic components will be sufficient
// to visit all the nodes in both.
//
// - Explicitly-written nodes should be walked in left-to-right
// syntactic order. The ordering of implicit nodes isn't
// particularly important.
//
// Note that semantic components will generally preserve the
// syntactic order of their children because doing something else
// could illegally change order of evaluation. This is why, for
// example, shuffling a TupleExpr creates a DestructureTupleExpr
// instead of just making a new TupleExpr with the elements in
// different order.
//
// - Sub-expressions and sub-statements should be replaceable.
// It's reasonable to expect that the replacement won't be
// completely unrelated to the original, but try to avoid making
// assumptions about the exact representation type. For example,
// assuming that a child expression is literally a TupleExpr may
// only be a reasonable assumption in an unchecked parse tree.
//
// - Avoid relying on the AST being type-checked or even
// well-formed during traversal.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/ASTWalker.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/GenericParamList.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/PrettyStackTrace.h"
using namespace swift;
void ASTWalker::anchor() {}
namespace {
/// Traversal - This class implements a simple expression/statement
/// recursive traverser which queries a user-provided walker class
/// on every node in an AST.
class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
/*Decl*/ bool,
Pattern *, /*TypeRepr*/ bool>
{
friend class ASTVisitor<Traversal, Expr*, Stmt*, bool, Pattern*, bool>;
typedef ASTVisitor<Traversal, Expr*, Stmt*, bool, Pattern*, bool> inherited;
ASTWalker &Walker;
/// RAII object that sets the parent of the walk context
/// appropriately.
class SetParentRAII {
ASTWalker &Walker;
decltype(ASTWalker::Parent) PriorParent;
public:
template<typename T>
SetParentRAII(ASTWalker &walker, T *newParent)
: Walker(walker), PriorParent(walker.Parent) {
walker.Parent = newParent;
}
~SetParentRAII() {
Walker.Parent = PriorParent;
}
};
[[nodiscard]]
Expr *visit(Expr *E) {
SetParentRAII SetParent(Walker, E);
return inherited::visit(E);
}
[[nodiscard]]
Stmt *visit(Stmt *S) {
SetParentRAII SetParent(Walker, S);
return inherited::visit(S);
}
[[nodiscard]]
Pattern *visit(Pattern *P) {
SetParentRAII SetParent(Walker, P);
return inherited::visit(P);
}
[[nodiscard]]
bool visit(Decl *D) {
SetParentRAII SetParent(Walker, D);
return inherited::visit(D);
}
[[nodiscard]]
bool visit(TypeRepr *T) {
SetParentRAII SetParent(Walker, T);
return inherited::visit(T);
}
[[nodiscard]]
bool visit(ParameterList *PL) {
return inherited::visit(PL);
}
//===--------------------------------------------------------------------===//
// Decls
//===--------------------------------------------------------------------===//
[[nodiscard]]
bool visitGenericParamListIfNeeded(GenericContext *GC) {
// Must check this first in case extensions have not been bound yet
if (Walker.shouldWalkIntoGenericParams()) {
if (auto *params = GC->getParsedGenericParams()) {
if (doIt(params))
return true;
}
return true;
}
return false;
}
[[nodiscard]]
bool visitTrailingRequirements(GenericContext *GC) {
if (const auto Where = GC->getTrailingWhereClause()) {
for (auto &Req: Where->getRequirements())
if (doIt(Req))
return true;
}
return false;
}
bool visitImportDecl(ImportDecl *ID) {
return false;
}
bool visitExtensionDecl(ExtensionDecl *ED) {
if (auto *typeRepr = ED->getExtendedTypeRepr())
if (doIt(typeRepr))
return true;
auto inheritedTypes = ED->getInherited();
for (auto i : inheritedTypes.getIndices()) {
if (auto *const TyR = inheritedTypes.getTypeRepr(i))
if (doIt(TyR))
return true;
}
if (visitTrailingRequirements(ED))
return true;
for (Decl *M : ED->getMembers()) {
if (doIt(M))
return true;
if (Walker.shouldWalkAccessorsTheOldWay()) {
// Pretend that accessors share a parent with the storage.
//
// FIXME: Update existing ASTWalkers to deal with accessors appearing as
// children of the storage instead.
if (auto *ASD = dyn_cast<AbstractStorageDecl>(M)) {
for (auto AD : ASD->getAllAccessors()) {
if (doIt(AD))
return true;
}
}
}
}
return false;
}
bool visitPatternBindingDecl(PatternBindingDecl *PBD) {
auto *singleVar = PBD->getSingleVar();
for (auto idx : range(PBD->getNumPatternEntries())) {
if (Pattern *Pat = doIt(PBD->getPattern(idx)))
PBD->setPattern(idx, Pat);
else
return true;
if (!PBD->getInit(idx))
continue;
if (singleVar && singleVar->getOriginalWrappedProperty())
continue;
if (PBD->isInitializerSubsumed(idx) && singleVar &&
singleVar->getAttrs().hasAttribute<LazyAttr>() &&
Walker.getLazyInitializerWalkingBehavior() !=
LazyInitializerWalking::InPatternBinding) {
break;
}
#ifndef NDEBUG
PrettyStackTraceDecl debugStack("walking into initializer for", PBD);
#endif
if (Expr *E2 = doIt(PBD->getInit(idx)))
PBD->setInit(idx, E2);
else
return true;
}
return false;
}
bool visitEnumCaseDecl(EnumCaseDecl *ECD) {
// We'll visit the EnumElementDecls separately.
return false;
}
bool visitTopLevelCodeDecl(TopLevelCodeDecl *TLCD) {
if (BraceStmt *S = cast_or_null<BraceStmt>(doIt(TLCD->getBody()))) {
TLCD->setBody(S);
return false;
}
return true;
}
bool visitIfConfigDecl(IfConfigDecl *ICD) {
// By default, just visit the elements that are actually
// injected into the enclosing context.
return false;
}
bool visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
// By default, ignore #error/#warning.
return false;
}
bool visitOperatorDecl(OperatorDecl *OD) {
return false;
}
bool visitPrecedenceGroupDecl(PrecedenceGroupDecl *PGD) {
return false;
}
bool visitTypeAliasDecl(TypeAliasDecl *TAD) {
bool WalkGenerics = visitGenericParamListIfNeeded(TAD);
if (auto typerepr = TAD->getUnderlyingTypeRepr())
if (doIt(typerepr))
return true;
return WalkGenerics && visitTrailingRequirements(TAD);
}
bool visitOpaqueTypeDecl(OpaqueTypeDecl *OTD) {
if (Walker.shouldWalkIntoGenericParams() && OTD->getGenericParams()) {
if (doIt(OTD->getGenericParams()))
return true;
}
return false;
}
bool visitGenericTypeParamDecl(GenericTypeParamDecl *GTPD) {
for (const auto &Inherit : GTPD->getInherited().getEntries()) {
if (auto *const TyR = Inherit.getTypeRepr())
if (doIt(TyR))
return true;
}
return false;
}
bool visitAssociatedTypeDecl(AssociatedTypeDecl *ATD) {
for (const auto &Inherit : ATD->getInherited().getEntries()) {
if (auto *const TyR = Inherit.getTypeRepr())
if (doIt(TyR))
return true;
}
if (const auto DefaultTy = ATD->getDefaultDefinitionTypeRepr())
if (doIt(DefaultTy))
return true;
if (auto *WhereClause = ATD->getTrailingWhereClause()) {
for (auto &Req: WhereClause->getRequirements()) {
if (doIt(Req))
return true;
}
}
return false;
}
bool visitNominalTypeDecl(NominalTypeDecl *NTD) {
#ifndef NDEBUG
PrettyStackTraceDecl debugStack("walking into", NTD);
#endif
bool WalkGenerics = visitGenericParamListIfNeeded(NTD);
auto inheritedTypes = NTD->getInherited();
for (auto i : inheritedTypes.getIndices()) {
if (auto *const TyR = inheritedTypes.getTypeRepr(i))
if (doIt(TyR))
return true;
}
// Visit requirements
if (WalkGenerics && visitTrailingRequirements(NTD))
return true;
for (Decl *Member : NTD->getMembers()) {
if (doIt(Member))
return true;
if (Walker.shouldWalkAccessorsTheOldWay()) {
// Pretend that accessors share a parent with the storage.
//
// FIXME: Update existing ASTWalkers to deal with accessors appearing as
// children of the storage instead.
if (auto *ASD = dyn_cast<AbstractStorageDecl>(Member)) {
for (auto AD : ASD->getAllAccessors()) {
if (doIt(AD))
return true;
}
}
}
}
return false;
}
bool visitModuleDecl(ModuleDecl *MD) {
// TODO: should we recurse within the module?
return false;
}
bool visitVarDecl(VarDecl *VD) {
if (!Walker.shouldWalkAccessorsTheOldWay()) {
for (auto *AD : VD->getAllAccessors())
if (doIt(AD))
return true;
}
return false;
}
bool visitParamDecl(ParamDecl *P) {
// Don't walk into the type if the decl is implicit, or if the type is
// implicit.
if (!P->isImplicit()) {
if (auto *repr = P->getTypeRepr()) {
if (doIt(repr)) {
return true;
}
}
}
if (auto *E = P->getStructuralDefaultExpr()) {
auto res = doIt(E);
if (!res) return true;
P->setDefaultExpr(res, /*isTypeChecked*/ (bool)res->getType());
}
if (!Walker.shouldWalkAccessorsTheOldWay()) {
for (auto *AD : P->getAllAccessors())
if (doIt(AD))
return true;
}
return false;
}
bool visitSubscriptDecl(SubscriptDecl *SD) {
bool WalkGenerics = visitGenericParamListIfNeeded(SD);
if (visit(SD->getIndices()))
return true;
if (auto *const TyR = SD->getElementTypeRepr())
if (doIt(TyR))
return true;
// Visit trailing requirements
if (WalkGenerics && visitTrailingRequirements(SD))
return true;
if (!Walker.shouldWalkAccessorsTheOldWay()) {
for (auto *AD : SD->getAllAccessors())
if (doIt(AD))
return true;
}
return false;
}
bool visitMissingDecl(MissingDecl *missing) {
return false;
}
bool visitMissingMemberDecl(MissingMemberDecl *MMD) {
return false;
}
bool visitMacroDecl(MacroDecl *MD) {
bool WalkGenerics = visitGenericParamListIfNeeded(MD);
if (MD->parameterList && visit(MD->parameterList))
return true;
if (auto resultTypeRepr = MD->resultType.getTypeRepr()) {
if (doIt(resultTypeRepr))
return true;
}
if (auto def = MD->definition) {
// Don't walk into unchecked definitions.
if (auto expansion = dyn_cast<MacroExpansionExpr>(def)) {
if (!expansion->getType().isNull() ||
Walker.shouldWalkIntoUncheckedMacroDefinitions()) {
if (auto newDef = doIt(def))
MD->definition = newDef;
else
return true;
}
}
}
// Visit trailing requirements
if (WalkGenerics && visitTrailingRequirements(MD))
return true;
return false;
}
bool visitFreestandingMacroArgs(FreestandingMacroExpansion *ME) {
for (TypeRepr *genArg : ME->getGenericArgs()) {
if (doIt(genArg))
return true;
}
if (ME->getArgs()) {
ArgumentList *args = doIt(ME->getArgs());
if (!args)
return true;
ME->setArgs(args);
}
return false;
}
bool visitMacroExpansionDecl(MacroExpansionDecl *MED) {
#ifndef NDEBUG
PrettyStackTraceDecl debugStack("walking into", MED);
#endif
bool shouldWalkArguments, shouldWalkExpansion;
std::tie(shouldWalkArguments, shouldWalkExpansion) =
Walker.shouldWalkMacroArgumentsAndExpansion();
if (shouldWalkArguments && visitFreestandingMacroArgs(MED))
return true;
bool alreadyFailed = false;
if (shouldWalkExpansion) {
MED->forEachExpandedNode([&](ASTNode expandedNode) {
if (alreadyFailed) return;
if (auto *expr = expandedNode.dyn_cast<Expr *>()) {
alreadyFailed = doIt(expr) == nullptr;
} else if (auto *stmt = expandedNode.dyn_cast<Stmt *>()) {
alreadyFailed = doIt(stmt) == nullptr;
} else {
auto decl = expandedNode.get<Decl *>();
if (!isa<VarDecl>(decl))
alreadyFailed = doIt(decl);
}
});
}
return alreadyFailed;
}
bool visitAbstractFunctionDecl(AbstractFunctionDecl *AFD) {
#ifndef NDEBUG
PrettyStackTraceDecl debugStack("walking into body of", AFD);
#endif
bool WalkGenerics =
// accessor generics are visited from the storage decl
!isa<AccessorDecl>(AFD) && visitGenericParamListIfNeeded(AFD);
if (auto *PD = AFD->getImplicitSelfDecl(/*createIfNeeded=*/false)) {
if (visit(PD))
return true;
}
if (visit(AFD->getParameters()))
return true;
if (auto *const ThrownTyR = AFD->getThrownTypeRepr()) {
if (doIt(ThrownTyR))
return true;
}
if (auto *FD = dyn_cast<FuncDecl>(AFD)) {
if (!isa<AccessorDecl>(FD))
if (auto *const TyR = FD->getResultTypeRepr())
if (doIt(TyR))
return true;
}
// Visit trailing requirements
if (WalkGenerics && visitTrailingRequirements(AFD))
return true;
if (AFD->getBody(/*canSynthesize=*/false)) {
AbstractFunctionDecl::BodyKind PreservedKind = AFD->getBodyKind();
if (BraceStmt *S = cast_or_null<BraceStmt>(doIt(AFD->getBody())))
AFD->setBody(S, PreservedKind);
else
return true;
}
if (auto ctor = dyn_cast<ConstructorDecl>(AFD)) {
if (auto superInit = ctor->getSuperInitCall()) {
if ((superInit = doIt(superInit)))
ctor->setSuperInitCall(superInit);
else
return true;
}
}
return false;
}
bool visitEnumElementDecl(EnumElementDecl *ED) {
if (auto *PL = ED->getParameterList()) {
if (visit(PL))
return true;
}
if (auto *rawLiteralExpr = ED->getRawValueUnchecked()) {
if (Expr *newRawExpr = doIt(rawLiteralExpr)) {
auto *newLiteralRawExpr = cast<LiteralExpr>(newRawExpr);
ED->setRawValueExpr(newLiteralRawExpr);
} else {
return true;
}
}
return false;
}
//===--------------------------------------------------------------------===//
// Exprs
//===--------------------------------------------------------------------===//
// A macro for handling the "semantic expressions" that are common
// on sugared expression nodes like string interpolation. The
// semantic expression is set up by type-checking to include all the
// other children as sub-expressions, so if it exists, we should
// just bypass the rest of the visitation.
#define HANDLE_SEMANTIC_EXPR(NODE) \
do { \
if (Expr *_semanticExpr = NODE->getSemanticExpr()) { \
if ((_semanticExpr = doIt(_semanticExpr))) { \
NODE->setSemanticExpr(_semanticExpr); \
} else { \
return nullptr; \
} \
return NODE; \
} \
} while (false)
Expr *visitErrorExpr(ErrorExpr *E) { return E; }
Expr *visitCodeCompletionExpr(CodeCompletionExpr *E) {
if (Expr *baseExpr = E->getBase()) {
Expr *newBaseExpr = doIt(baseExpr);
if (!newBaseExpr)
return nullptr;
E->setBase(newBaseExpr);
}
return E;
}
Expr *visitLiteralExpr(LiteralExpr *E) { return E; }
Expr *visitDiscardAssignmentExpr(DiscardAssignmentExpr *E) { return E; }
Expr *visitTypeExpr(TypeExpr *E) {
if (!E->isImplicit())
if (auto *typerepr = E->getTypeRepr())
if (doIt(typerepr))
return nullptr;
return E;
}
Expr *visitSuperRefExpr(SuperRefExpr *E) { return E; }
Expr *visitOtherConstructorDeclRefExpr(OtherConstructorDeclRefExpr *E) {
return E;
}
Expr *visitOverloadedDeclRefExpr(OverloadedDeclRefExpr *E) { return E; }
Expr *visitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *E) { return E; }
Expr *visitUnresolvedMemberExpr(UnresolvedMemberExpr *E) { return E; }
Expr *visitOpaqueValueExpr(OpaqueValueExpr *E) { return E; }
Expr *visitPropertyWrapperValuePlaceholderExpr(
PropertyWrapperValuePlaceholderExpr *E) {
if (E->getOpaqueValuePlaceholder()) {
if (auto *placeholder = doIt(E->getOpaqueValuePlaceholder()))
E->setOpaqueValuePlaceholder(dyn_cast<OpaqueValueExpr>(placeholder));
else
return nullptr;
}
if (Walker.shouldWalkIntoPropertyWrapperPlaceholderValue()) {
if (E->getOriginalWrappedValue()) {
if (auto *newValue = doIt(E->getOriginalWrappedValue()))
E->setOriginalWrappedValue(newValue);
else
return nullptr;
}
}
return E;
}
Expr *visitAppliedPropertyWrapperExpr(AppliedPropertyWrapperExpr *E) {
if (auto *newValue = doIt(E->getValue())) {
E->setValue(newValue);
} else {
return nullptr;
}
return E;
}
Expr *visitDefaultArgumentExpr(DefaultArgumentExpr *E) { return E; }
Expr *visitInterpolatedStringLiteralExpr(InterpolatedStringLiteralExpr *E) {
if (auto oldAppendingExpr = E->getAppendingExpr()) {
if (auto appendingExpr = doIt(oldAppendingExpr))
E->setAppendingExpr(dyn_cast<TapExpr>(appendingExpr));
else
return nullptr;
}
return E;
}
Expr *visitObjectLiteralExpr(ObjectLiteralExpr *E) {
if (auto *args = doIt(E->getArgs())) {
E->setArgs(args);
} else {
return nullptr;
}
return E;
}
Expr *visitCollectionExpr(CollectionExpr *E) {
for (auto &elt : E->getElements())
if (Expr *Sub = doIt(elt))
elt = Sub;
else
return nullptr;
return E;
}
Expr *visitDeclRefExpr(DeclRefExpr *E) {
return E;
}
Expr *visitMemberRefExpr(MemberRefExpr *E) {
if (Expr *Base = doIt(E->getBase())) {
E->setBase(Base);
return E;
}
return nullptr;
}
Expr *visitDynamicMemberRefExpr(DynamicMemberRefExpr *E) {
if (Expr *Base = doIt(E->getBase())) {
E->setBase(Base);
return E;
}
return nullptr;
}
Expr *visitAnyTryExpr(AnyTryExpr *E) {
if (Expr *subExpr = doIt(E->getSubExpr())) {
E->setSubExpr(subExpr);
return E;
}
return nullptr;
}
Expr *visitIdentityExpr(IdentityExpr *E) {
if (Expr *subExpr = doIt(E->getSubExpr())) {
E->setSubExpr(subExpr);
return E;
}
return nullptr;
}
Expr *visitCopyExpr(CopyExpr *E) {
if (Expr *subExpr = doIt(E->getSubExpr())) {
E->setSubExpr(subExpr);
return E;
}
return nullptr;
}
Expr *visitConsumeExpr(ConsumeExpr *E) {
if (Expr *subExpr = doIt(E->getSubExpr())) {
E->setSubExpr(subExpr);
return E;
}
return nullptr;
}
Expr *visitTupleExpr(TupleExpr *E) {
for (unsigned i = 0, e = E->getNumElements(); i != e; ++i)
if (E->getElement(i)) {
if (Expr *Elt = doIt(E->getElement(i)))
E->setElement(i, Elt);
else
return nullptr;
}
return E;
}
Expr *visitSubscriptExpr(SubscriptExpr *E) {
if (Expr *Base = doIt(E->getBase()))
E->setBase(Base);
else
return nullptr;
if (auto *args = doIt(E->getArgs())) {
E->setArgs(args);
} else {
return nullptr;
}
return E;
}
Expr *visitKeyPathApplicationExpr(KeyPathApplicationExpr *E) {
if (Expr *Base = doIt(E->getBase()))
E->setBase(Base);
else
return nullptr;
if (Expr *KeyPath = doIt(E->getKeyPath()))
E->setKeyPath(KeyPath);
else
return nullptr;
return E;
}
Expr *visitDynamicSubscriptExpr(DynamicSubscriptExpr *E) {
if (Expr *Base = doIt(E->getBase()))
E->setBase(Base);
else
return nullptr;
if (auto *args = doIt(E->getArgs())) {
E->setArgs(args);
} else {
return nullptr;
}
return E;
}
Expr *visitUnresolvedDotExpr(UnresolvedDotExpr *E) {
if (!E->getBase())
return E;
if (Expr *E2 = doIt(E->getBase())) {
E->setBase(E2);
return E;
}
return nullptr;
}
Expr *visitUnresolvedSpecializeExpr(UnresolvedSpecializeExpr *E) {
if (!E->getSubExpr())
return E;
if (Expr *Sub = doIt(E->getSubExpr()))
E->setSubExpr(Sub);
else
return nullptr;
for (auto &TyLoc : E->getUnresolvedParams()) {
if (doIt(TyLoc))
return nullptr;
}
return E;
}
Expr *visitTupleElementExpr(TupleElementExpr *E) {
if (Expr *E2 = doIt(E->getBase())) {
E->setBase(E2);
return E;
}
return nullptr;
}
Expr *visitImplicitConversionExpr(ImplicitConversionExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
return E;
}
return nullptr;
}
Expr *visitErasureExpr(ErasureExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
} else {
return nullptr;
}
for (unsigned i = 0; i < E->getArgumentConversions().size(); ++i) {
const auto &conv = E->getArgumentConversions()[i];
auto kConv = conv.Conversion;
if (!kConv) {
return nullptr;
} else if (Expr *E2 = doIt(kConv)) {
E->setArgumentConversion(i, {conv.OrigValue, E2});
} else {
return nullptr;
}
}
return E;
}
Expr *visitCollectionUpcastConversionExpr(CollectionUpcastConversionExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
} else {
return nullptr;
}
if (auto &keyConv = E->getKeyConversion()) {
auto kConv = keyConv.Conversion;
if (!kConv) {
return nullptr;
} else if (Expr *E2 = doIt(kConv)) {
E->setKeyConversion({keyConv.OrigValue, E2});
} else {
return nullptr;
}
}
if (auto &valueConv = E->getValueConversion()) {
auto vConv = valueConv.Conversion;
if (!vConv) {
return nullptr;
} else if (Expr *E2 = doIt(vConv)) {
E->setValueConversion({valueConv.OrigValue, E2});
} else {
return nullptr;
}
}
return E;
}
Expr *visitDestructureTupleExpr(DestructureTupleExpr *E) {
if (auto *src = doIt(E->getSubExpr())) {
E->setSubExpr(src);
} else {
return nullptr;
}
if (auto *dst = doIt(E->getResultExpr())) {
E->setResultExpr(dst);
} else {
return nullptr;
}
return E;
}
Expr *visitTryExpr(TryExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
return E;
}
return nullptr;
}
Expr *visitForceTryExpr(ForceTryExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
return E;
}
return nullptr;
}
Expr *visitOptionalTryExpr(OptionalTryExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
return E;
}
return nullptr;
}
Expr *visitInOutExpr(InOutExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
return E;
}
return nullptr;
}
Expr *visitVarargExpansionExpr(VarargExpansionExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
return E;
}
return nullptr;
}
Expr *visitPackExpansionExpr(PackExpansionExpr *E) {
if (Expr *pattern = doIt(E->getPatternExpr())) {
E->setPatternExpr(pattern);
return E;
}
return nullptr;
}
Expr *visitPackElementExpr(PackElementExpr *E) {
if (Expr *pattern = doIt(E->getPackRefExpr())) {
E->setPackRefExpr(pattern);
return E;
}
return nullptr;
}
Expr *visitMaterializePackExpr(MaterializePackExpr *E) {
if (Expr *fromExpr = doIt(E->getFromExpr())) {
E->setFromExpr(fromExpr);
return E;
}
return nullptr;
}
Expr *visitSequenceExpr(SequenceExpr *E) {
for (unsigned i = 0, e = E->getNumElements(); i != e; ++i)
if (Expr *Elt = doIt(E->getElement(i)))
E->setElement(i, Elt);
else
return nullptr;
return E;
}
Expr *visitDynamicTypeExpr(DynamicTypeExpr *E) {
Expr *base = E->getBase();
if ((base = doIt(base)))
E->setBase(base);
else
return nullptr;
return E;
}
Expr *visitCaptureListExpr(CaptureListExpr *expr) {
for (auto c : expr->getCaptureList()) {
if (Walker.shouldWalkCaptureInitializerExpressions()) {
for (auto entryIdx : range(c.PBD->getNumPatternEntries())) {
if (auto newInit = doIt(c.PBD->getInit(entryIdx)))
c.PBD->setInit(entryIdx, newInit);
else
return nullptr;
}
} else {
if (doIt(c.PBD))
return nullptr;
}
}
AbstractClosureExpr *body = expr->getClosureBody();
if ((body = cast_or_null<AbstractClosureExpr>(doIt(body))))
expr->setClosureBody(body);
else
return nullptr;
return expr;
}
Expr *visitClosureExpr(ClosureExpr *expr) {
if (visit(expr->getParameters()))
return nullptr;
if (auto thrownTypeRepr = expr->getExplicitThrownTypeRepr()) {
if (doIt(thrownTypeRepr))
return nullptr;
}
if (expr->hasExplicitResultType()) {
if (doIt(expr->getExplicitResultTypeRepr()))
return nullptr;
}
// If the closure was separately type checked and we don't want to
// visit separately-checked closure bodies, bail out now.
if (expr->isSeparatelyTypeChecked() &&
!Walker.shouldWalkIntoSeparatelyCheckedClosure(expr))
return expr;
// Handle other closures.
if (BraceStmt *body = cast_or_null<BraceStmt>(doIt(expr->getBody()))) {
expr->setBody(body);
return expr;
}
return nullptr;
}
Expr *visitAutoClosureExpr(AutoClosureExpr *E) {
if (Expr *E2 = doIt(E->getSingleExpressionBody())) {
E->setBody(E2);
return E;
}
return nullptr;
}
Expr *visitApplyExpr(ApplyExpr *E) {
if (E->getFn()) {
Expr *E2 = doIt(E->getFn());
if (E2 == nullptr) return nullptr;
E->setFn(E2);
}
if (auto *args = doIt(E->getArgs())) {
E->setArgs(args);
} else {
return nullptr;
}
return E;
}
Expr *visitSelfApplyExpr(SelfApplyExpr *E) {
if (E->getBase()) {
Expr *E2 = doIt(E->getBase());
if (E2 == nullptr) return nullptr;
E->setBase(E2);
}
if (E->getFn()) {
Expr *E2 = doIt(E->getFn());
if (E2 == nullptr) return nullptr;
E->setFn(E2);
}
return E;
}
Expr *visitDotSyntaxBaseIgnoredExpr(DotSyntaxBaseIgnoredExpr *E) {
Expr *E2 = doIt(E->getLHS());
if (E2 == nullptr) return nullptr;
E->setLHS(E2);
E2 = doIt(E->getRHS());
if (E2 == nullptr) return nullptr;
E->setRHS(E2);
return E;
}
Expr *visitExplicitCastExpr(ExplicitCastExpr *E) {
if (Expr *Sub = E->getSubExpr()) {
Sub = doIt(Sub);
if (!Sub) return nullptr;
E->setSubExpr(Sub);
}
if (auto *const tyRepr = E->getCastTypeRepr())
if (doIt(tyRepr))
return nullptr;
return E;
}
Expr *visitArrowExpr(ArrowExpr *E) {
if (Expr *Args = E->getArgsExpr()) {
Args = doIt(Args);
if (!Args) return nullptr;
E->setArgsExpr(Args);
}
if (Expr *Thrown = E->getThrownTypeExpr()) {
Thrown = doIt(Thrown);
if (!Thrown) return nullptr;
E->setThrownTypeExpr(Thrown);
}
if (Expr *Result = E->getResultExpr()) {
Result = doIt(Result);
if (!Result) return nullptr;
E->setResultExpr(Result);
}
return E;
}
Expr *visitRebindSelfInConstructorExpr(RebindSelfInConstructorExpr *E) {
Expr *Sub = doIt(E->getSubExpr());
if (!Sub) return nullptr;
E->setSubExpr(Sub);
return E;
}
Expr *visitAssignExpr(AssignExpr *AE) {
if (Expr *Dest = AE->getDest()) {
if (!(Dest = doIt(Dest)))
return nullptr;
AE->setDest(Dest);
}
if (Expr *Src = AE->getSrc()) {
if (!(Src = doIt(AE->getSrc())))
return nullptr;
AE->setSrc(Src);
}
return AE;
}
Expr *visitEnumIsCaseExpr(EnumIsCaseExpr *E) {
if (Expr *Sub = E->getSubExpr()) {
if (!(Sub = doIt(Sub)))
return nullptr;
E->setSubExpr(Sub);
}
if (auto *typerepr = E->getCaseTypeRepr())
if (doIt(typerepr))
return nullptr;
return E;
}
Expr *visitTernaryExpr(TernaryExpr *E) {
if (Expr *Cond = E->getCondExpr()) {
Cond = doIt(Cond);
if (!Cond) return nullptr;
E->setCondExpr(Cond);
}
Expr *Then = doIt(E->getThenExpr());
if (!Then) return nullptr;
E->setThenExpr(Then);
if (Expr *Else = E->getElseExpr()) {
Else = doIt(Else);
if (!Else) return nullptr;
E->setElseExpr(Else);
}
return E;
}
Expr *visitUnresolvedPatternExpr(UnresolvedPatternExpr *E) {
Pattern *sub = doIt(E->getSubPattern());
if (!sub) return nullptr;
E->setSubPattern(sub);
return E;
}
Expr *visitBindOptionalExpr(BindOptionalExpr *E) {
Expr *sub = doIt(E->getSubExpr());
if (!sub) return nullptr;
E->setSubExpr(sub);
return E;
}
Expr *visitOptionalEvaluationExpr(OptionalEvaluationExpr *E) {
Expr *sub = doIt(E->getSubExpr());
if (!sub) return nullptr;
E->setSubExpr(sub);
return E;
}
Expr *visitForceValueExpr(ForceValueExpr *E) {
Expr *sub = doIt(E->getSubExpr());
if (!sub) return nullptr;
E->setSubExpr(sub);
return E;
}
Expr *visitOpenExistentialExpr(OpenExistentialExpr *E) {
Expr *existential = doIt(E->getExistentialValue());
if (!existential) return nullptr;
Expr *sub = doIt(E->getSubExpr());
if (!sub) return nullptr;
E->setExistentialValue(existential);
E->setSubExpr(sub);
return E;
}
Expr *visitMakeTemporarilyEscapableExpr(MakeTemporarilyEscapableExpr *E) {
Expr *closure = doIt(E->getNonescapingClosureValue());
if (!closure) return nullptr;
Expr *sub = doIt(E->getSubExpr());
if (!sub) return nullptr;
E->setNonescapingClosureValue(closure);
E->setSubExpr(sub);
return E;
}
Expr *visitEditorPlaceholderExpr(EditorPlaceholderExpr *E) {
HANDLE_SEMANTIC_EXPR(E);
return E;
}
Expr *visitLazyInitializerExpr(LazyInitializerExpr *E) {
// The initializer is opaque unless we specifically want to visit it as part
// of the accessor body.
if (Walker.getLazyInitializerWalkingBehavior() !=
LazyInitializerWalking::InAccessor) {
return E;
}
auto *sub = doIt(E->getSubExpr());
if (!sub)
return nullptr;
E->setSubExpr(sub);
return E;
}
Expr *visitObjCSelectorExpr(ObjCSelectorExpr *E) {
Expr *sub = doIt(E->getSubExpr());
if (!sub) return nullptr;
E->setSubExpr(sub);
return E;
}
Expr *visitKeyPathExpr(KeyPathExpr *E) {
// For an ObjC key path, the string literal expr serves as the semantic
// expression.
if (auto objcStringLiteral = E->getObjCStringLiteralExpr()) {
Expr *sub = doIt(objcStringLiteral);
if (!sub) return nullptr;
E->setObjCStringLiteralExpr(sub);
}
auto components = E->getComponents();
if (components.empty()) {
// No components means a parsed-only/pre-resolution Swift key path.
assert(!E->isObjC());
if (auto parsedRoot = E->getParsedRoot()) {
Expr *newRoot = doIt(parsedRoot);
if (!newRoot)
return nullptr;
E->setParsedRoot(newRoot);
}
if (auto parsedPath = E->getParsedPath()) {
Expr *newPath = doIt(parsedPath);
if (!newPath)
return nullptr;
E->setParsedPath(newPath);
}
return E;
}
if (!E->isObjC()) {
auto rootType = E->getExplicitRootType();
if (rootType && doIt(rootType))
return nullptr;
}
for (auto &origComponent : components) {
auto component = origComponent;
switch (auto kind = component.getKind()) {
case KeyPathExpr::Component::Kind::Subscript:
case KeyPathExpr::Component::Kind::UnresolvedSubscript: {
if (auto *newArgs = doIt(component.getSubscriptArgs())) {
component.setSubscriptArgs(newArgs);
} else {
return nullptr;
}
break;
}
case KeyPathExpr::Component::Kind::OptionalChain:
case KeyPathExpr::Component::Kind::OptionalWrap:
case KeyPathExpr::Component::Kind::OptionalForce:
case KeyPathExpr::Component::Kind::Property:
case KeyPathExpr::Component::Kind::UnresolvedProperty:
case KeyPathExpr::Component::Kind::Invalid:
case KeyPathExpr::Component::Kind::Identity:
case KeyPathExpr::Component::Kind::TupleElement:
case KeyPathExpr::Component::Kind::DictionaryKey:
case KeyPathExpr::Component::Kind::CodeCompletion:
// No subexpr to visit.
break;
}
}
return E;
}
Expr *visitCurrentContextIsolationExpr(CurrentContextIsolationExpr *E) {
if (auto actor = E->getActor()) {
if (auto newActor = doIt(actor))
E->setActor(newActor);
else
return nullptr;
}
return E;
}
Expr *visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *E) {
if (auto fn = E->getFunctionExpr()) {
if (auto newFn = doIt(fn))
E->setFunctionExpr(newFn);
else
return nullptr;
}
return E;
}
Expr *visitKeyPathDotExpr(KeyPathDotExpr *E) { return E; }
Expr *visitSingleValueStmtExpr(SingleValueStmtExpr *E) {
if (auto *S = doIt(E->getStmt())) {
E->setStmt(S);
} else {
return nullptr;
}
return E;
}
Expr *visitOneWayExpr(OneWayExpr *E) {
if (auto oldSubExpr = E->getSubExpr()) {
if (auto subExpr = doIt(oldSubExpr)) {
E->setSubExpr(subExpr);
} else {
return nullptr;
}
}
return E;
}
Expr *visitTapExpr(TapExpr *E) {
if (auto oldSubExpr = E->getSubExpr()) {
if (auto subExpr = doIt(oldSubExpr)) {
E->setSubExpr(subExpr);
} else {
return nullptr;
}
}
if (!Walker.shouldWalkIntoTapExpression())
return E;
if (auto oldBody = E->getBody()) {
if (auto body = doIt(oldBody)) {
E->setBody(dyn_cast<BraceStmt>(body));
}
else {
return nullptr;
}
}
return E;
}
Expr *visitRegexLiteralExpr(RegexLiteralExpr *E) {
return E;
}
Expr *visitTypeJoinExpr(TypeJoinExpr *E) {
if (auto *var = E->getVar()) {
if (auto *newVar = dyn_cast<DeclRefExpr>(doIt(var))) {
E->setVar(newVar);
} else {
return nullptr;
}
}
for (unsigned i = 0, e = E->getNumElements(); i != e; ++i) {
if (auto *origElt = E->getElement(i)) {
if (Expr *Elt = doIt(origElt))
E->setElement(i, Elt);
else
return nullptr;
}
}
return E;
}
Expr *visitMacroExpansionExpr(MacroExpansionExpr *E) {
bool shouldWalkArguments, shouldWalkExpansion;
std::tie(shouldWalkArguments, shouldWalkExpansion) =
Walker.shouldWalkMacroArgumentsAndExpansion();
if (auto *substituteDecl = E->getSubstituteDecl()) {
if (doIt(substituteDecl))
return nullptr;
// Visiting the substitute macro expansion decl will visit the same
// argument list. Skip visiting it again.
shouldWalkArguments = false;
}
if (shouldWalkArguments && visitFreestandingMacroArgs(E))
return nullptr;
if (shouldWalkExpansion) {
Expr *rewritten = nullptr;
if (E->getRewritten()) {
rewritten = doIt(E->getRewritten());
if (!rewritten) return nullptr;
}
E->setRewritten(rewritten);
}
return E;
}
//===--------------------------------------------------------------------===//
// Everything Else
//===--------------------------------------------------------------------===//
#define STMT(Id, Parent) Stmt *visit##Id##Stmt(Id##Stmt *S);
#include "swift/AST/StmtNodes.def"
#define PATTERN(Id, Parent) Pattern *visit##Id##Pattern(Id##Pattern *P);
#include "swift/AST/PatternNodes.def"
#define TYPEREPR(Id, Parent) bool visit##Id##TypeRepr(Id##TypeRepr *T);
#include "swift/AST/TypeReprNodes.def"
bool visitDeclRefTypeRepr(DeclRefTypeRepr *T);
using Action = ASTWalker::Action;
using PreWalkAction = ASTWalker::PreWalkAction;
using PostWalkAction = ASTWalker::PostWalkAction;
template <typename T>
using PreWalkResult = ASTWalker::PreWalkResult<T>;
template <typename T>
using PostWalkResult = ASTWalker::PostWalkResult<T>;
[[nodiscard]]
bool traverse(PreWalkAction Pre, llvm::function_ref<bool(void)> VisitChildren,
llvm::function_ref<PostWalkAction(void)> WalkPost) {
switch (Pre.Action) {
case PreWalkAction::Stop:
return true;
case PreWalkAction::SkipNode:
return false;
case PreWalkAction::SkipChildren:
break;
case PreWalkAction::Continue:
if (VisitChildren())
return true;
break;
}
switch (WalkPost().Action) {
case PostWalkAction::Stop:
return true;
case PostWalkAction::Continue:
return false;
}
llvm_unreachable("Unhandled case in switch!");
}
template <typename T>
[[nodiscard]]
T *traverse(PreWalkResult<T *> Pre,
llvm::function_ref<T *(T *)> VisitChildren,
llvm::function_ref<PostWalkResult<T *>(T *)> WalkPost) {
auto Node = Pre.Value;
assert(!Node || *Node && "Use Action::Stop instead of returning nullptr");
switch (Pre.Action.Action) {
case PreWalkAction::Stop:
return nullptr;
case PreWalkAction::SkipNode:
return *Node;
case PreWalkAction::SkipChildren:
break;
case PreWalkAction::Continue: {
auto NewNode = VisitChildren(*Node);
if (!NewNode)
return nullptr;
Node = NewNode;
break;
}
}
auto Post = WalkPost(*Node);
switch (Post.Action.Action) {
case PostWalkAction::Stop:
return nullptr;
case PostWalkAction::Continue:
assert(*Post.Value && "Use Action::Stop instead of returning nullptr");
return *Post.Value;
}
llvm_unreachable("Unhandled case in switch!");
}
[[nodiscard]]
bool visitParameterList(ParameterList *PL) {
return traverse(
Walker.walkToParameterListPre(PL),
[&]() {
for (auto P : *PL) {
// Walk each parameter's decl and typeloc and default value.
if (doIt(P))
return true;
}
return false;
},
[&]() { return Walker.walkToParameterListPost(PL); });
}
public:
Traversal(ASTWalker &walker) : Walker(walker) {}
[[nodiscard]]
Expr *doIt(Expr *E) {
return traverse<Expr>(
Walker.walkToExprPre(E),
[&](Expr *E) { return visit(E); },
[&](Expr *E) { return Walker.walkToExprPost(E); });
}
[[nodiscard]]
Stmt *doIt(Stmt *S) {
return traverse<Stmt>(
Walker.walkToStmtPre(S),
[&](Stmt *S) { return visit(S); },
[&](Stmt *S) { return Walker.walkToStmtPost(S); });
}
bool shouldSkip(Decl *D) {
if (!Walker.shouldWalkMacroArgumentsAndExpansion().second &&
D->isInMacroExpansionInContext() && !Walker.Parent.isNull())
return true;
if (auto *VD = dyn_cast<VarDecl>(D)) {
// VarDecls are walked via their NamedPattern, ignore them if we encounter
// then in the few cases where they are also pushed outside as members.
// In all those cases we can walk them via the pattern binding decl.
// This is used for when vising VarDecls from source, when visiting a
// module file we walk them as we encounter them.
if (Walker.Parent.getAsModule() &&
D->getDeclContext()->getParentSourceFile())
return true;
if (Walker.Parent.getAsDecl() && VD->getParentPatternBinding())
return true;
auto walkerParentAsStmt = Walker.Parent.getAsStmt();
if (isa_and_nonnull<BraceStmt>(walkerParentAsStmt))
return true;
}
return false;
}
/// Returns true on failure.
[[nodiscard]]
bool doIt(Decl *D) {
if (shouldSkip(D))
return false;
return traverse(
Walker.walkToDeclPre(D),
[&]() { return visit(D); },
[&]() { return Walker.walkToDeclPost(D); });
}
[[nodiscard]]
Pattern *doIt(Pattern *P) {
return traverse<Pattern>(
Walker.walkToPatternPre(P),
[&](Pattern *P) { return visit(P); },
[&](Pattern *P) { return Walker.walkToPatternPost(P); });
}
[[nodiscard]]
bool doIt(const StmtCondition &C) {
for (auto &elt : C) {
switch (elt.getKind()) {
case StmtConditionElement::CK_Availability:
break;
case StmtConditionElement::CK_HasSymbol: {
auto E = elt.getHasSymbolInfo()->getSymbolExpr();
if (!E)
return true;
E = doIt(E);
if (!E)
return true;
elt.getHasSymbolInfo()->setSymbolExpr(E);
break;
}
case StmtConditionElement::CK_Boolean: {
auto E = elt.getBoolean();
// Walk an expression condition normally.
E = doIt(E);
if (!E)
return true;
elt.setBoolean(E);
break;
}
case StmtConditionElement::CK_PatternBinding: {
auto *P = doIt(elt.getPattern());
if (!P) return true;
elt.setPattern(P);
auto *I = doIt(elt.getInitializer());
if (!I) return true;
elt.setInitializer(I);
}
}
}
return false;
}
private:
/// Walk a `QualifiedIdentTypeRepr` in source order such that each subsequent
/// dot-separated component is a child of the previous one
[[nodiscard]] bool doItInSourceOrderRecursive(QualifiedIdentTypeRepr *T);
public:
/// Returns true on failure.
[[nodiscard]]
bool doIt(TypeRepr *T) {
if (auto *QualIdentTR = dyn_cast<QualifiedIdentTypeRepr>(T)) {
switch (Walker.getQualifiedIdentTypeReprWalkingScheme()) {
case QualifiedIdentTypeReprWalkingScheme::SourceOrderRecursive:
return doItInSourceOrderRecursive(QualIdentTR);
case QualifiedIdentTypeReprWalkingScheme::ASTOrderRecursive:
break;
}
}
return traverse(
Walker.walkToTypeReprPre(T),
[&]() { return visit(T); },
[&]() { return Walker.walkToTypeReprPost(T); });
}
[[nodiscard]]
bool doIt(RequirementRepr &Req) {
switch (Req.getKind()) {
case RequirementReprKind::SameType:
if (doIt(Req.getFirstTypeRepr()) || doIt(Req.getSecondTypeRepr()))
return true;
break;
case RequirementReprKind::TypeConstraint:
if (doIt(Req.getSubjectRepr()) || doIt(Req.getConstraintRepr()))
return true;
break;
case RequirementReprKind::LayoutConstraint:
if (doIt(Req.getSubjectRepr()))
return true;
break;
}
return false;
}
[[nodiscard]]
bool doIt(GenericParamList *GPL) {
// Visit generic params
for (auto &P : GPL->getParams()) {
if (doIt(P))
return true;
}
// Visit param conformance
for (auto Req : GPL->getRequirements()) {
if (doIt(Req))
return true;
}
return false;
}
[[nodiscard]]
bool doIt(ArgumentList *ArgList, unsigned Idx) {
auto Arg = ArgList->get(Idx);
return traverse(
Walker.walkToArgumentPre(Arg),
[&]() {
auto *E = doIt(Arg.getExpr());
if (!E)
return true;
ArgList->setExpr(Idx, E);
return false;
},
[&]() { return Walker.walkToArgumentPost(Arg); });
}
[[nodiscard]]
ArgumentList *visit(ArgumentList *ArgList) {
for (auto Idx : indices(*ArgList)) {
if (doIt(ArgList, Idx))
return nullptr;
}
return ArgList;
}
[[nodiscard]]
ArgumentList *doIt(ArgumentList *ArgList) {
return traverse<ArgumentList>(
Walker.walkToArgumentListPre(ArgList),
[&](ArgumentList *ArgList) { return visit(ArgList); },
[&](ArgumentList *ArgList) {
return Walker.walkToArgumentListPost(ArgList);
});
}
};
} // end anonymous namespace
bool Traversal::doItInSourceOrderRecursive(QualifiedIdentTypeRepr *T) {
// Qualified types are modeled resursively such that each previous
// dot-separated component is a child of the next one. To walk a member type
// representation according to
// `QualifiedIdentTypeReprWalkingScheme::SourceOrderRecursive`:
// 1. Pre-walk the dot-separated components in source order. If asked to skip
// the children of a given component:
// 1. Set the depth at which to start post-walking later.
// 2. Skip its generic arguments and subsequent components.
std::function<bool(TypeRepr *, std::optional<unsigned> &, unsigned)>
doItInSourceOrderPre = [&](TypeRepr *T,
std::optional<unsigned> &StartPostWalkDepth,
unsigned Depth) {
if (auto *QualIdentTR = dyn_cast<QualifiedIdentTypeRepr>(T)) {
if (doItInSourceOrderPre(QualIdentTR->getBase(), StartPostWalkDepth,
Depth + 1)) {
return true;
}
if (StartPostWalkDepth.has_value()) {
return false;
}
}
switch (this->Walker.walkToTypeReprPre(T).Action) {
case PreWalkAction::Stop:
return true;
case PreWalkAction::SkipChildren:
StartPostWalkDepth = Depth;
return false;
case PreWalkAction::SkipNode:
StartPostWalkDepth = Depth + 1;
return false;
case PreWalkAction::Continue:
break;
}
if (auto *DeclRefTR = dyn_cast<DeclRefTypeRepr>(T)) {
for (auto *Arg : DeclRefTR->getGenericArgs()) {
if (doIt(Arg)) {
return true;
}
}
} else if (visit(T)) {
return true;
}
return false;
};
// 2. Post-walk the components in reverse order, respecting the depth at which
// to start post-walking if set.
std::function<bool(TypeRepr *, std::optional<unsigned>, unsigned)>
doItInSourceOrderPost = [&](TypeRepr *T,
std::optional<unsigned> StartPostWalkDepth,
unsigned Depth) {
if (!StartPostWalkDepth.has_value() || Depth >= *StartPostWalkDepth) {
switch (this->Walker.walkToTypeReprPost(T).Action) {
case PostWalkAction::Continue:
break;
case PostWalkAction::Stop:
return true;
}
}
if (auto *QualIdentTR = dyn_cast<QualifiedIdentTypeRepr>(T)) {
return doItInSourceOrderPost(QualIdentTR->getBase(),
StartPostWalkDepth, Depth + 1);
}
return false;
};
std::optional<unsigned> StartPostWalkDepth;
if (doItInSourceOrderPre(T, StartPostWalkDepth, 0)) {
return true;
}
return doItInSourceOrderPost(T, StartPostWalkDepth, 0);
}
#pragma mark Statement traversal
Stmt *Traversal::visitBreakStmt(BreakStmt *BS) {
return BS;
}
Stmt *Traversal::visitContinueStmt(ContinueStmt *CS) {
return CS;
}
Stmt *Traversal::visitFallthroughStmt(FallthroughStmt *CS) {
return CS;
}
Stmt *Traversal::visitFailStmt(FailStmt *FS) {
return FS;
}
Stmt *Traversal::visitThrowStmt(ThrowStmt *TS) {
if (Expr *E = doIt(TS->getSubExpr())) {
TS->setSubExpr(E);
return TS;
}
return nullptr;
}
Stmt *Traversal::visitDiscardStmt(DiscardStmt *DS) {
if (Expr *E = doIt(DS->getSubExpr())) {
DS->setSubExpr(E);
return DS;
}
return nullptr;
}
Stmt *Traversal::visitPoundAssertStmt(PoundAssertStmt *S) {
if (auto *condition = doIt(S->getCondition())) {
S->setCondition(condition);
} else {
return nullptr;
}
return S;
}
Stmt *Traversal::visitBraceStmt(BraceStmt *BS) {
for (auto &Elem : BS->getElements()) {
if (auto *SubExpr = Elem.dyn_cast<Expr*>()) {
if (Expr *E2 = doIt(SubExpr))
Elem = E2;
else
return nullptr;
continue;
}
if (auto *S = Elem.dyn_cast<Stmt*>()) {
if (Stmt *S2 = doIt(S))
Elem = S2;
else
return nullptr;
continue;
}
auto *D = Elem.get<Decl*>();
if (doIt(D))
return nullptr;
if (Walker.shouldWalkAccessorsTheOldWay()) {
// Pretend that accessors share a parent with the storage.
//
// FIXME: Update existing ASTWalkers to deal with accessors appearing as
// children of the storage instead.
if (auto *ASD = dyn_cast<AbstractStorageDecl>(D)) {
for (auto AD : ASD->getAllAccessors()) {
if (doIt(AD))
return nullptr;
}
}
}
}
return BS;
}
Stmt *Traversal::visitReturnStmt(ReturnStmt *RS) {
if (!RS->hasResult())
return RS;
if (Expr *E = doIt(RS->getResult()))
RS->setResult(E);
else
return nullptr;
return RS;
}
Stmt *Traversal::visitYieldStmt(YieldStmt *YS) {
for (auto &yield : YS->getMutableYields()) {
if (Expr *E = doIt(yield))
yield = E;
else
return nullptr;
}
return YS;
}
Stmt *Traversal::visitThenStmt(ThenStmt *TS) {
auto *E = doIt(TS->getResult());
if (!E)
return nullptr;
TS->setResult(E);
return TS;
}
Stmt *Traversal::visitDeferStmt(DeferStmt *DS) {
if (doIt(DS->getTempDecl()))
return nullptr;
if (Expr *Call = doIt(DS->getCallExpr()))
DS->setCallExpr(Call);
else
return nullptr;
return DS;
}
Stmt *Traversal::visitIfStmt(IfStmt *IS) {
if (doIt(IS->getCond()))
return nullptr;
if (auto *S2 = doIt(IS->getThenStmt()))
IS->setThenStmt(cast<BraceStmt>(S2));
else
return nullptr;
if (IS->getElseStmt()) {
if (Stmt *S2 = doIt(IS->getElseStmt()))
IS->setElseStmt(S2);
else
return nullptr;
}
return IS;
}
Stmt *Traversal::visitGuardStmt(GuardStmt *US) {
if (doIt(US->getCond()))
return nullptr;
if (BraceStmt *S2 = cast_or_null<BraceStmt>(doIt(US->getBody())))
US->setBody(S2);
else
return nullptr;
return US;
}
Stmt *Traversal::visitDoStmt(DoStmt *DS) {
if (BraceStmt *S2 = cast_or_null<BraceStmt>(doIt(DS->getBody())))
DS->setBody(S2);
else
return nullptr;
return DS;
}
Stmt *Traversal::visitDoCatchStmt(DoCatchStmt *stmt) {
// Transform the body of the 'do'.
if (Stmt *newBody = doIt(stmt->getBody())) {
stmt->setBody(newBody);
} else {
return nullptr;
}
// Transform each of the catch clauses:
for (CaseStmt *&clause : stmt->getMutableCatches()) {
if (auto newClause = doIt(clause)) {
clause = cast<CaseStmt>(newClause);
} else {
return nullptr;
}
}
return stmt;
}
Stmt *Traversal::visitWhileStmt(WhileStmt *WS) {
if (doIt(WS->getCond()))
return nullptr;
if (Stmt *S2 = doIt(WS->getBody()))
WS->setBody(S2);
else
return nullptr;
return WS;
}
Stmt *Traversal::visitRepeatWhileStmt(RepeatWhileStmt *RWS) {
if (Stmt *S2 = doIt(RWS->getBody()))
RWS->setBody(S2);
else
return nullptr;
if (Expr *E2 = doIt(RWS->getCond()))
RWS->setCond(E2);
else
return nullptr;
return RWS;
}
Stmt *Traversal::visitForEachStmt(ForEachStmt *S) {
if (Pattern *P = S->getPattern()) {
if ((P = doIt(P)))
assert(P == S->getPattern() && "cannot change pattern of ForEachStmt");
else
return nullptr;
}
// The iterator decl is built directly on top of the sequence
// expression, so don't visit both.
//
// If for-in is already type-checked, the type-checked version
// of the sequence is going to be visited as part of `iteratorVar`.
if (auto IteratorVar = S->getIteratorVar()) {
if (doIt(IteratorVar))
return nullptr;
if (auto NextCall = S->getNextCall()) {
if ((NextCall = doIt(NextCall)))
S->setNextCall(NextCall);
else
return nullptr;
}
} else {
if (Expr *Sequence = S->getParsedSequence()) {
if ((Sequence = doIt(Sequence)))
S->setParsedSequence(Sequence);
else
return nullptr;
}
}
if (Expr *Where = S->getWhere()) {
if ((Where = doIt(Where)))
S->setWhere(Where);
else
return nullptr;
}
if (auto IteratorNext = S->getConvertElementExpr()) {
if ((IteratorNext = doIt(IteratorNext)))
S->setConvertElementExpr(IteratorNext);
else
return nullptr;
}
if (Stmt *Body = S->getBody()) {
if ((Body = doIt(Body)))
S->setBody(cast<BraceStmt>(Body));
else
return nullptr;
}
return S;
}
Stmt *Traversal::visitSwitchStmt(SwitchStmt *S) {
if (Expr *newSubject = doIt(S->getSubjectExpr()))
S->setSubjectExpr(newSubject);
else
return nullptr;
for (auto N : S->getRawCases()) {
if (Stmt *aCase = N.dyn_cast<Stmt*>()) {
assert(isa<CaseStmt>(aCase));
if (Stmt *aStmt = doIt(aCase)) {
assert(aCase == aStmt && "switch case remap not supported");
(void)aStmt;
} else
return nullptr;
} else {
assert(isa<IfConfigDecl>(N.get<Decl*>()) ||
isa<PoundDiagnosticDecl>(N.get<Decl*>()));
if (doIt(N.get<Decl*>()))
return nullptr;
}
}
return S;
}
Stmt *Traversal::visitCaseStmt(CaseStmt *S) {
for (auto &CLI : S->getMutableCaseLabelItems()) {
if (auto *newPattern = doIt(CLI.getPattern()))
CLI.setPattern(newPattern, /*resolved=*/CLI.isPatternResolved());
else
return nullptr;
if (CLI.getGuardExpr()) {
if (auto *newGuard = doIt(CLI.getGuardExpr()))
CLI.setGuardExpr(newGuard);
else
return nullptr;
}
}
if (BraceStmt *newBody = cast_or_null<BraceStmt>(doIt(S->getBody())))
S->setBody(newBody);
else
return nullptr;
return S;
}
#pragma mark Pattern traversal
Pattern *Traversal::visitParenPattern(ParenPattern *P) {
if (Pattern *newSub = doIt(P->getSubPattern()))
P->setSubPattern(newSub);
else
return nullptr;
return P;
}
Pattern *Traversal::visitTuplePattern(TuplePattern *P) {
for (auto &element : P->getElements()) {
if (Pattern *newField = doIt(element.getPattern()))
element.setPattern(newField);
else
return nullptr;
}
return P;
}
Pattern *Traversal::visitNamedPattern(NamedPattern *P) {
if (doIt(P->getDecl()))
return nullptr;
return P;
}
Pattern *Traversal::visitAnyPattern(AnyPattern *P) {
return P;
}
Pattern *Traversal::visitTypedPattern(TypedPattern *P) {
if (Pattern *newSub = doIt(P->getSubPattern()))
P->setSubPattern(newSub);
else
return nullptr;
if (!P->isImplicit())
if (auto *TR = P->getTypeRepr())
if (doIt(TR))
return nullptr;
return P;
}
Pattern *Traversal::visitIsPattern(IsPattern *P) {
if (auto sub = P->getSubPattern()) {
if (Pattern *newSub = doIt(sub)) {
P->setSubPattern(newSub);
} else {
return nullptr;
}
}
if (!P->isImplicit())
if (auto *TR = P->getCastTypeRepr())
if (doIt(TR))
return nullptr;
return P;
}
Pattern *Traversal::visitEnumElementPattern(EnumElementPattern *P) {
if (auto *TR = P->getParentTypeRepr())
if (doIt(TR))
return nullptr;
if (!P->hasSubPattern())
return P;
if (Pattern *newSub = doIt(P->getSubPattern())) {
P->setSubPattern(newSub);
return P;
}
return nullptr;
}
Pattern *Traversal::visitExprPattern(ExprPattern *P) {
// If the pattern has been type-checked, walk the match expression, which
// includes the explicit subexpression.
if (auto *match = P->getCachedMatchExpr()) {
if (Expr *newMatch = doIt(match)) {
P->setMatchExpr(newMatch);
return P;
}
return nullptr;
}
if (Expr *newSub = doIt(P->getSubExpr())) {
P->setSubExpr(newSub);
return P;
}
return nullptr;
}
Pattern *Traversal::visitBindingPattern(BindingPattern *P) {
if (Pattern *newSub = doIt(P->getSubPattern())) {
P->setSubPattern(newSub);
return P;
}
return nullptr;
}
Pattern *Traversal::visitOptionalSomePattern(OptionalSomePattern *P) {
if (Pattern *newSub = doIt(P->getSubPattern())) {
P->setSubPattern(newSub);
return P;
}
return nullptr;
}
Pattern *Traversal::visitBoolPattern(BoolPattern *P) {
return P;
}
#pragma mark Type representation traversal
bool Traversal::visitErrorTypeRepr(ErrorTypeRepr *T) {
return false;
}
bool Traversal::visitAttributedTypeRepr(AttributedTypeRepr *T) {
return doIt(T->getTypeRepr());
}
bool Traversal::visitDeclRefTypeRepr(DeclRefTypeRepr *T) {
if (auto *qualIdentTR = dyn_cast<QualifiedIdentTypeRepr>(T)) {
if (doIt(qualIdentTR->getBase())) {
return true;
}
}
for (auto *genericArg : T->getGenericArgs()) {
if (doIt(genericArg)) {
return true;
}
}
return false;
}
bool Traversal::visitUnqualifiedIdentTypeRepr(UnqualifiedIdentTypeRepr *T) {
return visitDeclRefTypeRepr(T);
}
bool Traversal::visitQualifiedIdentTypeRepr(QualifiedIdentTypeRepr *T) {
return visitDeclRefTypeRepr(T);
}
bool Traversal::visitFunctionTypeRepr(FunctionTypeRepr *T) {
return doIt(T->getArgsTypeRepr()) || doIt(T->getResultTypeRepr());
}
bool Traversal::visitArrayTypeRepr(ArrayTypeRepr *T) {
return doIt(T->getBase());
}
bool Traversal::visitDictionaryTypeRepr(DictionaryTypeRepr *T) {
return doIt(T->getKey()) || doIt(T->getValue());
}
bool Traversal::visitOptionalTypeRepr(OptionalTypeRepr *T) {
return doIt(T->getBase());
}
bool Traversal::visitImplicitlyUnwrappedOptionalTypeRepr(ImplicitlyUnwrappedOptionalTypeRepr *T) {
return doIt(T->getBase());
}
bool Traversal::visitVarargTypeRepr(VarargTypeRepr *T) {
return doIt(T->getElementType());
}
bool Traversal::visitPackTypeRepr(PackTypeRepr *T) {
for (auto &elem : T->getMutableElements())
if (doIt(elem))
return true;
return false;
}
bool Traversal::visitPackExpansionTypeRepr(PackExpansionTypeRepr *T) {
return doIt(T->getPatternType());
}
bool Traversal::visitPackElementTypeRepr(PackElementTypeRepr *T) {
return doIt(T->getPackType());
}
bool Traversal::visitTupleTypeRepr(TupleTypeRepr *T) {
for (auto &elem : T->getElements()) {
if (doIt(elem.Type))
return true;
}
return false;
}
bool Traversal::visitCompositionTypeRepr(CompositionTypeRepr *T) {
for (auto elem : T->getTypes()) {
if (doIt(elem))
return true;
}
return false;
}
bool Traversal::visitMetatypeTypeRepr(MetatypeTypeRepr *T) {
return doIt(T->getBase());
}
bool Traversal::visitProtocolTypeRepr(ProtocolTypeRepr *T) {
return doIt(T->getBase());
}
bool Traversal::visitOwnershipTypeRepr(OwnershipTypeRepr *T) {
return doIt(T->getBase());
}
bool Traversal::visitIsolatedTypeRepr(IsolatedTypeRepr *T) {
return doIt(T->getBase());
}
bool Traversal::visitSendingTypeRepr(SendingTypeRepr *T) {
return doIt(T->getBase());
}
bool Traversal::visitCompileTimeConstTypeRepr(CompileTimeConstTypeRepr *T) {
return doIt(T->getBase());
}
bool Traversal::visitResultDependsOnTypeRepr(ResultDependsOnTypeRepr *T) {
return doIt(T->getBase());
}
bool Traversal::visitOpaqueReturnTypeRepr(OpaqueReturnTypeRepr *T) {
return doIt(T->getConstraint());
}
bool Traversal::visitNamedOpaqueReturnTypeRepr(NamedOpaqueReturnTypeRepr *T) {
return doIt(T->getBase());
}
bool Traversal::visitExistentialTypeRepr(ExistentialTypeRepr *T) {
return doIt(T->getConstraint());
}
bool Traversal::visitInverseTypeRepr(InverseTypeRepr *T) {
return doIt(T->getConstraint());
}
bool Traversal::visitPlaceholderTypeRepr(PlaceholderTypeRepr *T) {
return false;
}
bool Traversal::visitFixedTypeRepr(FixedTypeRepr *T) {
return false;
}
bool Traversal::visitSelfTypeRepr(SelfTypeRepr *T) {
return false;
}
bool Traversal::visitSILBoxTypeRepr(SILBoxTypeRepr *T) {
for (auto &field : T->getFields()) {
if (doIt(field.getFieldType()))
return true;
}
for (auto &arg : T->getGenericArguments()) {
if (doIt(arg))
return true;
}
return false;
}
bool Traversal::visitLifetimeDependentReturnTypeRepr(
LifetimeDependentReturnTypeRepr *T) {
return doIt(T->getBase());
}
Expr *Expr::walk(ASTWalker &walker) {
return Traversal(walker).doIt(this);
}
Stmt *Stmt::walk(ASTWalker &walker) {
return Traversal(walker).doIt(this);
}
Pattern *Pattern::walk(ASTWalker &walker) {
return Traversal(walker).doIt(this);
}
TypeRepr *TypeRepr::walk(ASTWalker &walker) {
(void)Traversal(walker).doIt(this);
return this;
}
StmtConditionElement *StmtConditionElement::walk(ASTWalker &walker) {
(void)Traversal(walker).doIt(*this);
return this;
}
bool Decl::walk(ASTWalker &walker) {
return Traversal(walker).doIt(this);
}
bool GenericParamList::walk(ASTWalker &walker) {
return Traversal(walker).doIt(this);
}
ArgumentList *ArgumentList::walk(ASTWalker &walker) {
return Traversal(walker).doIt(this);
}
|