Skip to content
11 changes: 9 additions & 2 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ class alignas(1 << DeclAlignInBits) Decl {
IsStatic : 1
);

SWIFT_INLINE_BITFIELD(VarDecl, AbstractStorageDecl, 1+1+1+1+1+1+1,
SWIFT_INLINE_BITFIELD(VarDecl, AbstractStorageDecl, 1+1+1+1+1+1+1+1,
/// Encodes whether this is a 'let' binding.
Introducer : 1,

Expand All @@ -358,7 +358,10 @@ class alignas(1 << DeclAlignInBits) Decl {
IsLazyStorageProperty : 1,

/// Whether this is the backing storage for a property wrapper.
IsPropertyWrapperBackingProperty : 1
IsPropertyWrapperBackingProperty : 1,

/// Whether this is a lazily top-level global variable from the main file.
IsTopLevelGlobal : 1
);

SWIFT_INLINE_BITFIELD(ParamDecl, VarDecl, 1+2+NumDefaultArgumentKindBits,
Expand Down Expand Up @@ -5084,6 +5087,10 @@ class VarDecl : public AbstractStorageDecl {
Bits.VarDecl.IsLazyStorageProperty = IsLazyStorage;
}

/// True if this is a top-level global variable from the main source file.
bool isTopLevelGlobal() const { return Bits.VarDecl.IsTopLevelGlobal; }
void setTopLevelGlobal(bool b) { Bits.VarDecl.IsTopLevelGlobal = b; }

/// Retrieve the custom attributes that attach property wrappers to this
/// property. The returned list contains all of the attached property wrapper attributes in source order,
/// which means the outermost wrapper attribute is provided first.
Expand Down
7 changes: 2 additions & 5 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5312,6 +5312,7 @@ VarDecl::VarDecl(DeclKind kind, bool isStatic, VarDecl::Introducer introducer,
Bits.VarDecl.IsLazyStorageProperty = false;
Bits.VarDecl.HasNonPatternBindingInit = false;
Bits.VarDecl.IsPropertyWrapperBackingProperty = false;
Bits.VarDecl.IsTopLevelGlobal = false;
}

Type VarDecl::getType() const {
Expand Down Expand Up @@ -5410,11 +5411,7 @@ bool VarDecl::isLazilyInitializedGlobal() const {

// Top-level global variables in the main source file and in the REPL are not
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the above checks (hasClangNode() and isDebuggerVar()) needed at all then? The bit should not be set in those cases either.

// lazily initialized.
auto sourceFileContext = dyn_cast<SourceFile>(getDeclContext());
if (!sourceFileContext)
return true;

return !sourceFileContext->isScriptMode();
return !isTopLevelGlobal();
}

SourceRange VarDecl::getSourceRange() const {
Expand Down
1 change: 1 addition & 0 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5840,6 +5840,7 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
VD->setStatic(StaticLoc.isValid());
VD->getAttrs() = Attributes;
setLocalDiscriminator(VD);
VD->setTopLevelGlobal(topLevelDecl);

// Set original declaration in `@differentiable` attributes.
setOriginalDeclarationForDifferentiableAttributes(Attributes, VD);
Expand Down
3 changes: 3 additions & 0 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2657,6 +2657,7 @@ class DeclDeserializer {
uint8_t rawIntroducer;
bool isGetterMutating, isSetterMutating;
bool isLazyStorageProperty;
bool isTopLevelGlobal;
DeclID lazyStorageID;
unsigned numAccessors, numBackingProperties;
uint8_t readImpl, writeImpl, readWriteImpl, opaqueReadOwnership;
Expand All @@ -2673,6 +2674,7 @@ class DeclDeserializer {
hasNonPatternBindingInit,
isGetterMutating, isSetterMutating,
isLazyStorageProperty,
isTopLevelGlobal,
lazyStorageID,
opaqueReadOwnership,
readImpl, writeImpl, readWriteImpl,
Expand Down Expand Up @@ -2804,6 +2806,7 @@ class DeclDeserializer {
}

var->setLazyStorageProperty(isLazyStorageProperty);
var->setTopLevelGlobal(isTopLevelGlobal);

// If there are any backing properties, record them.
if (numBackingProperties > 0) {
Expand Down
1 change: 1 addition & 0 deletions lib/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,7 @@ namespace decls_block {
BCFixed<1>, // is getter mutating?
BCFixed<1>, // is setter mutating?
BCFixed<1>, // is this the backing storage for a lazy property?
BCFixed<1>, // top level global?
DeclIDField, // if this is a lazy property, this is the backing storage
OpaqueReadOwnershipField, // opaque read ownership
ReadImplKindField, // read implementation
Expand Down
1 change: 1 addition & 0 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3255,6 +3255,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
var->isGetterMutating(),
var->isSetterMutating(),
var->isLazyStorageProperty(),
var->isTopLevelGlobal(),
S.addDeclRef(lazyStorage),
accessors.OpaqueReadOwnership,
accessors.ReadImpl,
Expand Down