Skip to content

Commit cb75ab7

Browse files
committed
SILGen: Simplify AnyObject dispatch logic a bit
1 parent 806935c commit cb75ab7

File tree

1 file changed

+11
-65
lines changed

1 file changed

+11
-65
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 11 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,6 @@ getIndirectApplyAbstractionPattern(SILGenFunction &SGF,
6464
llvm_unreachable("bad representation");
6565
}
6666

67-
static CanType getDynamicMethodSelfType(SILGenFunction &SGF,
68-
const ArgumentSource &proto,
69-
ValueDecl *member) {
70-
if (member->isInstanceMember()) {
71-
return SGF.getASTContext().getAnyObjectType();
72-
} else {
73-
return proto.getSILSubstType(SGF).getSwiftRValueType();
74-
}
75-
}
76-
7767
/// Return the formal type for the partial-apply result type of a
7868
/// dynamic method invocation.
7969
static CanFunctionType
@@ -113,39 +103,6 @@ getPartialApplyOfDynamicMethodFormalType(SILGenModule &SGM, SILDeclRef member,
113103
return fnType;
114104
}
115105

116-
/// Replace the 'self' parameter in the given type.
117-
static CanSILFunctionType
118-
replaceSelfTypeForDynamicLookup(ASTContext &ctx,
119-
CanSILFunctionType fnType,
120-
CanType newSelfType,
121-
SILDeclRef methodName) {
122-
assert(!fnType->isCoroutine());
123-
auto oldParams = fnType->getParameters();
124-
SmallVector<SILParameterInfo, 4> newParams;
125-
newParams.append(oldParams.begin(), oldParams.end() - 1);
126-
newParams.push_back({newSelfType, oldParams.back().getConvention()});
127-
128-
// If the method returns Self, substitute AnyObject for the result type.
129-
SmallVector<SILResultInfo, 4> newResults;
130-
newResults.append(fnType->getResults().begin(), fnType->getResults().end());
131-
if (auto fnDecl = dyn_cast<FuncDecl>(methodName.getDecl())) {
132-
if (fnDecl->hasDynamicSelf()) {
133-
auto anyObjectTy = ctx.getAnyObjectType();
134-
for (auto &result : newResults) {
135-
auto newResultTy
136-
= result.getType()->replaceCovariantResultType(anyObjectTy, 0);
137-
result = result.getWithType(newResultTy->getCanonicalType());
138-
}
139-
}
140-
}
141-
142-
return SILFunctionType::get(nullptr, fnType->getExtInfo(),
143-
SILCoroutineKind::None,
144-
fnType->getCalleeConvention(), newParams, {},
145-
newResults, fnType->getOptionalErrorResult(), ctx,
146-
fnType->getWitnessMethodConformanceOrNone());
147-
}
148-
149106
/// Retrieve the type to use for a method found via dynamic lookup.
150107
static CanSILFunctionType
151108
getDynamicMethodLoweredType(SILGenFunction &SGF, SILValue v,
@@ -163,7 +120,7 @@ getDynamicMethodLoweredType(SILGenFunction &SGF, SILValue v,
163120

164121
auto methodTy = SGF.SGM.M.Types
165122
.getUncachedSILFunctionTypeForConstant(methodName, objcFormalTy);
166-
return replaceSelfTypeForDynamicLookup(ctx, methodTy, selfTy, methodName);
123+
return methodTy;
167124
}
168125

169126
/// Check if we can perform a dynamic dispatch on a super method call.
@@ -400,13 +357,11 @@ class Callee {
400357
}
401358
static Callee forDynamic(SILGenFunction &SGF, ArgumentSource &&arg,
402359
SILDeclRef c, const SubstitutionList &constantSubs,
403-
CanAnyFunctionType partialSubstFormalType,
360+
CanAnyFunctionType substFormalType,
404361
SubstitutionList subs, SILLocation l) {
405362
auto &ci = SGF.getConstantInfo(c);
406363
AbstractionPattern origFormalType = ci.FormalPattern;
407364

408-
auto selfType = getDynamicMethodSelfType(SGF, arg, c.getDecl());
409-
410365
// Replace the original self type with the partially-applied subst type.
411366
auto origFormalFnType = cast<AnyFunctionType>(origFormalType.getType());
412367
if (auto genericFnType = dyn_cast<GenericFunctionType>(origFormalFnType)) {
@@ -420,16 +375,8 @@ class Callee {
420375
cast<FunctionType>(genericFnType->substGenericArgs(constantSubs)
421376
->getCanonicalType());
422377
}
423-
origFormalFnType = CanFunctionType::get(selfType,
424-
origFormalFnType.getResult(),
425-
origFormalFnType->getExtInfo());
426378
origFormalType.rewriteType(CanGenericSignature(), origFormalFnType);
427379

428-
// Add the self type clause to the partially-applied subst type.
429-
auto substFormalType = CanFunctionType::get(selfType,
430-
partialSubstFormalType,
431-
origFormalFnType->getExtInfo());
432-
433380
return Callee(Kind::DynamicMethod, SGF, std::move(arg), c, origFormalType,
434381
substFormalType, subs, l);
435382
}
@@ -520,22 +467,17 @@ class Callee {
520467
return Constant;
521468
}
522469

523-
SILType getDynamicMethodType(SILGenFunction &SGF) const {
470+
SILType getDynamicMethodType(SILModule &M) const {
524471
// Lower the substituted type from the AST, which should have any generic
525472
// parameters in the original signature erased to their upper bounds.
526473
auto substFormalType = getSubstFormalType();
527474
auto objcFormalType = substFormalType.withExtInfo(
528475
substFormalType->getExtInfo().withSILRepresentation(
529476
SILFunctionTypeRepresentation::ObjCMethod));
530-
auto fnType = SGF.SGM.M.Types.getUncachedSILFunctionTypeForConstant(
477+
auto fnType = M.Types.getUncachedSILFunctionTypeForConstant(
531478
Constant, objcFormalType);
532479

533-
auto closureType = replaceSelfTypeForDynamicLookup(
534-
SGF.getASTContext(), fnType,
535-
SelfValue.getValue().getSILSubstType(SGF).getSwiftRValueType(),
536-
Constant);
537-
538-
return SILType::getPrimitiveObjectType(closureType);
480+
return SILType::getPrimitiveObjectType(fnType);
539481
}
540482

541483
ManagedValue getFnValue(SILGenFunction &SGF, bool isCurried) const & {
@@ -623,7 +565,7 @@ class Callee {
623565
}
624566
case Kind::DynamicMethod: {
625567
assert(constant->isForeign);
626-
auto closureType = getDynamicMethodType(SGF);
568+
auto closureType = getDynamicMethodType(SGF.SGM.M);
627569

628570
Scope S(SGF, Loc);
629571
ManagedValue self =
@@ -677,7 +619,7 @@ class Callee {
677619
return createCalleeTypeInfo(SGF, constant, constantInfo.getSILType());
678620
}
679621
case Kind::DynamicMethod: {
680-
auto formalType = getDynamicMethodType(SGF);
622+
auto formalType = getDynamicMethodType(SGF.SGM.M);
681623
return createCalleeTypeInfo(SGF, constant, formalType);
682624
}
683625
}
@@ -1463,6 +1405,10 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
14631405
auto substFormalType =
14641406
cast<FunctionType>(dynamicMemberRef->getType()->getCanonicalType()
14651407
.getAnyOptionalObjectType());
1408+
substFormalType = CanFunctionType::get(
1409+
dynamicMemberRef->getBase()->getType()->getCanonicalType(),
1410+
substFormalType, AnyFunctionType::ExtInfo());
1411+
14661412
setCallee(Callee::forDynamic(SGF, baseArgSource.delayedBorrow(SGF),
14671413
member, memberRef.getSubstitutions(),
14681414
substFormalType, {}, e));

0 commit comments

Comments
 (0)