Skip to content

Commit 1c82c71

Browse files
committed
Make an Error.h that declares the public ABI for errors.
NFC except that I added swift_errorRetain and swift_errorRelease functions on non-ObjC targets so that we have consistent functions to call in the runtime. I have not changed everywhere in the runtime to use these, nor have I changed the compiler to call them.
1 parent 8e9823c commit 1c82c71

File tree

3 files changed

+104
-57
lines changed

3 files changed

+104
-57
lines changed

include/swift/Runtime/Error.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

stdlib/public/runtime/ErrorObject.h

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
#ifndef __SWIFT_RUNTIME_ERROROBJECT_H__
2525
#define __SWIFT_RUNTIME_ERROROBJECT_H__
2626

27+
#include "swift/Runtime/Error.h"
2728
#include "swift/Runtime/Metadata.h"
28-
#include "swift/Runtime/HeapObject.h"
2929
#include "SwiftHashableSupport.h"
3030

3131
#include <atomic>
@@ -163,62 +163,6 @@ struct SwiftError : SwiftErrorHeader {
163163
SwiftError &operator=(SwiftError &&) = delete;
164164
};
165165

166-
/// Allocate a catchable error object.
167-
///
168-
/// If value is nonnull, it should point to a value of \c type, which will be
169-
/// copied (or taken if \c isTake is true) into the newly-allocated error box.
170-
/// If value is null, the box's contents will be left uninitialized, and
171-
/// \c isTake should be false.
172-
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
173-
BoxPair swift_allocError(const Metadata *type,
174-
const WitnessTable *errorConformance,
175-
OpaqueValue *value, bool isTake);
176-
177-
/// Deallocate an error object whose contained object has already been
178-
/// destroyed.
179-
SWIFT_RUNTIME_STDLIB_API
180-
void swift_deallocError(SwiftError *error, const Metadata *type);
181-
182-
struct ErrorValueResult {
183-
const OpaqueValue *value;
184-
const Metadata *type;
185-
const WitnessTable *errorConformance;
186-
};
187-
188-
/// Extract a pointer to the value, the type metadata, and the Error
189-
/// protocol witness from an error object.
190-
///
191-
/// The "scratch" pointer should point to an uninitialized word-sized
192-
/// temporary buffer. The implementation may write a reference to itself to
193-
/// that buffer if the error object is a toll-free-bridged NSError instead of
194-
/// a native Swift error, in which case the object itself is the "boxed" value.
195-
SWIFT_RUNTIME_STDLIB_API
196-
void swift_getErrorValue(const SwiftError *errorObject,
197-
void **scratch,
198-
ErrorValueResult *out);
199-
200-
/// Retain and release SwiftError boxes.
201-
SWIFT_RUNTIME_STDLIB_API
202-
SwiftError *swift_errorRetain(SwiftError *object);
203-
SWIFT_RUNTIME_STDLIB_API
204-
void swift_errorRelease(SwiftError *object);
205-
206-
/// Breakpoint hook for debuggers.
207-
SWIFT_CC(swift)
208-
SWIFT_RUNTIME_STDLIB_API void
209-
swift_willThrow(SWIFT_CONTEXT void *unused,
210-
SWIFT_ERROR_RESULT SwiftError **object);
211-
212-
/// Halt in response to an error.
213-
SWIFT_CC(swift)
214-
SWIFT_RUNTIME_STDLIB_API SWIFT_NORETURN void
215-
swift_errorInMain(SwiftError *object);
216-
217-
SWIFT_CC(swift)
218-
SWIFT_RUNTIME_STDLIB_API SWIFT_NORETURN void
219-
swift_unexpectedError(SwiftError *object, OpaqueValue *filenameStart,
220-
long filenameLength, bool isAscii, unsigned long line);
221-
222166
#if SWIFT_OBJC_INTEROP
223167

224168
/// Initialize an Error box to make it usable as an NSError instance.

stdlib/public/runtime/ErrorObjectNative.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,12 @@ swift::swift_getErrorValue(const SwiftError *errorObject,
106106
out->errorConformance = errorObject->errorConformance;
107107
}
108108

109+
SwiftError *swift::swift_errorRetain(SwiftError *error) {
110+
return static_cast<SwiftError*>(swift_retain(error));
111+
}
112+
113+
void swift::swift_errorRelease(SwiftError *error) {
114+
swift_release(error);
115+
}
116+
109117
#endif

0 commit comments

Comments
 (0)