[Sema/CS] Convenience function for adding a Requirement as a constraint.

This commit is contained in:
Huon Wilson
2017-09-15 19:27:47 -07:00
parent 6338c35542
commit 96172048d3
4 changed files with 60 additions and 81 deletions

View File

@@ -4859,6 +4859,43 @@ ConstraintSystem::addKeyPathConstraint(Type keypath,
}
}
void ConstraintSystem::addConstraint(Requirement req,
ConstraintLocatorBuilder locator,
bool isFavored) {
bool conformsToAnyObject = false;
Optional<ConstraintKind> kind;
switch (req.getKind()) {
case RequirementKind::Conformance:
kind = ConstraintKind::ConformsTo;
break;
case RequirementKind::Superclass:
conformsToAnyObject = true;
kind = ConstraintKind::Subtype;
break;
case RequirementKind::SameType:
kind = ConstraintKind::Equal;
break;
case RequirementKind::Layout:
// Only a class constraint can be modeled as a constraint, and only that can
// appear outside of a @_specialize at the moment anyway.
if (req.getLayoutConstraint()->isClass()) {
conformsToAnyObject = true;
break;
}
return;
}
auto firstType = req.getFirstType();
if (kind) {
addConstraint(*kind, req.getFirstType(), req.getSecondType(), locator,
isFavored);
}
if (conformsToAnyObject) {
auto anyObject = getASTContext().getAnyObjectType();
addConstraint(ConstraintKind::ConformsTo, firstType, anyObject, locator);
}
}
void ConstraintSystem::addConstraint(ConstraintKind kind, Type first,
Type second,