Skip to content

Commit d960a30

Browse files
committed
Improved TypeChecker performance in SyntaxRewriter & SyntaxVisitor
1 parent 911284b commit d960a30

File tree

7 files changed

+6856
-1156
lines changed

7 files changed

+6856
-1156
lines changed

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxRewriterFile.swift

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
6767
"""
6868
)
6969

70+
DeclSyntax(
71+
"""
72+
/// Forwards call to self.visit(_ token: TokenSyntax).
73+
/// - Parameter node: the node that is being visited
74+
/// - Returns: the rewritten node
75+
private func visitTokenSyntax(_ token: TokenSyntax) -> TokenSyntax {
76+
self.visit(token)
77+
}
78+
"""
79+
)
80+
7081
DeclSyntax(
7182
"""
7283
/// The function called before visiting the node and its descendants.
@@ -161,6 +172,46 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
161172
)
162173
}
163174

175+
for node in SYNTAX_NODES where !node.kind.isBase {
176+
if (node.base == .syntax || node.base == .syntaxCollection) && node.kind != .missing {
177+
DeclSyntax(
178+
"""
179+
/// Forward call to self.visit(_ node: ``\(node.kind.syntaxType)``).
180+
/// - Parameter node: the node that is being visited
181+
/// - Returns: the rewritten node
182+
private func visit\(node.kind.syntaxType)(_ node: \(node.kind.syntaxType)) -> \(node.kind.syntaxType) {
183+
visit(node)
184+
}
185+
"""
186+
)
187+
} else {
188+
DeclSyntax(
189+
"""
190+
/// Forward call to self.visit(_ node: ``\(node.kind.syntaxType)``).
191+
/// - Parameter node: the node that is being visited
192+
/// - Returns: the rewritten node
193+
private func visit\(node.kind.syntaxType)(_ node: \(node.kind.syntaxType)) -> \(node.baseType.syntaxBaseName) {
194+
visit(node)
195+
}
196+
"""
197+
)
198+
}
199+
}
200+
201+
for baseNode in SYNTAX_NODES where baseNode.kind.isBase && baseNode.kind != .syntax && baseNode.kind != .syntaxCollection {
202+
let baseKind = baseNode.kind
203+
DeclSyntax(
204+
"""
205+
/// Forward call to self.visit(_ node: ``\(baseKind.syntaxType)``).
206+
/// - Parameter node: the node that is being visited
207+
/// - Returns: the rewritten node
208+
private func visit\(baseKind.syntaxType)(_ node: \(baseKind.syntaxType)) -> \(baseKind.syntaxType) {
209+
visit(node)
210+
}
211+
"""
212+
)
213+
}
214+
164215
DeclSyntax(
165216
"""
166217
/// Interpret `node` as a node of type `nodeType`, visit it, calling
@@ -225,12 +276,12 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
225276
) {
226277
try SwitchExprSyntax("switch node.raw.kind") {
227278
SwitchCaseSyntax("case .token:") {
228-
StmtSyntax("return { self.visitImpl($0, TokenSyntax.self, self.visit) }")
279+
StmtSyntax("return { self.visitImpl($0, TokenSyntax.self, self.visitTokenSyntax) }")
229280
}
230281

231282
for node in NON_BASE_SYNTAX_NODES {
232283
SwitchCaseSyntax("case .\(node.varOrCaseName):") {
233-
StmtSyntax("return { self.visitImpl($0, \(node.kind.syntaxType).self, self.visit) }")
284+
StmtSyntax("return { self.visitImpl($0, \(node.kind.syntaxType).self, self.visit\(node.kind.syntaxType)) }")
234285
}
235286
}
236287
}
@@ -258,7 +309,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
258309

259310
for node in NON_BASE_SYNTAX_NODES {
260311
SwitchCaseSyntax("case .\(node.varOrCaseName):") {
261-
StmtSyntax("return visitImpl(node, \(node.kind.syntaxType).self, visit)")
312+
StmtSyntax("return visitImpl(node, \(node.kind.syntaxType).self, visit\(node.kind.syntaxType))")
262313
}
263314
}
264315
}

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxVisitorFile.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,29 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
7373
)
7474
}
7575

76+
for node in SYNTAX_NODES where !node.kind.isBase {
77+
DeclSyntax(
78+
"""
79+
/// The function forwards call to self.visit(_ node: ``\(node.kind.syntaxType)``).
80+
/// - node: the node we just finished visiting.
81+
/// - Returns: how should we continue visiting.
82+
private func visit\(node.kind.syntaxType)(_ node: \(node.kind.syntaxType)) -> SyntaxVisitorContinueKind {
83+
visit(node)
84+
}
85+
"""
86+
)
87+
88+
DeclSyntax(
89+
"""
90+
/// The function forwards call to self.visitPost(_ node: ``\(node.kind.syntaxType)``).
91+
/// - node: the node we just finished visiting.
92+
private func visitPost\(node.kind.syntaxType)(_ node: \(node.kind.syntaxType)) {
93+
visitPost(node)
94+
}
95+
"""
96+
)
97+
}
98+
7699
DeclSyntax(
77100
"""
78101
/// Visiting ``TokenSyntax`` specifically.
@@ -168,7 +191,7 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
168191

169192
for node in NON_BASE_SYNTAX_NODES {
170193
SwitchCaseSyntax("case .\(node.varOrCaseName):") {
171-
StmtSyntax("return { self.visitImpl($0, \(node.kind.syntaxType).self, self.visit, self.visitPost) }")
194+
StmtSyntax("return { self.visitImpl($0, \(node.kind.syntaxType).self, self.visit\(node.kind.syntaxType), self.visitPost\(node.kind.syntaxType)) }")
172195
}
173196
}
174197
}
@@ -203,7 +226,7 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
203226

