Skip to content

Commit b977adf

Browse files
committed
[AST] Add a helper method to PackExpansionExpr that gathers all packs
that are expanded within the pattern.
1 parent 2df72f6 commit b977adf

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

include/swift/AST/Expr.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3619,9 +3619,7 @@ class PackExpansionExpr final : public Expr,
36193619
return {getTrailingObjects<PackElementExpr *>(), getNumBindings()};
36203620
}
36213621

3622-
void setBinding(unsigned i, PackElementExpr *e) {
3623-
getMutableBindings()[i] = e;
3624-
}
3622+
void getExpandedPacks(SmallVectorImpl<ASTNode> &packs);
36253623

36263624
GenericEnvironment *getGenericEnvironment() {
36273625
return Environment;

lib/AST/Expr.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,41 @@ PackExpansionExpr::create(ASTContext &ctx, Expr *patternExpr,
12591259
implicit, type);
12601260
}
12611261

1262+
void PackExpansionExpr::getExpandedPacks(SmallVectorImpl<ASTNode> &packs) {
1263+
struct PackCollector : public ASTWalker {
1264+
llvm::SmallVector<ASTNode, 2> packs;
1265+
1266+
virtual PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
1267+
// Don't walk into nested pack expansions
1268+
if (isa<PackExpansionExpr>(E)) {
1269+
return Action::SkipChildren(E);
1270+
}
1271+
1272+
if (isa<PackElementExpr>(E)) {
1273+
packs.push_back(E);
1274+
}
1275+
1276+
return Action::Continue(E);
1277+
}
1278+
1279+
virtual PreWalkAction walkToTypeReprPre(TypeRepr *T) override {
1280+
// Don't walk into nested pack expansions
1281+
if (isa<PackExpansionTypeRepr>(T)) {
1282+
return Action::SkipChildren();
1283+
}
1284+
1285+
if (isa<PackReferenceTypeRepr>(T)) {
1286+
packs.push_back(T);
1287+
}
1288+
1289+
return Action::Continue();
1290+
}
1291+
} packCollector;
1292+
1293+
getPatternExpr()->walk(packCollector);
1294+
packs.append(packCollector.packs.begin(), packCollector.packs.end());
1295+
}
1296+
12621297
PackElementExpr *
12631298
PackElementExpr::create(ASTContext &ctx, SourceLoc eachLoc, Expr *packRefExpr,
12641299
bool implicit, Type type) {

0 commit comments

Comments
 (0)