Skip to content

Commit 302b7f5

Browse files
committed
[NFC] Define InheritedProtocolsRequest
Refactor the interface to ProtocolDecl::getInheritedProtocols in preparation for request-based dependency tracking.
1 parent 80cca7d commit 302b7f5

File tree

6 files changed

+75
-29
lines changed

6 files changed

+75
-29
lines changed

include/swift/AST/Decl.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4249,8 +4249,6 @@ class ProtocolDecl final : public NominalTypeDecl {
42494249
Bits.ProtocolDecl.ExistentialTypeSupported = supported;
42504250
}
42514251

4252-
ArrayRef<ProtocolDecl *> getInheritedProtocolsSlow();
4253-
42544252
bool hasLazyRequirementSignature() const {
42554253
return Bits.ProtocolDecl.HasLazyRequirementSignature;
42564254
}
@@ -4261,7 +4259,8 @@ class ProtocolDecl final : public NominalTypeDecl {
42614259
friend class ProtocolRequiresClassRequest;
42624260
friend class ExistentialConformsToSelfRequest;
42634261
friend class ExistentialTypeSupportedRequest;
4264-
4262+
friend class InheritedProtocolsRequest;
4263+
42654264
public:
42664265
ProtocolDecl(DeclContext *DC, SourceLoc ProtocolLoc, SourceLoc NameLoc,
42674266
Identifier Name, MutableArrayRef<TypeLoc> Inherited,
@@ -4270,12 +4269,7 @@ class ProtocolDecl final : public NominalTypeDecl {
42704269
using Decl::getASTContext;
42714270

42724271
/// Retrieve the set of protocols inherited from this protocol.
4273-
ArrayRef<ProtocolDecl *> getInheritedProtocols() const {
4274-
if (Bits.ProtocolDecl.InheritedProtocolsValid)
4275-
return InheritedProtocols;
4276-
4277-
return const_cast<ProtocolDecl *>(this)->getInheritedProtocolsSlow();
4278-
}
4272+
ArrayRef<ProtocolDecl *> getInheritedProtocols() const;
42794273

42804274
/// Determine whether this protocol has a superclass.
42814275
bool hasSuperclass() const { return (bool)getSuperclassDecl(); }
@@ -4370,6 +4364,13 @@ class ProtocolDecl final : public NominalTypeDecl {
43704364
private:
43714365
void computeKnownProtocolKind() const;
43724366

4367+
bool areInheritedProtocolsValid() const {
4368+
return Bits.ProtocolDecl.InheritedProtocolsValid;
4369+
}
4370+
void setInheritedProtocolsValid() {
4371+
Bits.ProtocolDecl.InheritedProtocolsValid = true;
4372+
}
4373+
43734374
public:
43744375
/// If this is known to be a compiler-known protocol, returns the kind.
43754376
/// Otherwise returns None.

include/swift/AST/NameLookupRequests.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,27 @@ class SuperclassDeclRequest :
163163
void cacheResult(ClassDecl *value) const;
164164
};
165165

166+
class InheritedProtocolsRequest
167+
: public SimpleRequest<InheritedProtocolsRequest,
168+
ArrayRef<ProtocolDecl *>(ProtocolDecl *),
169+
CacheKind::SeparatelyCached> {
170+
public:
171+
using SimpleRequest::SimpleRequest;
172+
173+
private:
174+
friend SimpleRequest;
175+
176+
// Evaluation.
177+
ArrayRef<ProtocolDecl *>
178+
evaluate(Evaluator &evaluator, ProtocolDecl *PD) const;
179+
180+
public:
181+
// Caching.
182+
bool isCached() const { return true; }
183+
Optional<ArrayRef<ProtocolDecl *>> getCachedResult() const;
184+
void cacheResult(ArrayRef<ProtocolDecl *> decls) const;
185+
};
186+
166187
/// Requests whether or not this class has designated initializers that are
167188
/// not public or @usableFromInline.
168189
class HasMissingDesignatedInitializersRequest :

include/swift/AST/NameLookupTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ SWIFT_REQUEST(NameLookup, InheritedDeclsReferencedRequest,
4747
DirectlyReferencedTypeDecls(
4848
llvm::PointerUnion<TypeDecl *, ExtensionDecl *>, unsigned),
4949
Uncached, HasNearestLocation)
50+
SWIFT_REQUEST(NameLookup, InheritedProtocolsRequest,
51+
ArrayRef<ProtocolDecl *>(ProtocolDecl *), SeparatelyCached,
52+
NoLocationInfo)
5053
SWIFT_REQUEST(NameLookup, LookupConformanceInModuleRequest,
5154
ProtocolConformanceRef(LookupConformanceDescriptor),
5255
Uncached, NoLocationInfo)

lib/AST/Decl.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4627,26 +4627,11 @@ ProtocolDecl::ProtocolDecl(DeclContext *DC, SourceLoc ProtocolLoc,
46274627
setTrailingWhereClause(TrailingWhere);
46284628
}
46294629

4630-
ArrayRef<ProtocolDecl *>
4631-
ProtocolDecl::getInheritedProtocolsSlow() {
4632-
Bits.ProtocolDecl.InheritedProtocolsValid = true;
4633-
4634-
llvm::SmallVector<ProtocolDecl *, 2> result;
4635-
SmallPtrSet<const ProtocolDecl *, 2> known;
4636-
known.insert(this);
4637-
bool anyObject = false;
4638-
for (const auto found :
4639-
getDirectlyInheritedNominalTypeDecls(
4640-
const_cast<ProtocolDecl *>(this), anyObject)) {
4641-
if (auto proto = dyn_cast<ProtocolDecl>(found.Item)) {
4642-
if (known.insert(proto).second)
4643-
result.push_back(proto);
4644-
}
4645-
}
4646-
4647-
auto &ctx = getASTContext();
4648-
InheritedProtocols = ctx.AllocateCopy(result);
4649-
return InheritedProtocols;
4630+
ArrayRef<ProtocolDecl *> ProtocolDecl::getInheritedProtocols() const {
4631+
auto *mutThis = const_cast<ProtocolDecl *>(this);
4632+
return evaluateOrDefault(getASTContext().evaluator,
4633+
InheritedProtocolsRequest{mutThis},
4634+
{});
46504635
}
46514636

46524637
llvm::TinyPtrVector<AssociatedTypeDecl *>

lib/AST/NameLookup.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,6 +2235,23 @@ SuperclassDeclRequest::evaluate(Evaluator &evaluator,
22352235
return nullptr;
22362236
}
22372237

2238+
ArrayRef<ProtocolDecl *>
2239+
InheritedProtocolsRequest::evaluate(Evaluator &evaluator,
2240+
ProtocolDecl *PD) const {
2241+
llvm::SmallVector<ProtocolDecl *, 2> result;
2242+
SmallPtrSet<const ProtocolDecl *, 2> known;
2243+
known.insert(PD);
2244+
bool anyObject = false;
2245+
for (const auto found : getDirectlyInheritedNominalTypeDecls(PD, anyObject)) {
2246+
if (auto proto = dyn_cast<ProtocolDecl>(found.Item)) {
2247+
if (known.insert(proto).second)
2248+
result.push_back(proto);
2249+
}
2250+
}
2251+
2252+
return PD->getASTContext().AllocateCopy(result);
2253+
}
2254+
22382255
llvm::Expected<NominalTypeDecl *>
22392256
ExtendedNominalRequest::evaluate(Evaluator &evaluator,
22402257
ExtensionDecl *ext) const {

lib/AST/NameLookupRequests.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ void SuperclassDeclRequest::cacheResult(ClassDecl *value) const {
6868
protocolDecl->LazySemanticInfo.SuperclassDecl.setPointerAndInt(value, true);
6969
}
7070

71+
//----------------------------------------------------------------------------//
72+
// InheritedProtocolsRequest computation.
73+
//----------------------------------------------------------------------------//
74+
75+
Optional<ArrayRef<ProtocolDecl *>>
76+
InheritedProtocolsRequest::getCachedResult() const {
77+
auto proto = std::get<0>(getStorage());
78+
if (!proto->areInheritedProtocolsValid())
79+
return None;
80+
81+
return proto->InheritedProtocols;
82+
}
83+
84+
void InheritedProtocolsRequest::cacheResult(ArrayRef<ProtocolDecl *> PDs) const {
85+
auto proto = std::get<0>(getStorage());
86+
proto->InheritedProtocols = PDs;
87+
proto->setInheritedProtocolsValid();
88+
}
89+
7190
//----------------------------------------------------------------------------//
7291
// Missing designated initializers computation
7392
//----------------------------------------------------------------------------//

0 commit comments

Comments
 (0)