Skip to content

Commit f368e5a

Browse files
committed
[ConstraintSystem] NFC: Extract Swift -> C pointer conversion simplification into a separate method
1 parent 4163881 commit f368e5a

File tree

2 files changed

+89
-77
lines changed

2 files changed

+89
-77
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4872,6 +4872,12 @@ class ConstraintSystem {
48724872
TypeMatchOptions flags,
48734873
ConstraintLocatorBuilder locator);
48744874

4875+
/// Simplify a conversion between Swift and C pointers.
4876+
SolutionKind
4877+
simplifyPointerToCPointerRestriction(Type type1, Type type2,
4878+
TypeMatchOptions flags,
4879+
ConstraintLocatorBuilder locator);
4880+
48754881
public:
48764882
/// Simplify the system of constraints, by breaking down complex
48774883
/// constraints into simpler constraints.

lib/Sema/CSSimplify.cpp

Lines changed: 83 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11152,83 +11152,8 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
1115211152
return matchPointerBaseTypes(ptr1, ptr2);
1115311153
}
1115411154

11155-
case ConversionRestrictionKind::PointerToCPointer: {
11156-
auto &ctx = getASTContext();
11157-
11158-
PointerTypeKind swiftPtrKind, cPtrKind;
11159-
11160-
auto swiftPtr =
11161-
type1->lookThroughAllOptionalTypes()->getAnyPointerElementType(
11162-
swiftPtrKind);
11163-
11164-
auto cPtr = type2->lookThroughAllOptionalTypes()->getAnyPointerElementType(
11165-
cPtrKind);
11166-
11167-
// Unsafe[Mutable]RawPointer -> Unsafe[Mutable]Pointer<[U]Int8>
11168-
if (swiftPtrKind == PTK_UnsafeRawPointer ||
11169-
swiftPtrKind == PTK_UnsafeMutableRawPointer) {
11170-
// Since it's a C pointer on parameter side it would always
11171-
// be fully resolved.
11172-
if (cPtr->isInt8() || cPtr->isUInt8())
11173-
return SolutionKind::Solved;
11174-
} else {
11175-
// Unsafe[Mutable]Pointer<T> -> Unsafe[Mutable]Pointer<[U]Int8>
11176-
if (cPtr->isInt8() || cPtr->isUInt8()) {
11177-
// <T> can default to the type of C pointer.
11178-
addConstraint(
11179-
ConstraintKind::Defaultable, swiftPtr, cPtr,
11180-
locator.withPathElement(LocatorPathElt::GenericArgument(0)));
11181-
return SolutionKind::Solved;
11182-
}
11183-
11184-
auto elementLoc =
11185-
locator.withPathElement(LocatorPathElt::GenericArgument(0));
11186-
11187-
// Unsafe[Mutable]Pointer<Int{8, 16, ...}> <->
11188-
// Unsafe[Mutable]Pointer<UInt{8, 16, ...}>
11189-
11190-
if (swiftPtr->isInt() || swiftPtr->isUInt()) {
11191-
addConstraint(ConstraintKind::Equal, cPtr,
11192-
swiftPtr->isUInt() ? ctx.getIntType() : ctx.getUIntType(),
11193-
elementLoc);
11194-
return SolutionKind::Solved;
11195-
}
11196-
11197-
if (swiftPtr->isInt8() || swiftPtr->isUInt8()) {
11198-
addConstraint(ConstraintKind::Equal, cPtr,
11199-
swiftPtr->isUInt8() ? ctx.getInt8Type()
11200-
: ctx.getUInt8Type(),
11201-
elementLoc);
11202-
return SolutionKind::Solved;
11203-
}
11204-
11205-
if (swiftPtr->isInt16() || swiftPtr->isUInt16()) {
11206-
addConstraint(ConstraintKind::Equal, cPtr,
11207-
swiftPtr->isUInt16() ? ctx.getInt16Type()
11208-
: ctx.getUInt16Type(),
11209-
elementLoc);
11210-
return SolutionKind::Solved;
11211-
}
11212-
11213-
if (swiftPtr->isInt32() || swiftPtr->isUInt32()) {
11214-
addConstraint(ConstraintKind::Equal, cPtr,
11215-
swiftPtr->isUInt32() ? ctx.getInt32Type()
11216-
: ctx.getUInt32Type(),
11217-
elementLoc);
11218-
return SolutionKind::Solved;
11219-
}
11220-
11221-
if (swiftPtr->isInt64() || swiftPtr->isUInt64()) {
11222-
addConstraint(ConstraintKind::Equal, cPtr,
11223-
swiftPtr->isUInt64() ? ctx.getInt64Type()
11224-
: ctx.getUInt64Type(),
11225-
elementLoc);
11226-
return SolutionKind::Solved;
11227-
}
11228-
}
11229-
11230-
return SolutionKind::Error;
11231-
}
11155+
case ConversionRestrictionKind::PointerToCPointer:
11156+
return simplifyPointerToCPointerRestriction(type1, type2, flags, locator);
1123211157

