Skip to content

Commit 06094aa

Browse files
committed
[Distributed] AST: Make invocation decoder discoverable given distributed actor
This is going to be used by IRGen later on to make it possible to decode arguments inside of distributed target accessor instead of allocating additional storage.
1 parent 9e93369 commit 06094aa

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

include/swift/AST/ASTContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,10 @@ class ASTContext final {
13451345
/// alternative specified via the -entry-point-function-name frontend flag.
13461346
std::string getEntryPointFunctionName() const;
13471347

1348+
/// Find the concrete invocation decoder associated with the given actor.
1349+
NominalTypeDecl *
1350+
getDistributedActorInvocationDecoder(NominalTypeDecl *);
1351+
13481352
private:
13491353
friend Decl;
13501354

include/swift/AST/KnownIdentifiers.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ IDENTIFIER(system)
289289
IDENTIFIER(target)
290290
IDENTIFIER(throwing)
291291
IDENTIFIER(using)
292+
IDENTIFIER(InvocationDecoder)
292293
IDENTIFIER(whenLocal)
293294

294295
#undef IDENTIFIER

include/swift/AST/TypeCheckRequests.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,24 @@ class GetDistributedActorIDPropertyRequest :
10911091
bool isCached() const { return true; }
10921092
};
10931093

1094+
/// Obtain the invocation decoder associated with the given distributed actor.
1095+
class GetDistributedActorInvocationDecoderRequest :
1096+
public SimpleRequest<GetDistributedActorInvocationDecoderRequest,
1097+
NominalTypeDecl *(NominalTypeDecl *),
1098+
RequestFlags::Cached> {
1099+
public:
1100+
using SimpleRequest::SimpleRequest;
1101+
1102+
private:
1103+
friend SimpleRequest;
1104+
1105+
NominalTypeDecl *evaluate(Evaluator &evaluator, NominalTypeDecl *actor) const;
1106+
1107+
public:
1108+
// Caching
1109+
bool isCached() const { return true; }
1110+
};
1111+
10941112
/// Retrieve the static "shared" property within a global actor that provides
10951113
/// the actor instance representing the global actor.
10961114
///

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ SWIFT_REQUEST(TypeChecker, GetDistributedActorIDPropertyRequest, VarDecl *(Nomin
112112
Cached, NoLocationInfo)
113113
SWIFT_REQUEST(TypeChecker, GetDistributedActorSystemPropertyRequest, VarDecl *(NominalTypeDecl *),
114114
Cached, NoLocationInfo)
115+
SWIFT_REQUEST(TypeChecker, GetDistributedActorInvocationDecoderRequest,
116+
NominalTypeDecl *(NominalTypeDecl *),
117+
Cached, NoLocationInfo)
115118
SWIFT_REQUEST(TypeChecker, GlobalActorInstanceRequest,
116119
VarDecl *(NominalTypeDecl *),
117120
Cached, NoLocationInfo)

lib/Sema/TypeCheckDistributed.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,49 @@ Type swift::getDistributedActorIDType(NominalTypeDecl *actor) {
427427

428428
return t;
429429
}
430+
431+
NominalTypeDecl *
432+
ASTContext::getDistributedActorInvocationDecoder(NominalTypeDecl *actor) {
433+
if (!actor->isDistributedActor())
434+
return nullptr;
435+
436+
return evaluateOrDefault(
437+
evaluator, GetDistributedActorInvocationDecoderRequest{actor}, nullptr);
438+
}
439+
440+
NominalTypeDecl *
441+
GetDistributedActorInvocationDecoderRequest::evaluate(Evaluator &evaluator,
442+
NominalTypeDecl *actor) const {
443+
assert(actor->isDistributedActor());
444+
auto &ctx = actor->getASTContext();
445+
446+
auto actorProtocol = ctx.getProtocol(KnownProtocolKind::DistributedActor);
447+
if (!actorProtocol)
448+
return nullptr;
449+
450+
AssociatedTypeDecl *actorSystemDecl =
451+
actorProtocol->getAssociatedType(ctx.Id_ActorSystem);
452+
if (!actorSystemDecl)
453+
return nullptr;
454+
455+
auto actorSystemProtocol = ctx.getProtocol(KnownProtocolKind::DistributedActorSystem);
456+
if (!actorSystemProtocol)
457+
return nullptr;
458+
459+
AssociatedTypeDecl *decoderAssocTypeDecl =
460+
actorSystemProtocol->getAssociatedType(ctx.Id_InvocationDecoder);
461+
if (!decoderAssocTypeDecl)
462+
return nullptr;
463+
464+
auto module = actor->getParentModule();
465+
Type selfType = actor->getSelfInterfaceType();
466+
auto conformance = module->lookupConformance(selfType, actorProtocol);
467+
Type dependentType = actorProtocol->getSelfInterfaceType();
468+
dependentType = DependentMemberType::get(dependentType, actorSystemDecl);
469+
dependentType = DependentMemberType::get(dependentType, decoderAssocTypeDecl);
470+
auto t = dependentType.subst(
471+
SubstitutionMap::getProtocolSubstitutions(
472+
actorProtocol, selfType, conformance));
473+
474+
return t->getAnyNominal();
475+
}

0 commit comments

Comments
 (0)