Skip to content

Commit f063701

Browse files
committed
[cxx-interop][libswift] Use std::string instead of BridgedStringRef.
The first C-bridge to be removed! 🚀
1 parent 25e5bc2 commit f063701

File tree

9 files changed

+35
-27
lines changed

9 files changed

+35
-27
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ endfunction()
723723
# cmake's builtin swift support.
724724
function(add_libswift name)
725725
set(libswift_compile_options
726-
"-target" "x86_64-apple-macosx10.15" # TODO: this is a hack until I figure out where the target is being set.
726+
"-target" "x86_64-apple-macosx10.15" # TODO: remove this once #38675 lands.
727727
"-Xfrontend" "-validate-tbd-against-ir=none"
728728
"-Xfrontend" "-enable-cxx-interop")
729729

include/swift/SIL/SILBridging.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515

1616
#include "BridgedSwiftObject.h"
1717
#include <stddef.h>
18-
19-
#ifdef __cplusplus
20-
extern "C" {
21-
#endif
18+
#include <string>
2219

2320
typedef struct {
2421
const unsigned char * _Nullable data;
@@ -155,17 +152,17 @@ BridgedSlab PassContext_freeSlab(BridgedPassContext passContext,
155152
BridgedSlab slab);
156153

157154
BridgedStringRef SILFunction_getName(BridgedFunction function);
158-
BridgedStringRef SILFunction_debugDescription(BridgedFunction function);
155+
std::string SILFunction_debugDescription(BridgedFunction function);
159156
OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function);
160157
OptionalBridgedBasicBlock SILFunction_lastBlock(BridgedFunction function);
161158

162159
BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global);
163-
BridgedStringRef SILGlobalVariable_debugDescription(BridgedGlobalVar global);
160+
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global);
164161

165162
OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
166163
OptionalBridgedBasicBlock SILBasicBlock_previous(BridgedBasicBlock block);
167164
BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block);
168-
BridgedStringRef SILBasicBlock_debugDescription(BridgedBasicBlock block);
165+
std::string SILBasicBlock_debugDescription(BridgedBasicBlock block);
169166
OptionalBridgedInstruction SILBasicBlock_firstInst(BridgedBasicBlock block);
170167
OptionalBridgedInstruction SILBasicBlock_lastInst(BridgedBasicBlock block);
171168
SwiftInt SILBasicBlock_getNumArguments(BridgedBasicBlock block);
@@ -179,7 +176,7 @@ BridgedValue Operand_getValue(BridgedOperand);
179176
OptionalBridgedOperand Operand_nextUse(BridgedOperand);
180177
BridgedInstruction Operand_getUser(BridgedOperand);
181178

182-
BridgedStringRef SILNode_debugDescription(BridgedNode node);
179+
std::string SILNode_debugDescription(BridgedNode node);
183180
OptionalBridgedOperand SILValue_firstUse(BridgedValue value);
184181
BridgedType SILValue_getType(BridgedValue value);
185182

@@ -227,8 +224,4 @@ BridgedInstruction SILBuilder_createBuiltinBinaryFunction(
227224
BridgedInstruction SILBuilder_createCondFail(BridgedInstruction insertionPoint,
228225
BridgedLocation loc, BridgedValue condition, BridgedStringRef messge);
229226

230-
#ifdef __cplusplus
231-
} // extern "C"
232-
#endif
233-
234227
#endif

lib/SIL/Utils/SILBridging.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "swift/SIL/SILGlobalVariable.h"
1616
#include "swift/SIL/SILBuilder.h"
1717

18+
#include <string>
19+
1820
using namespace swift;
1921

