Skip to content

Commit 1159abd

Browse files
authored
Merge pull request #1272 from DougGregor/freestanding-macro-expansion-syntax-trait
2 parents 82b90a8 + 5768213 commit 1159abd

File tree

8 files changed

+115
-0
lines changed

8 files changed

+115
-0
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/DeclNodes.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,9 @@ public let DECL_NODES: [Node] = [
16021602
Node(name: "MacroExpansionDecl",
16031603
nameForDiagnostics: "pound literal declaration",
16041604
kind: "Decl",
1605+
traits: [
1606+
"FreestandingMacroExpansion"
1607+
],
16051608
children: [
16061609
Child(name: "PoundToken",
16071610
kind: "PoundToken",

CodeGeneration/Sources/SyntaxSupport/gyb_generated/ExprNodes.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,9 @@ public let EXPR_NODES: [Node] = [
11621162
Node(name: "MacroExpansionExpr",
11631163
nameForDiagnostics: "pound literal expression",
11641164
kind: "Expr",
1165+
traits: [
1166+
"FreestandingMacroExpansion"
1167+
],
11651168
children: [
11661169
Child(name: "PoundToken",
11671170
kind: "PoundToken",

CodeGeneration/Sources/SyntaxSupport/gyb_generated/Traits.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ public let TRAITS: [Trait] = [
5959
Child(name: "RightParen", kind: "RightParenToken"),
6060
]
6161
),
62+
Trait(traitName: "FreestandingMacroExpansion",
63+
children: [
64+
Child(name: "PoundToken", kind: "PoundToken"),
65+
Child(name: "Macro", kind: "IdentifierToken"),
66+
Child(name: "GenericArguments", kind: "GenericArgumentClause", isOptional: true),
67+
Child(name: "LeftParen", kind: "LeftParenToken", isOptional: true),
68+
Child(name: "ArgumentList", kind: "TupleExprElementList"),
69+
Child(name: "RightParen", kind: "RightParenToken", isOptional: true),
70+
Child(name: "TrailingClosure", kind: "ClosureExpr", isOptional: true),
71+
Child(name: "AdditionalTrailingClosures", kind: "MultipleTrailingClosureElementList", isOptional: true),
72+
]
73+
),
6274
Trait(traitName: "WithTrailingComma",
6375
children: [
6476
Child(name: "TrailingComma", kind: "CommaToken", isOptional: true),

Sources/SwiftSyntax/Documentation.docc/gyb_generated/SwiftSyntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
435435
- <doc:SwiftSyntax/IdentifiedDeclSyntax>
436436
- <doc:SwiftSyntax/WithCodeBlockSyntax>
437437
- <doc:SwiftSyntax/ParenthesizedSyntax>
438+
- <doc:SwiftSyntax/FreestandingMacroExpansionSyntax>
438439
- <doc:SwiftSyntax/WithTrailingCommaSyntax>
439440
- <doc:SwiftSyntax/WithStatementsSyntax>
440441
- <doc:SwiftSyntax/EffectSpecifiersSyntax>

Sources/SwiftSyntax/generated/SyntaxTraits.swift

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,74 @@ public extension SyntaxProtocol {
180180
}
181181
}
182182

183+
// MARK: - FreestandingMacroExpansionSyntax
184+
185+
public protocol FreestandingMacroExpansionSyntax: SyntaxProtocol {
186+
var poundToken: TokenSyntax {
187+
get
188+
}
189+
190+
func withPoundToken(_ newChild: TokenSyntax) -> Self
191+
192+
var macro: TokenSyntax {
193+
get
194+
}
195+
196+
func withMacro(_ newChild: TokenSyntax) -> Self
197+
198+
var genericArguments: GenericArgumentClauseSyntax? {
199+
get
200+
}
201+
202+
func withGenericArguments(_ newChild: GenericArgumentClauseSyntax?) -> Self
203+
204+
var leftParen: TokenSyntax? {
205+
get
206+
}
207+
208+
func withLeftParen(_ newChild: TokenSyntax?) -> Self
209+
210+
var argumentList: TupleExprElementListSyntax {
211+
get
212+
}
213+
214+
func withArgumentList(_ newChild: TupleExprElementListSyntax) -> Self
215+
216+
var rightParen: TokenSyntax? {
217+
get
218+
}
219+
220+
func withRightParen(_ newChild: TokenSyntax?) -> Self
221+
222+
var trailingClosure: ClosureExprSyntax? {
223+
get
224+
}
225+
226+
func withTrailingClosure(_ newChild: ClosureExprSyntax?) -> Self
227+
228+
var additionalTrailingClosures: MultipleTrailingClosureElementListSyntax? {
229+
get
230+
}
231+
232+
func withAdditionalTrailingClosures(_ newChild: MultipleTrailingClosureElementListSyntax?) -> Self
233+
}
234+
235+
public extension SyntaxProtocol {
236+
/// Check whether the non-type erased version of this syntax node conforms to
237+
/// `FreestandingMacroExpansionSyntax`.
238+
/// Note that this will incur an existential conversion.
239+
func isProtocol(_: FreestandingMacroExpansionSyntax.Protocol) -> Bool {
240+
return self.asProtocol(FreestandingMacroExpansionSyntax.self) != nil
241+
}
242+
243+
/// Return the non-type erased version of this syntax node if it conforms to
244+
/// `FreestandingMacroExpansionSyntax`. Otherwise return `nil`.
245+
/// Note that this will incur an existential conversion.
246+
func asProtocol(_: FreestandingMacroExpansionSyntax.Protocol) -> FreestandingMacroExpansionSyntax? {
247+
return Syntax(self).asProtocol(SyntaxProtocol.self) as? FreestandingMacroExpansionSyntax
248+
}
249+
}
250+
183251
// MARK: - WithTrailingCommaSyntax
184252

185253
public protocol WithTrailingCommaSyntax: SyntaxProtocol {
@@ -417,6 +485,12 @@ extension LabeledSpecializeEntrySyntax: WithTrailingCommaSyntax {
417485
extension MacroDeclSyntax: IdentifiedDeclSyntax, AttributedSyntax {
418486
}
419487

488+
extension MacroExpansionDeclSyntax: FreestandingMacroExpansionSyntax {
489+
}
490+
491+
extension MacroExpansionExprSyntax: FreestandingMacroExpansionSyntax {
492+
}
493+
420494
extension MemberDeclBlockSyntax: BracedSyntax {
421495
}
422496

gyb_syntax_support/DeclNodes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@
917917
# e.g., "#embed("filename.txt")"
918918
Node('MacroExpansionDecl',
919919
name_for_diagnostics="pound literal declaration", kind='Decl',
920+
traits=['FreestandingMacroExpansion'],
920921
children=[
921922
Child('PoundToken', kind='PoundToken',
922923
description='The `#` sign.'),

gyb_syntax_support/ExprNodes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@
649649
# e.g., "#embed("filename.txt")"
650650
Node('MacroExpansionExpr',
651651
name_for_diagnostics="pound literal expression", kind='Expr',
652+
traits=['FreestandingMacroExpansion'],
652653
children=[
653654
Child('PoundToken', kind='PoundToken',
654655
description='The `#` sign.'),

gyb_syntax_support/Traits.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ def __init__(self, trait_name, description=None, children=None):
4343
Child('RightParen', kind='RightParenToken'),
4444
]),
4545

46+
Trait('FreestandingMacroExpansion',
47+
children=[
48+
Child('PoundToken', kind='PoundToken'),
49+
Child('Macro', kind='IdentifierToken'),
50+
Child('GenericArguments', kind='GenericArgumentClause',
51+
is_optional=True),
52+
Child('LeftParen', kind='LeftParenToken',
53+
is_optional=True),
54+
Child('ArgumentList', kind='TupleExprElementList',
55+
collection_element_name='Argument'),
56+
Child('RightParen', kind='RightParenToken',
57+
is_optional=True),
58+
Child('TrailingClosure', kind='ClosureExpr',
59+
is_optional=True),
60+
Child('AdditionalTrailingClosures',
61+
kind='MultipleTrailingClosureElementList',
62+
collection_element_name='AdditionalTrailingClosure',
63+
is_optional=True),
64+
]),
65+
4666
Trait('WithTrailingComma',
4767
children=[
4868
Child('TrailingComma', kind='CommaToken', is_optional=True),

0 commit comments

Comments
 (0)