Skip to content

Commit 54d6bc9

Browse files
committed
[BuilderTransform] Move buildVar to ResultBuilder type
1 parent e087489 commit 54d6bc9

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ struct ResultBuilder {
115115

116116
Identifier BuildOptionalId;
117117

118+
/// Counter used to give unique names to the variables that are
119+
/// created implicitly.
120+
unsigned VarCounter = 0;
121+
118122
public:
119123
ResultBuilder(ConstraintSystem *CS, DeclContext *DC, Type builderType);
120124

@@ -136,6 +140,9 @@ struct ResultBuilder {
136140
Expr *buildCall(SourceLoc loc, Identifier fnName,
137141
ArrayRef<Expr *> argExprs,
138142
ArrayRef<Identifier> argLabels) const;
143+
144+
/// Build an implicit variable in this context.
145+
VarDecl *buildVar(SourceLoc loc);
139146
};
140147

141148
/// Describes the algorithm to use for trailing closure matching.

lib/Sema/BuilderTransform.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ class BuilderClosureVisitor
7777
/// e.g., during constraint generation.
7878
bool hadError = false;
7979

80-
/// Counter used to give unique names to the variables that are
81-
/// created implicitly.
82-
unsigned varCounter = 0;
83-
8480
/// The record of what happened when we applied the builder transform.
8581
AppliedBuilderTransform applied;
8682

@@ -95,17 +91,6 @@ class BuilderClosureVisitor
9591
return builder.buildCall(loc, fnName, argExprs, argLabels);
9692
}
9793

98-
/// Build an implicit variable in this context.
99-
VarDecl *buildVar(SourceLoc loc) {
100-
// Create the implicit variable.
101-
Identifier name = ctx.getIdentifier(
102-
("$__builder" + Twine(varCounter++)).str());
103-
auto var = new (ctx) VarDecl(/*isStatic=*/false, VarDecl::Introducer::Var,
104-
loc, name, dc);
105-
var->setImplicit();
106-
return var;
107-
}
108-
10994
/// Capture the given expression into an implicitly-generated variable.
11095
VarDecl *captureExpr(Expr *expr, bool oneWay,
11196
llvm::PointerUnion<Stmt *, Expr *> forEntity = nullptr) {
@@ -127,7 +112,7 @@ class BuilderClosureVisitor
127112
}
128113

129114
// Create the implicit variable.
130-
auto var = buildVar(expr->getStartLoc());
115+
auto var = builder.buildVar(expr->getStartLoc());
131116

132117
// Record the new variable and its corresponding expression & statement.
133118
if (auto forStmt = forEntity.dyn_cast<Stmt *>()) {
@@ -563,7 +548,7 @@ class BuilderClosureVisitor
563548
}
564549

565550
// Create a variable to capture the result of this expression.
566-
auto ifVar = buildVar(ifStmt->getStartLoc());
551+
auto ifVar = builder.buildVar(ifStmt->getStartLoc());
567552
cs->setType(ifVar, resultType);
568553
applied.capturedStmts.insert({ifStmt, { ifVar, { thenExpr, elseExpr }}});
569554
return ifVar;
@@ -730,7 +715,7 @@ class BuilderClosureVisitor
730715
}
731716

732717
// Create a variable to capture the result of evaluating the switch.
733-
auto switchVar = buildVar(switchStmt->getStartLoc());
718+
auto switchVar = builder.buildVar(switchStmt->getStartLoc());
734719
cs->setType(switchVar, resultType);
735720
applied.capturedStmts.insert(
736721
{switchStmt, { switchVar, std::move(injectedCaseExprs) } });
@@ -820,7 +805,7 @@ class BuilderClosureVisitor
820805
// iteration of the loop. We need a fresh type variable to remove the
821806
// lvalue-ness of the array variable.
822807
SourceLoc loc = forEachStmt->getForLoc();
823-
VarDecl *arrayVar = buildVar(loc);
808+
VarDecl *arrayVar = builder.buildVar(loc);
824809
Type arrayElementType = cs->createTypeVariable(
825810
cs->getConstraintLocator(forEachStmt), 0);
826811
cs->addConstraint(ConstraintKind::Equal, cs->getType(bodyVar),
@@ -879,7 +864,7 @@ class BuilderClosureVisitor
879864

880865
// Form a final variable for the for-each expression itself, which will
881866
// be initialized with the call to the result builder's buildArray(_:).
882-
auto finalForEachVar = buildVar(loc);
867+
auto finalForEachVar = builder.buildVar(loc);
883868
cs->setType(finalForEachVar, cs->getType(buildArrayCall));
884869
applied.capturedStmts.insert(
885870
{forEachStmt, {
@@ -2226,3 +2211,14 @@ Expr *ResultBuilder::buildCall(SourceLoc loc, Identifier fnName,
22262211
auto *argList = ArgumentList::createImplicit(ctx, openLoc, args, closeLoc);
22272212
return CallExpr::createImplicit(ctx, memberRef, argList);
22282213
}
2214+
2215+
VarDecl *ResultBuilder::buildVar(SourceLoc loc) {
2216+
auto &ctx = DC->getASTContext();
2217+
// Create the implicit variable.
2218+
Identifier name =
2219+
ctx.getIdentifier(("$__builder" + Twine(VarCounter++)).str());
2220+
auto var = new (ctx)
2221+
VarDecl(/*isStatic=*/false, VarDecl::Introducer::Var, loc, name, DC);
2222+
var->setImplicit();
2223+
return var;
2224+
}

0 commit comments

Comments
 (0)