Skip to content

Commit c1b1feb

Browse files
committed
[AST] Clean up CaptureListEntry
Don't store a VarDecl separately, expose a `getVar` accessor that forwards onto `getSingleVar`, and rename `Init` to `PBD`.
1 parent cc62c11 commit c1b1feb

14 files changed

+47
-41
lines changed

include/swift/AST/Expr.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4180,13 +4180,11 @@ class AutoClosureExpr : public AbstractClosureExpr {
41804180
/// Instances of this structure represent elements of the capture list that can
41814181
/// optionally occur in a capture expression.
41824182
struct CaptureListEntry {
4183-
VarDecl *Var;
4184-
PatternBindingDecl *Init;
4183+
PatternBindingDecl *PBD;
41854184

4186-
CaptureListEntry(VarDecl *Var, PatternBindingDecl *Init)
4187-
: Var(Var), Init(Init) {
4188-
}
4185+
explicit CaptureListEntry(PatternBindingDecl *PBD);
41894186

4187+
VarDecl *getVar() const;
41904188
bool isSimpleSelfCapture() const;
41914189
};
41924190

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,8 +2496,7 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
24962496
for (auto capture : E->getCaptureList()) {
24972497
OS << '\n';
24982498
Indent += 2;
2499-
printRec(capture.Var);
2500-
printRec(capture.Init);
2499+
printRec(capture.PBD);
25012500
Indent -= 2;
25022501
}
25032502
printRec(E->getClosureBody());

lib/AST/ASTScopeLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ bool PatternEntryInitializerScope::lookupLocalsOrMembers(
408408

409409
bool CaptureListScope::lookupLocalsOrMembers(DeclConsumer consumer) const {
410410
for (auto &e : expr->getCaptureList()) {
411-
if (consumer.consume({e.Var}))
411+
if (consumer.consume({e.getVar()}))
412412
return true;
413413
}
414414
return false;

lib/AST/ASTWalker.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -796,16 +796,14 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
796796
Expr *visitCaptureListExpr(CaptureListExpr *expr) {
797797
for (auto c : expr->getCaptureList()) {
798798
if (Walker.shouldWalkCaptureInitializerExpressions()) {
799-
for (auto entryIdx : range(c.Init->getNumPatternEntries())) {
800-
if (auto newInit = doIt(c.Init->getInit(entryIdx)))
801-
c.Init->setInit(entryIdx, newInit);
799+
for (auto entryIdx : range(c.PBD->getNumPatternEntries())) {
800+
if (auto newInit = doIt(c.PBD->getInit(entryIdx)))
801+
c.PBD->setInit(entryIdx, newInit);
802802
else
803803
return nullptr;
804804
}
805805
} else {
806-
// Note we do not walk c.Var here, as it'll be visited as a part of the
807-
// PatternBindingDecl.
808-
if (doIt(c.Init))
806+
if (doIt(c.PBD))
809807
return nullptr;
810808
}
811809
}

lib/AST/Expr.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,18 @@ UnresolvedSpecializeExpr *UnresolvedSpecializeExpr::create(ASTContext &ctx,
12291229
UnresolvedParams, RAngleLoc);
12301230
}
12311231

1232+
CaptureListEntry::CaptureListEntry(PatternBindingDecl *PBD) : PBD(PBD) {
1233+
assert(PBD);
1234+
assert(PBD->getSingleVar() &&
1235+
"Capture lists only support single-var patterns");
1236+
}
1237+
1238+
VarDecl *CaptureListEntry::getVar() const {
1239+
return PBD->getSingleVar();
1240+
}
1241+
12321242
bool CaptureListEntry::isSimpleSelfCapture() const {
1243+
auto *Var = getVar();
12331244
auto &ctx = Var->getASTContext();
12341245

12351246
if (Var->getName() != ctx.Id_self)
@@ -1239,10 +1250,10 @@ bool CaptureListEntry::isSimpleSelfCapture() const {
12391250
if (attr->get() == ReferenceOwnership::Weak)
12401251
return false;
12411252

1242-
if (Init->getPatternList().size() != 1)
1253+
if (PBD->getPatternList().size() != 1)
12431254
return false;
12441255

1245-
auto *expr = Init->getInit(0);
1256+
auto *expr = PBD->getInit(0);
12461257

12471258
if (auto *DRE = dyn_cast<DeclRefExpr>(expr)) {
12481259
if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
@@ -1265,7 +1276,7 @@ CaptureListExpr *CaptureListExpr::create(ASTContext &ctx,
12651276
auto *expr = ::new(mem) CaptureListExpr(captureList, closureBody);
12661277

12671278
for (auto capture : captureList)
1268-
capture.Var->setParentCaptureList(expr);
1279+
capture.getVar()->setParentCaptureList(expr);
12691280

12701281
return expr;
12711282
}

lib/IDE/Formatting.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,18 +2586,17 @@ class FormatWalker : public ASTWalker {
25862586

25872587
ListAligner Aligner(SM, TargetLocation, ContextLoc, L, R);
25882588
for (auto &Entry: ParentCapture->getCaptureList()) {
2589-
if (auto *PBD = Entry.Init) {
2590-
NodesToSkip.insert(PBD);
2591-
SourceRange Range = PBD->getSourceRangeIncludingAttrs();
2592-
Aligner.updateAlignment(Range, PBD);
2589+
auto *PBD = Entry.PBD;
2590+
NodesToSkip.insert(PBD);
2591+
SourceRange Range = PBD->getSourceRangeIncludingAttrs();
2592+
Aligner.updateAlignment(Range, PBD);
25932593

2594-
if (isTargetContext(Range)) {
2595-
Aligner.setAlignmentIfNeeded(CtxOverride);
2596-
return IndentContext {
2597-
Range.Start,
2598-
!OutdentChecker::hasOutdent(SM, Range, PBD)
2599-
};
2600-
}
2594+
if (isTargetContext(Range)) {
2595+
Aligner.setAlignmentIfNeeded(CtxOverride);
2596+
return IndentContext {
2597+
Range.Start,
2598+
!OutdentChecker::hasOutdent(SM, Range, PBD)
2599+
};
26012600
}
26022601
}
26032602
return Aligner.getContextAndSetAlignment(CtxOverride);

lib/Parse/ParseExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2651,7 +2651,7 @@ ParserStatus Parser::parseClosureSignatureIfPresent(
26512651
/*VarLoc*/ nameLoc, pattern, /*EqualLoc*/ equalLoc, initializer,
26522652
CurDeclContext);
26532653

2654-
auto CLE = CaptureListEntry(VD, PBD);
2654+
auto CLE = CaptureListEntry(PBD);
26552655
if (CLE.isSimpleSelfCapture())
26562656
VD->setIsSelfParamCapture();
26572657

lib/SILGen/SILGenExpr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ ManagedValue emitCFunctionPointer(SILGenFunction &SGF,
15691569
DebugScope scope(SGF, CleanupLocation(captureList));
15701570
// CaptureListExprs evaluate their bound variables.
15711571
for (auto capture : captureList->getCaptureList())
1572-
SGF.visit(capture.Init);
1572+
SGF.visit(capture.PBD);
15731573

15741574
// Emit the closure body.
15751575
auto *closure = captureList->getClosureBody();
@@ -2417,7 +2417,7 @@ RValue RValueEmitter::visitCaptureListExpr(CaptureListExpr *E, SGFContext C) {
24172417
DebugScope scope(SGF, CleanupLocation(E));
24182418
// CaptureListExprs evaluate their bound variables.
24192419
for (auto capture : E->getCaptureList())
2420-
SGF.visit(capture.Init);
2420+
SGF.visit(capture.PBD);
24212421

24222422
// Then they evaluate to their body.
24232423
return visit(E->getClosureBody(), C);

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8108,7 +8108,7 @@ namespace {
81088108
if (auto captureList = dyn_cast<CaptureListExpr>(expr)) {
81098109
// Rewrite captures.
81108110
for (const auto &capture : captureList->getCaptureList()) {
8111-
(void)rewriteTarget(SolutionApplicationTarget(capture.Init));
8111+
(void)rewriteTarget(SolutionApplicationTarget(capture.PBD));
81128112
}
81138113
}
81148114

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3469,7 +3469,7 @@ namespace {
34693469

34703470
auto &CS = CG.getConstraintSystem();
34713471
for (const auto &capture : captureList->getCaptureList()) {
3472-
SolutionApplicationTarget target(capture.Init);
3472+
SolutionApplicationTarget target(capture.PBD);
34733473
if (CS.generateConstraints(target, FreeTypeVariableBinding::Disallow))
34743474
return {false, nullptr};
34753475
}

0 commit comments

Comments
 (0)