Skip to content

Commit c24866b

Browse files
authored
Merge pull request #68245 from xedin/thread-parent-module-through-astscope-5.9
[5.9][AST] ASTScope: Pass parent module to all search methods
2 parents 35a74b7 + ecd2ea3 commit c24866b

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

include/swift/AST/ASTScope.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,18 @@ class ASTScopeImpl : public ASTAllocated<ASTScopeImpl> {
298298

299299
protected:
300300
/// Not const because may reexpand some scopes.
301-
ASTScopeImpl *findInnermostEnclosingScope(SourceLoc,
301+
ASTScopeImpl *findInnermostEnclosingScope(ModuleDecl *,
302+
SourceLoc,
302303
NullablePtr<raw_ostream>);
303-
ASTScopeImpl *findInnermostEnclosingScopeImpl(SourceLoc,
304+
ASTScopeImpl *findInnermostEnclosingScopeImpl(ModuleDecl *,
305+
SourceLoc,
304306
NullablePtr<raw_ostream>,
305307
SourceManager &,
306308
ScopeCreator &);
307309

308310
private:
309-
NullablePtr<ASTScopeImpl> findChildContaining(SourceLoc loc,
311+
NullablePtr<ASTScopeImpl> findChildContaining(ModuleDecl *,
312+
SourceLoc loc,
310313
SourceManager &sourceMgr) const;
311314

312315
#pragma mark - - lookup- per scope

lib/AST/ASTScopeCreation.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ void ASTSourceFileScope::expandFunctionBody(AbstractFunctionDecl *AFD) {
251251
auto sr = AFD->getOriginalBodySourceRange();
252252
if (sr.isInvalid())
253253
return;
254-
ASTScopeImpl *bodyScope = findInnermostEnclosingScope(sr.Start, nullptr);
254+
ASTScopeImpl *bodyScope =
255+
findInnermostEnclosingScope(AFD->getParentModule(), sr.Start, nullptr);
255256
if (!bodyScope->getWasExpanded())
256257
bodyScope->expandAndBeCurrent(*scopeCreator);
257258
}

lib/AST/ASTScopeLookup.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,32 @@ void ASTScopeImpl::unqualifiedLookup(
4848
const ASTScopeImpl *ASTScopeImpl::findStartingScopeForLookup(
4949
SourceFile *sourceFile, const SourceLoc loc) {
5050
auto *const fileScope = sourceFile->getScope().impl;
51-
const auto *innermost = fileScope->findInnermostEnclosingScope(loc, nullptr);
51+
const auto *innermost = fileScope->findInnermostEnclosingScope(
52+
sourceFile->getParentModule(), loc, nullptr);
5253
ASTScopeAssert(innermost->getWasExpanded(),
5354
"If looking in a scope, it must have been expanded.");
5455

5556
return innermost;
5657
}
5758

5859
ASTScopeImpl *
59-
ASTScopeImpl::findInnermostEnclosingScope(SourceLoc loc,
60+
ASTScopeImpl::findInnermostEnclosingScope(ModuleDecl *parentModule,
61+
SourceLoc loc,
6062
NullablePtr<raw_ostream> os) {
61-
return findInnermostEnclosingScopeImpl(loc, os, getSourceManager(),
62-
getScopeCreator());
63+
return findInnermostEnclosingScopeImpl(parentModule, loc, os,
64+
getSourceManager(), getScopeCreator());
6365
}
6466

6567
ASTScopeImpl *ASTScopeImpl::findInnermostEnclosingScopeImpl(
66-
SourceLoc loc, NullablePtr<raw_ostream> os, SourceManager &sourceMgr,
67-
ScopeCreator &scopeCreator) {
68+
ModuleDecl *parentModule, SourceLoc loc, NullablePtr<raw_ostream> os,
69+
SourceManager &sourceMgr, ScopeCreator &scopeCreator) {
6870
if (!getWasExpanded())
6971
expandAndBeCurrent(scopeCreator);
70-
auto child = findChildContaining(loc, sourceMgr);
72+
auto child = findChildContaining(parentModule, loc, sourceMgr);
7173
if (!child)
7274
return this;
73-
return child.get()->findInnermostEnclosingScopeImpl(loc, os, sourceMgr,
74-
scopeCreator);
75+
return child.get()->findInnermostEnclosingScopeImpl(parentModule, loc, os,
76+
sourceMgr, scopeCreator);
7577
}
7678

7779
/// If the \p loc is in a new buffer but \p range is not, consider the location
@@ -89,10 +91,10 @@ static SourceLoc translateLocForReplacedRange(SourceManager &sourceMgr,
8991
}
9092

9193
NullablePtr<ASTScopeImpl>
92-
ASTScopeImpl::findChildContaining(SourceLoc loc,
94+
ASTScopeImpl::findChildContaining(ModuleDecl *parentModule,
95+
SourceLoc loc,
9396
SourceManager &sourceMgr) const {
94-
auto *moduleDecl = this->getSourceFile()->getParentModule();
95-
auto *locSourceFile = moduleDecl->getSourceFileContainingLocation(loc);
97+
auto *locSourceFile = parentModule->getSourceFileContainingLocation(loc);
9698

9799
// Use binary search to find the child that contains this location.
98100
auto *const *child = llvm::lower_bound(
@@ -109,7 +111,7 @@ ASTScopeImpl::findChildContaining(SourceLoc loc,
109111
// Note that `scope->getSourceFile()` returns the root of the source tree,
110112
// not the source file containing the location of the ASTScope.
111113
auto scopeStart = scope->getSourceRangeOfThisASTNode().Start;
112-
auto *scopeSourceFile = moduleDecl->getSourceFileContainingLocation(scopeStart);
114+
auto *scopeSourceFile = parentModule->getSourceFileContainingLocation(scopeStart);
113115

114116
if (scopeSourceFile != locSourceFile) {
115117
// To compare a source location that is possibly inside a macro expansion
@@ -624,7 +626,8 @@ llvm::SmallVector<LabeledStmt *, 4>
624626
ASTScopeImpl::lookupLabeledStmts(SourceFile *sourceFile, SourceLoc loc) {
625627
// Find the innermost scope from which to start our search.
626628
auto *const fileScope = sourceFile->getScope().impl;
627-
const auto *innermost = fileScope->findInnermostEnclosingScope(loc, nullptr);
629+
const auto *innermost = fileScope->findInnermostEnclosingScope(
630+
sourceFile->getParentModule(), loc, nullptr);
628631
ASTScopeAssert(innermost->getWasExpanded(),
629632
"If looking in a scope, it must have been expanded.");
630633

@@ -652,7 +655,8 @@ std::pair<CaseStmt *, CaseStmt *> ASTScopeImpl::lookupFallthroughSourceAndDest(
652655
SourceFile *sourceFile, SourceLoc loc) {
653656
// Find the innermost scope from which to start our search.
654657
auto *const fileScope = sourceFile->getScope().impl;
655-
const auto *innermost = fileScope->findInnermostEnclosingScope(loc, nullptr);
658+
const auto *innermost = fileScope->findInnermostEnclosingScope(
659+
sourceFile->getParentModule(), loc, nullptr);
656660
ASTScopeAssert(innermost->getWasExpanded(),
657661
"If looking in a scope, it must have been expanded.");
658662

@@ -686,7 +690,8 @@ void ASTScopeImpl::lookupEnclosingMacroScope(
686690
return;
687691

688692
auto *fileScope = sourceFile->getScope().impl;
689-
auto *scope = fileScope->findInnermostEnclosingScope(loc, nullptr);
693+
auto *scope = fileScope->findInnermostEnclosingScope(
694+
sourceFile->getParentModule(), loc, nullptr);
690695
do {
691696
auto *freestanding = scope->getFreestandingMacro().getPtrOrNull();
692697
if (freestanding && consume(freestanding))

lib/AST/ASTScopePrinting.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ void ASTScopeImpl::dumpOneScopeMapLocation(
5454

5555
llvm::errs() << "***Scope at " << lineColumn.first << ":" << lineColumn.second
5656
<< "***\n";
57-
auto *locScope = findInnermostEnclosingScope(loc, &llvm::errs());
57+
auto *parentModule = getSourceFile()->getParentModule();
58+
auto *locScope = findInnermostEnclosingScope(parentModule, loc, &llvm::errs());
5859
locScope->print(llvm::errs(), 0, false, false);
5960

6061
namelookup::ASTScopeDeclGatherer gatherer;

0 commit comments

Comments
 (0)