Skip to content

Commit 4775482

Browse files
committed
[CodeSynthesis] Adopt ArgumentList
Most of this should be fairly mechanical, the changes in PlaygroundTransform are a little more involved though.
1 parent 78d8d09 commit 4775482

20 files changed

+246
-320
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,9 @@ synthesizeEnumRawValueConstructorBody(AbstractFunctionDecl *afd,
492492
reinterpretCastRef->setType(
493493
FunctionType::get({FunctionType::Param(rawTy)}, enumTy, info));
494494

495+
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {paramRef});
495496
auto reinterpreted = CallExpr::createImplicit(ctx, reinterpretCastRef,
496-
{ paramRef }, { Identifier() });
497+
argList);
497498
reinterpreted->setType(enumTy);
498499
reinterpreted->setThrows(false);
499500

@@ -574,8 +575,9 @@ synthesizeEnumRawValueGetterBody(AbstractFunctionDecl *afd, void *context) {
574575
reinterpretCastRef->setType(
575576
FunctionType::get({FunctionType::Param(enumTy)}, rawTy, info));
576577

578+
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {selfRef});
577579
auto reinterpreted = CallExpr::createImplicit(ctx, reinterpretCastRef,
578-
{ selfRef }, { Identifier() });
580+
argList);
579581
reinterpreted->setType(rawTy);
580582
reinterpreted->setThrows(false);
581583

@@ -940,9 +942,9 @@ synthesizeUnionFieldGetterBody(AbstractFunctionDecl *afd, void *context) {
940942
FunctionType::get(AnyFunctionType::Param(selfDecl->getInterfaceType()),
941943
importedFieldDecl->getInterfaceType(), info));
942944

945+
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {selfRef});
943946
auto reinterpreted = CallExpr::createImplicit(ctx, reinterpretCastRefExpr,
944-
{ selfRef },
945-
{ Identifier() });
947+
argList);
946948
reinterpreted->setType(importedFieldDecl->getInterfaceType());
947949
reinterpreted->setThrows(false);
948950
auto ret = new (ctx) ReturnStmt(SourceLoc(), reinterpreted);
@@ -987,9 +989,10 @@ synthesizeUnionFieldSetterBody(AbstractFunctionDecl *afd, void *context) {
987989
AnyFunctionType::Param(inoutSelfDecl->getInterfaceType(), Identifier(),
988990
ParameterTypeFlags().withInOut(true)),
989991
ctx.TheRawPointerType, addressOfInfo));
992+
993+
auto *selfPtrArgs = ArgumentList::forImplicitUnlabeled(ctx, {inoutSelf});
990994
auto selfPointer = CallExpr::createImplicit(ctx, addressofFnRefExpr,
991-
{ inoutSelf },
992-
{ Identifier() });
995+
selfPtrArgs);
993996
selfPointer->setType(ctx.TheRawPointerType);
994997
selfPointer->setThrows(false);
995998

@@ -1007,9 +1010,11 @@ synthesizeUnionFieldSetterBody(AbstractFunctionDecl *afd, void *context) {
10071010
{AnyFunctionType::Param(newValueDecl->getInterfaceType()),
10081011
AnyFunctionType::Param(ctx.TheRawPointerType)},
10091012
TupleType::getEmpty(ctx), initializeInfo));
1013+
1014+
auto *initArgs =
1015+
ArgumentList::forImplicitUnlabeled(ctx, {newValueRef, selfPointer});
10101016
auto initialize = CallExpr::createImplicit(ctx, initializeFnRefExpr,
1011-
{ newValueRef, selfPointer },
1012-
{ Identifier(), Identifier() });
1017+
initArgs);
10131018
initialize->setType(TupleType::getEmpty(ctx));
10141019
initialize->setThrows(false);
10151020

