Skip to content

SyntaxSupport Node Documentation: StructDecl #1589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 68 additions & 3 deletions CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,63 @@ public let DECL_NODES: [Node] = [
Node(
name: "StructDecl",
nameForDiagnostics: "struct",
description: """
A struct declaration like the following.

```swift
struct SomeStruct {
let someMember: String
var anotherMember: Int

func foo() {
print(someMember)
}

mutating func bar() {
anotherMember = 42
}
}
```

A struct declaration may be declared without any members.

```swift
struct EmptyStruct {

}
```

A struct declaration may include a type inheritance clause listing
one or more protocols the struct conforms to.

The example below uses Hashable and Equatable protocols whose members
are automatically synthesized by the compiler if the struct contains
stored members that are themselves `Hashable` and `Equatable`.

```swift
struct AdvancedStruct: Hashable, Equatable {
let someMember: String
var anotherMember: Int
}
```

A struct declaration may include a generic parameter clause as well
as a generic where clause.

```swift
struct Stack<Element> {
var items: [Element] = []

mutating func push(_ item: Element) {
items.append(item)
}

mutating func pop() -> Element {
return items.removeLast()
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s nice and verbose. I like it 🤩

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

```
""",
kind: "Decl",
traits: [
"DeclGroup",
Expand All @@ -1816,43 +1873,51 @@ public let DECL_NODES: [Node] = [
name: "Attributes",
kind: .collection(kind: "AttributeList", collectionElementName: "Attribute"),
nameForDiagnostics: "attributes",
description: "Attributes that are attached to the struct declaration.",
isOptional: true
),
Child(
name: "Modifiers",
kind: .collection(kind: "ModifierList", collectionElementName: "Modifier"),
nameForDiagnostics: "modifiers",
description: "Modifiers that are attached to the struct declaration.",
isOptional: true
),
Child(
name: "StructKeyword",
kind: .token(choices: [.keyword(text: "struct")])
kind: .token(choices: [.keyword(text: "struct")]),
description: "The `struct` keyword for this declaration."
),
Child(
name: "Identifier",
kind: .token(choices: [.token(tokenKind: "IdentifierToken")])
kind: .token(choices: [.token(tokenKind: "IdentifierToken")]),
description: "Declares the name of this struct. If the name matches a reserved keyword use backticks to escape it."
),
Child(
name: "GenericParameterClause",
kind: .node(kind: "GenericParameterClause"),
nameForDiagnostics: "generic parameter clause",
description: "The generic parameters, if any, of the struct declaration.",
isOptional: true
),
Child(
name: "InheritanceClause",
kind: .node(kind: "TypeInheritanceClause"),
nameForDiagnostics: "type inheritance clause",
description: "The struct declaration inheritance clause describing one or more conformances for this struct declaration.",
isOptional: true
),
Child(
name: "GenericWhereClause",
kind: .node(kind: "GenericWhereClause"),
nameForDiagnostics: "generic where clause",
description: "The `where` clause that applies to the generic parameters of this struct declaration.",
isOptional: true
),
Child(
name: "MemberBlock",
kind: .node(kind: "MemberDeclBlock")
kind: .node(kind: "MemberDeclBlock"),
description: "The members of the struct declaration. Because struct extension declarations may declare additional members the contents of this member block isn't guaranteed to be a complete list of members for this type."
),
]
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5117,7 +5117,61 @@ public struct ProtocolDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {

// MARK: - StructDeclSyntax


/// A struct declaration like the following.
///
/// ```swift
/// struct SomeStruct {
/// let someMember: String
/// var anotherMember: Int
///
/// func foo() {
/// print(someMember)
/// }
///
/// mutating func bar() {
/// anotherMember = 42
/// }
/// }
/// ```
///
/// A struct declaration may be declared without any members.
///
/// ```swift
/// struct EmptyStruct {
///
/// }
/// ```
///
/// A struct declaration may include a type inheritance clause listing
/// one or more protocols the struct conforms to.
///
/// The example below uses Hashable and Equatable protocols whose members
/// are automatically synthesized by the compiler if the struct contains
/// stored members that are themselves `Hashable` and `Equatable`.
///
/// ```swift
/// struct AdvancedStruct: Hashable, Equatable {
/// let someMember: String
/// var anotherMember: Int
/// }
/// ```
///
/// A struct declaration may include a generic parameter clause as well
/// as a generic where clause.
///
/// ```swift
/// struct Stack<Element> {
/// var items: [Element] = []
///
/// mutating func push(_ item: Element) {
/// items.append(item)
/// }
///
/// mutating func pop() -> Element {
/// return items.removeLast()
/// }
/// }
/// ```
public struct StructDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
public let _syntaxNode: Syntax

Expand Down Expand Up @@ -5220,6 +5274,7 @@ public struct StructDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
}
}

