Skip to content

Commit cdb390d

Browse files
committed
Apply Alex's review comments.
1 parent a49ad7c commit cdb390d

File tree

3 files changed

+860
-1405
lines changed

3 files changed

+860
-1405
lines changed

Sources/SwiftSyntax/SyntaxTransform.swift.gyb

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -36,80 +36,48 @@ public protocol SyntaxTransformVisitor {
3636
}
3737

3838
extension SyntaxTransformVisitor {
39-
public func visit(_ token: TokenSyntax) -> ResultType { visitAny(Syntax(token)) }
40-
public func visit(_ node: UnknownSyntax) -> ResultType { visitAny(Syntax(node)) }
39+
public func visit(_ token: TokenSyntax) -> ResultType {
40+
visitAny(Syntax(token))
41+
}
42+
43+
public func visit(_ node: UnknownSyntax) -> ResultType {
44+
visitAny(Syntax(node))
45+
}
4146

4247
% for node in SYNTAX_NODES:
4348
% if is_visitable(node):
4449
/// Visiting `${node.name}` specifically.
4550
/// - Parameter node: the node we are visiting.
4651
/// - Returns: nil by default.
4752
public func visit(_ node: ${node.name}) -> ResultType {
48-
visitAny(Syntax(node))
53+
visitAny(Syntax(node))
4954
}
5055
% end
5156
% end
5257

53-
public func visit(_ data: Syntax) -> ResultType {
54-
switch data.raw.kind {
55-
case .token:
56-
let node = TokenSyntax(data)!
58+
public func visit(_ node: Syntax) -> ResultType {
59+
switch node.as(SyntaxEnum.self) {
60+
case .token(let node):
5761
return visit(node)
58-
case .unknown:
59-
let node = UnknownSyntax(data)!
62+
case .unknown(let node):
6063
return visit(node)
61-
// The implementation of every generated case goes into its own function. This
62-
// circumvents an issue where the compiler allocates stack space for every
63-
// case statement next to each other in debug builds, causing it to allocate
64-
// ~50KB per call to this function. rdar://55929175
65-
% for node in NON_BASE_SYNTAX_NODES:
66-
case .${node.swift_syntax_kind}:
67-
let node = ${node.name}(data)!
68-
return visit(node)
69-
% end
64+
% for node in NON_BASE_SYNTAX_NODES:
65+
case .${node.swift_syntax_kind}(let derived):
66+
return visit(derived)
67+
% end
7068
}
7169
}
7270

73-
public func visit(_ data: ExprSyntax) -> ResultType {
74-
switch data.raw.kind {
75-
% for node in NON_BASE_SYNTAX_NODES:
76-
% if node.base_kind == "Expr":
77-
case .${node.swift_syntax_kind}:
78-
let node = data.as(${node.name}.self)!
79-
return visit(node)
80-
% end
81-
% end
82-
default:
83-
fatalError("Not expression?")
84-
}
71+
public func visit(_ node: ExprSyntax) -> ResultType {
72+
visit(Syntax(node))
8573
}
8674

87-
public func visit(_ data: PatternSyntax) -> ResultType {
88-
switch data.raw.kind {
89-
% for node in NON_BASE_SYNTAX_NODES:
90-
% if node.base_kind == "Pattern":
91-
case .${node.swift_syntax_kind}:
92-
let node = data.as(${node.name}.self)!
93-
return visit(node)
94-
% end
95-
% end
96-
default:
97-
fatalError("Not expression?")
98-
}
75+
public func visit(_ node: PatternSyntax) -> ResultType {
76+
visit(Syntax(node))
9977
}
10078

101-
public func visit(_ data: TypeSyntax) -> ResultType {
102-
switch data.raw.kind {
103-
% for node in NON_BASE_SYNTAX_NODES:
104-
% if node.base_kind == "Type":
105-
case .${node.swift_syntax_kind}:
106-
let node = data.as(${node.name}.self)!
107-
return visit(node)
108-
% end
109-
% end
110-
default:
111-
fatalError("Not expression?")
112-
}
79+
public func visit(_ node: TypeSyntax) -> ResultType {
80+
visit(Syntax(node))
11381
}
11482

11583
public func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) -> [ResultType] {

0 commit comments

Comments
 (0)