Skip to content

Commit 6b9bcf3

Browse files
committed
[AST] Change DotSyntaxCallExpr to take an Argument base
This allows us to more easily propagate inout information to it, which will become a necessity once InOutExpr is removed.
1 parent ee49794 commit 6b9bcf3

12 files changed

+132
-120
lines changed

Diff for: include/swift/AST/ArgumentList.h

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class Argument final {
6868
/// The argument label written in the call.
6969
Identifier getLabel() const { return Label; }
7070

71+
/// Whether the argument has a non-empty label. Note that this returns `false`
72+
/// for an explicitly specified empty label e.g `_: {}` for a trailing
73+
/// closure.
74+
bool hasLabel() const { return !Label.empty(); }
75+
7176
/// Set a new argument label.
7277
///
7378
/// Note this is marked \c & to prevent its use on an rvalue Argument vended

Diff for: include/swift/AST/Expr.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -4736,8 +4736,13 @@ class DotSyntaxCallExpr : public SelfApplyExpr {
47364736
}
47374737

47384738
public:
4739+
/// Create a new method reference to \p fnExpr on the base value \p baseArg.
4740+
///
4741+
/// If this is for a 'mutating' method, \p baseArg should be created using
4742+
/// \c Argument::implicitInOut. Otherwise, \p Argument::unlabeled should be
4743+
/// used. \p baseArg must not be labeled.
47394744
static DotSyntaxCallExpr *create(ASTContext &ctx, Expr *fnExpr,
4740-
SourceLoc dotLoc, Expr *baseExpr,
4745+
SourceLoc dotLoc, Argument baseArg,
47414746
Type ty = Type());
47424747

47434748
SourceLoc getDotLoc() const { return DotLoc; }

Diff for: lib/AST/Expr.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1571,9 +1571,10 @@ BinaryExpr *BinaryExpr::create(ASTContext &ctx, Expr *lhs, Expr *fn, Expr *rhs,
15711571
}
15721572

15731573
DotSyntaxCallExpr *DotSyntaxCallExpr::create(ASTContext &ctx, Expr *fnExpr,
1574-
SourceLoc dotLoc, Expr *baseExpr,
1574+
SourceLoc dotLoc, Argument baseArg,
15751575
Type ty) {
1576-
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {baseExpr});
1576+
assert(!baseArg.hasLabel());
1577+
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {baseArg.getExpr()});
15771578
return new (ctx) DotSyntaxCallExpr(fnExpr, dotLoc, argList, ty);
15781579
}
15791580

Diff for: lib/ClangImporter/ClangImporter.cpp

