Skip to content

Commit 6bfffae

Browse files
committed
SILGen: More uniform construction of CallSites
This introduces a bit of repetition for now, but I'm going to factor it out a different way later.
1 parent 9e8acbe commit 6bfffae

File tree

2 files changed

+89
-81
lines changed

2 files changed

+89
-81
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 88 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,10 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
826826

827827
/// The lvalue or rvalue representing the argument source of self.
828828
ArgumentSource selfParam;
829+
830+
/// The method type with self stripped off (NOT the type of the self value).
829831
Type selfType;
832+
830833
std::vector<ApplyExpr*> callSites;
831834
Expr *sideEffect = nullptr;
832835

@@ -4024,51 +4027,13 @@ class CallSite {
40244027
bool Throws;
40254028

40264029
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-
40414030
CallSite(SILLocation loc, PreparedArguments &&args, CanType resultType,
40424031
bool throws)
40434032
: Loc(loc), SubstResultType(resultType), Args(std::move(args)),
40444033
Throws(throws) {
40454034
assert(Args.isValid());
40464035
}
40474036

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-
40724037
/// Return the substituted, unlowered AST parameter types of the argument.
40734038
ArrayRef<AnyFunctionType::Param> getParams() const { return Args.getParams(); }
40744039

@@ -4139,7 +4104,7 @@ class CallEmission {
41394104

41404105
/// A factory method for decomposing the apply expr \p e into a call
41414106
/// emission.
4142-
static CallEmission forApplyExpr(SILGenFunction &SGF, Expr *e);
4107+
static CallEmission forApplyExpr(SILGenFunction &SGF, ApplyExpr *e);
41434108

41444109
/// Add a level of function application by passing in its possibly
41454110
/// unevaluated arguments and their formal type.
@@ -4161,6 +4126,17 @@ class CallEmission {
41614126
addCallSite(CallSite{std::forward<T>(args)...});
41624127
}
41634128

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+
41644140
/// Is this a fully-applied enum element constructor call?
41654141
bool isEnumElementConstructor() {
41664142
return (callee.kind == Callee::Kind::EnumElement &&
@@ -4851,7 +4827,7 @@ RValue CallEmission::applyRemainingCallSites(RValue &&result,
48514827
return std::move(result);
48524828
}
48534829

4854-
CallEmission CallEmission::forApplyExpr(SILGenFunction &SGF, Expr *e) {
4830+
CallEmission CallEmission::forApplyExpr(SILGenFunction &SGF, ApplyExpr *e) {
48554831
// Set up writebacks for the call(s).
48564832
FormalEvaluationScope writebacks(SGF);
48574833

@@ -4871,16 +4847,35 @@ CallEmission CallEmission::forApplyExpr(SILGenFunction &SGF, Expr *e) {
48714847

48724848
// Apply 'self' if provided.
48734849
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());
48774858
}
48784859

48794860
// Apply arguments from call sites, innermost to outermost.
48804861
for (auto site = apply.callSites.rbegin(), end = apply.callSites.rend();
48814862
site != end;
48824863
++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());
48844879
}
48854880

48864881
return emission;
@@ -5356,7 +5351,7 @@ ManagedValue SILGenFunction::emitInjectEnum(SILLocation loc,
53565351
});
53575352
}
53585353

5359-
RValue SILGenFunction::emitApplyExpr(Expr *e, SGFContext c) {
5354+
RValue SILGenFunction::emitApplyExpr(ApplyExpr *e, SGFContext c) {
53605355
CallEmission emission = CallEmission::forApplyExpr(*this, e);
53615356
return emission.apply(c);
53625357
}
@@ -5475,36 +5470,37 @@ RValue SILGenFunction::emitApplyAllocatingInitializer(SILLocation loc,
54755470

54765471
auto substFormalType = callee->getSubstFormalType();
54775472

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-
54905473
// Form the call emission.
54915474
CallEmission emission(*this, std::move(*callee), std::move(writebackScope));
54925475

5476+
auto methodType = cast<FunctionType>(substFormalType.getResult());
5477+
auto resultType = methodType.getResult();
5478+
54935479
// 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);
55025488

55035489
// Arguments.
5490+
55045491
// 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+
}
55085504

55095505
// Perform the call.
55105506
RValue result = emission.apply(requiresDowncast ? SGFContext() : C);
@@ -6113,15 +6109,18 @@ RValue SILGenFunction::emitGetAccessor(SILLocation loc, SILDeclRef get,
61136109
CallEmission emission(*this, std::move(getter), std::move(writebackScope));
61146110
// Self ->
61156111
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());
61186115
accessType = cast<AnyFunctionType>(accessType.getResult());
61196116
}
61206117
// Index or () if none.
61216118
if (subscriptIndices.isNull())
61226119
subscriptIndices.emplaceEmptyArgumentList(*this);
61236120

6124-
emission.addCallSite(loc, std::move(subscriptIndices), accessType);
6121+
emission.addCallSite(loc, std::move(subscriptIndices),
6122+
accessType.getResult(),
6123+
accessType->throws());
61256124

61266125
// T
61276126
return emission.apply(c);
@@ -6146,8 +6145,9 @@ void SILGenFunction::emitSetAccessor(SILLocation loc, SILDeclRef set,
61466145
CallEmission emission(*this, std::move(setter), std::move(writebackScope));
61476146
// Self ->
61486147
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());
61516151
accessType = cast<AnyFunctionType>(accessType.getResult());
61526152
}
61536153

@@ -6163,7 +6163,9 @@ void SILGenFunction::emitSetAccessor(SILLocation loc, SILDeclRef set,
61636163
}
61646164
}
61656165
assert(values.isValid());
6166-
emission.addCallSite(loc, std::move(values), accessType);
6166+
emission.addCallSite(loc, std::move(values),
6167+
accessType.getResult(),
6168+
accessType->throws());
61676169
// ()
61686170
emission.apply();
61696171
}
@@ -6188,15 +6190,18 @@ ManagedValue SILGenFunction::emitAddressorAccessor(
61886190
CallEmission emission(*this, std::move(callee), std::move(writebackScope));
61896191
// Self ->
61906192
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());
61936196
accessType = cast<AnyFunctionType>(accessType.getResult());
61946197
}
61956198
// Index or () if none.
61966199
if (subscriptIndices.isNull())
61976200
subscriptIndices.emplaceEmptyArgumentList(*this);
61986201

