From f90e0904f3f062c07611f51e5e899494768f7778 Mon Sep 17 00:00:00 2001 From: Kai Lau Date: Thu, 29 Aug 2024 02:35:33 -0700 Subject: [PATCH] [CodeGeneration] improved creation of syntax nodes across raw and non-raw - introduced `TypeConvertible`, `ParameterConvertible` and `SyntaxNodeConvertible` - introduced raw representations of `SyntaxNodeKind`, `Node` and `Child` for raw - removed `SyntaxBuildableType` - fixed the typing of raw node choices --- .../Sources/SyntaxSupport/Child.swift | 88 +++++++- .../Sources/SyntaxSupport/Node.swift | 28 +-- .../SyntaxSupport/NodeChoiceConvertible.swift | 11 +- .../SyntaxSupport/ParameterConvertible.swift | 35 ++++ .../Sources/SyntaxSupport/RawChild.swift | 85 ++++++++ .../Sources/SyntaxSupport/RawNode.swift | 54 +++++ .../SyntaxSupport/RawSyntaxNodeKind.swift | 52 +++-- .../SyntaxSupport/SyntaxNodeConvertible.swift | 75 +++++++ .../SyntaxSupport/SyntaxNodeKind.swift | 51 +++-- .../SyntaxNodeKindProtocol.swift | 25 +++ .../Sources/SyntaxSupport/SyntaxNodes.swift | 6 +- .../Sources/SyntaxSupport/Traits.swift | 2 +- .../SyntaxSupport/TypeConvertible.swift | 30 ++- .../Sources/SyntaxSupport/Utils.swift | 10 + .../Sources/Utils/SyntaxBuildableChild.swift | 83 +++----- .../Sources/Utils/SyntaxBuildableNode.swift | 33 +-- .../Sources/Utils/SyntaxBuildableType.swift | 196 ------------------ .../ChildNodeChoices.swift | 37 +--- .../LayoutNode+Extensions.swift | 35 ++-- .../swiftparser/LayoutNodesParsableFile.swift | 2 +- .../swiftparser/ParserTokenSpecSetFile.swift | 2 +- .../ChildNameForDiagnosticsFile.swift | 2 +- .../swiftsyntax/ChildNameForKeyPathFile.swift | 8 +- .../swiftsyntax/RawSyntaxNodesFile.swift | 86 ++++---- .../swiftsyntax/RawSyntaxValidationFile.swift | 50 +---- .../RenamedChildrenCompatibilityFile.swift | 16 +- .../swiftsyntax/SwiftSyntaxDoccIndex.swift | 12 +- .../swiftsyntax/SyntaxAnyVisitorFile.swift | 10 +- .../swiftsyntax/SyntaxBaseNodesFile.swift | 124 +++++------ .../swiftsyntax/SyntaxCollectionsFile.swift | 4 +- .../swiftsyntax/SyntaxEnumFile.swift | 20 +- .../swiftsyntax/SyntaxKindFile.swift | 6 +- .../swiftsyntax/SyntaxNodeCasting.swift | 2 +- .../swiftsyntax/SyntaxNodesFile.swift | 36 ++-- .../swiftsyntax/SyntaxRewriterFile.swift | 35 ++-- .../swiftsyntax/SyntaxTraitsFile.swift | 7 +- .../swiftsyntax/SyntaxVisitorFile.swift | 18 +- .../BuildableNodesFile.swift | 4 +- ...amedChildrenBuilderCompatibilityFile.swift | 2 +- .../ResultBuildersFile.swift | 16 +- ...yStringInterpolationConformancesFile.swift | 2 +- .../ValidateSyntaxNodes.swift | 20 +- .../generated/raw/RawSyntaxNodesAB.swift | 20 +- .../generated/raw/RawSyntaxNodesC.swift | 12 +- .../generated/raw/RawSyntaxNodesD.swift | 12 +- .../generated/raw/RawSyntaxNodesGHI.swift | 12 +- .../generated/raw/RawSyntaxNodesJKLMN.swift | 4 +- .../generated/raw/RawSyntaxNodesOP.swift | 4 +- .../generated/raw/RawSyntaxNodesQRS.swift | 16 +- .../generated/raw/RawSyntaxNodesTUVWXYZ.swift | 8 +- .../generated/raw/RawSyntaxValidation.swift | 93 ++------- .../generated/BuildableNodes.swift | 14 +- .../RenamedChildrenBuilderCompatibility.swift | 10 +- 53 files changed, 812 insertions(+), 813 deletions(-) create mode 100644 CodeGeneration/Sources/SyntaxSupport/ParameterConvertible.swift create mode 100644 CodeGeneration/Sources/SyntaxSupport/RawChild.swift create mode 100644 CodeGeneration/Sources/SyntaxSupport/RawNode.swift create mode 100644 CodeGeneration/Sources/SyntaxSupport/SyntaxNodeConvertible.swift create mode 100644 CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKindProtocol.swift delete mode 100644 CodeGeneration/Sources/Utils/SyntaxBuildableType.swift diff --git a/CodeGeneration/Sources/SyntaxSupport/Child.swift b/CodeGeneration/Sources/SyntaxSupport/Child.swift index d893117a399..68d8a86573b 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: NodeChoiceConvertible { +public class Child: SyntaxNodeConvertible, NodeChoiceConvertible, ParameterConvertible { /// The name of the child. /// /// The first character of the name is always uppercase. @@ -227,9 +227,9 @@ public class Child: NodeChoiceConvertible { case .nodeChoices(let choices): return choices.isEmpty case .node(let kind): - return kind.isBase + return kind.isBaseType case .collection(kind: let kind, _, _, _): - return kind.isBase + return kind.isBaseType case .token: return false } @@ -244,6 +244,11 @@ public class Child: NodeChoiceConvertible { return AttributeListSyntax("@_spi(ExperimentalLanguageFeatures)").with(\.trailingTrivia, .newline) } + /// The ``Node`` representation of this child, if any. + public var node: Node? { + self.syntaxNodeKind.node + } + /// If a classification is passed, it specifies the color identifiers in /// that subtree should inherit for syntax coloring. Must be a member of /// ``SyntaxClassification``. @@ -273,3 +278,80 @@ public class Child: NodeChoiceConvertible { self.isOptional = isOptional } } + +// MARK: SyntaxNodeConvertible +public extension Child { + var isNode: Bool { + switch self.kind { + case .node, .collection: + return true + default: + return false + } + } + + var syntaxType: TypeSyntax { + switch self.kind { + case .node(let kind), .collection(let kind, _, _, _): + return kind.syntaxType + case .nodeChoices: + return self.syntaxChoicesType + case .token: + return "TokenSyntax" + } + } +} + +// MARK: ParameterConvertible +extension Child { + public var parameterAnyType: TypeSyntax { + self.parameterType(specifier: "any") + } + + public var parameterSomeType: TypeSyntax { + self.parameterType(specifier: "some") + } + + func parameterType( + specifier: TokenSyntax, + protocolType: TypeSyntax? = nil, + syntaxType: TypeSyntax? = nil + ) -> TypeSyntax { + let type: TypeSyntax + if self.isBaseNode { + type = "\(specifier) \(protocolType ?? self.protocolType)" + } else { + type = syntaxType ?? self.syntaxType + } + return self.isOptional ? type.optionalWrapped : type + } + + func defaultValue(syntaxType: TypeSyntax) -> ExprSyntax? { + guard !self.isOptional else { + if self.isBaseNode { + return "\(syntaxType.optionalWrapped).none" + } else { + return "nil" + } + } + if case .collection(_, _, defaultsToEmpty: true, _) = self.kind { + return "[]" + } + guard let token else { + return self.isOptional ? "nil" : nil + } + guard token.text == nil else { + return ".\(token.identifier)Token()" + } + guard case .token(let choices, _, _) = self.kind, + case .keyword(let keyword) = choices.only + else { + return nil + } + return ".\(token.memberCallName)(.\(keyword.spec.memberCallName))" + } + + public var defaultValue: ExprSyntax? { + self.defaultValue(syntaxType: self.syntaxType) + } +} diff --git a/CodeGeneration/Sources/SyntaxSupport/Node.swift b/CodeGeneration/Sources/SyntaxSupport/Node.swift index 03516b6643e..6342b15e9f3 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: NodeChoiceConvertible { +public class Node: SyntaxNodeConvertible, NodeChoiceConvertible { fileprivate enum Data { case layout(children: [Child], traits: [String]) case collection(choices: [SyntaxNodeKind]) @@ -38,7 +38,7 @@ public class Node: NodeChoiceConvertible { public let kind: SyntaxNodeKind /// The kind of node’s supertype. This kind must have `isBase == true` - public var base: SyntaxNodeKind { + public var baseKind: SyntaxNodeKind { self.kind.base } @@ -57,6 +57,10 @@ public class Node: NodeChoiceConvertible { /// function that should be invoked to create this node. public let parserFunction: TokenSyntax? + public let isOptional = false + + public let isNode = true + public var syntaxNodeKind: SyntaxNodeKind { self.kind } @@ -71,7 +75,7 @@ public class Node: NodeChoiceConvertible { public var layoutNode: LayoutNode? { switch data { case .layout: - if kind.isBase { + if kind.isBaseType { return nil } else { return LayoutNode(node: self) @@ -127,7 +131,7 @@ public class Node: NodeChoiceConvertible { children: [Child] = [] ) { precondition(kind.base != .syntaxCollection) - precondition(kind.base.isBase, "unknown base kind '\(kind.base)' for node '\(kind)'") + precondition(kind.base.isBaseType, "unknown base kind '\(kind.base)' for node '\(kind)'") self.kind = kind self.experimentalFeature = experimentalFeature @@ -219,18 +223,14 @@ public class Node: NodeChoiceConvertible { let list = childIn - .map { - if let childName = $0.child?.identifier { + .map { (node, child) in + if let childName = child?.identifier { // This will repeat the syntax type before and after the dot, which is // a little unfortunate, but it's the only way I found to get docc to // generate a fully-qualified type + member. - if $0.node.isAvailableInDocc { - return " - \($0.node.doccLink).``\($0.node.syntaxType)/\(childName)``" - } else { - return " - \($0.node.doccLink).`\($0.node.syntaxType)/\(childName)`" - } + return " - \(node.doccLink).\(node.doccLink(content: "\(node.syntaxType)/\(childName)"))" } else { - return " - \($0.node.doccLink)" + return " - \(node.doccLink)" } } .joined(separator: "\n") @@ -252,8 +252,8 @@ public class Node: NodeChoiceConvertible { let list = SYNTAX_NODES - .filter { $0.base == self.kind && !$0.isExperimental && !$0.kind.isDeprecated } - .map { "- \($0.kind.doccLink)" } + .filter { $0.baseKind == self.kind && !$0.isExperimental && !$0.isDeprecated } + .map { "- \($0.doccLink)" } .joined(separator: "\n") guard !list.isEmpty else { diff --git a/CodeGeneration/Sources/SyntaxSupport/NodeChoiceConvertible.swift b/CodeGeneration/Sources/SyntaxSupport/NodeChoiceConvertible.swift index 386ac90e804..f0b58d65015 100644 --- a/CodeGeneration/Sources/SyntaxSupport/NodeChoiceConvertible.swift +++ b/CodeGeneration/Sources/SyntaxSupport/NodeChoiceConvertible.swift @@ -13,23 +13,20 @@ 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 +public protocol NodeChoiceConvertible: TypeConvertible, IdentifierConvertible { + /// A docc comment describing the node choice, 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 + /// The experimental feature the node choice 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. + /// The attributes that should be printed on any API for the node choice. /// /// 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 { diff --git a/CodeGeneration/Sources/SyntaxSupport/ParameterConvertible.swift b/CodeGeneration/Sources/SyntaxSupport/ParameterConvertible.swift new file mode 100644 index 00000000000..fbf781c3e5a --- /dev/null +++ b/CodeGeneration/Sources/SyntaxSupport/ParameterConvertible.swift @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +/// Implementations should provide necessary information for generating code of a parameter. +public protocol ParameterConvertible { + /// The type that is used for parameters in SwiftSyntaxBuilder that take this + /// type of syntax node and expect an existential type if the parameter type is a protocol. + var parameterAnyType: TypeSyntax { + get + } + + /// The type that is used for parameters in SwiftSyntaxBuilder that take this + /// type of syntax node and expect a generic type if the parameter type is a protocol. + var parameterSomeType: TypeSyntax { + get + } + + /// If the type has a default value (because it is optional or a token + /// with fixed test), return an expression that can be used as the + /// default value for a function parameter. Otherwise, return `nil`. + var defaultValue: ExprSyntax? { + get + } +} diff --git a/CodeGeneration/Sources/SyntaxSupport/RawChild.swift b/CodeGeneration/Sources/SyntaxSupport/RawChild.swift new file mode 100644 index 00000000000..35126ed19a8 --- /dev/null +++ b/CodeGeneration/Sources/SyntaxSupport/RawChild.swift @@ -0,0 +1,85 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +public extension Child { + /// The raw representation of ``Child``. + struct Raw: SyntaxNodeConvertible, NodeChoiceConvertible, ParameterConvertible { + var child: Child + + public var isOptional: Bool { + self.child.isOptional + } + + public var isNode: Bool { + self.child.isNode + } + + public var syntaxType: TypeSyntax { + switch self.child.kind { + case .node(let kind), .collection(let kind, _, _, _): + return kind.raw.syntaxType + case .nodeChoices: + return self.child.syntaxChoicesType + case .token: + return "RawTokenSyntax" + } + } + + public var syntaxNodeKind: SyntaxNodeKind.Raw { + self.child.syntaxNodeKind.raw + } + + public var documentation: SwiftSyntax.Trivia { + self.child.documentation + } + + public var experimentalFeature: ExperimentalFeature? { + self.child.experimentalFeature + } + + public var apiAttributes: AttributeListSyntax { + self.child.apiAttributes + } + + public var identifier: TokenSyntax { + self.child.identifier + } + + public var parameterAnyType: TypeSyntax { + self.child.parameterType(specifier: "any", protocolType: self.protocolType, syntaxType: self.syntaxType) + } + + public var parameterSomeType: TypeSyntax { + if self.child.isBaseNode && !self.child.isOptional { + // we restrict the use of generic type to non-optional parameter types, otherwise call sites would no longer be + // able to just pass `nil` to this parameter without specializing `(some RawSyntaxNodeProtocol)?` + // + // 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 + return "some \(self.protocolType)" + } else { + return self.actualType + } + } + + public var defaultValue: ExprSyntax? { + self.child.defaultValue(syntaxType: self.syntaxType) + } + } + + /// The raw representation of this child. + var raw: Raw { + Raw(child: self) + } +} diff --git a/CodeGeneration/Sources/SyntaxSupport/RawNode.swift b/CodeGeneration/Sources/SyntaxSupport/RawNode.swift new file mode 100644 index 00000000000..98c9ce8acbb --- /dev/null +++ b/CodeGeneration/Sources/SyntaxSupport/RawNode.swift @@ -0,0 +1,54 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +public extension Node { + /// The raw representation of ``Node``. The definition of a syntax node that, when generated, conforms to + /// `RawSyntaxNodeProtocol`. + struct Raw: SyntaxNodeConvertible, NodeChoiceConvertible { + var node: Node + + public var isOptional: Bool { + self.node.isOptional + } + + public var syntaxNodeKind: SyntaxNodeKind.Raw { + self.node.syntaxNodeKind.raw + } + + public var isNode: Bool { + self.node.isNode + } + + public var documentation: SwiftSyntax.Trivia { + self.node.documentation + } + + public var experimentalFeature: ExperimentalFeature? { + self.node.experimentalFeature + } + + public var apiAttributes: AttributeListSyntax { + self.node.apiAttributes + } + + public var identifier: TokenSyntax { + self.node.identifier + } + } + + /// The raw representation of this node. + var raw: Raw { + Raw(node: self) + } +} diff --git a/CodeGeneration/Sources/SyntaxSupport/RawSyntaxNodeKind.swift b/CodeGeneration/Sources/SyntaxSupport/RawSyntaxNodeKind.swift index 39a4e8f9f0b..fc00ace5b35 100644 --- a/CodeGeneration/Sources/SyntaxSupport/RawSyntaxNodeKind.swift +++ b/CodeGeneration/Sources/SyntaxSupport/RawSyntaxNodeKind.swift @@ -12,36 +12,42 @@ import SwiftSyntax -/// A wrapper of ``SyntaxNodeKind`` providing syntax type information for the raw side. -public struct RawSyntaxNodeKind: TypeConvertible { - public var syntaxNodeKind: SyntaxNodeKind +public extension SyntaxNodeKind { + /// A wrapper of ``SyntaxNodeKind`` providing syntax type information for the raw side. + struct Raw: SyntaxNodeKindProtocol { + public var nonRaw: SyntaxNodeKind - public var isBase: Bool { - self.syntaxNodeKind.isBase - } + public var raw: Self { + self + } - public var base: RawSyntaxNodeKind { - Self(syntaxNodeKind: self.syntaxNodeKind.base) - } + public var isBaseType: Bool { + self.nonRaw.isBaseType + } - public var syntaxType: TypeSyntax { - "Raw\(self.syntaxNodeKind.syntaxType)" - } + public var syntaxType: TypeSyntax { + "Raw\(self.nonRaw.syntaxType)" + } - public var protocolType: TypeSyntax { - switch self { - case .syntax, .syntaxCollection: - return "RawSyntaxNodeProtocol" - default: - return "\(self.syntaxType)NodeProtocol" + public var protocolType: TypeSyntax { + switch self { + case .syntax, .syntaxCollection: + return "RawSyntaxNodeProtocol" + default: + return "\(self.syntaxType)NodeProtocol" + } } - } - public var isAvailableInDocc: Bool { - false + public func doccLink(content: String) -> String { + "`\(content)`" + } + + public static func ~= (lhs: SyntaxNodeKind, rhs: Self) -> Bool { + lhs == rhs.nonRaw + } } - public static func ~= (lhs: SyntaxNodeKind, rhs: Self) -> Bool { - lhs == rhs.syntaxNodeKind + var raw: Raw { + Raw(nonRaw: self) } } diff --git a/CodeGeneration/Sources/SyntaxSupport/SyntaxNodeConvertible.swift b/CodeGeneration/Sources/SyntaxSupport/SyntaxNodeConvertible.swift new file mode 100644 index 00000000000..0de55a8468e --- /dev/null +++ b/CodeGeneration/Sources/SyntaxSupport/SyntaxNodeConvertible.swift @@ -0,0 +1,75 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +/// Implementations should provide the definition of the syntax node to be generated. +public protocol SyntaxNodeConvertible: TypeConvertible { + associatedtype Kind: SyntaxNodeKindProtocol + + /// Whether the syntax node is optional. + var isOptional: Bool { + get + } + + /// The kind of the syntax node. + var syntaxNodeKind: Kind { + get + } + + /// Whether the syntax node is a ``Node``. + var isNode: Bool { + get + } +} + +// MARK: TypeConvertible +public extension SyntaxNodeConvertible { + var syntaxType: TypeSyntax { + self.syntaxNodeKind.syntaxType + } + + var protocolType: TypeSyntax { + self.syntaxNodeKind.protocolType + } + + var isBaseType: Bool { + self.syntaxNodeKind.isBaseType + } + + var isDeprecated: Bool { + self.syntaxNodeKind.isDeprecated + } + + func doccLink(content: String) -> String { + self.syntaxNodeKind.doccLink(content: content) + } +} + +public extension SyntaxNodeConvertible { + /// Whether the type of the syntax node is a base type and the syntax node is a ``Node``. + var isBaseNode: Bool { + self.isNode && self.isBaseType + } + + /// Assuming that this is a collection type, the non-optional type of the result builder + /// that can be used to build the collection. + var resultBuilderType: TypeSyntax { + precondition(self.isNode, "Non-node cannot be constructed using result builders") + return "\(raw: self.syntaxNodeKind.nameInProperCase)Builder" + } + + /// The type of the syntax node taking into account ``SyntaxNodeConvertible/isOptional``. + var actualType: TypeSyntax { + self.isOptional ? self.syntaxType.optionalWrapped : self.syntaxType + } +} diff --git a/CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKind.swift b/CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKind.swift index 057fef48458..6b543cb91b5 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, TypeConvertible { +public enum SyntaxNodeKind: String, CaseIterable, SyntaxNodeKindProtocol, IdentifierConvertible { // Please keep this list sorted alphabetically case _canImportExpr @@ -327,7 +327,7 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible, TypeCon } } - public var isBase: Bool { + public var isBaseType: Bool { switch self { case .decl, .expr, .pattern, .stmt, .syntax, .syntaxCollection, .type: return true @@ -411,7 +411,7 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible, TypeCon } public var identifier: TokenSyntax { - return .identifier(rawValue) + return .identifier(self.rawValue) } public var syntaxType: TypeSyntax { @@ -421,22 +421,20 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible, TypeCon case .syntaxCollection: return "SyntaxCollection" default: - return "\(raw: rawValue.withFirstCharacterUppercased)Syntax" + return "\(raw: self.nameInProperCase)Syntax" } } - public var isAvailableInDocc: Bool { - if let node = SYNTAX_NODE_MAP[self], node.isExperimental { - return false - } else if isDeprecated { - return false + public func doccLink(content: String) -> String { + if self.node?.isExperimental == true || self.isDeprecated { + return "`\(content)`" } else { - return true + return "``\(content)``" } } public var protocolType: TypeSyntax { - return "\(syntaxType)Protocol" + return "\(self.syntaxType)Protocol" } /// For base node types, generates the name of the protocol to which all @@ -445,8 +443,8 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible, TypeCon /// - Warning: This property can only be accessed for base node kinds; attempting to /// access it for a non-base kind will result in a runtime error. public var leafProtocolType: TypeSyntax { - if isBase { - return "_Leaf\(syntaxType)NodeProtocol" + if self.isBaseType { + return "_Leaf\(self.syntaxType)NodeProtocol" } else { fatalError("Only base kind can define leaf protocol") } @@ -537,19 +535,30 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible, TypeCon } } - public var isDeprecated: Bool { - return rawValue.first == "_" - } - var deprecationAttribute: AttributeSyntax? { - if let deprecationMessage = deprecationMessage { + if let deprecationMessage { AttributeSyntax("@available(*, deprecated, message: \(literal: deprecationMessage)") } else { - AttributeSyntax(#"@available(*, deprecated, renamed: "\#(syntaxType)")"#) + AttributeSyntax(#"@available(*, deprecated, renamed: "\#(self.syntaxType)")"#) } } - public var raw: RawSyntaxNodeKind { - RawSyntaxNodeKind(syntaxNodeKind: self) + public var nonRaw: Self { + self + } +} + +public extension SyntaxNodeKindProtocol where NonRaw == SyntaxNodeKind { + /// The ``Node`` representation of this kind, if any. + var node: Node? { + SYNTAX_NODE_MAP[self.nonRaw] + } + + var nameInProperCase: String { + self.nonRaw.rawValue.withFirstCharacterUppercased + } + + var isDeprecated: Bool { + self.nonRaw.rawValue.first == "_" } } diff --git a/CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKindProtocol.swift b/CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKindProtocol.swift new file mode 100644 index 00000000000..13cfeb6e296 --- /dev/null +++ b/CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKindProtocol.swift @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +public protocol SyntaxNodeKindProtocol: TypeConvertible { + associatedtype Raw: SyntaxNodeKindProtocol + associatedtype NonRaw: SyntaxNodeKindProtocol + + /// The raw representation of this kind. + var raw: Raw { get } + + /// The non-raw representation of this kind. + var nonRaw: NonRaw { get } + + /// The name of this kind in proper case. + var nameInProperCase: String { get } +} diff --git a/CodeGeneration/Sources/SyntaxSupport/SyntaxNodes.swift b/CodeGeneration/Sources/SyntaxSupport/SyntaxNodes.swift index 3987c07cbfc..51d12507ea0 100644 --- a/CodeGeneration/Sources/SyntaxSupport/SyntaxNodes.swift +++ b/CodeGeneration/Sources/SyntaxSupport/SyntaxNodes.swift @@ -24,8 +24,8 @@ private let unsortedSyntaxNodes: [Node] = public let SYNTAX_NODES: [Node] = unsortedSyntaxNodes .sorted { (lhs: Node, rhs: Node) -> Bool in - let lhsSortKey = lhs.kind.syntaxType.description.droppingLeadingUnderscores - let rhsSortKey = rhs.kind.syntaxType.description.droppingLeadingUnderscores + let lhsSortKey = lhs.syntaxType.description.droppingLeadingUnderscores + let rhsSortKey = rhs.syntaxType.description.droppingLeadingUnderscores return lhsSortKey < rhsSortKey } @@ -34,4 +34,4 @@ public let SYNTAX_NODE_MAP: [SyntaxNodeKind: Node] = Dictionary( uniqueKeysWithValues: SYNTAX_NODES.map { node in (node.kind, node) } ) -public let NON_BASE_SYNTAX_NODES = SYNTAX_NODES.filter { !$0.kind.isBase } +public let NON_BASE_SYNTAX_NODES = SYNTAX_NODES.filter { !$0.isBaseType } diff --git a/CodeGeneration/Sources/SyntaxSupport/Traits.swift b/CodeGeneration/Sources/SyntaxSupport/Traits.swift index 8838ecb7ad5..34570e1de2a 100644 --- a/CodeGeneration/Sources/SyntaxSupport/Traits.swift +++ b/CodeGeneration/Sources/SyntaxSupport/Traits.swift @@ -24,7 +24,7 @@ public class Trait { public let children: [Child] init(traitName: String, baseKind: SyntaxNodeKind? = nil, documentation: String? = nil, children: [Child]) { - precondition(baseKind?.isBase != false, "`baseKind` must be a base syntax node kind") + precondition(baseKind?.isBaseType != false, "`baseKind` must be a base syntax node kind") self.traitName = traitName self.baseKind = baseKind self.protocolName = .identifier("\(traitName)Syntax") diff --git a/CodeGeneration/Sources/SyntaxSupport/TypeConvertible.swift b/CodeGeneration/Sources/SyntaxSupport/TypeConvertible.swift index d783c77f705..24936101ce4 100644 --- a/CodeGeneration/Sources/SyntaxSupport/TypeConvertible.swift +++ b/CodeGeneration/Sources/SyntaxSupport/TypeConvertible.swift @@ -12,39 +12,37 @@ import SwiftSyntax -/// Instances of a conforming type should provide syntax type information to be used in code generation. +/// Implementations should provide the definition of the syntax type to be generated.. public protocol TypeConvertible { - /// Whether this is one of the syntax base nodes. - var isBase: Bool { + /// Whether this is one of the syntax base types. + var isBaseType: Bool { get } - /// The type name of this node in the SwiftSyntax module. + /// The name of this type 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. + /// For base types, the name of the corresponding protocol to which all the + /// concrete types that have this base type, 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 { + /// Whether this type is deprecated. + var isDeprecated: Bool { get } + + /// If this type is non-experimental and non-deprecated, `content` wrapped in two backticks. + /// Otherwise, `content` in code font. + func doccLink(content: String) -> String } 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. + /// Convenience version of ``doccLink(content:)`` with `content` set to the syntax type name. var doccLink: String { - if self.isAvailableInDocc { - return "``\(self.syntaxType)``" - } else { - return "`\(self.syntaxType)`" - } + self.doccLink(content: "\(self.syntaxType)") } } diff --git a/CodeGeneration/Sources/SyntaxSupport/Utils.swift b/CodeGeneration/Sources/SyntaxSupport/Utils.swift index 032192dd235..20d5c99fee0 100644 --- a/CodeGeneration/Sources/SyntaxSupport/Utils.swift +++ b/CodeGeneration/Sources/SyntaxSupport/Utils.swift @@ -95,3 +95,13 @@ extension TokenSyntax { } } } + +extension TypeSyntaxProtocol { + var optionalWrapped: TypeSyntax { + if self.is(SomeOrAnyTypeSyntax.self) { + return "(\(self))?" + } else { + return "\(self)?" + } + } +} diff --git a/CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift b/CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift index fe73c60d237..4de2ded23e9 100644 --- a/CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift +++ b/CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift @@ -14,72 +14,34 @@ import SwiftSyntax import SwiftSyntaxBuilder import SyntaxSupport -/// Something that can either be a node of the given kind or a token of the -/// given token kind. -public enum SyntaxOrTokenNodeKind: Hashable { - case node(kind: SyntaxNodeKind) - case token(Token) -} - /// Extension to the ``Child`` type to provide functionality specific to /// SwiftSyntaxBuilder. extension Child { - /// The type of this child, represented by a ``SyntaxBuildableType``, which can - /// be used to create the corresponding `Buildable` and `ExpressibleAs` types. - public var buildableType: SyntaxBuildableType { - let buildableKind: SyntaxOrTokenNodeKind - switch kind { - case .node(kind: let kind): - buildableKind = .node(kind: kind) - case .nodeChoices: - buildableKind = .node(kind: .syntax) - case .collection(kind: let kind, _, _, _): - buildableKind = .node(kind: kind) - case .token: - buildableKind = .token(self.tokenKind!) + public enum InitializableBuilder { + struct Wrapper: SyntaxNodeConvertible { + var syntaxNodeKind: SyntaxNodeKind + var isOptional: Bool + let isNode = true } - return SyntaxBuildableType( - kind: buildableKind, - isOptional: isOptional - ) - } - - public var parameterBaseType: TypeSyntax { - switch kind { - case .nodeChoices: - return self.syntaxChoicesType - default: - return buildableType.parameterBaseType - } - } - public var parameterType: TypeSyntax { - return self.buildableType.optionalWrapped(type: parameterBaseType) + case otherType(type: any SyntaxNodeConvertible, parameterLabel: TokenSyntax) + case sameType } - public var defaultValue: ExprSyntax? { - if isOptional || isUnexpectedNodes { - if buildableType.isBaseType && kind.isNodeChoicesEmpty { - return ExprSyntax("\(buildableType.buildable).none") - } else { - return ExprSyntax("nil") - } - } - if case .collection(_, _, defaultsToEmpty: true, _) = kind { - return ExprSyntax("[]") - } - guard let token = token, isToken else { - return buildableType.defaultValue + public var initializableBuilder: InitializableBuilder? { + guard self.isNode, let builderInitializableType = BUILDER_INITIALIZABLE_TYPES[self.syntaxNodeKind] else { + return nil } - if token.text != nil { - return ExprSyntax(".\(token.identifier)Token()") + guard let builderInitializableType, builderInitializableType != self.syntaxNodeKind else { + return .sameType } - if case .token(let choices, _, _) = kind, - case .keyword(let keyword) = choices.only - { - return ExprSyntax(".\(token.memberCallName)(.\(keyword.spec.memberCallName))") + guard let node = self.syntaxNodeKind.node else { + fatalError("Base name \(builderInitializableType) does not have a syntax node") } - return nil + return .otherType( + type: InitializableBuilder.Wrapper(syntaxNodeKind: builderInitializableType, isOptional: self.isOptional), + parameterLabel: node.layoutNode!.singleNonDefaultedChild.labelDeclName + ) } /// If the child node has a default value, return an expression of the form @@ -131,11 +93,14 @@ extension Child { } var preconditionChoices: [ExprSyntax] = [] - if buildableType.isOptional { + let baseRefExpr = DeclReferenceExprSyntax(baseName: .identifier(varName)) + let memberBaseRefExpr = self.isOptional ? "\(baseRefExpr)!" : ExprSyntax(baseRefExpr) + + if self.isOptional { preconditionChoices.append( ExprSyntax( SequenceExprSyntax { - DeclReferenceExprSyntax(baseName: .identifier(varName)) + baseRefExpr BinaryOperatorExprSyntax(text: "==") NilLiteralExprSyntax() } @@ -147,7 +112,7 @@ extension Child { ExprSyntax( SequenceExprSyntax { MemberAccessExprSyntax( - base: buildableType.forceUnwrappedIfNeeded(expr: DeclReferenceExprSyntax(baseName: .identifier(varName))), + base: memberBaseRefExpr, name: "text" ) BinaryOperatorExprSyntax(text: "==") diff --git a/CodeGeneration/Sources/Utils/SyntaxBuildableNode.swift b/CodeGeneration/Sources/Utils/SyntaxBuildableNode.swift index e7467150baa..e20ad2fc279 100644 --- a/CodeGeneration/Sources/Utils/SyntaxBuildableNode.swift +++ b/CodeGeneration/Sources/Utils/SyntaxBuildableNode.swift @@ -12,31 +12,6 @@ import SyntaxSupport -/// Extension to the ``Node`` type to provide functionality specific to -/// SwiftSyntaxBuilder. -extension Node { - /// The node's syntax kind as ``SyntaxBuildableType``. - public var type: SyntaxBuildableType { - SyntaxBuildableType(kind: .node(kind: kind)) - } - - /// The node's syntax kind as ``SyntaxBuildableType``. - public var baseType: SyntaxBuildableType { - if base == .syntaxCollection { - return SyntaxBuildableType(kind: .node(kind: .syntax)) - } else { - return SyntaxBuildableType(kind: .node(kind: base)) - } - } - - public static func from(type: SyntaxBuildableType) -> Node { - guard case .node(kind: let kind) = type.kind, let node = SYNTAX_NODE_MAP[kind] else { - fatalError("Base name \(type) does not have a syntax node") - } - return node - } -} - extension LayoutNode { /// Assuming this node has a single child without a default value, that child. public var singleNonDefaultedChild: Child { @@ -48,11 +23,11 @@ extension LayoutNode { extension CollectionNode { /// Assuming this node is a syntax collection, the type of its elements. - public var collectionElementType: SyntaxBuildableType { - if elementChoices.count > 1 { - return SyntaxBuildableType(kind: .node(kind: .syntax)) + public var collectionElementType: SyntaxNodeKind { + if let only = self.elementChoices.only { + only } else { - return SyntaxBuildableType(kind: .node(kind: elementChoices.first!)) + .syntax } } } diff --git a/CodeGeneration/Sources/Utils/SyntaxBuildableType.swift b/CodeGeneration/Sources/Utils/SyntaxBuildableType.swift deleted file mode 100644 index 954114c87bc..00000000000 --- a/CodeGeneration/Sources/Utils/SyntaxBuildableType.swift +++ /dev/null @@ -1,196 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2023 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 - -/// Wrapper around the syntax kind strings to provide functionality specific to -/// SwiftSyntaxBuilder. In particular, this includes the functionality to create -/// the `*Buildable`, `ExpressibleAs*` and `*Syntax` Swift types from the syntax -/// kind. -public struct SyntaxBuildableType: Hashable { - public let kind: SyntaxOrTokenNodeKind - public let isOptional: Bool - - public init(kind: SyntaxOrTokenNodeKind, isOptional: Bool = false) { - self.isOptional = isOptional - self.kind = kind - } - - /// Whether this is a token. - public var isToken: Bool { - switch kind { - case .token: - return true - default: - return false - } - } - - /// The token if this is a token. - public var token: TokenSpec? { - switch kind { - case .token(let token): - return token.spec - default: - return nil - } - } - - public var isBaseType: Bool { - switch kind { - case .node(let kind): - return kind.isBase - default: - return false - } - } - - /// If the type has a default value (because it is optional or a token - /// with fixed test), return an expression that can be used as the - /// default value for a function parameter. Otherwise, return `nil`. - public var defaultValue: ExprSyntax? { - if isOptional { - return ExprSyntax(NilLiteralExprSyntax()) - } else if let token = token { - if token.text != nil { - return ExprSyntax(".\(token.identifier)Token()") - } - } - return nil - } - - /// Whether the type is a syntax collection. - public var isSyntaxCollection: Bool { - kind == .node(kind: .syntaxCollection) - || (baseType.map(\.isSyntaxCollection) ?? false) - } - - /// Return the `Buildable` type that is the main entry point for building - /// SwiftSyntax trees using `SwiftSyntaxBuilder`. - /// - /// These names look as follows: - /// - For nodes: The node name, e.g. `IdentifierExpr` (these are implemented as structs) - /// - For base kinds: `Buildable`, e.g. `ExprBuildable` (these are implemented as protocols) - /// - For token: ``TokenSyntax`` (tokens don't have a dedicated type in SwiftSyntaxBuilder) - /// If the type is optional, the type is wrapped in an `OptionalType`. - public var buildable: TypeSyntax { - optionalWrapped(type: syntaxBaseName) - } - - /// Whether parameters of this type should be initializable by a result builder. - /// Used for certain syntax collections and block-like structures (e.g. `CodeBlock`, - /// `MemberDeclList`). - public var isBuilderInitializable: Bool { - switch kind { - case .node(let kind): - return BUILDER_INITIALIZABLE_TYPES.keys.contains(kind) - case .token: - return false - } - } - - /// A type suitable for initializing this type through a result builder (e.g. - /// returns `CodeBlockItemList` for `CodeBlock`) and otherwise itself. - public var builderInitializableType: Self { - switch kind { - case .node(let kind): - if case .some(.some(let builderInitializableType)) = BUILDER_INITIALIZABLE_TYPES[kind] { - return Self( - kind: .node(kind: builderInitializableType), - isOptional: isOptional - ) - } else { - return self - } - case .token: - return self - } - } - - /// The corresponding `*Syntax` type defined in the `SwiftSyntax` module, - /// without any question marks attached. - public var syntaxBaseName: TypeSyntax { - switch kind { - case .node(kind: let kind): - return kind.syntaxType - case .token: - return "TokenSyntax" - } - } - - /// The corresponding `*Syntax` type defined in the `SwiftSyntax` module, - /// which will eventually get built from `SwiftSyntaxBuilder`. If the type - /// is optional, this terminates with a `?`. - public var syntax: TypeSyntax { - return optionalWrapped(type: syntaxBaseName) - } - - /// The type that is used for parameters in SwiftSyntaxBuilder that take this - /// type of syntax node. - public var parameterBaseType: TypeSyntax { - if isBaseType { - return "\(syntaxBaseName)Protocol" - } else { - return syntaxBaseName - } - } - - /// Assuming that this is a collection type, the non-optional type of the result builder - /// that can be used to build the collection. - public var resultBuilderType: TypeSyntax { - switch kind { - case .node(kind: let kind): - return TypeSyntax("\(raw: kind.rawValue.withFirstCharacterUppercased)Builder") - case .token: - preconditionFailure("Tokens cannot be constructed using result builders") - } - } - - /// If this type is not a base kind, its base type (see `SyntaxBuildableNode.base_type()`), - /// otherwise `nil`. - public var baseType: Self? { - if !isBaseType && !isToken { - return Node.from(type: self).baseType - } else { - return nil - } - } - - /// Wraps a type in an optional depending on whether `isOptional` is true. - public func optionalWrapped(type: some TypeSyntaxProtocol) -> TypeSyntax { - if isOptional { - return TypeSyntax(OptionalTypeSyntax(wrappedType: type)) - } else { - return TypeSyntax(type) - } - } - - /// Wraps a type in an optional chaining depending on whether `isOptional` is true. - public func optionalChained(expr: some ExprSyntaxProtocol) -> ExprSyntax { - if isOptional { - return ExprSyntax(OptionalChainingExprSyntax(expression: expr)) - } else { - return ExprSyntax(expr) - } - } - - /// Wraps a type in a force unwrap expression depending on whether `isOptional` is true. - public func forceUnwrappedIfNeeded(expr: some ExprSyntaxProtocol) -> ExprSyntax { - if isOptional { - return ExprSyntax(ForceUnwrapExprSyntax(expression: expr)) - } else { - return ExprSyntax(expr) - } - } -} diff --git a/CodeGeneration/Sources/generate-swift-syntax/ChildNodeChoices.swift b/CodeGeneration/Sources/generate-swift-syntax/ChildNodeChoices.swift index f69d43b06e0..faf1805ea9a 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/ChildNodeChoices.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/ChildNodeChoices.swift @@ -15,33 +15,12 @@ import SwiftSyntaxBuilder import SyntaxSupport struct ChildNodeChoices { - struct Choice: IdentifierConvertible, TypeConvertible { - var documentation: SwiftSyntax.Trivia + @dynamicMemberLookup + struct Choice { + var node: any NodeChoiceConvertible - 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 + subscript(dynamicMember keyPath: KeyPath) -> T { + self.node[keyPath: keyPath] } } @@ -83,7 +62,7 @@ extension Node { case .nodeChoices(let choices): return ChildNodeChoices( name: child.syntaxChoicesType, - choices: choices.map { ChildNodeChoices.Choice($0, forRaw: forRaw) } + choices: choices.map { ChildNodeChoices.Choice(node: forRaw ? $0.raw : $0) } ) default: return nil @@ -94,7 +73,7 @@ extension Node { ChildNodeChoices( name: "Element", choices: node.elementChoices.map { - ChildNodeChoices.Choice(SYNTAX_NODE_MAP[$0]!, forRaw: forRaw) + ChildNodeChoices.Choice(node: forRaw ? $0.node!.raw : $0.node!) } ) ] @@ -117,7 +96,7 @@ extension ChildNodeChoices.Choice { } func baseTypeInitDecl(hasArgumentName: Bool) -> InitializerDeclSyntax? { - guard self.isBase else { + guard self.isBaseType else { return nil } let firstName: SyntaxNodeString diff --git a/CodeGeneration/Sources/generate-swift-syntax/LayoutNode+Extensions.swift b/CodeGeneration/Sources/generate-swift-syntax/LayoutNode+Extensions.swift index cb802c0b5e7..5611e399189 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/LayoutNode+Extensions.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/LayoutNode+Extensions.swift @@ -119,27 +119,27 @@ extension LayoutNode { childName = child.identifier } - if child.buildableType.isBuilderInitializable { + if let initializableBuilder = child.initializableBuilder { // Allow initializing certain syntax collections with result builders shouldCreateInitializer = true - let builderInitializableType = child.buildableType.builderInitializableType - if child.buildableType.builderInitializableType != child.buildableType { - let param = Node.from(type: child.buildableType).layoutNode!.singleNonDefaultedChild + let syntaxNodeConvertible: any SyntaxNodeConvertible + switch initializableBuilder { + case .otherType(let type, let parameterLabel): if child.isOptional { - produceExpr = ExprSyntax( - "\(childName)Builder().map { \(child.buildableType.syntaxBaseName)(\(param.labelDeclName): $0) }" - ) + produceExpr = + "\(childName)Builder().map { \(child.syntaxType)(\(parameterLabel): $0) }" } else { - produceExpr = ExprSyntax( - "\(child.buildableType.syntaxBaseName)(\(param.labelDeclName): \(childName)Builder())" - ) + produceExpr = + "\(child.syntaxType)(\(parameterLabel): \(childName)Builder())" } - } else { - produceExpr = ExprSyntax("\(childName)Builder()") + syntaxNodeConvertible = type + case .sameType: + produceExpr = "\(childName)Builder()" + syntaxNodeConvertible = child } builderParameters.append( FunctionParameterSyntax( - "@\(builderInitializableType.resultBuilderType) \(childName)Builder: () throws-> \(builderInitializableType.syntax)" + "@\(syntaxNodeConvertible.resultBuilderType) \(childName)Builder: () throws-> \(syntaxNodeConvertible.actualType)" ) ) } else { @@ -151,7 +151,7 @@ extension LayoutNode { FunctionParameterSyntax( firstName: childName.nonVarCallNameOrLabelDeclName, colon: .colonToken(), - type: child.parameterType, + type: child.parameterAnyType, defaultValue: child.defaultInitialization ) ) @@ -205,8 +205,9 @@ fileprivate func convertFromSyntaxProtocolToSyntaxType( childName = child.identifier } - if child.buildableType.isBaseType && !child.kind.isNodeChoices { - return ExprSyntax("\(child.buildableType.syntaxBaseName)(fromProtocol: \(childName.declNameOrVarCallName))") + if child.isBaseNode { + return "\(child.syntaxType)(fromProtocol: \(childName))" + } else { + return "\(childName)" } - return ExprSyntax("\(childName.declNameOrVarCallName)") } diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftparser/LayoutNodesParsableFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftparser/LayoutNodesParsableFile.swift index 06ff2069251..38f35f32160 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftparser/LayoutNodesParsableFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftparser/LayoutNodesParsableFile.swift @@ -38,7 +38,7 @@ let layoutNodesParsableFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { if let parserFunction = node.parserFunction { DeclSyntax( """ - extension \(node.kind.syntaxType): SyntaxParseable { + extension \(node.syntaxType): SyntaxParseable { public static func parse(from parser: inout Parser) -> Self { // Keep the parser alive so that the arena in which `raw` is allocated // doesn’t get deallocated before we have a chance to create a syntax node diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftparser/ParserTokenSpecSetFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftparser/ParserTokenSpecSetFile.swift index b98a8024348..e2b11e225c6 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftparser/ParserTokenSpecSetFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftparser/ParserTokenSpecSetFile.swift @@ -40,7 +40,7 @@ let parserTokenSpecSetFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { for layoutNode in SYNTAX_NODES.compactMap(\.layoutNode) { for child in layoutNode.children { if case let .token(choices, _, _) = child.kind, choices.count > 1 { - try! ExtensionDeclSyntax("extension \(layoutNode.kind.syntaxType)") { + try! ExtensionDeclSyntax("extension \(layoutNode.syntaxType)") { try EnumDeclSyntax( """ @_spi(Diagnostics) diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftparserdiagnostics/ChildNameForDiagnosticsFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftparserdiagnostics/ChildNameForDiagnosticsFile.swift index 8fb331984ba..631703d1b77 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftparserdiagnostics/ChildNameForDiagnosticsFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftparserdiagnostics/ChildNameForDiagnosticsFile.swift @@ -33,7 +33,7 @@ let childNameForDiagnosticFile = SourceFileSyntax(leadingTrivia: copyrightHeader for node in NON_BASE_SYNTAX_NODES.compactMap(\.layoutNode) { for child in node.children { if let nameForDiagnostics = child.nameForDiagnostics { - SwitchCaseSyntax("case \\\(node.type.syntaxBaseName).\(child.memberCallName):") { + SwitchCaseSyntax("case \\\(node.syntaxType).\(child.memberCallName):") { StmtSyntax(#"return "\#(raw: nameForDiagnostics)""#) } } diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/ChildNameForKeyPathFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/ChildNameForKeyPathFile.swift index 39e27eddc5e..c70d251db29 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/ChildNameForKeyPathFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/ChildNameForKeyPathFile.swift @@ -28,10 +28,10 @@ let childNameForKeyPathFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { for node in NON_BASE_SYNTAX_NODES.compactMap(\.layoutNode) { for child in node.children { SwitchCaseSyntax( - """ - case \\\(node.type.syntaxBaseName).\(child.memberCallName): - return \(literal: child.identifier.description) - """ + #""" + case \\#(node.syntaxType).\#(child.memberCallName): + return \#(literal: child.identifier.description) + """# ) } } diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxNodesFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxNodesFile.swift index cdc29c9cad5..c2d418ce659 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxNodesFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxNodesFile.swift @@ -18,24 +18,24 @@ import Utils func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax { return SourceFileSyntax(leadingTrivia: copyrightHeader) { for node in SYNTAX_NODES - where node.kind.isBase - && nodesStartingWith.contains(node.kind.syntaxType.description.droppingLeadingUnderscores.first!) - && !node.kind.isDeprecated + where node.isBaseType + && nodesStartingWith.contains(node.syntaxType.description.droppingLeadingUnderscores.first!) + && !node.isDeprecated { DeclSyntax( """ \(node.apiAttributes(forRaw: true))\ - public protocol \(node.kind.raw.protocolType): \(node.base.raw.protocolType) {} + public protocol \(node.raw.protocolType): \(node.baseKind.raw.protocolType) {} """ ) } for node in SYNTAX_NODES - where nodesStartingWith.contains(node.kind.syntaxType.description.droppingLeadingUnderscores.first!) { + where nodesStartingWith.contains(node.syntaxType.description.droppingLeadingUnderscores.first!) { try! StructDeclSyntax( """ \(node.apiAttributes(forRaw: true))\ - public struct \(node.kind.raw.syntaxType): \(node.kind.isBase ? node.kind.raw.protocolType : node.base.raw.protocolType) + public struct \(node.raw.syntaxType): \(node.isBaseType ? node.raw.protocolType : node.baseKind.raw.protocolType) """ ) { for childNodeChoices in node.childrenNodeChoices(forRaw: true) { @@ -52,10 +52,10 @@ func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax { ) try FunctionDeclSyntax("public static func isKindOf(_ raw: RawSyntax) -> Bool") { - if node.kind.isBase { + if node.isBaseType { let cases = SwitchCaseItemListSyntax { - for n in SYNTAX_NODES where n.base == node.kind { + for n in SYNTAX_NODES where n.baseKind == node.kind { SwitchCaseItemSyntax( pattern: ExpressionPatternSyntax( expression: ExprSyntax(".\(n.memberCallName)") @@ -105,10 +105,10 @@ func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax { """ ) - if node.kind.isBase { + if node.isBaseType { DeclSyntax( """ - public init(_ other: some \(node.kind.raw.protocolType)) { + public init(_ other: some \(node.raw.protocolType)) { self.init(unchecked: other.raw) } """ @@ -116,10 +116,10 @@ func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax { } if let node = node.collectionNode { - let element = node.elementChoices.only != nil ? node.elementChoices.only!.raw.syntaxType : "Element" + let element = node.elementChoices.only?.raw.syntaxType DeclSyntax( """ - public init(elements: [\(element)], arena: __shared SyntaxArena) { + public init(elements: [\(element ?? "Element")], arena: __shared SyntaxArena) { let raw = RawSyntax.makeLayout( kind: .\(node.memberCallName), uninitializedCount: elements.count, arena: arena) { layout in guard var ptr = layout.baseAddress else { return } @@ -133,13 +133,23 @@ func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax { """ ) - DeclSyntax( - """ - public var elements: [Raw\(node.collectionElementType.syntaxBaseName)] { - layoutView.children.map { Raw\(node.collectionElementType.syntaxBaseName)(raw: $0!) } - } - """ - ) + if let element { + DeclSyntax( + """ + public var elements: [\(element)] { + layoutView.children.map { \(element)(raw: $0!) } + } + """ + ) + } else { + DeclSyntax( + """ + public var elements: [Element] { + layoutView.children.map { Element($0!)! } + } + """ + ) + } } if let node = node.layoutNode { @@ -149,7 +159,7 @@ func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax { firstName: child.isUnexpectedNodes ? .wildcardToken(trailingTrivia: .space) : child.labelDeclName, secondName: child.isUnexpectedNodes ? child.labelDeclName : nil, colon: .colonToken(), - type: child.rawParameterType, + type: child.raw.parameterSomeType, defaultValue: child.isUnexpectedNodes ? child.defaultInitialization : nil ) } @@ -185,16 +195,20 @@ func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax { } for (index, child) in node.children.enumerated() { + let childNode = "layoutView.children[\(index)]" + try VariableDeclSyntax( - "public var \(child.varDeclName): Raw\(child.buildableType.buildable)" + "public var \(child.varDeclName): \(child.raw.actualType)" ) { let exclamationMark = child.isOptional ? "" : "!" - - if child.syntaxNodeKind == .syntax { - ExprSyntax("layoutView.children[\(raw: index)]\(raw: exclamationMark)") - } else { + switch child.kind { + case .node(kind: .syntax): + ExprSyntax("\(raw: childNode)\(raw: exclamationMark)") + case .nodeChoices: + ExprSyntax("\(raw: childNode).flatMap(\(child.raw.syntaxType).init)\(raw: exclamationMark)") + default: ExprSyntax( - "layoutView.children[\(raw: index)].map(\(child.syntaxNodeKind.raw.syntaxType).init(raw:))\(raw: exclamationMark)" + "\(raw: childNode).map(\(child.raw.syntaxType).init(raw:))\(raw: exclamationMark)" ) } } @@ -226,23 +240,3 @@ private extension ChildNodeChoices { } } } - -fileprivate extension Child { - var rawParameterType: TypeSyntax { - var paramType: TypeSyntax - if !kind.isNodeChoicesEmpty { - paramType = "\(syntaxChoicesType)" - } else if hasBaseType && !isOptional { - // we restrict the use of generic type to non-optional parameter types, otherwise call sites would no longer be - // able to just pass `nil` to this parameter without specializing `(some RawSyntaxNodeProtocol)?` - // - // 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.raw.protocolType)" - } else { - 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 edd459d3c0f..4aad38d1f49 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxValidationFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxValidationFile.swift @@ -170,26 +170,6 @@ let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: copyrightHead """# ) - DeclSyntax( - #""" - func assertAnyHasNoError(_ nodeKind: SyntaxKind, _ index: Int, _ errors: [ValidationError?]) { - let nonNilErrors = errors.compactMap({ $0 }) - if nonNilErrors.count == errors.count, let firstError = nonNilErrors.first { - let (file, line) = firstError.fileAndLine - assertionFailure(""" - Error validating child at index \(index) of \(nodeKind): - Node did not satisfy any node choice requirement. - Validation failures: - \(nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n")) - - See "RawSyntax Validation" in CONTRIBUTING.md to reproduce the failure locally. - """, file: file, line: line) - _ = 1 - } - } - """# - ) - try SwitchExprSyntax("switch kind") { SwitchCaseSyntax( """ @@ -205,16 +185,9 @@ let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: copyrightHead for (index, child) in node.children.enumerated() { switch child.kind { case .nodeChoices(let choices): - let verifiedChoices = ArrayExprSyntax { - ArrayElementSyntax( - leadingTrivia: .newline, - expression: ExprSyntax( - "verify(layout[\(raw: index)], as: Raw\(child.buildableType.buildable).self)" - ) - ) - } - - ExprSyntax("assertAnyHasNoError(kind, \(raw: index), \(verifiedChoices))") + ExprSyntax( + "assertNoError(kind, \(raw: index), verify(layout[\(raw: index)], as: \(node.raw.syntaxType).\(child.raw.actualType).self))" + ) case .token(choices: let choices, requiresLeadingSpace: _, requiresTrailingSpace: _): let choices = ArrayExprSyntax { for choice in choices { @@ -227,12 +200,12 @@ let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: copyrightHead } } let verifyCall = ExprSyntax( - "verify(layout[\(raw: index)], as: Raw\(child.buildableType.buildable).self, tokenChoices: \(choices))" + "verify(layout[\(raw: index)], as: \(child.raw.actualType).self, tokenChoices: \(choices))" ) ExprSyntax("assertNoError(kind, \(raw: index), \(verifyCall))") default: ExprSyntax( - "assertNoError(kind, \(raw: index), verify(layout[\(raw: index)], as: Raw\(child.buildableType.buildable).self))" + "assertNoError(kind, \(raw: index), verify(layout[\(raw: index)], as: \(child.raw.actualType).self))" ) } } @@ -243,16 +216,9 @@ let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: copyrightHead "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.raw.syntaxType).self)") - ) - } - } - ExprSyntax("assertAnyHasNoError(kind, index, \(verifiedChoices))") + ExprSyntax( + "assertNoError(kind, index, verify(element, as: \(node.raw.syntaxType).Element.self))" + ) } } } diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RenamedChildrenCompatibilityFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RenamedChildrenCompatibilityFile.swift index 455f4d18602..3da03eed9a1 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RenamedChildrenCompatibilityFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RenamedChildrenCompatibilityFile.swift @@ -17,17 +17,13 @@ import Utils let renamedChildrenCompatibilityFile = try! SourceFileSyntax(leadingTrivia: copyrightHeader) { for layoutNode in SYNTAX_NODES.compactMap(\.layoutNode).filter({ $0.children.hasDeprecatedChild }) { - try ExtensionDeclSyntax("extension \(layoutNode.type.syntaxBaseName)") { + try ExtensionDeclSyntax("extension \(layoutNode.syntaxType)") { for child in layoutNode.children { if let deprecatedVarName = child.deprecatedVarName { - let childType: TypeSyntax = - child.kind.isNodeChoicesEmpty ? child.syntaxNodeKind.syntaxType : child.syntaxChoicesType - let type = child.isOptional ? TypeSyntax("\(childType)?") : childType - DeclSyntax( """ @available(*, deprecated, renamed: "\(child.identifier)") - public var \(deprecatedVarName): \(type) { + public var \(deprecatedVarName): \(child.actualType) { get { return \(child.baseCallName) } @@ -37,7 +33,7 @@ let renamedChildrenCompatibilityFile = try! SourceFileSyntax(leadingTrivia: copy } """ ) - if let childNode = SYNTAX_NODE_MAP[child.syntaxNodeKind]?.collectionNode, + if let childNode = child.node?.collectionNode, !child.isUnexpectedNodes, case .collection( kind: _, @@ -47,12 +43,12 @@ let renamedChildrenCompatibilityFile = try! SourceFileSyntax(leadingTrivia: copy ) = child.kind, let deprecatedCollectionElementName { - let childEltType = childNode.collectionElementType.syntaxBaseName + let childEltType = childNode.collectionElementType.syntaxType DeclSyntax( """ @available(*, deprecated, renamed: "add\(raw: collectionElementName)") - public func add\(raw: deprecatedCollectionElementName)(_ element: \(childEltType)) -> \(layoutNode.kind.syntaxType) { + public func add\(raw: deprecatedCollectionElementName)(_ element: \(childEltType)) -> \(layoutNode.actualType) { return add\(raw: collectionElementName)(element) } """ @@ -75,7 +71,7 @@ let renamedChildrenCompatibilityFile = try! SourceFileSyntax(leadingTrivia: copy } }.joined(separator: "") - let renamedName = "\(layoutNode.type.syntaxBaseName)(leadingTrivia:\(renamedArguments)trailingTrivia:)" + let renamedName = "\(layoutNode.syntaxType)(leadingTrivia:\(renamedArguments)trailingTrivia:)" try! InitializerDeclSyntax( """ diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SwiftSyntaxDoccIndex.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SwiftSyntaxDoccIndex.swift index d81e1b11ca3..ecac3fac31e 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SwiftSyntaxDoccIndex.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SwiftSyntaxDoccIndex.swift @@ -39,8 +39,8 @@ let nodesSections: String = { let baseTypes = ["\(baseKind.syntaxType)", "\(baseKind.syntaxType)Protocol", "Missing\(baseKind.syntaxType)"] let leafTypes = SYNTAX_NODES - .filter({ $0.base == baseKind && !$0.kind.isMissing && !$0.isExperimental && !$0.kind.isDeprecated }) - .map(\.kind.syntaxType.description) + .filter({ $0.baseKind == baseKind && !$0.kind.isMissing && !$0.isExperimental && !$0.isDeprecated }) + .map(\.syntaxType.description) addSection(heading: heading, types: baseTypes + leafTypes) } @@ -54,9 +54,9 @@ let nodesSections: String = { guard let node = node.collectionNode, !node.isExperimental else { return [] } - return [node.kind.syntaxType.description] + return [node.syntaxType.description] + node.elementChoices - .filter { SYNTAX_NODE_MAP[$0] != nil && !SYNTAX_NODE_MAP[$0]!.isExperimental && !$0.isDeprecated } + .filter { $0.node != nil && !$0.node!.isExperimental && !$0.isDeprecated } .map(\.syntaxType.description) .filter { !handledSyntaxTypes.contains($0) } }) @@ -64,13 +64,13 @@ let nodesSections: String = { addSection( heading: "Attributes", - types: ATTRIBUTE_NODES.filter({ !$0.isExperimental && !$0.kind.isDeprecated }).map(\.kind.syntaxType.description) + types: ATTRIBUTE_NODES.filter({ !$0.isExperimental && !$0.isDeprecated }).map(\.syntaxType.description) .sorted() ) addSection( heading: "Miscellaneous Syntax", - types: SYNTAX_NODES.filter({ !$0.isExperimental && !$0.kind.isDeprecated }).map(\.kind.syntaxType.description) + types: SYNTAX_NODES.filter({ !$0.isExperimental && !$0.isDeprecated }).map(\.syntaxType.description) .filter({ !handledSyntaxTypes.contains($0) }) diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxAnyVisitorFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxAnyVisitorFile.swift index 08cd5619355..535d13db135 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxAnyVisitorFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxAnyVisitorFile.swift @@ -79,11 +79,11 @@ let syntaxAnyVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { """ ) - for node in SYNTAX_NODES where !node.kind.isBase { + for node in SYNTAX_NODES where !node.isBaseType { DeclSyntax( """ - \(node.apiAttributes())\ - override open func visit(_ node: \(node.kind.syntaxType)) -> SyntaxVisitorContinueKind { + \(node.apiAttributes)\ + override open func visit(_ node: \(node.syntaxType)) -> SyntaxVisitorContinueKind { return visitAny(node._syntaxNode) } """ @@ -91,8 +91,8 @@ let syntaxAnyVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { DeclSyntax( """ - \(node.apiAttributes())\ - override open func visitPost(_ node: \(node.kind.syntaxType)) { + \(node.apiAttributes)\ + override open func visitPost(_ node: \(node.syntaxType)) { visitAnyPost(node._syntaxNode) } """ diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxBaseNodesFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxBaseNodesFile.swift index e308b4364e6..9269822abdb 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxBaseNodesFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxBaseNodesFile.swift @@ -16,28 +16,28 @@ import SyntaxSupport import Utils let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { - for node in SYNTAX_NODES where node.kind.isBase { + for node in SYNTAX_NODES where node.isBaseType { let documentation = SwiftSyntax.Trivia(joining: [ node.documentation, node.subtypes, ]) DeclSyntax( """ - // MARK: - \(node.kind.syntaxType) + // MARK: - \(node.syntaxType) - /// Protocol to which all \(raw: node.kind.doccLink) nodes conform. + /// Protocol to which all \(raw: node.doccLink) nodes conform. /// - /// Extension point to add common methods to all \(raw: node.kind.doccLink) nodes. + /// Extension point to add common methods to all \(raw: node.doccLink) nodes. /// /// - Warning: Do not conform to this protocol yourself. - \(node.apiAttributes())\ - public protocol \(node.kind.protocolType): \(node.base.protocolType) {} + \(node.apiAttributes)\ + public protocol \(node.protocolType): \(node.baseKind.protocolType) {} """ ) DeclSyntax( #""" - /// Extension of ``\#(node.kind.protocolType)`` to provide casting methods. + /// Extension of ``\#(node.protocolType)`` to provide casting methods. /// /// These methods enable casting between syntax node types within the same /// base node protocol hierarchy (e.g., ``DeclSyntaxProtocol``). @@ -45,7 +45,7 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { /// While ``SyntaxProtocol`` offers general casting methods (``SyntaxProtocol.as(_:)``, /// ``SyntaxProtocol.is(_:)``, and ``SyntaxProtocol.cast(_:)``), these often aren't /// appropriate for use on types conforming to a specific base node protocol - /// like ``\#(node.kind.protocolType)``. That's because at this level, + /// like ``\#(node.protocolType)``. That's because at this level, /// we know that the cast to another base node type (e.g., ``DeclSyntaxProtocol`` /// when working with ``ExprSyntaxProtocol``) is guaranteed to fail. /// @@ -53,18 +53,18 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { /// of these casting methods that are restricted to the same base node type. /// Furthermore, it marks the inherited casting methods from ``SyntaxProtocol`` as /// deprecated, indicating that they will always fail when used in this context. - extension \#(node.kind.protocolType) { + extension \#(node.protocolType) { /// Checks if the current syntax node can be cast to a given specialized syntax type. /// /// - Returns: `true` if the node can be cast, `false` otherwise. - public func `is`(_ syntaxType: S.Type) -> Bool { + public func `is`(_ syntaxType: S.Type) -> Bool { return self.as(syntaxType) != nil } /// Attempts to cast the current syntax node to a given specialized syntax type. /// /// - Returns: An instance of the specialized type, or `nil` if the cast fails. - public func `as`(_ syntaxType: S.Type) -> S? { + public func `as`(_ syntaxType: S.Type) -> S? { return S.init(self) } @@ -73,71 +73,71 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { /// - 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. - public func cast(_ syntaxType: S.Type) -> S { + public func cast(_ syntaxType: S.Type) -> S { return self.as(S.self)! } - /// Checks if the current syntax node can be upcast to its base node type (\#(raw: node.kind.doccLink)). + /// Checks if the current syntax node can be upcast to its base node type (\#(raw: node.doccLink)). /// /// - Returns: `true` since the node can always be upcast to its base node. @available(*, deprecated, message: "This cast will always succeed") - public func `is`(_ syntaxType: \#(node.kind.syntaxType).Type) -> Bool { + public func `is`(_ syntaxType: \#(node.syntaxType).Type) -> Bool { return true } - /// Attempts to upcast the current syntax node to its base node type (\#(raw: node.kind.doccLink)). + /// Attempts to upcast the current syntax node to its base node type (\#(raw: node.doccLink)). /// /// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type. - @available(*, deprecated, message: "Use `\#(node.kind.syntaxType).init` for upcasting") - public func `as`(_ syntaxType: \#(node.kind.syntaxType).Type) -> \#(node.kind.syntaxType)? { - return \#(node.kind.syntaxType)(self) + @available(*, deprecated, message: "Use `\#(node.syntaxType).init` for upcasting") + public func `as`(_ syntaxType: \#(node.syntaxType).Type) -> \#(node.syntaxType)? { + return \#(node.syntaxType)(self) } - /// Force-upcast the current syntax node to its base node type (\#(raw: node.kind.doccLink)). + /// Force-upcast the current syntax node to its base node type (\#(raw: node.doccLink)). /// /// - Returns: The base node created from the current syntax node, as the node can always be upcast to its base type. - @available(*, deprecated, message: "Use `\#(node.kind.syntaxType).init` for upcasting") - public func cast(_ syntaxType: \#(node.kind.syntaxType).Type) -> \#(node.kind.syntaxType) { - return \#(node.kind.syntaxType)(self) + @available(*, deprecated, message: "Use `\#(node.syntaxType).init` for upcasting") + public func cast(_ syntaxType: \#(node.syntaxType).Type) -> \#(node.syntaxType) { + return \#(node.syntaxType)(self) } /// Checks if the current syntax node can be cast to a given node type from a base node protocol hierarchy other - /// than ``\#(node.kind.protocolType)``. + /// than ``\#(node.protocolType)``. /// /// - Returns: `true` if the node can be cast, `false` otherwise. /// - /// - Note: In most cases, this is comparing a ``\#(node.kind.protocolType)`` to a node that is not a - /// ``\#(node.kind.protocolType)``, which will always fail. If the `syntaxType` argument is a generic type, - /// constrain it to ``\#(node.kind.protocolType)`` instead of ``SyntaxProtocol``. - @available(*, deprecated, message: "Type argument should be part of the '\#(node.kind.protocolType)' hierarchy") + /// - Note: In most cases, this is comparing a ``\#(node.protocolType)`` to a node that is not a + /// ``\#(node.protocolType)``, which will always fail. If the `syntaxType` argument is a generic type, + /// constrain it to ``\#(node.protocolType)`` instead of ``SyntaxProtocol``. + @available(*, deprecated, message: "Type argument should be part of the '\#(node.protocolType)' hierarchy") public func `is`(_ syntaxType: S.Type) -> Bool { return self.as(syntaxType) != nil } /// Attempts to cast the current syntax node to a given node type from the a base node protocol hierarchy other than - /// ``\#(node.kind.protocolType)``. + /// ``\#(node.protocolType)``. /// /// - Returns: An instance of the specialized type, or `nil` if the cast fails. /// - /// - Note: In most cases, this is casting a ``\#(node.kind.protocolType)`` to a node that is not a - /// ``\#(node.kind.protocolType)``, which will always fail. If the `syntaxType` argument is a generic type, - /// constrain it to ``\#(node.kind.protocolType)`` instead of ``SyntaxProtocol``. - @available(*, deprecated, message: "Type argument should be part of the '\#(node.kind.protocolType)' hierarchy") + /// - Note: In most cases, this is casting a ``\#(node.protocolType)`` to a node that is not a + /// ``\#(node.protocolType)``, which will always fail. If the `syntaxType` argument is a generic type, + /// constrain it to ``\#(node.protocolType)`` instead of ``SyntaxProtocol``. + @available(*, deprecated, message: "Type argument should be part of the '\#(node.protocolType)' hierarchy") public func `as`(_ syntaxType: S.Type) -> S? { return S.init(self) } /// Force-casts the current syntax node to a given node type from a base node protocol hierarchy other than - /// ``\#(node.kind.protocolType)``. + /// ``\#(node.protocolType)``. /// /// - 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. /// - /// - Note: In most cases, this is casting a ``\#(node.kind.protocolType)`` to a node that is not a - /// ``\#(node.kind.protocolType)``, which will always fail. If the `syntaxType` argument is a generic type, - /// constrain it to ``\#(node.kind.protocolType)`` instead of ``SyntaxProtocol``. - @available(*, deprecated, message: "Type argument should be part of the '\#(node.kind.protocolType)' hierarchy") + /// - Note: In most cases, this is casting a ``\#(node.protocolType)`` to a node that is not a + /// ``\#(node.protocolType)``, which will always fail. If the `syntaxType` argument is a generic type, + /// constrain it to ``\#(node.protocolType)`` instead of ``SyntaxProtocol``. + @available(*, deprecated, message: "Type argument should be part of the '\#(node.protocolType)' hierarchy") public func cast(_ syntaxType: S.Type) -> S { return self.as(S.self)! } @@ -149,11 +149,11 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { DeclSyntax( """ /// Check whether the non-type erased version of this syntax node conforms to - /// \(node.kind.protocolType). + /// \(node.protocolType). /// /// - Note: This will incur an existential conversion. - public func isProtocol(_: \(node.kind.protocolType).Protocol) -> Bool { - return self.asProtocol(\(node.kind.protocolType).self) != nil + public func isProtocol(_: \(node.protocolType).Protocol) -> Bool { + return self.asProtocol(\(node.protocolType).self) != nil } """ ) @@ -161,11 +161,11 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { DeclSyntax( """ /// Return the non-type erased version of this syntax node if it conforms to - /// \(node.kind.protocolType). Otherwise return nil. + /// \(node.protocolType). Otherwise return nil. /// /// - Note: This will incur an existential conversion. - public func asProtocol(_: \(node.kind.protocolType).Protocol) -> \(node.kind.protocolType)? { - return self.asProtocol(SyntaxProtocol.self) as? \(node.kind.protocolType) + public func asProtocol(_: \(node.protocolType).Protocol) -> \(node.protocolType)? { + return self.asProtocol(SyntaxProtocol.self) as? \(node.protocolType) } """ ) @@ -174,16 +174,16 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { try! StructDeclSyntax( """ \(documentation)\ - \(node.apiAttributes())\ - public struct \(node.kind.syntaxType): \(node.kind.protocolType), SyntaxHashable + \(node.apiAttributes)\ + public struct \(node.syntaxType): \(node.protocolType), SyntaxHashable """ ) { DeclSyntax("public let _syntaxNode: Syntax") DeclSyntax( """ - /// Create a \(raw: node.kind.doccLink) node from a specialized syntax node. - public init(_ syntax: __shared some \(node.kind.protocolType)) { + /// Create a \(raw: node.doccLink) node from a specialized syntax node. + public init(_ syntax: __shared some \(node.protocolType)) { // We know this cast is going to succeed. Go through init(_: SyntaxData) // to do a sanity check and verify the kind matches in debug builds and get // maximum performance in release builds. @@ -194,8 +194,8 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { DeclSyntax( """ - /// Create a \(raw: node.kind.doccLink) node from a specialized optional syntax node. - public init?(_ syntax: __shared (some \(node.kind.protocolType))?) { + /// Create a \(raw: node.doccLink) node from a specialized optional syntax node. + public init?(_ syntax: __shared (some \(node.protocolType))?) { guard let syntax = syntax else { return nil } self.init(syntax) } @@ -204,7 +204,7 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { DeclSyntax( """ - public init(fromProtocol syntax: __shared \(node.kind.protocolType)) { + public init(fromProtocol syntax: __shared \(node.protocolType)) { // We know this cast is going to succeed. Go through init(_: SyntaxData) // to do a sanity check and verify the kind matches in debug builds and get // maximum performance in release builds. @@ -215,8 +215,8 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { DeclSyntax( """ - /// Create a \(raw: node.kind.doccLink) node from a specialized optional syntax node. - public init?(fromProtocol syntax: __shared \(node.kind.protocolType)?) { + /// Create a \(raw: node.doccLink) node from a specialized optional syntax node. + public init?(fromProtocol syntax: __shared \(node.protocolType)?) { guard let syntax = syntax else { return nil } self.init(fromProtocol: syntax) } @@ -229,7 +229,7 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { SwitchCaseSyntax( label: .case( SwitchCaseLabelSyntax { - for childNode in SYNTAX_NODES where childNode.base == node.kind { + for childNode in SYNTAX_NODES where childNode.baseKind == node.kind { SwitchCaseItemSyntax( pattern: ExpressionPatternSyntax( expression: ExprSyntax(".\(childNode.memberCallName)") @@ -251,12 +251,12 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { DeclSyntax( """ - /// Syntax nodes always conform to `\(node.kind.protocolType)`. This API is just + /// Syntax nodes always conform to `\(node.protocolType)`. This API is just /// added for consistency. /// /// - Note: This will incur an existential conversion. @available(*, deprecated, message: "Expression always evaluates to true") - public func isProtocol(_: \(node.kind.protocolType).Protocol) -> Bool { + public func isProtocol(_: \(node.protocolType).Protocol) -> Bool { return true } """ @@ -267,18 +267,18 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { /// Return the non-type erased version of this syntax node. /// /// - Note: This will incur an existential conversion. - public func asProtocol(_: \(node.kind.protocolType).Protocol) -> \(node.kind.protocolType) { - return Syntax(self).asProtocol(\(node.kind.protocolType).self)! + public func asProtocol(_: \(node.protocolType).Protocol) -> \(node.protocolType) { + return Syntax(self).asProtocol(\(node.protocolType).self)! } """ ) try VariableDeclSyntax("public static var structure: SyntaxNodeStructure") { let choices = ArrayExprSyntax { - for childNode in SYNTAX_NODES where childNode.base == node.kind { + for childNode in SYNTAX_NODES where childNode.baseKind == node.kind { ArrayElementSyntax( leadingTrivia: .newline, - expression: ExprSyntax(".node(\(childNode.kind.syntaxType).self)") + expression: ExprSyntax(".node(\(childNode.syntaxType).self)") ) } } @@ -287,8 +287,8 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { } } - leafProtocolDecl(type: node.kind.leafProtocolType, inheritedType: node.kind.protocolType) - leafProtocolExtension(type: node.kind.leafProtocolType, inheritedType: node.kind.protocolType) + leafProtocolDecl(type: node.kind.leafProtocolType, inheritedType: node.protocolType) + leafProtocolExtension(type: node.kind.leafProtocolType, inheritedType: node.protocolType) } try! ExtensionDeclSyntax( @@ -308,7 +308,7 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { for node in NON_BASE_SYNTAX_NODES { ArrayElementSyntax( leadingTrivia: .newline, - expression: ExprSyntax(".node(\(node.kind.syntaxType).self)") + expression: ExprSyntax(".node(\(node.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 cfa32fde2c2..f0f2fdae0bd 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxCollectionsFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxCollectionsFile.swift @@ -27,8 +27,8 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { try! StructDeclSyntax( """ \(documentation)\ - \(node.node.apiAttributes())\ - public struct \(node.kind.syntaxType): SyntaxCollection, SyntaxHashable + \(node.apiAttributes)\ + public struct \(node.syntaxType): SyntaxCollection, SyntaxHashable """ ) { if let onlyElement = node.elementChoices.only { diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxEnumFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxEnumFile.swift index b1758f26295..13698261810 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxEnumFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxEnumFile.swift @@ -26,8 +26,8 @@ let syntaxEnumFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { for node in NON_BASE_SYNTAX_NODES { DeclSyntax( """ - \(node.apiAttributes())\ - case \(node.enumCaseDeclName)(\(node.kind.syntaxType)) + \(node.apiAttributes)\ + case \(node.enumCaseDeclName)(\(node.syntaxType)) """ ) } @@ -51,16 +51,16 @@ let syntaxEnumFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { for node in NON_BASE_SYNTAX_NODES { SwitchCaseSyntax("case .\(node.enumCaseCallName):") { - StmtSyntax("return .\(node.memberCallName)(\(node.kind.syntaxType)(self)!)") + StmtSyntax("return .\(node.memberCallName)(\(node.syntaxType)(self)!)") } } } } } - for base in SYNTAX_NODES where base.kind.isBase { + for base in SYNTAX_NODES where base.isBaseType { let baseKind = base.kind - let baseName = baseKind.rawValue.withFirstCharacterUppercased + let baseName = baseKind.nameInProperCase let enumType: TypeSyntax = "\(raw: baseName)SyntaxEnum" try! EnumDeclSyntax( @@ -69,11 +69,11 @@ let syntaxEnumFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { public enum \(enumType) """ ) { - for node in NON_BASE_SYNTAX_NODES where node.base == baseKind { + for node in NON_BASE_SYNTAX_NODES where node.baseKind == baseKind { DeclSyntax( """ - \(node.apiAttributes())\ - case \(node.enumCaseDeclName)(\(node.kind.syntaxType)) + \(node.apiAttributes)\ + case \(node.enumCaseDeclName)(\(node.syntaxType)) """ ) } @@ -91,9 +91,9 @@ let syntaxEnumFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { """ ) { try SwitchExprSyntax("switch raw.kind") { - for node in NON_BASE_SYNTAX_NODES where node.base == baseKind { + for node in NON_BASE_SYNTAX_NODES where node.baseKind == baseKind { SwitchCaseSyntax("case .\(node.enumCaseCallName):") { - StmtSyntax("return .\(node.memberCallName)(\(node.kind.syntaxType)(self)!)") + StmtSyntax("return .\(node.memberCallName)(\(node.syntaxType)(self)!)") } } SwitchCaseSyntax("default:") { diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxKindFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxKindFile.swift index 2403c551d7e..160ce51201c 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxKindFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxKindFile.swift @@ -26,7 +26,7 @@ let syntaxKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { for node in NON_BASE_SYNTAX_NODES { DeclSyntax( """ - \(node.apiAttributes())\ + \(node.apiAttributes)\ case \(node.enumCaseDeclName) """ ) @@ -34,7 +34,7 @@ let syntaxKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { try VariableDeclSyntax("public var isSyntaxCollection: Bool") { try SwitchExprSyntax("switch self") { - for node in SYNTAX_NODES where node.base == .syntaxCollection { + for node in SYNTAX_NODES where node.baseKind == .syntaxCollection { SwitchCaseSyntax("case .\(node.enumCaseCallName):") { StmtSyntax("return true") } @@ -68,7 +68,7 @@ let syntaxKindFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { for node in NON_BASE_SYNTAX_NODES { SwitchCaseSyntax("case .\(node.enumCaseCallName):") { - StmtSyntax("return \(node.kind.syntaxType).self") + StmtSyntax("return \(node.syntaxType).self") } } } diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodeCasting.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodeCasting.swift index fd4978f886b..2f560e32130 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodeCasting.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodeCasting.swift @@ -17,7 +17,7 @@ import SyntaxSupport extension ChildNodeChoices.Choice { @MemberBlockItemListBuilder var castingMethods: MemberBlockItemListSyntax { - if self.isBase { + if self.isBaseType { DeclSyntax( """ /// Checks if the current syntax node can be cast to the type conforming to the ``\(self.protocolType)`` protocol. diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodesFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodesFile.swift index 79770fa77eb..30e0fec427d 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodesFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodesFile.swift @@ -22,15 +22,15 @@ import Utils func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax { SourceFileSyntax(leadingTrivia: copyrightHeader) { for node in SYNTAX_NODES.compactMap(\.layoutNode) - where nodesStartingWith.contains(node.kind.syntaxType.description.droppingLeadingUnderscores.first!) { + where nodesStartingWith.contains(node.syntaxType.description.droppingLeadingUnderscores.first!) { // We are actually handling this node now try! StructDeclSyntax( """ - // MARK: - \(node.kind.syntaxType) + // MARK: - \(node.syntaxType) \(SwiftSyntax.Trivia(joining: [node.documentation, node.experimentalDocNote, node.grammar, node.containedIn]))\ - \(node.node.apiAttributes())\ - public struct \(node.kind.syntaxType): \(node.baseType.syntaxBaseName)Protocol, SyntaxHashable, \(node.base.leafProtocolType) + \(node.apiAttributes)\ + public struct \(node.syntaxType): \(node.baseKind.protocolType), SyntaxHashable, \(node.baseKind.leafProtocolType) """ ) { for childNodeChoices in node.node.childrenNodeChoices() { @@ -76,11 +76,10 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax { ) let layoutList = ArrayExprSyntax { for child in node.children { + let baseExpr = ExprSyntax("\(child.baseCallName)") ArrayElementSyntax( expression: MemberAccessExprSyntax( - base: child.buildableType.optionalChained( - expr: ExprSyntax("\(child.baseCallName)") - ), + base: child.isOptional ? "\(baseExpr)?" : baseExpr, period: .periodToken(), name: "raw" ) @@ -133,26 +132,25 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax { // Children properties // =================== - let childType: TypeSyntax = - child.kind.isNodeChoicesEmpty ? child.syntaxNodeKind.syntaxType : child.syntaxChoicesType - let type = child.isOptional ? TypeSyntax("\(childType)?") : TypeSyntax("\(childType)") - - try! VariableDeclSyntax( + try VariableDeclSyntax( """ \(child.documentation)\ - \(child.apiAttributes)public var \(child.varDeclName): \(type) + \(child.apiAttributes)\ + public var \(child.varDeclName): \(child.actualType) """ ) { AccessorDeclSyntax(accessorSpecifier: .keyword(.get)) { let optionalityMarker: TokenSyntax = child.isOptional ? .infixQuestionMarkToken() : .exclamationMarkToken() - StmtSyntax("return Syntax(self).child(at: \(raw: index))\(optionalityMarker).cast(\(childType).self)") + StmtSyntax( + "return Syntax(self).child(at: \(raw: index))\(optionalityMarker).cast(\(child.syntaxType).self)" + ) } AccessorDeclSyntax( """ set(value) { - self = Syntax(self).replacingChild(at: \(raw: index), with: Syntax(value), arena: SyntaxArena()).cast(\(node.kind.syntaxType).self) + self = Syntax(self).replacingChild(at: \(raw: index), with: Syntax(value), arena: SyntaxArena()).cast(\(node.syntaxType).self) } """ ) @@ -163,11 +161,11 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax { // =============== // We don't currently support adding elements to a specific unexpected collection. // If needed, this could be added in the future, but for now withUnexpected should be sufficient. - if let childNode = SYNTAX_NODE_MAP[child.syntaxNodeKind]?.collectionNode, + if let childNode = child.node?.collectionNode, !child.isUnexpectedNodes, case .collection(_, collectionElementName: let childElt, _, _) = child.kind { - let childEltType = childNode.collectionElementType.syntaxBaseName + let childEltType = childNode.collectionElementType.syntaxType DeclSyntax( """ @@ -179,7 +177,7 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax { /// - returns: A copy of the receiver with the provided `\(raw: childElt)` /// appended to its `\(child.identifier)` collection. @available(*, deprecated, message: "Use node.\(child.identifier).append(newElement) instead") - public func add\(raw: childElt)(_ element: \(childEltType)) -> \(node.kind.syntaxType) { + public func add\(raw: childElt)(_ element: \(childEltType)) -> \(node.syntaxType) { var collection: RawSyntax let arena = SyntaxArena() if let col = raw.layoutView!.children[\(raw: index)] { @@ -190,7 +188,7 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax { } return Syntax(self) .replacingChild(at: \(raw: index), with: collection, rawNodeArena: arena, allocationArena: arena) - .cast(\(node.kind.syntaxType).self) + .cast(\(node.syntaxType).self) } """ ) diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxRewriterFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxRewriterFile.swift index 865e8890f18..fde88931723 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxRewriterFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxRewriterFile.swift @@ -155,28 +155,28 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { """ ) - for node in SYNTAX_NODES where !node.kind.isBase { - if (node.base == .syntax || node.base == .syntaxCollection) && node.kind != .missing { + for node in SYNTAX_NODES where !node.isBaseNode { + if (node.baseKind == .syntax || node.baseKind == .syntaxCollection) && node.kind != .missing { DeclSyntax( """ - /// Visit a \(raw: node.kind.doccLink). + /// Visit a \(raw: node.doccLink). /// - Parameter node: the node that is being visited /// - Returns: the rewritten node - \(node.apiAttributes())\ - open func visit(_ node: \(node.kind.syntaxType)) -> \(node.kind.syntaxType) { - return visitChildren(node._syntaxNode).cast(\(node.kind.syntaxType).self) + \(node.apiAttributes)\ + open func visit(_ node: \(node.syntaxType)) -> \(node.syntaxType) { + return visitChildren(node._syntaxNode).cast(\(node.syntaxType).self) } """ ) } else { DeclSyntax( """ - /// Visit a \(raw: node.kind.doccLink). + /// Visit a \(raw: node.doccLink). /// - Parameter node: the node that is being visited /// - Returns: the rewritten node - \(node.apiAttributes())\ - open func visit(_ node: \(node.kind.syntaxType)) -> \(node.baseType.syntaxBaseName) { - return \(node.baseType.syntaxBaseName)(visitChildren(node._syntaxNode).cast(\(node.kind.syntaxType).self)) + \(node.apiAttributes)\ + open func visit(_ node: \(node.syntaxType)) -> \(node.baseKind.syntaxType) { + return \(node.baseKind.syntaxType)(visitChildren(node._syntaxNode).cast(\(node.syntaxType).self)) } """ ) @@ -184,18 +184,17 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { } for baseNode in SYNTAX_NODES - where baseNode.kind.isBase && baseNode.kind != .syntax && baseNode.kind != .syntaxCollection { - let baseKind = baseNode.kind + where baseNode.isBaseNode && baseNode.kind != .syntax && baseNode.kind != .syntaxCollection { DeclSyntax( """ - /// Visit any \(baseKind.syntaxType) node. + /// Visit any \(baseNode.syntaxType) node. /// - Parameter node: the node that is being visited /// - Returns: the rewritten node - \(baseNode.apiAttributes())\ - public func visit(_ node: \(baseKind.syntaxType)) -> \(baseKind.syntaxType) { + \(baseNode.apiAttributes)\ + public func visit(_ node: \(baseNode.syntaxType)) -> \(baseNode.syntaxType) { var node: Syntax = Syntax(node) dispatchVisit(&node) - return node.cast(\(baseKind.syntaxType).self) + return node.cast(\(baseNode.syntaxType).self) } """ ) @@ -265,7 +264,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { for node in NON_BASE_SYNTAX_NODES { SwitchCaseSyntax("case .\(node.enumCaseCallName):") { - StmtSyntax("return { self.visitImpl(&$0, \(node.kind.syntaxType).self, self.visit) }") + StmtSyntax("return { self.visitImpl(&$0, \(node.syntaxType).self, self.visit) }") } } } @@ -293,7 +292,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { for node in NON_BASE_SYNTAX_NODES { SwitchCaseSyntax("case .\(node.enumCaseCallName):") { - StmtSyntax("return visitImpl(&node, \(node.kind.syntaxType).self, visit)") + StmtSyntax("return visitImpl(&node, \(node.syntaxType).self, visit)") } } } diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxTraitsFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxTraitsFile.swift index 32989b0fd2f..b082faea697 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxTraitsFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxTraitsFile.swift @@ -28,12 +28,11 @@ let syntaxTraitsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { """ ) { for child in trait.children { - let questionMark = child.isOptional ? "?" : "" - DeclSyntax( """ \(child.documentation)\ - \(child.apiAttributes)var \(child.varDeclName): \(child.syntaxNodeKind.syntaxType)\(raw: questionMark) { get set } + \(child.apiAttributes)\ + var \(child.varDeclName): \(child.actualType) { get set } """ ) } @@ -81,7 +80,7 @@ let syntaxTraitsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { for node in SYNTAX_NODES.compactMap(\.layoutNode) where !node.traits.isEmpty { DeclSyntax( - "extension \(node.kind.syntaxType): \(raw: node.traits.map { $0 + "Syntax" }.joined(separator: ", ")) {}" + "extension \(node.syntaxType): \(raw: node.traits.map { $0 + "Syntax" }.joined(separator: ", ")) {}" ) } } diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxVisitorFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxVisitorFile.swift index ca8ae9ecf06..d2ced85eff1 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxVisitorFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxVisitorFile.swift @@ -58,14 +58,14 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { """ ) - for node in SYNTAX_NODES where !node.kind.isBase { + for node in SYNTAX_NODES where !node.isBaseNode { DeclSyntax( """ - /// Visiting \(raw: node.kind.doccLink) specifically. + /// Visiting \(raw: node.doccLink) specifically. /// - Parameter node: the node we are visiting. /// - Returns: how should we continue visiting. - \(node.apiAttributes())\ - open func visit(_ node: \(node.kind.syntaxType)) -> SyntaxVisitorContinueKind { + \(node.apiAttributes)\ + open func visit(_ node: \(node.syntaxType)) -> SyntaxVisitorContinueKind { return .visitChildren } """ @@ -73,10 +73,10 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { DeclSyntax( """ - /// The function called after visiting \(raw: node.kind.doccLink) and its descendants. + /// The function called after visiting \(raw: node.doccLink) and its descendants. /// - node: the node we just finished visiting. - \(node.apiAttributes())\ - open func visitPost(_ node: \(node.kind.syntaxType)) {} + \(node.apiAttributes)\ + open func visitPost(_ node: \(node.syntaxType)) {} """ ) } @@ -186,7 +186,7 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { for node in NON_BASE_SYNTAX_NODES { SwitchCaseSyntax("case .\(node.enumCaseCallName):") { StmtSyntax( - "return { self.visitImpl(&$0, \(node.kind.syntaxType).self, self.visit, self.visitPost) }" + "return { self.visitImpl(&$0, \(node.syntaxType).self, self.visit, self.visitPost) }" ) } } @@ -227,7 +227,7 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { for node in NON_BASE_SYNTAX_NODES { SwitchCaseSyntax("case .\(node.enumCaseCallName):") { - ExprSyntax("visitImpl(&node, \(node.kind.syntaxType).self, visit, visitPost)") + ExprSyntax("visitImpl(&node, \(node.syntaxType).self, visit, visitPost)") } } } diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/BuildableNodesFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/BuildableNodesFile.swift index 79e25e94cb9..978e2d43e2d 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/BuildableNodesFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/BuildableNodesFile.swift @@ -27,12 +27,10 @@ let buildableNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { ) for node in SYNTAX_NODES.compactMap(\.layoutNode) { - let type = node.type - if let convenienceInit = try! node.createConvenienceBuilderInitializer() { DeclSyntax( """ - extension \(type.syntaxBaseName) { + extension \(node.syntaxType) { \(convenienceInit) } """ diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/RenamedChildrenBuilderCompatibilityFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/RenamedChildrenBuilderCompatibilityFile.swift index 6fa8f8b5921..0fa1026af0c 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/RenamedChildrenBuilderCompatibilityFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/RenamedChildrenBuilderCompatibilityFile.swift @@ -35,7 +35,7 @@ let renamedChildrenBuilderCompatibilityFile = try! SourceFileSyntax(leadingTrivi DeclSyntax( """ - extension \(layoutNode.type.syntaxBaseName) { + extension \(layoutNode.syntaxType) { @available(*, deprecated, message: "Use an initializer with \(raw: deprecatedNames) argument(s).") @_disfavoredOverload \(convenienceInit) diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/ResultBuildersFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/ResultBuildersFile.swift index 1cbc33b9e1e..159cf2f568a 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/ResultBuildersFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/ResultBuildersFile.swift @@ -27,20 +27,18 @@ let resultBuildersFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { ) for node in SYNTAX_NODES.compactMap(\.collectionNode) { - let type = SyntaxBuildableType(kind: .node(kind: node.kind)) - try! StructDeclSyntax( """ - // MARK: - \(type.resultBuilderType) + // MARK: - \(node.resultBuilderType) - \(node.node.apiAttributes())\ + \(node.apiAttributes)\ @resultBuilder - public struct \(type.resultBuilderType): ListBuilder + public struct \(node.resultBuilderType): ListBuilder """ ) { DeclSyntax( """ - public typealias FinalResult = \(type.syntaxBaseName) + public typealias FinalResult = \(node.syntaxType) """ ) @@ -48,7 +46,7 @@ let resultBuildersFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { for elementChoice in node.elementChoices { DeclSyntax( """ - \(SYNTAX_NODE_MAP[elementChoice]?.apiAttributes() ?? [])\ + \(elementChoice.node?.apiAttributes ?? [])\ public static func buildExpression(_ expression: \(elementChoice.syntaxType)) -> Component { buildExpression(.init(expression)) } @@ -60,8 +58,8 @@ let resultBuildersFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { DeclSyntax( """ - extension \(type.syntaxBaseName) { - public init(@\(type.resultBuilderType) itemsBuilder: () throws -> \(type.syntaxBaseName)) rethrows { + extension \(node.syntaxType) { + public init(@\(node.resultBuilderType) itemsBuilder: () throws -> \(node.syntaxType)) rethrows { self = try itemsBuilder() } } diff --git a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/SyntaxExpressibleByStringInterpolationConformancesFile.swift b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/SyntaxExpressibleByStringInterpolationConformancesFile.swift index d6c237bbca9..7736fc74182 100644 --- a/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/SyntaxExpressibleByStringInterpolationConformancesFile.swift +++ b/CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/SyntaxExpressibleByStringInterpolationConformancesFile.swift @@ -29,7 +29,7 @@ let syntaxExpressibleByStringInterpolationConformancesFile = SourceFileSyntax(le let typesExpressibleByStringInterpolation = SYNTAX_NODES .filter { $0.parserFunction != nil } - .map { $0.kind.syntaxType } + .map { $0.syntaxType } // `SyntaxParsable` conformance for collection nodes is hand-written. + [ "AccessorDeclListSyntax", diff --git a/CodeGeneration/Tests/ValidateSyntaxNodes/ValidateSyntaxNodes.swift b/CodeGeneration/Tests/ValidateSyntaxNodes/ValidateSyntaxNodes.swift index 8e071ea628f..14a901e95b5 100644 --- a/CodeGeneration/Tests/ValidateSyntaxNodes/ValidateSyntaxNodes.swift +++ b/CodeGeneration/Tests/ValidateSyntaxNodes/ValidateSyntaxNodes.swift @@ -87,7 +87,7 @@ fileprivate extension Child { func isFollowedByColonToken(in node: LayoutNode) -> Bool { let childIndex = node.children.firstIndex(where: { $0.identifier.description == self.identifier.description }) guard let childIndex else { - preconditionFailure("\(self.identifier) is not a child of \(node.kind.syntaxType)") + preconditionFailure("\(self.identifier) is not a child of \(node.syntaxType)") } guard childIndex + 2 < node.children.count else { return false @@ -120,13 +120,13 @@ class ValidateSyntaxNodes: XCTestCase { /// All nodes with base kind e.g. `ExprSyntax` should end with `ExprSyntax`. func testBaseKindSuffix() { var failures: [ValidationFailure] = [] - for node in SYNTAX_NODES where node.base != .syntaxCollection { - if !node.kind.syntaxType.description.hasSuffix(node.base.syntaxType.description) { + for node in SYNTAX_NODES where node.baseKind != .syntaxCollection { + if !node.syntaxType.description.hasSuffix(node.baseKind.syntaxType.description) { failures.append( ValidationFailure( node: node.kind, message: - "has base kind '\(node.base.syntaxType)' but type name doesn’t have '\(node.base.syntaxType)' suffix" + "has base kind '\(node.baseKind.syntaxType)' but type name doesn’t have '\(node.baseKind.syntaxType)' suffix" ) ) } @@ -151,7 +151,7 @@ class ValidateSyntaxNodes: XCTestCase { var failures: [ValidationFailure] = [] for node in SYNTAX_NODES.compactMap(\.collectionNode) { - if !node.kind.syntaxType.description.hasSuffix("ListSyntax") { + if !node.syntaxType.description.hasSuffix("ListSyntax") { failures.append( ValidationFailure( node: node.kind, @@ -456,7 +456,7 @@ class ValidateSyntaxNodes: XCTestCase { } } - for (kind, children) in childrenByNodeKind where !kind.isBase && kind != .token && kind != .stringLiteralExpr { + for (kind, children) in childrenByNodeKind where !kind.isBaseType && kind != .token && kind != .stringLiteralExpr { let childNames = children.map(\.child.identifier.description) let mostCommonChildName = childNames.mostCommon! let mostCommonChild = children.first(where: { $0.child.identifier.description == mostCommonChildName })! @@ -467,7 +467,7 @@ class ValidateSyntaxNodes: XCTestCase { ValidationFailure( node: node.kind, message: - "child '\(child.identifier)' is named inconsistently with '\(mostCommonChild.node.kind.syntaxType).\(mostCommonChildName)', which has the same type ('\(kind.syntaxType)')" + "child '\(child.identifier)' is named inconsistently with '\(mostCommonChild.node.syntaxType).\(mostCommonChildName)', which has the same type ('\(kind.syntaxType)')" ) ) } @@ -585,7 +585,7 @@ class ValidateSyntaxNodes: XCTestCase { for node in SYNTAX_NODES.compactMap(\.layoutNode) { for child in node.children { - if case .node(kind: let kind) = child.kind, SYNTAX_NODE_MAP[kind]?.collectionNode != nil { + if case .node(kind: let kind) = child.kind, kind.node?.collectionNode != nil { failures.append( ValidationFailure( node: node.kind, @@ -756,7 +756,7 @@ class ValidateSyntaxNodes: XCTestCase { var failures: [ValidationFailure] = [] for node in SYNTAX_NODES.compactMap(\.layoutNode) { - if node.kind.syntaxType.description.contains("List") { + if node.syntaxType.description.contains("List") { failures.append( ValidationFailure( node: node.kind, @@ -797,7 +797,7 @@ class ValidateSyntaxNodes: XCTestCase { var failures: [ValidationFailure] = [] for node in SYNTAX_NODES { - if node.kind.syntaxType.description.contains("Entry") { + if node.syntaxType.description.contains("Entry") { failures.append( ValidationFailure( node: node.kind, diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesAB.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesAB.swift index fb241124488..6ee2ee6c0a7 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesAB.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesAB.swift @@ -105,8 +105,8 @@ public struct RawAccessorBlockSyntax: RawSyntaxNodeProtocol { layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var accessors: RawSyntax { - layoutView.children[3]! + public var accessors: Accessors { + layoutView.children[3].flatMap(Accessors.init)! } public var unexpectedBetweenAccessorsAndRightBrace: RawUnexpectedNodesSyntax? { @@ -1292,9 +1292,9 @@ public struct RawAttributeListSyntax: RawSyntaxNodeProtocol { self.init(unchecked: raw) } - public var elements: [RawSyntax] { + public var elements: [Element] { layoutView.children.map { - RawSyntax(raw: $0!) + Element($0!)! } } } @@ -1506,8 +1506,8 @@ public struct RawAttributeSyntax: RawSyntaxNodeProtocol { layoutView.children[6].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var arguments: RawSyntax? { - layoutView.children[7] + public var arguments: Arguments? { + layoutView.children[7].flatMap(Arguments.init) } public var unexpectedBetweenArgumentsAndRightParen: RawUnexpectedNodesSyntax? { @@ -1746,8 +1746,8 @@ public struct RawAvailabilityArgumentSyntax: RawSyntaxNodeProtocol { layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var argument: RawSyntax { - layoutView.children[1]! + public var argument: Argument { + layoutView.children[1].flatMap(Argument.init)! } public var unexpectedBetweenArgumentAndTrailingComma: RawUnexpectedNodesSyntax? { @@ -1958,8 +1958,8 @@ public struct RawAvailabilityLabeledArgumentSyntax: RawSyntaxNodeProtocol { layoutView.children[4].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var value: RawSyntax { - layoutView.children[5]! + public var value: Value { + layoutView.children[5].flatMap(Value.init)! } public var unexpectedAfterValue: RawUnexpectedNodesSyntax? { diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesC.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesC.swift index 7ae0f124984..7f911eacd8e 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesC.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesC.swift @@ -1597,8 +1597,8 @@ public struct RawClosureSignatureSyntax: RawSyntaxNodeProtocol { layoutView.children[4].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var parameterClause: RawSyntax? { - layoutView.children[5] + public var parameterClause: ParameterClause? { + layoutView.children[5].flatMap(ParameterClause.init) } public var unexpectedBetweenParameterClauseAndEffectSpecifiers: RawUnexpectedNodesSyntax? { @@ -1778,8 +1778,8 @@ public struct RawCodeBlockItemSyntax: RawSyntaxNodeProtocol { layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var item: RawSyntax { - layoutView.children[1]! + public var item: Item { + layoutView.children[1].flatMap(Item.init)! } public var unexpectedBetweenItemAndSemicolon: RawUnexpectedNodesSyntax? { @@ -2200,8 +2200,8 @@ public struct RawConditionElementSyntax: RawSyntaxNodeProtocol { layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var condition: RawSyntax { - layoutView.children[1]! + public var condition: Condition { + layoutView.children[1].flatMap(Condition.init)! } public var unexpectedBetweenConditionAndTrailingComma: RawUnexpectedNodesSyntax? { diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesD.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesD.swift index f79f3b86996..665d4a349b0 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesD.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesD.swift @@ -1252,8 +1252,8 @@ public struct RawDictionaryExprSyntax: RawExprSyntaxNodeProtocol { layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var content: RawSyntax { - layoutView.children[3]! + public var content: Content { + layoutView.children[3].flatMap(Content.init)! } public var unexpectedBetweenContentAndRightSquare: RawUnexpectedNodesSyntax? { @@ -1678,8 +1678,8 @@ public struct RawDifferentiabilityWithRespectToArgumentSyntax: RawSyntaxNodeProt layoutView.children[4].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var arguments: RawSyntax { - layoutView.children[5]! + public var arguments: Arguments { + layoutView.children[5].flatMap(Arguments.init)! } public var unexpectedAfterArguments: RawUnexpectedNodesSyntax? { @@ -2264,8 +2264,8 @@ public struct RawDocumentationAttributeArgumentSyntax: RawSyntaxNodeProtocol { layoutView.children[4].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var value: RawSyntax { - layoutView.children[5]! + public var value: Value { + layoutView.children[5].flatMap(Value.init)! } public var unexpectedBetweenValueAndTrailingComma: RawUnexpectedNodesSyntax? { diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesGHI.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesGHI.swift index b997f6e8583..0be568b8b70 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesGHI.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesGHI.swift @@ -612,8 +612,8 @@ public struct RawGenericRequirementSyntax: RawSyntaxNodeProtocol { layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var requirement: RawSyntax { - layoutView.children[1]! + public var requirement: Requirement { + layoutView.children[1].flatMap(Requirement.init)! } public var unexpectedBetweenRequirementAndTrailingComma: RawUnexpectedNodesSyntax? { @@ -1161,8 +1161,8 @@ public struct RawIfConfigClauseSyntax: RawSyntaxNodeProtocol { layoutView.children[4].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var elements: RawSyntax? { - layoutView.children[5] + public var elements: Elements? { + layoutView.children[5].flatMap(Elements.init) } public var unexpectedAfterElements: RawUnexpectedNodesSyntax? { @@ -1365,8 +1365,8 @@ public struct RawIfExprSyntax: RawExprSyntaxNodeProtocol { layoutView.children[8].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var elseBody: RawSyntax? { - layoutView.children[9] + public var elseBody: ElseBody? { + layoutView.children[9].flatMap(ElseBody.init) } public var unexpectedAfterElseBody: RawUnexpectedNodesSyntax? { diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesJKLMN.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesJKLMN.swift index e16b561e9de..d814def8a07 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesJKLMN.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesJKLMN.swift @@ -156,8 +156,8 @@ public struct RawKeyPathComponentSyntax: RawSyntaxNodeProtocol { layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var component: RawSyntax { - layoutView.children[3]! + public var component: Component { + layoutView.children[3].flatMap(Component.init)! } public var unexpectedAfterComponent: RawUnexpectedNodesSyntax? { diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesOP.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesOP.swift index 7b5c3e5dd0d..750e9fd9fe4 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesOP.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesOP.swift @@ -2063,9 +2063,9 @@ public struct RawPrecedenceGroupAttributeListSyntax: RawSyntaxNodeProtocol { self.init(unchecked: raw) } - public var elements: [RawSyntax] { + public var elements: [Element] { layoutView.children.map { - RawSyntax(raw: $0!) + Element($0!)! } } } diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesQRS.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesQRS.swift index 7a0dd1310f0..986617ace2e 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesQRS.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesQRS.swift @@ -922,9 +922,9 @@ public struct RawSpecializeAttributeArgumentListSyntax: RawSyntaxNodeProtocol { self.init(unchecked: raw) } - public var elements: [RawSyntax] { + public var elements: [Element] { layoutView.children.map { - RawSyntax(raw: $0!) + Element($0!)! } } } @@ -1339,9 +1339,9 @@ public struct RawStringLiteralSegmentListSyntax: RawSyntaxNodeProtocol { self.init(unchecked: raw) } - public var elements: [RawSyntax] { + public var elements: [Element] { layoutView.children.map { - RawSyntax(raw: $0!) + Element($0!)! } } } @@ -2219,9 +2219,9 @@ public struct RawSwitchCaseListSyntax: RawSyntaxNodeProtocol { self.init(unchecked: raw) } - public var elements: [RawSyntax] { + public var elements: [Element] { layoutView.children.map { - RawSyntax(raw: $0!) + Element($0!)! } } } @@ -2319,8 +2319,8 @@ public struct RawSwitchCaseSyntax: RawSyntaxNodeProtocol { layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var label: RawSyntax { - layoutView.children[3]! + public var label: Label { + layoutView.children[3].flatMap(Label.init)! } public var unexpectedBetweenLabelAndStatements: RawUnexpectedNodesSyntax? { diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesTUVWXYZ.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesTUVWXYZ.swift index 0339afcc782..792e38ef6c1 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesTUVWXYZ.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxNodesTUVWXYZ.swift @@ -1485,9 +1485,9 @@ public struct RawTypeSpecifierListSyntax: RawSyntaxNodeProtocol { self.init(unchecked: raw) } - public var elements: [RawSyntax] { + public var elements: [Element] { layoutView.children.map { - RawSyntax(raw: $0!) + Element($0!)! } } } @@ -2612,8 +2612,8 @@ public struct RawYieldStmtSyntax: RawStmtSyntaxNodeProtocol { layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:)) } - public var yieldedExpressions: RawSyntax { - layoutView.children[3]! + public var yieldedExpressions: YieldedExpressions { + layoutView.children[3].flatMap(YieldedExpressions.init)! } public var unexpectedAfterYieldedExpressions: RawUnexpectedNodesSyntax? { diff --git a/Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift b/Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift index 9d9ef539397..116af8ffec0 100644 --- a/Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift +++ b/Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift @@ -189,23 +189,6 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { """, file: file, line: line) } } - func assertAnyHasNoError(_ nodeKind: SyntaxKind, _ index: Int, _ errors: [ValidationError?]) { - let nonNilErrors = errors.compactMap({ - $0 - }) - if nonNilErrors.count == errors.count, let firstError = nonNilErrors.first { - let (file, line) = firstError.fileAndLine - assertionFailure(""" - Error validating child at index \(index) of \(nodeKind): - Node did not satisfy any node choice requirement. - Validation failures: - \(nonNilErrors.map({ "- \($0.description)" }).joined(separator: "\n")) - - See "RawSyntax Validation" in CONTRIBUTING.md to reproduce the failure locally. - """, file: file, line: line) - _ = 1 - } - } switch kind { case .token: assertionFailure("validateLayout for .token kind is not supported") @@ -214,8 +197,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 1, verify(layout[1], as: RawTokenSyntax.self, tokenChoices: [.tokenKind(.leftBrace)])) assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 3, [ - verify(layout[3], as: RawSyntax.self)]) + assertNoError(kind, 3, verify(layout[3], as: RawAccessorBlockSyntax.Accessors.self)) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 5, verify(layout[5], as: RawTokenSyntax.self, tokenChoices: [.tokenKind(.rightBrace)])) assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self)) @@ -358,9 +340,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 14, verify(layout[14], as: RawUnexpectedNodesSyntax?.self)) case .attributeList: for (index, element) in layout.enumerated() { - assertAnyHasNoError(kind, index, [ - verify(element, as: RawAttributeSyntax.self), - verify(element, as: RawIfConfigDeclSyntax.self)]) + assertNoError(kind, index, verify(element, as: RawAttributeListSyntax.Element.self)) } case .attribute: assert(layout.count == 11) @@ -371,8 +351,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 5, verify(layout[5], as: RawTokenSyntax?.self, tokenChoices: [.tokenKind(.leftParen)])) assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 7, [ - verify(layout[7], as: RawSyntax?.self)]) + assertNoError(kind, 7, verify(layout[7], as: RawAttributeSyntax.Arguments?.self)) assertNoError(kind, 8, verify(layout[8], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 9, verify(layout[9], as: RawTokenSyntax?.self, tokenChoices: [.tokenKind(.rightParen)])) assertNoError(kind, 10, verify(layout[10], as: RawUnexpectedNodesSyntax?.self)) @@ -392,8 +371,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { case .availabilityArgument: assert(layout.count == 5) assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 1, [ - verify(layout[1], as: RawSyntax.self)]) + assertNoError(kind, 1, verify(layout[1], as: RawAvailabilityArgumentSyntax.Argument.self)) assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 3, verify(layout[3], as: RawTokenSyntax?.self, tokenChoices: [.tokenKind(.comma)])) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) @@ -421,8 +399,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 3, verify(layout[3], as: RawTokenSyntax.self, tokenChoices: [.tokenKind(.colon)])) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 5, [ - verify(layout[5], as: RawSyntax.self)]) + assertNoError(kind, 5, verify(layout[5], as: RawAvailabilityLabeledArgumentSyntax.Value.self)) assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self)) case .awaitExpr: assert(layout.count == 5) @@ -634,8 +611,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 3, verify(layout[3], as: RawClosureCaptureClauseSyntax?.self)) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 5, [ - verify(layout[5], as: RawSyntax?.self)]) + assertNoError(kind, 5, verify(layout[5], as: RawClosureSignatureSyntax.ParameterClause?.self)) assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 7, verify(layout[7], as: RawTypeEffectSpecifiersSyntax?.self)) assertNoError(kind, 8, verify(layout[8], as: RawUnexpectedNodesSyntax?.self)) @@ -650,8 +626,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { case .codeBlockItem: assert(layout.count == 5) assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 1, [ - verify(layout[1], as: RawSyntax.self)]) + assertNoError(kind, 1, verify(layout[1], as: RawCodeBlockItemSyntax.Item.self)) assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 3, verify(layout[3], as: RawTokenSyntax?.self, tokenChoices: [.tokenKind(.semicolon)])) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) @@ -687,8 +662,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { case .conditionElement: assert(layout.count == 5) assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 1, [ - verify(layout[1], as: RawSyntax.self)]) + assertNoError(kind, 1, verify(layout[1], as: RawConditionElementSyntax.Condition.self)) assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 3, verify(layout[3], as: RawTokenSyntax?.self, tokenChoices: [.tokenKind(.comma)])) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) @@ -911,8 +885,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 1, verify(layout[1], as: RawTokenSyntax.self, tokenChoices: [.tokenKind(.leftSquare)])) assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 3, [ - verify(layout[3], as: RawSyntax.self)]) + assertNoError(kind, 3, verify(layout[3], as: RawDictionaryExprSyntax.Content.self)) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 5, verify(layout[5], as: RawTokenSyntax.self, tokenChoices: [.tokenKind(.rightSquare)])) assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self)) @@ -956,8 +929,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 3, verify(layout[3], as: RawTokenSyntax.self, tokenChoices: [.tokenKind(.colon)])) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 5, [ - verify(layout[5], as: RawSyntax.self)]) + assertNoError(kind, 5, verify(layout[5], as: RawDifferentiabilityWithRespectToArgumentSyntax.Arguments.self)) assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self)) case .differentiableAttributeArguments: assert(layout.count == 11) @@ -1015,8 +987,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 3, verify(layout[3], as: RawTokenSyntax.self, tokenChoices: [.tokenKind(.colon)])) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 5, [ - verify(layout[5], as: RawSyntax.self)]) + assertNoError(kind, 5, verify(layout[5], as: RawDocumentationAttributeArgumentSyntax.Value.self)) assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 7, verify(layout[7], as: RawTokenSyntax?.self, tokenChoices: [.tokenKind(.comma)])) assertNoError(kind, 8, verify(layout[8], as: RawUnexpectedNodesSyntax?.self)) @@ -1374,8 +1345,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { case .genericRequirement: assert(layout.count == 5) assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 1, [ - verify(layout[1], as: RawSyntax.self)]) + assertNoError(kind, 1, verify(layout[1], as: RawGenericRequirementSyntax.Requirement.self)) assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 3, verify(layout[3], as: RawTokenSyntax?.self, tokenChoices: [.tokenKind(.comma)])) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) @@ -1438,8 +1408,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 3, verify(layout[3], as: RawExprSyntax?.self)) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 5, [ - verify(layout[5], as: RawSyntax?.self)]) + assertNoError(kind, 5, verify(layout[5], as: RawIfConfigClauseSyntax.Elements?.self)) assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self)) case .ifConfigDecl: assert(layout.count == 5) @@ -1459,8 +1428,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 7, verify(layout[7], as: RawTokenSyntax?.self, tokenChoices: [.keyword("else")])) assertNoError(kind, 8, verify(layout[8], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 9, [ - verify(layout[9], as: RawSyntax?.self)]) + assertNoError(kind, 9, verify(layout[9], as: RawIfExprSyntax.ElseBody?.self)) assertNoError(kind, 10, verify(layout[10], as: RawUnexpectedNodesSyntax?.self)) case .implementsAttributeArguments: assert(layout.count == 7) @@ -1607,8 +1575,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 1, verify(layout[1], as: RawTokenSyntax?.self, tokenChoices: [.tokenKind(.period)])) assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 3, [ - verify(layout[3], as: RawSyntax.self)]) + assertNoError(kind, 3, verify(layout[3], as: RawKeyPathComponentSyntax.Component.self)) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) case .keyPathExpr: assert(layout.count == 7) @@ -2130,10 +2097,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self)) case .precedenceGroupAttributeList: for (index, element) in layout.enumerated() { - assertAnyHasNoError(kind, index, [ - verify(element, as: RawPrecedenceGroupRelationSyntax.self), - verify(element, as: RawPrecedenceGroupAssignmentSyntax.self), - verify(element, as: RawPrecedenceGroupAssociativitySyntax.self)]) + assertNoError(kind, index, verify(element, as: RawPrecedenceGroupAttributeListSyntax.Element.self)) } case .precedenceGroupDecl: assert(layout.count == 15) @@ -2315,12 +2279,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self)) case .specializeAttributeArgumentList: for (index, element) in layout.enumerated() { - assertAnyHasNoError(kind, index, [ - verify(element, as: RawLabeledSpecializeArgumentSyntax.self), - verify(element, as: RawSpecializeAvailabilityArgumentSyntax.self), - verify(element, as: RawSpecializeTargetFunctionArgumentSyntax.self), - verify(element, as: RawGenericWhereClauseSyntax.self) - ]) + assertNoError(kind, index, verify(element, as: RawSpecializeAttributeArgumentListSyntax.Element.self)) } case .specializeAvailabilityArgument: assert(layout.count == 9) @@ -2359,9 +2318,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 10, verify(layout[10], as: RawUnexpectedNodesSyntax?.self)) case .stringLiteralSegmentList: for (index, element) in layout.enumerated() { - assertAnyHasNoError(kind, index, [ - verify(element, as: RawStringSegmentSyntax.self), - verify(element, as: RawExpressionSegmentSyntax.self)]) + assertNoError(kind, index, verify(element, as: RawStringLiteralSegmentListSyntax.Element.self)) } case .stringSegment: assert(layout.count == 3) @@ -2457,17 +2414,14 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self)) case .switchCaseList: for (index, element) in layout.enumerated() { - assertAnyHasNoError(kind, index, [ - verify(element, as: RawSwitchCaseSyntax.self), - verify(element, as: RawIfConfigDeclSyntax.self)]) + assertNoError(kind, index, verify(element, as: RawSwitchCaseListSyntax.Element.self)) } case .switchCase: assert(layout.count == 7) assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 1, verify(layout[1], as: RawAttributeSyntax?.self)) assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 3, [ - verify(layout[3], as: RawSyntax.self)]) + assertNoError(kind, 3, verify(layout[3], as: RawSwitchCaseSyntax.Label.self)) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 5, verify(layout[5], as: RawCodeBlockItemListSyntax.self)) assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self)) @@ -2646,9 +2600,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) case .typeSpecifierList: for (index, element) in layout.enumerated() { - assertAnyHasNoError(kind, index, [ - verify(element, as: RawSimpleTypeSpecifierSyntax.self), - verify(element, as: RawLifetimeTypeSpecifierSyntax.self)]) + assertNoError(kind, index, verify(element, as: RawTypeSpecifierListSyntax.Element.self)) } case .unavailableFromAsyncAttributeArguments: assert(layout.count == 7) @@ -2770,8 +2722,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) { assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self)) assertNoError(kind, 1, verify(layout[1], as: RawTokenSyntax.self, tokenChoices: [.keyword("yield")])) assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self)) - assertAnyHasNoError(kind, 3, [ - verify(layout[3], as: RawSyntax.self)]) + assertNoError(kind, 3, verify(layout[3], as: RawYieldStmtSyntax.YieldedExpressions.self)) assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self)) case .yieldedExpressionList: for (index, element) in layout.enumerated() { diff --git a/Sources/SwiftSyntaxBuilder/generated/BuildableNodes.swift b/Sources/SwiftSyntaxBuilder/generated/BuildableNodes.swift index da21bcb71da..9e5cf681aab 100644 --- a/Sources/SwiftSyntaxBuilder/generated/BuildableNodes.swift +++ b/Sources/SwiftSyntaxBuilder/generated/BuildableNodes.swift @@ -534,7 +534,7 @@ extension ExtensionDeclSyntax { unexpectedBetweenModifiersAndExtensionKeyword: UnexpectedNodesSyntax? = nil, extensionKeyword: TokenSyntax = .keyword(.extension), unexpectedBetweenExtensionKeywordAndExtendedType: UnexpectedNodesSyntax? = nil, - extendedType: TypeSyntaxProtocol, + extendedType: any TypeSyntaxProtocol, unexpectedBetweenExtendedTypeAndInheritanceClause: UnexpectedNodesSyntax? = nil, inheritanceClause: InheritanceClauseSyntax? = nil, unexpectedBetweenInheritanceClauseAndGenericWhereClause: UnexpectedNodesSyntax? = nil, @@ -579,13 +579,13 @@ extension ForStmtSyntax { unexpectedBetweenAwaitKeywordAndCaseKeyword: UnexpectedNodesSyntax? = nil, caseKeyword: TokenSyntax? = nil, unexpectedBetweenCaseKeywordAndPattern: UnexpectedNodesSyntax? = nil, - pattern: PatternSyntaxProtocol, + pattern: any PatternSyntaxProtocol, unexpectedBetweenPatternAndTypeAnnotation: UnexpectedNodesSyntax? = nil, typeAnnotation: TypeAnnotationSyntax? = nil, unexpectedBetweenTypeAnnotationAndInKeyword: UnexpectedNodesSyntax? = nil, inKeyword: TokenSyntax = .keyword(.in), unexpectedBetweenInKeywordAndSequence: UnexpectedNodesSyntax? = nil, - sequence: ExprSyntaxProtocol, + sequence: any ExprSyntaxProtocol, unexpectedBetweenSequenceAndWhereClause: UnexpectedNodesSyntax? = nil, whereClause: WhereClauseSyntax? = nil, unexpectedBetweenWhereClauseAndBody: UnexpectedNodesSyntax? = nil, @@ -626,7 +626,7 @@ extension FunctionCallExprSyntax { public init( leadingTrivia: Trivia? = nil, unexpectedBeforeCalledExpression: UnexpectedNodesSyntax? = nil, - calledExpression: ExprSyntaxProtocol, + calledExpression: any ExprSyntaxProtocol, unexpectedBetweenCalledExpressionAndLeftParen: UnexpectedNodesSyntax? = nil, leftParen: TokenSyntax? = nil, unexpectedBetweenLeftParenAndArguments: UnexpectedNodesSyntax? = nil, @@ -1168,7 +1168,7 @@ extension RepeatStmtSyntax { unexpectedBetweenBodyAndWhileKeyword: UnexpectedNodesSyntax? = nil, whileKeyword: TokenSyntax = .keyword(.while), unexpectedBetweenWhileKeywordAndCondition: UnexpectedNodesSyntax? = nil, - condition: ExprSyntaxProtocol, + condition: any ExprSyntaxProtocol, unexpectedAfterCondition: UnexpectedNodesSyntax? = nil, @CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax, trailingTrivia: Trivia? = nil @@ -1287,7 +1287,7 @@ extension SubscriptCallExprSyntax { public init( leadingTrivia: Trivia? = nil, unexpectedBeforeCalledExpression: UnexpectedNodesSyntax? = nil, - calledExpression: ExprSyntaxProtocol, + calledExpression: any ExprSyntaxProtocol, unexpectedBetweenCalledExpressionAndLeftSquare: UnexpectedNodesSyntax? = nil, leftSquare: TokenSyntax = .leftSquareToken(), unexpectedBetweenLeftSquareAndArguments: UnexpectedNodesSyntax? = nil, @@ -1382,7 +1382,7 @@ extension SwitchExprSyntax { unexpectedBeforeSwitchKeyword: UnexpectedNodesSyntax? = nil, switchKeyword: TokenSyntax = .keyword(.switch), unexpectedBetweenSwitchKeywordAndSubject: UnexpectedNodesSyntax? = nil, - subject: ExprSyntaxProtocol, + subject: any ExprSyntaxProtocol, unexpectedBetweenSubjectAndLeftBrace: UnexpectedNodesSyntax? = nil, leftBrace: TokenSyntax = .leftBraceToken(), unexpectedBetweenLeftBraceAndCases: UnexpectedNodesSyntax? = nil, diff --git a/Sources/SwiftSyntaxBuilder/generated/RenamedChildrenBuilderCompatibility.swift b/Sources/SwiftSyntaxBuilder/generated/RenamedChildrenBuilderCompatibility.swift index 15657f7cee6..ba81e68501e 100644 --- a/Sources/SwiftSyntaxBuilder/generated/RenamedChildrenBuilderCompatibility.swift +++ b/Sources/SwiftSyntaxBuilder/generated/RenamedChildrenBuilderCompatibility.swift @@ -289,13 +289,13 @@ extension ForStmtSyntax { unexpectedBetweenAwaitKeywordAndCaseKeyword: UnexpectedNodesSyntax? = nil, caseKeyword: TokenSyntax? = nil, unexpectedBetweenCaseKeywordAndPattern: UnexpectedNodesSyntax? = nil, - pattern: PatternSyntaxProtocol, + pattern: any PatternSyntaxProtocol, unexpectedBetweenPatternAndTypeAnnotation: UnexpectedNodesSyntax? = nil, typeAnnotation: TypeAnnotationSyntax? = nil, unexpectedBetweenTypeAnnotationAndInKeyword: UnexpectedNodesSyntax? = nil, inKeyword: TokenSyntax = .keyword(.in), unexpectedBetweenInKeywordAndSequenceExpr: UnexpectedNodesSyntax? = nil, - sequenceExpr: ExprSyntaxProtocol, + sequenceExpr: any ExprSyntaxProtocol, unexpectedBetweenSequenceExprAndWhereClause: UnexpectedNodesSyntax? = nil, whereClause: WhereClauseSyntax? = nil, unexpectedBetweenWhereClauseAndBody: UnexpectedNodesSyntax? = nil, @@ -338,7 +338,7 @@ extension FunctionCallExprSyntax { public init( leadingTrivia: Trivia? = nil, unexpectedBeforeCalledExpression: UnexpectedNodesSyntax? = nil, - calledExpression: ExprSyntaxProtocol, + calledExpression: any ExprSyntaxProtocol, unexpectedBetweenCalledExpressionAndLeftParen: UnexpectedNodesSyntax? = nil, leftParen: TokenSyntax? = nil, unexpectedBetweenLeftParenAndArgumentList: UnexpectedNodesSyntax? = nil, @@ -833,7 +833,7 @@ extension SubscriptCallExprSyntax { public init( leadingTrivia: Trivia? = nil, unexpectedBeforeCalledExpression: UnexpectedNodesSyntax? = nil, - calledExpression: ExprSyntaxProtocol, + calledExpression: any ExprSyntaxProtocol, unexpectedBetweenCalledExpressionAndLeftBracket: UnexpectedNodesSyntax? = nil, leftBracket: TokenSyntax = .leftSquareToken(), unexpectedBetweenLeftBracketAndArgumentList: UnexpectedNodesSyntax? = nil, @@ -905,7 +905,7 @@ extension SwitchExprSyntax { unexpectedBeforeSwitchKeyword: UnexpectedNodesSyntax? = nil, switchKeyword: TokenSyntax = .keyword(.switch), unexpectedBetweenSwitchKeywordAndExpression: UnexpectedNodesSyntax? = nil, - expression: ExprSyntaxProtocol, + expression: any ExprSyntaxProtocol, unexpectedBetweenExpressionAndLeftBrace: UnexpectedNodesSyntax? = nil, leftBrace: TokenSyntax = .leftBraceToken(), unexpectedBetweenLeftBraceAndCases: UnexpectedNodesSyntax? = nil,