Skip to content

Commit d92ae77

Browse files
committedMar 18, 2016
Eliminate most remaining uses of _convertNSFooToFoo and _convertFooToNSFoo.
Generalized bridging has fully subsumed most of these. NSError is still special, and _convertStringToNSString remains for the the runtime's implementation of SwiftObject's -description method.
1 parent 4c49e67 commit d92ae77

File tree

11 files changed

+103
-213
lines changed

11 files changed

+103
-213
lines changed
 

‎lib/SILGen/SILGen.cpp

+1-9
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,9 @@ GET_BRIDGING_FN(ObjectiveC, REQUIRED, Bool, REQUIRED, ObjCBool)
146146
GET_BRIDGING_FN(ObjectiveC, REQUIRED, ObjCBool, REQUIRED, Bool)
147147
GET_BRIDGING_FN(Foundation, OPTIONAL, NSError, REQUIRED, ErrorProtocol)
148148
GET_BRIDGING_FN(Foundation, REQUIRED, ErrorProtocol, REQUIRED, NSError)
149-
GET_BRIDGING_FN(Foundation, REQUIRED, String, REQUIRED, NSString)
150-
GET_BRIDGING_FN(Foundation, OPTIONAL, NSString, REQUIRED, String)
151-
GET_BRIDGING_FN(Foundation, GENERIC, Array, REQUIRED, NSArray)
152-
GET_BRIDGING_FN(Foundation, OPTIONAL, NSArray, GENERIC, Array)
153-
GET_BRIDGING_FN(Foundation, GENERIC, Set, REQUIRED, NSSet)
154-
GET_BRIDGING_FN(Foundation, OPTIONAL, NSSet, GENERIC, Set)
155-
GET_BRIDGING_FN(Foundation, GENERIC, Dictionary, REQUIRED, NSDictionary)
156-
GET_BRIDGING_FN(Foundation, OPTIONAL, NSDictionary, GENERIC, Dictionary)
157149

158150
#undef GET_BRIDGING_FN
159-
#undef REQURIED
151+
#undef REQUIRED
160152
#undef OPTIONAL
161153
#undef GENERIC
162154

‎lib/Sema/TypeCheckDecl.cpp

