Skip to content

Commit 78384d5

Browse files
committed
[Concurrency] Add ExtractFunctionIsolationExpr to represent the isolation
of a dynamically isolated function value in the AST.
1 parent 5d9ad87 commit 78384d5

File tree

9 files changed

+79
-0
lines changed

9 files changed

+79
-0
lines changed

include/swift/AST/Expr.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3518,6 +3518,43 @@ class ActorIsolationErasureExpr : public ImplicitConversionExpr {
35183518
}
35193519
};
35203520

3521+
/// Extracts the isolation of a dynamically isolated function value.
3522+
class ExtractFunctionIsolationExpr : public Expr {
3523+
/// The function value expression from which to extract the
3524+
/// isolation. The type of `fnExpr` must be an ``@isolated(any)`
3525+
/// funciton.
3526+
Expr *fnExpr;
3527+
3528+
/// The source location of `.isolation`
3529+
SourceLoc isolationLoc;
3530+
3531+
public:
3532+
ExtractFunctionIsolationExpr(Expr *fnExpr, SourceLoc isolationLoc,
3533+
Type type, bool implicit)
3534+
: Expr(ExprKind::ExtractFunctionIsolation, implicit, type),
3535+
fnExpr(fnExpr), isolationLoc(isolationLoc) {}
3536+
3537+
Expr *getFunctionExpr() const {
3538+
return fnExpr;
3539+
}
3540+
3541+
void setFunctionExpr(Expr *fnExpr) {
3542+
this->fnExpr = fnExpr;
3543+
}
3544+
3545+
SourceLoc getStartLoc() const {
3546+
return fnExpr->getStartLoc();
3547+
}
3548+
3549+
SourceLoc getEndLoc() const {
3550+
return isolationLoc;
3551+
}
3552+
3553+
static bool classof(const Expr *E) {
3554+
return E->getKind() == ExprKind::ExtractFunctionIsolation;
3555+
}
3556+
};
3557+
35213558
/// UnresolvedSpecializeExpr - Represents an explicit specialization using
35223559
/// a type parameter list (e.g. "Vector<Int>") that has not been resolved.
35233560
class UnresolvedSpecializeExpr final : public Expr,

include/swift/AST/ExprNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ EXPR(VarargExpansion, Expr)
134134
EXPR(PackExpansion, Expr)
135135
EXPR(PackElement, Expr)
136136
EXPR(MaterializePack, Expr)
137+
EXPR(ExtractFunctionIsolation, Expr)
137138
EXPR(DynamicType, Expr)
138139
EXPR(RebindSelfInConstructor, Expr)
139140
EXPR(OpaqueValue, Expr)

lib/AST/ASTDumper.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2699,6 +2699,13 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, StringRef>,
26992699
printFoot();
27002700
}
27012701

2702+
void visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *E,
2703+
StringRef label) {
2704+
printCommon(E, "extract_function_isolation", label);
2705+
printRec(E->getFunctionExpr());
2706+
printFoot();
2707+
}
2708+
27022709
void visitInOutExpr(InOutExpr *E, StringRef label) {
27032710
printCommon(E, "inout_expr", label);
27042711
printRec(E->getSubExpr());

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5290,6 +5290,11 @@ void PrintAST::visitLinearToDifferentiableFunctionExpr(swift::LinearToDifferenti
52905290
void PrintAST::visitActorIsolationErasureExpr(ActorIsolationErasureExpr *expr) {
52915291
}
52925292

5293+
void PrintAST::visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *expr) {
5294+
visit(expr->getFunctionExpr());
5295+
Printer << ".isolation";
5296+
}
5297+
52935298
void PrintAST::visitPropertyWrapperValuePlaceholderExpr(swift::PropertyWrapperValuePlaceholderExpr *expr) {
52945299
}
52955300

lib/AST/ASTWalker.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,17 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
13261326
return E;
13271327
}
13281328

