diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index ade4c55deda8a..3c000b25c20c5 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -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}) diff --git a/include/swift/SIL/SILBridging.h b/include/swift/SIL/SILBridging.h index 6d02c63d57f57..d0b9aeec633ce 100644 --- a/include/swift/SIL/SILBridging.h +++ b/include/swift/SIL/SILBridging.h @@ -15,10 +15,7 @@ #include "BridgedSwiftObject.h" #include - -#ifdef __cplusplus -extern "C" { -#endif +#include typedef struct { const unsigned char * _Nullable data; @@ -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); @@ -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); @@ -227,8 +224,4 @@ BridgedInstruction SILBuilder_createBuiltinBinaryFunction( BridgedInstruction SILBuilder_createCondFail(BridgedInstruction insertionPoint, BridgedLocation loc, BridgedValue condition, BridgedStringRef messge); -#ifdef __cplusplus -} // extern "C" -#endif - #endif diff --git a/lib/SIL/Utils/SILBridging.cpp b/lib/SIL/Utils/SILBridging.cpp index 42261c507b2e6..d15f3f5891705 100644 --- a/lib/SIL/Utils/SILBridging.cpp +++ b/lib/SIL/Utils/SILBridging.cpp @@ -15,6 +15,8 @@ #include "swift/SIL/SILGlobalVariable.h" #include "swift/SIL/SILBuilder.h" +#include + using namespace swift; namespace { @@ -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) { @@ -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) { @@ -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) { @@ -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; } //===----------------------------------------------------------------------===// diff --git a/libswift/Sources/SIL/BasicBlock.swift b/libswift/Sources/SIL/BasicBlock.swift index 4e276a0f0d8c0..1190ee46e8494 100644 --- a/libswift/Sources/SIL/BasicBlock.swift +++ b/libswift/Sources/SIL/BasicBlock.swift @@ -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) } diff --git a/libswift/Sources/SIL/Function.swift b/libswift/Sources/SIL/Function.swift index f39f3719b47fa..0f6b615bb6999 100644 --- a/libswift/Sources/SIL/Function.swift +++ b/libswift/Sources/SIL/Function.swift @@ -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 { diff --git a/libswift/Sources/SIL/GlobalVariable.swift b/libswift/Sources/SIL/GlobalVariable.swift index 57124be60da75..d2e2115431add 100644 --- a/libswift/Sources/SIL/GlobalVariable.swift +++ b/libswift/Sources/SIL/GlobalVariable.swift @@ -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 diff --git a/libswift/Sources/SIL/Instruction.swift b/libswift/Sources/SIL/Instruction.swift index 9107748b295fc..2bbcfaf132015 100644 --- a/libswift/Sources/SIL/Instruction.swift +++ b/libswift/Sources/SIL/Instruction.swift @@ -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 { @@ -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? { diff --git a/libswift/Sources/SIL/Utils.swift b/libswift/Sources/SIL/Utils.swift index 6444c182e2c0c..9b3b7a0e358e2 100644 --- a/libswift/Sources/SIL/Utils.swift +++ b/libswift/Sources/SIL/Utils.swift @@ -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 + // c_str() directly). + var mutableSelf = self + return String(cString: mutableSelf.c_str()) + } +} + extension String { public func withBridgedStringRef(_ c: (BridgedStringRef) -> T) -> T { var str = self diff --git a/libswift/Sources/SIL/Value.swift b/libswift/Sources/SIL/Value.swift index 097bbfb58c7d1..d8e2658b9fe13 100644 --- a/libswift/Sources/SIL/Value.swift +++ b/libswift/Sources/SIL/Value.swift @@ -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 {