|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 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 | + |
| 17 | +@MemberBlockItemListBuilder |
| 18 | +func choiceNodeCastingMethods(for syntaxNodeKind: SyntaxNodeKind) -> MemberBlockItemListSyntax { |
| 19 | + if syntaxNodeKind.isBase { |
| 20 | + DeclSyntax( |
| 21 | + """ |
| 22 | + /// Checks if the current syntax node can be cast to the type conforming to the ``\(syntaxNodeKind.protocolType)`` protocol. |
| 23 | + /// |
| 24 | + /// - Returns: `true` if the node can be cast, `false` otherwise. |
| 25 | + public func `is`<S: \(syntaxNodeKind.protocolType)>(_ syntaxType: S.Type) -> Bool { |
| 26 | + return self.as(syntaxType) != nil |
| 27 | + } |
| 28 | + """ |
| 29 | + ) |
| 30 | + |
| 31 | + DeclSyntax( |
| 32 | + """ |
| 33 | + /// Attempts to cast the current syntax node to the type conforming to the ``\(syntaxNodeKind.protocolType)`` protocol. |
| 34 | + /// |
| 35 | + /// - Returns: An instance of the specialized type, or `nil` if the cast fails. |
| 36 | + public func `as`<S: \(syntaxNodeKind.protocolType)>(_ syntaxType: S.Type) -> S? { |
| 37 | + return S.init(self) |
| 38 | + } |
| 39 | + """ |
| 40 | + ) |
| 41 | + |
| 42 | + DeclSyntax( |
| 43 | + """ |
| 44 | + /// Force-casts the current syntax node to the type conforming to the ``\(syntaxNodeKind.protocolType)`` protocol. |
| 45 | + /// |
| 46 | + /// - Returns: An instance of the specialized type. |
| 47 | + /// - Warning: This function will crash if the cast is not possible. Use `as` to safely attempt a cast. |
| 48 | + public func cast<S: \(syntaxNodeKind.protocolType)>(_ syntaxType: S.Type) -> S { |
| 49 | + return self.as(S.self)! |
| 50 | + } |
| 51 | + """ |
| 52 | + ) |
| 53 | + } else { |
| 54 | + DeclSyntax( |
| 55 | + """ |
| 56 | + /// Checks if the current syntax node can be cast to ``\(syntaxNodeKind.syntaxType)``. |
| 57 | + /// |
| 58 | + /// - Returns: `true` if the node can be cast, `false` otherwise. |
| 59 | + public func `is`(_ syntaxType: \(syntaxNodeKind.syntaxType).Type) -> Bool { |
| 60 | + return self.as(syntaxType) != nil |
| 61 | + } |
| 62 | + """ |
| 63 | + ) |
| 64 | + |
| 65 | + DeclSyntax( |
| 66 | + """ |
| 67 | + /// Attempts to cast the current syntax node to ``\(syntaxNodeKind.syntaxType)``. |
| 68 | + /// |
| 69 | + /// - Returns: An instance of ``\(syntaxNodeKind.syntaxType)``, or `nil` if the cast fails. |
| 70 | + public func `as`(_ syntaxType: \(syntaxNodeKind.syntaxType).Type) -> \(syntaxNodeKind.syntaxType)? { |
| 71 | + return \(syntaxNodeKind.syntaxType).init(self) |
| 72 | + } |
| 73 | + """ |
| 74 | + ) |
| 75 | + |
| 76 | + DeclSyntax( |
| 77 | + """ |
| 78 | + /// Force-casts the current syntax node to ``\(syntaxNodeKind.syntaxType)``. |
| 79 | + /// |
| 80 | + /// - Returns: An instance of ``\(syntaxNodeKind.syntaxType)``. |
| 81 | + /// - Warning: This function will crash if the cast is not possible. Use `as` to safely attempt a cast. |
| 82 | + public func cast(_ syntaxType: \(syntaxNodeKind.syntaxType).Type) -> \(syntaxNodeKind.syntaxType) { |
| 83 | + return self.as(\(syntaxNodeKind.syntaxType).self)! |
| 84 | + } |
| 85 | + """ |
| 86 | + ) |
| 87 | + } |
| 88 | +} |
0 commit comments