Skip to content

Improve Type Checker performance in SyntaxVisitor, SyntaxRewriter, TokenKind, SyntaxUtils #2328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
"""
)

DeclSyntax(
"""
/// Forwards call to self.visit(_ token: TokenSyntax).
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
private func visitTokenSyntax(_ token: TokenSyntax) -> TokenSyntax {
self.visit(token)
}
"""
)

DeclSyntax(
"""
/// The function called before visiting the node and its descendants.
Expand Down Expand Up @@ -161,6 +172,46 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
)
}

for node in SYNTAX_NODES where !node.kind.isBase {
if (node.base == .syntax || node.base == .syntaxCollection) && node.kind != .missing {
DeclSyntax(
"""
/// Forward call to self.visit(_ node: ``\(node.kind.syntaxType)``).
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
private func visit\(node.kind.syntaxType)(_ node: \(node.kind.syntaxType)) -> \(node.kind.syntaxType) {
visit(node)
}
"""
)
} else {
DeclSyntax(
"""
/// Forward call to self.visit(_ node: ``\(node.kind.syntaxType)``).
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
private func visit\(node.kind.syntaxType)(_ node: \(node.kind.syntaxType)) -> \(node.baseType.syntaxBaseName) {
visit(node)
}
"""
)
}
}

for baseNode in SYNTAX_NODES where baseNode.kind.isBase && baseNode.kind != .syntax && baseNode.kind != .syntaxCollection {
let baseKind = baseNode.kind
DeclSyntax(
"""
/// Forward call to self.visit(_ node: ``\(baseKind.syntaxType)``).
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
private func visit\(baseKind.syntaxType)(_ node: \(baseKind.syntaxType)) -> \(baseKind.syntaxType) {
visit(node)
}
"""
)
}

DeclSyntax(
"""
/// Interpret `node` as a node of type `nodeType`, visit it, calling
Expand Down Expand Up @@ -225,12 +276,12 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
) {
try SwitchExprSyntax("switch node.raw.kind") {
SwitchCaseSyntax("case .token:") {
StmtSyntax("return { self.visitImpl($0, TokenSyntax.self, self.visit) }")
StmtSyntax("return { self.visitImpl($0, TokenSyntax.self, self.visitTokenSyntax) }")
}

for node in NON_BASE_SYNTAX_NODES {
SwitchCaseSyntax("case .\(node.varOrCaseName):") {
StmtSyntax("return { self.visitImpl($0, \(node.kind.syntaxType).self, self.visit) }")
StmtSyntax("return { self.visitImpl($0, \(node.kind.syntaxType).self, self.visit\(node.kind.syntaxType)) }")
}
}
}
Expand Down Expand Up @@ -258,7 +309,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {

for node in NON_BASE_SYNTAX_NODES {
SwitchCaseSyntax("case .\(node.varOrCaseName):") {
StmtSyntax("return visitImpl(node, \(node.kind.syntaxType).self, visit)")
StmtSyntax("return visitImpl(node, \(node.kind.syntaxType).self, visit\(node.kind.syntaxType))")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
)
}

for node in SYNTAX_NODES where !node.kind.isBase {
DeclSyntax(
"""
/// The function forwards call to self.visit(_ node: ``\(node.kind.syntaxType)``).
/// - node: the node we just finished visiting.
/// - Returns: how should we continue visiting.
private func visit\(node.kind.syntaxType)(_ node: \(node.kind.syntaxType)) -> SyntaxVisitorContinueKind {
visit(node)
}
"""
)

DeclSyntax(
"""
/// The function forwards call to self.visitPost(_ node: ``\(node.kind.syntaxType)``).
/// - node: the node we just finished visiting.
private func visitPost\(node.kind.syntaxType)(_ node: \(node.kind.syntaxType)) {
visitPost(node)
}
"""
)
}

DeclSyntax(
"""
/// Visiting ``TokenSyntax`` specifically.
Expand Down Expand Up @@ -168,7 +191,7 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {

for node in NON_BASE_SYNTAX_NODES {
SwitchCaseSyntax("case .\(node.varOrCaseName):") {
StmtSyntax("return { self.visitImpl($0, \(node.kind.syntaxType).self, self.visit, self.visitPost) }")
StmtSyntax("return { self.visitImpl($0, \(node.kind.syntaxType).self, self.visit\(node.kind.syntaxType), self.visitPost\(node.kind.syntaxType)) }")
}
}
}
Expand Down Expand Up @@ -203,7 +226,7 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {

for node in NON_BASE_SYNTAX_NODES {
SwitchCaseSyntax("case .\(node.varOrCaseName):") {
ExprSyntax("visitImpl(node, \(node.kind.syntaxType).self, visit, visitPost)")
ExprSyntax("visitImpl(node, \(node.kind.syntaxType).self, visit\(node.kind.syntaxType), visitPost\(node.kind.syntaxType))")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
}
}

try! ExtensionDeclSyntax("private extension RawTokenKind") {
try! VariableDeclSyntax(
"""
var defaultTextString: String?
"""
) {
StmtSyntax("guard let defaultText else { return nil }")
StmtSyntax("return String(syntaxText: defaultText)")
}
}

try! ExtensionDeclSyntax("extension TokenKind") {
try! FunctionDeclSyntax(
"""
Expand All @@ -227,7 +238,7 @@ let tokenKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
}
} else if tokenSpec.text != nil {
SwitchCaseSyntax("case .\(tokenSpec.varOrCaseName):") {
ExprSyntax("precondition(text.isEmpty || rawKind.defaultText.map(String.init) == text)")
ExprSyntax("precondition(((text.isEmpty as Bool) || ((rawKind.defaultTextString == text) as Bool)) as Bool)")
StmtSyntax("return .\(tokenSpec.varOrCaseName)")
}
} else {
Expand Down
6 changes: 5 additions & 1 deletion Sources/SwiftParser/SyntaxUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ extension RawUnexpectedNodesSyntax {
_ syntax4: some UnexpectedNodesCombinable,
arena: __shared SyntaxArena
) {
self.init(syntax1.elements + syntax2.elements + syntax3.elements + syntax4.elements, arena: arena)
var elements: [RawSyntax] = syntax1.elements
elements.append(contentsOf: syntax2.elements)
elements.append(contentsOf: syntax3.elements)
elements.append(contentsOf: syntax4.elements)
self.init(elements, arena: arena)
}
}

Expand Down
Loading