@@ -826,7 +826,10 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
826
826
827
827
// / The lvalue or rvalue representing the argument source of self.
828
828
ArgumentSource selfParam;
829
+
830
+ // / The method type with self stripped off (NOT the type of the self value).
829
831
Type selfType;
832
+
830
833
std::vector<ApplyExpr*> callSites;
831
834
Expr *sideEffect = nullptr ;
832
835
@@ -4024,51 +4027,13 @@ class CallSite {
4024
4027
bool Throws;
4025
4028
4026
4029
public:
4027
- CallSite (ApplyExpr *apply)
4028
- : Loc(apply), SubstResultType(apply->getType ()->getCanonicalType()),
4029
- Throws(apply->throws ()) {
4030
- Expr *arg = apply->getArg ();
4031
-
4032
- SmallVector<AnyFunctionType::Param, 8 > params;
4033
- AnyFunctionType::decomposeInput (arg->getType (), params);
4034
-
4035
- // FIXME: Split up the argument expression here instead of passing
4036
- // scalar=true.
4037
- Args.emplace (params, /* scalar*/ true );
4038
- Args.addArbitrary (arg);
4039
- }
4040
-
4041
4030
CallSite (SILLocation loc, PreparedArguments &&args, CanType resultType,
4042
4031
bool throws)
4043
4032
: Loc(loc), SubstResultType(resultType), Args(std::move(args)),
4044
4033
Throws (throws) {
4045
4034
assert (Args.isValid ());
4046
4035
}
4047
4036
4048
- // FIXME: Remove this entry point or refactor it so that isScalar is always
4049
- // false.
4050
- CallSite (SILLocation loc, ArgumentSource &&value, bool isScalar,
4051
- CanType resultType, bool throws)
4052
- : Loc(loc), SubstResultType(resultType), Throws(throws) {
4053
-
4054
- auto type = (value.hasLValueType ()
4055
- ? CanInOutType::get (value.getSubstRValueType ())
4056
- : value.getSubstRValueType ());
4057
- SmallVector<AnyFunctionType::Param, 8 > params;
4058
- AnyFunctionType::decomposeInput (type, params);
4059
- Args.emplace (params, isScalar);
4060
- Args.addArbitrary (std::move (value));
4061
- assert (Args.isValid ());
4062
- }
4063
-
4064
- CallSite (SILLocation loc, ArgumentSource &&value, bool isScalar,
4065
- CanAnyFunctionType fnType)
4066
- : CallSite(loc, std::move(value), isScalar,
4067
- fnType.getResult(), fnType->throws()) {}
4068
-
4069
- CallSite (SILLocation loc, PreparedArguments &&args, CanAnyFunctionType fnType)
4070
- : CallSite(loc, std::move(args), fnType.getResult(), fnType->throws()) {}
4071
-
4072
4037
// / Return the substituted, unlowered AST parameter types of the argument.
4073
4038
ArrayRef<AnyFunctionType::Param> getParams () const { return Args.getParams (); }
4074
4039
@@ -4139,7 +4104,7 @@ class CallEmission {
4139
4104
4140
4105
// / A factory method for decomposing the apply expr \p e into a call
4141
4106
// / emission.
4142
- static CallEmission forApplyExpr (SILGenFunction &SGF, Expr *e);
4107
+ static CallEmission forApplyExpr (SILGenFunction &SGF, ApplyExpr *e);
4143
4108
4144
4109
// / Add a level of function application by passing in its possibly
4145
4110
// / unevaluated arguments and their formal type.
@@ -4161,6 +4126,17 @@ class CallEmission {
4161
4126
addCallSite (CallSite{std::forward<T>(args)...});
4162
4127
}
4163
4128
4129
+ void addSelfParam (SILLocation loc,
4130
+ ArgumentSource &&selfArg,
4131
+ AnyFunctionType::Param selfParam,
4132
+ CanType methodType) {
4133
+ PreparedArguments preparedSelf ({selfParam}, /* scalar*/ false );
4134
+ preparedSelf.addArbitrary (std::move (selfArg));
4135
+
4136
+ addCallSite (loc, std::move (preparedSelf), methodType,
4137
+ /* throws*/ false );
4138
+ }
4139
+
4164
4140
// / Is this a fully-applied enum element constructor call?
4165
4141
bool isEnumElementConstructor () {
4166
4142
return (callee.kind == Callee::Kind::EnumElement &&
@@ -4851,7 +4827,7 @@ RValue CallEmission::applyRemainingCallSites(RValue &&result,
4851
4827
return std::move (result);
4852
4828
}
4853
4829
4854
- CallEmission CallEmission::forApplyExpr (SILGenFunction &SGF, Expr *e) {
4830
+ CallEmission CallEmission::forApplyExpr (SILGenFunction &SGF, ApplyExpr *e) {
4855
4831
// Set up writebacks for the call(s).
4856
4832
FormalEvaluationScope writebacks (SGF);
4857
4833
@@ -4871,16 +4847,35 @@ CallEmission CallEmission::forApplyExpr(SILGenFunction &SGF, Expr *e) {
4871
4847
4872
4848
// Apply 'self' if provided.
4873
4849
if (apply.selfParam ) {
4874
- emission.addCallSite (RegularLocation (e),
4875
- std::move (apply.selfParam ), /* scalar*/ false ,
4876
- apply.selfType ->getCanonicalType (), /* throws*/ false );
4850
+ AnyFunctionType::Param selfParam (
4851
+ apply.selfParam .getSubstRValueType (),
4852
+ Identifier (),
4853
+ apply.selfParam .isLValue ()
4854
+ ? ParameterTypeFlags ().withInOut (true )
4855
+ : ParameterTypeFlags ());
4856
+ emission.addSelfParam (e, std::move (apply.selfParam ), selfParam,
4857
+ apply.selfType ->getCanonicalType ());
4877
4858
}
4878
4859
4879
4860
// Apply arguments from call sites, innermost to outermost.
4880
4861
for (auto site = apply.callSites .rbegin (), end = apply.callSites .rend ();
4881
4862
site != end;
4882
4863
++site) {
4883
- emission.addCallSite (*site);
4864
+ ApplyExpr *apply = *site;
4865
+
4866
+ Expr *arg = apply->getArg ();
4867
+
4868
+ SmallVector<AnyFunctionType::Param, 8 > params;
4869
+ AnyFunctionType::decomposeInput (arg->getType (), params);
4870
+
4871
+ // FIXME: Split up the argument expression here instead of passing
4872
+ // scalar=true.
4873
+ PreparedArguments preparedArgs (params, /* scalar*/ true );
4874
+ preparedArgs.addArbitrary (arg);
4875
+
4876
+ emission.addCallSite (apply, std::move (preparedArgs),
4877
+ apply->getType ()->getCanonicalType (),
4878
+ apply->throws ());
4884
4879
}
4885
4880
4886
4881
return emission;
@@ -5356,7 +5351,7 @@ ManagedValue SILGenFunction::emitInjectEnum(SILLocation loc,
5356
5351
});
5357
5352
}
5358
5353
5359
- RValue SILGenFunction::emitApplyExpr (Expr *e, SGFContext c) {
5354
+ RValue SILGenFunction::emitApplyExpr (ApplyExpr *e, SGFContext c) {
5360
5355
CallEmission emission = CallEmission::forApplyExpr (*this , e);
5361
5356
return emission.apply (c);
5362
5357
}
@@ -5475,36 +5470,37 @@ RValue SILGenFunction::emitApplyAllocatingInitializer(SILLocation loc,
5475
5470
5476
5471
auto substFormalType = callee->getSubstFormalType ();
5477
5472
5478
- // For an inheritable initializer, determine whether we'll need to adjust the
5479
- // result type.
5480
- bool requiresDowncast = false ;
5481
- if (ctor->isRequired () && overriddenSelfType) {
5482
- CanType substResultType = substFormalType;
5483
- substResultType = cast<FunctionType>(substResultType).getResult ();
5484
- substResultType = cast<FunctionType>(substResultType).getResult ();
5485
-
5486
- if (!substResultType->isEqual (overriddenSelfType))
5487
- requiresDowncast = true ;
5488
- }
5489
-
5490
5473
// Form the call emission.
5491
5474
CallEmission emission (*this , std::move (*callee), std::move (writebackScope));
5492
5475
5476
+ auto methodType = cast<FunctionType>(substFormalType.getResult ());
5477
+ auto resultType = methodType.getResult ();
5478
+
5493
5479
// Self metatype.
5494
- emission.addCallSite (loc,
5495
- ArgumentSource (loc,
5496
- RValue (*this , loc,
5497
- selfMetaVal.getType ()
5498
- .getASTType (),
5499
- std::move (selfMetaVal))),
5500
- /* scalar */ false ,
5501
- substFormalType );
5480
+ emission.addSelfParam (loc,
5481
+ ArgumentSource (loc,
5482
+ RValue (*this , loc,
5483
+ selfMetaVal.getType ()
5484
+ .getASTType (),
5485
+ std::move (selfMetaVal))),
5486
+ substFormalType. getParams ()[ 0 ] ,
5487
+ methodType );
5502
5488
5503
5489
// Arguments.
5490
+
5504
5491
// FIXME: Rework this so that scalar=false.
5505
- emission.addCallSite (loc,
5506
- ArgumentSource (loc, std::move (args)), /* scalar*/ true ,
5507
- cast<FunctionType>(substFormalType.getResult ()));
5492
+ PreparedArguments preparedArgs (methodType->getParams (), /* scalar*/ true );
5493
+ preparedArgs.addArbitrary (ArgumentSource (loc, std::move (args)));
5494
+
5495
+ emission.addCallSite (loc, std::move (preparedArgs), resultType, /* throws*/ false );
5496
+
5497
+ // For an inheritable initializer, determine whether we'll need to adjust the
5498
+ // result type.
5499
+ bool requiresDowncast = false ;
5500
+ if (ctor->isRequired () && overriddenSelfType) {
5501
+ if (!resultType->isEqual (overriddenSelfType))
5502
+ requiresDowncast = true ;
5503
+ }
5508
5504
5509
5505
// Perform the call.
5510
5506
RValue result = emission.apply (requiresDowncast ? SGFContext () : C);
@@ -6113,15 +6109,18 @@ RValue SILGenFunction::emitGetAccessor(SILLocation loc, SILDeclRef get,
6113
6109
CallEmission emission (*this , std::move (getter), std::move (writebackScope));
6114
6110
// Self ->
6115
6111
if (hasSelf) {
6116
- emission.addCallSite (loc, std::move (selfValue), /* scalar*/ false ,
6117
- accessType);
6112
+ emission.addSelfParam (loc, std::move (selfValue),
6113
+ accessType.getParams ()[0 ],
6114
+ accessType.getResult ());
6118
6115
accessType = cast<AnyFunctionType>(accessType.getResult ());
6119
6116
}
6120
6117
// Index or () if none.
6121
6118
if (subscriptIndices.isNull ())
6122
6119
subscriptIndices.emplaceEmptyArgumentList (*this );
6123
6120
6124
- emission.addCallSite (loc, std::move (subscriptIndices), accessType);
6121
+ emission.addCallSite (loc, std::move (subscriptIndices),
6122
+ accessType.getResult (),
6123
+ accessType->throws ());
6125
6124
6126
6125
// T
6127
6126
return emission.apply (c);
@@ -6146,8 +6145,9 @@ void SILGenFunction::emitSetAccessor(SILLocation loc, SILDeclRef set,
6146
6145
CallEmission emission (*this , std::move (setter), std::move (writebackScope));
6147
6146
// Self ->
6148
6147
if (hasSelf) {
6149
- emission.addCallSite (loc, std::move (selfValue), /* scalar*/ false ,
6150
- accessType);
6148
+ emission.addSelfParam (loc, std::move (selfValue),
6149
+ accessType.getParams ()[0 ],
6150
+ accessType.getResult ());
6151
6151
accessType = cast<AnyFunctionType>(accessType.getResult ());
6152
6152
}
6153
6153
@@ -6163,7 +6163,9 @@ void SILGenFunction::emitSetAccessor(SILLocation loc, SILDeclRef set,
6163
6163
}
6164
6164
}
6165
6165
assert (values.isValid ());
6166
- emission.addCallSite (loc, std::move (values), accessType);
6166
+ emission.addCallSite (loc, std::move (values),
6167
+ accessType.getResult (),
6168
+ accessType->throws ());
6167
6169
// ()
6168
6170
emission.apply ();
6169
6171
}
@@ -6188,15 +6190,18 @@ ManagedValue SILGenFunction::emitAddressorAccessor(
6188
6190
CallEmission emission (*this , std::move (callee), std::move (writebackScope));
6189
6191
// Self ->
6190
6192
if (hasSelf) {
6191
- emission.addCallSite (loc, std::move (selfValue), /* scalar*/ false ,
6192
- accessType);
6193
+ emission.addSelfParam (loc, std::move (selfValue),
6194
+ accessType.getParams ()[0 ],
6195
+ accessType.getResult ());
6193
6196
accessType = cast<AnyFunctionType>(accessType.getResult ());
6194
6197
}
6195
6198
// Index or () if none.
6196
6199
if (subscriptIndices.isNull ())
6197
6200
subscriptIndices.emplaceEmptyArgumentList (*this );
6198
6201
6199
- emission.addCallSite (loc, std::move (subscriptIndices), accessType);
6202
+ emission.addCallSite (loc, std::move (subscriptIndices),
6203
+ accessType.getResult (),
6204
+ accessType->throws ());
6200
6205
6201
6206
// Unsafe{Mutable}Pointer<T> or
6202
6207
// (Unsafe{Mutable}Pointer<T>, Builtin.UnknownPointer) or
@@ -6250,15 +6255,18 @@ SILGenFunction::emitCoroutineAccessor(SILLocation loc, SILDeclRef accessor,
6250
6255
CallEmission emission (*this , std::move (callee), std::move (writebackScope));
6251
6256
// Self ->
6252
6257
if (hasSelf) {
6253
- emission.addCallSite (loc, std::move (selfValue), /* scalar*/ false ,
6254
- accessType);
6258
+ emission.addSelfParam (loc, std::move (selfValue),
6259
+ accessType.getParams ()[0 ],
6260
+ accessType.getResult ());
6255
6261
accessType = cast<AnyFunctionType>(accessType.getResult ());
6256
6262
}
6257
6263
// Index or () if none.
6258
6264
if (subscriptIndices.isNull ())
6259
6265
subscriptIndices.emplaceEmptyArgumentList (*this );
6260
6266
6261
- emission.addCallSite (loc, std::move (subscriptIndices), accessType);
6267
+ emission.addCallSite (loc, std::move (subscriptIndices),
6268
+ accessType.getResult (),
6269
+ accessType->throws ());
6262
6270
6263
6271
auto endApplyHandle = emission.applyCoroutine (yields);
6264
6272
0 commit comments