Skip to content

Commit bc5cc43

Browse files
committed
Don’t include <vector> in IDEBridging.h
1 parent 14bc814 commit bc5cc43

File tree

4 files changed

+74
-80
lines changed

4 files changed

+74
-80
lines changed

include/swift/IDE/IDEBridging.h

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
#define SWIFT_IDE_IDEBRIDGING
1515

1616
#include "swift/Basic/BasicBridging.h"
17+
18+
#ifdef USED_IN_CPP_SOURCE
1719
#include "swift/Basic/SourceLoc.h"
1820
#include "llvm/ADT/Optional.h"
1921
#include "llvm/CAS/CASReference.h"
2022
#include <vector>
23+
#endif
2124

2225
enum class LabelRangeType {
2326
None,
@@ -37,6 +40,7 @@ enum class LabelRangeType {
3740

3841
enum class ResolvedLocContext { Default, Selector, Comment, StringLiteral };
3942

43+
#ifdef USED_IN_CPP_SOURCE
4044
struct ResolvedLoc {
4145
/// The range of the call's base name.
4246
swift::CharSourceRange range;
@@ -65,13 +69,6 @@ struct ResolvedLoc {
6569

6670
ResolvedLocContext context;
6771

68-
SWIFT_NAME(
69-
"init(range:labelRanges:firstTrailingLabel:labelType:isActive:context:)")
70-
ResolvedLoc(BridgedCharSourceRange range,
71-
BridgedCharSourceRangeVector labelRanges,
72-
unsigned firstTrailingLabel, LabelRangeType labelType,
73-
bool isActive, ResolvedLocContext context);
74-
7572
ResolvedLoc(swift::CharSourceRange range,
7673
std::vector<swift::CharSourceRange> labelRanges,
7774
llvm::Optional<unsigned> firstTrailingLabel,
@@ -81,60 +78,68 @@ struct ResolvedLoc {
8178
ResolvedLoc();
8279
};
8380

81+
#endif // USED_IN_CPP_SOURCE
82+
83+
/// An opaque, heap-allocated `ResolvedLoc`.
84+
///
85+
/// This type is manually memory managed. The creator of the object needs to
86+
/// ensure that `takeUnbridged` is called to free the memory.
87+
struct BridgedResolvedLoc {
88+
/// Opaque pointer to `ResolvedLoc`.
89+
void *resolvedLoc;
90+
91+
/// This consumes `labelRanges` by calling `takeUnbridged` on it.
92+
SWIFT_NAME(
93+
"init(range:labelRanges:firstTrailingLabel:labelType:isActive:context:)")
94+
BridgedResolvedLoc(BridgedCharSourceRange range,
95+
BridgedCharSourceRangeVector labelRanges,
96+
unsigned firstTrailingLabel, LabelRangeType labelType,
97+
bool isActive, ResolvedLocContext context);
98+
99+
#ifdef USED_IN_CPP_SOURCE
100+
ResolvedLoc takeUnbridged() {
101+
ResolvedLoc *resolvedLocPtr = static_cast<ResolvedLoc *>(resolvedLoc);
102+
ResolvedLoc unbridged = *resolvedLocPtr;
103+
delete resolvedLocPtr;
104+
return unbridged;
105+
}
106+
#endif
107+
};
108+
84109
/// A heap-allocated `std::vector<ResoledLoc>` that can be represented by an
85110
/// opaque pointer value.
86111
///
87-
/// This allows us to perform all the memory management for the heap-allocated
88-
/// vector in C++. This makes it easier to manage because creating and
89-
/// destorying an object in C++ is consistent with whether elements within the
90-
/// vector are destroyed as well.
91-
///
92-
/// - Note: This should no longer be necessary when we use C++ to Swift interop.
93-
/// In that case `swift_SwiftIDEUtilsBridging_runNameMatcher` can return a
94-
/// `ResolvedLocVector`.
112+
/// This type is manually memory managed. The creator of the object needs to
113+
/// ensure that `takeUnbridged` is called to free the memory.
95114
class BridgedResolvedLocVector {
96-
friend void *BridgedResolvedLocVector_getOpaqueValue(const BridgedResolvedLocVector &vector);
97-
98-
std::vector<ResolvedLoc> *vector;
115+
/// Opaque pointer to `std::vector<ResolvedLoc>`
116+
void *vector;
99117

100118
public:
101-
/// Create heap-allocaed vector with the same elements as `vector`.
102-
BridgedResolvedLocVector(const std::vector<ResolvedLoc> &vector);
119+
BridgedResolvedLocVector();
103120

104121
/// Create a `BridgedResolvedLocVector` from an opaque value obtained from
105122
/// `getOpaqueValue`.
106123
BridgedResolvedLocVector(void *opaqueValue);
107124

108-
void push_back(const ResolvedLoc &Loc);
109-
110-
/// Get the underlying vector.
111-
const std::vector<ResolvedLoc> &unbridged();
112-
113-
/// Delete the heap-allocated memory owned by this object. Accessing
114-
/// `unbridged` is illegal after calling `destroy`.
115-
void destroy();
125+
/// This consumes `Loc`, calling `takeUnbridged` on it.
126+
SWIFT_NAME("append(_:)")
127+
void push_back(BridgedResolvedLoc Loc);
128+
129+
#ifdef USED_IN_CPP_SOURCE
130+
std::vector<ResolvedLoc> takeUnbridged() {
131+
std::vector<ResolvedLoc> *vectorPtr =
132+
static_cast<std::vector<ResolvedLoc> *>(vector);
133+
std::vector<ResolvedLoc> unbridged = *vectorPtr;
134+
delete vectorPtr;
135+
return unbridged;
136+
}
137+
#endif
116138

117139
SWIFT_IMPORT_UNSAFE
118140
void *getOpaqueValue() const;
119141
};
120142

121-
122-
SWIFT_NAME("BridgedResolvedLocVector.empty()")
123-
BridgedResolvedLocVector BridgedResolvedLocVector_createEmpty();
124-
125-
typedef std::vector<BridgedSourceLoc> SourceLocVector;
126-
127-
/// Needed so that we can manually conform `SourceLocVectorIterator` to
128-
/// `UnsafeCxxInputIterator` on the Swift side because the conformance is not
129-
/// automatically generated by C++ interop by a Swift 5.8 compiler.
130-
typedef std::vector<BridgedSourceLoc>::const_iterator SourceLocVectorIterator;
131-
132-
SWIFT_NAME("SourceLocVectorIterator.equals(_:_:)")
133-
inline bool SourceLocVectorIterator_equal(const SourceLocVectorIterator &lhs,
134-
const SourceLocVectorIterator &rhs) {
135-
return lhs == rhs;
136-
}
137-
138143
#ifdef __cplusplus
139144
extern "C" {
140145
#endif

lib/ASTGen/Sources/SwiftIDEUtilsBridging/NameMatcherBridging.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ fileprivate extension BridgedCharSourceRangeVector {
3737

3838
fileprivate extension BridgedResolvedLocVector {
3939
init(from resolvedLocs: some Sequence<DeclNameLocation>, in sourceFile: ExportedSourceFile) {
40-
self = .empty()
40+
self = .init()
4141
for resolvedLoc in resolvedLocs {
42-
self.push_back(IDEBridging.ResolvedLoc(from: resolvedLoc, in: sourceFile))
42+
self.append(BridgedResolvedLoc(from: resolvedLoc, in: sourceFile))
4343
}
4444
}
4545
}
@@ -56,7 +56,7 @@ fileprivate extension IDEBridging.LabelRangeType {
5656
}
5757
}
5858

59-
extension IDEBridging.ResolvedLoc {
59+
extension BridgedResolvedLoc {
6060
init(from resolvedLoc: DeclNameLocation, in sourceFile: ExportedSourceFile) {
6161
let firstTrailingClosureIndex: UInt32
6262
if case .call(_, .some(let index)) = resolvedLoc.arguments {

lib/IDE/IDEBridging.cpp

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@
1414
#include "llvm/Support/raw_ostream.h"
1515
#include <climits>
1616

17-
ResolvedLoc::ResolvedLoc(BridgedCharSourceRange range,
18-
BridgedCharSourceRangeVector labelRanges,
19-
unsigned firstTrailingLabel, LabelRangeType labelType,
20-
bool isActive, ResolvedLocContext context)
21-
: range(range.unbridged()), labelRanges(labelRanges.takeUnbridged()),
22-
firstTrailingLabel(firstTrailingLabel == UINT_MAX
23-
? llvm::None
24-
: llvm::Optional<unsigned>(firstTrailingLabel)),
25-
labelType(labelType), isActive(isActive), context(context) {}
26-
2717
ResolvedLoc::ResolvedLoc(swift::CharSourceRange range,
2818
std::vector<swift::CharSourceRange> labelRanges,
2919
llvm::Optional<unsigned> firstTrailingLabel,
@@ -35,27 +25,27 @@ ResolvedLoc::ResolvedLoc(swift::CharSourceRange range,
3525

3626
ResolvedLoc::ResolvedLoc() {}
3727

38-
BridgedResolvedLocVector BridgedResolvedLocVector_createEmpty() {
39-
return BridgedResolvedLocVector(new std::vector<ResolvedLoc>());
40-
}
41-
42-
void BridgedResolvedLocVector::push_back(const ResolvedLoc &Loc) {
43-
this->vector->push_back(Loc);
28+
BridgedResolvedLoc::BridgedResolvedLoc(BridgedCharSourceRange range,
29+
BridgedCharSourceRangeVector labelRanges,
30+
unsigned firstTrailingLabel,
31+
LabelRangeType labelType, bool isActive,
32+
ResolvedLocContext context)
33+
: resolvedLoc(
34+
new ResolvedLoc(range.unbridged(), labelRanges.takeUnbridged(),
35+
firstTrailingLabel == UINT_MAX
36+
? llvm::None
37+
: llvm::Optional<unsigned>(firstTrailingLabel),
38+
labelType, isActive, context)) {}
39+
40+
BridgedResolvedLocVector::BridgedResolvedLocVector()
41+
: vector(new std::vector<BridgedResolvedLoc>()) {}
42+
43+
void BridgedResolvedLocVector::push_back(BridgedResolvedLoc Loc) {
44+
static_cast<std::vector<ResolvedLoc> *>(vector)->push_back(
45+
Loc.takeUnbridged());
4446
}
4547

46-
BridgedResolvedLocVector::BridgedResolvedLocVector(
47-
const std::vector<ResolvedLoc> &vector)
48-
: vector(new std::vector<ResolvedLoc>(vector)) {}
49-
5048
BridgedResolvedLocVector::BridgedResolvedLocVector(void *opaqueValue)
51-
: vector(static_cast<std::vector<ResolvedLoc> *>(opaqueValue)) {}
49+
: vector(opaqueValue) {}
5250

53-
const std::vector<ResolvedLoc> &BridgedResolvedLocVector::unbridged() {
54-
return *vector;
55-
}
56-
57-
void BridgedResolvedLocVector::destroy() { delete vector; }
58-
59-
void *BridgedResolvedLocVector::getOpaqueValue() const {
60-
return static_cast<void *>(this->vector);
61-
}
51+
void *BridgedResolvedLocVector::getOpaqueValue() const { return vector; }

lib/Refactoring/SyntacticRename.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ swift::ide::resolveRenameLocations(ArrayRef<RenameLoc> RenameLocs,
8181
swift_SwiftIDEUtilsBridging_runNameMatcher(SF.getExportedSourceFile(),
8282
BridgedUnresolvedLocs.data(),
8383
BridgedUnresolvedLocs.size());
84-
SWIFT_DEFER { bridgedResolvedLocs.destroy(); };
8584
const std::vector<ResolvedLoc> &resolvedLocsInSourceOrder =
86-
bridgedResolvedLocs.unbridged();
85+
bridgedResolvedLocs.takeUnbridged();
8786

8887
// Callers expect the resolved locs in the same order as the unresolved locs.
8988
// Sort them.

0 commit comments

Comments
 (0)