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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
//===--- ErrorObject.h - Cocoa-interoperable recoverable error object -----===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This implements the object representation of the standard Error
// protocol type, which represents recoverable errors in the language. This
// implementation is designed to interoperate efficiently with Cocoa libraries
// by:
// - allowing for NSError and CFError objects to "toll-free bridge" to
// Error existentials, which allows for cheap Cocoa to Swift interop
// - allowing a native Swift error to lazily "become" an NSError when
// passed into Cocoa, allowing for cheap Swift to Cocoa interop
//
//===----------------------------------------------------------------------===//
#ifndef __SWIFT_RUNTIME_ERROROBJECT_H__
#define __SWIFT_RUNTIME_ERROROBJECT_H__
#include "swift/Runtime/Error.h"
#include "swift/Runtime/Metadata.h"
#include "SwiftHashableSupport.h"
#include <atomic>
#if SWIFT_OBJC_INTEROP
# include <CoreFoundation/CoreFoundation.h>
# include <objc/objc.h>
#endif
namespace swift {
#if SWIFT_OBJC_INTEROP
// Copied from CoreFoundation/CFRuntime.h.
struct CFRuntimeBase {
void *opaque1;
void *opaque2;
};
/// When ObjC interop is enabled, SwiftError uses an NSError-layout-compatible
/// header.
struct SwiftErrorHeader {
// CFError has a CF refcounting header. NSError reserves a word after the
// 'isa' in order to be layout-compatible.
CFRuntimeBase base;
// The NSError part of the object is lazily initialized, so we need atomic
// semantics.
std::atomic<CFIndex> code;
std::atomic<CFStringRef> domain;
std::atomic<CFDictionaryRef> userInfo;
};
#else
/// When ObjC interop is disabled, SwiftError uses a normal Swift heap object
/// header.
using SwiftErrorHeader = HeapObject;
#endif
/// The layout of the Swift Error box.
struct SwiftError : SwiftErrorHeader {
// By inheriting OpaqueNSError, the SwiftError structure reserves enough
// space within itself to lazily emplace an NSError instance, and gets
// Core Foundation's refcounting scheme.
/// The type of Swift error value contained in the box.
/// This member is only available for native Swift errors.
const Metadata *type;
/// The witness table for `Error` conformance.
/// This member is only available for native Swift errors.
const WitnessTable *errorConformance;
#if SWIFT_OBJC_INTEROP
/// The base type that introduces the `Hashable` conformance.
/// This member is only available for native Swift errors.
/// This member is lazily-initialized.
/// Instead of using it directly, call `getHashableBaseType()`.
mutable std::atomic<const Metadata *> hashableBaseType;
/// The witness table for `Hashable` conformance.
/// This member is only available for native Swift errors.
/// This member is lazily-initialized.
/// Instead of using it directly, call `getHashableConformance()`.
mutable std::atomic<const hashable_support::HashableWitnessTable *> hashableConformance;
#endif
/// Get a pointer to the value contained inside the indirectly-referenced
/// box reference.
static const OpaqueValue *getIndirectValue(const SwiftError * const *ptr) {
// If the box is a bridged NSError, then the box's address is itself the
// value.
if ((*ptr)->isPureNSError())
return reinterpret_cast<const OpaqueValue *>(ptr);
return (*ptr)->getValue();
}
static OpaqueValue *getIndirectValue(SwiftError * const *ptr) {
return const_cast<OpaqueValue *>(getIndirectValue(
const_cast<const SwiftError * const *>(ptr)));
}
/// Get a pointer to the value, which is tail-allocated after
/// the fixed header.
const OpaqueValue *getValue() const {
// If the box is a bridged NSError, then the box's address is itself the
// value. We can't provide an address for that; getIndirectValue must be
// used if we haven't established this as an NSError yet..
assert(!isPureNSError());
auto baseAddr = reinterpret_cast<uintptr_t>(this + 1);
// Round up to the value's alignment.
unsigned alignMask = type->getValueWitnesses()->getAlignmentMask();
baseAddr = (baseAddr + alignMask) & ~(uintptr_t)alignMask;
return reinterpret_cast<const OpaqueValue *>(baseAddr);
}
OpaqueValue *getValue() {
return const_cast<OpaqueValue*>(
const_cast<const SwiftError *>(this)->getValue());
}
#if SWIFT_OBJC_INTEROP
// True if the object is really an NSError or CFError instance.
// The type and errorConformance fields don't exist in an NSError.
bool isPureNSError() const;
#else
bool isPureNSError() const { return false; }
#endif
#if SWIFT_OBJC_INTEROP
/// Get the type of the contained value.
const Metadata *getType() const;
/// Get the Error protocol witness table for the contained type.
const WitnessTable *getErrorConformance() const;
#else
/// Get the type of the contained value.
const Metadata *getType() const { return type; }
/// Get the Error protocol witness table for the contained type.
const WitnessTable *getErrorConformance() const { return errorConformance; }
#endif
#if SWIFT_OBJC_INTEROP
/// Get the base type that conforms to `Hashable`.
/// Returns NULL if the type does not conform.
const Metadata *getHashableBaseType() const;
/// Get the `Hashable` protocol witness table for the contained type.
/// Returns NULL if the type does not conform.
const hashable_support::HashableWitnessTable *getHashableConformance() const;
#endif
// Don't copy or move, please.
SwiftError(const SwiftError &) = delete;
SwiftError(SwiftError &&) = delete;
SwiftError &operator=(const SwiftError &) = delete;
SwiftError &operator=(SwiftError &&) = delete;
};
#if SWIFT_OBJC_INTEROP
/// Initialize an Error box to make it usable as an NSError instance.
///
/// errorObject is assumed to be passed at +1 and consumed in this function.
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_SPI
id _swift_stdlib_bridgeErrorToNSError(SwiftError *errorObject);
/// Attempt to dynamically cast an NSError object to a Swift ErrorType
/// implementation using the _ObjectiveCBridgeableErrorType protocol or by
/// putting it directly into an Error existential.
bool tryDynamicCastNSErrorObjectToValue(HeapObject *object,
OpaqueValue *dest,
const Metadata *destType,
DynamicCastFlags flags);
/// Attempt to dynamically cast an NSError instance to a Swift ErrorType
/// implementation using the _ObjectiveCBridgeableErrorType protocol or by
/// putting it directly into an Error existential.
///
/// srcType must be some kind of class metadata.
bool tryDynamicCastNSErrorToValue(OpaqueValue *dest,
OpaqueValue *src,
const Metadata *srcType,
const Metadata *destType,
DynamicCastFlags flags);
/// Get the NSError Objective-C class.
Class getNSErrorClass();
/// Get the NSError metadata.
const Metadata *getNSErrorMetadata();
/// Find the witness table for the conformance of the given type to the
/// Error protocol, or return nullptr if it does not conform.
const WitnessTable *findErrorWitness(const Metadata *srcType);
/// Dynamically cast a value whose conformance to the Error protocol is known
/// into an NSError instance.
id dynamicCastValueToNSError(OpaqueValue *src,
const Metadata *srcType,
const WitnessTable *srcErrorWitness,
DynamicCastFlags flags);
#endif
} // namespace swift
#if SWIFT_OBJC_INTEROP
// internal func _getErrorEmbeddedNSErrorIndirect<T : Error>(
// _ x: UnsafePointer<T>) -> AnyObject?
#define getErrorEmbeddedNSErrorIndirect \
MANGLE_SYM(s32_getErrorEmbeddedNSErrorIndirectyyXlSgSPyxGs0B0RzlF)
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
id getErrorEmbeddedNSErrorIndirect(const swift::OpaqueValue *error,
const swift::Metadata *T,
const swift::WitnessTable *Error);
#endif
#endif
|