@@ -7653,9 +7658,9 @@ createAccessorImplCallExpr(FuncDecl *accessorImpl,
76537658
accessorImplDotCallExpr->setType(accessorImpl->getMethodInterfaceType());
76547659
accessorImplDotCallExpr->setThrows(false);
76557660

7661+
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {keyRefExpr});
76567662
auto *accessorImplCallExpr =
7657-
CallExpr::createImplicit(ctx, accessorImplDotCallExpr,
7658-
{ keyRefExpr }, { Identifier() });
7663+
CallExpr::createImplicit(ctx, accessorImplDotCallExpr, argList);
76597664
accessorImplCallExpr->setType(accessorImpl->getResultInterfaceType());
76607665
accessorImplCallExpr->setThrows(false);
76617666
return accessorImplCallExpr;
@@ -9705,8 +9710,8 @@ synthesizeConstantGetterBody(AbstractFunctionDecl *afd, void *voidContext) {
97059710
// (rawValue: T) -> ...
97069711
initTy = initTy->castTo<FunctionType>()->getResult();
97079712

9708-
auto initCall = CallExpr::createImplicit(ctx, initRef, { expr },
9709-
{ ctx.Id_rawValue });
9713+
auto *argList = ArgumentList::forImplicitSingle(ctx, ctx.Id_rawValue, expr);
9714+
auto initCall = CallExpr::createImplicit(ctx, initRef, argList);
97109715
initCall->setType(initTy);
97119716
initCall->setThrows(false);
97129717

lib/Sema/BuilderTransform.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ class BuilderClosureVisitor
8888

8989
/// Produce a builder call to the given named function with the given
9090
/// arguments.
91-
Expr *buildCallIfWanted(SourceLoc loc,
92-
Identifier fnName, ArrayRef<Expr *> args,
91+
Expr *buildCallIfWanted(SourceLoc loc, Identifier fnName,
92+
ArrayRef<Expr *> argExprs,
9393
ArrayRef<Identifier> argLabels) {
9494
if (!cs)
9595
return nullptr;
@@ -120,23 +120,24 @@ class BuilderClosureVisitor
120120
}
121121
cs->setType(typeExpr, MetatypeType::get(builderType));
122122

123-
SmallVector<SourceLoc, 4> argLabelLocs;
124-
for (auto i : indices(argLabels)) {
125-
argLabelLocs.push_back(args[i]->getStartLoc());
123+
SmallVector<Argument, 4> args;
124+
for (auto i : indices(argExprs)) {
125+
auto *expr = argExprs[i];
126+
auto label = argLabels.empty() ? Identifier() : argLabels[i];
127+
auto labelLoc = argLabels.empty() ? SourceLoc() : expr->getStartLoc();
128+
args.emplace_back(labelLoc, label, expr);
126129
}
127130

128131
auto memberRef = new (ctx) UnresolvedDotExpr(
129132
typeExpr, loc, DeclNameRef(fnName), DeclNameLoc(loc),
130133
/*implicit=*/true);
131134
memberRef->setFunctionRefKind(FunctionRefKind::SingleApply);
132-
SourceLoc openLoc = args.empty() ? loc : args.front()->getStartLoc();
133-
SourceLoc closeLoc = args.empty() ? loc : args.back()->getEndLoc();
134-
Expr *result = CallExpr::create(ctx, memberRef, openLoc, args,
135-
argLabels, argLabelLocs, closeLoc,
136-
/*trailing closures*/{},
137-
/*implicit*/true);
138135

139-
return result;
136+
auto openLoc = args.empty() ? loc : argExprs.front()->getStartLoc();
137+
auto closeLoc = args.empty() ? loc : argExprs.back()->getEndLoc();
138+
139+
auto *argList = ArgumentList::createImplicit(ctx, openLoc, args, closeLoc);
140+
return CallExpr::createImplicit(ctx, memberRef, argList);
140141
}
141142

142143
/// Check whether the builder supports the given operation.
@@ -653,7 +654,8 @@ class BuilderClosureVisitor
653654
auto someRef = new (ctx) UnresolvedDotExpr(
654655
optionalTypeExpr, loc, DeclNameRef(ctx.getIdentifier("some")),
655656
DeclNameLoc(loc), /*implicit=*/true);
656-
return CallExpr::createImplicit(ctx, someRef, arg, { });
657+
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {arg});
658+
return CallExpr::createImplicit(ctx, someRef, argList);
657659
}
658660

