@@ -36,6 +36,44 @@ using namespace Lowering;
36
36
/* ***************** DISTRIBUTED ACTOR STORAGE INITIALIZATION ******************/
37
37
/* *****************************************************************************/
38
38
39
+ // / Get the `ActorTransport` parameter of the constructor.
40
+ // / Sema should have guaranteed that there is exactly one of them for any
41
+ // / designated initializer of a distributed actor.
42
+ static SILArgument*
43
+ getActorTransportArgument (ASTContext& C, SILFunction& F, ConstructorDecl *ctor) {
44
+ auto *DC = cast<DeclContext>(ctor);
45
+ auto module = DC->getParentModule ();
46
+
47
+ auto *transportProto =
48
+ C.getProtocol (KnownProtocolKind::ActorTransport);
49
+ Type transportTy = transportProto->getDeclaredInterfaceType ();
50
+
51
+ for (auto arg : F.getArguments ()) {
52
+ // TODO(distributed): also be able to locate a generic transport
53
+ Type argTy = arg->getType ().getASTType ();
54
+ auto argDecl = arg->getDecl ();
55
+ argTy->dump ();
56
+ argDecl->dump ();
57
+
58
+ auto conformsToTransport = module->lookupConformance (
59
+ argDecl->getInterfaceType (), transportProto);
60
+
61
+ // Is it a protocol that conforms to ActorTransport?
62
+ if (argTy->isEqual (transportTy) || conformsToTransport) {
63
+ return arg;
64
+ }
65
+
66
+ // Is it some specific ActorTransport?
67
+ auto result = module->lookupConformance (argTy, transportProto);
68
+ if (!result.isInvalid ()) {
69
+ return arg;
70
+ }
71
+ }
72
+
73
+ // did not find argument of ActorTransport type!
74
+ llvm_unreachable (" Missing required ActorTransport argument!" );
75
+ }
76
+
39
77
40
78
static AbstractFunctionDecl*
41
79
lookupAssignIdentityFunc (ASTContext& C) {
@@ -56,8 +94,11 @@ lookupAssignIdentityFunc(ASTContext& C) {
56
94
// / // }
57
95
// / \endverbatim
58
96
static void
59
- emitDistributedActorTransportInit (SILGenFunction &SGF, ManagedValue borrowedSelfArg, VarDecl *selfDecl, ConstructorDecl *ctor,
60
- Pattern *pattern, VarDecl *var) {
97
+ emitDistributedActorTransportInit (
98
+ SILGenFunction &SGF,
99
+ ManagedValue borrowedSelfArg, VarDecl *selfDecl,
100
+ ConstructorDecl *ctor,
101
+ Pattern *pattern, VarDecl *var) {
61
102
auto &C = selfDecl->getASTContext ();
62
103
auto &B = SGF.B ;
63
104
auto &F = SGF.F ;
@@ -71,7 +112,7 @@ emitDistributedActorTransportInit(SILGenFunction &SGF, ManagedValue borrowedSelf
71
112
SGF.F .dump ();
72
113
73
114
// ==== Prepare assignment: get the self.transport address
74
- SILValue transportArgValue = F. getArgument ( 0 );
115
+ SILValue transportArgValue = getActorTransportArgument (C, F, ctor );
75
116
ManagedValue transportArgManaged = ManagedValue::forUnmanaged (transportArgValue);
76
117
auto transportDecl = C.getActorTransportDecl ();
77
118
@@ -96,10 +137,11 @@ emitDistributedActorTransportInit(SILGenFunction &SGF, ManagedValue borrowedSelf
96
137
// / // }
97
138
// / \endverbatim
98
139
static void
99
- emitDistributedActorIdentityInit (SILGenFunction &SGF,
100
- ManagedValue borrowedSelfArg,
101
- VarDecl *selfVarDecl, ConstructorDecl *ctor,
102
- Pattern *pattern, VarDecl *var) {
140
+ emitDistributedActorIdentityInit (
141
+ SILGenFunction &SGF,
142
+ ManagedValue borrowedSelfArg, VarDecl *selfVarDecl,
143
+ ConstructorDecl *ctor,
144
+ Pattern *pattern, VarDecl *var) {
103
145
auto &C = selfVarDecl->getASTContext ();
104
146
auto &B = SGF.B ;
105
147
auto &F = SGF.F ;
@@ -120,8 +162,8 @@ emitDistributedActorIdentityInit(SILGenFunction &SGF,
120
162
auto assignIdentityFnRef = SILDeclRef (assignIdentityFuncDecl);
121
163
122
164
// === Open the transport existential before call
123
- SILValue transportArgValue = F. getArgument ( 0 );
124
- SILValue selfArgValue = F.getArgument ( 1 );
165
+ SILValue transportArgValue = getActorTransportArgument (C, F, ctor );
166
+ SILValue selfArgValue = F.getSelfArgument ( );
125
167
ProtocolDecl *distributedActorProto = C.getProtocol (KnownProtocolKind::DistributedActor);
126
168
ProtocolDecl *transportProto = C.getProtocol (KnownProtocolKind::ActorTransport);
127
169
assert (distributedActorProto);
@@ -137,8 +179,6 @@ emitDistributedActorIdentityInit(SILGenFunction &SGF,
137
179
loc, transportArgValue, openedTransportSILType, OpenedExistentialAccess::Immutable);
138
180
139
181
// --- prepare `Self.self` metatype
140
- // TODO: how to get @dynamic_self, do we need it?
141
- // %14 = metatype $@thick @dynamic_self DA_DefaultDeinit.Type // type-defs: %1; user: %16
142
182
auto *selfTyDecl = ctor->getParent ()->getSelfNominalTypeDecl ();
143
183
// This would be bad: since not ok for generic
144
184
// auto selfMetatype = SGF.getLoweredType(selfTyDecl->getInterfaceType());
@@ -162,11 +202,6 @@ emitDistributedActorIdentityInit(SILGenFunction &SGF,
162
202
auto transportConfRef = ProtocolConformanceRef (transportProto);
163
203
assert (!transportConfRef.isInvalid () && " Missing conformance to `ActorTransport`" );
164
204
165
- // fprintf(stderr, "[%s:%d] (%s) selfArg->getType()\n", __FILE__, __LINE__, __FUNCTION__);
166
- // selfArg.getType().dump();
167
- // fprintf(stderr, "[%s:%d] (%s) distributedActorProto\n", __FILE__, __LINE__, __FUNCTION__);
168
- // distributedActorProto->dump();
169
-
170
205
auto selfTy = F.mapTypeIntoContext (selfTyDecl->getDeclaredInterfaceType ()); // TODO: thats just self var devl getType
171
206
172
207
auto distributedActorConfRef = module->lookupConformance (
@@ -236,6 +271,13 @@ void SILGenFunction::initializeDistributedActorImplicitStorageInit(
236
271
auto classDecl = dc->getSelfClassDecl ();
237
272
auto &C = classDecl->getASTContext ();
238
273
274
+ // Only designated initializers get the lifecycle handling injected
275
+ if (!ctor->isDesignatedInit ()) {
276
+ fprintf (stderr, " [%s:%d] (%s) NOT DESIGNATED INIT SKIP\n " , __FILE__, __LINE__, __FUNCTION__);
277
+
278
+ return ;
279
+ }
280
+
239
281
SILLocation prologueLoc = RegularLocation (ctor);
240
282
prologueLoc.markAsPrologue (); // TODO: no idea if this is necessary or makes sense
241
283
0 commit comments