Skip to content

Commit 4cf0389

Browse files
authored
Merge pull request #8063 from gottesmm/cast_to_native_object
2 parents 5ff4656 + 6d7b11c commit 4cf0389

File tree

10 files changed

+44
-14
lines changed

10 files changed

+44
-14
lines changed

include/swift/AST/Builtins.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,15 @@ BUILTIN_SIL_OPERATION(CastToUnknownObject, "castToUnknownObject", Special)
206206
BUILTIN_SIL_OPERATION(CastFromUnknownObject, "castFromUnknownObject", Special)
207207

208208
/// CastToNativeObject has type (T) -> Builtin.NativeObject.
209+
///
210+
/// This builtin asserts if the underlying type /could/ be objc.
209211
BUILTIN_SIL_OPERATION(CastToNativeObject, "castToNativeObject", Special)
210212

213+
/// UnsafeCastToNativeObject has type (T) -> Builtin.NativeObject.
214+
///
215+
/// This builtin does not check if the underlying type /could/ be objc.
216+
BUILTIN_SIL_OPERATION(UnsafeCastToNativeObject, "unsafeCastToNativeObject", Special)
217+
211218
/// CastFromNativeObject has type (Builtin.NativeObject) -> T
212219
BUILTIN_SIL_OPERATION(CastFromNativeObject, "castFromNativeObject", Special)
213220

