@@ -28,10 +28,10 @@ using namespace Lowering;
28
28
// / object, using the appropriate witness for the
29
29
// / _ObjectiveCBridgeable._bridgeToObjectiveC requirement.
30
30
static Optional<ManagedValue>
31
- emitBridgeNativeToObjectiveC (SILGenFunction &gen,
32
- SILLocation loc,
33
- ManagedValue swiftValue,
34
- ProtocolConformance *conformance) {
31
+ emitBridgeToObjectiveC (SILGenFunction &gen,
32
+ SILLocation loc,
33
+ ManagedValue swiftValue,
34
+ ProtocolConformance *conformance) {
35
35
// Dig out the nominal type we're bridging from.
36
36
Type swiftValueType = swiftValue.getSwiftType ()->getRValueType ();
37
37
@@ -80,10 +80,9 @@ emitBridgeNativeToObjectiveC(SILGenFunction &gen,
80
80
// / Bridge the given Swift value to its corresponding Objective-C
81
81
// / object, using the appropriate witness for the
82
82
// / _ObjectiveCBridgeable._bridgeToObjectiveC requirement.
83
- static Optional<ManagedValue>
84
- emitBridgeNativeToObjectiveC (SILGenFunction &gen,
85
- SILLocation loc,
86
- ManagedValue swiftValue) {
83
+ static Optional<ManagedValue> emitBridgeToObjectiveC (SILGenFunction &gen,
84
+ SILLocation loc,
85
+ ManagedValue swiftValue) {
87
86
// Dig out the nominal type we're bridging from.
88
87
Type swiftValueType = swiftValue.getSwiftType ()->getRValueType ();
89
88
@@ -92,7 +91,7 @@ emitBridgeNativeToObjectiveC(SILGenFunction &gen,
92
91
gen.SGM .getConformanceToObjectiveCBridgeable (loc, swiftValueType);
93
92
if (!conformance) return None;
94
93
95
- return emitBridgeNativeToObjectiveC (gen, loc, swiftValue, conformance);
94
+ return emitBridgeToObjectiveC (gen, loc, swiftValue, conformance);
96
95
}
97
96
98
97
static ManagedValue emitBridgeStringToNSString (SILGenFunction &gen,
@@ -101,80 +100,26 @@ static ManagedValue emitBridgeStringToNSString(SILGenFunction &gen,
101
100
llvm_unreachable (" Handled via the _bridgeToObjectiveC witness" );
102
101
}
103
102
104
- // / Bridge the given Objective-C object to its corresponding Swift
105
- // / value, using the appropriate witness for the
106
- // / _ObjectiveCBridgeable._unconditionallyBridgeFromObjectiveC requirement.
107
- static Optional<ManagedValue>
108
- emitBridgeObjectiveCToNative (SILGenFunction &gen,
109
- SILLocation loc,
110
- ManagedValue objcValue,
111
- ProtocolConformance *conformance) {
112
- // Find the _unconditionallyBridgeFromObjectiveC requirement.
113
- auto requirement =
114
- gen.SGM .getUnconditionallyBridgeFromObjectiveCRequirement (loc);
115
- if (!requirement) return None;
116
-
117
- // Retrieve the _unconditionallyBridgeFromObjectiveC witness.
118
- auto witness = conformance->getWitness (requirement, nullptr );
119
- assert (witness);
120
-
121
- // Create a reference to the witness.
122
- SILDeclRef witnessConstant (witness.getDecl ());
123
- auto witnessRef = gen.emitGlobalFunctionRef (loc, witnessConstant);
124
-
125
- // Determine the substitutions.
126
- ArrayRef<Substitution> substitutions;
127
- auto witnessFnTy = witnessRef->getType ().castTo <SILFunctionType>();
128
-
129
- Type swiftValueType = conformance->getType ();
130
- if (auto valueTypeBGT = swiftValueType->getAs <BoundGenericType>()) {
131
- // Compute the substitutions.
132
- substitutions = valueTypeBGT->getSubstitutions (gen.SGM .SwiftModule ,
133
- nullptr );
134
- }
135
-
136
- // Substitute into the witness function type.
137
- if (!substitutions.empty ())
138
- witnessFnTy = witnessFnTy->substGenericArgs (gen.SGM .M , gen.SGM .SwiftModule ,
139
- substitutions);
103
+ static ManagedValue emitBridgeNSStringToString (SILGenFunction &gen,
104
+ SILLocation loc,
105
+ ManagedValue nsstr) {
106
+ SILValue bridgeFn =
107
+ gen.emitGlobalFunctionRef (loc, gen.SGM .getNSStringToStringFn ());
140
108
141
- // If the Objective-C value isn't optional, wrap it in an optional.
142
- Type objcValueType = objcValue.getType ().getSwiftRValueType ();
143
- if (!objcValueType->getOptionalObjectType ()) {
144
- SILType loweredOptTy =
145
- gen.SGM .getLoweredType (OptionalType::get (objcValueType));
109
+ Type inputType = nsstr.getType ().getSwiftRValueType ();
110
+ if (!inputType->getOptionalObjectType ()) {
111
+ SILType loweredOptTy = gen.SGM .getLoweredType (OptionalType::get (inputType));
146
112
auto *someDecl = gen.getASTContext ().getOptionalSomeDecl ();
147
- auto *enumInst = gen.B .createEnum (loc, objcValue .getValue (), someDecl,
113
+ auto *enumInst = gen.B .createEnum (loc, nsstr .getValue (), someDecl,
148
114
loweredOptTy);
149
- objcValue = ManagedValue (enumInst, objcValue .getCleanup ());
115
+ nsstr = ManagedValue (enumInst, nsstr .getCleanup ());
150
116
}
151
117
152
- // Call the witness.
153
- Type metatype = MetatypeType::get (swiftValueType);
154
- SILValue metatypeValue = gen.B .createMetatype (loc,
155
- gen.getLoweredType (metatype));
156
-
157
- auto witnessCI = gen.getConstantInfo (witnessConstant);
158
- CanType formalResultTy = witnessCI.LoweredInterfaceType .getResult ();
159
-
160
- // Set up the generic signature.
161
- CanGenericSignature witnessGenericSignature;
162
- if (auto genericSig =
163
- cast<AbstractFunctionDecl>(witness.getDecl ())->getGenericSignature ())
164
- witnessGenericSignature = genericSig->getCanonicalSignature ();
165
-
166
- GenericContextScope genericContextScope (gen.SGM .Types ,
167
- witnessGenericSignature);
168
- return gen.emitApply (loc, ManagedValue::forUnmanaged (witnessRef),
169
- substitutions,
170
- { objcValue, ManagedValue::forUnmanaged (metatypeValue) },
171
- witnessFnTy,
172
- AbstractionPattern (witnessGenericSignature,
173
- formalResultTy),
174
- swiftValueType->getCanonicalType (),
175
- ApplyOptions::None, None, None,
176
- SGFContext ())
177
- .getAsSingleValue (gen, loc);
118
+ SILType nativeTy = gen.getLoweredType (gen.SGM .Types .getStringType ());
119
+ SILValue str = gen.B .createApply (loc, bridgeFn, bridgeFn->getType (), nativeTy,
120
+ {}, { nsstr.forward (gen) });
121
+
122
+ return gen.emitManagedRValueWithCleanup (str);
178
123
}
179
124
180
125
static ManagedValue emitBridgeCollectionToNative (SILGenFunction &gen,
@@ -431,7 +376,7 @@ static ManagedValue emitNativeToCBridgedNonoptionalValue(SILGenFunction &gen,
431
376
// hardcoding String -> NSString.
432
377
if (loweredNativeTy == gen.SGM .Types .getStringType ()
433
378
&& loweredBridgedTy == gen.SGM .Types .getNSStringType ()) {
434
- if (auto result = emitBridgeNativeToObjectiveC (gen, loc, v))
379
+ if (auto result = emitBridgeToObjectiveC (gen, loc, v))
435
380
return *result;
436
381
437
382
return gen.emitUndef (loc, bridgedTy);
@@ -468,7 +413,7 @@ static ManagedValue emitNativeToCBridgedNonoptionalValue(SILGenFunction &gen,
468
413
// _bridgeToObjectiveC witness.
469
414
if (auto conformance =
470
415
gen.SGM .getConformanceToObjectiveCBridgeable (loc, loweredNativeTy)) {
471
- if (auto result = emitBridgeNativeToObjectiveC (gen, loc, v, conformance))
416
+ if (auto result = emitBridgeToObjectiveC (gen, loc, v, conformance))
472
417
return *result;
473
418
474
419
return gen.emitUndef (loc, bridgedTy);
@@ -662,14 +607,7 @@ static ManagedValue emitCBridgedToNativeValue(SILGenFunction &gen,
662
607
// Bridge NSString to String.
663
608
if (auto stringDecl = gen.getASTContext ().getStringDecl ()) {
664
609
if (nativeTy.getSwiftRValueType ()->getAnyNominal () == stringDecl) {
665
- auto conformance =
666
- gen.SGM .getConformanceToObjectiveCBridgeable (loc, loweredNativeTy);
667
- assert (conformance &&
668
- " Missing String conformation to _ObjectiveCBridgeable?" );
669
- if (auto result = emitBridgeObjectiveCToNative (gen, loc, v, conformance))
670
- return *result;
671
-
672
- return gen.emitUndef (loc, nativeTy);
610
+ return emitBridgeNSStringToString (gen, loc, v);
673
611
}
674
612
}
675
613
0 commit comments