Skip to content

Commit 7fe577f

Browse files
committed
Sema: Clean up modeling of non-member VarDecl references
Give them substitutions just like with everything else, which eliminates some special cases from SILGen.
1 parent a3c15f2 commit 7fe577f

File tree

6 files changed

+26
-49
lines changed

6 files changed

+26
-49
lines changed

lib/SILGen/LValue.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ class LValue {
413413
}
414414

415415
void addNonMemberVarComponent(SILGenFunction &SGF, SILLocation loc,
416-
VarDecl *var, Optional<SubstitutionMap> subs,
416+
VarDecl *var, SubstitutionMap subs,
417417
LValueOptions options,
418418
SGFAccessKind accessKind,
419419
AccessStrategy strategy,

lib/SILGen/SILGen.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,6 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
439439
void emitMarkFunctionEscapeForTopLevelCodeGlobals(SILLocation loc,
440440
const CaptureInfo &captureInfo);
441441

442-
/// Get the substitutions necessary to invoke a non-member (global or local)
443-
/// property.
444-
SubstitutionMap
445-
getNonMemberVarDeclSubstitutions(VarDecl *var);
446-
447442
/// Map the substitutions for the original declaration to substitutions for
448443
/// the overridden declaration.
449444
static SubstitutionMap mapSubstitutionsForWitnessOverride(

lib/SILGen/SILGenExpr.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -867,13 +867,9 @@ emitRValueForDecl(SILLocation loc, ConcreteDeclRef declRef, Type ncRefType,
867867
}
868868

869869
// If this is a reference to a var, emit it as an l-value and then load.
870-
if (auto *var = dyn_cast<VarDecl>(decl)) {
871-
assert(!declRef.isSpecialized() &&
872-
"Cannot handle specialized variable references");
870+
if (auto *var = dyn_cast<VarDecl>(decl))
871+
return emitRValueForNonMemberVarDecl(loc, declRef, refType, semantics, C);
873872

874-
return emitRValueForNonMemberVarDecl(loc, var, refType, semantics, C);
875-
}
876-
877873
assert(!isa<TypeDecl>(decl));
878874

879875
// If the referenced decl isn't a VarDecl, it should be a constant of some

lib/SILGen/SILGenFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1874,7 +1874,8 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
18741874
LValue emitLValue(Expr *E, SGFAccessKind accessKind,
18751875
LValueOptions options = LValueOptions());
18761876

1877-
RValue emitRValueForNonMemberVarDecl(SILLocation loc, VarDecl *var,
1877+
RValue emitRValueForNonMemberVarDecl(SILLocation loc,
1878+
ConcreteDeclRef declRef,
18781879
CanType formalRValueType,
18791880
AccessSemantics semantics,
18801881
SGFContext C);

lib/SILGen/SILGenLValue.cpp

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,37 +2408,34 @@ namespace {
24082408
};
24092409
}
24102410

2411-
SubstitutionMap
2412-
SILGenModule::getNonMemberVarDeclSubstitutions(VarDecl *var) {
2413-
auto *dc = var->getDeclContext();
2414-
if (auto *genericEnv = dc->getGenericEnvironmentOfContext())
2415-
return genericEnv->getForwardingSubstitutionMap();
2416-
2417-
return SubstitutionMap();
2418-
}
2419-
24202411
static LValue emitLValueForNonMemberVarDecl(SILGenFunction &SGF,
2421-
SILLocation loc, VarDecl *var,
2412+
SILLocation loc,
2413+
ConcreteDeclRef declRef,
24222414
CanType formalRValueType,
24232415
SGFAccessKind accessKind,
24242416
LValueOptions options,
24252417
AccessSemantics semantics) {
24262418
LValue lv;
24272419

2420+
auto *var = cast<VarDecl>(declRef.getDecl());
2421+
auto subs = declRef.getSubstitutions();
2422+
if (!subs)
2423+
subs = SGF.F.getForwardingSubstitutionMap();
2424+
24282425
auto access = getFormalAccessKind(accessKind);
24292426
auto strategy = var->getAccessStrategy(semantics, access,
24302427
SGF.SGM.M.getSwiftModule(),
24312428
SGF.F.getResilienceExpansion());
24322429

2433-
lv.addNonMemberVarComponent(SGF, loc, var, /*be lazy*/ None,
2430+
lv.addNonMemberVarComponent(SGF, loc, var, subs,
24342431
options, accessKind, strategy, formalRValueType);
24352432

24362433
return lv;
24372434
}
24382435

24392436
void LValue::addNonMemberVarComponent(SILGenFunction &SGF, SILLocation loc,
24402437
VarDecl *var,
2441-
Optional<SubstitutionMap> subs,
2438+
SubstitutionMap subs,
24422439
LValueOptions options,
24432440
SGFAccessKind accessKind,
24442441
AccessStrategy strategy,
@@ -2447,17 +2444,11 @@ void LValue::addNonMemberVarComponent(SILGenFunction &SGF, SILLocation loc,
24472444
AccessEmitter<NonMemberVarAccessEmitter, VarDecl> {
24482445
LValue &LV;
24492446
SILLocation Loc;
2450-
Optional<SubstitutionMap> Subs;
2447+
SubstitutionMap Subs;
24512448
LValueOptions Options;
24522449

2453-
SubstitutionMap getSubs() {
2454-
if (Subs) return *Subs;
2455-
Subs = SGF.SGM.getNonMemberVarDeclSubstitutions(Storage);
2456-
return *Subs;
2457-
}
2458-
24592450
NonMemberVarAccessEmitter(SILGenFunction &SGF, SILLocation loc,
2460-
VarDecl *var, Optional<SubstitutionMap> subs,
2451+
VarDecl *var, SubstitutionMap subs,
24612452
SGFAccessKind accessKind,
24622453
CanType formalRValueType,
24632454
LValueOptions options, LValue &lv)
@@ -2469,7 +2460,7 @@ void LValue::addNonMemberVarComponent(SILGenFunction &SGF, SILLocation loc,
24692460
SILType storageType =
24702461
SGF.getLoweredType(Storage->getType()).getAddressType();
24712462
LV.add<AddressorComponent>(Storage, addressor,
2472-
/*isSuper=*/false, isDirect, getSubs(),
2463+
/*isSuper=*/false, isDirect, Subs,
24732464
CanType(), typeData, storageType, nullptr,
24742465
PreparedArguments(),
24752466
/* isOnSelfParameter */ false);
@@ -2479,23 +2470,23 @@ void LValue::addNonMemberVarComponent(SILGenFunction &SGF, SILLocation loc,
24792470
LValueTypeData typeData) {
24802471
LV.add<CoroutineAccessorComponent>(
24812472
Storage, accessor,
2482-
/*isSuper*/ false, isDirect, getSubs(), CanType(), typeData, nullptr,
2473+
/*isSuper*/ false, isDirect, Subs, CanType(), typeData, nullptr,
24832474
PreparedArguments(), /*isOnSelfParameter*/ false);
24842475
}
24852476

24862477
void emitUsingGetterSetter(SILDeclRef accessor, bool isDirect,
24872478
LValueTypeData typeData) {
24882479
LV.add<GetterSetterComponent>(
24892480
Storage, accessor,
2490-
/*isSuper=*/false, isDirect, getSubs(), CanType(), typeData, nullptr,
2481+
/*isSuper=*/false, isDirect, Subs, CanType(), typeData, nullptr,
24912482
PreparedArguments(), /* isOnSelfParameter */ false);
24922483
}
24932484

24942485
void emitUsingMaterialization(AccessStrategy readStrategy,
24952486
AccessStrategy writeStrategy,
24962487
LValueTypeData typeData) {
24972488
LV.add<MaterializeToTemporaryComponent>(
2498-
Storage, /*super*/ false, getSubs(), Options, readStrategy,
2489+
Storage, /*super*/ false, Subs, Options, readStrategy,
24992490
writeStrategy,
25002491
/*base type*/ CanType(), typeData, nullptr, PreparedArguments(),
25012492
/* isOnSelfParameter */ false);
@@ -2574,13 +2565,14 @@ SILGenFunction::emitAddressOfLocalVarDecl(SILLocation loc, VarDecl *var,
25742565
}
25752566

25762567
RValue SILGenFunction::emitRValueForNonMemberVarDecl(SILLocation loc,
2577-
VarDecl *var,
2568+
ConcreteDeclRef declRef,
25782569
CanType formalRValueType,
25792570
AccessSemantics semantics,
25802571
SGFContext C) {
25812572
// Any writebacks for this access are tightly scoped.
25822573
FormalEvaluationScope scope(*this);
25832574

2575+
auto *var = cast<VarDecl>(declRef.getDecl());
25842576
auto localValue = maybeEmitValueOfLocalVarDecl(var);
25852577

25862578
// If this VarDecl is represented as an address, emit it as an lvalue, then
@@ -2678,7 +2670,8 @@ RValue SILGenFunction::emitRValueForNonMemberVarDecl(SILLocation loc,
26782670
? Result : Result.copyUnmanaged(*this, loc));
26792671
}
26802672

2681-
LValue lv = emitLValueForNonMemberVarDecl(*this, loc, var, formalRValueType,
2673+
LValue lv = emitLValueForNonMemberVarDecl(*this, loc, declRef,
2674+
formalRValueType,
26822675
SGFAccessKind::OwnedObjectRead,
26832676
LValueOptions(), semantics);
26842677
return emitLoadOfLValue(loc, std::move(lv), C);
@@ -2703,8 +2696,7 @@ LValue SILGenLValue::visitDiscardAssignmentExpr(DiscardAssignmentExpr *e,
27032696

27042697
LValue SILGenLValue::visitDeclRefExpr(DeclRefExpr *e, SGFAccessKind accessKind,
27052698
LValueOptions options) {
2706-
// The only non-member decl that can be an lvalue is VarDecl.
2707-
return emitLValueForNonMemberVarDecl(SGF, e, cast<VarDecl>(e->getDecl()),
2699+
return emitLValueForNonMemberVarDecl(SGF, e, e->getDeclRef(),
27082700
getSubstFormalRValueType(e),
27092701
accessKind, options,
27102702
e->getAccessSemantics());

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -561,17 +561,10 @@ namespace {
561561
return typeExpr;
562562
}
563563

564-
SubstitutionMap substitutions;
565-
566-
// Due to a SILGen quirk, unqualified property references do not
567-
// need substitutions.
568-
if (!isa<VarDecl>(decl)) {
569-
substitutions =
564+
auto substitutions =
570565
solution.computeSubstitutions(
571566
decl->getInnermostDeclContext()->getGenericSignatureOfContext(),
572567
locator);
573-
}
574-
575568
auto declRefExpr =
576569
new (ctx) DeclRefExpr(ConcreteDeclRef(decl, substitutions),
577570
loc, implicit, semantics, type);

0 commit comments

Comments
 (0)