-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathDerivedConformanceDistributedActor.cpp
927 lines (779 loc) · 35.5 KB
/
DerivedConformanceDistributedActor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
//===--- DerivedConformanceActor.cpp - Derived Actor Conformance ----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements implicit derivation of the Actor protocol.
//
//===----------------------------------------------------------------------===//
#include "CodeSynthesis.h"
#include "DerivedConformances.h"
#include "TypeCheckDistributed.h"
#include "TypeChecker.h"
#include "swift/AST/AvailabilityInference.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/DistributedDecl.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/NameLookupRequests.h"
#include "swift/AST/ParameterList.h"
#include "swift/Basic/Assertions.h"
#include "swift/Strings.h"
using namespace swift;
bool DerivedConformance::canDeriveIdentifiable(
NominalTypeDecl *nominal, DeclContext *dc) {
// we only synthesize for concrete 'distributed actor' decls (which are class)
if (!isa<ClassDecl>(nominal))
return false;
auto &C = nominal->getASTContext();
if (!C.getLoadedModule(C.Id_Distributed))
return false;
return nominal->isDistributedActor();
}
bool DerivedConformance::canDeriveDistributedActor(
NominalTypeDecl *nominal, DeclContext *dc) {
auto &C = nominal->getASTContext();
auto classDecl = dyn_cast<ClassDecl>(nominal);
return C.getLoadedModule(C.Id_Distributed) &&
classDecl && classDecl->isDistributedActor() &&
dc == nominal;
}
bool DerivedConformance::canDeriveDistributedActorSystem(
NominalTypeDecl *nominal, DeclContext *dc) {
auto &C = nominal->getASTContext();
// Make sure ad-hoc requirements that we'll use in synthesis are present, before we try to use them.
// This leads to better error reporting because we already have errors happening (missing witnesses).
if (auto handlerType = getDistributedActorSystemResultHandlerType(nominal)) {
if (!getOnReturnOnDistributedTargetInvocationResultHandler(
handlerType->getAnyNominal()))
return false;
}
return C.getLoadedModule(C.Id_Distributed);
}
/******************************************************************************/
/******************************* RESOLVE FUNCTION *****************************/
/******************************************************************************/
/// Synthesizes the
///
/// \verbatim
/// static resolve(id: ActorID,
/// using system: DistributedActorSystem) throws -> Self {
/// <filled in by SILGenDistributed>
/// }
/// \endverbatim
///
/// factory function in the AST, with an empty body. Its body is
/// expected to be filled-in during SILGen.
static FuncDecl *deriveDistributedActor_resolve(DerivedConformance &derived) {
auto decl = dyn_cast<ClassDecl>(derived.Nominal);
assert(decl->isDistributedActor());
auto &C = decl->getASTContext();
auto idType = getDistributedActorIDType(decl);
auto actorSystemType = getDistributedActorSystemType(decl);
if (!idType || !actorSystemType)
return nullptr;
// (id: Self.ID, using system: Self.ActorSystem)
auto *params = ParameterList::create(
C,
/*LParenLoc=*/SourceLoc(),
/*params=*/{
ParamDecl::createImplicit(
C, C.Id_id, C.Id_id, idType, decl),
ParamDecl::createImplicit(
C, C.Id_using, C.Id_system, actorSystemType, decl)
},
/*RParenLoc=*/SourceLoc()
);
// Func name: resolve(id:using:)
DeclName name(C, C.Id_resolve, params);
// Expected type: (Self) -> (Self.ID, Self.ActorSystem) throws -> (Self)
auto *factoryDecl =
FuncDecl::createImplicit(C, StaticSpellingKind::KeywordStatic,
name, SourceLoc(),
/*async=*/false,
/*throws=*/true,
/*ThrownType=*/Type(),
/*genericParams=*/nullptr,
params,
/*returnType*/decl->getDeclaredInterfaceType(),
decl);
factoryDecl->setDistributedActorFactory(); // TODO(distributed): should we mark this specifically as the resolve factory?
factoryDecl->copyFormalAccessFrom(decl, /*sourceIsParentContext=*/true);
derived.addMembersToConformanceContext({factoryDecl});
return factoryDecl;
}
/******************************************************************************/
/*************** INVOKE HANDLER ON-RETURN FUNCTION ****************************/
/******************************************************************************/
namespace {
struct DoInvokeOnReturnContext {
ParamDecl *handlerParam;
ParamDecl *resultBufferParam;
};
} // namespace
static std::pair<BraceStmt *, bool>
deriveBodyDistributed_doInvokeOnReturn(AbstractFunctionDecl *afd, void *arg) {
auto &C = afd->getASTContext();
auto *context = static_cast<DoInvokeOnReturnContext *>(arg);
// mock locations, we're a thunk and don't really need detailed locations
const SourceLoc sloc = SourceLoc();
const DeclNameLoc dloc = DeclNameLoc();
bool implicit = true;
auto returnTypeParam = afd->getParameters()->get(0);
SmallVector<ASTNode, 8> stmts;
VarDecl *resultVar =
new (C) VarDecl(/*isStatic=*/false, VarDecl::Introducer::Let, sloc,
C.getIdentifier("result"), afd);
{
Expr *resultLoadCall = CallExpr::createImplicit(
C,
UnresolvedDotExpr::createImplicit(
C,
/*base=*/
new (C) DeclRefExpr(ConcreteDeclRef(context->resultBufferParam),
dloc, implicit),
/*baseName=*/DeclBaseName(C.getIdentifier("load")),
/*argLabels=*/
{C.getIdentifier("fromByteOffset"), C.getIdentifier("as")}),
ArgumentList::createImplicit(
C, {Argument(sloc, C.getIdentifier("as"),
new (C) DeclRefExpr(ConcreteDeclRef(returnTypeParam),
dloc, implicit))}));
if (C.LangOpts.hasFeature(Feature::StrictMemorySafety))
resultLoadCall = new (C) UnsafeExpr(sloc, resultLoadCall, Type(), true);
auto resultPattern = NamedPattern::createImplicit(C, resultVar);
auto resultPB = PatternBindingDecl::createImplicit(
C, swift::StaticSpellingKind::None, resultPattern,
/*expr=*/resultLoadCall, afd);
stmts.push_back(resultPB);
stmts.push_back(resultVar);
}
// call the ad-hoc `handler.onReturn`
{
// Find the ad-hoc requirement ensured function on the concrete handler:
auto onReturnFunc = getOnReturnOnDistributedTargetInvocationResultHandler(
context->handlerParam->getInterfaceType()->getAnyNominal());
assert(onReturnFunc && "did not find ad-hoc requirement witness!");
Expr *callExpr = CallExpr::createImplicit(
C,
UnresolvedDotExpr::createImplicit(
C,
/*base=*/
new (C) DeclRefExpr(ConcreteDeclRef(context->handlerParam), dloc,
implicit),
/*baseName=*/onReturnFunc->getBaseName(),
/*paramList=*/onReturnFunc->getParameters()),
ArgumentList::forImplicitCallTo(
DeclNameRef(onReturnFunc->getName()),
{new (C) DeclRefExpr(ConcreteDeclRef(resultVar), dloc, implicit)},
C));
callExpr = TryExpr::createImplicit(C, sloc, callExpr);
callExpr = AwaitExpr::createImplicit(C, sloc, callExpr);
stmts.push_back(callExpr);
}
auto body = BraceStmt::create(C, sloc, {stmts}, sloc, implicit);
return {body, /*isTypeChecked=*/false};
}
// Create local function:
// func invokeOnReturn<R: Self.SerializationRequirement>(
// _ returnType: R.Type
// ) async throws {
// let value = resultBuffer.load(as: returnType)
// try await handler.onReturn(value: value)
// }
static FuncDecl* createLocalFunc_doInvokeOnReturn(
ASTContext& C, FuncDecl* parentFunc,
NominalTypeDecl* systemNominal,
ParamDecl* handlerParam,
ParamDecl* resultBufParam) {
auto DC = parentFunc;
auto doInvokeLocalFuncIdent = C.getIdentifier("doInvokeOnReturn");
// mock locations, we're a synthesized func and don't need real locations
const SourceLoc sloc = SourceLoc();
// <R: Self.SerializationRequirement>
// We create the generic param at invalid depth, which means it'll be filled
// by semantic analysis.
auto *resultGenericParamDecl = GenericTypeParamDecl::createImplicit(
parentFunc, C.getIdentifier("R"), /*depth*/ 0, /*index*/ 0,
GenericTypeParamKind::Type);
GenericParamList *doInvokeGenericParamList =
GenericParamList::create(C, sloc, {resultGenericParamDecl}, sloc);
auto returnTypeIdent = C.getIdentifier("returnType");
auto resultTyParamDecl =
ParamDecl::createImplicit(C,
/*argument=*/returnTypeIdent,
/*parameter=*/returnTypeIdent,
resultGenericParamDecl->getInterfaceType(), DC);
ParameterList *doInvokeParamsList =
ParameterList::create(C, {resultTyParamDecl});
SmallVector<Requirement, 2> requirements;
auto serializationLayout =
getDistributedActorSystemSerializationType(systemNominal)
->getExistentialLayout();
for (auto p : serializationLayout.getProtocols()) {
auto requirement =
Requirement(RequirementKind::Conformance,
resultGenericParamDecl->getDeclaredInterfaceType(),
p->getDeclaredInterfaceType());
requirements.push_back(requirement);
}
GenericSignature doInvokeGenSig =
buildGenericSignature(C, parentFunc->getGenericSignature(),
{resultGenericParamDecl->getDeclaredInterfaceType()
->castTo<GenericTypeParamType>()},
std::move(requirements),
/*allowInverses=*/true);
FuncDecl *doInvokeOnReturnFunc = FuncDecl::createImplicit(
C, swift::StaticSpellingKind::None,
DeclName(C, doInvokeLocalFuncIdent, doInvokeParamsList),
sloc,
/*async=*/true,
/*throws=*/true,
/*ThrownType=*/Type(),
doInvokeGenericParamList, doInvokeParamsList,
/*returnType=*/C.TheEmptyTupleType, parentFunc);
doInvokeOnReturnFunc->setImplicit();
doInvokeOnReturnFunc->setSynthesized();
doInvokeOnReturnFunc->setGenericSignature(doInvokeGenSig);
auto *doInvokeContext = C.Allocate<DoInvokeOnReturnContext>();
doInvokeContext->handlerParam = handlerParam;
doInvokeContext->resultBufferParam = resultBufParam;
doInvokeOnReturnFunc->setBodySynthesizer(
deriveBodyDistributed_doInvokeOnReturn, doInvokeContext);
return doInvokeOnReturnFunc;
}
static std::pair<BraceStmt *, bool>
deriveBodyDistributed_invokeHandlerOnReturn(AbstractFunctionDecl *afd,
void *context) {
auto implicit = true;
ASTContext &C = afd->getASTContext();
auto DC = afd->getDeclContext();
// mock locations, we're a thunk and don't really need detailed locations
const SourceLoc sloc = SourceLoc();
const DeclNameLoc dloc = DeclNameLoc();
NominalTypeDecl *nominal = dyn_cast<NominalTypeDecl>(DC);
assert(nominal);
auto func = dyn_cast<FuncDecl>(afd);
assert(func);
// === parameters
auto params = func->getParameters();
assert(params->size() == 3);
auto handlerParam = params->get(0);
auto resultBufParam = params->get(1);
auto metatypeParam = params->get(2);
auto serializationRequirementTypeTy =
getDistributedActorSystemSerializationType(nominal);
auto serializationRequirementMetaTypeTy =
ExistentialMetatypeType::get(serializationRequirementTypeTy);
// Statements
SmallVector<ASTNode, 8> stmts;
// --- `let m = metatype as! SerializationRequirement.Type`
VarDecl *metatypeVar =
new (C) VarDecl(/*isStatic=*/false, VarDecl::Introducer::Let, sloc,
C.getIdentifier("m"), func);
{
metatypeVar->setImplicit();
metatypeVar->setSynthesized();
// metatype as! <<concrete SerializationRequirement.Type>>
auto metatypeRef =
new (C) DeclRefExpr(ConcreteDeclRef(metatypeParam), dloc, implicit);
auto metatypeSRCastExpr = ForcedCheckedCastExpr::createImplicit(
C, metatypeRef, serializationRequirementMetaTypeTy);
auto metatypePattern = NamedPattern::createImplicit(C, metatypeVar);
auto metatypePB = PatternBindingDecl::createImplicit(
C, swift::StaticSpellingKind::None, metatypePattern,
/*expr=*/metatypeSRCastExpr, func);
stmts.push_back(metatypePB);
stmts.push_back(metatypeVar);
}
// --- Declare the local function `doInvokeOnReturn`...
FuncDecl *doInvokeOnReturnFunc = createLocalFunc_doInvokeOnReturn(
C, func,
nominal, handlerParam, resultBufParam);
stmts.push_back(doInvokeOnReturnFunc);
// --- try await _openExistential(metatypeVar, do: <<doInvokeLocalFunc>>)
{
auto openExistentialBaseIdent = C.getIdentifier("_openExistential");
auto doIdent = C.getIdentifier("do");
auto openExArgs = ArgumentList::createImplicit(
C, {
Argument(sloc, Identifier(),
new (C) DeclRefExpr(ConcreteDeclRef(metatypeVar), dloc,
implicit)),
Argument(sloc, doIdent,
new (C) DeclRefExpr(ConcreteDeclRef(doInvokeOnReturnFunc),
dloc, implicit)),
});
Expr *tryAwaitDoOpenExistential =
CallExpr::createImplicit(C,
UnresolvedDeclRefExpr::createImplicit(
C, openExistentialBaseIdent),
openExArgs);
tryAwaitDoOpenExistential =
AwaitExpr::createImplicit(C, sloc, tryAwaitDoOpenExistential);
tryAwaitDoOpenExistential =
TryExpr::createImplicit(C, sloc, tryAwaitDoOpenExistential);
stmts.push_back(tryAwaitDoOpenExistential);
}
auto body = BraceStmt::create(C, sloc, {stmts}, sloc, implicit);
return {body, /*isTypeChecked=*/false};
}
/// Synthesizes the
///
/// \verbatim
/// static func invokeHandlerOnReturn(
//// handler: ResultHandler,
//// resultBuffer: UnsafeRawPointer,
//// metatype _metatype: Any.Type
//// ) async throws
/// \endverbatim
static FuncDecl *deriveDistributedActorSystem_invokeHandlerOnReturn(
DerivedConformance &derived) {
auto system = derived.Nominal;
auto &C = system->getASTContext();
// auto serializationRequirementType = getDistributedActorSystemType(decl);
auto resultHandlerType = getDistributedActorSystemResultHandlerType(system);
auto unsafeRawPointerType = C.getUnsafeRawPointerType();
auto anyTypeType = ExistentialMetatypeType::get(C.TheAnyType); // Any.Type
// params:
// - handler: Self.ResultHandler
// - resultBuffer:
// - metatype _metatype: Any.Type
auto *params = ParameterList::create(
C,
/*LParenLoc=*/SourceLoc(),
/*params=*/
{
ParamDecl::createImplicit(
C, C.Id_handler, C.Id_handler,
system->mapTypeIntoContext(resultHandlerType), system),
ParamDecl::createImplicit(
C, C.Id_resultBuffer, C.Id_resultBuffer,
unsafeRawPointerType, system),
ParamDecl::createImplicit(
C, C.Id_metatype, C.Id_metatype,
anyTypeType, system)
},
/*RParenLoc=*/SourceLoc());
// Func name: invokeHandlerOnReturn(handler:resultBuffer:metatype)
DeclName name(C, C.Id_invokeHandlerOnReturn, params);
// Expected type: (Self.ResultHandler, UnsafeRawPointer, any Any.Type) async
// throws -> ()
auto *funcDecl =
FuncDecl::createImplicit(C, StaticSpellingKind::None, name, SourceLoc(),
/*async=*/true,
/*throws=*/true,
/*ThrownType=*/Type(),
/*genericParams=*/nullptr, params,
/*returnType*/ TupleType::getEmpty(C), system);
funcDecl->setSynthesized(true);
funcDecl->copyFormalAccessFrom(system, /*sourceIsParentContext=*/true);
funcDecl->setBodySynthesizer(deriveBodyDistributed_invokeHandlerOnReturn);
derived.addMembersToConformanceContext({funcDecl});
return funcDecl;
}
/******************************************************************************/
/******************************* PROPERTIES ***********************************/
/******************************************************************************/
static ValueDecl *deriveDistributedActor_id(DerivedConformance &derived) {
assert(derived.Nominal->isDistributedActor());
auto &C = derived.Context;
// ```
// nonisolated let id: Self.ID // Self.ActorSystem.ActorID
// ```
auto propertyType = getDistributedActorIDType(derived.Nominal);
VarDecl *propDecl;
PatternBindingDecl *pbDecl;
std::tie(propDecl, pbDecl) = derived.declareDerivedProperty(
DerivedConformance::SynthesizedIntroducer::Let, C.Id_id, propertyType,
/*isStatic=*/false, /*isFinal=*/true);
// mark as nonisolated, allowing access to it from everywhere
propDecl->getAttrs().add(
new (C) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
derived.addMemberToConformanceContext(pbDecl, /*insertAtHead=*/true);
derived.addMemberToConformanceContext(propDecl, /*insertAtHead=*/true);
return propDecl;
}
static ValueDecl *deriveDistributedActor_actorSystem(
DerivedConformance &derived) {
auto &C = derived.Context;
auto classDecl = dyn_cast<ClassDecl>(derived.Nominal);
assert(classDecl && derived.Nominal->isDistributedActor());
if (!C.getLoadedModule(C.Id_Distributed))
return nullptr;
// ```
// nonisolated let actorSystem: ActorSystem
// ```
// (no need for @actorIndependent because it is an immutable let)
auto propertyType = getDistributedActorSystemType(classDecl);
VarDecl *propDecl;
PatternBindingDecl *pbDecl;
std::tie(propDecl, pbDecl) = derived.declareDerivedProperty(
DerivedConformance::SynthesizedIntroducer::Let, C.Id_actorSystem,
propertyType, /*isStatic=*/false, /*isFinal=*/true);
// mark as nonisolated, allowing access to it from everywhere
propDecl->getAttrs().add(
new (C) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
// IMPORTANT: `id` MUST be the first field of a distributed actor, and
// `actorSystem` MUST be the second field, because for a remote instance
// we don't allocate memory after those two fields, so their order is very
// important. The `hint` below makes sure the system is inserted right after.
if (auto id = derived.Nominal->getDistributedActorIDProperty()) {
derived.addMemberToConformanceContext(propDecl, /*hint=*/id);
derived.addMemberToConformanceContext(pbDecl, /*hint=*/id);
} else {
// `id` will be synthesized next, and will insert at head,
// so in order for system to be SECOND (as it must be),
// we'll insert at head right now and as id gets synthesized we'll get
// the correct order: id, actorSystem.
derived.addMemberToConformanceContext(propDecl, /*insertAtHead=*/true);
derived.addMemberToConformanceContext(pbDecl, /*insertAtHead==*/true);
}
return propDecl;
}
/******************************************************************************/
/***************************** ASSOC TYPES ************************************/
/******************************************************************************/
static Type
deriveDistributedActorType_ActorSystem(
DerivedConformance &derived) {
assert(derived.Nominal->isDistributedActor());
auto &C = derived.Context;
// Look for a type DefaultDistributedActorSystem within the parent context.
auto defaultDistributedActorSystemLookup = TypeChecker::lookupUnqualified(
derived.getConformanceContext()->getModuleScopeContext(),
DeclNameRef(C.Id_DefaultDistributedActorSystem),
derived.ConformanceDecl->getLoc());
TypeDecl *defaultDistributedActorSystemTypeDecl = nullptr;
for (const auto &found : defaultDistributedActorSystemLookup) {
if (auto foundType = dyn_cast_or_null<TypeDecl>(found.getValueDecl())) {
if (defaultDistributedActorSystemTypeDecl) {
// Note: ambiguity, for now just fail.
return nullptr;
}
defaultDistributedActorSystemTypeDecl = foundType;
continue;
}
}
// There is no default, so fail to synthesize.
if (!defaultDistributedActorSystemTypeDecl)
return nullptr;
// Return the default system type.
return defaultDistributedActorSystemTypeDecl->getDeclaredInterfaceType();
}
static Type
deriveDistributedActorType_ID(
DerivedConformance &derived) {
if (!derived.Nominal->isDistributedActor())
return nullptr;
// Look for a type DefaultDistributedActorSystem within the parent context.
auto systemTy = getDistributedActorSystemType(derived.Nominal);
// There is no known actor system type, so fail to synthesize.
if (!systemTy || systemTy->hasError())
return nullptr;
if (auto systemNominal = systemTy->getAnyNominal()) {
return getDistributedActorSystemActorIDType(systemNominal);
}
return nullptr;
}
static Type
deriveDistributedActorType_SerializationRequirement(
DerivedConformance &derived) {
if (!derived.Nominal->isDistributedActor())
return nullptr;
// Look for a type DefaultDistributedActorSystem within the parent context.
auto systemTy = getDistributedActorSystemType(derived.Nominal);
// There is no known actor system type, so fail to synthesize.
if (!systemTy || systemTy->hasError())
return nullptr;
auto DAS = derived.Context.getDistributedActorSystemDecl();
if (!DAS)
return nullptr;
if (auto systemNominal = systemTy->getAnyNominal())
return getDistributedActorSystemSerializationType(systemNominal);
return nullptr;
}
/******************************************************************************/
/// Turn a Builtin.Executor value into an UnownedSerialExecutor.
static Expr *constructDistributedUnownedSerialExecutor(ASTContext &ctx,
Expr *arg) {
auto executorDecl = ctx.getUnownedSerialExecutorDecl();
if (!executorDecl) return nullptr;
for (auto member: executorDecl->getAllMembers()) {
auto ctor = dyn_cast<ConstructorDecl>(member);
if (!ctor) continue;
auto params = ctor->getParameters();
if (params->size() != 1 ||
!params->get(0)->getInterfaceType()->is<BuiltinExecutorType>())
continue;
Type executorType = executorDecl->getDeclaredInterfaceType();
Type ctorType = ctor->getInterfaceType();
// We have the right initializer. Build a reference to it of type:
// (UnownedSerialExecutor.Type)
// -> (Builtin.Executor) -> UnownedSerialExecutor
auto initRef = new (ctx) DeclRefExpr(ctor, DeclNameLoc(), /*implicit*/true,
AccessSemantics::Ordinary,
ctorType);
// Apply the initializer to the metatype, building an expression of type:
// (Builtin.Executor) -> UnownedSerialExecutor
auto metatypeRef = TypeExpr::createImplicit(executorType, ctx);
Type ctorAppliedType = ctorType->getAs<FunctionType>()->getResult();
auto selfApply = ConstructorRefCallExpr::create(ctx, initRef, metatypeRef,
ctorAppliedType);
selfApply->setImplicit(true);
selfApply->setThrows(nullptr);
// Call the constructor, building an expression of type
// UnownedSerialExecutor.
auto *argList = ArgumentList::forImplicitUnlabeled(ctx, {arg});
auto call = CallExpr::createImplicit(ctx, selfApply, argList);
call->setType(executorType);
call->setThrows(nullptr);
return call;
}
return nullptr;
}
static std::pair<BraceStmt *, bool>
deriveBodyDistributedActor_unownedExecutor(AbstractFunctionDecl *getter, void *) {
// var unownedExecutor: UnownedSerialExecutor {
// get {
// guard __isLocalActor(self) else {
// return buildDefaultDistributedRemoteActorExecutor(self)
// }
// return Builtin.buildDefaultActorExecutorRef(self)
// }
// }
ASTContext &ctx = getter->getASTContext();
// Produce an empty brace statement on failure.
auto failure = [&]() -> std::pair<BraceStmt *, bool> {
auto body = BraceStmt::create(
ctx, SourceLoc(), { }, SourceLoc(), /*implicit=*/true);
return { body, /*isTypeChecked=*/true };
};
// Build a reference to self.
Type selfType = getter->getImplicitSelfDecl()->getTypeInContext();
Expr *selfArg = DerivedConformance::createSelfDeclRef(getter);
selfArg->setType(selfType);
// Prepare the builtin call, we'll use it after the guard, but want to take the type
// of its return type earlier, so we prepare it here.
// The builtin call gives us a Builtin.Executor.
auto builtinCall =
DerivedConformance::createBuiltinCall(ctx,
BuiltinValueKind::BuildDefaultActorExecutorRef,
{selfType}, {selfArg});
// Turn that into an UnownedSerialExecutor.
auto initCall = constructDistributedUnownedSerialExecutor(ctx, builtinCall);
if (!initCall) return failure();
// guard __isLocalActor(self) else {
// return buildDefaultDistributedRemoteActorExecutor(self)
// }
auto isLocalActorDecl = ctx.getIsLocalDistributedActor();
DeclRefExpr *isLocalActorExpr =
new (ctx) DeclRefExpr(ConcreteDeclRef(isLocalActorDecl), DeclNameLoc(), /*implicit=*/true,
AccessSemantics::Ordinary,
FunctionType::get({AnyFunctionType::Param(ctx.getAnyObjectType())},
ctx.getBoolType()));
Expr *selfForIsLocalArg = DerivedConformance::createSelfDeclRef(getter);
selfForIsLocalArg->setType(selfType);
auto conformances = collectExistentialConformances(selfType->getCanonicalType(),
ctx.getAnyObjectType());
auto *argListForIsLocal =
ArgumentList::forImplicitSingle(ctx, Identifier(),
ErasureExpr::create(ctx, selfForIsLocalArg,
ctx.getAnyObjectType(),
conformances, {}));
CallExpr *isLocalActorCall = CallExpr::createImplicit(ctx, isLocalActorExpr, argListForIsLocal);
isLocalActorCall->setType(ctx.getBoolType());
isLocalActorCall->setThrows(nullptr);
GuardStmt* guardElseRemoteReturnExec;
{
// Find the buildDefaultDistributedRemoteActorExecutor method
auto buildRemoteExecutorDecl =
ctx.getBuildDefaultDistributedRemoteActorUnownedExecutor();
assert(buildRemoteExecutorDecl && "cannot find buildDefaultDistributedRemoteActorExecutor");
auto substitutions = SubstitutionMap::get(
buildRemoteExecutorDecl->getGenericSignature(),
[&](SubstitutableType *dependentType) {
if (auto gp = dyn_cast<GenericTypeParamType>(dependentType)) {
if (gp->getDepth() == 0 && gp->getIndex() == 0) {
return getter->getImplicitSelfDecl()->getTypeInContext();
}
}
return Type();
},
LookUpConformanceInModule()
);
DeclRefExpr *buildRemoteExecutorExpr =
new (ctx) DeclRefExpr(
ConcreteDeclRef(buildRemoteExecutorDecl, substitutions),
DeclNameLoc(),/*implicit=*/true,
AccessSemantics::Ordinary);
buildRemoteExecutorExpr->setType(
buildRemoteExecutorDecl->getInterfaceType()
.subst(substitutions)
);
Expr *selfForBuildRemoteExecutor = DerivedConformance::createSelfDeclRef(getter);
selfForBuildRemoteExecutor->setType(selfType);
auto *argListForBuildRemoteExecutor =
ArgumentList::forImplicitCallTo(buildRemoteExecutorDecl->getParameters(),
/*argExprs=*/{selfForBuildRemoteExecutor}, ctx);
CallExpr *buildRemoteExecutorCall = CallExpr::createImplicit(ctx, buildRemoteExecutorExpr,
argListForBuildRemoteExecutor);
buildRemoteExecutorCall->setType(ctx.getUnownedSerialExecutorType());
buildRemoteExecutorCall->setThrows(nullptr);
SmallVector<ASTNode, 1> statements = {
ReturnStmt::createImplicit(ctx, buildRemoteExecutorCall)
};
SmallVector<StmtConditionElement, 1> conditions = {
isLocalActorCall
};
// Build and return the complete guard statement.
guardElseRemoteReturnExec =
new(ctx) GuardStmt(SourceLoc(), ctx.AllocateCopy(conditions),
BraceStmt::create(ctx, SourceLoc(), statements, SourceLoc()));
}
// Finalize preparing the unowned executor for returning.
// auto wrappedCall = new (ctx) InjectIntoOptionalExpr(initCall, initCall->getType());
auto *returnDefaultExec = ReturnStmt::createImplicit(ctx, initCall);
auto body = BraceStmt::create(
ctx, SourceLoc(), { guardElseRemoteReturnExec, returnDefaultExec }, SourceLoc(), /*implicit=*/true);
return { body, /*isTypeChecked=*/true };
}
/// Derive the declaration of DistributedActor's unownedExecutor property.
static ValueDecl *deriveDistributedActor_unownedExecutor(DerivedConformance &derived) {
ASTContext &ctx = derived.Context;
// Retrieve the types and declarations we'll need to form this operation.
auto executorDecl = ctx.getUnownedSerialExecutorDecl();
if (!executorDecl) {
derived.Nominal->diagnose(
diag::concurrency_lib_missing, "UnownedSerialExecutor");
return nullptr;
}
Type executorType = executorDecl->getDeclaredInterfaceType();
if (auto classDecl = dyn_cast<ClassDecl>(derived.Nominal)) {
if (auto existing = classDecl->getUnownedExecutorProperty()) {
if (existing->getInterfaceType()->isEqual(executorType)) {
return const_cast<VarDecl *>(existing);
} else {
// bad type, should be diagnosed elsewhere
return nullptr;
}
}
}
auto propertyPair = derived.declareDerivedProperty(
DerivedConformance::SynthesizedIntroducer::Var, ctx.Id_unownedExecutor,
executorType, /*static*/ false, /*final*/ false);
auto property = propertyPair.first;
property->setSynthesized(true);
property->getAttrs().add(new (ctx) SemanticsAttr(SEMANTICS_DEFAULT_ACTOR,
SourceLoc(), SourceRange(),
/*implicit*/ true));
property->getAttrs().add(
new (ctx) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
// Make the property implicitly final.
property->getAttrs().add(new (ctx) FinalAttr(/*IsImplicit=*/true));
if (property->getFormalAccess() == AccessLevel::Open)
property->overwriteAccess(AccessLevel::Public);
// Infer availability.
SmallVector<const Decl *, 2> asAvailableAs;
asAvailableAs.push_back(executorDecl);
if (auto enclosingDecl = property->getInnermostDeclWithAvailability())
asAvailableAs.push_back(enclosingDecl);
AvailabilityInference::applyInferredAvailableAttrs(property, asAvailableAs);
auto getter = derived.addGetterToReadOnlyDerivedProperty(property);
getter->setBodySynthesizer(deriveBodyDistributedActor_unownedExecutor);
// IMPORTANT: MUST BE AFTER [id, actorSystem].
if (auto id = derived.Nominal->getDistributedActorIDProperty()) {
if (auto system = derived.Nominal->getDistributedActorSystemProperty()) {
// good, we must be after the system; this is the final order
derived.addMemberToConformanceContext(propertyPair.second, /*hint=*/system);
derived.addMemberToConformanceContext(property, /*hint=*/system);
} else {
// system was not yet synthesized, it'll insert after id and we'll be okey
derived.addMemberToConformanceContext(propertyPair.second, /*hint=*/id);
derived.addMemberToConformanceContext(property, /*hint=*/id);
}
} else {
// nor id or system synthesized yet, id will insert first and system will be after it
derived.addMemberToConformanceContext(propertyPair.second, /*insertAtHead==*/true);
derived.addMemberToConformanceContext(property, /*insertAtHead==*/true);
}
return property;
}
/******************************************************************************/
/**************************** ENTRY POINTS ************************************/
/******************************************************************************/
// !!!!!!!!!!!!! IMPORTANT WHEN MAKING CHANGES TO REQUIREMENTS !!!!!!!!!!!!!!!!!
// !! Remember to update DerivedConformance::getDerivableRequirement !!
// !! any time the signatures or list of derived requirements change. !!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ValueDecl *DerivedConformance::deriveDistributedActor(ValueDecl *requirement) {
if (auto var = dyn_cast<VarDecl>(requirement)) {
ValueDecl *derivedValue = nullptr;
if (var->getName() == Context.Id_id) {
derivedValue = deriveDistributedActor_id(*this);
} else if (var->getName() == Context.Id_actorSystem) {
derivedValue = deriveDistributedActor_actorSystem(*this);
} else if (var->getName() == Context.Id_unownedExecutor) {
derivedValue = deriveDistributedActor_unownedExecutor(*this);
}
if (derivedValue) {
assertRequiredSynthesizedPropertyOrder(Context, Nominal);
}
return derivedValue;
}
if (auto func = dyn_cast<FuncDecl>(requirement)) {
// just a simple name check is enough here,
// if we are invoked here we know for sure it is for the "right" function
if (func->getName().getBaseName() == Context.Id_resolve) {
return deriveDistributedActor_resolve(*this);
}
}
return nullptr;
}
std::pair<Type, TypeDecl *> DerivedConformance::deriveDistributedActor(
AssociatedTypeDecl *assocType) {
if (!canDeriveDistributedActor(Nominal, cast<DeclContext>(ConformanceDecl)))
return std::make_pair(Type(), nullptr);
if (assocType->getName() == Context.Id_ActorSystem) {
return std::make_pair(deriveDistributedActorType_ActorSystem(*this),
nullptr);
}
if (assocType->getName() == Context.Id_SerializationRequirement) {
return std::make_pair(
deriveDistributedActorType_SerializationRequirement(*this), nullptr);
}
if (assocType->getName() == Context.Id_ID) {
return std::make_pair(deriveDistributedActorType_ID(*this), nullptr);
}
Context.Diags.diagnose(assocType->getLoc(),
diag::broken_distributed_actor_requirement);
return std::make_pair(Type(), nullptr);
}
ValueDecl *
DerivedConformance::deriveDistributedActorSystem(ValueDecl *requirement) {
if (auto func = dyn_cast<FuncDecl>(requirement)) {
// just a simple name check is enough here,
// if we are invoked here we know for sure it is for the "right" function
if (func->getName().getBaseName() == Context.Id_invokeHandlerOnReturn) {
return deriveDistributedActorSystem_invokeHandlerOnReturn(*this);
}
}
return nullptr;
}
/******************************************************************************/
/*************************** ERRORS & DIAGNOSTICS *****************************/
/******************************************************************************/
void DerivedConformance::tryDiagnoseFailedDistributedActorDerivation(
DeclContext *DC, NominalTypeDecl *nominal) {
// TODO(distributed): offer better diagnosis for error scenarios here
}
void DerivedConformance::tryDiagnoseFailedDistributedActorSystemDerivation(
DeclContext *DC, NominalTypeDecl *nominal) {
// TODO(distributed): offer better diagnosis for error scenarios here
}