204227
for node in NON_BASE_SYNTAX_NODES {
205228
SwitchCaseSyntax("case .\(node.varOrCaseName):") {
206-
ExprSyntax("visitImpl(node, \(node.kind.syntaxType).self, visit, visitPost)")
229+
ExprSyntax("visitImpl(node, \(node.kind.syntaxType).self, visit\(node.kind.syntaxType), visitPost\(node.kind.syntaxType))")
207230
}
208231
}
209232
}

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/TokenKindFile.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
204204
}
205205
}
206206

207+
try! ExtensionDeclSyntax("private extension RawTokenKind") {
208+
try! VariableDeclSyntax(
209+
"""
210+
var defaultTextString: String?
211+
"""
212+
) {
213+
StmtSyntax("guard let defaultText else { return nil }")
214+
StmtSyntax("return String(syntaxText: defaultText)")
215+
}
216+
}
217+
207218
try! ExtensionDeclSyntax("extension TokenKind") {
208219
try! FunctionDeclSyntax(
209220
"""
@@ -227,7 +238,7 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
227238
}
228239
} else if tokenSpec.text != nil {
229240
SwitchCaseSyntax("case .\(tokenSpec.varOrCaseName):") {
230-
ExprSyntax("precondition(text.isEmpty || rawKind.defaultText.map(String.init) == text)")
241+
ExprSyntax("precondition(((text.isEmpty as Bool) || ((rawKind.defaultTextString == text) as Bool)) as Bool)")
231242
StmtSyntax("return .\(tokenSpec.varOrCaseName)")
232243
}
233244
} else {

Sources/SwiftParser/SyntaxUtils.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ extension RawUnexpectedNodesSyntax {
9393
_ syntax4: some UnexpectedNodesCombinable,
9494
arena: __shared SyntaxArena
9595
) {
96-
self.init(syntax1.elements + syntax2.elements + syntax3.elements + syntax4.elements, arena: arena)
96+
var elements: [RawSyntax] = syntax1.elements
97+
elements.append(contentsOf: syntax2.elements)
98+
elements.append(contentsOf: syntax3.elements)
99+
elements.append(contentsOf: syntax4.elements)
100+
self.init(elements, arena: arena)
97101
}
98102
}
99103

0 commit comments

Comments
 (0)