4343#define SWIFT_NAME (NAME )
4444#endif
4545
46+ #if __has_attribute(availability)
47+ #define SWIFT_UNAVAILABLE (msg ) \
48+ __attribute__ ((availability(swift, unavailable, message=msg)))
49+ #else
50+ #define SWIFT_UNAVAILABLE (msg )
51+ #endif
52+
4653#ifdef PURE_BRIDGING_MODE
4754// In PURE_BRIDGING_MODE, briding functions are not inlined
4855#define BRIDGED_INLINE
@@ -55,6 +62,43 @@ SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
5562typedef intptr_t SwiftInt;
5663typedef uintptr_t SwiftUInt;
5764
65+ // Define a bridging wrapper that wraps an underlying C++ pointer type. When
66+ // importing into Swift, we expose an initializer and accessor that works with
67+ // `void *`, which is imported as UnsafeMutableRawPointer. Note we can't rely on
68+ // Swift importing the underlying C++ pointer as an OpaquePointer since that is
69+ // liable to change with PURE_BRIDGING_MODE, since that changes what we include,
70+ // and Swift could import the underlying pointee type instead. We need to be
71+ // careful that the interface we expose remains consistent regardless of
72+ // PURE_BRIDGING_MODE.
73+ #define BRIDGING_WRAPPER_IMPL (Node, Name, Nullability ) \
74+ class Bridged ##Name { \
75+ swift::Node * Nullability Ptr; \
76+ \
77+ public: \
78+ SWIFT_UNAVAILABLE (" Use init(raw:) instead" ) \
79+ Bridged##Name(swift::Node * Nullability ptr) : Ptr(ptr) {} \
80+ \
81+ SWIFT_UNAVAILABLE (" Use '.raw' instead" ) \
82+ swift::Node * Nullability get () const { return Ptr; } \
83+ }; \
84+ \
85+ SWIFT_NAME (" getter:Bridged" #Name " .raw(self:)" ) \
86+ inline void * Nullability Bridged##Name##_getRaw(Bridged##Name bridged) { \
87+ return bridged.get (); \
88+ } \
89+ \
90+ SWIFT_NAME (" Bridged" #Name " .init(raw:)" ) \
91+ inline Bridged##Name Bridged##Name##_fromRaw(void * Nullability ptr) { \
92+ return static_cast <swift::Node *>(ptr); \
93+ }
94+
95+ // Bridging wrapper macros for convenience.
96+ #define BRIDGING_WRAPPER_NONNULL (Name ) \
97+ BRIDGING_WRAPPER_IMPL (Name, Name, _Nonnull)
98+
99+ #define BRIDGING_WRAPPER_NULLABLE (Name ) \
100+ BRIDGING_WRAPPER_IMPL (Name, Nullable##Name, _Nullable)
101+
58102struct BridgedOStream {
59103 void * _Nonnull streamAddr;
60104};
0 commit comments