Skip to content

SILGen: Handle existential keypath root types. #10014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,9 @@ class alignas(1 << TypeAlignInBits) TypeBase {
/// bound.
bool isClassExistentialType();

/// Opens an existential instance or meta-type and returns the opened type.
Type openAnyExistentialType(ArchetypeType *&opened);

/// Break an existential down into a set of constraints.
ExistentialLayout getExistentialLayout();

Expand Down
13 changes: 13 additions & 0 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4146,3 +4146,16 @@ getRecursivePropertiesFromSubstitutions(SubstitutionList Params) {
}
return props;
}

Type TypeBase::openAnyExistentialType(ArchetypeType *&opened) {
assert(isAnyExistentialType());
if (auto metaty = getAs<ExistentialMetatypeType>()) {
opened = ArchetypeType::getOpened(metaty->getInstanceType());
if (metaty->hasRepresentation())
return MetatypeType::get(opened, metaty->getRepresentation());
else
return MetatypeType::get(opened);
}
opened = ArchetypeType::getOpened(this);
return opened;
}
85 changes: 55 additions & 30 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2415,6 +2415,44 @@ RValue RValueEmitter::visitObjCSelectorExpr(ObjCSelectorExpr *e, SGFContext C) {
return RValue(SGF, e, ManagedValue::forUnmanaged(selectorValue));
}

static ManagedValue
emitKeyPathRValueBase(SILGenFunction &subSGF,
VarDecl *property,
SILLocation loc,
SILValue paramArg,
CanType &baseType) {
auto paramOrigValue = subSGF.emitManagedRValueWithCleanup(paramArg);
auto paramSubstValue = subSGF.emitOrigToSubstValue(loc, paramOrigValue,
AbstractionPattern::getOpaque(),
baseType);

// Upcast a class instance to the property's declared type if necessary.
if (auto propertyClass = dyn_cast<ClassDecl>(property->getDeclContext())) {
if (baseType->getClassOrBoundGenericClass() != propertyClass) {
baseType = baseType->getSuperclassForDecl(propertyClass)
->getCanonicalType();
paramSubstValue = subSGF.B.createUpcast(loc, paramSubstValue,
SILType::getPrimitiveObjectType(baseType));
}
}
// …or pop open an existential container.
else if (baseType->isAnyExistentialType()) {
ArchetypeType *opened;
baseType = baseType->openAnyExistentialType(opened)->getCanonicalType();
auto openedOpaqueValue = subSGF.emitOpenExistential(loc, paramSubstValue,
opened, subSGF.SGM.getLoweredType(baseType),
AccessKind::Read);
// Maybe we could peephole this if we know the property load can borrow the
// base value…
if (!openedOpaqueValue.IsConsumable) {
paramSubstValue = openedOpaqueValue.Value.copyUnmanaged(subSGF, loc);
} else {
paramSubstValue = openedOpaqueValue.Value;
}
}
return paramSubstValue;
}

