diff --git a/CodeGeneration/Sources/SyntaxSupport/Child.swift b/CodeGeneration/Sources/SyntaxSupport/Child.swift index c3c7df17fb1..d893117a399 100644 --- a/CodeGeneration/Sources/SyntaxSupport/Child.swift +++ b/CodeGeneration/Sources/SyntaxSupport/Child.swift @@ -80,7 +80,7 @@ public enum ChildKind { /// A child of a node, that may be declared optional or a token with a /// restricted subset of acceptable kinds or texts. -public class Child: IdentifierConvertible { +public class Child: NodeChoiceConvertible { /// The name of the child. /// /// The first character of the name is always uppercase. @@ -97,8 +97,6 @@ public class Child: IdentifierConvertible { /// Whether this child is optional and can be `nil`. public let isOptional: Bool - /// The experimental feature the child represents, or `nil` if this isn't - /// for an experimental feature. public let experimentalFeature: ExperimentalFeature? /// A name of this child that can be shown in diagnostics. @@ -129,10 +127,6 @@ public class Child: IdentifierConvertible { /// The first line of the child's documentation public let documentationAbstract: String - /// If `true`, this is for an experimental language feature, and any public - /// API generated should be SPI. - public var isExperimental: Bool { experimentalFeature != nil } - public var syntaxNodeKind: SyntaxNodeKind { switch kind { case .node(kind: let kind): diff --git a/CodeGeneration/Sources/SyntaxSupport/Node.swift b/CodeGeneration/Sources/SyntaxSupport/Node.swift index eac32b2e002..3205f0425f7 100644 --- a/CodeGeneration/Sources/SyntaxSupport/Node.swift +++ b/CodeGeneration/Sources/SyntaxSupport/Node.swift @@ -21,7 +21,7 @@ import SwiftSyntax /// but fixed types. /// - Collection nodes contains an arbitrary number of children but all those /// children are of the same type. -public class Node: IdentifierConvertible { +public class Node: NodeChoiceConvertible { fileprivate enum Data { case layout(children: [Child], traits: [String]) case collection(choices: [SyntaxNodeKind]) @@ -40,8 +40,6 @@ public class Node: IdentifierConvertible { /// The kind of node’s supertype. This kind must have `isBase == true` public let base: SyntaxNodeKind - /// The experimental feature the node is part of, or `nil` if this isn't - /// for an experimental feature. public let experimentalFeature: ExperimentalFeature? /// When the node name is printed for diagnostics, this name is used. @@ -57,9 +55,9 @@ public class Node: IdentifierConvertible { /// function that should be invoked to create this node. public let parserFunction: TokenSyntax? - /// If `true`, this is for an experimental language feature, and any public - /// API generated should be SPI. - public var isExperimental: Bool { experimentalFeature != nil } + public var syntaxNodeKind: SyntaxNodeKind { + self.kind + } /// A name for this node as an identifier. public var identifier: TokenSyntax { @@ -112,14 +110,8 @@ public class Node: IdentifierConvertible { return attrList.with(\.trailingTrivia, attrList.isEmpty ? [] : .newline) } - /// The documentation note to print for an experimental feature. - public var experimentalDocNote: SwiftSyntax.Trivia { - let comment = experimentalFeature.map { - """ - - Experiment: Requires experimental feature `\($0.token)`. - """ - } - return SwiftSyntax.Trivia.docCommentTrivia(from: comment) + public var apiAttributes: AttributeListSyntax { + self.apiAttributes() } /// Construct the specification for a layout syntax node. diff --git a/CodeGeneration/Sources/SyntaxSupport/NodeChoiceConvertible.swift b/CodeGeneration/Sources/SyntaxSupport/NodeChoiceConvertible.swift new file mode 100644 index 00000000000..386ac90e804 --- /dev/null +++ b/CodeGeneration/Sources/SyntaxSupport/NodeChoiceConvertible.swift @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import SwiftSyntax + +/// Instances of a conforming type should provide necessary information for generating code of a node choice. +public protocol NodeChoiceConvertible: IdentifierConvertible { + /// A docc comment describing the syntax node convertible, including the trivia provided when + /// initializing the syntax node convertible, and the list of possible token choices inferred automatically. + var documentation: SwiftSyntax.Trivia { get } + + /// The experimental feature the syntax node convertible represents, or `nil` if this isn't + /// for an experimental feature. + var experimentalFeature: ExperimentalFeature? { get } + + /// The attributes that should be printed on any API for the syntax node convertible. + /// + /// This is typically used to mark APIs as SPI when the keyword is part of + /// an experimental language feature. + var apiAttributes: AttributeListSyntax { get } + + /// The kind of the syntax node convertible. + var syntaxNodeKind: SyntaxNodeKind { get } +} + +public extension NodeChoiceConvertible { + /// The documentation note to print for an experimental feature. + var experimentalDocNote: SwiftSyntax.Trivia { + guard let experimentalFeature else { + return [] + } + return .docCommentTrivia(from: "- Note: Requires experimental feature `\(experimentalFeature.token)`.") + } + + /// If `true`, this is for an experimental language feature, and any public + /// API generated should be SPI. + var isExperimental: Bool { + self.experimentalFeature != nil + } +} diff --git a/CodeGeneration/Sources/SyntaxSupport/RawSyntaxNodeKind.swift b/CodeGeneration/Sources/SyntaxSupport/RawSyntaxNodeKind.swift new file mode 100644 index 00000000000..ded2446914e --- /dev/null +++ b/CodeGeneration/Sources/SyntaxSupport/RawSyntaxNodeKind.swift @@ -0,0 +1,43 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import SwiftSyntax + +/// A wrapper of ``SyntaxNodeKind`` providing syntax type information for the raw side. +public struct RawSyntaxNodeKind: TypeConvertible { + public var syntaxNodeKind: SyntaxNodeKind + + public var isBase: Bool { + self.syntaxNodeKind.isBase + } + + public var syntaxType: TypeSyntax { + "Raw\(self.syntaxNodeKind.syntaxType)" + } + + public var protocolType: TypeSyntax { + switch self { + case .syntax, .syntaxCollection: + return "RawSyntaxNodeProtocol" + default: + return "\(self.syntaxType)NodeProtocol" + } + } + + public var isAvailableInDocc: Bool { + false + } + + public static func ~= (lhs: SyntaxNodeKind, rhs: Self) -> Bool { + lhs == rhs.syntaxNodeKind + } +} diff --git a/CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKind.swift b/CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKind.swift index 4d67096d0cb..aca5c52aa86 100644 --- a/CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKind.swift +++ b/CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKind.swift @@ -17,7 +17,7 @@ import SwiftSyntaxBuilder /// /// Using the cases of this enum, children of syntax nodes can refer the syntax /// node that defines their layout. -public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible { +public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible, TypeConvertible { // Please keep this list sorted alphabetically case _canImportExpr @@ -327,7 +327,6 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible { } } - /// Whether this is one of the syntax base nodes. public var isBase: Bool { switch self { case .decl, .expr, .pattern, .stmt, .syntax, .syntaxCollection, .type: @@ -337,12 +336,10 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible { } } - /// A name for this node as an identifier. public var identifier: TokenSyntax { return .identifier(rawValue) } - /// The type name of this node in the SwiftSyntax module. public var syntaxType: TypeSyntax { switch self { case .syntax: @@ -354,7 +351,6 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible { } } - /// Whether the node is public API and not underscored/deprecated and can thus be referenced in docc links. public var isAvailableInDocc: Bool { if let node = SYNTAX_NODE_MAP[self], node.isExperimental { return false @@ -365,39 +361,10 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible { } } - /// If this node is non-experimental a docc link wrapped in two backticks. - /// - /// For experimental nodes, the node's type name in code font. - public var doccLink: String { - if isAvailableInDocc { - return "``\(syntaxType)``" - } else { - return "`\(syntaxType)`" - } - } - - /// For base nodes, the name of the corresponding protocol to which all the - /// concrete nodes that have this base kind, conform. public var protocolType: TypeSyntax { return "\(syntaxType)Protocol" } - /// The name of this node at the `RawSyntax` level. - public var rawType: TypeSyntax { - return "Raw\(syntaxType)" - } - - /// For base nodes, the name of the corresponding raw protocol to which all the - /// concrete raw nodes that have this base kind, conform. - public var rawProtocolType: TypeSyntax { - switch self { - case .syntax, .syntaxCollection: - return "RawSyntaxNodeProtocol" - default: - return "Raw\(raw: rawValue.withFirstCharacterUppercased)SyntaxNodeProtocol" - } - } - /// For base node types, generates the name of the protocol to which all /// concrete leaf nodes that derive from this base kind should conform. /// @@ -507,4 +474,8 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible { AttributeSyntax(#"@available(*, deprecated, renamed: "\#(syntaxType)")"#) } } + + public var raw: RawSyntaxNodeKind { + RawSyntaxNodeKind(syntaxNodeKind: self) + } } diff --git a/CodeGeneration/Sources/SyntaxSupport/TypeConvertible.swift b/CodeGeneration/Sources/SyntaxSupport/TypeConvertible.swift new file mode 100644 index 00000000000..d783c77f705 --- /dev/null +++ b/CodeGeneration/Sources/SyntaxSupport/TypeConvertible.swift @@ -0,0 +1,50 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import SwiftSyntax + +/// Instances of a conforming type should provide syntax type information to be used in code generation. +public protocol TypeConvertible { + /// Whether this is one of the syntax base nodes. + var isBase: Bool { + get + } + + /// The type name of this node in the SwiftSyntax module. + var syntaxType: TypeSyntax { + get + } + + /// For base nodes, the name of the corresponding protocol to which all the + /// concrete nodes that have this base kind, conform. + var protocolType: TypeSyntax { + get + } + + /// Whether the node is public API and not underscored/deprecated and can thus be referenced in docc links. + var isAvailableInDocc: Bool { + get + } +} + +public extension TypeConvertible { + /// If this node is non-experimental a docc link wrapped in two backticks. + /// + /// For experimental nodes, the node's type name in code font. + var doccLink: String { + if self.isAvailableInDocc { + return "``\(self.syntaxType)``" + } else { + return "`\(self.syntaxType)`" + } + } +} diff --git a/CodeGeneration/Sources/generate-swift-syntax/ChildNodeChoices.swift b/CodeGeneration/Sources/generate-swift-syntax/ChildNodeChoices.swift new file mode 100644 index 00000000000..f69d43b06e0 --- /dev/null +++ b/CodeGeneration/Sources/generate-swift-syntax/ChildNodeChoices.swift @@ -0,0 +1,181 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import SwiftSyntax +import SwiftSyntaxBuilder +import SyntaxSupport + +struct ChildNodeChoices { + struct Choice: IdentifierConvertible, TypeConvertible { + var documentation: SwiftSyntax.Trivia + + var experimentalDocNote: SwiftSyntax.Trivia + + var apiAttributes: AttributeListSyntax + + var identifier: TokenSyntax + + var isBase: Bool + + var syntaxType: TypeSyntax + + var protocolType: TypeSyntax + + var isAvailableInDocc: Bool + + init(_ node: some NodeChoiceConvertible, forRaw: Bool) { + self.documentation = node.documentation + self.experimentalDocNote = node.experimentalDocNote + self.apiAttributes = node.apiAttributes + self.identifier = node.identifier + let type: any TypeConvertible = forRaw ? node.syntaxNodeKind.raw : node.syntaxNodeKind + self.isBase = type.isBase + self.syntaxType = type.syntaxType + self.protocolType = type.protocolType + self.isAvailableInDocc = type.isAvailableInDocc + } + } + + let name: TypeSyntax + let choices: [Choice] + + func isKindOfFuncDecl(parameterName: TokenSyntax, parameterType: TypeSyntax) -> FunctionDeclSyntax { + try! FunctionDeclSyntax("public static func isKindOf(_ \(parameterName): \(parameterType)) -> Bool") { + ExprSyntax( + "\(raw: self.choices.map { "\($0.syntaxType).isKindOf(\(parameterName))" }.joined(separator: " || "))" + ) + } + } + + func syntaxGetter(propertyName: TokenSyntax, propertyType: TypeSyntax) -> VariableDeclSyntax { + try! VariableDeclSyntax("public var \(propertyName.declNameOrVarCallName): \(propertyType)") { + try! SwitchExprSyntax("switch self") { + for choice in self.choices { + SwitchCaseSyntax("case .\(choice.enumCaseCallName)(let node):") { + StmtSyntax("return node.\(propertyName.nonVarCallNameOrLabelDeclName)") + } + } + } + } + } + + func syntaxInitDecl(inputType: TypeSyntax) -> InitializerDeclSyntax { + try! InitializerDeclSyntax("public init?(_ node: \(inputType))") { + self.choices.ifExpr + } + } +} + +extension Node { + func childrenNodeChoices(forRaw: Bool = false) -> [ChildNodeChoices] { + if let node = self.layoutNode { + return node.children.compactMap { child in + switch child.kind { + case .nodeChoices(let choices): + return ChildNodeChoices( + name: child.syntaxChoicesType, + choices: choices.map { ChildNodeChoices.Choice($0, forRaw: forRaw) } + ) + default: + return nil + } + } + } else if let node = self.collectionNode, node.elementChoices.count > 1 { + return [ + ChildNodeChoices( + name: "Element", + choices: node.elementChoices.map { + ChildNodeChoices.Choice(SYNTAX_NODE_MAP[$0]!, forRaw: forRaw) + } + ) + ] + } else { + return [] + } + } +} + +extension ChildNodeChoices.Choice { + var enumCaseDecl: EnumCaseDeclSyntax { + try! EnumCaseDeclSyntax( + """ + \(self.documentation)\ + \(self.experimentalDocNote)\ + \(self.apiAttributes)\ + case \(self.enumCaseDeclName)(\(self.syntaxType)) + """ + ) + } + + func baseTypeInitDecl(hasArgumentName: Bool) -> InitializerDeclSyntax? { + guard self.isBase else { + return nil + } + let firstName: SyntaxNodeString + let secondName: TokenSyntax + if hasArgumentName { + firstName = "" + secondName = self.labelDeclName + } else { + firstName = "_ " + secondName = "node" + } + return try! InitializerDeclSyntax( + """ + \(self.experimentalDocNote)\ + \(self.apiAttributes)\ + public init(\(firstName)\(secondName): some \(self.protocolType)) + """ + ) { + ExprSyntax( + "self = .\(self.memberCallName)(\(self.syntaxType)(\(secondName)))" + ) + } + } + + var concreteTypeInitDecl: InitializerDeclSyntax { + try! InitializerDeclSyntax( + """ + \(self.experimentalDocNote)\ + \(self.apiAttributes)\ + public init(_ node: \(self.syntaxType)) + """ + ) { + ExprSyntax("self = .\(self.memberCallName)(node)") + } + } +} + +private extension Array where Element == ChildNodeChoices.Choice { + var ifExpr: IfExprSyntax { + precondition(!self.isEmpty) + var result = try! IfExprSyntax( + "if let node = node.as(\(self.last!.syntaxType).self)", + bodyBuilder: { + ExprSyntax("self = .\(self.last!.memberCallName)(node)") + }, + else: { + ["return nil"] + } + ) + for choice in self.reversed().dropFirst() { + result = try! IfExprSyntax( + "if let node = node.as(\(choice.syntaxType).self)", + bodyBuilder: { + ExprSyntax("self = .\(choice.memberCallName)(node)") + }, + elseIf: result + ) + } + return result + } +} diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxNodesFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxNodesFile.swift index caa3e214dc6..cdc29c9cad5 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxNodesFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxNodesFile.swift @@ -15,30 +15,6 @@ import SwiftSyntaxBuilder import SyntaxSupport import Utils -fileprivate extension Node { - var childrenChoicesEnums: [(name: TypeSyntax, choices: [(caseName: TokenSyntax, kind: SyntaxNodeKind)])] { - let node = self - if let node = node.layoutNode { - return node.children.compactMap { - child -> (name: TypeSyntax, choices: [(caseName: TokenSyntax, kind: SyntaxNodeKind)])? in - switch child.kind { - case .nodeChoices(let choices): - return (child.syntaxChoicesType, choices.map { ($0.identifier, $0.syntaxNodeKind) }) - default: - return nil - } - } - } else if let node = node.collectionNode, node.elementChoices.count > 1 { - let choices = node.elementChoices.map { choice -> (TokenSyntax, SyntaxNodeKind) in - (SYNTAX_NODE_MAP[choice]!.identifier, SYNTAX_NODE_MAP[choice]!.kind) - } - return [("Element", choices)] - } else { - return [] - } - } -} - func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax { return SourceFileSyntax(leadingTrivia: copyrightHeader) { for node in SYNTAX_NODES @@ -49,7 +25,7 @@ func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax { DeclSyntax( """ \(node.apiAttributes(forRaw: true))\ - public protocol \(node.kind.rawType)NodeProtocol: \(node.base.rawProtocolType) {} + public protocol \(node.kind.raw.protocolType): \(node.base.raw.protocolType) {} """ ) } @@ -59,62 +35,11 @@ func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax { try! StructDeclSyntax( """ \(node.apiAttributes(forRaw: true))\ - public struct \(node.kind.rawType): \(node.kind.isBase ? node.kind.rawProtocolType : node.base.rawProtocolType) + public struct \(node.kind.raw.syntaxType): \(node.kind.isBase ? node.kind.raw.protocolType : node.base.raw.protocolType) """ ) { - for (name, choices) in node.childrenChoicesEnums { - try EnumDeclSyntax( - """ - public enum \(name): RawSyntaxNodeProtocol - """ - ) { - for (caseName, kind) in choices { - DeclSyntax("case \(caseName.declNameOrVarCallName)(\(kind.rawType))") - } - - DeclSyntax( - """ - public static func isKindOf(_ raw: RawSyntax) -> Bool { - return \(raw: choices.map { "\($0.kind.rawType).isKindOf(raw)" }.joined(separator: " || ")) - } - """ - ) - - try VariableDeclSyntax("public var raw: RawSyntax") { - try SwitchExprSyntax("switch self") { - for (swiftName, _) in choices { - SwitchCaseSyntax("case .\(swiftName.nonVarCallNameOrLabelDeclName)(let node): return node.raw") - } - } - } - - try InitializerDeclSyntax("public init?(_ other: some RawSyntaxNodeProtocol)") { - for (swiftName, kind) in choices { - StmtSyntax( - """ - if let node = \(kind.rawType)(other) { - self = .\(swiftName.nonVarCallNameOrLabelDeclName)(node) - return - } - """ - ) - } - - StmtSyntax("return nil") - } - - for (swiftName, kind) in choices { - if let choiceNode = SYNTAX_NODE_MAP[kind], choiceNode.kind.isBase { - DeclSyntax( - """ - public init(\(swiftName.nonVarCallNameOrLabelDeclName): some \(choiceNode.kind.rawProtocolType)) { - self = .\(swiftName.nonVarCallNameOrLabelDeclName)(\(choiceNode.kind.rawType)(\(swiftName.declNameOrVarCallName))) - } - """ - ) - } - } - } + for childNodeChoices in node.childrenNodeChoices(forRaw: true) { + childNodeChoices.rawEnumDecl } DeclSyntax( @@ -183,7 +108,7 @@ func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax { if node.kind.isBase { DeclSyntax( """ - public init(_ other: some \(node.kind.rawType)NodeProtocol) { + public init(_ other: some \(node.kind.raw.protocolType)) { self.init(unchecked: other.raw) } """ @@ -191,7 +116,7 @@ func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax { } if let node = node.collectionNode { - let element = node.elementChoices.only != nil ? node.elementChoices.only!.rawType : "Element" + let element = node.elementChoices.only != nil ? node.elementChoices.only!.raw.syntaxType : "Element" DeclSyntax( """ public init(elements: [\(element)], arena: __shared SyntaxArena) { @@ -269,7 +194,7 @@ func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax { ExprSyntax("layoutView.children[\(raw: index)]\(raw: exclamationMark)") } else { ExprSyntax( - "layoutView.children[\(raw: index)].map(\(child.syntaxNodeKind.rawType).init(raw:))\(raw: exclamationMark)" + "layoutView.children[\(raw: index)].map(\(child.syntaxNodeKind.raw.syntaxType).init(raw:))\(raw: exclamationMark)" ) } } @@ -280,6 +205,28 @@ func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax { } } +private extension ChildNodeChoices { + var rawEnumDecl: EnumDeclSyntax { + try! EnumDeclSyntax("public enum \(self.name): RawSyntaxNodeProtocol") { + for choice in self.choices { + choice.enumCaseDecl + } + + self.isKindOfFuncDecl(parameterName: "raw", parameterType: "RawSyntax") + + self.syntaxGetter(propertyName: "raw", propertyType: "RawSyntax") + + self.syntaxInitDecl(inputType: "__shared some RawSyntaxNodeProtocol") + + for choice in self.choices { + if let baseTypeInitDecl = choice.baseTypeInitDecl(hasArgumentName: true) { + baseTypeInitDecl + } + } + } + } +} + fileprivate extension Child { var rawParameterType: TypeSyntax { var paramType: TypeSyntax @@ -291,9 +238,9 @@ fileprivate extension Child { // // we've opted out of providing a default value to the parameter (e.g. `RawExprSyntax?.none`) as a workaround, // as passing an explicit `nil` would prompt developers to think clearly whether this parameter should be parsed - paramType = "some \(syntaxNodeKind.rawProtocolType)" + paramType = "some \(syntaxNodeKind.raw.protocolType)" } else { - paramType = syntaxNodeKind.rawType + paramType = syntaxNodeKind.raw.syntaxType } return buildableType.optionalWrapped(type: paramType) diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxValidationFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxValidationFile.swift index 734dd75c931..edd459d3c0f 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxValidationFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxValidationFile.swift @@ -239,14 +239,16 @@ let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: copyrightHead } else if let node = node.collectionNode { try ForStmtSyntax("for (index, element) in layout.enumerated()") { if let onlyElement = node.elementChoices.only { - ExprSyntax("assertNoError(kind, index, verify(element, as: \(onlyElement.rawType).self))") + ExprSyntax( + "assertNoError(kind, index, verify(element, as: \(onlyElement.raw.syntaxType).self))" + ) } else { let verifiedChoices = ArrayExprSyntax { for choiceName in node.elementChoices { let choice = SYNTAX_NODE_MAP[choiceName]! ArrayElementSyntax( leadingTrivia: .newline, - expression: ExprSyntax("verify(element, as: \(choice.kind.rawType).self)") + expression: ExprSyntax("verify(element, as: \(choice.kind.raw.syntaxType).self)") ) } } diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxCollectionsFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxCollectionsFile.swift index 71e2f12ecae..cfa32fde2c2 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxCollectionsFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxCollectionsFile.swift @@ -34,90 +34,8 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { if let onlyElement = node.elementChoices.only { DeclSyntax("public typealias Element = \(onlyElement.syntaxType)") } else { - try EnumDeclSyntax( - """ - public enum Element: SyntaxChildChoices, SyntaxHashable - """ - ) { - for choiceName in node.elementChoices { - let choice = SYNTAX_NODE_MAP[choiceName]! - DeclSyntax( - """ - \(choice.apiAttributes())\ - case \(choice.enumCaseDeclName)(\(choice.kind.syntaxType)) - """ - ) - } - - try VariableDeclSyntax("public var _syntaxNode: Syntax") { - SwitchExprSyntax(switchKeyword: .keyword(.switch), subject: ExprSyntax("self")) { - for choiceName in node.elementChoices { - let choice = SYNTAX_NODE_MAP[choiceName]! - SwitchCaseSyntax("case .\(choice.enumCaseCallName)(let node):") { - StmtSyntax("return node._syntaxNode") - } - } - } - } - - for choiceName in node.elementChoices { - let choiceNode = SYNTAX_NODE_MAP[choiceName]! - if choiceNode.kind.isBase { - DeclSyntax( - """ - \(choiceNode.apiAttributes())\ - public init(_ node: some \(choiceNode.kind.protocolType)) { - self = .\(choiceNode.memberCallName)(\(choiceNode.kind.syntaxType)(node)) - } - """ - ) - - } else { - DeclSyntax( - """ - \(choiceNode.apiAttributes())\ - public init(_ node: \(choiceNode.kind.syntaxType)) { - self = .\(choiceNode.memberCallName)(node) - } - """ - ) - } - } - - try InitializerDeclSyntax("public init?(_ node: some SyntaxProtocol)") { - for choiceName in node.elementChoices { - let choiceNode = SYNTAX_NODE_MAP[choiceName]! - StmtSyntax( - """ - if let node = node.as(\(choiceNode.kind.syntaxType).self) { - self = .\(choiceNode.memberCallName)(node) - return - } - """ - ) - } - - StmtSyntax("return nil") - } - - try VariableDeclSyntax("public static var structure: SyntaxNodeStructure") { - let choices = ArrayExprSyntax { - for choiceName in node.elementChoices { - let choice = SYNTAX_NODE_MAP[choiceName]! - ArrayElementSyntax( - leadingTrivia: .newline, - expression: ExprSyntax(".node(\(choice.kind.syntaxType).self)") - ) - } - } - - StmtSyntax("return .choices(\(choices))") - } - - for choiceNodeName in node.elementChoices { - let choiceNode = SYNTAX_NODE_MAP[choiceNodeName]! - choiceNodeCastingMethods(for: choiceNode.kind) - } + for childNodeChoices in node.node.childrenNodeChoices() { + childNodeChoices.enumDecl } } diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodeCasting.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodeCasting.swift index 8afa15e6f29..fd4978f886b 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodeCasting.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodeCasting.swift @@ -14,82 +14,89 @@ import SwiftSyntax import SwiftSyntaxBuilder import SyntaxSupport -@MemberBlockItemListBuilder -func choiceNodeCastingMethods(for syntaxNodeKind: SyntaxNodeKind) -> MemberBlockItemListSyntax { - let apiAttributes = SYNTAX_NODE_MAP[syntaxNodeKind]?.apiAttributes() ?? [] - if syntaxNodeKind.isBase { - DeclSyntax( - """ - /// Checks if the current syntax node can be cast to the type conforming to the ``\(syntaxNodeKind.protocolType)`` protocol. - /// - /// - Returns: `true` if the node can be cast, `false` otherwise. - \(apiAttributes)\ - public func `is`(_ syntaxType: S.Type) -> Bool { - return self.as(syntaxType) != nil - } - """ - ) +extension ChildNodeChoices.Choice { + @MemberBlockItemListBuilder + var castingMethods: MemberBlockItemListSyntax { + if self.isBase { + DeclSyntax( + """ + /// Checks if the current syntax node can be cast to the type conforming to the ``\(self.protocolType)`` protocol. + /// + /// - Returns: `true` if the node can be cast, `false` otherwise. + \(self.experimentalDocNote)\ + \(self.apiAttributes)\ + public func `is`(_ syntaxType: (some \(self.protocolType)).Type) -> Bool { + return self.as(syntaxType) != nil + } + """ + ) - DeclSyntax( - """ - /// Attempts to cast the current syntax node to the type conforming to the ``\(syntaxNodeKind.protocolType)`` protocol. - /// - /// - Returns: An instance of the specialized type, or `nil` if the cast fails. - \(apiAttributes)\ - public func `as`(_ syntaxType: S.Type) -> S? { - return S.init(self) - } - """ - ) + DeclSyntax( + """ + /// Attempts to cast the current syntax node to the type conforming to the ``\(self.protocolType)`` protocol. + /// + /// - Returns: An instance of the specialized type, or `nil` if the cast fails. + \(self.experimentalDocNote)\ + \(self.apiAttributes)\ + public func `as`(_ syntaxType: S.Type) -> S? { + return S.init(self) + } + """ + ) - DeclSyntax( - """ - /// Force-casts the current syntax node to the type conforming to the ``\(syntaxNodeKind.protocolType)`` protocol. - /// - /// - Returns: An instance of the specialized type. - /// - Warning: This function will crash if the cast is not possible. Use `as` to safely attempt a cast. - \(apiAttributes)\ - public func cast(_ syntaxType: S.Type) -> S { - return self.as(S.self)! - } - """ - ) - } else { - DeclSyntax( - """ - /// Checks if the current syntax node can be cast to \(raw: syntaxNodeKind.doccLink). - /// - /// - Returns: `true` if the node can be cast, `false` otherwise. - \(apiAttributes)\ - public func `is`(_ syntaxType: \(syntaxNodeKind.syntaxType).Type) -> Bool { - return self.as(syntaxType) != nil - } - """ - ) + DeclSyntax( + """ + /// Force-casts the current syntax node to the type conforming to the ``\(self.protocolType)`` protocol. + /// + /// - Returns: An instance of the specialized type. + /// - Warning: This function will crash if the cast is not possible. Use `as` to safely attempt a cast. + \(self.experimentalDocNote)\ + \(self.apiAttributes)\ + public func cast(_ syntaxType: S.Type) -> S { + return self.as(S.self)! + } + """ + ) + } else { + DeclSyntax( + """ + /// Checks if the current syntax node can be cast to \(raw: self.doccLink). + /// + /// - Returns: `true` if the node can be cast, `false` otherwise. + \(self.experimentalDocNote)\ + \(self.apiAttributes)\ + public func `is`(_ syntaxType: \(self.syntaxType).Type) -> Bool { + return self.as(syntaxType) != nil + } + """ + ) - DeclSyntax( - """ - /// Attempts to cast the current syntax node to \(raw: syntaxNodeKind.doccLink). - /// - /// - Returns: An instance of \(raw: syntaxNodeKind.doccLink), or `nil` if the cast fails. - \(apiAttributes)\ - public func `as`(_ syntaxType: \(syntaxNodeKind.syntaxType).Type) -> \(syntaxNodeKind.syntaxType)? { - return \(syntaxNodeKind.syntaxType).init(self) - } - """ - ) + DeclSyntax( + """ + /// Attempts to cast the current syntax node to \(raw: self.doccLink). + /// + /// - Returns: An instance of \(raw: self.doccLink), or `nil` if the cast fails. + \(self.experimentalDocNote)\ + \(self.apiAttributes)\ + public func `as`(_ syntaxType: \(self.syntaxType).Type) -> \(self.syntaxType)? { + return \(self.syntaxType).init(self) + } + """ + ) - DeclSyntax( - """ - /// Force-casts the current syntax node to \(raw: syntaxNodeKind.doccLink). - /// - /// - Returns: An instance of \(raw: syntaxNodeKind.doccLink). - /// - Warning: This function will crash if the cast is not possible. Use `as` to safely attempt a cast. - \(apiAttributes)\ - public func cast(_ syntaxType: \(syntaxNodeKind.syntaxType).Type) -> \(syntaxNodeKind.syntaxType) { - return self.as(\(syntaxNodeKind.syntaxType).self)! - } - """ - ) + DeclSyntax( + """ + /// Force-casts the current syntax node to \(raw: self.doccLink). + /// + /// - Returns: An instance of \(raw: self.doccLink). + /// - Warning: This function will crash if the cast is not possible. Use `as` to safely attempt a cast. + \(self.experimentalDocNote)\ + \(self.apiAttributes)\ + public func cast(_ syntaxType: \(self.syntaxType).Type) -> \(self.syntaxType) { + return self.as(\(self.syntaxType).self)! + } + """ + ) + } } } diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodesFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodesFile.swift index 57f4f28d9f9..79770fa77eb 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodesFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodesFile.swift @@ -33,10 +33,8 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax { public struct \(node.kind.syntaxType): \(node.baseType.syntaxBaseName)Protocol, SyntaxHashable, \(node.base.leafProtocolType) """ ) { - for child in node.children { - if let childChoiceDecl = try! generateSyntaxChildChoices(for: child) { - childChoiceDecl - } + for childNodeChoices in node.node.childrenNodeChoices() { + childNodeChoices.enumDecl } // ============== @@ -212,76 +210,36 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax { } } -private func generateSyntaxChildChoices(for child: Child) throws -> EnumDeclSyntax? { - guard case .nodeChoices(let choices) = child.kind else { - return nil - } +extension ChildNodeChoices { + var enumDecl: EnumDeclSyntax { + try! EnumDeclSyntax("public enum \(self.name): SyntaxChildChoices, SyntaxHashable") { + for choice in self.choices { + choice.enumCaseDecl + } - return try! EnumDeclSyntax("public enum \(child.syntaxChoicesType): SyntaxChildChoices, SyntaxHashable") { - for choice in choices { - DeclSyntax("case \(choice.enumCaseDeclName)(\(choice.syntaxNodeKind.syntaxType))") - } + self.syntaxGetter(propertyName: "_syntaxNode", propertyType: "Syntax") - try! VariableDeclSyntax("public var _syntaxNode: Syntax") { - try! SwitchExprSyntax("switch self") { - for choice in choices { - SwitchCaseSyntax("case .\(choice.enumCaseCallName)(let node):") { - StmtSyntax("return node._syntaxNode") - } - } + for choice in self.choices { + choice.baseTypeInitDecl(hasArgumentName: false) ?? choice.concreteTypeInitDecl } - } - for choice in choices { - if let choiceNode = SYNTAX_NODE_MAP[choice.syntaxNodeKind], choiceNode.kind.isBase { - DeclSyntax( - """ - public init(_ node: some \(choiceNode.kind.protocolType)) { - self = .\(choice.enumCaseCallName)(\(choiceNode.kind.syntaxType)(node)) - } - """ - ) + self.syntaxInitDecl(inputType: "__shared some SyntaxProtocol") - } else { - DeclSyntax( - """ - public init(_ node: \(choice.syntaxNodeKind.syntaxType)) { - self = .\(choice.enumCaseCallName)(node) + try! VariableDeclSyntax("public static var structure: SyntaxNodeStructure") { + let choices = ArrayExprSyntax { + for choice in self.choices { + ArrayElementSyntax( + expression: ExprSyntax(".node(\(choice.syntaxType).self)") + ) } - """ - ) - } - } + } - try! InitializerDeclSyntax("public init?(_ node: __shared some SyntaxProtocol)") { - for choice in choices { - StmtSyntax( - """ - if let node = node.as(\(choice.syntaxNodeKind.syntaxType).self) { - self = .\(choice.enumCaseCallName)(node) - return - } - """ - ) + StmtSyntax("return .choices(\(choices))") } - StmtSyntax("return nil") - } - - try! VariableDeclSyntax("public static var structure: SyntaxNodeStructure") { - let choices = ArrayExprSyntax { - for choice in choices { - ArrayElementSyntax( - expression: ExprSyntax(".node(\(choice.syntaxNodeKind.syntaxType).self)") - ) - } + for choice in self.choices { + choice.castingMethods } - - StmtSyntax("return .choices(\(choices))") - } - - for choiceNode in choices { - choiceNodeCastingMethods(for: choiceNode.syntaxNodeKind) } } } diff --git a/Sources/SwiftSyntax/generated/SyntaxCollections.swift b/Sources/SwiftSyntax/generated/SyntaxCollections.swift index 31ce42a68c7..a2a9e02eea7 100644 --- a/Sources/SwiftSyntax/generated/SyntaxCollections.swift +++ b/Sources/SwiftSyntax/generated/SyntaxCollections.swift @@ -104,6 +104,7 @@ public struct ArrayElementListSyntax: SyntaxCollection, SyntaxHashable { /// - ``VariableDeclSyntax``.``VariableDeclSyntax/attributes`` public struct AttributeListSyntax: SyntaxCollection, SyntaxHashable { public enum Element: SyntaxChildChoices, SyntaxHashable { + /// An `@` attribute. case attribute(AttributeSyntax) case ifConfigDecl(IfConfigDeclSyntax) @@ -124,22 +125,18 @@ public struct AttributeListSyntax: SyntaxCollection, SyntaxHashable { self = .ifConfigDecl(node) } - public init?(_ node: some SyntaxProtocol) { + public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(AttributeSyntax.self) { self = .attribute(node) - return - } - if let node = node.as(IfConfigDeclSyntax.self) { + } else if let node = node.as(IfConfigDeclSyntax.self) { self = .ifConfigDecl(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { - return .choices([ - .node(AttributeSyntax.self), - .node(IfConfigDeclSyntax.self)]) + return .choices([.node(AttributeSyntax.self), .node(IfConfigDeclSyntax.self)]) } /// Checks if the current syntax node can be cast to ``AttributeSyntax``. @@ -887,7 +884,7 @@ public struct LabeledExprListSyntax: SyntaxCollection, SyntaxHashable { public static let syntaxKind = SyntaxKind.labeledExprList } -/// - Experiment: Requires experimental feature `nonescapableTypes`. +/// - Note: Requires experimental feature `nonescapableTypes`. /// /// ### Children /// @@ -1034,8 +1031,11 @@ public struct PlatformVersionItemListSyntax: SyntaxCollection, SyntaxHashable { /// - ``PrecedenceGroupDeclSyntax``.``PrecedenceGroupDeclSyntax/groupAttributes`` public struct PrecedenceGroupAttributeListSyntax: SyntaxCollection, SyntaxHashable { public enum Element: SyntaxChildChoices, SyntaxHashable { + /// Specify the new precedence group's relation to existing precedence groups. case precedenceGroupRelation(PrecedenceGroupRelationSyntax) + /// Specifies the precedence of an operator when used in an operation that includes optional chaining. case precedenceGroupAssignment(PrecedenceGroupAssignmentSyntax) + /// Specifies how a sequence of operators with the same precedence level are grouped together in the absence of grouping parentheses. case precedenceGroupAssociativity(PrecedenceGroupAssociativitySyntax) public var _syntaxNode: Syntax { @@ -1061,27 +1061,20 @@ public struct PrecedenceGroupAttributeListSyntax: SyntaxCollection, SyntaxHashab self = .precedenceGroupAssociativity(node) } - public init?(_ node: some SyntaxProtocol) { + public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(PrecedenceGroupRelationSyntax.self) { self = .precedenceGroupRelation(node) - return - } - if let node = node.as(PrecedenceGroupAssignmentSyntax.self) { + } else if let node = node.as(PrecedenceGroupAssignmentSyntax.self) { self = .precedenceGroupAssignment(node) - return - } - if let node = node.as(PrecedenceGroupAssociativitySyntax.self) { + } else if let node = node.as(PrecedenceGroupAssociativitySyntax.self) { self = .precedenceGroupAssociativity(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { - return .choices([ - .node(PrecedenceGroupRelationSyntax.self), - .node(PrecedenceGroupAssignmentSyntax.self), - .node(PrecedenceGroupAssociativitySyntax.self)]) + return .choices([.node(PrecedenceGroupRelationSyntax.self), .node(PrecedenceGroupAssignmentSyntax.self), .node(PrecedenceGroupAssociativitySyntax.self)]) } /// Checks if the current syntax node can be cast to ``PrecedenceGroupRelationSyntax``. @@ -1242,9 +1235,13 @@ public struct SimpleStringLiteralSegmentListSyntax: SyntaxCollection, SyntaxHash /// - ``AttributeSyntax``.``AttributeSyntax/arguments`` public struct SpecializeAttributeArgumentListSyntax: SyntaxCollection, SyntaxHashable { public enum Element: SyntaxChildChoices, SyntaxHashable { + /// A labeled argument for the `@_specialize` attribute like `exported: true` case labeledSpecializeArgument(LabeledSpecializeArgumentSyntax) + /// The availability argument for the _specialize attribute case specializeAvailabilityArgument(SpecializeAvailabilityArgumentSyntax) + /// A labeled argument for the `@_specialize` attribute with a function decl value like `target: myFunc(_:)` case specializeTargetFunctionArgument(SpecializeTargetFunctionArgumentSyntax) + /// A `where` clause that places additional constraints on generic parameters like `where Element: Hashable`. case genericWhereClause(GenericWhereClauseSyntax) public var _syntaxNode: Syntax { @@ -1276,31 +1273,25 @@ public struct SpecializeAttributeArgumentListSyntax: SyntaxCollection, SyntaxHas self = .genericWhereClause(node) } - public init?(_ node: some SyntaxProtocol) { + public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(LabeledSpecializeArgumentSyntax.self) { self = .labeledSpecializeArgument(node) - return - } - if let node = node.as(SpecializeAvailabilityArgumentSyntax.self) { + } else if let node = node.as(SpecializeAvailabilityArgumentSyntax.self) { self = .specializeAvailabilityArgument(node) - return - } - if let node = node.as(SpecializeTargetFunctionArgumentSyntax.self) { + } else if let node = node.as(SpecializeTargetFunctionArgumentSyntax.self) { self = .specializeTargetFunctionArgument(node) - return - } - if let node = node.as(GenericWhereClauseSyntax.self) { + } else if let node = node.as(GenericWhereClauseSyntax.self) { self = .genericWhereClause(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { return .choices([ - .node(LabeledSpecializeArgumentSyntax.self), - .node(SpecializeAvailabilityArgumentSyntax.self), - .node(SpecializeTargetFunctionArgumentSyntax.self), + .node(LabeledSpecializeArgumentSyntax.self), + .node(SpecializeAvailabilityArgumentSyntax.self), + .node(SpecializeTargetFunctionArgumentSyntax.self), .node(GenericWhereClauseSyntax.self) ]) } @@ -1415,7 +1406,13 @@ public struct SpecializeAttributeArgumentListSyntax: SyntaxCollection, SyntaxHas /// - ``StringLiteralExprSyntax``.``StringLiteralExprSyntax/segments`` public struct StringLiteralSegmentListSyntax: SyntaxCollection, SyntaxHashable { public enum Element: SyntaxChildChoices, SyntaxHashable { + /// A literal segment inside a string segment. + /// + /// - SeeAlso: ``ExpressionSegmentSyntax`` case stringSegment(StringSegmentSyntax) + /// An interpolated expression inside a string literal. + /// + /// - SeeAlso: ``StringSegmentSyntax`` case expressionSegment(ExpressionSegmentSyntax) public var _syntaxNode: Syntax { @@ -1435,22 +1432,18 @@ public struct StringLiteralSegmentListSyntax: SyntaxCollection, SyntaxHashable { self = .expressionSegment(node) } - public init?(_ node: some SyntaxProtocol) { + public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(StringSegmentSyntax.self) { self = .stringSegment(node) - return - } - if let node = node.as(ExpressionSegmentSyntax.self) { + } else if let node = node.as(ExpressionSegmentSyntax.self) { self = .expressionSegment(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { - return .choices([ - .node(StringSegmentSyntax.self), - .node(ExpressionSegmentSyntax.self)]) + return .choices([.node(StringSegmentSyntax.self), .node(ExpressionSegmentSyntax.self)]) } /// Checks if the current syntax node can be cast to ``StringSegmentSyntax``. @@ -1562,22 +1555,18 @@ public struct SwitchCaseListSyntax: SyntaxCollection, SyntaxHashable { self = .ifConfigDecl(node) } - public init?(_ node: some SyntaxProtocol) { + public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(SwitchCaseSyntax.self) { self = .switchCase(node) - return - } - if let node = node.as(IfConfigDeclSyntax.self) { + } else if let node = node.as(IfConfigDeclSyntax.self) { self = .ifConfigDecl(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { - return .choices([ - .node(SwitchCaseSyntax.self), - .node(IfConfigDeclSyntax.self)]) + return .choices([.node(SwitchCaseSyntax.self), .node(IfConfigDeclSyntax.self)]) } /// Checks if the current syntax node can be cast to ``SwitchCaseSyntax``. @@ -1693,7 +1682,10 @@ public struct TupleTypeElementListSyntax: SyntaxCollection, SyntaxHashable { /// - ``AttributedTypeSyntax``.``AttributedTypeSyntax/specifiers`` public struct TypeSpecifierListSyntax: SyntaxCollection, SyntaxHashable { public enum Element: SyntaxChildChoices, SyntaxHashable { + /// A specifier that can be attached to a type to eg. mark a parameter as `inout` or `consuming` case simpleTypeSpecifier(SimpleTypeSpecifierSyntax) + /// A specifier that specifies function parameter on whose lifetime a type depends + /// - Note: Requires experimental feature `nonescapableTypes`. #if compiler(>=5.8) @_spi(ExperimentalLanguageFeatures) #endif @@ -1712,6 +1704,7 @@ public struct TypeSpecifierListSyntax: SyntaxCollection, SyntaxHashable { self = .simpleTypeSpecifier(node) } + /// - Note: Requires experimental feature `nonescapableTypes`. #if compiler(>=5.8) @_spi(ExperimentalLanguageFeatures) #endif @@ -1719,22 +1712,18 @@ public struct TypeSpecifierListSyntax: SyntaxCollection, SyntaxHashable { self = .lifetimeTypeSpecifier(node) } - public init?(_ node: some SyntaxProtocol) { + public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(SimpleTypeSpecifierSyntax.self) { self = .simpleTypeSpecifier(node) - return - } - if let node = node.as(LifetimeTypeSpecifierSyntax.self) { + } else if let node = node.as(LifetimeTypeSpecifierSyntax.self) { self = .lifetimeTypeSpecifier(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { - return .choices([ - .node(SimpleTypeSpecifierSyntax.self), - .node(LifetimeTypeSpecifierSyntax.self)]) + return .choices([.node(SimpleTypeSpecifierSyntax.self), .node(LifetimeTypeSpecifierSyntax.self)]) } /// Checks if the current syntax node can be cast to ``SimpleTypeSpecifierSyntax``. @@ -1762,6 +1751,7 @@ public struct TypeSpecifierListSyntax: SyntaxCollection, SyntaxHashable { /// Checks if the current syntax node can be cast to `LifetimeTypeSpecifierSyntax`. /// /// - Returns: `true` if the node can be cast, `false` otherwise. + /// - Note: Requires experimental feature `nonescapableTypes`. #if compiler(>=5.8) @_spi(ExperimentalLanguageFeatures) #endif @@ -1772,6 +1762,7 @@ public struct TypeSpecifierListSyntax: SyntaxCollection, SyntaxHashable { /// Attempts to cast the current syntax node to `LifetimeTypeSpecifierSyntax`. /// /// - Returns: An instance of `LifetimeTypeSpecifierSyntax`, or `nil` if the cast fails. + /// - Note: Requires experimental feature `nonescapableTypes`. #if compiler(>=5.8) @_spi(ExperimentalLanguageFeatures) #endif @@ -1783,6 +1774,7 @@ public struct TypeSpecifierListSyntax: SyntaxCollection, SyntaxHashable { /// /// - Returns: An instance of `LifetimeTypeSpecifierSyntax`. /// - Warning: This function will crash if the cast is not possible. Use `as` to safely attempt a cast. + /// - Note: Requires experimental feature `nonescapableTypes`. #if compiler(>=5.8) @_spi(ExperimentalLanguageFeatures) #endif diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesAB.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesAB.swift index b5bdff870e5..fb241124488 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesAB.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesAB.swift @@ -19,7 +19,7 @@ public struct RawAccessorBlockSyntax: RawSyntaxNodeProtocol { case getter(RawCodeBlockItemListSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawAccessorDeclListSyntax.isKindOf(raw) || RawCodeBlockItemListSyntax.isKindOf(raw) + RawAccessorDeclListSyntax.isKindOf(raw) || RawCodeBlockItemListSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -31,16 +31,14 @@ public struct RawAccessorBlockSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawAccessorDeclListSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawAccessorDeclListSyntax.self) { self = .accessors(node) - return - } - if let node = RawCodeBlockItemListSyntax(other) { + } else if let node = node.as(RawCodeBlockItemListSyntax.self) { self = .getter(node) - return + } else { + return nil } - return nil } } @@ -1225,11 +1223,12 @@ public struct RawAssociatedTypeDeclSyntax: RawDeclSyntaxNodeProtocol { @_spi(RawSyntax) public struct RawAttributeListSyntax: RawSyntaxNodeProtocol { public enum Element: RawSyntaxNodeProtocol { + /// An `@` attribute. case attribute(RawAttributeSyntax) case ifConfigDecl(RawIfConfigDeclSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawAttributeSyntax.isKindOf(raw) || RawIfConfigDeclSyntax.isKindOf(raw) + RawAttributeSyntax.isKindOf(raw) || RawIfConfigDeclSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -1241,16 +1240,14 @@ public struct RawAttributeListSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawAttributeSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawAttributeSyntax.self) { self = .attribute(node) - return - } - if let node = RawIfConfigDeclSyntax(other) { + } else if let node = node.as(RawIfConfigDeclSyntax.self) { self = .ifConfigDecl(node) - return + } else { + return nil } - return nil } } @@ -1327,7 +1324,7 @@ public struct RawAttributeSyntax: RawSyntaxNodeProtocol { case documentationArguments(RawDocumentationAttributeArgumentListSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawLabeledExprListSyntax.isKindOf(raw) || RawTokenSyntax.isKindOf(raw) || RawStringLiteralExprSyntax.isKindOf(raw) || RawAvailabilityArgumentListSyntax.isKindOf(raw) || RawSpecializeAttributeArgumentListSyntax.isKindOf(raw) || RawObjCSelectorPieceListSyntax.isKindOf(raw) || RawImplementsAttributeArgumentsSyntax.isKindOf(raw) || RawDifferentiableAttributeArgumentsSyntax.isKindOf(raw) || RawDerivativeAttributeArgumentsSyntax.isKindOf(raw) || RawBackDeployedAttributeArgumentsSyntax.isKindOf(raw) || RawConventionAttributeArgumentsSyntax.isKindOf(raw) || RawConventionWitnessMethodAttributeArgumentsSyntax.isKindOf(raw) || RawOpaqueReturnTypeOfAttributeArgumentsSyntax.isKindOf(raw) || RawExposeAttributeArgumentsSyntax.isKindOf(raw) || RawOriginallyDefinedInAttributeArgumentsSyntax.isKindOf(raw) || RawUnderscorePrivateAttributeArgumentsSyntax.isKindOf(raw) || RawDynamicReplacementAttributeArgumentsSyntax.isKindOf(raw) || RawUnavailableFromAsyncAttributeArgumentsSyntax.isKindOf(raw) || RawEffectsAttributeArgumentListSyntax.isKindOf(raw) || RawDocumentationAttributeArgumentListSyntax.isKindOf(raw) + RawLabeledExprListSyntax.isKindOf(raw) || RawTokenSyntax.isKindOf(raw) || RawStringLiteralExprSyntax.isKindOf(raw) || RawAvailabilityArgumentListSyntax.isKindOf(raw) || RawSpecializeAttributeArgumentListSyntax.isKindOf(raw) || RawObjCSelectorPieceListSyntax.isKindOf(raw) || RawImplementsAttributeArgumentsSyntax.isKindOf(raw) || RawDifferentiableAttributeArgumentsSyntax.isKindOf(raw) || RawDerivativeAttributeArgumentsSyntax.isKindOf(raw) || RawBackDeployedAttributeArgumentsSyntax.isKindOf(raw) || RawConventionAttributeArgumentsSyntax.isKindOf(raw) || RawConventionWitnessMethodAttributeArgumentsSyntax.isKindOf(raw) || RawOpaqueReturnTypeOfAttributeArgumentsSyntax.isKindOf(raw) || RawExposeAttributeArgumentsSyntax.isKindOf(raw) || RawOriginallyDefinedInAttributeArgumentsSyntax.isKindOf(raw) || RawUnderscorePrivateAttributeArgumentsSyntax.isKindOf(raw) || RawDynamicReplacementAttributeArgumentsSyntax.isKindOf(raw) || RawUnavailableFromAsyncAttributeArgumentsSyntax.isKindOf(raw) || RawEffectsAttributeArgumentListSyntax.isKindOf(raw) || RawDocumentationAttributeArgumentListSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -1375,88 +1372,50 @@ public struct RawAttributeSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawLabeledExprListSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawLabeledExprListSyntax.self) { self = .argumentList(node) - return - } - if let node = RawTokenSyntax(other) { + } else if let node = node.as(RawTokenSyntax.self) { self = .token(node) - return - } - if let node = RawStringLiteralExprSyntax(other) { + } else if let node = node.as(RawStringLiteralExprSyntax.self) { self = .string(node) - return - } - if let node = RawAvailabilityArgumentListSyntax(other) { + } else if let node = node.as(RawAvailabilityArgumentListSyntax.self) { self = .availability(node) - return - } - if let node = RawSpecializeAttributeArgumentListSyntax(other) { + } else if let node = node.as(RawSpecializeAttributeArgumentListSyntax.self) { self = .specializeArguments(node) - return - } - if let node = RawObjCSelectorPieceListSyntax(other) { + } else if let node = node.as(RawObjCSelectorPieceListSyntax.self) { self = .objCName(node) - return - } - if let node = RawImplementsAttributeArgumentsSyntax(other) { + } else if let node = node.as(RawImplementsAttributeArgumentsSyntax.self) { self = .implementsArguments(node) - return - } - if let node = RawDifferentiableAttributeArgumentsSyntax(other) { + } else if let node = node.as(RawDifferentiableAttributeArgumentsSyntax.self) { self = .differentiableArguments(node) - return - } - if let node = RawDerivativeAttributeArgumentsSyntax(other) { + } else if let node = node.as(RawDerivativeAttributeArgumentsSyntax.self) { self = .derivativeRegistrationArguments(node) - return - } - if let node = RawBackDeployedAttributeArgumentsSyntax(other) { + } else if let node = node.as(RawBackDeployedAttributeArgumentsSyntax.self) { self = .backDeployedArguments(node) - return - } - if let node = RawConventionAttributeArgumentsSyntax(other) { + } else if let node = node.as(RawConventionAttributeArgumentsSyntax.self) { self = .conventionArguments(node) - return - } - if let node = RawConventionWitnessMethodAttributeArgumentsSyntax(other) { + } else if let node = node.as(RawConventionWitnessMethodAttributeArgumentsSyntax.self) { self = .conventionWitnessMethodArguments(node) - return - } - if let node = RawOpaqueReturnTypeOfAttributeArgumentsSyntax(other) { + } else if let node = node.as(RawOpaqueReturnTypeOfAttributeArgumentsSyntax.self) { self = .opaqueReturnTypeOfAttributeArguments(node) - return - } - if let node = RawExposeAttributeArgumentsSyntax(other) { + } else if let node = node.as(RawExposeAttributeArgumentsSyntax.self) { self = .exposeAttributeArguments(node) - return - } - if let node = RawOriginallyDefinedInAttributeArgumentsSyntax(other) { + } else if let node = node.as(RawOriginallyDefinedInAttributeArgumentsSyntax.self) { self = .originallyDefinedInArguments(node) - return - } - if let node = RawUnderscorePrivateAttributeArgumentsSyntax(other) { + } else if let node = node.as(RawUnderscorePrivateAttributeArgumentsSyntax.self) { self = .underscorePrivateAttributeArguments(node) - return - } - if let node = RawDynamicReplacementAttributeArgumentsSyntax(other) { + } else if let node = node.as(RawDynamicReplacementAttributeArgumentsSyntax.self) { self = .dynamicReplacementArguments(node) - return - } - if let node = RawUnavailableFromAsyncAttributeArgumentsSyntax(other) { + } else if let node = node.as(RawUnavailableFromAsyncAttributeArgumentsSyntax.self) { self = .unavailableFromAsyncArguments(node) - return - } - if let node = RawEffectsAttributeArgumentListSyntax(other) { + } else if let node = node.as(RawEffectsAttributeArgumentListSyntax.self) { self = .effectsArguments(node) - return - } - if let node = RawDocumentationAttributeArgumentListSyntax(other) { + } else if let node = node.as(RawDocumentationAttributeArgumentListSyntax.self) { self = .documentationArguments(node) - return + } else { + return nil } - return nil } } @@ -1699,12 +1658,17 @@ public struct RawAvailabilityArgumentListSyntax: RawSyntaxNodeProtocol { @_spi(RawSyntax) public struct RawAvailabilityArgumentSyntax: RawSyntaxNodeProtocol { public enum Argument: RawSyntaxNodeProtocol { + /// ### Tokens + /// + /// For syntax trees generated by the parser, this is guaranteed to be one of the following kinds: + /// - `` + /// - `` case token(RawTokenSyntax) case availabilityVersionRestriction(RawPlatformVersionSyntax) case availabilityLabeledArgument(RawAvailabilityLabeledArgumentSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawTokenSyntax.isKindOf(raw) || RawPlatformVersionSyntax.isKindOf(raw) || RawAvailabilityLabeledArgumentSyntax.isKindOf(raw) + RawTokenSyntax.isKindOf(raw) || RawPlatformVersionSyntax.isKindOf(raw) || RawAvailabilityLabeledArgumentSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -1718,20 +1682,16 @@ public struct RawAvailabilityArgumentSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawTokenSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawTokenSyntax.self) { self = .token(node) - return - } - if let node = RawPlatformVersionSyntax(other) { + } else if let node = node.as(RawPlatformVersionSyntax.self) { self = .availabilityVersionRestriction(node) - return - } - if let node = RawAvailabilityLabeledArgumentSyntax(other) { + } else if let node = node.as(RawAvailabilityLabeledArgumentSyntax.self) { self = .availabilityLabeledArgument(node) - return + } else { + return nil } - return nil } } @@ -1904,7 +1864,7 @@ public struct RawAvailabilityLabeledArgumentSyntax: RawSyntaxNodeProtocol { case version(RawVersionTupleSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawSimpleStringLiteralExprSyntax.isKindOf(raw) || RawVersionTupleSyntax.isKindOf(raw) + RawSimpleStringLiteralExprSyntax.isKindOf(raw) || RawVersionTupleSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -1916,16 +1876,14 @@ public struct RawAvailabilityLabeledArgumentSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawSimpleStringLiteralExprSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawSimpleStringLiteralExprSyntax.self) { self = .string(node) - return - } - if let node = RawVersionTupleSyntax(other) { + } else if let node = node.as(RawVersionTupleSyntax.self) { self = .version(node) - return + } else { + return nil } - return nil } } diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesC.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesC.swift index 2df22d4f2b6..7ae0f124984 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesC.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesC.swift @@ -1491,7 +1491,7 @@ public struct RawClosureSignatureSyntax: RawSyntaxNodeProtocol { case parameterClause(RawClosureParameterClauseSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawClosureShorthandParameterListSyntax.isKindOf(raw) || RawClosureParameterClauseSyntax.isKindOf(raw) + RawClosureShorthandParameterListSyntax.isKindOf(raw) || RawClosureParameterClauseSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -1503,16 +1503,14 @@ public struct RawClosureSignatureSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawClosureShorthandParameterListSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawClosureShorthandParameterListSyntax.self) { self = .simpleInput(node) - return - } - if let node = RawClosureParameterClauseSyntax(other) { + } else if let node = node.as(RawClosureParameterClauseSyntax.self) { self = .parameterClause(node) - return + } else { + return nil } - return nil } } @@ -1690,7 +1688,7 @@ public struct RawCodeBlockItemSyntax: RawSyntaxNodeProtocol { case expr(RawExprSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawDeclSyntax.isKindOf(raw) || RawStmtSyntax.isKindOf(raw) || RawExprSyntax.isKindOf(raw) + RawDeclSyntax.isKindOf(raw) || RawStmtSyntax.isKindOf(raw) || RawExprSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -1704,20 +1702,16 @@ public struct RawCodeBlockItemSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawDeclSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawDeclSyntax.self) { self = .decl(node) - return - } - if let node = RawStmtSyntax(other) { + } else if let node = node.as(RawStmtSyntax.self) { self = .stmt(node) - return - } - if let node = RawExprSyntax(other) { + } else if let node = node.as(RawExprSyntax.self) { self = .expr(node) - return + } else { + return nil } - return nil } public init(decl: some RawDeclSyntaxNodeProtocol) { @@ -2120,7 +2114,7 @@ public struct RawConditionElementSyntax: RawSyntaxNodeProtocol { case optionalBinding(RawOptionalBindingConditionSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawExprSyntax.isKindOf(raw) || RawAvailabilityConditionSyntax.isKindOf(raw) || RawMatchingPatternConditionSyntax.isKindOf(raw) || RawOptionalBindingConditionSyntax.isKindOf(raw) + RawExprSyntax.isKindOf(raw) || RawAvailabilityConditionSyntax.isKindOf(raw) || RawMatchingPatternConditionSyntax.isKindOf(raw) || RawOptionalBindingConditionSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -2136,24 +2130,18 @@ public struct RawConditionElementSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawExprSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawExprSyntax.self) { self = .expression(node) - return - } - if let node = RawAvailabilityConditionSyntax(other) { + } else if let node = node.as(RawAvailabilityConditionSyntax.self) { self = .availability(node) - return - } - if let node = RawMatchingPatternConditionSyntax(other) { + } else if let node = node.as(RawMatchingPatternConditionSyntax.self) { self = .matchingPattern(node) - return - } - if let node = RawOptionalBindingConditionSyntax(other) { + } else if let node = node.as(RawOptionalBindingConditionSyntax.self) { self = .optionalBinding(node) - return + } else { + return nil } - return nil } public init(expression: some RawExprSyntaxNodeProtocol) { diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesD.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesD.swift index cf089912bec..f79f3b86996 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesD.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesD.swift @@ -1159,11 +1159,14 @@ public struct RawDictionaryElementSyntax: RawSyntaxNodeProtocol { @_spi(RawSyntax) public struct RawDictionaryExprSyntax: RawExprSyntaxNodeProtocol { public enum Content: RawSyntaxNodeProtocol { + /// ### Tokens + /// + /// For syntax trees generated by the parser, this is guaranteed to be `:`. case colon(RawTokenSyntax) case elements(RawDictionaryElementListSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawTokenSyntax.isKindOf(raw) || RawDictionaryElementListSyntax.isKindOf(raw) + RawTokenSyntax.isKindOf(raw) || RawDictionaryElementListSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -1175,16 +1178,14 @@ public struct RawDictionaryExprSyntax: RawExprSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawTokenSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawTokenSyntax.self) { self = .colon(node) - return - } - if let node = RawDictionaryElementListSyntax(other) { + } else if let node = node.as(RawDictionaryElementListSyntax.self) { self = .elements(node) - return + } else { + return nil } - return nil } } @@ -1583,7 +1584,7 @@ public struct RawDifferentiabilityWithRespectToArgumentSyntax: RawSyntaxNodeProt case argumentList(RawDifferentiabilityArgumentsSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawDifferentiabilityArgumentSyntax.isKindOf(raw) || RawDifferentiabilityArgumentsSyntax.isKindOf(raw) + RawDifferentiabilityArgumentSyntax.isKindOf(raw) || RawDifferentiabilityArgumentsSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -1595,16 +1596,14 @@ public struct RawDifferentiabilityWithRespectToArgumentSyntax: RawSyntaxNodeProt } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawDifferentiabilityArgumentSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawDifferentiabilityArgumentSyntax.self) { self = .argument(node) - return - } - if let node = RawDifferentiabilityArgumentsSyntax(other) { + } else if let node = node.as(RawDifferentiabilityArgumentsSyntax.self) { self = .argumentList(node) - return + } else { + return nil } - return nil } } @@ -2154,11 +2153,20 @@ public struct RawDocumentationAttributeArgumentListSyntax: RawSyntaxNodeProtocol @_spi(RawSyntax) public struct RawDocumentationAttributeArgumentSyntax: RawSyntaxNodeProtocol { public enum Value: RawSyntaxNodeProtocol { + /// ### Tokens + /// + /// For syntax trees generated by the parser, this is guaranteed to be one of the following kinds: + /// - `` + /// - `private` + /// - `fileprivate` + /// - `internal` + /// - `public` + /// - `open` case token(RawTokenSyntax) case string(RawStringLiteralExprSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawTokenSyntax.isKindOf(raw) || RawStringLiteralExprSyntax.isKindOf(raw) + RawTokenSyntax.isKindOf(raw) || RawStringLiteralExprSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -2170,16 +2178,14 @@ public struct RawDocumentationAttributeArgumentSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawTokenSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawTokenSyntax.self) { self = .token(node) - return - } - if let node = RawStringLiteralExprSyntax(other) { + } else if let node = node.as(RawStringLiteralExprSyntax.self) { self = .string(node) - return + } else { + return nil } - return nil } } diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesGHI.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesGHI.swift index 2e83b396a3b..b997f6e8583 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesGHI.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesGHI.swift @@ -534,7 +534,7 @@ public struct RawGenericRequirementSyntax: RawSyntaxNodeProtocol { case layoutRequirement(RawLayoutRequirementSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawSameTypeRequirementSyntax.isKindOf(raw) || RawConformanceRequirementSyntax.isKindOf(raw) || RawLayoutRequirementSyntax.isKindOf(raw) + RawSameTypeRequirementSyntax.isKindOf(raw) || RawConformanceRequirementSyntax.isKindOf(raw) || RawLayoutRequirementSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -548,20 +548,16 @@ public struct RawGenericRequirementSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawSameTypeRequirementSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawSameTypeRequirementSyntax.self) { self = .sameTypeRequirement(node) - return - } - if let node = RawConformanceRequirementSyntax(other) { + } else if let node = node.as(RawConformanceRequirementSyntax.self) { self = .conformanceRequirement(node) - return - } - if let node = RawLayoutRequirementSyntax(other) { + } else if let node = node.as(RawLayoutRequirementSyntax.self) { self = .layoutRequirement(node) - return + } else { + return nil } - return nil } } @@ -1055,7 +1051,7 @@ public struct RawIfConfigClauseSyntax: RawSyntaxNodeProtocol { case attributes(RawAttributeListSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawCodeBlockItemListSyntax.isKindOf(raw) || RawSwitchCaseListSyntax.isKindOf(raw) || RawMemberBlockItemListSyntax.isKindOf(raw) || RawExprSyntax.isKindOf(raw) || RawAttributeListSyntax.isKindOf(raw) + RawCodeBlockItemListSyntax.isKindOf(raw) || RawSwitchCaseListSyntax.isKindOf(raw) || RawMemberBlockItemListSyntax.isKindOf(raw) || RawExprSyntax.isKindOf(raw) || RawAttributeListSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -1073,28 +1069,20 @@ public struct RawIfConfigClauseSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawCodeBlockItemListSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawCodeBlockItemListSyntax.self) { self = .statements(node) - return - } - if let node = RawSwitchCaseListSyntax(other) { + } else if let node = node.as(RawSwitchCaseListSyntax.self) { self = .switchCases(node) - return - } - if let node = RawMemberBlockItemListSyntax(other) { + } else if let node = node.as(RawMemberBlockItemListSyntax.self) { self = .decls(node) - return - } - if let node = RawExprSyntax(other) { + } else if let node = node.as(RawExprSyntax.self) { self = .postfixExpression(node) - return - } - if let node = RawAttributeListSyntax(other) { + } else if let node = node.as(RawAttributeListSyntax.self) { self = .attributes(node) - return + } else { + return nil } - return nil } public init(postfixExpression: some RawExprSyntaxNodeProtocol) { @@ -1259,7 +1247,7 @@ public struct RawIfExprSyntax: RawExprSyntaxNodeProtocol { case codeBlock(RawCodeBlockSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawIfExprSyntax.isKindOf(raw) || RawCodeBlockSyntax.isKindOf(raw) + RawIfExprSyntax.isKindOf(raw) || RawCodeBlockSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -1271,16 +1259,14 @@ public struct RawIfExprSyntax: RawExprSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawIfExprSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawIfExprSyntax.self) { self = .ifExpr(node) - return - } - if let node = RawCodeBlockSyntax(other) { + } else if let node = node.as(RawCodeBlockSyntax.self) { self = .codeBlock(node) - return + } else { + return nil } - return nil } } diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesJKLMN.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesJKLMN.swift index e42f0c743bc..e16b561e9de 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesJKLMN.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesJKLMN.swift @@ -70,7 +70,7 @@ public struct RawKeyPathComponentSyntax: RawSyntaxNodeProtocol { case optional(RawKeyPathOptionalComponentSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawKeyPathPropertyComponentSyntax.isKindOf(raw) || RawKeyPathSubscriptComponentSyntax.isKindOf(raw) || RawKeyPathOptionalComponentSyntax.isKindOf(raw) + RawKeyPathPropertyComponentSyntax.isKindOf(raw) || RawKeyPathSubscriptComponentSyntax.isKindOf(raw) || RawKeyPathOptionalComponentSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -84,20 +84,16 @@ public struct RawKeyPathComponentSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawKeyPathPropertyComponentSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawKeyPathPropertyComponentSyntax.self) { self = .property(node) - return - } - if let node = RawKeyPathSubscriptComponentSyntax(other) { + } else if let node = node.as(RawKeyPathSubscriptComponentSyntax.self) { self = .subscript(node) - return - } - if let node = RawKeyPathOptionalComponentSyntax(other) { + } else if let node = node.as(RawKeyPathOptionalComponentSyntax.self) { self = .optional(node) - return + } else { + return nil } - return nil } } diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesOP.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesOP.swift index cef4bd310f9..7b5c3e5dd0d 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesOP.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesOP.swift @@ -1987,12 +1987,15 @@ public struct RawPrecedenceGroupAssociativitySyntax: RawSyntaxNodeProtocol { @_spi(RawSyntax) public struct RawPrecedenceGroupAttributeListSyntax: RawSyntaxNodeProtocol { public enum Element: RawSyntaxNodeProtocol { + /// Specify the new precedence group's relation to existing precedence groups. case precedenceGroupRelation(RawPrecedenceGroupRelationSyntax) + /// Specifies the precedence of an operator when used in an operation that includes optional chaining. case precedenceGroupAssignment(RawPrecedenceGroupAssignmentSyntax) + /// Specifies how a sequence of operators with the same precedence level are grouped together in the absence of grouping parentheses. case precedenceGroupAssociativity(RawPrecedenceGroupAssociativitySyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawPrecedenceGroupRelationSyntax.isKindOf(raw) || RawPrecedenceGroupAssignmentSyntax.isKindOf(raw) || RawPrecedenceGroupAssociativitySyntax.isKindOf(raw) + RawPrecedenceGroupRelationSyntax.isKindOf(raw) || RawPrecedenceGroupAssignmentSyntax.isKindOf(raw) || RawPrecedenceGroupAssociativitySyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -2006,20 +2009,16 @@ public struct RawPrecedenceGroupAttributeListSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawPrecedenceGroupRelationSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawPrecedenceGroupRelationSyntax.self) { self = .precedenceGroupRelation(node) - return - } - if let node = RawPrecedenceGroupAssignmentSyntax(other) { + } else if let node = node.as(RawPrecedenceGroupAssignmentSyntax.self) { self = .precedenceGroupAssignment(node) - return - } - if let node = RawPrecedenceGroupAssociativitySyntax(other) { + } else if let node = node.as(RawPrecedenceGroupAssociativitySyntax.self) { self = .precedenceGroupAssociativity(node) - return + } else { + return nil } - return nil } } diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesQRS.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesQRS.swift index 55f376371f5..7a0dd1310f0 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesQRS.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesQRS.swift @@ -840,13 +840,17 @@ public struct RawSourceFileSyntax: RawSyntaxNodeProtocol { @_spi(RawSyntax) public struct RawSpecializeAttributeArgumentListSyntax: RawSyntaxNodeProtocol { public enum Element: RawSyntaxNodeProtocol { + /// A labeled argument for the `@_specialize` attribute like `exported: true` case labeledSpecializeArgument(RawLabeledSpecializeArgumentSyntax) + /// The availability argument for the _specialize attribute case specializeAvailabilityArgument(RawSpecializeAvailabilityArgumentSyntax) + /// A labeled argument for the `@_specialize` attribute with a function decl value like `target: myFunc(_:)` case specializeTargetFunctionArgument(RawSpecializeTargetFunctionArgumentSyntax) + /// A `where` clause that places additional constraints on generic parameters like `where Element: Hashable`. case genericWhereClause(RawGenericWhereClauseSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawLabeledSpecializeArgumentSyntax.isKindOf(raw) || RawSpecializeAvailabilityArgumentSyntax.isKindOf(raw) || RawSpecializeTargetFunctionArgumentSyntax.isKindOf(raw) || RawGenericWhereClauseSyntax.isKindOf(raw) + RawLabeledSpecializeArgumentSyntax.isKindOf(raw) || RawSpecializeAvailabilityArgumentSyntax.isKindOf(raw) || RawSpecializeTargetFunctionArgumentSyntax.isKindOf(raw) || RawGenericWhereClauseSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -862,24 +866,18 @@ public struct RawSpecializeAttributeArgumentListSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawLabeledSpecializeArgumentSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawLabeledSpecializeArgumentSyntax.self) { self = .labeledSpecializeArgument(node) - return - } - if let node = RawSpecializeAvailabilityArgumentSyntax(other) { + } else if let node = node.as(RawSpecializeAvailabilityArgumentSyntax.self) { self = .specializeAvailabilityArgument(node) - return - } - if let node = RawSpecializeTargetFunctionArgumentSyntax(other) { + } else if let node = node.as(RawSpecializeTargetFunctionArgumentSyntax.self) { self = .specializeTargetFunctionArgument(node) - return - } - if let node = RawGenericWhereClauseSyntax(other) { + } else if let node = node.as(RawGenericWhereClauseSyntax.self) { self = .genericWhereClause(node) - return + } else { + return nil } - return nil } } @@ -1267,11 +1265,17 @@ public struct RawStringLiteralExprSyntax: RawExprSyntaxNodeProtocol { @_spi(RawSyntax) public struct RawStringLiteralSegmentListSyntax: RawSyntaxNodeProtocol { public enum Element: RawSyntaxNodeProtocol { + /// A literal segment inside a string segment. + /// + /// - SeeAlso: ``ExpressionSegmentSyntax`` case stringSegment(RawStringSegmentSyntax) + /// An interpolated expression inside a string literal. + /// + /// - SeeAlso: ``StringSegmentSyntax`` case expressionSegment(RawExpressionSegmentSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawStringSegmentSyntax.isKindOf(raw) || RawExpressionSegmentSyntax.isKindOf(raw) + RawStringSegmentSyntax.isKindOf(raw) || RawExpressionSegmentSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -1283,16 +1287,14 @@ public struct RawStringLiteralSegmentListSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawStringSegmentSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawStringSegmentSyntax.self) { self = .stringSegment(node) - return - } - if let node = RawExpressionSegmentSyntax(other) { + } else if let node = node.as(RawExpressionSegmentSyntax.self) { self = .expressionSegment(node) - return + } else { + return nil } - return nil } } @@ -2153,7 +2155,7 @@ public struct RawSwitchCaseListSyntax: RawSyntaxNodeProtocol { case ifConfigDecl(RawIfConfigDeclSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawSwitchCaseSyntax.isKindOf(raw) || RawIfConfigDeclSyntax.isKindOf(raw) + RawSwitchCaseSyntax.isKindOf(raw) || RawIfConfigDeclSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -2165,16 +2167,14 @@ public struct RawSwitchCaseListSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawSwitchCaseSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawSwitchCaseSyntax.self) { self = .switchCase(node) - return - } - if let node = RawIfConfigDeclSyntax(other) { + } else if let node = node.as(RawIfConfigDeclSyntax.self) { self = .ifConfigDecl(node) - return + } else { + return nil } - return nil } } @@ -2233,7 +2233,7 @@ public struct RawSwitchCaseSyntax: RawSyntaxNodeProtocol { case `case`(RawSwitchCaseLabelSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawSwitchDefaultLabelSyntax.isKindOf(raw) || RawSwitchCaseLabelSyntax.isKindOf(raw) + RawSwitchDefaultLabelSyntax.isKindOf(raw) || RawSwitchCaseLabelSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -2245,16 +2245,14 @@ public struct RawSwitchCaseSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawSwitchDefaultLabelSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawSwitchDefaultLabelSyntax.self) { self = .default(node) - return - } - if let node = RawSwitchCaseLabelSyntax(other) { + } else if let node = node.as(RawSwitchCaseLabelSyntax.self) { self = .case(node) - return + } else { + return nil } - return nil } } diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesTUVWXYZ.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesTUVWXYZ.swift index 1e24d3a36dd..0339afcc782 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesTUVWXYZ.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesTUVWXYZ.swift @@ -1411,11 +1411,17 @@ public struct RawTypeInitializerClauseSyntax: RawSyntaxNodeProtocol { @_spi(RawSyntax) public struct RawTypeSpecifierListSyntax: RawSyntaxNodeProtocol { public enum Element: RawSyntaxNodeProtocol { + /// A specifier that can be attached to a type to eg. mark a parameter as `inout` or `consuming` case simpleTypeSpecifier(RawSimpleTypeSpecifierSyntax) + /// A specifier that specifies function parameter on whose lifetime a type depends + /// - Note: Requires experimental feature `nonescapableTypes`. + #if compiler(>=5.8) + @_spi(ExperimentalLanguageFeatures) + #endif case lifetimeTypeSpecifier(RawLifetimeTypeSpecifierSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawSimpleTypeSpecifierSyntax.isKindOf(raw) || RawLifetimeTypeSpecifierSyntax.isKindOf(raw) + RawSimpleTypeSpecifierSyntax.isKindOf(raw) || RawLifetimeTypeSpecifierSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -1427,16 +1433,14 @@ public struct RawTypeSpecifierListSyntax: RawSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawSimpleTypeSpecifierSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawSimpleTypeSpecifierSyntax.self) { self = .simpleTypeSpecifier(node) - return - } - if let node = RawLifetimeTypeSpecifierSyntax(other) { + } else if let node = node.as(RawLifetimeTypeSpecifierSyntax.self) { self = .lifetimeTypeSpecifier(node) - return + } else { + return nil } - return nil } } @@ -2522,7 +2526,7 @@ public struct RawYieldStmtSyntax: RawStmtSyntaxNodeProtocol { case single(RawExprSyntax) public static func isKindOf(_ raw: RawSyntax) -> Bool { - return RawYieldedExpressionsClauseSyntax.isKindOf(raw) || RawExprSyntax.isKindOf(raw) + RawYieldedExpressionsClauseSyntax.isKindOf(raw) || RawExprSyntax.isKindOf(raw) } public var raw: RawSyntax { @@ -2534,16 +2538,14 @@ public struct RawYieldStmtSyntax: RawStmtSyntaxNodeProtocol { } } - public init?(_ other: some RawSyntaxNodeProtocol) { - if let node = RawYieldedExpressionsClauseSyntax(other) { + public init?(_ node: __shared some RawSyntaxNodeProtocol) { + if let node = node.as(RawYieldedExpressionsClauseSyntax.self) { self = .multiple(node) - return - } - if let node = RawExprSyntax(other) { + } else if let node = node.as(RawExprSyntax.self) { self = .single(node) - return + } else { + return nil } - return nil } public init(single: some RawExprSyntaxNodeProtocol) { diff --git a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesAB.swift b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesAB.swift index 1d97f124248..8bb4a134a3f 100644 --- a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesAB.swift +++ b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesAB.swift @@ -49,13 +49,11 @@ public struct AccessorBlockSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxNo public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(AccessorDeclListSyntax.self) { self = .accessors(node) - return - } - if let node = node.as(CodeBlockItemListSyntax.self) { + } else if let node = node.as(CodeBlockItemListSyntax.self) { self = .getter(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { @@ -2449,85 +2447,47 @@ public struct AttributeSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxNodePr public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(LabeledExprListSyntax.self) { self = .argumentList(node) - return - } - if let node = node.as(TokenSyntax.self) { + } else if let node = node.as(TokenSyntax.self) { self = .token(node) - return - } - if let node = node.as(StringLiteralExprSyntax.self) { + } else if let node = node.as(StringLiteralExprSyntax.self) { self = .string(node) - return - } - if let node = node.as(AvailabilityArgumentListSyntax.self) { + } else if let node = node.as(AvailabilityArgumentListSyntax.self) { self = .availability(node) - return - } - if let node = node.as(SpecializeAttributeArgumentListSyntax.self) { + } else if let node = node.as(SpecializeAttributeArgumentListSyntax.self) { self = .specializeArguments(node) - return - } - if let node = node.as(ObjCSelectorPieceListSyntax.self) { + } else if let node = node.as(ObjCSelectorPieceListSyntax.self) { self = .objCName(node) - return - } - if let node = node.as(ImplementsAttributeArgumentsSyntax.self) { + } else if let node = node.as(ImplementsAttributeArgumentsSyntax.self) { self = .implementsArguments(node) - return - } - if let node = node.as(DifferentiableAttributeArgumentsSyntax.self) { + } else if let node = node.as(DifferentiableAttributeArgumentsSyntax.self) { self = .differentiableArguments(node) - return - } - if let node = node.as(DerivativeAttributeArgumentsSyntax.self) { + } else if let node = node.as(DerivativeAttributeArgumentsSyntax.self) { self = .derivativeRegistrationArguments(node) - return - } - if let node = node.as(BackDeployedAttributeArgumentsSyntax.self) { + } else if let node = node.as(BackDeployedAttributeArgumentsSyntax.self) { self = .backDeployedArguments(node) - return - } - if let node = node.as(ConventionAttributeArgumentsSyntax.self) { + } else if let node = node.as(ConventionAttributeArgumentsSyntax.self) { self = .conventionArguments(node) - return - } - if let node = node.as(ConventionWitnessMethodAttributeArgumentsSyntax.self) { + } else if let node = node.as(ConventionWitnessMethodAttributeArgumentsSyntax.self) { self = .conventionWitnessMethodArguments(node) - return - } - if let node = node.as(OpaqueReturnTypeOfAttributeArgumentsSyntax.self) { + } else if let node = node.as(OpaqueReturnTypeOfAttributeArgumentsSyntax.self) { self = .opaqueReturnTypeOfAttributeArguments(node) - return - } - if let node = node.as(ExposeAttributeArgumentsSyntax.self) { + } else if let node = node.as(ExposeAttributeArgumentsSyntax.self) { self = .exposeAttributeArguments(node) - return - } - if let node = node.as(OriginallyDefinedInAttributeArgumentsSyntax.self) { + } else if let node = node.as(OriginallyDefinedInAttributeArgumentsSyntax.self) { self = .originallyDefinedInArguments(node) - return - } - if let node = node.as(UnderscorePrivateAttributeArgumentsSyntax.self) { + } else if let node = node.as(UnderscorePrivateAttributeArgumentsSyntax.self) { self = .underscorePrivateAttributeArguments(node) - return - } - if let node = node.as(DynamicReplacementAttributeArgumentsSyntax.self) { + } else if let node = node.as(DynamicReplacementAttributeArgumentsSyntax.self) { self = .dynamicReplacementArguments(node) - return - } - if let node = node.as(UnavailableFromAsyncAttributeArgumentsSyntax.self) { + } else if let node = node.as(UnavailableFromAsyncAttributeArgumentsSyntax.self) { self = .unavailableFromAsyncArguments(node) - return - } - if let node = node.as(EffectsAttributeArgumentListSyntax.self) { + } else if let node = node.as(EffectsAttributeArgumentListSyntax.self) { self = .effectsArguments(node) - return - } - if let node = node.as(DocumentationAttributeArgumentListSyntax.self) { + } else if let node = node.as(DocumentationAttributeArgumentListSyntax.self) { self = .documentationArguments(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { @@ -3411,6 +3371,11 @@ public struct AttributedTypeSyntax: TypeSyntaxProtocol, SyntaxHashable, _LeafTyp /// - ``AvailabilityArgumentListSyntax`` public struct AvailabilityArgumentSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxNodeProtocol { public enum Argument: SyntaxChildChoices, SyntaxHashable { + /// ### Tokens + /// + /// For syntax trees generated by the parser, this is guaranteed to be one of the following kinds: + /// - `` + /// - `` case token(TokenSyntax) case availabilityVersionRestriction(PlatformVersionSyntax) case availabilityLabeledArgument(AvailabilityLabeledArgumentSyntax) @@ -3441,17 +3406,13 @@ public struct AvailabilityArgumentSyntax: SyntaxProtocol, SyntaxHashable, _LeafS public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(TokenSyntax.self) { self = .token(node) - return - } - if let node = node.as(PlatformVersionSyntax.self) { + } else if let node = node.as(PlatformVersionSyntax.self) { self = .availabilityVersionRestriction(node) - return - } - if let node = node.as(AvailabilityLabeledArgumentSyntax.self) { + } else if let node = node.as(AvailabilityLabeledArgumentSyntax.self) { self = .availabilityLabeledArgument(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { @@ -3878,13 +3839,11 @@ public struct AvailabilityLabeledArgumentSyntax: SyntaxProtocol, SyntaxHashable, public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(SimpleStringLiteralExprSyntax.self) { self = .string(node) - return - } - if let node = node.as(VersionTupleSyntax.self) { + } else if let node = node.as(VersionTupleSyntax.self) { self = .version(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { diff --git a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesC.swift b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesC.swift index a8b30b98541..ca3da237b78 100644 --- a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesC.swift +++ b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesC.swift @@ -2592,13 +2592,11 @@ public struct ClosureSignatureSyntax: SyntaxProtocol, SyntaxHashable, _LeafSynta public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(ClosureShorthandParameterListSyntax.self) { self = .simpleInput(node) - return - } - if let node = node.as(ClosureParameterClauseSyntax.self) { + } else if let node = node.as(ClosureParameterClauseSyntax.self) { self = .parameterClause(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { @@ -2930,17 +2928,13 @@ public struct CodeBlockItemSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxNo public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(DeclSyntax.self) { self = .decl(node) - return - } - if let node = node.as(StmtSyntax.self) { + } else if let node = node.as(StmtSyntax.self) { self = .stmt(node) - return - } - if let node = node.as(ExprSyntax.self) { + } else if let node = node.as(ExprSyntax.self) { self = .expr(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { @@ -2950,7 +2944,7 @@ public struct CodeBlockItemSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxNo /// Checks if the current syntax node can be cast to the type conforming to the ``DeclSyntaxProtocol`` protocol. /// /// - Returns: `true` if the node can be cast, `false` otherwise. - public func `is`(_ syntaxType: S.Type) -> Bool { + public func `is`(_ syntaxType: (some DeclSyntaxProtocol).Type) -> Bool { return self.as(syntaxType) != nil } @@ -2972,7 +2966,7 @@ public struct CodeBlockItemSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxNo /// Checks if the current syntax node can be cast to the type conforming to the ``StmtSyntaxProtocol`` protocol. /// /// - Returns: `true` if the node can be cast, `false` otherwise. - public func `is`(_ syntaxType: S.Type) -> Bool { + public func `is`(_ syntaxType: (some StmtSyntaxProtocol).Type) -> Bool { return self.as(syntaxType) != nil } @@ -2994,7 +2988,7 @@ public struct CodeBlockItemSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxNo /// Checks if the current syntax node can be cast to the type conforming to the ``ExprSyntaxProtocol`` protocol. /// /// - Returns: `true` if the node can be cast, `false` otherwise. - public func `is`(_ syntaxType: S.Type) -> Bool { + public func `is`(_ syntaxType: (some ExprSyntaxProtocol).Type) -> Bool { return self.as(syntaxType) != nil } @@ -3574,21 +3568,15 @@ public struct ConditionElementSyntax: SyntaxProtocol, SyntaxHashable, _LeafSynta public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(ExprSyntax.self) { self = .expression(node) - return - } - if let node = node.as(AvailabilityConditionSyntax.self) { + } else if let node = node.as(AvailabilityConditionSyntax.self) { self = .availability(node) - return - } - if let node = node.as(MatchingPatternConditionSyntax.self) { + } else if let node = node.as(MatchingPatternConditionSyntax.self) { self = .matchingPattern(node) - return - } - if let node = node.as(OptionalBindingConditionSyntax.self) { + } else if let node = node.as(OptionalBindingConditionSyntax.self) { self = .optionalBinding(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { @@ -3603,7 +3591,7 @@ public struct ConditionElementSyntax: SyntaxProtocol, SyntaxHashable, _LeafSynta /// Checks if the current syntax node can be cast to the type conforming to the ``ExprSyntaxProtocol`` protocol. /// /// - Returns: `true` if the node can be cast, `false` otherwise. - public func `is`(_ syntaxType: S.Type) -> Bool { + public func `is`(_ syntaxType: (some ExprSyntaxProtocol).Type) -> Bool { return self.as(syntaxType) != nil } diff --git a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesD.swift b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesD.swift index 9ac2750d5f9..364a39a1928 100644 --- a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesD.swift +++ b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesD.swift @@ -1772,6 +1772,9 @@ public struct DictionaryElementSyntax: SyntaxProtocol, SyntaxHashable, _LeafSynt /// - `rightSquare`: `]` public struct DictionaryExprSyntax: ExprSyntaxProtocol, SyntaxHashable, _LeafExprSyntaxNodeProtocol { public enum Content: SyntaxChildChoices, SyntaxHashable { + /// ### Tokens + /// + /// For syntax trees generated by the parser, this is guaranteed to be `:`. case colon(TokenSyntax) case elements(DictionaryElementListSyntax) @@ -1795,13 +1798,11 @@ public struct DictionaryExprSyntax: ExprSyntaxProtocol, SyntaxHashable, _LeafExp public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(TokenSyntax.self) { self = .colon(node) - return - } - if let node = node.as(DictionaryElementListSyntax.self) { + } else if let node = node.as(DictionaryElementListSyntax.self) { self = .elements(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { @@ -2527,13 +2528,11 @@ public struct DifferentiabilityWithRespectToArgumentSyntax: SyntaxProtocol, Synt public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(DifferentiabilityArgumentSyntax.self) { self = .argument(node) - return - } - if let node = node.as(DifferentiabilityArgumentsSyntax.self) { + } else if let node = node.as(DifferentiabilityArgumentsSyntax.self) { self = .argumentList(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { @@ -3168,7 +3167,7 @@ public struct DiscardStmtSyntax: StmtSyntaxProtocol, SyntaxHashable, _LeafStmtSy /// } /// ``` /// -/// - Experiment: Requires experimental feature `doExpressions`. +/// - Note: Requires experimental feature `doExpressions`. /// /// ### Children /// @@ -3546,6 +3545,15 @@ public struct DoStmtSyntax: StmtSyntaxProtocol, SyntaxHashable, _LeafStmtSyntaxN /// - ``DocumentationAttributeArgumentListSyntax`` public struct DocumentationAttributeArgumentSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxNodeProtocol { public enum Value: SyntaxChildChoices, SyntaxHashable { + /// ### Tokens + /// + /// For syntax trees generated by the parser, this is guaranteed to be one of the following kinds: + /// - `` + /// - `private` + /// - `fileprivate` + /// - `internal` + /// - `public` + /// - `open` case token(TokenSyntax) case string(StringLiteralExprSyntax) @@ -3569,13 +3577,11 @@ public struct DocumentationAttributeArgumentSyntax: SyntaxProtocol, SyntaxHashab public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(TokenSyntax.self) { self = .token(node) - return - } - if let node = node.as(StringLiteralExprSyntax.self) { + } else if let node = node.as(StringLiteralExprSyntax.self) { self = .string(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { diff --git a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesGHI.swift b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesGHI.swift index 2cef12a9e79..851a79b9d26 100644 --- a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesGHI.swift +++ b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesGHI.swift @@ -829,17 +829,13 @@ public struct GenericRequirementSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyn public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(SameTypeRequirementSyntax.self) { self = .sameTypeRequirement(node) - return - } - if let node = node.as(ConformanceRequirementSyntax.self) { + } else if let node = node.as(ConformanceRequirementSyntax.self) { self = .conformanceRequirement(node) - return - } - if let node = node.as(LayoutRequirementSyntax.self) { + } else if let node = node.as(LayoutRequirementSyntax.self) { self = .layoutRequirement(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { @@ -1749,25 +1745,17 @@ public struct IfConfigClauseSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxN public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(CodeBlockItemListSyntax.self) { self = .statements(node) - return - } - if let node = node.as(SwitchCaseListSyntax.self) { + } else if let node = node.as(SwitchCaseListSyntax.self) { self = .switchCases(node) - return - } - if let node = node.as(MemberBlockItemListSyntax.self) { + } else if let node = node.as(MemberBlockItemListSyntax.self) { self = .decls(node) - return - } - if let node = node.as(ExprSyntax.self) { + } else if let node = node.as(ExprSyntax.self) { self = .postfixExpression(node) - return - } - if let node = node.as(AttributeListSyntax.self) { + } else if let node = node.as(AttributeListSyntax.self) { self = .attributes(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { @@ -1849,7 +1837,7 @@ public struct IfConfigClauseSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxN /// Checks if the current syntax node can be cast to the type conforming to the ``ExprSyntaxProtocol`` protocol. /// /// - Returns: `true` if the node can be cast, `false` otherwise. - public func `is`(_ syntaxType: S.Type) -> Bool { + public func `is`(_ syntaxType: (some ExprSyntaxProtocol).Type) -> Bool { return self.as(syntaxType) != nil } @@ -2208,13 +2196,11 @@ public struct IfExprSyntax: ExprSyntaxProtocol, SyntaxHashable, _LeafExprSyntaxN public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(IfExprSyntax.self) { self = .ifExpr(node) - return - } - if let node = node.as(CodeBlockSyntax.self) { + } else if let node = node.as(CodeBlockSyntax.self) { self = .codeBlock(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { diff --git a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesJKLMN.swift b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesJKLMN.swift index 6abb2b175ec..ffa04c4a78e 100644 --- a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesJKLMN.swift +++ b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesJKLMN.swift @@ -56,17 +56,13 @@ public struct KeyPathComponentSyntax: SyntaxProtocol, SyntaxHashable, _LeafSynta public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(KeyPathPropertyComponentSyntax.self) { self = .property(node) - return - } - if let node = node.as(KeyPathSubscriptComponentSyntax.self) { + } else if let node = node.as(KeyPathSubscriptComponentSyntax.self) { self = .subscript(node) - return - } - if let node = node.as(KeyPathOptionalComponentSyntax.self) { + } else if let node = node.as(KeyPathOptionalComponentSyntax.self) { self = .optional(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { @@ -1623,7 +1619,7 @@ public struct LayoutRequirementSyntax: SyntaxProtocol, SyntaxHashable, _LeafSynt /// ### Example /// `data` in `func foo(data: Array) -> borrow(data) ComplexReferenceType` /// -/// - Experiment: Requires experimental feature `nonescapableTypes`. +/// - Note: Requires experimental feature `nonescapableTypes`. /// /// ### Children /// @@ -1754,7 +1750,7 @@ public struct LifetimeSpecifierArgumentSyntax: SyntaxProtocol, SyntaxHashable, _ /// A specifier that specifies function parameter on whose lifetime a type depends /// -/// - Experiment: Requires experimental feature `nonescapableTypes`. +/// - Note: Requires experimental feature `nonescapableTypes`. /// /// ### Children /// diff --git a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesQRS.swift b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesQRS.swift index e14e02407e5..021e73e494c 100644 --- a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesQRS.swift +++ b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesQRS.swift @@ -3726,13 +3726,11 @@ public struct SwitchCaseSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxNodeP public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(SwitchDefaultLabelSyntax.self) { self = .default(node) - return - } - if let node = node.as(SwitchCaseLabelSyntax.self) { + } else if let node = node.as(SwitchCaseLabelSyntax.self) { self = .case(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { diff --git a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesTUVWXYZ.swift b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesTUVWXYZ.swift index 3abd273e999..605a77ee292 100644 --- a/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesTUVWXYZ.swift +++ b/Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesTUVWXYZ.swift @@ -229,7 +229,7 @@ public struct TernaryExprSyntax: ExprSyntaxProtocol, SyntaxHashable, _LeafExprSy /// then /// ``` /// -/// - Experiment: Requires experimental feature `thenStatements`. +/// - Note: Requires experimental feature `thenStatements`. /// /// ### Children /// @@ -4246,13 +4246,11 @@ public struct YieldStmtSyntax: StmtSyntaxProtocol, SyntaxHashable, _LeafStmtSynt public init?(_ node: __shared some SyntaxProtocol) { if let node = node.as(YieldedExpressionsClauseSyntax.self) { self = .multiple(node) - return - } - if let node = node.as(ExprSyntax.self) { + } else if let node = node.as(ExprSyntax.self) { self = .single(node) - return + } else { + return nil } - return nil } public static var structure: SyntaxNodeStructure { @@ -4284,7 +4282,7 @@ public struct YieldStmtSyntax: StmtSyntaxProtocol, SyntaxHashable, _LeafStmtSynt /// Checks if the current syntax node can be cast to the type conforming to the ``ExprSyntaxProtocol`` protocol. /// /// - Returns: `true` if the node can be cast, `false` otherwise. - public func `is`(_ syntaxType: S.Type) -> Bool { + public func `is`(_ syntaxType: (some ExprSyntaxProtocol).Type) -> Bool { return self.as(syntaxType) != nil }