-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy path_SwiftStdlibCxxOverlay.h
430 lines (340 loc) · 11.5 KB
/
_SwiftStdlibCxxOverlay.h
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//===--- _SwiftStlibCxxOverlay.h - Additions for Stdlib ---------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 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
//
//===----------------------------------------------------------------------===//
#ifndef __cplusplus
#error "no C++"
#endif
#ifdef SWIFT_CXX_INTEROP_OPTIONAL_MIXIN
/// True when the Optional has a value.
SWIFT_INLINE_THUNK operator bool() const noexcept { return *this != none; }
/// Returns the value stored in the Optional.
///
/// The returned value is copied using the appropriate Swift / C++ copy
/// semantics.
SWIFT_INLINE_THUNK T_0_0 get() const
noexcept(noexcept(getUnsafelyUnwrapped())) {
// FIXME: Fail with source location.
return getUnsafelyUnwrapped();
}
#undef SWIFT_CXX_INTEROP_OPTIONAL_MIXIN
#elif defined(SWIFT_CXX_INTEROP_STRING_MIXIN)
#ifndef SWIFT_CXX_INTEROP_HIDE_STL_OVERLAY
/// Constructs a Swift string from a C string.
SWIFT_INLINE_THUNK String(const char *cString) noexcept {
if (!cString) {
auto res = _impl::$sS2SycfC();
memcpy(_getOpaquePointer(), &res, sizeof(res));
return;
}
auto res = _impl::$sSS7cStringSSSPys4Int8VG_tcfC(cString);
memcpy(_getOpaquePointer(), &res, sizeof(res));
}
/// Constructs a Swift string from a C++ string.
SWIFT_INLINE_THUNK String(const std::string &str) noexcept {
auto res = _impl::$sSS7cStringSSSPys4Int8VG_tcfC(str.c_str());
memcpy(_getOpaquePointer(), &res, sizeof(res));
}
/// Casts the Swift String value to a C++ std::string.
SWIFT_INLINE_THUNK operator std::string() const;
#endif // SWIFT_CXX_INTEROP_HIDE_STL_OVERLAY
#undef SWIFT_CXX_INTEROP_STRING_MIXIN
#else
// out-of-class overlay for Swift standard library.
static_assert(sizeof(_impl::_impl_String) >= 0,
"included outside of stdlib bindings");
#ifndef SWIFT_CXX_INTEROP_HIDE_STL_OVERLAY
SWIFT_INLINE_THUNK String::operator std::string() const {
auto u = getUtf8();
std::string result;
result.reserve(u.getCount() + 1);
using IndexType = decltype(u.getStartIndex());
for (auto s = u.getStartIndex().getEncodedOffset(),
e = u.getEndIndex().getEncodedOffset();
s != e; s = u.indexOffsetBy(IndexType::init(s), 1).getEncodedOffset()) {
result.push_back(u[IndexType::init(s)]);
}
return result;
}
#endif // SWIFT_CXX_INTEROP_HIDE_STL_OVERLAY
namespace cxxOverlay {
class IterationEndSentinel;
/// Abstract Swift collection iterator.
template <class Collection, class T> class CollectionIterator {
public:
using Index =
decltype(reinterpret_cast<Collection *>(0x123)->getStartIndex());
SWIFT_INLINE_THUNK CollectionIterator(const Collection &c) noexcept(
noexcept(c.getStartIndex()) &&noexcept(c.getEndIndex()))
: collection(c) {
index = collection.getStartIndex();
endIndex = collection.getEndIndex();
// FIXME: Begin read access.
}
SWIFT_INLINE_THUNK ~CollectionIterator() noexcept {
// FIXME: End read access.
}
SWIFT_INLINE_THUNK T operator*() const noexcept {
return collection[index];
}
SWIFT_INLINE_THUNK void operator++() noexcept {
++index;
// FIXME: assert(index <= endIndex); // No need to go past the end.
}
SWIFT_INLINE_THUNK bool
operator!=(const IterationEndSentinel &) const noexcept {
return index != endIndex;
}
private:
Index index, endIndex;
const Collection &collection;
};
class IterationEndSentinel {};
template <class T> using ArrayIterator = CollectionIterator<Array<T>, T>;
} // namespace cxxOverlay
// FIXME: This should apply to more than the Array type.
template <class T>
SWIFT_INLINE_THUNK cxxOverlay::ArrayIterator<T> begin(const Array<T> &array
[[clang::lifetimebound]]) {
return cxxOverlay::ArrayIterator<T>(array);
}
template <class T>
SWIFT_INLINE_THUNK cxxOverlay::IterationEndSentinel end(const Array<T> &) {
return {};
}
#ifdef SWIFT_CXX_INTEROP_EXPERIMENTAL_SWIFT_ERROR
extern "C" void *_Nonnull swift_errorRetain(void *_Nonnull swiftError) noexcept;
extern "C" void swift_errorRelease(void *_Nonnull swiftError) noexcept;
extern "C" int $ss5ErrorMp; // external global %swift.protocol, align 4
extern "C" const void *_Nullable swift_getTypeByMangledNameInContext(
const char *_Nullable typeNameStart, size_t typeNameLength,
const void *_Nullable context,
const void *_Nullable const *_Nullable genericArgs) SWIFT_CALL;
extern "C" bool swift_dynamicCast(void *_Nullable dest, void *_Nullable src,
const void *_Nullable srcType,
const void *_Nullable targetType,
uint32_t flags);
struct SymbolicP {
alignas(2) uint8_t _1;
uint32_t _2;
uint8_t _3[2];
uint8_t _4;
} __attribute__((packed));
SWIFT_INLINE_THUNK const void *_Nullable getErrorMetadata() {
static SymbolicP errorSymbol;
static int *_Nonnull got_ss5ErrorMp = &$ss5ErrorMp;
errorSymbol._1 = 2;
errorSymbol._2 =
static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&got_ss5ErrorMp) -
reinterpret_cast<uintptr_t>(&errorSymbol._2));
errorSymbol._3[0] = '_';
errorSymbol._3[1] = 'p';
errorSymbol._4 = 0;
static_assert(sizeof(errorSymbol) == 8, "");
auto charErrorSymbol = reinterpret_cast<const char *>(&errorSymbol);
const void *ptr2 = swift_getTypeByMangledNameInContext(
charErrorSymbol, sizeof(errorSymbol) - 1, nullptr, nullptr);
return ptr2;
}
#ifndef SWIFT_CXX_INTEROP_HIDE_SWIFT_ERROR
class Error {
public:
SWIFT_INLINE_THUNK Error() {}
SWIFT_INLINE_THUNK Error(void *_Nonnull swiftError) {
opaqueValue = swiftError;
}
SWIFT_INLINE_THUNK ~Error() {
if (opaqueValue)
swift_errorRelease(opaqueValue);
}
SWIFT_INLINE_THUNK void *_Nonnull getPointerToOpaquePointer() {
return opaqueValue;
}
SWIFT_INLINE_THUNK Error(Error &&other) : opaqueValue(other.opaqueValue) {
other.opaqueValue = nullptr;
}
SWIFT_INLINE_THUNK Error(const Error &other) {
if (other.opaqueValue)
swift_errorRetain(other.opaqueValue);
opaqueValue = other.opaqueValue;
}
template <class T> SWIFT_INLINE_THUNK swift::Optional<T> as() {
alignas(alignof(T)) char buffer[sizeof(T)];
const void *em = getErrorMetadata();
void *ep = getPointerToOpaquePointer();
auto metadata = swift::TypeMetadataTrait<T>::getTypeMetadata();
// Dynamic cast will release the error, so we need to retain it.
swift_errorRetain(ep);
bool dynamicCast =
swift_dynamicCast(buffer, &ep, em, metadata,
/*take on success destroy on failure*/ 6);
if (dynamicCast) {
auto result = swift::_impl::implClassFor<T>::type::returnNewValue(
[&](char *dest) {
swift::_impl::implClassFor<T>::type::initializeWithTake(dest,
buffer);
});
return swift::Optional<T>::init(result);
}
return swift::Optional<T>::none();
}
private:
void *_Nonnull opaqueValue = nullptr;
};
namespace _impl {
constexpr inline std::size_t max(std::size_t a, std::size_t b) {
return a > b ? a : b;
}
} // namespace _impl
/// The Expected class has either an error or an value.
template<class T>
class Expected {
public:
/// Default
constexpr Expected() noexcept {
new (&buffer) Error();
has_val = false;
}
constexpr Expected(const swift::Error &error_val) noexcept {
new (&buffer) Error(error_val);
has_val = false;
}
constexpr Expected(const T &val) noexcept {
new (&buffer) T(val);
has_val = true;
}
/// Copy
constexpr Expected(Expected const& other) noexcept {
if (other.has_value())
new (&buffer) T(other.value());
else
new (&buffer) Error(other.error());
has_val = other.has_value();
}
/// Move
// FIXME: Implement move semantics when move Swift values is possible
constexpr Expected(Expected&&) noexcept { abort(); }
~Expected() noexcept {
if (has_value())
reinterpret_cast<const T *>(buffer)->~T();
else
reinterpret_cast<swift::Error *>(buffer)->~Error();
}
/// assignment
constexpr auto operator=(Expected&& other) noexcept = delete;
constexpr auto operator=(Expected&) noexcept = delete;
/// For accessing T's members
constexpr T const *_Nonnull operator->() const noexcept {
if (!has_value())
abort();
return reinterpret_cast<const T *>(buffer);
}
constexpr T *_Nonnull operator->() noexcept {
if (!has_value())
abort();
return reinterpret_cast<T *>(buffer);
}
/// Getting reference to T
constexpr T const &operator*() const & noexcept {
if (!has_value())
abort();
return reinterpret_cast<const T &>(buffer);
}
constexpr T &operator*() & noexcept {
if (!has_value())
abort();
return reinterpret_cast<T &>(buffer);
}
constexpr explicit operator bool() const noexcept { return has_value(); }
// Get value, if not exists abort
constexpr T const& value() const& {
if (!has_value())
abort();
return *reinterpret_cast<const T *>(buffer);
}
constexpr T& value() & {
if (!has_value())
abort();
return *reinterpret_cast<T *>(buffer);
}
// Get error
constexpr swift::Error const &error() const & {
if (has_value())
abort();
return reinterpret_cast<const swift::Error &>(buffer);
}
constexpr swift::Error &error() & {
if (has_value())
abort();
return reinterpret_cast<swift::Error &>(buffer);
}
constexpr bool has_value() const noexcept { return has_val; }
private:
alignas(_impl::max(alignof(T), alignof(swift::Error))) char buffer[_impl::max(
sizeof(T), sizeof(swift::Error))];
bool has_val;
};
template<>
class Expected<void> {
public:
/// Default
Expected() noexcept {
new (&buffer) Error();
has_val = false;
}
Expected(const swift::Error &error_val) noexcept {
new (&buffer) Error(error_val);
has_val = false;
}
/// Copy
Expected(Expected const& other) noexcept {
if (other.has_value())
abort();
else
new (&buffer) Error(other.error());
has_val = other.has_value();
}
/// Move
// FIXME: Implement move semantics when move swift values is possible
[[noreturn]] Expected(Expected&&) noexcept { abort(); }
~Expected() noexcept { reinterpret_cast<swift::Error *>(buffer)->~Error(); }
/// assignment
constexpr auto operator=(Expected&& other) noexcept = delete;
constexpr auto operator=(Expected&) noexcept = delete;
constexpr explicit operator bool() const noexcept { return has_value(); }
// Get error
constexpr swift::Error const &error() const & {
if (has_value())
abort();
return reinterpret_cast<const swift::Error &>(buffer);
}
constexpr swift::Error &error() & {
if (has_value())
abort();
return reinterpret_cast<swift::Error &>(buffer);
}
constexpr bool has_value() const noexcept { return has_val; }
private:
alignas(alignof(swift::Error)) char buffer[sizeof(swift::Error)];
bool has_val;
};
#ifdef __cpp_exceptions
template<class T>
using ThrowingResult = T;
#define SWIFT_RETURN_THUNK(T, v) v
#define SWIFT_NORETURN_EXCEPT_ERRORS SWIFT_NORETURN
#else
template <class T> using ThrowingResult = swift::Expected<T>;
#define SWIFT_RETURN_THUNK(T, v) swift::Expected<T>(v)
#define SWIFT_NORETURN_EXCEPT_ERRORS
#endif
#endif // SWIFT_CXX_INTEROP_HIDE_SWIFT_ERROR
#endif // SWIFT_CXX_INTEROP_EXPERIMENTAL_SWIFT_ERROR
#endif