4545
4646using namespace swift ;
4747
48+ static SILFunction *
49+ getObjCToSwiftBridgingFunction (SILOptFunctionBuilder &funcBuilder,
50+ SILDynamicCastInst dynamicCast) {
51+ // inline constructor.
52+ auto *bridgeFuncDecl = [&]() -> FuncDecl * {
53+ auto &astContext = dynamicCast.getModule ().getASTContext ();
54+ if (dynamicCast.isConditional ()) {
55+ return astContext.getConditionallyBridgeFromObjectiveCBridgeable ();
56+ }
57+ return astContext.getForceBridgeFromObjectiveCBridgeable ();
58+ }();
59+
60+ assert (bridgeFuncDecl && " Bridging function doesn't exist?!" );
61+
62+ SILDeclRef funcDeclRef (bridgeFuncDecl, SILDeclRef::Kind::Func);
63+
64+ // Lookup a function from the stdlib.
65+ return funcBuilder.getOrCreateFunction (dynamicCast.getLocation (), funcDeclRef,
66+ ForDefinition_t::NotForDefinition);
67+ }
68+
4869// / Create a call of _forceBridgeFromObjectiveC_bridgeable or
4970// / _conditionallyBridgeFromObjectiveC_bridgeable which converts an ObjC
5071// / instance into a corresponding Swift type, conforming to
@@ -67,20 +88,9 @@ CastOptimizer::optimizeBridgedObjCToSwiftCast(SILDynamicCastInst dynamicCast) {
6788 // The conformance to _BridgedToObjectiveC is statically known.
6889 // Retrieve the bridging operation to be used if a static conformance
6990 // to _BridgedToObjectiveC can be proven.
70- FuncDecl *BridgeFuncDecl =
71- isConditional
72- ? M.getASTContext ().getConditionallyBridgeFromObjectiveCBridgeable ()
73- : M.getASTContext ().getForceBridgeFromObjectiveCBridgeable ();
74-
75- assert (BridgeFuncDecl && " _forceBridgeFromObjectiveC should exist" );
76-
77- SILDeclRef FuncDeclRef (BridgeFuncDecl, SILDeclRef::Kind::Func);
78-
79- // Lookup a function from the stdlib.
80- SILFunction *BridgedFunc = FunctionBuilder.getOrCreateFunction (
81- Loc, FuncDeclRef, ForDefinition_t::NotForDefinition);
82-
83- if (!BridgedFunc)
91+ SILFunction *bridgingFunc =
92+ getObjCToSwiftBridgingFunction (FunctionBuilder, dynamicCast);
93+ if (!bridgingFunc)
8494 return nullptr ;
8595
8696 CanType CanBridgedTy = BridgedTargetTy->getCanonicalType ();
@@ -170,9 +180,9 @@ CastOptimizer::optimizeBridgedObjCToSwiftCast(SILDynamicCastInst dynamicCast) {
170180 M.getASTContext ().getProtocol (KnownProtocolKind::ObjectiveCBridgeable);
171181 auto Conf = *M.getSwiftModule ()->lookupConformance (Target, BridgedProto);
172182
173- auto ParamTypes = BridgedFunc ->getLoweredFunctionType ()->getParameters ();
183+ auto ParamTypes = bridgingFunc ->getLoweredFunctionType ()->getParameters ();
174184
175- auto *FuncRef = Builder.createFunctionRef (Loc, BridgedFunc );
185+ auto *FuncRef = Builder.createFunctionRef (Loc, bridgingFunc );
176186
177187 auto MetaTy = MetatypeType::get (Target, MetatypeRepresentation::Thick);
178188 auto SILMetaTy = F->getTypeLowering (MetaTy).getLoweredType ();
0 commit comments