Skip to content

Commit 78a889f

Browse files
committed
[DistributedActor] ok progress on getting the system via witness
1 parent b65f66d commit 78a889f

File tree

9 files changed

+312
-75
lines changed

9 files changed

+312
-75
lines changed

include/swift/AST/ASTContext.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -658,12 +658,11 @@ class ASTContext final {
658658

659659
/// Retrieve the declaration of DistributedActorSystem.make().
660660
///
661-
/// \param actorOrSystem distributed actor or actor system to get the
662-
/// remoteCall function for. Since the method we're looking for is an ad-hoc
663-
/// requirement, a specific type MUST be passed here as it is not possible
664-
/// to obtain the decl from just the `DistributedActorSystem` protocol type.
661+
/// \param thunk the function from which we'll be invoking things on the obtained
662+
/// actor system; This way we'll always get the right type, taking care of any
663+
/// where clauses etc.
665664
FuncDecl *getMakeInvocationEncoderOnDistributedActorSystem(
666-
NominalTypeDecl *actorOrSystem) const;
665+
AbstractFunctionDecl *thunk) const;
667666

668667
// Retrieve the declaration of
669668
// DistributedInvocationEncoder.recordGenericSubstitution(_:).

include/swift/AST/DistributedDecl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class DeclContext;
3131
class FuncDecl;
3232
class NominalTypeDecl;
3333

34+
/// Determine the concrete type of 'ActorSystem' as seen from the member.
35+
/// E.g. when in a protocol, and trying to determine what the actor system was
36+
/// constrained to.
37+
Type getConcreteReplacementForProtocolActorSystemType(ValueDecl *member);
38+
3439
/// Determine the `ActorSystem` type for the given actor.
3540
Type getDistributedActorSystemType(NominalTypeDecl *actor);
3641

