Skip to content

Commit e79763b

Browse files
committed
AST: Factor out mapLocalArchetypesOutOfContext() utility function
1 parent 52ce02e commit e79763b

File tree

7 files changed

+33
-47
lines changed

7 files changed

+33
-47
lines changed

include/swift/AST/LocalArchetypeRequirementCollector.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ struct MapLocalArchetypesOutOfContext {
5454
Type operator()(SubstitutableType *type) const;
5555
};
5656

57+
Type mapLocalArchetypesOutOfContext(Type type,
58+
GenericSignature baseGenericSig,
59+
ArrayRef<GenericEnvironment *> capturedEnvs);
60+
5761
struct MapIntoLocalArchetypeContext {
5862
GenericEnvironment *baseGenericEnv;
5963
ArrayRef<GenericEnvironment *> capturedEnvs;

include/swift/SIL/SILCloner.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ struct SubstitutionMapWithLocalArchetypes {
6060

6161
// Map the local archetype to an interface type in the new generic
6262
// signature.
63-
MapLocalArchetypesOutOfContext mapOutOfContext(BaseGenericSig,
64-
CapturedEnvs);
65-
auto interfaceTy = mapOutOfContext(local);
63+
auto interfaceTy = mapLocalArchetypesOutOfContext(
64+
local, BaseGenericSig, CapturedEnvs);
6665

6766
// Map this interface type into the new generic environment to get
6867
// a primary archetype.

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3796,12 +3796,8 @@ void ASTMangler::appendClosureComponents(CanType Ty, unsigned discriminator,
37963796
appendContext(parentContext, base, StringRef());
37973797

37983798
auto Sig = parentContext->getGenericSignatureOfContext();
3799-
3800-
Ty = Ty.subst(MapLocalArchetypesOutOfContext(Sig, capturedEnvs),
3801-
MakeAbstractConformanceForGenericType(),
3802-
SubstFlags::PreservePackExpansionLevel |
3803-
SubstFlags::SubstitutePrimaryArchetypes |
3804-
SubstFlags::SubstituteLocalArchetypes)->getCanonicalType();
3799+
Ty = mapLocalArchetypesOutOfContext(Ty, Sig, capturedEnvs)
3800+
->getCanonicalType();
38053801

38063802
appendType(Ty, Sig);
38073803
appendOperator(isImplicit ? "fu" : "fU", Index(discriminator));

lib/AST/LocalArchetypeRequirementCollector.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ Type MapLocalArchetypesOutOfContext::operator()(SubstitutableType *type) const {
203203
return getInterfaceType(archetypeTy->getInterfaceType(), genericEnv);
204204
}
205205

206+
Type swift::mapLocalArchetypesOutOfContext(
207+
Type type,
208+
GenericSignature baseGenericSig,
209+
ArrayRef<GenericEnvironment *> capturedEnvs) {
210+
return type.subst(MapLocalArchetypesOutOfContext(baseGenericSig, capturedEnvs),
211+
MakeAbstractConformanceForGenericType(),
212+
SubstFlags::PreservePackExpansionLevel |
213+
SubstFlags::SubstitutePrimaryArchetypes |
214+
SubstFlags::SubstituteLocalArchetypes);
215+
}
216+
206217
static Type mapIntoLocalContext(GenericTypeParamType *param, unsigned baseDepth,
207218
ArrayRef<GenericEnvironment *> capturedEnvs) {
208219
assert(!param->isParameterPack());

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,14 +1967,11 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
19671967
auto mapTypeOutOfContext = [&](Type t) -> CanType {
19681968
LLVM_DEBUG(llvm::dbgs() << "-- capture with contextual type " << t << "\n");
19691969

1970-
t = t.subst(MapLocalArchetypesOutOfContext(origGenericSig, capturedEnvs),
1971-
MakeAbstractConformanceForGenericType(),
1972-
SubstFlags::PreservePackExpansionLevel |
1973-
SubstFlags::SubstitutePrimaryArchetypes |
1974-
SubstFlags::SubstituteLocalArchetypes);
1975-
1976-
LLVM_DEBUG(llvm::dbgs() << "-- maps to " << t->getCanonicalType() << "\n");
1977-
return t->getCanonicalType();
1970+
auto result = mapLocalArchetypesOutOfContext(t, origGenericSig, capturedEnvs)
1971+
->getCanonicalType();
1972+
1973+
LLVM_DEBUG(llvm::dbgs() << "-- maps to " << result << "\n");
1974+
return result;
19781975
};
19791976

19801977
for (auto capture : loweredCaptures.getCaptures()) {
@@ -3004,26 +3001,17 @@ CanSILFunctionType swift::buildSILFunctionThunkType(
30043001
interfaceSubs);
30053002
}
30063003

3007-
MapLocalArchetypesOutOfContext mapOutOfContext(baseGenericSig, capturedEnvs);
30083004
auto substFormalTypeIntoThunkContext =
30093005
[&](CanType t) -> CanType {
30103006
return genericEnv->mapTypeIntoContext(
3011-
t.subst(mapOutOfContext,
3012-
MakeAbstractConformanceForGenericType(),
3013-
SubstFlags::PreservePackExpansionLevel |
3014-
SubstFlags::SubstitutePrimaryArchetypes |
3015-
SubstFlags::SubstituteLocalArchetypes))
3007+
mapLocalArchetypesOutOfContext(t, baseGenericSig, capturedEnvs))
30163008
->getCanonicalType();
30173009
};
30183010
auto substLoweredTypeIntoThunkContext =
30193011
[&](CanSILFunctionType t) -> CanSILFunctionType {
30203012
return cast<SILFunctionType>(
30213013
genericEnv->mapTypeIntoContext(
3022-
Type(t).subst(mapOutOfContext,
3023-
MakeAbstractConformanceForGenericType(),
3024-
SubstFlags::PreservePackExpansionLevel |
3025-
SubstFlags::SubstitutePrimaryArchetypes |
3026-
SubstFlags::SubstituteLocalArchetypes))
3014+
mapLocalArchetypesOutOfContext(t, baseGenericSig, capturedEnvs))
30273015
->getCanonicalType());
30283016
};
30293017

lib/SIL/IR/TypeLowering.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3905,12 +3905,8 @@ getAnyFunctionRefInterfaceType(TypeConverter &TC,
39053905

39063906
if (funcType->hasArchetype()) {
39073907
assert(isa<FunctionType>(funcType));
3908-
auto substType = Type(funcType).subst(
3909-
MapLocalArchetypesOutOfContext(sig.baseGenericSig, sig.capturedEnvs),
3910-
MakeAbstractConformanceForGenericType(),
3911-
SubstFlags::PreservePackExpansionLevel |
3912-
SubstFlags::SubstitutePrimaryArchetypes |
3913-
SubstFlags::SubstituteLocalArchetypes);
3908+
auto substType = mapLocalArchetypesOutOfContext(
3909+
funcType, sig.baseGenericSig, sig.capturedEnvs);
39143910
funcType = cast<FunctionType>(substType->getCanonicalType());
39153911
}
39163912

@@ -4964,15 +4960,10 @@ TypeConverter::getInterfaceBoxTypeForCapture(ValueDecl *captured,
49644960
SmallSetVector<GenericEnvironment *, 2> boxCapturedEnvs;
49654961
findCapturedEnvironments(loweredContextType, boxCapturedEnvs);
49664962

4967-
MapLocalArchetypesOutOfContext mapOutOfContext(baseGenericSig,
4968-
boxCapturedEnvs.getArrayRef());
4969-
4970-
auto loweredInterfaceType = loweredContextType.subst(
4971-
mapOutOfContext,
4972-
MakeAbstractConformanceForGenericType(),
4973-
SubstFlags::PreservePackExpansionLevel |
4974-
SubstFlags::SubstitutePrimaryArchetypes |
4975-
SubstFlags::SubstituteLocalArchetypes)->getCanonicalType();
4963+
auto loweredInterfaceType =
4964+
mapLocalArchetypesOutOfContext(loweredContextType, baseGenericSig,
4965+
boxCapturedEnvs.getArrayRef())
4966+
->getCanonicalType();
49764967

49774968
// If the type is not dependent at all, we can form a concrete box layout.
49784969
// We don't need to capture the generic environment.

lib/SILGen/ResultPlan.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,7 @@ mapTypeOutOfOpenedExistentialContext(CanType t, GenericEnvironment *genericEnv)
121121
forwardingSubs = genericEnv->getForwardingSubstitutionMap();
122122
}
123123

124-
MapLocalArchetypesOutOfContext mapOutOfContext(baseGenericSig, capturedEnvs);
125-
auto mappedTy = t.subst(mapOutOfContext,
126-
MakeAbstractConformanceForGenericType(),
127-
SubstFlags::SubstituteLocalArchetypes);
124+
auto mappedTy = mapLocalArchetypesOutOfContext(t, baseGenericSig, capturedEnvs);
128125

129126
auto genericSig = buildGenericSignatureWithCapturedEnvironments(
130127
ctx, baseGenericSig, capturedEnvs);

0 commit comments

Comments
 (0)