Skip to content

Commit e19954f

Browse files
authored
Merge pull request #69534 from hamishknight/bridgnt
2 parents 6589897 + a3ca2d3 commit e19954f

File tree

19 files changed

+826
-766
lines changed

19 files changed

+826
-766
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,73 @@
2626
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2727

2828
namespace swift {
29-
class DiagnosticArgument;
30-
class DiagnosticEngine;
29+
class ASTContext;
30+
class DiagnosticArgument;
31+
class DiagnosticEngine;
3132
}
3233

3334
//===----------------------------------------------------------------------===//
3435
// MARK: Identifier
3536
//===----------------------------------------------------------------------===//
3637

37-
struct BridgedIdentifier {
38-
const void *_Nullable raw;
38+
class BridgedIdentifier {
39+
public:
40+
SWIFT_UNAVAILABLE("Use '.raw' instead")
41+
const void *_Nullable Raw;
42+
43+
BridgedIdentifier() : Raw(nullptr) {}
44+
45+
SWIFT_NAME("init(raw:)")
46+
BridgedIdentifier(const void *_Nullable raw) : Raw(raw) {}
47+
48+
#ifdef USED_IN_CPP_SOURCE
49+
BridgedIdentifier(swift::Identifier ident)
50+
: Raw(ident.getAsOpaquePointer()) {}
51+
52+
swift::Identifier unbridged() const {
53+
return swift::Identifier::getFromOpaquePointer(Raw);
54+
}
55+
#endif
3956
};
4057

58+
SWIFT_NAME("getter:BridgedIdentifier.raw(self:)")
59+
inline const void *_Nullable BridgedIdentifier_raw(BridgedIdentifier ident) {
60+
return ident.Raw;
61+
}
62+
4163
struct BridgedIdentifierAndSourceLoc {
42-
BridgedIdentifier name;
43-
BridgedSourceLoc nameLoc;
64+
SWIFT_NAME("name")
65+
BridgedIdentifier Name;
66+
67+
SWIFT_NAME("nameLoc")
68+
BridgedSourceLoc NameLoc;
4469
};
4570

4671
//===----------------------------------------------------------------------===//
4772
// MARK: ASTContext
4873
//===----------------------------------------------------------------------===//
4974

50-
struct BridgedASTContext {
51-
void *_Nonnull raw;
75+
class BridgedASTContext {
76+
swift::ASTContext * _Nonnull Ctx;
77+
78+
public:
79+
#ifdef USED_IN_CPP_SOURCE
80+
SWIFT_UNAVAILABLE("Use init(raw:) instead")
81+
BridgedASTContext(swift::ASTContext &ctx) : Ctx(&ctx) {}
82+
83+
SWIFT_UNAVAILABLE("Use '.raw' instead")
84+
swift::ASTContext &unbridged() const { return *Ctx; }
85+
#endif
5286
};
5387

88+
SWIFT_NAME("getter:BridgedASTContext.raw(self:)")
89+
BRIDGED_INLINE
90+
void * _Nonnull BridgedASTContext_raw(BridgedASTContext bridged);
91+
92+
SWIFT_NAME("BridgedASTContext.init(raw:)")
93+
BRIDGED_INLINE
94+
BridgedASTContext BridgedASTContext_fromRaw(void * _Nonnull ptr);
95+
5496
SWIFT_NAME("BridgedASTContext.getIdentifier(self:_:)")
5597
BridgedIdentifier BridgedASTContext_getIdentifier(BridgedASTContext cContext,
5698
BridgedStringRef cStr);
@@ -70,8 +112,11 @@ enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : size_t {
70112
};
71113

72114
struct BridgedASTNode {
73-
void *_Nonnull ptr;
74-
ASTNodeKind kind;
115+
SWIFT_NAME("raw")
116+
void *_Nonnull Raw;
117+
118+
SWIFT_NAME("kind")
119+
ASTNodeKind Kind;
75120
};
76121

77122
// Forward declare the underlying AST node type for each wrapper.
@@ -81,11 +126,12 @@ namespace swift {
81126
} // end namespace swift
82127

83128
// Define the bridging wrappers for each AST node.
84-
#define AST_BRIDGING_WRAPPER(Name) BRIDGING_WRAPPER_NONNULL(Name)
129+
#define AST_BRIDGING_WRAPPER(Name) BRIDGING_WRAPPER_NONNULL(swift::Name, Name)
85130
#include "swift/AST/ASTBridgingWrappers.def"
86131

87132
// For nullable nodes, also define a nullable variant.
88-
#define AST_BRIDGING_WRAPPER_NULLABLE(Name) BRIDGING_WRAPPER_NULLABLE(Name)
133+
#define AST_BRIDGING_WRAPPER_NULLABLE(Name) \
134+
BRIDGING_WRAPPER_NULLABLE(swift::Name, Name)
89135
#define AST_BRIDGING_WRAPPER_NONNULL(Name)
90136
#include "swift/AST/ASTBridgingWrappers.def"
91137

@@ -150,7 +196,7 @@ class BridgedDiagnosticArgument {
150196
BridgedDiagnosticArgument(const swift::DiagnosticArgument &arg) {
151197
*reinterpret_cast<swift::DiagnosticArgument *>(&storage) = arg;
152198
}
153-
const swift::DiagnosticArgument &get() const {
199+
const swift::DiagnosticArgument &unbridged() const {
154200
return *reinterpret_cast<const swift::DiagnosticArgument *>(&storage);
155201
}
156202
#endif
@@ -167,7 +213,7 @@ class BridgedDiagnosticFixIt {
167213
BridgedDiagnosticFixIt(const swift::DiagnosticInfo::FixIt &fixit){
168214
*reinterpret_cast<swift::DiagnosticInfo::FixIt *>(&storage) = fixit;
169215
}
170-
const swift::DiagnosticInfo::FixIt &get() const {
216+
const swift::DiagnosticInfo::FixIt &unbridged() const {
171217
return *reinterpret_cast<const swift::DiagnosticInfo::FixIt *>(&storage);
172218
}
173219
#endif
@@ -184,8 +230,18 @@ enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : size_t {
184230
BridgedNote,
185231
};
186232

187-
struct BridgedDiagnostic {
188-
void *_Nonnull raw;
233+
class BridgedDiagnostic {
234+
public:
235+
struct Impl;
236+
237+
SWIFT_UNAVAILABLE("Unavailable in Swift")
238+
Impl *_Nonnull Raw;
239+
240+
SWIFT_UNAVAILABLE("Unavailable in Swift")
241+
BridgedDiagnostic(Impl *_Nonnull raw) : Raw(raw) {}
242+
243+
SWIFT_UNAVAILABLE("Unavailable in Swift")
244+
Impl *_Nonnull unbridged() const { return Raw; }
189245
};
190246

191247
// FIXME: Can we bridge InFlightDiagnostic?

include/swift/AST/ASTBridgingImpl.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,36 @@
1717

1818
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
1919

20+
//===----------------------------------------------------------------------===//
21+
// MARK: BridgedASTContext
22+
//===----------------------------------------------------------------------===//
23+
24+
void * _Nonnull BridgedASTContext_raw(BridgedASTContext bridged) {
25+
return &bridged.unbridged();
26+
}
27+
28+
BridgedASTContext BridgedASTContext_fromRaw(void * _Nonnull ptr) {
29+
return *static_cast<swift::ASTContext *>(ptr);
30+
}
31+
2032
//===----------------------------------------------------------------------===//
2133
// MARK: BridgedNominalTypeDecl
2234
//===----------------------------------------------------------------------===//
2335

2436
BridgedStringRef BridgedNominalTypeDecl_getName(BridgedNominalTypeDecl decl) {
25-
return decl.get()->getName().str();
37+
return decl.unbridged()->getName().str();
2638
}
2739

2840
bool BridgedNominalTypeDecl_isGlobalActor(BridgedNominalTypeDecl decl) {
29-
return decl.get()->isGlobalActor();
41+
return decl.unbridged()->isGlobalActor();
3042
}
3143

3244
//===----------------------------------------------------------------------===//
3345
// MARK: BridgedVarDecl
3446
//===----------------------------------------------------------------------===//
3547

3648
BridgedStringRef BridgedVarDecl_getUserFacingName(BridgedVarDecl decl) {
37-
return decl.get()->getBaseName().userFacingName();
49+
return decl.unbridged()->getBaseName().userFacingName();
3850
}
3951

4052
SWIFT_END_NULLABILITY_ANNOTATIONS

include/swift/AST/Identifier.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class Identifier {
9393
}
9494

9595
bool empty() const { return Pointer == nullptr; }
96+
bool nonempty() const { return !empty(); }
9697

9798
LLVM_ATTRIBUTE_USED bool is(StringRef string) const {
9899
return str().equals(string);
@@ -180,7 +181,7 @@ class Identifier {
180181
return static_cast<const void *>(Pointer);
181182
}
182183

183-
static Identifier getFromOpaquePointer(void *P) {
184+
static Identifier getFromOpaquePointer(const void *P) {
184185
return Identifier((const char*)P);
185186
}
186187

include/swift/Basic/BasicBridging.h

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
#define BRIDGED_INLINE inline
5858
#endif
5959

60+
namespace llvm {
61+
class raw_ostream;
62+
} // end namespace llvm
63+
6064
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
6165

6266
typedef intptr_t SwiftInt;
@@ -72,32 +76,32 @@ typedef uintptr_t SwiftUInt;
7276
// PURE_BRIDGING_MODE.
7377
#define BRIDGING_WRAPPER_IMPL(Node, Name, Nullability) \
7478
class Bridged##Name { \
75-
swift::Node * Nullability Ptr; \
79+
Node * Nullability Ptr; \
7680
\
7781
public: \
7882
SWIFT_UNAVAILABLE("Use init(raw:) instead") \
79-
Bridged##Name(swift::Node * Nullability ptr) : Ptr(ptr) {} \
83+
Bridged##Name(Node * Nullability ptr) : Ptr(ptr) {} \
8084
\
8185
SWIFT_UNAVAILABLE("Use '.raw' instead") \
82-
swift::Node * Nullability get() const { return Ptr; } \
86+
Node * Nullability unbridged() const { return Ptr; } \
8387
}; \
8488
\
8589
SWIFT_NAME("getter:Bridged" #Name ".raw(self:)") \
8690
inline void * Nullability Bridged##Name##_getRaw(Bridged##Name bridged) { \
87-
return bridged.get(); \
91+
return bridged.unbridged(); \
8892
} \
8993
\
9094
SWIFT_NAME("Bridged" #Name ".init(raw:)") \
9195
inline Bridged##Name Bridged##Name##_fromRaw(void * Nullability ptr) { \
92-
return static_cast<swift::Node *>(ptr); \
96+
return static_cast<Node *>(ptr); \
9397
}
9498

9599
// Bridging wrapper macros for convenience.
96-
#define BRIDGING_WRAPPER_NONNULL(Name) \
97-
BRIDGING_WRAPPER_IMPL(Name, Name, _Nonnull)
100+
#define BRIDGING_WRAPPER_NONNULL(Node, Name) \
101+
BRIDGING_WRAPPER_IMPL(Node, Name, _Nonnull)
98102

99-
#define BRIDGING_WRAPPER_NULLABLE(Name) \
100-
BRIDGING_WRAPPER_IMPL(Name, Nullable##Name, _Nullable)
103+
#define BRIDGING_WRAPPER_NULLABLE(Node, Name) \
104+
BRIDGING_WRAPPER_IMPL(Node, Nullable##Name, _Nullable)
101105

102106
//===----------------------------------------------------------------------===//
103107
// MARK: ArrayRef
@@ -123,7 +127,7 @@ class BridgedArrayRef {
123127
: Data(arr.data()), Length(arr.size()) {}
124128

125129
template <typename T>
126-
llvm::ArrayRef<T> get() const {
130+
llvm::ArrayRef<T> unbridged() const {
127131
return {static_cast<const T *>(Data), Length};
128132
}
129133
#endif
@@ -179,9 +183,7 @@ enum ENUM_EXTENSIBILITY_ATTR(open) BridgedFeature {
179183
// MARK: OStream
180184
//===----------------------------------------------------------------------===//
181185

182-
struct BridgedOStream {
183-
void * _Nonnull streamAddr;
184-
};
186+
BRIDGING_WRAPPER_NONNULL(llvm::raw_ostream, OStream)
185187

186188
//===----------------------------------------------------------------------===//
187189
// MARK: StringRef
@@ -196,7 +198,7 @@ class BridgedStringRef {
196198
BridgedStringRef(llvm::StringRef sref)
197199
: Data(sref.data()), Length(sref.size()) {}
198200

199-
llvm::StringRef get() const { return llvm::StringRef(Data, Length); }
201+
llvm::StringRef unbridged() const { return llvm::StringRef(Data, Length); }
200202
#endif
201203

202204
BridgedStringRef() : Data(nullptr), Length(0) {}
@@ -226,7 +228,7 @@ class BridgedOwnedString {
226228
#ifdef USED_IN_CPP_SOURCE
227229
BridgedOwnedString(const std::string &stringToCopy);
228230

229-
llvm::StringRef getRef() const { return llvm::StringRef(Data, Length); }
231+
llvm::StringRef unbridgedRef() const { return llvm::StringRef(Data, Length); }
230232
#endif
231233

232234
void destroy() const;
@@ -258,7 +260,7 @@ class BridgedSourceLoc {
258260
#ifdef USED_IN_CPP_SOURCE
259261
BridgedSourceLoc(swift::SourceLoc loc) : Raw(loc.getOpaquePointerValue()) {}
260262

261-
swift::SourceLoc get() const {
263+
swift::SourceLoc unbridged() const {
262264
return swift::SourceLoc(
263265
llvm::SMLoc::getFromPointer(static_cast<const char *>(Raw)));
264266
}
@@ -279,16 +281,62 @@ BRIDGED_INLINE bool BridgedSourceLoc_isValid(BridgedSourceLoc loc);
279281
// MARK: SourceRange
280282
//===----------------------------------------------------------------------===//
281283

282-
struct BridgedSourceRange {
283-
BridgedSourceLoc startLoc;
284-
BridgedSourceLoc endLoc;
284+
class BridgedSourceRange {
285+
public:
286+
SWIFT_NAME("start")
287+
BridgedSourceLoc Start;
288+
289+
SWIFT_NAME("end")
290+
BridgedSourceLoc End;
291+
292+
SWIFT_NAME("init(start:end:)")
293+
BridgedSourceRange(BridgedSourceLoc start, BridgedSourceLoc end)
294+
: Start(start), End(end) {}
295+
296+
#ifdef USED_IN_CPP_SOURCE
297+
BridgedSourceRange(swift::SourceRange range)
298+
: Start(range.Start), End(range.End) {}
299+
300+
swift::SourceRange unbridged() const {
301+
return swift::SourceRange(Start.unbridged(), End.unbridged());
302+
}
303+
#endif
285304
};
286305

287-
struct BridgedCharSourceRange {
288-
void *_Nonnull start;
289-
size_t byteLength;
306+
class BridgedCharSourceRange {
307+
public:
308+
SWIFT_UNAVAILABLE("Use '.start' instead")
309+
BridgedSourceLoc Start;
310+
311+
SWIFT_UNAVAILABLE("Use '.byteLength' instead")
312+
unsigned ByteLength;
313+
314+
SWIFT_NAME("init(start:byteLength:)")
315+
BridgedCharSourceRange(BridgedSourceLoc start, unsigned byteLength)
316+
: Start(start), ByteLength(byteLength) {}
317+
318+
#ifdef USED_IN_CPP_SOURCE
319+
BridgedCharSourceRange(swift::CharSourceRange range)
320+
: Start(range.getStart()), ByteLength(range.getByteLength()) {}
321+
322+
swift::CharSourceRange unbridged() const {
323+
return swift::CharSourceRange(Start.unbridged(), ByteLength);
324+
}
325+
#endif
290326
};
291327

328+
SWIFT_NAME("getter:BridgedCharSourceRange.start(self:)")
329+
inline BridgedSourceLoc
330+
BridgedCharSourceRange_start(BridgedCharSourceRange range) {
331+
return range.Start;
332+
}
333+
334+
SWIFT_NAME("getter:BridgedCharSourceRange.byteLength(self:)")
335+
inline SwiftInt
336+
BridgedCharSourceRange_byteLength(BridgedCharSourceRange range) {
337+
return static_cast<SwiftInt>(range.ByteLength);
338+
}
339+
292340
//===----------------------------------------------------------------------===//
293341
// MARK: Plugins
294342
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)