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
2225enum class LabelRangeType {
2326 None,
@@ -37,6 +40,7 @@ enum class LabelRangeType {
3740
3841enum class ResolvedLocContext { Default, Selector, Comment, StringLiteral };
3942
43+ #ifdef USED_IN_CPP_SOURCE
4044struct 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.
95114class 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
100118public:
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
139144extern " C" {
140145#endif
0 commit comments