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
|
//===--- DeclContext.cpp - DeclContext implementation ---------------------===//
//
// 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 "swift/AST/DeclContext.h"
#include "swift/AST/AccessScope.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/ClangModuleLoader.h"
#include "swift/AST/Expr.h"
#include "swift/AST/FileUnit.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/LazyResolver.h"
#include "swift/AST/Module.h"
#include "swift/AST/ParseRequests.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/Types.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/Statistic.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SaveAndRestore.h"
#include "clang/AST/ASTContext.h"
using namespace swift;
#define DEBUG_TYPE "Name lookup"
STATISTIC(NumLazyIterableDeclContexts,
"# of serialized iterable declaration contexts");
STATISTIC(NumUnloadedLazyIterableDeclContexts,
"# of serialized iterable declaration contexts never loaded");
ASTContext &DeclContext::getASTContext() const {
return getParentModule()->getASTContext();
}
GenericTypeDecl *DeclContext::getSelfTypeDecl() const {
auto decl = const_cast<Decl*>(getAsDecl());
if (!decl) return nullptr;
auto ext = dyn_cast<ExtensionDecl>(decl);
if (!ext) return dyn_cast<GenericTypeDecl>(decl);
return ext->getExtendedNominal();
}
/// If this DeclContext is a NominalType declaration or an
/// extension thereof, return the NominalTypeDecl.
NominalTypeDecl *DeclContext::getSelfNominalTypeDecl() const {
return dyn_cast_or_null<NominalTypeDecl>(getSelfTypeDecl());
}
ClassDecl *DeclContext::getSelfClassDecl() const {
return dyn_cast_or_null<ClassDecl>(getSelfTypeDecl());
}
EnumDecl *DeclContext::getSelfEnumDecl() const {
return dyn_cast_or_null<EnumDecl>(getSelfTypeDecl());
}
StructDecl *DeclContext::getSelfStructDecl() const {
return dyn_cast_or_null<StructDecl>(getSelfTypeDecl());
}
ProtocolDecl *DeclContext::getSelfProtocolDecl() const {
return dyn_cast_or_null<ProtocolDecl>(getSelfTypeDecl());
}
ProtocolDecl *DeclContext::getExtendedProtocolDecl() const {
if (auto decl = const_cast<Decl*>(getAsDecl()))
if (auto ED = dyn_cast<ExtensionDecl>(decl))
return dyn_cast_or_null<ProtocolDecl>(ED->getExtendedNominal());
return nullptr;
}
VarDecl *DeclContext::getNonLocalVarDecl() const {
if (auto *init = dyn_cast<PatternBindingInitializer>(this)) {
if (auto binding = init->getBinding()) {
if (auto *var = binding->getAnchoringVarDecl(init->getBindingIndex())) {
return var;
}
}
}
return nullptr;
}
Type DeclContext::getDeclaredTypeInContext() const {
if (auto declaredType = getDeclaredInterfaceType())
return mapTypeIntoContext(declaredType);
return Type();
}
Type DeclContext::getDeclaredInterfaceType() const {
if (auto *ED = dyn_cast<ExtensionDecl>(this)) {
auto *NTD = ED->getExtendedNominal();
if (NTD == nullptr)
return ErrorType::get(ED->getASTContext());
return NTD->getDeclaredInterfaceType();
}
if (auto *NTD = dyn_cast<NominalTypeDecl>(this))
return NTD->getDeclaredInterfaceType();
return Type();
}
void DeclContext::forEachGenericContext(
llvm::function_ref<void (GenericParamList *)> fn) const {
auto dc = this;
do {
if (auto decl = dc->getAsDecl()) {
// Extensions do not capture outer generic parameters.
if (auto *ext = dyn_cast<ExtensionDecl>(decl)) {
for (auto *gpList = ext->getGenericParams();
gpList != nullptr;
gpList = gpList->getOuterParameters()) {
fn(gpList);
}
return;
}
if (auto genericCtx = decl->getAsGenericContext())
if (auto *gpList = genericCtx->getGenericParams())
fn(gpList);
// Protocols do not capture outer generic parameters.
if (isa<ProtocolDecl>(decl))
return;
}
} while ((dc = dc->getParentForLookup()));
}
unsigned DeclContext::getGenericContextDepth() const {
unsigned depth = -1;
forEachGenericContext([&](GenericParamList *) { ++depth; });
return depth;
}
GenericSignature DeclContext::getGenericSignatureOfContext() const {
auto dc = this;
do {
if (auto decl = dc->getAsDecl())
if (auto GC = decl->getAsGenericContext())
return GC->getGenericSignature();
} while ((dc = dc->getParent()));
return nullptr;
}
GenericEnvironment *DeclContext::getGenericEnvironmentOfContext() const {
auto dc = this;
do {
if (auto decl = dc->getAsDecl())
if (auto GC = decl->getAsGenericContext())
return GC->getGenericEnvironment();
} while ((dc = dc->getParent()));
return nullptr;
}
Type DeclContext::mapTypeIntoContext(Type type) const {
return GenericEnvironment::mapTypeIntoContext(
getGenericEnvironmentOfContext(), type);
}
DeclContext *DeclContext::getLocalContext() {
if (isLocalContext())
return this;
if (isModuleContext())
return nullptr;
return getParent()->getLocalContext();
}
AbstractFunctionDecl *DeclContext::getInnermostMethodContext() {
auto dc = this;
do {
if (auto decl = dc->getAsDecl()) {
auto func = dyn_cast<AbstractFunctionDecl>(decl);
// If we found a non-func decl, we're done.
if (func == nullptr)
return nullptr;
if (func->getDeclContext()->isTypeContext())
return func;
}
} while ((dc = dc->getParent()));
return nullptr;
}
AccessorDecl *DeclContext::getInnermostPropertyAccessorContext() {
auto dc = this;
do {
if (auto decl = dc->getAsDecl()) {
auto accessor = dyn_cast<AccessorDecl>(decl);
// If we found a non-accessor decl, we're done.
if (accessor == nullptr)
return nullptr;
auto *storage = accessor->getStorage();
if (isa<VarDecl>(storage) && storage->getDeclContext()->isTypeContext())
return accessor;
}
} while ((dc = dc->getParent()));
return nullptr;
}
bool DeclContext::isTypeContext() const {
if (auto decl = getAsDecl())
return isa<NominalTypeDecl>(decl) || isa<ExtensionDecl>(decl);
return false;
}
DeclContext *DeclContext::getInnermostTypeContext() {
auto dc = this;
do {
if (dc->isTypeContext())
return dc;
} while ((dc = dc->getParent()));
return nullptr;
}
Decl *DeclContext::getInnermostDeclarationDeclContext() {
auto DC = this;
do {
if (auto decl = DC->getAsDecl())
return isa<ModuleDecl>(decl) ? nullptr : decl;
} while ((DC = DC->getParent()));
return nullptr;
}
Decl *DeclContext::getTopmostDeclarationDeclContext() {
auto *dc = this;
Decl *topmost = nullptr;
while (auto *decl = dc->getInnermostDeclarationDeclContext()) {
topmost = decl;
dc = decl->getDeclContext();
}
return topmost;
}
DeclContext *DeclContext::getInnermostSkippedFunctionContext() {
auto dc = this;
do {
if (auto afd = dyn_cast<AbstractFunctionDecl>(dc))
if (afd->isBodySkipped())
return afd;
} while ((dc = dc->getParent()));
return nullptr;
}
ClosureExpr *DeclContext::getInnermostClosureForSelfCapture() {
auto dc = this;
if (auto closure = dyn_cast<ClosureExpr>(dc)) {
return closure;
}
// Stop searching if we find a type decl, since types always
// redefine what 'self' means, even when nested inside a closure.
if (dc->isTypeContext()) {
return nullptr;
}
if (auto parent = dc->getParent()) {
return parent->getInnermostClosureForSelfCapture();
}
return nullptr;
}
DeclContext *DeclContext::getParentForLookup() const {
if (isa<ExtensionDecl>(this)) {
// If we are inside an extension, skip directly
// to the module scope context, without looking at any (invalid)
// outer types.
return getModuleScopeContext();
}
if (isa<ProtocolDecl>(this) && getParent()->isGenericContext()) {
// Protocols in generic contexts must not look in to their parents,
// as the parents may contain types with inferred implicit
// generic parameters not present in the protocol's generic signature.
return getModuleScopeContext();
}
if (isa<NominalTypeDecl>(this)) {
// If we are inside a nominal type that is inside a protocol,
// skip the protocol.
if (isa<ProtocolDecl>(getParent()))
return getModuleScopeContext();
}
return getParent();
}
PackageUnit *DeclContext::getPackageContext(bool lookupIfNotCurrent) const {
// Current decl context might be PackageUnit, which is not in the
// DeclContext hierarchy, so check for it first
if (isPackageContext())
return const_cast<PackageUnit *>(cast<PackageUnit>(this));
// If the current context is not PackageUnit, look it up via
// the parent module if needed
if (lookupIfNotCurrent) {
auto mdecl = getParentModule();
return mdecl->getPackage();
}
return nullptr;
}
ModuleDecl *DeclContext::getParentModule() const {
// If the current context is PackageUnit, return the module
// decl context pointing to the current context. This check
// needs to be done first as PackageUnit is not in the DeclContext
// hierarchy.
if (auto pkg = getPackageContext()) {
auto &mdecl = pkg->getSourceModule();
return &mdecl;
}
const DeclContext *DC = this;
while (!DC->isModuleContext())
DC = DC->getParent();
return const_cast<ModuleDecl *>(cast<ModuleDecl>(DC));
}
SourceFile *DeclContext::getParentSourceFile() const {
const DeclContext *DC = this;
SourceLoc loc;
while (DC && !DC->isModuleScopeContext()) {
// If we don't have a source location yet, try to grab one from this
// context.
if (loc.isInvalid()) {
switch (DC->getContextKind()) {
case DeclContextKind::AbstractClosureExpr:
loc = cast<AbstractClosureExpr>(DC)->getLoc();
break;
case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::EnumElementDecl:
case DeclContextKind::ExtensionDecl:
case DeclContextKind::GenericTypeDecl:
case DeclContextKind::MacroDecl:
case DeclContextKind::SubscriptDecl:
case DeclContextKind::TopLevelCodeDecl:
loc = DC->getAsDecl()->getLoc(/*SerializedOK=*/false);
break;
case DeclContextKind::Initializer:
case DeclContextKind::FileUnit:
case DeclContextKind::Module:
case DeclContextKind::Package:
case DeclContextKind::SerializedAbstractClosure:
case DeclContextKind::SerializedTopLevelCodeDecl:
break;
}
}
DC = DC->getParent();
}
if (!DC)
return nullptr;
auto fallbackSF = const_cast<SourceFile *>(dyn_cast<SourceFile>(DC));
if (auto module = DC->getParentModule()) {
if (auto sf = module->getSourceFileContainingLocation(loc))
return sf;
}
return fallbackSF;
}
SourceFile *DeclContext::getOutermostParentSourceFile() const {
auto sf = getParentSourceFile();
if (!sf)
return nullptr;
// Find the originating source file.
while (auto enclosingSF = sf->getEnclosingSourceFile())
sf = enclosingSF;
return sf;
}
DeclContext *DeclContext::getModuleScopeContext() const {
// If the current context is PackageUnit, return the module
// decl context pointing to the current context. This check
// needs to be done first as PackageUnit is not in the DeclContext
// hierarchy.
if (auto pkg = getPackageContext()) {
auto &mdecl = pkg->getSourceModule();
return &mdecl;
}
auto DC = const_cast<DeclContext *>(this);
while (true) {
if (DC->ParentAndKind.getInt() == ASTHierarchy::FileUnit)
return DC;
if (auto NextDC = DC->getParent()) {
DC = NextDC;
} else {
assert(DC && isa<ModuleDecl>(DC->getAsDecl()) &&
"no module decl context found!");
return DC;
}
}
}
void DeclContext::getSeparatelyImportedOverlays(
ModuleDecl *declaring, SmallVectorImpl<ModuleDecl *> &overlays) const {
if (auto SF = getOutermostParentSourceFile())
SF->getSeparatelyImportedOverlays(declaring, overlays);
}
/// Determine whether the given context is generic at any level.
bool DeclContext::isGenericContext() const {
auto dc = this;
do {
if (auto decl = dc->getAsDecl()) {
if (auto GC = decl->getAsGenericContext()) {
if (GC->getGenericParams())
return true;
}
}
} while ((dc = dc->getParentForLookup()));
return false;
}
ResilienceExpansion DeclContext::getResilienceExpansion() const {
auto fragileKind = getFragileFunctionKind();
switch (fragileKind.kind) {
case FragileFunctionKind::Transparent:
case FragileFunctionKind::Inlinable:
case FragileFunctionKind::AlwaysEmitIntoClient:
case FragileFunctionKind::DefaultArgument:
case FragileFunctionKind::PropertyInitializer:
case FragileFunctionKind::BackDeploy:
return ResilienceExpansion::Minimal;
case FragileFunctionKind::None:
return ResilienceExpansion::Maximal;
}
llvm_unreachable("Bad fragile function kind");
}
FragileFunctionKind DeclContext::getFragileFunctionKind() const {
auto &context = getASTContext();
return evaluateOrDefault(
context.evaluator,
FragileFunctionKindRequest{const_cast<DeclContext *>(this)},
{FragileFunctionKind::None});
}
FragileFunctionKind
swift::FragileFunctionKindRequest::evaluate(Evaluator &evaluator,
DeclContext *context) const {
for (const auto *dc = context->getLocalContext(); dc && dc->isLocalContext();
dc = dc->getParent()) {
// Default argument initializer contexts have their resilience expansion
// set when they're type checked.
if (isa<DefaultArgumentInitializer>(dc)) {
dc = dc->getParent();
auto *VD = cast<ValueDecl>(dc->getAsDecl());
assert(VD->hasParameterList());
if (VD->getDeclContext()->isLocalContext()) {
auto kind = VD->getDeclContext()->getFragileFunctionKind();
if (kind.kind != FragileFunctionKind::None)
return {FragileFunctionKind::DefaultArgument};
}
auto effectiveAccess =
VD->getFormalAccessScope(/*useDC=*/nullptr,
/*treatUsableFromInlineAsPublic=*/true);
if (effectiveAccess.isPublic()) {
return {FragileFunctionKind::DefaultArgument};
}
return {FragileFunctionKind::None};
}
// Stored property initializer contexts use minimal resilience expansion
// if the type is formally fixed layout.
if (auto *init = dyn_cast <PatternBindingInitializer>(dc)) {
auto bindingIndex = init->getBindingIndex();
if (auto *varDecl = init->getBinding()->getAnchoringVarDecl(bindingIndex)) {
if (varDecl->isInitExposedToClients()) {
return {FragileFunctionKind::PropertyInitializer};
}
}
return {FragileFunctionKind::None};
}
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(dc)) {
// If the function is a nested function, we will serialize its body if
// we serialize the parent's body.
if (AFD->getDeclContext()->isLocalContext())
continue;
auto funcAccess =
AFD->getFormalAccessScope(/*useDC=*/nullptr,
/*treatUsableFromInlineAsPublic=*/true);
// If the function is not externally visible, we will not be serializing
// its body.
if (!funcAccess.isPublic()) {
return {FragileFunctionKind::None};
}
// If the function is public, @_transparent implies @inlinable.
if (AFD->isTransparent()) {
return {FragileFunctionKind::Transparent};
}
if (AFD->getAttrs().hasAttribute<InlinableAttr>()) {
return {FragileFunctionKind::Inlinable};
}
if (AFD->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>()) {
return {FragileFunctionKind::AlwaysEmitIntoClient};
}
if (AFD->isBackDeployed(context->getASTContext())) {
return {FragileFunctionKind::BackDeploy};
}
// Property and subscript accessors inherit @_alwaysEmitIntoClient,
// @backDeployed, and @inlinable from their storage declarations.
if (auto accessor = dyn_cast<AccessorDecl>(AFD)) {
auto *storage = accessor->getStorage();
if (storage->getAttrs().getAttribute<InlinableAttr>()) {
return {FragileFunctionKind::Inlinable};
}
if (storage->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>()) {
return {FragileFunctionKind::AlwaysEmitIntoClient};
}
if (storage->isBackDeployed(context->getASTContext())) {
return {FragileFunctionKind::BackDeploy};
}
}
}
}
return {FragileFunctionKind::None};
}
/// Determine whether the innermost context is generic.
bool DeclContext::isInnermostContextGeneric() const {
if (auto Decl = getAsDecl())
if (auto GC = Decl->getAsGenericContext())
return GC->isGeneric();
return false;
}
unsigned DeclContext::getSyntacticDepth() const {
// Module scope == depth 0.
if (isModuleScopeContext())
return 0;
return 1 + getParent()->getSyntacticDepth();
}
unsigned DeclContext::getSemanticDepth() const {
// For extensions, count the depth of the nominal type being extended.
if (isa<ExtensionDecl>(this)) {
if (auto nominal = getSelfNominalTypeDecl())
return nominal->getSemanticDepth();
return 1;
}
// Module scope == depth 0.
if (isModuleScopeContext())
return 0;
return 1 + getParent()->getSemanticDepth();
}
bool DeclContext::mayContainMembersAccessedByDynamicLookup() const {
// Members of non-generic classes and class extensions can be found by
/// dynamic lookup.
if (auto *CD = getSelfClassDecl())
return !CD->isGenericContext();
// Members of @objc protocols (but not protocol extensions) can be
// found by dynamic lookup.
if (auto *PD = dyn_cast<ProtocolDecl>(this))
return PD->getAttrs().hasAttribute<ObjCAttr>();
return false;
}
bool DeclContext::canBeParentOfExtension() const {
return isa<SourceFile>(this);
}
bool DeclContext::walkContext(ASTWalker &Walker) {
switch (getContextKind()) {
case DeclContextKind::Package:
return false;
case DeclContextKind::Module:
return cast<ModuleDecl>(this)->walk(Walker);
case DeclContextKind::FileUnit:
return cast<FileUnit>(this)->walk(Walker);
case DeclContextKind::AbstractClosureExpr:
return cast<AbstractClosureExpr>(this)->walk(Walker);
case DeclContextKind::GenericTypeDecl:
return cast<GenericTypeDecl>(this)->walk(Walker);
case DeclContextKind::ExtensionDecl:
return cast<ExtensionDecl>(this)->walk(Walker);
case DeclContextKind::TopLevelCodeDecl:
return cast<TopLevelCodeDecl>(this)->walk(Walker);
case DeclContextKind::AbstractFunctionDecl:
return cast<AbstractFunctionDecl>(this)->walk(Walker);
case DeclContextKind::SubscriptDecl:
return cast<SubscriptDecl>(this)->walk(Walker);
case DeclContextKind::EnumElementDecl:
return cast<EnumElementDecl>(this)->walk(Walker);
case DeclContextKind::MacroDecl:
return cast<MacroDecl>(this)->walk(Walker);
case DeclContextKind::SerializedAbstractClosure:
case DeclContextKind::SerializedTopLevelCodeDecl:
llvm_unreachable("walk is unimplemented for deserialized contexts");
case DeclContextKind::Initializer:
// Is there any point in trying to walk the expression?
return false;
}
llvm_unreachable("bad DeclContextKind");
}
void DeclContext::dumpContext() const {
printContext(llvm::errs());
}
void AccessScope::dump() const {
llvm::errs() << getAccessLevelSpelling(accessLevelForDiagnostics()) << ": ";
if (isPublic() || isPackage()) {
llvm::errs() << "(null)\n";
return;
}
if (auto *file = dyn_cast<SourceFile>(getDeclContext())) {
llvm::errs() << "file '" << file->getFilename() << "'\n";
return;
}
if (auto *decl = getDeclContext()->getAsDecl()) {
llvm::errs() << Decl::getKindName(decl->getKind()) << " ";
if (auto *ext = dyn_cast<ExtensionDecl>(decl)) {
auto *extended = ext->getExtendedNominal();
if (extended)
llvm::errs() << extended->getName();
else
llvm::errs() << "(null)";
} else if (auto *named = dyn_cast<ValueDecl>(decl)) {
llvm::errs() << named->getName();
} else {
llvm::errs() << (const void *)decl;
}
SourceLoc loc = decl->getLoc();
if (loc.isValid()) {
llvm::errs() << " at ";
loc.print(llvm::errs(), decl->getASTContext().SourceMgr);
}
llvm::errs() << "\n";
return;
}
// If all else fails, dump the DeclContext tree.
getDeclContext()->printContext(llvm::errs());
}
template <typename DCType>
static unsigned getLineNumber(DCType *DC) {
SourceLoc loc = DC->getLoc();
if (loc.isInvalid())
return 0;
const ASTContext &ctx = static_cast<const DeclContext *>(DC)->getASTContext();
return ctx.SourceMgr.getPresumedLineAndColumnForLoc(loc).first;
}
unsigned DeclContext::printContext(raw_ostream &OS, const unsigned indent,
const bool onlyAPartialLine) const {
unsigned Depth = 0;
if (!onlyAPartialLine)
if (auto *P = getParent())
Depth = P->printContext(OS, indent);
const char *Kind;
switch (getContextKind()) {
case DeclContextKind::Package: Kind = "Package"; break;
case DeclContextKind::Module: Kind = "Module"; break;
case DeclContextKind::FileUnit: Kind = "FileUnit"; break;
case DeclContextKind::AbstractClosureExpr:
Kind = "AbstractClosureExpr";
break;
case DeclContextKind::SerializedAbstractClosure:
Kind = "SerializedAbstractClosure";
break;
case DeclContextKind::GenericTypeDecl:
switch (cast<GenericTypeDecl>(this)->getKind()) {
#define DECL(ID, PARENT) \
case DeclKind::ID: Kind = #ID "Decl"; break;
#include "swift/AST/DeclNodes.def"
}
break;
case DeclContextKind::ExtensionDecl: Kind = "ExtensionDecl"; break;
case DeclContextKind::TopLevelCodeDecl: Kind = "TopLevelCodeDecl"; break;
case DeclContextKind::SerializedTopLevelCodeDecl:
Kind = "SerializedTopLevelCodeDecl";
break;
case DeclContextKind::Initializer: Kind = "Initializer"; break;
case DeclContextKind::AbstractFunctionDecl:
Kind = "AbstractFunctionDecl";
break;
case DeclContextKind::SubscriptDecl: Kind = "SubscriptDecl"; break;
case DeclContextKind::EnumElementDecl: Kind = "EnumElementDecl"; break;
case DeclContextKind::MacroDecl: Kind = "MacroDecl"; break;
}
OS.indent(Depth*2 + indent) << (void*)this << " " << Kind;
switch (getContextKind()) {
case DeclContextKind::Package:
OS << " name=" << cast<PackageUnit>(this)->getName();
break;
case DeclContextKind::Module:
OS << " name=" << cast<ModuleDecl>(this)->getName();
break;
case DeclContextKind::FileUnit:
switch (cast<FileUnit>(this)->getKind()) {
case FileUnitKind::Builtin:
OS << " Builtin";
break;
case FileUnitKind::Source:
OS << " file=\"" << cast<SourceFile>(this)->getFilename() << "\"";
break;
case FileUnitKind::Synthesized:
OS << " synthesized file";
break;
case FileUnitKind::SerializedAST:
case FileUnitKind::ClangModule:
case FileUnitKind::DWARFModule:
OS << " file=\"" << cast<LoadedFile>(this)->getFilename() << "\"";
break;
}
break;
case DeclContextKind::AbstractClosureExpr:
OS << " line=" << getLineNumber(cast<AbstractClosureExpr>(this));
OS << " : " << cast<AbstractClosureExpr>(this)->getType();
break;
case DeclContextKind::SerializedAbstractClosure: {
OS << " : " << cast<SerializedAbstractClosureExpr>(this)->getType();
break;
}
case DeclContextKind::GenericTypeDecl:
OS << " name=" << cast<GenericTypeDecl>(this)->getName();
break;
case DeclContextKind::ExtensionDecl:
OS << " line=" << getLineNumber(cast<ExtensionDecl>(this));
OS << " base=" << cast<ExtensionDecl>(this)->getExtendedType();
break;
case DeclContextKind::TopLevelCodeDecl:
OS << " line=" << getLineNumber(cast<TopLevelCodeDecl>(this));
break;
case DeclContextKind::SerializedTopLevelCodeDecl:
// Already printed the kind, nothing else to do.
break;
case DeclContextKind::AbstractFunctionDecl: {
auto *AFD = cast<AbstractFunctionDecl>(this);
OS << " name=" << AFD->getName();
if (AFD->hasInterfaceType())
OS << " : " << AFD->getInterfaceType();
else
OS << " : (no type set)";
break;
}
case DeclContextKind::SubscriptDecl: {
auto *SD = cast<SubscriptDecl>(this);
OS << " name=" << SD->getBaseName();
if (SD->hasInterfaceType())
OS << " : " << SD->getInterfaceType();
else
OS << " : (no type set)";
break;
}
case DeclContextKind::EnumElementDecl: {
auto *EED = cast<EnumElementDecl>(this);
OS << " name=" << EED->getBaseName();
if (EED->hasInterfaceType())
OS << " : " << EED->getInterfaceType();
else
OS << " : (no type set)";
break;
}
case DeclContextKind::MacroDecl: {
auto *MD = cast<MacroDecl>(this);
OS << " name=" << MD->getBaseName();
if (MD->hasInterfaceType())
OS << " : " << MD->getInterfaceType();
else
OS << " : (no type set)";
break;
}
case DeclContextKind::Initializer:
switch (cast<Initializer>(this)->getInitializerKind()) {
case InitializerKind::PatternBinding: {
auto init = cast<PatternBindingInitializer>(this);
OS << " PatternBinding 0x" << (void*) init->getBinding()
<< " #" << init->getBindingIndex();
break;
}
case InitializerKind::DefaultArgument: {
auto init = cast<DefaultArgumentInitializer>(this);
OS << " DefaultArgument index=" << init->getIndex();
break;
}
case InitializerKind::PropertyWrapper: {
auto init = cast<PropertyWrapperInitializer>(this);
OS << "PropertyWrapper 0x" << (void*)init->getWrappedVar() << ", kind=";
switch (init->getKind()) {
case PropertyWrapperInitializer::Kind::WrappedValue:
OS << "wrappedValue";
break;
case PropertyWrapperInitializer::Kind::ProjectedValue:
OS << "projectedValue";
break;
}
break;
}
}
break;
}
if (auto decl = getAsDecl())
if (decl->getClangNode().getLocation().isValid()) {
auto &clangSM = getASTContext().getClangModuleLoader()
->getClangASTContext().getSourceManager();
OS << " clang_loc=";
decl->getClangNode().getLocation().print(OS, clangSM);
}
if (!onlyAPartialLine)
OS << "\n";
return Depth + 1;
}
const Decl *
IterableDeclContext::getDecl() const {
switch (getIterableContextKind()) {
case IterableDeclContextKind::NominalTypeDecl:
return cast<NominalTypeDecl>(this);
break;
case IterableDeclContextKind::ExtensionDecl:
return cast<ExtensionDecl>(this);
break;
}
llvm_unreachable("Unhandled IterableDeclContextKind in switch.");
}
GenericContext *IterableDeclContext::getAsGenericContext() {
switch (getIterableContextKind()) {
case IterableDeclContextKind::NominalTypeDecl:
return cast<NominalTypeDecl>(this);
case IterableDeclContextKind::ExtensionDecl:
return cast<ExtensionDecl>(this);
}
llvm_unreachable("Unhandled IterableDeclContextKind in switch.");
}
ASTContext &IterableDeclContext::getASTContext() const {
return getDecl()->getASTContext();
}
DeclRange IterableDeclContext::getCurrentMembersWithoutLoading() const {
return DeclRange(FirstDeclAndLazyMembers.getPointer(), nullptr);
}
DeclRange IterableDeclContext::getMembers() const {
return getCurrentMembers();
}
DeclRange IterableDeclContext::getCurrentMembers() const {
loadAllMembers();
return getCurrentMembersWithoutLoading();
}
ArrayRef<Decl *> IterableDeclContext::getParsedMembers() const {
ASTContext &ctx = getASTContext();
auto mutableThis = const_cast<IterableDeclContext *>(this);
return evaluateOrDefault(
ctx.evaluator, ParseMembersRequest{mutableThis},
FingerprintAndMembers())
.members;
}
ArrayRef<Decl *> IterableDeclContext::getABIMembers() const {
ASTContext &ctx = getASTContext();
return evaluateOrDefault(
ctx.evaluator,
ABIMembersRequest{const_cast<IterableDeclContext *>(this)},
ArrayRef<Decl *>());
}
ArrayRef<Decl *> IterableDeclContext::getAllMembers() const {
ASTContext &ctx = getASTContext();
return evaluateOrDefault(
ctx.evaluator,
AllMembersRequest{const_cast<IterableDeclContext *>(this)},
ArrayRef<Decl *>());
}
void IterableDeclContext::addMemberPreservingSourceOrder(Decl *member) {
auto &SM = getASTContext().SourceMgr;
SourceLoc start = member->getStartLoc();
Decl *hint = nullptr;
for (auto *existingMember : getMembers()) {
if (existingMember->isImplicit())
continue;
// An EnumCaseDecl contains one or more EnumElementDecls,
// but the EnumElementDecls are also added as members of
// the parent enum. We ignore the EnumCaseDecl since its
// source range overlaps with that of the EnumElementDecls.
if (isa<EnumCaseDecl>(existingMember))
continue;
// The elements of the active clause of an IfConfigDecl
// are added to the parent type. We ignore the IfConfigDecl
// since its source range overlaps with the source ranges
// of the active elements.
if (isa<IfConfigDecl>(existingMember))
continue;
if (!SM.isBeforeInBuffer(existingMember->getEndLoc(), start))
break;
hint = existingMember;
}
addMember(member, hint, /*insertAtHead=*/hint == nullptr);
}
/// Add a member to this context.
void IterableDeclContext::addMember(Decl *member, Decl *hint, bool insertAtHead) {
// Add the member to the list of declarations without notification.
addMemberSilently(member, hint, insertAtHead);
// Notify our parent declaration that we have added the member, which can
// be used to update the lookup tables.
switch (getIterableContextKind()) {
case IterableDeclContextKind::NominalTypeDecl: {
auto nominal = cast<NominalTypeDecl>(this);
nominal->addedMember(member);
assert(member->getDeclContext() == nominal &&
"Added member to the wrong context");
break;
}
case IterableDeclContextKind::ExtensionDecl: {
auto ext = cast<ExtensionDecl>(this);
ext->addedMember(member);
assert(member->getDeclContext() == ext &&
"Added member to the wrong context");
break;
}
}
}
void IterableDeclContext::addMemberSilently(Decl *member, Decl *hint,
bool insertAtHead) const {
assert(!isa<AccessorDecl>(member) && "Accessors should not be added here");
assert(!member->NextDecl && "Already added to a container");
#ifndef NDEBUG
// Assert that new declarations are always added in source order.
auto checkSourceRange = [&](Decl *prev, Decl *next) {
// SKip these checks for imported and deserialized decls.
if (!member->getDeclContext()->getParentSourceFile())
return;
auto shouldSkip = [](Decl *d) {
// PatternBindingDecl source ranges overlap with VarDecls,
// EnumCaseDecl source ranges overlap with EnumElementDecls,
// and IfConfigDecl source ranges overlap with the elements
// of the active clause. Skip them all here to avoid
// spurious assertions.
if (isa<PatternBindingDecl>(d) ||
isa<EnumCaseDecl>(d) ||
isa<IfConfigDecl>(d))
return true;
// Ignore source location information of implicit declarations.
if (d->isImplicit())
return true;
// Imported decls won't have complete location info.
if (d->hasClangNode())
return true;
return false;
};
if (shouldSkip(prev) || shouldSkip(next))
return;
SourceLoc prevEnd = prev->getEndLoc();
SourceLoc nextStart = next->getStartLoc();
assert(prevEnd.isValid() &&
"Only implicit decls can have invalid source location");
assert(nextStart.isValid() &&
"Only implicit decls can have invalid source location");
if (getASTContext().SourceMgr.isAtOrBefore(prevEnd, nextStart))
return;
llvm::errs() << "Source ranges out of order in addMember():\n";
prev->dump(llvm::errs());
next->dump(llvm::errs());
abort();
};
#endif
// Empty list.
if (!FirstDeclAndLazyMembers.getPointer()) {
assert(hint == nullptr);
FirstDeclAndLazyMembers.setPointer(member);
LastDeclAndKind.setPointer(member);
// Insertion at the head.
} else if (insertAtHead) {
assert(hint == nullptr);
member->NextDecl = FirstDeclAndLazyMembers.getPointer();
FirstDeclAndLazyMembers.setPointer(member);
// Insertion at the tail.
} else if (hint == nullptr) {
auto *last = LastDeclAndKind.getPointer();
#ifndef NDEBUG
checkSourceRange(last, member);
#endif
last->NextDecl = member;
LastDeclAndKind.setPointer(member);
// Insertion after 'hint' (which may be the tail).
} else {
#ifndef NDEBUG
checkSourceRange(hint, member);
#endif
member->NextDecl = hint->NextDecl;
hint->NextDecl = member;
// Handle case where the 'hint' is the tail.
if (LastDeclAndKind.getPointer() == hint)
LastDeclAndKind.setPointer(member);
}
}
void IterableDeclContext::setMemberLoader(LazyMemberLoader *loader,
uint64_t contextData) {
assert(!hasLazyMembers() && "already have lazy members");
ASTContext &ctx = getASTContext();
auto contextInfo = ctx.getOrCreateLazyIterableContextData(this, loader);
FirstDeclAndLazyMembers.setInt(true);
contextInfo->memberData = contextData;
++NumLazyIterableDeclContexts;
++NumUnloadedLazyIterableDeclContexts;
// FIXME: (transitional) increment the redundant "always-on" counter.
if (auto s = ctx.Stats) {
++s->getFrontendCounters().NumLazyIterableDeclContexts;
++s->getFrontendCounters().NumUnloadedLazyIterableDeclContexts;
}
}
bool IterableDeclContext::hasUnparsedMembers() const {
if (AddedParsedMembers)
return false;
if (!getAsGenericContext()->getParentSourceFile()) {
// There will never be any parsed members to add, so set the flag to say
// we are done so we can short-circuit next time.
const_cast<IterableDeclContext *>(this)->AddedParsedMembers = 1;
return false;
}
return true;
}
void IterableDeclContext::setHasLazyMembers(bool hasLazyMembers) const {
FirstDeclAndLazyMembers.setInt(hasLazyMembers);
}
void IterableDeclContext::loadAllMembers() const {
ASTContext &ctx = getASTContext();
// For contexts within a source file, get the list of parsed members.
if (getAsGenericContext()->getParentSourceFile()) {
// Retrieve the parsed members. Even if we've already added the parsed
// members to this context, this call is important for recording the
// dependency edge.
auto mutableThis = const_cast<IterableDeclContext *>(this);
auto members = getParsedMembers();
// If we haven't already done so, add these members to this context.
if (!AddedParsedMembers) {
mutableThis->AddedParsedMembers = 1;
for (auto member : members) {
mutableThis->addMember(member);
}
}
}
if (!hasLazyMembers())
return;
// Don't try to load all members re-entrant-ly.
setHasLazyMembers(false);
const Decl *container = getDecl();
auto contextInfo = ctx.getOrCreateLazyIterableContextData(this,
/*lazyLoader=*/nullptr);
contextInfo->loader->loadAllMembers(const_cast<Decl *>(container),
contextInfo->memberData);
--NumUnloadedLazyIterableDeclContexts;
// FIXME: (transitional) decrement the redundant "always-on" counter.
if (auto s = ctx.Stats)
--s->getFrontendCounters().NumUnloadedLazyIterableDeclContexts;
}
bool IterableDeclContext::wasDeserialized() const {
const DeclContext *DC = getAsGenericContext();
if (auto F = dyn_cast<FileUnit>(DC->getModuleScopeContext())) {
return F->getKind() == FileUnitKind::SerializedAST;
}
return false;
}
bool IterableDeclContext::classof(const Decl *D) {
switch (D->getKind()) {
default: return false;
#define DECL(ID, PARENT) // See previous line
#define ITERABLE_DECL(ID, PARENT) \
case DeclKind::ID: return true;
#include "swift/AST/DeclNodes.def"
}
}
IterableDeclContext *
IterableDeclContext::castDeclToIterableDeclContext(const Decl *D) {
switch (D->getKind()) {
default: llvm_unreachable("Decl is not a IterableDeclContext.");
#define DECL(ID, PARENT) // See previous line
#define ITERABLE_DECL(ID, PARENT) \
case DeclKind::ID: \
return const_cast<IterableDeclContext *>( \
static_cast<const IterableDeclContext*>(cast<ID##Decl>(D)));
#include "swift/AST/DeclNodes.def"
}
}
std::optional<Fingerprint> IterableDeclContext::getBodyFingerprint() const {
auto fileUnit = dyn_cast<FileUnit>(getAsGenericContext()->getModuleScopeContext());
if (!fileUnit)
return std::nullopt;
if (isa<SourceFile>(fileUnit)) {
auto mutableThis = const_cast<IterableDeclContext *>(this);
return evaluateOrDefault(getASTContext().evaluator,
ParseMembersRequest{mutableThis},
FingerprintAndMembers())
.fingerprint;
}
if (getDecl()->isImplicit())
return std::nullopt;
return fileUnit->loadFingerprint(this);
}
/// Return the DeclContext to compare when checking private access in
/// Swift 4 mode. The context returned is the type declaration if the context
/// and the type declaration are in the same file, otherwise it is the types
/// last extension in the source file. If the context does not refer to a
/// declaration or extension, the supplied context is returned.
static const DeclContext *
getPrivateDeclContext(const DeclContext *DC, const SourceFile *useSF) {
auto NTD = DC->getSelfNominalTypeDecl();
if (!NTD)
return DC;
// use the type declaration as the private scope if it is in the same
// file as useSF. This occurs for both extensions and declarations.
if (NTD->getOutermostParentSourceFile() == useSF)
return NTD;
// Otherwise use the last extension declaration in the same file.
const DeclContext *lastExtension = nullptr;
for (ExtensionDecl *ED : NTD->getExtensions())
if (ED->getOutermostParentSourceFile() == useSF)
lastExtension = ED;
// If there's no last extension, return the supplied context.
return lastExtension ? lastExtension : DC;
}
AccessScope::AccessScope(const DeclContext *DC, bool isPrivate)
: Value(DC, isPrivate) {
if (isPrivate) {
DC = getPrivateDeclContext(DC, DC->getOutermostParentSourceFile());
Value.setPointer(DC);
}
if (!DC || isa<ModuleDecl>(DC) || isa<PackageUnit>(DC))
assert(!isPrivate && "public, package, or internal scope can't be private");
}
bool AccessScope::isFileScope() const {
auto DC = getDeclContext();
return DC && isa<FileUnit>(DC);
}
bool AccessScope::isInternal() const {
auto DC = getDeclContext();
return DC && isa<ModuleDecl>(DC);
}
bool AccessScope::isPackage() const {
auto DC = getDeclContext();
return DC && isa<PackageUnit>(DC);
}
AccessLevel AccessScope::accessLevelForDiagnostics() const {
if (isPublic())
return AccessLevel::Public;
if (isPackage())
return AccessLevel::Package;
if (isa<ModuleDecl>(getDeclContext()))
return AccessLevel::Internal;
if (getDeclContext()->isModuleScopeContext()) {
return isPrivate() ? AccessLevel::Private : AccessLevel::FilePrivate;
}
return AccessLevel::Private;
}
bool AccessScope::allowsPrivateAccess(const DeclContext *useDC, const DeclContext *sourceDC) {
// Check the lexical scope.
if (useDC->isChildContextOf(sourceDC))
return true;
// If the decl site has package acl, but the use site
// has internal or less acl, check if it belongs to
// the same package as the decl site's to allow access.
if (auto srcPkg = sourceDC->getPackageContext()) {
if (auto usePkg = useDC->getPackageContext(/*lookupIfNotCurrent*/ true)) {
return usePkg->isSamePackageAs(srcPkg);
}
}
// Do not allow access if the sourceDC is in a different file
auto useSF = useDC->getOutermostParentSourceFile();
if (useSF != sourceDC->getOutermostParentSourceFile())
return false;
// Do not allow access if the sourceDC does not represent a type.
auto sourceNTD = sourceDC->getSelfNominalTypeDecl();
if (!sourceNTD)
return false;
// Compare the private scopes and iterate over the parent types.
sourceDC = getPrivateDeclContext(sourceDC, useSF);
while (!useDC->isModuleContext()) {
useDC = getPrivateDeclContext(useDC, useSF);
if (useDC == sourceDC)
return true;
// Get the parent type. If the context represents a type, look at the types
// declaring context instead of the contexts parent. This will crawl up
// the type hierarchy in nested extensions correctly.
if (auto NTD = useDC->getSelfNominalTypeDecl())
useDC = NTD->getDeclContext();
else
useDC = useDC->getParent();
}
return false;
}
DeclContext *Decl::getDeclContextForModule() const {
if (auto module = dyn_cast<ModuleDecl>(this))
return const_cast<ModuleDecl *>(module);
return nullptr;
}
DeclContextKind DeclContext::getContextKind() const {
switch (ParentAndKind.getInt()) {
case ASTHierarchy::Expr:
return DeclContextKind::AbstractClosureExpr;
case ASTHierarchy::Initializer:
return DeclContextKind::Initializer;
case ASTHierarchy::SerializedAbstractClosure:
return DeclContextKind::SerializedAbstractClosure;
case ASTHierarchy::SerializedTopLevelCodeDecl:
return DeclContextKind::SerializedTopLevelCodeDecl;
case ASTHierarchy::FileUnit:
return DeclContextKind::FileUnit;
case ASTHierarchy::Package:
return DeclContextKind::Package;
case ASTHierarchy::Decl: {
auto decl = reinterpret_cast<const Decl*>(this + 1);
if (isa<AbstractFunctionDecl>(decl))
return DeclContextKind::AbstractFunctionDecl;
if (isa<GenericTypeDecl>(decl))
return DeclContextKind::GenericTypeDecl;
switch (decl->getKind()) {
case DeclKind::Module:
return DeclContextKind::Module;
case DeclKind::TopLevelCode:
return DeclContextKind::TopLevelCodeDecl;
case DeclKind::Subscript:
return DeclContextKind::SubscriptDecl;
case DeclKind::EnumElement:
return DeclContextKind::EnumElementDecl;
case DeclKind::Extension:
return DeclContextKind::ExtensionDecl;
case DeclKind::Macro:
return DeclContextKind::MacroDecl;
default:
llvm_unreachable("Unhandled Decl kind");
}
}
}
llvm_unreachable("Unhandled DeclContext ASTHierarchy");
}
bool DeclContext::hasValueSemantics() const {
if (getExtendedProtocolDecl())
return !isClassConstrainedProtocolExtension();
return !getDeclaredInterfaceType()->hasReferenceSemantics();
}
bool DeclContext::isClassConstrainedProtocolExtension() const {
if (getExtendedProtocolDecl()) {
auto ED = cast<ExtensionDecl>(this);
if (auto sig = ED->getGenericSignature()) {
return sig->requiresClass(ED->getSelfInterfaceType());
}
}
return false;
}
bool DeclContext::isAsyncContext() const {
switch (getContextKind()) {
case DeclContextKind::Initializer:
case DeclContextKind::EnumElementDecl:
case DeclContextKind::ExtensionDecl:
case DeclContextKind::SerializedAbstractClosure:
case DeclContextKind::SerializedTopLevelCodeDecl:
case DeclContextKind::Package:
case DeclContextKind::Module:
case DeclContextKind::GenericTypeDecl:
case DeclContextKind::MacroDecl:
return false;
case DeclContextKind::FileUnit:
if (const SourceFile *sf = dyn_cast<SourceFile>(this))
return sf->isAsyncTopLevelSourceFile();
return false;
case DeclContextKind::TopLevelCodeDecl:
return getParent()->isAsyncContext();
case DeclContextKind::AbstractClosureExpr:
return cast<AbstractClosureExpr>(this)->isBodyAsync();
case DeclContextKind::AbstractFunctionDecl: {
const AbstractFunctionDecl *function = cast<AbstractFunctionDecl>(this);
return function->hasAsync();
}
case DeclContextKind::SubscriptDecl: {
AccessorDecl *getter =
cast<SubscriptDecl>(this)->getAccessor(AccessorKind::Get);
return getter != nullptr && getter->hasAsync();
}
}
llvm_unreachable("Unhandled DeclContextKind switch");
}
SourceLoc swift::extractNearestSourceLoc(const DeclContext *dc) {
switch (dc->getContextKind()) {
case DeclContextKind::Package:
case DeclContextKind::Module:
return SourceLoc();
case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::EnumElementDecl:
case DeclContextKind::ExtensionDecl:
case DeclContextKind::GenericTypeDecl:
case DeclContextKind::SubscriptDecl:
case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::MacroDecl:
return extractNearestSourceLoc(dc->getAsDecl());
case DeclContextKind::AbstractClosureExpr: {
SourceLoc loc = cast<AbstractClosureExpr>(dc)->getLoc();
if (loc.isValid())
return loc;
return extractNearestSourceLoc(dc->getParent());
}
case DeclContextKind::FileUnit:
return SourceLoc();
case DeclContextKind::Initializer:
case DeclContextKind::SerializedAbstractClosure:
case DeclContextKind::SerializedTopLevelCodeDecl:
return extractNearestSourceLoc(dc->getParent());
}
llvm_unreachable("Unhandled DeclContextKindIn switch");
}
#define DECL(Id, Parent) \
static_assert(!std::is_base_of<DeclContext, Id##Decl>::value, \
"Non-context Decl node has context?");
#define CONTEXT_DECL(Id, Parent) \
static_assert(alignof(DeclContext) == alignof(Id##Decl), "Alignment error"); \
static_assert(std::is_base_of<DeclContext, Id##Decl>::value, \
"CONTEXT_DECL nodes must inherit from DeclContext");
#define CONTEXT_VALUE_DECL(Id, Parent) \
static_assert(alignof(DeclContext) == alignof(Id##Decl), "Alignment error"); \
static_assert(std::is_base_of<DeclContext, Id##Decl>::value, \
"CONTEXT_VALUE_DECL nodes must inherit from DeclContext");
#include "swift/AST/DeclNodes.def"
#define EXPR(Id, Parent) \
static_assert(!std::is_base_of<DeclContext, Id##Expr>::value, \
"Non-context Expr node has context?");
#define CONTEXT_EXPR(Id, Parent) \
static_assert(alignof(DeclContext) == alignof(Id##Expr), "Alignment error"); \
static_assert(std::is_base_of<DeclContext, Id##Expr>::value, \
"CONTEXT_EXPR nodes must inherit from DeclContext");
#include "swift/AST/ExprNodes.def"
#ifndef NDEBUG
// XXX -- static_cast is not static enough for use with static_assert().
// DO verify this by temporarily breaking a Decl or Expr.
// DO NOT assume that the compiler will emit this code blindly.
SWIFT_CONSTRUCTOR
static void verify_DeclContext_is_start_of_node() {
auto decl = reinterpret_cast<Decl*>(0x1000 + sizeof(DeclContext));
#define DECL(Id, Parent)
#define CONTEXT_DECL(Id, Parent) \
assert(reinterpret_cast<DeclContext*>(0x1000) == \
static_cast<Id##Decl*>(decl));
#define CONTEXT_VALUE_DECL(Id, Parent) \
assert(reinterpret_cast<DeclContext*>(0x1000) == \
static_cast<Id##Decl*>(decl));
#include "swift/AST/DeclNodes.def"
auto expr = reinterpret_cast<Expr*>(0x1000 + sizeof(DeclContext));
#define EXPR(Id, Parent)
#define CONTEXT_EXPR(Id, Parent) \
assert(reinterpret_cast<DeclContext*>(0x1000) == \
static_cast<Id##Expr*>(expr));
#include "swift/AST/ExprNodes.def"
}
#endif
void swift::simple_display(llvm::raw_ostream &out,
const IterableDeclContext *idc) {
simple_display(out, idc->getDecl());
}
SourceLoc swift::extractNearestSourceLoc(const IterableDeclContext *idc) {
return extractNearestSourceLoc(idc->getDecl());
}
static bool isSpecializeExtensionContext(const DeclContext *dc) {
if (dc->isModuleScopeContext())
return false;
if (auto *extCtx = dyn_cast<ExtensionDecl>(dc)) {
// and has specialized attr ...
return extCtx->getAttrs().hasAttribute<SpecializeExtensionAttr>();
}
auto *parentDecl = dc->getParent();
return isSpecializeExtensionContext(parentDecl);
}
bool DeclContext::isInSpecializeExtensionContext() const {
return isSpecializeExtensionContext(this);
}
bool DeclContext::isAlwaysAvailableConformanceContext() const {
auto *ext = dyn_cast<ExtensionDecl>(this);
if (ext == nullptr)
return true;
if (AvailableAttr::isUnavailable(ext))
return false;
auto &ctx = getASTContext();
AvailabilityContext conformanceAvailability{
AvailabilityInference::availableRange(ext, ctx)};
auto deploymentTarget =
AvailabilityContext::forDeploymentTarget(ctx);
return deploymentTarget.isContainedIn(conformanceAvailability);
}
|