Skip to content

Commit 302e803

Browse files
committed
[AST] Store original lazy VarDecl for backing storage
Store the original VarDecl in the same map we use for tracking the original wrapper var for property wrappers. This will allow us to use the same logic to determine the original var for both.
1 parent c02fb8b commit 302e803

File tree

7 files changed

+38
-35
lines changed

7 files changed

+38
-35
lines changed

include/swift/AST/Decl.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6781,9 +6781,14 @@ class VarDecl : public AbstractStorageDecl {
67816781
bool isLazyStorageProperty() const {
67826782
return Bits.VarDecl.IsLazyStorageProperty;
67836783
}
6784-
void setLazyStorageProperty(bool IsLazyStorage) {
6785-
Bits.VarDecl.IsLazyStorageProperty = IsLazyStorage;
6786-
}
6784+
6785+
/// Mark this VarDecl as backing storage for a the lazy variable `VD`.
6786+
void setLazyStorageFor(VarDecl *VD);
6787+
6788+
/// For a backing storage variable for either a property wrapper or `lazy`
6789+
/// variable, retrieves the original declared variable. Otherwise returns
6790+
/// \c nullptr.
6791+
VarDecl *getOriginalVarForBackingStorage() const;
67876792

67886793
/// Retrieve the backing storage property for a lazy property.
67896794
VarDecl *getLazyStorageProperty() const;

lib/AST/ASTContext.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,9 @@ struct ASTContext::Implementation {
519519
/// Mapping from property declarations to the backing variable types.
520520
llvm::DenseMap<const VarDecl *, Type> PropertyWrapperBackingVarTypes;
521521

522-
/// A mapping from the backing storage of a property that has a wrapper
523-
/// to the original property with the wrapper.
524-
llvm::DenseMap<const VarDecl *, VarDecl *> OriginalWrappedProperties;
522+
/// A mapping from the backing storage of a property that has a wrapper or
523+
/// is `lazy` to the original property.
524+
llvm::DenseMap<const VarDecl *, VarDecl *> OriginalVarsForBackingStorage;
525525

526526
/// The builtin initializer witness for a literal. Used when building
527527
/// LiteralExprs in fully-checked AST.
@@ -917,7 +917,7 @@ void ASTContext::Implementation::dump(llvm::raw_ostream &os) const {
917917
SIZE_AND_BYTES(DefaultAssociatedConformanceWitnesses);
918918
SIZE_AND_BYTES(DefaultTypeRequestCaches);
919919
SIZE_AND_BYTES(PropertyWrapperBackingVarTypes);
920-
SIZE_AND_BYTES(OriginalWrappedProperties);
920+
SIZE_AND_BYTES(OriginalVarsForBackingStorage);
921921
SIZE_AND_BYTES(BuiltinInitWitness);
922922
SIZE_AND_BYTES(OriginalBodySourceRanges);
923923
SIZE_AND_BYTES(NextMacroDiscriminator);
@@ -7066,9 +7066,8 @@ VarDecl *VarDecl::getOriginalWrappedProperty(
70667066
if (!Bits.VarDecl.IsPropertyWrapperBackingProperty)
70677067
return nullptr;
70687068

7069-
ASTContext &ctx = getASTContext();
7070-
assert(ctx.getImpl().OriginalWrappedProperties.count(this) > 0);
7071-
auto original = ctx.getImpl().OriginalWrappedProperties[this];
7069+
auto *original = getOriginalVarForBackingStorage();
7070+
ASSERT(original);
70727071
if (!kind)
70737072
return original;
70747073

@@ -7086,8 +7085,24 @@ VarDecl *VarDecl::getOriginalWrappedProperty(
70867085
void VarDecl::setOriginalWrappedProperty(VarDecl *originalProperty) {
70877086
Bits.VarDecl.IsPropertyWrapperBackingProperty = true;
70887087
ASTContext &ctx = getASTContext();
7089-
assert(ctx.getImpl().OriginalWrappedProperties.count(this) == 0);
7090-
ctx.getImpl().OriginalWrappedProperties[this] = originalProperty;
7088+
assert(ctx.getImpl().OriginalVarsForBackingStorage.count(this) == 0);
7089+
ctx.getImpl().OriginalVarsForBackingStorage[this] = originalProperty;
7090+
}
7091+
7092+
void VarDecl::setLazyStorageFor(VarDecl *VD) {
7093+
Bits.VarDecl.IsLazyStorageProperty = true;
7094+
ASTContext &ctx = getASTContext();
7095+
ASSERT(ctx.getImpl().OriginalVarsForBackingStorage.count(this) == 0);
7096+
ctx.getImpl().OriginalVarsForBackingStorage[this] = VD;
7097+
}
7098+
7099+
VarDecl *VarDecl::getOriginalVarForBackingStorage() const {
7100+
const auto &map = getASTContext().getImpl().OriginalVarsForBackingStorage;
7101+
auto iter = map.find(this);
7102+
if (iter == map.end())
7103+
return nullptr;
7104+
7105+
return iter->second;
70917106
}
70927107

70937108
#ifndef NDEBUG

lib/AST/Decl.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8455,20 +8455,8 @@ bool VarDecl::isActorSelf() const {
84558455
return nominal && nominal->isActor();
84568456
}
84578457

8458-
/// Whether the given variable is the backing storage property for
8459-
/// a declared property that is either `lazy` or has an attached
8460-
/// property wrapper.
8461-
static bool isBackingStorageForDeclaredProperty(const VarDecl *var) {
8462-
if (var->isLazyStorageProperty())
8463-
return true;
8464-
8465-
if (var->getOriginalWrappedProperty())
8466-
return true;
8467-
8468-
return false;
8469-
}
8470-
8471-
/// Whether the given variable is a declared property that has separate backing storage.
8458+
/// Whether the given variable is a declared property that has separate backing
8459+
/// storage.
84728460
static bool isDeclaredPropertyWithBackingStorage(const VarDecl *var) {
84738461
if (var->getAttrs().hasAttribute<LazyAttr>())
84748462
return true;
@@ -8488,8 +8476,7 @@ bool VarDecl::isMemberwiseInitialized(bool preferDeclaredProperties) const {
84888476
// If this is a stored property, and not a backing property in a case where
84898477
// we only want to see the declared properties, it can be memberwise
84908478
// initialized.
8491-
if (hasStorage() && preferDeclaredProperties &&
8492-
isBackingStorageForDeclaredProperty(this))
8479+
if (preferDeclaredProperties && getOriginalVarForBackingStorage())
84938480
return false;
84948481

84958482
// If this is a computed property with `init` accessor, it's

lib/Sema/TypeCheckStorage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3031,7 +3031,7 @@ LazyStoragePropertyRequest::evaluate(Evaluator &evaluator,
30313031
VD->getLoc(), StorageName,
30323032
VD->getDeclContext());
30333033
Storage->setInterfaceType(StorageInterfaceTy);
3034-
Storage->setLazyStorageProperty(true);
3034+
Storage->setLazyStorageFor(VD);
30353035
Storage->setUserAccessible(false);
30363036

30373037
// The storage is implicit and private.

lib/Serialization/Deserialization.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4002,7 +4002,6 @@ class DeclDeserializer {
40024002
bool isImplicit, isObjC, isStatic;
40034003
uint8_t rawIntroducer;
40044004
bool isGetterMutating, isSetterMutating;
4005-
bool isLazyStorageProperty;
40064005
bool isTopLevelGlobal;
40074006
DeclID lazyStorageID;
40084007
unsigned numAccessors, numBackingProperties;
@@ -4018,7 +4017,6 @@ class DeclDeserializer {
40184017
decls_block::VarLayout::readRecord(scratch, nameID, contextID,
40194018
isImplicit, isObjC, isStatic, rawIntroducer,
40204019
isGetterMutating, isSetterMutating,
4021-
isLazyStorageProperty,
40224020
isTopLevelGlobal,
40234021
lazyStorageID,
40244022
opaqueReadOwnership,
@@ -4169,9 +4167,9 @@ class DeclDeserializer {
41694167
VarDecl *storage = cast<VarDecl>(lazyStorageDecl.get());
41704168
ctx.evaluator.cacheOutput(
41714169
LazyStoragePropertyRequest{var}, std::move(storage));
4170+
storage->setLazyStorageFor(var);
41724171
}
41734172

4174-
var->setLazyStorageProperty(isLazyStorageProperty);
41754173
var->setTopLevelGlobal(isTopLevelGlobal);
41764174

41774175
// If there are any backing properties, record them.

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 976; // Expand one type layout
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 977; // remove lazy storage bit
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///
@@ -1730,7 +1730,6 @@ namespace decls_block {
17301730
VarDeclIntroducerField, // introducer
17311731
BCFixed<1>, // is getter mutating?
17321732
BCFixed<1>, // is setter mutating?
1733-
BCFixed<1>, // is this the backing storage for a lazy property?
17341733
BCFixed<1>, // top level global?
17351734
DeclIDField, // if this is a lazy property, this is the backing storage
17361735
OpaqueReadOwnershipField, // opaque read ownership

lib/Serialization/Serialization.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4768,7 +4768,6 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
47684768
rawIntroducer,
47694769
var->isGetterMutating(),
47704770
var->isSetterMutating(),
4771-
var->isLazyStorageProperty(),
47724771
var->isTopLevelGlobal(),
47734772
S.addDeclRef(lazyStorage),
47744773
accessors.OpaqueReadOwnership,

0 commit comments

Comments
 (0)