@@ -27,17 +27,14 @@ using namespace Lowering;
27
27
// / Bridge the given Swift value to its corresponding Objective-C
28
28
// / object, using the appropriate witness for the
29
29
// / _ObjectiveCBridgeable._bridgeToObjectiveC requirement.
30
- static Optional<ManagedValue> emitBridgeToObjectiveC (SILGenFunction &gen,
31
- SILLocation loc,
32
- ManagedValue swiftValue) {
30
+ static Optional<ManagedValue>
31
+ emitBridgeToObjectiveC (SILGenFunction &gen,
32
+ SILLocation loc,
33
+ ManagedValue swiftValue,
34
+ ProtocolConformance *conformance) {
33
35
// Dig out the nominal type we're bridging from.
34
36
Type swiftValueType = swiftValue.getSwiftType ()->getRValueType ();
35
37
36
- // Dig out its conformance to _ObjectiveCBridgeable.
37
- auto conformance =
38
- gen.SGM .getConformanceToObjectiveCBridgeable (loc, swiftValueType);
39
- if (!conformance) return None;
40
-
41
38
// Find the _bridgeToObjectiveC requirement.
42
39
auto requirement = gen.SGM .getBridgeToObjectiveCRequirement (loc);
43
40
if (!requirement) return None;
@@ -52,7 +49,7 @@ static Optional<ManagedValue> emitBridgeToObjectiveC(SILGenFunction &gen,
52
49
53
50
Type objcType =
54
51
conformance->getTypeWitness (objcTypeReq, nullptr ).getReplacement ();
55
- if (!objcTypeReq) return None ;
52
+ assert (objcType) ;
56
53
57
54
// Create a reference to the witness.
58
55
SILDeclRef witnessConstant (witness.getDecl ());
@@ -80,6 +77,23 @@ static Optional<ManagedValue> emitBridgeToObjectiveC(SILGenFunction &gen,
80
77
return gen.emitManagedRValueWithCleanup (bridgedValue);
81
78
}
82
79
80
+ // / Bridge the given Swift value to its corresponding Objective-C
81
+ // / object, using the appropriate witness for the
82
+ // / _ObjectiveCBridgeable._bridgeToObjectiveC requirement.
83
+ static Optional<ManagedValue> emitBridgeToObjectiveC (SILGenFunction &gen,
84
+ SILLocation loc,
85
+ ManagedValue swiftValue) {
86
+ // Dig out the nominal type we're bridging from.
87
+ Type swiftValueType = swiftValue.getSwiftType ()->getRValueType ();
88
+
89
+ // Dig out its conformance to _ObjectiveCBridgeable.
90
+ auto conformance =
91
+ gen.SGM .getConformanceToObjectiveCBridgeable (loc, swiftValueType);
92
+ if (!conformance) return None;
93
+
94
+ return emitBridgeToObjectiveC (gen, loc, swiftValue, conformance);
95
+ }
96
+
83
97
static ManagedValue emitBridgeStringToNSString (SILGenFunction &gen,
84
98
SILLocation loc,
85
99
ManagedValue str) {
@@ -108,40 +122,6 @@ static ManagedValue emitBridgeNSStringToString(SILGenFunction &gen,
108
122
return gen.emitManagedRValueWithCleanup (str);
109
123
}
110
124
111
- static ManagedValue emitBridgeCollectionFromNative (SILGenFunction &gen,
112
- SILLocation loc,
113
- SILDeclRef bridgeFnRef,
114
- ManagedValue collection,
115
- SILType bridgedTy) {
116
- SILValue bridgeFn = gen.emitGlobalFunctionRef (loc, bridgeFnRef);
117
-
118
- // If the expected return is optional, we'll need to wrap it.
119
- OptionalTypeKind OTK = OTK_None;
120
- SILType origBridgedTy = bridgedTy;
121
- if (auto bridgedObjTy = bridgedTy.getAnyOptionalObjectType (gen.SGM .M , OTK)) {
122
- bridgedTy = bridgedObjTy;
123
- }
124
-
125
- // Figure out the type parameters.
126
- auto inputTy
127
- = collection.getType ().getSwiftRValueType ()->castTo <BoundGenericType>();
128
- auto subs = inputTy->getSubstitutions (gen.SGM .M .getSwiftModule (), nullptr );
129
- auto substFnType = bridgeFn->getType ().substGenericArgs (gen.SGM .M , subs);
130
- SILValue bridged = gen.B .createApply (loc, bridgeFn,
131
- substFnType,
132
- bridgedTy,
133
- subs,
134
- { collection.forward (gen) });
135
- // Wrap the result if necessary.
136
- if (OTK != OTK_None) {
137
- bridged = gen.B .createEnum (loc, bridged,
138
- gen.getASTContext ().getOptionalSomeDecl (OTK),
139
- origBridgedTy);
140
- }
141
-
142
- return gen.emitManagedRValueWithCleanup (bridged);
143
- }
144
-
145
125
static ManagedValue emitBridgeCollectionToNative (SILGenFunction &gen,
146
126
SILLocation loc,
147
127
SILDeclRef bridgeFnRef,
@@ -429,30 +409,14 @@ static ManagedValue emitNativeToCBridgedNonoptionalValue(SILGenFunction &gen,
429
409
return gen.emitFuncToBlock (loc, v, bridgedFTy);
430
410
}
431
411
432
- // Bridge Array to NSArray.
433
- if (auto arrayDecl = gen.getASTContext ().getArrayDecl ()) {
434
- if (v.getType ().getSwiftRValueType ().getAnyNominal () == arrayDecl) {
435
- if (auto result = emitBridgeToObjectiveC (gen, loc, v))
436
- return *result;
437
-
438
- return gen.emitUndef (loc, bridgedTy);
439
- }
440
- }
441
-
442
- // Bridge Dictionary to NSDictionary.
443
- if (auto dictDecl = gen.getASTContext ().getDictionaryDecl ()) {
444
- if (v.getType ().getSwiftRValueType ().getAnyNominal () == dictDecl) {
445
- SILDeclRef bridgeFn = gen.SGM .getDictionaryToNSDictionaryFn ();
446
- return emitBridgeCollectionFromNative (gen, loc, bridgeFn, v, bridgedTy);
447
- }
448
- }
412
+ // If the native type conforms to _ObjectiveCBridgeable, use its
413
+ // _bridgeToObjectiveC witness.
414
+ if (auto conformance =
415
+ gen.SGM .getConformanceToObjectiveCBridgeable (loc, loweredNativeTy)) {
416
+ if (auto result = emitBridgeToObjectiveC (gen, loc, v, conformance))
417
+ return *result;
449
418
450
- // Bridge Set to NSSet.
451
- if (auto setDecl = gen.getASTContext ().getSetDecl ()) {
452
- if (v.getType ().getSwiftRValueType ().getAnyNominal () == setDecl) {
453
- SILDeclRef bridgeFn = gen.SGM .getSetToNSSetFn ();
454
- return emitBridgeCollectionFromNative (gen, loc, bridgeFn, v, bridgedTy);
455
- }
419
+ return gen.emitUndef (loc, bridgedTy);
456
420
}
457
421
458
422
return v;
0 commit comments