45
45
46
46
using namespace swift ;
47
47
48
- // / If target is a Swift type bridging to an ObjC type,
49
- // / return the ObjC type it bridges to.
50
- // / If target is an ObjC type, return this type.
51
- static Type getCastFromObjC (SILModule &M, CanType source, CanType target) {
52
- return M.getASTContext ().getBridgedToObjC (M.getSwiftModule (), target);
53
- }
54
-
55
48
// / Create a call of _forceBridgeFromObjectiveC_bridgeable or
56
49
// / _conditionallyBridgeFromObjectiveC_bridgeable which converts an ObjC
57
50
// / instance into a corresponding Swift type, conforming to
58
51
// / _ObjectiveCBridgeable.
59
- SILInstruction *CastOptimizer::optimizeBridgedObjCToSwiftCast (
60
- SILInstruction *Inst, bool isConditional, SILValue Src, SILValue Dest,
61
- CanType Source, CanType Target, Type BridgedSourceTy, Type BridgedTargetTy,
62
- SILBasicBlock *SuccessBB, SILBasicBlock *FailureBB) {
52
+ SILInstruction *
53
+ CastOptimizer::optimizeBridgedObjCToSwiftCast (SILDynamicCastInst dynamicCast) {
54
+
55
+ SILInstruction *Inst = dynamicCast.getInstruction ();
56
+ bool isConditional = dynamicCast.isConditional ();
57
+ SILValue Src = dynamicCast.getSource ();
58
+ SILValue Dest = dynamicCast.getDest ();
59
+ CanType Target = dynamicCast.getTargetType ();
60
+ CanType BridgedTargetTy = dynamicCast.getBridgedTargetType ();
61
+ SILBasicBlock *SuccessBB = dynamicCast.getSuccessBlock ();
62
+ SILBasicBlock *FailureBB = dynamicCast.getFailureBlock ();
63
63
auto *F = Inst->getFunction ();
64
64
auto &M = Inst->getModule ();
65
65
auto Loc = Inst->getLoc ();
@@ -313,12 +313,17 @@ static bool canOptimizeCast(const swift::Type &BridgedTargetTy,
313
313
314
314
// / Create a call of _bridgeToObjectiveC which converts an _ObjectiveCBridgeable
315
315
// / instance into a bridged ObjC type.
316
- SILInstruction *CastOptimizer::optimizeBridgedSwiftToObjCCast (
317
- SILInstruction *Inst, CastConsumptionKind ConsumptionKind,
318
- bool isConditional, SILValue Src, SILValue Dest, CanType Source,
319
- CanType Target, Type BridgedSourceTy, Type BridgedTargetTy,
320
- SILBasicBlock *SuccessBB, SILBasicBlock *FailureBB) {
321
-
316
+ SILInstruction *
317
+ CastOptimizer::optimizeBridgedSwiftToObjCCast (SILDynamicCastInst dynamicCast) {
318
+ SILInstruction *Inst = dynamicCast.getInstruction ();
319
+ CastConsumptionKind ConsumptionKind = dynamicCast.getBridgedConsumptionKind ();
320
+ bool isConditional = dynamicCast.isConditional ();
321
+ SILValue Src = dynamicCast.getSource ();
322
+ SILValue Dest = dynamicCast.getDest ();
323
+ CanType Source = dynamicCast.getSourceType ();
324
+ CanType BridgedTargetTy = dynamicCast.getBridgedTargetType ();
325
+ SILBasicBlock *SuccessBB = dynamicCast.getSuccessBlock ();
326
+ SILBasicBlock *FailureBB = dynamicCast.getFailureBlock ();
322
327
auto &M = Inst->getModule ();
323
328
auto Loc = Inst->getLoc ();
324
329
@@ -596,24 +601,17 @@ SILInstruction *CastOptimizer::optimizeBridgedSwiftToObjCCast(
596
601
return NewI;
597
602
}
598
603
599
- // / Make use of the fact that some of these casts cannot fail. For example, if
600
- // / the ObjC type is exactly the expected _ObjectiveCType type, then it would
601
- // / always succeed for NSString, NSNumber, etc. Casts from NSArray,
602
- // / NSDictionary and NSSet may fail.
604
+ // / Make use of the fact that some of these casts cannot fail. For
605
+ // / example, if the ObjC type is exactly the expected _ObjectiveCType
606
+ // / type, then it would always succeed for NSString, NSNumber, etc.
607
+ // / Casts from NSArray, NSDictionary and NSSet may fail.
603
608
// /
604
- // / If ObjC class is not exactly _ObjectiveCType, then its conversion to a
605
- // / required _ObjectiveCType may fail.
609
+ // / If ObjC class is not exactly _ObjectiveCType, then its conversion
610
+ // / to a required _ObjectiveCType may fail.
606
611
SILInstruction *
607
612
CastOptimizer::optimizeBridgedCasts (SILDynamicCastInst dynamicCast) {
608
- SILInstruction *Inst = dynamicCast.getInstruction ();
609
- CastConsumptionKind ConsumptionKind = dynamicCast.getBridgedConsumptionKind ();
610
- bool isConditional = dynamicCast.isConditional ();
611
- SILValue Src = dynamicCast.getSource ();
612
- SILValue Dest = dynamicCast.getDest ();
613
613
CanType source = dynamicCast.getSourceType ();
614
614
CanType target = dynamicCast.getTargetType ();
615
- SILBasicBlock *SuccessBB = dynamicCast.getSuccessBlock ();
616
- SILBasicBlock *FailureBB = dynamicCast.getFailureBlock ();
617
615
auto &M = dynamicCast.getModule ();
618
616
619
617
// To apply the bridged optimizations, we should ensure that types are not
@@ -631,17 +629,14 @@ CastOptimizer::optimizeBridgedCasts(SILDynamicCastInst dynamicCast) {
631
629
if (source->hasArchetype () || target->hasArchetype ())
632
630
return nullptr ;
633
631
634
- auto BridgedTargetTy = getCastFromObjC (M, source, target);
635
- if (!BridgedTargetTy)
636
- return nullptr ;
632
+ CanType CanBridgedSourceTy = dynamicCast.getBridgedSourceType ();
633
+ CanType CanBridgedTargetTy = dynamicCast.getBridgedTargetType ();
637
634
638
- auto BridgedSourceTy = getCastFromObjC (M, target, source);
639
- if (!BridgedSourceTy)
635
+ // If we were unable to bridge either of our source/target types, return
636
+ // nullptr.
637
+ if (!CanBridgedSourceTy || !CanBridgedTargetTy)
640
638
return nullptr ;
641
639
642
- CanType CanBridgedTargetTy = BridgedTargetTy->getCanonicalType ();
643
- CanType CanBridgedSourceTy = BridgedSourceTy->getCanonicalType ();
644
-
645
640
if (CanBridgedSourceTy == source && CanBridgedTargetTy == target) {
646
641
// Both source and target type are ObjC types.
647
642
return nullptr ;
@@ -660,19 +655,13 @@ CastOptimizer::optimizeBridgedCasts(SILDynamicCastInst dynamicCast) {
660
655
return nullptr ;
661
656
}
662
657
663
- if (CanBridgedSourceTy || CanBridgedTargetTy) {
664
- // Check what kind of conversion it is? ObjC->Swift or Swift-ObjC?
665
- if (CanBridgedTargetTy != target) {
666
- // This is an ObjC to Swift cast.
667
- return optimizeBridgedObjCToSwiftCast (
668
- Inst, isConditional, Src, Dest, source, target, BridgedSourceTy,
669
- BridgedTargetTy, SuccessBB, FailureBB);
670
- } else {
671
- // This is a Swift to ObjC cast
672
- return optimizeBridgedSwiftToObjCCast (
673
- Inst, ConsumptionKind, isConditional, Src, Dest, source, target,
674
- BridgedSourceTy, BridgedTargetTy, SuccessBB, FailureBB);
675
- }
658
+ // Check what kind of conversion it is? ObjC->Swift or Swift-ObjC?
659
+ if (CanBridgedTargetTy != target) {
660
+ // This is an ObjC to Swift cast.
661
+ return optimizeBridgedObjCToSwiftCast (dynamicCast);
662
+ } else {
663
+ // This is a Swift to ObjC cast
664
+ return optimizeBridgedSwiftToObjCCast (dynamicCast);
676
665
}
677
666
678
667
llvm_unreachable (" Unknown kind of bridging" );
0 commit comments