Skip to content

Commit fae8f94

Browse files
committed
AST: Add 'hoisted' flag to Decl
1 parent 923b1fb commit fae8f94

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

include/swift/AST/Decl.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class alignas(1 << DeclAlignInBits) Decl {
284284
protected:
285285
union { uint64_t OpaqueBits;
286286

287-
SWIFT_INLINE_BITFIELD_BASE(Decl, bitmax(NumDeclKindBits,8)+1+1+1+1,
287+
SWIFT_INLINE_BITFIELD_BASE(Decl, bitmax(NumDeclKindBits,8)+1+1+1+1+1,
288288
Kind : bitmax(NumDeclKindBits,8),
289289

290290
/// Whether this declaration is invalid.
@@ -301,7 +301,13 @@ class alignas(1 << DeclAlignInBits) Decl {
301301

302302
/// Whether this declaration was added to the surrounding
303303
/// DeclContext of an active #if config clause.
304-
EscapedFromIfConfig : 1
304+
EscapedFromIfConfig : 1,
305+
306+
/// Whether this declaration is syntactically scoped inside of
307+
/// a local context, but should behave like a top-level
308+
/// declaration for name lookup purposes. This is used by
309+
/// lldb.
310+
Hoisted : 1
305311
);
306312

307313
SWIFT_INLINE_BITFIELD_FULL(PatternBindingDecl, Decl, 1+2+16,
@@ -690,6 +696,7 @@ class alignas(1 << DeclAlignInBits) Decl {
690696
Bits.Decl.Implicit = false;
691697
Bits.Decl.FromClang = false;
692698
Bits.Decl.EscapedFromIfConfig = false;
699+
Bits.Decl.Hoisted = false;
693700
}
694701

695702
/// Get the Clang node associated with this declaration.
@@ -837,6 +844,16 @@ class alignas(1 << DeclAlignInBits) Decl {
837844
/// Mark this declaration as implicit.
838845
void setImplicit(bool implicit = true) { Bits.Decl.Implicit = implicit; }
839846

847+
/// Determine whether this declaration is syntactically scoped inside of
848+
/// a local context, but should behave like a top-level declaration
849+
/// for name lookup purposes. This is used by lldb.
850+
bool isHoisted() const { return Bits.Decl.Hoisted; }
851+
852+
/// Set whether this declaration should be syntactically scoped inside
853+
/// of a local context, but should behave like a top-level declaration,
854+
/// but should behave like a top-level declaration. This is used by lldb.
855+
void setHoisted(bool hoisted = true) { Bits.Decl.Hoisted = hoisted; }
856+
840857
public:
841858
bool escapedFromIfConfig() const {
842859
return Bits.Decl.EscapedFromIfConfig;

include/swift/AST/SourceFile.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ class SourceFile final : public FileUnit {
187187
/// have been removed, this can become an optional ArrayRef.
188188
Optional<std::vector<Decl *>> Decls;
189189

190+
/// The list of hoisted declarations. See Decl::isHoisted().
191+
/// This is only used by lldb.
192+
std::vector<Decl *> Hoisted;
193+
190194
using SeparatelyImportedOverlayMap =
191195
llvm::SmallDenseMap<ModuleDecl *, llvm::SmallPtrSet<ModuleDecl *, 1>>;
192196

@@ -231,9 +235,18 @@ class SourceFile final : public FileUnit {
231235
Decls->insert(Decls->begin(), d);
232236
}
233237

238+
/// Add a hoisted declaration. See Decl::isHoisted().
239+
void addHoistedDecl(Decl *d) {
240+
Hoisted.push_back(d);
241+
}
242+
234243
/// Retrieves an immutable view of the list of top-level decls in this file.
235244
ArrayRef<Decl *> getTopLevelDecls() const;
236245

246+
/// Retrieves an immutable view of the list of hoisted decls in this file.
247+
/// See Decl::isHoisted().
248+
ArrayRef<Decl *> getHoistedDecls() const;
249+
237250
/// Retrieves an immutable view of the top-level decls if they have already
238251
/// been parsed, or \c None if they haven't. Should only be used for dumping.
239252
Optional<ArrayRef<Decl *>> getCachedTopLevelDecls() const {

lib/AST/ASTDumper.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,9 @@ namespace {
585585
if (D->isImplicit())
586586
PrintWithColorRAII(OS, DeclModifierColor) << " implicit";
587587

588+
if (D->isHoisted())
589+
PrintWithColorRAII(OS, DeclModifierColor) << " hoisted";
590+
588591
auto R = D->getSourceRange();
589592
if (R.isValid()) {
590593
PrintWithColorRAII(OS, RangeColor) << " range=";

lib/AST/Module.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,10 @@ ArrayRef<Decl *> SourceFile::getTopLevelDecls() const {
23422342
{}).TopLevelDecls;
23432343
}
23442344

2345+
ArrayRef<Decl *> SourceFile::getHoistedDecls() const {
2346+
return Hoisted;
2347+
}
2348+
23452349
bool FileUnit::walk(ASTWalker &walker) {
23462350
SmallVector<Decl *, 64> Decls;
23472351
getTopLevelDecls(Decls);

0 commit comments

Comments
 (0)