Skip to content

[AST] Add some factory constructors #38504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 64 additions & 58 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -4721,6 +4721,14 @@ class CallExpr final : public ApplyExpr,
/*trailingClosures=*/{}, /*implicit=*/true, getType);
}

/// Create a new implicit call expression with no arguments and no
/// source-location information.
///
/// \param fn The nullary function being called.
static CallExpr *createImplicitEmpty(ASTContext &ctx, Expr *fn) {
return createImplicit(ctx, fn, {}, {});
}

/// Create a new call expression.
///
/// \param fn The function being called
Expand Down Expand Up @@ -4779,9 +4787,12 @@ class CallExpr final : public ApplyExpr,

/// PrefixUnaryExpr - Prefix unary expressions like '!y'.
class PrefixUnaryExpr : public ApplyExpr {
PrefixUnaryExpr(Expr *fn, Expr *operand, Type ty = Type())
: ApplyExpr(ExprKind::PrefixUnary, fn, operand, /*implicit*/ false, ty) {}

public:
PrefixUnaryExpr(Expr *Fn, Expr *Arg, Type Ty = Type())
: ApplyExpr(ExprKind::PrefixUnary, Fn, Arg, /*Implicit=*/false, Ty) {}
static PrefixUnaryExpr *create(ASTContext &ctx, Expr *fn, Expr *operand,
Type ty = Type());

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

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

/// PostfixUnaryExpr - Postfix unary expressions like 'y!'.
class PostfixUnaryExpr : public ApplyExpr {
PostfixUnaryExpr(Expr *fn, Expr *operand, Type ty = Type())
: ApplyExpr(ExprKind::PostfixUnary, fn, operand, /*implicit*/ false, ty) {
}

public:
PostfixUnaryExpr(Expr *Fn, Expr *Arg, Type Ty = Type())
: ApplyExpr(ExprKind::PostfixUnary, Fn, Arg, /*Implicit=*/false, Ty) {}
static PostfixUnaryExpr *create(ASTContext &ctx, Expr *fn, Expr *operand,
Type ty = Type());

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

Expand Down Expand Up @@ -4869,15 +4884,19 @@ class SelfApplyExpr : public ApplyExpr {
/// is modeled as a DeclRefExpr or OverloadSetRefExpr on the method.
class DotSyntaxCallExpr : public SelfApplyExpr {
SourceLoc DotLoc;

public:
DotSyntaxCallExpr(Expr *FnExpr, SourceLoc DotLoc, Expr *BaseExpr,
Type Ty = Type())
: SelfApplyExpr(ExprKind::DotSyntaxCall, FnExpr, BaseExpr, Ty),
DotLoc(DotLoc) {

DotSyntaxCallExpr(Expr *fnExpr, SourceLoc dotLoc, Expr *baseExpr,
Type ty = Type())
: SelfApplyExpr(ExprKind::DotSyntaxCall, fnExpr, baseExpr, ty),
DotLoc(dotLoc) {
setImplicit(DotLoc.isInvalid());
}

public:
static DotSyntaxCallExpr *create(ASTContext &ctx, Expr *fnExpr,
SourceLoc dotLoc, Expr *baseExpr,
Type ty = Type());

SourceLoc getDotLoc() const { return DotLoc; }

SourceLoc getLoc() const;
Expand All @@ -4893,9 +4912,12 @@ class DotSyntaxCallExpr : public SelfApplyExpr {
/// actual reference to function which returns the constructor is modeled
/// as a DeclRefExpr.
class ConstructorRefCallExpr : public SelfApplyExpr {
ConstructorRefCallExpr(Expr *fnExpr, Expr *baseExpr, Type ty = Type())
: SelfApplyExpr(ExprKind::ConstructorRefCall, fnExpr, baseExpr, ty) {}

public:
ConstructorRefCallExpr(Expr *FnExpr, Expr *BaseExpr, Type Ty = Type())
: SelfApplyExpr(ExprKind::ConstructorRefCall, FnExpr, BaseExpr, Ty) {}
static ConstructorRefCallExpr *create(ASTContext &ctx, Expr *fnExpr,
Expr *baseExpr, Type ty = Type());

SourceLoc getLoc() const { return getFn()->getLoc(); }
SourceLoc getStartLoc() const { return getBase()->getStartLoc(); }
Expand Down Expand Up @@ -5587,7 +5609,8 @@ class KeyPathExpr : public Expr {
Kind KindValue;
Type ComponentType;
SourceLoc Loc;


// Private constructor for subscript component.
explicit Component(ASTContext *ctxForCopyingLabels,
DeclNameOrRef decl,
Expr *indexExpr,
Expand All @@ -5597,27 +5620,31 @@ class KeyPathExpr : public Expr {
Type type,
SourceLoc loc);

// Private constructor for tuple element kind
Component(unsigned tupleIndex, Type elementType, SourceLoc loc)
: Component(nullptr, {}, nullptr, {}, {}, Kind::TupleElement,
elementType, loc) {
// Private constructor for property or #keyPath dictionary key.
explicit Component(DeclNameOrRef decl, Kind kind, Type type, SourceLoc loc)
: Component(kind, type, loc) {
assert(kind == Kind::Property || kind == Kind::UnresolvedProperty ||
kind == Kind::DictionaryKey);
Decl = decl;
}

// Private constructor for tuple element kind.
explicit Component(unsigned tupleIndex, Type elementType, SourceLoc loc)
: Component(Kind::TupleElement, elementType, loc) {
TupleIndex = tupleIndex;
}


// Private constructor for basic components with no additional information.
explicit Component(Kind kind, Type type, SourceLoc loc)
: Decl(), KindValue(kind), ComponentType(type), Loc(loc) {}

public:
Component()
: Component(nullptr, {}, nullptr, {}, {}, Kind::Invalid,
Type(), SourceLoc())
{}

Component() : Component(Kind::Invalid, Type(), SourceLoc()) {}

/// Create an unresolved component for a property.
static Component forUnresolvedProperty(DeclNameRef UnresolvedName,
SourceLoc Loc) {
return Component(nullptr,
UnresolvedName, nullptr, {}, {},
Kind::UnresolvedProperty,
Type(),
Loc);
return Component(UnresolvedName, Kind::UnresolvedProperty, Type(), Loc);
}

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

/// Create an unresolved optional force `!` component.
static Component forUnresolvedOptionalForce(SourceLoc BangLoc) {
return Component(nullptr, {}, nullptr, {}, {},
Kind::OptionalForce,
Type(),
BangLoc);
return Component(Kind::OptionalForce, Type(), BangLoc);
}

/// Create an unresolved optional chain `?` component.
static Component forUnresolvedOptionalChain(SourceLoc QuestionLoc) {
return Component(nullptr, {}, nullptr, {}, {},
Kind::OptionalChain,
Type(),
QuestionLoc);
return Component(Kind::OptionalChain, Type(), QuestionLoc);
}

/// Create a component for a property.
static Component forProperty(ConcreteDeclRef property,
Type propertyType,
SourceLoc loc) {
return Component(nullptr, property, nullptr, {}, {},
Kind::Property,
propertyType,
loc);
return Component(property, Kind::Property, propertyType, loc);
}

/// Create a component for a dictionary key (#keyPath only).
static Component forDictionaryKey(DeclNameRef UnresolvedName,
Type valueType,
SourceLoc loc) {
return Component(nullptr, UnresolvedName, nullptr, {}, {},
Kind::DictionaryKey,
valueType,
loc);
return Component(UnresolvedName, Kind::DictionaryKey, valueType, loc);
}

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

/// Create an optional-forcing `!` component.
static Component forOptionalForce(Type forcedType, SourceLoc bangLoc) {
return Component(nullptr, {}, nullptr, {}, {},
Kind::OptionalForce, forcedType,
bangLoc);
return Component(Kind::OptionalForce, forcedType, bangLoc);
}

/// Create an optional-chaining `?` component.
static Component forOptionalChain(Type unwrappedType,
SourceLoc questionLoc) {
return Component(nullptr, {}, nullptr, {}, {},
Kind::OptionalChain, unwrappedType,
questionLoc);
return Component(Kind::OptionalChain, unwrappedType, questionLoc);
}

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

static Component forIdentity(SourceLoc selfLoc) {
return Component(nullptr, {}, nullptr, {}, {},
Kind::Identity, Type(),
selfLoc);
return Component(Kind::Identity, Type(), selfLoc);
}

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

static Component forCodeCompletion(SourceLoc Loc) {
return Component(nullptr, {}, nullptr, {}, {}, Kind::CodeCompletion,
Type(), Loc);
return Component(Kind::CodeCompletion, Type(), Loc);
}

SourceLoc getLoc() const {
Expand Down
15 changes: 6 additions & 9 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3018,19 +3018,16 @@ class AnyFunctionType : public TypeBase {
}

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

/// Take an array of parameters and turn it into an input type.
///
/// The result type is only there as a way to extract the ASTContext when
/// needed.
static Type composeInput(ASTContext &ctx, ArrayRef<Param> params,
/// Take an array of parameters and turn it into a tuple or paren type.
static Type composeTuple(ASTContext &ctx, ArrayRef<Param> params,
bool canonicalVararg);
static Type composeInput(ASTContext &ctx, CanParamArrayRef params,
static Type composeTuple(ASTContext &ctx, CanParamArrayRef params,
bool canonicalVararg) {
return composeInput(ctx, params.getOriginalArray(), canonicalVararg);
return composeTuple(ctx, params.getOriginalArray(), canonicalVararg);
}

/// Given two arrays of parameters determine if they are equal in their
Expand Down
2 changes: 1 addition & 1 deletion lib/APIDigester/ModuleAnalyzerNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@ SwiftDeclCollector::constructTypeNode(Type T, TypeInitInfo Info) {
// Still, return type first
Root->addChild(constructTypeNode(Fun->getResult()));

auto Input = AnyFunctionType::composeInput(Fun->getASTContext(),
auto Input = AnyFunctionType::composeTuple(Fun->getASTContext(),
Fun->getParams(),
/*canonicalVararg=*/false);
Root->addChild(constructTypeNode(Input));
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3317,7 +3317,7 @@ AnyFunctionType *AnyFunctionType::withExtInfo(ExtInfo info) const {
getParams(), getResult(), info);
}

void AnyFunctionType::decomposeInput(
void AnyFunctionType::decomposeTuple(
Type type, SmallVectorImpl<AnyFunctionType::Param> &result) {
switch (type->getKind()) {
case TypeKind::Tuple: {
Expand Down Expand Up @@ -3366,7 +3366,7 @@ Type AnyFunctionType::Param::getParameterType(bool forCanonical,
return type;
}

Type AnyFunctionType::composeInput(ASTContext &ctx, ArrayRef<Param> params,
Type AnyFunctionType::composeTuple(ASTContext &ctx, ArrayRef<Param> params,
bool canonicalVararg) {
SmallVector<TupleTypeElt, 4> elements;
for (const auto &param : params) {
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ class Verifier : public ASTWalker {

SmallVector<AnyFunctionType::Param, 8> Args;
Type InputExprTy = E->getArg()->getType();
AnyFunctionType::decomposeInput(InputExprTy, Args);
AnyFunctionType::decomposeTuple(InputExprTy, Args);
auto Params = FT->getParams();
if (!equalParamsIgnoringIsolation(Args, Params)) {
Out << "Argument type does not match parameter type in ApplyExpr:"
Expand Down
25 changes: 24 additions & 1 deletion lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,16 @@ Expr *CallExpr::getDirectCallee() const {
}
}

PrefixUnaryExpr *PrefixUnaryExpr::create(ASTContext &ctx, Expr *fn,
Expr *operand, Type ty) {
return new (ctx) PrefixUnaryExpr(fn, operand, ty);
}

PostfixUnaryExpr *PostfixUnaryExpr::create(ASTContext &ctx, Expr *fn,
Expr *operand, Type ty) {
return new (ctx) PostfixUnaryExpr(fn, operand, ty);
}

BinaryExpr *BinaryExpr::create(ASTContext &ctx, Expr *lhs, Expr *fn, Expr *rhs,
bool implicit, Type ty) {
auto *packedArg = TupleExpr::createImplicit(ctx, {lhs, rhs}, /*labels*/ {});
Expand All @@ -1787,6 +1797,12 @@ BinaryExpr *BinaryExpr::create(ASTContext &ctx, Expr *lhs, Expr *fn, Expr *rhs,
return new (ctx) BinaryExpr(fn, packedArg, implicit, ty);
}

DotSyntaxCallExpr *DotSyntaxCallExpr::create(ASTContext &ctx, Expr *fnExpr,
SourceLoc dotLoc, Expr *baseExpr,
Type ty) {
return new (ctx) DotSyntaxCallExpr(fnExpr, dotLoc, baseExpr, ty);
}

SourceLoc DotSyntaxCallExpr::getLoc() const {
if (isImplicit()) {
SourceLoc baseLoc = getBase()->getLoc();
Expand Down Expand Up @@ -1814,6 +1830,13 @@ SourceLoc DotSyntaxCallExpr::getEndLoc() const {
return getFn()->getEndLoc();
}

ConstructorRefCallExpr *ConstructorRefCallExpr::create(ASTContext &ctx,
Expr *fnExpr,
Expr *baseExpr,
Type ty) {
return new (ctx) ConstructorRefCallExpr(fnExpr, baseExpr, ty);
}

void ExplicitCastExpr::setCastType(Type type) {
CastTy->setType(MetatypeType::get(type));
}
Expand Down Expand Up @@ -2379,7 +2402,7 @@ KeyPathExpr::Component::Component(ASTContext *ctxForCopyingLabels,
: Decl(decl), SubscriptIndexExpr(indexExpr), KindValue(kind),
ComponentType(type), Loc(loc)
{
assert(kind != Kind::TupleElement || subscriptLabels.empty());
assert(kind == Kind::Subscript || kind == Kind::UnresolvedSubscript);
assert(subscriptLabels.size() == indexHashables.size()
|| indexHashables.empty());
SubscriptLabelsData = subscriptLabels.data();
Expand Down
12 changes: 6 additions & 6 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ synthesizeStructDefaultConstructorBody(AbstractFunctionDecl *afd,
FunctionType::ExtInfo info;
zeroInitializerRef->setType(FunctionType::get({}, selfType, info));

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

Expand Down Expand Up @@ -6490,8 +6490,8 @@ Decl *SwiftDeclConverter::importEnumCaseAlias(
/*implicit*/ true);
constantRef->setType(enumElt->getInterfaceType());

auto instantiate = new (Impl.SwiftContext)
DotSyntaxCallExpr(constantRef, SourceLoc(), typeRef);
auto instantiate = DotSyntaxCallExpr::create(Impl.SwiftContext, constantRef,
SourceLoc(), typeRef);
instantiate->setType(importedEnumTy);
instantiate->setThrows(false);

Expand Down Expand Up @@ -7634,7 +7634,7 @@ createAccessorImplCallExpr(FuncDecl *accessorImpl,
accessorImplExpr->setType(accessorImpl->getInterfaceType());

auto accessorImplDotCallExpr =
new (ctx) DotSyntaxCallExpr(accessorImplExpr, SourceLoc(), selfExpr);
DotSyntaxCallExpr::create(ctx, accessorImplExpr, SourceLoc(), selfExpr);
accessorImplDotCallExpr->setType(accessorImpl->getMethodInterfaceType());
accessorImplDotCallExpr->setThrows(false);

Expand Down Expand Up @@ -9662,8 +9662,8 @@ synthesizeConstantGetterBody(AbstractFunctionDecl *afd, void *voidContext) {

// (Self) -> ...
initTy = initTy->castTo<FunctionType>()->getResult();
auto initRef = new (ctx) DotSyntaxCallExpr(declRef, SourceLoc(),
typeRef, initTy);
auto initRef =
DotSyntaxCallExpr::create(ctx, declRef, SourceLoc(), typeRef, initTy);
initRef->setThrows(false);

// (rawValue: T) -> ...
Expand Down
Loading