/// Attributes that are attached to the struct declaration.
public var attributes: AttributeListSyntax? {
get {
return data.child(at: 1, parent: Syntax(self)).map(AttributeListSyntax.init)
Expand Down Expand Up @@ -5257,6 +5312,7 @@ public struct StructDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
}
}

/// Modifiers that are attached to the struct declaration.
public var modifiers: ModifierListSyntax? {
get {
return data.child(at: 3, parent: Syntax(self)).map(ModifierListSyntax.init)
Expand Down Expand Up @@ -5294,6 +5350,7 @@ public struct StructDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
}
}

/// The `struct` keyword for this declaration.
public var structKeyword: TokenSyntax {
get {
return TokenSyntax(data.child(at: 5, parent: Syntax(self))!)
Expand All @@ -5312,6 +5369,7 @@ public struct StructDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
}
}

/// Declares the name of this struct. If the name matches a reserved keyword use backticks to escape it.
public var identifier: TokenSyntax {
get {
return TokenSyntax(data.child(at: 7, parent: Syntax(self))!)
Expand All @@ -5330,6 +5388,7 @@ public struct StructDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
}
}

/// The generic parameters, if any, of the struct declaration.
public var genericParameterClause: GenericParameterClauseSyntax? {
get {
return data.child(at: 9, parent: Syntax(self)).map(GenericParameterClauseSyntax.init)
Expand All @@ -5348,6 +5407,7 @@ public struct StructDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
}
}

/// The struct declaration inheritance clause describing one or more conformances for this struct declaration.
public var inheritanceClause: TypeInheritanceClauseSyntax? {
get {
return data.child(at: 11, parent: Syntax(self)).map(TypeInheritanceClauseSyntax.init)
Expand All @@ -5366,6 +5426,7 @@ public struct StructDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
}
}

/// The `where` clause that applies to the generic parameters of this struct declaration.
public var genericWhereClause: GenericWhereClauseSyntax? {
get {
return data.child(at: 13, parent: Syntax(self)).map(GenericWhereClauseSyntax.init)
Expand All @@ -5384,6 +5445,7 @@ public struct StructDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
}
}

/// The members of the struct declaration. Because struct extension declarations may declare additional members the contents of this member block isn't guaranteed to be a complete list of members for this type.
public var memberBlock: MemberDeclBlockSyntax {
get {
return MemberDeclBlockSyntax(data.child(at: 15, parent: Syntax(self))!)
Expand Down
55 changes: 55 additions & 0 deletions Sources/SwiftSyntaxBuilder/generated/BuildableNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,61 @@ extension SourceFileSyntax {
}
}

/// A struct declaration like the following.
///
/// ```swift
/// struct SomeStruct {
/// let someMember: String
/// var anotherMember: Int
///
/// func foo() {
/// print(someMember)
/// }
///
/// mutating func bar() {
/// anotherMember = 42
/// }
/// }
/// ```
///
/// A struct declaration may be declared without any members.
///
/// ```swift
/// struct EmptyStruct {
///
/// }
/// ```
///
/// A struct declaration may include a type inheritance clause listing
/// one or more protocols the struct conforms to.
///
/// The example below uses Hashable and Equatable protocols whose members
/// are automatically synthesized by the compiler if the struct contains
/// stored members that are themselves `Hashable` and `Equatable`.
///
/// ```swift
/// struct AdvancedStruct: Hashable, Equatable {
/// let someMember: String
/// var anotherMember: Int
/// }
/// ```
///
/// A struct declaration may include a generic parameter clause as well
/// as a generic where clause.
///
/// ```swift
/// struct Stack<Element> {
/// var items: [Element] = []
///
/// mutating func push(_ item: Element) {
/// items.append(item)
/// }
///
/// mutating func pop() -> Element {
/// return items.removeLast()
/// }
/// }
/// ```
extension StructDeclSyntax {
/// A convenience initializer that allows initializing syntax collections using result builders
public init(
Expand Down