lib/AST/Builtins.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ static ValueDecl *getNativeObjectCast(ASTContext &Context, Identifier Id,
814814

815815
BuiltinGenericSignatureBuilder builder(Context);
816816
if (BV == BuiltinValueKind::CastToNativeObject ||
817+
BV == BuiltinValueKind::UnsafeCastToNativeObject ||
817818
BV == BuiltinValueKind::CastToUnknownObject ||
818819
BV == BuiltinValueKind::BridgeToRawPointer) {
819820
builder.addParameter(makeGenericParam());
@@ -1661,6 +1662,7 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
16611662
return getDeallocOperation(Context, Id);
16621663

16631664
case BuiltinValueKind::CastToNativeObject:
1665+
case BuiltinValueKind::UnsafeCastToNativeObject:
16641666
case BuiltinValueKind::CastFromNativeObject:
16651667
case BuiltinValueKind::CastToUnknownObject:
16661668
case BuiltinValueKind::CastFromUnknownObject:

lib/SIL/SILOwnershipVerifier.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,7 @@ BUILTINS_THAT_SHOULD_HAVE_BEEN_LOWERED_TO_SILINSTS(Init)
11181118
BUILTINS_THAT_SHOULD_HAVE_BEEN_LOWERED_TO_SILINSTS(CastToUnknownObject)
11191119
BUILTINS_THAT_SHOULD_HAVE_BEEN_LOWERED_TO_SILINSTS(CastFromUnknownObject)
11201120
BUILTINS_THAT_SHOULD_HAVE_BEEN_LOWERED_TO_SILINSTS(CastToNativeObject)
1121+
BUILTINS_THAT_SHOULD_HAVE_BEEN_LOWERED_TO_SILINSTS(UnsafeCastToNativeObject)
11211122
BUILTINS_THAT_SHOULD_HAVE_BEEN_LOWERED_TO_SILINSTS(CastFromNativeObject)
11221123
BUILTINS_THAT_SHOULD_HAVE_BEEN_LOWERED_TO_SILINSTS(CastToBridgeObject)
11231124
BUILTINS_THAT_SHOULD_HAVE_BEEN_LOWERED_TO_SILINSTS(

lib/SIL/SILValue.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,7 @@ CONSTANT_OWNERSHIP_BUILTIN(Trivial, FCMP_UNO)
691691
CONSTANT_OWNERSHIP_BUILTIN(Unowned, CastToUnknownObject)
692692
CONSTANT_OWNERSHIP_BUILTIN(Unowned, CastFromUnknownObject)
693693
CONSTANT_OWNERSHIP_BUILTIN(Unowned, CastToNativeObject)
694+
CONSTANT_OWNERSHIP_BUILTIN(Unowned, UnsafeCastToNativeObject)
694695
CONSTANT_OWNERSHIP_BUILTIN(Unowned, CastFromNativeObject)
695696
CONSTANT_OWNERSHIP_BUILTIN(Unowned, CastToBridgeObject)
696697
CONSTANT_OWNERSHIP_BUILTIN(Unowned, CastReferenceFromBridgeObject)

lib/SILGen/SILGenBuiltin.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ static ManagedValue emitCastToReferenceType(SILGenFunction &gen,
361361
return ManagedValue(result, cleanup);
362362
}
363363

364-
/// Specialized emitter for Builtin.castToNativeObject.
365-
static ManagedValue emitBuiltinCastToNativeObject(SILGenFunction &gen,
364+
/// Specialized emitter for Builtin.unsafeCastToNativeObject.
365+
static ManagedValue emitBuiltinUnsafeCastToNativeObject(SILGenFunction &gen,
366366
SILLocation loc,
367367
SubstitutionList substitutions,
368368
ArrayRef<ManagedValue> args,
@@ -372,6 +372,25 @@ static ManagedValue emitBuiltinCastToNativeObject(SILGenFunction &gen,
372372
SILType::getNativeObjectType(gen.F.getASTContext()));
373373
}
374374

375+
376+
/// Specialized emitter for Builtin.castToNativeObject.
377+
static ManagedValue emitBuiltinCastToNativeObject(SILGenFunction &gen,
378+
SILLocation loc,
379+
SubstitutionList substitutions,
380+
ArrayRef<ManagedValue> args,
381+
CanFunctionType formalApplyType,
382+
SGFContext C) {
383+
CanType ty = args[0].getType().getSwiftRValueType();
384+
(void)ty;
385+
assert(ty->usesNativeReferenceCounting(ResilienceExpansion::Maximal) &&
386+
"Can only cast types that use native reference counting to native "
387+
"object");
388+
return emitBuiltinUnsafeCastToNativeObject(gen, loc, substitutions,
389+
args, formalApplyType,
390+
C);
391+
}
392+
393+
375394
/// Specialized emitter for Builtin.castToUnknownObject.
376395
static ManagedValue emitBuiltinCastToUnknownObject(SILGenFunction &gen,
377396
SILLocation loc,

stdlib/public/core/Arrays.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ public struct ${Self}<Element>
762762
/// - Precondition: The array has a native buffer.
763763
@_semantics("array.owner")
764764
internal func _getOwnerWithSemanticLabel_native() -> Builtin.NativeObject {
765-
return Builtin.castToNativeObject(_buffer.nativeOwner)
765+
return Builtin.unsafeCastToNativeObject(_buffer.nativeOwner)
766766
}
767767

768768
/// - Precondition: The array has a native buffer.
@@ -779,7 +779,7 @@ public struct ${Self}<Element>
779779
#endif
780780
// In the value typed case the extra call to
781781
// _getOwnerWithSemanticLabel_native hinders optimization.
782-
return Builtin.castToNativeObject(_buffer.owner)
782+
return Builtin.unsafeCastToNativeObject(_buffer.owner)
783783
}
784784

785785
// FIXME(ABI)#12 : move to an initializer on _Buffer.

stdlib/public/core/Equatable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ public func === (lhs: AnyObject?, rhs: AnyObject?) -> Bool {
236236
switch (lhs, rhs) {
237237
case let (l?, r?):
238238
return Bool(Builtin.cmp_eq_RawPointer(
239-
Builtin.bridgeToRawPointer(Builtin.castToNativeObject(l)),
240-
Builtin.bridgeToRawPointer(Builtin.castToNativeObject(r))
239+
Builtin.bridgeToRawPointer(Builtin.castToUnknownObject(l)),
240+
Builtin.bridgeToRawPointer(Builtin.castToUnknownObject(r))
241241
))
242242
case (nil, nil):
243243
return true

stdlib/public/core/HeapBuffer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ struct _HeapBuffer<Value, Element> : Equatable {
132132

133133
public // @testable
134134
init(_ storage: _HeapBufferStorage<Value, Element>) {
135-
self._storage = Builtin.castToNativeObject(storage)
135+
self._storage = Builtin.unsafeCastToNativeObject(storage)
136136
}
137137

138138
internal init(_ storage: AnyObject) {
139139
_sanityCheck(
140140
_usesNativeSwiftReferenceCounting(type(of: storage)),
141141
"HeapBuffer manages only native objects"
142142
)
143-
self._storage = Builtin.castToNativeObject(storage)
143+
self._storage = Builtin.unsafeCastToNativeObject(storage)
144144
}
145145

146146
internal init<T : AnyObject>(_ storage: T?) {
@@ -172,7 +172,7 @@ struct _HeapBuffer<Value, Element> : Equatable {
172172
bufferType: storageClass,
173173
size: totalSize,
174174
alignmentMask: alignMask)
175-
self._storage = Builtin.castToNativeObject(object)
175+
self._storage = Builtin.unsafeCastToNativeObject(object)
176176
self._value.initialize(to: initializer)
177177
}
178178

stdlib/public/core/ManagedBuffer.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public struct ManagedBufferPointer<Header, Element> : Equatable {
202202
public init(unsafeBufferObject buffer: AnyObject) {
203203
ManagedBufferPointer._checkValidBufferClass(type(of: buffer))
204204

205-
self._nativeBuffer = Builtin.castToNativeObject(buffer)
205+
self._nativeBuffer = Builtin.unsafeCastToNativeObject(buffer)
206206
}
207207

208208
/// Internal version for use by _ContiguousArrayBuffer where we know that we
@@ -216,7 +216,7 @@ public struct ManagedBufferPointer<Header, Element> : Equatable {
216216
@_versioned
217217
internal init(_uncheckedUnsafeBufferObject buffer: AnyObject) {
218218
ManagedBufferPointer._sanityCheckValidBufferClass(type(of: buffer))
219-
self._nativeBuffer = Builtin.castToNativeObject(buffer)
219+
self._nativeBuffer = Builtin.unsafeCastToNativeObject(buffer)
220220
}
221221

222222
/// The stored `Header` instance.
@@ -330,15 +330,15 @@ public struct ManagedBufferPointer<Header, Element> : Equatable {
330330
size: totalSize,
331331
alignmentMask: _My._alignmentMask)
332332

333-
self._nativeBuffer = Builtin.castToNativeObject(newBuffer)
333+
self._nativeBuffer = Builtin.unsafeCastToNativeObject(newBuffer)
334334
}
335335

336336
/// Manage the given `buffer`.
337337
///
338338
/// - Note: It is an error to use the `header` property of the resulting
339339
/// instance unless it has been initialized.
340340
internal init(_ buffer: ManagedBuffer<Header, Element>) {
341-
_nativeBuffer = Builtin.castToNativeObject(buffer)
341+
_nativeBuffer = Builtin.unsafeCastToNativeObject(buffer)
342342
}
343343

344344
internal typealias _My = ManagedBufferPointer

test/SILGen/builtins.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func class_archetype_to_unknown_object<T : C>(_ t: T) -> Builtin.UnknownObject {
236236
func class_existential_to_native_object(_ t:ClassProto) -> Builtin.NativeObject {
237237
// CHECK: [[REF:%[0-9]+]] = open_existential_ref [[T:%[0-9]+]] : $ClassProto
238238
// CHECK: [[PTR:%[0-9]+]] = unchecked_ref_cast [[REF]] : $@opened({{.*}}) ClassProto to $Builtin.NativeObject
239-
return Builtin.castToNativeObject(t)
239+
return Builtin.unsafeCastToNativeObject(t)
240240
}
241241

242242
// CHECK-LABEL: sil hidden @_T08builtins35class_existential_to_unknown_object{{[_0-9a-zA-Z]*}}F

0 commit comments

Comments
 (0)