Skip to content

Commit ca2febe

Browse files
authored
Merge pull request #79169 from rintaro/briding-const-wrapper
[Bridging] Add 'const' variations to BRIDGING_WRAPPER
2 parents 0e1a03a + ebf7a42 commit ca2febe

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,17 @@ namespace swift {
7676

7777
// Define the bridging wrappers for each AST node.
7878
#define AST_BRIDGING_WRAPPER(Name) BRIDGING_WRAPPER_NONNULL(swift::Name, Name)
79+
#define AST_BRIDGING_WRAPPER_CONST(Name) \
80+
BRIDGING_WRAPPER_CONST_NONNULL(swift::Name, Name)
7981
#include "swift/AST/ASTBridgingWrappers.def"
8082

8183
// For nullable nodes, also define a nullable variant.
8284
#define AST_BRIDGING_WRAPPER_NULLABLE(Name) \
8385
BRIDGING_WRAPPER_NULLABLE(swift::Name, Name)
86+
#define AST_BRIDGING_WRAPPER_CONST_NULLABLE(Name) \
87+
BRIDGING_WRAPPER_CONST_NULLABLE(swift::Name, Name)
8488
#define AST_BRIDGING_WRAPPER_NONNULL(Name)
89+
#define AST_BRIDGING_WRAPPER_CONST_NONNULL(Name)
8590
#include "swift/AST/ASTBridgingWrappers.def"
8691

8792
//===----------------------------------------------------------------------===//

include/swift/AST/ASTBridgingWrappers.def

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@
3131
#define AST_BRIDGING_WRAPPER_NONNULL(Name) AST_BRIDGING_WRAPPER(Name)
3232
#endif
3333

34+
/// AST_BRIDGING_WRAPPER_CONST(Name)
35+
/// The default macro to define a const bridging wrapper type.
36+
#ifndef AST_BRIDGING_WRAPPER_CONST
37+
#define AST_BRIDGING_WRAPPER_CONST(Name) AST_BRIDGING_WRAPPER(Name)
38+
#endif
39+
40+
/// AST_BRIDGING_WRAPPER_CONST_NULLABLE(Name)
41+
/// Specifies that both a nullable and non-null variant of the wrapper should
42+
/// be created.
43+
#ifndef AST_BRIDGING_WRAPPER_CONST_NULLABLE
44+
#define AST_BRIDGING_WRAPPER_CONST_NULLABLE(Name) AST_BRIDGING_WRAPPER_CONST(Name)
45+
#endif
46+
47+
/// AST_BRIDGING_WRAPPER_CONST_NONNULL(Name)
48+
/// Specifies that only a non-null variant of the wrapper should be created.
49+
#ifndef AST_BRIDGING_WRAPPER_CONST_NONNULL
50+
#define AST_BRIDGING_WRAPPER_CONST_NONNULL(Name) AST_BRIDGING_WRAPPER_CONST(Name)
51+
#endif
52+
3453
#ifndef DECL
3554
#define DECL(Id, Parent) AST_BRIDGING_WRAPPER_NULLABLE(Id##Decl)
3655
#endif
@@ -91,11 +110,14 @@ AST_BRIDGING_WRAPPER_NULLABLE(AvailabilitySpec)
91110
AST_BRIDGING_WRAPPER_NULLABLE(PlatformVersionConstraintAvailabilitySpec)
92111
AST_BRIDGING_WRAPPER_NULLABLE(PlatformAgnosticVersionConstraintAvailabilitySpec)
93112
AST_BRIDGING_WRAPPER_NULLABLE(OtherPlatformAvailabilitySpec)
94-
AST_BRIDGING_WRAPPER_NONNULL(AvailabilityMacroMap)
113+
AST_BRIDGING_WRAPPER_CONST_NONNULL(AvailabilityMacroMap)
95114

96115
// Non-AST types to generate wrappers for.
97116
AST_BRIDGING_WRAPPER_NULLABLE(DiagnosticEngine)
98117

99118
#undef AST_BRIDGING_WRAPPER_NONNULL
100119
#undef AST_BRIDGING_WRAPPER_NULLABLE
120+
#undef AST_BRIDGING_WRAPPER_CONST_NONNULL
121+
#undef AST_BRIDGING_WRAPPER_CONST_NULLABLE
122+
#undef AST_BRIDGING_WRAPPER_CONST
101123
#undef AST_BRIDGING_WRAPPER

include/swift/AST/AvailabilitySpec.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class AvailabilityMacroMap {
254254
}
255255

256256
ArrayRef<AvailabilitySpec *> getEntry(StringRef name,
257-
llvm::VersionTuple version) {
257+
llvm::VersionTuple version) const {
258258
auto versions = Impl.find(name);
259259
if (versions == Impl.end()) {
260260
return {};

include/swift/Basic/BasicBridging.h

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,34 +100,42 @@ typedef uintptr_t SwiftUInt;
100100
// and Swift could import the underlying pointee type instead. We need to be
101101
// careful that the interface we expose remains consistent regardless of
102102
// PURE_BRIDGING_MODE.
103-
#define BRIDGING_WRAPPER_IMPL(Node, Name, Nullability) \
103+
#define BRIDGING_WRAPPER_IMPL(Node, Name, Qualifier, Nullability) \
104104
class Bridged##Name { \
105-
Node * Nullability Ptr; \
105+
Qualifier Node *Nullability Ptr; \
106106
\
107107
public: \
108108
SWIFT_UNAVAILABLE("Use init(raw:) instead") \
109-
Bridged##Name(Node * Nullability ptr) : Ptr(ptr) {} \
109+
Bridged##Name(Qualifier Node *Nullability ptr) : Ptr(ptr) {} \
110110
\
111111
SWIFT_UNAVAILABLE("Use '.raw' instead") \
112-
Node * Nullability unbridged() const { return Ptr; } \
112+
Qualifier Node *Nullability unbridged() const { return Ptr; } \
113113
}; \
114114
\
115115
SWIFT_NAME("getter:Bridged" #Name ".raw(self:)") \
116-
inline void * Nullability Bridged##Name##_getRaw(Bridged##Name bridged) { \
116+
inline Qualifier void *Nullability Bridged##Name##_getRaw( \
117+
Bridged##Name bridged) { \
117118
return bridged.unbridged(); \
118119
} \
119120
\
120121
SWIFT_NAME("Bridged" #Name ".init(raw:)") \
121-
inline Bridged##Name Bridged##Name##_fromRaw(void * Nullability ptr) { \
122-
return static_cast<Node *>(ptr); \
122+
inline Bridged##Name Bridged##Name##_fromRaw( \
123+
Qualifier void *Nullability ptr) { \
124+
return static_cast<Qualifier Node *>(ptr); \
123125
}
124126

125127
// Bridging wrapper macros for convenience.
126-
#define BRIDGING_WRAPPER_NONNULL(Node, Name) \
127-
BRIDGING_WRAPPER_IMPL(Node, Name, _Nonnull)
128+
#define BRIDGING_WRAPPER_NONNULL(Node, Name) \
129+
BRIDGING_WRAPPER_IMPL(Node, Name, /*unqualified*/, _Nonnull)
128130

129-
#define BRIDGING_WRAPPER_NULLABLE(Node, Name) \
130-
BRIDGING_WRAPPER_IMPL(Node, Nullable##Name, _Nullable)
131+
#define BRIDGING_WRAPPER_NULLABLE(Node, Name) \
132+
BRIDGING_WRAPPER_IMPL(Node, Nullable##Name, /*unqualified*/, _Nullable)
133+
134+
#define BRIDGING_WRAPPER_CONST_NONNULL(Node, Name) \
135+
BRIDGING_WRAPPER_IMPL(Node, Name, const, _Nonnull)
136+
137+
#define BRIDGING_WRAPPER_CONST_NULLABLE(Node, Name) \
138+
BRIDGING_WRAPPER_IMPL(Node, Nullable##Name, const, _Nullable)
131139

132140
void assertFail(const char * _Nonnull msg, const char * _Nonnull file,
133141
SwiftUInt line, const char * _Nonnull function);

lib/AST/Bridging/ASTContextBridging.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,5 @@ bool BridgedASTContext_canImport(BridgedASTContext cContext,
171171

172172
BridgedAvailabilityMacroMap
173173
BridgedASTContext_getAvailabilityMacroMap(BridgedASTContext cContext) {
174-
return const_cast<AvailabilityMacroMap *>(
175-
&cContext.unbridged().getAvailabilityMacroMap());
174+
return &cContext.unbridged().getAvailabilityMacroMap();
176175
}

0 commit comments

Comments
 (0)