Skip to content

Commit 5d8252b

Browse files
committed
Pass around whether storage is mutable as an enum instead of a bool.
1 parent c8857bd commit 5d8252b

File tree

5 files changed

+31
-21
lines changed

5 files changed

+31
-21
lines changed

include/swift/AST/Decl.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4143,15 +4143,15 @@ class AbstractStorageDecl : public ValueDecl {
41434143

41444144
protected:
41454145
AbstractStorageDecl(DeclKind Kind, DeclContext *DC, DeclName Name,
4146-
SourceLoc NameLoc, bool supportsMutation)
4146+
SourceLoc NameLoc, StorageIsMutable_t supportsMutation)
41474147
: ValueDecl(Kind, DC, Name, NameLoc) {
41484148
Bits.AbstractStorageDecl.HasStorage = true;
41494149
Bits.AbstractStorageDecl.SupportsMutation = supportsMutation;
41504150
Bits.AbstractStorageDecl.IsGetterMutating = false;
41514151
Bits.AbstractStorageDecl.IsSetterMutating = true;
41524152
}
41534153

4154-
void setSupportsMutationIfStillStored(bool supportsMutation) {
4154+
void setSupportsMutationIfStillStored(StorageIsMutable_t supportsMutation) {
41554155
if (auto ptr = Accessors.getPointer()) {
41564156
auto impl = ptr->getImplInfo();
41574157
if (!impl.isSimpleStored()) return;
@@ -4210,8 +4210,8 @@ class AbstractStorageDecl : public ValueDecl {
42104210
/// don't support mutation (e.g. to initialize them), and sometimes we
42114211
/// can't mutate things that do support mutation (e.g. because their
42124212
/// setter is private).
4213-
bool supportsMutation() const {
4214-
return Bits.AbstractStorageDecl.SupportsMutation;
4213+
StorageIsMutable_t supportsMutation() const {
4214+
return StorageIsMutable_t(Bits.AbstractStorageDecl.SupportsMutation);
42154215
}
42164216

42174217
/// Are there any accessors for this declaration, including implicit ones?
@@ -4452,7 +4452,8 @@ class VarDecl : public AbstractStorageDecl {
44524452

44534453
VarDecl(DeclKind Kind, bool IsStatic, Specifier Sp, bool IsCaptureList,
44544454
SourceLoc NameLoc, Identifier Name, DeclContext *DC)
4455-
: AbstractStorageDecl(Kind, DC, Name, NameLoc, !isImmutableSpecifier(Sp))
4455+
: AbstractStorageDecl(Kind, DC, Name, NameLoc,
4456+
StorageIsMutable_t(!isImmutableSpecifier(Sp)))
44564457
{
44574458
Bits.VarDecl.IsStatic = IsStatic;
44584459
Bits.VarDecl.Specifier = static_cast<unsigned>(Sp);
@@ -4853,7 +4854,7 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
48534854
GenericParamList *GenericParams)
48544855
: GenericContext(DeclContextKind::SubscriptDecl, Parent),
48554856
AbstractStorageDecl(DeclKind::Subscript, Parent, Name, SubscriptLoc,
4856-
/*supports mutation (will be overwritten)*/ true),
4857+
/*will be overwritten*/ StorageIsNotMutable),
48574858
ArrowLoc(ArrowLoc), Indices(nullptr), ElementTy(ElementTy) {
48584859
setIndices(Indices);
48594860
setGenericParams(GenericParams);

include/swift/AST/StorageImpl.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
namespace swift {
2424

25+
enum StorageIsMutable_t : bool {
26+
StorageIsNotMutable = false,
27+
StorageIsMutable = true
28+
};
29+
2530
// Note that the values of these enums line up with %select values in
2631
// diagnostics.
2732
enum class AccessorKind {
@@ -309,16 +314,17 @@ class StorageImplInfo {
309314
#endif
310315
}
311316

312-
static StorageImplInfo getSimpleStored(bool supportsMutation) {
317+
static StorageImplInfo getSimpleStored(StorageIsMutable_t isMutable) {
313318
return { ReadImplKind::Stored,
314-
supportsMutation ? WriteImplKind::Stored
315-
: WriteImplKind::Immutable,
316-
supportsMutation ? ReadWriteImplKind::Stored
317-
: ReadWriteImplKind::Immutable };
319+
isMutable ? WriteImplKind::Stored
320+
: WriteImplKind::Immutable,
321+
isMutable ? ReadWriteImplKind::Stored
322+
: ReadWriteImplKind::Immutable };
318323
}
319324

320-
static StorageImplInfo getOpaque(bool supportsMutation) {
321-
return (supportsMutation ? getMutableOpaque() : getImmutableOpaque());
325+
static StorageImplInfo getOpaque(StorageIsMutable_t isMutable) {
326+
return (isMutable ? getMutableOpaque()
327+
: getImmutableOpaque());
322328
}
323329

324330
/// Describe the implementation of a immutable property implemented opaquely.
@@ -332,8 +338,9 @@ class StorageImplInfo {
332338
ReadWriteImplKind::MaterializeForSet };
333339
}
334340

335-
static StorageImplInfo getComputed(bool supportsMutation) {
336-
return (supportsMutation ? getMutableComputed() : getImmutableComputed());
341+
static StorageImplInfo getComputed(StorageIsMutable_t isMutable) {
342+
return (isMutable ? getMutableComputed()
343+
: getImmutableComputed());
337344
}
338345

339346
/// Describe the implementation of an immutable property implemented
@@ -362,8 +369,8 @@ class StorageImplInfo {
362369
}
363370

364371
/// Does this describe storage that supports mutation?
365-
bool supportsMutation() const {
366-
return getWriteImpl() != WriteImplKind::Immutable;
372+
StorageIsMutable_t supportsMutation() const {
373+
return StorageIsMutable_t(getWriteImpl() != WriteImplKind::Immutable);
367374
}
368375

369376
ReadImplKind getReadImpl() const {

lib/AST/Decl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4577,7 +4577,8 @@ bool VarDecl::isSelfParameter() const {
45774577

45784578
void VarDecl::setSpecifier(Specifier specifier) {
45794579
Bits.VarDecl.Specifier = static_cast<unsigned>(specifier);
4580-
setSupportsMutationIfStillStored(!isImmutableSpecifier(specifier));
4580+
setSupportsMutationIfStillStored(
4581+
StorageIsMutable_t(!isImmutableSpecifier(specifier)));
45814582
}
45824583

45834584
bool VarDecl::isAnonClosureParam() const {

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4768,7 +4768,7 @@ Parser::ParsedAccessors::classify(Parser &P, AbstractStorageDecl *storage,
47684768
// Allow the sil_stored attribute to override all the accessors we parsed
47694769
// when making the final classification.
47704770
if (attrs.hasAttribute<SILStoredAttr>()) {
4771-
return StorageImplInfo::getSimpleStored(Set != nullptr);
4771+
return StorageImplInfo::getSimpleStored(StorageIsMutable_t(Set != nullptr));
47724772
}
47734773

47744774
return StorageImplInfo(readImpl, writeImpl, readWriteImpl);

lib/Sema/CodeSynthesis.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,8 +1836,9 @@ static void maybeAddAccessorsToBehaviorStorage(TypeChecker &TC, VarDecl *var) {
18361836

18371837
SmallVector<AccessorDecl*, 2> accessors;
18381838
accessors.push_back(getter);
1839-
if (setter) accessors.push_back(setter);
1840-
var->setAccessors(StorageImplInfo::getComputed(setter != nullptr),
1839+
auto isMutable = StorageIsMutable_t(setter != nullptr);
1840+
if (isMutable) accessors.push_back(setter);
1841+
var->setAccessors(StorageImplInfo::getComputed(isMutable),
18411842
SourceLoc(), accessors, SourceLoc());
18421843

18431844
// Save the conformance and 'value' decl for later type checking.

0 commit comments

Comments
 (0)