forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwiftThunkTranslator.swift
More file actions
184 lines (156 loc) · 5.49 KB
/
SwiftThunkTranslator.swift
File metadata and controls
184 lines (156 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import SwiftBasicFormat
import SwiftParser
import SwiftSyntax
struct SwiftThunkTranslator {
let st: Swift2JavaTranslator
init(_ st: Swift2JavaTranslator) {
self.st = st
}
func renderGlobalThunks() -> [DeclSyntax] {
var decls: [DeclSyntax] = []
for decl in st.importedGlobalFuncs {
decls.append(contentsOf: render(forFunc: decl))
}
return decls
}
/// Render all the thunks that make Swift methods accessible to Java.
func renderThunks(forType nominal: ImportedNominalType) -> [DeclSyntax] {
var decls: [DeclSyntax] = []
decls.reserveCapacity(nominal.initializers.count + nominal.methods.count)
decls.append(renderSwiftTypeAccessor(nominal))
for decl in nominal.initializers {
decls.append(contentsOf: renderSwiftInitAccessor(decl))
}
for decl in nominal.methods {
decls.append(contentsOf: render(forFunc: decl))
}
// TODO: handle variables
// for v in nominal.variables {
// if let acc = v.accessorFunc(kind: .get) {
// decls.append(contentsOf: render(forFunc: acc))
// }
// if let acc = v.accessorFunc(kind: .set) {
// decls.append(contentsOf: render(forFunc: acc))
// }
// }
return decls
}
/// Accessor to get the `T.self` of the Swift type, without having to rely on mangled name lookups.
func renderSwiftTypeAccessor(_ nominal: ImportedNominalType) -> DeclSyntax {
let funcName = SwiftKitPrinting.Names.getType(
module: st.swiftModuleName,
nominal: nominal)
return
"""
@_cdecl("\(raw: funcName)")
public func \(raw: funcName)() -> UnsafeMutableRawPointer /* Any.Type */ {
return unsafeBitCast(\(raw: nominal.swiftNominal.qualifiedName).self, to: UnsafeMutableRawPointer.self)
}
"""
}
func renderSwiftInitAccessor(_ function: ImportedFunc) -> [DeclSyntax] {
guard let parent = function.parent else {
fatalError(
"Cannot render initializer accessor if init function has no parent! Was: \(function)")
}
let thunkName = self.st.thunkNameRegistry.functionThunkName(
module: st.swiftModuleName, decl: function)
let cDecl =
"""
@_cdecl("\(thunkName)")
"""
let typeName = "\(parent.swiftTypeName)"
return [
"""
\(raw: cDecl)
public func \(raw: thunkName)(
\(raw: st.renderSwiftParamDecls(function, paramPassingStyle: nil)),
resultBuffer: /* \(raw: typeName) */ UnsafeMutableRawPointer
) {
var _self = \(raw: typeName)(\(raw: st.renderForwardSwiftParams(function, paramPassingStyle: nil)))
resultBuffer.assumingMemoryBound(to: \(raw: typeName).self).initialize(to: _self)
}
"""
]
}
func render(forFunc decl: ImportedFunc) -> [DeclSyntax] {
st.log.trace("Rendering thunks for: \(decl.baseIdentifier)")
let thunkName = st.thunkNameRegistry.functionThunkName(module: st.swiftModuleName, decl: decl)
let returnArrowTy =
if decl.returnType.cCompatibleJavaMemoryLayout == .primitive(.void) {
"/* \(decl.returnType.swiftTypeName) */"
} else {
"-> \(decl.returnType.cCompatibleSwiftType) /* \(decl.returnType.swiftTypeName) */"
}
// Do we need to pass a self parameter?
let paramPassingStyle: SelfParameterVariant?
let callBase: String
let callBaseDot: String
if let parent = decl.parent {
paramPassingStyle = .swiftThunkSelf
callBase =
"var self$ = _self.assumingMemoryBound(to: \(parent.originalSwiftType).self).pointee"
callBaseDot = "self$."
} else {
paramPassingStyle = nil
callBase = ""
callBaseDot = ""
}
// FIXME: handle in thunk: errors
let returnStatement: String
if decl.returnType.javaType.isString {
returnStatement =
"""
let adaptedReturnValue = fatalError("Not implemented: adapting return types in Swift thunks")
return adaptedReturnValue
"""
} else {
returnStatement = "return returnValue"
}
let declParams = st.renderSwiftParamDecls(
decl,
paramPassingStyle: paramPassingStyle,
style: .cDeclThunk
)
return
[
"""
@_cdecl("\(raw: thunkName)")
public func \(raw: thunkName)(\(raw: declParams)) \(raw: returnArrowTy) {
\(raw: adaptArgumentsInThunk(decl))
\(raw: callBase)
let returnValue = \(raw: callBaseDot)\(raw: decl.baseIdentifier)(\(raw: st.renderForwardSwiftParams(decl, paramPassingStyle: paramPassingStyle)))
\(raw: returnStatement)
}
"""
]
}
func adaptArgumentsInThunk(_ decl: ImportedFunc) -> String {
var lines: [String] = []
for p in decl.parameters {
if p.type.javaType.isString {
// FIXME: is there a way we can avoid the copying here?
let adaptedType =
"""
let \(p.effectiveValueName) = String(cString: \(p.effectiveValueName))
"""
lines += [adaptedType]
}
}
return lines.joined(separator: "\n")
}
}