Skip to content

Commit 048b335

Browse files
committed
ASTScope: Remove re-expansion mechanism
1 parent c4314eb commit 048b335

File tree

2 files changed

+3
-164
lines changed

2 files changed

+3
-164
lines changed

include/swift/AST/ASTScope.h

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,6 @@ class ASTScopeImpl {
146146

147147
bool wasExpanded = false;
148148

149-
/// For use-before-def, ASTAncestor scopes may be added to a BraceStmt.
150-
unsigned astAncestorScopeCount = 0;
151-
152149
/// Can clear storedChildren, so must remember this
153150
bool haveAddedCleanup = false;
154151

@@ -192,26 +189,10 @@ class ASTScopeImpl {
192189

193190
const Children &getChildren() const { return storedChildren; }
194191

195-
/// Get ride of descendants and remove them from scopedNodes so the scopes
196-
/// can be recreated. Needed because typechecking inserts a return statment
197-
/// into initializers.
198-
void disownDescendants(ScopeCreator &);
199-
200-
public: // for addReusedBodyScopes
192+
public:
201193
void addChild(ASTScopeImpl *child, ASTContext &);
202-
std::vector<ASTScopeImpl *> rescueASTAncestorScopesForReuseFromMe();
203-
204-
/// When reexpanding, do we always create a new body?
205-
virtual NullablePtr<ASTScopeImpl> getParentOfASTAncestorScopesToBeRescued();
206-
std::vector<ASTScopeImpl *>
207-
rescueASTAncestorScopesForReuseFromMeOrDescendants();
208-
void replaceASTAncestorScopes(ArrayRef<ASTScopeImpl *>);
209194

210195
private:
211-
void removeChildren();
212-
213-
private:
214-
void emancipate() { parent = nullptr; }
215196
NullablePtr<ASTScopeImpl> getPriorSibling() const;
216197

217198
public:
@@ -278,12 +259,6 @@ class ASTScopeImpl {
278259
private:
279260
void clearCachedSourceRangesOfMeAndAncestors();
280261

281-
public:
282-
/// Since source ranges are cached but depend on child ranges,
283-
/// when descendants are added, my and my ancestor ranges must be
284-
/// recalculated.
285-
void ensureSourceRangesAreCorrectWhenAddingDescendants(function_ref<void()>);
286-
287262
public: // public for debugging
288263
/// Returns source range of this node alone, without factoring in any
289264
/// children.
@@ -349,23 +324,11 @@ class ASTScopeImpl {
349324
/// fact, above and beyond adding Decls to the SourceFile.
350325
ASTScopeImpl *expandAndBeCurrent(ScopeCreator &);
351326

352-
unsigned getASTAncestorScopeCount() const { return astAncestorScopeCount; }
353327
bool getWasExpanded() const { return wasExpanded; }
354328

355329
protected:
356-
void resetASTAncestorScopeCount() { astAncestorScopeCount = 0; }
357-
void increaseASTAncestorScopeCount(unsigned c) { astAncestorScopeCount += c; }
358330
void setWasExpanded() { wasExpanded = true; }
359331
virtual ASTScopeImpl *expandSpecifically(ScopeCreator &) = 0;
360-
virtual void beCurrent();
361-
virtual bool doesExpansionOnlyAddNewDeclsAtEnd() const;
362-
363-
public:
364-
bool isExpansionNeeded(const ScopeCreator &) const;
365-
366-
protected:
367-
bool isCurrent() const;
368-
virtual bool isCurrentIfWasExpanded() const;
369332

370333
private:
371334
/// Compare the pre-expasion range with the post-expansion range and return
@@ -382,13 +345,6 @@ class ASTScopeImpl {
382345
virtual SourceRange sourceRangeForDeferredExpansion() const;
383346

384347
public:
385-
// Some nodes (VarDecls and Accessors) are created directly from
386-
// pattern scope code and should neither be deferred nor should
387-
// contribute to widenSourceRangeForIgnoredASTNode.
388-
// Closures and captures are also created directly but are
389-
// screened out because they are expressions.
390-
static bool isHandledSpeciallyByPatterns(const ASTNode n);
391-
392348
virtual NullablePtr<AbstractStorageDecl>
393349
getEnclosingAbstractStorageDecl() const;
394350

@@ -554,7 +510,6 @@ class ASTSourceFileScope final : public ASTScopeImpl {
554510

555511
protected:
556512
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
557-
bool doesExpansionOnlyAddNewDeclsAtEnd() const override;
558513

559514
ScopeCreator &getScopeCreator() override;
560515

@@ -997,8 +952,6 @@ class AbstractFunctionBodyScope : public ASTScopeImpl {
997952
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
998953
Decl *getDecl() const { return decl; }
999954

1000-
NullablePtr<ASTScopeImpl> getParentOfASTAncestorScopesToBeRescued() override;
1001-
1002955
protected:
1003956
bool lookupLocalsOrMembers(DeclConsumer) const override;
1004957

@@ -1325,8 +1278,6 @@ class TopLevelCodeScope final : public ASTScopeImpl {
13251278
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
13261279
Decl *getDecl() const { return decl; }
13271280
NullablePtr<const void> getReferrent() const override;
1328-
1329-
NullablePtr<ASTScopeImpl> getParentOfASTAncestorScopesToBeRescued() override;
13301281
};
13311282

13321283
/// The \c _@specialize attribute.

lib/AST/ASTScopeCreation.cpp

Lines changed: 2 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,8 @@ class ScopeCreator final {
229229
ArrayRef<ASTNode> nodesOrDeclsToAdd) {
230230
auto *ip = insertionPoint;
231231
for (auto nd : sortBySourceRange(cull(nodesOrDeclsToAdd))) {
232-
const unsigned preCount = ip->getChildren().size();
233232
auto *const newIP =
234233
addToScopeTreeAndReturnInsertionPoint(nd, ip).getPtrOr(ip);
235-
if (ip != organicInsertionPoint)
236-
ip->increaseASTAncestorScopeCount(ip->getChildren().size() -
237-
preCount);
238234
ip = newIP;
239235
}
240236
return ip;
@@ -923,20 +919,6 @@ void ASTScopeImpl::addChild(ASTScopeImpl *child, ASTContext &ctx) {
923919
clearCachedSourceRangesOfMeAndAncestors();
924920
}
925921

926-
void ASTScopeImpl::removeChildren() {
927-
clearCachedSourceRangesOfMeAndAncestors();
928-
storedChildren.clear();
929-
}
930-
931-
void ASTScopeImpl::disownDescendants(ScopeCreator &scopeCreator) {
932-
for (auto *c : getChildren()) {
933-
c->disownDescendants(scopeCreator);
934-
c->emancipate();
935-
scopeCreator.scopedNodes.erase(c);
936-
}
937-
removeChildren();
938-
}
939-
940922
#pragma mark implementations of expansion
941923

942924
ASTScopeImpl *
@@ -955,26 +937,7 @@ ExpandASTScopeRequest::evaluate(Evaluator &evaluator, ASTScopeImpl *parent,
955937
return insertionPoint;
956938
}
957939

958-
bool ASTScopeImpl::doesExpansionOnlyAddNewDeclsAtEnd() const { return false; }
959-
bool ASTSourceFileScope::doesExpansionOnlyAddNewDeclsAtEnd() const {
960-
return true;
961-
}
962-
963940
ASTScopeImpl *ASTScopeImpl::expandAndBeCurrent(ScopeCreator &scopeCreator) {
964-
965-
// We might be reexpanding, so save any scopes that were inserted here from
966-
// above it in the AST
967-
auto astAncestorScopes = rescueASTAncestorScopesForReuseFromMeOrDescendants();
968-
ASTScopeAssert(astAncestorScopes.empty() ||
969-
!doesExpansionOnlyAddNewDeclsAtEnd(),
970-
"ASTSourceFileScope has no ancestors to be rescued.");
971-
972-
// If reexpanding, we need to remove descendant decls from the duplication set
973-
// in order to re-add them as sub-scopes. Since expansion only adds new Decls
974-
// at end, don't bother with descendants
975-
if (!doesExpansionOnlyAddNewDeclsAtEnd())
976-
disownDescendants(scopeCreator);
977-
978941
auto *insertionPoint = expandSpecifically(scopeCreator);
979942
ASTScopeAssert(!insertionPointForDeferredExpansion() ||
980943
insertionPointForDeferredExpansion().get() ==
@@ -983,9 +946,8 @@ ASTScopeImpl *ASTScopeImpl::expandAndBeCurrent(ScopeCreator &scopeCreator) {
983946
"accurate before expansion, the insertion point before "
984947
"expansion must be the same as after expansion.");
985948

986-
replaceASTAncestorScopes(astAncestorScopes);
987949
setWasExpanded();
988-
beCurrent();
950+
989951
ASTScopeAssert(checkSourceRangeAfterExpansion(scopeCreator.getASTContext()),
990952
"Bad range.");
991953
return insertionPoint;
@@ -1675,79 +1637,6 @@ IterableTypeBodyPortion::insertionPointForDeferredExpansion(
16751637
return s->getParent().get();
16761638
}
16771639

1678-
bool ASTScopeImpl::isExpansionNeeded(const ScopeCreator &scopeCreator) const {
1679-
return !isCurrent() ||
1680-
scopeCreator.getASTContext().LangOpts.StressASTScopeLookup;
1681-
}
1682-
1683-
bool ASTScopeImpl::isCurrent() const {
1684-
return getWasExpanded() && isCurrentIfWasExpanded();
1685-
}
1686-
1687-
void ASTScopeImpl::beCurrent() {}
1688-
bool ASTScopeImpl::isCurrentIfWasExpanded() const { return true; }
1689-
1690-
#pragma mark getParentOfASTAncestorScopesToBeRescued
1691-
NullablePtr<ASTScopeImpl>
1692-
ASTScopeImpl::getParentOfASTAncestorScopesToBeRescued() {
1693-
return this;
1694-
}
1695-
NullablePtr<ASTScopeImpl>
1696-
AbstractFunctionBodyScope::getParentOfASTAncestorScopesToBeRescued() {
1697-
// Reexpansion always creates a new body as the first child
1698-
// That body contains the scopes to be rescued.
1699-
return getChildren().empty() ? nullptr : getChildren().front();
1700-
}
1701-
NullablePtr<ASTScopeImpl>
1702-
TopLevelCodeScope::getParentOfASTAncestorScopesToBeRescued() {
1703-
// Reexpansion always creates a new body as the first child
1704-
// That body contains the scopes to be rescued.
1705-
return getChildren().empty() ? nullptr : getChildren().front();
1706-
}
1707-
1708-
#pragma mark rescuing & reusing
1709-
std::vector<ASTScopeImpl *>
1710-
ASTScopeImpl::rescueASTAncestorScopesForReuseFromMeOrDescendants() {
1711-
if (auto *p = getParentOfASTAncestorScopesToBeRescued().getPtrOrNull()) {
1712-
return p->rescueASTAncestorScopesForReuseFromMe();
1713-
}
1714-
ASTScopeAssert(
1715-
getASTAncestorScopeCount() == 0,
1716-
"If receives ASTAncestor scopes, must know where to find parent");
1717-
return {};
1718-
}
1719-
1720-
void ASTScopeImpl::replaceASTAncestorScopes(
1721-
ArrayRef<ASTScopeImpl *> scopesToAdd) {
1722-
auto *p = getParentOfASTAncestorScopesToBeRescued().getPtrOrNull();
1723-
if (!p) {
1724-
ASTScopeAssert(scopesToAdd.empty(), "Non-empty body disappeared?!");
1725-
return;
1726-
}
1727-
auto &ctx = getASTContext();
1728-
for (auto *s : scopesToAdd) {
1729-
p->addChild(s, ctx);
1730-
ASTScopeAssert(s->verifyThatThisNodeComeAfterItsPriorSibling(),
1731-
"Ensure search will work");
1732-
}
1733-
p->increaseASTAncestorScopeCount(scopesToAdd.size());
1734-
}
1735-
1736-
std::vector<ASTScopeImpl *>
1737-
ASTScopeImpl::rescueASTAncestorScopesForReuseFromMe() {
1738-
std::vector<ASTScopeImpl *> astAncestorScopes;
1739-
for (unsigned i = getChildren().size() - getASTAncestorScopeCount();
1740-
i < getChildren().size(); ++i)
1741-
astAncestorScopes.push_back(getChildren()[i]);
1742-
// So they don't get disowned and children cleared.
1743-
for (unsigned i = 0; i < getASTAncestorScopeCount(); ++i) {
1744-
storedChildren.back()->emancipate();
1745-
storedChildren.pop_back();
1746-
}
1747-
resetASTAncestorScopeCount();
1748-
return astAncestorScopes;
1749-
}
1750-
17511640
#pragma mark verification
17521641

17531642
namespace {
@@ -1830,8 +1719,7 @@ void ast_scope::simple_display(llvm::raw_ostream &out,
18301719

18311720
bool ExpandASTScopeRequest::isCached() const {
18321721
ASTScopeImpl *scope = std::get<0>(getStorage());
1833-
ScopeCreator *scopeCreator = std::get<1>(getStorage());
1834-
return !scope->isExpansionNeeded(*scopeCreator);
1722+
return scope->getWasExpanded();
18351723
}
18361724

18371725
Optional<ASTScopeImpl *> ExpandASTScopeRequest::getCachedResult() const {

0 commit comments

Comments
 (0)