Skip to content

[SwiftCompiler] Move common bridging facilities to 'Basic' #41493

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
Feb 21, 2022
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
30 changes: 16 additions & 14 deletions SwiftCompilerSources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# See http://swift.org/LICENSE.txt for license information
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors


# Following function are needed as a workaround until it's possible to compile
# swift code with cmake's builtin swift support.

Expand All @@ -19,15 +18,9 @@ function(add_swift_compiler_module module)
cmake_parse_arguments(ALSM
""
""
"DEPENDS"
"DEPENDS;SOURCES"
${ARGN})
set(raw_sources ${ALSM_UNPARSED_ARGUMENTS})
set(sources)
foreach(raw_source ${raw_sources})
get_filename_component(
raw_source "${raw_source}" REALPATH BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
list(APPEND sources "${raw_source}")
endforeach()
set(raw_sources ${ALSM_SOURCES} ${ALSM_UNPARSED_ARGUMENTS})

set(target_name "SwiftModule${module}")

Expand All @@ -36,9 +29,10 @@ function(add_swift_compiler_module module)
# This target is mainly used to add properties, like the list of source files.
add_custom_target(
${target_name}
SOURCES ${sources}
COMMENT "swift compiler module ${module}")

swift_compiler_sources(${module} ${raw_sources})

set_property(TARGET ${target_name} PROPERTY module_name ${module})
set_property(TARGET ${target_name} PROPERTY module_depends ${ALSM_DEPENDS})

Expand All @@ -54,8 +48,13 @@ function(swift_compiler_sources module)
""
""
${ARGN})
set(sources ${LSS_UNPARSED_ARGUMENTS})
list(TRANSFORM sources PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/")
set(raw_sources ${LSS_UNPARSED_ARGUMENTS})
set(sources)
foreach(raw_source ${raw_sources})
get_filename_component(
raw_source "${raw_source}" REALPATH BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
list(APPEND sources "${raw_source}")
endforeach()

set(target_name "SwiftModule${module}")
set_property(TARGET "SwiftModule${module}" APPEND PROPERTY SOURCES ${sources})
Expand Down Expand Up @@ -146,8 +145,11 @@ function(add_swift_compiler_modules_library name)
"-emit-module-path" "${build_dir}/${module}.swiftmodule"
"-parse-as-library" ${sources}
"-wmo" ${swift_compile_options}
"-I" "${SWIFT_SOURCE_DIR}/include/swift"
"-I" "${SWIFT_SOURCE_DIR}/include"
# Bridging modules and headers.
"-Xcc" "-I" "-Xcc" "${SWIFT_SOURCE_DIR}/include"
# Generated C headers.
"-Xcc" "-I" "-Xcc" "${CMAKE_BINARY_DIR}/include"
# Generated swift modules.
"-I" "${build_dir}"
COMMENT "Building swift module ${module}")

Expand Down
10 changes: 10 additions & 0 deletions SwiftCompilerSources/Sources/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2022 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See http://swift.org/LICENSE.txt for license information
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

add_swift_compiler_module(Basic
Utils.swift)
77 changes: 77 additions & 0 deletions SwiftCompilerSources/Sources/Basic/Utils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//===--- Utils.swift - Some bridging utilities ----------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

@_exported import BasicBridging

//===----------------------------------------------------------------------===//
// Bridging Utilities
//===----------------------------------------------------------------------===//

extension BridgedStringRef {
public var string: String {
let buffer = UnsafeBufferPointer<UInt8>(start: data, count: Int(length))
return String(decoding: buffer, as: UTF8.self)
}

public func takeString() -> String {
let str = string
freeBridgedStringRef(self)
return str
}
}

extension String {
public func withBridgedStringRef<T>(_ c: (BridgedStringRef) -> T) -> T {
var str = self
return str.withUTF8 { buffer in
return c(BridgedStringRef(data: buffer.baseAddress, length: buffer.count))
}
}
}

extension Array {
public func withBridgedArrayRef<T>(_ c: (BridgedArrayRef) -> T) -> T {
return withUnsafeBytes { buf in
return c(BridgedArrayRef(data: buf.baseAddress!, numElements: count))
}
}
}

public typealias SwiftObject = UnsafeMutablePointer<BridgedSwiftObject>

extension UnsafeMutablePointer where Pointee == BridgedSwiftObject {
public init<T: AnyObject>(_ object: T) {
let ptr = Unmanaged.passUnretained(object).toOpaque()
self = ptr.bindMemory(to: BridgedSwiftObject.self, capacity: 1)
}

public func getAs<T: AnyObject>(_ objectType: T.Type) -> T {
return Unmanaged<T>.fromOpaque(self).takeUnretainedValue()
}
}

extension Optional where Wrapped == UnsafeMutablePointer<BridgedSwiftObject> {
public func getAs<T: AnyObject>(_ objectType: T.Type) -> T? {
if let pointer = self {
return Unmanaged<T>.fromOpaque(pointer).takeUnretainedValue()
}
return nil
}
}

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);
return c(buffer)
}
}
3 changes: 3 additions & 0 deletions SwiftCompilerSources/Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
# See http://swift.org/LICENSE.txt for license information
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

