@@ -131,6 +131,10 @@ typedef uintptr_t SwiftUInt;
131131void assertFail(const char * _Nonnull msg, const char * _Nonnull file,
132132 SwiftUInt line, const char * _Nonnull function);
133133
134+ BRIDGED_INLINE
135+ SWIFT_UNAVAILABLE (" Unavailable in Swift" )
136+ void ASSERT_inBridgingHeader(bool condition);
137+
134138// ===----------------------------------------------------------------------===//
135139// MARK: ArrayRef
136140// ===----------------------------------------------------------------------===//
@@ -261,22 +265,42 @@ class BridgedOwnedString {
261265} SWIFT_SELF_CONTAINED;
262266
263267// ===----------------------------------------------------------------------===//
264- // MARK: BridgedOptionalInt
268+ // MARK: BridgedOptional
265269// ===----------------------------------------------------------------------===//
266270
267- struct BridgedOptionalInt {
268- SwiftInt value;
269- bool hasValue;
271+ // FIXME: We should be able to make this a template once
272+ // https://github.com/swiftlang/swift/issues/82258 is fixed.
273+ #define BRIDGED_OPTIONAL (TYPE, SUFFIX ) \
274+ class SWIFT_CONFORMS_TO_PROTOCOL (Swift.ExpressibleByNilLiteral) \
275+ BridgedOptional##SUFFIX { \
276+ TYPE _value; \
277+ bool _hasValue; \
278+ \
279+ public: \
280+ SWIFT_NAME (" init(nilLiteral:)" ) \
281+ BridgedOptional##SUFFIX (void ) : _hasValue (false ) {} \
282+ BridgedOptional##SUFFIX (TYPE value) : _value (value), _hasValue (true ) {} \
283+ \
284+ SWIFT_COMPUTED_PROPERTY \
285+ TYPE getValue () const { \
286+ ASSERT_inBridgingHeader (_hasValue); \
287+ return _value; \
288+ } \
289+ \
290+ SWIFT_COMPUTED_PROPERTY \
291+ bool getHasValue () const { return _hasValue; } \
292+ };
293+ BRIDGED_OPTIONAL (SwiftInt, Int)
270294
271295#ifdef USED_IN_CPP_SOURCE
272- static BridgedOptionalInt getFromAPInt (llvm::APInt i) {
273- if (i.getSignificantBits () <= std::min (std::numeric_limits<SwiftInt>::digits, 64 )) {
274- return {(SwiftInt)i.getSExtValue (), true };
275- }
276- return {0 , false };
296+ inline BridgedOptionalInt getFromAPInt (llvm::APInt i) {
297+ if (i.getSignificantBits () <=
298+ std::min (std::numeric_limits<SwiftInt>::digits, 64 )) {
299+ return {(SwiftInt)i.getSExtValue ()};
277300 }
301+ return {};
302+ }
278303#endif
279- };
280304
281305// ===----------------------------------------------------------------------===//
282306// MARK: OStream
0 commit comments