Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/SIL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,7 @@ typed, so aliasing of classes is constrained by the type system as follows:
A violation of the above aliasing rules only results in undefined
behavior if the aliasing references are dereferenced within Swift code.
For example,
``_SwiftNativeNS[Array|Dictionary|String]`` classes alias with
``__SwiftNativeNS[Array|Dictionary|String]`` classes alias with
``NS[Array|Dictionary|String]`` classes even though they are not
statically related. Since Swift never directly accesses stored
properties on the Foundation classes, this aliasing does not pose a
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Remote/MetadataReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class MetadataReader {
// error existential with NSError-compatible layout.
std::string ObjCClassName;
if (readObjCClassName(*MetadataAddress, ObjCClassName)) {
if (ObjCClassName == "_SwiftNativeNSError")
if (ObjCClassName == "__SwiftNativeNSError")
isObjC = true;
} else {
// Otherwise, we can check to see if this is a class metadata with the
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/LoopTransforms/COWArrayOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ bool COWArrayOpt::checkSafeArrayElementUse(SILInstruction *UseInst,
// %57 = load %56 : $*Builtin.BridgeObject from Array<Int>
// %58 = unchecked_ref_cast %57 : $Builtin.BridgeObject to
// $_ContiguousArray
// %59 = unchecked_ref_cast %58 : $_ContiguousArrayStorageBase to
// %59 = unchecked_ref_cast %58 : $__ContiguousArrayStorageBase to
// $Builtin.NativeObject
// %60 = struct_extract %53 : $UnsafeMutablePointer<Int>,
// #UnsafeMutablePointer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal var _temporaryLocaleCurrentLocale: NSLocale?

extension NSLocale {
@objc
public class func _swiftUnittest_currentLocale() -> NSLocale {
public class func __swiftUnittest_currentLocale() -> NSLocale {
return _temporaryLocaleCurrentLocale!
}
}
Expand All @@ -34,9 +34,9 @@ public func withOverriddenLocaleCurrentLocale<Result>(
}

guard let newMethod = class_getClassMethod(
NSLocale.self, #selector(NSLocale._swiftUnittest_currentLocale)) as Optional
NSLocale.self, #selector(NSLocale.__swiftUnittest_currentLocale)) as Optional
else {
preconditionFailure("Could not find +[Locale _swiftUnittest_currentLocale]")
preconditionFailure("Could not find +[Locale __swiftUnittest_currentLocale]")
}

precondition(_temporaryLocaleCurrentLocale == nil,
Expand Down
13 changes: 8 additions & 5 deletions stdlib/public/SDK/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -929,20 +929,20 @@ internal final class _DataStorage {

switch _backing {
case .swift:
return _NSSwiftData(backing: self, range: range)
return __NSSwiftData(backing: self, range: range)
case .immutable(let d):
guard range.lowerBound == 0 && range.upperBound == _length else {
return _NSSwiftData(backing: self, range: range)
return __NSSwiftData(backing: self, range: range)
}
return d
case .mutable(let d):
guard range.lowerBound == 0 && range.upperBound == _length else {
return _NSSwiftData(backing: self, range: range)
return __NSSwiftData(backing: self, range: range)
}
return d
case .customReference(let d):
guard range.lowerBound == 0 && range.upperBound == d.length else {
return _NSSwiftData(backing: self, range: range)
return __NSSwiftData(backing: self, range: range)
}
return d
case .customMutableReference(let d):
Expand All @@ -966,7 +966,10 @@ internal final class _DataStorage {
}
}

internal class _NSSwiftData : NSData {
// NOTE: older runtimes called this _NSSwiftData. The two must
// coexist, so it was renamed. The old name must not be used in the new
// runtime.
internal class __NSSwiftData : NSData {
var _backing: _DataStorage!
var _range: Range<Data.Index>!

Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/SDK/Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ extension NSDictionary : Sequence {
// Bridging subscript.
@objc
public subscript(key: Any) -> Any? {
@objc(_swift_objectForKeyedSubscript:)
@objc(__swift_objectForKeyedSubscript:)
get {
// Deliberately avoid the subscript operator in case the dictionary
// contains non-copyable keys. This is rare since NSMutableDictionary
Expand All @@ -193,11 +193,11 @@ extension NSDictionary : Sequence {
extension NSMutableDictionary {
// Bridging subscript.
@objc override public subscript(key: Any) -> Any? {
@objc(_swift_objectForKeyedSubscript:)
@objc(__swift_objectForKeyedSubscript:)
get {
return self.object(forKey: key)
}
@objc(_swift_setObject:forKeyedSubscript:)
@objc(__swift_setObject:forKeyedSubscript:)
set {
// FIXME: Unfortunate that the `NSCopying` check has to be done at
// runtime.
Expand All @@ -218,7 +218,7 @@ extension NSDictionary {
/// - Returns: An initialized dictionary--which might be different
/// than the original receiver--containing the keys and values
/// found in `otherDictionary`.
@objc(_swiftInitWithDictionary_NSDictionary:)
@objc(__swiftInitWithDictionary_NSDictionary:)
public convenience init(dictionary otherDictionary: __shared NSDictionary) {
// FIXME(performance)(compiler limitation): we actually want to do just
// `self = otherDictionary.copy()`, but Swift does not have factory
Expand Down
70 changes: 38 additions & 32 deletions stdlib/public/SDK/Foundation/NSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,49 @@ public protocol NSKeyValueObservingCustomization : NSObjectProtocol {

fileprivate extension NSObject {

@objc class func _old_unswizzled_automaticallyNotifiesObservers(forKey key: String?) -> Bool {
@objc class func __old_unswizzled_automaticallyNotifiesObservers(forKey key: String?) -> Bool {
fatalError("Should never be reached")
}

@objc class func _old_unswizzled_keyPathsForValuesAffectingValue(forKey key: String?) -> Set<String> {
@objc class func __old_unswizzled_keyPathsForValuesAffectingValue(forKey key: String?) -> Set<String> {
fatalError("Should never be reached")
}

}

@objc private class _KVOKeyPathBridgeMachinery : NSObject {
// NOTE: older overlays called this _KVOKeyPathBridgeMachinery. The two
// must coexist, so it was renamed. The old name must not be used in the
// new runtime.
@objc private class __KVOKeyPathBridgeMachinery : NSObject {
@nonobjc static var keyPathTable: [String : AnyKeyPath] = {
/*
Move all our methods into place. We want the following:
_KVOKeyPathBridgeMachinery's automaticallyNotifiesObserversForKey:, and keyPathsForValuesAffectingValueForKey: methods replaces NSObject's versions of them
NSObject's automaticallyNotifiesObserversForKey:, and keyPathsForValuesAffectingValueForKey: methods replace NSObject's _old_unswizzled_* methods
NSObject's _old_unswizzled_* methods replace _KVOKeyPathBridgeMachinery's methods, and are never invoked
__KVOKeyPathBridgeMachinery's automaticallyNotifiesObserversForKey:, and keyPathsForValuesAffectingValueForKey: methods replaces NSObject's versions of them
NSObject's automaticallyNotifiesObserversForKey:, and keyPathsForValuesAffectingValueForKey: methods replace NSObject's __old_unswizzled_* methods
NSObject's _old_unswizzled_* methods replace __KVOKeyPathBridgeMachinery's methods, and are never invoked
*/
let rootClass: AnyClass = NSObject.self
let bridgeClass: AnyClass = _KVOKeyPathBridgeMachinery.self
let bridgeClass: AnyClass = __KVOKeyPathBridgeMachinery.self

let dependentSel = #selector(NSObject.keyPathsForValuesAffectingValue(forKey:))
let rootDependentImpl = class_getClassMethod(rootClass, dependentSel)!
let bridgeDependentImpl = class_getClassMethod(bridgeClass, dependentSel)!
method_exchangeImplementations(rootDependentImpl, bridgeDependentImpl) // NSObject <-> Us

let originalDependentImpl = class_getClassMethod(bridgeClass, dependentSel)! //we swizzled it onto this class, so this is actually NSObject's old implementation
let originalDependentSel = #selector(NSObject._old_unswizzled_keyPathsForValuesAffectingValue(forKey:))
let originalDependentSel = #selector(NSObject.__old_unswizzled_keyPathsForValuesAffectingValue(forKey:))
let dummyDependentImpl = class_getClassMethod(rootClass, originalDependentSel)!
method_exchangeImplementations(originalDependentImpl, dummyDependentImpl) // NSObject's original version <-> NSObject's _old_unswizzled_ version
method_exchangeImplementations(originalDependentImpl, dummyDependentImpl) // NSObject's original version <-> NSObject's __old_unswizzled_ version

let autoSel = #selector(NSObject.automaticallyNotifiesObservers(forKey:))
let rootAutoImpl = class_getClassMethod(rootClass, autoSel)!
let bridgeAutoImpl = class_getClassMethod(bridgeClass, autoSel)!
method_exchangeImplementations(rootAutoImpl, bridgeAutoImpl) // NSObject <-> Us

let originalAutoImpl = class_getClassMethod(bridgeClass, autoSel)! //we swizzled it onto this class, so this is actually NSObject's old implementation
let originalAutoSel = #selector(NSObject._old_unswizzled_automaticallyNotifiesObservers(forKey:))
let originalAutoSel = #selector(NSObject.__old_unswizzled_automaticallyNotifiesObservers(forKey:))
let dummyAutoImpl = class_getClassMethod(rootClass, originalAutoSel)!
method_exchangeImplementations(originalAutoImpl, dummyAutoImpl) // NSObject's original version <-> NSObject's _old_unswizzled_ version
method_exchangeImplementations(originalAutoImpl, dummyAutoImpl) // NSObject's original version <-> NSObject's __old_unswizzled_ version

return [:]
}()
Expand All @@ -83,62 +86,65 @@ fileprivate extension NSObject {

@nonobjc fileprivate static func _bridgeKeyPath(_ keyPath: __owned AnyKeyPath) -> String {
guard let keyPathString = keyPath._kvcKeyPathString else { fatalError("Could not extract a String from KeyPath \(keyPath)") }
_KVOKeyPathBridgeMachinery.keyPathTableLock.lock()
defer { _KVOKeyPathBridgeMachinery.keyPathTableLock.unlock() }
_KVOKeyPathBridgeMachinery.keyPathTable[keyPathString] = keyPath
__KVOKeyPathBridgeMachinery.keyPathTableLock.lock()
defer { __KVOKeyPathBridgeMachinery.keyPathTableLock.unlock() }
__KVOKeyPathBridgeMachinery.keyPathTable[keyPathString] = keyPath
return keyPathString
}

@nonobjc fileprivate static func _bridgeKeyPath(_ keyPath:String?) -> AnyKeyPath? {
guard let keyPath = keyPath else { return nil }
_KVOKeyPathBridgeMachinery.keyPathTableLock.lock()
defer { _KVOKeyPathBridgeMachinery.keyPathTableLock.unlock() }
let path = _KVOKeyPathBridgeMachinery.keyPathTable[keyPath]
__KVOKeyPathBridgeMachinery.keyPathTableLock.lock()
defer { __KVOKeyPathBridgeMachinery.keyPathTableLock.unlock() }
let path = __KVOKeyPathBridgeMachinery.keyPathTable[keyPath]
return path
}

@objc override class func automaticallyNotifiesObservers(forKey key: String) -> Bool {
//This is swizzled so that it's -[NSObject automaticallyNotifiesObserversForKey:]
if let customizingSelf = self as? NSKeyValueObservingCustomization.Type, let path = _KVOKeyPathBridgeMachinery._bridgeKeyPath(key) {
if let customizingSelf = self as? NSKeyValueObservingCustomization.Type, let path = __KVOKeyPathBridgeMachinery._bridgeKeyPath(key) {
return customizingSelf.automaticallyNotifiesObservers(for: path)
} else {
return self._old_unswizzled_automaticallyNotifiesObservers(forKey: key) //swizzled to be NSObject's original implementation
return self.__old_unswizzled_automaticallyNotifiesObservers(forKey: key) //swizzled to be NSObject's original implementation
}
}

@objc override class func keyPathsForValuesAffectingValue(forKey key: String?) -> Set<String> {
//This is swizzled so that it's -[NSObject keyPathsForValuesAffectingValueForKey:]
if let customizingSelf = self as? NSKeyValueObservingCustomization.Type, let path = _KVOKeyPathBridgeMachinery._bridgeKeyPath(key!) {
if let customizingSelf = self as? NSKeyValueObservingCustomization.Type, let path = __KVOKeyPathBridgeMachinery._bridgeKeyPath(key!) {
let resultSet = customizingSelf.keyPathsAffectingValue(for: path)
return Set(resultSet.lazy.map {
guard let str = $0._kvcKeyPathString else { fatalError("Could not extract a String from KeyPath \($0)") }
return str
})
} else {
return self._old_unswizzled_keyPathsForValuesAffectingValue(forKey: key) //swizzled to be NSObject's original implementation
return self.__old_unswizzled_keyPathsForValuesAffectingValue(forKey: key) //swizzled to be NSObject's original implementation
}
}
}

func _bridgeKeyPathToString(_ keyPath:AnyKeyPath) -> String {
return _KVOKeyPathBridgeMachinery._bridgeKeyPath(keyPath)
return __KVOKeyPathBridgeMachinery._bridgeKeyPath(keyPath)
}

func _bridgeStringToKeyPath(_ keyPath:String) -> AnyKeyPath? {
return _KVOKeyPathBridgeMachinery._bridgeKeyPath(keyPath)
return __KVOKeyPathBridgeMachinery._bridgeKeyPath(keyPath)
}

public class NSKeyValueObservation : NSObject {
// NOTE: older overlays called this NSKeyValueObservation. The two must
// coexist, so it was renamed. The old name must not be used in the new
// runtime.
public class _NSKeyValueObservation : NSObject {

@nonobjc weak var object : NSObject?
@nonobjc let callback : (NSObject, NSKeyValueObservedChange<Any>) -> Void
@nonobjc let path : String

//workaround for <rdar://problem/31640524> Erroneous (?) error when using bridging in the Foundation overlay
@nonobjc static var swizzler : NSKeyValueObservation? = {
let bridgeClass: AnyClass = NSKeyValueObservation.self
@nonobjc static var swizzler : _NSKeyValueObservation? = {
let bridgeClass: AnyClass = _NSKeyValueObservation.self
let observeSel = #selector(NSObject.observeValue(forKeyPath:of:change:context:))
let swapSel = #selector(NSKeyValueObservation._swizzle_me_observeValue(forKeyPath:of:change:context:))
let swapSel = #selector(_NSKeyValueObservation._swizzle_me_observeValue(forKeyPath:of:change:context:))
let rootObserveImpl = class_getInstanceMethod(bridgeClass, observeSel)
let swapObserveImpl = class_getInstanceMethod(bridgeClass, swapSel)
method_exchangeImplementations(rootObserveImpl, swapObserveImpl)
Expand All @@ -147,7 +153,7 @@ public class NSKeyValueObservation : NSObject {

fileprivate init(object: NSObject, keyPath: AnyKeyPath, callback: @escaping (NSObject, NSKeyValueObservedChange<Any>) -> Void) {
path = _bridgeKeyPathToString(keyPath)
let _ = NSKeyValueObservation.swizzler
let _ = _NSKeyValueObservation.swizzler
self.object = object
self.callback = callback
}
Expand All @@ -156,7 +162,7 @@ public class NSKeyValueObservation : NSObject {
object?.addObserver(self, forKeyPath: path, options: options, context: nil)
}

///invalidate() will be called automatically when an NSKeyValueObservation is deinited
///invalidate() will be called automatically when an _NSKeyValueObservation is deinited
@objc public func invalidate() {
object?.removeObserver(self, forKeyPath: path, context: nil)
object = nil
Expand All @@ -181,13 +187,13 @@ public class NSKeyValueObservation : NSObject {

extension _KeyValueCodingAndObserving {

///when the returned NSKeyValueObservation is deinited or invalidated, it will stop observing
///when the returned _NSKeyValueObservation is deinited or invalidated, it will stop observing
public func observe<Value>(
_ keyPath: KeyPath<Self, Value>,
options: NSKeyValueObservingOptions = [],
changeHandler: @escaping (Self, NSKeyValueObservedChange<Value>) -> Void)
-> NSKeyValueObservation {
let result = NSKeyValueObservation(object: self as! NSObject, keyPath: keyPath) { (obj, change) in
-> _NSKeyValueObservation {
let result = _NSKeyValueObservation(object: self as! NSObject, keyPath: keyPath) { (obj, change) in
let notification = NSKeyValueObservedChange(kind: change.kind,
newValue: change.newValue as? Value,
oldValue: change.oldValue as? Value,
Expand Down
6 changes: 5 additions & 1 deletion stdlib/public/SDK/SpriteKit/SpriteKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public typealias SKColor = UIColor
// this class only exists to allow AnyObject lookup of _copyImageData
// since that method only exists in a private header in SpriteKit, the lookup
// mechanism by default fails to accept it as a valid AnyObject call
@objc class _SpriteKitMethodProvider : NSObject {
//
// NOTE: older runtimes called this _SpriteKitMethodProvider. The two must
// coexist, so it was renamed. The old name must not be used in the new
// runtime.
@objc class __SpriteKitMethodProvider : NSObject {
override init() { preconditionFailure("don't touch me") }
@objc func _copyImageData() -> NSData! { return nil }
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ extension Array {
_ source: AnyObject
) -> Array? {
// If source is deferred, we indirect to get its native storage
let maybeNative = (source as? _SwiftDeferredNSArray)?._nativeStorage ?? source
let maybeNative = (source as? __SwiftDeferredNSArray)?._nativeStorage ?? source

return (maybeNative as? _ContiguousArrayStorage<Element>).map {
Array(_ContiguousArrayBuffer($0))
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/ArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import SwiftShims

@usableFromInline
internal typealias _ArrayBridgeStorage
= _BridgeStorage<_ContiguousArrayStorageBase, _NSArrayCore>
= _BridgeStorage<__ContiguousArrayStorageBase, _NSArrayCore>

@usableFromInline
@_fixed_layout
Expand Down
14 changes: 7 additions & 7 deletions stdlib/public/core/BridgeObjectiveC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ protocol _NSSwiftValue: class {
}

@usableFromInline
internal class _SwiftValue {
internal class __SwiftValue {
@usableFromInline
let value: Any

Expand All @@ -606,7 +606,7 @@ internal class _SwiftValue {
}

@usableFromInline
static let null = _SwiftValue(Optional<Any>.none as Any)
static let null = __SwiftValue(Optional<Any>.none as Any)
}

// Internal stdlib SPI
Expand All @@ -623,7 +623,7 @@ public func swift_unboxFromSwiftValueWithType<T>(
}
}

if let box = source as? _SwiftValue {
if let box = source as? __SwiftValue {
if let value = box.value as? T {
result.initialize(to: value)
return true
Expand All @@ -644,7 +644,7 @@ public func _swiftValueConformsTo<T>(_ type: T.Type) -> Bool {
if let foundationType = _foundationSwiftValueType {
return foundationType is T.Type
} else {
return _SwiftValue.self is T.Type
return __SwiftValue.self is T.Type
}
}

Expand All @@ -665,14 +665,14 @@ extension Optional: _Unwrappable {
}
}

private let _foundationSwiftValueType = _typeByName("Foundation._SwiftValue") as? _NSSwiftValue.Type
private let _foundationSwiftValueType = _typeByName("Foundation.__SwiftValue") as? _NSSwiftValue.Type

@usableFromInline
internal var _nullPlaceholder: AnyObject {
if let foundationType = _foundationSwiftValueType {
return foundationType.null
} else {
return _SwiftValue.null
return __SwiftValue.null
}
}

Expand All @@ -681,7 +681,7 @@ func _makeSwiftValue(_ value: Any) -> AnyObject {
if let foundationType = _foundationSwiftValueType {
return foundationType.init(value)
} else {
return _SwiftValue(value)
return __SwiftValue(value)
}
}

Expand Down
Loading