Skip to content

Commit 8046423

Browse files
committed
[Distributed] AST: Make argument decoding method 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 06094aa commit 8046423

File tree

5 files changed

+78
-48
lines changed

5 files changed

+78
-48
lines changed

include/swift/AST/ASTContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,10 @@ class ASTContext final {
13491349
NominalTypeDecl *
13501350
getDistributedActorInvocationDecoder(NominalTypeDecl *);
13511351

1352+
/// Find `decodeNextArgument<T>(type: T.Type) -> T` method associated with
1353+
/// invocation decoder of the given distributed actor.
1354+
FuncDecl *getDistributedActorArgumentDecodingMethod(NominalTypeDecl *);
1355+
13521356
private:
13531357
friend Decl;
13541358

include/swift/AST/KnownIdentifiers.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ IDENTIFIER(throwing)
291291
IDENTIFIER(using)
292292
IDENTIFIER(InvocationDecoder)
293293
IDENTIFIER(whenLocal)
294+
IDENTIFIER(decodeNextArgument)
294295

295296
#undef IDENTIFIER
296297
#undef IDENTIFIER_

include/swift/AST/TypeCheckRequests.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,25 @@ class GetDistributedActorInvocationDecoderRequest :
11091109
bool isCached() const { return true; }
11101110
};
11111111

