Skip to content

Commit d00dcaa

Browse files
authored
Merge pull request #23090 from gottesmm/pr-937b49293145ea525de4ee0dd53dba98c7cefd0f
[cast-opt] Extract out the retrieval of the ObjC -> Swift bridging fu…
2 parents f70c43a + aefbc49 commit d00dcaa

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@
4545

4646
using 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

Comments
 (0)