Skip to content

Commit 33f4f57

Browse files
committed
SILGen: Add TypeExpansionContext to SILGen
1 parent e997110 commit 33f4f57

17 files changed

+404
-241
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ getBridgingFn(Optional<SILDeclRef> &cacheSlot,
110110
// Check that the function takes the expected arguments and returns the
111111
// expected result type.
112112
SILDeclRef c(fd);
113-
auto funcTy = SGM.Types.getConstantFunctionType(c);
113+
auto funcTy =
114+
SGM.Types.getConstantFunctionType(TypeExpansionContext::minimal(), c);
114115
SILFunctionConventions fnConv(funcTy, SGM.M);
115116

116117
auto toSILType = [&SGM](Type ty) {
117-
return SGM.Types.getLoweredType(ty, ResilienceExpansion::Minimal);
118+
return SGM.Types.getLoweredType(ty, TypeExpansionContext::minimal());
118119
};
119120

120121
if (inputTypes) {
@@ -916,7 +917,6 @@ SILFunction *SILGenModule::emitClosure(AbstractClosureExpr *ce) {
916917
// initializer of the containing type.
917918
if (!f->isExternalDeclaration())
918919
return f;
919-
920920
preEmitFunction(constant, ce, f, ce);
921921
PrettyStackTraceSILFunction X("silgen closureexpr", f);
922922
SILGenFunction(*this, *f, ce).emitClosure(ce);
@@ -947,8 +947,8 @@ bool SILGenModule::hasNonTrivialIVars(ClassDecl *cd) {
947947
auto *vd = dyn_cast<VarDecl>(member);
948948
if (!vd || !vd->hasStorage()) continue;
949949

950-
auto &ti = Types.getTypeLowering(vd->getType(),
951-
ResilienceExpansion::Maximal);
950+
auto &ti = Types.getTypeLowering(
951+
vd->getType(), TypeExpansionContext::maximalResilienceExpansionOnly());
952952
if (!ti.isTrivial())
953953
return true;
954954
}
@@ -1170,7 +1170,7 @@ SILFunction *SILGenModule::emitLazyGlobalInitializer(StringRef funcName,
11701170
Type initType = FunctionType::get({}, TupleType::getEmpty(C),
11711171
type->getExtInfo());
11721172
auto initSILType = cast<SILFunctionType>(
1173-
Types.getLoweredRValueType(initType));
1173+
Types.getLoweredRValueType(TypeExpansionContext::minimal(), initType));
11741174

11751175
SILGenFunctionBuilder builder(*this);
11761176
auto *f = builder.createFunction(
@@ -1354,11 +1354,12 @@ SILGenModule::canStorageUseStoredKeyPathComponent(AbstractStorageDecl *decl,
13541354
if (auto genericEnv =
13551355
decl->getInnermostDeclContext()->getGenericEnvironmentOfContext())
13561356
componentObjTy = genericEnv->mapTypeIntoContext(componentObjTy);
1357-
auto storageTy = M.Types.getSubstitutedStorageType(decl, componentObjTy);
1358-
auto opaqueTy =
1359-
M.Types.getLoweredRValueType(AbstractionPattern::getOpaque(),
1360-
componentObjTy);
1361-
1357+
auto storageTy = M.Types.getSubstitutedStorageType(
1358+
TypeExpansionContext::minimal(), decl, componentObjTy);
1359+
auto opaqueTy = M.Types.getLoweredRValueType(
1360+
TypeExpansionContext::noOpaqueTypeArchetypesSubstitution(expansion),
1361+
AbstractionPattern::getOpaque(), componentObjTy);
1362+
13621363
return storageTy.getASTType() == opaqueTy;
13631364
}
13641365
case AccessStrategy::DirectToAccessor:

lib/SILGen/SILGenApply.cpp

Lines changed: 76 additions & 52 deletions
Large diffs are not rendered by default.

lib/SILGen/SILGenBridging.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ emitBridgeNativeToObjectiveC(SILGenFunction &SGF,
123123
SGF.SGM.SwiftModule, dc);
124124

125125
// Substitute into the witness function type.
126-
witnessFnTy = witnessFnTy.substGenericArgs(SGF.SGM.M, typeSubMap);
126+
witnessFnTy = witnessFnTy.substGenericArgs(SGF.SGM.M, typeSubMap,
127+
SGF.getTypeExpansionContext());
127128

128129
// We might have to re-abstract the 'self' value if it is an
129130
// Optional.
@@ -204,7 +205,8 @@ emitBridgeObjectiveCToNative(SILGenFunction &SGF,
204205
SubstitutionMap typeSubMap = witness.getSubstitutions();
205206

206207
// Substitute into the witness function type.
207-
witnessFnTy = witnessFnTy->substGenericArgs(SGF.SGM.M, typeSubMap);
208+
witnessFnTy = witnessFnTy->substGenericArgs(SGF.SGM.M, typeSubMap,
209+
SGF.getTypeExpansionContext());
208210

209211
// The witness takes an _ObjectiveCType?, so convert to that type.
210212
CanType desiredValueType = OptionalType::get(objcType)->getCanonicalType();
@@ -220,7 +222,8 @@ emitBridgeObjectiveCToNative(SILGenFunction &SGF,
220222
SGF.B.createMetatype(loc,
221223
metatypeParam.getSILStorageType(SGF.SGM.M, witnessFnTy));
222224

223-
auto witnessCI = SGF.getConstantInfo(witnessConstant);
225+
auto witnessCI =
226+
SGF.getConstantInfo(SGF.getTypeExpansionContext(), witnessConstant);
224227
CanType formalResultTy = witnessCI.LoweredType.getResult();
225228

226229
auto subs = witness.getSubstitutions();
@@ -954,7 +957,8 @@ SILGenFunction::emitBlockToFunc(SILLocation loc,
954957

955958
if (thunkTy->getInvocationGenericSignature()) {
956959
substFnTy = thunkTy->substGenericArgs(F.getModule(),
957-
interfaceSubs);
960+
interfaceSubs,
961+
getTypeExpansionContext());
958962
}
959963

960964
// Create it in the current function.
@@ -1263,12 +1267,16 @@ static SILFunctionType *emitObjCThunkArguments(SILGenFunction &SGF,
12631267

12641268
auto subs = SGF.F.getForwardingSubstitutionMap();
12651269

1266-
auto objcInfo = SGF.SGM.Types.getConstantInfo(thunk);
1267-
auto objcFnTy = objcInfo.SILFnType->substGenericArgs(SGF.SGM.M, subs);
1270+
auto objcInfo =
1271+
SGF.SGM.Types.getConstantInfo(SGF.getTypeExpansionContext(), thunk);
1272+
auto objcFnTy = objcInfo.SILFnType->substGenericArgs(
1273+
SGF.SGM.M, subs, SGF.getTypeExpansionContext());
12681274
auto objcFormalFnTy = substGenericArgs(objcInfo.LoweredType, subs);
12691275

1270-
auto swiftInfo = SGF.SGM.Types.getConstantInfo(native);
1271-
auto swiftFnTy = swiftInfo.SILFnType->substGenericArgs(SGF.SGM.M, subs);
1276+
auto swiftInfo =
1277+
SGF.SGM.Types.getConstantInfo(SGF.getTypeExpansionContext(), native);
1278+
auto swiftFnTy = swiftInfo.SILFnType->substGenericArgs(
1279+
SGF.SGM.M, subs, SGF.getTypeExpansionContext());
12721280
auto swiftFormalFnTy = substGenericArgs(swiftInfo.LoweredType, subs);
12731281
SILFunctionConventions swiftConv(swiftFnTy, SGF.SGM.M);
12741282

@@ -1402,9 +1410,10 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
14021410
}
14031411
}
14041412

1405-
auto nativeInfo = getConstantInfo(native);
1413+
auto nativeInfo = getConstantInfo(getTypeExpansionContext(), native);
14061414
auto subs = F.getForwardingSubstitutionMap();
1407-
auto substTy = nativeInfo.SILFnType->substGenericArgs(SGM.M, subs);
1415+
auto substTy = nativeInfo.SILFnType->substGenericArgs(
1416+
SGM.M, subs, getTypeExpansionContext());
14081417
SILFunctionConventions substConv(substTy, SGM.M);
14091418

14101419
// Use the same generic environment as the native entry point.
@@ -1605,15 +1614,16 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
16051614
// Wrap the function in its original form.
16061615

16071616
auto fd = cast<AbstractFunctionDecl>(thunk.getDecl());
1608-
auto nativeCI = getConstantInfo(thunk);
1617+
auto nativeCI = getConstantInfo(getTypeExpansionContext(), thunk);
16091618
auto nativeFnTy = F.getLoweredFunctionType();
16101619
assert(nativeFnTy == nativeCI.SILFnType);
16111620

16121621
// Use the same generic environment as the native entry point.
16131622
F.setGenericEnvironment(SGM.Types.getConstantGenericEnvironment(thunk));
16141623

16151624
SILDeclRef foreignDeclRef = thunk.asForeign(true);
1616-
SILConstantInfo foreignCI = getConstantInfo(foreignDeclRef);
1625+
SILConstantInfo foreignCI =
1626+
getConstantInfo(getTypeExpansionContext(), foreignDeclRef);
16171627
auto foreignFnTy = foreignCI.SILFnType;
16181628

16191629
// Find the foreign error convention and 'self' parameter index.
@@ -1780,7 +1790,7 @@ void SILGenFunction::emitForeignToNativeThunk(SILDeclRef thunk) {
17801790
foreignCI);
17811791

17821792
auto fnType = fn->getType().castTo<SILFunctionType>();
1783-
fnType = fnType->substGenericArgs(SGM.M, subs);
1793+
fnType = fnType->substGenericArgs(SGM.M, subs, getTypeExpansionContext());
17841794

17851795
CanType nativeFormalResultType =
17861796
fd->mapTypeIntoContext(nativeCI.LoweredType.getResult())

lib/SILGen/SILGenConstructor.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ static SILValue emitConstructorMetatypeArg(SILGenFunction &SGF,
4949
VD->setInterfaceType(metatype);
5050

5151
SGF.AllocatorMetatype = SGF.F.begin()->createFunctionArgument(
52-
SGF.getLoweredType(DC->mapTypeIntoContext(metatype)), VD);
52+
SGF.getLoweredTypeForFunctionArgument(DC->mapTypeIntoContext(metatype)),
53+
VD);
5354

5455
return SGF.AllocatorMetatype;
5556
}
@@ -78,8 +79,7 @@ static RValue emitImplicitValueConstructorArg(SILGenFunction &SGF,
7879
VD->setSpecifier(ParamSpecifier::Default);
7980
VD->setInterfaceType(interfaceType);
8081

81-
auto argType = SGF.SGM.Types.getLoweredType(type,
82-
ResilienceExpansion::Minimal);
82+
auto argType = SGF.getLoweredTypeForFunctionArgument(type);
8383
auto *arg = SGF.F.begin()->createFunctionArgument(argType, VD);
8484
ManagedValue mvArg;
8585
if (arg->getArgumentConvention().isOwnedConvention()) {
@@ -129,7 +129,7 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
129129
auto *paramList = ctor->getParameters();
130130
auto *selfDecl = ctor->getImplicitSelfDecl();
131131
auto selfIfaceTy = selfDecl->getInterfaceType();
132-
SILType selfTy = SGF.getLoweredType(selfDecl->getType());
132+
SILType selfTy = SGF.getLoweredTypeForFunctionArgument(selfDecl->getType());
133133

134134
// Emit the indirect return argument, if any.
135135
SILValue resultSlot;
@@ -142,7 +142,8 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
142142
ctor);
143143
VD->setSpecifier(ParamSpecifier::InOut);
144144
VD->setInterfaceType(selfIfaceTy);
145-
resultSlot = SGF.F.begin()->createFunctionArgument(selfTy.getAddressType(), VD);
145+
resultSlot =
146+
SGF.F.begin()->createFunctionArgument(selfTy.getAddressType(), VD);
146147
}
147148

148149
// Emit the elementwise arguments.
@@ -164,7 +165,8 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
164165
if (resultSlot) {
165166
auto elti = elements.begin(), eltEnd = elements.end();
166167
for (VarDecl *field : decl->getStoredProperties()) {
167-
auto fieldTy = selfTy.getFieldType(field, SGF.SGM.M);
168+
auto fieldTy =
169+
selfTy.getFieldType(field, SGF.SGM.M, SGF.getTypeExpansionContext());
168170
SILValue slot =
169171
SGF.B.createStructElementAddr(Loc, resultSlot, field,
170172
fieldTy.getAddressType());
@@ -201,7 +203,8 @@ static void emitImplicitValueConstructor(SILGenFunction &SGF,
201203

202204
auto elti = elements.begin(), eltEnd = elements.end();
203205
for (VarDecl *field : decl->getStoredProperties()) {
204-
auto fieldTy = selfTy.getFieldType(field, SGF.SGM.M);
206+
auto fieldTy =
207+
selfTy.getFieldType(field, SGF.SGM.M, SGF.getTypeExpansionContext());
205208
SILValue v;
206209

207210
// If it's memberwise initialized, do so now.
@@ -416,8 +419,8 @@ void SILGenFunction::emitValueConstructor(ConstructorDecl *ctor) {
416419
void SILGenFunction::emitEnumConstructor(EnumElementDecl *element) {
417420
Type enumIfaceTy = element->getParentEnum()->getDeclaredInterfaceType();
418421
Type enumTy = F.mapTypeIntoContext(enumIfaceTy);
419-
auto &enumTI = SGM.Types.getTypeLowering(enumTy,
420-
ResilienceExpansion::Minimal);
422+
auto &enumTI =
423+
SGM.Types.getTypeLowering(enumTy, TypeExpansionContext::minimal());
421424

422425
RegularLocation Loc(element);
423426
CleanupLocation CleanupLoc(element);

lib/SILGen/SILGenDecl.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,12 +371,13 @@ class LocalVariableInitialization : public SingleBufferInitialization {
371371
"can't emit a local var for a non-local var decl");
372372
assert(decl->hasStorage() && "can't emit storage for a computed variable");
373373
assert(!SGF.VarLocs.count(decl) && "Already have an entry for this decl?");
374-
375-
auto boxType = SGF.SGM.Types
376-
.getContextBoxTypeForCapture(decl,
377-
SGF.SGM.Types.getLoweredRValueType(decl->getType()),
378-
SGF.F.getGenericEnvironment(),
379-
/*mutable*/ true);
374+
// The box type's context is lowered in the minimal resilience domain.
375+
auto boxType = SGF.SGM.Types.getContextBoxTypeForCapture(
376+
decl,
377+
SGF.SGM.Types.getLoweredRValueType(TypeExpansionContext::minimal(),
378+
decl->getType()),
379+
SGF.F.getGenericEnvironment(),
380+
/*mutable*/ true);
380381

381382
// The variable may have its lifetime extended by a closure, heap-allocate
382383
// it using a box.
@@ -850,7 +851,8 @@ void EnumElementPatternInitialization::emitEnumMatch(
850851
}
851852

852853
// Otherwise, the bound value for the enum case is available.
853-
SILType eltTy = value.getType().getEnumElementType(eltDecl, SGF.SGM.M);
854+
SILType eltTy = value.getType().getEnumElementType(
855+
eltDecl, SGF.SGM.M, SGF.getTypeExpansionContext());
854856
auto &eltTL = SGF.getTypeLowering(eltTy);
855857

856858
if (mv.getType().isAddress()) {
@@ -1237,7 +1239,7 @@ SILValue SILGenFunction::emitOSVersionRangeCheck(SILLocation loc,
12371239

12381240
auto silDeclRef = SILDeclRef(versionQueryDecl);
12391241
SILValue availabilityGTEFn = emitGlobalFunctionRef(
1240-
loc, silDeclRef, getConstantInfo(silDeclRef));
1242+
loc, silDeclRef, getConstantInfo(getTypeExpansionContext(), silDeclRef));
12411243

12421244
SILValue args[] = {majorValue, minorValue, subminorValue};
12431245
return B.createApply(loc, availabilityGTEFn, SubstitutionMap(), args);

lib/SILGen/SILGenDestructor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ void SILGenFunction::emitObjCDestructor(SILDeclRef dtor) {
232232
auto superclassDtor = SILDeclRef(superclassDtorDecl,
233233
SILDeclRef::Kind::Deallocator)
234234
.asForeign();
235-
auto superclassDtorType = SGM.Types.getConstantType(superclassDtor);
235+
auto superclassDtorType =
236+
SGM.Types.getConstantType(getTypeExpansionContext(), superclassDtor);
236237
SILValue superclassDtorValue = B.createObjCSuperMethod(
237238
cleanupLoc, selfValue, superclassDtor,
238239
superclassDtorType);

0 commit comments

Comments
 (0)