Skip to content

Commit 3a41f7a

Browse files
committed
[ConstraintSystem] Add a new conversion - Swift to C pointers
Following pointer conversions are supported in argument positions (when referencing C/ObjC functions): - Unsafe[Mutable]RawPointer -> Unsafe[Mutable]Pointer<[U]Int> - Unsafe[Mutable]Pointer<Int{8, 16, ...}> -> Unsafe[Mutable]Pointer<UInt{8, 16, ...}>
1 parent 8304e6c commit 3a41f7a

File tree

6 files changed

+15
-2
lines changed

6 files changed

+15
-2
lines changed

include/swift/Sema/Constraint.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ enum class ConversionRestrictionKind {
269269
/// Implicit conversion from a value of CGFloat type to a value of Double type
270270
/// via an implicit Double initializer call passing a CGFloat value.
271271
CGFloatToDouble,
272+
/// Implicit conversion between Swift and C pointers:
273+
// - Unsafe[Mutable]RawPointer -> Unsafe[Mutable]Pointer<[U]Int>
274+
// - Unsafe[Mutable]Pointer<Int{8, 16, ...}> -> Unsafe[Mutable]Pointer<UInt{8, 16, ...}>
275+
PointerToCPointer,
272276
};
273277

274278
/// Specifies whether a given conversion requires the creation of a temporary

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6645,7 +6645,8 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
66456645
return result;
66466646
}
66476647

6648-
case ConversionRestrictionKind::PointerToPointer: {
6648+
case ConversionRestrictionKind::PointerToPointer:
6649+
case ConversionRestrictionKind::PointerToCPointer: {
66496650
TypeChecker::requirePointerArgumentIntrinsics(ctx, expr->getLoc());
66506651
Type unwrappedToTy = toType->getOptionalObjectType();
66516652

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6691,6 +6691,7 @@ void NonEphemeralConversionFailure::emitSuggestionNotes() const {
66916691
case ConversionRestrictionKind::ExistentialMetatypeToAnyObject:
66926692
case ConversionRestrictionKind::ProtocolMetatypeToProtocolClass:
66936693
case ConversionRestrictionKind::PointerToPointer:
6694+
case ConversionRestrictionKind::PointerToCPointer:
66946695
case ConversionRestrictionKind::ArrayUpcast:
66956696
case ConversionRestrictionKind::DictionaryUpcast:
66966697
case ConversionRestrictionKind::SetUpcast:

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11114,7 +11114,11 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
1111411114

1111511115
return matchPointerBaseTypes(ptr1, ptr2);
1111611116
}
11117-
11117+
11118+
case ConversionRestrictionKind::PointerToCPointer: {
11119+
return SolutionKind::Error; // not yet implemented
11120+
}
11121+
1111811122
// T < U or T is bridged to V where V < U ===> Array<T> <c Array<U>
1111911123
case ConversionRestrictionKind::ArrayUpcast: {
1112011124
Type baseType1 = *isArrayType(type1);

lib/Sema/Constraint.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,8 @@ StringRef swift::constraints::getName(ConversionRestrictionKind kind) {
546546
return "[inout-to-pointer]";
547547
case ConversionRestrictionKind::PointerToPointer:
548548
return "[pointer-to-pointer]";
549+
case ConversionRestrictionKind::PointerToCPointer:
550+
return "[pointer-to-c-pointer]";
549551
case ConversionRestrictionKind::ArrayUpcast:
550552
return "[array-upcast]";
551553
case ConversionRestrictionKind::DictionaryUpcast:

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5129,6 +5129,7 @@ ConstraintSystem::isConversionEphemeral(ConversionRestrictionKind conversion,
51295129
case ConversionRestrictionKind::ExistentialMetatypeToAnyObject:
51305130
case ConversionRestrictionKind::ProtocolMetatypeToProtocolClass:
51315131
case ConversionRestrictionKind::PointerToPointer:
5132+
case ConversionRestrictionKind::PointerToCPointer:
51325133
case ConversionRestrictionKind::ArrayUpcast:
51335134
case ConversionRestrictionKind::DictionaryUpcast:
51345135
case ConversionRestrictionKind::SetUpcast:

0 commit comments

Comments
 (0)