Skip to content

Commit 20b3a20

Browse files
committed
xxx
1 parent 81d7ec2 commit 20b3a20

File tree

8 files changed

+76
-6
lines changed

8 files changed

+76
-6
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ SWIFT_NAME("BridgedDeclNameRef.createParsed(_:baseName:argumentLabels:)")
120120
BridgedDeclNameRef
121121
BridgedDeclNameRef_createParsed(BridgedASTContext cContext,
122122
BridgedDeclBaseName cBaseName,
123-
BridgedErasedArrayRef cLabels);
123+
BridgedArrayRef<BridgedIdentifier> cLabels);
124124

125125
SWIFT_NAME("BridgedDeclNameRef.createParsed(_:)")
126126
BridgedDeclNameRef

include/swift/Basic/BasicBridging.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,55 @@ const void *_Nullable BridgedErasedArrayRef_data(BridgedErasedArrayRef arr);
147147
SWIFT_NAME("getter:BridgedErasedArrayRef.count(self:)")
148148
BRIDGED_INLINE SwiftInt BridgedErasedArrayRef_count(BridgedErasedArrayRef arr);
149149

150+
151+
/// Bridgable wrapper for 'llvm::ArrayRef<Element>'.
152+
template <typename _Element>
153+
class BridgedArrayRef {
154+
public:
155+
// Seemingly needed for Swift-side conformance.
156+
using Element = _Element;
157+
158+
SWIFT_UNAVAILABLE("Use '.data' instead")
159+
const void *_Nullable Data;
160+
161+
SWIFT_UNAVAILABLE("Use '.count' instead")
162+
size_t Length;
163+
164+
SWIFT_NAME("init(data:count:)")
165+
BridgedArrayRef(const Element *_Nullable data, SwiftInt length)
166+
: Data(data), Length(length) {}
167+
168+
#ifdef USED_IN_CPP_SOURCE
169+
using ArrRefTy = llvm::ArrayRef<Element>;
170+
171+
BridgedArrayRef(ArrRefTy arrRef)
172+
: Data(arrRef.data()), Length(arrRef.size()) {}
173+
174+
ArrRefTy unbridged() const {
175+
return ArrRefTy(static_cast<const Element *>(Data), Length);
176+
}
177+
#endif
178+
179+
bool empty() const { return Length == 0; }
180+
181+
}
182+
#ifdef IMPORTING_INTO_ASTGEN
183+
__attribute__((swift_attr("conforms_to:swiftASTGen.BridgedArrayRefProtocol")))
184+
#endif
185+
;
186+
187+
template<typename T>
188+
SWIFT_NAME("getter:BridgedArrayRef.data(self:)")
189+
inline const void *_Nullable BridgedArrayRef_data(BridgedArrayRef<T> arr) {
190+
return arr.Data;
191+
}
192+
193+
template<typename T>
194+
SWIFT_NAME("getter:BridgedArrayRef.count(self:)")
195+
inline SwiftInt BridgedArrayRef_count(BridgedArrayRef<T> arr) {
196+
return arr.Length;
197+
}
198+
150199
//===----------------------------------------------------------------------===//
151200
// MARK: Data
152201
//===----------------------------------------------------------------------===//

lib/AST/ASTBridging.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ BridgedDeclBaseName_createIdentifier(BridgedIdentifier identifier) {
7070
BridgedDeclNameRef
7171
BridgedDeclNameRef_createParsed(BridgedASTContext cContext,
7272
BridgedDeclBaseName cBaseName,
73-
BridgedErasedArrayRef cLabels) {
73+
BridgedArrayRef<BridgedIdentifier> cLabels) {
7474
ASTContext &context = cContext.unbridged();
7575
SmallVector<Identifier, 4> labels;
76-
for (auto &cLabel : cLabels.unbridged<BridgedIdentifier>()) {
76+
for (auto &cLabel : cLabels.unbridged()) {
7777
labels.push_back(cLabel.unbridged());
7878
}
7979
return DeclNameRef(DeclName(context, cBaseName.unbridged(), labels));

lib/ASTGen/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ endforeach()
8686

8787
set(compile_options
8888
${c_include_paths_args}
89-
"SHELL: -Xcc -std=c++17 -Xcc -DCOMPILED_WITH_SWIFT"
89+
"SHELL: -Xcc -std=c++17 -Xcc -DCOMPILED_WITH_SWIFT -Xcc -DIMPORTING_INTO_ASTGEN"
9090

9191
# FIXME: Needed to work around an availability issue with CxxStdlib
9292
"SHELL: -Xfrontend -disable-target-os-checking"

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,22 @@ extension Optional where Wrapped: LazyCollectionProtocol {
400400
}
401401
}
402402

403+
extension BridgedArrayRefProtocol {
404+
init<C: Collection>(
405+
_ collection: C?, in astgen: ASTGenVisitor
406+
) where C.Element == Element {
407+
guard let collection = collection, !collection.isEmpty else {
408+
self.init(data: nil, count: 0)
409+
return
410+
}
411+
412+
let buffer = astgen.allocator.allocate(Element.self, count: collection.count)
413+
_ = buffer.initialize(from: collection)
414+
415+
self.init(data: buffer.baseAddress, count: collection.count)
416+
}
417+
}
418+
403419
extension TokenSyntax {
404420
/// Get `Keyword` kind if the token is a keyword.
405421
var keywordKind: Keyword? {

lib/ASTGen/Sources/ASTGen/Bridge.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ extension BridgedSourceRange {
237237
}
238238
}
239239

240+
protocol BridgedArrayRefProtocol {
241+
associatedtype Element
242+
init(data: UnsafePointer<Element>?, count: Int)
243+
}
244+
240245
/// Helper collection type that lazily concatenates two collections.
241246
struct ConcatCollection<C1: Collection, C2: Collection> where C1.Element == C2.Element {
242247
let c1: C1

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ extension ASTGenVisitor {
416416
name: .createParsed(
417417
self.ctx,
418418
baseName: baseName,
419-
argumentLabels: labels.bridgedArray(in: self)
419+
argumentLabels: .init(labels, in: self)
420420
),
421421
loc: .createParsed(
422422
self.ctx,

lib/ASTGen/Sources/ASTGen/Literals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ extension ASTGenVisitor {
7676
let appendLiteral = BridgedDeclNameRef.createParsed(
7777
self.ctx,
7878
baseName: .createIdentifier(self.ctx.getIdentifier("appendLiteral")),
79-
argumentLabels: CollectionOfOne(BridgedIdentifier()).bridgedArray(in: self)
79+
argumentLabels: .init(CollectionOfOne(BridgedIdentifier()), in: self)
8080
)
8181
// Name reference to `appendInterpolation`. Arguments labels are not determined yet.
8282
let appendInterpolation = BridgedDeclNameRef.createParsed(

0 commit comments

Comments
 (0)