Skip to content

Commit 6325915

Browse files
committed
NFC: Solidify and tidy up the BraceStmt interface
1 parent 022c6ac commit 6325915

17 files changed

+47
-46
lines changed

include/swift/AST/Stmt.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,13 @@ class BraceStmt final : public Stmt,
173173

174174
SourceRange getSourceRange() const { return SourceRange(LBLoc, RBLoc); }
175175

176+
bool empty() const { return getNumElements() == 0; }
176177
unsigned getNumElements() const { return Bits.BraceStmt.NumElements; }
177178

178-
ASTNode getElement(unsigned i) const { return getElements()[i]; }
179-
void setElement(unsigned i, ASTNode node) { getElements()[i] = node; }
179+
ASTNode getFirstElement() const { return getElements().front(); }
180+
ASTNode getLastElement() const { return getElements().back(); }
181+
182+
void setFirstElement(ASTNode node) { getElements().front() = node; }
180183

181184
/// The elements contained within the BraceStmt.
182185
MutableArrayRef<ASTNode> getElements() {

lib/AST/ASTScopeCreation.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,7 @@ class ScopeCreator final {
640640
// Can occur in illegal code
641641
if (auto *const s = n.dyn_cast<Stmt *>()) {
642642
if (auto *const bs = dyn_cast<BraceStmt>(s))
643-
ASTScopeAssert(bs->getNumElements() == 0,
644-
"Might mess up insertion point");
643+
ASTScopeAssert(bs->empty(), "Might mess up insertion point");
645644
}
646645
return !n.isDecl(DeclKind::Var);
647646
}

lib/AST/Decl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ Expr *AbstractFunctionDecl::getSingleExpressionBody() const {
538538
assert(hasSingleExpressionBody() && "Not a single-expression body");
539539
auto braceStmt = getBody();
540540
assert(braceStmt != nullptr && "No body currently available.");
541-
auto body = getBody()->getElement(0);
541+
auto body = getBody()->getFirstElement();
542542
if (auto *stmt = body.dyn_cast<Stmt *>()) {
543543
if (auto *returnStmt = dyn_cast<ReturnStmt>(stmt)) {
544544
return returnStmt->getResult();
@@ -555,7 +555,7 @@ Expr *AbstractFunctionDecl::getSingleExpressionBody() const {
555555

556556
void AbstractFunctionDecl::setSingleExpressionBody(Expr *NewBody) {
557557
assert(hasSingleExpressionBody() && "Not a single-expression body");
558-
auto body = getBody()->getElement(0);
558+
auto body = getBody()->getFirstElement();
559559
if (auto *stmt = body.dyn_cast<Stmt *>()) {
560560
if (auto *returnStmt = dyn_cast<ReturnStmt>(stmt)) {
561561
returnStmt->setResult(NewBody);
@@ -571,7 +571,7 @@ void AbstractFunctionDecl::setSingleExpressionBody(Expr *NewBody) {
571571
return;
572572
}
573573
}
574-
getBody()->setElement(0, NewBody);
574+
getBody()->setFirstElement(NewBody);
575575
}
576576

577577
bool AbstractStorageDecl::isTransparent() const {

lib/AST/Expr.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,24 +1854,24 @@ FORWARD_SOURCE_LOCS_TO(ClosureExpr, Body.getPointer())
18541854

18551855
Expr *ClosureExpr::getSingleExpressionBody() const {
18561856
assert(hasSingleExpressionBody() && "Not a single-expression body");
1857-
auto body = getBody()->getElement(0);
1857+
auto body = getBody()->getFirstElement();
18581858
if (body.is<Stmt *>())
18591859
return cast<ReturnStmt>(body.get<Stmt *>())->getResult();
18601860
return body.get<Expr *>();
18611861
}
18621862

18631863
void ClosureExpr::setSingleExpressionBody(Expr *NewBody) {
18641864
assert(hasSingleExpressionBody() && "Not a single-expression body");
1865-
auto body = getBody()->getElement(0);
1865+
auto body = getBody()->getFirstElement();
18661866
if (body.is<Stmt *>()) {
18671867
cast<ReturnStmt>(body.get<Stmt *>())->setResult(NewBody);
18681868
return;
18691869
}
1870-
getBody()->setElement(0, NewBody);
1870+
getBody()->setFirstElement(NewBody);
18711871
}
18721872

18731873
bool ClosureExpr::hasEmptyBody() const {
1874-
return getBody()->getNumElements() == 0;
1874+
return getBody()->empty();
18751875
}
18761876

18771877
FORWARD_SOURCE_LOCS_TO(AutoClosureExpr, Body)
@@ -1883,7 +1883,7 @@ void AutoClosureExpr::setBody(Expr *E) {
18831883
}
18841884

18851885
Expr *AutoClosureExpr::getSingleExpressionBody() const {
1886-
return cast<ReturnStmt>(Body->getElement(0).get<Stmt *>())->getResult();
1886+
return cast<ReturnStmt>(Body->getFirstElement().get<Stmt *>())->getResult();
18871887
}
18881888

18891889
FORWARD_SOURCE_LOCS_TO(UnresolvedPatternExpr, subPattern)
@@ -2222,14 +2222,14 @@ TapExpr::TapExpr(Expr * SubExpr, BraceStmt *Body)
22222222
: Expr(ExprKind::Tap, /*Implicit=*/true),
22232223
SubExpr(SubExpr), Body(Body) {
22242224
if (Body) {
2225-
assert(Body->getNumElements() > 0 &&
2226-
Body->getElement(0).isDecl(DeclKind::Var) &&
2225+
assert(!Body->empty() &&
2226+
Body->getFirstElement().isDecl(DeclKind::Var) &&
22272227
"First element of Body should be a variable to init with the subExpr");
22282228
}
22292229
}
22302230

22312231
VarDecl * TapExpr::getVar() const {
2232-
return dyn_cast<VarDecl>(Body->getElement(0).dyn_cast<Decl *>());
2232+
return dyn_cast<VarDecl>(Body->getFirstElement().dyn_cast<Decl *>());
22332233
}
22342234

22352235
SourceLoc TapExpr::getEndLoc() const {

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ class ExprContextAnalyzer {
759759
/// in order to avoid a base expression affecting the type. However, now that
760760
/// we've typechecked, we will take the context type into account.
761761
static bool isSingleExpressionBodyForCodeCompletion(BraceStmt *body) {
762-
return body->getNumElements() == 1 && body->getElements()[0].is<Expr *>();
762+
return body->getNumElements() == 1 && body->getFirstElement().is<Expr *>();
763763
}
764764

765765
public:

lib/IDE/IDERequests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ struct RangeResolver::Implementation {
486486
// Unbox the brace statement to find its type.
487487
if (auto BS = dyn_cast<BraceStmt>(N.get<Stmt*>())) {
488488
if (!BS->getElements().empty()) {
489-
return resolveNodeType(BS->getElements().back(),
489+
return resolveNodeType(BS->getLastElement(),
490490
RangeKind::SingleStatement);
491491
}
492492
}

lib/IDE/Refactoring.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,7 +1692,7 @@ findCollapseNestedIfTarget(ResolvedCursorInfo CursorInfo) {
16921692
return {};
16931693

16941694
IfStmt *InnerIf =
1695-
dyn_cast_or_null<IfStmt>(Body->getElement(0).dyn_cast<Stmt *>());
1695+
dyn_cast_or_null<IfStmt>(Body->getFirstElement().dyn_cast<Stmt *>());
16961696
if (!InnerIf)
16971697
return {};
16981698

@@ -2119,8 +2119,8 @@ bool RefactoringActionConvertIfLetExprToGuardExpr::performChange() {
21192119
auto Body = dyn_cast_or_null<BraceStmt>(If->getThenStmt());
21202120

21212121
// Get if-let then body.
2122-
auto firstElement = Body->getElements()[0];
2123-
auto lastElement = Body->getElements().back();
2122+
auto firstElement = Body->getFirstElement();
2123+
auto lastElement = Body->getLastElement();
21242124
SourceRange bodyRange = firstElement.getSourceRange();
21252125
bodyRange.widen(lastElement.getSourceRange());
21262126
auto BodyCharRange = Lexer::getCharSourceRangeFromSourceRange(SM, bodyRange);
@@ -2138,8 +2138,8 @@ bool RefactoringActionConvertIfLetExprToGuardExpr::performChange() {
21382138

21392139
// Get if-let else body.
21402140
if (auto *ElseBody = dyn_cast_or_null<BraceStmt>(If->getElseStmt())) {
2141-
auto firstElseElement = ElseBody->getElements()[0];
2142-
auto lastElseElement = ElseBody->getElements().back();
2141+
auto firstElseElement = ElseBody->getFirstElement();
2142+
auto lastElseElement = ElseBody->getLastElement();
21432143
SourceRange elseBodyRange = firstElseElement.getSourceRange();
21442144
elseBodyRange.widen(lastElseElement.getSourceRange());
21452145
auto ElseBodyCharRange = Lexer::getCharSourceRangeFromSourceRange(SM, elseBodyRange);
@@ -2225,9 +2225,9 @@ bool RefactoringActionConvertGuardExprToIfLetExpr::performChange() {
22252225
// Get guard body
22262226
auto Body = dyn_cast_or_null<BraceStmt>(Guard->getBody());
22272227

2228-
if (Body && Body->getElements().size() > 1) {
2229-
auto firstElement = Body->getElements()[0];
2230-
auto lastElement = Body->getElements().back();
2228+
if (Body && Body->getNumElements() > 1) {
2229+
auto firstElement = Body->getFirstElement();
2230+
auto lastElement = Body->getLastElement();
22312231
SourceRange bodyRange = firstElement.getSourceRange();
22322232
bodyRange.widen(lastElement.getSourceRange());
22332233
auto BodyCharRange = Lexer::getCharSourceRangeFromSourceRange(SM, bodyRange);

lib/IRGen/GenClass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,7 @@ namespace {
14661466
// If we have the destructor body, we know whether SILGen
14671467
// generated a -dealloc body.
14681468
if (auto braceStmt = destructor->getBody())
1469-
return braceStmt->getNumElements() != 0;
1469+
return !braceStmt->empty();
14701470

14711471
// We don't have a destructor body, so hunt for the SIL function
14721472
// for it.

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5586,7 +5586,7 @@ void Parser::parseAbstractFunctionBody(AbstractFunctionDecl *AFD) {
55865586
// may be incomplete and the type mismatch in return statement will just
55875587
// confuse the type checker.
55885588
if (!Body.hasCodeCompletion() && BS->getNumElements() == 1) {
5589-
auto Element = BS->getElement(0);
5589+
auto Element = BS->getFirstElement();
55905590
if (auto *stmt = Element.dyn_cast<Stmt *>()) {
55915591
if (isa<FuncDecl>(AFD)) {
55925592
if (auto *returnStmt = dyn_cast<ReturnStmt>(stmt)) {
@@ -5611,15 +5611,15 @@ void Parser::parseAbstractFunctionBody(AbstractFunctionDecl *AFD) {
56115611
}
56125612
if (auto F = dyn_cast<FuncDecl>(AFD)) {
56135613
auto RS = new (Context) ReturnStmt(SourceLoc(), E);
5614-
BS->setElement(0, RS);
5614+
BS->setFirstElement(RS);
56155615
AFD->setHasSingleExpressionBody();
56165616
AFD->setSingleExpressionBody(E);
56175617
} else if (auto *F = dyn_cast<ConstructorDecl>(AFD)) {
56185618
if (F->isFailable() && isa<NilLiteralExpr>(E)) {
56195619
// If it's a nil literal, just insert return. This is the only
56205620
// legal thing to return.
56215621
auto RS = new (Context) ReturnStmt(E->getStartLoc(), E);
5622-
BS->setElement(0, RS);
5622+
BS->setFirstElement(RS);
56235623
AFD->setHasSingleExpressionBody();
56245624
AFD->setSingleExpressionBody(E);
56255625
}

lib/SILGen/SILGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ void SILGenModule::emitObjCAllocatorDestructor(ClassDecl *cd,
977977

978978
// Emit the Objective-C -dealloc entry point if it has
979979
// something to do beyond messaging the superclass's -dealloc.
980-
if (dd->hasBody() && dd->getBody()->getNumElements() != 0)
980+
if (dd->hasBody() && !dd->getBody()->empty())
981981
emitObjCDestructorThunk(dd);
982982

983983
// Emit the ivar initializer, if needed.

0 commit comments

Comments
 (0)