Skip to content

[cxx-interop][libswift] Use std::string instead of BridgedStringRef 🚀 #39356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -722,10 +722,15 @@ endfunction()
# This is a temporary workaround until it's possible to compile libswift with
# cmake's builtin swift support.
function(add_libswift name)
set(libswift_compile_options
"-target" "x86_64-apple-macosx10.15" # TODO: remove this once #38675 lands.
"-Xfrontend" "-validate-tbd-against-ir=none"
"-Xfrontend" "-enable-cxx-interop")

if(CMAKE_BUILD_TYPE STREQUAL Debug)
set(libswift_compile_options "-g")
list(APPEND libswift_compile_options "-g")
else()
set(libswift_compile_options "-O" "-cross-module-optimization")
list(APPEND libswift_compile_options "-O" "-cross-module-optimization")
endif()

set(build_dir ${CMAKE_CURRENT_BINARY_DIR})
Expand Down
17 changes: 5 additions & 12 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

#include "BridgedSwiftObject.h"
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif
#include <string>

typedef struct {
const unsigned char * _Nullable data;
Expand Down Expand Up @@ -155,17 +152,17 @@ BridgedSlab PassContext_freeSlab(BridgedPassContext passContext,
BridgedSlab slab);

BridgedStringRef SILFunction_getName(BridgedFunction function);
BridgedStringRef SILFunction_debugDescription(BridgedFunction function);
std::string SILFunction_debugDescription(BridgedFunction function);
OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function);
OptionalBridgedBasicBlock SILFunction_lastBlock(BridgedFunction function);

BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global);
BridgedStringRef SILGlobalVariable_debugDescription(BridgedGlobalVar global);
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global);

OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
OptionalBridgedBasicBlock SILBasicBlock_previous(BridgedBasicBlock block);
BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block);
BridgedStringRef SILBasicBlock_debugDescription(BridgedBasicBlock block);
std::string SILBasicBlock_debugDescription(BridgedBasicBlock block);
OptionalBridgedInstruction SILBasicBlock_firstInst(BridgedBasicBlock block);
OptionalBridgedInstruction SILBasicBlock_lastInst(BridgedBasicBlock block);
SwiftInt SILBasicBlock_getNumArguments(BridgedBasicBlock block);
Expand All @@ -179,7 +176,7 @@ BridgedValue Operand_getValue(BridgedOperand);
OptionalBridgedOperand Operand_nextUse(BridgedOperand);
BridgedInstruction Operand_getUser(BridgedOperand);

BridgedStringRef SILNode_debugDescription(BridgedNode node);
std::string SILNode_debugDescription(BridgedNode node);
OptionalBridgedOperand SILValue_firstUse(BridgedValue value);
BridgedType SILValue_getType(BridgedValue value);

Expand Down Expand Up @@ -227,8 +224,4 @@ BridgedInstruction SILBuilder_createBuiltinBinaryFunction(
BridgedInstruction SILBuilder_createCondFail(BridgedInstruction insertionPoint,
BridgedLocation loc, BridgedValue condition, BridgedStringRef messge);

#ifdef __cplusplus
} // extern "C"
#endif

#endif
22 changes: 14 additions & 8 deletions lib/SIL/Utils/SILBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "swift/SIL/SILGlobalVariable.h"
#include "swift/SIL/SILBuilder.h"

#include <string>

using namespace swift;

namespace {
Expand Down Expand Up @@ -159,11 +161,12 @@ BridgedStringRef SILFunction_getName(BridgedFunction function) {
return getBridgedStringRef(castToFunction(function)->getName());
}

BridgedStringRef SILFunction_debugDescription(BridgedFunction function) {
std::string SILFunction_debugDescription(BridgedFunction function) {
std::string str;
llvm::raw_string_ostream os(str);
castToFunction(function)->print(os);
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
str.pop_back(); // Remove trailing newline.
return str;
}

OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function) {
Expand Down Expand Up @@ -207,11 +210,12 @@ BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block) {
return {castToBasicBlock(block)->getParent()};
}

BridgedStringRef SILBasicBlock_debugDescription(BridgedBasicBlock block) {
std::string SILBasicBlock_debugDescription(BridgedBasicBlock block) {
std::string str;
llvm::raw_string_ostream os(str);
castToBasicBlock(block)->print(os);
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
str.pop_back(); // Remove trailing newline.
return str;
}

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

BridgedStringRef SILNode_debugDescription(BridgedNode node) {
std::string SILNode_debugDescription(BridgedNode node) {
std::string str;
llvm::raw_string_ostream os(str);
castToSILNode(node)->print(os);
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
str.pop_back(); // Remove trailing newline.
return str;
}

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

BridgedStringRef SILGlobalVariable_debugDescription(BridgedGlobalVar global) {
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global) {
std::string str;
llvm::raw_string_ostream os(str);
castToGlobal(global)->print(os);
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
str.pop_back(); // Remove trailing newline.
return str;
}

//===----------------------------------------------------------------------===//
Expand Down
2 changes: 1 addition & 1 deletion libswift/Sources/SIL/BasicBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final public class BasicBlock : ListNode, CustomStringConvertible {
public var function: Function { SILBasicBlock_getFunction(bridged).function }

public var description: String {
SILBasicBlock_debugDescription(bridged).takeString()
SILBasicBlock_debugDescription(bridged).string
}

public var arguments: ArgumentArray { ArgumentArray(block: self) }
Expand Down
2 changes: 1 addition & 1 deletion libswift/Sources/SIL/Function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final public class Function : CustomStringConvertible {
}

final public var description: String {
return SILFunction_debugDescription(bridged).takeString()
return SILFunction_debugDescription(bridged).string
}

public var entryBlock: BasicBlock {
Expand Down
2 changes: 1 addition & 1 deletion libswift/Sources/SIL/GlobalVariable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final public class GlobalVariable : CustomStringConvertible {
}

public var description: String {
return SILGlobalVariable_debugDescription(bridged).takeString()
return SILGlobalVariable_debugDescription(bridged).string
}

// TODO: initializer instructions
Expand Down
4 changes: 2 additions & 2 deletions libswift/Sources/SIL/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Instruction : ListNode, CustomStringConvertible, Hashable {
}

final public var description: String {
SILNode_debugDescription(bridgedNode).takeString()
SILNode_debugDescription(bridgedNode).string
}

final public var operands: OperandArray {
Expand Down Expand Up @@ -132,7 +132,7 @@ public class SingleValueInstruction : Instruction, Value {

public final class MultipleValueInstructionResult : Value {
final public var description: String {
SILNode_debugDescription(bridgedNode).takeString()
SILNode_debugDescription(bridgedNode).string
}

public var definingInstruction: Instruction? {
Expand Down
9 changes: 9 additions & 0 deletions libswift/Sources/SIL/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ extension BridgedStringRef {
}
}

extension std.__1.string {
public var string: String {
// TODO: remove this once a new version of Swift is released (and call
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess Egor's patch for this didn't quite make it into 5.5 :(

// c_str() directly).
var mutableSelf = self
return String(cString: mutableSelf.c_str())
}
}

extension String {
public func withBridgedStringRef<T>(_ c: (BridgedStringRef) -> T) -> T {
var str = self
Expand Down
2 changes: 1 addition & 1 deletion libswift/Sources/SIL/Value.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public protocol Value : AnyObject, CustomStringConvertible {

extension Value {
public var description: String {
SILNode_debugDescription(bridgedNode).takeString()
SILNode_debugDescription(bridgedNode).string
}

public var uses: UseList {
Expand Down