Skip to content

Commit cead30d

Browse files
committed
[SIL] Adopt ArgumentList
1 parent 0574da3 commit cead30d

File tree

10 files changed

+159
-158
lines changed

10 files changed

+159
-158
lines changed

lib/SILGen/ArgumentSource.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,11 @@ void ArgumentSource::dump(raw_ostream &out, unsigned indent) const {
251251
llvm_unreachable("bad kind");
252252
}
253253

254-
PreparedArguments::PreparedArguments(
255-
ArrayRef<AnyFunctionType::Param> params,
256-
Expr *arg) : PreparedArguments(params) {
257-
if (auto *PE = dyn_cast<ParenExpr>(arg))
258-
addArbitrary(PE->getSubExpr());
259-
else if (auto *TE = dyn_cast<TupleExpr>(arg)) {
260-
for (auto *elt : TE->getElements())
261-
addArbitrary(elt);
262-
} else {
263-
// FIXME: All ApplyExprs should have a ParenExpr or TupleExpr as their argument
264-
addArbitrary(arg);
265-
}
254+
PreparedArguments::PreparedArguments(ArrayRef<AnyFunctionType::Param> params,
255+
ArgumentList *argList)
256+
: PreparedArguments(params) {
257+
for (auto arg : *argList)
258+
addArbitrary(arg.getExpr());
266259
}
267260

268261
PreparedArguments

lib/SILGen/ArgumentSource.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,9 @@ class PreparedArguments {
296296
emplace(params);
297297
}
298298

299-
// Decompse an argument list expression.
300-
PreparedArguments(ArrayRef<AnyFunctionType::Param> params, Expr *arg);
299+
// Create from an argument list.
300+
PreparedArguments(ArrayRef<AnyFunctionType::Param> params,
301+
ArgumentList *argList);
301302

302303
// Move-only.
303304
PreparedArguments(const PreparedArguments &) = delete;

lib/SILGen/LValue.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class LogicalPathComponent : public PathComponent {
279279
AbstractStorageDecl *Storage;
280280
bool IsSuper;
281281
const PreparedArguments *Indices;
282-
Expr *IndexExprForDiagnostics;
282+
ArgumentList *ArgListForDiagnostics;
283283
};
284284

285285
/// Get the storage accessed by this component.
@@ -459,7 +459,7 @@ class LValue {
459459
AccessStrategy accessStrategy,
460460
CanType formalRValueType,
461461
PreparedArguments &&indices,
462-
Expr *indexExprForDiagnostics);
462+
ArgumentList *argListForDiagnostics);
463463

464464
void addMemberVarComponent(SILGenFunction &SGF, SILLocation loc,
465465
VarDecl *var,
@@ -481,7 +481,7 @@ class LValue {
481481
AccessStrategy accessStrategy,
482482
CanType formalRValueType,
483483
PreparedArguments &&indices,
484-
Expr *indexExprForDiagnostics,
484+
ArgumentList *argListForDiagnostics,
485485
bool isOnSelfParameter = false,
486486
Optional<ActorIsolation> actorIso = None);
487487

lib/SILGen/SILGenApply.cpp

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
808808
/// The lvalue or rvalue representing the argument source of self.
809809
ArgumentSource selfParam;
810810

811-
ApplyExpr *selfApply = nullptr;
811+
SelfApplyExpr *selfApply = nullptr;
812812
ApplyExpr *callSite = nullptr;
813813
Expr *sideEffect = nullptr;
814814

@@ -831,26 +831,29 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
831831
selfParam = std::move(theSelfParam);
832832
}
833833

