Skip to content

A couple of bridging cleanups #69534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 72 additions & 16 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,73 @@
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS

namespace swift {
class DiagnosticArgument;
class DiagnosticEngine;
class ASTContext;
class DiagnosticArgument;
class DiagnosticEngine;
}

//===----------------------------------------------------------------------===//
// MARK: Identifier
//===----------------------------------------------------------------------===//

struct BridgedIdentifier {
const void *_Nullable raw;
class BridgedIdentifier {
public:
SWIFT_UNAVAILABLE("Use '.raw' instead")
const void *_Nullable Raw;

BridgedIdentifier() : Raw(nullptr) {}

SWIFT_NAME("init(raw:)")
BridgedIdentifier(const void *_Nullable raw) : Raw(raw) {}

#ifdef USED_IN_CPP_SOURCE
BridgedIdentifier(swift::Identifier ident)
: Raw(ident.getAsOpaquePointer()) {}

swift::Identifier unbridged() const {
return swift::Identifier::getFromOpaquePointer(Raw);
}
#endif
};

SWIFT_NAME("getter:BridgedIdentifier.raw(self:)")
inline const void *_Nullable BridgedIdentifier_raw(BridgedIdentifier ident) {
return ident.Raw;
}

struct BridgedIdentifierAndSourceLoc {
BridgedIdentifier name;
BridgedSourceLoc nameLoc;
SWIFT_NAME("name")
BridgedIdentifier Name;

SWIFT_NAME("nameLoc")
BridgedSourceLoc NameLoc;
};

//===----------------------------------------------------------------------===//
// MARK: ASTContext
//===----------------------------------------------------------------------===//

struct BridgedASTContext {
void *_Nonnull raw;
class BridgedASTContext {
swift::ASTContext * _Nonnull Ctx;

public:
#ifdef USED_IN_CPP_SOURCE
SWIFT_UNAVAILABLE("Use init(raw:) instead")
BridgedASTContext(swift::ASTContext &ctx) : Ctx(&ctx) {}

SWIFT_UNAVAILABLE("Use '.raw' instead")
swift::ASTContext &unbridged() const { return *Ctx; }
#endif
};

SWIFT_NAME("getter:BridgedASTContext.raw(self:)")
BRIDGED_INLINE
void * _Nonnull BridgedASTContext_raw(BridgedASTContext bridged);

SWIFT_NAME("BridgedASTContext.init(raw:)")
BRIDGED_INLINE
BridgedASTContext BridgedASTContext_fromRaw(void * _Nonnull ptr);

SWIFT_NAME("BridgedASTContext.getIdentifier(self:_:)")
BridgedIdentifier BridgedASTContext_getIdentifier(BridgedASTContext cContext,
BridgedStringRef cStr);
Expand All @@ -70,8 +112,11 @@ enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : size_t {
};

struct BridgedASTNode {
void *_Nonnull ptr;
ASTNodeKind kind;
SWIFT_NAME("raw")
void *_Nonnull Raw;

SWIFT_NAME("kind")
ASTNodeKind Kind;
};

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

// Define the bridging wrappers for each AST node.
#define AST_BRIDGING_WRAPPER(Name) BRIDGING_WRAPPER_NONNULL(Name)
#define AST_BRIDGING_WRAPPER(Name) BRIDGING_WRAPPER_NONNULL(swift::Name, Name)
#include "swift/AST/ASTBridgingWrappers.def"

// For nullable nodes, also define a nullable variant.
#define AST_BRIDGING_WRAPPER_NULLABLE(Name) BRIDGING_WRAPPER_NULLABLE(Name)
#define AST_BRIDGING_WRAPPER_NULLABLE(Name) \
BRIDGING_WRAPPER_NULLABLE(swift::Name, Name)
#define AST_BRIDGING_WRAPPER_NONNULL(Name)
#include "swift/AST/ASTBridgingWrappers.def"

Expand Down Expand Up @@ -150,7 +196,7 @@ class BridgedDiagnosticArgument {
BridgedDiagnosticArgument(const swift::DiagnosticArgument &arg) {
*reinterpret_cast<swift::DiagnosticArgument *>(&storage) = arg;
}
const swift::DiagnosticArgument &get() const {
const swift::DiagnosticArgument &unbridged() const {
return *reinterpret_cast<const swift::DiagnosticArgument *>(&storage);
}
#endif
Expand All @@ -167,7 +213,7 @@ class BridgedDiagnosticFixIt {
BridgedDiagnosticFixIt(const swift::DiagnosticInfo::FixIt &fixit){
*reinterpret_cast<swift::DiagnosticInfo::FixIt *>(&storage) = fixit;
}
const swift::DiagnosticInfo::FixIt &get() const {
const swift::DiagnosticInfo::FixIt &unbridged() const {
return *reinterpret_cast<const swift::DiagnosticInfo::FixIt *>(&storage);
}
#endif
Expand All @@ -184,8 +230,18 @@ enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : size_t {
BridgedNote,
};

struct BridgedDiagnostic {
void *_Nonnull raw;
class BridgedDiagnostic {
public:
struct Impl;

SWIFT_UNAVAILABLE("Unavailable in Swift")
Impl *_Nonnull Raw;

SWIFT_UNAVAILABLE("Unavailable in Swift")
BridgedDiagnostic(Impl *_Nonnull raw) : Raw(raw) {}

SWIFT_UNAVAILABLE("Unavailable in Swift")
Impl *_Nonnull unbridged() const { return Raw; }
};

// FIXME: Can we bridge InFlightDiagnostic?
Expand Down
18 changes: 15 additions & 3 deletions include/swift/AST/ASTBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,36 @@

SWIFT_BEGIN_NULLABILITY_ANNOTATIONS

//===----------------------------------------------------------------------===//
// MARK: BridgedASTContext
//===----------------------------------------------------------------------===//

void * _Nonnull BridgedASTContext_raw(BridgedASTContext bridged) {
return &bridged.unbridged();
}

BridgedASTContext BridgedASTContext_fromRaw(void * _Nonnull ptr) {
return *static_cast<swift::ASTContext *>(ptr);
}

//===----------------------------------------------------------------------===//
// MARK: BridgedNominalTypeDecl
//===----------------------------------------------------------------------===//

BridgedStringRef BridgedNominalTypeDecl_getName(BridgedNominalTypeDecl decl) {
return decl.get()->getName().str();
return decl.unbridged()->getName().str();
}

bool BridgedNominalTypeDecl_isGlobalActor(BridgedNominalTypeDecl decl) {
return decl.get()->isGlobalActor();
return decl.unbridged()->isGlobalActor();
}

//===----------------------------------------------------------------------===//
// MARK: BridgedVarDecl
//===----------------------------------------------------------------------===//

BridgedStringRef BridgedVarDecl_getUserFacingName(BridgedVarDecl decl) {
return decl.get()->getBaseName().userFacingName();
return decl.unbridged()->getBaseName().userFacingName();
}

SWIFT_END_NULLABILITY_ANNOTATIONS
Expand Down
3 changes: 2 additions & 1 deletion include/swift/AST/Identifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Identifier {
}

bool empty() const { return Pointer == nullptr; }
bool nonempty() const { return !empty(); }

LLVM_ATTRIBUTE_USED bool is(StringRef string) const {
return str().equals(string);
Expand Down Expand Up @@ -180,7 +181,7 @@ class Identifier {
return static_cast<const void *>(Pointer);
}

static Identifier getFromOpaquePointer(void *P) {
static Identifier getFromOpaquePointer(const void *P) {
return Identifier((const char*)P);
}

Expand Down
92 changes: 70 additions & 22 deletions include/swift/Basic/BasicBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
#define BRIDGED_INLINE inline
#endif

namespace llvm {
class raw_ostream;
} // end namespace llvm

SWIFT_BEGIN_NULLABILITY_ANNOTATIONS

typedef intptr_t SwiftInt;
Expand All @@ -72,32 +76,32 @@ typedef uintptr_t SwiftUInt;
// PURE_BRIDGING_MODE.
#define BRIDGING_WRAPPER_IMPL(Node, Name, Nullability) \
class Bridged##Name { \
swift::Node * Nullability Ptr; \
Node * Nullability Ptr; \
\
public: \
SWIFT_UNAVAILABLE("Use init(raw:) instead") \
Bridged##Name(swift::Node * Nullability ptr) : Ptr(ptr) {} \
Bridged##Name(Node * Nullability ptr) : Ptr(ptr) {} \
\
SWIFT_UNAVAILABLE("Use '.raw' instead") \
swift::Node * Nullability get() const { return Ptr; } \
Node * Nullability unbridged() const { return Ptr; } \
}; \
\
SWIFT_NAME("getter:Bridged" #Name ".raw(self:)") \
inline void * Nullability Bridged##Name##_getRaw(Bridged##Name bridged) { \
return bridged.get(); \
return bridged.unbridged(); \
} \
\
SWIFT_NAME("Bridged" #Name ".init(raw:)") \
inline Bridged##Name Bridged##Name##_fromRaw(void * Nullability ptr) { \
return static_cast<swift::Node *>(ptr); \
return static_cast<Node *>(ptr); \
}

// Bridging wrapper macros for convenience.
#define BRIDGING_WRAPPER_NONNULL(Name) \
BRIDGING_WRAPPER_IMPL(Name, Name, _Nonnull)
#define BRIDGING_WRAPPER_NONNULL(Node, Name) \
BRIDGING_WRAPPER_IMPL(Node, Name, _Nonnull)

#define BRIDGING_WRAPPER_NULLABLE(Name) \
BRIDGING_WRAPPER_IMPL(Name, Nullable##Name, _Nullable)
#define BRIDGING_WRAPPER_NULLABLE(Node, Name) \
BRIDGING_WRAPPER_IMPL(Node, Nullable##Name, _Nullable)

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

template <typename T>
llvm::ArrayRef<T> get() const {
llvm::ArrayRef<T> unbridged() const {
return {static_cast<const T *>(Data), Length};
}
#endif
Expand Down Expand Up @@ -179,9 +183,7 @@ enum ENUM_EXTENSIBILITY_ATTR(open) BridgedFeature {
// MARK: OStream
//===----------------------------------------------------------------------===//

struct BridgedOStream {
void * _Nonnull streamAddr;
};
BRIDGING_WRAPPER_NONNULL(llvm::raw_ostream, OStream)

//===----------------------------------------------------------------------===//
// MARK: StringRef
Expand All @@ -196,7 +198,7 @@ class BridgedStringRef {
BridgedStringRef(llvm::StringRef sref)
: Data(sref.data()), Length(sref.size()) {}

llvm::StringRef get() const { return llvm::StringRef(Data, Length); }
llvm::StringRef unbridged() const { return llvm::StringRef(Data, Length); }
#endif

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

llvm::StringRef getRef() const { return llvm::StringRef(Data, Length); }
llvm::StringRef unbridgedRef() const { return llvm::StringRef(Data, Length); }
#endif

void destroy() const;
Expand Down Expand Up @@ -258,7 +260,7 @@ class BridgedSourceLoc {
#ifdef USED_IN_CPP_SOURCE
BridgedSourceLoc(swift::SourceLoc loc) : Raw(loc.getOpaquePointerValue()) {}

swift::SourceLoc get() const {
swift::SourceLoc unbridged() const {
return swift::SourceLoc(
llvm::SMLoc::getFromPointer(static_cast<const char *>(Raw)));
}
Expand All @@ -279,16 +281,62 @@ BRIDGED_INLINE bool BridgedSourceLoc_isValid(BridgedSourceLoc loc);
// MARK: SourceRange
//===----------------------------------------------------------------------===//

struct BridgedSourceRange {
BridgedSourceLoc startLoc;
BridgedSourceLoc endLoc;
class BridgedSourceRange {
public:
SWIFT_NAME("start")
BridgedSourceLoc Start;

SWIFT_NAME("end")
BridgedSourceLoc End;

SWIFT_NAME("init(start:end:)")
BridgedSourceRange(BridgedSourceLoc start, BridgedSourceLoc end)
: Start(start), End(end) {}

#ifdef USED_IN_CPP_SOURCE
BridgedSourceRange(swift::SourceRange range)
: Start(range.Start), End(range.End) {}

swift::SourceRange unbridged() const {
return swift::SourceRange(Start.unbridged(), End.unbridged());
}
#endif
};

struct BridgedCharSourceRange {
void *_Nonnull start;
size_t byteLength;
class BridgedCharSourceRange {
public:
SWIFT_UNAVAILABLE("Use '.start' instead")
BridgedSourceLoc Start;

SWIFT_UNAVAILABLE("Use '.byteLength' instead")
unsigned ByteLength;

SWIFT_NAME("init(start:byteLength:)")
BridgedCharSourceRange(BridgedSourceLoc start, unsigned byteLength)
: Start(start), ByteLength(byteLength) {}

#ifdef USED_IN_CPP_SOURCE
BridgedCharSourceRange(swift::CharSourceRange range)
: Start(range.getStart()), ByteLength(range.getByteLength()) {}

swift::CharSourceRange unbridged() const {
return swift::CharSourceRange(Start.unbridged(), ByteLength);
}
#endif
};

SWIFT_NAME("getter:BridgedCharSourceRange.start(self:)")
inline BridgedSourceLoc
BridgedCharSourceRange_start(BridgedCharSourceRange range) {
return range.Start;
}

SWIFT_NAME("getter:BridgedCharSourceRange.byteLength(self:)")
inline SwiftInt
BridgedCharSourceRange_byteLength(BridgedCharSourceRange range) {
return static_cast<SwiftInt>(range.ByteLength);
}

//===----------------------------------------------------------------------===//
// MARK: Plugins
//===----------------------------------------------------------------------===//
Expand Down
Loading