|
| 1 | +%{ |
| 2 | + from gyb_syntax_support import * |
| 3 | + # -*- mode: Swift -*- |
| 4 | + # Ignore the following admonition it applies to the resulting .swift file only |
| 5 | +}% |
| 6 | +//// Automatically Generated From SyntaxTransform.swift.gyb. |
| 7 | +//// Do Not Edit Directly! |
| 8 | +//===------ SyntaxTransform.swift - Syntax Transform Visitor Protocol -----===// |
| 9 | +// |
| 10 | +// This source file is part of the Swift.org open source project |
| 11 | +// |
| 12 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 13 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 14 | +// |
| 15 | +// See https://swift.org/LICENSE.txt for license information |
| 16 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 17 | +// |
| 18 | +//===----------------------------------------------------------------------===// |
| 19 | + |
| 20 | +public protocol SyntaxTransformVisitor { |
| 21 | + associatedtype ResultType = Void |
| 22 | + |
| 23 | + func visit(_ token: TokenSyntax) -> [ResultType] |
| 24 | + func visit(_ node: UnknownSyntax) -> [ResultType] |
| 25 | + |
| 26 | +% for node in SYNTAX_NODES: |
| 27 | +% if is_visitable(node): |
| 28 | + /// Visiting `${node.name}` specifically. |
| 29 | + /// - Parameter node: the node we are visiting. |
| 30 | + /// - Returns: the sum of whatever the child visitors return. |
| 31 | + func visit(_ node: ${node.name}) -> [ResultType] |
| 32 | +% end |
| 33 | +% end |
| 34 | +} |
| 35 | + |
| 36 | +extension SyntaxTransformVisitor { |
| 37 | + public func visit(_ token: TokenSyntax) -> [ResultType] { [] } |
| 38 | + public func visit(_ node: UnknownSyntax) -> [ResultType] { [] } |
| 39 | + |
| 40 | +% for node in SYNTAX_NODES: |
| 41 | +% if is_visitable(node): |
| 42 | + /// Visiting `${node.name}` specifically. |
| 43 | + /// - Parameter node: the node we are visiting. |
| 44 | + /// - Returns: nil by default. |
| 45 | + public func visit(_ node: ${node.name}) -> [ResultType] { |
| 46 | +% if node.is_base(): |
| 47 | + // Avoid calling into visitChildren if possible. |
| 48 | + if !node.raw.layoutView!.children.isEmpty { |
| 49 | + return visitChildren(node) |
| 50 | + } |
| 51 | + return [] |
| 52 | +% else: |
| 53 | + // Avoid calling into visitChildren if possible. |
| 54 | + if !node.raw.layoutView!.children.isEmpty { |
| 55 | + return visitChildren(node) |
| 56 | + } |
| 57 | + return [] |
| 58 | +% end |
| 59 | + } |
| 60 | +% end |
| 61 | +% end |
| 62 | + |
| 63 | + public func visit(_ data: Syntax) -> [ResultType] { |
| 64 | + switch data.raw.kind { |
| 65 | + case .token: |
| 66 | + let node = TokenSyntax(data)! |
| 67 | + return visit(node) |
| 68 | + case .unknown: |
| 69 | + let node = UnknownSyntax(data)! |
| 70 | + return visit(node) |
| 71 | + // The implementation of every generated case goes into its own function. This |
| 72 | + // circumvents an issue where the compiler allocates stack space for every |
| 73 | + // case statement next to each other in debug builds, causing it to allocate |
| 74 | + // ~50KB per call to this function. rdar://55929175 |
| 75 | + % for node in NON_BASE_SYNTAX_NODES: |
| 76 | + case .${node.swift_syntax_kind}: |
| 77 | + let node = ${node.name}(data)! |
| 78 | + return visit(node) |
| 79 | + % end |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public func visit(_ data: ExprSyntax) -> [ResultType] { |
| 84 | + switch data.raw.kind { |
| 85 | + % for node in NON_BASE_SYNTAX_NODES: |
| 86 | + % if node.base_kind == "Expr": |
| 87 | + case .${node.swift_syntax_kind}: |
| 88 | + let node = data.as(${node.name}.self)! |
| 89 | + return visit(node) |
| 90 | + % end |
| 91 | + % end |
| 92 | + default: |
| 93 | + fatalError("Not expression?") |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public func visit(_ data: PatternSyntax) -> [ResultType] { |
| 98 | + switch data.raw.kind { |
| 99 | + % for node in NON_BASE_SYNTAX_NODES: |
| 100 | + % if node.base_kind == "Pattern": |
| 101 | + case .${node.swift_syntax_kind}: |
| 102 | + let node = data.as(${node.name}.self)! |
| 103 | + return visit(node) |
| 104 | + % end |
| 105 | + % end |
| 106 | + default: |
| 107 | + fatalError("Not expression?") |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public func visit(_ data: TypeSyntax) -> [ResultType] { |
| 112 | + switch data.raw.kind { |
| 113 | + % for node in NON_BASE_SYNTAX_NODES: |
| 114 | + % if node.base_kind == "Type": |
| 115 | + case .${node.swift_syntax_kind}: |
| 116 | + let node = data.as(${node.name}.self)! |
| 117 | + return visit(node) |
| 118 | + % end |
| 119 | + % end |
| 120 | + default: |
| 121 | + fatalError("Not expression?") |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + public func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) -> [ResultType] { |
| 126 | + let syntaxNode = Syntax(node) |
| 127 | + return NonNilRawSyntaxChildren(syntaxNode, viewMode: .sourceAccurate).flatMap { rawChild in |
| 128 | + let child = Syntax(SyntaxData(rawChild, parent: syntaxNode)) |
| 129 | + return visit(child) |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments