Skip to content

Commit a204997

Browse files
committed
AST: Add ModuleDecl::isResilient()
This cleans up some code. I'm keeping the ResilienceStrategy enum around though, in case we want to use it to version the ABI in the future.
1 parent 1ad5b70 commit a204997

12 files changed

+28
-64
lines changed

include/swift/AST/Module.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ class ModuleDecl : public DeclContext, public TypeDecl {
324324
Bits.ModuleDecl.RawResilienceStrategy = unsigned(strategy);
325325
}
326326

327+
bool isResilient() const {
328+
return getResilienceStrategy() != ResilienceStrategy::Default;
329+
}
330+
327331
/// Look up a (possibly overloaded) value set at top-level scope
328332
/// (but with the specified access path, which may come from an import decl)
329333
/// within the current module.

lib/AST/Decl.cpp

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,14 +1927,7 @@ bool AbstractStorageDecl::isResilient() const {
19271927
if (!isFormallyResilient())
19281928
return false;
19291929

1930-
switch (getDeclContext()->getParentModule()->getResilienceStrategy()) {
1931-
case ResilienceStrategy::Resilient:
1932-
return true;
1933-
case ResilienceStrategy::Default:
1934-
return false;
1935-
}
1936-
1937-
llvm_unreachable("Unhandled ResilienceStrategy in switch.");
1930+
return getModuleContext()->isResilient();
19381931
}
19391932

