Skip to content

Commit fa2cd6c

Browse files
committed
SIL: Support element archetypes inside lowerCaptureContextParameters()
1 parent 1cad5f9 commit fa2cd6c

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,11 +1934,7 @@ void updateResultTypeForForeignInfo(
19341934
llvm_unreachable("unhandled kind");
19351935
}
19361936

1937-
/// Lower any/all capture context parameters.
1938-
///
1939-
/// *NOTE* Currently default arg generators can not capture anything.
1940-
/// If we ever add that ability, it will be a different capture list
1941-
/// from the function to which the argument is attached.
1937+
/// Captured values become SIL function parameters in this function.
19421938
static void
19431939
lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
19441940
CanGenericSignature genericSig,
@@ -1962,28 +1958,38 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
19621958
// signature from the AST for that.
19631959
auto origGenericSig = function.getAnyFunctionRef()->getGenericSignature();
19641960
auto loweredCaptures = TC.getLoweredLocalCaptures(function);
1961+
auto capturedEnvs = loweredCaptures.getGenericEnvironments();
19651962
auto *isolatedParam = loweredCaptures.getIsolatedParamCapture();
19661963

1964+
auto mapTypeOutOfContext = [&](Type t) -> CanType {
1965+
LLVM_DEBUG(llvm::dbgs() << "-- capture with contextual type " << t << "\n");
1966+
1967+
t = t.subst(MapLocalArchetypesOutOfContext(origGenericSig, capturedEnvs),
1968+
MakeAbstractConformanceForGenericType(),
1969+
SubstFlags::PreservePackExpansionLevel);
1970+
1971+
LLVM_DEBUG(llvm::dbgs() << "-- maps to " << t->getCanonicalType() << "\n");
1972+
return t->getCanonicalType();
1973+
};
1974+
19671975
for (auto capture : loweredCaptures.getCaptures()) {
19681976
if (capture.isDynamicSelfMetadata()) {
19691977
ParameterConvention convention = ParameterConvention::Direct_Unowned;
19701978
auto dynamicSelfInterfaceType =
1971-
loweredCaptures.getDynamicSelfType()->mapTypeOutOfContext();
1979+
mapTypeOutOfContext(loweredCaptures.getDynamicSelfType());
19721980

1973-
auto selfMetatype = MetatypeType::get(dynamicSelfInterfaceType,
1974-
MetatypeRepresentation::Thick);
1981+
auto selfMetatype = CanMetatypeType::get(dynamicSelfInterfaceType,
1982+
MetatypeRepresentation::Thick);
19751983

1976-
auto canSelfMetatype = selfMetatype->getReducedType(origGenericSig);
1977-
SILParameterInfo param(canSelfMetatype, convention);
1984+
SILParameterInfo param(selfMetatype, convention);
19781985
inputs.push_back(param);
19791986

19801987
continue;
19811988
}
19821989

19831990
if (capture.isOpaqueValue()) {
19841991
OpaqueValueExpr *opaqueValue = capture.getOpaqueValue();
1985-
auto canType = opaqueValue->getType()->mapTypeOutOfContext()
1986-
->getReducedType(origGenericSig);
1992+
auto canType = mapTypeOutOfContext(opaqueValue->getType());
19871993
auto &loweredTL =
19881994
TC.getTypeLowering(AbstractionPattern(genericSig, canType),
19891995
canType, expansion);
@@ -2001,9 +2007,16 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
20012007
continue;
20022008
}
20032009

2004-
auto *varDecl = capture.getDecl();
2005-
auto type = varDecl->getInterfaceType();
2006-
auto canType = type->getReducedType(origGenericSig);
2010+
auto *varDecl = cast<VarDecl>(capture.getDecl());
2011+
2012+
auto type = varDecl->getTypeInContext();
2013+
assert(!type->hasLocalArchetype() ||
2014+
(genericSig && origGenericSig &&
2015+
!genericSig->isEqual(origGenericSig)));
2016+
type = mapTypeOutOfContext(type);
2017+
2018+
auto canType = type->getReducedType(
2019+
genericSig ? genericSig : origGenericSig);
20072020

20082021
auto options = SILParameterInfo::Options();
20092022
if (isolatedParam == varDecl) {

0 commit comments

Comments
 (0)