Skip to content

Commit 629fb68

Browse files
committed
[nfc] Split ASTGen into files + extensions.
1 parent 1b1944e commit 629fb68

File tree

8 files changed

+285
-250
lines changed

8 files changed

+285
-250
lines changed

lib/ASTGen/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ set(CMAKE_Swift_ARCHIVE_FINISH "")
44

55
if (SWIFT_SWIFT_PARSER)
66
add_library(ASTGen
7-
Sources/ASTGen/ASTGen.swift)
7+
Sources/ASTGen/ASTGen.swift
8+
Sources/ASTGen/Decls.swift
9+
Sources/ASTGen/Exprs.swift
10+
Sources/ASTGen/Literals.swift
11+
Sources/ASTGen/Misc.swift
12+
Sources/ASTGen/Stmts.swift)
813

914
target_link_libraries(ASTGen PUBLIC
1015
SwiftSyntax

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 1 addition & 249 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
3838
fatalError("Use other overload.")
3939
}
4040

41-
public func visitAny(_ node: Syntax) -> ResultType {
41+
public func visitAny(_ node: Syntax) -> UnsafeMutableRawPointer {
4242
fatalError("Not implemented.")
4343
}
4444

@@ -60,254 +60,6 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
6060

6161
return out
6262
}
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-
}
31163
}
31264

31365
@_cdecl("parseTopLevelSwift")

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import SwiftParser
2+
import SwiftSyntax
3+
4+
import CASTBridging
5+
6+
extension ASTGenVisitor {
7+
public func visit(_ node: StructDeclSyntax) -> UnsafeMutableRawPointer {
8+
let loc = self.base.advanced(by: node.position.utf8Offset).raw
9+
var nameText = node.identifier.text
10+
let name = nameText.withUTF8 { buf in
11+
return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
12+
}
13+
14+
let out = StructDecl_create(ctx, loc, name, loc, declContext)
15+
let oldDeclContext = declContext
16+
declContext = out.declContext
17+
defer { declContext = oldDeclContext }
18+
19+
node.members.members.map(self.visit).withBridgedArrayRef { ref in
20+
NominalTypeDecl_setMembers(out.nominalDecl, ref)
21+
}
22+
23+
return out.decl
24+
}
25+
26+
public func visit(_ node: ClassDeclSyntax) -> UnsafeMutableRawPointer {
27+
let loc = self.base.advanced(by: node.position.utf8Offset).raw
28+
var nameText = node.identifier.text
29+
let name = nameText.withUTF8 { buf in
30+
return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
31+
}
32+
33+
let out = ClassDecl_create(ctx, loc, name, loc, declContext)
34+
let oldDeclContext = declContext
35+
declContext = out.declContext
36+
defer { declContext = oldDeclContext }
37+
38+
node.members.members.map(self.visit).withBridgedArrayRef { ref in
39+
NominalTypeDecl_setMembers(out.nominalDecl, ref)
40+
}
41+
42+
return out.decl
43+
}
44+
45+
public func visit(_ node: VariableDeclSyntax) -> UnsafeMutableRawPointer {
46+
let pattern = visit(node.bindings.first!.pattern)
47+
let initializer = visit(node.bindings.first!.initializer!)
48+
49+
let loc = self.base.advanced(by: node.position.utf8Offset).raw
50+
let isStateic = false // TODO: compute this
51+
let isLet = node.letOrVarKeyword.tokenKind == .letKeyword
52+
53+
// TODO: don't drop "initializer" on the floor.
54+
return SwiftVarDecl_create(ctx, pattern, loc, isStateic, isLet, declContext)
55+
}
56+
57+
public func visit(_ node: CodeBlockSyntax) -> UnsafeMutableRawPointer {
58+
let statements = node.statements.map(self.visit)
59+
let loc = self.base.advanced(by: node.position.utf8Offset).raw
60+
61+
return statements.withBridgedArrayRef { ref in
62+
BraceStmt_create(ctx, loc, ref, loc)
63+
}
64+
}
65+
66+
public func visit(_ node: FunctionParameterSyntax) -> UnsafeMutableRawPointer {
67+
let loc = self.base.advanced(by: node.position.utf8Offset).raw
68+
69+
let firstName: UnsafeMutableRawPointer?
70+
let secondName: UnsafeMutableRawPointer?
71+
72+
if let nodeFirstName = node.firstName {
73+
var text = nodeFirstName.text
74+
firstName = text.withUTF8 { buf in
75+
SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count).raw
76+
}
77+
} else {
78+
firstName = nil
79+
}
80+
81+
if let nodeSecondName = node.secondName {
82+
var text = nodeSecondName.text
83+
secondName = text.withUTF8 { buf in
84+
SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count).raw
85+
}
86+
} else {
87+
secondName = nil
88+
}
89+
90+
return ParamDecl_create(ctx, loc, loc, firstName, loc, secondName, declContext)
91+
}
92+
93+
public func visit(_ node: FunctionDeclSyntax) -> UnsafeMutableRawPointer {
94+
let loc = self.base.advanced(by: node.position.utf8Offset).raw
95+
96+
var nameText = node.identifier.text
97+
let name = nameText.withUTF8 { buf in
98+
return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
99+
}
100+
101+
let body: UnsafeMutableRawPointer?
102+
if let nodeBody = node.body {
103+
body = visit(nodeBody)
104+
} else {
105+
body = nil
106+
}
107+
108+
let returnType: UnsafeMutableRawPointer?
109+
if let output = node.signature.output {
110+
returnType = visit(output.returnType)
111+
} else {
112+
returnType = nil
113+
}
114+
115+
let params = node.signature.input.parameterList.map { visit($0) }
116+
return params.withBridgedArrayRef { ref in
117+
FuncDecl_create(ctx, loc, false, loc, name, loc, false, nil, false, nil, loc, ref, loc, body, returnType, declContext)
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)