@@ -64,16 +64,6 @@ getIndirectApplyAbstractionPattern(SILGenFunction &SGF,
64
64
llvm_unreachable (" bad representation" );
65
65
}
66
66
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
-
77
67
// / Return the formal type for the partial-apply result type of a
78
68
// / dynamic method invocation.
79
69
static CanFunctionType
@@ -113,39 +103,6 @@ getPartialApplyOfDynamicMethodFormalType(SILGenModule &SGM, SILDeclRef member,
113
103
return fnType;
114
104
}
115
105
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
-
149
106
// / Retrieve the type to use for a method found via dynamic lookup.
150
107
static CanSILFunctionType
151
108
getDynamicMethodLoweredType (SILGenFunction &SGF, SILValue v,
@@ -163,7 +120,7 @@ getDynamicMethodLoweredType(SILGenFunction &SGF, SILValue v,
163
120
164
121
auto methodTy = SGF.SGM .M .Types
165
122
.getUncachedSILFunctionTypeForConstant (methodName, objcFormalTy);
166
- return replaceSelfTypeForDynamicLookup (ctx, methodTy, selfTy, methodName) ;
123
+ return methodTy;
167
124
}
168
125
169
126
// / Check if we can perform a dynamic dispatch on a super method call.
@@ -400,13 +357,11 @@ class Callee {
400
357
}
401
358
static Callee forDynamic (SILGenFunction &SGF, ArgumentSource &&arg,
402
359
SILDeclRef c, const SubstitutionList &constantSubs,
403
- CanAnyFunctionType partialSubstFormalType ,
360
+ CanAnyFunctionType substFormalType ,
404
361
SubstitutionList subs, SILLocation l) {
405
362
auto &ci = SGF.getConstantInfo (c);
406
363
AbstractionPattern origFormalType = ci.FormalPattern ;
407
364
408
- auto selfType = getDynamicMethodSelfType (SGF, arg, c.getDecl ());
409
-
410
365
// Replace the original self type with the partially-applied subst type.
411
366
auto origFormalFnType = cast<AnyFunctionType>(origFormalType.getType ());
412
367
if (auto genericFnType = dyn_cast<GenericFunctionType>(origFormalFnType)) {
@@ -420,16 +375,8 @@ class Callee {
420
375
cast<FunctionType>(genericFnType->substGenericArgs (constantSubs)
421
376
->getCanonicalType ());
422
377
}
423
- origFormalFnType = CanFunctionType::get (selfType,
424
- origFormalFnType.getResult (),
425
- origFormalFnType->getExtInfo ());
426
378
origFormalType.rewriteType (CanGenericSignature (), origFormalFnType);
427
379
428
- // Add the self type clause to the partially-applied subst type.
429
- auto substFormalType = CanFunctionType::get (selfType,
430
- partialSubstFormalType,
431
- origFormalFnType->getExtInfo ());
432
-
433
380
return Callee (Kind::DynamicMethod, SGF, std::move (arg), c, origFormalType,
434
381
substFormalType, subs, l);
435
382
}
@@ -520,22 +467,17 @@ class Callee {
520
467
return Constant;
521
468
}
522
469
523
- SILType getDynamicMethodType (SILGenFunction &SGF ) const {
470
+ SILType getDynamicMethodType (SILModule &M ) const {
524
471
// Lower the substituted type from the AST, which should have any generic
525
472
// parameters in the original signature erased to their upper bounds.
526
473
auto substFormalType = getSubstFormalType ();
527
474
auto objcFormalType = substFormalType.withExtInfo (
528
475
substFormalType->getExtInfo ().withSILRepresentation (
529
476
SILFunctionTypeRepresentation::ObjCMethod));
530
- auto fnType = SGF. SGM . M .Types .getUncachedSILFunctionTypeForConstant (
477
+ auto fnType = M.Types .getUncachedSILFunctionTypeForConstant (
531
478
Constant, objcFormalType);
532
479
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);
539
481
}
540
482
541
483
ManagedValue getFnValue (SILGenFunction &SGF, bool isCurried) const & {
@@ -623,7 +565,7 @@ class Callee {
623
565
}
624
566
case Kind::DynamicMethod: {
625
567
assert (constant->isForeign );
626
- auto closureType = getDynamicMethodType (SGF);
568
+ auto closureType = getDynamicMethodType (SGF. SGM . M );
627
569
628
570
Scope S (SGF, Loc);
629
571
ManagedValue self =
@@ -677,7 +619,7 @@ class Callee {
677
619
return createCalleeTypeInfo (SGF, constant, constantInfo.getSILType ());
678
620
}
679
621
case Kind::DynamicMethod: {
680
- auto formalType = getDynamicMethodType (SGF);
622
+ auto formalType = getDynamicMethodType (SGF. SGM . M );
681
623
return createCalleeTypeInfo (SGF, constant, formalType);
682
624
}
683
625
}
@@ -1463,6 +1405,10 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
1463
1405
auto substFormalType =
1464
1406
cast<FunctionType>(dynamicMemberRef->getType ()->getCanonicalType ()
1465
1407
.getAnyOptionalObjectType ());
1408
+ substFormalType = CanFunctionType::get (
1409
+ dynamicMemberRef->getBase ()->getType ()->getCanonicalType (),
1410
+ substFormalType, AnyFunctionType::ExtInfo ());
1411
+
1466
1412
setCallee (Callee::forDynamic (SGF, baseArgSource.delayedBorrow (SGF),
1467
1413
member, memberRef.getSubstitutions (),
1468
1414
substFormalType, {}, e));
@@ -4128,8 +4074,7 @@ CallEmission::applyNormalCall(SGFContext C) {
4128
4074
// In C language modes, substitute the type of the AbstractionPattern
4129
4075
// so that we won't see type parameters down when we try to form bridging
4130
4076
// conversions.
4131
- CanSILFunctionType calleeFnTy = mv.getType ().castTo <SILFunctionType>();
4132
- if (calleeFnTy->getLanguage () == SILFunctionLanguage::C) {
4077
+ if (calleeTypeInfo.substFnType ->getLanguage () == SILFunctionLanguage::C) {
4133
4078
if (auto genericFnType =
4134
4079
dyn_cast<GenericFunctionType>(origFormalType.getType ())) {
4135
4080
auto fnType = genericFnType->substGenericArgs (callee.getSubstitutions ());
0 commit comments