1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
//===--- 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 <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#include <objc/NSObject.h>
#include <objc/runtime.h>
#include <objc/objc.h>
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/Metadata.h"
#include "swift/Runtime/ObjCBridge.h"
#include "llvm/ADT/DenseMap.h"
#include <stdio.h>
#include <stdlib.h>
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
- (id)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:
|