@@ -128,9 +128,6 @@ static void emitDistributedActorStore_transport(
128
128
SILArgument *transportArg) {
129
129
auto &B = SGF.B ;
130
130
131
- auto &SGM = SGF.SGM ;
132
- SILGenFunctionBuilder builder (SGM);
133
-
134
131
auto *dc = func->getDeclContext ();
135
132
auto classDecl = dc->getSelfClassDecl ();
136
133
@@ -148,42 +145,36 @@ static void emitDistributedActorStore_transport(
148
145
loc, actorSelf, var,
149
146
SGF.getLoweredType (var->getInterfaceType ()));
150
147
151
- // ==== Store the transport
152
- B.createCopyAddr (loc,
153
- /* src*/ transportArg,
154
- /* dest*/ fieldAddr,
155
- IsNotTake, IsInitialization);
156
- }
157
-
158
- // TODO(distributed): remove this store impl and reuse Store_transport
159
- static void
160
- emitDistributedActor_init_transportStore (
161
- SILGenFunction &SGF,
162
- ManagedValue borrowedSelfArg, VarDecl *selfDecl,
163
- ConstructorDecl *ctor,
164
- Pattern *pattern, VarDecl *var) {
165
- auto &C = selfDecl->getASTContext ();
166
- auto &B = SGF.B ;
167
- auto &F = SGF.F ;
168
- auto &SGM = SGF.SGM ;
169
- SILGenFunctionBuilder builder (SGM);
170
-
171
- auto loc = SILLocation (ctor);
172
- loc.markAutoGenerated ();
173
-
174
- // ==== Prepare assignment: get the self.transport address
175
- SILValue transportArgValue = getActorTransportArgument (C, F, ctor);
176
-
177
- // ----
178
-
179
- auto transportFieldAddr = B.createRefElementAddr (
180
- loc, borrowedSelfArg.getValue (), var,
181
- SGF.getLoweredType (var->getInterfaceType ()));
148
+ // If the argument is not existential, it will be a concrete type
149
+ // that can be erased to that existential.
150
+ SILValue transportValue = transportArg;
151
+ if (!transportArg->getType ().isExistentialType ()) {
152
+ auto &existentialTL = SGF.getTypeLowering (var->getInterfaceType ());
153
+ auto concreteFormalType = transportArg->getType ().getASTType ();
154
+
155
+ auto archetype = OpenedArchetypeType::getAny (var->getInterfaceType ());
156
+ AbstractionPattern abstractionPattern (archetype);
157
+ auto &concreteTL = SGF.getTypeLowering (abstractionPattern,
158
+ concreteFormalType);
159
+
160
+ auto module = dc->getParentModule ();
161
+ auto actorTransportProto = C.getProtocol (KnownProtocolKind::ActorTransport);
162
+ ProtocolConformanceRef conformances[] = {
163
+ module ->lookupConformance (concreteFormalType, actorTransportProto) };
164
+ ManagedValue mv = SGF.emitExistentialErasure (loc, concreteFormalType,
165
+ concreteTL, existentialTL,
166
+ C.AllocateCopy (conformances),
167
+ SGFContext (),
168
+ [&](SGFContext C) -> ManagedValue {
169
+ return ManagedValue::forBorrowedRValue (transportArg);
170
+ });
171
+ transportValue = mv.getValue ();
172
+ }
182
173
183
174
// ==== Store the transport
184
175
B.createCopyAddr (loc,
185
- /* src*/ transportArgValue ,
186
- /* dest*/ transportFieldAddr ,
176
+ /* src*/ transportValue ,
177
+ /* dest*/ fieldAddr ,
187
178
IsNotTake, IsInitialization);
188
179
}
189
180
@@ -254,14 +245,17 @@ static void emitDistributedActorStore_init_assignIdentity(
254
245
assert (distributedActorProto);
255
246
assert (transportProto);
256
247
257
- // --- Open the transport existential
258
- OpenedArchetypeType *Opened ;
248
+ // --- Open the transport existential, if needed.
249
+ SILValue transportValue = transportArgValue ;
259
250
auto transportASTType = transportArgValue->getType ().getASTType ();
260
- auto openedTransportType =
261
- transportASTType->openAnyExistentialType (Opened)->getCanonicalType ();
262
- auto openedTransportSILType = F.getLoweredType (openedTransportType);
263
- auto transportArchetypeValue = B.createOpenExistentialAddr (
264
- loc, transportArgValue, openedTransportSILType, OpenedExistentialAccess::Immutable);
251
+ if (transportASTType->isAnyExistentialType ()) {
252
+ OpenedArchetypeType *Opened;
253
+ transportASTType =
254
+ transportASTType->openAnyExistentialType (Opened)->getCanonicalType ();
255
+ transportValue = B.createOpenExistentialAddr (
256
+ loc, transportValue, F.getLoweredType (transportASTType),
257
+ OpenedExistentialAccess::Immutable);
258
+ }
265
259
266
260
// --- prepare `Self.self` metatype
267
261
auto *selfTyDecl = ctor->getParent ()->getSelfNominalTypeDecl ();
@@ -298,7 +292,7 @@ static void emitDistributedActorStore_init_assignIdentity(
298
292
299
293
auto assignWitnessMethod = B.createWitnessMethod (
300
294
loc,
301
- /* lookupTy*/ openedTransportType ,
295
+ /* lookupTy*/ transportASTType ,
302
296
/* Conformance*/ transportConfRef,
303
297
/* member*/ assignIdentityRef,
304
298
/* methodTy*/ assignIdentitySILTy);
@@ -308,7 +302,7 @@ static void emitDistributedActorStore_init_assignIdentity(
308
302
309
303
SubstitutionMap subs =
310
304
SubstitutionMap::get (genericSig,
311
- {openedTransportType , selfTy},
305
+ {transportASTType , selfTy},
312
306
{transportConfRef, distributedActorConfRef});
313
307
314
308
// --- create a temporary storage for the result of the call
@@ -319,7 +313,7 @@ static void emitDistributedActorStore_init_assignIdentity(
319
313
// ---- actually call transport.assignIdentity(Self.self)
320
314
B.createApply (
321
315
loc, assignWitnessMethod, subs,
322
- { temp, selfMetatypeValue, transportArchetypeValue });
316
+ { temp, selfMetatypeValue, transportValue });
323
317
324
318
// ==== Assign the identity to stored property
325
319
// TODO(distributed): reuse emitDistributedActorStore_id here, pass the SILValue
@@ -374,9 +368,9 @@ void SILGenFunction::initializeDistributedActorImplicitStorageInit(
374
368
if (var->getName () == C.Id_actorTransport &&
375
369
var->getInterfaceType ()->isEqual (transportTy)) {
376
370
transportMember = var;
377
- // TODO(distributed): reuse emitDistributedActorStore_transport
378
- emitDistributedActor_init_transportStore (
379
- * this , borrowedSelfArg, selfVarDecl, ctor, pattern, var );
371
+ emitDistributedActorStore_transport (
372
+ C, * this , borrowedSelfArg. getValue (), ctor,
373
+ getActorTransportArgument (C, F, ctor) );
380
374
} else if (var->getName () == C.Id_id &&
381
375
(var->getInterfaceType ()->isEqual (identityProtoTy) ||
382
376
var->getInterfaceType ()->isEqual (anyIdentityTy))) { // TODO(distributed): stick one way to store, but today we can't yet store the existential
@@ -417,13 +411,16 @@ void SILGenFunction::emitDistributedActorReady(
417
411
assert (transportProto);
418
412
419
413
// --- Open the transport existential
420
- OpenedArchetypeType *Opened;
421
- auto transportASTType = transportArgValue->getType ().getASTType ();
422
- auto openedTransportType =
423
- transportASTType->openAnyExistentialType (Opened)->getCanonicalType ();
424
- auto openedTransportSILType = F.getLoweredType (openedTransportType);
425
- auto transportArchetypeValue = B.createOpenExistentialAddr (
426
- loc, transportArgValue, openedTransportSILType, OpenedExistentialAccess::Immutable);
414
+ SILValue transportValue = transportArgValue;
415
+ auto transportASTType = transportValue->getType ().getASTType ();
416
+ if (transportASTType->isAnyExistentialType ()) {
417
+ OpenedArchetypeType *Opened;
418
+ transportASTType =
419
+ transportASTType->openAnyExistentialType (Opened)->getCanonicalType ();
420
+ transportValue = B.createOpenExistentialAddr (
421
+ loc, transportValue, F.getLoweredType (transportASTType),
422
+ OpenedExistentialAccess::Immutable);
423
+ }
427
424
428
425
// === Make the transport.actorReady call
429
426
// --- prepare the witness_method
@@ -453,7 +450,7 @@ void SILGenFunction::emitDistributedActorReady(
453
450
454
451
auto readyWitnessMethod = B.createWitnessMethod (
455
452
loc,
456
- /* lookupTy*/ openedTransportType ,
453
+ /* lookupTy*/ transportASTType ,
457
454
/* Conformance*/ transportConfRef,
458
455
/* member*/ actorReadyRef,
459
456
/* methodTy*/ actorReadySILTy);
@@ -463,13 +460,13 @@ void SILGenFunction::emitDistributedActorReady(
463
460
464
461
SubstitutionMap subs =
465
462
SubstitutionMap::get (genericSig,
466
- {openedTransportType , selfTy},
463
+ {transportASTType , selfTy},
467
464
{transportConfRef, distributedActorConfRef});
468
465
469
466
// ---- actually call transport.actorReady(self)
470
467
B.createApply (
471
468
loc, readyWitnessMethod, subs,
472
- { selfArgValue, transportArchetypeValue });
469
+ { selfArgValue, transportValue });
473
470
}
474
471
475
472
/* *****************************************************************************/
0 commit comments