19401933
bool AbstractStorageDecl::isResilient(ModuleDecl *M,
@@ -3077,20 +3070,10 @@ bool NominalTypeDecl::isFormallyResilient() const {
30773070
}
30783071

30793072
bool NominalTypeDecl::isResilient() const {
3080-
// If we're not formally resilient, don't check the module resilience
3081-
// strategy.
30823073
if (!isFormallyResilient())
30833074
return false;
30843075

3085-
// Otherwise, check the module.
3086-
switch (getParentModule()->getResilienceStrategy()) {
3087-
case ResilienceStrategy::Resilient:
3088-
return true;
3089-
case ResilienceStrategy::Default:
3090-
return false;
3091-
}
3092-
3093-
llvm_unreachable("Unhandled ResilienceStrategy in switch.");
3076+
return getModuleContext()->isResilient();
30943077
}
30953078

30963079
bool NominalTypeDecl::isResilient(ModuleDecl *M,
@@ -3549,8 +3532,7 @@ bool ClassDecl::hasResilientMetadata() const {
35493532
return false;
35503533

35513534
// If the module is not resilient, neither is the class metadata.
3552-
if (getParentModule()->getResilienceStrategy()
3553-
!= ResilienceStrategy::Resilient)
3535+
if (!getModuleContext()->isResilient())
35543536
return false;
35553537

35563538
// If the class is not public, we can't use it outside the module at all.
@@ -3891,12 +3873,8 @@ bool EnumDecl::isFormallyExhaustive(const DeclContext *useDC) const {
38913873

38923874
// Non-imported enums in non-resilient modules are exhaustive.
38933875
const ModuleDecl *containingModule = getModuleContext();
3894-
switch (containingModule->getResilienceStrategy()) {
3895-
case ResilienceStrategy::Default:
3876+
if (!containingModule->isResilient())
38963877
return true;
3897-
case ResilienceStrategy::Resilient:
3898-
break;
3899-
}
39003878

39013879
// Non-public, non-versioned enums are always exhaustive.
39023880
AccessScope accessScope = getFormalAccessScope(/*useDC*/nullptr,

lib/AST/ProtocolConformance.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,7 @@ bool NormalProtocolConformance::isResilient() const {
458458
if (!getType()->getAnyNominal()->isResilient())
459459
return false;
460460

461-
switch (getDeclContext()->getParentModule()->getResilienceStrategy()) {
462-
case ResilienceStrategy::Resilient:
463-
return true;
464-
case ResilienceStrategy::Default:
465-
return false;
466-
}
461+
return getDeclContext()->getParentModule()->isResilient();
467462
}
468463

469464
Optional<ArrayRef<Requirement>>

lib/SIL/SILDeclRef.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,7 @@ SILLinkage SILDeclRef::getLinkage(ForDefinition_t forDefinition) const {
330330
d = cast<NominalTypeDecl>(d->getDeclContext());
331331

332332
// FIXME: This should always be true.
333-
if (d->getDeclContext()->getParentModule()->getResilienceStrategy() ==
334-
ResilienceStrategy::Resilient)
333+
if (d->getModuleContext()->isResilient())
335334
limit = Limit::NeverPublic;
336335
}
337336

lib/SILGen/SILGen.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,8 +1368,7 @@ static bool canStorageUseTrivialDescriptor(SILGenModule &SGM,
13681368
// stored) and doesn't have a setter with less-than-public visibility.
13691369
auto expansion = ResilienceExpansion::Maximal;
13701370

1371-
switch (SGM.M.getSwiftModule()->getResilienceStrategy()) {
1372-
case ResilienceStrategy::Default: {
1371+
if (!SGM.M.getSwiftModule()->isResilient()) {
13731372
if (SGM.canStorageUseStoredKeyPathComponent(decl, expansion)) {
13741373
// External modules can't directly access storage, unless this is a
13751374
// property in a fixed-layout type.
@@ -1387,18 +1386,15 @@ static bool canStorageUseTrivialDescriptor(SILGenModule &SGM,
13871386

13881387
return false;
13891388
}
1390-
case ResilienceStrategy::Resilient: {
1391-
// A resilient module needs to handle binaries compiled against its older
1392-
// versions. This means we have to be a bit more conservative, since in
1393-
// earlier versions, a settable property may have withheld the setter,
1394-
// or a fixed-layout type may not have been.
1395-
// Without availability information, only get-only computed properties
1396-
// can resiliently use trivial descriptors.
1397-
return !SGM.canStorageUseStoredKeyPathComponent(decl, expansion)
1398-
&& decl->getSetter() == nullptr;
1399-
}
1400-
}
1401-
llvm_unreachable("unhandled strategy");
1389+
1390+
// A resilient module needs to handle binaries compiled against its older
1391+
// versions. This means we have to be a bit more conservative, since in
1392+
// earlier versions, a settable property may have withheld the setter,
1393+
// or a fixed-layout type may not have been.
1394+
// Without availability information, only get-only computed properties
1395+
// can resiliently use trivial descriptors.
1396+
return !SGM.canStorageUseStoredKeyPathComponent(decl, expansion)
1397+
&& decl->getSetter() == nullptr;
14021398
}
14031399

14041400
void SILGenModule::tryEmitPropertyDescriptor(AbstractStorageDecl *decl) {

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,7 @@ deriveEquatable_eq(DerivedConformance &derived,
631631
auto boolTy = C.getBoolDecl()->getDeclaredType();
632632

633633
Identifier generatedIdentifier;
634-
if (parentDC->getParentModule()->getResilienceStrategy() ==
635-
ResilienceStrategy::Resilient) {
634+
if (parentDC->getParentModule()->isResilient()) {
636635
generatedIdentifier = C.Id_EqualsOperator;
637636
} else if (selfIfaceTy->getEnumOrBoundGenericEnum()) {
638637
generatedIdentifier = C.Id_derived_enum_equals;

lib/Sema/DerivedConformanceRawRepresentable.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ static void maybeMarkAsInlinable(DerivedConformance &derived,
147147
AbstractFunctionDecl *afd) {
148148
ASTContext &C = derived.TC.Context;
149149
auto parentDC = derived.getConformanceContext();
150-
if (parentDC->getParentModule()->getResilienceStrategy() !=
151-
ResilienceStrategy::Resilient) {
150+
if (!parentDC->getParentModule()->isResilient()) {
152151
AccessScope access =
153152
afd->getFormalAccessScope(nullptr,
154153
/*treatUsableFromInlineAsPublic*/true);

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ bool TypeChecker::diagnoseInlinableDeclRef(SourceLoc loc,
119119
// Do enforce the restriction even in pre-Swift-5 modes if the module we're
120120
// building is resilient, though.
121121
if (D->isObjCDynamic() && !Context.isSwiftVersionAtLeast(5) &&
122-
DC->getParentModule()->getResilienceStrategy() !=
123-
ResilienceStrategy::Resilient) {
122+
!DC->getParentModule()->isResilient()) {
124123
return false;
125124
}
126125

lib/Sema/TypeCheckAccess.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,11 @@ void AccessControlCheckerBase::checkTypeAccessImpl(
205205

206206
} else {
207207
// The type violates the rules of access control (with or without taking the
208-
// TypeRepr into account).
208+
// TypeRepr into account).
209209

210210
if (typeRepr && mayBeInferred &&
211211
!TC.getLangOpts().isSwiftVersionAtLeast(5) &&
212-
useDC->getParentModule()->getResilienceStrategy() !=
213-
ResilienceStrategy::Resilient) {
212+
!useDC->getParentModule()->isResilient()) {
214213
// Swift 4.2 and earlier didn't check the Type when a TypeRepr was
215214
// present. However, this is a major hole when generic parameters are
216215
// inferred:

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,12 +2380,9 @@ void AttributeChecker::visitImplementsAttr(ImplementsAttr *attr) {
23802380
void AttributeChecker::visitFrozenAttr(FrozenAttr *attr) {
23812381
auto *ED = cast<EnumDecl>(D);
23822382

2383-
switch (ED->getModuleContext()->getResilienceStrategy()) {
2384-
case ResilienceStrategy::Default:
2383+
if (!ED->getModuleContext()->isResilient()) {
23852384
diagnoseAndRemoveAttr(attr, diag::enum_frozen_nonresilient, attr);
23862385
return;
2387-
case ResilienceStrategy::Resilient:
2388-
break;
23892386
}
23902387

23912388
if (ED->getFormalAccess() < AccessLevel::Public &&

0 commit comments

Comments
 (0)