1112+
/// Obtain the method that could be used to decode argument values passed
1113+
/// to a particular actor invocation type.
1114+
class GetDistributedActorArgumentDecodingMethodRequest :
1115+
public SimpleRequest<GetDistributedActorArgumentDecodingMethodRequest,
1116+
FuncDecl *(NominalTypeDecl *),
1117+
RequestFlags::Cached> {
1118+
public:
1119+
using SimpleRequest::SimpleRequest;
1120+
1121+
private:
1122+
friend SimpleRequest;
1123+
1124+
FuncDecl *evaluate(Evaluator &evaluator, NominalTypeDecl *actor) const;
1125+
1126+
public:
1127+
// Caching
1128+
bool isCached() const { return true; }
1129+
};
1130+
11121131
/// Retrieve the static "shared" property within a global actor that provides
11131132
/// the actor instance representing the global actor.
11141133
///

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ SWIFT_REQUEST(TypeChecker, GetDistributedActorSystemPropertyRequest, VarDecl *(N
115115
SWIFT_REQUEST(TypeChecker, GetDistributedActorInvocationDecoderRequest,
116116
NominalTypeDecl *(NominalTypeDecl *),
117117
Cached, NoLocationInfo)
118+
SWIFT_REQUEST(TypeChecker, GetDistributedActorArgumentDecodingMethodRequest,
119+
FuncDecl *(NominalTypeDecl *),
120+
Cache, NoLocationInfo)
118121
SWIFT_REQUEST(TypeChecker, GlobalActorInstanceRequest,
119122
VarDecl *(NominalTypeDecl *),
120123
Cached, NoLocationInfo)

lib/Sema/TypeCheckDistributed.cpp

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -378,22 +378,8 @@ void TypeChecker::checkDistributedActor(ClassDecl *decl) {
378378
(void)decl->getDistributedActorIDProperty();
379379
}
380380

381-
Type swift::getDistributedActorSystemType(NominalTypeDecl *actor) {
382-
assert(actor->isDistributedActor());
383-
auto &ctx = actor->getASTContext();
384-
385-
auto protocol = ctx.getProtocol(KnownProtocolKind::DistributedActor);
386-
if (!protocol)
387-
return ErrorType::get(ctx);
388-
389-
// Dig out the actor system type.
390-
auto module = actor->getParentModule();
391-
Type selfType = actor->getSelfInterfaceType();
392-
auto conformance = module->lookupConformance(selfType, protocol);
393-
return conformance.getTypeWitnessByName(selfType, ctx.Id_ActorSystem);
394-
}
395-
396-
Type swift::getDistributedActorIDType(NominalTypeDecl *actor) {
381+
static Type getAssociatedTypeOfDistributedSystem(NominalTypeDecl *actor,
382+
Identifier member) {
397383
assert(actor->isDistributedActor());
398384
auto &ctx = actor->getASTContext();
399385

@@ -410,22 +396,40 @@ Type swift::getDistributedActorIDType(NominalTypeDecl *actor) {
410396
if (!actorSystemProtocol)
411397
return ErrorType::get(ctx);
412398

413-
AssociatedTypeDecl *idAssocTypeDecl =
414-
actorSystemProtocol->getAssociatedType(ctx.Id_ActorID);
415-
if (!idAssocTypeDecl)
399+
AssociatedTypeDecl *assocTypeDecl =
400+
actorSystemProtocol->getAssociatedType(member);
401+
if (!assocTypeDecl)
416402
return ErrorType::get(ctx);
417403

418404
auto module = actor->getParentModule();
419405
Type selfType = actor->getSelfInterfaceType();
420406
auto conformance = module->lookupConformance(selfType, actorProtocol);
421407
Type dependentType = actorProtocol->getSelfInterfaceType();
422408
dependentType = DependentMemberType::get(dependentType, actorSystemDecl);
423-
dependentType = DependentMemberType::get(dependentType, idAssocTypeDecl);
424-
auto t = dependentType.subst(
425-
SubstitutionMap::getProtocolSubstitutions(
426-
actorProtocol, selfType, conformance));
409+
dependentType = DependentMemberType::get(dependentType, assocTypeDecl);
427410

428-
return t;
411+
return dependentType.subst(SubstitutionMap::getProtocolSubstitutions(
412+
actorProtocol, selfType, conformance));
413+
}
414+
415+
Type swift::getDistributedActorSystemType(NominalTypeDecl *actor) {
416+
assert(actor->isDistributedActor());
417+
auto &ctx = actor->getASTContext();
418+
419+
auto protocol = ctx.getProtocol(KnownProtocolKind::DistributedActor);
420+
if (!protocol)
421+
return ErrorType::get(ctx);
422+
423+
// Dig out the actor system type.
424+
auto module = actor->getParentModule();
425+
Type selfType = actor->getSelfInterfaceType();
426+
auto conformance = module->lookupConformance(selfType, protocol);
427+
return conformance.getTypeWitnessByName(selfType, ctx.Id_ActorSystem);
428+
}
429+
430+
Type swift::getDistributedActorIDType(NominalTypeDecl *actor) {
431+
auto &ctx = actor->getASTContext();
432+
return getAssociatedTypeOfDistributedSystem(actor, ctx.Id_ActorID);
429433
}
430434

431435
NominalTypeDecl *
@@ -440,36 +444,35 @@ ASTContext::getDistributedActorInvocationDecoder(NominalTypeDecl *actor) {
440444
NominalTypeDecl *
441445
GetDistributedActorInvocationDecoderRequest::evaluate(Evaluator &evaluator,
442446
NominalTypeDecl *actor) const {
443-
assert(actor->isDistributedActor());
444447
auto &ctx = actor->getASTContext();
448+
auto decoderTy =
449+
getAssociatedTypeOfDistributedSystem(actor, ctx.Id_InvocationDecoder);
450+
return decoderTy->hasError() ? nullptr : decoderTy->getAnyNominal();
451+
}
445452

446-
auto actorProtocol = ctx.getProtocol(KnownProtocolKind::DistributedActor);
447-
if (!actorProtocol)
453+
FuncDecl *ASTContext::getDistributedActorArgumentDecodingMethod(NominalTypeDecl *actor) {
454+
if (!actor->isDistributedActor())
448455
return nullptr;
449456

450-
AssociatedTypeDecl *actorSystemDecl =
451-
actorProtocol->getAssociatedType(ctx.Id_ActorSystem);
452-
if (!actorSystemDecl)
453-
return nullptr;
457+
return evaluateOrDefault(
458+
evaluator, GetDistributedActorArgumentDecodingMethodRequest{actor}, nullptr);
459+
}
454460

455-
auto actorSystemProtocol = ctx.getProtocol(KnownProtocolKind::DistributedActorSystem);
456-
if (!actorSystemProtocol)
457-
return nullptr;
458461

459-
AssociatedTypeDecl *decoderAssocTypeDecl =
460-
actorSystemProtocol->getAssociatedType(ctx.Id_InvocationDecoder);
461-
if (!decoderAssocTypeDecl)
462-
return nullptr;
462+
FuncDecl *
463+
GetDistributedActorArgumentDecodingMethodRequest::evaluate(Evaluator &evaluator,
464+
NominalTypeDecl *actor) const {
465+
auto &ctx = actor->getASTContext();
463466

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));
467+
auto *decoder = ctx.getDistributedActorInvocationDecoder(actor);
468+
assert(decoder);
469+
470+
auto decoderTy = decoder->getInterfaceType()->getMetatypeInstanceType();
471+
472+
DeclName methodName(ctx, {ctx.Id_decodeNextArgument}, {ctx.Id_type});
473+
auto members = TypeChecker::lookupMember(actor->getDeclContext(), decoderTy,
474+
DeclNameRef(methodName));
473475

474-
return t->getAnyNominal();
476+
assert(members.size() == 1);
477+
return cast<FuncDecl>(members[0].getValueDecl());
475478
}

0 commit comments

Comments
 (0)