Skip to content

Commit 51ecdf2

Browse files
Merge pull request #4778 from swiftwasm/main
[pull] swiftwasm from main
2 parents 5e1816b + 7693167 commit 51ecdf2

31 files changed

+381
-303
lines changed

include/swift/AST/ArgumentList.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class Argument final {
5151
return Argument(SourceLoc(), Identifier(), expr);
5252
}
5353

54+
/// Make an implicit unlabeled 'inout' argument.
55+
static Argument implicitInOut(ASTContext &ctx, Expr *expr);
56+
5457
SourceLoc getStartLoc() const { return getSourceRange().Start; }
5558
SourceLoc getEndLoc() const { return getSourceRange().End; }
5659
SourceRange getSourceRange() const;
@@ -65,6 +68,11 @@ class Argument final {
6568
/// The argument label written in the call.
6669
Identifier getLabel() const { return Label; }
6770

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+
6876
/// Set a new argument label.
6977
///
7078
/// Note this is marked \c & to prevent its use on an rvalue Argument vended

include/swift/AST/Expr.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4763,8 +4763,13 @@ class DotSyntaxCallExpr : public SelfApplyExpr {
47634763
}
47644764

47654765
public:
4766+
/// Create a new method reference to \p fnExpr on the base value \p baseArg.
4767+
///
4768+
/// If this is for a 'mutating' method, \p baseArg should be created using
4769+
/// \c Argument::implicitInOut. Otherwise, \p Argument::unlabeled should be
4770+
/// used. \p baseArg must not be labeled.
47664771
static DotSyntaxCallExpr *create(ASTContext &ctx, Expr *fnExpr,
4767-
SourceLoc dotLoc, Expr *baseExpr,
4772+
SourceLoc dotLoc, Argument baseArg,
47684773
Type ty = Type());
47694774

47704775
SourceLoc getDotLoc() const { return DotLoc; }

lib/AST/ArgumentList.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ SourceRange Argument::getSourceRange() const {
3535
return SourceRange(labelLoc, exprEndLoc);
3636
}
3737

38+
Argument Argument::implicitInOut(ASTContext &ctx, Expr *expr) {
39+
assert(!isa<InOutExpr>(expr) && "Cannot nest InOutExpr");
40+
41+
// Eventually this will set an 'inout' bit on Argument, but for now,
42+
// synthesize in the InOutExpr.
43+
Type objectTy;
44+
if (auto subTy = expr->getType())
45+
objectTy = subTy->castTo<LValueType>()->getObjectType();
46+
47+
return Argument::unlabeled(
48+
new (ctx) InOutExpr(SourceLoc(), expr, objectTy, /*isImplicit*/ true));
49+
}
50+
3851
bool Argument::isInOut() const {
3952
return ArgExpr->isSemanticallyInOutExpr();
4053
}

lib/AST/Expr.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,9 +1574,10 @@ BinaryExpr *BinaryExpr::create(ASTContext &ctx, Expr *lhs, Expr *fn, Expr *rhs,
15741574
}
15751575

15761576
DotSyntaxCallExpr *DotSyntaxCallExpr::create(ASTContext &ctx, Expr *fnExpr,
1577-
SourceLoc dotLoc, Expr *baseExpr,
1577+
SourceLoc dotLoc, Argument baseArg,
15781578
Type ty) {
1579-
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {baseExpr});
1579+
assert(!baseArg.hasLabel());
1580+
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {baseArg.getExpr()});
15801581
return new (ctx) DotSyntaxCallExpr(fnExpr, dotLoc, argList, ty);
15811582
}
15821583

lib/ClangImporter/ClangImporter.cpp