+41-49
Original file line numberDiff line numberDiff line change
@@ -4444,29 +4444,23 @@ DeclRefExpr *getInteropStaticCastDeclRefExpr(ASTContext &ctx,
44444444
// %2 = __swift_interopStaticCast<UnsafeMutablePointer<Base>?>(%1)
44454445
// %3 = %2!
44464446
// return %3.pointee
4447-
MemberRefExpr *getInOutSelfInteropStaticCast(FuncDecl *funcDecl,
4448-
NominalTypeDecl *baseStruct,
4449-
NominalTypeDecl *derivedStruct) {
4447+
MemberRefExpr *getSelfInteropStaticCast(FuncDecl *funcDecl,
4448+
NominalTypeDecl *baseStruct,
4449+
NominalTypeDecl *derivedStruct) {
44504450
auto &ctx = funcDecl->getASTContext();
44514451

4452-
auto inoutSelf = [&ctx](FuncDecl *funcDecl) {
4453-
auto inoutSelfDecl = funcDecl->getImplicitSelfDecl();
4452+
auto mutableSelf = [&ctx](FuncDecl *funcDecl) {
4453+
auto selfDecl = funcDecl->getImplicitSelfDecl();
44544454

4455-
auto inoutSelfRef =
4456-
new (ctx) DeclRefExpr(inoutSelfDecl, DeclNameLoc(), /*implicit*/ true);
4457-
inoutSelfRef->setType(LValueType::get(inoutSelfDecl->getInterfaceType()));
4455+
auto selfRef =
4456+
new (ctx) DeclRefExpr(selfDecl, DeclNameLoc(), /*implicit*/ true);
4457+
selfRef->setType(LValueType::get(selfDecl->getInterfaceType()));
44584458

4459-
auto inoutSelf = new (ctx) InOutExpr(
4460-
SourceLoc(), inoutSelfRef,
4461-
funcDecl->mapTypeIntoContext(inoutSelfDecl->getValueInterfaceType()),
4462-
/*implicit*/ true);
4463-
inoutSelf->setType(InOutType::get(inoutSelfDecl->getInterfaceType()));
4464-
4465-
return inoutSelf;
4459+
return selfRef;
44664460
}(funcDecl);
44674461

44684462
auto createCallToBuiltin = [&](Identifier name, ArrayRef<Type> substTypes,
4469-
Expr *arg) {
4463+
Argument arg) {
44704464
auto builtinFn = cast<FuncDecl>(getBuiltinValueDecl(ctx, name));
44714465
auto substMap =
44724466
SubstitutionMap::get(builtinFn->getGenericSignature(), substTypes,
@@ -4479,22 +4473,23 @@ MemberRefExpr *getInOutSelfInteropStaticCast(FuncDecl *funcDecl,
44794473
if (auto genericFnType = dyn_cast<GenericFunctionType>(fnType.getPointer()))
44804474
fnType = genericFnType->substGenericArgs(substMap);
44814475
builtinFnRefExpr->setType(fnType);
4482-
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {arg});
4476+
auto *argList = ArgumentList::createImplicit(ctx, {arg});
44834477
auto callExpr = CallExpr::create(ctx, builtinFnRefExpr, argList, /*implicit*/ true);
44844478
callExpr->setThrows(false);
44854479
return callExpr;
44864480
};
44874481

4488-
auto rawSelfPointer =
4489-
createCallToBuiltin(ctx.getIdentifier("addressof"),
4490-
{derivedStruct->getSelfInterfaceType()}, inoutSelf);
4482+
auto rawSelfPointer = createCallToBuiltin(
4483+
ctx.getIdentifier("addressof"), {derivedStruct->getSelfInterfaceType()},
4484+
Argument::implicitInOut(ctx, mutableSelf));
44914485
rawSelfPointer->setType(ctx.TheRawPointerType);
44924486

44934487
auto derivedPtrType = derivedStruct->getSelfInterfaceType()->wrapInPointer(
44944488
PTK_UnsafeMutablePointer);
4495-
auto selfPointer = createCallToBuiltin(
4496-
ctx.getIdentifier("reinterpretCast"),
4497-
{ctx.TheRawPointerType, derivedPtrType}, rawSelfPointer);
4489+
auto selfPointer =
4490+
createCallToBuiltin(ctx.getIdentifier("reinterpretCast"),
4491+
{ctx.TheRawPointerType, derivedPtrType},
4492+
Argument::unlabeled(rawSelfPointer));
44984493
selfPointer->setType(derivedPtrType);
44994494

45004495
auto staticCastRefExpr = getInteropStaticCastDeclRefExpr(
@@ -4554,14 +4549,11 @@ synthesizeBaseClassMethodBody(AbstractFunctionDecl *afd, void *context) {
45544549
forwardingParams.push_back(paramRefExpr);
45554550
}
45564551

4557-
Expr *casted = nullptr;
4558-
if (funcDecl->isMutating()) {
4559-
auto pointeeMemberRefExpr =
4560-
getInOutSelfInteropStaticCast(funcDecl, baseStruct, derivedStruct);
4561-
casted = new (ctx) InOutExpr(SourceLoc(), pointeeMemberRefExpr, baseType,
4562-
/*implicit*/ true);
4563-
casted->setType(InOutType::get(baseType));
4564-
} else {
4552+
Argument casted = [&]() {
4553+
if (funcDecl->isMutating()) {
4554+
return Argument::implicitInOut(
4555+
ctx, getSelfInteropStaticCast(funcDecl, baseStruct, derivedStruct));
4556+
}
45654557
auto *selfDecl = funcDecl->getImplicitSelfDecl();
45664558
auto selfExpr = new (ctx) DeclRefExpr(selfDecl, DeclNameLoc(),
45674559
/*implicit*/ true);
@@ -4575,8 +4567,8 @@ synthesizeBaseClassMethodBody(AbstractFunctionDecl *afd, void *context) {
45754567
auto castedCall = CallExpr::createImplicit(ctx, staticCastRefExpr, argList);
45764568
castedCall->setType(baseType);
45774569
castedCall->setThrows(false);
4578-
casted = castedCall;
4579-
}
4570+
return Argument::unlabeled(castedCall);
4571+
}();
45804572

45814573
auto *baseMemberExpr =
45824574
new (ctx) DeclRefExpr(ConcreteDeclRef(baseMember), DeclNameLoc(),
@@ -4680,7 +4672,7 @@ synthesizeBaseClassFieldSetterBody(AbstractFunctionDecl *afd, void *context) {
46804672
cast<NominalTypeDecl>(setterDecl->getDeclContext()->getAsDecl());
46814673

46824674
auto *pointeePropertyRefExpr =
4683-
getInOutSelfInteropStaticCast(setterDecl, baseStruct, derivedStruct);
4675+
getSelfInteropStaticCast(setterDecl, baseStruct, derivedStruct);
46844676

46854677
Expr *storedRef = nullptr;
46864678
if (auto subscript = dyn_cast<SubscriptDecl>(baseClassVar)) {
@@ -5378,7 +5370,7 @@ static ValueDecl *rewriteIntegerTypes(SubstitutionMap subst, ValueDecl *oldDecl,
53785370
return newDecl;
53795371
}
53805372

5381-
static Expr *createSelfExpr(FuncDecl *fnDecl) {
5373+
static Argument createSelfArg(FuncDecl *fnDecl) {
53825374
ASTContext &ctx = fnDecl->getASTContext();
53835375

53845376
auto selfDecl = fnDecl->getImplicitSelfDecl();
@@ -5387,16 +5379,10 @@ static Expr *createSelfExpr(FuncDecl *fnDecl) {
53875379

53885380
if (!fnDecl->isMutating()) {
53895381
selfRefExpr->setType(selfDecl->getInterfaceType());
5390-
return selfRefExpr;
5382+
return Argument::unlabeled(selfRefExpr);
53915383
}
53925384
selfRefExpr->setType(LValueType::get(selfDecl->getInterfaceType()));
5393-
5394-
auto inoutSelfExpr = new (ctx) InOutExpr(
5395-
SourceLoc(), selfRefExpr,
5396-
fnDecl->mapTypeIntoContext(selfDecl->getValueInterfaceType()),
5397-
/*isImplicit*/ true);
5398-
inoutSelfExpr->setType(InOutType::get(selfDecl->getInterfaceType()));
5399-
return inoutSelfExpr;
5385+
return Argument::implicitInOut(ctx, selfRefExpr);
54005386
}
54015387

54025388
// Synthesize a thunk body for the function created in
@@ -5449,8 +5435,9 @@ synthesizeDependentTypeThunkParamForwarding(AbstractFunctionDecl *afd, void *con
54495435
specializedFuncDeclRef->setType(specializedFuncDecl->getInterfaceType());
54505436

54515437
if (specializedFuncDecl->isInstanceMember()) {
5452-
auto selfExpr = createSelfExpr(thunkDecl);
5453-
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(), selfExpr);
5438+
auto selfArg = createSelfArg(thunkDecl);
5439+
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef,
5440+
SourceLoc(), selfArg);
54545441
memberCall->setThrows(false);
54555442
auto resultType = specializedFuncDecl->getInterfaceType()->getAs<FunctionType>()->getResult();
54565443
specializedFuncDeclRef = memberCall;
@@ -5459,7 +5446,9 @@ synthesizeDependentTypeThunkParamForwarding(AbstractFunctionDecl *afd, void *con
54595446
auto resultType = specializedFuncDecl->getInterfaceType()->getAs<FunctionType>()->getResult();
54605447
auto selfType = cast<NominalTypeDecl>(thunkDecl->getDeclContext()->getAsDecl())->getDeclaredInterfaceType();
54615448
auto selfTypeExpr = TypeExpr::createImplicit(selfType, ctx);
5462-
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(), selfTypeExpr);
5449+
auto *memberCall =
5450+
DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(),
5451+
Argument::unlabeled(selfTypeExpr));
54635452
memberCall->setThrows(false);
54645453
specializedFuncDeclRef = memberCall;
54655454
specializedFuncDeclRef->setType(resultType);
@@ -5576,8 +5565,9 @@ synthesizeForwardingThunkBody(AbstractFunctionDecl *afd, void *context) {
55765565
specializedFuncDeclRef->setType(specializedFuncDecl->getInterfaceType());
55775566

55785567
if (specializedFuncDecl->isInstanceMember()) {
5579-
auto selfExpr = createSelfExpr(thunkDecl);
5580-
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(), selfExpr);
5568+
auto selfArg = createSelfArg(thunkDecl);
5569+
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef,
5570+
SourceLoc(), selfArg);
55815571
memberCall->setThrows(false);
55825572
auto resultType = specializedFuncDecl->getInterfaceType()->getAs<FunctionType>()->getResult();
55835573
specializedFuncDeclRef = memberCall;
@@ -5586,7 +5576,9 @@ synthesizeForwardingThunkBody(AbstractFunctionDecl *afd, void *context) {
55865576
auto resultType = specializedFuncDecl->getInterfaceType()->getAs<FunctionType>()->getResult();
55875577
auto selfType = cast<NominalTypeDecl>(thunkDecl->getDeclContext()->getAsDecl())->getDeclaredInterfaceType();
55885578
auto selfTypeExpr = TypeExpr::createImplicit(selfType, ctx);
5589-
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(), selfTypeExpr);
5579+
auto *memberCall =
5580+
DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(),
5581+
Argument::unlabeled(selfTypeExpr));
55905582
memberCall->setThrows(false);
55915583
specializedFuncDeclRef = memberCall;
55925584
specializedFuncDeclRef->setType(resultType);

Diff for: lib/ClangImporter/ImportDecl.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -5457,8 +5457,9 @@ Decl *SwiftDeclConverter::importEnumCaseAlias(
54575457
/*implicit*/ true);
54585458
constantRef->setType(enumElt->getInterfaceType());
54595459

5460-
auto instantiate = DotSyntaxCallExpr::create(Impl.SwiftContext, constantRef,
5461-
SourceLoc(), typeRef);
5460+
auto instantiate =
5461+
DotSyntaxCallExpr::create(Impl.SwiftContext, constantRef, SourceLoc(),
5462+
Argument::unlabeled(typeRef));
54625463
instantiate->setType(importedEnumTy);
54635464
instantiate->setThrows(false);
54645465

0 commit comments

Comments
 (0)