32
32
using namespace swift ;
33
33
using namespace Lowering ;
34
34
35
+ static AbstractFunctionDecl *lookupAssignIdentityFunc (ASTContext &C) {
36
+ auto transportDecl = C.getActorTransportDecl ();
37
+
38
+ for (auto decl : transportDecl->lookupDirect (DeclName (C.Id_assignIdentity )))
39
+ if (auto funcDecl = dyn_cast<AbstractFunctionDecl>(decl))
40
+ return funcDecl;
41
+
42
+ llvm_unreachable (" Missing ActorTransport.assignIdentity function" );
43
+ }
44
+
45
+ static AbstractFunctionDecl *lookupActorReadyFunc (ASTContext &C) {
46
+ auto transportDecl = C.getActorTransportDecl ();
47
+
48
+ for (auto decl : transportDecl->lookupDirect (DeclName (C.Id_actorReady )))
49
+ if (auto funcDecl = dyn_cast<AbstractFunctionDecl>(decl))
50
+ return funcDecl;
51
+
52
+ llvm_unreachable (" Missing ActorTransport.actorReady function" );
53
+ }
54
+
35
55
/* *****************************************************************************/
36
56
/* ***************** DISTRIBUTED ACTOR STORAGE INITIALIZATION ******************/
37
57
/* *****************************************************************************/
@@ -72,18 +92,6 @@ getActorTransportArgument(ASTContext& C, SILFunction& F, ConstructorDecl *ctor)
72
92
llvm_unreachable (" Missing required ActorTransport argument!" );
73
93
}
74
94
75
-
76
- static AbstractFunctionDecl*
77
- lookupAssignIdentityFunc (ASTContext& C) {
78
- auto transportDecl = C.getActorTransportDecl ();
79
-
80
- for (auto decl : transportDecl->lookupDirect (DeclName (C.Id_assignIdentity )))
81
- if (auto funcDecl = dyn_cast<AbstractFunctionDecl>(decl))
82
- return funcDecl;
83
-
84
- return nullptr ;
85
- }
86
-
87
95
// / Synthesize the actorTransport initialization:
88
96
// /
89
97
// / \verbatim
@@ -183,22 +191,19 @@ static void emitDistributedActorIdentity_init_assignIdentity(
183
191
auto loc = SILLocation (ctor);
184
192
loc.markAutoGenerated ();
185
193
186
- // === prepare the transport.assignIdentity(_:) function
187
-
194
+ // ==== prepare the transport.assignIdentity(_:) function
188
195
AbstractFunctionDecl *assignIdentityFuncDecl = lookupAssignIdentityFunc (C);
189
-
190
- assert (assignIdentityFuncDecl && " Cannot find ActorTransport.assignIdentity!" );
191
196
auto assignIdentityFnRef = SILDeclRef (assignIdentityFuncDecl);
192
197
193
- // === Open the transport existential before call
198
+ // --- Prepare the arguments
194
199
SILValue transportArgValue = getActorTransportArgument (C, F, ctor);
195
200
SILValue selfArgValue = F.getSelfArgument ();
196
201
ProtocolDecl *distributedActorProto = C.getProtocol (KnownProtocolKind::DistributedActor);
197
202
ProtocolDecl *transportProto = C.getProtocol (KnownProtocolKind::ActorTransport);
198
203
assert (distributedActorProto);
199
204
assert (transportProto);
200
205
201
- // --- open the transport existential
206
+ // --- Open the transport existential
202
207
OpenedArchetypeType *Opened;
203
208
auto transportASTType = transportArgValue->getType ().getASTType ();
204
209
auto openedTransportType =
@@ -279,6 +284,10 @@ static void emitDistributedActorIdentity_init_assignIdentity(
279
284
IsTake, IsInitialization);
280
285
}
281
286
287
+ /* *****************************************************************************/
288
+ /* ****************** DISTRIBUTED ACTOR LOCAL INIT *****************************/
289
+ /* *****************************************************************************/
290
+
282
291
void SILGenFunction::initializeDistributedActorImplicitStorageInit (
283
292
ConstructorDecl *ctor, ManagedValue selfArg) {
284
293
VarDecl *selfVarDecl = ctor->getImplicitSelfDecl ();
@@ -336,7 +345,89 @@ void SILGenFunction::initializeDistributedActorImplicitStorageInit(
336
345
337
346
void SILGenFunction::emitDistributedActorReady (
338
347
ConstructorDecl *ctor, ManagedValue selfArg) {
339
- // TODO(distributed): implement actorReady call
348
+ VarDecl *selfVarDecl = ctor->getImplicitSelfDecl ();
349
+ auto *dc = ctor->getDeclContext ();
350
+ auto classDecl = dc->getSelfClassDecl ();
351
+ auto &C = classDecl->getASTContext ();
352
+
353
+ // Only designated initializers get the lifecycle handling injected
354
+ if (!ctor->isDesignatedInit ())
355
+ return ;
356
+
357
+ SILLocation loc = RegularLocation (ctor);
358
+ loc.markAutoGenerated ();
359
+
360
+ // // ==== Prepare basic blocks
361
+ // auto actorReadyBB = createBasicBlock();
362
+ // B.setInsertionPoint(actorReadyBB);
363
+
364
+ // === Prepare the arguments
365
+ SILValue transportArgValue = getActorTransportArgument (C, F, ctor);
366
+ SILValue selfArgValue = F.getSelfArgument ();
367
+
368
+ ProtocolDecl *distributedActorProto = C.getProtocol (KnownProtocolKind::DistributedActor);
369
+ ProtocolDecl *transportProto = C.getProtocol (KnownProtocolKind::ActorTransport);
370
+ assert (distributedActorProto);
371
+ assert (transportProto);
372
+
373
+ // --- Open the transport existential
374
+ OpenedArchetypeType *Opened;
375
+ auto transportASTType = transportArgValue->getType ().getASTType ();
376
+ auto openedTransportType =
377
+ transportASTType->openAnyExistentialType (Opened)->getCanonicalType ();
378
+ auto openedTransportSILType = F.getLoweredType (openedTransportType);
379
+ auto transportArchetypeValue = B.createOpenExistentialAddr (
380
+ loc, transportArgValue, openedTransportSILType, OpenedExistentialAccess::Immutable);
381
+
382
+ // === Make the transport.actorReady call
383
+ // --- prepare the witness_method
384
+ // Note: it does not matter on what module we perform the lookup,
385
+ // it is currently ignored. So the Stdlib module is good enough.
386
+ auto *module = getModule ().getSwiftModule ();
387
+
388
+ // the conformance here is just an abstract thing so we can simplify
389
+ // auto transportConfRef = module->lookupConformance(
390
+ // openedTransportType, transportProto);
391
+ auto transportConfRef = ProtocolConformanceRef (transportProto);
392
+ assert (!transportConfRef.isInvalid () && " Missing conformance to `ActorTransport`" );
393
+
394
+ auto *selfTyDecl = ctor->getParent ()->getSelfNominalTypeDecl ();
395
+ auto selfTy = F.mapTypeIntoContext (selfTyDecl->getDeclaredInterfaceType ()); // TODO: thats just self var devl getType
396
+
397
+ auto distributedActorConfRef = module ->lookupConformance (
398
+ selfTy,
399
+ distributedActorProto);
400
+ assert (!distributedActorConfRef.isInvalid () && " Missing conformance to `DistributedActor`" );
401
+
402
+ // === Prepare the actorReady function
403
+ auto actorReadyMethod =
404
+ cast<FuncDecl>(transportProto->getSingleRequirement (C.Id_actorReady ));
405
+ auto actorReadyRef = SILDeclRef (actorReadyMethod, SILDeclRef::Kind::Func);
406
+ auto actorReadySILTy =
407
+ getConstantInfo (getTypeExpansionContext (), actorReadyRef)
408
+ .getSILType ();
409
+
410
+ auto readyWitnessMethod = B.createWitnessMethod (
411
+ loc,
412
+ /* lookupTy*/ openedTransportType,
413
+ /* Conformance*/ transportConfRef,
414
+ /* member*/ actorReadyRef,
415
+ /* methodTy*/ actorReadySILTy);
416
+
417
+ // --- prepare conformance subs
418
+ auto genericSig = actorReadyMethod->getGenericSignature ();
419
+ fprintf (stderr, " [%s:%d] (%s) GENERIC SIG\n " , __FILE__, __LINE__, __FUNCTION__);
420
+ genericSig->dump ();
421
+
422
+ SubstitutionMap subs =
423
+ SubstitutionMap::get (genericSig,
424
+ {openedTransportType, selfTy},
425
+ {transportConfRef, distributedActorConfRef});
426
+
427
+ // ---- actually call transport.actorReady(self)
428
+ B.createApply (
429
+ loc, readyWitnessMethod, subs,
430
+ { selfArgValue, transportArchetypeValue});
340
431
}
341
432
342
433
/* *****************************************************************************/
@@ -437,13 +528,10 @@ void SILGenFunction::emitDistributedActorFactory(FuncDecl *fd) {
437
528
/* subs*/ {},
438
529
{selfMetatypeValue});
439
530
440
- // ManagedValue remoteCast =
441
- // B.createUncheckedBitCast(loc, ManagedValue::forUnmanaged(remote), returnTy);
442
-
443
531
// ==== Initialize distributed actor properties
444
532
// --- Store the identity: self.id = identity
445
- // emitDistributedActorIdentityStore(
446
- // C, *this, /*actorSelf*/remote, fd, identityArg);
533
+ emitDistributedActorIdentityStore (
534
+ C, *this , /* actorSelf*/ remote, fd, identityArg);
447
535
448
536
// --- Store the transport: self.transport = transport
449
537
// FIXME(distributed): IMPLEMENT:
@@ -452,7 +540,6 @@ void SILGenFunction::emitDistributedActorFactory(FuncDecl *fd) {
452
540
453
541
// ==== Return the fully initialized remote instance
454
542
B.createReturn (loc, remote);
455
- // B.createReturn(loc, remoteCast);
456
543
457
544
// // ==== Branch to return the fully initialized remote instance
458
545
// B.createBranch(loc, returnBB, {remote});
@@ -492,7 +579,6 @@ void SILGenFunction::emitDistributedActorFactory(FuncDecl *fd) {
492
579
// TODO: implement calling resignAddress via a defer in deinit
493
580
494
581
495
-
496
582
/* *****************************************************************************/
497
583
/* **************************** DISTRIBUTED THUNKS *****************************/
498
584
/* *****************************************************************************/
0 commit comments