Skip to content

Commit 72d4d9f

Browse files
committed
[AST] Add some 'create' factory methods
This provides consistency with other AST nodes, and will be useful for implementing ArgumentList construction in the future.
1 parent c70f280 commit 72d4d9f

12 files changed

+87
-49
lines changed

include/swift/AST/Expr.h

+26-12
Original file line numberDiff line numberDiff line change
@@ -4787,9 +4787,12 @@ class CallExpr final : public ApplyExpr,
47874787

47884788
/// PrefixUnaryExpr - Prefix unary expressions like '!y'.
47894789
class PrefixUnaryExpr : public ApplyExpr {
4790+
PrefixUnaryExpr(Expr *fn, Expr *operand, Type ty = Type())
4791+
: ApplyExpr(ExprKind::PrefixUnary, fn, operand, /*implicit*/ false, ty) {}
4792+
47904793
public:
4791-
PrefixUnaryExpr(Expr *Fn, Expr *Arg, Type Ty = Type())
4792-
: ApplyExpr(ExprKind::PrefixUnary, Fn, Arg, /*Implicit=*/false, Ty) {}
4794+
static PrefixUnaryExpr *create(ASTContext &ctx, Expr *fn, Expr *operand,
4795+
Type ty = Type());
47934796

47944797
SourceLoc getLoc() const { return getFn()->getStartLoc(); }
47954798

@@ -4807,9 +4810,13 @@ class PrefixUnaryExpr : public ApplyExpr {
48074810

48084811
/// PostfixUnaryExpr - Postfix unary expressions like 'y!'.
48094812
class PostfixUnaryExpr : public ApplyExpr {
4813+
PostfixUnaryExpr(Expr *fn, Expr *operand, Type ty = Type())
4814+
: ApplyExpr(ExprKind::PostfixUnary, fn, operand, /*implicit*/ false, ty) {
4815+
}
4816+
48104817
public:
4811-
PostfixUnaryExpr(Expr *Fn, Expr *Arg, Type Ty = Type())
4812-
: ApplyExpr(ExprKind::PostfixUnary, Fn, Arg, /*Implicit=*/false, Ty) {}
4818+
static PostfixUnaryExpr *create(ASTContext &ctx, Expr *fn, Expr *operand,
4819+
Type ty = Type());
48134820

48144821
SourceLoc getLoc() const { return getFn()->getStartLoc(); }
48154822

@@ -4877,15 +4884,19 @@ class SelfApplyExpr : public ApplyExpr {
48774884
/// is modeled as a DeclRefExpr or OverloadSetRefExpr on the method.
48784885
class DotSyntaxCallExpr : public SelfApplyExpr {
48794886
SourceLoc DotLoc;
4880-
4881-
public:
4882-
DotSyntaxCallExpr(Expr *FnExpr, SourceLoc DotLoc, Expr *BaseExpr,
4883-
Type Ty = Type())
4884-
: SelfApplyExpr(ExprKind::DotSyntaxCall, FnExpr, BaseExpr, Ty),
4885-
DotLoc(DotLoc) {
4887+
4888+
DotSyntaxCallExpr(Expr *fnExpr, SourceLoc dotLoc, Expr *baseExpr,
4889+
Type ty = Type())
4890+
: SelfApplyExpr(ExprKind::DotSyntaxCall, fnExpr, baseExpr, ty),
4891+
DotLoc(dotLoc) {
48864892
setImplicit(DotLoc.isInvalid());
48874893
}
48884894

4895+
public:
4896+
static DotSyntaxCallExpr *create(ASTContext &ctx, Expr *fnExpr,
4897+
SourceLoc dotLoc, Expr *baseExpr,
4898+
Type ty = Type());
4899+
48894900
SourceLoc getDotLoc() const { return DotLoc; }
48904901

48914902
SourceLoc getLoc() const;
@@ -4901,9 +4912,12 @@ class DotSyntaxCallExpr : public SelfApplyExpr {
49014912
/// actual reference to function which returns the constructor is modeled
49024913
/// as a DeclRefExpr.
49034914
class ConstructorRefCallExpr : public SelfApplyExpr {
4915+
ConstructorRefCallExpr(Expr *fnExpr, Expr *baseExpr, Type ty = Type())
4916+
: SelfApplyExpr(ExprKind::ConstructorRefCall, fnExpr, baseExpr, ty) {}
4917+
49044918
public:
4905-
ConstructorRefCallExpr(Expr *FnExpr, Expr *BaseExpr, Type Ty = Type())
4906-
: SelfApplyExpr(ExprKind::ConstructorRefCall, FnExpr, BaseExpr, Ty) {}
4919+
static ConstructorRefCallExpr *create(ASTContext &ctx, Expr *fnExpr,
4920+
Expr *baseExpr, Type ty = Type());
49074921

49084922
SourceLoc getLoc() const { return getFn()->getLoc(); }
49094923
SourceLoc getStartLoc() const { return getBase()->getStartLoc(); }

lib/AST/Expr.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,16 @@ Expr *CallExpr::getDirectCallee() const {
17791779
}
17801780
}
17811781

1782+
PrefixUnaryExpr *PrefixUnaryExpr::create(ASTContext &ctx, Expr *fn,
1783+
Expr *operand, Type ty) {
1784+
return new (ctx) PrefixUnaryExpr(fn, operand, ty);
1785+
}
1786+
1787+
PostfixUnaryExpr *PostfixUnaryExpr::create(ASTContext &ctx, Expr *fn,
1788+
Expr *operand, Type ty) {
1789+
return new (ctx) PostfixUnaryExpr(fn, operand, ty);
1790+
}
1791+
17821792
BinaryExpr *BinaryExpr::create(ASTContext &ctx, Expr *lhs, Expr *fn, Expr *rhs,
17831793
bool implicit, Type ty) {
17841794
auto *packedArg = TupleExpr::createImplicit(ctx, {lhs, rhs}, /*labels*/ {});
@@ -1787,6 +1797,12 @@ BinaryExpr *BinaryExpr::create(ASTContext &ctx, Expr *lhs, Expr *fn, Expr *rhs,
17871797
return new (ctx) BinaryExpr(fn, packedArg, implicit, ty);
17881798
}
17891799

1800+
DotSyntaxCallExpr *DotSyntaxCallExpr::create(ASTContext &ctx, Expr *fnExpr,
1801+
SourceLoc dotLoc, Expr *baseExpr,
1802+
Type ty) {
1803+
return new (ctx) DotSyntaxCallExpr(fnExpr, dotLoc, baseExpr, ty);
1804+
}
1805+
17901806
SourceLoc DotSyntaxCallExpr::getLoc() const {
17911807
if (isImplicit()) {
17921808
SourceLoc baseLoc = getBase()->getLoc();
@@ -1814,6 +1830,13 @@ SourceLoc DotSyntaxCallExpr::getEndLoc() const {
18141830
return getFn()->getEndLoc();
18151831
}
18161832

1833+
ConstructorRefCallExpr *ConstructorRefCallExpr::create(ASTContext &ctx,
1834+
Expr *fnExpr,
1835+
Expr *baseExpr,
1836+
Type ty) {
1837+
return new (ctx) ConstructorRefCallExpr(fnExpr, baseExpr, ty);
1838+
}
1839+
18171840
void ExplicitCastExpr::setCastType(Type type) {
18181841
CastTy->setType(MetatypeType::get(type));
18191842
}

lib/ClangImporter/ImportDecl.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -6490,8 +6490,8 @@ Decl *SwiftDeclConverter::importEnumCaseAlias(
64906490
/*implicit*/ true);
64916491
constantRef->setType(enumElt->getInterfaceType());
64926492

6493-
auto instantiate = new (Impl.SwiftContext)
6494-
DotSyntaxCallExpr(constantRef, SourceLoc(), typeRef);
6493+
auto instantiate = DotSyntaxCallExpr::create(Impl.SwiftContext, constantRef,
6494+
SourceLoc(), typeRef);
64956495
instantiate->setType(importedEnumTy);
64966496
instantiate->setThrows(false);
64976497

@@ -7634,7 +7634,7 @@ createAccessorImplCallExpr(FuncDecl *accessorImpl,
76347634
accessorImplExpr->setType(accessorImpl->getInterfaceType());
76357635

76367636
auto accessorImplDotCallExpr =
7637-
new (ctx) DotSyntaxCallExpr(accessorImplExpr, SourceLoc(), selfExpr);
7637+
DotSyntaxCallExpr::create(ctx, accessorImplExpr, SourceLoc(), selfExpr);
76387638
accessorImplDotCallExpr->setType(accessorImpl->getMethodInterfaceType());
76397639
accessorImplDotCallExpr->setThrows(false);
76407640

@@ -9662,8 +9662,8 @@ synthesizeConstantGetterBody(AbstractFunctionDecl *afd, void *voidContext) {
96629662

96639663
// (Self) -> ...
96649664
initTy = initTy->castTo<FunctionType>()->getResult();
9665-
auto initRef = new (ctx) DotSyntaxCallExpr(declRef, SourceLoc(),
9666-
typeRef, initTy);
9665+
auto initRef =
9666+
DotSyntaxCallExpr::create(ctx, declRef, SourceLoc(), typeRef, initTy);
96679667
initRef->setThrows(false);
96689668

96699669
// (rawValue: T) -> ...

lib/Parse/ParseExpr.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,9 @@ ParserResult<Expr> Parser::parseExprUnary(Diag<> Message, bool isExprBasic) {
586586
}
587587
}
588588

589-
return makeParserResult(
590-
Status, new (Context) PrefixUnaryExpr(
591-
Operator, formUnaryArgument(Context, SubExpr.get())));
589+
auto *opCall = PrefixUnaryExpr::create(
590+
Context, Operator, formUnaryArgument(Context, SubExpr.get()));
591+
return makeParserResult(Status, opCall);
592592
}
593593

594594
/// expr-keypath-swift:
@@ -1323,9 +1323,9 @@ Parser::parseExprPostfixSuffix(ParserResult<Expr> Result, bool isExprBasic,
13231323

13241324
Expr *oper = parseExprOperator();
13251325

1326-
Result = makeParserResult(
1327-
Result, new (Context) PostfixUnaryExpr(
1328-
oper, formUnaryArgument(Context, Result.get())));
1326+
auto *opCall = PostfixUnaryExpr::create(
1327+
Context, oper, formUnaryArgument(Context, Result.get()));
1328+
Result = makeParserResult(Result, opCall);
13291329
SyntaxContext->createNodeInPlace(SyntaxKind::PostfixUnaryExpr);
13301330
continue;
13311331
}

lib/Sema/CSApply.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,8 @@ namespace {
655655
ctx);
656656
cs.setType(base, MetatypeType::get(baseTy));
657657

658-
refExpr = new (ctx) DotSyntaxCallExpr(declRefExpr,
659-
SourceLoc(), base);
658+
refExpr = DotSyntaxCallExpr::create(ctx, declRefExpr,
659+
SourceLoc(), base);
660660
auto refType = fullType->castTo<FunctionType>()->getResult();
661661
cs.setType(refExpr, refType);
662662
} else {
@@ -1175,8 +1175,8 @@ namespace {
11751175
}
11761176

11771177
// (Self) -> ...
1178-
ApplyExpr *selfCall = new (context) DotSyntaxCallExpr(
1179-
ref, SourceLoc(), selfOpenedRef);
1178+
ApplyExpr *selfCall =
1179+
DotSyntaxCallExpr::create(context, ref, SourceLoc(), selfOpenedRef);
11801180
selfCall->setType(refTy->getResult());
11811181
cs.cacheType(selfCall);
11821182

@@ -1743,7 +1743,7 @@ namespace {
17431743
if (isa<ConstructorDecl>(member)) {
17441744
// FIXME: Provide type annotation.
17451745
ref = forceUnwrapIfExpected(ref, choice, memberLocator);
1746-
apply = new (context) ConstructorRefCallExpr(ref, base);
1746+
apply = ConstructorRefCallExpr::create(context, ref, base);
17471747
} else if (isUnboundInstanceMember) {
17481748
auto refType = cs.simplifyType(openedType);
17491749
if (!cs.getType(ref)->isEqual(refType)) {
@@ -1762,7 +1762,7 @@ namespace {
17621762
assert((!baseIsInstance || member->isInstanceMember()) &&
17631763
"can't call a static method on an instance");
17641764
ref = forceUnwrapIfExpected(ref, choice, memberLocator);
1765-
apply = new (context) DotSyntaxCallExpr(ref, dotLoc, base);
1765+
apply = DotSyntaxCallExpr::create(context, ref, dotLoc, base);
17661766
if (Implicit) {
17671767
apply->setImplicit();
17681768
}
@@ -3152,8 +3152,8 @@ namespace {
31523152
Expr *ctorRef = buildOtherConstructorRef(overload.openedFullType, callee,
31533153
base, nameLoc, ctorLocator,
31543154
implicit);
3155-
auto *call = new (cs.getASTContext()) DotSyntaxCallExpr(ctorRef, dotLoc,
3156-
base);
3155+
auto *call =
3156+
DotSyntaxCallExpr::create(cs.getASTContext(), ctorRef, dotLoc, base);
31573157

31583158
return finishApply(call, cs.getType(expr), locator, ctorLocator);
31593159
}

lib/Sema/CodeSynthesis.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ synthesizeDesignatedInitOverride(AbstractFunctionDecl *fn, void *context) {
663663
if (auto *funcTy = type->getAs<FunctionType>())
664664
type = funcTy->getResult();
665665
auto *superclassCtorRefExpr =
666-
new (ctx) DotSyntaxCallExpr(ctorRefExpr, SourceLoc(), superRef, type);
666+
DotSyntaxCallExpr::create(ctx, ctorRefExpr, SourceLoc(), superRef, type);
667667
superclassCtorRefExpr->setThrows(false);
668668

669669
auto *bodyParams = ctor->getParameters();

lib/Sema/DerivedConformanceActor.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ static Expr *constructUnownedSerialExecutor(ASTContext &ctx,
7070
// (Builtin.Executor) -> UnownedSerialExecutor
7171
auto metatypeRef = TypeExpr::createImplicit(executorType, ctx);
7272
Type ctorAppliedType = ctorType->getAs<FunctionType>()->getResult();
73-
auto selfApply = new (ctx) ConstructorRefCallExpr(initRef, metatypeRef,
74-
ctorAppliedType);
73+
auto selfApply = ConstructorRefCallExpr::create(ctx, initRef, metatypeRef,
74+
ctorAppliedType);
7575
selfApply->setImplicit(true);
7676
selfApply->setThrows(false);
7777

lib/Sema/DerivedConformanceAdditiveArithmetic.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ deriveBodyMathOperator(AbstractFunctionDecl *funcDecl, MathOperator op) {
9898
auto *nominalTypeExpr = TypeExpr::createImplicitForDecl(
9999
DeclNameLoc(), nominal, funcDecl,
100100
funcDecl->mapTypeIntoContext(nominal->getInterfaceType()));
101-
auto *initExpr = new (C) ConstructorRefCallExpr(initDRE, nominalTypeExpr);
101+
auto *initExpr = ConstructorRefCallExpr::create(C, initDRE, nominalTypeExpr);
102102

103103
// Get operator protocol requirement.
104104
auto *proto = C.getProtocol(KnownProtocolKind::AdditiveArithmetic);
@@ -229,7 +229,7 @@ deriveBodyPropertyGetter(AbstractFunctionDecl *funcDecl, ProtocolDecl *proto,
229229
auto *nominalTypeExpr = TypeExpr::createImplicitForDecl(
230230
DeclNameLoc(), nominal, funcDecl,
231231
funcDecl->mapTypeIntoContext(nominal->getInterfaceType()));
232-
auto *initExpr = new (C) ConstructorRefCallExpr(initDRE, nominalTypeExpr);
232+
auto *initExpr = ConstructorRefCallExpr::create(C, initDRE, nominalTypeExpr);
233233

234234
auto createMemberPropertyExpr = [&](VarDecl *member) -> Expr * {
235235
auto memberType =

lib/Sema/DerivedConformanceCodable.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,8 @@ deriveBodyEncodable_encode(AbstractFunctionDecl *encodeDecl, void *) {
856856
auto *method = UnresolvedDeclRefExpr::createImplicit(C, C.Id_superEncoder);
857857

858858
// container.superEncoder()
859-
auto *superEncoderRef = new (C) DotSyntaxCallExpr(containerExpr,
860-
SourceLoc(), method);
859+
auto *superEncoderRef =
860+
DotSyntaxCallExpr::create(C, containerExpr, SourceLoc(), method);
861861

862862
// encode(to:) expr
863863
auto *encodeDeclRef = new (C) DeclRefExpr(ConcreteDeclRef(encodeDecl),
@@ -868,8 +868,8 @@ deriveBodyEncodable_encode(AbstractFunctionDecl *encodeDecl, void *) {
868868
SourceLoc(), /*Implicit=*/true);
869869

870870
// super.encode(to:)
871-
auto *encodeCall = new (C) DotSyntaxCallExpr(superRef, SourceLoc(),
872-
encodeDeclRef);
871+
auto *encodeCall =
872+
DotSyntaxCallExpr::create(C, superRef, SourceLoc(), encodeDeclRef);
873873

874874
// super.encode(to: container.superEncoder())
875875
Expr *args[1] = {superEncoderRef};

lib/Sema/DerivedConformanceEquatableHashable.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ deriveBodyEquatable_enum_noAssociatedValues_eq(AbstractFunctionDecl *eqDecl,
123123
AccessSemantics::Ordinary, fnType);
124124

125125
fnType = fnType->getResult()->castTo<FunctionType>();
126-
auto *callExpr = new (C) DotSyntaxCallExpr(ref, SourceLoc(), base, fnType);
126+
auto *callExpr =
127+
DotSyntaxCallExpr::create(C, ref, SourceLoc(), base, fnType);
127128
callExpr->setImplicit();
128129
callExpr->setThrows(false);
129130
cmpFuncExpr = callExpr;

lib/Sema/TypeCheckCodeCompletion.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ TypeChecker::getTypeOfCompletionOperator(DeclContext *DC, Expr *LHS,
566566
auto *opExpr = TypeChecker::resolveDeclRefExpr(
567567
&UDRE, DC, /*replaceInvalidRefsWithErrors=*/true);
568568

569+
auto &ctx = DC->getASTContext();
569570
switch (refKind) {
570571
case DeclRefKind::PostfixOperator: {
571572
// (postfix_unary_expr
@@ -574,9 +575,8 @@ TypeChecker::getTypeOfCompletionOperator(DeclContext *DC, Expr *LHS,
574575
// (<LHS>)))
575576
ParenExpr Args(SourceLoc(), LHS, SourceLoc(),
576577
/*hasTrailingClosure=*/false);
577-
PostfixUnaryExpr postfixExpr(opExpr, &Args);
578-
return getTypeOfCompletionOperatorImpl(DC, &postfixExpr,
579-
referencedDecl);
578+
auto *postfixExpr = PostfixUnaryExpr::create(ctx, opExpr, &Args);
579+
return getTypeOfCompletionOperatorImpl(DC, postfixExpr, referencedDecl);
580580
}
581581

582582
case DeclRefKind::BinaryOperator: {
@@ -586,8 +586,8 @@ TypeChecker::getTypeOfCompletionOperator(DeclContext *DC, Expr *LHS,
586586
// (<LHS>)
587587
// (code_completion_expr)))
588588
CodeCompletionExpr dummyRHS(Loc);
589-
auto *binaryExpr = BinaryExpr::create(DC->getASTContext(), LHS, opExpr,
590-
&dummyRHS, /*implicit*/ true);
589+
auto *binaryExpr = BinaryExpr::create(ctx, LHS, opExpr, &dummyRHS,
590+
/*implicit*/ true);
591591
return getTypeOfCompletionOperatorImpl(DC, binaryExpr, referencedDecl);
592592
}
593593

lib/Sema/TypeCheckStorage.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ static Expr *synthesizeCopyWithZoneCall(Expr *Val, VarDecl *VD,
10761076
// Drop the self type
10771077
copyMethodType = copyMethodType->getResult()->castTo<FunctionType>();
10781078

1079-
auto DSCE = new (Ctx) DotSyntaxCallExpr(DRE, SourceLoc(), Val);
1079+
auto DSCE = DotSyntaxCallExpr::create(Ctx, DRE, SourceLoc(), Val);
10801080
DSCE->setImplicit();
10811081
DSCE->setType(copyMethodType);
10821082
DSCE->setThrows(false);
@@ -1561,7 +1561,7 @@ synthesizeObservedSetterBody(AccessorDecl *Set, TargetImpl target,
15611561
auto *SelfDRE =
15621562
buildSelfReference(SelfDecl, SelfAccessorKind::Peer, IsSelfLValue);
15631563
SelfDRE = maybeWrapInOutExpr(SelfDRE, Ctx);
1564-
auto *DSCE = new (Ctx) DotSyntaxCallExpr(Callee, SourceLoc(), SelfDRE);
1564+
auto *DSCE = DotSyntaxCallExpr::create(Ctx, Callee, SourceLoc(), SelfDRE);
15651565

15661566
if (auto funcType = type->getAs<FunctionType>())
15671567
type = funcType->getResult();
@@ -1742,7 +1742,7 @@ synthesizeModifyCoroutineBodyWithSimpleDidSet(AccessorDecl *accessor,
17421742
auto *SelfDRE = buildSelfReference(SelfDecl, SelfAccessorKind::Peer,
17431743
storage->isSetterMutating());
17441744
SelfDRE = maybeWrapInOutExpr(SelfDRE, ctx);
1745-
auto *DSCE = new (ctx) DotSyntaxCallExpr(Callee, SourceLoc(), SelfDRE);
1745+
auto *DSCE = DotSyntaxCallExpr::create(ctx, Callee, SourceLoc(), SelfDRE);
17461746

17471747
if (auto funcType = type->getAs<FunctionType>())
17481748
type = funcType->getResult();

0 commit comments

Comments
 (0)