Skip to content

Commit fce1cb5

Browse files
committed
[AST] Merge BridgedDiagnosticEngine + BridgedDiagEngine
Introduce a macro that can stamp out wrapper classes for underlying C++ pointers, and use it to define BridgedDiagnosticEngine in ASTBridging. Then, migrate users of BridgedDiagEngine onto it.
1 parent fe0ad60 commit fce1cb5

File tree

18 files changed

+82
-58
lines changed

18 files changed

+82
-58
lines changed

SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ public struct DiagnosticFixIt {
5252
}
5353

5454
public struct DiagnosticEngine {
55-
private let bridged: BridgedDiagEngine
55+
private let bridged: BridgedDiagnosticEngine
5656

57-
public init(bridged: BridgedDiagEngine) {
57+
public init(bridged: BridgedDiagnosticEngine) {
5858
self.bridged = bridged
5959
}
60-
public init?(bridged: BridgedOptionalDiagnosticEngine) {
61-
guard let object = bridged.object else {
60+
public init?(bridged: BridgedNullableDiagnosticEngine) {
61+
guard let raw = bridged.raw else {
6262
return nil
6363
}
64-
self.bridged = BridgedDiagEngine(object: object)
64+
self.bridged = BridgedDiagnosticEngine(raw: raw)
6565
}
6666

6767
public func diagnose(_ position: SourceLoc?,

SwiftCompilerSources/Sources/Parse/Regex.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private func _RegexLiteralLexingFn(
4646
_ curPtrPtr: UnsafeMutablePointer<UnsafePointer<CChar>>,
4747
_ bufferEndPtr: UnsafePointer<CChar>,
4848
_ mustBeRegex: CBool,
49-
_ bridgedDiagnosticEngine: BridgedOptionalDiagnosticEngine
49+
_ bridgedDiagnosticEngine: BridgedNullableDiagnosticEngine
5050
) -> /*CompletelyErroneous*/ CBool {
5151
let inputPtr = curPtrPtr.pointee
5252

@@ -92,7 +92,7 @@ public func _RegexLiteralParsingFn(
9292
_ captureStructureOut: UnsafeMutableRawPointer,
9393
_ captureStructureSize: CUnsignedInt,
9494
_ bridgedDiagnosticBaseLoc: BridgedSourceLoc,
95-
_ bridgedDiagnosticEngine: BridgedDiagEngine
95+
_ bridgedDiagnosticEngine: BridgedDiagnosticEngine
9696
) -> Bool {
9797
let str = String(cString: inputPtr)
9898
let captureBuffer = UnsafeMutableRawBufferPointer(

include/swift/AST/ASTBridging.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2727

2828
namespace swift {
2929
class DiagnosticArgument;
30+
class DiagnosticEngine;
3031
}
3132

3233
//===----------------------------------------------------------------------===//
@@ -40,14 +41,8 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagID : uint32_t {
4041
#include "swift/AST/DiagnosticsAll.def"
4142
} BridgedDiagID;
4243

43-
// The name must not collide with BridgedDiagnosticEngine in CASTBridging.h.
44-
struct BridgedDiagEngine {
45-
void * _Nonnull object;
46-
};
47-
48-
struct BridgedOptionalDiagnosticEngine {
49-
void *_Nullable object;
50-
};
44+
BRIDGING_WRAPPER_NONNULL(DiagnosticEngine)
45+
BRIDGING_WRAPPER_NULLABLE(DiagnosticEngine)
5146

5247
class BridgedDiagnosticArgument {
5348
int64_t storage[3];
@@ -83,12 +78,13 @@ class BridgedDiagnosticFixIt {
8378
};
8479

8580
// FIXME: Can we bridge InFlightDiagnostic?
86-
void DiagnosticEngine_diagnose(BridgedDiagEngine, BridgedSourceLoc loc,
81+
void DiagnosticEngine_diagnose(BridgedDiagnosticEngine, BridgedSourceLoc loc,
8782
BridgedDiagID diagID, BridgedArrayRef arguments,
88-
BridgedSourceLoc highlightStart, uint32_t hightlightLength,
83+
BridgedSourceLoc highlightStart,
84+
uint32_t hightlightLength,
8985
BridgedArrayRef fixIts);
9086

91-
bool DiagnosticEngine_hadAnyError(BridgedDiagEngine);
87+
bool DiagnosticEngine_hadAnyError(BridgedDiagnosticEngine);
9288

9389
SWIFT_END_NULLABILITY_ANNOTATIONS
9490

include/swift/AST/BridgingUtils.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
1818

1919
namespace swift {
2020

21-
inline BridgedDiagEngine getBridgedDiagnosticEngine(DiagnosticEngine *D) {
22-
return {(void *)D};
23-
}
24-
inline BridgedOptionalDiagnosticEngine
25-
getBridgedOptionalDiagnosticEngine(DiagnosticEngine *D) {
26-
return {(void *)D};
27-
}
28-
2921
} // namespace swift
3022

3123
#endif

include/swift/AST/CASTBridging.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#ifndef SWIFT_C_AST_ASTBRIDGING_H
1414
#define SWIFT_C_AST_ASTBRIDGING_H
1515

16-
#include "swift/Basic/BasicBridging.h"
16+
#include "swift/AST/ASTBridging.h"
1717
#include "swift/Basic/CBasicBridging.h"
1818
#include "swift/Basic/Compiler.h"
1919
#include "swift/Basic/Nullability.h"
@@ -170,10 +170,6 @@ typedef struct BridgedDiagnostic {
170170
void *raw;
171171
} BridgedDiagnostic;
172172

173-
typedef struct BridgedDiagnosticEngine {
174-
void *raw;
175-
} BridgedDiagnosticEngine;
176-
177173
typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : size_t {
178174
/// An expanded macro.
179175
BridgedExpandedMacro = 0,

include/swift/Basic/BasicBridging.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@
4343
#define SWIFT_NAME(NAME)
4444
#endif
4545

46+
#if __has_attribute(availability)
47+
#define SWIFT_UNAVAILABLE(msg) \
48+
__attribute__((availability(swift, unavailable, message=msg)))
49+
#else
50+
#define SWIFT_UNAVAILABLE(msg)
51+
#endif
52+
4653
#ifdef PURE_BRIDGING_MODE
4754
// In PURE_BRIDGING_MODE, briding functions are not inlined
4855
#define BRIDGED_INLINE
@@ -55,6 +62,43 @@ SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
5562
typedef intptr_t SwiftInt;
5663
typedef uintptr_t SwiftUInt;
5764

65+
// Define a bridging wrapper that wraps an underlying C++ pointer type. When
66+
// importing into Swift, we expose an initializer and accessor that works with
67+
// `void *`, which is imported as UnsafeMutableRawPointer. Note we can't rely on
68+
// Swift importing the underlying C++ pointer as an OpaquePointer since that is
69+
// liable to change with PURE_BRIDGING_MODE, since that changes what we include,
70+
// and Swift could import the underlying pointee type instead. We need to be
71+
// careful that the interface we expose remains consistent regardless of
72+
// PURE_BRIDGING_MODE.
73+
#define BRIDGING_WRAPPER_IMPL(Node, Name, Nullability) \
74+
class Bridged##Name { \
75+
swift::Node * Nullability Ptr; \
76+
\
77+
public: \
78+
SWIFT_UNAVAILABLE("Use init(raw:) instead") \
79+
Bridged##Name(swift::Node * Nullability ptr) : Ptr(ptr) {} \
80+
\
81+
SWIFT_UNAVAILABLE("Use '.raw' instead") \
82+
swift::Node * Nullability get() const { return Ptr; } \
83+
}; \
84+
\
85+
SWIFT_NAME("getter:Bridged" #Name ".raw(self:)") \
86+
inline void * Nullability Bridged##Name##_getRaw(Bridged##Name bridged) { \
87+
return bridged.get(); \
88+
} \
89+
\
90+
SWIFT_NAME("Bridged" #Name ".init(raw:)") \
91+
inline Bridged##Name Bridged##Name##_fromRaw(void * Nullability ptr) { \
92+
return static_cast<swift::Node *>(ptr); \
93+
}
94+
95+
// Bridging wrapper macros for convenience.
96+
#define BRIDGING_WRAPPER_NONNULL(Name) \
97+
BRIDGING_WRAPPER_IMPL(Name, Name, _Nonnull)
98+
99+
#define BRIDGING_WRAPPER_NULLABLE(Name) \
100+
BRIDGING_WRAPPER_IMPL(Name, Nullable##Name, _Nullable)
101+
58102
struct BridgedOStream {
59103
void * _Nonnull streamAddr;
60104
};

include/swift/Parse/RegexParserBridging.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/// past.
2929
/// - MustBeRegex: whether an error during lexing should be considered a regex
3030
/// literal, or some thing else.
31-
/// - BridgedOptionalDiagnosticEngine: RegexLiteralLexingFn should diagnose the
31+
/// - BridgedNullableDiagnosticEngine: RegexLiteralLexingFn should diagnose the
3232
/// token using this engine.
3333
///
3434
/// Returns: A bool indicating whether lexing was completely erroneous, and
@@ -37,7 +37,7 @@
3737
typedef bool (*RegexLiteralLexingFn)(
3838
/*CurPtrPtr*/ const char *_Nonnull *_Nonnull,
3939
/*BufferEnd*/ const char *_Nonnull,
40-
/*MustBeRegex*/ bool, BridgedOptionalDiagnosticEngine);
40+
/*MustBeRegex*/ bool, BridgedNullableDiagnosticEngine);
4141
void Parser_registerRegexLiteralLexingFn(RegexLiteralLexingFn _Nullable fn);
4242

4343
/// Parse a regex literal string. Takes the following arguments:
@@ -58,7 +58,7 @@ typedef bool (*RegexLiteralParsingFn)(/*InputPtr*/ const char *_Nonnull,
5858
/*CaptureStructureOut*/ void *_Nonnull,
5959
/*CaptureStructureSize*/ unsigned,
6060
/*DiagnosticBaseLoc*/ BridgedSourceLoc,
61-
BridgedDiagEngine);
61+
BridgedDiagnosticEngine);
6262
void Parser_registerRegexLiteralParsingFn(RegexLiteralParsingFn _Nullable fn);
6363

6464
#endif // REGEX_PARSER_BRIDGING

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ struct BridgedPassContext {
181181
// AST
182182

183183
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE
184-
BridgedDiagEngine getDiagnosticEngine() const;
184+
BridgedDiagnosticEngine getDiagnosticEngine() const;
185185

186186
// SIL modifications
187187

include/swift/SILOptimizer/OptimizerBridgingImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ BridgedNominalTypeDecl BridgedPassContext::getSwiftArrayDecl() const {
175175
// AST
176176

177177
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE
178-
BridgedDiagEngine BridgedPassContext::getDiagnosticEngine() const {
178+
BridgedDiagnosticEngine BridgedPassContext::getDiagnosticEngine() const {
179179
swift::SILModule *mod = invocation->getPassManager()->getModule();
180180
return {&mod->getASTContext().Diags};
181181
}

lib/AST/ASTBridging.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@
1717

1818
using namespace swift;
1919

20-
namespace {
21-
/// BridgedDiagEngine -> DiagnosticEngine *.
22-
DiagnosticEngine *getDiagnosticEngine(const BridgedDiagEngine &bridged) {
23-
return static_cast<DiagnosticEngine *>(bridged.object);
24-
}
25-
26-
} // namespace
27-
2820
static_assert(sizeof(BridgedDiagnosticArgument) >= sizeof(DiagnosticArgument),
2921
"BridgedDiagnosticArgument has wrong size");
3022

@@ -45,12 +37,12 @@ BridgedDiagnosticFixIt::BridgedDiagnosticFixIt(BridgedSourceLoc start,
4537
llvm::ArrayRef<DiagnosticArgument>())) {}
4638

4739
void DiagnosticEngine_diagnose(
48-
BridgedDiagEngine bridgedEngine, BridgedSourceLoc loc,
40+
BridgedDiagnosticEngine bridgedEngine, BridgedSourceLoc loc,
4941
BridgedDiagID bridgedDiagID,
5042
BridgedArrayRef /*BridgedDiagnosticArgument*/ bridgedArguments,
5143
BridgedSourceLoc highlightStart, uint32_t hightlightLength,
5244
BridgedArrayRef /*BridgedDiagnosticFixIt*/ bridgedFixIts) {
53-
auto *D = getDiagnosticEngine(bridgedEngine);
45+
auto *D = bridgedEngine.get();
5446

5547
auto diagID = static_cast<DiagID>(bridgedDiagID);
5648
SmallVector<DiagnosticArgument, 2> arguments;
@@ -73,7 +65,6 @@ void DiagnosticEngine_diagnose(
7365
}
7466
}
7567

76-
bool DiagnosticEngine_hadAnyError(BridgedDiagEngine bridgedEngine) {
77-
auto *D = getDiagnosticEngine(bridgedEngine);
78-
return D->hadAnyError();
68+
bool DiagnosticEngine_hadAnyError(BridgedDiagnosticEngine bridgedEngine) {
69+
return bridgedEngine.get()->hadAnyError();
7970
}

0 commit comments

Comments
 (0)