forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwift2JavaVisitor.swift
More file actions
268 lines (221 loc) · 8.17 KB
/
Swift2JavaVisitor.swift
File metadata and controls
268 lines (221 loc) · 8.17 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//===----------------------------------------------------------------------===//
//
// 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 SwiftParser
import SwiftSyntax
final class Swift2JavaVisitor: SyntaxVisitor {
let translator: Swift2JavaTranslator
/// The Swift module we're visiting declarations in
let moduleName: String
/// The target java package we are going to generate types into eventually,
/// store this along with type names as we import them.
let targetJavaPackage: String
/// Type context stack associated with the syntax.
var typeContext: [(syntaxID: Syntax.ID, type: ImportedNominalType)] = []
/// Innermost type context.
var currentType: ImportedNominalType? { typeContext.last?.type }
var currentSwiftType: SwiftType? {
guard let currentType else { return nil }
return .nominal(SwiftNominalType(nominalTypeDecl: currentType.swiftNominal))
}
/// The current type name as a nested name like A.B.C.
var currentTypeName: String? { self.currentType?.swiftNominal.qualifiedName }
var log: Logger { translator.log }
init(moduleName: String, targetJavaPackage: String, translator: Swift2JavaTranslator) {
self.moduleName = moduleName
self.targetJavaPackage = targetJavaPackage
self.translator = translator
super.init(viewMode: .all)
}
/// Push specified type to the type context associated with the syntax.
func pushTypeContext(syntax: some SyntaxProtocol, importedNominal: ImportedNominalType) {
typeContext.append((syntax.id, importedNominal))
}
/// Pop type context if the current context is associated with the syntax.
func popTypeContext(syntax: some SyntaxProtocol) -> Bool {
if typeContext.last?.syntaxID == syntax.id {
typeContext.removeLast()
return true
} else {
return false
}
}
override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
log.debug("Visit \(node.kind): '\(node.qualifiedNameForDebug)'")
guard let importedNominalType = translator.importedNominalType(node, parent: self.currentType) else {
return .skipChildren
}
self.pushTypeContext(syntax: node, importedNominal: importedNominalType)
return .visitChildren
}
override func visitPost(_ node: ClassDeclSyntax) {
if self.popTypeContext(syntax: node) {
log.debug("Completed import: \(node.kind) \(node.name)")
}
}
override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind {
log.debug("Visit \(node.kind): \(node.qualifiedNameForDebug)")
guard let importedNominalType = translator.importedNominalType(node, parent: self.currentType) else {
return .skipChildren
}
self.pushTypeContext(syntax: node, importedNominal: importedNominalType)
return .visitChildren
}
override func visitPost(_ node: StructDeclSyntax) {
if self.popTypeContext(syntax: node) {
log.debug("Completed import: \(node.kind) \(node.qualifiedNameForDebug)")
}
}
override func visit(_ node: ExtensionDeclSyntax) -> SyntaxVisitorContinueKind {
// Resolve the extended type of the extension as an imported nominal, and
// recurse if we found it.
guard let importedNominalType = translator.importedNominalType(node.extendedType) else {
return .skipChildren
}
self.pushTypeContext(syntax: node, importedNominal: importedNominalType)
return .visitChildren
}
override func visitPost(_ node: ExtensionDeclSyntax) {
if self.popTypeContext(syntax: node) {
log.debug("Completed import: \(node.kind) \(node.qualifiedNameForDebug)")
}
}
override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
guard node.shouldImport(log: log) else {
return .skipChildren
}
self.log.debug("Import function: '\(node.qualifiedNameForDebug)'")
let signature: SwiftFunctionSignature
do {
signature = try SwiftFunctionSignature(
node,
enclosingType: self.currentSwiftType,
symbolTable: self.translator.symbolTable
)
} catch {
self.log.debug("Failed to import: '\(node.qualifiedNameForDebug)'; \(error)")
return .skipChildren
}
let imported = ImportedFunc(
module: translator.swiftModuleName,
swiftDecl: node,
name: node.name.text,
apiKind: .function,
functionSignature: signature
)
log.debug("Record imported method \(node.qualifiedNameForDebug)")
if let currentType {
currentType.methods.append(imported)
} else {
translator.importedGlobalFuncs.append(imported)
}
return .skipChildren
}
override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {
guard node.shouldImport(log: log) else {
return .skipChildren
}
guard let binding = node.bindings.first else {
return .skipChildren
}
let varName = "\(binding.pattern.trimmed)"
self.log.debug("Import variable: \(node.kind) '\(node.qualifiedNameForDebug)'")
func importAccessor(kind: SwiftAPIKind) throws {
let signature = try SwiftFunctionSignature(
node,
isSet: kind == .setter,
enclosingType: self.currentSwiftType,
symbolTable: self.translator.symbolTable
)
let imported = ImportedFunc(
module: translator.swiftModuleName,
swiftDecl: node,
name: varName,
apiKind: kind,
functionSignature: signature
)
log.debug("Record imported variable accessor \(kind == .getter ? "getter" : "setter"):\(node.qualifiedNameForDebug)")
if let currentType {
currentType.variables.append(imported)
} else {
translator.importedGlobalVariables.append(imported)
}
}
do {
let supportedAccessors = node.supportedAccessorKinds(binding: binding)
if supportedAccessors.contains(.get) {
try importAccessor(kind: .getter)
}
if supportedAccessors.contains(.set) {
try importAccessor(kind: .setter)
}
} catch {
self.log.debug("Failed to import: \(node.qualifiedNameForDebug); \(error)")
return .skipChildren
}
return .skipChildren
}
override func visit(_ node: InitializerDeclSyntax) -> SyntaxVisitorContinueKind {
guard let currentType else {
fatalError("Initializer must be within a current type, was: \(node)")
}
guard node.shouldImport(log: log) else {
return .skipChildren
}
self.log.debug("Import initializer: \(node.kind) '\(node.qualifiedNameForDebug)'")
let signature: SwiftFunctionSignature
do {
signature = try SwiftFunctionSignature(
node,
enclosingType: self.currentSwiftType,
symbolTable: self.translator.symbolTable
)
} catch {
self.log.debug("Failed to import: \(node.qualifiedNameForDebug); \(error)")
return .skipChildren
}
let imported = ImportedFunc(
module: translator.swiftModuleName,
swiftDecl: node,
name: "init",
apiKind: .initializer,
functionSignature: signature
)
currentType.initializers.append(imported)
return .skipChildren
}
override func visit(_ node: DeinitializerDeclSyntax) -> SyntaxVisitorContinueKind {
return .skipChildren
}
}
extension DeclSyntaxProtocol where Self: WithModifiersSyntax & WithAttributesSyntax {
func shouldImport(log: Logger) -> Bool {
guard accessControlModifiers.contains(where: { $0.isPublic }) else {
log.trace("Skip import '\(self.qualifiedNameForDebug)': not public")
return false
}
guard !attributes.contains(where: { $0.isJava }) else {
log.trace("Skip import '\(self.qualifiedNameForDebug)': is Java")
return false
}
if let node = self.as(InitializerDeclSyntax.self) {
let isFailable = node.optionalMark != nil
if isFailable {
log.warning("Skip import '\(self.qualifiedNameForDebug)': failable initializer")
return false
}
}
return true
}
}