Skip to content

Commit f652c8e

Browse files
[Interop][SwiftToCxx] Passing swift::Error declaration from _SwiftCxxInteroperability.h to Swift::Error in _SwiftStdlibCxxOverlay.h
1 parent c6e6f75 commit f652c8e

File tree

3 files changed

+89
-90
lines changed

3 files changed

+89
-90
lines changed

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
12381238
// Create the condition and the statement to throw an exception.
12391239
if (hasThrows) {
12401240
os << " if (opaqueError != nullptr)\n";
1241-
os << " throw (swift::Error(opaqueError));\n";
1241+
os << " throw (Swift::Error(opaqueError));\n";
12421242
}
12431243

12441244
// Return the function result value if it doesn't throw.

lib/PrintAsClang/_SwiftCxxInteroperability.h

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -195,95 +195,6 @@ template <class T> inline void *_Nonnull getOpaquePointer(T &value) {
195195

196196
} // namespace _impl
197197

198-
extern "C" void *_Nonnull swift_errorRetain(void *_Nonnull swiftError) noexcept;
199-
200-
extern "C" void swift_errorRelease(void *_Nonnull swiftError) noexcept;
201-
202-
extern "C" int $ss5ErrorMp; // external global %swift.protocol, align 4
203-
204-
extern "C"
205-
const void * _Nullable
206-
swift_getTypeByMangledNameInContext(
207-
const char *_Nullable typeNameStart,
208-
size_t typeNameLength,
209-
const void *_Nullable context,
210-
const void *_Nullable const *_Nullable genericArgs) SWIFT_CALL;
211-
212-
extern "C" bool swift_dynamicCast(void *_Nullable dest, void *_Nullable src,
213-
const void *_Nullable srcType,
214-
const void * _Nullable targetType,
215-
uint32_t flags);
216-
217-
struct SymbolicP {
218-
alignas(2) uint8_t _1;
219-
uint32_t _2;
220-
uint8_t _3[2];
221-
uint8_t _4;
222-
} __attribute__((packed));
223-
224-
inline const void *_Nullable getErrorMetadata() {
225-
static swift::SymbolicP errorSymbol;
226-
static int *_Nonnull got_ss5ErrorMp = &$ss5ErrorMp;
227-
errorSymbol._1 = 2;
228-
errorSymbol._2 = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&got_ss5ErrorMp) - reinterpret_cast<uintptr_t>(&errorSymbol._2));
229-
errorSymbol._3[0] = '_';
230-
errorSymbol._3[1] = 'p';
231-
errorSymbol._4 = 0;
232-
static_assert(sizeof(errorSymbol) == 8, "");
233-
auto charErrorSymbol = reinterpret_cast<const char *>(&errorSymbol);
234-
235-
const void *ptr2 =
236-
swift::swift_getTypeByMangledNameInContext(charErrorSymbol,
237-
sizeof(errorSymbol) - 1,
238-
nullptr, nullptr);
239-
return ptr2;
240-
}
241-
242-
class Error {
243-
public:
244-
Error() {}
245-
Error(void* _Nonnull swiftError) { opaqueValue = swiftError; }
246-
~Error() {
247-
if (opaqueValue)
248-
swift_errorRelease(opaqueValue);
249-
}
250-
void* _Nonnull getPointerToOpaquePointer() { return opaqueValue; }
251-
Error(Error &&other) : opaqueValue(other.opaqueValue) {
252-
other.opaqueValue = nullptr;
253-
}
254-
Error(const Error &other) {
255-
if (other.opaqueValue)
256-
swift_errorRetain(other.opaqueValue);
257-
opaqueValue = other.opaqueValue;
258-
}
259-
260-
// FIXME: Return a Swift::Optional instead.
261-
template<class T>
262-
T as() {
263-
alignas(alignof(T)) char buffer[sizeof(T)];
264-
const void *em = getErrorMetadata();
265-
void *ep = getPointerToOpaquePointer();
266-
auto metadata = swift::TypeMetadataTrait<T>::getTypeMetadata();
267-
268-
// Dynamic cast will release the error, so we need to retain it.
269-
swift::swift_errorRetain(ep);
270-
bool dynamicCast =
271-
swift::swift_dynamicCast(buffer, &ep, em, metadata,
272-
/*take on success destroy on failure*/ 6);
273-
274-
if (dynamicCast) {
275-
return swift::_impl::implClassFor<T>::type::returnNewValue([&](char *dest) {
276-
swift::_impl::implClassFor<T>::type::initializeWithTake(dest, buffer);
277-
});
278-
}
279-
abort();
280-
// FIXME: return nil.
281-
}
282-
283-
private:
284-
void * _Nonnull opaqueValue = nullptr;
285-
};
286-
287198
#pragma clang diagnostic pop
288199

