Skip to content

Commit acf1484

Browse files
committed
Add syntax trait covering genericParameterClause and genericWhereClause
1 parent 0d813c3 commit acf1484

File tree

4 files changed

+151
-67
lines changed

4 files changed

+151
-67
lines changed

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ public let DECL_NODES: [Node] = [
243243
"DeclGroup",
244244
"IdentifiedDecl",
245245
"WithAttributes",
246+
"WithGenericParameters",
246247
"WithModifiers",
247248
],
248249
children: [
@@ -416,6 +417,7 @@ public let DECL_NODES: [Node] = [
416417
"DeclGroup",
417418
"IdentifiedDecl",
418419
"WithAttributes",
420+
"WithGenericParameters",
419421
"WithModifiers",
420422
],
421423
children: [
@@ -837,6 +839,7 @@ public let DECL_NODES: [Node] = [
837839
"DeclGroup",
838840
"IdentifiedDecl",
839841
"WithAttributes",
842+
"WithGenericParameters",
840843
"WithModifiers",
841844
],
842845
children: [
@@ -957,6 +960,7 @@ public let DECL_NODES: [Node] = [
957960
traits: [
958961
"IdentifiedDecl",
959962
"WithAttributes",
963+
"WithGenericParameters",
960964
"WithModifiers",
961965
],
962966
children: [
@@ -1300,6 +1304,7 @@ public let DECL_NODES: [Node] = [
13001304
""",
13011305
traits: [
13021306
"WithAttributes",
1307+
"WithGenericParameters",
13031308
"WithModifiers",
13041309
],
13051310
children: [
@@ -1368,6 +1373,7 @@ public let DECL_NODES: [Node] = [
13681373
traits: [
13691374
"IdentifiedDecl",
13701375
"WithAttributes",
1376+
"WithGenericParameters",
13711377
"WithModifiers",
13721378
],
13731379
children: [
@@ -2128,6 +2134,7 @@ public let DECL_NODES: [Node] = [
21282134
"DeclGroup",
21292135
"IdentifiedDecl",
21302136
"WithAttributes",
2137+
"WithGenericParameters",
21312138
"WithModifiers",
21322139
],
21332140
children: [
@@ -2191,6 +2198,7 @@ public let DECL_NODES: [Node] = [
21912198
nameForDiagnostics: "subscript",
21922199
traits: [
21932200
"WithAttributes",
2201+
"WithGenericParameters",
21942202
"WithModifiers",
21952203
],
21962204
children: [
@@ -2295,6 +2303,7 @@ public let DECL_NODES: [Node] = [
22952303
traits: [
22962304
"IdentifiedDecl",
22972305
"WithAttributes",
2306+
"WithGenericParameters",
22982307
"WithModifiers",
22992308
],
23002309
children: [

CodeGeneration/Sources/SyntaxSupport/Traits.swift

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ public let TRAITS: [Trait] = [
7373
Child(name: "Identifier", kind: .token(choices: [.token(tokenKind: "IdentifierToken")]))
7474
]
7575
),
76+
Trait(
77+
traitName: "MissingNode",
78+
documentation: """
79+
Represents a layout node that is missing in the source file.
80+
81+
See the types conforming to this protocol for examples of where missing nodes can occur.
82+
""",
83+
children: [
84+
Child(
85+
name: "Placeholder",
86+
kind: .token(choices: [.token(tokenKind: "IdentifierToken")]),
87+
documentation: """
88+
A placeholder, i.e. `<#placeholder#>`, that can be inserted into the source code to represent the missing node.
89+
"""
90+
)
91+
]
92+
),
7693
Trait(
7794
traitName: "Parenthesized",
7895
children: [
@@ -92,6 +109,29 @@ public let TRAITS: [Trait] = [
92109
Child(name: "Body", kind: .node(kind: .codeBlock))
93110
]
94111
),
112+
Trait(
113+
traitName: "WithGenericParameters",
114+
documentation: """
115+
Syntax nodes that have generic parameters.
116+
117+
For example, functions or nominal types like `class` or `struct` can have generic parameters \
118+
and have a generic where clause that restricts these generic parameters.
119+
""",
120+
children: [
121+
Child(
122+
name: "GenericParameterClause",
123+
kind: .node(kind: .genericParameterClause),
124+
documentation: "The parameter clause that defines the generic parameters.",
125+
isOptional: true
126+
),
127+
Child(
128+
name: "GenericWhereClause",
129+
kind: .node(kind: .genericWhereClause),
130+
documentation: "A `where` clause that places additional constraints on generic parameters like `where Element: Hashable`.",
131+
isOptional: true
132+
),
133+
]
134+
),
95135
Trait(
96136
traitName: "WithModifiers",
97137
children: [
@@ -110,21 +150,9 @@ public let TRAITS: [Trait] = [
110150
Child(name: "TrailingComma", kind: .token(choices: [.token(tokenKind: "CommaToken")]), isOptional: true)
111151
]
112152
),
113-
Trait(
114-
traitName: "MissingNode",
115-
documentation: """
116-
Represents a layout node that is missing in the source file.
117-
118-
See the types conforming to this protocol for examples of where missing nodes can occur.
119-
""",
120-
children: [
121-
Child(
122-
name: "Placeholder",
123-
kind: .token(choices: [.token(tokenKind: "IdentifierToken")]),
124-
documentation: """
125-
A placeholder, i.e. `<#placeholder#>`, that can be inserted into the source code to represent the missing node.
126-
"""
127-
)
128-
]
129-
),
130153
]
154+
155+
//==========================================================================//
156+
// IMPORTANT: If you are tempted to add a trait here please insert in in //
157+
// alphabetical order above //
158+
//==========================================================================//

Sources/SwiftSyntax/Documentation.docc/generated/SwiftSyntax.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,14 @@ These articles are intended for developers wishing to contribute to SwiftSyntax
377377
- <doc:SwiftSyntax/EffectSpecifiersSyntax>
378378
- <doc:SwiftSyntax/FreestandingMacroExpansionSyntax>
379379
- <doc:SwiftSyntax/IdentifiedDeclSyntax>
380+
- <doc:SwiftSyntax/MissingNodeSyntax>
380381
- <doc:SwiftSyntax/ParenthesizedSyntax>
381382
- <doc:SwiftSyntax/WithAttributesSyntax>
382383
- <doc:SwiftSyntax/WithCodeBlockSyntax>
384+
- <doc:SwiftSyntax/WithGenericParametersSyntax>
383385
- <doc:SwiftSyntax/WithModifiersSyntax>
384386
- <doc:SwiftSyntax/WithStatementsSyntax>
385387
- <doc:SwiftSyntax/WithTrailingCommaSyntax>
386-
- <doc:SwiftSyntax/MissingNodeSyntax>
387388

388389

389390

Sources/SwiftSyntax/generated/SyntaxTraits.swift

Lines changed: 95 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,46 @@ public extension SyntaxProtocol {
277277
}
278278
}
279279

280+
// MARK: - MissingNodeSyntax
281+
282+
/// Represents a layout node that is missing in the source file.
283+
///
284+
/// See the types conforming to this protocol for examples of where missing nodes can occur.
285+
public protocol MissingNodeSyntax: SyntaxProtocol {
286+
/// A placeholder, i.e. `<#placeholder#>`, that can be inserted into the source code to represent the missing node.
287+
var placeholder: TokenSyntax {
288+
get
289+
set
290+
}
291+
}
292+
293+
public extension MissingNodeSyntax {
294+
/// Without this function, the `with` function defined on `SyntaxProtocol`
295+
/// does not work on existentials of this protocol type.
296+
@_disfavoredOverload
297+
func with<T>(_ keyPath: WritableKeyPath<MissingNodeSyntax, T>, _ newChild: T) -> MissingNodeSyntax {
298+
var copy: MissingNodeSyntax = self
299+
copy[keyPath: keyPath] = newChild
300+
return copy
301+
}
302+
}
303+
304+
public extension SyntaxProtocol {
305+
/// Check whether the non-type erased version of this syntax node conforms to
306+
/// `MissingNodeSyntax`.
307+
/// Note that this will incur an existential conversion.
308+
func isProtocol(_: MissingNodeSyntax.Protocol) -> Bool {
309+
return self.asProtocol(MissingNodeSyntax.self) != nil
310+
}
311+
312+
/// Return the non-type erased version of this syntax node if it conforms to
313+
/// `MissingNodeSyntax`. Otherwise return `nil`.
314+
/// Note that this will incur an existential conversion.
315+
func asProtocol(_: MissingNodeSyntax.Protocol) -> MissingNodeSyntax? {
316+
return Syntax(self).asProtocol(SyntaxProtocol.self) as? MissingNodeSyntax
317+
}
318+
}
319+
280320
// MARK: - ParenthesizedSyntax
281321

282322

@@ -393,6 +433,52 @@ public extension SyntaxProtocol {
393433
}
394434
}
395435

436+
// MARK: - WithGenericParametersSyntax
437+
438+
/// Syntax nodes that have generic parameters.
439+
///
440+
/// For example, functions or nominal types like `class` or `struct` can have generic parameters and have a generic where clause that restricts these generic parameters.
441+
public protocol WithGenericParametersSyntax: SyntaxProtocol {
442+
/// The parameter clause that defines the generic parameters.
443+
var genericParameterClause: GenericParameterClauseSyntax? {
444+
get
445+
set
446+
}
447+
448+
/// A `where` clause that places additional constraints on generic parameters like `where Element: Hashable`.
449+
var genericWhereClause: GenericWhereClauseSyntax? {
450+
get
451+
set
452+
}
453+
}
454+
455+
public extension WithGenericParametersSyntax {
456+
/// Without this function, the `with` function defined on `SyntaxProtocol`
457+
/// does not work on existentials of this protocol type.
458+
@_disfavoredOverload
459+
func with<T>(_ keyPath: WritableKeyPath<WithGenericParametersSyntax, T>, _ newChild: T) -> WithGenericParametersSyntax {
460+
var copy: WithGenericParametersSyntax = self
461+
copy[keyPath: keyPath] = newChild
462+
return copy
463+
}
464+
}
465+
466+
public extension SyntaxProtocol {
467+
/// Check whether the non-type erased version of this syntax node conforms to
468+
/// `WithGenericParametersSyntax`.
469+
/// Note that this will incur an existential conversion.
470+
func isProtocol(_: WithGenericParametersSyntax.Protocol) -> Bool {
471+
return self.asProtocol(WithGenericParametersSyntax.self) != nil
472+
}
473+
474+
/// Return the non-type erased version of this syntax node if it conforms to
475+
/// `WithGenericParametersSyntax`. Otherwise return `nil`.
476+
/// Note that this will incur an existential conversion.
477+
func asProtocol(_: WithGenericParametersSyntax.Protocol) -> WithGenericParametersSyntax? {
478+
return Syntax(self).asProtocol(SyntaxProtocol.self) as? WithGenericParametersSyntax
479+
}
480+
}
481+
396482
// MARK: - WithModifiersSyntax
397483

398484

@@ -504,46 +590,6 @@ public extension SyntaxProtocol {
504590
}
505591
}
506592

507-
// MARK: - MissingNodeSyntax
508-
509-
/// Represents a layout node that is missing in the source file.
510-
///
511-
/// See the types conforming to this protocol for examples of where missing nodes can occur.
512-
public protocol MissingNodeSyntax: SyntaxProtocol {
513-
/// A placeholder, i.e. `<#placeholder#>`, that can be inserted into the source code to represent the missing node.
514-
var placeholder: TokenSyntax {
515-
get
516-
set
517-
}
518-
}
519-
520-
public extension MissingNodeSyntax {
521-
/// Without this function, the `with` function defined on `SyntaxProtocol`
522-
/// does not work on existentials of this protocol type.
523-
@_disfavoredOverload
524-
func with<T>(_ keyPath: WritableKeyPath<MissingNodeSyntax, T>, _ newChild: T) -> MissingNodeSyntax {
525-
var copy: MissingNodeSyntax = self
526-
copy[keyPath: keyPath] = newChild
527-
return copy
528-
}
529-
}
530-
531-
public extension SyntaxProtocol {
532-
/// Check whether the non-type erased version of this syntax node conforms to
533-
/// `MissingNodeSyntax`.
534-
/// Note that this will incur an existential conversion.
535-
func isProtocol(_: MissingNodeSyntax.Protocol) -> Bool {
536-
return self.asProtocol(MissingNodeSyntax.self) != nil
537-
}
538-
539-
/// Return the non-type erased version of this syntax node if it conforms to
540-
/// `MissingNodeSyntax`. Otherwise return `nil`.
541-
/// Note that this will incur an existential conversion.
542-
func asProtocol(_: MissingNodeSyntax.Protocol) -> MissingNodeSyntax? {
543-
return Syntax(self).asProtocol(SyntaxProtocol.self) as? MissingNodeSyntax
544-
}
545-
}
546-
547593
extension AccessorBlockSyntax: BracedSyntax {}
548594

549595
extension AccessorDeclSyntax: WithAttributesSyntax {}
@@ -552,7 +598,7 @@ extension AccessorEffectSpecifiersSyntax: EffectSpecifiersSyntax {}
552598

553599
extension AccessorParameterSyntax: ParenthesizedSyntax {}
554600

555-
extension ActorDeclSyntax: DeclGroupSyntax, IdentifiedDeclSyntax, WithAttributesSyntax, WithModifiersSyntax {}
601+
extension ActorDeclSyntax: DeclGroupSyntax, IdentifiedDeclSyntax, WithAttributesSyntax, WithGenericParametersSyntax, WithModifiersSyntax {}
556602

557603
extension ArrayElementSyntax: WithTrailingCommaSyntax {}
558604

@@ -570,7 +616,7 @@ extension CatchClauseSyntax: WithCodeBlockSyntax {}
570616

571617
extension CatchItemSyntax: WithTrailingCommaSyntax {}
572618

573-
extension ClassDeclSyntax: DeclGroupSyntax, IdentifiedDeclSyntax, WithAttributesSyntax, WithModifiersSyntax {}
619+
extension ClassDeclSyntax: DeclGroupSyntax, IdentifiedDeclSyntax, WithAttributesSyntax, WithGenericParametersSyntax, WithModifiersSyntax {}
574620

575621
extension ClosureCaptureItemSyntax: WithTrailingCommaSyntax {}
576622

@@ -614,15 +660,15 @@ extension EnumCaseParameterClauseSyntax: ParenthesizedSyntax {}
614660

615661
extension EnumCaseParameterSyntax: WithTrailingCommaSyntax, WithModifiersSyntax {}
616662

617-
extension EnumDeclSyntax: DeclGroupSyntax, IdentifiedDeclSyntax, WithAttributesSyntax, WithModifiersSyntax {}
663+
extension EnumDeclSyntax: DeclGroupSyntax, IdentifiedDeclSyntax, WithAttributesSyntax, WithGenericParametersSyntax, WithModifiersSyntax {}
618664

619665
extension ExpressionSegmentSyntax: ParenthesizedSyntax {}
620666

621667
extension ExtensionDeclSyntax: DeclGroupSyntax, WithAttributesSyntax, WithModifiersSyntax {}
622668

623669
extension ForInStmtSyntax: WithCodeBlockSyntax {}
624670

625-
extension FunctionDeclSyntax: IdentifiedDeclSyntax, WithAttributesSyntax, WithModifiersSyntax {}
671+
extension FunctionDeclSyntax: IdentifiedDeclSyntax, WithAttributesSyntax, WithGenericParametersSyntax, WithModifiersSyntax {}
626672

627673
extension FunctionEffectSpecifiersSyntax: EffectSpecifiersSyntax {}
628674

@@ -644,11 +690,11 @@ extension ImportDeclSyntax: WithAttributesSyntax, WithModifiersSyntax {}
644690

645691
extension InheritedTypeSyntax: WithTrailingCommaSyntax {}
646692

647-
extension InitializerDeclSyntax: WithAttributesSyntax, WithModifiersSyntax {}
693+
extension InitializerDeclSyntax: WithAttributesSyntax, WithGenericParametersSyntax, WithModifiersSyntax {}
648694

649695
extension LabeledSpecializeEntrySyntax: WithTrailingCommaSyntax {}
650696

651-
extension MacroDeclSyntax: IdentifiedDeclSyntax, WithAttributesSyntax, WithModifiersSyntax {}
697+
extension MacroDeclSyntax: IdentifiedDeclSyntax, WithAttributesSyntax, WithGenericParametersSyntax, WithModifiersSyntax {}
652698

653699
extension MacroExpansionDeclSyntax: FreestandingMacroExpansionSyntax, WithAttributesSyntax, WithModifiersSyntax {}
654700

@@ -688,9 +734,9 @@ extension RepeatWhileStmtSyntax: WithCodeBlockSyntax {}
688734

689735
extension SourceFileSyntax: WithStatementsSyntax {}
690736

691-
extension StructDeclSyntax: DeclGroupSyntax, IdentifiedDeclSyntax, WithAttributesSyntax, WithModifiersSyntax {}
737+
extension StructDeclSyntax: DeclGroupSyntax, IdentifiedDeclSyntax, WithAttributesSyntax, WithGenericParametersSyntax, WithModifiersSyntax {}
692738

693-
extension SubscriptDeclSyntax: WithAttributesSyntax, WithModifiersSyntax {}
739+
extension SubscriptDeclSyntax: WithAttributesSyntax, WithGenericParametersSyntax, WithModifiersSyntax {}
694740

695741
extension SwitchCaseSyntax: WithStatementsSyntax {}
696742

@@ -712,7 +758,7 @@ extension TupleTypeSyntax: ParenthesizedSyntax {}
712758

713759
extension TypeEffectSpecifiersSyntax: EffectSpecifiersSyntax {}
714760

715-
extension TypealiasDeclSyntax: IdentifiedDeclSyntax, WithAttributesSyntax, WithModifiersSyntax {}
761+
extension TypealiasDeclSyntax: IdentifiedDeclSyntax, WithAttributesSyntax, WithGenericParametersSyntax, WithModifiersSyntax {}
716762

717763
extension VariableDeclSyntax: WithAttributesSyntax, WithModifiersSyntax {}
718764

0 commit comments

Comments
 (0)