Skip to content

Commit 3991c28

Browse files
committed
Rebase off changes to #643.
1 parent bfcb3d1 commit 3991c28

File tree

2 files changed

+62
-67
lines changed

2 files changed

+62
-67
lines changed

Sources/ASTGen/ASTGen.swift

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ extension Array {
1111
}
1212
}
1313

14-
extension Collection {
15-
public var only: Element {
16-
if isEmpty { fatalError("Empty Collection.") }
17-
else if count > 1 { fatalError("More than one element") }
18-
return first!
19-
}
20-
}
21-
2214
extension UnsafePointer {
2315
public var raw: UnsafeMutableRawPointer {
2416
UnsafeMutableRawPointer(mutating: self)
@@ -38,12 +30,17 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
3830
// return nil
3931
// }
4032

33+
@_disfavoredOverload
34+
public func visit(_ node: SourceFileSyntax) -> UnsafeMutableRawPointer {
35+
fatalError("Use other overload.")
36+
}
37+
4138
public func visit(_ node: SourceFileSyntax) -> [UnsafeMutableRawPointer] {
4239
let loc = self.base.advanced(by: node.position.utf8Offset).raw
4340
var out = [UnsafeMutableRawPointer]()
4441

4542
for element in node.statements {
46-
let swiftASTNodes = visit(element).only
43+
let swiftASTNodes = visit(element)
4744
if element.item.is(StmtSyntax.self) || element.item.is(ExprSyntax.self) {
4845
out.append(SwiftTopLevelCodeDecl_create(ctx, declContext, loc, swiftASTNodes))
4946
} else {
@@ -55,103 +52,103 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
5552
return out
5653
}
5754

58-
public func visit(_ node: FunctionCallExprSyntax) -> [UnsafeMutableRawPointer] {
59-
let args = visit(node.argumentList).only
55+
public func visit(_ node: FunctionCallExprSyntax) -> UnsafeMutableRawPointer {
56+
let args = visit(node.argumentList)
6057
// TODO: hack
61-
let callee = visit(node.calledExpression).only
62-
let call = SwiftFunctionCallExpr_create(self.ctx, callee, args)
63-
64-
return [call]
58+
let callee = visit(node.calledExpression)
59+
return SwiftFunctionCallExpr_create(self.ctx, callee, args)
6560
}
6661

67-
public func visit(_ node: IdentifierExprSyntax) -> [UnsafeMutableRawPointer] {
62+
public func visit(_ node: IdentifierExprSyntax) -> UnsafeMutableRawPointer {
6863
let loc = self.base.advanced(by: node.position.utf8Offset).raw
6964

7065
var text = node.identifier.text
7166
let id = text.withUTF8 { buf in
7267
return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
7368
}
7469

75-
return [SwiftIdentifierExpr_create(ctx, id, loc)]
70+
return SwiftIdentifierExpr_create(ctx, id, loc)
7671
}
7772

78-
public func visit(_ node: SimpleTypeIdentifierSyntax) -> [UnsafeMutableRawPointer] {
73+
public func visit(_ node: SimpleTypeIdentifierSyntax) -> UnsafeMutableRawPointer {
7974
let loc = self.base.advanced(by: node.position.utf8Offset).raw
8075

8176
var text = node.name.text
8277
let id = text.withUTF8 { buf in
8378
return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
8479
}
8580

86-
return [SimpleIdentTypeRepr_create(ctx, loc, id)]
81+
return SimpleIdentTypeRepr_create(ctx, loc, id)
8782
}
8883

89-
public func visit(_ node: IdentifierPatternSyntax) -> [UnsafeMutableRawPointer] {
84+
public func visit(_ node: IdentifierPatternSyntax) -> UnsafeMutableRawPointer {
9085
let loc = self.base.advanced(by: node.position.utf8Offset).raw
9186

9287
var text = node.identifier.text
9388
let id = text.withUTF8 { buf in
9489
return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
9590
}
9691

97-
return [SwiftIdentifierExpr_create(ctx, id, loc)]
92+
return SwiftIdentifierExpr_create(ctx, id, loc)
9893
}
9994

100-
public func visit(_ node: MemberAccessExprSyntax) -> [UnsafeMutableRawPointer] {
95+
public func visit(_ node: MemberAccessExprSyntax) -> UnsafeMutableRawPointer {
10196
let loc = self.base.advanced(by: node.position.utf8Offset).raw
102-
let base = visit(node.base!).only
97+
let base = visit(node.base!)
10398
var nameText = node.name.text
10499
let name = nameText.withUTF8 { buf in
105100
return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
106101
}
107102

108-
return [UnresolvedDotExpr_create(ctx, base, loc, name, loc)]
103+
return UnresolvedDotExpr_create(ctx, base, loc, name, loc)
109104
}
110105

106+
public func visit(_ node: TupleExprElementSyntax) -> UnsafeMutableRawPointer {
107+
visit(node.expression)
108+
}
111109

112-
public func visit(_ node: TupleExprElementListSyntax) -> [UnsafeMutableRawPointer] {
113-
let elements = node.map { visit($0).only }
110+
public func visit(_ node: TupleExprElementListSyntax) -> UnsafeMutableRawPointer {
111+
let elements = node.map(self.visit)
114112

115113
// TODO: find correct paren locs.
116114
let lParenLoc = self.base.advanced(by: node.position.utf8Offset).raw
117115
let rParenLoc = self.base.advanced(by: node.position.utf8Offset).raw
118116

119-
return [elements.withBridgedArrayRef { elementsRef in
117+
return elements.withBridgedArrayRef { elementsRef in
120118
SwiftTupleExpr_create(self.ctx, lParenLoc, elementsRef, rParenLoc)
121-
}]
122-
}
123-
124-
public func visit(_ node: PatternBindingSyntax) -> [UnsafeMutableRawPointer] {
125-
let pattern = visit(node.pattern)
126-
let initializer = visit(node.initializer!)
127-
128-
return [pattern.only, initializer.only]
119+
}
129120
}
130121

131-
public func visit(_ node: VariableDeclSyntax) -> [UnsafeMutableRawPointer] {
132-
let components = visit(node.bindings)
133-
assert(components.count == 2)
134-
let pattern = components.first!
135-
let initializer = components.last!
122+
public func visit(_ node: VariableDeclSyntax) -> UnsafeMutableRawPointer {
123+
let pattern = visit(node.bindings.first!.pattern)
124+
let initializer = visit(node.bindings.first!.initializer!)
136125

137126
let loc = self.base.advanced(by: node.position.utf8Offset).raw
138127
let isStateic = false // TODO: compute this
139128
let isLet = node.letOrVarKeyword.tokenKind == .letKeyword
140129

141130
// TODO: don't drop "initializer" on the floor.
142-
return [SwiftVarDecl_create(ctx, pattern, loc, isStateic, isLet, declContext)]
131+
return SwiftVarDecl_create(ctx, pattern, loc, isStateic, isLet, declContext)
143132
}
144133

145-
public func visit(_ node: CodeBlockSyntax) -> [UnsafeMutableRawPointer] {
146-
let statements = visit(node.statements)
134+
public func visit(_ node: ConditionElementSyntax) -> UnsafeMutableRawPointer {
135+
visit(node.condition)
136+
}
137+
138+
public func visit(_ node: CodeBlockItemSyntax) -> UnsafeMutableRawPointer {
139+
visit(node.item)
140+
}
141+
142+
public func visit(_ node: CodeBlockSyntax) -> UnsafeMutableRawPointer {
143+
let statements = node.statements.map(self.visit)
147144
let loc = self.base.advanced(by: node.position.utf8Offset).raw
148145

149-
return [statements.withBridgedArrayRef { ref in
146+
return statements.withBridgedArrayRef { ref in
150147
BraceStmt_create(ctx, loc, ref, loc)
151-
}]
148+
}
152149
}
153150

154-
public func visit(_ node: FunctionParameterSyntax) -> [UnsafeMutableRawPointer] {
151+
public func visit(_ node: FunctionParameterSyntax) -> UnsafeMutableRawPointer {
155152
let loc = self.base.advanced(by: node.position.utf8Offset).raw
156153

157154
let firstName: UnsafeMutableRawPointer?
@@ -175,10 +172,10 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
175172
secondName = nil
176173
}
177174

178-
return [ParamDecl_create(ctx, loc, loc, firstName, loc, secondName, declContext)]
175+
return ParamDecl_create(ctx, loc, loc, firstName, loc, secondName, declContext)
179176
}
180177

181-
public func visit(_ node: FunctionDeclSyntax) -> [UnsafeMutableRawPointer] {
178+
public func visit(_ node: FunctionDeclSyntax) -> UnsafeMutableRawPointer {
182179
let loc = self.base.advanced(by: node.position.utf8Offset).raw
183180

184181
var nameText = node.identifier.text
@@ -188,55 +185,53 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
188185

189186
let body: UnsafeMutableRawPointer?
190187
if let nodeBody = node.body {
191-
body = visit(nodeBody).only
188+
body = visit(nodeBody)
192189
} else {
193190
body = nil
194191
}
195192

196193
let returnType: UnsafeMutableRawPointer?
197194
if let output = node.signature.output {
198-
returnType = visit(output.returnType).only
195+
returnType = visit(output.returnType)
199196
} else {
200197
returnType = nil
201198
}
202199

203-
let params = node.signature.input.parameterList.map { visit($0).only }
204-
return [params.withBridgedArrayRef { ref in
200+
let params = node.signature.input.parameterList.map { visit($0) }
201+
return params.withBridgedArrayRef { ref in
205202
FuncDecl_create(ctx, loc, false, loc, name, loc, false, nil, false, nil, loc, ref, loc, body, returnType, declContext)
206-
}]
203+
}
207204
}
208205

209-
public func visit(_ node: IfStmtSyntax) -> [UnsafeMutableRawPointer] {
210-
let conditions = node.conditions.map { self.visit($0).only }
206+
public func visit(_ node: IfStmtSyntax) -> UnsafeMutableRawPointer {
207+
let conditions = node.conditions.map(self.visit)
211208
assert(conditions.count == 1) // TODO: handle multiple conditions.
212209

213-
let body = visit(node.body).only
210+
let body = visit(node.body)
214211
let loc = self.base.advanced(by: node.position.utf8Offset).raw
215212

216213
if let elseBody = node.elseBody, node.elseKeyword != nil {
217-
let elseStmt = visit(elseBody.data).only // TODO: don't use SyntaxData
218-
return [IfStmt_create(ctx, loc, conditions.only, body, loc,
219-
elseStmt)]
214+
return IfStmt_create(ctx, loc, conditions.first!, body, loc, visit(elseBody))
220215
}
221216

222-
return [IfStmt_create(ctx, loc, conditions.only, body, nil, nil)]
217+
return IfStmt_create(ctx, loc, conditions.first!, body, nil, nil)
223218
}
224219

225-
public func visit(_ node: StringLiteralExprSyntax) -> [UnsafeMutableRawPointer] {
220+
public func visit(_ node: StringLiteralExprSyntax) -> UnsafeMutableRawPointer {
226221
let loc = self.base.advanced(by: node.position.utf8Offset).raw
227-
var segment = node.segments.only.as(StringSegmentSyntax.self)!.content.text
222+
var segment = node.segments.first!.as(StringSegmentSyntax.self)!.content.text
228223
return segment.withUTF8 { buf in
229224
let id = SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
230-
return [SwiftStringLiteralExpr_create(ctx, id, buf.count, loc)]
225+
return SwiftStringLiteralExpr_create(ctx, id, buf.count, loc)
231226
}
232227
}
233228

234-
public func visit(_ node: IntegerLiteralExprSyntax) -> [UnsafeMutableRawPointer] {
229+
public func visit(_ node: IntegerLiteralExprSyntax) -> UnsafeMutableRawPointer {
235230
let loc = self.base.advanced(by: node.position.utf8Offset).raw
236231
var segment = node.digits.text
237232
return segment.withUTF8 { buf in
238233
let id = SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
239-
return [SwiftIntegerLiteralExpr_create(ctx, id, buf.count, loc)]
234+
return SwiftIntegerLiteralExpr_create(ctx, id, buf.count, loc)
240235
}
241236
}
242237
}
@@ -249,7 +244,7 @@ public func parseTopLevelSwift(
249244
callback: @convention(c) (UnsafeMutableRawPointer, UnsafeMutableRawPointer) -> Void
250245
) {
251246
let syntax = try! Parser.parse(source: String(cString: buffer))
252-
// dump(syntax)
247+
dump(syntax)
253248
ASTGenVisitor(ctx: ctx, base: buffer, declContext: declContext)
254249
.visit(syntax)
255250
.forEach { callback($0, outputContext) }

Sources/SwiftSyntax/Syntax.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/// Each node has accessors for its known children, and allows efficient
1515
/// iteration over the children through its `children` property.
1616
public struct Syntax: SyntaxProtocol, SyntaxHashable {
17-
public let data: SyntaxData
17+
let data: SyntaxData
1818

1919
public var _syntaxNode: Syntax {
2020
return self

0 commit comments

Comments
 (0)