Skip to content

Commit fc8f9dc

Browse files
authored
Merge pull request #1675 from rintaro/5.9-macros-attributes-rdar107386648
[5.9][Macro] Attributes for freestanding macro expansion
2 parents 03cb7b0 + d403bc0 commit fc8f9dc

32 files changed

+1533
-376
lines changed

CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ public let COMMON_NODES: [Node] = [
147147
description: "In case the source code is missing a declaration, this node stands in place of the missing declaration.",
148148
kind: "Decl",
149149
traits: [
150-
"Attributed"
150+
"WithAttributes",
151+
"WithModifiers",
151152
],
152153
children: [
153154
Child(

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public let DECL_NODES: [Node] = [
6868
nameForDiagnostics: "accessor",
6969
kind: "Decl",
7070
traits: [
71-
"Attributed"
71+
"WithAttributes"
7272
],
7373
parserFunction: "parseAccessorDecl",
7474
children: [
@@ -154,7 +154,8 @@ public let DECL_NODES: [Node] = [
154154
traits: [
155155
"DeclGroup",
156156
"IdentifiedDecl",
157-
"Attributed",
157+
"WithAttributes",
158+
"WithModifiers",
158159
],
159160
children: [
160161
Child(
@@ -213,7 +214,8 @@ public let DECL_NODES: [Node] = [
213214
kind: "Decl",
214215
traits: [
215216
"IdentifiedDecl",
216-
"Attributed",
217+
"WithAttributes",
218+
"WithModifiers",
217219
],
218220
children: [
219221
Child(
@@ -270,7 +272,8 @@ public let DECL_NODES: [Node] = [
270272
traits: [
271273
"DeclGroup",
272274
"IdentifiedDecl",
273-
"Attributed",
275+
"WithAttributes",
276+
"WithModifiers",
274277
],
275278
children: [
276279
Child(
@@ -401,7 +404,8 @@ public let DECL_NODES: [Node] = [
401404
nameForDiagnostics: "deinitializer",
402405
kind: "Decl",
403406
traits: [
404-
"Attributed"
407+
"WithAttributes",
408+
"WithModifiers",
405409
],
406410
children: [
407411
Child(
@@ -505,7 +509,8 @@ public let DECL_NODES: [Node] = [
505509
nameForDiagnostics: "parameter",
506510
kind: "Syntax",
507511
traits: [
508-
"WithTrailingComma"
512+
"WithTrailingComma",
513+
"WithModifiers",
509514
],
510515
parserFunction: "parseEnumCaseParameter",
511516
children: [
@@ -559,7 +564,8 @@ public let DECL_NODES: [Node] = [
559564
description: "A `case` declaration of a Swift `enum`. It can have 1 or more `EnumCaseElement`s inside, each declaring a different case of the enum.",
560565
kind: "Decl",
561566
traits: [
562-
"Attributed"
567+
"WithAttributes",
568+
"WithModifiers",
563569
],
564570
children: [
565571
Child(
@@ -642,7 +648,8 @@ public let DECL_NODES: [Node] = [
642648
traits: [
643649
"DeclGroup",
644650
"IdentifiedDecl",
645-
"Attributed",
651+
"WithAttributes",
652+
"WithModifiers",
646653
],
647654
children: [
648655
Child(
@@ -710,7 +717,8 @@ public let DECL_NODES: [Node] = [
710717
kind: "Decl",
711718
traits: [
712719
"DeclGroup",
713-
"Attributed",
720+
"WithAttributes",
721+
"WithModifiers",
714722
],
715723
children: [
716724
Child(
@@ -758,7 +766,8 @@ public let DECL_NODES: [Node] = [
758766
kind: "Decl",
759767
traits: [
760768
"IdentifiedDecl",
761-
"Attributed",
769+
"WithAttributes",
770+
"WithModifiers",
762771
],
763772
children: [
764773
Child(
@@ -822,7 +831,8 @@ public let DECL_NODES: [Node] = [
822831
kind: "Syntax",
823832
traits: [
824833
"WithTrailingComma",
825-
"Attributed",
834+
"WithAttributes",
835+
"WithModifiers",
826836
],
827837
parserFunction: "parseFunctionParameter",
828838
children: [
@@ -983,7 +993,8 @@ public let DECL_NODES: [Node] = [
983993
nameForDiagnostics: "import",
984994
kind: "Decl",
985995
traits: [
986-
"Attributed"
996+
"WithAttributes",
997+
"WithModifiers",
987998
],
988999
children: [
9891000
Child(
@@ -1073,7 +1084,8 @@ public let DECL_NODES: [Node] = [
10731084
""",
10741085
kind: "Decl",
10751086
traits: [
1076-
"Attributed"
1087+
"WithAttributes",
1088+
"WithModifiers",
10771089
],
10781090
children: [
10791091
Child(
@@ -1136,7 +1148,8 @@ public let DECL_NODES: [Node] = [
11361148
kind: "Decl",
11371149
traits: [
11381150
"IdentifiedDecl",
1139-
"Attributed",
1151+
"WithAttributes",
1152+
"WithModifiers",
11401153
],
11411154
children: [
11421155
Child(
@@ -1191,9 +1204,23 @@ public let DECL_NODES: [Node] = [
11911204
nameForDiagnostics: "macro expansion",
11921205
kind: "Decl",
11931206
traits: [
1194-
"FreestandingMacroExpansion"
1207+
"FreestandingMacroExpansion",
1208+
"WithAttributes",
1209+
"WithModifiers",
11951210
],
11961211
children: [
1212+
Child(
1213+
name: "Attributes",
1214+
kind: .collection(kind: "AttributeList", collectionElementName: "Attribute"),
1215+
nameForDiagnostics: "attributes",
1216+
isOptional: true
1217+
),
1218+
Child(
1219+
name: "Modifiers",
1220+
kind: .collection(kind: "ModifierList", collectionElementName: "Modifier"),
1221+
nameForDiagnostics: "modifiers",
1222+
isOptional: true
1223+
),
11971224
Child(
11981225
name: "PoundToken",
11991226
kind: .token(choices: [.token(tokenKind: "PoundToken")]),
@@ -1331,7 +1358,8 @@ public let DECL_NODES: [Node] = [
13311358
kind: "Decl",
13321359
traits: [
13331360
"IdentifiedDecl",
1334-
"Attributed",
1361+
"WithAttributes",
1362+
"WithModifiers",
13351363
],
13361364
children: [
13371365
Child(
@@ -1607,7 +1635,8 @@ public let DECL_NODES: [Node] = [
16071635
kind: "Decl",
16081636
traits: [
16091637
"IdentifiedDecl",
1610-
"Attributed",
1638+
"WithAttributes",
1639+
"WithModifiers",
16111640
],
16121641
children: [
16131642
Child(
@@ -1709,7 +1738,8 @@ public let DECL_NODES: [Node] = [
17091738
traits: [
17101739
"DeclGroup",
17111740
"IdentifiedDecl",
1712-
"Attributed",
1741+
"WithAttributes",
1742+
"WithModifiers",
17131743
],
17141744
children: [
17151745
Child(
@@ -1811,7 +1841,8 @@ public let DECL_NODES: [Node] = [
18111841
traits: [
18121842
"DeclGroup",
18131843
"IdentifiedDecl",
1814-
"Attributed",
1844+
"WithAttributes",
1845+
"WithModifiers",
18151846
],
18161847
children: [
18171848
Child(
@@ -1864,7 +1895,8 @@ public let DECL_NODES: [Node] = [
18641895
nameForDiagnostics: "subscript",
18651896
kind: "Decl",
18661897
traits: [
1867-
"Attributed"
1898+
"WithAttributes",
1899+
"WithModifiers",
18681900
],
18691901
children: [
18701902
Child(
@@ -1965,7 +1997,7 @@ public let DECL_NODES: [Node] = [
19651997
kind: "Decl",
19661998
traits: [
19671999
"IdentifiedDecl",
1968-
"Attributed",
2000+
"WithAttributes",
19692001
],
19702002
children: [
19712003
Child(
@@ -2012,7 +2044,8 @@ public let DECL_NODES: [Node] = [
20122044
nameForDiagnostics: "variable",
20132045
kind: "Decl",
20142046
traits: [
2015-
"Attributed"
2047+
"WithAttributes",
2048+
"WithModifiers",
20162049
],
20172050
children: [
20182051
Child(

CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ public let EXPR_NODES: [Node] = [
306306
nameForDiagnostics: "parameter",
307307
kind: "Syntax",
308308
traits: [
309-
"WithTrailingComma"
309+
"WithTrailingComma",
310+
"WithAttributes",
311+
"WithModifiers",
310312
],
311313
parserFunction: "parseClosureParameter",
312314
children: [
@@ -461,7 +463,7 @@ public let EXPR_NODES: [Node] = [
461463
nameForDiagnostics: "closure signature",
462464
kind: "Syntax",
463465
traits: [
464-
"Attributed"
466+
"WithAttributes"
465467
],
466468
children: [
467469
Child(

CodeGeneration/Sources/SyntaxSupport/GenericNodes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public let GENERIC_NODES: [Node] = [
7575
kind: "Syntax",
7676
traits: [
7777
"WithTrailingComma",
78-
"Attributed",
78+
"WithAttributes",
7979
],
8080
children: [
8181
Child(

CodeGeneration/Sources/SyntaxSupport/Traits.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ public class Trait {
2323
}
2424

2525
public let TRAITS: [Trait] = [
26-
Trait(
27-
traitName: "Attributed",
28-
children: [
29-
Child(name: "Attributes", kind: .node(kind: "AttributeList"), isOptional: true)
30-
]
31-
),
3226
Trait(
3327
traitName: "Braced",
3428
children: [
@@ -80,12 +74,24 @@ public let TRAITS: [Trait] = [
8074
Child(name: "RightParen", kind: .token(choices: [.token(tokenKind: "RightParenToken")])),
8175
]
8276
),
77+
Trait(
78+
traitName: "WithAttributes",
79+
children: [
80+
Child(name: "Attributes", kind: .node(kind: "AttributeList"), isOptional: true)
81+
]
82+
),
8383
Trait(
8484
traitName: "WithCodeBlock",
8585
children: [
8686
Child(name: "Body", kind: .node(kind: "CodeBlock"))
8787
]
8888
),
89+
Trait(
90+
traitName: "WithModifiers",
91+
children: [
92+
Child(name: "Modifiers", kind: .node(kind: "ModifierList"), isOptional: true)
93+
]
94+
),
8995
Trait(
9096
traitName: "WithStatements",
9197
children: [

CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public let TYPE_NODES: [Node] = [
3939
nameForDiagnostics: "type",
4040
kind: "Type",
4141
traits: [
42-
"Attributed"
42+
"WithAttributes"
4343
],
4444
children: [
4545
Child(

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/SyntaxCollectionsFile.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
1919
DeclSyntax(
2020
"""
2121
public protocol SyntaxCollection: SyntaxProtocol, Sequence where Element: SyntaxProtocol {
22+
/// Creates a new collection with the elements.
23+
init(_ children: [Element])
2224
/// The number of elements, `present` or `missing`, in this collection.
2325
var count: Int { get }
2426
}

Package.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ let package = Package(
5757
.library(name: "SwiftSyntax", targets: ["SwiftSyntax"]),
5858
.library(name: "SwiftSyntaxBuilder", targets: ["SwiftSyntaxBuilder"]),
5959
.library(name: "SwiftSyntaxMacros", targets: ["SwiftSyntaxMacros"]),
60+
.library(name: "SwiftSyntaxMacroExpansion", targets: ["SwiftSyntaxMacroExpansion"]),
6061
.library(name: "SwiftSyntaxMacrosTestSupport", targets: ["SwiftSyntaxMacrosTestSupport"]),
6162
],
6263
targets: [
@@ -113,7 +114,7 @@ let package = Package(
113114

114115
.target(
115116
name: "SwiftCompilerPluginMessageHandling",
116-
dependencies: ["SwiftDiagnostics", "SwiftOperators", "SwiftParser", "SwiftSyntax", "SwiftSyntaxMacros"],
117+
dependencies: ["SwiftDiagnostics", "SwiftOperators", "SwiftParser", "SwiftSyntax", "SwiftSyntaxMacros", "SwiftSyntaxMacroExpansion"],
117118
exclude: ["CMakeLists.txt"]
118119
),
119120

@@ -183,6 +184,14 @@ let package = Package(
183184
dependencies: ["_SwiftSyntaxTestSupport", "SwiftDiagnostics", "SwiftOperators", "SwiftParser", "SwiftSyntaxBuilder", "SwiftSyntaxMacros", "SwiftSyntaxMacrosTestSupport"]
184185
),
185186

187+
// MARK: SwiftSyntaxMacroExpansion
188+
189+
.target(
190+
name: "SwiftSyntaxMacroExpansion",
191+
dependencies: ["SwiftSyntax", "SwiftSyntaxMacros"],
192+
exclude: ["CMakeLists.txt"]
193+
),
194+
186195
// MARK: SwiftSyntaxMacrosTestSupport
187196

188197
.target(

Sources/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ add_subdirectory(SwiftParserDiagnostics)
3939
add_subdirectory(SwiftOperators)
4040
add_subdirectory(SwiftSyntaxBuilder)
4141
add_subdirectory(SwiftSyntaxMacros)
42+
add_subdirectory(SwiftSyntaxMacroExpansion)
4243
add_subdirectory(SwiftCompilerPluginMessageHandling)
4344
add_subdirectory(SwiftIDEUtils)

Sources/SwiftCompilerPluginMessageHandling/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ target_link_libraries(SwiftCompilerPluginMessageHandling PUBLIC
2020
SwiftDiagnostics
2121
SwiftParser
2222
SwiftSyntaxMacros
23+
SwiftSyntaxMacroExpansion
2324
SwiftOperators)

0 commit comments

Comments
 (0)