Skip to content

Commit 85f61c5

Browse files
committed
AST: Factor out MapIntoLocalArchetypeContext from buildSubstitutionMapWithCapturedEnvironments()
1 parent 7251f76 commit 85f61c5

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

include/swift/AST/LocalArchetypeRequirementCollector.h

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

56+
struct MapIntoLocalArchetypeContext {
57+
GenericEnvironment *baseGenericEnv;
58+
ArrayRef<GenericEnvironment *> capturedEnvs;
59+
60+
MapIntoLocalArchetypeContext(GenericEnvironment *baseGenericEnv,
61+
ArrayRef<GenericEnvironment *> capturedEnvs)
62+
: baseGenericEnv(baseGenericEnv), capturedEnvs(capturedEnvs) {}
63+
64+
Type operator()(SubstitutableType *type) const;
65+
};
66+
5667
GenericSignature buildGenericSignatureWithCapturedEnvironments(
5768
ASTContext &ctx,
5869
GenericSignature sig,

lib/AST/LocalArchetypeRequirementCollector.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,28 @@ Type MapLocalArchetypesOutOfContext::operator()(SubstitutableType *type) const {
194194
llvm_unreachable("Fell off the end");
195195
}
196196

197+
static Type mapIntoLocalContext(GenericTypeParamType *param, unsigned baseDepth,
198+
ArrayRef<GenericEnvironment *> capturedEnvs) {
199+
assert(!param->isParameterPack());
200+
unsigned envIndex = param->getDepth() - baseDepth;
201+
assert(envIndex < capturedEnvs.size());
202+
auto *capturedEnv = capturedEnvs[envIndex];
203+
auto localInterfaceType = capturedEnv->getGenericSignature()
204+
.getInnermostGenericParams()[param->getIndex()];
205+
assert(localInterfaceType->getIndex() == param->getIndex());
206+
return capturedEnvs[envIndex]->mapTypeIntoContext(localInterfaceType);
207+
}
208+
209+
Type MapIntoLocalArchetypeContext::operator()(SubstitutableType *type) const {
210+
unsigned baseDepth = baseGenericEnv->getGenericSignature().getNextDepth();
211+
212+
auto param = cast<GenericTypeParamType>(type);
213+
if (param->getDepth() >= baseDepth)
214+
return mapIntoLocalContext(param, baseDepth, capturedEnvs);
215+
216+
return baseGenericEnv->mapTypeIntoContext(param);
217+
}
218+
197219
/// Given a substitution map for a call to a local function or closure, extend
198220
/// it to include all captured element archetypes; they become primary archetypes
199221
/// inside the body of the function.
@@ -215,15 +237,8 @@ swift::buildSubstitutionMapWithCapturedEnvironments(
215237
genericSigWithCaptures,
216238
[&](SubstitutableType *type) -> Type {
217239
auto param = cast<GenericTypeParamType>(type);
218-
if (param->getDepth() >= baseDepth) {
219-
assert(!param->isParameterPack());
220-
unsigned envIndex = param->getDepth() - baseDepth;
221-
assert(envIndex < capturedEnvs.size());
222-
auto *capturedEnv = capturedEnvs[envIndex];
223-
auto localInterfaceType = capturedEnv->getGenericSignature()
224-
.getInnermostGenericParams()[param->getIndex()];
225-
return capturedEnvs[envIndex]->mapTypeIntoContext(localInterfaceType);
226-
}
240+
if (param->getDepth() >= baseDepth)
241+
return mapIntoLocalContext(param, baseDepth, capturedEnvs);
227242
return Type(type).subst(baseSubMap);
228243
},
229244
[&](CanType origType, Type substType,

0 commit comments

Comments
 (0)