# NOTE: Subdirectories must be added in dependency order.

add_subdirectory(Basic)
if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
add_subdirectory(ExperimentalRegex)
endif()
Expand Down
2 changes: 1 addition & 1 deletion SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

set(dependencies)
list(APPEND dependencies SIL)
list(APPEND dependencies Basic SIL)
if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
list(APPEND dependencies ExperimentalRegex)
endif()
Expand Down
1 change: 1 addition & 0 deletions SwiftCompilerSources/Sources/SIL/Argument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

import Basic
import SILBridging

/// A basic block argument.
Expand Down
3 changes: 2 additions & 1 deletion SwiftCompilerSources/Sources/SIL/BasicBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

import Basic
import SILBridging

final public class BasicBlock : ListNode, CustomStringConvertible, HasName {
Expand Down Expand Up @@ -95,7 +96,7 @@ public struct SuccessorArray : RandomAccessCollection, FormattedLikeArray {

public subscript(_ index: Int) -> BasicBlock {
precondition(index >= 0 && index < endIndex)
let s = BridgedSuccessor(succ: succArray.data + index &* BridgedSuccessorSize);
let s = BridgedSuccessor(succ: succArray.data! + index &* BridgedSuccessorSize);
return SILSuccessor_getTargetBlock(s).block
}
}
Expand Down
1 change: 1 addition & 0 deletions SwiftCompilerSources/Sources/SIL/Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

import Basic
import SILBridging

/// A utility to create new instructions at a given insertion point.
Expand Down
35 changes: 19 additions & 16 deletions SwiftCompilerSources/Sources/SIL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

add_swift_compiler_module(SIL
ApplySite.swift
Argument.swift
BasicBlock.swift
Builder.swift
Effects.swift
Function.swift
GlobalVariable.swift
Instruction.swift
Location.swift
Operand.swift
Registration.swift
SmallProjectionPath.swift
SubstitutionMap.swift
Type.swift
Utils.swift
Value.swift)
DEPENDS
Basic
SOURCES
ApplySite.swift
Argument.swift
BasicBlock.swift
Builder.swift
Effects.swift
Function.swift
GlobalVariable.swift
Instruction.swift
Location.swift
Operand.swift
Registration.swift
SmallProjectionPath.swift
SubstitutionMap.swift
Type.swift
Utils.swift
Value.swift)

1 change: 1 addition & 0 deletions SwiftCompilerSources/Sources/SIL/Function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

import Basic
import SILBridging

