@@ -38,7 +38,7 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
38
38
fatalError ( " Use other overload. " )
39
39
}
40
40
41
- public func visitAny( _ node: Syntax ) -> ResultType {
41
+ public func visitAny( _ node: Syntax ) -> UnsafeMutableRawPointer {
42
42
fatalError ( " Not implemented. " )
43
43
}
44
44
@@ -60,254 +60,6 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
60
60
61
61
return out
62
62
}
63
-
64
- public func visit( _ node: MemberDeclListItemSyntax ) -> UnsafeMutableRawPointer {
65
- visit ( Syntax ( node. decl) )
66
- }
67
-
68
- public func visit( _ node: StructDeclSyntax ) -> UnsafeMutableRawPointer {
69
- let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
70
- var nameText = node. identifier. text
71
- let name = nameText. withUTF8 { buf in
72
- return SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
73
- }
74
-
75
- let out = StructDecl_create ( ctx, loc, name, loc, declContext)
76
- let oldDeclContext = declContext
77
- declContext = out. declContext
78
- defer { declContext = oldDeclContext }
79
-
80
- node. members. members. map ( self . visit) . withBridgedArrayRef { ref in
81
- NominalTypeDecl_setMembers ( out. nominalDecl, ref)
82
- }
83
-
84
- return out. decl
85
- }
86
-
87
- public func visit( _ node: ClassDeclSyntax ) -> UnsafeMutableRawPointer {
88
- let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
89
- var nameText = node. identifier. text
90
- let name = nameText. withUTF8 { buf in
91
- return SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
92
- }
93
-
94
- let out = ClassDecl_create ( ctx, loc, name, loc, declContext)
95
- let oldDeclContext = declContext
96
- declContext = out. declContext
97
- defer { declContext = oldDeclContext }
98
-
99
- node. members. members. map ( self . visit) . withBridgedArrayRef { ref in
100
- NominalTypeDecl_setMembers ( out. nominalDecl, ref)
101
- }
102
-
103
- return out. decl
104
- }
105
-
106
- public func visit( _ node: ClosureExprSyntax ) -> UnsafeMutableRawPointer {
107
- let statements = node. statements. map ( self . visit)
108
- let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
109
-
110
- let body = statements. withBridgedArrayRef { ref in
111
- BraceStmt_create ( ctx, loc, ref, loc)
112
- }
113
-
114
- return ClosureExpr_create ( ctx, body, declContext)
115
- }
116
-
117
- public func visit( _ node: FunctionCallExprSyntax ) -> UnsafeMutableRawPointer {
118
- // Transform the trailing closure into an argument.
119
- if let trailingClosure = node. trailingClosure {
120
- let tupleElement = TupleExprElementSyntax ( label: nil , colon: nil , expression: ExprSyntax ( trailingClosure) , trailingComma: nil )
121
-
122
- return visit ( node. addArgument ( tupleElement) . withTrailingClosure ( nil ) )
123
- }
124
-
125
- let args = visit ( node. argumentList)
126
- // TODO: hack
127
- let callee = visit ( node. calledExpression)
128
-
129
- return SwiftFunctionCallExpr_create ( self . ctx, callee, args)
130
- }
131
-
132
- public func visit( _ node: IdentifierExprSyntax ) -> UnsafeMutableRawPointer {
133
- let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
134
-
135
- var text = node. identifier. text
136
- let id = text. withUTF8 { buf in
137
- return SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
138
- }
139
-
140
- return SwiftIdentifierExpr_create ( ctx, id, loc)
141
- }
142
-
143
- public func visit( _ node: SimpleTypeIdentifierSyntax ) -> UnsafeMutableRawPointer {
144
- let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
145
-
146
- var text = node. name. text
147
- let id = text. withUTF8 { buf in
148
- return SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
149
- }
150
-
151
- return SimpleIdentTypeRepr_create ( ctx, loc, id)
152
- }
153
-
154
- public func visit( _ node: IdentifierPatternSyntax ) -> UnsafeMutableRawPointer {
155
- let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
156
-
157
- var text = node. identifier. text
158
- let id = text. withUTF8 { buf in
159
- return SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
160
- }
161
-
162
- return SwiftIdentifierExpr_create ( ctx, id, loc)
163
- }
164
-
165
- public func visit( _ node: MemberAccessExprSyntax ) -> UnsafeMutableRawPointer {
166
- let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
167
- let base = visit ( node. base!)
168
- var nameText = node. name. text
169
- let name = nameText. withUTF8 { buf in
170
- return SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
171
- }
172
-
173
- return UnresolvedDotExpr_create ( ctx, base, loc, name, loc)
174
- }
175
-
176
- public func visit( _ node: TupleExprElementSyntax ) -> UnsafeMutableRawPointer {
177
- visit ( node. expression)
178
- }
179
-
180
- public func visit( _ node: TupleExprElementListSyntax ) -> UnsafeMutableRawPointer {
181
- let elements = node. map ( self . visit)
182
-
183
- // TODO: find correct paren locs.
184
- let lParenLoc = self . base. advanced ( by: node. position. utf8Offset) . raw
185
- let rParenLoc = self . base. advanced ( by: node. position. utf8Offset) . raw
186
-
187
- return elements. withBridgedArrayRef { elementsRef in
188
- SwiftTupleExpr_create ( self . ctx, lParenLoc, elementsRef, rParenLoc)
189
- }
190
- }
191
-
192
- public func visit( _ node: InitializerClauseSyntax ) -> UnsafeMutableRawPointer {
193
- visit ( node. value)
194
- }
195
-
196
- public func visit( _ node: VariableDeclSyntax ) -> UnsafeMutableRawPointer {
197
- let pattern = visit ( node. bindings. first!. pattern)
198
- let initializer = visit ( node. bindings. first!. initializer!)
199
-
200
- let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
201
- let isStateic = false // TODO: compute this
202
- let isLet = node. letOrVarKeyword. tokenKind == . letKeyword
203
-
204
- // TODO: don't drop "initializer" on the floor.
205
- return SwiftVarDecl_create ( ctx, pattern, loc, isStateic, isLet, declContext)
206
- }
207
-
208
- public func visit( _ node: ConditionElementSyntax ) -> UnsafeMutableRawPointer {
209
- visit ( node. condition)
210
- }
211
-
212
- public func visit( _ node: CodeBlockItemSyntax ) -> UnsafeMutableRawPointer {
213
- visit ( node. item)
214
- }
215
-
216
- public func visit( _ node: CodeBlockSyntax ) -> UnsafeMutableRawPointer {
217
- let statements = node. statements. map ( self . visit)
218
- let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
219
-
220
- return statements. withBridgedArrayRef { ref in
221
- BraceStmt_create ( ctx, loc, ref, loc)
222
- }
223
- }
224
-
225
- public func visit( _ node: FunctionParameterSyntax ) -> UnsafeMutableRawPointer {
226
- let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
227
-
228
- let firstName : UnsafeMutableRawPointer ?
229
- let secondName : UnsafeMutableRawPointer ?
230
-
231
- if let nodeFirstName = node. firstName {
232
- var text = nodeFirstName. text
233
- firstName = text. withUTF8 { buf in
234
- SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count) . raw
235
- }
236
- } else {
237
- firstName = nil
238
- }
239
-
240
- if let nodeSecondName = node. secondName {
241
- var text = nodeSecondName. text
242
- secondName = text. withUTF8 { buf in
243
- SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count) . raw
244
- }
245
- } else {
246
- secondName = nil
247
- }
248
-
249
- return ParamDecl_create ( ctx, loc, loc, firstName, loc, secondName, declContext)
250
- }
251
-
252
- public func visit( _ node: FunctionDeclSyntax ) -> UnsafeMutableRawPointer {
253
- let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
254
-
255
- var nameText = node. identifier. text
256
- let name = nameText. withUTF8 { buf in
257
- return SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
258
- }
259
-
260
- let body : UnsafeMutableRawPointer ?
261
- if let nodeBody = node. body {
262
- body = visit ( nodeBody)
263
- } else {
264
- body = nil
265
- }
266
-
267
- let returnType : UnsafeMutableRawPointer ?
268
- if let output = node. signature. output {
269
- returnType = visit ( output. returnType)
270
- } else {
271
- returnType = nil
272
- }
273
-
274
- let params = node. signature. input. parameterList. map { visit ( $0) }
275
- return params. withBridgedArrayRef { ref in
276
- FuncDecl_create ( ctx, loc, false , loc, name, loc, false , nil , false , nil , loc, ref, loc, body, returnType, declContext)
277
- }
278
- }
279
-
280
- public func visit( _ node: IfStmtSyntax ) -> UnsafeMutableRawPointer {
281
- let conditions = node. conditions. map ( self . visit)
282
- assert ( conditions. count == 1 ) // TODO: handle multiple conditions.
283
-
284
- let body = visit ( node. body)
285
- let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
286
-
287
- if let elseBody = node. elseBody, node. elseKeyword != nil {
288
- return IfStmt_create ( ctx, loc, conditions. first!, body, loc, visit ( elseBody) )
289
- }
290
-
291
- return IfStmt_create ( ctx, loc, conditions. first!, body, nil , nil )
292
- }
293
-
294
- public func visit( _ node: StringLiteralExprSyntax ) -> UnsafeMutableRawPointer {
295
- let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
296
- var segment = node. segments. first!. as ( StringSegmentSyntax . self) !. content. text
297
- return segment. withUTF8 { buf in
298
- let id = SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
299
- return SwiftStringLiteralExpr_create ( ctx, id, buf. count, loc)
300
- }
301
- }
302
-
303
- public func visit( _ node: IntegerLiteralExprSyntax ) -> UnsafeMutableRawPointer {
304
- let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
305
- var segment = node. digits. text
306
- return segment. withUTF8 { buf in
307
- let id = SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
308
- return SwiftIntegerLiteralExpr_create ( ctx, id, buf. count, loc)
309
- }
310
- }
311
63
}
312
64
313
65
@_cdecl ( " parseTopLevelSwift " )
0 commit comments