Skip to content

Commit a88f17a

Browse files
committed
[AST] Add method to create an implicit SwitchStmt
There are a number of occurances that create implicit `Switch`s by passing `SourceLoc()` for all location paramters. Refactor those occurances out to a separate `createImplicit` method that automatically fills the locations with invalid source locations.
1 parent 47ca24d commit a88f17a

7 files changed

+31
-33
lines changed

include/swift/AST/Stmt.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,16 @@ class SwitchStmt final : public LabeledStmt,
11351135
SourceLoc RBraceLoc,
11361136
SourceLoc EndLoc,
11371137
ASTContext &C);
1138-
1138+
1139+
static SwitchStmt *createImplicit(LabeledStmtInfo LabelInfo,
1140+
Expr *SubjectExpr, ArrayRef<ASTNode> Cases,
1141+
ASTContext &C) {
1142+
return SwitchStmt::create(LabelInfo, /*SwitchLoc=*/SourceLoc(), SubjectExpr,
1143+
/*LBraceLoc=*/SourceLoc(), Cases,
1144+
/*RBraceLoc=*/SourceLoc(), /*EndLoc=*/SourceLoc(),
1145+
C);
1146+
}
1147+
11391148
/// Get the source location of the 'switch' keyword.
11401149
SourceLoc getSwitchLoc() const { return SwitchLoc; }
11411150
/// Get the source location of the opening brace.

