[CSSimplify] Implement logic for an implicit conversion to/from CGFloat

To support implicit conversion to/from CGFloat type we need to:

- Reference an `init` member on the right-hand side type (either
  CGFloat if we are converting to it, or compatible type if from).

- Add a `ApplicableFunction` constraint to model an implicit
  call to previously referenced initializer with an argument
  of left-hand side type.
This commit is contained in:
Pavel Yaskevich
2020-10-17 23:12:07 -07:00
parent 2b935bab7c
commit fac81884fb

View File

@@ -10490,7 +10490,28 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
case ConversionRestrictionKind::TypeToCGFloat:
case ConversionRestrictionKind::CGFloatToType: {
llvm_unreachable("not yet implemented");
increaseScore(SK_ImplicitValueConversion);
if (worseThanBestSolution())
return SolutionKind::Error;
auto *applicationLoc = getConstraintLocator(
locator, {LocatorPathElt::ImplicitConversion(restriction),
ConstraintLocator::ApplyFunction});
auto *memberLoc = getConstraintLocator(
applicationLoc, ConstraintLocator::ConstructorMember);
auto *memberTy = createTypeVariable(memberLoc, TVO_CanBindToNoEscape);
addValueMemberConstraint(MetatypeType::get(type2, getASTContext()),
DeclNameRef(DeclBaseName::createConstructor()),
memberTy, DC, FunctionRefKind::DoubleApply,
/*outerAlternatives=*/{}, memberLoc);
addConstraint(ConstraintKind::ApplicableFunction,
FunctionType::get({FunctionType::Param(type1)}, type2),
memberTy, applicationLoc);
return SolutionKind::Solved;
}
}