forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportedDecls.swift
More file actions
135 lines (113 loc) · 3.45 KB
/
ImportedDecls.swift
File metadata and controls
135 lines (113 loc) · 3.45 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
//===----------------------------------------------------------------------===//
//
// 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 SwiftSyntax
/// Any imported (Swift) declaration
protocol ImportedDecl: AnyObject {}
package enum SwiftAPIKind {
case function
case initializer
case getter
case setter
}
/// Describes a Swift nominal type (e.g., a class, struct, enum) that has been
/// imported and is being translated into Java.
package class ImportedNominalType: ImportedDecl {
let swiftNominal: SwiftNominalTypeDeclaration
package var initializers: [ImportedFunc] = []
package var methods: [ImportedFunc] = []
package var variables: [ImportedFunc] = []
init(swiftNominal: SwiftNominalTypeDeclaration) {
self.swiftNominal = swiftNominal
}
var javaClassName: String {
swiftNominal.name
}
}
public final class ImportedFunc: ImportedDecl, CustomStringConvertible {
/// Swift module name (e.g. the target name where a type or function was declared)
public var module: String
/// The function name.
/// e.g., "init" for an initializer or "foo" for "foo(a:b:)".
public var name: String
public var swiftDecl: any DeclSyntaxProtocol
package var apiKind: SwiftAPIKind
var functionSignature: SwiftFunctionSignature
public var signatureString: String {
self.swiftDecl.signatureString
}
var parentType: SwiftType? {
guard let selfParameter = functionSignature.selfParameter else {
return nil
}
switch selfParameter {
case .instance(let parameter):
return parameter.type
case .staticMethod(let type):
return type
case .initializer(let type):
return type
}
}
/// If this function/method is member of a class/struct/protocol,
/// this will contain that declaration's imported name.
///
/// This is necessary when rendering accessor Java code we need the type that "self" is expecting to have.
public var hasParent: Bool { functionSignature.selfParameter != nil }
/// A display name to use to refer to the Swift declaration with its
/// enclosing type, if there is one.
public var displayName: String {
let prefix = switch self.apiKind {
case .getter: "getter:"
case .setter: "setter:"
case .function, .initializer: ""
}
let context = if let parentType {
"\(parentType)."
} else {
""
}
return prefix + context + self.name
}
init(
module: String,
swiftDecl: any DeclSyntaxProtocol,
name: String,
apiKind: SwiftAPIKind,
functionSignature: SwiftFunctionSignature
) {
self.module = module
self.name = name
self.swiftDecl = swiftDecl
self.apiKind = apiKind
self.functionSignature = functionSignature
}
public var description: String {
"""
ImportedFunc {
apiKind: \(apiKind)
module: \(module)
name: \(name)
signature: \(self.swiftDecl.signatureString)
}
"""
}
}
extension ImportedFunc: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
public static func == (lhs: ImportedFunc, rhs: ImportedFunc) -> Bool {
return lhs === rhs
}
}