-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathStubs.h
More file actions
305 lines (274 loc) · 10.2 KB
/
Stubs.h
File metadata and controls
305 lines (274 loc) · 10.2 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 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 Swift project authors
//
#if !defined(SWT_STUBS_H)
#define SWT_STUBS_H
#include "Defines.h"
#include "Includes.h"
SWT_ASSUME_NONNULL_BEGIN
/// Mark a code path as unreachable.
///
/// This function is necessary because Swift does not have an equivalent of
/// `__builtin_unreachable()`.
__attribute__((always_inline, noreturn))
static inline void swt_unreachable(void) {
__builtin_unreachable();
}
#if !SWT_NO_FILE_IO
/// The C file handle type.
///
/// This typedef is necessary because `FILE *` may be imported into Swift as
/// either `OpaquePointer` or `UnsafeMutablePointer<FILE>` depending on the
/// current platform.
typedef FILE *SWT_FILEHandle;
/// Get the standard output stream.
///
/// This function is provided because directly accessing `stdout` from Swift
/// triggers concurrency warnings on some platforms about accessing shared
/// mutable state.
static SWT_FILEHandle swt_stdout(void) {
return stdout;
}
/// Get the standard error stream.
///
/// This function is provided because directly accessing `stderr` from Swift
/// triggers concurrency warnings on some platforms about accessing shared
/// mutable state.
static SWT_FILEHandle swt_stderr(void) {
return stderr;
}
#endif
/// Get the current C error.
///
/// This function is provided because `errno` is a complex macro on some
/// platforms and cannot be imported directly into Swift.
static int swt_errno(void) {
return errno;
}
#if !SWT_NO_FILE_IO
#if __has_include(<sys/stat.h>) && defined(S_ISFIFO)
/// Check if a given `mode_t` value indicates that a file is a pipe (FIFO.)
///
/// This function is exactly equivalent to the `S_ISFIFO()` macro. It is
/// necessary because the mode flag macros are not imported into Swift
/// consistently across platforms.
static bool swt_S_ISFIFO(mode_t mode) {
return S_ISFIFO(mode);
}
#endif
#endif
#if defined(__APPLE__) && !SWT_NO_MACH_PORTS
/// Get a Mach port representing the current task (process.)
///
/// This function is provided because `mach_task_self()` is a complex macro, but
/// directly accessing `mach_task_self_` from Swift triggers concurrency
/// warnings about accessing shared mutable state.
static mach_port_t swt_mach_task_self(void) {
return mach_task_self();
}
#endif
#if defined(__APPLE__)
/// Define the minimal set of atomic operations supported and used by the
/// testing library for a given C type.
///
/// This macro is provided because Swift cannot directly import the symbols in
/// `<stdatomic.h>` nor clang's/GCC's generic atomic intrinsics. This macro
/// simplifies the definition of atomic operations across the set of types we
/// need them for.
#define SWT_DEFINE_ATOMIC_OPERATIONS(T) \
SWT_SWIFT_NAME(swt_atomicLoad(_:)) \
static inline T SWT_CONCAT(swt_atomicLoad, __COUNTER__)(T *const _Nonnull src) { \
return __atomic_load_n(src, __ATOMIC_SEQ_CST); \
} \
SWT_SWIFT_NAME(swt_atomicStore(_:_:)) \
static inline void SWT_CONCAT(swt_atomicStore, __COUNTER__)(T *_Nonnull dst, T src) { \
__atomic_store_n(dst, src, __ATOMIC_SEQ_CST); \
} \
SWT_SWIFT_NAME(swt_atomicCompareExchange(_:_:_:)) \
static inline bool SWT_CONCAT(swt_atomicCompareExchange, __COUNTER__)(T *_Nonnull dst, T *_Nonnull expected, T desired) { \
return __atomic_compare_exchange_n(dst, expected, desired, /*weak: */false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
}
/// Define the minimal set of atomic operations that we use on various C types.
SWT_DEFINE_ATOMIC_OPERATIONS(bool)
SWT_DEFINE_ATOMIC_OPERATIONS(int)
SWT_DEFINE_ATOMIC_OPERATIONS(intptr_t)
SWT_DEFINE_ATOMIC_OPERATIONS(void const *_Nullable)
#endif
#if defined(_WIN32)
/// Make a Win32 language ID.
///
/// This function is provided because `MAKELANGID()` is a complex macro and
/// cannot be imported directly into Swift.
static LANGID swt_MAKELANGID(int p, int s) {
return MAKELANGID(p, s);
}
/// Get the value of `PROC_THREAD_ATTRIBUTE_HANDLE_LIST`.
///
/// This function is provided because `PROC_THREAD_ATTRIBUTE_HANDLE_LIST` is a
/// complex macro and cannot be imported directly into Swift.
static DWORD_PTR swt_PROC_THREAD_ATTRIBUTE_HANDLE_LIST(void) {
return PROC_THREAD_ATTRIBUTE_HANDLE_LIST;
}
/// Get the first section in an NT image.
///
/// This function is provided because `IMAGE_FIRST_SECTION()` is a complex macro
/// and cannot be imported directly into Swift.
static const IMAGE_SECTION_HEADER *_Null_unspecified swt_IMAGE_FIRST_SECTION(const IMAGE_NT_HEADERS *ntHeader) {
return IMAGE_FIRST_SECTION(ntHeader);
}
#endif
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__)
/// The environment block.
///
/// By POSIX convention, the environment block variable is declared in client
/// code rather than in a header.
SWT_EXTERN char *_Nullable *_Null_unspecified environ;
/// Get the environment block.
///
/// This function is provided because directly accessing `environ` from Swift
/// triggers concurrency warnings about accessing shared mutable state.
static char *_Nullable *_Null_unspecified swt_environ(void) {
return environ;
}
#endif
#if defined(__linux__)
/// Get the `FICLONE` `ioctl()` argument.
///
/// This function is provided because `FICLONE` is a complex macro and cannot be
/// imported directly into Swift.
static unsigned long swt_FICLONE(void) {
return FICLONE;
}
#endif
#if defined(__FreeBSD__)
/// Get the `COPY_FILE_RANGE_CLONE` `copy_file_range()` flag.
///
/// This function is provided because `COPY_FILE_RANGE_CLONE` is not available
/// prior to FreeBSD 15.0. The caller should check `getosreldate()` before using
/// this flag.
static unsigned int swt_COPY_FILE_RANGE_CLONE(void) {
#if defined(COPY_FILE_RANGE_CLONE)
return COPY_FILE_RANGE_CLONE;
#else
// Compiled against an older unistd.h, but presumably running on FreeBSD 15.0
// or newer. SEE: https://github.com/freebsd/freebsd-src/blob/main/sys/sys/unistd.h
return 0x00800000;
#endif
}
#endif
#if !defined(__ANDROID__)
#if __has_include(<signal.h>) && defined(si_pid)
/// Get the value of the `si_pid` field of a `siginfo_t` structure.
///
/// This function is provided because `si_pid` is a complex macro on some
/// platforms and cannot be imported directly into Swift. It is renamed back to
/// `siginfo_t.si_pid` in Swift.
SWT_SWIFT_NAME(getter:siginfo_t.si_pid(self:))
static pid_t swt_siginfo_t_si_pid(const siginfo_t *siginfo) {
return siginfo->si_pid;
}
#endif
#if __has_include(<signal.h>) && defined(si_status)
/// Get the value of the `si_status` field of a `siginfo_t` structure.
///
/// This function is provided because `si_status` is a complex macro on some
/// platforms and cannot be imported directly into Swift. It is renamed back to
/// `siginfo_t.si_status` in Swift.
SWT_SWIFT_NAME(getter:siginfo_t.si_status(self:))
static int swt_siginfo_t_si_status(const siginfo_t *siginfo) {
return siginfo->si_status;
}
#endif
#endif
/// Get the value of `EEXIST`.
///
/// This function is provided because `EEXIST` is a complex macro in wasi-libc
/// and cannot be imported directly into Swift.
static int swt_EEXIST(void) {
return EEXIST;
}
#if defined(F_GETFD)
/// Call `fcntl(F_GETFD)`.
///
/// This function is provided because `fcntl()` is a variadic function and
/// cannot be imported directly into Swift.
static int swt_getfdflags(int fd) {
return fcntl(fd, F_GETFD);
}
#endif
#if defined(F_SETFD)
/// Call `fcntl(F_SETFD)`.
///
/// This function is provided because `fcntl()` is a variadic function and
/// cannot be imported directly into Swift.
static int swt_setfdflags(int fd, int flags) {
return fcntl(fd, F_SETFD, flags);
}
#endif
/// Get the name of the given exit code if one is available.
///
/// - Parameters:
/// - exitCode: An exit code.
///
/// - Returns: The name of `exitCode` if it is a known constant such as
/// `EXIT_FAILURE` or if a name for it is defined in `<sysexits.h>` and that
/// header is present at compile time. If no name is available for `exitCode`,
/// returns `NULL`.
///
/// - Note: The set of exit codes in `<sysexits.h>` is _de facto_ standardized
/// on platforms that include that header.
static const char *_Nullable swt_getExitCodeName(int exitCode) {
#define SWT_EXIT_CODE(NAME) NAME: return #NAME
switch (exitCode) {
case SWT_EXIT_CODE(EXIT_SUCCESS);
case SWT_EXIT_CODE(EXIT_FAILURE);
#if __has_include(<sysexits.h>)
case SWT_EXIT_CODE(EX_USAGE);
case SWT_EXIT_CODE(EX_DATAERR);
case SWT_EXIT_CODE(EX_NOINPUT);
case SWT_EXIT_CODE(EX_NOUSER);
case SWT_EXIT_CODE(EX_NOHOST);
case SWT_EXIT_CODE(EX_UNAVAILABLE);
case SWT_EXIT_CODE(EX_SOFTWARE);
case SWT_EXIT_CODE(EX_OSERR);
case SWT_EXIT_CODE(EX_OSFILE);
case SWT_EXIT_CODE(EX_CANTCREAT);
case SWT_EXIT_CODE(EX_IOERR);
case SWT_EXIT_CODE(EX_TEMPFAIL);
case SWT_EXIT_CODE(EX_PROTOCOL);
case SWT_EXIT_CODE(EX_NOPERM);
case SWT_EXIT_CODE(EX_CONFIG);
#endif
default: return 0;
}
#undef SWT_SYSEXIT_CODE
};
#if !SWT_NO_INTEROP
/// A type describing a fallback event handler that testing API can invoke as an
/// alternate method of reporting test events to the current test runner.
/// Shadows the type with the same name in _TestingInterop.
///
/// - Parameters:
/// - recordJSONSchemaVersionNumber: The JSON schema version used to encode
/// the event record.
/// - recordJSONBaseAddress: A pointer to the first byte of the encoded event.
/// - recordJSONByteCount: The size of the encoded event in bytes.
/// - reserved: Reserved for future use.
typedef void (* SWTFallbackEventHandler)(const char *recordJSONSchemaVersionNumber,
const void *recordJSONBaseAddress,
size_t recordJSONByteCount,
const void *_Nullable reserved);
/// Get the current fallback event handler.
/// Shadows the function with the same name in _TestingInterop.
///
/// - Returns: The currently-set handler function, if any.
SWT_EXTERN SWTFallbackEventHandler _Nullable _swift_testing_getFallbackEventHandler(void);
#endif
SWT_ASSUME_NONNULL_END
#endif