Skip to content

Commit 443919a

Browse files
committed
AST: Introduce ProtocolDecl::getAllInheritedProtocols()
1 parent 1267f87 commit 443919a

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5172,6 +5172,10 @@ class ProtocolDecl final : public NominalTypeDecl {
51725172
/// Retrieve the set of protocols inherited from this protocol.
51735173
ArrayRef<ProtocolDecl *> getInheritedProtocols() const;
51745174

5175+
/// Retrieve the transitive closure of the inherited protocols, not including
5176+
/// this protocol itself.
5177+
ArrayRef<ProtocolDecl *> getAllInheritedProtocols() const;
5178+
51755179
/// Determine whether this protocol has a superclass.
51765180
bool hasSuperclass() const { return (bool)getSuperclassDecl(); }
51775181

include/swift/AST/NameLookupRequests.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,25 @@ class InheritedProtocolsRequest
199199
ArrayRef<ProtocolDecl *> result) const;
200200
};
201201

202+
class AllInheritedProtocolsRequest
203+
: public SimpleRequest<
204+
AllInheritedProtocolsRequest, ArrayRef<ProtocolDecl *>(ProtocolDecl *),
205+
RequestFlags::Cached> {
206+
public:
207+
using SimpleRequest::SimpleRequest;
208+
209+
private:
210+
friend SimpleRequest;
211+
212+
// Evaluation.
213+
ArrayRef<ProtocolDecl *>
214+
evaluate(Evaluator &evaluator, ProtocolDecl *PD) const;
215+
216+
public:
217+
// Caching
218+
bool isCached() const { return true; }
219+
};
220+
202221
class ProtocolRequirementsRequest
203222
: public SimpleRequest<ProtocolRequirementsRequest,
204223
ArrayRef<ValueDecl *>(ProtocolDecl *),

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
SWIFT_REQUEST(NameLookup, InheritedProtocolsRequest,
4848
ArrayRef<ProtocolDecl *>(ProtocolDecl *), SeparatelyCached,
4949
NoLocationInfo)
50+
SWIFT_REQUEST(NameLookup, AllInheritedProtocolsRequest,
51+
ArrayRef<ProtocolDecl *>(ProtocolDecl *), Cached,
52+
NoLocationInfo)
5053
SWIFT_REQUEST(NameLookup, ProtocolRequirementsRequest,
5154
ArrayRef<ValueDecl *>(ProtocolDecl *), SeparatelyCached,
5255
NoLocationInfo)

lib/AST/Decl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6470,6 +6470,13 @@ ArrayRef<ProtocolDecl *> ProtocolDecl::getInheritedProtocols() const {
64706470
{});
64716471
}
64726472

6473+
ArrayRef<ProtocolDecl *> ProtocolDecl::getAllInheritedProtocols() const {
6474+
auto *mutThis = const_cast<ProtocolDecl *>(this);
6475+
return evaluateOrDefault(getASTContext().evaluator,
6476+
AllInheritedProtocolsRequest{mutThis},
6477+
{});
6478+
}
6479+
64736480
ArrayRef<AssociatedTypeDecl *>
64746481
ProtocolDecl::getAssociatedTypeMembers() const {
64756482
if (Bits.ProtocolDecl.HasAssociatedTypes)

lib/AST/NameLookup.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3323,6 +3323,20 @@ InheritedProtocolsRequest::evaluate(Evaluator &evaluator,
33233323
return ctx.AllocateCopy(inherited.getArrayRef());
33243324
}
33253325

3326+
ArrayRef<ProtocolDecl *>
3327+
AllInheritedProtocolsRequest::evaluate(Evaluator &evaluator,
3328+
ProtocolDecl *PD) const {
3329+
llvm::SmallSetVector<ProtocolDecl *, 2> result;
3330+
3331+
PD->walkInheritedProtocols([&](ProtocolDecl *inherited) {
3332+
if (inherited != PD)
3333+
result.insert(inherited);
3334+
return TypeWalker::Action::Continue;
3335+
});
3336+
3337+
return PD->getASTContext().AllocateCopy(result.getArrayRef());
3338+
}
3339+
33263340
ArrayRef<ValueDecl *>
33273341
ProtocolRequirementsRequest::evaluate(Evaluator &evaluator,
33283342
ProtocolDecl *PD) const {

0 commit comments

Comments
 (0)