|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "swift/Basic/Lazy.h" |
| 14 | +#include "swift/Basic/LLVM.h" |
| 15 | +#include "swift/Runtime/Metadata.h" |
| 16 | +#include "swift/Runtime/Mutex.h" |
| 17 | +#include "swift/Runtime/ObjCBridge.h" |
| 18 | +#include <vector> |
| 19 | +#import <Foundation/Foundation.h> |
| 20 | +#import <CoreFoundation/CoreFoundation.h> |
| 21 | + |
| 22 | +using namespace swift; |
| 23 | + |
| 24 | +/// Class of sentinel objects used to represent the `nil` value of nested |
| 25 | +/// optionals. |
| 26 | +@interface _SwiftNull : NSObject { |
| 27 | +@public |
| 28 | + unsigned depth; |
| 29 | +} |
| 30 | +@end |
| 31 | + |
| 32 | +@implementation _SwiftNull : NSObject |
| 33 | + |
| 34 | +- (NSString*)description { |
| 35 | + return [NSString stringWithFormat:@"<%@ %p depth = %u>", [self class], |
| 36 | + self, |
| 37 | + self->depth]; |
| 38 | +} |
| 39 | + |
| 40 | +@end |
| 41 | + |
| 42 | +namespace { |
| 43 | + |
| 44 | +struct SwiftNullSentinelCache { |
| 45 | + std::vector<id> Cache; |
| 46 | + StaticReadWriteLock Lock; |
| 47 | +}; |
| 48 | + |
| 49 | +static Lazy<SwiftNullSentinelCache> Sentinels; |
| 50 | + |
| 51 | +static id getSentinelForDepth(unsigned depth) { |
| 52 | + // For unnested optionals, use NSNull. |
| 53 | + if (depth == 1) |
| 54 | + return (id)kCFNull; |
| 55 | + // Otherwise, make up our own sentinel. |
| 56 | + // See if we created one for this depth. |
| 57 | + auto &theSentinels = Sentinels.get(); |
| 58 | + unsigned depthIndex = depth - 2; |
| 59 | + { |
| 60 | + StaticScopedReadLock lock(theSentinels.Lock); |
| 61 | + const auto &cache = theSentinels.Cache; |
| 62 | + if (depthIndex < cache.size()) { |
| 63 | + id cached = cache[depthIndex]; |
| 64 | + if (cached) |
| 65 | + return cached; |
| 66 | + } |
| 67 | + } |
| 68 | + // Make one if we need to. |
| 69 | + { |
| 70 | + StaticScopedWriteLock lock(theSentinels.Lock); |
| 71 | + if (depthIndex >= theSentinels.Cache.size()) |
| 72 | + theSentinels.Cache.resize(depthIndex + 1); |
| 73 | + auto &cached = theSentinels.Cache[depthIndex]; |
| 74 | + // Make sure another writer didn't sneak in. |
| 75 | + if (!cached) { |
| 76 | + auto sentinel = [[_SwiftNull alloc] init]; |
| 77 | + sentinel->depth = depth; |
| 78 | + cached = sentinel; |
| 79 | + } |
| 80 | + return cached; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +} |
| 85 | + |
| 86 | +/// Return the sentinel object to use to represent `nil` for a given Optional |
| 87 | +/// type. |
| 88 | +SWIFT_RUNTIME_STDLIB_INTERFACE SWIFT_CC(swift) |
| 89 | +extern "C" |
| 90 | +id _swift_Foundation_getOptionalNilSentinelObject(const Metadata *Wrapped) { |
| 91 | + // Figure out the depth of optionality we're working with. |
| 92 | + unsigned depth = 1; |
| 93 | + while (Wrapped->getKind() == MetadataKind::Optional) { |
| 94 | + ++depth; |
| 95 | + Wrapped = cast<EnumMetadata>(Wrapped)->getGenericArgs()[0]; |
| 96 | + } |
| 97 | + |
| 98 | + return objc_retain(getSentinelForDepth(depth)); |
| 99 | +} |
0 commit comments