834-
bool isMethodSelfApply(Expr *e) {
835-
return (isa<SelfApplyExpr>(e) &&
836-
!isa<AutoClosureExpr>(
837-
cast<SelfApplyExpr>(e)->getFn()));
834+
SelfApplyExpr *getAsMethodSelfApply(Expr *e) {
835+
auto *SAE = dyn_cast<SelfApplyExpr>(e);
836+
if (!SAE)
837+
return nullptr;
838+
if (isa<AutoClosureExpr>(SAE->getFn()))
839+
return nullptr;
840+
return SAE;
838841
}
839842

840843
void decompose(ApplyExpr *e) {
841-
if (isMethodSelfApply(e)) {
842-
selfApply = e;
844+
if (auto *SAE = getAsMethodSelfApply(e)) {
845+
selfApply = SAE;
843846

844847
visit(selfApply->getFn());
845848
return;
846849
}
847850

848851
callSite = e;
849852

850-
if (isMethodSelfApply(e->getFn())) {
851-
selfApply = cast<ApplyExpr>(e->getFn());
853+
if (auto *SAE = getAsMethodSelfApply(e->getFn())) {
854+
selfApply = SAE;
852855

853-
if (selfApply->getArg()->isSuperExpr()) {
856+
if (selfApply->getBase()->isSuperExpr()) {
854857
applySuper(selfApply);
855858
return;
856859
}
@@ -960,14 +963,14 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
960963

961964
void processProtocolMethod(DeclRefExpr *e, AbstractFunctionDecl *afd,
962965
ProtocolDecl *proto) {
963-
ArgumentSource selfValue = selfApply->getArg();
966+
ArgumentSource selfValue = selfApply->getBase();
964967

965968
auto subs = e->getDeclRef().getSubstitutions();
966969

967970
SILDeclRef::Kind kind = SILDeclRef::Kind::Func;
968971
if (isa<ConstructorDecl>(afd)) {
969972
if (proto->isObjC()) {
970-
SILLocation loc = selfApply->getArg();
973+
SILLocation loc = selfApply->getBase();
971974

972975
// For Objective-C initializers, we only have an initializing
973976
// initializer. We need to allocate the object ourselves.
@@ -1017,8 +1020,8 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
10171020

10181021
// Required constructors are statically dispatched when the 'self'
10191022
// value is statically derived.
1020-
assert(selfApply->getArg()->getType()->is<AnyMetatypeType>());
1021-
if (selfApply->getArg()->isStaticallyDerivedMetatype())
1023+
assert(selfApply->getBase()->getType()->is<AnyMetatypeType>());
1024+
if (selfApply->getBase()->isStaticallyDerivedMetatype())
10221025
return false;
10231026
}
10241027

@@ -1027,7 +1030,7 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
10271030
}
10281031

10291032
void processClassMethod(DeclRefExpr *e, AbstractFunctionDecl *afd) {
1030-
ArgumentSource selfArgSource(selfApply->getArg());
1033+
ArgumentSource selfArgSource(selfApply->getBase());
10311034
setSelfParam(std::move(selfArgSource));
10321035

10331036
// Directly dispatch to calls of the replaced function inside of
@@ -1036,7 +1039,7 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
10361039
if (SGF.getOptions()
10371040
.EnableDynamicReplacementCanCallPreviousImplementation &&
10381041
isCallToReplacedInDynamicReplacement(SGF, afd, isObjCReplacementCall) &&
1039-
selfApply->getArg()->isSelfExprOf(
1042+
selfApply->getBase()->isSelfExprOf(
10401043
cast<AbstractFunctionDecl>(SGF.FunctionDC->getAsDecl()), false)) {
10411044
auto constant = SILDeclRef(afd).asForeign(
10421045
!isObjCReplacementCall && requiresForeignEntryPoint(e->getDecl()));
@@ -1089,7 +1092,7 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
10891092
// Enum case constructor references are open-coded.
10901093
if (auto *eed = dyn_cast<EnumElementDecl>(e->getDecl())) {
10911094
if (selfApply) {
1092-
ArgumentSource selfArgSource(selfApply->getArg());
1095+
ArgumentSource selfArgSource(selfApply->getBase());
10931096
setSelfParam(std::move(selfArgSource));
10941097
}
10951098

@@ -1131,15 +1134,16 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
11311134
// dynamically_replaceable inside of a dynamic_replacement(for:) function.
11321135
ApplyExpr *thisCallSite = (selfApply ? selfApply : callSite);
11331136
bool isObjCReplacementSelfCall = false;
1137+
auto *unaryArg = thisCallSite->getArgs()->getUnaryExpr();
11341138
bool isSelfCallToReplacedInDynamicReplacement =
11351139
SGF.getOptions()
11361140
.EnableDynamicReplacementCanCallPreviousImplementation &&
11371141
isCallToReplacedInDynamicReplacement(
11381142
SGF, cast<AbstractFunctionDecl>(constant.getDecl()),
11391143
isObjCReplacementSelfCall) &&
11401144
(afd->getDeclContext()->isModuleScopeContext() ||
1141-
thisCallSite->getArg()->isSelfExprOf(
1142-
cast<AbstractFunctionDecl>(SGF.FunctionDC->getAsDecl()), false));
1145+
(unaryArg && unaryArg->isSelfExprOf(
1146+
cast<AbstractFunctionDecl>(SGF.FunctionDC->getAsDecl()), false)));
11431147

11441148
if (isSelfCallToReplacedInDynamicReplacement && !isObjCReplacementSelfCall)
11451149
setCallee(Callee::forDirect(
@@ -1152,7 +1156,7 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
11521156

11531157
if (selfApply) {
11541158
// This is a statically-dispatched method with a 'self' parameter.
1155-
ArgumentSource selfArgSource(selfApply->getArg());
1159+
ArgumentSource selfArgSource(selfApply->getBase());
11561160
setSelfParam(std::move(selfArgSource));
11571161
}
11581162

@@ -1259,9 +1263,9 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
12591263
visit(e->getSubExpr());
12601264
}
12611265

1262-
void applySuper(ApplyExpr *apply) {
1266+
void applySuper(SelfApplyExpr *apply) {
12631267
// Load the 'super' argument.
1264-
Expr *arg = apply->getArg();
1268+
Expr *arg = apply->getBase();
12651269
RValue super;
12661270
CanType superFormalType = arg->getType()->getCanonicalType();
12671271

@@ -1400,7 +1404,7 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
14001404
}
14011405

14021406
/// Try to emit the given application as initializer delegation.
1403-
bool applyInitDelegation(ApplyExpr *expr) {
1407+
bool applyInitDelegation(SelfApplyExpr *expr) {
14041408
// Dig out the constructor we're delegating to.
14051409
Expr *fn = expr->getFn();
14061410
auto ctorRef = dyn_cast<OtherConstructorDeclRefExpr>(
@@ -1439,7 +1443,7 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
14391443
}
14401444

14411445
// Load the 'self' argument.
1442-
Expr *arg = expr->getArg();
1446+
Expr *arg = expr->getBase();
14431447
ManagedValue self;
14441448
CanType selfFormalType = arg->getType()->getCanonicalType();
14451449

@@ -4273,20 +4277,14 @@ CallEmission CallEmission::forApplyExpr(SILGenFunction &SGF, ApplyExpr *e) {
42734277
}
42744278

42754279
// Apply arguments from the actual call site.
4276-
if (apply.callSite) {
4277-
Expr *arg = apply.callSite->getArg();
4278-
4279-
SmallVector<AnyFunctionType::Param, 8> params;
4280-
AnyFunctionType::decomposeTuple(arg->getType(), params);
4281-
4282-
PreparedArguments preparedArgs(params, arg);
4283-
4284-
emission.addCallSite(apply.callSite, std::move(preparedArgs),
4285-
apply.callSite->isNoThrows(),
4286-
apply.callSite->isNoAsync());
4280+
if (auto *call = apply.callSite) {
4281+
auto fnTy = call->getFn()->getType()->castTo<FunctionType>();
4282+
PreparedArguments preparedArgs(fnTy->getParams(), call->getArgs());
4283+
emission.addCallSite(call, std::move(preparedArgs), call->isNoThrows(),
4284+
call->isNoAsync());
42874285

42884286
// For an implicitly-async call, record the target of the actor hop.
4289-
if (auto target = apply.callSite->isImplicitlyAsync())
4287+
if (auto target = call->isImplicitlyAsync())
42904288
emission.setImplicitlyAsync(target);
42914289
}
42924290

@@ -5629,9 +5627,7 @@ PreparedArguments
56295627
SILGenFunction::prepareSubscriptIndices(SubscriptDecl *subscript,
56305628
SubstitutionMap subs,
56315629
AccessStrategy strategy,
5632-
Expr *indexExpr) {
5633-
// FIXME: we should expect an array of index expressions.
5634-
5630+
ArgumentList *argList) {
56355631
// TODO: use the real abstraction pattern from the accessor(s) in the
56365632
// strategy.
56375633
// Currently we use the substituted type so that we can reconstitute these
@@ -5653,7 +5649,7 @@ SILGenFunction::prepareSubscriptIndices(SubscriptDecl *subscript,
56535649

56545650
// Prepare the unevaluated index expression.
56555651
auto substParams = substFnType->getParams();
5656-
PreparedArguments args(substParams, indexExpr);
5652+
PreparedArguments args(substParams, argList);
56575653

56585654
// Now, force it to be evaluated.
56595655
SmallVector<ManagedValue, 4> argValues;
@@ -5666,11 +5662,11 @@ SILGenFunction::prepareSubscriptIndices(SubscriptDecl *subscript,
56665662
PreparedArguments result(substParams);
56675663

56685664
ArrayRef<ManagedValue> remainingArgs = argValues;
5669-
for (auto substParam : substParams) {
5670-
auto substParamType = substParam.getParameterType()->getCanonicalType();
5665+
for (auto i : indices(substParams)) {
5666+
auto substParamType = substParams[i].getParameterType()->getCanonicalType();
56715667
auto count = RValue::getRValueSize(substParamType);
56725668
RValue elt(*this, remainingArgs.slice(0, count), substParamType);
5673-
result.add(indexExpr, std::move(elt));
5669+
result.add(argList->getExpr(i), std::move(elt));
56745670
remainingArgs = remainingArgs.slice(count);
56755671
}
56765672
assert(remainingArgs.empty());
@@ -6178,7 +6174,9 @@ RValue SILGenFunction::emitDynamicSubscriptExpr(DynamicSubscriptExpr *e,
61786174
SILValue base = managedBase.getValue();
61796175

61806176
// Emit the index.
6181-
RValue index = emitRValue(e->getIndex());
6177+
auto *indexExpr = e->getArgs()->getUnaryExpr();
6178+
assert(indexExpr);
6179+
RValue index = emitRValue(indexExpr);
61826180

61836181
// Create the continuation block.
61846182
SILBasicBlock *contBB = createBasicBlock();
@@ -6214,7 +6212,7 @@ RValue SILGenFunction::emitDynamicSubscriptExpr(DynamicSubscriptExpr *e,
62146212
//
62156213
// FIXME: Verify ExtInfo state is correct, not working by accident.
62166214
CanFunctionType::ExtInfo methodInfo;
6217-
FunctionType::Param indexArg(e->getIndex()->getType()->getCanonicalType());
6215+
FunctionType::Param indexArg(indexExpr->getType()->getCanonicalType());
62186216
auto methodTy = CanFunctionType::get({indexArg}, valueTy, methodInfo);
62196217
auto foreignMethodTy =
62206218
getPartialApplyOfDynamicMethodFormalType(SGM, member, e->getMember());
@@ -6268,7 +6266,7 @@ RValue SILGenFunction::emitDynamicSubscriptExpr(DynamicSubscriptExpr *e,
62686266
}
62696267

62706268
SmallVector<ManagedValue, 4> SILGenFunction::emitKeyPathSubscriptOperands(
6271-
SubscriptDecl *subscript, SubstitutionMap subs, Expr *indexExpr) {
6269+
SubscriptDecl *subscript, SubstitutionMap subs, ArgumentList *argList) {
62726270
Type interfaceType = subscript->getInterfaceType();
62736271
CanFunctionType substFnType =
62746272
subs ? cast<FunctionType>(interfaceType->castTo<GenericFunctionType>()
@@ -6291,7 +6289,7 @@ SmallVector<ManagedValue, 4> SILGenFunction::emitKeyPathSubscriptOperands(
62916289
auto prepared =
62926290
prepareSubscriptIndices(subscript, subs,
62936291
// Strategy doesn't matter
6294-
AccessStrategy::getStorage(), indexExpr);
6292+
AccessStrategy::getStorage(), argList);
62956293
emitter.emitPreparedArgs(std::move(prepared), origFnType);
62966294

62976295
if (!delayedArgs.empty())

lib/SILGen/SILGenExpr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,7 +2524,7 @@ visitObjectLiteralExpr(ObjectLiteralExpr *E, SGFContext C) {
25242524
AnyFunctionType *fnTy = decl->getMethodInterfaceType()
25252525
.subst(init.getSubstitutions())
25262526
->getAs<AnyFunctionType>();
2527-
PreparedArguments args(fnTy->getParams(), E->getArg());
2527+
PreparedArguments args(fnTy->getParams(), E->getArgs());
25282528
return SGF.emitApplyAllocatingInitializer(SILLocation(E), init,
25292529
std::move(args), E->getType(), C);
25302530
}
@@ -3673,7 +3673,7 @@ RValue RValueEmitter::visitKeyPathExpr(KeyPathExpr *E, SGFContext C) {
36733673
auto subscript = cast<SubscriptDecl>(decl);
36743674
auto loweredArgs = SGF.emitKeyPathSubscriptOperands(
36753675
subscript, component.getDeclRef().getSubstitutions(),
3676-
component.getIndexExpr());
3676+
component.getSubscriptArgs());
36773677

36783678
for (auto &arg : loweredArgs) {
36793679
operands.push_back(arg.forward(SGF));

lib/SILGen/SILGenFunction.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,11 +731,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
731731
/// \returns Lowered index arguments.
732732
/// \param subscript - The subscript decl who's arguments are being lowered.
733733
/// \param subs - Used to get subscript function type and to substitute generic args.
734-
/// \param indexExpr - An expression holding the indices of the
735-
/// subscript (either a TupleExpr or a ParenExpr).
734+
/// \param argList - The argument list of the subscript.
736735
SmallVector<ManagedValue, 4>
737736
emitKeyPathSubscriptOperands(SubscriptDecl *subscript, SubstitutionMap subs,
738-
Expr *indexExpr);
737+
ArgumentList *argList);
739738

740739
/// Convert a block to a native function with a thunk.
741740
ManagedValue emitBlockToFunc(SILLocation loc,
@@ -1366,7 +1365,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
13661365
PreparedArguments prepareSubscriptIndices(SubscriptDecl *subscript,
13671366
SubstitutionMap subs,
13681367
AccessStrategy strategy,
1369-
Expr *indices);
1368+
ArgumentList *argList);
13701369

13711370
ArgumentSource prepareAccessorBaseArg(SILLocation loc, ManagedValue base,
13721371
CanType baseFormalType,

0 commit comments

Comments
 (0)