Skip to content

Commit 8ebfd9a

Browse files
authored
Merge pull request #34204 from slavapestov/astscope-related-refactorings
A few small ASTScope-related refactorings
2 parents b22458e + da1c5c9 commit 8ebfd9a

File tree

9 files changed

+42
-38
lines changed

9 files changed

+42
-38
lines changed

include/swift/AST/Stmt.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -632,17 +632,17 @@ class IfStmt : public LabeledConditionalStmt {
632632
///
633633
class GuardStmt : public LabeledConditionalStmt {
634634
SourceLoc GuardLoc;
635-
Stmt *Body;
635+
BraceStmt *Body;
636636

637637
public:
638638
GuardStmt(SourceLoc GuardLoc, StmtCondition Cond,
639-
Stmt *Body, Optional<bool> implicit = None)
639+
BraceStmt *Body, Optional<bool> implicit = None)
640640
: LabeledConditionalStmt(StmtKind::Guard,
641641
getDefaultImplicitFlag(implicit, GuardLoc),
642642
LabeledStmtInfo(), Cond),
643643
GuardLoc(GuardLoc), Body(Body) {}
644644

645-
GuardStmt(SourceLoc GuardLoc, Expr *Cond, Stmt *Body,
645+
GuardStmt(SourceLoc GuardLoc, Expr *Cond, BraceStmt *Body,
646646
Optional<bool> implicit, ASTContext &Ctx);
647647

648648
SourceLoc getGuardLoc() const { return GuardLoc; }
@@ -654,8 +654,8 @@ class GuardStmt : public LabeledConditionalStmt {
654654
return Body->getEndLoc();
655655
}
656656

657-
Stmt *getBody() const { return Body; }
658-
void setBody(Stmt *s) { Body = s; }
657+
BraceStmt *getBody() const { return Body; }
658+
void setBody(BraceStmt *s) { Body = s; }
659659

660660
// Implement isa/cast/dyncast/etc.
661661
static bool classof(const Stmt *S) { return S->getKind() == StmtKind::Guard; }
@@ -945,13 +945,13 @@ class CaseStmt final
945945
SourceLoc ItemTerminatorLoc;
946946
CaseParentKind ParentKind;
947947

948-
llvm::PointerIntPair<Stmt *, 1, bool> BodyAndHasFallthrough;
948+
llvm::PointerIntPair<BraceStmt *, 1, bool> BodyAndHasFallthrough;
949949

950950
Optional<MutableArrayRef<VarDecl *>> CaseBodyVariables;
951951

952952
CaseStmt(CaseParentKind ParentKind, SourceLoc ItemIntroducerLoc,
953953
ArrayRef<CaseLabelItem> CaseLabelItems, SourceLoc UnknownAttrLoc,
954-
SourceLoc ItemTerminatorLoc, Stmt *Body,
954+
SourceLoc ItemTerminatorLoc, BraceStmt *Body,
955955
Optional<MutableArrayRef<VarDecl *>> CaseBodyVariables,
956956
Optional<bool> Implicit,
957957
NullablePtr<FallthroughStmt> fallthroughStmt);
@@ -960,7 +960,7 @@ class CaseStmt final
960960
static CaseStmt *
961961
create(ASTContext &C, CaseParentKind ParentKind, SourceLoc ItemIntroducerLoc,
962962
ArrayRef<CaseLabelItem> CaseLabelItems, SourceLoc UnknownAttrLoc,
963-
SourceLoc ItemTerminatorLoc, Stmt *Body,
963+
SourceLoc ItemTerminatorLoc, BraceStmt *Body,
964964
Optional<MutableArrayRef<VarDecl *>> CaseBodyVariables,
965965
Optional<bool> Implicit = None,
966966
NullablePtr<FallthroughStmt> fallthroughStmt = nullptr);
@@ -997,8 +997,8 @@ class CaseStmt final
997997

998998
bool hasFallthroughDest() const { return BodyAndHasFallthrough.getInt(); }
999999

1000-
Stmt *getBody() const { return BodyAndHasFallthrough.getPointer(); }
1001-
void setBody(Stmt *body) { BodyAndHasFallthrough.setPointer(body); }
1000+
BraceStmt *getBody() const { return BodyAndHasFallthrough.getPointer(); }
1001+
void setBody(BraceStmt *body) { BodyAndHasFallthrough.setPointer(body); }
10021002

10031003
/// True if the case block declares any patterns with local variable bindings.
10041004
bool hasBoundDecls() const { return CaseBodyVariables.hasValue(); }

lib/AST/ASTDumper.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,10 @@ namespace {
631631
void visitExtensionDecl(ExtensionDecl *ED) {
632632
printCommon(ED, "extension_decl", ExtensionColor);
633633
OS << ' ';
634-
ED->getExtendedType().print(OS);
634+
if (ED->hasBeenBound())
635+
ED->getExtendedType().print(OS);
636+
else
637+
ED->getExtendedTypeRepr()->print(OS);
635638
printCommonPost(ED);
636639
}
637640

@@ -854,20 +857,22 @@ namespace {
854857
if (D->isStatic())
855858
PrintWithColorRAII(OS, DeclModifierColor) << " type";
856859

857-
auto impl = D->getImplInfo();
858-
PrintWithColorRAII(OS, DeclModifierColor)
859-
<< " readImpl="
860-
<< getReadImplKindName(impl.getReadImpl());
861-
if (!impl.supportsMutation()) {
862-
PrintWithColorRAII(OS, DeclModifierColor)
863-
<< " immutable";
864-
} else {
860+
if (D->hasInterfaceType()) {
861+
auto impl = D->getImplInfo();
865862
PrintWithColorRAII(OS, DeclModifierColor)
866-
<< " writeImpl="
867-
<< getWriteImplKindName(impl.getWriteImpl());
868-
PrintWithColorRAII(OS, DeclModifierColor)
869-
<< " readWriteImpl="
870-
<< getReadWriteImplKindName(impl.getReadWriteImpl());
863+
<< " readImpl="
864+
<< getReadImplKindName(impl.getReadImpl());
865+
if (!impl.supportsMutation()) {
866+
PrintWithColorRAII(OS, DeclModifierColor)
867+
<< " immutable";
868+
} else {
869+
PrintWithColorRAII(OS, DeclModifierColor)
870+
<< " writeImpl="
871+
<< getWriteImplKindName(impl.getWriteImpl());
872+
PrintWithColorRAII(OS, DeclModifierColor)
873+
<< " readWriteImpl="
874+
<< getReadWriteImplKindName(impl.getReadWriteImpl());
875+
}
871876
}
872877
}
873878

lib/AST/ASTWalker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,7 @@ Stmt *Traversal::visitGuardStmt(GuardStmt *US) {
14781478
if (doIt(US->getCond()))
14791479
return nullptr;
14801480

1481-
if (Stmt *S2 = doIt(US->getBody()))
1481+
if (BraceStmt *S2 = cast_or_null<BraceStmt>(doIt(US->getBody())))
14821482
US->setBody(S2);
14831483
else
14841484
return nullptr;
@@ -1631,7 +1631,7 @@ Stmt *Traversal::visitCaseStmt(CaseStmt *S) {
16311631
}
16321632
}
16331633

1634-
if (Stmt *newBody = doIt(S->getBody()))
1634+
if (BraceStmt *newBody = cast_or_null<BraceStmt>(doIt(S->getBody())))
16351635
S->setBody(newBody);
16361636
else
16371637
return nullptr;

lib/AST/Stmt.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ IfStmt::IfStmt(SourceLoc IfLoc, Expr *Cond, Stmt *Then, SourceLoc ElseLoc,
380380
implicit) {
381381
}
382382

383-
GuardStmt::GuardStmt(SourceLoc GuardLoc, Expr *Cond, Stmt *Body,
383+
GuardStmt::GuardStmt(SourceLoc GuardLoc, Expr *Cond, BraceStmt *Body,
384384
Optional<bool> implicit, ASTContext &Ctx)
385385
: GuardStmt(GuardLoc, exprToCond(Cond, Ctx), Body, implicit) {
386386

@@ -407,7 +407,7 @@ SourceLoc CaseLabelItem::getEndLoc() const {
407407
CaseStmt::CaseStmt(CaseParentKind parentKind, SourceLoc itemIntroducerLoc,
408408
ArrayRef<CaseLabelItem> caseLabelItems,
409409
SourceLoc unknownAttrLoc, SourceLoc itemTerminatorLoc,
410-
Stmt *body,
410+
BraceStmt *body,
411411
Optional<MutableArrayRef<VarDecl *>> caseBodyVariables,
412412
Optional<bool> implicit,
413413
NullablePtr<FallthroughStmt> fallthroughStmt)
@@ -445,7 +445,7 @@ CaseStmt *CaseStmt::create(ASTContext &ctx, CaseParentKind ParentKind,
445445
SourceLoc caseLoc,
446446
ArrayRef<CaseLabelItem> caseLabelItems,
447447
SourceLoc unknownAttrLoc, SourceLoc colonLoc,
448-
Stmt *body,
448+
BraceStmt *body,
449449
Optional<MutableArrayRef<VarDecl *>> caseVarDecls,
450450
Optional<bool> implicit,
451451
NullablePtr<FallthroughStmt> fallthroughStmt) {

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6000,7 +6000,7 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
60006000
// up in Decls to be returned to caller.
60016001
if (topLevelDecl) {
60026002
PBD->setDeclContext(topLevelDecl);
6003-
auto range = PBD->getSourceRange();
6003+
auto range = PBD->getSourceRangeIncludingAttrs();
60046004
topLevelDecl->setBody(BraceStmt::create(Context, range.Start,
60056005
ASTNode(PBD), range.End, true));
60066006
Decls.insert(Decls.begin()+NumDeclsInResult, topLevelDecl);

lib/Parse/ParseStmt.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,9 +1876,8 @@ ParserResult<Stmt> Parser::parseStmtRepeat(LabeledStmtInfo labelInfo) {
18761876

18771877
ParserResult<Expr> condition;
18781878
if (Tok.is(tok::l_brace)) {
1879-
SourceLoc lbraceLoc = Tok.getLoc();
18801879
diagnose(whileLoc, diag::missing_condition_after_while);
1881-
condition = makeParserErrorResult(new (Context) ErrorExpr(lbraceLoc));
1880+
condition = makeParserErrorResult(new (Context) ErrorExpr(whileLoc));
18821881
} else {
18831882
condition = parseExpr(diag::expected_expr_repeat_while);
18841883
status |= condition;

lib/Sema/PCMacro.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ class Instrumenter : InstrumenterBase {
187187
transformStmtCondition(SC, GS->getStartLoc());
188188
GS->setCond(SC);
189189

190-
if (Stmt *BS = GS->getBody())
191-
GS->setBody(transformStmt(BS));
190+
if (BraceStmt *BS = GS->getBody())
191+
GS->setBody(transformBraceStmt(BS));
192192
return GS;
193193
}
194194

lib/Sema/PlaygroundTransform.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ class Instrumenter : InstrumenterBase {
204204
}
205205

206206
GuardStmt *transformGuardStmt(GuardStmt *GS) {
207-
if (Stmt *BS = GS->getBody())
208-
GS->setBody(transformStmt(BS));
207+
if (BraceStmt *BS = GS->getBody())
208+
GS->setBody(transformBraceStmt(BS));
209209
return GS;
210210
}
211211

lib/Sema/TypeCheckStmt.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
912912
Stmt *visitGuardStmt(GuardStmt *GS) {
913913
typeCheckConditionForStatement(GS, DC);
914914

915-
Stmt *S = GS->getBody();
915+
BraceStmt *S = GS->getBody();
916916
typeCheckStmt(S);
917917
GS->setBody(S);
918918
return GS;
@@ -1150,7 +1150,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
11501150
getASTContext(), caseBlock, limitExhaustivityChecks);
11511151
}
11521152

1153-
Stmt *body = caseBlock->getBody();
1153+
BraceStmt *body = caseBlock->getBody();
11541154
limitExhaustivityChecks |= typeCheckStmt(body);
11551155
caseBlock->setBody(body);
11561156
}

0 commit comments

Comments
 (0)