Skip to content

Commit b803861

Browse files
committed
Bridging: Rename USED_IN_CPP_SOURCE and define it in Basic/SwiftBridging.h
1 parent dbedb21 commit b803861

File tree

6 files changed

+48
-37
lines changed

6 files changed

+48
-37
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "swift/AST/PlatformKind.h"
3030
#include "swift/Basic/BasicBridging.h"
3131

32-
#ifdef USED_IN_CPP_SOURCE
32+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
3333
#include "swift/AST/Attr.h"
3434
#include "swift/AST/Decl.h"
3535
#endif
@@ -379,7 +379,7 @@ struct OptionalBridgedDeclObj {
379379

380380
OptionalBridgedDeclObj(OptionalSwiftObject obj) : obj(obj) {}
381381

382-
#ifdef USED_IN_CPP_SOURCE
382+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
383383
template <class D> D *_Nullable getAs() const {
384384
if (obj)
385385
return llvm::cast<D>(static_cast<swift::Decl *>(obj));
@@ -391,7 +391,7 @@ struct OptionalBridgedDeclObj {
391391
struct BridgedDeclObj {
392392
SwiftObject obj;
393393

394-
#ifdef USED_IN_CPP_SOURCE
394+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
395395
template <class D> D *_Nonnull getAs() const {
396396
return llvm::cast<D>(static_cast<swift::Decl *>(obj));
397397
}

include/swift/Basic/BasicBridging.h

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,26 @@
1313
#ifndef SWIFT_BASIC_BASICBRIDGING_H
1414
#define SWIFT_BASIC_BASICBRIDGING_H
1515

16-
#if !defined(COMPILED_WITH_SWIFT) || !defined(PURE_BRIDGING_MODE)
17-
#define USED_IN_CPP_SOURCE
18-
#endif
19-
2016
// Do not add other C++/llvm/swift header files here!
2117
// Function implementations should be placed into BasicBridging.cpp and required header files should be added there.
2218
//
2319
// Pure bridging mode does not permit including any C++/llvm/swift headers.
2420
// See also the comments for `BRIDGING_MODE` in the top-level CMakeLists.txt file.
2521
//
26-
//
27-
// Note: On Windows ARM64, how a C++ struct/class value type is
28-
// returned is sensitive to conditions including whether a
29-
// user-defined constructor exists, etc. See
30-
// https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#return-values
31-
// So, if a C++ struct/class type is returned as a value between Swift
32-
// and C++, we need to be careful to match the return convention
33-
// matches between the non-USED_IN_CPP_SOURCE (Swift) side and the
34-
// USE_IN_CPP_SOURCE (C++) side.
35-
//
3622
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
37-
// !! Do not put any constructors inside an `#ifdef USED_IN_CPP_SOURCE` block !!
23+
// !! Do not put any constructors inside an !!
24+
// !! `#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE` block !!
3825
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3926

40-
#include "swift/Basic/BridgedSwiftObject.h"
27+
/// This header defines `NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE`, so it
28+
/// needs to be imported before the first use of the macro.
4129
#include "swift/Basic/SwiftBridging.h"
4230

31+
#include "swift/Basic/BridgedSwiftObject.h"
4332
#include <stddef.h>
4433
#include <stdint.h>
45-
#ifdef USED_IN_CPP_SOURCE
34+
35+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
4636
// Workaround to avoid a compiler error because `cas::ObjectRef` is not defined
4737
// when including VirtualFileSystem.h
4838
#include <cassert>
@@ -146,7 +136,7 @@ class BridgedArrayRef {
146136
BridgedArrayRef(const void *_Nullable data, size_t length)
147137
: Data(data), Length(length) {}
148138

149-
#ifdef USED_IN_CPP_SOURCE
139+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
150140
template <typename T>
151141
BridgedArrayRef(llvm::ArrayRef<T> arr)
152142
: Data(arr.data()), Length(arr.size()) {}
@@ -285,7 +275,7 @@ class BridgedOwnedString {
285275
};
286276
BRIDGED_OPTIONAL(SwiftInt, Int)
287277

288-
#ifdef USED_IN_CPP_SOURCE
278+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
289279
inline BridgedOptionalInt getFromAPInt(llvm::APInt i) {
290280
if (i.getSignificantBits() <=
291281
std::min(std::numeric_limits<SwiftInt>::digits, 64)) {
@@ -421,7 +411,7 @@ class BridgedCharSourceRangeVector {
421411
SWIFT_NAME("append(_:)")
422412
void push_back(BridgedCharSourceRange range);
423413

424-
#ifdef USED_IN_CPP_SOURCE
414+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
425415
/// Returns the `std::vector<swift::CharSourceRange>` that this
426416
/// `BridgedCharSourceRangeVector` represents and frees the memory owned by
427417
/// this `BridgedCharSourceRangeVector`.

include/swift/Basic/SwiftBridging.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,27 @@
9696
#define SWIFT_UNAVAILABLE(msg)
9797
#endif
9898

99+
#if !(defined(COMPILED_WITH_SWIFT) && defined(PURE_BRIDGING_MODE))
100+
/// Use this macro in a `#ifdef`/`#endif` fashion to wrap code that should not
101+
/// be imported into Swift in pure bridging mode, e.g. because an API is
102+
/// irrelevant on the Swift side, or because it requires std/llvm headers, which
103+
/// we don't want to import in this mode.
104+
///
105+
/// - Important: Do not put a constructor inside a
106+
/// `NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE` block unless there already is
107+
/// another unconditionally available user-defined constructor!
108+
///
109+
/// Note: On Windows ARM64, how a C++ struct/class value type is
110+
/// returned is sensitive to conditions including whether a
111+
/// user-defined constructor exists, etc. See
112+
/// https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#return-values
113+
///
114+
/// So, if a C++ struct/class type is returned as a value between Swift
115+
/// and C++, we need to be careful to match the return convention
116+
/// matches between the `NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE` (C++) side
117+
/// and the non-`NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE` (Swift) side.
118+
#define NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
119+
#endif
120+
99121
#endif // SWIFT_BASIC_SWIFT_BRIDGING_H
100122

include/swift/IDE/IDEBridging.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2022 - 2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2022 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -15,8 +15,7 @@
1515

1616
#include "swift/Basic/BasicBridging.h"
1717

18-
#ifdef USED_IN_CPP_SOURCE
19-
#include "swift/Basic/SourceLoc.h"
18+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
2019
#include "llvm/CAS/CASReference.h"
2120
#include <optional>
2221
#include <vector>
@@ -66,7 +65,7 @@ enum class LabelRangeType {
6665

6766
enum class ResolvedLocContext { Default, Selector, Comment, StringLiteral };
6867

69-
#ifdef USED_IN_CPP_SOURCE
68+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
7069
struct ResolvedLoc {
7170
/// The range of the call's base name.
7271
swift::CharSourceRange range;
@@ -105,7 +104,7 @@ struct ResolvedLoc {
105104
ResolvedLoc();
106105
};
107106

108-
#endif // USED_IN_CPP_SOURCE
107+
#endif // #ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
109108

110109
/// An opaque, heap-allocated `ResolvedLoc`.
111110
///
@@ -123,7 +122,7 @@ struct BridgedResolvedLoc {
123122
unsigned firstTrailingLabel, LabelRangeType labelType,
124123
bool isActive, ResolvedLocContext context);
125124

126-
#ifdef USED_IN_CPP_SOURCE
125+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
127126
ResolvedLoc takeUnbridged() {
128127
ResolvedLoc *resolvedLocPtr = static_cast<ResolvedLoc *>(resolvedLoc);
129128
ResolvedLoc unbridged = *resolvedLocPtr;
@@ -153,7 +152,7 @@ class BridgedResolvedLocVector {
153152
SWIFT_NAME("append(_:)")
154153
void push_back(BridgedResolvedLoc Loc);
155154

156-
#ifdef USED_IN_CPP_SOURCE
155+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
157156
std::vector<ResolvedLoc> takeUnbridged() {
158157
std::vector<ResolvedLoc> *vectorPtr =
159158
static_cast<std::vector<ResolvedLoc> *>(vector);

include/swift/SIL/SILBridging.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
//
2323
#include "swift/AST/ASTBridging.h"
2424

25-
#ifdef USED_IN_CPP_SOURCE
25+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
2626
#include "llvm/ADT/ArrayRef.h"
2727
#include "swift/SIL/SILInstruction.h"
2828
#include "swift/SIL/SILWitnessTable.h"
@@ -329,7 +329,7 @@ struct BridgedValueArray {
329329
const BridgeValueExistential * _Nullable base;
330330
size_t count;
331331

332-
#ifdef USED_IN_CPP_SOURCE
332+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
333333
llvm::ArrayRef<swift::SILValue> getValues(llvm::SmallVectorImpl<swift::SILValue> &storage);
334334
#endif
335335
};
@@ -639,7 +639,7 @@ struct BridgedSILDebugVariable {
639639
struct BridgedInstruction {
640640
SwiftObject obj;
641641

642-
#ifdef USED_IN_CPP_SOURCE
642+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
643643
template <class I> I *_Nonnull getAs() const {
644644
return llvm::cast<I>(static_cast<swift::SILNode *>(obj)->castToInstruction());
645645
}
@@ -1068,7 +1068,7 @@ struct BridgedWitnessTableEntry {
10681068
baseProtocol
10691069
};
10701070

1071-
#ifdef USED_IN_CPP_SOURCE
1071+
#ifdef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
10721072
static BridgedWitnessTableEntry bridge(const swift::SILWitnessTable::Entry &entry) {
10731073
BridgedWitnessTableEntry bridgedEntry;
10741074
*reinterpret_cast<swift::SILWitnessTable::Entry *>(&bridgedEntry.storage) = entry;

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -23,7 +23,7 @@
2323
#include "swift/AST/ASTBridging.h"
2424
#include "swift/SIL/SILBridging.h"
2525

26-
#ifndef USED_IN_CPP_SOURCE
26+
#ifndef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
2727

2828
// Pure bridging mode does not permit including any C++/llvm/swift headers.
2929
// See also the comments for `BRIDGING_MODE` in the top-level CMakeLists.txt file.
@@ -34,7 +34,7 @@
3434
#error "should not include llvm headers into bridging header"
3535
#endif
3636

37-
#endif // USED_IN_CPP_SOURCE
37+
#endif // #ifndef NOT_COMPILED_WITH_SWIFT_PURE_BRIDGING_MODE
3838

3939
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
4040

0 commit comments

Comments
 (0)