static SILFunction *getOrCreateKeyPathGetter(SILGenFunction &SGF,
SILLocation loc,
VarDecl *property,
Expand Down Expand Up @@ -2482,22 +2520,10 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenFunction &SGF,

Scope scope(subSGF, loc);

auto paramOrigValue = subSGF.emitManagedRValueWithCleanup(paramArg);
auto paramSubstValue = subSGF.emitOrigToSubstValue(loc, paramOrigValue,
AbstractionPattern::getOpaque(),
baseType);

// Upcast a class instance to the property's declared type if necessary.
if (auto propertyClass = dyn_cast<ClassDecl>(property->getDeclContext())) {
if (baseType->getClassOrBoundGenericClass() != propertyClass) {
do {
baseType = baseType->getSuperclass()->getCanonicalType();
} while (baseType->getClassOrBoundGenericClass() != propertyClass);
paramSubstValue = subSGF.B.createUpcast(loc, paramSubstValue,
SILType::getPrimitiveObjectType(baseType));
}
}

auto paramSubstValue = emitKeyPathRValueBase(subSGF, property,
loc, paramArg,
baseType);

auto subs = baseType->getContextSubstitutionMap(subSGF.SGM.M.getSwiftModule(),
property->getInnermostDeclContext()->getInnermostTypeContext());
SmallVector<Substitution, 4> subsList;
Expand Down Expand Up @@ -2602,27 +2628,26 @@ SILFunction *getOrCreateKeyPathSetter(SILGenFunction &SGF,

LValue lv;
if (property->isSetterNonMutating()) {
auto baseOrig = subSGF.emitManagedRValueWithCleanup(baseArg);
auto baseSubst = subSGF.emitOrigToSubstValue(loc, baseOrig,
AbstractionPattern::getOpaque(),
baseType);
// Upcast a class instance to the property's declared type if necessary.
if (auto propertyClass = dyn_cast<ClassDecl>(property->getDeclContext())) {
if (baseType->getClassOrBoundGenericClass() != propertyClass) {
do {
baseType = baseType->getSuperclass()->getCanonicalType();
} while (baseType->getClassOrBoundGenericClass() != propertyClass);
baseSubst = subSGF.B.createUpcast(loc, baseSubst,
SILType::getPrimitiveObjectType(baseType));
}
}
auto baseSubst = emitKeyPathRValueBase(subSGF, property,
loc, baseArg,
baseType);

lv = LValue::forValue(baseSubst, baseType);
} else {
auto baseOrig = ManagedValue::forLValue(baseArg);
lv = LValue::forAddress(baseOrig, None,
AbstractionPattern::getOpaque(),
baseType);

// Open an existential lvalue, if necessary.
if (baseType->isAnyExistentialType()) {
ArchetypeType *opened;
baseType = baseType->openAnyExistentialType(opened)->getCanonicalType();
lv = subSGF.emitOpenExistentialLValue(loc, std::move(lv),
CanArchetypeType(opened),
baseType,
AccessKind::ReadWrite);
}
}

auto subs = baseType->getContextSubstitutionMap(subSGF.SGM.M.getSwiftModule(),
Expand Down
19 changes: 13 additions & 6 deletions lib/SILGen/SILGenLValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1751,13 +1751,20 @@ namespace {

LValue LValue::forValue(ManagedValue value,
CanType substFormalType) {
assert(value.getType().isObject());
LValueTypeData typeData = getValueTypeData(substFormalType,
value.getValue());
if (value.getType().isObject()) {
LValueTypeData typeData = getValueTypeData(substFormalType,
value.getValue());

LValue lv;
lv.add<ValueComponent>(value, None, typeData, /*isRValue=*/true);
return lv;
LValue lv;
lv.add<ValueComponent>(value, None, typeData, /*isRValue=*/true);
return lv;
} else {
// Treat an address-only value as an lvalue we only read from.
if (!value.isLValue())
value = ManagedValue::forLValue(value.getValue());
return forAddress(value, None, AbstractionPattern(substFormalType),
substFormalType);
}
}

LValue LValue::forAddress(ManagedValue address,
Expand Down
8 changes: 1 addition & 7 deletions lib/SILGen/SILGenMaterializeForSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,7 @@ struct MaterializeForSetEmitter {
// Metatypes and bases of non-mutating setters on value types
// are always rvalues.
if (!SubstSelfType->getRValueInstanceType()->mayHaveSuperclass()) {
if (self.getType().isObject())
return LValue::forValue(self, SubstSelfType);
else {
if (!self.isLValue())
self = ManagedValue::forLValue(self.getValue());
return LValue::forAddress(self, None, selfPattern, SubstSelfType);
}
return LValue::forValue(self, SubstSelfType);
}

CanType witnessSelfType =
Expand Down
11 changes: 11 additions & 0 deletions test/SILGen/keypaths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ extension P {
var z: String {
return y
}
var w: String {
get { return "" }
nonmutating set { }
}
}

// CHECK-LABEL: sil hidden @{{.*}}storedProperties
Expand Down Expand Up @@ -171,3 +175,10 @@ class BB<U, V>: AA<V> {
func keyPathForInheritedMember() {
_ = \BB<Int, String>.a
}

func keyPathForExistentialMember() {
_ = \P.x
_ = \P.y
_ = \P.z
_ = \P.w
}
34 changes: 33 additions & 1 deletion test/stdlib/KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,18 @@ keyPath.test("computed properties") {

class AB {
}
class ABC: AB {
class ABC: AB, ABCProtocol {
var a = LifetimeTracked(1)
var b = LifetimeTracked(2)
var c = LifetimeTracked(3)
}

protocol ABCProtocol {
var a: LifetimeTracked { get }
var b: LifetimeTracked { get set }
var c: LifetimeTracked { get nonmutating set }
}

keyPath.test("dynamically-typed application") {
let cPaths = [\ABC.a, \ABC.b, \ABC.c]

Expand Down Expand Up @@ -312,6 +318,32 @@ keyPath.test("dynamically-typed application") {
expectTrue(wrongFields[1] == nil)
expectTrue(wrongFields[2] == nil)
}

var protoErasedSubject: ABCProtocol = subject
let protoErasedPathA = \ABCProtocol.a
let protoErasedPathB = \ABCProtocol.b
let protoErasedPathC = \ABCProtocol.c

do {
expectTrue(protoErasedSubject.a ===
protoErasedSubject[keyPath: protoErasedPathA])

let newB = LifetimeTracked(4)
expectTrue(protoErasedSubject.b ===
protoErasedSubject[keyPath: protoErasedPathB])
protoErasedSubject[keyPath: protoErasedPathB] = newB
expectTrue(protoErasedSubject.b ===
protoErasedSubject[keyPath: protoErasedPathB])
expectTrue(protoErasedSubject.b === newB)

let newC = LifetimeTracked(5)
expectTrue(protoErasedSubject.c ===
protoErasedSubject[keyPath: protoErasedPathC])
protoErasedSubject[keyPath: protoErasedPathC] = newC
expectTrue(protoErasedSubject.c ===
protoErasedSubject[keyPath: protoErasedPathC])
expectTrue(protoErasedSubject.c === newC)
}
}

runAllTests()