forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwiftFunctionSignature.swift
More file actions
258 lines (227 loc) · 8.01 KB
/
SwiftFunctionSignature.swift
File metadata and controls
258 lines (227 loc) · 8.01 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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 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
import SwiftSyntaxBuilder
/// Provides a complete signature for a Swift function, which includes its
/// parameters and return type.
public struct SwiftFunctionSignature: Equatable {
var selfParameter: SwiftSelfParameter?
var parameters: [SwiftParameter]
var result: SwiftResult
init(selfParameter: SwiftSelfParameter? = nil, parameters: [SwiftParameter], result: SwiftResult) {
self.selfParameter = selfParameter
self.parameters = parameters
self.result = result
}
}
/// Describes the "self" parameter of a Swift function signature.
enum SwiftSelfParameter: Equatable {
/// 'self' is an instance parameter.
case instance(SwiftParameter)
/// 'self' is a metatype for a static method. We only need the type to
/// form the call.
case staticMethod(SwiftType)
/// 'self' is the type for a call to an initializer. We only need the type
/// to form the call.
case initializer(SwiftType)
}
extension SwiftFunctionSignature {
init(
_ node: InitializerDeclSyntax,
enclosingType: SwiftType?,
symbolTable: SwiftSymbolTable
) throws {
// Prohibit generics for now.
if let generics = node.genericParameterClause {
throw SwiftFunctionTranslationError.generic(generics)
}
guard let enclosingType else {
throw SwiftFunctionTranslationError.missingEnclosingType(node)
}
// We do not yet support failable initializers.
if node.optionalMark != nil {
throw SwiftFunctionTranslationError.failableInitializer(node)
}
// Prohibit generics for now.
if let generics = node.genericParameterClause {
throw SwiftFunctionTranslationError.generic(generics)
}
self.init(
selfParameter: .initializer(enclosingType),
parameters: try Self.translateFunctionSignature(
node.signature,
symbolTable: symbolTable
),
result: SwiftResult(convention: .direct, type: enclosingType)
)
}
init(
_ node: FunctionDeclSyntax,
enclosingType: SwiftType?,
symbolTable: SwiftSymbolTable
) throws {
// Prohibit generics for now.
if let generics = node.genericParameterClause {
throw SwiftFunctionTranslationError.generic(generics)
}
// If this is a member of a type, so we will have a self parameter. Figure out the
// type and convention for the self parameter.
let selfParameter: SwiftSelfParameter?
if let enclosingType {
var isMutating = false
var isConsuming = false
var isStatic = false
for modifier in node.modifiers {
switch modifier.name.tokenKind {
case .keyword(.mutating): isMutating = true
case .keyword(.static): isStatic = true
case .keyword(.consuming): isConsuming = true
case .keyword(.class): throw SwiftFunctionTranslationError.classMethod(modifier.name)
default: break
}
}
if isStatic {
selfParameter = .staticMethod(enclosingType)
} else {
selfParameter = .instance(
SwiftParameter(
convention: isMutating ? .inout : isConsuming ? .consuming : .byValue,
type: enclosingType
)
)
}
} else {
selfParameter = nil
}
// Translate the parameters.
let parameters = try Self.translateFunctionSignature(
node.signature,
symbolTable: symbolTable
)
// Translate the result type.
let result: SwiftResult
if let resultType = node.signature.returnClause?.type {
result = try SwiftResult(
convention: .direct,
type: SwiftType(resultType, symbolTable: symbolTable)
)
} else {
result = .void
}
self.init(selfParameter: selfParameter, parameters: parameters, result: result)
}
/// Translate the function signature, returning the list of translated
/// parameters.
static func translateFunctionSignature(
_ signature: FunctionSignatureSyntax,
symbolTable: SwiftSymbolTable
) throws -> [SwiftParameter] {
// FIXME: Prohibit effects for now.
if let throwsClause = signature.effectSpecifiers?.throwsClause {
throw SwiftFunctionTranslationError.throws(throwsClause)
}
if let asyncSpecifier = signature.effectSpecifiers?.asyncSpecifier {
throw SwiftFunctionTranslationError.async(asyncSpecifier)
}
return try signature.parameterClause.parameters.map { param in
try SwiftParameter(param, symbolTable: symbolTable)
}
}
init(_ varNode: VariableDeclSyntax, isSet: Bool, enclosingType: SwiftType?, symbolTable: SwiftSymbolTable) throws {
// If this is a member of a type, so we will have a self parameter. Figure out the
// type and convention for the self parameter.
if let enclosingType {
var isStatic = false
for modifier in varNode.modifiers {
switch modifier.name.tokenKind {
case .keyword(.static): isStatic = true
case .keyword(.class): throw SwiftFunctionTranslationError.classMethod(modifier.name)
default: break
}
}
if isStatic {
self.selfParameter = .staticMethod(enclosingType)
} else {
self.selfParameter = .instance(
SwiftParameter(
convention: isSet && !enclosingType.isReferenceType ? .inout : .byValue,
type: enclosingType
)
)
}
} else {
self.selfParameter = nil
}
guard let binding = varNode.bindings.first, varNode.bindings.count == 1 else {
throw SwiftFunctionTranslationError.multipleBindings(varNode)
}
guard let varTypeNode = binding.typeAnnotation?.type else {
throw SwiftFunctionTranslationError.missingTypeAnnotation(varNode)
}
let valueType = try SwiftType(varTypeNode, symbolTable: symbolTable)
if isSet {
self.parameters = [SwiftParameter(convention: .byValue, parameterName: "newValue", type: valueType)]
self.result = .void
} else {
self.parameters = []
self.result = .init(convention: .direct, type: valueType)
}
}
}
extension VariableDeclSyntax {
struct SupportedAccessorKinds: OptionSet {
var rawValue: UInt8
static var get: Self = .init(rawValue: 1 << 0)
static var set: Self = .init(rawValue: 1 << 1)
}
/// Determine what operations (i.e. get and/or set) supported in this `VariableDeclSyntax`
///
/// - Parameters:
/// - binding the pattern binding in this declaration.
func supportedAccessorKinds(binding: PatternBindingSyntax) -> SupportedAccessorKinds {
if self.bindingSpecifier == .keyword(.let) {
return [.get]
}
if let accessorBlock = binding.accessorBlock {
switch accessorBlock.accessors {
case .getter:
return [.get]
case .accessors(let accessors):
for accessor in accessors {
switch accessor.accessorSpecifier.tokenKind {
// Existence of any write accessor or observer implies this supports read/write.
case .keyword(.set), .keyword(._modify), .keyword(.unsafeMutableAddress),
.keyword(.willSet), .keyword(.didSet):
return [.get, .set]
default: // Ignore willSet/didSet and unknown accessors.
break
}
}
return [.get]
}
}
return [.get, .set]
}
}
enum SwiftFunctionTranslationError: Error {
case `throws`(ThrowsClauseSyntax)
case async(TokenSyntax)
case generic(GenericParameterClauseSyntax)
case classMethod(TokenSyntax)
case missingEnclosingType(InitializerDeclSyntax)
case failableInitializer(InitializerDeclSyntax)
case multipleBindings(VariableDeclSyntax)
case missingTypeAnnotation(VariableDeclSyntax)
case unsupportedAccessor(AccessorDeclSyntax)
}