Skip to content

Commit 0fa84c1

Browse files
committed
ASTScope: Remove the ExpandASTScopeRequest
This doesn't really fit the request evaluator model since the result of evaluating the request is the new insertion point, but we don't have a way to get the insertion point of an already-expanded scope. Instead, let's change the callers of the now-removed expandAndBeCurrentDetectingRecursion() to instead call expandAndBeCurrent(), while checking first if the scope was already expanded. Also, set the expanded flag before calling expandSpecifically() from inside expandAndBeCurrent(), to ensure that re-entrant calls to expandAndBeCurrent() are flagged by the existing assertion there. Finally, replace a couple of existing counters, and the now-gone request counter with a single ASTScopeExpansions counter to track expansions.
1 parent e985d97 commit 0fa84c1

File tree

6 files changed

+22
-102
lines changed

6 files changed

+22
-102
lines changed

include/swift/AST/ASTScope.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,7 @@ class ASTScopeImpl {
247247

248248
#pragma mark - Scope tree creation
249249
public:
250-
/// expandScope me, sending deferred nodes to my descendants.
251-
/// Return the scope into which to place subsequent decls
252-
ASTScopeImpl *expandAndBeCurrentDetectingRecursion(ScopeCreator &);
253-
254-
/// Expand or reexpand the scope if unexpanded or if not current.
255-
/// There are several places in the compiler that mutate the AST after the
256-
/// fact, above and beyond adding Decls to the SourceFile.
250+
/// Expand the scope. Asserts if it was already expanded.
257251
ASTScopeImpl *expandAndBeCurrent(ScopeCreator &);
258252

259253
bool getWasExpanded() const { return wasExpanded; }
@@ -629,8 +623,6 @@ class IterableTypeScope : public GenericTypeScope {
629623

630624
public:
631625
NullablePtr<ASTScopeImpl> insertionPointForDeferredExpansion() override;
632-
633-
void countBodies(ScopeCreator &) const;
634626
};
635627

636628
class NominalTypeScope final : public IterableTypeScope {

include/swift/AST/NameLookupRequests.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -350,30 +350,6 @@ class GenericParamListRequest :
350350
void cacheResult(GenericParamList *value) const;
351351
};
352352

353-
/// Expand the given ASTScope. Requestified to detect recursion.
354-
class ExpandASTScopeRequest
355-
: public SimpleRequest<ExpandASTScopeRequest,
356-
ast_scope::ASTScopeImpl *(ast_scope::ASTScopeImpl *,
357-
ast_scope::ScopeCreator *),
358-
RequestFlags::SeparatelyCached> {
359-
public:
360-
using SimpleRequest::SimpleRequest;
361-
362-
private:
363-
friend SimpleRequest;
364-
365-
// Evaluation.
366-
ast_scope::ASTScopeImpl *
367-
evaluate(Evaluator &evaluator, ast_scope::ASTScopeImpl *,
368-
ast_scope::ScopeCreator *) const;
369-
370-
public:
371-
// Separate caching.
372-
bool isCached() const;
373-
Optional<ast_scope::ASTScopeImpl *> getCachedResult() const;
374-
void cacheResult(ast_scope::ASTScopeImpl *) const {}
375-
};
376-
377353
/// The input type for an unqualified lookup request.
378354
class UnqualifiedLookupDescriptor {
379355
using LookupOptions = OptionSet<UnqualifiedLookupFlags>;

include/swift/AST/NameLookupTypeIDZone.def

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ SWIFT_REQUEST(NameLookup, DirectOperatorLookupRequest,
3131
SWIFT_REQUEST(NameLookup, DirectPrecedenceGroupLookupRequest,
3232
TinyPtrVector<PrecedenceGroupDecl *>(OperatorLookupDescriptor),
3333
Uncached, NoLocationInfo)
34-
SWIFT_REQUEST(NameLookup, ExpandASTScopeRequest,
35-
ast_scope::ASTScopeImpl* (ast_scope::ASTScopeImpl*, ast_scope::ScopeCreator*),
36-
SeparatelyCached,
37-
NoLocationInfo)
3834
SWIFT_REQUEST(NameLookup, ExtendedNominalRequest,
3935
NominalTypeDecl *(ExtensionDecl *), SeparatelyCached,
4036
NoLocationInfo)

include/swift/Basic/Statistics.def

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,12 @@ FRONTEND_STATISTIC(AST, NumModuleLookupValue)
144144
/// AnyObject lookup.
145145
FRONTEND_STATISTIC(AST, NumModuleLookupClassMember)
146146

147-
/// Number of body scopes for iterable types
148-
FRONTEND_STATISTIC(AST, NumIterableTypeBodyASTScopes)
149-
150-
/// Number of expansions of body scopes for iterable types
151-
FRONTEND_STATISTIC(AST, NumIterableTypeBodyASTScopeExpansions)
152-
153-
/// Number of brace statment scopes for iterable types
154-
FRONTEND_STATISTIC(AST, NumBraceStmtASTScopes)
155-
156-
/// Number of expansions of brace statement scopes for iterable types
157-
FRONTEND_STATISTIC(AST, NumBraceStmtASTScopeExpansions)
158-
159147
/// Number of ASTScope lookups
160148
FRONTEND_STATISTIC(AST, NumASTScopeLookups)
161149

150+
/// Number of ASTScope expansions
151+
FRONTEND_STATISTIC(AST, NumASTScopeExpansions)
152+
162153
/// Number of lookups of the cached import graph for a module or
163154
/// source file.
164155
FRONTEND_STATISTIC(AST, ImportSetFoldHit)

lib/AST/ASTScopeCreation.cpp

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ class ScopeCreator final {
8282
if (auto *ip = child->insertionPointForDeferredExpansion().getPtrOrNull())
8383
return ip;
8484

85-
ASTScopeImpl *insertionPoint =
86-
child->expandAndBeCurrentDetectingRecursion(*this);
85+
ASTScopeImpl *insertionPoint = child->expandAndBeCurrent(*this);
8786
return insertionPoint;
8887
}
8988

@@ -224,15 +223,18 @@ ASTSourceFileScope *ASTScope::createScopeTree(SourceFile *SF) {
224223
}
225224

226225
void ASTSourceFileScope::buildFullyExpandedTree() {
227-
expandAndBeCurrentDetectingRecursion(*scopeCreator);
226+
if (!getWasExpanded())
227+
expandAndBeCurrent(*scopeCreator);
228228
preOrderChildrenDo([&](ASTScopeImpl *s) {
229-
s->expandAndBeCurrentDetectingRecursion(*scopeCreator);
229+
if (!s->getWasExpanded())
230+
s->expandAndBeCurrent(*scopeCreator);
230231
});
231232
}
232233

233234
void ASTSourceFileScope::
234235
buildEnoughOfTreeForTopLevelExpressionsButDontRequestGenericsOrExtendedNominals() {
235-
expandAndBeCurrentDetectingRecursion(*scopeCreator);
236+
if (!getWasExpanded())
237+
expandAndBeCurrent(*scopeCreator);
236238
}
237239

238240
void ASTSourceFileScope::expandFunctionBody(AbstractFunctionDecl *AFD) {
@@ -242,7 +244,8 @@ void ASTSourceFileScope::expandFunctionBody(AbstractFunctionDecl *AFD) {
242244
if (sr.isInvalid())
243245
return;
244246
ASTScopeImpl *bodyScope = findInnermostEnclosingScope(sr.Start, nullptr);
245-
bodyScope->expandAndBeCurrentDetectingRecursion(*scopeCreator);
247+
if (!bodyScope->getWasExpanded())
248+
bodyScope->expandAndBeCurrent(*scopeCreator);
246249
}
247250

248251
ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
@@ -411,8 +414,6 @@ class NodeAdder
411414
endLocForBraceStmt = *endLoc;
412415

413416
ASTContext &ctx = scopeCreator.getASTContext();
414-
if (auto *s = ctx.Stats)
415-
++s->getFrontendCounters().NumBraceStmtASTScopes;
416417

417418
return
418419
scopeCreator.constructExpandAndInsert<BraceStmtScope>(
@@ -596,26 +597,17 @@ void ASTScopeImpl::addChild(ASTScopeImpl *child, ASTContext &ctx) {
596597

597598
#pragma mark implementations of expansion
598599

599-
ASTScopeImpl *
600-
ASTScopeImpl::expandAndBeCurrentDetectingRecursion(ScopeCreator &scopeCreator) {
601-
return evaluateOrDefault(scopeCreator.getASTContext().evaluator,
602-
ExpandASTScopeRequest{this, &scopeCreator}, nullptr);
603-
}
604-
605-
ASTScopeImpl *
606-
ExpandASTScopeRequest::evaluate(Evaluator &evaluator, ASTScopeImpl *parent,
607-
ScopeCreator *scopeCreator) const {
608-
auto *insertionPoint = parent->expandAndBeCurrent(*scopeCreator);
609-
ASTScopeAssert(insertionPoint,
610-
"Used to return a null pointer if the insertion point would "
611-
"not be used, but it breaks the request dependency hashing");
612-
return insertionPoint;
613-
}
614-
615600
ASTScopeImpl *ASTScopeImpl::expandAndBeCurrent(ScopeCreator &scopeCreator) {
616601
ASTScopeAssert(!getWasExpanded(),
617602
"Cannot expand the same scope twice");
618603

604+
// Set the flag before we actually expand, to detect re-entrant calls
605+
// via the above assertion.
606+
setWasExpanded();
607+
608+
if (auto *s = scopeCreator.getASTContext().Stats)
609+
++s->getFrontendCounters().NumASTScopeExpansions;
610+
619611
auto *insertionPoint = expandSpecifically(scopeCreator);
620612
ASTScopeAssert(!insertionPointForDeferredExpansion() ||
621613
insertionPointForDeferredExpansion().get() ==
@@ -624,8 +616,6 @@ ASTScopeImpl *ASTScopeImpl::expandAndBeCurrent(ScopeCreator &scopeCreator) {
624616
"accurate before expansion, the insertion point before "
625617
"expansion must be the same as after expansion.");
626618

627-
setWasExpanded();
628-
629619
return insertionPoint;
630620
}
631621

@@ -826,9 +816,6 @@ BraceStmtScope::expandAScopeThatCreatesANewInsertionPoint(
826816
nd, insertionPoint, endLoc);
827817
}
828818

829-
if (auto *s = scopeCreator.getASTContext().Stats)
830-
++s->getFrontendCounters().NumBraceStmtASTScopeExpansions;
831-
832819
return {
833820
insertionPoint,
834821
"For top-level code decls, need the scope under, say a guard statment."};
@@ -1085,24 +1072,17 @@ ASTScopeImpl *GenericTypeOrExtensionWherePortion::expandScope(
10851072

10861073
#pragma mark createBodyScope
10871074

1088-
void IterableTypeScope::countBodies(ScopeCreator &scopeCreator) const {
1089-
if (auto *s = scopeCreator.getASTContext().Stats)
1090-
++s->getFrontendCounters().NumIterableTypeBodyASTScopes;
1091-
}
1092-
10931075
void ExtensionScope::createBodyScope(ASTScopeImpl *leaf,
10941076
ScopeCreator &scopeCreator) {
10951077
scopeCreator.constructWithPortionExpandAndInsert<ExtensionScope,
10961078
IterableTypeBodyPortion>(
10971079
leaf, decl);
1098-
countBodies(scopeCreator);
10991080
}
11001081
void NominalTypeScope::createBodyScope(ASTScopeImpl *leaf,
11011082
ScopeCreator &scopeCreator) {
11021083
scopeCreator.constructWithPortionExpandAndInsert<NominalTypeScope,
11031084
IterableTypeBodyPortion>(
11041085
leaf, decl);
1105-
countBodies(scopeCreator);
11061086
}
11071087

11081088
#pragma mark createTrailingWhereClauseScope
@@ -1192,9 +1172,6 @@ void GenericTypeOrExtensionScope::expandBody(ScopeCreator &) {}
11921172
void IterableTypeScope::expandBody(ScopeCreator &scopeCreator) {
11931173
for (auto *d : getIterableDeclContext().get()->getMembers())
11941174
scopeCreator.addToScopeTree(ASTNode(d), this);
1195-
1196-
if (auto *s = scopeCreator.getASTContext().Stats)
1197-
++s->getFrontendCounters().NumIterableTypeBodyASTScopeExpansions;
11981175
}
11991176

12001177
#pragma mark getScopeCreator
@@ -1241,16 +1218,3 @@ void ast_scope::simple_display(llvm::raw_ostream &out,
12411218
const ScopeCreator *scopeCreator) {
12421219
scopeCreator->print(out);
12431220
}
1244-
1245-
//----------------------------------------------------------------------------//
1246-
// ExpandASTScopeRequest computation.
1247-
//----------------------------------------------------------------------------//
1248-
1249-
bool ExpandASTScopeRequest::isCached() const {
1250-
ASTScopeImpl *scope = std::get<0>(getStorage());
1251-
return scope->getWasExpanded();
1252-
}
1253-
1254-
Optional<ASTScopeImpl *> ExpandASTScopeRequest::getCachedResult() const {
1255-
return std::get<0>(getStorage());
1256-
}

lib/AST/ASTScopeLookup.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ ASTScopeImpl::findInnermostEnclosingScope(SourceLoc loc,
6464
ASTScopeImpl *ASTScopeImpl::findInnermostEnclosingScopeImpl(
6565
SourceLoc loc, NullablePtr<raw_ostream> os, SourceManager &sourceMgr,
6666
ScopeCreator &scopeCreator) {
67-
expandAndBeCurrentDetectingRecursion(scopeCreator);
67+
if (!getWasExpanded())
68+
expandAndBeCurrent(scopeCreator);
6869
auto child = findChildContaining(loc, sourceMgr);
6970
if (!child)
7071
return this;

0 commit comments

Comments
 (0)