final public class Function : CustomStringConvertible, HasName {
Expand Down
1 change: 1 addition & 0 deletions SwiftCompilerSources/Sources/SIL/GlobalVariable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

import Basic
import SILBridging

final public class GlobalVariable : CustomStringConvertible, HasName {
Expand Down
1 change: 1 addition & 0 deletions SwiftCompilerSources/Sources/SIL/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

import Basic
import SILBridging

//===----------------------------------------------------------------------===//
Expand Down
6 changes: 3 additions & 3 deletions SwiftCompilerSources/Sources/SIL/Operand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public struct OperandArray : RandomAccessCollection, CustomReflectable {

public subscript(_ index: Int) -> Operand {
precondition(index >= 0 && index < endIndex)
return Operand(BridgedOperand(op: opArray.data + index &* BridgedOperandSize))
return Operand(BridgedOperand(op: opArray.data! + index &* BridgedOperandSize))
}

public func getIndex(of operand: Operand) -> Int {
let idx = (operand.bridged.op - UnsafeRawPointer(opArray.data)) /
let idx = (operand.bridged.op - UnsafeRawPointer(opArray.data!)) /
BridgedOperandSize
precondition(self[idx].bridged.op == operand.bridged.op)
return idx
Expand All @@ -86,7 +86,7 @@ public struct OperandArray : RandomAccessCollection, CustomReflectable {
precondition(bounds.lowerBound >= 0)
precondition(bounds.upperBound <= endIndex)
return OperandArray(opArray: BridgedArrayRef(
data: opArray.data + bounds.lowerBound &* BridgedOperandSize,
data: opArray.data! + bounds.lowerBound &* BridgedOperandSize,
numElements: bounds.upperBound - bounds.lowerBound))
}
}
Expand Down
1 change: 1 addition & 0 deletions SwiftCompilerSources/Sources/SIL/Registration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

import Basic
import SILBridging

private func register<T: AnyObject>(_ cl: T.Type) {
Expand Down
1 change: 1 addition & 0 deletions SwiftCompilerSources/Sources/SIL/Type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

import Basic
import SILBridging

public struct Type : CustomStringConvertible, CustomReflectable {
Expand Down
52 changes: 0 additions & 52 deletions SwiftCompilerSources/Sources/SIL/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,28 +234,6 @@ public struct ParsingError : Error {
// Bridging Utilities
//===----------------------------------------------------------------------===//

extension BridgedStringRef {
public var string: String {
let buffer = UnsafeBufferPointer<UInt8>(start: data, count: Int(length))
return String(decoding: buffer, as: UTF8.self)
}

func takeString() -> String {
let str = string
freeBridgedStringRef(self)
return str
}
}

extension String {
public func withBridgedStringRef<T>(_ c: (BridgedStringRef) -> T) -> T {
var str = self
return str.withUTF8 { buffer in
return c(BridgedStringRef(data: buffer.baseAddress, length: buffer.count))
}
}
}

extension Array where Element == Value {
public func withBridgedValues<T>(_ c: (BridgedValueArray) -> T) -> T {
return self.withUnsafeBytes { valPtr in
Expand All @@ -265,33 +243,3 @@ extension Array where Element == Value {
}
}

public typealias SwiftObject = UnsafeMutablePointer<BridgedSwiftObject>

extension UnsafeMutablePointer where Pointee == BridgedSwiftObject {
init<T: AnyObject>(_ object: T) {
let ptr = Unmanaged.passUnretained(object).toOpaque()
self = ptr.bindMemory(to: BridgedSwiftObject.self, capacity: 1)
}

func getAs<T: AnyObject>(_ objectType: T.Type) -> T {
return Unmanaged<T>.fromOpaque(self).takeUnretainedValue()
}
}

extension Optional where Wrapped == UnsafeMutablePointer<BridgedSwiftObject> {
func getAs<T: AnyObject>(_ objectType: T.Type) -> T? {
if let pointer = self {
return Unmanaged<T>.fromOpaque(pointer).takeUnretainedValue()
}
return nil
}
}

extension BridgedArrayRef {
func withElements<T, R>(ofType ty: T.Type, _ c: (UnsafeBufferPointer<T>) -> R) -> R {
return data.withMemoryRebound(to: ty, capacity: numElements) { (ptr: UnsafePointer<T>) -> R in
let buffer = UnsafeBufferPointer(start: ptr, count: numElements)
return c(buffer)
}
}
}
1 change: 1 addition & 0 deletions SwiftCompilerSources/Sources/SIL/Value.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

import Basic
import SILBridging

public protocol Value : AnyObject, CustomStringConvertible {
Expand Down
Loading