1329+
Expr *visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *E) {
1330+
if (auto fn = E->getFunctionExpr()) {
1331+
if (auto newFn = doIt(fn))
1332+
E->setFunctionExpr(newFn);
1333+
else
1334+
return nullptr;
1335+
}
1336+
1337+
return E;
1338+
}
1339+
13291340
Expr *visitKeyPathDotExpr(KeyPathDotExpr *E) { return E; }
13301341

13311342
Expr *visitSingleValueStmtExpr(SingleValueStmtExpr *E) {

lib/AST/Expr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ ConcreteDeclRef Expr::getReferencedDecl(bool stopAtParenExpr) const {
378378
PASS_THROUGH_REFERENCE(Try, getSubExpr);
379379
PASS_THROUGH_REFERENCE(ForceTry, getSubExpr);
380380
PASS_THROUGH_REFERENCE(OptionalTry, getSubExpr);
381+
PASS_THROUGH_REFERENCE(ExtractFunctionIsolation, getFunctionExpr);
381382

382383
NO_REFERENCE(Tuple);
383384
SIMPLE_REFERENCE(Array, getInitializer);
@@ -723,6 +724,7 @@ bool Expr::canAppendPostfixExpression(bool appendingPostfixOperator) const {
723724
case ExprKind::UnresolvedSpecialize:
724725
case ExprKind::UnresolvedMember:
725726
case ExprKind::UnresolvedDot:
727+
case ExprKind::ExtractFunctionIsolation:
726728
return true;
727729

728730
case ExprKind::Sequence:
@@ -1032,6 +1034,7 @@ bool Expr::isValidParentOfTypeExpr(Expr *typeExpr) const {
10321034
case ExprKind::MacroExpansion:
10331035
case ExprKind::CurrentContextIsolation:
10341036
case ExprKind::ActorIsolationErasure:
1037+
case ExprKind::ExtractFunctionIsolation:
10351038
return false;
10361039
}
10371040

lib/SILGen/SILGenExpr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ namespace {
486486
SGFContext C);
487487
RValue visitActorIsolationErasureExpr(ActorIsolationErasureExpr *E,
488488
SGFContext C);
489+
RValue visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *E,
490+
SGFContext C);
489491
RValue visitCovariantFunctionConversionExpr(
490492
CovariantFunctionConversionExpr *E,
491493
SGFContext C);
@@ -2107,6 +2109,11 @@ RValue RValueEmitter::visitActorIsolationErasureExpr(ActorIsolationErasureExpr *
21072109
nonIsolatedType));
21082110
}
21092111

2112+
RValue RValueEmitter::visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *E,
2113+
SGFContext C) {
2114+
llvm_unreachable("not yet implemented for ExtractFunctionIsolationExpr");
2115+
}
2116+
21102117
RValue RValueEmitter::visitErasureExpr(ErasureExpr *E, SGFContext C) {
21112118
if (auto result = tryEmitAsBridgingConversion(SGF, E, false, C)) {
21122119
return RValue(SGF, E, *result);

lib/Sema/CSApply.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5372,6 +5372,10 @@ namespace {
53725372
return E;
53735373
}
53745374

5375+
Expr *visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *E) {
5376+
llvm_unreachable("found ExtractFunctionIsolationExpr in CSApply");
5377+
}
5378+
53755379
Expr *visitKeyPathDotExpr(KeyPathDotExpr *E) {
53765380
llvm_unreachable("found KeyPathDotExpr in CSApply");
53775381
}

lib/Sema/CSGen.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3935,6 +3935,10 @@ namespace {
39353935
return OptionalType::get(actorProto->getDeclaredExistentialType());
39363936
}
39373937

3938+
Type visitExtractFunctionIsolationExpr(ExtractFunctionIsolationExpr *E) {
3939+
llvm_unreachable("found ExtractFunctionIsolationExpr in CSGen");
3940+
}
3941+
39383942
Type visitKeyPathDotExpr(KeyPathDotExpr *E) {
39393943
llvm_unreachable("found KeyPathDotExpr in CSGen");
39403944
}

0 commit comments

Comments
 (0)