6199-
emission.addCallSite(loc, std::move(subscriptIndices), accessType);
6202+
emission.addCallSite(loc, std::move(subscriptIndices),
6203+
accessType.getResult(),
6204+
accessType->throws());
62006205

62016206
// Unsafe{Mutable}Pointer<T> or
62026207
// (Unsafe{Mutable}Pointer<T>, Builtin.UnknownPointer) or
@@ -6250,15 +6255,18 @@ SILGenFunction::emitCoroutineAccessor(SILLocation loc, SILDeclRef accessor,
62506255
CallEmission emission(*this, std::move(callee), std::move(writebackScope));
62516256
// Self ->
62526257
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());
62556261
accessType = cast<AnyFunctionType>(accessType.getResult());
62566262
}
62576263
// Index or () if none.
62586264
if (subscriptIndices.isNull())
62596265
subscriptIndices.emplaceEmptyArgumentList(*this);
62606266

6261-
emission.addCallSite(loc, std::move(subscriptIndices), accessType);
6267+
emission.addCallSite(loc, std::move(subscriptIndices),
6268+
accessType.getResult(),
6269+
accessType->throws());
62626270

62636271
auto endApplyHandle = emission.applyCoroutine(yields);
62646272

lib/SILGen/SILGenFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
14131413
// Helpers for emitting ApplyExpr chains.
14141414
//
14151415

1416-
RValue emitApplyExpr(Expr *e, SGFContext c);
1416+
RValue emitApplyExpr(ApplyExpr *e, SGFContext c);
14171417

14181418
/// Emit a function application, assuming that the arguments have been
14191419
/// lowered appropriately for the abstraction level but that the

0 commit comments

Comments
 (0)