Skip to content

Commit 4ac0239

Browse files
committed
fix top level vars
1 parent aa3576b commit 4ac0239

23 files changed

+151
-141
lines changed

include/swift/AST/ASTScope.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class ASTScopeImpl : public ASTAllocated<ASTScopeImpl> {
140140
friend class GenericTypeOrExtensionWherePortion;
141141
friend class GenericTypeOrExtensionWherePortion;
142142
friend class IterableTypeBodyPortion;
143+
friend class TopLevelCodeScope;
143144
friend class ScopeCreator;
144145
friend class ASTSourceFileScope;
145146
friend class ABIAttributeScope;
@@ -245,7 +246,7 @@ class ASTScopeImpl : public ASTAllocated<ASTScopeImpl> {
245246
#pragma mark - debugging and printing
246247

247248
public:
248-
const SourceFile *getSourceFile() const;
249+
SourceFile *getSourceFile() const;
249250
std::string getClassName() const;
250251

251252
/// Print out this scope for debugging/reporting purposes.
@@ -1212,18 +1213,19 @@ class ClosureParametersScope final : public ASTScopeImpl {
12121213
class TopLevelCodeScope final : public ASTScopeImpl {
12131214
public:
12141215
TopLevelCodeDecl *const decl;
1215-
SourceLoc endLoc;
1216+
ASTScopeImpl *insertionPoint = nullptr;
12161217

1217-
TopLevelCodeScope(TopLevelCodeDecl *e, SourceLoc endLoc)
1218-
: ASTScopeImpl(ScopeKind::TopLevelCode), decl(e), endLoc(endLoc) {}
1218+
TopLevelCodeScope(TopLevelCodeDecl *e)
1219+
: ASTScopeImpl(ScopeKind::TopLevelCode), decl(e) {}
12191220
virtual ~TopLevelCodeScope() {}
12201221

12211222
protected:
12221223
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
12231224

1225+
NullablePtr<const ASTScopeImpl> getLookupParent() const override;
1226+
12241227
private:
1225-
AnnotatedInsertionPoint
1226-
expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &);
1228+
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
12271229

12281230
public:
12291231
SourceRange

include/swift/AST/Decl.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
503503
IsStatic : 1
504504
);
505505

506-
SWIFT_INLINE_BITFIELD(VarDecl, AbstractStorageDecl, 2+1+1+1+1+1+1+1+1,
506+
SWIFT_INLINE_BITFIELD(VarDecl, AbstractStorageDecl, 2+1+1+1+1+1+1+1,
507507
/// Encodes whether this is a 'let' binding.
508508
Introducer : 2,
509509

@@ -520,9 +520,6 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
520520
/// Whether this is the backing storage for a property wrapper.
521521
IsPropertyWrapperBackingProperty : 1,
522522

523-
/// Whether this is a lazily top-level global variable from the main file.
524-
IsTopLevelGlobal : 1,
525-
526523
/// Whether this variable has no attached property wrappers.
527524
NoAttachedPropertyWrappers : 1,
528525

@@ -2879,6 +2876,8 @@ class PatternBindingDecl final : public Decl,
28792876
/// global variables.
28802877
class TopLevelCodeDecl : public DeclContext, public Decl {
28812878
BraceStmt *Body;
2879+
std::optional<TopLevelCodeDecl *> Previous;
2880+
28822881
SourceLoc getLocFromSource() const { return getStartLoc(); }
28832882
friend class Decl;
28842883
public:
@@ -2890,6 +2889,8 @@ class TopLevelCodeDecl : public DeclContext, public Decl {
28902889
BraceStmt *getBody() const { return Body; }
28912890
void setBody(BraceStmt *b) { Body = b; }
28922891

2892+
TopLevelCodeDecl *getPrevious() const;
2893+
28932894
SourceLoc getStartLoc() const;
28942895
SourceRange getSourceRange() const;
28952896

@@ -6816,10 +6817,6 @@ class VarDecl : public AbstractStorageDecl {
68166817
/// Retrieve the backing storage property for a lazy property.
68176818
VarDecl *getLazyStorageProperty() const;
68186819

6819-
/// True if this is a top-level global variable from the main source file.
6820-
bool isTopLevelGlobal() const { return Bits.VarDecl.IsTopLevelGlobal; }
6821-
void setTopLevelGlobal(bool b) { Bits.VarDecl.IsTopLevelGlobal = b; }
6822-
68236820
/// True if this is any storage of static duration (global scope or static).
68246821
bool isGlobalStorage() const;
68256822

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6203,8 +6203,6 @@ ERROR(global_actor_arg,none,
62036203
(Type))
62046204
ERROR(global_actor_non_final_class,none,
62056205
"non-final class %0 cannot be a global actor", (DeclName))
6206-
ERROR(global_actor_top_level_var,none,
6207-
"top-level code variables cannot have a global actor", ())
62086206
ERROR(global_actor_access,none,
62096207
"%select{private|fileprivate|internal|package|public|open}0 %kind1 "
62106208
"cannot have %select{private|fileprivate|internal|package|%error|%error}2 "
@@ -7216,14 +7214,6 @@ ERROR(availability_decl_no_unavailable, none,
72167214
"%kindonly0 cannot be marked unavailable with '@available'",
72177215
(const Decl *))
72187216

7219-
ERROR(availability_global_script_no_potential,
7220-
none, "global variable cannot be marked potentially "
7221-
"unavailable with '@available' in script mode", ())
7222-
7223-
ERROR(availability_global_script_no_unavailable,
7224-
none, "global variable cannot be marked unavailable "
7225-
"with '@available' in script mode", ())
7226-
72277217
ERROR(availability_stored_property_no_potential,
72287218
none, "stored properties cannot be marked potentially unavailable with "
72297219
"'@available'", ())

include/swift/Parse/Parser.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,8 +1299,7 @@ class Parser {
12991299
StaticSpellingKind StaticSpelling,
13001300
SourceLoc VarLoc,
13011301
bool hasInitializer,
1302-
const DeclAttributes &Attributes,
1303-
SmallVectorImpl<Decl *> &Decls);
1302+
const DeclAttributes &Attributes);
13041303
ParserStatus parseGetEffectSpecifier(ParsedAccessors &accessors,
13051304
SourceLoc &asyncLoc,
13061305
SourceLoc &throwsLoc,

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2620,7 +2620,6 @@ namespace {
26202620
printFlag(VD->isDebuggerVar(), "debugger_var", DeclModifierColor);
26212621
printFlag(VD->isLazyStorageProperty(), "lazy_storage_property",
26222622
DeclModifierColor);
2623-
printFlag(VD->isTopLevelGlobal(), "top_level_global", DeclModifierColor);
26242623
printFlag(VD->isLazyStorageProperty(), "lazy_storage_property",
26252624
DeclModifierColor);
26262625

lib/AST/ASTScope.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ ASTContext &ASTScopeImpl::getASTContext() const {
334334

335335
#pragma mark getSourceFile
336336

337-
const SourceFile *ASTScopeImpl::getSourceFile() const {
337+
SourceFile *ASTScopeImpl::getSourceFile() const {
338338
if (auto sourceFileScope = dyn_cast<ASTSourceFileScope>(this))
339339
return sourceFileScope->SF;
340340

lib/AST/ASTScopeCreation.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,7 @@ class NodeAdder
483483
ASTScopeImpl *visitTopLevelCodeDecl(TopLevelCodeDecl *d,
484484
ASTScopeImpl *p,
485485
ScopeCreator &scopeCreator) {
486-
ASTScopeAssert(endLoc.has_value(), "TopLevelCodeDecl in wrong place?");
487-
return scopeCreator.constructExpandAndInsert<TopLevelCodeScope>(
488-
p, d, *endLoc);
486+
return scopeCreator.constructExpandAndInsert<TopLevelCodeScope>(p, d);
489487
}
490488

491489
#pragma mark special-case creation
@@ -800,10 +798,10 @@ CREATES_NEW_INSERTION_POINT(GuardStmtScope)
800798
CREATES_NEW_INSERTION_POINT(PatternEntryDeclScope)
801799
CREATES_NEW_INSERTION_POINT(GenericTypeOrExtensionScope)
802800
CREATES_NEW_INSERTION_POINT(BraceStmtScope)
803-
CREATES_NEW_INSERTION_POINT(TopLevelCodeScope)
804801
CREATES_NEW_INSERTION_POINT(ConditionalClausePatternUseScope)
805802
CREATES_NEW_INSERTION_POINT(ABIAttributeScope)
806803

804+
NO_NEW_INSERTION_POINT(TopLevelCodeScope)
807805
NO_NEW_INSERTION_POINT(FunctionBodyScope)
808806
NO_NEW_INSERTION_POINT(AbstractFunctionDeclScope)
809807
NO_NEW_INSERTION_POINT(CustomAttributeScope)
@@ -1017,19 +1015,6 @@ BraceStmtScope::expandAScopeThatCreatesANewInsertionPoint(
10171015
"For top-level code decls, need the scope under, say a guard statement."};
10181016
}
10191017

1020-
AnnotatedInsertionPoint
1021-
TopLevelCodeScope::expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &
1022-
scopeCreator) {
1023-
1024-
auto *body =
1025-
scopeCreator
1026-
.addToScopeTreeAndReturnInsertionPoint(decl->getBody(), this, endLoc);
1027-
1028-
return {body, "So next top level code scope and put its decls in its body "
1029-
"under a guard statement scope (etc) from the last top level "
1030-
"code scope"};
1031-
}
1032-
10331018
AnnotatedInsertionPoint
10341019
ABIAttributeScope::expandAScopeThatCreatesANewInsertionPoint(
10351020
ScopeCreator &scopeCreator) {
@@ -1294,6 +1279,12 @@ void CustomAttributeScope::
12941279
}
12951280
}
12961281

1282+
void TopLevelCodeScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
1283+
ScopeCreator &scopeCreator) {
1284+
insertionPoint = scopeCreator.addToScopeTreeAndReturnInsertionPoint(decl->getBody(), this,
1285+
std::nullopt);
1286+
}
1287+
12971288
#pragma mark expandScope
12981289

12991290
ASTScopeImpl *GenericTypeOrExtensionWholePortion::expandScope(

lib/AST/ASTScopeLookup.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,20 @@ ConditionalClauseInitializerScope::getLookupParent() const {
353353
return parent->getLookupParent();
354354
}
355355

356+
NullablePtr<const ASTScopeImpl> TopLevelCodeScope::getLookupParent() const {
357+
auto parent = getParent().get();
358+
if (auto *prev = decl->getPrevious()) {
359+
auto *fileScope = cast<ASTSourceFileScope>(parent);
360+
auto prevScope = fileScope->findChildContaining(prev->getEndLoc(), getSourceManager());
361+
if (auto *prevTopLevel = dyn_cast<TopLevelCodeScope>(prevScope.getPtrOrNull())) {
362+
if (!prevTopLevel->getWasExpanded())
363+
prevTopLevel->expandAndBeCurrent(const_cast<TopLevelCodeScope *>(this)->getScopeCreator());
364+
return prevTopLevel->insertionPoint;
365+
}
366+
}
367+
return parent;
368+
}
369+
356370
#pragma mark looking in locals or members - locals
357371

358372
bool GenericParamScope::lookupLocalsOrMembers(DeclConsumer consumer) const {

lib/AST/ASTScopeSourceRange.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ SourceRange FunctionBodyScope::getSourceRangeOfThisASTNode(
127127

128128
SourceRange TopLevelCodeScope::getSourceRangeOfThisASTNode(
129129
const bool omitAssertions) const {
130-
return SourceRange(decl->getStartLoc(), endLoc);
130+
return decl->getSourceRange();
131131
}
132132

133133
SourceRange SubscriptDeclScope::getSourceRangeOfThisASTNode(

lib/AST/ASTVerifier.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,10 +2642,14 @@ class Verifier : public ASTWalker {
26422642
}
26432643

26442644
void verifyChecked(PatternBindingDecl *binding) {
2645+
auto isTopLevel = isa<TopLevelCodeDecl>(binding->getDeclContext());
26452646
// Look at all of the VarDecls being bound.
26462647
for (auto idx : range(binding->getNumPatternEntries()))
26472648
if (auto *P = binding->getPattern(idx))
26482649
P->forEachVariable([&](VarDecl *VD) {
2650+
auto varIsTopLevel = isa<TopLevelCodeDecl>(VD->getDeclContext());
2651+
ASSERT(isTopLevel == varIsTopLevel &&
2652+
"Must have consistent top-level context");
26492653
// ParamDecls never get PBD's.
26502654
assert(!isa<ParamDecl>(VD) && "ParamDecl has a PatternBindingDecl?");
26512655
});

0 commit comments

Comments
 (0)