@@ -19,13 +19,25 @@ import SwiftSyntaxBuilder
19
19
/// parameters and return type.
20
20
@_spi ( Testing)
21
21
public struct SwiftFunctionSignature : Equatable {
22
- // FIXME: isStaticOrClass probably shouldn't be here?
23
- var isStaticOrClass : Bool
24
- var selfParameter : SwiftParameter ?
22
+ var selfParameter : SwiftSelfParameter ?
25
23
var parameters : [ SwiftParameter ]
26
24
var result : SwiftResult
27
25
}
28
26
27
+ /// Describes the "self" parameter of a Swift function signature.
28
+ enum SwiftSelfParameter : Equatable {
29
+ /// 'self' is an instance parameter.
30
+ case instance( SwiftParameter )
31
+
32
+ /// 'self' is a metatype for a static method. We only need the type to
33
+ /// form the call.
34
+ case staticMethod( SwiftType )
35
+
36
+ /// 'self' is the type for a call to an initializer. We only need the type
37
+ /// to form the call.
38
+ case initializer( SwiftType )
39
+ }
40
+
29
41
extension SwiftFunctionSignature {
30
42
/// Create a function declaration with the given name that has this
31
43
/// signature.
@@ -49,6 +61,33 @@ extension SwiftFunctionSignature {
49
61
}
50
62
51
63
extension SwiftFunctionSignature {
64
+ init (
65
+ _ node: InitializerDeclSyntax ,
66
+ enclosingType: SwiftType ? ,
67
+ symbolTable: SwiftSymbolTable
68
+ ) throws {
69
+ guard let enclosingType else {
70
+ throw SwiftFunctionTranslationError . missingEnclosingType ( node)
71
+ }
72
+
73
+ // We do not yet support failable initializers.
74
+ if node. optionalMark != nil {
75
+ throw SwiftFunctionTranslationError . failableInitializer ( node)
76
+ }
77
+
78
+ // Prohibit generics for now.
79
+ if let generics = node. genericParameterClause {
80
+ throw SwiftFunctionTranslationError . generic ( generics)
81
+ }
82
+
83
+ self . selfParameter = . initializer( enclosingType)
84
+ self . result = SwiftResult ( convention: . direct, type: enclosingType)
85
+ self . parameters = try Self . translateFunctionSignature (
86
+ node. signature,
87
+ symbolTable: symbolTable
88
+ )
89
+ }
90
+
52
91
init (
53
92
_ node: FunctionDeclSyntax ,
54
93
enclosingType: SwiftType ? ,
@@ -59,40 +98,36 @@ extension SwiftFunctionSignature {
59
98
if let enclosingType {
60
99
var isMutating = false
61
100
var isConsuming = false
62
- var isStaticOrClass = false
101
+ var isStatic = false
63
102
for modifier in node. modifiers {
64
103
switch modifier. name. tokenKind {
65
104
case . keyword( . mutating) : isMutating = true
66
- case . keyword( . static) , . keyword ( . class ) : isStaticOrClass = true
105
+ case . keyword( . static) : isStatic = true
67
106
case . keyword( . consuming) : isConsuming = true
107
+ case . keyword( . class) : throw SwiftFunctionTranslationError . classMethod ( modifier. name)
68
108
default : break
69
109
}
70
110
}
71
111
72
- if isStaticOrClass {
73
- self . selfParameter = SwiftParameter (
74
- convention: . byValue,
75
- type: . metatype(
76
- enclosingType
77
- )
78
- )
112
+ if isStatic {
113
+ self . selfParameter = . staticMethod( enclosingType)
79
114
} else {
80
- self . selfParameter = SwiftParameter (
81
- convention: isMutating ? . inout : isConsuming ? . consuming : . byValue,
82
- type: enclosingType
115
+ self . selfParameter = . instance(
116
+ SwiftParameter (
117
+ convention: isMutating ? . inout : isConsuming ? . consuming : . byValue,
118
+ type: enclosingType
119
+ )
83
120
)
84
121
}
85
-
86
- self . isStaticOrClass = isStaticOrClass
87
122
} else {
88
123
self . selfParameter = nil
89
- self . isStaticOrClass = false
90
124
}
91
125
92
126
// Translate the parameters.
93
- self . parameters = try node. signature. parameterClause. parameters. map { param in
94
- try SwiftParameter ( param, symbolTable: symbolTable)
95
- }
127
+ self . parameters = try Self . translateFunctionSignature (
128
+ node. signature,
129
+ symbolTable: symbolTable
130
+ )
96
131
97
132
// Translate the result type.
98
133
if let resultType = node. signature. returnClause? . type {
@@ -104,17 +139,28 @@ extension SwiftFunctionSignature {
104
139
self . result = SwiftResult ( convention: . direct, type: . tuple( [ ] ) )
105
140
}
106
141
142
+ // Prohibit generics for now.
143
+ if let generics = node. genericParameterClause {
144
+ throw SwiftFunctionTranslationError . generic ( generics)
145
+ }
146
+ }
147
+
148
+ /// Translate the function signature, returning the list of translated
149
+ /// parameters.
150
+ static func translateFunctionSignature(
151
+ _ signature: FunctionSignatureSyntax ,
152
+ symbolTable: SwiftSymbolTable
153
+ ) throws -> [ SwiftParameter ] {
107
154
// FIXME: Prohibit effects for now.
108
- if let throwsClause = node . signature. effectSpecifiers? . throwsClause {
155
+ if let throwsClause = signature. effectSpecifiers? . throwsClause {
109
156
throw SwiftFunctionTranslationError . throws ( throwsClause)
110
157
}
111
- if let asyncSpecifier = node . signature. effectSpecifiers? . asyncSpecifier {
158
+ if let asyncSpecifier = signature. effectSpecifiers? . asyncSpecifier {
112
159
throw SwiftFunctionTranslationError . async ( asyncSpecifier)
113
160
}
114
161
115
- // Prohibit generics for now.
116
- if let generics = node. genericParameterClause {
117
- throw SwiftFunctionTranslationError . generic ( generics)
162
+ return try signature. parameterClause. parameters. map { param in
163
+ try SwiftParameter ( param, symbolTable: symbolTable)
118
164
}
119
165
}
120
166
}
@@ -123,4 +169,7 @@ enum SwiftFunctionTranslationError: Error {
123
169
case `throws`( ThrowsClauseSyntax )
124
170
case async ( TokenSyntax )
125
171
case generic( GenericParameterClauseSyntax )
172
+ case classMethod( TokenSyntax )
173
+ case missingEnclosingType( InitializerDeclSyntax )
174
+ case failableInitializer( InitializerDeclSyntax )
126
175
}
0 commit comments