Skip to content

Commit 020d698

Browse files
committed
AST: Simplify ProtocolConformance::getSubstitutions()
1 parent 6330776 commit 020d698

File tree

1 file changed

+9
-36
lines changed

1 file changed

+9
-36
lines changed

lib/AST/ProtocolConformance.cpp

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -347,46 +347,19 @@ GenericSignature ProtocolConformance::getGenericSignature() const {
347347
}
348348

349349
SubstitutionMap ProtocolConformance::getSubstitutions(ModuleDecl *M) const {
350-
// Walk down to the base NormalProtocolConformance.
351-
SubstitutionMap subMap;
352-
const ProtocolConformance *parent = this;
353-
while (!isa<RootProtocolConformance>(parent)) {
354-
switch (parent->getKind()) {
355-
case ProtocolConformanceKind::Normal:
356-
case ProtocolConformanceKind::Self:
357-
case ProtocolConformanceKind::Builtin:
358-
llvm_unreachable("should have exited the loop?!");
359-
case ProtocolConformanceKind::Inherited:
360-
parent =
361-
cast<InheritedProtocolConformance>(parent)->getInheritedConformance();
362-
break;
363-
case ProtocolConformanceKind::Specialized: {
364-
auto SC = cast<SpecializedProtocolConformance>(parent);
365-
parent = SC->getGenericConformance();
366-
assert(subMap.empty() && "multiple conformance specializations?!");
367-
subMap = SC->getSubstitutionMap();
368-
break;
369-
}
370-
}
371-
}
350+
const ProtocolConformance *conformance = this;
372351

373-
// Found something; we're done!
374-
if (!subMap.empty())
375-
return subMap;
352+
if (auto *inheritedC = dyn_cast<InheritedProtocolConformance>(conformance))
353+
conformance = inheritedC->getInheritedConformance();
376354

377-
// If the normal conformance is for a generic type, and we didn't hit a
378-
// specialized conformance, collect the substitutions from the generic type.
379-
// FIXME: The AST should do this for us.
380-
const NormalProtocolConformance *normalC =
381-
dyn_cast<NormalProtocolConformance>(parent);
382-
if (!normalC)
383-
return SubstitutionMap();
355+
if (auto *specializedC = dyn_cast<SpecializedProtocolConformance>(conformance))
356+
return specializedC->getSubstitutionMap();
384357

385-
if (!normalC->getType()->isSpecialized())
386-
return SubstitutionMap();
358+
auto *rootC = cast<RootProtocolConformance>(conformance);
359+
if (auto genericSig = rootC->getGenericSignature())
360+
return genericSig->getIdentitySubstitutionMap();
387361

388-
auto *DC = normalC->getDeclContext();
389-
return normalC->getType()->getContextSubstitutionMap(M, DC);
362+
return SubstitutionMap();
390363
}
391364

392365
bool RootProtocolConformance::isInvalid() const {

0 commit comments

Comments
 (0)