Skip to content

Commit 9216672

Browse files
committed
Improve some bridging interfaces
Migrate a bunch of structs to classes, and uppercase field names.
1 parent e2b8d98 commit 9216672

File tree

7 files changed

+179
-63
lines changed

7 files changed

+179
-63
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 67 additions & 12 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.
@@ -185,8 +230,18 @@ enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : size_t {
185230
BridgedNote,
186231
};
187232

188-
struct BridgedDiagnostic {
189-
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; }
190245
};
191246

192247
// FIXME: Can we bridge InFlightDiagnostic?

include/swift/AST/ASTBridgingImpl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
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
//===----------------------------------------------------------------------===//

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: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,62 @@ BRIDGED_INLINE bool BridgedSourceLoc_isValid(BridgedSourceLoc loc);
281281
// MARK: SourceRange
282282
//===----------------------------------------------------------------------===//
283283

284-
struct BridgedSourceRange {
285-
BridgedSourceLoc startLoc;
286-
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
287304
};
288305

289-
struct BridgedCharSourceRange {
290-
void *_Nonnull start;
291-
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
292326
};
293327

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+
294340
//===----------------------------------------------------------------------===//
295341
// MARK: Plugins
296342
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)