Skip to content

Commit 7217cfa

Browse files
committed
[Constraint system] Move statement condition constraint generation.
Move constraint generation for statement conditions onto the constraint system; it doesn't really have any reason to be located within the function builder transform.
1 parent 6238923 commit 7217cfa

File tree

3 files changed

+61
-45
lines changed

3 files changed

+61
-45
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -324,27 +324,11 @@ class BuilderClosureVisitor
324324
CONTROL_FLOW_STMT(Yield)
325325
CONTROL_FLOW_STMT(Defer)
326326

327-
/// Whether we can handle all of the conditions for this statement.
328-
static bool canHandleStmtConditions(StmtCondition condition) {
329-
for (const auto &element : condition) {
330-
switch (element.getKind()) {
331-
case StmtConditionElement::CK_Boolean:
332-
continue;
333-
334-
case StmtConditionElement::CK_PatternBinding:
335-
case StmtConditionElement::CK_Availability:
336-
return false;
337-
}
338-
}
339-
340-
return true;
341-
}
342-
343327
static bool isBuildableIfChainRecursive(IfStmt *ifStmt,
344328
unsigned &numPayloads,
345329
bool &isOptional) {
346330
// Check whether we can handle the conditional.
347-
if (!canHandleStmtConditions(ifStmt->getCond()))
331+
if (!ConstraintSystem::canGenerateConstraints(ifStmt->getCond()))
348332
return false;
349333

350334
// The 'then' clause contributes a payload.
@@ -470,38 +454,12 @@ class BuilderClosureVisitor
470454
payloadIndex + 1, numPayloads, isOptional);
471455
}
472456

473-
// Condition must convert to Bool.
474-
// FIXME: This should be folded into constraint generation for conditions.
475-
auto boolDecl = ctx.getBoolDecl();
476-
if (!boolDecl) {
457+
// Generate constraints for the conditions.
458+
if (cs->generateConstraints(ifStmt->getCond(), dc)) {
477459
hadError = true;
478460
return nullptr;
479461
}
480462

481-
// Generate constraints for the conditions.
482-
for (const auto &condElement : ifStmt->getCond()) {
483-
switch (condElement.getKind()) {
484-
case StmtConditionElement::CK_Boolean: {
485-
Expr *condExpr = condElement.getBoolean();
486-
condExpr = cs->generateConstraints(condExpr, dc);
487-
if (!condExpr) {
488-
hadError = true;
489-
return nullptr;
490-
}
491-
492-
cs->addConstraint(ConstraintKind::Conversion,
493-
cs->getType(condExpr),
494-
boolDecl->getDeclaredType(),
495-
cs->getConstraintLocator(condExpr));
496-
continue;
497-
}
498-
499-
case StmtConditionElement::CK_PatternBinding:
500-
case StmtConditionElement::CK_Availability:
501-
llvm_unreachable("unhandled statement condition");
502-
}
503-
}
504-
505463
// The operand should have optional type if we had optional results,
506464
// so we just need to call `buildIf` now, since we're at the top level.
507465
if (isOptional && isTopLevel) {

lib/Sema/CSGen.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3798,6 +3798,54 @@ Type ConstraintSystem::generateConstraints(Pattern *pattern,
37983798
return cg.getTypeForPattern(pattern, locator);
37993799
}
38003800

3801+
bool ConstraintSystem::canGenerateConstraints(StmtCondition condition) {
3802+
for (const auto &element : condition) {
3803+
switch (element.getKind()) {
3804+
case StmtConditionElement::CK_Boolean:
3805+
continue;
3806+
3807+
case StmtConditionElement::CK_PatternBinding:
3808+
case StmtConditionElement::CK_Availability:
3809+
return false;
3810+
}
3811+
}
3812+
3813+
return true;
3814+
}
3815+
3816+
bool ConstraintSystem::generateConstraints(StmtCondition condition,
3817+
DeclContext *dc) {
3818+
// FIXME: This should be folded into constraint generation for conditions.
3819+
auto boolDecl = getASTContext().getBoolDecl();
3820+
if (!boolDecl) {
3821+
return true;
3822+
}
3823+
3824+
for (const auto &condElement : condition) {
3825+
switch (condElement.getKind()) {
3826+
case StmtConditionElement::CK_Boolean: {
3827+
Expr *condExpr = condElement.getBoolean();
3828+
condExpr = generateConstraints(condExpr, dc);
3829+
if (!condExpr) {
3830+
return true;
3831+
}
3832+
3833+
addConstraint(ConstraintKind::Conversion,
3834+
getType(condExpr),
3835+
boolDecl->getDeclaredType(),
3836+
getConstraintLocator(condExpr));
3837+
continue;
3838+
}
3839+
3840+
case StmtConditionElement::CK_PatternBinding:
3841+
case StmtConditionElement::CK_Availability:
3842+
llvm_unreachable("unhandled statement condition");
3843+
}
3844+
}
3845+
3846+
return false;
3847+
}
3848+
38013849
void ConstraintSystem::optimizeConstraints(Expr *e) {
38023850
if (getASTContext().TypeCheckerOpts.DisableConstraintSolverPerformanceHacks)
38033851
return;

lib/Sema/ConstraintSystem.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3065,6 +3065,16 @@ class ConstraintSystem {
30653065
/// \returns a possibly-sanitized initializer, or null if an error occurred.
30663066
Type generateConstraints(Pattern *P, ConstraintLocatorBuilder locator);
30673067

3068+
/// Determines whether we can generate constraints for this statement
3069+
/// condition.
3070+
static bool canGenerateConstraints(StmtCondition condition);
3071+
3072+
/// Generate constraints for a statement condition.
3073+
///
3074+
/// \returns true if there was an error in constraint generation, false
3075+
/// if generation succeeded.
3076+
bool generateConstraints(StmtCondition condition, DeclContext *dc);
3077+
30683078
/// Generate constraints for a given set of overload choices.
30693079
///
30703080
/// \param constraints The container of generated constraint choices.

0 commit comments

Comments
 (0)