|
| 1 | +//===--- ActorIsolation.h - Actor isolation ---------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file provides a description of actor isolation state. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | +#ifndef SWIFT_AST_ACTORISOLATIONSTATE_H |
| 17 | +#define SWIFT_AST_ACTORISOLATIONSTATE_H |
| 18 | + |
| 19 | +#include "llvm/ADT/Hashing.h" |
| 20 | + |
| 21 | +namespace llvm { |
| 22 | +class raw_ostream; |
| 23 | +} |
| 24 | + |
| 25 | +namespace swift { |
| 26 | +class ClassDecl; |
| 27 | + |
| 28 | +/// Describes the actor isolation of a given declaration, which determines |
| 29 | +/// the actors with which it can interact. |
| 30 | +class ActorIsolation { |
| 31 | +public: |
| 32 | + enum Kind { |
| 33 | + /// The actor isolation has not been specified. It is assumed to be |
| 34 | + /// unsafe to interact with this declaration from any actor. |
| 35 | + Unspecified = 0, |
| 36 | + /// The declaration is isolated to the instance of an actor class. |
| 37 | + /// For example, a mutable stored property or synchronous function within |
| 38 | + /// the actor is isolated to the instance of that actor. |
| 39 | + ActorInstance, |
| 40 | + /// The declaration can refer to actor-isolated state, but can also be |
| 41 | + //// referenced from outside the actor. |
| 42 | + ActorPrivileged, |
| 43 | + /// The declaration is explicitly specified to be independent of any actor, |
| 44 | + /// meaning that it can be used from any actor but is also unable to |
| 45 | + /// refer to the isolated state of any given actor. |
| 46 | + Independent, |
| 47 | + }; |
| 48 | + |
| 49 | +private: |
| 50 | + Kind kind; |
| 51 | + ClassDecl *actor; |
| 52 | + |
| 53 | + ActorIsolation(Kind kind, ClassDecl *actor) : kind(kind), actor(actor) { } |
| 54 | + |
| 55 | +public: |
| 56 | + static ActorIsolation forUnspecified() { |
| 57 | + return ActorIsolation(Unspecified, nullptr); |
| 58 | + } |
| 59 | + |
| 60 | + static ActorIsolation forIndependent() { |
| 61 | + return ActorIsolation(Independent, nullptr); |
| 62 | + } |
| 63 | + |
| 64 | + static ActorIsolation forActorPrivileged(ClassDecl *actor) { |
| 65 | + return ActorIsolation(ActorPrivileged, actor); |
| 66 | + } |
| 67 | + |
| 68 | + static ActorIsolation forActorInstance(ClassDecl *actor) { |
| 69 | + return ActorIsolation(ActorInstance, actor); |
| 70 | + } |
| 71 | + |
| 72 | + Kind getKind() const { return kind; } |
| 73 | + |
| 74 | + operator Kind() const { return getKind(); } |
| 75 | + |
| 76 | + ClassDecl *getActor() const { |
| 77 | + assert(getKind() == ActorInstance || getKind() == ActorPrivileged); |
| 78 | + return actor; |
| 79 | + } |
| 80 | + |
| 81 | + friend bool operator==(const ActorIsolation &lhs, |
| 82 | + const ActorIsolation &rhs) { |
| 83 | + if (lhs.kind != rhs.kind) |
| 84 | + return false; |
| 85 | + |
| 86 | + switch (lhs.kind) { |
| 87 | + case Independent: |
| 88 | + case Unspecified: |
| 89 | + return true; |
| 90 | + |
| 91 | + case ActorInstance: |
| 92 | + case ActorPrivileged: |
| 93 | + return lhs.actor == rhs.actor; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + friend bool operator!=(const ActorIsolation &lhs, |
| 98 | + const ActorIsolation &rhs) { |
| 99 | + return !(lhs == rhs); |
| 100 | + } |
| 101 | + |
| 102 | + friend llvm::hash_code hash_value(const ActorIsolation &state) { |
| 103 | + return llvm::hash_combine(state.kind, state.actor); |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +void simple_display(llvm::raw_ostream &out, const ActorIsolation &state); |
| 108 | + |
| 109 | +} // end namespace swift |
| 110 | + |
| 111 | +#endif /* SWIFT_AST_ACTORISOLATIONSTATE_H */ |
0 commit comments