659661
Expr *buildNoneExpr(SourceLoc endLoc) {
@@ -873,10 +875,12 @@ class BuilderClosureVisitor
873875
arrayVarRef, endLoc, DeclNameRef(ctx.getIdentifier("append")),
874876
DeclNameLoc(endLoc), /*implicit=*/true);
875877
arrayAppendRef->setFunctionRefKind(FunctionRefKind::SingleApply);
878+
876879
auto bodyVarRef = buildVarRef(bodyVar, endLoc);
877-
Expr *arrayAppendCall = CallExpr::create(
878-
ctx, arrayAppendRef, endLoc, { bodyVarRef } , { Identifier() },
879-
{ endLoc }, endLoc, /*trailingClosures=*/{}, /*implicit=*/true);
880+
auto *argList = ArgumentList::createImplicit(
881+
ctx, endLoc, {Argument::unlabeled(bodyVarRef)}, endLoc);
882+
Expr *arrayAppendCall =
883+
CallExpr::createImplicit(ctx, arrayAppendRef, argList);
880884
arrayAppendCall = cs->generateConstraints(arrayAppendCall, dc);
881885
if (!arrayAppendCall) {
882886
hadError = true;

lib/Sema/CodeSynthesis.cpp

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,13 @@ Expr *swift::buildSelfReference(VarDecl *selfDecl,
9595
llvm_unreachable("bad self access kind");
9696
}
9797

98-
/// Build an expression that evaluates the specified parameter list as a tuple
99-
/// or paren expr, suitable for use in an apply expr.
100-
Expr *swift::buildArgumentForwardingExpr(ArrayRef<ParamDecl*> params,
101-
ASTContext &ctx) {
102-
SmallVector<Identifier, 4> labels;
103-
SmallVector<SourceLoc, 4> labelLocs;
104-
SmallVector<Expr *, 4> args;
105-
SmallVector<AnyFunctionType::Param, 4> elts;
106-
107-
for (auto param : params) {
98+
/// Build an argument list that forwards references to the specified parameter
99+
/// list.
100+
ArgumentList *swift::buildForwardingArgumentList(ArrayRef<ParamDecl *> params,
101+
ASTContext &ctx) {
102+
SmallVector<Argument, 4> args;
103+
for (auto *param : params) {
108104
auto type = param->getType();
109-
elts.push_back(param->toFunctionParam(type));
110105

111106
Expr *ref = new (ctx) DeclRefExpr(param, DeclNameLoc(), /*implicit*/ true);
112107
ref->setType(param->isInOut() ? LValueType::get(type) : type);
@@ -117,29 +112,9 @@ Expr *swift::buildArgumentForwardingExpr(ArrayRef<ParamDecl*> params,
117112
ref = new (ctx) VarargExpansionExpr(ref, /*implicit*/ true);
118113
ref->setType(type);
119114
}
120-
121-
args.push_back(ref);
122-
123-
labels.push_back(param->getArgumentName());
124-
labelLocs.push_back(SourceLoc());
125-
}
126-
127-
Expr *argExpr;
128-
if (args.size() == 1 &&
129-
labels[0].empty() &&
130-
!isa<VarargExpansionExpr>(args[0])) {
131-
argExpr = new (ctx) ParenExpr(SourceLoc(), args[0], SourceLoc(),
132-
/*hasTrailingClosure=*/false);
133-
argExpr->setImplicit();
134-
} else {
135-
argExpr = TupleExpr::create(ctx, SourceLoc(), args, labels, labelLocs,
136-
SourceLoc(), false, IsImplicit);
115+
args.emplace_back(SourceLoc(), param->getArgumentName(), ref);
137116
}
138-
139-
auto argTy = AnyFunctionType::composeTuple(ctx, elts, /*canonical*/false);
140-
argExpr->setType(argTy);
141-
142-
return argExpr;
117+
return ArgumentList::createImplicit(ctx, args);
143118
}
144119

145120
static void maybeAddMemberwiseDefaultArg(ParamDecl *arg, VarDecl *var,
@@ -430,8 +405,9 @@ synthesizeStubBody(AbstractFunctionDecl *fn, void *) {
430405
column->setType(uintType);
431406
column->setBuiltinInitializer(uintInit);
432407

433-
auto *call = CallExpr::createImplicit(
434-
ctx, ref, { className, initName, file, line, column }, {});
408+
auto *argList = ArgumentList::forImplicitUnlabeled(
409+
ctx, {className, initName, file, line, column});
410+
auto *call = CallExpr::createImplicit(ctx, ref, argList);
435411
call->setType(ctx.getNeverType());
436412
call->setThrows(false);
437413

@@ -682,11 +658,9 @@ synthesizeDesignatedInitOverride(AbstractFunctionDecl *fn, void *context) {
682658
superclassCtorRefExpr->setThrows(false);
683659

684660
auto *bodyParams = ctor->getParameters();
685-
auto ctorArgs = buildArgumentForwardingExpr(bodyParams->getArray(), ctx);
661+
auto *ctorArgs = buildForwardingArgumentList(bodyParams->getArray(), ctx);
686662
auto *superclassCallExpr =
687-
CallExpr::create(ctx, superclassCtorRefExpr, ctorArgs,
688-
superclassCtor->getName().getArgumentNames(), { },
689-
/*hasTrailingClosure=*/false, /*implicit=*/true);
663+
CallExpr::createImplicit(ctx, superclassCtorRefExpr, ctorArgs);
690664

691665
if (auto *funcTy = type->getAs<FunctionType>())
692666
type = funcTy->getResult();

lib/Sema/CodeSynthesis.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace swift {
2828

2929
class AbstractFunctionDecl;
3030
class AbstractStorageDecl;
31+
class ArgumentList;
3132
class ASTContext;
3233
class ClassDecl;
3334
class ConstructorDecl;
@@ -61,10 +62,10 @@ enum class SelfAccessorKind {
6162
Expr *buildSelfReference(VarDecl *selfDecl, SelfAccessorKind selfAccessorKind,
6263
bool isLValue, Type convertTy = Type());
6364

64-
/// Build an expression that evaluates the specified parameter list as a tuple
65-
/// or paren expr, suitable for use in an apply expr.
66-
Expr *buildArgumentForwardingExpr(ArrayRef<ParamDecl*> params,
67-
ASTContext &ctx);
65+
/// Build an argument list that forwards references to the specified parameter
66+
/// list.
67+
ArgumentList *buildForwardingArgumentList(ArrayRef<ParamDecl *> params,
68+
ASTContext &ctx);
6869

6970
/// Returns the protocol requirement with the specified name.
7071
ValueDecl *getProtocolRequirement(ProtocolDecl *protocol, Identifier name);

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ synthesizeRemoteFuncStubBody(AbstractFunctionDecl *func, void *context) {
9999
column->setType(uintType);
100100
column->setBuiltinInitializer(uintInit);
101101

102-
auto *call = CallExpr::createImplicit(
103-
ctx, ref, { className, funcName, file, line, column }, {});
102+
auto *argList = ArgumentList::forImplicitUnlabeled(
103+
ctx, {className, funcName, file, line, column});
104+
auto *call = CallExpr::createImplicit(ctx, ref, argList);
104105
call->setType(ctx.getNeverType());
105106
call->setThrows(false);
106107

lib/Sema/DebuggerTestingTransform.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,18 @@ class DebuggerTestingTransform : public ASTWalker {
208208
auto *PODeclRef = new (Ctx)
209209
UnresolvedDeclRefExpr(StringForPrintObjectName,
210210
DeclRefKind::Ordinary, DeclNameLoc());
211-
Expr *POArgs[] = {DstRef};
212-
Identifier POLabels[] = {Identifier()};
213-
auto *POCall = CallExpr::createImplicit(Ctx, PODeclRef, POArgs, POLabels);
211+
auto *POArgList = ArgumentList::forImplicitUnlabeled(Ctx, {DstRef});
212+
auto *POCall = CallExpr::createImplicit(Ctx, PODeclRef, POArgList);
214213
POCall->setThrows(false);
215214

216215
// Create the call to checkExpect.
217-
Identifier CheckExpectLabels[] = {Identifier(), Identifier()};
218-
Expr *CheckExpectArgs[] = {Varname, POCall};
219216
UnresolvedDeclRefExpr *CheckExpectDRE = new (Ctx)
220217
UnresolvedDeclRefExpr(DebuggerTestingCheckExpectName,
221218
DeclRefKind::Ordinary, DeclNameLoc());
222-
auto *CheckExpectExpr = CallExpr::createImplicit(
223-
Ctx, CheckExpectDRE, CheckExpectArgs, CheckExpectLabels);
219+
auto *CheckArgList =
220+
ArgumentList::forImplicitUnlabeled(Ctx, {Varname, POCall});
221+
auto *CheckExpectExpr =
222+
CallExpr::createImplicit(Ctx, CheckExpectDRE, CheckArgList);
224223
CheckExpectExpr->setThrows(false);
225224

226225
// Create the closure.

lib/Sema/DerivedConformanceActor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ static Expr *constructUnownedSerialExecutor(ASTContext &ctx,
7777

7878
// Call the constructor, building an expression of type
7979
// UnownedSerialExecutor.
80-
auto call = CallExpr::createImplicit(ctx, selfApply, arg, /*labels*/ {});
80+
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {arg});
81+
auto call = CallExpr::createImplicit(ctx, selfApply, argList);
8182
call->setType(executorType);
8283
call->setThrows(false);
8384
return call;

lib/Sema/DerivedConformanceAdditiveArithmetic.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,14 @@ deriveBodyMathOperator(AbstractFunctionDecl *funcDecl, MathOperator op) {
148148
};
149149

150150
// Create array of member operator call expressions.
151-
llvm::SmallVector<Expr *, 2> memberOpExprs;
152-
llvm::SmallVector<Identifier, 2> memberNames;
151+
llvm::SmallVector<Argument, 2> memberOpArgs;
153152
for (auto member : nominal->getStoredProperties()) {
154-
memberOpExprs.push_back(createMemberOpExpr(member));
155-
memberNames.push_back(member->getName());
153+
memberOpArgs.emplace_back(SourceLoc(), member->getName(),
154+
createMemberOpExpr(member));
156155
}
157156
// Call memberwise initializer with member operator call expressions.
158-
auto *callExpr =
159-
CallExpr::createImplicit(C, initExpr, memberOpExprs, memberNames);
157+
auto *argList = ArgumentList::createImplicit(C, memberOpArgs);
158+
auto *callExpr = CallExpr::createImplicit(C, initExpr, argList);
160159
ASTNode returnStmt = new (C) ReturnStmt(SourceLoc(), callExpr, true);
161160
return std::pair<BraceStmt *, bool>(
162161
BraceStmt::create(C, SourceLoc(), returnStmt, SourceLoc(), true), false);
@@ -267,15 +266,14 @@ deriveBodyPropertyGetter(AbstractFunctionDecl *funcDecl, ProtocolDecl *proto,
267266
};
268267

269268
// Create array of `member.<property>` expressions.
270-
llvm::SmallVector<Expr *, 2> memberPropExprs;
271-
llvm::SmallVector<Identifier, 2> memberNames;
269+
llvm::SmallVector<Argument, 2> args;
272270
for (auto member : nominal->getStoredProperties()) {
273-
memberPropExprs.push_back(createMemberPropertyExpr(member));
274-
memberNames.push_back(member->getName());
271+
args.emplace_back(SourceLoc(), member->getName(),
272+
createMemberPropertyExpr(member));
275273
}
276274
// Call memberwise initializer with member property expressions.
277-
auto *callExpr =
278-
CallExpr::createImplicit(C, initExpr, memberPropExprs, memberNames);
275+
auto *callExpr = CallExpr::createImplicit(
276+
C, initExpr, ArgumentList::createImplicit(C, args));
279277
ASTNode returnStmt = new (C) ReturnStmt(SourceLoc(), callExpr, true);
280278
auto *braceStmt =
281279
BraceStmt::create(C, SourceLoc(), returnStmt, SourceLoc(), true);

0 commit comments

Comments
 (0)