@@ -3553,6 +3553,79 @@ class SequenceExpr final : public Expr,
35533553 }
35543554};
35553555
3556+ // / Actor isolation for a closure.
3557+ class ClosureActorIsolation {
3558+ public:
3559+ enum Kind {
3560+ // / The closure is independent of any actor.
3561+ Independent,
3562+
3563+ // / The closure is tied to the actor instance described by the given
3564+ // / \c VarDecl*, which is the (captured) `self` of an actor.
3565+ ActorInstance,
3566+
3567+ // / The closure is tied to the global actor described by the given type.
3568+ GlobalActor,
3569+ };
3570+
3571+ private:
3572+ // / The actor to which this closure is isolated.
3573+ // /
3574+ // / There are three possible states:
3575+ // / - NULL: The closure is independent of any actor.
3576+ // / - VarDecl*: The 'self' variable for the actor instance to which
3577+ // / this closure is isolated. It will always have a type that conforms
3578+ // / to the \c Actor protocol.
3579+ // / - Type: The type of the global actor on which
3580+ llvm::PointerUnion<VarDecl *, Type> storage;
3581+
3582+ ClosureActorIsolation (VarDecl *selfDecl) : storage(selfDecl) { }
3583+ ClosureActorIsolation (Type globalActorType) : storage(globalActorType) { }
3584+
3585+ public:
3586+ ClosureActorIsolation () : storage() { }
3587+
3588+ static ClosureActorIsolation forIndependent () {
3589+ return ClosureActorIsolation ();
3590+ }
3591+
3592+ static ClosureActorIsolation forActorInstance (VarDecl *selfDecl) {
3593+ return ClosureActorIsolation (selfDecl);
3594+ }
3595+
3596+ static ClosureActorIsolation forGlobalActor (Type globalActorType) {
3597+ return ClosureActorIsolation (globalActorType);
3598+ }
3599+
3600+ // / Determine the kind of isolation.
3601+ Kind getKind () const {
3602+ if (storage.isNull ())
3603+ return Kind::Independent;
3604+
3605+ if (storage.is <VarDecl *>())
3606+ return Kind::ActorInstance;
3607+
3608+ return Kind::GlobalActor;
3609+ }
3610+
3611+ // / Whether the closure is isolated at all.
3612+ explicit operator bool () const {
3613+ return getKind () != Kind::Independent;
3614+ }
3615+
3616+ // / Whether the closure is isolated at all.
3617+ operator Kind () const {
3618+ return getKind ();
3619+ }
3620+
3621+ VarDecl *getActorInstance () const {
3622+ return storage.dyn_cast <VarDecl *>();
3623+ }
3624+
3625+ Type getGlobalActor () const {
3626+ return storage.dyn_cast <Type>();
3627+ }
3628+ };
35563629
35573630// / A base class for closure expressions.
35583631class AbstractClosureExpr : public DeclContext , public Expr {
@@ -3561,6 +3634,9 @@ class AbstractClosureExpr : public DeclContext, public Expr {
35613634 // / The set of parameters.
35623635 ParameterList *parameterList;
35633636
3637+ // / Actor isolation of the closure.
3638+ ClosureActorIsolation actorIsolation;
3639+
35643640public:
35653641 AbstractClosureExpr (ExprKind Kind, Type FnType, bool Implicit,
35663642 unsigned Discriminator, DeclContext *Parent)
@@ -3625,6 +3701,12 @@ class AbstractClosureExpr : public DeclContext, public Expr {
36253701 // / Only valid when \c hasSingleExpressionBody() is true.
36263702 Expr *getSingleExpressionBody () const ;
36273703
3704+ ClosureActorIsolation getActorIsolation () const { return actorIsolation; }
3705+
3706+ void setActorIsolation (ClosureActorIsolation actorIsolation) {
3707+ this ->actorIsolation = actorIsolation;
3708+ }
3709+
36283710 static bool classof (const Expr *E) {
36293711 return E->getKind () >= ExprKind::First_AbstractClosureExpr &&
36303712 E->getKind () <= ExprKind::Last_AbstractClosureExpr;
0 commit comments