Skip to content

Commit ebdae7c

Browse files
authored
Merge pull request swiftlang#38504 from hamishknight/construction-site
2 parents d8686f7 + df566e3 commit ebdae7c

27 files changed

+170
-147
lines changed

include/swift/AST/Expr.h

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4721,6 +4721,14 @@ class CallExpr final : public ApplyExpr,
47214721
/*trailingClosures=*/{}, /*implicit=*/true, getType);
47224722
}
47234723

4724+
/// Create a new implicit call expression with no arguments and no
4725+
/// source-location information.
4726+
///
4727+
/// \param fn The nullary function being called.
4728+
static CallExpr *createImplicitEmpty(ASTContext &ctx, Expr *fn) {
4729+
return createImplicit(ctx, fn, {}, {});
4730+
}
4731+
47244732
/// Create a new call expression.
47254733
///
47264734
/// \param fn The function being called
@@ -4779,9 +4787,12 @@ class CallExpr final : public ApplyExpr,
47794787

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

47864797
SourceLoc getLoc() const { return getFn()->getStartLoc(); }
47874798

@@ -4799,9 +4810,13 @@ class PrefixUnaryExpr : public ApplyExpr {
47994810

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

48064821
SourceLoc getLoc() const { return getFn()->getStartLoc(); }
48074822

@@ -4869,15 +4884,19 @@ class SelfApplyExpr : public ApplyExpr {
48694884
/// is modeled as a DeclRefExpr or OverloadSetRefExpr on the method.
48704885
class DotSyntaxCallExpr : public SelfApplyExpr {
48714886
SourceLoc DotLoc;
4872-
4873-
public:
4874-
DotSyntaxCallExpr(Expr *FnExpr, SourceLoc DotLoc, Expr *BaseExpr,
4875-
Type Ty = Type())
4876-
: SelfApplyExpr(ExprKind::DotSyntaxCall, FnExpr, BaseExpr, Ty),
4877-
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) {
48784892
setImplicit(DotLoc.isInvalid());
48794893
}
48804894

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

48834902
SourceLoc getLoc() const;
@@ -4893,9 +4912,12 @@ class DotSyntaxCallExpr : public SelfApplyExpr {
48934912
/// actual reference to function which returns the constructor is modeled
48944913
/// as a DeclRefExpr.
48954914
class ConstructorRefCallExpr : public SelfApplyExpr {
4915+
ConstructorRefCallExpr(Expr *fnExpr, Expr *baseExpr, Type ty = Type())
4916+
: SelfApplyExpr(ExprKind::ConstructorRefCall, fnExpr, baseExpr, ty) {}
4917+
48964918
public:
4897-
ConstructorRefCallExpr(Expr *FnExpr, Expr *BaseExpr, Type Ty = Type())
4898-
: SelfApplyExpr(ExprKind::ConstructorRefCall, FnExpr, BaseExpr, Ty) {}
4919+
static ConstructorRefCallExpr *create(ASTContext &ctx, Expr *fnExpr,
4920+
Expr *baseExpr, Type ty = Type());
48994921

49004922
SourceLoc getLoc() const { return getFn()->getLoc(); }
49014923
SourceLoc getStartLoc() const { return getBase()->getStartLoc(); }
@@ -5587,7 +5609,8 @@ class KeyPathExpr : public Expr {
55875609
Kind KindValue;
55885610
Type ComponentType;
55895611
SourceLoc Loc;
5590-
5612+
5613+
// Private constructor for subscript component.
55915614
explicit Component(ASTContext *ctxForCopyingLabels,
55925615
DeclNameOrRef decl,
55935616
Expr *indexExpr,
@@ -5597,27 +5620,31 @@ class KeyPathExpr : public Expr {
55975620
Type type,
55985621
SourceLoc loc);
55995622

5600-
// Private constructor for tuple element kind
5601-
Component(unsigned tupleIndex, Type elementType, SourceLoc loc)
5602-
: Component(nullptr, {}, nullptr, {}, {}, Kind::TupleElement,
5603-
elementType, loc) {
5623+
// Private constructor for property or #keyPath dictionary key.
5624+
explicit Component(DeclNameOrRef decl, Kind kind, Type type, SourceLoc loc)
5625+
: Component(kind, type, loc) {
5626+
assert(kind == Kind::Property || kind == Kind::UnresolvedProperty ||
5627+
kind == Kind::DictionaryKey);
5628+
Decl = decl;
5629+
}
5630+
5631+
// Private constructor for tuple element kind.
5632+
explicit Component(unsigned tupleIndex, Type elementType, SourceLoc loc)
5633+
: Component(Kind::TupleElement, elementType, loc) {
56045634
TupleIndex = tupleIndex;
56055635
}
5606-
5636+
5637+
// Private constructor for basic components with no additional information.
5638+
explicit Component(Kind kind, Type type, SourceLoc loc)
5639+
: Decl(), KindValue(kind), ComponentType(type), Loc(loc) {}
5640+
56075641
public:
5608-
Component()
5609-
: Component(nullptr, {}, nullptr, {}, {}, Kind::Invalid,
5610-
Type(), SourceLoc())
5611-
{}
5612-
5642+
Component() : Component(Kind::Invalid, Type(), SourceLoc()) {}
5643+
56135644
/// Create an unresolved component for a property.
56145645
static Component forUnresolvedProperty(DeclNameRef UnresolvedName,
56155646
SourceLoc Loc) {
5616-
return Component(nullptr,
5617-
UnresolvedName, nullptr, {}, {},
5618-
Kind::UnresolvedProperty,
5619-
Type(),
5620-
Loc);
5647+
return Component(UnresolvedName, Kind::UnresolvedProperty, Type(), Loc);
56215648
}
56225649

56235650
/// Create an unresolved component for a subscript.
@@ -5647,38 +5674,26 @@ class KeyPathExpr : public Expr {
56475674

56485675
/// Create an unresolved optional force `!` component.
56495676
static Component forUnresolvedOptionalForce(SourceLoc BangLoc) {
5650-
return Component(nullptr, {}, nullptr, {}, {},
5651-
Kind::OptionalForce,
5652-
Type(),
5653-
BangLoc);
5677+
return Component(Kind::OptionalForce, Type(), BangLoc);
56545678
}
56555679

56565680
/// Create an unresolved optional chain `?` component.
56575681
static Component forUnresolvedOptionalChain(SourceLoc QuestionLoc) {
5658-
return Component(nullptr, {}, nullptr, {}, {},
5659-
Kind::OptionalChain,
5660-
Type(),
5661-
QuestionLoc);
5682+
return Component(Kind::OptionalChain, Type(), QuestionLoc);
56625683
}
56635684

56645685
/// Create a component for a property.
56655686
static Component forProperty(ConcreteDeclRef property,
56665687
Type propertyType,
56675688
SourceLoc loc) {
5668-
return Component(nullptr, property, nullptr, {}, {},
5669-
Kind::Property,
5670-
propertyType,
5671-
loc);
5689+
return Component(property, Kind::Property, propertyType, loc);
56725690
}
56735691

56745692
/// Create a component for a dictionary key (#keyPath only).
56755693
static Component forDictionaryKey(DeclNameRef UnresolvedName,
56765694
Type valueType,
56775695
SourceLoc loc) {
5678-
return Component(nullptr, UnresolvedName, nullptr, {}, {},
5679-
Kind::DictionaryKey,
5680-
valueType,
5681-
loc);
5696+
return Component(UnresolvedName, Kind::DictionaryKey, valueType, loc);
56825697
}
56835698

56845699
/// Create a component for a subscript.
@@ -5704,32 +5719,24 @@ class KeyPathExpr : public Expr {
57045719

57055720
/// Create an optional-forcing `!` component.
57065721
static Component forOptionalForce(Type forcedType, SourceLoc bangLoc) {
5707-
return Component(nullptr, {}, nullptr, {}, {},
5708-
Kind::OptionalForce, forcedType,
5709-
bangLoc);
5722+
return Component(Kind::OptionalForce, forcedType, bangLoc);
57105723
}
57115724

57125725
/// Create an optional-chaining `?` component.
57135726
static Component forOptionalChain(Type unwrappedType,
57145727
SourceLoc questionLoc) {
5715-
return Component(nullptr, {}, nullptr, {}, {},
5716-
Kind::OptionalChain, unwrappedType,
5717-
questionLoc);
5728+
return Component(Kind::OptionalChain, unwrappedType, questionLoc);
57185729
}
57195730

57205731
/// Create an optional-wrapping component. This doesn't have a surface
57215732
/// syntax but may appear when the non-optional result of an optional chain
57225733
/// is implicitly wrapped.
57235734
static Component forOptionalWrap(Type wrappedType) {
5724-
return Component(nullptr, {}, nullptr, {}, {},
5725-
Kind::OptionalWrap, wrappedType,
5726-
SourceLoc());
5735+
return Component(Kind::OptionalWrap, wrappedType, SourceLoc());
57275736
}
57285737

57295738
static Component forIdentity(SourceLoc selfLoc) {
5730-
return Component(nullptr, {}, nullptr, {}, {},
5731-
Kind::Identity, Type(),
5732-
selfLoc);
5739+
return Component(Kind::Identity, Type(), selfLoc);
57335740
}
57345741

57355742
static Component forTupleElement(unsigned fieldNumber,
@@ -5739,8 +5746,7 @@ class KeyPathExpr : public Expr {
57395746
}
57405747

57415748
static Component forCodeCompletion(SourceLoc Loc) {
5742-
return Component(nullptr, {}, nullptr, {}, {}, Kind::CodeCompletion,
5743-
Type(), Loc);
5749+
return Component(Kind::CodeCompletion, Type(), Loc);
57445750
}
57455751

57465752
SourceLoc getLoc() const {

include/swift/AST/Types.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3018,19 +3018,16 @@ class AnyFunctionType : public TypeBase {
30183018
}
30193019

30203020
public:
3021-
/// Break an input type into an array of \c AnyFunctionType::Params.
3022-
static void decomposeInput(Type type,
3021+
/// Break a tuple or paren type into an array of \c AnyFunctionType::Params.
3022+
static void decomposeTuple(Type type,
30233023
SmallVectorImpl<Param> &result);
30243024

3025-
/// Take an array of parameters and turn it into an input type.
3026-
///
3027-
/// The result type is only there as a way to extract the ASTContext when
3028-
/// needed.
3029-
static Type composeInput(ASTContext &ctx, ArrayRef<Param> params,
3025+
/// Take an array of parameters and turn it into a tuple or paren type.
3026+
static Type composeTuple(ASTContext &ctx, ArrayRef<Param> params,
30303027
bool canonicalVararg);
3031-
static Type composeInput(ASTContext &ctx, CanParamArrayRef params,
3028+
static Type composeTuple(ASTContext &ctx, CanParamArrayRef params,
30323029
bool canonicalVararg) {
3033-
return composeInput(ctx, params.getOriginalArray(), canonicalVararg);
3030+
return composeTuple(ctx, params.getOriginalArray(), canonicalVararg);
30343031
}
30353032

30363033
/// Given two arrays of parameters determine if they are equal in their

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ SwiftDeclCollector::constructTypeNode(Type T, TypeInitInfo Info) {
15001500
// Still, return type first
15011501
Root->addChild(constructTypeNode(Fun->getResult()));
15021502

1503-
auto Input = AnyFunctionType::composeInput(Fun->getASTContext(),
1503+
auto Input = AnyFunctionType::composeTuple(Fun->getASTContext(),
15041504
Fun->getParams(),
15051505
/*canonicalVararg=*/false);
15061506
Root->addChild(constructTypeNode(Input));

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3345,7 +3345,7 @@ AnyFunctionType *AnyFunctionType::withExtInfo(ExtInfo info) const {
33453345
getParams(), getResult(), info);
33463346
}
33473347

3348-
void AnyFunctionType::decomposeInput(
3348+
void AnyFunctionType::decomposeTuple(
33493349
Type type, SmallVectorImpl<AnyFunctionType::Param> &result) {
33503350
switch (type->getKind()) {
33513351
case TypeKind::Tuple: {
@@ -3394,7 +3394,7 @@ Type AnyFunctionType::Param::getParameterType(bool forCanonical,
33943394
return type;
33953395
}
33963396

3397-
Type AnyFunctionType::composeInput(ASTContext &ctx, ArrayRef<Param> params,
3397+
Type AnyFunctionType::composeTuple(ASTContext &ctx, ArrayRef<Param> params,
33983398
bool canonicalVararg) {
33993399
SmallVector<TupleTypeElt, 4> elements;
34003400
for (const auto &param : params) {

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,7 @@ class Verifier : public ASTWalker {
18001800

18011801
SmallVector<AnyFunctionType::Param, 8> Args;
18021802
Type InputExprTy = E->getArg()->getType();
1803-
AnyFunctionType::decomposeInput(InputExprTy, Args);
1803+
AnyFunctionType::decomposeTuple(InputExprTy, Args);
18041804
auto Params = FT->getParams();
18051805
if (!equalParamsIgnoringIsolation(Args, Params)) {
18061806
Out << "Argument type does not match parameter type in ApplyExpr:"

lib/AST/Expr.cpp

Lines changed: 24 additions & 1 deletion
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
}
@@ -2379,7 +2402,7 @@ KeyPathExpr::Component::Component(ASTContext *ctxForCopyingLabels,
23792402
: Decl(decl), SubscriptIndexExpr(indexExpr), KindValue(kind),
23802403
ComponentType(type), Loc(loc)
23812404
{
2382-
assert(kind != Kind::TupleElement || subscriptLabels.empty());
2405+
assert(kind == Kind::Subscript || kind == Kind::UnresolvedSubscript);
23832406
assert(subscriptLabels.size() == indexHashables.size()
23842407
|| indexHashables.empty());
23852408
SubscriptLabelsData = subscriptLabels.data();

lib/ClangImporter/ImportDecl.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ synthesizeStructDefaultConstructorBody(AbstractFunctionDecl *afd,
12781278
FunctionType::ExtInfo info;
12791279
zeroInitializerRef->setType(FunctionType::get({}, selfType, info));
12801280

1281-
auto call = CallExpr::createImplicit(ctx, zeroInitializerRef, {}, {});
1281+
auto call = CallExpr::createImplicitEmpty(ctx, zeroInitializerRef);
12821282
call->setType(selfType);
12831283
call->setThrows(false);
12841284

@@ -6492,8 +6492,8 @@ Decl *SwiftDeclConverter::importEnumCaseAlias(
64926492
/*implicit*/ true);
64936493
constantRef->setType(enumElt->getInterfaceType());
64946494

6495-
auto instantiate = new (Impl.SwiftContext)
6496-
DotSyntaxCallExpr(constantRef, SourceLoc(), typeRef);
6495+
auto instantiate = DotSyntaxCallExpr::create(Impl.SwiftContext, constantRef,
6496+
SourceLoc(), typeRef);
64976497
instantiate->setType(importedEnumTy);
64986498
instantiate->setThrows(false);
64996499

@@ -7636,7 +7636,7 @@ createAccessorImplCallExpr(FuncDecl *accessorImpl,
76367636
accessorImplExpr->setType(accessorImpl->getInterfaceType());
76377637

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

@@ -9652,8 +9652,8 @@ synthesizeConstantGetterBody(AbstractFunctionDecl *afd, void *voidContext) {
96529652

96539653
// (Self) -> ...
96549654
initTy = initTy->castTo<FunctionType>()->getResult();
9655-
auto initRef = new (ctx) DotSyntaxCallExpr(declRef, SourceLoc(),
9656-
typeRef, initTy);
9655+
auto initRef =
9656+
DotSyntaxCallExpr::create(ctx, declRef, SourceLoc(), typeRef, initTy);
96579657
initRef->setThrows(false);
96589658

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

0 commit comments

Comments
 (0)