Skip to content

Commit 89629fe

Browse files
kavonktoso
authored andcommitted
Generalize BodyKind::MemberwiseInitializer
We'd like to support factor initializers for distributed actor types that are synthesized by SILGen. We already do something similar for memberwise initializers for structs. Thus, this patch generalizes that concept into a new BodyKind for AbstractFunctionDecls called BodyKind::SILSynthesize. In addition, to help differentiate the kinds of AFDs that are SILSynthesized into different families for SILGen to recognize, we also have a new enum SILSynthesizeKind to indicate whether it is a memberwise init, etc.
1 parent c9bfccb commit 89629fe

File tree

8 files changed

+49
-21
lines changed

8 files changed

+49
-21
lines changed

include/swift/AST/Decl.h

+36-5
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,13 @@ class alignas(1 << DeclAlignInBits) Decl {
403403
SWIFT_INLINE_BITFIELD(SubscriptDecl, VarDecl, 2,
404404
StaticSpelling : 2
405405
);
406-
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+8+1+1+1+1+1+1,
406+
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+2+8+1+1+1+1+1+1,
407407
/// \see AbstractFunctionDecl::BodyKind
408408
BodyKind : 3,
409409

410+
/// \see AbstractFunctionDecl::SILSynthesizeKind
411+
SILSynthesizeKind : 2,
412+
410413
/// Import as member status.
411414
IAMStatus : 8,
412415

@@ -5773,6 +5776,15 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
57735776
friend class NeedsNewVTableEntryRequest;
57745777

57755778
public:
5779+
/// records the kind of SILGen-synthesized body this decl represents
5780+
enum class SILSynthesizeKind {
5781+
None,
5782+
MemberwiseInitializer,
5783+
DistributedActorFactory
5784+
5785+
// This enum currently needs to fit in a 2-bit bitfield.
5786+
};
5787+
57765788
enum class BodyKind {
57775789
/// The function did not have a body in the source code file.
57785790
None,
@@ -5792,8 +5804,8 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
57925804
/// Function body is present and type-checked.
57935805
TypeChecked,
57945806

5795-
/// This is a memberwise initializer that will be synthesized by SILGen.
5796-
MemberwiseInitializer,
5807+
// Function body will be synthesized by SILGen.
5808+
SILSynthesize,
57975809

57985810
/// Function body text was deserialized from a .swiftmodule.
57995811
Deserialized
@@ -5893,6 +5905,14 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
58935905
Bits.AbstractFunctionDecl.BodyKind = unsigned(K);
58945906
}
58955907

5908+
void setSILSynthesizeKind(SILSynthesizeKind K) {
5909+
Bits.AbstractFunctionDecl.SILSynthesizeKind = unsigned(K);
5910+
}
5911+
5912+
SILSynthesizeKind getSILSynthesizeKind() const {
5913+
return SILSynthesizeKind(Bits.AbstractFunctionDecl.SILSynthesizeKind);
5914+
}
5915+
58965916
public:
58975917
void setHasSingleExpressionBody(bool Has = true) {
58985918
Bits.AbstractFunctionDecl.HasSingleExpressionBody = Has;
@@ -6069,7 +6089,17 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
60696089
void setIsMemberwiseInitializer() {
60706090
assert(getBodyKind() == BodyKind::None);
60716091
assert(isa<ConstructorDecl>(this));
6072-
setBodyKind(BodyKind::MemberwiseInitializer);
6092+
setBodyKind(BodyKind::SILSynthesize);
6093+
setSILSynthesizeKind(SILSynthesizeKind::MemberwiseInitializer);
6094+
}
6095+
6096+
/// Mark that the body should be filled in to be a factory method for creating
6097+
/// a distributed actor.
6098+
void setDistributedActorFactory() {
6099+
assert(getBodyKind() == BodyKind::None);
6100+
assert(isa<FuncDecl>(this));
6101+
setBodyKind(BodyKind::SILSynthesize);
6102+
setSILSynthesizeKind(SILSynthesizeKind::DistributedActorFactory);
60736103
}
60746104

60756105
/// Gets the body of this function, stripping the unused portions of #if
@@ -6093,7 +6123,8 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
60936123
}
60946124

60956125
bool isMemberwiseInitializer() const {
6096-
return getBodyKind() == BodyKind::MemberwiseInitializer;
6126+
return getBodyKind() == BodyKind::SILSynthesize
6127+
&& getSILSynthesizeKind() == SILSynthesizeKind::MemberwiseInitializer;
60976128
}
60986129

60996130
/// For a method of a class, checks whether it will require a new entry in the

lib/AST/ASTVerifier.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ class Verifier : public ASTWalker {
475475
case AbstractFunctionDecl::BodyKind::None:
476476
case AbstractFunctionDecl::BodyKind::TypeChecked:
477477
case AbstractFunctionDecl::BodyKind::Skipped:
478-
case AbstractFunctionDecl::BodyKind::MemberwiseInitializer:
478+
case AbstractFunctionDecl::BodyKind::SILSynthesize:
479479
case AbstractFunctionDecl::BodyKind::Deserialized:
480480
return true;
481481

lib/AST/Decl.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -7310,7 +7310,7 @@ void AbstractFunctionDecl::setBodyToBeReparsed(SourceRange bodyRange) {
73107310
SourceRange AbstractFunctionDecl::getBodySourceRange() const {
73117311
switch (getBodyKind()) {
73127312
case BodyKind::None:
7313-
case BodyKind::MemberwiseInitializer:
7313+
case BodyKind::SILSynthesize:
73147314
case BodyKind::Deserialized:
73157315
case BodyKind::Synthesize:
73167316
return SourceRange();
@@ -7625,7 +7625,7 @@ bool AbstractFunctionDecl::hasInlinableBodyText() const {
76257625
case BodyKind::None:
76267626
case BodyKind::Synthesize:
76277627
case BodyKind::Skipped:
7628-
case BodyKind::MemberwiseInitializer:
7628+
case BodyKind::SILSynthesize:
76297629
return false;
76307630
}
76317631
llvm_unreachable("covered switch");
@@ -8686,7 +8686,7 @@ ParseAbstractFunctionBodyRequest::getCachedResult() const {
86868686
auto afd = std::get<0>(getStorage());
86878687
switch (afd->getBodyKind()) {
86888688
case BodyKind::Deserialized:
8689-
case BodyKind::MemberwiseInitializer:
8689+
case BodyKind::SILSynthesize:
86908690
case BodyKind::None:
86918691
case BodyKind::Skipped:
86928692
return nullptr;
@@ -8707,7 +8707,7 @@ void ParseAbstractFunctionBodyRequest::cacheResult(BraceStmt *value) const {
87078707
auto afd = std::get<0>(getStorage());
87088708
switch (afd->getBodyKind()) {
87098709
case BodyKind::Deserialized:
8710-
case BodyKind::MemberwiseInitializer:
8710+
case BodyKind::SILSynthesize:
87118711
case BodyKind::None:
87128712
case BodyKind::Skipped:
87138713
// The body is always empty, so don't cache anything.

lib/AST/TypeCheckRequests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ Optional<BraceStmt *> TypeCheckFunctionBodyRequest::getCachedResult() const {
13211321
auto *afd = std::get<0>(getStorage());
13221322
switch (afd->getBodyKind()) {
13231323
case BodyKind::Deserialized:
1324-
case BodyKind::MemberwiseInitializer:
1324+
case BodyKind::SILSynthesize:
13251325
case BodyKind::None:
13261326
case BodyKind::Skipped:
13271327
// These cases don't have any body available.

lib/Index/Index.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static bool printDisplayName(const swift::ValueDecl *D, llvm::raw_ostream &OS) {
7979

8080
static bool isMemberwiseInit(swift::ValueDecl *D) {
8181
if (auto AFD = dyn_cast<AbstractFunctionDecl>(D))
82-
return AFD->getBodyKind() == AbstractFunctionDecl::BodyKind::MemberwiseInitializer;
82+
return AFD->isMemberwiseInitializer();
8383
return false;
8484
}
8585

lib/Parse/ParseRequests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ BraceStmt *ParseAbstractFunctionBodyRequest::evaluate(
9393

9494
switch (afd->getBodyKind()) {
9595
case BodyKind::Deserialized:
96-
case BodyKind::MemberwiseInitializer:
96+
case BodyKind::SILSynthesize:
9797
case BodyKind::None:
9898
case BodyKind::Skipped:
9999
return nullptr;

lib/Sema/TypeCheckProtocol.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -4097,14 +4097,11 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
40974097
witness->getName(), isSetter, requiredAccess,
40984098
protoAccessScope.accessLevelForDiagnostics(),
40994099
proto->getName());
4100-
if (auto *decl = dyn_cast<AbstractFunctionDecl>(witness)) {
4101-
auto isMemberwiseInitializer =
4102-
decl->getBodyKind() ==
4103-
AbstractFunctionDecl::BodyKind::MemberwiseInitializer;
4104-
if (isMemberwiseInitializer) {
4100+
4101+
if (auto *decl = dyn_cast<AbstractFunctionDecl>(witness))
4102+
if (decl->isMemberwiseInitializer())
41054103
return;
4106-
}
4107-
}
4104+
41084105
diagnoseWitnessFixAccessLevel(diags, witness, requiredAccess,
41094106
isSetter);
41104107
});

lib/Serialization/ModuleFormat.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5656
/// describe what change you made. The content of this comment isn't important;
5757
/// it just ensures a conflict if two people change the module format.
5858
/// Don't worry about adhering to the 80-column limit for this line.
59-
const uint16_t SWIFTMODULE_VERSION_MINOR = 623; // remove designated types
59+
const uint16_t SWIFTMODULE_VERSION_MINOR = 624; // new BodyKind for AbstractFunctionDecl
6060

6161
/// A standard hash seed used for all string hashes in a serialized module.
6262
///

0 commit comments

Comments
 (0)