@@ -454,6 +454,9 @@ void SILGenFunction::emitProlog(CaptureInfo captureInfo,
454
454
if (!F.isAsync ())
455
455
return ;
456
456
457
+ // Initialize 'actor' if the function is an actor-isolated function or
458
+ // closure.
459
+
457
460
if (auto *funcDecl =
458
461
dyn_cast_or_null<AbstractFunctionDecl>(FunctionDC->getAsDecl ())) {
459
462
auto actorIsolation = getActorIsolation (funcDecl);
@@ -465,38 +468,58 @@ void SILGenFunction::emitProlog(CaptureInfo captureInfo,
465
468
case ActorIsolation::ActorInstance: {
466
469
assert (selfParam && " no self parameter for ActorInstance isolation" );
467
470
ManagedValue selfArg = ManagedValue::forUnmanaged (F.getSelfArgument ());
468
- ManagedValue borrowedSelf = selfArg.borrow (*this , F.getLocation ());
469
- actor = borrowedSelf.getValue ();
471
+ actor = selfArg.borrow (*this , F.getLocation ()).getValue ();
470
472
break ;
471
473
}
472
- case ActorIsolation::GlobalActor: {
473
- CanType actorType = CanType (actorIsolation.getGlobalActor ());
474
- NominalTypeDecl *nominal = actorType->getNominalOrBoundGenericNominal ();
475
- VarDecl *sharedInstanceDecl = nominal->getGlobalActorInstance ();
476
- assert (sharedInstanceDecl && " no shared actor field in global actor" );
477
- SubstitutionMap subs =
478
- actorType->getContextSubstitutionMap (SGM.SwiftModule , nominal);
479
- SILLocation loc = F.getLocation ();
480
- Type instanceType =
481
- actorType->getTypeOfMember (SGM.SwiftModule , sharedInstanceDecl);
482
-
483
- ManagedValue actorMetaType =
484
- ManagedValue::forUnmanaged (B.createMetatype (loc,
485
- SILType::getPrimitiveObjectType (
486
- CanMetatypeType::get (actorType, MetatypeRepresentation::Thin))));
487
-
488
- RValue actorInstanceRV = emitRValueForStorageLoad (loc, actorMetaType,
489
- actorType, /* isSuper*/ false , sharedInstanceDecl, PreparedArguments (),
490
- subs, AccessSemantics::Ordinary, instanceType, SGFContext ());
474
+ case ActorIsolation::GlobalActor:
475
+ loadGlobalActor (actorIsolation.getGlobalActor ());
476
+ break ;
477
+ }
478
+ } else if (auto *closureExpr = dyn_cast<AbstractClosureExpr>(FunctionDC)) {
479
+ auto actorIsolation = closureExpr->getActorIsolation ();
480
+ switch (actorIsolation.getKind ()) {
481
+ case ClosureActorIsolation::Independent:
482
+ break ;
483
+ case ClosureActorIsolation::ActorInstance: {
484
+ VarDecl *actorDecl = actorIsolation.getActorInstance ();
485
+ RValue actorInstanceRV = emitRValueForDecl (F.getLocation (),
486
+ actorDecl, actorDecl->getType (), AccessSemantics::Ordinary);
491
487
ManagedValue actorInstance = std::move (actorInstanceRV).getScalarValue ();
492
- actor = actorInstance.borrow (*this , loc ).getValue ();
488
+ actor = actorInstance.borrow (*this , F. getLocation () ).getValue ();
493
489
break ;
494
490
}
491
+ case ClosureActorIsolation::GlobalActor:
492
+ loadGlobalActor (actorIsolation.getGlobalActor ());
493
+ break ;
495
494
}
496
495
}
497
496
emitHopToCurrentExecutor (F.getLocation ());
498
497
}
499
498
499
+ void SILGenFunction::loadGlobalActor (Type globalActor) {
500
+ assert (F.isAsync ());
501
+ CanType actorType = CanType (globalActor);
502
+ NominalTypeDecl *nominal = actorType->getNominalOrBoundGenericNominal ();
503
+ VarDecl *sharedInstanceDecl = nominal->getGlobalActorInstance ();
504
+ assert (sharedInstanceDecl && " no shared actor field in global actor" );
505
+ SubstitutionMap subs =
506
+ actorType->getContextSubstitutionMap (SGM.SwiftModule , nominal);
507
+ SILLocation loc = F.getLocation ();
508
+ Type instanceType =
509
+ actorType->getTypeOfMember (SGM.SwiftModule , sharedInstanceDecl);
510
+
511
+ ManagedValue actorMetaType =
512
+ ManagedValue::forUnmanaged (B.createMetatype (loc,
513
+ SILType::getPrimitiveObjectType (
514
+ CanMetatypeType::get (actorType, MetatypeRepresentation::Thin))));
515
+
516
+ RValue actorInstanceRV = emitRValueForStorageLoad (loc, actorMetaType,
517
+ actorType, /* isSuper*/ false , sharedInstanceDecl, PreparedArguments (),
518
+ subs, AccessSemantics::Ordinary, instanceType, SGFContext ());
519
+ ManagedValue actorInstance = std::move (actorInstanceRV).getScalarValue ();
520
+ actor = actorInstance.borrow (*this , loc).getValue ();
521
+ }
522
+
500
523
static void emitIndirectResultParameters (SILGenFunction &SGF, Type resultType,
501
524
DeclContext *DC) {
502
525
// Expand tuples.
0 commit comments