@@ -1845,6 +1845,67 @@ namespace {
1845
1845
}
1846
1846
};
1847
1847
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
+
1848
1909
// / Remap a weak value to Optional<T>*, or unowned pointer to T*.
1849
1910
class OwnershipComponent : public LogicalPathComponent {
1850
1911
public:
@@ -2219,6 +2280,13 @@ static LValue emitLValueForNonMemberVarDecl(SILGenFunction &SGF,
2219
2280
2220
2281
if (address.getType ().is <ReferenceStorageType>())
2221
2282
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
+ }
2222
2290
break ;
2223
2291
}
2224
2292
@@ -3213,9 +3281,6 @@ RValue SILGenFunction::emitLoadOfLValue(SILLocation loc, LValue &&src,
3213
3281
// Any writebacks should be scoped to after the load.
3214
3282
FormalEvaluationScope scope (*this );
3215
3283
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 ();
3219
3284
auto substFormalType = src.getSubstFormalType ();
3220
3285
auto &rvalueTL = getTypeLowering (src.getTypeOfRValue ());
3221
3286
@@ -3229,7 +3294,6 @@ RValue SILGenFunction::emitLoadOfLValue(SILLocation loc, LValue &&src,
3229
3294
.offset (*this , loc, addr, AccessKind::Read);
3230
3295
return RValue (*this , loc, substFormalType,
3231
3296
emitLoad (loc, addr.getValue (),
3232
- origFormalType, substFormalType,
3233
3297
rvalueTL, C, IsNotTake,
3234
3298
isBaseGuaranteed));
3235
3299
}
0 commit comments