Skip to content

Commit 454281b

Browse files
committed
AST: Split off StorageImplInfo from AbstractStorageDecl::AccessorRecord
We want to compute the former independently of the latter. It's only 16 bits so storing it inside the Decl is fine; it also allows us to eliminate the 'compact' representation where an AbstractStorageDecl without an accessor record is assumed to be stored.
1 parent a532a32 commit 454281b

File tree

8 files changed

+48
-88
lines changed

8 files changed

+48
-88
lines changed

include/swift/AST/Decl.h

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -344,16 +344,7 @@ class alignas(1 << DeclAlignInBits) Decl {
344344
IsUserAccessible : 1
345345
);
346346

347-
SWIFT_INLINE_BITFIELD(AbstractStorageDecl, ValueDecl, 1+1+2+1+1+1,
348-
/// Whether this represents physical storage.
349-
HasStorage : 1,
350-
351-
/// Whether this storage supports semantic mutation in some way.
352-
SupportsMutation : 1,
353-
354-
/// Whether an opaque read of this storage produces an owned value.
355-
OpaqueReadOwnership : 2,
356-
347+
SWIFT_INLINE_BITFIELD(AbstractStorageDecl, ValueDecl, 1+1+1,
357348
/// Whether a keypath component can directly reference this storage,
358349
/// or if it must use the overridden declaration instead.
359350
HasComputedValidKeyPathComponent : 1,
@@ -4416,10 +4407,6 @@ class AbstractStorageDecl : public ValueDecl {
44164407
/// The range of the braces around the accessor clause.
44174408
SourceRange Braces;
44184409

4419-
/// The implementation info for the accessors. If there's no
4420-
/// AccessorRecord for a storage decl, the decl is just stored.
4421-
StorageImplInfo ImplInfo;
4422-
44234410
/// The number of accessors currently stored in this record.
44244411
AccessorIndex NumAccessors;
44254412

@@ -4433,19 +4420,15 @@ class AbstractStorageDecl : public ValueDecl {
44334420
/// or the index+1 of the accessor in the accessors array.
44344421
AccessorIndex AccessorIndices[NumAccessorKinds];
44354422

4436-
AccessorRecord(SourceRange braces, StorageImplInfo implInfo,
4423+
AccessorRecord(SourceRange braces,
44374424
ArrayRef<AccessorDecl*> accessors,
44384425
AccessorIndex accessorsCapacity);
44394426
public:
44404427
static AccessorRecord *create(ASTContext &ctx, SourceRange braces,
4441-
StorageImplInfo implInfo,
44424428
ArrayRef<AccessorDecl*> accessors);
44434429

44444430
SourceRange getBracesRange() const { return Braces; }
44454431

4446-
const StorageImplInfo &getImplInfo() const { return ImplInfo; }
4447-
void overwriteImplInfo(StorageImplInfo newInfo) { ImplInfo = newInfo; }
4448-
44494432
inline AccessorDecl *getAccessor(AccessorKind kind) const;
44504433

44514434
ArrayRef<AccessorDecl *> getAllAccessors() const {
@@ -4464,27 +4447,25 @@ class AbstractStorageDecl : public ValueDecl {
44644447

44654448
llvm::PointerIntPair<AccessorRecord*, 3, OptionalEnum<AccessLevel>> Accessors;
44664449

4467-
void setFieldsFromImplInfo(StorageImplInfo implInfo) {
4468-
Bits.AbstractStorageDecl.HasStorage = implInfo.hasStorage();
4469-
Bits.AbstractStorageDecl.SupportsMutation = implInfo.supportsMutation();
4470-
}
4471-
44724450
struct {
44734451
unsigned IsGetterMutatingComputed : 1;
44744452
unsigned IsGetterMutating : 1;
44754453
unsigned IsSetterMutatingComputed : 1;
44764454
unsigned IsSetterMutating : 1;
44774455
unsigned OpaqueReadOwnershipComputed : 1;
44784456
unsigned OpaqueReadOwnership : 2;
4457+
unsigned ImplInfoComputed : 1;
44794458
} LazySemanticInfo = { };
44804459

4460+
/// The implementation info for the accessors.
4461+
StorageImplInfo ImplInfo;
4462+
44814463
protected:
44824464
AbstractStorageDecl(DeclKind Kind, bool IsStatic, DeclContext *DC,
44834465
DeclName Name, SourceLoc NameLoc,
44844466
StorageIsMutable_t supportsMutation)
4485-
: ValueDecl(Kind, DC, Name, NameLoc) {
4486-
Bits.AbstractStorageDecl.HasStorage = true;
4487-
Bits.AbstractStorageDecl.SupportsMutation = supportsMutation;
4467+
: ValueDecl(Kind, DC, Name, NameLoc),
4468+
ImplInfo(StorageImplInfo::getSimpleStored(supportsMutation)) {
44884469
Bits.AbstractStorageDecl.IsStatic = IsStatic;
44894470
}
44904471

@@ -4513,10 +4494,13 @@ class AbstractStorageDecl : public ValueDecl {
45134494
Type getValueInterfaceType() const;
45144495

45154496
/// Determine how this storage is implemented.
4516-
StorageImplInfo getImplInfo() const {
4517-
if (auto ptr = Accessors.getPointer())
4518-
return ptr->getImplInfo();
4519-
return StorageImplInfo::getSimpleStored(supportsMutation());
4497+
StorageImplInfo getImplInfo() const { return ImplInfo; }
4498+
4499+
/// Overwrite the registered implementation-info. This should be
4500+
/// used carefully.
4501+
void setImplInfo(StorageImplInfo implInfo) {
4502+
LazySemanticInfo.ImplInfoComputed = 1;
4503+
ImplInfo = implInfo;
45204504
}
45214505

45224506
ReadImplKind getReadImpl() const {
@@ -4529,14 +4513,11 @@ class AbstractStorageDecl : public ValueDecl {
45294513
return getImplInfo().getReadWriteImpl();
45304514
}
45314515

4532-
/// Overwrite the registered implementation-info. This should be
4533-
/// used carefully.
4534-
void overwriteImplInfo(StorageImplInfo implInfo);
45354516

45364517
/// Return true if this is a VarDecl that has storage associated with
45374518
/// it.
45384519
bool hasStorage() const {
4539-
return Bits.AbstractStorageDecl.HasStorage;
4520+
return getImplInfo().hasStorage();
45404521
}
45414522

45424523
/// Return true if this storage has the basic accessors/capability
@@ -4549,7 +4530,7 @@ class AbstractStorageDecl : public ValueDecl {
45494530
/// can't mutate things that do support mutation (e.g. because their
45504531
/// setter is private).
45514532
StorageIsMutable_t supportsMutation() const {
4552-
return StorageIsMutable_t(Bits.AbstractStorageDecl.SupportsMutation);
4533+
return getImplInfo().supportsMutation();
45534534
}
45544535

45554536
/// Are there any accessors for this declaration, including implicit ones?
@@ -4599,8 +4580,7 @@ class AbstractStorageDecl : public ValueDecl {
45994580
/// Visit all the opaque accessors of this storage declaration.
46004581
void visitOpaqueAccessors(llvm::function_ref<void (AccessorDecl*)>) const;
46014582

4602-
void setAccessors(StorageImplInfo storageImpl,
4603-
SourceLoc lbraceLoc, ArrayRef<AccessorDecl*> accessors,
4583+
void setAccessors(SourceLoc lbraceLoc, ArrayRef<AccessorDecl*> accessors,
46044584
SourceLoc rbraceLoc);
46054585

46064586
/// Add a setter to an existing Computed var.

lib/AST/Decl.cpp

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4657,20 +4657,6 @@ void ProtocolDecl::computeKnownProtocolKind() const {
46574657
const_cast<ProtocolDecl *>(this)->Bits.ProtocolDecl.KnownProtocol = value;
46584658
}
46594659

4660-
void AbstractStorageDecl::overwriteImplInfo(StorageImplInfo implInfo) {
4661-
setFieldsFromImplInfo(implInfo);
4662-
4663-
auto *accessors = Accessors.getPointer();
4664-
if (!accessors) {
4665-
accessors = AccessorRecord::create(getASTContext(), SourceRange(),
4666-
implInfo, {});
4667-
Accessors.setPointer(accessors);
4668-
return;
4669-
}
4670-
4671-
accessors->overwriteImplInfo(implInfo);
4672-
}
4673-
46744660
bool AbstractStorageDecl::hasPrivateAccessor() const {
46754661
for (auto accessor : getAllAccessors()) {
46764662
if (hasPrivateOrFilePrivateFormalAccess(accessor))
@@ -4702,12 +4688,9 @@ bool AbstractStorageDecl::hasAnyDynamicReplacementAccessors() const {
47024688
}
47034689
return false;
47044690
}
4705-
void AbstractStorageDecl::setAccessors(StorageImplInfo implInfo,
4706-
SourceLoc lbraceLoc,
4691+
void AbstractStorageDecl::setAccessors(SourceLoc lbraceLoc,
47074692
ArrayRef<AccessorDecl *> accessors,
47084693
SourceLoc rbraceLoc) {
4709-
setFieldsFromImplInfo(implInfo);
4710-
47114694
// This method is called after we've already recorded an accessors clause
47124695
// only on recovery paths and only when that clause was empty.
47134696
auto record = Accessors.getPointer();
@@ -4719,7 +4702,7 @@ void AbstractStorageDecl::setAccessors(StorageImplInfo implInfo,
47194702
} else {
47204703
record = AccessorRecord::create(getASTContext(),
47214704
SourceRange(lbraceLoc, rbraceLoc),
4722-
implInfo, accessors);
4705+
accessors);
47234706
Accessors.setPointer(record);
47244707
}
47254708
}
@@ -4736,7 +4719,6 @@ const size_t NumOpaqueAccessors =
47364719
AbstractStorageDecl::AccessorRecord *
47374720
AbstractStorageDecl::AccessorRecord::create(ASTContext &ctx,
47384721
SourceRange braces,
4739-
StorageImplInfo storageInfo,
47404722
ArrayRef<AccessorDecl*> accessors) {
47414723
// Silently cap the number of accessors we store at a number that should
47424724
// be easily sufficient for all the valid cases, including space for adding
@@ -4776,15 +4758,13 @@ AbstractStorageDecl::AccessorRecord::create(ASTContext &ctx,
47764758
auto accessorsCapacity = AccessorIndex(accessors.size() + numMissingOpaque);
47774759
void *mem = ctx.Allocate(totalSizeToAlloc<AccessorDecl*>(accessorsCapacity),
47784760
alignof(AccessorRecord));
4779-
return new (mem) AccessorRecord(braces, storageInfo,
4780-
accessors, accessorsCapacity);
4761+
return new (mem) AccessorRecord(braces, accessors, accessorsCapacity);
47814762
}
47824763

47834764
AbstractStorageDecl::AccessorRecord::AccessorRecord(SourceRange braces,
4784-
StorageImplInfo implInfo,
4785-
ArrayRef<AccessorDecl *> accessors,
4786-
AccessorIndex accessorsCapacity)
4787-
: Braces(braces), ImplInfo(implInfo), NumAccessors(accessors.size()),
4765+
ArrayRef<AccessorDecl *> accessors,
4766+
AccessorIndex accessorsCapacity)
4767+
: Braces(braces), NumAccessors(accessors.size()),
47884768
AccessorsCapacity(accessorsCapacity), AccessorIndices{} {
47894769

47904770
// Copy the complete accessors list into place.
@@ -4852,7 +4832,7 @@ void AbstractStorageDecl::setComputedSetter(AccessorDecl *setter) {
48524832
assert(isAccessor(setter, AccessorKind::Set, this));
48534833
assert(setter && "should not be called for readonly properties");
48544834

4855-
overwriteImplInfo(StorageImplInfo::getMutableComputed());
4835+
setImplInfo(StorageImplInfo::getMutableComputed());
48564836
Accessors.getPointer()->addOpaqueAccessor(setter);
48574837
}
48584838

@@ -4863,8 +4843,7 @@ AbstractStorageDecl::setSynthesizedGetter(AccessorDecl *accessor) {
48634843

48644844
auto accessors = Accessors.getPointer();
48654845
if (!accessors) {
4866-
accessors = AccessorRecord::create(getASTContext(), SourceRange(),
4867-
getImplInfo(), {});
4846+
accessors = AccessorRecord::create(getASTContext(), SourceRange(), {});
48684847
Accessors.setPointer(accessors);
48694848
}
48704849

@@ -4878,8 +4857,7 @@ AbstractStorageDecl::setSynthesizedReadCoroutine(AccessorDecl *accessor) {
48784857

48794858
auto accessors = Accessors.getPointer();
48804859
if (!accessors) {
4881-
accessors = AccessorRecord::create(getASTContext(), SourceRange(),
4882-
getImplInfo(), {});
4860+
accessors = AccessorRecord::create(getASTContext(), SourceRange(), {});
48834861
Accessors.setPointer(accessors);
48844862
}
48854863

@@ -5434,7 +5412,7 @@ void ParamDecl::setSpecifier(Specifier specifier) {
54345412
? VarDecl::Introducer::Let
54355413
: VarDecl::Introducer::Var);
54365414
Bits.ParamDecl.Specifier = static_cast<unsigned>(specifier);
5437-
overwriteImplInfo(
5415+
setImplInfo(
54385416
StorageImplInfo::getSimpleStored(
54395417
isImmutableSpecifier(specifier)
54405418
? StorageIsNotMutable

lib/ClangImporter/ImportDecl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ static void makeComputed(AbstractStorageDecl *storage,
187187
AccessorDecl *getter, AccessorDecl *setter) {
188188
assert(getter);
189189
if (setter) {
190-
storage->setAccessors(StorageImplInfo::getMutableComputed(),
191-
SourceLoc(), {getter, setter}, SourceLoc());
190+
storage->setImplInfo(StorageImplInfo::getMutableComputed());
191+
storage->setAccessors(SourceLoc(), {getter, setter}, SourceLoc());
192192
} else {
193-
storage->setAccessors(StorageImplInfo::getImmutableComputed(),
194-
SourceLoc(), {getter}, SourceLoc());
193+
storage->setImplInfo(StorageImplInfo::getImmutableComputed());
194+
storage->setAccessors(SourceLoc(), {getter}, SourceLoc());
195195
}
196196
}
197197

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4947,10 +4947,10 @@ void Parser::ParsedAccessors::record(Parser &P, AbstractStorageDecl *storage,
49474947
SmallVectorImpl<Decl *> &decls) {
49484948
auto storageKind = classify(P, storage, invalid, flags, staticLoc, attrs,
49494949
elementTy, indices);
4950+
storage->setImplInfo(storageKind);
49504951

49514952
decls.append(Accessors.begin(), Accessors.end());
4952-
4953-
storage->setAccessors(storageKind, LBLoc, Accessors, RBLoc);
4953+
storage->setAccessors(LBLoc, Accessors, RBLoc);
49544954
}
49554955

49564956
static void flagInvalidAccessor(AccessorDecl *func) {

lib/Sema/CodeSynthesis.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,9 +1791,9 @@ static VarDecl *synthesizePropertyWrapperStorageWrapperProperty(
17911791
bool hasSetter = wrapperVar->isSettable(nullptr) &&
17921792
wrapperVar->isSetterAccessibleFrom(var->getInnermostDeclContext());
17931793
if (hasSetter)
1794-
property->overwriteImplInfo(StorageImplInfo::getMutableComputed());
1794+
property->setImplInfo(StorageImplInfo::getMutableComputed());
17951795
else
1796-
property->overwriteImplInfo(StorageImplInfo::getImmutableComputed());
1796+
property->setImplInfo(StorageImplInfo::getImmutableComputed());
17971797
addExpectedOpaqueAccessorsToStorage(property, ctx);
17981798

17991799
var->getAttrs().add(
@@ -2051,15 +2051,15 @@ static void finishProtocolStorageImplInfo(AbstractStorageDecl *storage) {
20512051
}
20522052
}
20532053

2054-
storage->overwriteImplInfo(getProtocolStorageImpl(storage));
2054+
storage->setImplInfo(getProtocolStorageImpl(storage));
20552055
}
20562056

20572057
static void finishLazyVariableImplInfo(VarDecl *var) {
20582058
// If there are already accessors, something is invalid; bail out.
20592059
if (!var->getImplInfo().isSimpleStored())
20602060
return;
20612061

2062-
var->overwriteImplInfo(StorageImplInfo::getMutableComputed());
2062+
var->setImplInfo(StorageImplInfo::getMutableComputed());
20632063
}
20642064

20652065
/// Determine whether all of the wrapped-value setters for the property
@@ -2091,17 +2091,17 @@ static void finishPropertyWrapperImplInfo(VarDecl *var) {
20912091
allPropertyWrapperValueSettersAreAccessible(var));
20922092

20932093
if (wrapperSetterIsUsable)
2094-
var->overwriteImplInfo(StorageImplInfo::getMutableComputed());
2094+
var->setImplInfo(StorageImplInfo::getMutableComputed());
20952095
else
2096-
var->overwriteImplInfo(StorageImplInfo::getImmutableComputed());
2096+
var->setImplInfo(StorageImplInfo::getImmutableComputed());
20972097
}
20982098

20992099
static void finishNSManagedImplInfo(VarDecl *VD) {
21002100
// If it's not still stored, just bail out.
21012101
if (!VD->getImplInfo().isSimpleStored())
21022102
return;
21032103

2104-
VD->overwriteImplInfo(StorageImplInfo::getMutableComputed());
2104+
VD->setImplInfo(StorageImplInfo::getMutableComputed());
21052105
}
21062106

21072107
static void finishStorageImplInfo(AbstractStorageDecl *storage) {

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,8 +1208,8 @@ static ValueDecl *deriveHashable_hashValue(DerivedConformance &derived) {
12081208
hashValueDecl->setImplicit();
12091209
hashValueDecl->setInterfaceType(intType);
12101210
hashValueDecl->setValidationToChecked();
1211-
hashValueDecl->setAccessors(StorageImplInfo::getImmutableComputed(),
1212-
SourceLoc(), {getterDecl}, SourceLoc());
1211+
hashValueDecl->setImplInfo(StorageImplInfo::getImmutableComputed());
1212+
hashValueDecl->setAccessors(SourceLoc(), {getterDecl}, SourceLoc());
12131213
hashValueDecl->copyFormalAccessFrom(derived.Nominal,
12141214
/*sourceIsParentContext*/ true);
12151215

lib/Sema/DerivedConformances.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ addGetterToReadOnlyDerivedProperty(VarDecl *property,
283283
auto getter =
284284
declareDerivedPropertyGetter(property, propertyContextType);
285285

286-
property->setAccessors(StorageImplInfo::getImmutableComputed(),
287-
SourceLoc(), {getter}, SourceLoc());
286+
property->setImplInfo(StorageImplInfo::getImmutableComputed());
287+
property->setAccessors(SourceLoc(), {getter}, SourceLoc());
288288

289289
return getter;
290290
}

lib/Serialization/Deserialization.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2267,13 +2267,15 @@ void ModuleFile::configureStorage(AbstractStorageDecl *decl,
22672267
}
22682268

22692269
auto implInfo = StorageImplInfo(*readImpl, *writeImpl, *readWriteImpl);
2270+
decl->setImplInfo(implInfo);
2271+
22702272
if (implInfo.isSimpleStored() && accessors.empty())
22712273
return;
22722274

22732275
// We currently don't serialize these locations.
22742276
SourceLoc beginLoc, endLoc;
22752277

2276-
decl->setAccessors(implInfo, beginLoc, accessors, endLoc);
2278+
decl->setAccessors(beginLoc, accessors, endLoc);
22772279
}
22782280

22792281
template <typename T, typename ...Args>

0 commit comments

Comments
 (0)