289200
} // namespace swift

lib/PrintAsClang/_SwiftStdlibCxxOverlay.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,92 @@ SWIFT_INLINE_THUNK cxxOverlay::IterationEndSentinel end(const Array<T> &) {
9191
return {};
9292
}
9393

94+
extern "C" void *_Nonnull swift_errorRetain(void *_Nonnull swiftError) noexcept;
95+
96+
extern "C" void swift_errorRelease(void *_Nonnull swiftError) noexcept;
97+
98+
extern "C" int $ss5ErrorMp; // external global %swift.protocol, align 4
99+
100+
extern "C" const void *_Nullable swift_getTypeByMangledNameInContext(
101+
const char *_Nullable typeNameStart, size_t typeNameLength,
102+
const void *_Nullable context,
103+
const void *_Nullable const *_Nullable genericArgs) SWIFT_CALL;
104+
105+
extern "C" bool swift_dynamicCast(void *_Nullable dest, void *_Nullable src,
106+
const void *_Nullable srcType,
107+
const void *_Nullable targetType,
108+
uint32_t flags);
109+
110+
struct SymbolicP {
111+
alignas(2) uint8_t _1;
112+
uint32_t _2;
113+
uint8_t _3[2];
114+
uint8_t _4;
115+
} __attribute__((packed));
116+
117+
inline const void *_Nullable getErrorMetadata() {
118+
static SymbolicP errorSymbol;
119+
static int *_Nonnull got_ss5ErrorMp = &$ss5ErrorMp;
120+
errorSymbol._1 = 2;
121+
errorSymbol._2 =
122+
static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&got_ss5ErrorMp) -
123+
reinterpret_cast<uintptr_t>(&errorSymbol._2));
124+
errorSymbol._3[0] = '_';
125+
errorSymbol._3[1] = 'p';
126+
errorSymbol._4 = 0;
127+
static_assert(sizeof(errorSymbol) == 8, "");
128+
auto charErrorSymbol = reinterpret_cast<const char *>(&errorSymbol);
129+
130+
const void *ptr2 = swift_getTypeByMangledNameInContext(
131+
charErrorSymbol, sizeof(errorSymbol) - 1, nullptr, nullptr);
132+
return ptr2;
133+
}
134+
135+
class Error {
136+
public:
137+
Error() {}
138+
Error(void *_Nonnull swiftError) { opaqueValue = swiftError; }
139+
~Error() {
140+
if (opaqueValue)
141+
swift_errorRelease(opaqueValue);
142+
}
143+
void *_Nonnull getPointerToOpaquePointer() { return opaqueValue; }
144+
Error(Error &&other) : opaqueValue(other.opaqueValue) {
145+
other.opaqueValue = nullptr;
146+
}
147+
Error(const Error &other) {
148+
if (other.opaqueValue)
149+
swift_errorRetain(other.opaqueValue);
150+
opaqueValue = other.opaqueValue;
151+
}
152+
153+
template <class T>
154+
Swift::Optional<T> as() {
155+
alignas(alignof(T)) char buffer[sizeof(T)];
156+
const void *em = getErrorMetadata();
157+
void *ep = getPointerToOpaquePointer();
158+
auto metadata = swift::TypeMetadataTrait<T>::getTypeMetadata();
159+
160+
// Dynamic cast will release the error, so we need to retain it.
161+
swift_errorRetain(ep);
162+
bool dynamicCast =
163+
swift_dynamicCast(buffer, &ep, em, metadata,
164+
/*take on success destroy on failure*/ 6);
165+
166+
if (dynamicCast) {
167+
auto result = swift::_impl::implClassFor<T>::type::returnNewValue(
168+
[&](char *dest) {
169+
swift::_impl::implClassFor<T>::type::initializeWithTake(dest,
170+
buffer);
171+
});
172+
return Swift::Optional<T>::init(result);
173+
}
174+
175+
return Swift::Optional<T>::none();
176+
}
177+
178+
private:
179+
void *_Nonnull opaqueValue = nullptr;
180+
};
181+
94182
#endif

0 commit comments

Comments
 (0)