Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions include/swift/AST/ASTScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,18 +327,15 @@ class ASTScopeImpl : public ASTAllocated<ASTScopeImpl> {

protected:
/// Not const because may reexpand some scopes.
ASTScopeImpl *findInnermostEnclosingScope(ModuleDecl *,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity (and laziness) – why was it necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It used to use ModuleDecl::getSourceFileContainingLocation to handle different buffer cases, we have since added an API to SourceManager that handles that (#70126)

SourceLoc,
ASTScopeImpl *findInnermostEnclosingScope(SourceLoc,
NullablePtr<raw_ostream>);
ASTScopeImpl *findInnermostEnclosingScopeImpl(ModuleDecl *,
SourceLoc,
ASTScopeImpl *findInnermostEnclosingScopeImpl(SourceLoc,
NullablePtr<raw_ostream>,
SourceManager &,
ScopeCreator &);

private:
NullablePtr<ASTScopeImpl> findChildContaining(ModuleDecl *,
SourceLoc loc,
NullablePtr<ASTScopeImpl> findChildContaining(SourceLoc loc,
SourceManager &sourceMgr) const;

#pragma mark - - lookup- per scope
Expand Down
3 changes: 1 addition & 2 deletions lib/AST/ASTScopeCreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,7 @@ void ASTSourceFileScope::expandFunctionBody(AbstractFunctionDecl *AFD) {
auto sr = AFD->getOriginalBodySourceRange();
if (sr.isInvalid())
return;
ASTScopeImpl *bodyScope =
findInnermostEnclosingScope(AFD->getParentModule(), sr.Start, nullptr);
ASTScopeImpl *bodyScope = findInnermostEnclosingScope(sr.Start, nullptr);
if (!bodyScope->getWasExpanded())
bodyScope->expandAndBeCurrent(*scopeCreator);
}
Expand Down
38 changes: 15 additions & 23 deletions lib/AST/ASTScopeLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,30 @@ const ASTScopeImpl *ASTScopeImpl::findStartingScopeForLookup(
if (actualSF != sourceFile)
fileScope = actualSF->getScope().impl;

const auto *innermost = fileScope->findInnermostEnclosingScope(
sourceFile->getParentModule(), loc, nullptr);
const auto *innermost = fileScope->findInnermostEnclosingScope(loc, nullptr);
ASTScopeAssert(innermost->getWasExpanded(),
"If looking in a scope, it must have been expanded.");

return innermost;
}

ASTScopeImpl *
ASTScopeImpl::findInnermostEnclosingScope(ModuleDecl *parentModule,
SourceLoc loc,
ASTScopeImpl::findInnermostEnclosingScope(SourceLoc loc,
NullablePtr<raw_ostream> os) {
return findInnermostEnclosingScopeImpl(parentModule, loc, os,
getSourceManager(), getScopeCreator());
return findInnermostEnclosingScopeImpl(loc, os, getSourceManager(),
getScopeCreator());
}

ASTScopeImpl *ASTScopeImpl::findInnermostEnclosingScopeImpl(
ModuleDecl *parentModule, SourceLoc loc, NullablePtr<raw_ostream> os,
SourceManager &sourceMgr, ScopeCreator &scopeCreator) {
SourceLoc loc, NullablePtr<raw_ostream> os, SourceManager &sourceMgr,
ScopeCreator &scopeCreator) {
if (!getWasExpanded())
expandAndBeCurrent(scopeCreator);
auto child = findChildContaining(parentModule, loc, sourceMgr);
auto child = findChildContaining(loc, sourceMgr);
if (!child)
return this;
return child.get()->findInnermostEnclosingScopeImpl(parentModule, loc, os,
sourceMgr, scopeCreator);
return child.get()->findInnermostEnclosingScopeImpl(loc, os, sourceMgr,
scopeCreator);
}

/// If the \p loc is in a new buffer but \p range is not, consider the location
Expand All @@ -111,8 +109,7 @@ static SourceLoc translateLocForReplacedRange(SourceManager &sourceMgr,
}

NullablePtr<ASTScopeImpl>
ASTScopeImpl::findChildContaining(ModuleDecl *parentModule,
SourceLoc loc,
ASTScopeImpl::findChildContaining(SourceLoc loc,
SourceManager &sourceMgr) const {
if (loc.isInvalid())
return nullptr;
Expand Down Expand Up @@ -596,8 +593,7 @@ llvm::SmallVector<LabeledStmt *, 4>
ASTScopeImpl::lookupLabeledStmts(SourceFile *sourceFile, SourceLoc loc) {
// Find the innermost scope from which to start our search.
auto *const fileScope = sourceFile->getScope().impl;
const auto *innermost = fileScope->findInnermostEnclosingScope(
sourceFile->getParentModule(), loc, nullptr);
const auto *innermost = fileScope->findInnermostEnclosingScope(loc, nullptr);
ASTScopeAssert(innermost->getWasExpanded(),
"If looking in a scope, it must have been expanded.");

Expand Down Expand Up @@ -625,8 +621,7 @@ std::pair<CaseStmt *, CaseStmt *> ASTScopeImpl::lookupFallthroughSourceAndDest(
SourceFile *sourceFile, SourceLoc loc) {
// Find the innermost scope from which to start our search.
auto *const fileScope = sourceFile->getScope().impl;
const auto *innermost = fileScope->findInnermostEnclosingScope(
sourceFile->getParentModule(), loc, nullptr);
const auto *innermost = fileScope->findInnermostEnclosingScope(loc, nullptr);
ASTScopeAssert(innermost->getWasExpanded(),
"If looking in a scope, it must have been expanded.");

Expand Down Expand Up @@ -660,8 +655,7 @@ void ASTScopeImpl::lookupEnclosingMacroScope(
return;

auto *fileScope = sourceFile->getScope().impl;
auto *scope = fileScope->findInnermostEnclosingScope(
sourceFile->getParentModule(), loc, nullptr);
auto *scope = fileScope->findInnermostEnclosingScope(loc, nullptr);
do {
if (auto expansionScope = dyn_cast<MacroExpansionDeclScope>(scope)) {
auto *expansionDecl = expansionScope->decl;
Expand Down Expand Up @@ -692,8 +686,7 @@ lookupEnclosingABIAttributeScope(SourceFile *sourceFile, SourceLoc loc) {
return nullptr;

auto *fileScope = sourceFile->getScope().impl;
auto *scope = fileScope->findInnermostEnclosingScope(
sourceFile->getParentModule(), loc, nullptr);
auto *scope = fileScope->findInnermostEnclosingScope(loc, nullptr);
do {
if (auto abiAttrScope = dyn_cast<ABIAttributeScope>(scope)) {
return abiAttrScope->attr;
Expand Down Expand Up @@ -744,8 +737,7 @@ CatchNode ASTScopeImpl::lookupCatchNode(ModuleDecl *module, SourceLoc loc) {
return nullptr;

auto *fileScope = sourceFile->getScope().impl;
const auto *innermost = fileScope->findInnermostEnclosingScope(
module, loc, nullptr);
const auto *innermost = fileScope->findInnermostEnclosingScope(loc, nullptr);
ASTScopeAssert(innermost->getWasExpanded(),
"If looking in a scope, it must have been expanded.");

Expand Down
3 changes: 1 addition & 2 deletions lib/AST/ASTScopePrinting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ void ASTScopeImpl::dumpOneScopeMapLocation(

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

namelookup::ASTScopeDeclGatherer gatherer;
Expand Down