Skip to content

Remove CBasicBridging + CASTBridging #69440

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 17 commits into from
Oct 31, 2023
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
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,10 @@ set(SWIFT_STDLIB_MSVC_RUNTIME_LIBRARY


if(BRIDGING_MODE STREQUAL "DEFAULT" OR NOT BRIDGING_MODE)
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows" OR (CMAKE_Swift_COMPILER AND CMAKE_Swift_COMPILER_VERSION VERSION_LESS 5.8))
# In debug builds, to workaround a problem with LLDB's `po` command (rdar://115770255).
# On windows to workaround a build problem.
# If the host Swift version is less than 5.8, use pure mode to workaround a C++ interop compiler crash.
set(BRIDGING_MODE "PURE")
else()
set(BRIDGING_MODE "INLINE")
Expand Down
21 changes: 20 additions & 1 deletion SwiftCompilerSources/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
//
//===----------------------------------------------------------------------===//

// To successfully build, you'll need to create a couple of symlinks to an
// existing Ninja build:
//
// ln -s <project-root>/build/<Ninja-Build>/llvm-<os+arch> <project-root>/build/Default/llvm
// ln -s <project-root>/build/<Ninja-Build>/swift-<os+arch> <project-root>/build/Default/swift
//
// where <project-root> is the parent directory of the swift repository.
//
// FIXME: We may want to consider generating Package.swift as a part of the
// build.

import PackageDescription

private extension Target {
Expand All @@ -30,9 +41,14 @@ private extension Target {
.interoperabilityMode(.Cxx),
.unsafeFlags([
"-static",
"-Xcc", "-DCOMPILED_WITH_SWIFT", "-Xcc", "-DPURE_BRIDGING_MODE",
"-Xcc", "-UIBOutlet", "-Xcc", "-UIBAction", "-Xcc", "-UIBInspectable",
"-Xcc", "-I../include",
"-Xcc", "-I../../llvm-project/llvm/include",
"-Xcc", "-I../../llvm-project/clang/include",
"-Xcc", "-I../../build/Default/swift/include",
"-Xcc", "-I../../build/Default/llvm/include",
"-Xcc", "-I../../build/Default/llvm/tools/clang/include",
"-cross-module-optimization",
]),
] + swiftSettings)
Expand All @@ -42,7 +58,10 @@ private extension Target {
let package = Package(
name: "SwiftCompilerSources",
platforms: [
.macOS("10.9"),
// We need at least macOS 13 here to avoid hitting an availability error
// for CxxStdlib. It's only needed for the package though, the CMake build
// works fine with a lower deployment target.
.macOS(.v13),
],
products: [
.library(
Expand Down
18 changes: 9 additions & 9 deletions SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ public struct DiagnosticFixIt {
}

public struct DiagnosticEngine {
private let bridged: BridgedDiagEngine
private let bridged: BridgedDiagnosticEngine

public init(bridged: BridgedDiagEngine) {
public init(bridged: BridgedDiagnosticEngine) {
self.bridged = bridged
}
public init?(bridged: BridgedOptionalDiagnosticEngine) {
guard let object = bridged.object else {
public init?(bridged: BridgedNullableDiagnosticEngine) {
guard let raw = bridged.raw else {
return nil
}
self.bridged = BridgedDiagEngine(object: object)
self.bridged = BridgedDiagnosticEngine(raw: raw)
}

public func diagnose(_ position: SourceLoc?,
Expand Down Expand Up @@ -91,10 +91,10 @@ public struct DiagnosticEngine {
var closure: () -> Void = {
bridgedArgs.withBridgedArrayRef { bridgedArgsRef in
bridgedFixIts.withBridgedArrayRef { bridgedFixItsRef in
DiagnosticEngine_diagnose(bridged, bridgedSourceLoc,
id, bridgedArgsRef,
highlightStart, highlightLength,
bridgedFixItsRef)
bridged.diagnose(at: bridgedSourceLoc, id, bridgedArgsRef,
highlightAt: highlightStart,
highlightLength: highlightLength,
fixIts: bridgedFixItsRef)
}
}
}
Expand Down
19 changes: 4 additions & 15 deletions SwiftCompilerSources/Sources/Basic/SourceLoc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,19 @@ import ASTBridging

public struct SourceLoc {
/// Points into a source file.
let locationInFile: UnsafePointer<UInt8>

public init?(locationInFile: UnsafePointer<UInt8>?) {
guard let locationInFile = locationInFile else {
return nil
}
self.locationInFile = locationInFile
}
public let bridged: BridgedSourceLoc

public init?(bridged: BridgedSourceLoc) {
guard bridged.isValid() else {
guard bridged.isValid else {
return nil
}
self.locationInFile = bridged.uint8Pointer()!
}

public var bridged: BridgedSourceLoc {
.init(locationInFile)
self.bridged = bridged
}
}

extension SourceLoc {
public func advanced(by n: Int) -> SourceLoc {
SourceLoc(locationInFile: locationInFile.advanced(by: n))!
SourceLoc(bridged: bridged.advanced(by: n))!
}
}

Expand Down
22 changes: 11 additions & 11 deletions SwiftCompilerSources/Sources/Basic/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,23 @@ public struct StringRef : CustomStringConvertible, NoReflectionChildren {
public var description: String { string }

public var count: Int {
Int(_bridged.size())
_bridged.count
}

public subscript(index: Int) -> UInt8 {
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.uintData(), count: count)
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.data, count: count)
return buffer[index]
}

public static func ==(lhs: StringRef, rhs: StringRef) -> Bool {
let lhsBuffer = UnsafeBufferPointer<UInt8>(start: lhs._bridged.uintData(), count: lhs.count)
let rhsBuffer = UnsafeBufferPointer<UInt8>(start: rhs._bridged.uintData(), count: rhs.count)
let lhsBuffer = UnsafeBufferPointer<UInt8>(start: lhs._bridged.data, count: lhs.count)
let rhsBuffer = UnsafeBufferPointer<UInt8>(start: rhs._bridged.data, count: rhs.count)
if lhsBuffer.count != rhsBuffer.count { return false }
return lhsBuffer.elementsEqual(rhsBuffer, by: ==)
}

public static func ==(lhs: StringRef, rhs: StaticString) -> Bool {
let lhsBuffer = UnsafeBufferPointer<UInt8>(start: lhs._bridged.uintData(), count: lhs.count)
let lhsBuffer = UnsafeBufferPointer<UInt8>(start: lhs._bridged.data, count: lhs.count)
return rhs.withUTF8Buffer { (rhsBuffer: UnsafeBufferPointer<UInt8>) in
if lhsBuffer.count != rhsBuffer.count { return false }
return lhsBuffer.elementsEqual(rhsBuffer, by: ==)
Expand All @@ -116,17 +116,17 @@ extension String {
public func _withBridgedStringRef<T>(_ c: (BridgedStringRef) -> T) -> T {
var str = self
return str.withUTF8 { buffer in
return c(BridgedStringRef(buffer.baseAddress, buffer.count))
return c(BridgedStringRef(data: buffer.baseAddress, count: buffer.count))
}
}

public init(_ s: BridgedStringRef) {
let buffer = UnsafeBufferPointer<UInt8>(start: s.uintData(), count: Int(s.size()))
let buffer = UnsafeBufferPointer<UInt8>(start: s.data, count: s.count)
self.init(decoding: buffer, as: UTF8.self)
}

public init(taking s: BridgedOwnedString) {
let buffer = UnsafeBufferPointer<UInt8>(start: s.uintData(), count: s.size())
let buffer = UnsafeBufferPointer<UInt8>(start: s.data, count: s.count)
self.init(decoding: buffer, as: UTF8.self)
s.destroy()
}
Expand All @@ -135,7 +135,7 @@ extension String {
extension Array {
public func withBridgedArrayRef<T>(_ c: (BridgedArrayRef) -> T) -> T {
return withUnsafeBytes { buf in
return c(BridgedArrayRef(data: buf.baseAddress!, numElements: count))
return c(BridgedArrayRef(data: buf.baseAddress!, count: count))
}
}
}
Expand Down Expand Up @@ -164,8 +164,8 @@ extension Optional where Wrapped == UnsafeMutablePointer<BridgedSwiftObject> {

extension BridgedArrayRef {
public func withElements<T, R>(ofType ty: T.Type, _ c: (UnsafeBufferPointer<T>) -> R) -> R {
let start = data?.bindMemory(to: ty, capacity: numElements);
let buffer = UnsafeBufferPointer(start: start, count: numElements);
let start = data?.bindMemory(to: ty, capacity: count)
let buffer = UnsafeBufferPointer(start: start, count: count)
return c(buffer)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ struct FunctionPassContext : MutatingContext {

func loadFunction(name: StaticString, loadCalleesRecursively: Bool) -> Function? {
return name.withUTF8Buffer { (nameBuffer: UnsafeBufferPointer<UInt8>) in
let nameStr = BridgedStringRef(nameBuffer.baseAddress, nameBuffer.count)
let nameStr = BridgedStringRef(data: nameBuffer.baseAddress, count: nameBuffer.count)
return _bridged.loadFunction(nameStr, loadCalleesRecursively).function
}
}
Expand All @@ -244,7 +244,7 @@ struct FunctionPassContext : MutatingContext {
/// Returns nil if no such function or multiple matching functions are found.
func lookupStdlibFunction(name: StaticString) -> Function? {
return name.withUTF8Buffer { (nameBuffer: UnsafeBufferPointer<UInt8>) in
let nameStr = BridgedStringRef(nameBuffer.baseAddress, nameBuffer.count)
let nameStr = BridgedStringRef(data: nameBuffer.baseAddress, count: nameBuffer.count)
return _bridged.lookupStdlibFunction(nameStr).function
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ protocol EscapeVisitorWithResult : EscapeVisitor {
var result: Result { get }
}

private struct DefaultVisitor : EscapeVisitor {}
// FIXME: This ought to be marked private, but that triggers a compiler bug
// in debug builds (rdar://117413192)
struct DefaultVisitor : EscapeVisitor {}

struct EscapeUtilityTypes {

Expand Down
7 changes: 3 additions & 4 deletions SwiftCompilerSources/Sources/Parse/Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private func _RegexLiteralLexingFn(
_ curPtrPtr: UnsafeMutablePointer<UnsafePointer<CChar>>,
_ bufferEndPtr: UnsafePointer<CChar>,
_ mustBeRegex: CBool,
_ bridgedDiagnosticEngine: BridgedOptionalDiagnosticEngine
_ bridgedDiagnosticEngine: BridgedNullableDiagnosticEngine
) -> /*CompletelyErroneous*/ CBool {
let inputPtr = curPtrPtr.pointee

Expand All @@ -63,8 +63,7 @@ private func _RegexLiteralLexingFn(
if let error = error {
// Emit diagnostic if diagnostics are enabled.
if let diagEngine = DiagnosticEngine(bridged: bridgedDiagnosticEngine) {
let startLoc = SourceLoc(
locationInFile: error.location.assumingMemoryBound(to: UInt8.self))!
let startLoc = SourceLoc(bridged: BridgedSourceLoc(raw: error.location))!
diagEngine.diagnose(startLoc, .foreign_diagnostic, error.message)
}
return error.completelyErroneous
Expand Down Expand Up @@ -93,7 +92,7 @@ public func _RegexLiteralParsingFn(
_ captureStructureOut: UnsafeMutableRawPointer,
_ captureStructureSize: CUnsignedInt,
_ bridgedDiagnosticBaseLoc: BridgedSourceLoc,
_ bridgedDiagnosticEngine: BridgedDiagEngine
_ bridgedDiagnosticEngine: BridgedDiagnosticEngine
) -> Bool {
let str = String(cString: inputPtr)
let captureBuffer = UnsafeMutableRawBufferPointer(
Expand Down
2 changes: 1 addition & 1 deletion SwiftCompilerSources/Sources/SIL/Function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash

public func hasSemanticsAttribute(_ attr: StaticString) -> Bool {
attr.withUTF8Buffer { (buffer: UnsafeBufferPointer<UInt8>) in
bridged.hasSemanticsAttr(BridgedStringRef(buffer.baseAddress!, buffer.count))
bridged.hasSemanticsAttr(BridgedStringRef(data: buffer.baseAddress!, count: buffer.count))
}
}

Expand Down
6 changes: 3 additions & 3 deletions SwiftCompilerSources/Sources/SIL/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ final public class FixLifetimeInst : Instruction, UnaryInstruction {}
public struct VarDecl {
var bridged: BridgedVarDecl

public init?(bridged: OptionalBridgedVarDecl) {
guard let decl = bridged.decl else { return nil }
self.bridged = BridgedVarDecl(decl: decl)
public init?(bridged: BridgedNullableVarDecl) {
guard let decl = bridged.raw else { return nil }
self.bridged = BridgedVarDecl(raw: decl)
}

public var userFacingName: String { String(bridged.getUserFacingName()) }
Expand Down
4 changes: 2 additions & 2 deletions SwiftCompilerSources/Sources/SIL/Type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ public struct NominalTypeDecl : Equatable, Hashable {
public var name: StringRef { StringRef(bridged: bridged.getName()) }

public static func ==(lhs: NominalTypeDecl, rhs: NominalTypeDecl) -> Bool {
lhs.bridged.decl == rhs.bridged.decl
lhs.bridged.raw == rhs.bridged.raw
}

public func hash(into hasher: inout Hasher) {
hasher.combine(bridged.decl)
hasher.combine(bridged.raw)
}

public var isStructWithUnreferenceableStorage: Bool {
Expand Down
5 changes: 5 additions & 0 deletions cmake/modules/AddPureSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ function(_add_host_swift_compile_options name)
target_link_options(${name} PRIVATE ${_cov_flags})
endif()

if("${BRIDGING_MODE}" STREQUAL "PURE")
target_compile_options(${name} PRIVATE
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -DPURE_BRIDGING_MODE>")
endif()

# The compat56 library is not available in current toolchains. The stage-0
# compiler will build fine since the builder compiler is not aware of the 56
# compat library, but the stage-1 and subsequent stage compilers will fail as
Expand Down
8 changes: 0 additions & 8 deletions include/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@ module BasicBridging {
export *
}

module CBasicBridging {
header "swift/Basic/CBasicBridging.h"
}

module ASTBridging {
header "swift/AST/ASTBridging.h"
requires cplusplus
export *
}

module CASTBridging {
header "swift/AST/CASTBridging.h"
}

module SILBridging {
header "swift/SIL/SILBridging.h"
requires cplusplus
Expand Down
Loading