Lines changed: 68 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -4491,29 +4491,23 @@ DeclRefExpr *getInteropStaticCastDeclRefExpr(ASTContext &ctx,
44914491
// %2 = __swift_interopStaticCast<UnsafeMutablePointer<Base>?>(%1)
44924492
// %3 = %2!
44934493
// return %3.pointee
4494-
MemberRefExpr *getInOutSelfInteropStaticCast(FuncDecl *funcDecl,
4495-
NominalTypeDecl *baseStruct,
4496-
NominalTypeDecl *derivedStruct) {
4494+
MemberRefExpr *getSelfInteropStaticCast(FuncDecl *funcDecl,
4495+
NominalTypeDecl *baseStruct,
4496+
NominalTypeDecl *derivedStruct) {
44974497
auto &ctx = funcDecl->getASTContext();
44984498

4499-
auto inoutSelf = [&ctx](FuncDecl *funcDecl) {
4500-
auto inoutSelfDecl = funcDecl->getImplicitSelfDecl();
4499+
auto mutableSelf = [&ctx](FuncDecl *funcDecl) {
4500+
auto selfDecl = funcDecl->getImplicitSelfDecl();
45014501

4502-
auto inoutSelfRef =
4503-
new (ctx) DeclRefExpr(inoutSelfDecl, DeclNameLoc(), /*implicit*/ true);
4504-
inoutSelfRef->setType(LValueType::get(inoutSelfDecl->getInterfaceType()));
4502+
auto selfRef =
4503+
new (ctx) DeclRefExpr(selfDecl, DeclNameLoc(), /*implicit*/ true);
4504+
selfRef->setType(LValueType::get(selfDecl->getInterfaceType()));
45054505

4506-
auto inoutSelf = new (ctx) InOutExpr(
4507-
SourceLoc(), inoutSelfRef,
4508-
funcDecl->mapTypeIntoContext(inoutSelfDecl->getValueInterfaceType()),
4509-
/*implicit*/ true);
4510-
inoutSelf->setType(InOutType::get(inoutSelfDecl->getInterfaceType()));
4511-
4512-
return inoutSelf;
4506+
return selfRef;
45134507
}(funcDecl);
45144508

45154509
auto createCallToBuiltin = [&](Identifier name, ArrayRef<Type> substTypes,
4516-
Expr *arg) {
4510+
Argument arg) {
45174511
auto builtinFn = cast<FuncDecl>(getBuiltinValueDecl(ctx, name));
45184512
auto substMap =
45194513
SubstitutionMap::get(builtinFn->getGenericSignature(), substTypes,
@@ -4526,22 +4520,23 @@ MemberRefExpr *getInOutSelfInteropStaticCast(FuncDecl *funcDecl,
45264520
if (auto genericFnType = dyn_cast<GenericFunctionType>(fnType.getPointer()))
45274521
fnType = genericFnType->substGenericArgs(substMap);
45284522
builtinFnRefExpr->setType(fnType);
4529-
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {arg});
4523+
auto *argList = ArgumentList::createImplicit(ctx, {arg});
45304524
auto callExpr = CallExpr::create(ctx, builtinFnRefExpr, argList, /*implicit*/ true);
45314525
callExpr->setThrows(false);
45324526
return callExpr;
45334527
};
45344528

4535-
auto rawSelfPointer =
4536-
createCallToBuiltin(ctx.getIdentifier("addressof"),
4537-
{derivedStruct->getSelfInterfaceType()}, inoutSelf);
4529+
auto rawSelfPointer = createCallToBuiltin(
4530+
ctx.getIdentifier("addressof"), {derivedStruct->getSelfInterfaceType()},
4531+
Argument::implicitInOut(ctx, mutableSelf));
45384532
rawSelfPointer->setType(ctx.TheRawPointerType);
45394533

45404534
auto derivedPtrType = derivedStruct->getSelfInterfaceType()->wrapInPointer(
45414535
PTK_UnsafeMutablePointer);
4542-
auto selfPointer = createCallToBuiltin(
4543-
ctx.getIdentifier("reinterpretCast"),
4544-
{ctx.TheRawPointerType, derivedPtrType}, rawSelfPointer);
4536+
auto selfPointer =
4537+
createCallToBuiltin(ctx.getIdentifier("reinterpretCast"),
4538+
{ctx.TheRawPointerType, derivedPtrType},
4539+
Argument::unlabeled(rawSelfPointer));
45454540
selfPointer->setType(derivedPtrType);
45464541

45474542
auto staticCastRefExpr = getInteropStaticCastDeclRefExpr(
@@ -4601,14 +4596,11 @@ synthesizeBaseClassMethodBody(AbstractFunctionDecl *afd, void *context) {
46014596
forwardingParams.push_back(paramRefExpr);
46024597
}
46034598

4604-
Expr *casted = nullptr;
4605-
if (funcDecl->isMutating()) {
4606-
auto pointeeMemberRefExpr =
4607-
getInOutSelfInteropStaticCast(funcDecl, baseStruct, derivedStruct);
4608-
casted = new (ctx) InOutExpr(SourceLoc(), pointeeMemberRefExpr, baseType,
4609-
/*implicit*/ true);
4610-
casted->setType(InOutType::get(baseType));
4611-
} else {
4599+
Argument casted = [&]() {
4600+
if (funcDecl->isMutating()) {
4601+
return Argument::implicitInOut(
4602+
ctx, getSelfInteropStaticCast(funcDecl, baseStruct, derivedStruct));
4603+
}
46124604
auto *selfDecl = funcDecl->getImplicitSelfDecl();
46134605
auto selfExpr = new (ctx) DeclRefExpr(selfDecl, DeclNameLoc(),
46144606
/*implicit*/ true);
@@ -4622,8 +4614,8 @@ synthesizeBaseClassMethodBody(AbstractFunctionDecl *afd, void *context) {
46224614
auto castedCall = CallExpr::createImplicit(ctx, staticCastRefExpr, argList);
46234615
castedCall->setType(baseType);
46244616
castedCall->setThrows(false);
4625-
casted = castedCall;
4626-
}
4617+
return Argument::unlabeled(castedCall);
4618+
}();
46274619

46284620
auto *baseMemberExpr =
46294621
new (ctx) DeclRefExpr(ConcreteDeclRef(baseMember), DeclNameLoc(),
@@ -4727,7 +4719,7 @@ synthesizeBaseClassFieldSetterBody(AbstractFunctionDecl *afd, void *context) {
47274719
cast<NominalTypeDecl>(setterDecl->getDeclContext()->getAsDecl());
47284720

47294721
auto *pointeePropertyRefExpr =
4730-
getInOutSelfInteropStaticCast(setterDecl, baseStruct, derivedStruct);
4722+
getSelfInteropStaticCast(setterDecl, baseStruct, derivedStruct);
47314723

47324724
Expr *storedRef = nullptr;
47334725
if (auto subscript = dyn_cast<SubscriptDecl>(baseClassVar)) {
@@ -5442,7 +5434,7 @@ static ValueDecl *rewriteIntegerTypes(SubstitutionMap subst, ValueDecl *oldDecl,
54425434
return newDecl;
54435435
}
54445436

5445-
static Expr *createSelfExpr(FuncDecl *fnDecl) {
5437+
static Argument createSelfArg(FuncDecl *fnDecl) {
54465438
ASTContext &ctx = fnDecl->getASTContext();
54475439

54485440
auto selfDecl = fnDecl->getImplicitSelfDecl();
@@ -5451,16 +5443,10 @@ static Expr *createSelfExpr(FuncDecl *fnDecl) {
54515443

54525444
if (!fnDecl->isMutating()) {
54535445
selfRefExpr->setType(selfDecl->getInterfaceType());
5454-
return selfRefExpr;
5446+
return Argument::unlabeled(selfRefExpr);
54555447
}
54565448
selfRefExpr->setType(LValueType::get(selfDecl->getInterfaceType()));
5457-
5458-
auto inoutSelfExpr = new (ctx) InOutExpr(
5459-
SourceLoc(), selfRefExpr,
5460-
fnDecl->mapTypeIntoContext(selfDecl->getValueInterfaceType()),
5461-
/*isImplicit*/ true);
5462-
inoutSelfExpr->setType(InOutType::get(selfDecl->getInterfaceType()));
5463-
return inoutSelfExpr;
5449+
return Argument::implicitInOut(ctx, selfRefExpr);
54645450
}
54655451

54665452
// Synthesize a thunk body for the function created in
@@ -5481,30 +5467,30 @@ synthesizeDependentTypeThunkParamForwarding(AbstractFunctionDecl *afd, void *con
54815467
paramIndex++;
54825468
continue;
54835469
}
5470+
auto paramTy = param->getType();
5471+
auto isInOut = param->isInOut();
5472+
auto specParamTy =
5473+
specializedFuncDecl->getParameters()->get(paramIndex)->getType();
54845474

54855475
Expr *paramRefExpr = new (ctx) DeclRefExpr(param, DeclNameLoc(),
54865476
/*Implicit=*/true);
5487-
paramRefExpr->setType(param->getType());
5488-
5489-
if (param->isInOut()) {
5490-
paramRefExpr->setType(LValueType::get(param->getType()));
5491-
5492-
paramRefExpr = new (ctx) InOutExpr(
5493-
SourceLoc(), paramRefExpr, param->getType(), /*isImplicit*/ true);
5494-
paramRefExpr->setType(InOutType::get(param->getType()));
5495-
}
5496-
5497-
auto specParamTy = specializedFuncDecl->getParameters()->get(paramIndex)->getType();
5477+
paramRefExpr->setType(isInOut ? LValueType::get(paramTy) : paramTy);
54985478

5499-
Expr *argExpr = nullptr;
5500-
if (specParamTy->isEqual(param->getType())) {
5501-
argExpr = paramRefExpr;
5502-
} else {
5503-
argExpr = ForcedCheckedCastExpr::createImplicit(ctx, paramRefExpr,
5504-
specParamTy);
5505-
}
5506-
5507-
forwardingParams.push_back(Argument(SourceLoc(), Identifier(), argExpr));
5479+
Argument arg = [&]() {
5480+
if (isInOut) {
5481+
assert(specParamTy->isEqual(paramTy));
5482+
return Argument::implicitInOut(ctx, paramRefExpr);
5483+
}
5484+
Expr *argExpr = nullptr;
5485+
if (specParamTy->isEqual(paramTy)) {
5486+
argExpr = paramRefExpr;
5487+
} else {
5488+
argExpr = ForcedCheckedCastExpr::createImplicit(ctx, paramRefExpr,
5489+
specParamTy);
5490+
}
5491+
return Argument::unlabeled(argExpr);
5492+
}();
5493+
forwardingParams.push_back(arg);
55085494
paramIndex++;
55095495
}
55105496

@@ -5513,8 +5499,9 @@ synthesizeDependentTypeThunkParamForwarding(AbstractFunctionDecl *afd, void *con
55135499
specializedFuncDeclRef->setType(specializedFuncDecl->getInterfaceType());
55145500

55155501
if (specializedFuncDecl->isInstanceMember()) {
5516-
auto selfExpr = createSelfExpr(thunkDecl);
5517-
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(), selfExpr);
5502+
auto selfArg = createSelfArg(thunkDecl);
5503+
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef,
5504+
SourceLoc(), selfArg);
55185505
memberCall->setThrows(false);
55195506
auto resultType = specializedFuncDecl->getInterfaceType()->getAs<FunctionType>()->getResult();
55205507
specializedFuncDeclRef = memberCall;
@@ -5523,7 +5510,9 @@ synthesizeDependentTypeThunkParamForwarding(AbstractFunctionDecl *afd, void *con
55235510
auto resultType = specializedFuncDecl->getInterfaceType()->getAs<FunctionType>()->getResult();
55245511
auto selfType = cast<NominalTypeDecl>(thunkDecl->getDeclContext()->getAsDecl())->getDeclaredInterfaceType();
55255512
auto selfTypeExpr = TypeExpr::createImplicit(selfType, ctx);
5526-
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(), selfTypeExpr);
5513+
auto *memberCall =
5514+
DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(),
5515+
Argument::unlabeled(selfTypeExpr));
55275516
memberCall->setThrows(false);
55285517
specializedFuncDeclRef = memberCall;
55295518
specializedFuncDeclRef->setType(resultType);
@@ -5623,28 +5612,26 @@ synthesizeForwardingThunkBody(AbstractFunctionDecl *afd, void *context) {
56235612
if (isa<MetatypeType>(param->getType().getPointer())) {
56245613
continue;
56255614
}
5615+
auto paramTy = param->getType();
5616+
auto isInOut = param->isInOut();
5617+
56265618
Expr *paramRefExpr = new (ctx) DeclRefExpr(param, DeclNameLoc(),
56275619
/*Implicit=*/true);
5628-
paramRefExpr->setType(param->getType());
5629-
5630-
if (param->isInOut()) {
5631-
paramRefExpr->setType(LValueType::get(param->getType()));
5632-
5633-
paramRefExpr = new (ctx) InOutExpr(
5634-
SourceLoc(), paramRefExpr, param->getType(), /*isImplicit*/ true);
5635-
paramRefExpr->setType(InOutType::get(param->getType()));
5636-
}
5620+
paramRefExpr->setType(isInOut ? LValueType::get(paramTy) : paramTy);
56375621

5638-
forwardingParams.push_back(Argument(SourceLoc(), Identifier(), paramRefExpr));
5622+
auto arg = isInOut ? Argument::implicitInOut(ctx, paramRefExpr)
5623+
: Argument::unlabeled(paramRefExpr);
5624+
forwardingParams.push_back(arg);
56395625
}
56405626

56415627
Expr *specializedFuncDeclRef = new (ctx) DeclRefExpr(ConcreteDeclRef(specializedFuncDecl),
56425628
DeclNameLoc(), true);
56435629
specializedFuncDeclRef->setType(specializedFuncDecl->getInterfaceType());
56445630

56455631
if (specializedFuncDecl->isInstanceMember()) {
5646-
auto selfExpr = createSelfExpr(thunkDecl);
5647-
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(), selfExpr);
5632+
auto selfArg = createSelfArg(thunkDecl);
5633+
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef,
5634+
SourceLoc(), selfArg);
56485635
memberCall->setThrows(false);
56495636
auto resultType = specializedFuncDecl->getInterfaceType()->getAs<FunctionType>()->getResult();
56505637
specializedFuncDeclRef = memberCall;
@@ -5653,7 +5640,9 @@ synthesizeForwardingThunkBody(AbstractFunctionDecl *afd, void *context) {
56535640
auto resultType = specializedFuncDecl->getInterfaceType()->getAs<FunctionType>()->getResult();
56545641
auto selfType = cast<NominalTypeDecl>(thunkDecl->getDeclContext()->getAsDecl())->getDeclaredInterfaceType();
56555642
auto selfTypeExpr = TypeExpr::createImplicit(selfType, ctx);
5656-
auto *memberCall = DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(), selfTypeExpr);
5643+
auto *memberCall =
5644+
DotSyntaxCallExpr::create(ctx, specializedFuncDeclRef, SourceLoc(),
5645+
Argument::unlabeled(selfTypeExpr));
56575646
memberCall->setThrows(false);
56585647
specializedFuncDeclRef = memberCall;
56595648
specializedFuncDeclRef->setType(resultType);

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 2 deletions
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)