Skip to content

Commit 5c2171c

Browse files
committed
[ASTScope] Eliminate ASTScopeImpl::getCatchNodeBody() and centralize
The implementation of "lookup the catch node" is scattered throughout various `ASTScopeImpl` subclasses. Remove that, and instead centralize the code to find the innermost catch scope.
1 parent 519e3e8 commit 5c2171c

File tree

3 files changed

+41
-75
lines changed

3 files changed

+41
-75
lines changed

include/swift/AST/ASTScope.h

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,8 @@ class ASTScopeImpl : public ASTAllocated<ASTScopeImpl> {
190190
return parentAndWasExpanded.getPointer();
191191
}
192192

193-
const Children &getChildren() const { return storedChildren; }
194-
195193
public:
194+
const Children &getChildren() const { return storedChildren; }
196195
void addChild(ASTScopeImpl *child, ASTContext &);
197196

198197
public:
@@ -235,7 +234,6 @@ class ASTScopeImpl : public ASTAllocated<ASTScopeImpl> {
235234
ScopeKind getKind() const { return kind; }
236235

237236
virtual NullablePtr<AbstractClosureExpr> getClosureIfClosureScope() const;
238-
virtual NullablePtr<const BraceStmtScope> getAsBraceStmtScope() const;
239237
ASTContext &getASTContext() const;
240238
NullablePtr<Decl> getDeclIfAny() const;
241239
NullablePtr<Stmt> getStmtIfAny() const;
@@ -312,12 +310,6 @@ class ASTScopeImpl : public ASTAllocated<ASTScopeImpl> {
312310
/// compact scope tree in the debug info.
313311
virtual bool ignoreInDebugInfo() const { return false; }
314312

315-
/// If this scope node represents a potential catch node, return body the
316-
/// AST node describing the catch (a function, closure, or do...catch) and
317-
/// the node of it's "body", i.e., the brace statement from which errors
318-
/// thrown will be caught by that node.
319-
virtual std::pair<CatchNode, const BraceStmtScope *> getCatchNodeBody() const;
320-
321313
#pragma mark - - lookup- starting point
322314
private:
323315
static const ASTScopeImpl *findStartingScopeForLookup(SourceFile *,
@@ -896,8 +888,6 @@ class FunctionBodyScope : public ASTScopeImpl {
896888
Decl *getDecl() const { return decl; }
897889
bool ignoreInDebugInfo() const override { return true; }
898890

899-
std::pair<CatchNode, const BraceStmtScope *> getCatchNodeBody() const override;
900-
901891
protected:
902892
bool lookupLocalsOrMembers(DeclConsumer) const override;
903893

@@ -1174,8 +1164,6 @@ class ClosureParametersScope final : public ASTScopeImpl {
11741164
NullablePtr<AbstractClosureExpr> getClosureIfClosureScope() const override {
11751165
return closureExpr;
11761166
}
1177-
std::pair<CatchNode, const BraceStmtScope *> getCatchNodeBody() const override;
1178-
11791167
Expr *getExpr() const { return closureExpr; }
11801168
bool ignoreInDebugInfo() const override { return true; }
11811169

@@ -1610,8 +1598,6 @@ class DoCatchStmtScope final : public AbstractStmtScope {
16101598
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
16111599

16121600
public:
1613-
std::pair<CatchNode, const BraceStmtScope *> getCatchNodeBody() const override;
1614-
16151601
Stmt *getStmt() const override { return stmt; }
16161602

16171603
static bool classof(const ASTScopeImpl *scope) {
@@ -1848,9 +1834,7 @@ class BraceStmtScope final : public AbstractStmtScope {
18481834
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
18491835

18501836
NullablePtr<AbstractClosureExpr> parentClosureIfAny() const; // public??
1851-
Stmt *getStmt() const override { return stmt; }
1852-
1853-
NullablePtr<const BraceStmtScope> getAsBraceStmtScope() const override;
1837+
BraceStmt *getStmt() const override { return stmt; }
18541838

18551839
protected:
18561840
bool lookupLocalsOrMembers(DeclConsumer) const override;

lib/AST/ASTScope.cpp

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -101,65 +101,10 @@ NullablePtr<AbstractClosureExpr> BraceStmtScope::parentClosureIfAny() const {
101101
return !getParent() ? nullptr : getParent().get()->getClosureIfClosureScope();
102102
}
103103

104-
NullablePtr<const BraceStmtScope> BraceStmtScope::getAsBraceStmtScope() const {
105-
return this;
106-
}
107-
108104
NullablePtr<AbstractClosureExpr> ASTScopeImpl::getClosureIfClosureScope() const {
109105
return nullptr;
110106
}
111107

112-
NullablePtr<const BraceStmtScope> ASTScopeImpl::getAsBraceStmtScope() const {
113-
return nullptr;
114-
}
115-
116-
std::pair<CatchNode, const BraceStmtScope *>
117-
ASTScopeImpl::getCatchNodeBody() const {
118-
return { nullptr, nullptr };
119-
}
120-
121-
std::pair<CatchNode, const BraceStmtScope *>
122-
ClosureParametersScope::getCatchNodeBody() const {
123-
const BraceStmtScope *body = nullptr;
124-
const auto &children = getChildren();
125-
if (!children.empty()) {
126-
body = children[0]->getAsBraceStmtScope().getPtrOrNull();
127-
assert(body && "Not a brace statement?");
128-
}
129-
return { const_cast<AbstractClosureExpr *>(closureExpr), body };
130-
}
131-
132-
std::pair<CatchNode, const BraceStmtScope *>
133-
FunctionBodyScope::getCatchNodeBody() const {
134-
const BraceStmtScope *body = nullptr;
135-
const auto &children = getChildren();
136-
if (!children.empty()) {
137-
body = children[0]->getAsBraceStmtScope().getPtrOrNull();
138-
assert(body && "Not a brace statement?");
139-
}
140-
return { const_cast<AbstractFunctionDecl *>(decl), body };
141-
}
142-
143-
/// Determine whether this is an empty brace statement, which doesn't have a
144-
/// node associated with it.
145-
static bool isEmptyBraceStmt(Stmt *stmt) {
146-
if (auto braceStmt = dyn_cast_or_null<BraceStmt>(stmt))
147-
return braceStmt->empty();
148-
149-
return false;
150-
}
151-
152-
std::pair<CatchNode, const BraceStmtScope *>
153-
DoCatchStmtScope::getCatchNodeBody() const {
154-
const BraceStmtScope *body = nullptr;
155-
const auto &children = getChildren();
156-
if (!children.empty() && !isEmptyBraceStmt(stmt->getBody())) {
157-
body = children[0]->getAsBraceStmtScope().getPtrOrNull();
158-
assert(body && "Not a brace statement?");
159-
}
160-
return { const_cast<DoCatchStmt *>(stmt), body };
161-
}
162-
163108
std::string ASTScopeImpl::getClassName() const {
164109
// GenericTypeOrExtensionScope provides a custom implementation that deals
165110
// with declaration names and "portions".

lib/AST/ASTScopeLookup.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,43 @@ void ASTScopeImpl::lookupEnclosingMacroScope(
666666
} while ((scope = scope->getParent().getPtrOrNull()));
667667
}
668668

669+
static std::pair<CatchNode, const BraceStmtScope *>
670+
getCatchNodeBody(const ASTScopeImpl *scope, CatchNode node) {
671+
const auto &children = scope->getChildren();
672+
if (children.empty())
673+
return { CatchNode(), nullptr };
674+
675+
auto stmt = dyn_cast<BraceStmtScope>(children[0]);
676+
if (!stmt || stmt->getStmt()->empty())
677+
return { CatchNode(), nullptr };
678+
679+
return std::make_pair(node, stmt);
680+
}
681+
682+
/// Retrieve the catch node associated with this scope, if any.
683+
static std::pair<CatchNode, const BraceStmtScope *>
684+
getCatchNode(const ASTScopeImpl *scope) {
685+
// Closures introduce a catch scope for errors initiated in their body.
686+
if (auto closureParams = dyn_cast<ClosureParametersScope>(scope)) {
687+
return getCatchNodeBody(
688+
scope, const_cast<AbstractClosureExpr *>(closureParams->closureExpr));
689+
}
690+
691+
// Functions introduce a catch scope for errors initiated in their body.
692+
if (auto function = dyn_cast<FunctionBodyScope>(scope)) {
693+
return getCatchNodeBody(
694+
scope, const_cast<AbstractFunctionDecl *>(function->decl));
695+
}
696+
697+
// Do..catch blocks introduce a catch scope for errors initiated in the `do`
698+
// body.
699+
if (auto doCatch = dyn_cast<DoCatchStmtScope>(scope)) {
700+
return getCatchNodeBody(scope, const_cast<DoCatchStmt *>(doCatch->stmt));
701+
}
702+
703+
return { CatchNode(), nullptr };
704+
}
705+
669706
CatchNode ASTScopeImpl::lookupCatchNode(ModuleDecl *module, SourceLoc loc) {
670707
auto sourceFile = module->getSourceFileContainingLocation(loc);
671708
if (!sourceFile)
@@ -682,12 +719,12 @@ CatchNode ASTScopeImpl::lookupCatchNode(ModuleDecl *module, SourceLoc loc) {
682719
for (auto scope = innermost; scope; scope = scope->getParent().getPtrOrNull()) {
683720
// If we are at a catch node and in the body of the region from which that
684721
// node catches thrown errors, we have our result.
685-
auto caught = scope->getCatchNodeBody();
722+
auto caught = getCatchNode(scope);
686723
if (caught.first && caught.second == innerBodyScope) {
687724
return caught.first;
688725
}
689726

690-
innerBodyScope = scope->getAsBraceStmtScope().getPtrOrNull();
727+
innerBodyScope = dyn_cast<BraceStmtScope>(scope);
691728
}
692729

693730
return nullptr;

0 commit comments

Comments
 (0)