14
14
#define SWIFT_IDE_IDEBRIDGING
15
15
16
16
#include " swift/Basic/BasicBridging.h"
17
+
18
+ #ifdef USED_IN_CPP_SOURCE
17
19
#include " swift/Basic/SourceLoc.h"
18
20
#include " llvm/ADT/Optional.h"
19
21
#include " llvm/CAS/CASReference.h"
20
22
#include < vector>
23
+ #endif
21
24
22
25
enum class LabelRangeType {
23
26
None,
@@ -37,6 +40,7 @@ enum class LabelRangeType {
37
40
38
41
enum class ResolvedLocContext { Default, Selector, Comment, StringLiteral };
39
42
43
+ #ifdef USED_IN_CPP_SOURCE
40
44
struct ResolvedLoc {
41
45
// / The range of the call's base name.
42
46
swift::CharSourceRange range;
@@ -65,13 +69,6 @@ struct ResolvedLoc {
65
69
66
70
ResolvedLocContext context;
67
71
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
-
75
72
ResolvedLoc (swift::CharSourceRange range,
76
73
std::vector<swift::CharSourceRange> labelRanges,
77
74
llvm::Optional<unsigned > firstTrailingLabel,
@@ -81,60 +78,68 @@ struct ResolvedLoc {
81
78
ResolvedLoc ();
82
79
};
83
80
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
+
84
109
// / A heap-allocated `std::vector<ResoledLoc>` that can be represented by an
85
110
// / opaque pointer value.
86
111
// /
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.
95
114
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;
99
117
100
118
public:
101
- // / Create heap-allocaed vector with the same elements as `vector`.
102
- BridgedResolvedLocVector (const std::vector<ResolvedLoc> &vector);
119
+ BridgedResolvedLocVector ();
103
120
104
121
// / Create a `BridgedResolvedLocVector` from an opaque value obtained from
105
122
// / `getOpaqueValue`.
106
123
BridgedResolvedLocVector (void *opaqueValue);
107
124
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
116
138
117
139
SWIFT_IMPORT_UNSAFE
118
140
void *getOpaqueValue () const ;
119
141
};
120
142
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
-
138
143
#ifdef __cplusplus
139
144
extern " C" {
140
145
#endif
0 commit comments