Skip to content

Commit b8a7102

Browse files
committed
[cast-opt] Refactor finding the ObjectiveCBridgeable protocol substitutions into a helper function.
1 parent c57a032 commit b8a7102

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ getObjCToSwiftBridgingFunction(SILOptFunctionBuilder &funcBuilder,
6666
ForDefinition_t::NotForDefinition);
6767
}
6868

69+
static SubstitutionMap lookupBridgeToObjCProtocolSubs(SILModule &mod,
70+
CanType target) {
71+
auto bridgedProto =
72+
mod.getASTContext().getProtocol(KnownProtocolKind::ObjectiveCBridgeable);
73+
auto conf = *mod.getSwiftModule()->lookupConformance(target, bridgedProto);
74+
return SubstitutionMap::getProtocolSubstitutions(conf.getRequirement(),
75+
target, conf);
76+
}
77+
6978
/// Create a call of _forceBridgeFromObjectiveC_bridgeable or
7079
/// _conditionallyBridgeFromObjectiveC_bridgeable which converts an ObjC
7180
/// instance into a corresponding Swift type, conforming to
@@ -181,17 +190,8 @@ CastOptimizer::optimizeBridgedObjCToSwiftCast(SILDynamicCastInst dynamicCast) {
181190
// Now emit the a cast from the casted ObjC object into a target type.
182191
// This is done by means of calling _forceBridgeFromObjectiveC or
183192
// _conditionallyBridgeFromObjectiveC_bridgeable from the Target type.
184-
// Lookup the required function in the Target type.
185-
186-
// Lookup the _ObjectiveCBridgeable protocol.
187-
auto BridgedProto =
188-
mod.getASTContext().getProtocol(KnownProtocolKind::ObjectiveCBridgeable);
189-
auto Conf = *mod.getSwiftModule()->lookupConformance(target, BridgedProto);
190-
// Add substitutions
191-
auto SubMap = SubstitutionMap::getProtocolSubstitutions(Conf.getRequirement(),
192-
target, Conf);
193-
194-
auto *FuncRef = Builder.createFunctionRef(Loc, bridgingFunc);
193+
auto *funcRef = Builder.createFunctionRef(Loc, bridgingFunc);
194+
SubstitutionMap subMap = lookupBridgeToObjCProtocolSubs(mod, target);
195195

196196
auto MetaTy = MetatypeType::get(target, MetatypeRepresentation::Thick);
197197
auto SILMetaTy = F->getTypeLowering(MetaTy).getLoweredType();
@@ -221,7 +221,7 @@ CastOptimizer::optimizeBridgedObjCToSwiftCast(SILDynamicCastInst dynamicCast) {
221221
Args.push_back(srcOp);
222222
Args.push_back(MetaTyVal);
223223

224-
auto *AI = Builder.createApply(Loc, FuncRef, SubMap, Args, false);
224+
auto *AI = Builder.createApply(Loc, funcRef, subMap, Args, false);
225225

226226
// If we have guaranteed normal arguments, insert the destroy.
227227
//

0 commit comments

Comments
 (0)