[Distributed] move dist. init synthesis to default synthesis

This commit is contained in:
Konrad `ktoso` Malawski
2021-08-07 10:34:56 +09:00
parent 1f33a0cc45
commit 2ef84212c4
6 changed files with 70 additions and 160 deletions

View File

@@ -209,10 +209,14 @@ enum class ImplicitConstructorKind {
/// The default constructor, which default-initializes each
/// of the instance variables.
Default,
/// The default constructor of a distributed actor.
/// Similarly to a Default one it initializes each of the instance variables,
/// however it also implicitly gains an ActorTransport parameter.
DefaultDistributedActor,
/// The memberwise constructor, which initializes each of
/// the instance variables from a parameter of the same type and
/// name.
Memberwise
Memberwise,
};
/// Create an implicit struct or class constructor.
@@ -227,16 +231,6 @@ static ConstructorDecl *createImplicitConstructor(NominalTypeDecl *decl,
ASTContext &ctx) {
assert(!decl->hasClangNode());
// Don't synthesize for distributed actors, they're a bit special.
//
// They have their special inits, and should not get the usual
// default/implicit constructor (i.e. `init()` is illegal for them, as they
// always must have an associated transport - via `init(transport:)` or
// `init(resolve:using:)`).
if (auto clazz = dyn_cast<ClassDecl>(decl))
if (clazz->isDistributedActor())
return nullptr;
SourceLoc Loc = decl->getLoc();
auto accessLevel = AccessLevel::Internal;
@@ -319,6 +313,23 @@ static ConstructorDecl *createImplicitConstructor(NominalTypeDecl *decl,
maybeAddMemberwiseDefaultArg(arg, var, params.size(), ctx);
params.push_back(arg);
}
} else if (ICK == ImplicitConstructorKind::DefaultDistributedActor) {
assert(isa<ClassDecl>(decl));
assert(decl->isDistributedActor() &&
"Only 'distributed actor' type can gain implicit distributed actor init");
if (swift::ensureDistributedModuleLoaded(decl)) {
auto transportDecl = ctx.getActorTransportDecl();
// Create the parameter.
auto *arg = new (ctx) ParamDecl(SourceLoc(), Loc, ctx.Id_transport, Loc,
ctx.Id_transport, transportDecl);
arg->setSpecifier(ParamSpecifier::Default);
arg->setInterfaceType(transportDecl->getDeclaredInterfaceType());
arg->setImplicit();
params.push_back(arg);
}
}
@@ -1193,7 +1204,6 @@ static bool shouldAttemptInitializerSynthesis(const NominalTypeDecl *decl) {
}
void TypeChecker::addImplicitConstructors(NominalTypeDecl *decl) {
fprintf(stderr, "[%s:%d] (%s) ADD IMPLICIT\n", __FILE__, __LINE__, __FUNCTION__);
// If we already added implicit initializers, we're done.
if (decl->addedImplicitInitializers())
return;
@@ -1493,10 +1503,10 @@ SynthesizeDefaultInitRequest::evaluate(Evaluator &evaluator,
decl);
// Create the default constructor.
if (auto ctor = createImplicitConstructor(decl,
ImplicitConstructorKind::Default,
ctx)) {
auto ctorKind = decl->isDistributedActor() ?
ImplicitConstructorKind::DefaultDistributedActor :
ImplicitConstructorKind::Default;
if (auto ctor = createImplicitConstructor(decl, ctorKind, ctx)) {
// Add the constructor.
decl->addMember(ctor);