-15
Original file line numberDiff line numberDiff line change
@@ -2172,21 +2172,6 @@ static void checkBridgedFunctions(TypeChecker &TC) {
21722172
#include "swift/SIL/BridgedTypes.def"
21732173

21742174
if (Module *module = TC.Context.getLoadedModule(TC.Context.Id_Foundation)) {
2175-
checkObjCBridgingFunctions(TC, module,
2176-
TC.Context.getSwiftName(
2177-
KnownFoundationEntity::NSArray),
2178-
"_convertNSArrayToArray",
2179-
"_convertArrayToNSArray");
2180-
checkObjCBridgingFunctions(TC, module,
2181-
TC.Context.getSwiftName(
2182-
KnownFoundationEntity::NSDictionary),
2183-
"_convertNSDictionaryToDictionary",
2184-
"_convertDictionaryToNSDictionary");
2185-
checkObjCBridgingFunctions(TC, module,
2186-
TC.Context.getSwiftName(
2187-
KnownFoundationEntity::NSSet),
2188-
"_convertNSSetToSet",
2189-
"_convertSetToNSSet");
21902175
checkObjCBridgingFunctions(TC, module,
21912176
TC.Context.getSwiftName(
21922177
KnownFoundationEntity::NSError),

‎stdlib/public/SDK/Foundation/Foundation.swift

-124
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,6 @@ func _convertStringToNSString(string: String) -> NSString {
7575
return string._bridgeToObjectiveC()
7676
}
7777

78-
@warn_unused_result
79-
@_semantics("convertFromObjectiveC")
80-
public // COMPILER_INTRINSIC
81-
func _convertNSStringToString(nsstring: NSString?) -> String {
82-
if nsstring == nil { return "" }
83-
var result: String?
84-
String._forceBridgeFromObjectiveC(nsstring!, result: &result)
85-
return result!
86-
}
87-
8878
extension NSString : StringLiteralConvertible {
8979
/// Create an instance initialized to `value`.
9080
public required convenience init(unicodeScalarLiteral value: StaticString) {
@@ -461,33 +451,6 @@ extension NSArray : ArrayLiteralConvertible {
461451
}
462452
}
463453

464-
/// The entry point for converting `NSArray` to `Array` in bridge
465-
/// thunks. Used, for example, to expose :
466-
///
467-
/// func f([NSView]) {}
468-
///
469-
/// to Objective-C code as a method that accepts an `NSArray`. This operation
470-
/// is referred to as a "forced conversion" in ../../../docs/Arrays.rst
471-
@warn_unused_result
472-
@_semantics("convertFromObjectiveC")
473-
public func _convertNSArrayToArray<T>(source: NSArray?) -> [T] {
474-
if _slowPath(source == nil) { return [] }
475-
var result: [T]?
476-
Array._forceBridgeFromObjectiveC(source!, result: &result)
477-
return result!
478-
}
479-
480-
/// The entry point for converting `Array` to `NSArray` in bridge
481-
/// thunks. Used, for example, to expose :
482-
///
483-
/// func f() -> [NSView] { return [] }
484-
///
485-
/// to Objective-C code as a method that returns an `NSArray`.
486-
@warn_unused_result
487-
public func _convertArrayToNSArray<T>(array: [T]) -> NSArray {
488-
return array._bridgeToObjectiveC()
489-
}
490-
491454
extension Array : _ObjectiveCBridgeable {
492455

493456
/// Private initializer used for bridging.
@@ -626,54 +589,6 @@ extension Dictionary {
626589
}
627590
}
628591

629-
/// The entry point for bridging `NSDictionary` to `Dictionary` in bridge
630-
/// thunks. Used, for example, to expose:
631-
///
632-
/// func f([String : String]) {}
633-
///
634-
/// to Objective-C code as a method that accepts an `NSDictionary`.
635-
///
636-
/// This is a forced downcast. This operation should have O(1) complexity
637-
/// when `Key` and `Value` are bridged verbatim.
638-
///
639-
/// The cast can fail if bridging fails. The actual checks and bridging can be
640-
/// deferred.
641-
@warn_unused_result
642-
@_semantics("convertFromObjectiveC")
643-
public func _convertNSDictionaryToDictionary<
644-
Key : Hashable, Value
645-
>(d: NSDictionary?) -> [Key : Value] {
646-
// Note: there should be *a good justification* for doing something else
647-
// than just dispatching to `_forceBridgeFromObjectiveC`.
648-
if _slowPath(d == nil) { return [:] }
649-
var result: [Key : Value]?
650-
Dictionary._forceBridgeFromObjectiveC(d!, result: &result)
651-
return result!
652-
}
653-
654-
// FIXME: right now the following is O(n), not O(1).
655-
656-
/// The entry point for bridging `Dictionary` to `NSDictionary` in bridge
657-
/// thunks. Used, for example, to expose:
658-
///
659-
/// func f() -> [String : String] {}
660-
///
661-
/// to Objective-C code as a method that returns an `NSDictionary`.
662-
///
663-
/// This is a forced downcast. This operation should have O(1) complexity.
664-
///
665-
/// The cast can fail if bridging fails. The actual checks and bridging can be
666-
/// deferred.
667-
@warn_unused_result
668-
public func _convertDictionaryToNSDictionary<Key, Value>(
669-
d: [Key : Value]
670-
) -> NSDictionary {
671-
672-
// Note: there should be *a good justification* for doing something else
673-
// than just dispatching to `_bridgeToObjectiveC`.
674-
return d._bridgeToObjectiveC()
675-
}
676-
677592
// Dictionary<Key, Value> is conditionally bridged to NSDictionary
678593
extension Dictionary : _ObjectiveCBridgeable {
679594
public static func _getObjectiveCType() -> Any.Type {
@@ -925,45 +840,6 @@ extension NSIndexSet : Sequence {
925840
}
926841
}
927842

928-
// FIXME: right now the following is O(n), not O(1).
929-
930-
/// The entry point for bridging `Set` to `NSSet` in bridge
931-
/// thunks. Used, for example, to expose:
932-
///
933-
/// func f() -> Set<String> {}
934-
///
935-
/// to Objective-C code as a method that returns an `NSSet`.
936-
///
937-
/// This is a forced downcast. This operation should have O(1) complexity.
938-
///
939-
/// The cast can fail if bridging fails. The actual checks and bridging can be
940-
/// deferred.
941-
@warn_unused_result
942-
public func _convertSetToNSSet<T>(s: Set<T>) -> NSSet {
943-
return s._bridgeToObjectiveC()
944-
}
945-
946-
/// The entry point for bridging `NSSet` to `Set` in bridge
947-
/// thunks. Used, for example, to expose:
948-
///
949-
/// func f(Set<String>) {}
950-
///
951-
/// to Objective-C code as a method that accepts an `NSSet`.
952-
///
953-
/// This is a forced downcast. This operation should have O(1) complexity
954-
/// when `T` is bridged verbatim.
955-
///
956-
/// The cast can fail if bridging fails. The actual checks and bridging can be
957-
/// deferred.
958-
@warn_unused_result
959-
@_semantics("convertFromObjectiveC")
960-
public func _convertNSSetToSet<T : Hashable>(s: NSSet?) -> Set<T> {
961-
if _slowPath(s == nil) { return [] }
962-
var result: Set<T>?
963-
Set._forceBridgeFromObjectiveC(s!, result: &result)
964-
return result!
965-
}
966-
967843
// Set<Element> is conditionally bridged to NSSet
968844
extension Set : _ObjectiveCBridgeable {
969845
public static func _getObjectiveCType() -> Any.Type {

‎stdlib/public/SDK/Foundation/NSStringAPI.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ extension String {
384384
if let matches = nsMatches {
385385
// Since this function is effectively a bridge thunk, use the
386386
// bridge thunk semantics for the NSArray conversion
387-
matchesIntoArray._setIfNonNil { _convertNSArrayToArray(matches) }
387+
matchesIntoArray._setIfNonNil { return matches as! [String] }
388388
}
389389

390390
if let n = nsOutputName {
@@ -406,7 +406,7 @@ extension String {
406406
let nsa = _ns.componentsSeparatedByCharacters(in: separator) as NSArray
407407
// Since this function is effectively a bridge thunk, use the
408408
// bridge thunk semantics for the NSArray conversion
409-
return _convertNSArrayToArray(nsa)
409+
return nsa as! [String]
410410
}
411411

412412

@@ -418,7 +418,7 @@ extension String {
418418
let nsa = _ns.componentsSeparated(by: separator) as NSArray
419419
// Since this function is effectively a bridge thunk, use the
420420
// bridge thunk semantics for the NSArray conversion
421-
return _convertNSArrayToArray(nsa)
421+
return nsa as! [String]
422422
}
423423

424424
// - (const char *)cStringUsingEncoding:(NSStringEncoding)encoding
@@ -1040,7 +1040,7 @@ extension String {
10401040
}
10411041
}
10421042

1043-
return _convertNSArrayToArray(result)
1043+
return result as! [String]
10441044
}
10451045

10461046
// - (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)aString

‎test/1_stdlib/ArrayTrapsObjC.swift.gyb

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class ViolateInoutSafetySwitchToObjcBuffer {
115115

116116
@inline(never)
117117
func accessArrayViaInoutViolation() {
118-
anArray = _convertNSArrayToArray(nsArray)
118+
anArray = nsArray as! [ElementClass]
119119
}
120120

121121
@inline(never)

‎test/1_stdlib/Inputs/DictionaryKeyValueTypesObjC.swift

+31-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ import Darwin
33
import StdlibUnittest
44
import Foundation
55

6+
func convertDictionaryToNSDictionary<Key, Value>(
7+
d: [Key : Value]
8+
) -> NSDictionary {
9+
return d._bridgeToObjectiveC()
10+
}
11+
12+
public func convertNSDictionaryToDictionary<
13+
Key : Hashable, Value
14+
>(d: NSDictionary?) -> [Key : Value] {
15+
if _slowPath(d == nil) { return [:] }
16+
var result: [Key : Value]?
17+
Dictionary._forceBridgeFromObjectiveC(d!, result: &result)
18+
return result!
19+
}
20+
621
func isNativeDictionary<KeyTy : Hashable, ValueTy>(
722
d: Dictionary<KeyTy, ValueTy>) -> Bool {
823
switch d._variantStorage {
@@ -426,7 +441,7 @@ func getBridgedNSDictionaryOfRefTypesBridgedVerbatim() -> NSDictionary {
426441
d[TestObjCKeyTy(30)] = TestObjCValueTy(1030)
427442

428443
let bridged =
429-
unsafeBitCast(_convertDictionaryToNSDictionary(d), to: NSDictionary.self)
444+
unsafeBitCast(convertDictionaryToNSDictionary(d), to: NSDictionary.self)
430445

431446
assert(isNativeNSDictionary(bridged))
432447

@@ -437,7 +452,7 @@ func getBridgedEmptyNSDictionary() -> NSDictionary {
437452
let d = Dictionary<TestObjCKeyTy, TestObjCValueTy>()
438453

439454
let bridged =
440-
unsafeBitCast(_convertDictionaryToNSDictionary(d), to: NSDictionary.self)
455+
unsafeBitCast(convertDictionaryToNSDictionary(d), to: NSDictionary.self)
441456
assert(isNativeNSDictionary(bridged))
442457

443458
return bridged
@@ -454,7 +469,7 @@ func getBridgedNSDictionaryOfKeyValue_ValueTypesCustomBridged(
454469
d[TestBridgedKeyTy(i * 10)] = TestBridgedValueTy(i * 10 + 1000)
455470
}
456471

457-
let bridged = _convertDictionaryToNSDictionary(d)
472+
let bridged = convertDictionaryToNSDictionary(d)
458473
assert(isNativeNSDictionary(bridged))
459474

460475
return bridged
@@ -906,12 +921,23 @@ func getBridgedNSArrayOfRefTypeVerbatimBridged(
906921
a.append(TestObjCValueTy(i * 10))
907922
}
908923

909-
let bridged = _convertArrayToNSArray(a)
924+
let bridged = convertArrayToNSArray(a)
910925
assert(isNativeNSArray(bridged))
911926

912927
return bridged
913928
}
914929

930+
func convertNSArrayToArray<T>(source: NSArray?) -> [T] {
931+
if _slowPath(source == nil) { return [] }
932+
var result: [T]?
933+
Array._forceBridgeFromObjectiveC(source!, result: &result)
934+
return result!
935+
}
936+
937+
func convertArrayToNSArray<T>(array: [T]) -> NSArray {
938+
return array._bridgeToObjectiveC()
939+
}
940+
915941
func getBridgedNSArrayOfValueTypeCustomBridged(
916942
numElements numElements: Int = 3,
917943
capacity: Int? = nil
@@ -926,7 +952,7 @@ func getBridgedNSArrayOfValueTypeCustomBridged(
926952
a.append(TestBridgedValueTy(i * 10))
927953
}
928954

929-
let bridged = _convertArrayToNSArray(a)
955+
let bridged = convertArrayToNSArray(a)
930956
assert(isNativeNSArray(bridged))
931957

932958
return bridged

‎test/1_stdlib/NewArray.swift.gyb

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func nsArrayOfStrings() -> Array<NSString> {
226226

227227
return src.withUnsafeBufferPointer {
228228
let ns = NSArray(objects: UnsafePointer($0.baseAddress), count: $0.count)
229-
return _convertNSArrayToArray(ns)
229+
return ns as! [NSString]
230230
}
231231
}
232232

‎test/Interpreter/SDK/objc_implicit_unwrapped_bridge.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func testConvertToArrayOfImplicitUnwrappedClass() {
116116
nsarr.add(X(value: 1))
117117
nsarr.add(X(value: 2))
118118

119-
var arr: [X!] = _convertNSArrayToArray(nsarr)
119+
var arr: [X!] = nsarr as! [X!]
120120

121121
// CHECK: Class array count = 2
122122
// CHECK: Element 0 has value X(1)
@@ -140,7 +140,7 @@ func testConvertToArrayOfImplicitUnwrappedString() {
140140
nsarr.add(NSString(string: "Hello"))
141141
nsarr.add(NSString(string: "World"))
142142

143-
var arr: [String!] = _convertNSArrayToArray(nsarr)
143+
var arr: [String!] = nsarr as! [String!]
144144

145145
// CHECK: String array count = 2
146146
// CHECK: Element 0 has value Hello

0 commit comments

Comments
 (0)