//===--- SwiftNativeNSXXXBase.mm.gyb - Cocoa classes with fast refcounts --===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// // // Classes derived from ObjC bases but that use native swift reference // counting, layout, and allocation. // // These classes declare a native Swift object header and override the // NSObject methods that do reference counting to use it accordingly. // We can only do this trick with objc classes that are known not to // use the storage where Swift places its native object header. This // takes care of how the classes are handled from Objective-C code. // _NSSwiftArrayBase, _NSSwiftDictionaryBase, _NSSwiftSetBase // _NSSwiftSetBase, _NSSwiftStringBase // // To trick Swift into using its fast refcounting and allocation // directly (rather than going through objc_msgSend to arrive at the // implementations defined here), we define subclasses on the Swift // side but we establish the inheritance relationship with // the special @_swift_native_objc_runtime_base attribute rather // than directly subclassing the classes defined here. // //===----------------------------------------------------------------------===// #include "swift/Runtime/Config.h" #if SWIFT_OBJC_INTEROP #import #import #include #include #include #include "swift/Runtime/HeapObject.h" #include "swift/Runtime/Metadata.h" #include "swift/Runtime/ObjCBridge.h" #include "llvm/ADT/DenseMap.h" #include #include using namespace swift; // NOTE: older runtimes called these _SwiftNativeNSXXXBase. The two must // coexist, so these were renamed. The old names must not be used in the new // runtime. % for Class in ('Array', 'MutableArray', 'Dictionary', 'Set', 'String', 'Enumerator'): SWIFT_RUNTIME_STDLIB_API @interface __SwiftNativeNS${Class}Base : NSObject { @private SWIFT_HEAPOBJECT_NON_OBJC_MEMBERS; } @end @implementation __SwiftNativeNS${Class}Base - (instancetype)initWithCoder: (NSCoder *)coder { return [super init]; } + (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key { return NO; } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wobjc-missing-super-calls" STANDARD_OBJC_METHOD_IMPLS_FOR_SWIFT_OBJECTS #pragma clang diagnostic pop @end % end SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_SPI bool swift_stdlib_NSObject_isEqual(id lhs, id rhs) { return (lhs == rhs) || [lhs isEqual:rhs]; } SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_SPI bool swift_stdlib_connectNSBaseClasses() { % for Class in ('Array', 'MutableArray', 'Dictionary', 'Set', 'String', 'Enumerator'): Class NS${Class}Super = objc_lookUpClass("NS${Class}"); if (!NS${Class}Super) return false; Class NS${Class}OurClass = objc_lookUpClass("__SwiftNativeNS${Class}Base"); if (!NS${Class}OurClass) return false; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" class_setSuperclass(NS${Class}OurClass, NS${Class}Super); #pragma clang diagnostic pop % end return true; } @interface __SwiftNativeNSArrayBase (Compatibility) + (id) new; @end @implementation __SwiftNativeNSArrayBase (Compatibility) + (id) new { // Some apps accidentally do [[[aSwiftArray class] new] mutableCopy] return [objc_lookUpClass("NSArray") new]; } @end #endif // ${'Local Variables'}: // eval: (read-only-mode 1) // End: