Skip to content

Commit 4bd1ffc

Browse files
adrian-prantlxedin
authored andcommitted
Serialize whether a VarDecl is a top-level global.
The current way that VarDecl::isLazilyInitializedGlobal() is implemented does not work in the debugger, since the DeclContext of all VarDecls are deserialized Swift modules. By adding a bit to the VarDecl we can recover the fact that a VarDecl was in fact a global even in the debugger. <rdar://problem/58939370>
1 parent 93c2321 commit 4bd1ffc

File tree

6 files changed

+17
-7
lines changed

6 files changed

+17
-7
lines changed

include/swift/AST/Decl.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ class alignas(1 << DeclAlignInBits) Decl {
335335
IsStatic : 1
336336
);
337337

338-
SWIFT_INLINE_BITFIELD(VarDecl, AbstractStorageDecl, 1+1+1+1+1+1+1,
338+
SWIFT_INLINE_BITFIELD(VarDecl, AbstractStorageDecl, 1+1+1+1+1+1+1+1,
339339
/// Encodes whether this is a 'let' binding.
340340
Introducer : 1,
341341

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

360360
/// Whether this is the backing storage for a property wrapper.
361-
IsPropertyWrapperBackingProperty : 1
361+
IsPropertyWrapperBackingProperty : 1,
362+
363+
/// Whether this is a lazily top-level global variable from the main file.
364+
IsTopLevelGlobal : 1
362365
);
363366

364367
SWIFT_INLINE_BITFIELD(ParamDecl, VarDecl, 1+2+NumDefaultArgumentKindBits,
@@ -5084,6 +5087,10 @@ class VarDecl : public AbstractStorageDecl {
50845087
Bits.VarDecl.IsLazyStorageProperty = IsLazyStorage;
50855088
}
50865089

5090+
/// True if this is a top-level global variable from the main source file.
5091+
bool isTopLevelGlobal() const { return Bits.VarDecl.IsTopLevelGlobal; }
5092+
void setTopLevelGlobal(bool b) { Bits.VarDecl.IsTopLevelGlobal = b; }
5093+
50875094
/// Retrieve the custom attributes that attach property wrappers to this
50885095
/// property. The returned list contains all of the attached property wrapper attributes in source order,
50895096
/// which means the outermost wrapper attribute is provided first.

lib/AST/Decl.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -5312,6 +5312,7 @@ VarDecl::VarDecl(DeclKind kind, bool isStatic, VarDecl::Introducer introducer,
53125312
Bits.VarDecl.IsLazyStorageProperty = false;
53135313
Bits.VarDecl.HasNonPatternBindingInit = false;
53145314
Bits.VarDecl.IsPropertyWrapperBackingProperty = false;
5315+
Bits.VarDecl.IsTopLevelGlobal = false;
53155316
}
53165317

53175318
Type VarDecl::getType() const {
@@ -5410,11 +5411,7 @@ bool VarDecl::isLazilyInitializedGlobal() const {
54105411

54115412
// Top-level global variables in the main source file and in the REPL are not
54125413
// lazily initialized.
5413-
auto sourceFileContext = dyn_cast<SourceFile>(getDeclContext());
5414-
if (!sourceFileContext)
5415-
return true;
5416-
5417-
return !sourceFileContext->isScriptMode();
5414+
return !isTopLevelGlobal();
54185415
}
54195416

54205417
SourceRange VarDecl::getSourceRange() const {

lib/Parse/ParseDecl.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -5840,6 +5840,7 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
58405840
VD->setStatic(StaticLoc.isValid());
58415841
VD->getAttrs() = Attributes;
58425842
setLocalDiscriminator(VD);
5843+
VD->setTopLevelGlobal(topLevelDecl);
58435844

58445845
// Set original declaration in `@differentiable` attributes.
58455846
setOriginalDeclarationForDifferentiableAttributes(Attributes, VD);

lib/Serialization/Deserialization.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -2657,6 +2657,7 @@ class DeclDeserializer {
26572657
uint8_t rawIntroducer;
26582658
bool isGetterMutating, isSetterMutating;
26592659
bool isLazyStorageProperty;
2660+
bool isTopLevelGlobal;
26602661
DeclID lazyStorageID;
26612662
unsigned numAccessors, numBackingProperties;
26622663
uint8_t readImpl, writeImpl, readWriteImpl, opaqueReadOwnership;
@@ -2673,6 +2674,7 @@ class DeclDeserializer {
26732674
hasNonPatternBindingInit,
26742675
isGetterMutating, isSetterMutating,
26752676
isLazyStorageProperty,
2677+
isTopLevelGlobal,
26762678
lazyStorageID,
26772679
opaqueReadOwnership,
26782680
readImpl, writeImpl, readWriteImpl,
@@ -2804,6 +2806,7 @@ class DeclDeserializer {
28042806
}
28052807

28062808
var->setLazyStorageProperty(isLazyStorageProperty);
2809+
var->setTopLevelGlobal(isTopLevelGlobal);
28072810

28082811
// If there are any backing properties, record them.
28092812
if (numBackingProperties > 0) {

lib/Serialization/ModuleFormat.h

+1
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,7 @@ namespace decls_block {
11911191
BCFixed<1>, // is getter mutating?
11921192
BCFixed<1>, // is setter mutating?
11931193
BCFixed<1>, // is this the backing storage for a lazy property?
1194+
BCFixed<1>, // top level global?
11941195
DeclIDField, // if this is a lazy property, this is the backing storage
11951196
OpaqueReadOwnershipField, // opaque read ownership
11961197
ReadImplKindField, // read implementation

lib/Serialization/Serialization.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -3255,6 +3255,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
32553255
var->isGetterMutating(),
32563256
var->isSetterMutating(),
32573257
var->isLazyStorageProperty(),
3258+
var->isTopLevelGlobal(),
32583259
S.addDeclRef(lazyStorage),
32593260
accessors.OpaqueReadOwnership,
32603261
accessors.ReadImpl,

0 commit comments

Comments
 (0)