|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftSyntax |
| 14 | +import SwiftSyntaxBuilder |
| 15 | +import SyntaxSupport |
| 16 | +import Utils |
| 17 | + |
| 18 | +let syntaxBaseNodesFile = SourceFile { |
| 19 | + for node in SYNTAX_NODES where node.isBase { |
| 20 | + ProtocolDecl(""" |
| 21 | + // MARK: - \(raw: node.name) |
| 22 | + |
| 23 | + /// Protocol to which all `\(raw: node.name)` nodes conform. Extension point to add |
| 24 | + /// common methods to all `\(raw: node.name)` nodes. |
| 25 | + /// DO NOT CONFORM TO THIS PROTOCOL YOURSELF! |
| 26 | + public protocol \(raw: node.name)Protocol: \(raw: node.baseType.baseName)Protocol {} |
| 27 | + """) |
| 28 | + |
| 29 | + ExtensionDecl("public extension Syntax") { |
| 30 | + FunctionDecl(""" |
| 31 | + /// Check whether the non-type erased version of this syntax node conforms to |
| 32 | + /// \(raw: node.name)Protocol. |
| 33 | + /// Note that this will incur an existential conversion. |
| 34 | + func isProtocol(_: \(raw: node.name)Protocol.Protocol) -> Bool { |
| 35 | + return self.asProtocol(\(raw: node.name)Protocol.self) != nil |
| 36 | + } |
| 37 | + """) |
| 38 | + |
| 39 | + FunctionDecl(""" |
| 40 | + /// Return the non-type erased version of this syntax node if it conforms to |
| 41 | + /// \(raw: node.name)Protocol. Otherwise return nil. |
| 42 | + /// Note that this will incur an existential conversion. |
| 43 | + func asProtocol(_: \(raw: node.name)Protocol.Protocol) -> \(raw: node.name)Protocol? { |
| 44 | + return self.asProtocol(SyntaxProtocol.self) as? \(raw: node.name)Protocol |
| 45 | + } |
| 46 | + """) |
| 47 | + } |
| 48 | + |
| 49 | + StructDecl(""" |
| 50 | + \(node.description ?? "") |
| 51 | + public struct \(node.name): \(node.name)Protocol, SyntaxHashable |
| 52 | + """) { |
| 53 | + VariableDecl("public let _syntaxNode: Syntax") |
| 54 | + |
| 55 | + InitializerDecl(""" |
| 56 | + /// Create a `\(raw: node.name)` node from a specialized syntax node. |
| 57 | + public init<S: \(raw: node.name)Protocol>(_ syntax: S) { |
| 58 | + // We know this cast is going to succeed. Go through init(_: SyntaxData) |
| 59 | + // to do a sanity check and verify the kind matches in debug builds and get |
| 60 | + // maximum performance in release builds. |
| 61 | + self.init(syntax._syntaxNode.data) |
| 62 | + } |
| 63 | + """) |
| 64 | + |
| 65 | + InitializerDecl(""" |
| 66 | + /// Create a `\(raw: node.name)` node from a specialized optional syntax node. |
| 67 | + public init?<S: \(raw: node.name)Protocol>(_ syntax: S?) { |
| 68 | + guard let syntax = syntax else { return nil } |
| 69 | + self.init(syntax) |
| 70 | + } |
| 71 | + """) |
| 72 | + |
| 73 | + InitializerDecl(""" |
| 74 | + public init(fromProtocol syntax: \(raw: node.name)Protocol) { |
| 75 | + // We know this cast is going to succeed. Go through init(_: SyntaxData) |
| 76 | + // to do a sanity check and verify the kind matches in debug builds and get |
| 77 | + // maximum performance in release builds. |
| 78 | + self.init(syntax._syntaxNode.data) |
| 79 | + } |
| 80 | + """) |
| 81 | + |
| 82 | + InitializerDecl(""" |
| 83 | + /// Create a `\(raw: node.name)` node from a specialized optional syntax node. |
| 84 | + public init?(fromProtocol syntax: \(raw: node.name)Protocol?) { |
| 85 | + guard let syntax = syntax else { return nil } |
| 86 | + self.init(fromProtocol: syntax) |
| 87 | + } |
| 88 | + """) |
| 89 | + |
| 90 | + InitializerDecl("public init?<S: SyntaxProtocol>(_ node: S)") { |
| 91 | + SwitchStmt(expression: MemberAccessExpr("node.raw.kind")) { |
| 92 | + SwitchCaseList { |
| 93 | + SwitchCase( |
| 94 | + label: .case(SwitchCaseLabel { |
| 95 | + for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind { |
| 96 | + CaseItem( |
| 97 | + pattern: EnumCasePattern( |
| 98 | + period: .period, |
| 99 | + caseName: .identifier(childNode.swiftSyntaxKind)), |
| 100 | + trailingComma: .comma) |
| 101 | + } |
| 102 | + })) { |
| 103 | + Expr("self._syntaxNode = node._syntaxNode") |
| 104 | + } |
| 105 | + |
| 106 | + SwitchCase("default:") { |
| 107 | + ReturnStmt("return nil") |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + InitializerDecl(""" |
| 114 | + /// Creates a `\(node.name)` node from the given `SyntaxData`. This assumes |
| 115 | + /// that the `SyntaxData` is of the correct kind. If it is not, the behaviour |
| 116 | + /// is undefined. |
| 117 | + internal init(_ data: SyntaxData) |
| 118 | + """) { |
| 119 | + IfConfigDecl( |
| 120 | + clauses: IfConfigClauseList { |
| 121 | + IfConfigClause( |
| 122 | + poundKeyword: .poundIfKeyword(), |
| 123 | + condition: Expr("DEBUG"), |
| 124 | + elements: IfConfigClauseSyntax.Elements.statements(CodeBlockItemList { |
| 125 | + SwitchStmt( |
| 126 | + expression: Expr("data.raw.kind")) { |
| 127 | + SwitchCase( |
| 128 | + label: .case(SwitchCaseLabel { |
| 129 | + for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind { |
| 130 | + CaseItem( |
| 131 | + pattern: EnumCasePatternSyntax( |
| 132 | + period: .period, |
| 133 | + caseName: .identifier(childNode.swiftSyntaxKind)), |
| 134 | + trailingComma: .comma) |
| 135 | + } |
| 136 | + })) { |
| 137 | + BreakStmt() |
| 138 | + } |
| 139 | + |
| 140 | + SwitchCase("default:") { |
| 141 | + FunctionCallExpr("fatalError(\"Unable to create \(raw: node.name) from \\(data.raw.kind)\")") |
| 142 | + } |
| 143 | + } |
| 144 | + }) |
| 145 | + ) |
| 146 | + } |
| 147 | + ) |
| 148 | + |
| 149 | + Expr("self._syntaxNode = Syntax(data)") |
| 150 | + } |
| 151 | + |
| 152 | + FunctionDecl(""" |
| 153 | + public func `is`<S: \(raw: node.name)Protocol>(_ syntaxType: S.Type) -> Bool { |
| 154 | + return self.as(syntaxType) != nil |
| 155 | + } |
| 156 | + """) |
| 157 | + |
| 158 | + FunctionDecl(""" |
| 159 | + public func `as`<S: \(raw: node.name)Protocol>(_ syntaxType: S.Type) -> S? { |
| 160 | + return S.init(self) |
| 161 | + } |
| 162 | + """) |
| 163 | + |
| 164 | + FunctionDecl(""" |
| 165 | + public func cast<S: \(raw: node.name)Protocol>(_ syntaxType: S.Type) -> S { |
| 166 | + return self.as(S.self)! |
| 167 | + } |
| 168 | + """) |
| 169 | + |
| 170 | + FunctionDecl(""" |
| 171 | + /// Syntax nodes always conform to `\(raw: node.name)Protocol`. This API is just |
| 172 | + /// added for consistency. |
| 173 | + /// Note that this will incur an existential conversion. |
| 174 | + @available(*, deprecated, message: "Expression always evaluates to true") |
| 175 | + public func isProtocol(_: \(raw: node.name)Protocol.Protocol) -> Bool { |
| 176 | + return true |
| 177 | + } |
| 178 | + """) |
| 179 | + |
| 180 | + FunctionDecl(""" |
| 181 | + /// Return the non-type erased version of this syntax node. |
| 182 | + /// Note that this will incur an existential conversion. |
| 183 | + public func asProtocol(_: \(raw: node.name)Protocol.Protocol) -> \(raw: node.name)Protocol { |
| 184 | + return Syntax(self).asProtocol(\(raw: node.name)Protocol.self)! |
| 185 | + } |
| 186 | + """) |
| 187 | + |
| 188 | + |
| 189 | + VariableDecl( |
| 190 | + modifiers: [DeclModifier(name: .public), DeclModifier(name: .static)], |
| 191 | + name: IdentifierPattern("structure"), |
| 192 | + type: TypeAnnotation( |
| 193 | + type: TypeSyntax("SyntaxNodeStructure")) |
| 194 | + ) { |
| 195 | + ReturnStmt( |
| 196 | + expression: FunctionCallExpr( |
| 197 | + calledExpression: MemberAccessExpr(".choices"), |
| 198 | + leftParen: Token.leftParen, |
| 199 | + argumentList: |
| 200 | + TupleExprElementList { |
| 201 | + TupleExprElement( |
| 202 | + expression: ArrayExprSyntax { |
| 203 | + for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind { |
| 204 | + ArrayElement( |
| 205 | + expression: FunctionCallExpr("\n.node(\(raw: childNode.name).self)"), |
| 206 | + trailingComma: Token.comma) |
| 207 | + } |
| 208 | + }) |
| 209 | + }, |
| 210 | + rightParen: Token.rightParen) |
| 211 | + ) |
| 212 | + } |
| 213 | + |
| 214 | + FunctionDecl(""" |
| 215 | + public func childNameForDiagnostics(_ index: SyntaxChildrenIndex) -> String? { |
| 216 | + return Syntax(self).childNameForDiagnostics(index) |
| 217 | + } |
| 218 | + """) |
| 219 | + } |
| 220 | + |
| 221 | + ExtensionDecl(""" |
| 222 | + extension \(raw: node.name): CustomReflectable { |
| 223 | + /// Reconstructs the real syntax type for this type from the node's kind and |
| 224 | + /// provides a mirror that reflects this type. |
| 225 | + public var customMirror: Mirror { |
| 226 | + return Mirror(reflecting: Syntax(self).asProtocol(SyntaxProtocol.self)) |
| 227 | + } |
| 228 | + } |
| 229 | + """) |
| 230 | + |
| 231 | + } |
| 232 | +} |
0 commit comments