2022
namespace {
@@ -159,11 +161,12 @@ BridgedStringRef SILFunction_getName(BridgedFunction function) {
159161
return getBridgedStringRef(castToFunction(function)->getName());
160162
}
161163

162-
BridgedStringRef SILFunction_debugDescription(BridgedFunction function) {
164+
std::string SILFunction_debugDescription(BridgedFunction function) {
163165
std::string str;
164166
llvm::raw_string_ostream os(str);
165167
castToFunction(function)->print(os);
166-
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
168+
str.pop_back(); // Remove trailing newline.
169+
return str;
167170
}
168171

169172
OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function) {
@@ -207,11 +210,12 @@ BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block) {
207210
return {castToBasicBlock(block)->getParent()};
208211
}
209212

210-
BridgedStringRef SILBasicBlock_debugDescription(BridgedBasicBlock block) {
213+
std::string SILBasicBlock_debugDescription(BridgedBasicBlock block) {
211214
std::string str;
212215
llvm::raw_string_ostream os(str);
213216
castToBasicBlock(block)->print(os);
214-
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
217+
str.pop_back(); // Remove trailing newline.
218+
return str;
215219
}
216220

217221
OptionalBridgedInstruction SILBasicBlock_firstInst(BridgedBasicBlock block) {
@@ -271,11 +275,12 @@ BridgedBasicBlock SILArgument_getParent(BridgedArgument argument) {
271275
static_assert(BridgedOperandSize == sizeof(Operand),
272276
"wrong bridged Operand size");
273277

274-
BridgedStringRef SILNode_debugDescription(BridgedNode node) {
278+
std::string SILNode_debugDescription(BridgedNode node) {
275279
std::string str;
276280
llvm::raw_string_ostream os(str);
277281
castToSILNode(node)->print(os);
278-
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
282+
str.pop_back(); // Remove trailing newline.
283+
return str;
279284
}
280285

281286
static Operand *castToOperand(BridgedOperand operand) {
@@ -318,11 +323,12 @@ BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global) {
318323
return getBridgedStringRef(castToGlobal(global)->getName());
319324
}
320325

321-
BridgedStringRef SILGlobalVariable_debugDescription(BridgedGlobalVar global) {
326+
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global) {
322327
std::string str;
323328
llvm::raw_string_ostream os(str);
324329
castToGlobal(global)->print(os);
325-
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
330+
str.pop_back(); // Remove trailing newline.
331+
return str;
326332
}
327333

328334
//===----------------------------------------------------------------------===//

libswift/Sources/SIL/BasicBlock.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final public class BasicBlock : ListNode, CustomStringConvertible {
1919
public var function: Function { SILBasicBlock_getFunction(bridged).function }
2020

2121
public var description: String {
22-
SILBasicBlock_debugDescription(bridged).takeString()
22+
SILBasicBlock_debugDescription(bridged).string
2323
}
2424

2525
public var arguments: ArgumentArray { ArgumentArray(block: self) }

libswift/Sources/SIL/Function.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final public class Function : CustomStringConvertible {
1818
}
1919

2020
final public var description: String {
21-
return SILFunction_debugDescription(bridged).takeString()
21+
return SILFunction_debugDescription(bridged).string
2222
}
2323

2424
public var entryBlock: BasicBlock {

libswift/Sources/SIL/GlobalVariable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final public class GlobalVariable : CustomStringConvertible {
1818
}
1919

2020
public var description: String {
21-
return SILGlobalVariable_debugDescription(bridged).takeString()
21+
return SILGlobalVariable_debugDescription(bridged).string
2222
}
2323

2424
// TODO: initializer instructions

libswift/Sources/SIL/Instruction.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class Instruction : ListNode, CustomStringConvertible, Hashable {
3030
}
3131

3232
final public var description: String {
33-
SILNode_debugDescription(bridgedNode).takeString()
33+
SILNode_debugDescription(bridgedNode).string
3434
}
3535

3636
final public var operands: OperandArray {
@@ -132,7 +132,7 @@ public class SingleValueInstruction : Instruction, Value {
132132

133133
public final class MultipleValueInstructionResult : Value {
134134
final public var description: String {
135-
SILNode_debugDescription(bridgedNode).takeString()
135+
SILNode_debugDescription(bridgedNode).string
136136
}
137137

138138
public var definingInstruction: Instruction? {

libswift/Sources/SIL/Utils.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ extension BridgedStringRef {
8181
}
8282
}
8383

84+
extension std.__1.string {
85+
public var string: String {
86+
// TODO: remove this once a new version of Swift is released (and call
87+
// c_str() directly).
88+
var mutableSelf = self
89+
return String(cString: mutableSelf.c_str())
90+
}
91+
}
92+
8493
extension String {
8594
public func withBridgedStringRef<T>(_ c: (BridgedStringRef) -> T) -> T {
8695
var str = self

libswift/Sources/SIL/Value.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public protocol Value : AnyObject, CustomStringConvertible {
2020

2121
extension Value {
2222
public var description: String {
23-
SILNode_debugDescription(bridgedNode).takeString()
23+
SILNode_debugDescription(bridgedNode).string
2424
}
2525

2626
public var uses: UseList {

0 commit comments

Comments
 (0)