Skip to content

Commit 7486cd1

Browse files
committed
[SwiftCompiler] Move common bridging facilities to 'Basic'
A preparation for AST/DiagnosticEngine bridging
1 parent 30d318a commit 7486cd1

33 files changed

+308
-162
lines changed

SwiftCompilerSources/CMakeLists.txt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

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

@@ -19,15 +18,9 @@ function(add_swift_compiler_module module)
1918
cmake_parse_arguments(ALSM
2019
""
2120
""
22-
"DEPENDS"
21+
"DEPENDS;SOURCES"
2322
${ARGN})
24-
set(raw_sources ${ALSM_UNPARSED_ARGUMENTS})
25-
set(sources)
26-
foreach(raw_source ${raw_sources})
27-
get_filename_component(
28-
raw_source "${raw_source}" REALPATH BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
29-
list(APPEND sources "${raw_source}")
30-
endforeach()
23+
set(raw_sources ${ALSM_SOURCES} ${ALSM_UNPARSED_ARGUMENTS})
3124

3225
set(target_name "SwiftModule${module}")
3326

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

34+
swift_compiler_sources(${module} ${raw_sources})
35+
4236
set_property(TARGET ${target_name} PROPERTY module_name ${module})
4337
set_property(TARGET ${target_name} PROPERTY module_depends ${ALSM_DEPENDS})
4438

@@ -54,8 +48,13 @@ function(swift_compiler_sources module)
5448
""
5549
""
5650
${ARGN})
57-
set(sources ${LSS_UNPARSED_ARGUMENTS})
58-
list(TRANSFORM sources PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/")
51+
set(raw_sources ${LSS_UNPARSED_ARGUMENTS})
52+
set(sources)
53+
foreach(raw_source ${raw_sources})
54+
get_filename_component(
55+
raw_source "${raw_source}" REALPATH BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
56+
list(APPEND sources "${raw_source}")
57+
endforeach()
5958

6059
set(target_name "SwiftModule${module}")
6160
set_property(TARGET "SwiftModule${module}" APPEND PROPERTY SOURCES ${sources})
@@ -146,8 +145,11 @@ function(add_swift_compiler_modules_library name)
146145
"-emit-module-path" "${build_dir}/${module}.swiftmodule"
147146
"-parse-as-library" ${sources}
148147
"-wmo" ${swift_compile_options}
149-
"-I" "${SWIFT_SOURCE_DIR}/include/swift"
150-
"-I" "${SWIFT_SOURCE_DIR}/include"
148+
# Bridging modules and headers.
149+
"-Xcc" "-I" "-Xcc" "${SWIFT_SOURCE_DIR}/include"
150+
# Generated C headers.
151+
"-Xcc" "-I" "-Xcc" "${CMAKE_BINARY_DIR}/include"
152+
# Generated swift modules.
151153
"-I" "${build_dir}"
152154
COMMENT "Building swift module ${module}")
153155

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2022 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
add_swift_compiler_module(Basic
10+
Utils.swift)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===--- Utils.swift - Some bridging utilities ----------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@_exported import BasicBridging
14+
15+
//===----------------------------------------------------------------------===//
16+
// Bridging Utilities
17+
//===----------------------------------------------------------------------===//
18+
19+
extension BridgedStringRef {
20+
public var string: String {
21+
let buffer = UnsafeBufferPointer<UInt8>(start: data, count: Int(length))
22+
return String(decoding: buffer, as: UTF8.self)
23+
}
24+
25+
public func takeString() -> String {
26+
let str = string
27+
freeBridgedStringRef(self)
28+
return str
29+
}
30+
}
31+
32+
extension String {
33+
public func withBridgedStringRef<T>(_ c: (BridgedStringRef) -> T) -> T {
34+
var str = self
35+
return str.withUTF8 { buffer in
36+
return c(BridgedStringRef(data: buffer.baseAddress, length: buffer.count))
37+
}
38+
}
39+
}
40+
41+
extension Array {
42+
public func withBridgedArrayRef<T>(_ c: (BridgedArrayRef) -> T) -> T {
43+
return withUnsafeBytes { buf in
44+
return c(BridgedArrayRef(data: buf.baseAddress!, numElements: count))
45+
}
46+
}
47+
}
48+
49+
public typealias SwiftObject = UnsafeMutablePointer<BridgedSwiftObject>
50+
51+
extension UnsafeMutablePointer where Pointee == BridgedSwiftObject {
52+
public init<T: AnyObject>(_ object: T) {
53+
let ptr = Unmanaged.passUnretained(object).toOpaque()
54+
self = ptr.bindMemory(to: BridgedSwiftObject.self, capacity: 1)
55+
}
56+
57+
public func getAs<T: AnyObject>(_ objectType: T.Type) -> T {
58+
return Unmanaged<T>.fromOpaque(self).takeUnretainedValue()
59+
}
60+
}
61+
62+
extension Optional where Wrapped == UnsafeMutablePointer<BridgedSwiftObject> {
63+
public func getAs<T: AnyObject>(_ objectType: T.Type) -> T? {
64+
if let pointer = self {
65+
return Unmanaged<T>.fromOpaque(pointer).takeUnretainedValue()
66+
}
67+
return nil
68+
}
69+
}
70+
71+
extension BridgedArrayRef {
72+
public func withElements<T, R>(ofType ty: T.Type, _ c: (UnsafeBufferPointer<T>) -> R) -> R {
73+
let start = data.bindMemory(to: ty, capacity: numElements);
74+
let buffer = UnsafeBufferPointer(start: start, count: numElements);
75+
return c(buffer)
76+
}
77+
}

SwiftCompilerSources/Sources/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9+
# NOTE: Subdirectories must be added in dependency order.
10+
11+
add_subdirectory(Basic)
912
if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
1013
add_subdirectory(ExperimentalRegex)
1114
endif()

SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
set(dependencies)
10-
list(APPEND dependencies SIL)
10+
list(APPEND dependencies Basic SIL)
1111
if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
1212
list(APPEND dependencies ExperimentalRegex)
1313
endif()

SwiftCompilerSources/Sources/SIL/Argument.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import Basic
1314
import SILBridging
1415

1516
/// A basic block argument.

SwiftCompilerSources/Sources/SIL/BasicBlock.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import Basic
1314
import SILBridging
1415

1516
final public class BasicBlock : ListNode, CustomStringConvertible, HasName {

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import Basic
1314
import SILBridging
1415

1516
/// A utility to create new instructions at a given insertion point.

SwiftCompilerSources/Sources/SIL/CMakeLists.txt

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
add_swift_compiler_module(SIL
10-
ApplySite.swift
11-
Argument.swift
12-
BasicBlock.swift
13-
Builder.swift
14-
Effects.swift
15-
Function.swift
16-
GlobalVariable.swift
17-
Instruction.swift
18-
Location.swift
19-
Operand.swift
20-
Registration.swift
21-
SmallProjectionPath.swift
22-
SubstitutionMap.swift
23-
Type.swift
24-
Utils.swift
25-
Value.swift)
10+
DEPENDS
11+
Basic
12+
SOURCES
13+
ApplySite.swift
14+
Argument.swift
15+
BasicBlock.swift
16+
Builder.swift
17+
Effects.swift
18+
Function.swift
19+
GlobalVariable.swift
20+
Instruction.swift
21+
Location.swift
22+
Operand.swift
23+
Registration.swift
24+
SmallProjectionPath.swift
25+
SubstitutionMap.swift
26+
Type.swift
27+
Utils.swift
28+
Value.swift)
2629

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import Basic
1314
import SILBridging
1415

1516
final public class Function : CustomStringConvertible, HasName {

0 commit comments

Comments
 (0)