Skip to content

Commit d838ebd

Browse files
committed
[SILGen] Handle arbitrary ObjC-to-Swift bridging for globals
A more general fix for import-as-member NSStrings than Slava's dfdd3a7. Reverts the SILGen change from that commit in favor of a new PathComponent for lvalue access. In theory this will handle mutable bridged import-as-member globals as well, but we don't have any of those yet.
1 parent 652edc9 commit d838ebd

File tree

1 file changed

+68
-4
lines changed

1 file changed

+68
-4
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,67 @@ namespace {
18451845
}
18461846
};
18471847

1848+
/// Remap an lvalue referencing an imported global variable to the
1849+
/// corresponding Swift type.
1850+
class BridgeToNativeComponent : public TranslationPathComponent {
1851+
AbstractionPattern OrigType;
1852+
1853+
public:
1854+
BridgeToNativeComponent(AbstractionPattern origType,
1855+
CanType substFormalType,
1856+
SILType loweredSubstType)
1857+
: TranslationPathComponent({ AbstractionPattern(substFormalType),
1858+
substFormalType, loweredSubstType },
1859+
OrigToSubstKind),
1860+
OrigType(origType)
1861+
{}
1862+
1863+
RValue untranslate(SILGenFunction &SGF, SILLocation loc,
1864+
RValue &&rv, SGFContext c) && override {
1865+
SILType originalLoweredType = SGF.getLoweredType(OrigType.getType());
1866+
auto bridging = Conversion::getBridging(Conversion::BridgeToObjC,
1867+
getSubstFormalType(),
1868+
OrigType.getType(),
1869+
originalLoweredType);
1870+
return RValue(SGF, loc, originalLoweredType.getSwiftRValueType(),
1871+
SGF.emitConvertedRValue(loc, bridging, c,
1872+
[&](SILGenFunction &SGF,
1873+
SILLocation loc,
1874+
SGFContext c) {
1875+
return std::move(rv).getScalarValue();
1876+
}));
1877+
}
1878+
1879+
RValue translate(SILGenFunction &SGF, SILLocation loc,
1880+
RValue &&rv, SGFContext c) && override {
1881+
auto bridging = Conversion::getBridging(Conversion::BridgeFromObjC,
1882+
OrigType.getType(),
1883+
getSubstFormalType(),
1884+
getTypeOfRValue());
1885+
return RValue(SGF, loc, getSubstFormalType(),
1886+
SGF.emitConvertedRValue(loc, bridging, c,
1887+
[&](SILGenFunction &SGF,
1888+
SILLocation loc,
1889+
SGFContext c) {
1890+
return std::move(rv).getScalarValue();
1891+
}));
1892+
}
1893+
1894+
std::unique_ptr<LogicalPathComponent>
1895+
clone(SILGenFunction &SGF, SILLocation loc) const override {
1896+
return llvm::make_unique<BridgeToNativeComponent>(OrigType,
1897+
getSubstFormalType(),
1898+
getTypeOfRValue());
1899+
}
1900+
1901+
void dump(raw_ostream &OS, unsigned indent) const override {
1902+
OS.indent(indent) << "BridgeToNativeComponent("
1903+
<< getOrigFormalType() << ", "
1904+
<< getSubstFormalType() << ", "
1905+
<< getTypeOfRValue() << ")\n";
1906+
}
1907+
};
1908+
18481909
/// Remap a weak value to Optional<T>*, or unowned pointer to T*.
18491910
class OwnershipComponent : public LogicalPathComponent {
18501911
public:
@@ -2219,6 +2280,13 @@ static LValue emitLValueForNonMemberVarDecl(SILGenFunction &SGF,
22192280

22202281
if (address.getType().is<ReferenceStorageType>())
22212282
lv.add<OwnershipComponent>(typeData);
2283+
2284+
AbstractionPattern pattern = SGF.SGM.Types.getAbstractionPattern(var);
2285+
if (pattern.isClangType() &&
2286+
lv.getTypeOfRValue().getSwiftRValueType() != formalRValueType) {
2287+
lv.add<BridgeToNativeComponent>(pattern, formalRValueType,
2288+
SGF.getLoweredType(formalRValueType));
2289+
}
22222290
break;
22232291
}
22242292

@@ -3213,9 +3281,6 @@ RValue SILGenFunction::emitLoadOfLValue(SILLocation loc, LValue &&src,
32133281
// Any writebacks should be scoped to after the load.
32143282
FormalEvaluationScope scope(*this);
32153283

3216-
// We shouldn't need to re-abstract here, but we might have to bridge.
3217-
// This should only happen if we have a global variable of NSString type.
3218-
auto origFormalType = src.getOrigFormalType();
32193284
auto substFormalType = src.getSubstFormalType();
32203285
auto &rvalueTL = getTypeLowering(src.getTypeOfRValue());
32213286

@@ -3229,7 +3294,6 @@ RValue SILGenFunction::emitLoadOfLValue(SILLocation loc, LValue &&src,
32293294
.offset(*this, loc, addr, AccessKind::Read);
32303295
return RValue(*this, loc, substFormalType,
32313296
emitLoad(loc, addr.getValue(),
3232-
origFormalType, substFormalType,
32333297
rvalueTL, C, IsNotTake,
32343298
isBaseGuaranteed));
32353299
}

0 commit comments

Comments
 (0)