|
| 1 | +//===--- Error.h - Swift Runtime ABI for error values -----------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Swift runtime support for working with error values. |
| 14 | +// |
| 15 | +// The ABI here is quite different in ObjC and non-ObjC modes. |
| 16 | +// In ObjC mode, SwiftError is closely related to the NSError class: |
| 17 | +// native errors are boxed as a subclass of NSError, but non-native |
| 18 | +// errors may simply be NSError objects directly from Objective-C. |
| 19 | +// In non-ObjC mode, SwiftError boxes are always native. |
| 20 | +// |
| 21 | +//===----------------------------------------------------------------------===// |
| 22 | + |
| 23 | +#ifndef SWIFT_RUNTIME_ERROR_H |
| 24 | +#define SWIFT_RUNTIME_ERROR_H |
| 25 | + |
| 26 | +#include "swift/Runtime/HeapObject.h" |
| 27 | +#include "swift/Runtime/Metadata.h" |
| 28 | + |
| 29 | +namespace swift { |
| 30 | + |
| 31 | +struct SwiftError; |
| 32 | + |
| 33 | +/// Allocate a catchable error object. |
| 34 | +/// |
| 35 | +/// If value is nonnull, it should point to a value of \c type, which will be |
| 36 | +/// copied (or taken if \c isTake is true) into the newly-allocated error box. |
| 37 | +/// If value is null, the box's contents will be left uninitialized, and |
| 38 | +/// \c isTake should be false. |
| 39 | +SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API |
| 40 | +BoxPair swift_allocError(const Metadata *type, |
| 41 | + const WitnessTable *errorConformance, |
| 42 | + OpaqueValue *value, bool isTake); |
| 43 | + |
| 44 | +/// Deallocate an error object whose contained object has already been |
| 45 | +/// destroyed. |
| 46 | +SWIFT_RUNTIME_STDLIB_API |
| 47 | +void swift_deallocError(SwiftError *error, const Metadata *type); |
| 48 | + |
| 49 | +struct ErrorValueResult { |
| 50 | + const OpaqueValue *value; |
| 51 | + const Metadata *type; |
| 52 | + const WitnessTable *errorConformance; |
| 53 | +}; |
| 54 | + |
| 55 | +/// Extract a pointer to the value, the type metadata, and the Error |
| 56 | +/// protocol witness from an error object. |
| 57 | +/// |
| 58 | +/// The "scratch" pointer should point to an uninitialized word-sized |
| 59 | +/// temporary buffer. The implementation may write a reference to itself to |
| 60 | +/// that buffer if the error object is a toll-free-bridged NSError instead of |
| 61 | +/// a native Swift error, in which case the object itself is the "boxed" value. |
| 62 | +SWIFT_RUNTIME_STDLIB_API |
| 63 | +void swift_getErrorValue(const SwiftError *errorObject, |
| 64 | + void **scratch, |
| 65 | + ErrorValueResult *out); |
| 66 | + |
| 67 | +/// Called when throwing an error. Serves as a breakpoint hook |
| 68 | +/// for debuggers. |
| 69 | +SWIFT_CC(swift) |
| 70 | +SWIFT_RUNTIME_STDLIB_API void |
| 71 | +swift_willThrow(SWIFT_CONTEXT void *unused, |
| 72 | + SWIFT_ERROR_RESULT SwiftError **object); |
| 73 | + |
| 74 | +/// Called when an error is thrown out of the top level of a script. |
| 75 | +SWIFT_CC(swift) |
| 76 | +SWIFT_RUNTIME_STDLIB_API SWIFT_NORETURN void |
| 77 | +swift_errorInMain(SwiftError *object); |
| 78 | + |
| 79 | +/// Called when the try! operator fails. |
| 80 | +SWIFT_CC(swift) |
| 81 | +SWIFT_RUNTIME_STDLIB_API SWIFT_NORETURN void |
| 82 | +swift_unexpectedError(SwiftError *object, OpaqueValue *filenameStart, |
| 83 | + long filenameLength, bool isAscii, unsigned long line); |
| 84 | + |
| 85 | +/// Retain an error box. |
| 86 | +SWIFT_RUNTIME_STDLIB_API |
| 87 | +SwiftError *swift_errorRetain(SwiftError *object); |
| 88 | + |
| 89 | +/// Release an error box. |
| 90 | +SWIFT_RUNTIME_STDLIB_API |
| 91 | +void swift_errorRelease(SwiftError *object); |
| 92 | + |
| 93 | +} // end namespace swift |
| 94 | + |
| 95 | +#endif |
0 commit comments