Skip to content

Commit 6bc6ded

Browse files
committed
[BuilderTransform] Move buildVarRef to ResultBuilder type
1 parent f54172e commit 6bc6ded

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ struct ResultBuilder {
143143

144144
/// Build an implicit variable in this context.
145145
VarDecl *buildVar(SourceLoc loc);
146+
147+
/// Build a reference to a given variable and mark it
148+
/// as located at a given source location.
149+
DeclRefExpr *buildVarRef(VarDecl *var, SourceLoc loc);
146150
};
147151

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

lib/Sema/BuilderTransform.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@ class BuilderClosureVisitor
128128
return var;
129129
}
130130

131-
/// Build an implicit reference to the given variable.
132-
DeclRefExpr *buildVarRef(VarDecl *var, SourceLoc loc) {
133-
return new (ctx) DeclRefExpr(var, DeclNameLoc(loc), /*Implicit=*/true);
134-
}
135-
136131
public:
137132
BuilderClosureVisitor(ASTContext &ctx, ConstraintSystem *cs, DeclContext *dc,
138133
Type builderType, Type bodyResultType)
@@ -147,7 +142,7 @@ class BuilderClosureVisitor
147142
if (!bodyVar)
148143
return None;
149144

150-
applied.returnExpr = buildVarRef(bodyVar, stmt->getEndLoc());
145+
applied.returnExpr = builder.buildVarRef(bodyVar, stmt->getEndLoc());
151146

152147
// If there is a buildFinalResult(_:), call it.
153148
ASTContext &ctx = cs->getASTContext();
@@ -227,7 +222,7 @@ class BuilderClosureVisitor
227222
if (!childVar)
228223
return;
229224

230-
expressions.push_back(buildVarRef(childVar, childVar->getLoc()));
225+
expressions.push_back(builder.buildVarRef(childVar, childVar->getLoc()));
231226
};
232227

233228
for (auto node : braceStmt->getElements()) {
@@ -342,7 +337,7 @@ class BuilderClosureVisitor
342337
if (!childVar)
343338
return nullptr;
344339

345-
auto childRef = buildVarRef(childVar, doStmt->getEndLoc());
340+
auto childRef = builder.buildVarRef(childVar, doStmt->getEndLoc());
346341

347342
return captureExpr(childRef, /*oneWay=*/true, doStmt);
348343
}
@@ -455,8 +450,8 @@ class BuilderClosureVisitor
455450
if (!cs || !thenVar || (elseChainVar && !*elseChainVar))
456451
return nullptr;
457452

458-
Expr *thenVarRefExpr = buildVarRef(
459-
thenVar, ifStmt->getThenStmt()->getEndLoc());
453+
Expr *thenVarRefExpr =
454+
builder.buildVarRef(thenVar, ifStmt->getThenStmt()->getEndLoc());
460455

461456
// If there is a #available in the condition, wrap the 'then' in a call to
462457
// buildLimitedAvailability(_:).
@@ -487,12 +482,13 @@ class BuilderClosureVisitor
487482
// - If there's an `else if`, the chain expression from that
488483
// should already be producing a chain result.
489484
} else if (isElseIf) {
490-
elseExpr = buildVarRef(*elseChainVar, ifStmt->getEndLoc());
485+
elseExpr = builder.buildVarRef(*elseChainVar, ifStmt->getEndLoc());
491486
elseLoc = ifStmt->getElseLoc();
492487

493488
// - Otherwise, wrap it to produce a chain result.
494489
} else {
495-
Expr *elseVarRefExpr = buildVarRef(*elseChainVar, ifStmt->getEndLoc());
490+
Expr *elseVarRefExpr =
491+
builder.buildVarRef(*elseChainVar, ifStmt->getEndLoc());
496492

497493
// If there is a #unavailable in the condition, wrap the 'else' in a call
498494
// to buildLimitedAvailability(_:).
@@ -679,7 +675,7 @@ class BuilderClosureVisitor
679675

680676
// Build the expression that injects the case variable into appropriate
681677
// buildEither(first:)/buildEither(second:) chain.
682-
Expr *caseVarRef = buildVarRef(caseVar, caseStmt->getEndLoc());
678+
Expr *caseVarRef = builder.buildVarRef(caseVar, caseStmt->getEndLoc());
683679
Expr *injectedCaseExpr = buildWrappedChainPayload(
684680
caseVarRef, idx, capturedCaseVars.size(), /*isOptional=*/false);
685681

@@ -826,13 +822,13 @@ class BuilderClosureVisitor
826822
// Form a call to Array.append(_:) to add the result of executing each
827823
// iteration of the loop body to the array formed above.
828824
SourceLoc endLoc = forEachStmt->getEndLoc();
829-
auto arrayVarRef = buildVarRef(arrayVar, endLoc);
825+
auto arrayVarRef = builder.buildVarRef(arrayVar, endLoc);
830826
auto arrayAppendRef = new (ctx) UnresolvedDotExpr(
831827
arrayVarRef, endLoc, DeclNameRef(ctx.getIdentifier("append")),
832828
DeclNameLoc(endLoc), /*implicit=*/true);
833829
arrayAppendRef->setFunctionRefKind(FunctionRefKind::SingleApply);
834830

835-
auto bodyVarRef = buildVarRef(bodyVar, endLoc);
831+
auto bodyVarRef = builder.buildVarRef(bodyVar, endLoc);
836832
auto *argList = ArgumentList::createImplicit(
837833
ctx, endLoc, {Argument::unlabeled(bodyVarRef)}, endLoc);
838834
Expr *arrayAppendCall =
@@ -846,7 +842,7 @@ class BuilderClosureVisitor
846842
// Form the final call to buildArray(arrayVar) to allow the function
847843
// builder to reshape the array into whatever it wants as the result of
848844
// the for-each loop.
849-
auto finalArrayVarRef = buildVarRef(arrayVar, endLoc);
845+
auto finalArrayVarRef = builder.buildVarRef(arrayVar, endLoc);
850846
auto buildArrayCall = buildCallIfWanted(
851847
endLoc, ctx.Id_buildArray, { finalArrayVarRef }, { Identifier() });
852848
assert(buildArrayCall);
@@ -2217,3 +2213,8 @@ VarDecl *ResultBuilder::buildVar(SourceLoc loc) {
22172213
var->setImplicit();
22182214
return var;
22192215
}
2216+
2217+
DeclRefExpr *ResultBuilder::buildVarRef(VarDecl *var, SourceLoc loc) {
2218+
return new (DC->getASTContext())
2219+
DeclRefExpr(var, DeclNameLoc(loc), /*Implicit=*/true);
2220+
}

0 commit comments

Comments
 (0)