Skip to content

Commit 4d44441

Browse files
authored
Merge pull request #6606 from DougGregor/substitution-map-cleanup
2 parents 62e825a + 1658599 commit 4d44441

File tree

12 files changed

+46
-30
lines changed

12 files changed

+46
-30
lines changed

include/swift/AST/SubstitutionMap.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ namespace swift {
3636

3737
class SubstitutableType;
3838

39+
template<class Type> class CanTypeWrapper;
40+
typedef CanTypeWrapper<SubstitutableType> CanSubstitutableType;
41+
3942
class SubstitutionMap {
4043
using ParentType = std::pair<CanType, AssociatedTypeDecl *>;
4144

@@ -61,7 +64,7 @@ class SubstitutionMap {
6164
/// Retrieve the conformances for the given type.
6265
ArrayRef<ProtocolConformanceRef> getConformances(CanType type) const;
6366

64-
void addSubstitution(CanType type, Type replacement);
67+
void addSubstitution(CanSubstitutableType type, Type replacement);
6568

6669
void addConformances(CanType type, ArrayRef<ProtocolConformanceRef> conformances);
6770

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class SILCloner : protected SILVisitor<ImplClass> {
230230

231231
// Register a re-mapping for opened existentials.
232232
void registerOpenedExistentialRemapping(ArchetypeType *From, ArchetypeType *To) {
233-
OpenedExistentialSubs.addSubstitution(CanType(From), CanType(To));
233+
OpenedExistentialSubs.addSubstitution(CanArchetypeType(From), CanType(To));
234234
}
235235

236236
protected:

lib/AST/GenericEnvironment.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ getSubstitutionMap(ModuleDecl *mod,
357357

358358
// Record the replacement type and its conformances.
359359
if (auto *archetype = contextTy->getAs<ArchetypeType>()) {
360-
result.addSubstitution(CanType(archetype), sub.getReplacement());
360+
result.addSubstitution(CanArchetypeType(archetype), sub.getReplacement());
361361
result.addConformances(CanType(archetype), sub.getConformances());
362362
continue;
363363
}

lib/AST/GenericSignature.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,10 @@ GenericSignature::getSubstitutionMap(ArrayRef<Substitution> subs,
270270
subs = subs.slice(1);
271271

272272
auto canTy = depTy->getCanonicalType();
273-
if (isa<GenericTypeParamType>(canTy))
274-
result.addSubstitution(canTy, sub.getReplacement());
273+
if (isa<SubstitutableType>(canTy)) {
274+
result.addSubstitution(cast<SubstitutableType>(canTy),
275+
sub.getReplacement());
276+
}
275277
result.addConformances(canTy, sub.getConformances());
276278
}
277279

lib/AST/SubstitutionMap.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ SubstitutionMap::lookupConformance(CanType type,
9090
}
9191

9292
void SubstitutionMap::
93-
addSubstitution(CanType type, Type replacement) {
94-
auto result = subMap.insert(std::make_pair(type->castTo<SubstitutableType>(),
95-
replacement));
93+
addSubstitution(CanSubstitutableType type, Type replacement) {
94+
auto result = subMap.insert(std::make_pair(type, replacement));
9695
assert(result.second);
9796
(void) result;
9897
}
@@ -182,14 +181,12 @@ SubstitutionMap::getOverrideSubstitutions(const ClassDecl *baseClass,
182181
assert(baseParams.size() == derivedParams.size());
183182

184183
for (unsigned i = 0, e = derivedParams.size(); i < e; i++) {
185-
auto paramTy = baseParams[i]->getCanonicalType()
186-
->castTo<GenericTypeParamType>();
184+
auto paramTy = cast<GenericTypeParamType>(baseParams[i]->getCanonicalType());
187185
assert(paramTy->getDepth() >= minDepth);
188186
Type replacementTy = derivedParams[i];
189187
if (derivedSubs)
190188
replacementTy = replacementTy.subst(*derivedSubs);
191-
subMap.addSubstitution(paramTy->getCanonicalType(),
192-
replacementTy);
189+
subMap.addSubstitution(paramTy, replacementTy);
193190
}
194191

195192
auto isRootedInInnermostParameter = [&](Type t) -> bool {

lib/SILGen/SILGenDecl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,8 +1758,9 @@ SILGenModule::emitProtocolWitness(ProtocolConformance *conformance,
17581758
genericEnv = witnessRef.getDecl()->getInnermostDeclContext()
17591759
->getGenericEnvironmentOfContext();
17601760

1761-
auto selfTy = conformance->getProtocol()->getSelfInterfaceType()
1762-
->getCanonicalType();
1761+
auto selfTy = cast<GenericTypeParamType>(
1762+
conformance->getProtocol()->getSelfInterfaceType()
1763+
->getCanonicalType());
17631764

17641765
Type concreteTy = conformance->getInterfaceType();
17651766

lib/SILGen/SILGenPoly.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,8 +2484,12 @@ buildThunkSignature(SILGenFunction &gen,
24842484
auto newArchetype = genericEnv->mapTypeIntoContext(mod, depTy)
24852485
->castTo<ArchetypeType>();
24862486

2487-
contextSubs.addSubstitution(CanType(oldArchetype), newArchetype);
2488-
interfaceSubs.addSubstitution(canTy, oldArchetype);
2487+
contextSubs.addSubstitution(CanArchetypeType(oldArchetype), newArchetype);
2488+
2489+
if (isa<SubstitutableType>(canTy)) {
2490+
interfaceSubs.addSubstitution(cast<SubstitutableType>(canTy),
2491+
oldArchetype);
2492+
}
24892493

24902494
contextSubs.addConformances(CanType(oldArchetype), conformances);
24912495
interfaceSubs.addConformances(canTy, conformances);

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ SILCombiner::createApplyWithConcreteType(FullApplySite AI,
714714
NewSubstCalleeType = SILType::getPrimitiveObjectType(SFT);
715715
} else {
716716
SubstitutionMap Subs;
717-
Subs.addSubstitution(CanType(OpenedArchetype), ConcreteType);
717+
Subs.addSubstitution(CanArchetypeType(OpenedArchetype), ConcreteType);
718718
Subs.addConformances(CanType(OpenedArchetype), Conformance);
719719
NewSubstCalleeType = SubstCalleeType.subst(AI.getModule(), Subs);
720720
}

lib/SILOptimizer/Transforms/CSE.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,8 @@ bool CSE::processOpenExistentialRef(SILInstruction *Inst, ValueBase *V,
645645
auto OldOpenedArchetype = getOpenedArchetypeOf(Inst);
646646
auto NewOpenedArchetype = getOpenedArchetypeOf(dyn_cast<SILInstruction>(V));
647647
SubstitutionMap TypeSubstMap;
648-
TypeSubstMap.addSubstitution(CanType(OldOpenedArchetype), NewOpenedArchetype);
648+
TypeSubstMap.addSubstitution(CanArchetypeType(OldOpenedArchetype),
649+
NewOpenedArchetype);
649650
// Collect all candidates that may contain opened archetypes
650651
// that need to be replaced.
651652
for (auto Use : Inst->getUses()) {

lib/SILOptimizer/Utils/Devirtualize.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,10 @@ getSubstitutionsForCallee(SILModule &M,
463463

464464
// Otherwise, record the replacement and conformances for the mapped
465465
// type.
466-
if (isa<GenericTypeParamType>(canTy))
467-
subMap.addSubstitution(canTy, sub.getReplacement());
466+
if (isa<SubstitutableType>(canTy)) {
467+
subMap.addSubstitution(cast<SubstitutableType>(canTy),
468+
sub.getReplacement());
469+
}
468470
subMap.addConformances(canTy, sub.getConformances());
469471
}
470472
assert(origSubs.empty());
@@ -834,7 +836,8 @@ static void getWitnessMethodSubstitutions(
834836
unsigned depth = 0;
835837
if (isDefaultWitness) {
836838
// For default witnesses, we substitute all of Self.
837-
auto gp = witnessThunkSig->getGenericParams().front()->getCanonicalType();
839+
auto gp = cast<GenericTypeParamType>(witnessThunkSig->getGenericParams()
840+
.front()->getCanonicalType());
838841
subMap.addSubstitution(gp, origSubs.front().getReplacement());
839842
subMap.addConformances(gp, origSubs.front().getConformances());
840843

@@ -899,8 +902,10 @@ static void getWitnessMethodSubstitutions(
899902
// Otherwise, record the replacement and conformances for the mapped
900903
// type.
901904
auto canTy = mappedDepTy->getCanonicalType();
902-
if (isa<GenericTypeParamType>(canTy))
903-
subMap.addSubstitution(canTy, sub.getReplacement());
905+
if (isa<SubstitutableType>(canTy)) {
906+
subMap.addSubstitution(cast<SubstitutableType>(canTy),
907+
sub.getReplacement());
908+
}
904909
subMap.addConformances(canTy, sub.getConformances());
905910
}
906911
assert(subs.empty() && "Did not consume all substitutions");

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,8 @@ void AttributeChecker::visitSpecializeAttr(SpecializeAttr *attr) {
14381438
}
14391439

14401440
tl.setType(ty, /*validated=*/true);
1441-
subMap.addSubstitution(genericTypeParamTy->getCanonicalType(), ty);
1441+
subMap.addSubstitution(
1442+
cast<GenericTypeParamType>(genericTypeParamTy->getCanonicalType()), ty);
14421443
}
14431444

14441445
// Capture the conformances needed for the substitution map.

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -983,13 +983,14 @@ RequirementEnvironment::RequirementEnvironment(
983983
// type parameters of the conformance context and the parameters of the
984984
// requirement.
985985
Type concreteType = conformanceDC->getSelfInterfaceType();
986-
auto selfType = proto->getSelfInterfaceType()->getCanonicalType();
986+
auto selfType = cast<GenericTypeParamType>(
987+
proto->getSelfInterfaceType()->getCanonicalType());
988+
989+
987990
reqToSyntheticEnvMap.addSubstitution(selfType, concreteType);
988991

989992
// 'Self' is always at depth 0, index 0. Anything else is invalid code.
990-
auto selfGenericParam = selfType->castTo<GenericTypeParamType>();
991-
if (selfGenericParam->getDepth() != 0 ||
992-
selfGenericParam->getIndex() != 0) {
993+
if (selfType->getDepth() != 0 || selfType->getIndex() != 0) {
993994
return;
994995
}
995996

@@ -1058,8 +1059,9 @@ RequirementEnvironment::RequirementEnvironment(
10581059

10591060
// Create a substitution from the requirement's generic parameter to the
10601061
// generic parameter known to the builder.
1061-
reqToSyntheticEnvMap.addSubstitution(genericParam->getCanonicalType(),
1062-
substGenericParam);
1062+
reqToSyntheticEnvMap.addSubstitution(
1063+
cast<GenericTypeParamType>(genericParam->getCanonicalType()),
1064+
substGenericParam);
10631065
if (auto archetypeType
10641066
= reqEnv->mapTypeIntoContext(genericParam)->getAs<ArchetypeType>()) {
10651067
// Add substitutions.

0 commit comments

Comments
 (0)