Skip to content

Commit e88f0c2

Browse files
authored
Merge pull request #26282 from slavapestov/storage-impl-info-request
Use a request to compute AbstractStorageDecl::getImplInfo()
2 parents 10a4e3e + 60e1dfe commit e88f0c2

File tree

10 files changed

+263
-182
lines changed

10 files changed

+263
-182
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4385,6 +4385,7 @@ class AbstractStorageDecl : public ValueDecl {
43854385
friend class IsGetterMutatingRequest;
43864386
friend class IsSetterMutatingRequest;
43874387
friend class OpaqueReadOwnershipRequest;
4388+
friend class StorageImplInfoRequest;
43884389

43894390
public:
43904391
static const size_t MaxNumAccessors = 255;
@@ -4487,7 +4488,7 @@ class AbstractStorageDecl : public ValueDecl {
44874488
Type getValueInterfaceType() const;
44884489

44894490
/// Determine how this storage is implemented.
4490-
StorageImplInfo getImplInfo() const { return ImplInfo; }
4491+
StorageImplInfo getImplInfo() const;
44914492

44924493
/// Overwrite the registered implementation-info. This should be
44934494
/// used carefully.

include/swift/AST/TypeCheckRequests.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
namespace swift {
3131

32+
class AbstractStorageDecl;
3233
class GenericParamList;
3334
struct PropertyWrapperBackingPropertyInfo;
3435
class RequirementRepr;
@@ -836,6 +837,27 @@ class StoredPropertiesAndMissingMembersRequest :
836837
bool isCached() const { return true; }
837838
};
838839

840+
class StorageImplInfoRequest :
841+
public SimpleRequest<StorageImplInfoRequest,
842+
StorageImplInfo(AbstractStorageDecl *),
843+
CacheKind::SeparatelyCached> {
844+
public:
845+
using SimpleRequest::SimpleRequest;
846+
847+
private:
848+
friend SimpleRequest;
849+
850+
// Evaluation.
851+
llvm::Expected<StorageImplInfo>
852+
evaluate(Evaluator &evaluator, AbstractStorageDecl *decl) const;
853+
854+
public:
855+
// Separate caching.
856+
bool isCached() const { return true; }
857+
Optional<StorageImplInfo> getCachedResult() const;
858+
void cacheResult(StorageImplInfo value) const;
859+
};
860+
839861
// Allow AnyValue to compare two Type values, even though Type doesn't
840862
// support ==.
841863
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ SWIFT_TYPEID(LazyStoragePropertyRequest)
4646
SWIFT_TYPEID(TypeCheckFunctionBodyUntilRequest)
4747
SWIFT_TYPEID(StoredPropertiesRequest)
4848
SWIFT_TYPEID(StoredPropertiesAndMissingMembersRequest)
49+
SWIFT_TYPEID(StorageImplInfoRequest)

include/swift/Basic/Statistics.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ FRONTEND_STATISTIC(Sema, NumDeclsTypechecked)
179179
/// Number of declarations finalized.
180180
FRONTEND_STATISTIC(Sema, NumDeclsFinalized)
181181

182+
/// Number of synthesized accessors.
183+
FRONTEND_STATISTIC(Sema, NumAccessorsSynthesized)
184+
185+
/// Number of synthesized accessor bodies.
186+
FRONTEND_STATISTIC(Sema, NumAccessorBodiesSynthesized)
187+
182188
/// Number of full function bodies typechecked.
183189
FRONTEND_STATISTIC(Sema, NumFunctionsTypechecked)
184190

lib/AST/AccessRequests.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,37 @@ void AccessLevelRequest::cacheResult(AccessLevel value) const {
148148
// the cycle of computation associated with formal accesses, we give it its own
149149
// request.
150150

151+
// In a .swiftinterface file, a stored property with an explicit @_hasStorage
152+
// attribute but no setter is assumed to have originally been a private(set).
153+
static bool isStoredWithPrivateSetter(VarDecl *VD) {
154+
auto *HSA = VD->getAttrs().getAttribute<HasStorageAttr>();
155+
if (!HSA || HSA->isImplicit())
156+
return false;
157+
158+
auto *DC = VD->getDeclContext();
159+
auto *SF = DC->getParentSourceFile();
160+
if (!SF || SF->Kind != SourceFileKind::Interface)
161+
return false;
162+
163+
if (VD->isLet() ||
164+
(VD->getSetter() &&
165+
!VD->getSetter()->isImplicit()))
166+
return false;
167+
168+
return true;
169+
}
170+
151171
llvm::Expected<AccessLevel>
152172
SetterAccessLevelRequest::evaluate(Evaluator &evaluator,
153173
AbstractStorageDecl *ASD) const {
154174
assert(!ASD->Accessors.getInt().hasValue());
155-
if (auto *AA = ASD->getAttrs().getAttribute<SetterAccessAttr>())
156-
return AA->getAccess();
175+
if (auto *SAA = ASD->getAttrs().getAttribute<SetterAccessAttr>())
176+
return SAA->getAccess();
177+
178+
if (auto *VD = dyn_cast<VarDecl>(ASD))
179+
if (isStoredWithPrivateSetter(VD))
180+
return AccessLevel::Private;
181+
157182
return ASD->getFormalAccess();
158183
}
159184

lib/AST/Decl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4621,6 +4621,14 @@ void ProtocolDecl::computeKnownProtocolKind() const {
46214621
const_cast<ProtocolDecl *>(this)->Bits.ProtocolDecl.KnownProtocol = value;
46224622
}
46234623

4624+
4625+
StorageImplInfo AbstractStorageDecl::getImplInfo() const {
4626+
ASTContext &ctx = getASTContext();
4627+
return evaluateOrDefault(ctx.evaluator,
4628+
StorageImplInfoRequest{const_cast<AbstractStorageDecl *>(this)},
4629+
StorageImplInfo::getSimpleStored(StorageIsMutable));
4630+
}
4631+
46244632
bool AbstractStorageDecl::hasPrivateAccessor() const {
46254633
for (auto accessor : getAllAccessors()) {
46264634
if (hasPrivateOrFilePrivateFormalAccess(accessor))

lib/AST/TypeCheckRequests.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,3 +659,20 @@ void OpaqueReadOwnershipRequest::cacheResult(OpaqueReadOwnership value) const {
659659
auto *storage = std::get<0>(getStorage());
660660
storage->setOpaqueReadOwnership(value);
661661
}
662+
663+
//----------------------------------------------------------------------------//
664+
// StorageImplInfoRequest computation.
665+
//----------------------------------------------------------------------------//
666+
667+
Optional<StorageImplInfo>
668+
StorageImplInfoRequest::getCachedResult() const {
669+
auto *storage = std::get<0>(getStorage());
670+
if (storage->LazySemanticInfo.ImplInfoComputed)
671+
return storage->ImplInfo;
672+
return None;
673+
}
674+
675+
void StorageImplInfoRequest::cacheResult(StorageImplInfo value) const {
676+
auto *storage = std::get<0>(getStorage());
677+
storage->setImplInfo(value);
678+
}

0 commit comments

Comments
 (0)