Skip to content

Commit 7d3dd49

Browse files
committed
[AST] Eliminate two simple uses of (NominalType|Extension)Decl::getInheritedType().
Simple checks for the presence of a protocol in the “inherited” list should only require a scan through that list + name lookup; use those facilities instead of recursing through the type checker.
1 parent 1610a89 commit 7d3dd49

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

lib/AST/DeclContext.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ ProtocolDecl *DeclContext::getAsProtocolExtensionContext() const {
9191
GenericTypeParamType *DeclContext::getProtocolSelfType() const {
9292
assert(getAsProtocolOrProtocolExtensionContext() && "not a protocol");
9393

94+
auto genericParams = getGenericParamsOfContext();
95+
96+
if (!genericParams) {
97+
if (auto proto = dyn_cast<ProtocolDecl>(this)) {
98+
getASTContext().getLazyResolver()
99+
->resolveDeclSignature(const_cast<ProtocolDecl *>(proto));
100+
genericParams = getGenericParamsOfContext();
101+
}
102+
}
103+
94104
return getGenericParamsOfContext()->getParams().front()
95105
->getDeclaredInterfaceType()
96106
->castTo<GenericTypeParamType>();

lib/ClangImporter/ImportDecl.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5168,21 +5168,15 @@ Decl *SwiftDeclConverter::importCompatibilityTypeAlias(
51685168
namespace {
51695169
template<typename D>
51705170
bool inheritanceListContainsProtocol(D decl, const ProtocolDecl *proto) {
5171-
return llvm::any_of(range(decl->getInherited().size()),
5172-
[decl, proto](unsigned index) -> bool {
5173-
Type type = decl->getInheritedType(index);
5174-
if (!type || !type->isExistentialType())
5175-
return false;
5176-
5177-
auto layout = type->getExistentialLayout();
5178-
for (auto protoTy : layout.getProtocols()) {
5179-
auto *protoDecl = protoTy->getDecl();
5171+
bool anyObject = false;
5172+
for (const auto &found :
5173+
getDirectlyInheritedNominalTypeDecls(decl, anyObject)) {
5174+
if (auto protoDecl = dyn_cast<ProtocolDecl>(found.second))
51805175
if (protoDecl == proto || protoDecl->inheritsFrom(proto))
51815176
return true;
5182-
}
5177+
}
51835178

5184-
return false;
5185-
});
5179+
return false;
51865180
}
51875181
}
51885182

lib/Sema/TypeCheckDecl.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -560,16 +560,11 @@ getInheritedForCycleCheck(TypeChecker &tc,
560560
ProtocolDecl **scratch) {
561561
TinyPtrVector<ProtocolDecl *> result;
562562

563-
for (unsigned index : indices(proto->getInherited())) {
564-
if (auto type = proto->getInheritedType(index)) {
565-
if (type->isExistentialType()) {
566-
auto layout = type->getExistentialLayout();
567-
for (auto protoTy : layout.getProtocols()) {
568-
auto *protoDecl = protoTy->getDecl();
569-
result.push_back(protoDecl);
570-
}
571-
}
572-
}
563+
bool anyObject = false;
564+
for (const auto &found :
565+
getDirectlyInheritedNominalTypeDecls(proto, anyObject)) {
566+
if (auto protoDecl = dyn_cast<ProtocolDecl>(found.second))
567+
result.push_back(protoDecl);
573568
}
574569

575570
return result;

0 commit comments

Comments
 (0)