Skip to content

Commit 0e2d438

Browse files
committed
[cxx-interop][SwiftCompilerSources] Use llvm::StringRef instead of BridgedStringRef
rdar://83361000
1 parent 023c9a0 commit 0e2d438

File tree

19 files changed

+88
-117
lines changed

19 files changed

+88
-117
lines changed

SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public protocol DiagnosticArgument {
2121
}
2222
extension String: DiagnosticArgument {
2323
public func _withBridgedDiagnosticArgument(_ fn: (swift.DiagnosticArgument) -> Void) {
24-
withBridgedStringRef { fn(swift.DiagnosticArgument(llvm.StringRef($0))) }
24+
_withStringRef { fn(swift.DiagnosticArgument($0)) }
2525
}
2626
}
2727
extension Int: DiagnosticArgument {
@@ -42,10 +42,10 @@ public struct DiagnosticFixIt {
4242
}
4343

4444
func withBridgedDiagnosticFixIt(_ fn: (swift.DiagnosticInfo.FixIt) -> Void) {
45-
text.withBridgedStringRef { bridgedTextRef in
45+
text._withStringRef { bridgedTextRef in
4646
let bridgedDiagnosticFixIt = swift.DiagnosticInfo.FixIt(
4747
swift.CharSourceRange(start.bridged, UInt32(byteLength)),
48-
llvm.StringRef(bridgedTextRef),
48+
bridgedTextRef,
4949
llvm.ArrayRef<swift.DiagnosticArgument>())
5050
fn(bridgedDiagnosticFixIt)
5151
}

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@ import std
1818
//===----------------------------------------------------------------------===//
1919

2020
public struct StringRef : CustomStringConvertible, CustomReflectable {
21-
let _bridged : BridgedStringRef
21+
let _bridged: llvm.StringRef
2222

23-
public init(bridged: BridgedStringRef) { self._bridged = bridged }
23+
public init(bridged: llvm.StringRef) { self._bridged = bridged }
2424

2525
public var string: String { _bridged.string }
2626
public var description: String { string }
2727
public var customMirror: Mirror { Mirror(self, children: []) }
2828

2929
public static func ==(lhs: StringRef, rhs: StaticString) -> Bool {
30-
let lhsBuffer = UnsafeBufferPointer<UInt8>(start: lhs._bridged.data, count: Int(lhs._bridged.length))
30+
let lhsBuffer = UnsafeBufferPointer<UInt8>(
31+
start: lhs._bridged.__bytes_beginUnsafe(),
32+
count: Int(lhs._bridged.__bytes_endUnsafe() - lhs._bridged.__bytes_beginUnsafe()))
3133
return rhs.withUTF8Buffer { (rhsBuffer: UnsafeBufferPointer<UInt8>) in
3234
if lhsBuffer.count != rhsBuffer.count { return false }
3335
return lhsBuffer.elementsEqual(rhsBuffer, by: ==)
@@ -41,24 +43,19 @@ public struct StringRef : CustomStringConvertible, CustomReflectable {
4143
// Bridging Utilities
4244
//===----------------------------------------------------------------------===//
4345

44-
extension BridgedStringRef {
45-
public var string: String {
46-
let buffer = UnsafeBufferPointer<UInt8>(start: data, count: Int(length))
47-
return String(decoding: buffer, as: UTF8.self)
48-
}
49-
}
50-
5146
extension llvm.StringRef {
52-
public init(_ bridged: BridgedStringRef) {
53-
self.init(bridged.data, bridged.length)
47+
public var string: String {
48+
String(_cxxString: self.str())
5449
}
5550
}
5651

5752
extension String {
58-
public func withBridgedStringRef<T>(_ c: (BridgedStringRef) -> T) -> T {
53+
/// Underscored to avoid name collision with Swift LLVM Bindings.
54+
/// To be replaced with a bindings call once bindings are a dependency.
55+
public func _withStringRef<T>(_ c: (llvm.StringRef) -> T) -> T {
5956
var str = self
6057
return str.withUTF8 { buffer in
61-
return c(BridgedStringRef(data: buffer.baseAddress, length: buffer.count))
58+
return c(llvm.StringRef(buffer.baseAddress, buffer.count))
6259
}
6360
}
6461

SwiftCompilerSources/Sources/Optimizer/PassManager/PassContext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct PassContext {
6666

6767
func loadFunction(name: StaticString) -> Function? {
6868
return name.withUTF8Buffer { (nameBuffer: UnsafeBufferPointer<UInt8>) in
69-
PassContext_loadFunction(_bridged, BridgedStringRef(data: nameBuffer.baseAddress, length: nameBuffer.count)).function
69+
PassContext_loadFunction(_bridged, llvm.StringRef(nameBuffer.baseAddress, nameBuffer.count)).function
7070
}
7171
}
7272

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public func initializeSwiftModules() {
2424
private func registerPass(
2525
_ pass: FunctionPass,
2626
_ runFn: @escaping (@convention(c) (BridgedFunctionPassCtxt) -> ())) {
27-
pass.name.withBridgedStringRef { nameStr in
27+
pass.name._withStringRef { nameStr in
2828
SILPassManager_registerFunctionPass(nameStr, runFn)
2929
}
3030
}
3131

3232
private func registerPass<InstType: Instruction>(
3333
_ pass: InstructionPass<InstType>,
3434
_ runFn: @escaping (@convention(c) (BridgedInstructionPassCtxt) -> ())) {
35-
pass.name.withBridgedStringRef { nameStr in
35+
pass.name._withStringRef { nameStr in
3636
SILCombine_registerInstructionPass(nameStr, runFn)
3737
}
3838
}

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public struct Builder {
6161
operandType: Type, resultType: Type, arguments: [Value]) -> BuiltinInst {
6262
notifyInstructionsChanged()
6363
return arguments.withBridgedValues { valuesRef in
64-
return name.withBridgedStringRef { nameStr in
64+
return name._withStringRef { nameStr in
6565
let bi = SILBuilder_createBuiltinBinaryFunction(
6666
bridged, nameStr, operandType.bridged, resultType.bridged, valuesRef)
6767
return bi.getAs(BuiltinInst.self)
@@ -71,7 +71,7 @@ public struct Builder {
7171

7272
public func createCondFail(condition: Value, message: String) -> CondFailInst {
7373
notifyInstructionsChanged()
74-
return message.withBridgedStringRef { messageStr in
74+
return message._withStringRef { messageStr in
7575
let cf = SILBuilder_createCondFail(bridged, condition.bridged, messageStr)
7676
return cf.getAs(CondFailInst.self)
7777
}

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ final public class Function : CustomStringConvertible, HasShortDescription {
6767

6868
public func hasSemanticsAttribute(_ attr: StaticString) -> Bool {
6969
attr.withUTF8Buffer { (buffer: UnsafeBufferPointer<UInt8>) in
70-
SILFunction_hasSemanticsAttr(bridged, BridgedStringRef(data: buffer.baseAddress!, length: buffer.count)) != 0
70+
SILFunction_hasSemanticsAttr(bridged, llvm.StringRef(buffer.baseAddress!, buffer.count)) != 0
7171
}
7272
}
7373

@@ -105,18 +105,18 @@ final public class Function : CustomStringConvertible, HasShortDescription {
105105
// writeFn
106106
{ (f: BridgedFunction, os: BridgedOStream, idx: Int) in
107107
let s = f.function.effects.argumentEffects[idx].description
108-
s.withBridgedStringRef { OStream_write(os, $0) }
108+
s._withStringRef { OStream_write(os, $0) }
109109
},
110110
// parseFn:
111-
{ (f: BridgedFunction, str: BridgedStringRef, fromSIL: Int, isDerived: Int, paramNames: BridgedArrayRef) -> BridgedParsingError in
111+
{ (f: BridgedFunction, str: llvm.StringRef, fromSIL: Int, isDerived: Int, paramNames: BridgedArrayRef) -> BridgedParsingError in
112112
do {
113113
var parser = StringParser(str.string)
114114
let effect: ArgumentEffect
115115
if fromSIL != 0 {
116116
effect = try parser.parseEffectFromSIL(for: f.function, isDerived: isDerived != 0)
117117
} else {
118-
let paramToIdx = paramNames.withElements(ofType: BridgedStringRef.self) {
119-
(buffer: UnsafeBufferPointer<BridgedStringRef>) -> Dictionary<String, Int> in
118+
let paramToIdx = paramNames.withElements(ofType: llvm.StringRef.self) {
119+
(buffer: UnsafeBufferPointer<llvm.StringRef>) -> Dictionary<String, Int> in
120120
let keyValPairs = buffer.enumerated().lazy.map { ($0.1.string, $0.0) }
121121
return Dictionary(uniqueKeysWithValues: keyValPairs)
122122
}

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Basic
1414
import SILBridging
1515

1616
private func register<T: AnyObject>(_ cl: T.Type) {
17-
String(describing: cl).withBridgedStringRef { nameStr in
17+
String(describing: cl)._withStringRef { nameStr in
1818
let metatype = unsafeBitCast(cl, to: SwiftMetatype.self)
1919
registerBridgedClass(nameStr, metatype)
2020
}

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public struct Type : CustomStringConvertible, CustomReflectable {
4444
}
4545

4646
public func getIndexOfEnumCase(withName name: String) -> Int? {
47-
let idx = name.withBridgedStringRef {
47+
let idx = name._withStringRef {
4848
SILType_getCaseIdxOfEnumType(bridged, $0)
4949
}
5050
return idx >= 0 ? idx : nil
@@ -75,7 +75,7 @@ public struct NominalFieldsArray : RandomAccessCollection, FormattedLikeArray {
7575
}
7676

7777
public func getIndexOfField(withName name: String) -> Int? {
78-
let idx = name.withBridgedStringRef {
78+
let idx = name._withStringRef {
7979
SILType_getFieldIdxOfNominalType(type.bridged, $0)
8080
}
8181
return idx >= 0 ? idx : nil

include/swift/Basic/BasicBridging.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2222
typedef intptr_t SwiftInt;
2323
typedef uintptr_t SwiftUInt;
2424

25-
typedef struct {
26-
const unsigned char * _Nullable data;
27-
size_t length;
28-
} BridgedStringRef;
29-
3025
typedef struct {
3126
const void * _Nullable data;
3227
size_t numElements;
@@ -36,9 +31,7 @@ typedef struct {
3631
void * _Nonnull streamAddr;
3732
} BridgedOStream;
3833

39-
void OStream_write(BridgedOStream os, BridgedStringRef str);
40-
41-
void freeBridgedStringRef(BridgedStringRef str);
34+
void OStream_write(BridgedOStream os, llvm::StringRef str);
4235

4336
SWIFT_END_NULLABILITY_ANNOTATIONS
4437

include/swift/Basic/BridgingUtils.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@
2323

2424
namespace swift {
2525

26-
inline BridgedStringRef getBridgedStringRef(llvm::StringRef str) {
27-
return {(const unsigned char *)str.data(), str.size()};
28-
}
29-
30-
inline llvm::StringRef getStringRef(BridgedStringRef str) {
31-
return llvm::StringRef((const char *)str.data, str.length);
32-
}
33-
3426
template <typename T>
3527
inline llvm::ArrayRef<T> getArrayRef(BridgedArrayRef bridged) {
3628
return {static_cast<const T *>(bridged.data), bridged.numElements};

0 commit comments

Comments
 (0)