lib/Sema/DerivedConformanceCodable.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,9 +1163,8 @@ deriveBodyEncodable_enum_encode(AbstractFunctionDecl *encodeDecl, void *) {
11631163
new (C) DeclRefExpr(ConcreteDeclRef(selfRef), DeclNameLoc(),
11641164
/*implicit*/ true, AccessSemantics::Ordinary);
11651165

1166-
auto switchStmt = SwitchStmt::create(LabeledStmtInfo(), SourceLoc(), enumRef,
1167-
SourceLoc(), cases, SourceLoc(),
1168-
SourceLoc(), C);
1166+
auto switchStmt =
1167+
SwitchStmt::createImplicit(LabeledStmtInfo(), enumRef, cases, C);
11691168
statements.push_back(switchStmt);
11701169

11711170
auto *body = BraceStmt::create(C, SourceLoc(), statements, SourceLoc(),
@@ -1801,8 +1800,7 @@ deriveBodyDecodable_enum_init(AbstractFunctionDecl *initDecl, void *) {
18011800
UnresolvedDotExpr::createImplicit(C, allKeysExpr, C.Id_first);
18021801

18031802
auto switchStmt =
1804-
SwitchStmt::create(LabeledStmtInfo(), SourceLoc(), firstExpr,
1805-
SourceLoc(), cases, SourceLoc(), SourceLoc(), C);
1803+
SwitchStmt::createImplicit(LabeledStmtInfo(), firstExpr, cases, C);
18061804

18071805
statements.push_back(switchStmt);
18081806
}

lib/Sema/DerivedConformanceCodingKey.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,8 @@ deriveBodyCodingKey_enum_stringValue(AbstractFunctionDecl *strValDecl, void *) {
233233
}
234234

235235
auto *selfRef = DerivedConformance::createSelfDeclRef(strValDecl);
236-
auto *switchStmt = SwitchStmt::create(LabeledStmtInfo(), SourceLoc(),
237-
selfRef, SourceLoc(), cases,
238-
SourceLoc(), SourceLoc(), C);
236+
auto *switchStmt =
237+
SwitchStmt::createImplicit(LabeledStmtInfo(), selfRef, cases, C);
239238
body = BraceStmt::create(C, SourceLoc(), ASTNode(switchStmt), SourceLoc());
240239
}
241240

@@ -313,9 +312,8 @@ deriveBodyCodingKey_init_stringValue(AbstractFunctionDecl *initDecl, void *) {
313312
auto *stringValueDecl = initDecl->getParameters()->get(0);
314313
auto *stringValueRef = new (C) DeclRefExpr(stringValueDecl, DeclNameLoc(),
315314
/*Implicit=*/true);
316-
auto *switchStmt = SwitchStmt::create(LabeledStmtInfo(), SourceLoc(),
317-
stringValueRef, SourceLoc(), cases,
318-
SourceLoc(), SourceLoc(), C);
315+
auto *switchStmt =
316+
SwitchStmt::createImplicit(LabeledStmtInfo(), stringValueRef, cases, C);
319317
auto *body = BraceStmt::create(C, SourceLoc(), ASTNode(switchStmt),
320318
SourceLoc());
321319
return { body, /*isTypeChecked=*/false };

lib/Sema/DerivedConformanceComparable.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,8 @@ deriveBodyComparable_enum_hasAssociatedValues_lt(AbstractFunctionDecl *ltDecl, v
210210
auto abExpr = TupleExpr::create(C, SourceLoc(), { aRef, bRef }, {}, {},
211211
SourceLoc(), /*HasTrailingClosure*/ false,
212212
/*implicit*/ true);
213-
auto switchStmt = SwitchStmt::create(LabeledStmtInfo(), SourceLoc(), abExpr,
214-
SourceLoc(), cases, SourceLoc(),
215-
SourceLoc(), C);
213+
auto switchStmt =
214+
SwitchStmt::createImplicit(LabeledStmtInfo(), abExpr, cases, C);
216215
statements.push_back(switchStmt);
217216

218217
auto body = BraceStmt::create(C, SourceLoc(), statements, SourceLoc());

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ deriveBodyEquatable_enum_uninhabited_eq(AbstractFunctionDecl *eqDecl, void *) {
7979
SourceLoc(), /*HasTrailingClosure*/ false,
8080
/*implicit*/ true,
8181
TupleType::get(abTupleElts, C));
82-
auto switchStmt = SwitchStmt::create(LabeledStmtInfo(), SourceLoc(), abExpr,
83-
SourceLoc(), cases, SourceLoc(),
84-
SourceLoc(), C);
82+
auto switchStmt =
83+
SwitchStmt::createImplicit(LabeledStmtInfo(), abExpr, cases, C);
8584
statements.push_back(switchStmt);
8685

8786
auto body = BraceStmt::create(C, SourceLoc(), statements, SourceLoc());
@@ -277,9 +276,8 @@ deriveBodyEquatable_enum_hasAssociatedValues_eq(AbstractFunctionDecl *eqDecl,
277276
auto abExpr = TupleExpr::create(C, SourceLoc(), { aRef, bRef }, {}, {},
278277
SourceLoc(), /*HasTrailingClosure*/ false,
279278
/*implicit*/ true);
280-
auto switchStmt = SwitchStmt::create(LabeledStmtInfo(), SourceLoc(), abExpr,
281-
SourceLoc(), cases, SourceLoc(),
282-
SourceLoc(), C);
279+
auto switchStmt =
280+
SwitchStmt::createImplicit(LabeledStmtInfo(), abExpr, cases, C);
283281
statements.push_back(switchStmt);
284282

285283
auto body = BraceStmt::create(C, SourceLoc(), statements, SourceLoc());
@@ -752,9 +750,8 @@ deriveBodyHashable_enum_hasAssociatedValues_hashInto(
752750
// generate: switch enumVar { }
753751
auto enumRef = new (C) DeclRefExpr(selfDecl, DeclNameLoc(),
754752
/*implicit*/true);
755-
auto switchStmt = SwitchStmt::create(LabeledStmtInfo(), SourceLoc(), enumRef,
756-
SourceLoc(), cases, SourceLoc(),
757-
SourceLoc(), C);
753+
auto switchStmt =
754+
SwitchStmt::createImplicit(LabeledStmtInfo(), enumRef, cases, C);
758755

759756
auto body = BraceStmt::create(C, SourceLoc(), {ASTNode(switchStmt)},
760757
SourceLoc());

lib/Sema/DerivedConformanceRawRepresentable.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,8 @@ deriveBodyRawRepresentable_raw(AbstractFunctionDecl *toRawDecl, void *) {
128128
}
129129

130130
auto selfRef = DerivedConformance::createSelfDeclRef(toRawDecl);
131-
auto switchStmt = SwitchStmt::create(LabeledStmtInfo(), SourceLoc(), selfRef,
132-
SourceLoc(), cases, SourceLoc(),
133-
SourceLoc(), C);
131+
auto switchStmt =
132+
SwitchStmt::createImplicit(LabeledStmtInfo(), selfRef, cases, C);
134133
auto body = BraceStmt::create(C, SourceLoc(),
135134
ASTNode(switchStmt),
136135
SourceLoc());
@@ -386,9 +385,8 @@ deriveBodyRawRepresentable_init(AbstractFunctionDecl *initDecl, void *) {
386385
auto *CallExpr = CallExpr::create(C, Fun, Args, {}, {}, false, false);
387386
switchArg = CallExpr;
388387
}
389-
auto switchStmt = SwitchStmt::create(LabeledStmtInfo(), SourceLoc(), switchArg,
390-
SourceLoc(), cases, SourceLoc(),
391-
SourceLoc(), C);
388+
auto switchStmt =
389+
SwitchStmt::createImplicit(LabeledStmtInfo(), switchArg, cases, C);
392390
auto body = BraceStmt::create(C, SourceLoc(),
393391
ASTNode(switchStmt),
394392
SourceLoc());

lib/Sema/DerivedConformances.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,8 @@ DeclRefExpr *DerivedConformance::convertEnumToIndex(SmallVectorImpl<ASTNode> &st
665665
/*implicit*/true,
666666
AccessSemantics::Ordinary,
667667
enumVarDecl->getType());
668-
auto switchStmt = SwitchStmt::create(LabeledStmtInfo(), SourceLoc(), enumRef,
669-
SourceLoc(), cases, SourceLoc(),
670-
SourceLoc(), C);
668+
auto switchStmt =
669+
SwitchStmt::createImplicit(LabeledStmtInfo(), enumRef, cases, C);
671670

672671
stmts.push_back(indexBind);
673672
stmts.push_back(switchStmt);

0 commit comments

Comments
 (0)