-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathActorIsolation.cpp
202 lines (161 loc) · 6.36 KB
/
ActorIsolation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//===--- ActorIsolation.cpp -----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/AST/ActorIsolation.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
using namespace swift;
ActorIsolation ActorIsolation::forMainActor(ASTContext &ctx) {
return ActorIsolation::forGlobalActor(
ctx.getMainActorType()->mapTypeOutOfContext());
}
ActorIsolation::ActorIsolation(Kind kind, NominalTypeDecl *actor,
unsigned parameterIndex)
: actorInstance(actor), kind(kind), isolatedByPreconcurrency(false),
silParsed(false), parameterIndex(parameterIndex) {}
ActorIsolation::ActorIsolation(Kind kind, VarDecl *actor,
unsigned parameterIndex)
: actorInstance(actor), kind(kind), isolatedByPreconcurrency(false),
silParsed(false), parameterIndex(parameterIndex) {}
ActorIsolation::ActorIsolation(Kind kind, Expr *actor, unsigned parameterIndex)
: actorInstance(actor), kind(kind), isolatedByPreconcurrency(false),
silParsed(false), parameterIndex(parameterIndex) {}
ActorIsolation::ActorIsolation(Kind kind, Type globalActor)
: globalActor(globalActor), kind(kind), isolatedByPreconcurrency(false),
silParsed(false), parameterIndex(0) {}
ActorIsolation
ActorIsolation::forActorInstanceParameter(Expr *actor,
unsigned parameterIndex) {
auto &ctx = actor->getType()->getASTContext();
// An isolated value of `nil` is statically nonisolated.
// FIXME: Also allow 'Optional.none'
if (isa<NilLiteralExpr>(actor))
return ActorIsolation::forNonisolated(/*unsafe*/ false);
// An isolated value of `<global actor type>.shared` is statically
// global actor isolated.
if (auto *memberRef = dyn_cast<MemberRefExpr>(actor)) {
// Check that the member declaration witnesses the `shared`
// requirement of the `GlobalActor` protocol.
auto declRef = memberRef->getDecl();
auto baseType = memberRef->getBase()->getType()->getMetatypeInstanceType();
if (auto globalActor = ctx.getProtocol(KnownProtocolKind::GlobalActor)) {
auto conformance = checkConformance(baseType, globalActor);
if (conformance &&
conformance.getWitnessByName(baseType, ctx.Id_shared) == declRef) {
return ActorIsolation::forGlobalActor(baseType);
}
}
}
return ActorIsolation(ActorInstance, actor, parameterIndex + 1);
}
ActorIsolation ActorIsolation::forActorInstanceSelf(ValueDecl *decl) {
if (auto *fn = dyn_cast<AbstractFunctionDecl>(decl))
return ActorIsolation(ActorInstance, fn->getImplicitSelfDecl(), 0);
if (auto *storage = dyn_cast<AbstractStorageDecl>(decl)) {
if (auto *fn = storage->getAccessor(AccessorKind::Get)) {
return ActorIsolation(ActorInstance, fn->getImplicitSelfDecl(), 0);
}
}
auto *dc = decl->getDeclContext();
return ActorIsolation(ActorInstance, dc->getSelfNominalTypeDecl(), 0);
}
ActorIsolation ActorIsolation::forActorInstanceSelf(NominalTypeDecl *selfDecl) {
return ActorIsolation(ActorInstance, selfDecl, 0);
}
NominalTypeDecl *ActorIsolation::getActor() const {
assert(getKind() == ActorInstance || getKind() == GlobalActor);
if (silParsed)
return nullptr;
if (getKind() == GlobalActor) {
return getGlobalActor()->getAnyNominal();
}
Type actorType;
if (auto *instance = actorInstance.dyn_cast<VarDecl *>()) {
actorType = instance->getTypeInContext();
} else if (auto *expr = actorInstance.dyn_cast<Expr *>()) {
actorType = expr->getType()->getRValueType();
}
if (actorType) {
if (auto wrapped = actorType->getOptionalObjectType()) {
actorType = wrapped;
}
return actorType->getReferenceStorageReferent()->getAnyActor();
}
return actorInstance.get<NominalTypeDecl *>();
}
VarDecl *ActorIsolation::getActorInstance() const {
assert(getKind() == ActorInstance);
if (silParsed)
return nullptr;
return actorInstance.dyn_cast<VarDecl *>();
}
Expr *ActorIsolation::getActorInstanceExpr() const {
assert(getKind() == ActorInstance);
if (silParsed)
return nullptr;
return actorInstance.dyn_cast<Expr *>();
}
bool ActorIsolation::isMainActor() const {
if (silParsed)
return false;
if (isGlobalActor()) {
if (auto *nominal = getGlobalActor()->getAnyNominal())
return nominal->isMainActor();
}
return false;
}
bool ActorIsolation::isDistributedActor() const {
if (silParsed)
return false;
if (getKind() != ActorInstance)
return false;
return getActor()->isDistributedActor();
}
bool ActorIsolation::isEqual(const ActorIsolation &lhs,
const ActorIsolation &rhs) {
if (lhs.getKind() != rhs.getKind())
return false;
switch (lhs.getKind()) {
case Nonisolated:
case NonisolatedUnsafe:
case Unspecified:
return true;
case Erased:
// Different functions with erased isolation have the same *kind* of
// isolation, but we must generally assume that they're not isolated
// the *same way*, which is what this function is apparently supposed
// to answer.
return false;
case CallerIsolationInheriting:
// This returns false for the same reason as erased. The caller has to check
// against the actual caller isolation.
return false;
case ActorInstance: {
auto *lhsActor = lhs.getActorInstance();
auto *rhsActor = rhs.getActorInstance();
if (lhsActor && rhsActor) {
// FIXME: This won't work for arbitrary isolated parameter captures.
if ((lhsActor->isSelfParameter() && rhsActor->isSelfParamCapture()) ||
(lhsActor->isSelfParamCapture() && rhsActor->isSelfParameter())) {
return true;
}
}
// The parameter index doesn't matter; only the actor instance
// values must be equal.
return (lhs.getActor() == rhs.getActor() &&
lhs.actorInstance == rhs.actorInstance);
}
case GlobalActor:
return areTypesEqual(lhs.globalActor, rhs.globalActor);
}
}