Skip to content

Commit 7de8827

Browse files
committed
BasicBridging: Turn BridgedOptional into a macro
1 parent 722bc0f commit 7de8827

File tree

5 files changed

+60
-15
lines changed

5 files changed

+60
-15
lines changed

include/swift/AST/ASTBridgingImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2023 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -605,7 +605,7 @@ BridgedASTType::MetatypeRepresentation BridgedASTType::getRepresentationOfMetaty
605605
}
606606

607607
BridgedOptionalInt BridgedASTType::getValueOfIntegerType() const {
608-
return BridgedOptionalInt::getFromAPInt(unbridged()->getAs<swift::IntegerType>()->getValue());
608+
return getFromAPInt(unbridged()->getAs<swift::IntegerType>()->getValue());
609609
}
610610

611611
BridgedSubstitutionMap BridgedASTType::getContextSubstitutionMap() const {

include/swift/Basic/BasicBridging.h

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ typedef uintptr_t SwiftUInt;
131131
void 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

include/swift/Basic/BasicBridgingImpl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2023 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -19,6 +19,8 @@
1919

2020
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2121

22+
void ASSERT_inBridgingHeader(bool condition) { ASSERT(condition); }
23+
2224
//===----------------------------------------------------------------------===//
2325
// MARK: BridgedStringRef
2426
//===----------------------------------------------------------------------===//

include/swift/Basic/SwiftBridging.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,29 @@
4848
#define SWIFT_COMPUTED_PROPERTY \
4949
__attribute__((swift_attr("import_computed_property")))
5050

51+
#define _CXX_INTEROP_STRINGIFY(_x) #_x
52+
53+
/// Specifies that a specific C++ `class` or `struct` conforms to a
54+
/// a specific Swift protocol.
55+
///
56+
/// This example shows how to use this macro to conform a class template to a
57+
/// Swift protocol:
58+
/// ```
59+
/// template<class T>
60+
/// class SWIFT_CONFORMS_TO_PROTOCOL(SwiftModule.ProtocolName)
61+
/// CustomClass {};
62+
/// ```
63+
// clang-format off
64+
#define SWIFT_CONFORMS_TO_PROTOCOL(_moduleName_protocolName) \
65+
__attribute__((swift_attr( \
66+
_CXX_INTEROP_STRINGIFY(conforms_to:_moduleName_protocolName))))
67+
// clang-format on
68+
5169
#else // #if __has_attribute(swift_attr)
5270

5371
#define SWIFT_SELF_CONTAINED
5472
#define SWIFT_COMPUTED_PROPERTY
73+
#define SWIFT_CONFORMS_TO_PROTOCOL(_moduleName_protocolName)
5574

5675
#endif // #if __has_attribute(swift_attr)
5776

include/swift/SIL/SILBridgingImpl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -1176,7 +1176,7 @@ BridgedFunction BridgedInstruction::FunctionRefBaseInst_getReferencedFunction()
11761176

11771177
BridgedOptionalInt BridgedInstruction::IntegerLiteralInst_getValue() const {
11781178
llvm::APInt result = getAs<swift::IntegerLiteralInst>()->getValue();
1179-
return BridgedOptionalInt::getFromAPInt(result);
1179+
return getFromAPInt(result);
11801180
}
11811181

11821182
BridgedStringRef BridgedInstruction::StringLiteralInst_getValue() const {

0 commit comments

Comments
 (0)