1123311158
// T < U or T is bridged to V where V < U ===> Array<T> <c Array<U>
1123411159
case ConversionRestrictionKind::ArrayUpcast: {
@@ -11456,6 +11381,87 @@ ConstraintSystem::simplifyRestrictedConstraint(
1145611381
llvm_unreachable("Unhandled SolutionKind in switch.");
1145711382
}
1145811383

11384+
ConstraintSystem::SolutionKind
11385+
ConstraintSystem::simplifyPointerToCPointerRestriction(
11386+
Type type1, Type type2, TypeMatchOptions flags,
11387+
ConstraintLocatorBuilder locator) {
11388+
auto &ctx = getASTContext();
11389+
11390+
PointerTypeKind swiftPtrKind, cPtrKind;
11391+
11392+
auto swiftPtr =
11393+
type1->lookThroughAllOptionalTypes()->getAnyPointerElementType(
11394+
swiftPtrKind);
11395+
11396+
auto cPtr =
11397+
type2->lookThroughAllOptionalTypes()->getAnyPointerElementType(cPtrKind);
11398+
11399+
// Unsafe[Mutable]RawPointer -> Unsafe[Mutable]Pointer<[U]Int8>
11400+
if (swiftPtrKind == PTK_UnsafeRawPointer ||
11401+
swiftPtrKind == PTK_UnsafeMutableRawPointer) {
11402+
// Since it's a C pointer on parameter side it would always
11403+
// be fully resolved.
11404+
if (cPtr->isInt8() || cPtr->isUInt8())
11405+
return SolutionKind::Solved;
11406+
} else {
11407+
// Unsafe[Mutable]Pointer<T> -> Unsafe[Mutable]Pointer<[U]Int8>
11408+
if (cPtr->isInt8() || cPtr->isUInt8()) {
11409+
// <T> can default to the type of C pointer.
11410+
addConstraint(
11411+
ConstraintKind::Defaultable, swiftPtr, cPtr,
11412+
locator.withPathElement(LocatorPathElt::GenericArgument(0)));
11413+
return SolutionKind::Solved;
11414+
}
11415+
11416+
auto elementLoc =
11417+
locator.withPathElement(LocatorPathElt::GenericArgument(0));
11418+
11419+
// Unsafe[Mutable]Pointer<Int{8, 16, ...}> <->
11420+
// Unsafe[Mutable]Pointer<UInt{8, 16, ...}>
11421+
11422+
if (swiftPtr->isInt() || swiftPtr->isUInt()) {
11423+
addConstraint(ConstraintKind::Equal, cPtr,
11424+
swiftPtr->isUInt() ? ctx.getIntType() : ctx.getUIntType(),
11425+
elementLoc);
11426+
return SolutionKind::Solved;
11427+
}
11428+
11429+
if (swiftPtr->isInt8() || swiftPtr->isUInt8()) {
11430+
addConstraint(ConstraintKind::Equal, cPtr,
11431+
swiftPtr->isUInt8() ? ctx.getInt8Type()
11432+
: ctx.getUInt8Type(),
11433+
elementLoc);
11434+
return SolutionKind::Solved;
11435+
}
11436+
11437+
if (swiftPtr->isInt16() || swiftPtr->isUInt16()) {
11438+
addConstraint(ConstraintKind::Equal, cPtr,
11439+
swiftPtr->isUInt16() ? ctx.getInt16Type()
11440+
: ctx.getUInt16Type(),
11441+
elementLoc);
11442+
return SolutionKind::Solved;
11443+
}
11444+
11445+
if (swiftPtr->isInt32() || swiftPtr->isUInt32()) {
11446+
addConstraint(ConstraintKind::Equal, cPtr,
11447+
swiftPtr->isUInt32() ? ctx.getInt32Type()
11448+
: ctx.getUInt32Type(),
11449+
elementLoc);
11450+
return SolutionKind::Solved;
11451+
}
11452+
11453+
if (swiftPtr->isInt64() || swiftPtr->isUInt64()) {
11454+
addConstraint(ConstraintKind::Equal, cPtr,
11455+
swiftPtr->isUInt64() ? ctx.getInt64Type()
11456+
: ctx.getUInt64Type(),
11457+
elementLoc);
11458+
return SolutionKind::Solved;
11459+
}
11460+
}
11461+
11462+
return SolutionKind::Error;
11463+
}
11464+
1145911465
static bool isAugmentingFix(ConstraintFix *fix) {
1146011466
switch (fix->getKind()) {
1146111467
case FixKind::TreatRValueAsLValue:

0 commit comments

Comments
 (0)