@@ -87,12 +87,10 @@ lookupAssignIdentityFunc(ASTContext& C) {
87
87
// / Synthesize the actorTransport initialization:
88
88
// /
89
89
// / \verbatim
90
- // / // init(..., <transport>: ActorTransport) ... {
91
- // / self.actorTransport = transport
92
- // / // }
90
+ // / self.actorTransport = <<constructor parameter:ActorTransport>>
93
91
// / \endverbatim
94
92
static void
95
- emitDistributedActorTransportInit (
93
+ emitDistributedActor_init_transportStore (
96
94
SILGenFunction &SGF,
97
95
ManagedValue borrowedSelfArg, VarDecl *selfDecl,
98
96
ConstructorDecl *ctor,
@@ -109,7 +107,6 @@ emitDistributedActorTransportInit(
109
107
// ==== Prepare assignment: get the self.transport address
110
108
SILValue transportArgValue = getActorTransportArgument (C, F, ctor);
111
109
ManagedValue transportArgManaged = ManagedValue::forUnmanaged (transportArgValue);
112
- auto transportDecl = C.getActorTransportDecl ();
113
110
114
111
// ----
115
112
auto *selfTyDecl = ctor->getParent ()->getSelfNominalTypeDecl ();
@@ -124,15 +121,55 @@ emitDistributedActorTransportInit(
124
121
IsNotTake, IsInitialization);
125
122
}
126
123
124
+ // / Synthesize storing the passed in managed identity to the `id` property:
125
+ // /
126
+ // / \verbatim
127
+ // / self.id = <<parameter:identity>>
128
+ // / \endverbatim
129
+ static void
130
+ emitDistributedActorIdentityStore (
131
+ ASTContext& C, SILGenFunction &SGF,
132
+ SILValue actorSelf, FuncDecl *func, SILArgument *identityArg) {
133
+ auto &B = SGF.B ;
134
+ auto &F = SGF.F ;
135
+ auto &SGM = SGF.SGM ;
136
+ SILGenFunctionBuilder builder (SGM);
137
+
138
+ auto *dc = func->getDeclContext ();
139
+ auto classDecl = dc->getSelfClassDecl ();
140
+ assert (classDecl->isDistributedActor ());
141
+
142
+ auto loc = SILLocation (func);
143
+ loc.markAutoGenerated ();
144
+
145
+ // ==== Prepare the property reference: self.id
146
+ auto idVars = classDecl->lookupDirect (C.Id_id );
147
+ assert (idVars.size () == 1 );
148
+ auto *idVar = dyn_cast<VarDecl>(idVars.front ());
149
+
150
+ // ==== Prepare assignment
151
+ // SILValue identityArgValue = identityArg->getValue();
152
+ fprintf (stderr, " [%s:%d] (%s) THE ACTOR SELF\n " , __FILE__, __LINE__, __FUNCTION__);
153
+ actorSelf->dump ();
154
+
155
+ SILValue identityArgValue = identityArg;
156
+ auto idFieldAddr = B.createRefElementAddr (
157
+ loc, actorSelf, idVar,
158
+ SGF.getLoweredType (idVar->getInterfaceType ()));
159
+
160
+ // ==== Store the transport
161
+ B.createCopyAddr (loc,
162
+ /* src*/ identityArgValue,
163
+ /* dest*/ idFieldAddr,
164
+ IsNotTake, IsInitialization); // TODO(distributed): should it be take?
165
+ }
166
+
127
167
// / Synthesize the distributed actor's identity (`id`) initialization:
128
168
// /
129
169
// / \verbatim
130
- // / // init(..., <transport>: ActorTransport) ... {
131
170
// / self.id = transport.assignIdentity(Self.self)
132
- // / // }
133
171
// / \endverbatim
134
- static void
135
- emitDistributedActorIdentityInit (
172
+ static void emitDistributedActorIdentity_init_assignIdentity (
136
173
SILGenFunction &SGF,
137
174
ManagedValue borrowedSelfArg, VarDecl *selfVarDecl,
138
175
ConstructorDecl *ctor,
@@ -198,8 +235,6 @@ emitDistributedActorIdentityInit(
198
235
distributedActorProto);
199
236
assert (!distributedActorConfRef.isInvalid () && " Missing conformance to `DistributedActor`" );
200
237
201
- // auto anyActorIdentityDecl = C.getAnyActorIdentityDecl();
202
-
203
238
auto assignIdentityMethod =
204
239
cast<FuncDecl>(transportProto->getSingleRequirement (C.Id_assignIdentity ));
205
240
auto assignIdentityRef = SILDeclRef (assignIdentityMethod, SILDeclRef::Kind::Func);
@@ -252,11 +287,8 @@ void SILGenFunction::initializeDistributedActorImplicitStorageInit(
252
287
auto &C = classDecl->getASTContext ();
253
288
254
289
// Only designated initializers get the lifecycle handling injected
255
- if (!ctor->isDesignatedInit ()) {
256
- fprintf (stderr, " [%s:%d] (%s) NOT DESIGNATED INIT SKIP\n " , __FILE__, __LINE__, __FUNCTION__);
257
-
290
+ if (!ctor->isDesignatedInit ())
258
291
return ;
259
- }
260
292
261
293
SILLocation prologueLoc = RegularLocation (ctor);
262
294
prologueLoc.markAsPrologue (); // TODO: no idea if this is necessary or makes sense
@@ -284,12 +316,14 @@ void SILGenFunction::initializeDistributedActorImplicitStorageInit(
284
316
if (var->getName () == C.Id_actorTransport &&
285
317
var->getInterfaceType ()->isEqual (transportTy)) {
286
318
transportMember = var;
287
- emitDistributedActorTransportInit (*this , borrowedSelfArg, selfVarDecl, ctor, pattern, var);
319
+ emitDistributedActor_init_transportStore (
320
+ *this , borrowedSelfArg, selfVarDecl, ctor, pattern, var);
288
321
} else if (var->getName () == C.Id_id &&
289
322
(var->getInterfaceType ()->isEqual (identityProtoTy) ||
290
323
var->getInterfaceType ()->isEqual (anyIdentityTy))) { // TODO(distributed): stick one way to store, but today we can't yet store the existential
291
324
idMember = var;
292
- emitDistributedActorIdentityInit (*this , borrowedSelfArg, selfVarDecl, ctor, pattern, var);
325
+ emitDistributedActorIdentity_init_assignIdentity (
326
+ *this , borrowedSelfArg, selfVarDecl, ctor, pattern, var);
293
327
}
294
328
if (transportMember && idMember) {
295
329
break ; // we found all properties we care about, break out of the loop early
@@ -305,6 +339,146 @@ void SILGenFunction::emitDistributedActorReady(
305
339
// TODO(distributed): implement actorReady call
306
340
}
307
341
342
+ /* *****************************************************************************/
343
+ /* ****************** DISTRIBUTED ACTOR RESOLVE FUNCTION ***********************/
344
+ /* *****************************************************************************/
345
+
346
+ // / Function body of:
347
+ // / \verbatim
348
+ // / DistributedActor.resolve(
349
+ // / _ identity: Identity,
350
+ // / using transport: ActorTransport
351
+ // / ) throws -> Self where Identity: ActorIdentity
352
+ // / \endverbatim
353
+ void SILGenFunction::emitDistributedActorFactory (FuncDecl *fd) {
354
+ // / NOTE: this will only be reached if the resolve function is actually
355
+ // / demanded. For example, by declaring the actor as `public` or
356
+ // / having at least one call to the resolve function.
357
+
358
+ auto &C = getASTContext ();
359
+ SILLocation loc = fd;
360
+
361
+ fd->dump ();
362
+ F.dump ();
363
+
364
+ // ==== Prepare argument references
365
+ // --- Parameter: identity
366
+ SILArgument *identityArg = F.getArgument (0 );
367
+ assert (identityArg->getType ().getASTType ()->isEqual (C.getAnyActorIdentityType ()));
368
+
369
+ // --- Parameter: transport
370
+ SILArgument *transportArg = F.getArgument (1 ); // existential
371
+ assert (
372
+ transportArg->getType ().getASTType ()->isEqual (C.getActorTransportType ()));
373
+
374
+ // --- Parameter: self
375
+ // ClassDecl *selfDecl = cast<ClassDecl>(fd->getDeclContext()->getAsDecl());
376
+ auto *selfTyDecl = fd->getParent ()->getSelfNominalTypeDecl ();
377
+ assert (selfTyDecl->isDistributedActor ());
378
+ auto selfTy = F.mapTypeIntoContext (selfTyDecl->getDeclaredInterfaceType ()); // TODO: thats just self var devl getType
379
+
380
+ // ManagedValue selfArg = B.createInputFunctionArgument(selfTy, selfDecl);
381
+ VarDecl *selfVarDecl = fd->getImplicitSelfDecl ();
382
+
383
+ SILValue selfArgValue = F.getSelfArgument ();
384
+ ManagedValue selfArg = ManagedValue::forUnmanaged (selfArgValue);
385
+
386
+ // // ==== Prepare all the basic blocks
387
+ // auto returnBB = createBasicBlock();
388
+ // auto errorBB = createBasicBlock();
389
+
390
+ SILFunctionConventions fnConv = F.getConventions (); // TODO: no idea?
391
+
392
+ // TODO(distributed): call the transport 'transport.resolve(identity)'
393
+ // TODO(distributed): switch over result to determine if local or remote
394
+ // NOTES: to make that call on the transport we need to open tne existential
395
+ // but we already do such things in in the initializer synthesis (in this file)
396
+ // so we can steal that code, it's fairly easy once we know how.
397
+ //
398
+ // That call is throwing, so we may need to cover it with some try?
399
+
400
+ // ==== Case 'local') return the resolved instance
401
+ // {
402
+ // auto local = ...
403
+ // // TODO(distributed): 'case .resolved(let instance): return instance'
404
+ // // ==== Return the fully initialized 'remote' instance
405
+ // B.createReturn(loc, local);
406
+ // }
407
+
408
+ // ==== Case 'remote') Create the remote instance
409
+ {
410
+ // ==== Create 'remote' distributed actor instance
411
+ // --- Prepare arguments to remote actor initialization builtin
412
+ // auto borrowedSelfArg = selfArg.borrow(*this, loc);
413
+
414
+ // --- Prepare param: Self.self
415
+ // type: SpecificDistributedActor
416
+ auto returnTy = getLoweredType (
417
+ F.mapTypeIntoContext (selfTyDecl->getDeclaredInterfaceType ()));
418
+ fprintf (stderr, " [%s:%d] (%s) auto returnTy = selfArg.getType().getDeclaredInterfaceType();\n " , __FILE__, __LINE__, __FUNCTION__);
419
+ returnTy.dump ();
420
+
421
+ // type: SpecificDistributedActor.Type
422
+ auto selfMetatype =
423
+ getLoweredType (F.mapTypeIntoContext (selfArg.getType ().getASTType ()));
424
+ SILValue selfMetatypeValue = B.createMetatype (loc, selfMetatype);
425
+
426
+ // --- get the uninitialized allocation from the runtime system.
427
+ FullExpr scope (Cleanups, CleanupLocation (fd));
428
+
429
+ // --- Call: _distributedActorRemoteInitialize(Self.self)
430
+ auto builtinName = C.getIdentifier (
431
+ getBuiltinName (BuiltinValueKind::InitializeDistributedRemoteActor));
432
+ auto *remote = B.createBuiltin (
433
+ loc, builtinName,
434
+ /* returnTy*/ returnTy,
435
+ /* subs*/ {},
436
+ {selfMetatypeValue});
437
+
438
+ // ==== Initialize identity and transport
439
+ // --- Store the identity: self.id = identity
440
+ emitDistributedActorIdentityStore (
441
+ C, *this , /* actorSelf*/ remote, fd, identityArg);
442
+
443
+ // --- Store the transport: self.transport = transport
444
+ // FIXME(distributed): IMPLEMENT:
445
+ // emitDistributedActorTransportStore(
446
+ // *this, borrowedSelfArg, selfVarDecl, fd, transportArg);
447
+
448
+ // ==== Return the fully initialized remote instance
449
+ B.createReturn (loc, remote);
450
+
451
+ // // ==== Branch to return the fully initialized remote instance
452
+ // B.createBranch(loc, returnBB, {remote});
453
+ //
454
+ // // --- Emit return logic
455
+ // // return <remote>
456
+ // {
457
+ // B.emitBlock(returnBB);
458
+ // SILValue result = returnBB->createPhiArgument(
459
+ // returnTy, OwnershipKind::Owned);
460
+ //
461
+ // B.createReturn(loc, result);
462
+ // }
463
+ //
464
+ // // --- Emit rethrow logic
465
+ // // throw error
466
+ // {
467
+ // B.emitBlock(errorBB);
468
+ //
469
+ // SILValue error = errorBB->createPhiArgument(
470
+ // fnConv.getSILErrorType(getTypeExpansionContext()),
471
+ // OwnershipKind::Owned);
472
+ //
473
+ // Cleanups.emitCleanupsForReturn(CleanupLocation(loc), IsForUnwind);
474
+ // B.createThrow(loc, error);
475
+ // }
476
+ }
477
+
478
+ fprintf (stderr, " [%s:%d] (%s) DONE HERE\n " , __FILE__, __LINE__, __FUNCTION__);
479
+ F.dump ();
480
+ }
481
+
308
482
/* *****************************************************************************/
309
483
/* ****************** DISTRIBUTED DEINIT: resignAddress ************************/
310
484
/* *****************************************************************************/
@@ -364,7 +538,7 @@ void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {
364
538
365
539
bindParametersForForwarding (fd->getParameters (), params);
366
540
bindParameterForForwarding (selfVarDecl, params);
367
- auto selfValue = ManagedValue::forUnmanaged (params[params.size () - 1 ]);
541
+ auto selfValue = ManagedValue::forUnmanaged (params[params.size () - 1 ]); // TODO(distributed): getSelfArgument instead
368
542
auto selfType = selfVarDecl->getType ();
369
543
370
544
// if __isRemoteActor(self) {
0 commit comments