@@ -11271,18 +11271,82 @@ ActorIsolation::ActorIsolation(Kind kind, NominalTypeDecl *actor,
1127111271 : actorInstance(actor), kind(kind), isolatedByPreconcurrency(false ),
1127211272 silParsed(false ), parameterIndex(parameterIndex) {}
1127311273
11274- ActorIsolation::ActorIsolation (Kind kind, VarDecl *capturedActor)
11275- : actorInstance(capturedActor), kind(kind), isolatedByPreconcurrency(false ),
11276- silParsed(false ), parameterIndex(0 ) {}
11274+ ActorIsolation::ActorIsolation (Kind kind, VarDecl *actor,
11275+ unsigned parameterIndex)
11276+ : actorInstance(actor), kind(kind), isolatedByPreconcurrency(false ),
11277+ silParsed(false ), parameterIndex(parameterIndex) {}
11278+
11279+ ActorIsolation::ActorIsolation (Kind kind, Expr *actor,
11280+ unsigned parameterIndex)
11281+ : actorInstance(actor), kind(kind), isolatedByPreconcurrency(false ),
11282+ silParsed(false ), parameterIndex(parameterIndex) {}
11283+
11284+ ActorIsolation
11285+ ActorIsolation::forActorInstanceParameter (Expr *actor,
11286+ unsigned parameterIndex) {
11287+ auto &ctx = actor->getType ()->getASTContext ();
11288+
11289+ // An isolated value of `nil` is statically nonisolated.
11290+ // FIXME: Also allow 'Optional.none'
11291+ if (dyn_cast<NilLiteralExpr>(actor))
11292+ return ActorIsolation::forNonisolated (/* unsafe*/ false );
11293+
11294+ // An isolated value of `<global actor type>.shared` is statically
11295+ // global actor isolated.
11296+ if (auto *memberRef = dyn_cast<MemberRefExpr>(actor)) {
11297+ // Check that the member declaration witnesses the `shared`
11298+ // requirement of the `GlobalActor` protocol.
11299+ auto declRef = memberRef->getDecl ();
11300+ auto baseType =
11301+ memberRef->getBase ()->getType ()->getMetatypeInstanceType ();
11302+ if (auto globalActor = ctx.getProtocol (KnownProtocolKind::GlobalActor)) {
11303+ auto *dc = declRef.getDecl ()->getDeclContext ();
11304+ auto *module = dc->getParentModule ();
11305+ auto conformance = module ->checkConformance (baseType, globalActor);
11306+ if (conformance &&
11307+ conformance.getWitnessByName (baseType, ctx.Id_shared ) == declRef) {
11308+ return ActorIsolation::forGlobalActor (baseType);
11309+ }
11310+ }
11311+ }
11312+
11313+ return ActorIsolation (ActorInstance, actor, parameterIndex + 1 );
11314+ }
11315+
11316+ ActorIsolation
11317+ ActorIsolation::forActorInstanceSelf (ValueDecl *decl) {
11318+ if (auto *fn = dyn_cast<AbstractFunctionDecl>(decl))
11319+ return ActorIsolation (ActorInstance, fn->getImplicitSelfDecl (), 0 );
11320+
11321+ if (auto *storage = dyn_cast<AbstractStorageDecl>(decl)) {
11322+ if (auto *fn = storage->getAccessor (AccessorKind::Get)) {
11323+ return ActorIsolation (ActorInstance, fn->getImplicitSelfDecl (), 0 );
11324+ }
11325+ }
11326+
11327+ auto *dc = decl->getDeclContext ();
11328+ return ActorIsolation (ActorInstance, dc->getSelfNominalTypeDecl (), 0 );
11329+ }
1127711330
1127811331NominalTypeDecl *ActorIsolation::getActor () const {
1127911332 assert (getKind () == ActorInstance);
1128011333
1128111334 if (silParsed)
1128211335 return nullptr ;
1128311336
11337+ Type actorType;
11338+
1128411339 if (auto *instance = actorInstance.dyn_cast <VarDecl *>()) {
11285- return instance->getTypeInContext ()
11340+ actorType = instance->getTypeInContext ();
11341+ } else if (auto *instance = actorInstance.dyn_cast <Expr *>()) {
11342+ actorType = instance->getType ();
11343+ }
11344+
11345+ if (actorType) {
11346+ if (auto wrapped = actorType->getOptionalObjectType ()) {
11347+ actorType = wrapped;
11348+ }
11349+ return actorType
1128611350 ->getReferenceStorageReferent ()->getAnyActor ();
1128711351 }
1128811352
@@ -11298,6 +11362,15 @@ VarDecl *ActorIsolation::getActorInstance() const {
1129811362 return actorInstance.dyn_cast <VarDecl *>();
1129911363}
1130011364
11365+ Expr *ActorIsolation::getActorInstanceExpr () const {
11366+ assert (getKind () == ActorInstance);
11367+
11368+ if (silParsed)
11369+ return nullptr ;
11370+
11371+ return actorInstance.dyn_cast <Expr *>();
11372+ }
11373+
1130111374bool ActorIsolation::isMainActor () const {
1130211375 if (silParsed)
1130311376 return false ;
@@ -11317,6 +11390,39 @@ bool ActorIsolation::isDistributedActor() const {
1131711390 return getKind () == ActorInstance && getActor ()->isDistributedActor ();
1131811391}
1131911392
11393+ bool ActorIsolation::isEqual (const ActorIsolation &lhs,
11394+ const ActorIsolation &rhs) {
11395+ if (lhs.getKind () != rhs.getKind ())
11396+ return false ;
11397+
11398+ switch (lhs.getKind ()) {
11399+ case Nonisolated:
11400+ case NonisolatedUnsafe:
11401+ case Unspecified:
11402+ return true ;
11403+
11404+ case ActorInstance: {
11405+ auto *lhsActor = lhs.getActorInstance ();
11406+ auto *rhsActor = rhs.getActorInstance ();
11407+ if (lhsActor && rhsActor) {
11408+ // FIXME: This won't work for arbitrary isolated parameter captures.
11409+ if ((lhsActor->isSelfParameter () && rhsActor->isSelfParamCapture ()) ||
11410+ (lhsActor->isSelfParamCapture () && rhsActor->isSelfParameter ())) {
11411+ return true ;
11412+ }
11413+ }
11414+
11415+ // The parameter index doesn't matter; only the actor instance
11416+ // values must be equal.
11417+ return (lhs.getActor () == rhs.getActor () &&
11418+ lhs.actorInstance == rhs.actorInstance );
11419+ }
11420+
11421+ case GlobalActor:
11422+ return areTypesEqual (lhs.globalActor , rhs.globalActor );
11423+ }
11424+ }
11425+
1132011426BuiltinTupleDecl::BuiltinTupleDecl (Identifier Name, DeclContext *Parent)
1132111427 : NominalTypeDecl(DeclKind::BuiltinTuple, Parent, Name, SourceLoc(),
1132211428 ArrayRef<InheritedEntry>(), nullptr) {}
0 commit comments