include/swift/SIL/SILDeclRef.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ struct SILDeclRef {
105105
/// entry point of a class ConstructorDecl or the constructor of a value
106106
/// ConstructorDecl.
107107
Allocator,
108+
108109
/// Initializer - this constant references the initializing constructor
109110
/// entry point of the class ConstructorDecl in loc.
110111
Initializer,

lib/AST/ASTContext.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/AST/ConcreteDeclRef.h"
2323
#include "swift/AST/DiagnosticEngine.h"
2424
#include "swift/AST/DiagnosticsSema.h"
25+
#include "swift/AST/DistributedDecl.h"
2526
#include "swift/AST/ExistentialLayout.h"
2627
#include "swift/AST/FileUnit.h"
2728
#include "swift/AST/ForeignAsyncConvention.h"
@@ -1277,15 +1278,32 @@ AbstractFunctionDecl *ASTContext::getRemoteCallOnDistributedActorSystem(
12771278
}
12781279

12791280
FuncDecl *ASTContext::getMakeInvocationEncoderOnDistributedActorSystem(
1280-
NominalTypeDecl *actorOrSystem) const {
1281-
NominalTypeDecl *system = actorOrSystem;
1282-
assert(actorOrSystem && "distributed actor (or system) decl must be provided");
1283-
if (actorOrSystem->isDistributedActor()) {
1284-
auto var = actorOrSystem->getDistributedActorSystemProperty();
1285-
system = var->getInterfaceType()->getAnyNominal();
1286-
}
1281+
AbstractFunctionDecl *thunk) const {
1282+
// NominalTypeDecl *system = actorOrSystem;
1283+
// assert(actorOrSystem && "distributed actor (or system) decl must be provided");
1284+
// if (actorOrSystem->isDistributedActor()) {
1285+
// auto ty = getDistributedActorSystemType(actorOrSystem);
1286+
// fprintf(stderr, "[%s:%d] (%s) getDistributedActorSystemType(actorOrSystem)\n", __FILE__, __LINE__, __FUNCTION__);
1287+
// ty.dump();
1288+
//
1289+
// auto actor = actorOrSystem;
1290+
// auto var = actor->getDistributedActorSystemProperty();
1291+
// assert(var && "Could not locate 'actorSystem' property!");
1292+
// system = var->getInterfaceType()->getAnyNominal();
1293+
// }
1294+
1295+
auto systemTy = getConcreteReplacementForProtocolActorSystemType(thunk);
1296+
assert(systemTy && "No specific ActorSystem type found!");
1297+
fprintf(stderr, "[%s:%d] (%s) FOUND SYSTEM TYPE: %s\n", __FILE__, __LINE__, __FUNCTION__, systemTy->getAnyNominal()->getName().str().str().c_str());
1298+
1299+
auto systemNominal = systemTy->getNominalOrBoundGenericNominal();
1300+
assert(systemNominal && "No system nominal type found!");
1301+
1302+
// // NO, since we need the specific one
1303+
// auto &C = thunk->getASTContext();
1304+
// auto systemNominal = C.getProtocol(KnownProtocolKind::DistributedActorSystem);
12871305

1288-
for (auto result : system->lookupDirect(Id_makeInvocationEncoder)) {
1306+
for (auto result : systemNominal->lookupDirect(Id_makeInvocationEncoder)) {
12891307
auto *fd = dyn_cast<FuncDecl>(result);
12901308
if (!fd)
12911309
continue;

lib/AST/DistributedDecl.cpp

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -65,19 +65,50 @@ using namespace swift;
6565
/************** Distributed Actor System Associated Types *********************/
6666
/******************************************************************************/
6767

68-
Type swift::getDistributedActorSystemType(NominalTypeDecl *actor) {
69-
assert(actor->isDistributedActor());
70-
auto &ctx = actor->getASTContext();
68+
Type swift::getConcreteReplacementForProtocolActorSystemType(ValueDecl *member) {
69+
auto &C = member->getASTContext();
70+
auto *DC = member->getDeclContext();
71+
auto DA = C.getDistributedActorDecl();
7172

72-
auto protocol = ctx.getProtocol(KnownProtocolKind::DistributedActor);
73-
if (!protocol)
74-
return ErrorType::get(ctx);
73+
if (auto *protocol = DC->getSelfProtocolDecl()) {
74+
GenericSignature signature;
75+
if (auto *genericContext = member->getAsGenericContext()) {
76+
signature = genericContext->getGenericSignature();
77+
} else {
78+
signature = DC->getGenericSignatureOfContext();
79+
}
80+
81+
auto ActorSystemAssocType =
82+
DA->getAssociatedType(C.Id_ActorSystem)->getDeclaredInterfaceType();
83+
84+
auto systemTy = signature->getConcreteType(ActorSystemAssocType);
85+
assert(systemTy && "couldn't find concrete type for actor system");
86+
return systemTy;
87+
} else if (auto classDecl = dyn_cast<ClassDecl>(DC)) {
88+
if (!classDecl)
89+
return Type();
90+
91+
return getDistributedActorSystemType(classDecl);
92+
}
93+
94+
llvm_unreachable("Unable to fetch ActorSystem type!");
95+
}
96+
97+
Type swift::getDistributedActorSystemType(NominalTypeDecl *nominal) {
98+
assert(!isa<ProtocolDecl>(nominal) && "FIXME: we should deal with it here too or collapse the two impls");
99+
assert(nominal->isDistributedActor());
100+
auto &C = nominal->getASTContext();
101+
auto *DC = nominal->getDeclContext();
102+
103+
auto DAP = C.getDistributedActorDecl();
104+
if (!DAP)
105+
return ErrorType::get(C);
75106

76107
// Dig out the actor system type.
77-
auto module = actor->getParentModule();
78-
Type selfType = actor->getSelfInterfaceType();
79-
auto conformance = module->lookupConformance(selfType, protocol);
80-
return conformance.getTypeWitnessByName(selfType, ctx.Id_ActorSystem);
108+
auto module = nominal->getParentModule();
109+
Type selfType = nominal->getSelfInterfaceType();
110+
auto conformance = module->lookupConformance(selfType, DAP);
111+
return conformance.getTypeWitnessByName(selfType, C.Id_ActorSystem);
81112
}
82113

83114
Type swift::getDistributedActorIDType(NominalTypeDecl *actor) {

0 commit comments

Comments
 (0)