Skip to content

bridging: reduce #ifdef USED_IN_CPP_SOURCE in bridging headers #77185

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 1 commit into from
Oct 25, 2024
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
191 changes: 34 additions & 157 deletions include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,30 @@
#include "swift/Basic/BasicBridging.h"

#ifdef USED_IN_CPP_SOURCE
#include "swift/AST/ArgumentList.h"
#include "swift/AST/Attr.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DiagnosticConsumer.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/IfConfigClauseRangeInfo.h"
#include "swift/AST/Stmt.h"
#endif

SWIFT_BEGIN_NULLABILITY_ANNOTATIONS

namespace swift {
class Argument;
class ASTContext;
struct ASTNode;
class DeclAttributes;
class DeclBaseName;
class DeclNameLoc;
class DeclNameRef;
class DiagnosticArgument;
class DiagnosticEngine;
class Identifier;
class IfConfigClauseRangeInfo;
struct LabeledStmtInfo;
class ProtocolConformanceRef;
class Type;
class CanType;
class TypeBase;
class StmtConditionElement;
class SubstitutionMap;
}

Expand All @@ -62,14 +68,9 @@ class BridgedIdentifier {
SWIFT_NAME("init(raw:)")
BridgedIdentifier(const void *_Nullable raw) : Raw(raw) {}

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

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

SWIFT_NAME("getter:BridgedIdentifier.raw(self:)")
Expand All @@ -89,17 +90,9 @@ class BridgedDeclBaseName {
BridgedIdentifier Ident;

public:
// Ensure that this struct value type will be indirectly returned on
// Windows ARM64
BridgedDeclBaseName() : Ident() {}

#ifdef USED_IN_CPP_SOURCE
BridgedDeclBaseName(swift::DeclBaseName baseName) : Ident(baseName.Ident) {}
BRIDGED_INLINE BridgedDeclBaseName(swift::DeclBaseName baseName);

swift::DeclBaseName unbridged() const {
return swift::DeclBaseName(Ident.unbridged());
}
#endif
BRIDGED_INLINE swift::DeclBaseName unbridged() const;
};

SWIFT_NAME("BridgedDeclBaseName.createConstructor()")
Expand All @@ -119,17 +112,9 @@ class BridgedDeclNameRef {
void *_Nonnull opaque;

public:
// Ensure that this struct value type will be indirectly returned on
// Windows ARM64
BridgedDeclNameRef() : opaque() {}
BRIDGED_INLINE BridgedDeclNameRef(swift::DeclNameRef name);

#ifdef USED_IN_CPP_SOURCE
BridgedDeclNameRef(swift::DeclNameRef name) : opaque(name.getOpaqueValue()) {}

swift::DeclNameRef unbridged() const {
return swift::DeclNameRef::getFromOpaqueValue(opaque);
}
#endif
BRIDGED_INLINE swift::DeclNameRef unbridged() const;
};

SWIFT_NAME("BridgedDeclNameRef.createParsed(_:baseName:argumentLabels:)")
Expand All @@ -149,15 +134,9 @@ class BridgedDeclNameLoc {
public:
BridgedDeclNameLoc() : LocationInfo(nullptr), NumArgumentLabels(0) {}

#ifdef USED_IN_CPP_SOURCE
BridgedDeclNameLoc(swift::DeclNameLoc loc)
: LocationInfo(loc.LocationInfo),
NumArgumentLabels(loc.NumArgumentLabels) {}
BRIDGED_INLINE BridgedDeclNameLoc(swift::DeclNameLoc loc);

swift::DeclNameLoc unbridged() const {
return swift::DeclNameLoc(LocationInfo, NumArgumentLabels);
}
#endif
BRIDGED_INLINE swift::DeclNameLoc unbridged() const;
};

SWIFT_NAME("BridgedDeclNameLoc.createParsed(_:baseNameLoc:lParenLoc:"
Expand All @@ -179,17 +158,11 @@ class BridgedASTContext {
swift::ASTContext * _Nonnull Ctx;

public:
// Ensure that this struct value type will be indirectly returned on
// Windows ARM64
BridgedASTContext() : Ctx() {}

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

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

SWIFT_NAME("getter:BridgedASTContext.raw(self:)")
Expand Down Expand Up @@ -343,18 +316,7 @@ struct BridgedASTNode {
SWIFT_NAME("kind")
ASTNodeKind Kind;

#ifdef USED_IN_CPP_SOURCE
swift::ASTNode unbridged() const {
switch (Kind) {
case ASTNodeKindExpr:
return swift::ASTNode(static_cast<swift::Expr *>(Raw));
case ASTNodeKindStmt:
return swift::ASTNode(static_cast<swift::Stmt *>(Raw));
case ASTNodeKindDecl:
return swift::ASTNode(static_cast<swift::Decl *>(Raw));
}
}
#endif
BRIDGED_INLINE swift::ASTNode unbridged() const;
};

// Forward declare the underlying AST node type for each wrapper.
Expand Down Expand Up @@ -453,39 +415,16 @@ class BridgedDiagnosticArgument {
int64_t storage[3];

public:
// Ensure that this struct value type will be indirectly returned on
// Windows ARM64
BridgedDiagnosticArgument() {}

#ifdef USED_IN_CPP_SOURCE
BridgedDiagnosticArgument(const swift::DiagnosticArgument &arg) {
*reinterpret_cast<swift::DiagnosticArgument *>(&storage) = arg;
}
const swift::DiagnosticArgument &unbridged() const {
return *reinterpret_cast<const swift::DiagnosticArgument *>(&storage);
}
#endif
BRIDGED_INLINE BridgedDiagnosticArgument(const swift::DiagnosticArgument &arg);
BRIDGED_INLINE const swift::DiagnosticArgument &unbridged() const;

BridgedDiagnosticArgument(SwiftInt i);
BridgedDiagnosticArgument(BridgedStringRef s);
};

class BridgedDiagnosticFixIt {
int64_t storage[7];

public:
// Ensure that this struct value type will be indirectly returned on
// Windows ARM64
BridgedDiagnosticFixIt() {}

#ifdef USED_IN_CPP_SOURCE
BridgedDiagnosticFixIt(const swift::DiagnosticInfo::FixIt &fixit){
*reinterpret_cast<swift::DiagnosticInfo::FixIt *>(&storage) = fixit;
}
const swift::DiagnosticInfo::FixIt &unbridged() const {
return *reinterpret_cast<const swift::DiagnosticInfo::FixIt *>(&storage);
}
#endif
int64_t storage[7];

BridgedDiagnosticFixIt(BridgedSourceLoc start, uint32_t length, BridgedStringRef text);
};
Expand Down Expand Up @@ -585,16 +524,9 @@ struct BridgedDeclAttributes {

BridgedDeclAttributes() : chain(nullptr){};

#ifdef USED_IN_CPP_SOURCE
BridgedDeclAttributes(swift::DeclAttributes attrs)
: chain(attrs.getRawAttributeChain()) {}
BRIDGED_INLINE BridgedDeclAttributes(swift::DeclAttributes attrs);

swift::DeclAttributes unbridged() const {
swift::DeclAttributes attrs;
attrs.setRawAttributeChain(chain.unbridged());
return attrs;
}
#endif
BRIDGED_INLINE swift::DeclAttributes unbridged() const;
};

SWIFT_NAME("BridgedDeclAttributes.add(self:_:)")
Expand Down Expand Up @@ -1209,12 +1141,7 @@ struct BridgedCallArgument {
BridgedIdentifier label;
BridgedExpr argExpr;

#ifdef USED_IN_CPP_SOURCE
swift::Argument unbridged() const {
return swift::Argument(labelLoc.unbridged(), label.unbridged(),
argExpr.unbridged());
}
#endif
BRIDGED_INLINE swift::Argument unbridged() const;
};

SWIFT_NAME("BridgedArgumentList.createImplicitUnlabeled(_:exprs:)")
Expand Down Expand Up @@ -1528,29 +1455,16 @@ struct BridgedLabeledStmtInfo {
SWIFT_NAME("loc")
BridgedSourceLoc Loc;

#ifdef USED_IN_CPP_SOURCE
swift::LabeledStmtInfo unbridged() const {
return {Name.unbridged(), Loc.unbridged()};
}
#endif
BRIDGED_INLINE swift::LabeledStmtInfo unbridged() const;
};

class BridgedStmtConditionElement {
void *_Nonnull Raw;

public:
// Ensure that this struct value type will be indirectly returned on
// Windows ARM64
BridgedStmtConditionElement() {}

#ifdef USED_IN_CPP_SOURCE
BridgedStmtConditionElement(swift::StmtConditionElement elem)
: Raw(elem.getOpaqueValue()) {}
BRIDGED_INLINE BridgedStmtConditionElement(swift::StmtConditionElement elem);

swift::StmtConditionElement unbridged() const {
return swift::StmtConditionElement::fromOpaqueValue(Raw);
}
#endif
BRIDGED_INLINE swift::StmtConditionElement unbridged() const;
};

SWIFT_NAME("BridgedStmtConditionElement.createBoolean(expr:)")
Expand Down Expand Up @@ -2099,14 +2013,8 @@ class BridgedCanType {
struct BridgedConformance {
void * _Nullable opaqueValue;

#ifdef USED_IN_CPP_SOURCE
BridgedConformance(swift::ProtocolConformanceRef conformance)
: opaqueValue(conformance.getOpaqueValue()) {}

swift::ProtocolConformanceRef unbridged() const {
return swift::ProtocolConformanceRef::getFromOpaqueValue(opaqueValue);
}
#endif
BRIDGED_INLINE BridgedConformance(swift::ProtocolConformanceRef conformance);
BRIDGED_INLINE swift::ProtocolConformanceRef unbridged() const;

BridgedOwnedString getDebugDescription() const;
BRIDGED_INLINE bool isConcrete() const;
Expand All @@ -2122,15 +2030,6 @@ struct BridgedConformance {
struct BridgedConformanceArray {
BridgedArrayRef pcArray;

#ifdef USED_IN_CPP_SOURCE
BridgedConformanceArray(llvm::ArrayRef<swift::ProtocolConformanceRef> conformances)
: pcArray(conformances) {}

llvm::ArrayRef<swift::ProtocolConformanceRef> unbridged() const {
return pcArray.unbridged<swift::ProtocolConformanceRef>();
}
#endif

SwiftInt getCount() const { return SwiftInt(pcArray.Length); }

SWIFT_IMPORT_UNSAFE BRIDGED_INLINE
Expand Down Expand Up @@ -2168,29 +2067,7 @@ struct BridgedIfConfigClauseRangeInfo {
BridgedSourceLoc endLoc;
BridgedIfConfigClauseKind kind;

#ifdef USED_IN_CPP_SOURCE
swift::IfConfigClauseRangeInfo unbridged() const {
swift::IfConfigClauseRangeInfo::ClauseKind clauseKind;
switch (kind) {
case IfConfigActive:
clauseKind = swift::IfConfigClauseRangeInfo::ActiveClause;
break;

case IfConfigInactive:
clauseKind = swift::IfConfigClauseRangeInfo::InactiveClause;
break;

case IfConfigEnd:
clauseKind = swift::IfConfigClauseRangeInfo::EndDirective;
break;
}

return swift::IfConfigClauseRangeInfo(directiveLoc.unbridged(),
bodyLoc.unbridged(),
endLoc.unbridged(),
clauseKind);
}
#endif
BRIDGED_INLINE swift::IfConfigClauseRangeInfo unbridged() const;
};

//===----------------------------------------------------------------------===//
Expand Down
Loading