Skip to content

Commit 3688938

Browse files
authored
Merge pull request #2968 from rintaro/compiler-generated-files
Add Syntax nodes for compiler generated files
2 parents d137b28 + 32c599d commit 3688938

32 files changed

+1854
-151
lines changed

CodeGeneration/Sources/SyntaxSupport/AvailabilityNodes.swift

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -181,30 +181,4 @@ public let AVAILABILITY_NODES: [Node] = [
181181
),
182182
]
183183
),
184-
185-
Node(
186-
kind: .availabilityMacroDefinition,
187-
base: .syntax,
188-
spi: "Compiler",
189-
nameForDiagnostics: "availability macro definition",
190-
documentation: "Syntax for '-define-availability' compiler arguments, never appear in Swift source code",
191-
parserFunction: "parseAvailabilityMacroDefinition",
192-
children: [
193-
Child(
194-
name: "platformVersion",
195-
kind: .node(kind: .platformVersion)
196-
),
197-
Child(
198-
name: "colon",
199-
kind: .token(choices: [.token(.colon)])
200-
),
201-
Child(
202-
name: "specs",
203-
kind: .collection(
204-
kind: .availabilityArgumentList,
205-
collectionElementName: "AvailabilityArgument"
206-
)
207-
),
208-
]
209-
),
210184
]

CodeGeneration/Sources/SyntaxSupport/Child.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public enum ChildKind {
4545
/// The child is a collection of `kind`.
4646
case collection(
4747
kind: SyntaxNodeKind,
48-
collectionElementName: String,
48+
collectionElementName: String? = nil,
4949
defaultsToEmpty: Bool = false,
5050
deprecatedCollectionElementName: String? = nil
5151
)
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
public let COMPILER_NODES: [Node] = [
14+
Node(
15+
kind: .availabilityMacroDefinitionFile,
16+
base: .syntax,
17+
spi: "Compiler",
18+
nameForDiagnostics: "availability macro definition",
19+
documentation: """
20+
Syntax for '-define-availability' compiler arguments, never appear in Swift source code.
21+
""",
22+
parserFunction: "parseAvailabilityMacroDefinitionFile",
23+
children: [
24+
Child(
25+
name: "platformVersion",
26+
kind: .node(kind: .platformVersion)
27+
),
28+
Child(
29+
name: "colon",
30+
kind: .token(choices: [.token(.colon)])
31+
),
32+
Child(
33+
name: "specs",
34+
kind: .collection(kind: .availabilityArgumentList)
35+
),
36+
Child(
37+
name: "endOfFileToken",
38+
kind: .token(choices: [.token(.endOfFile)])
39+
),
40+
]
41+
),
42+
43+
Node(
44+
kind: .accessorBlockFile,
45+
base: .syntax,
46+
spi: "Compiler",
47+
nameForDiagnostics: "accessors",
48+
documentation: """
49+
Syntax for 'accessor' macro expansion, never appear in Swift source code.
50+
""",
51+
parserFunction: "parseAccessorBlockFile",
52+
children: [
53+
Child(
54+
name: "leftBrace",
55+
kind: .token(choices: [.token(.leftBrace)]),
56+
isOptional: true
57+
),
58+
Child(
59+
name: "accessors",
60+
kind: .collection(kind: .accessorDeclList)
61+
),
62+
Child(
63+
name: "rightBrace",
64+
kind: .token(choices: [.token(.rightBrace)]),
65+
isOptional: true
66+
),
67+
Child(
68+
name: "endOfFileToken",
69+
kind: .token(choices: [.token(.endOfFile)])
70+
),
71+
]
72+
),
73+
74+
Node(
75+
kind: .attributeClauseFile,
76+
base: .syntax,
77+
spi: "Compiler",
78+
nameForDiagnostics: "attribute list",
79+
documentation: """
80+
Syntax for 'memberAttribute' macro expansion or 'swift_attr' attribute in Clang, never appear in Swift source code.
81+
""",
82+
parserFunction: "parseAttributeClauseFile",
83+
traits: [
84+
"WithAttributes",
85+
"WithModifiers",
86+
],
87+
children: [
88+
Child(
89+
name: "attributes",
90+
kind: .collection(
91+
kind: .attributeList,
92+
collectionElementName: "Attribute"
93+
)
94+
),
95+
Child(
96+
name: "modifiers",
97+
kind: .collection(kind: .declModifierList)
98+
),
99+
Child(
100+
name: "endOfFileToken",
101+
kind: .token(choices: [.token(.endOfFile)])
102+
),
103+
]
104+
),
105+
106+
Node(
107+
kind: .codeBlockFile,
108+
base: .syntax,
109+
spi: "Compiler",
110+
nameForDiagnostics: "code block",
111+
documentation: """
112+
Syntax for 'body' macro expansions, never appear in Swift source code.
113+
""",
114+
parserFunction: "parseCodeBlockFile",
115+
children: [
116+
Child(
117+
name: "body",
118+
kind: .node(kind: .codeBlock)
119+
),
120+
Child(
121+
name: "endOfFileToken",
122+
kind: .token(choices: [.token(.endOfFile)])
123+
),
124+
]
125+
),
126+
127+
Node(
128+
kind: .memberBlockItemListFile,
129+
base: .syntax,
130+
spi: "Compiler",
131+
nameForDiagnostics: "member list",
132+
documentation: """
133+
Syntax for declaration macro expansions in type contexts, never appear in Swift source code.
134+
""",
135+
parserFunction: "parseMemberBlockItemListFile",
136+
children: [
137+
Child(
138+
name: "members",
139+
kind: .collection(kind: .memberBlockItemList)
140+
),
141+
Child(
142+
name: "endOfFileToken",
143+
kind: .token(choices: [.token(.endOfFile)])
144+
),
145+
]
146+
),
147+
]

CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKind.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible, TypeCon
2424
case _canImportVersionInfo
2525
case abiAttributeArguments
2626
case accessorBlock
27+
case accessorBlockFile
2728
case accessorDecl
2829
case accessorDeclList
2930
case accessorEffectSpecifiers
@@ -39,12 +40,13 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible, TypeCon
3940
case associatedTypeDecl
4041
case attribute
4142
case attributedType
43+
case attributeClauseFile
4244
case attributeList
4345
case availabilityArgument
4446
case availabilityArgumentList
4547
case availabilityCondition
4648
case availabilityLabeledArgument
47-
case availabilityMacroDefinition
49+
case availabilityMacroDefinitionFile
4850
case awaitExpr
4951
case backDeployedAttributeArguments
5052
case binaryOperatorExpr
@@ -69,6 +71,7 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible, TypeCon
6971
case closureShorthandParameterList
7072
case closureSignature
7173
case codeBlock
74+
case codeBlockFile
7275
case codeBlockItem
7376
case codeBlockItemList
7477
case compositionType
@@ -195,6 +198,7 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible, TypeCon
195198
case memberBlock
196199
case memberBlockItem
197200
case memberBlockItemList
201+
case memberBlockItemListFile
198202
case memberType
199203
case metatypeType
200204
case missing

CodeGeneration/Sources/SyntaxSupport/SyntaxNodes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ private let unsortedSyntaxNodes: [Node] =
2020
+ TYPE_NODES
2121
+ PATTERN_NODES
2222
+ AVAILABILITY_NODES
23+
+ COMPILER_NODES
2324

2425
public let SYNTAX_NODES: [Node] =
2526
unsortedSyntaxNodes

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RenamedChildrenCompatibilityFile.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,10 @@ func makeCompatibilityAddMethod(for child: Child) -> DeclSyntax? {
8383
!child.isUnexpectedNodes,
8484
case .collection(
8585
kind: _,
86-
collectionElementName: let collectionElementName,
86+
collectionElementName: let collectionElementName?,
8787
defaultsToEmpty: _,
88-
deprecatedCollectionElementName: let deprecatedCollectionElementName
89-
) = child.kind,
90-
let deprecatedCollectionElementName
88+
deprecatedCollectionElementName: let deprecatedCollectionElementName?
89+
) = child.kind
9190
{
9291
let childEltType = childNode.collectionElementType.syntaxBaseName
9392

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodesFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax {
176176
// If needed, this could be added in the future, but for now withUnexpected should be sufficient.
177177
if let childNode = SYNTAX_NODE_MAP[child.syntaxNodeKind]?.collectionNode,
178178
!child.isUnexpectedNodes,
179-
case .collection(_, collectionElementName: let childElt, _, _) = child.kind
179+
case .collection(_, collectionElementName: let childElt?, _, _) = child.kind
180180
{
181181
let childEltType = childNode.collectionElementType.syntaxBaseName
182182

CodeGeneration/Tests/ValidateSyntaxNodes/ValidateSyntaxNodes.swift

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,26 @@ class ValidateSyntaxNodes: XCTestCase {
300300
node: .sourceFile,
301301
message: "child 'endOfFileToken' has a token as its only token choice and should thus be named 'endOfFile'"
302302
),
303+
ValidationFailure(
304+
node: .availabilityMacroDefinitionFile,
305+
message: "child 'endOfFileToken' has a token as its only token choice and should thus be named 'endOfFile'"
306+
),
307+
ValidationFailure(
308+
node: .accessorBlockFile,
309+
message: "child 'endOfFileToken' has a token as its only token choice and should thus be named 'endOfFile'"
310+
),
311+
ValidationFailure(
312+
node: .attributeClauseFile,
313+
message: "child 'endOfFileToken' has a token as its only token choice and should thus be named 'endOfFile'"
314+
),
315+
ValidationFailure(
316+
node: .codeBlockFile,
317+
message: "child 'endOfFileToken' has a token as its only token choice and should thus be named 'endOfFile'"
318+
),
319+
ValidationFailure(
320+
node: .memberBlockItemListFile,
321+
message: "child 'endOfFileToken' has a token as its only token choice and should thus be named 'endOfFile'"
322+
),
303323
ValidationFailure(
304324
node: .stringLiteralExpr,
305325
message:
@@ -549,6 +569,7 @@ class ValidateSyntaxNodes: XCTestCase {
549569
message: "could conform to trait 'Parenthesized' but does not"
550570
),
551571
ValidationFailure(node: .lifetimeTypeSpecifier, message: "could conform to trait 'Parenthesized' but does not"),
572+
ValidationFailure(node: .codeBlockFile, message: "could conform to trait 'WithCodeBlock' but does not"),
552573
]
553574
)
554575
}
@@ -573,7 +594,27 @@ class ValidateSyntaxNodes: XCTestCase {
573594
failures,
574595
expectedFailures: [
575596
// it's not obvious that the end of file is represented by a token, thus its good to highlight it in the name
576-
ValidationFailure(node: .sourceFile, message: "child 'endOfFileToken' should not end with 'Token'")
597+
ValidationFailure(node: .sourceFile, message: "child 'endOfFileToken' should not end with 'Token'"),
598+
ValidationFailure(
599+
node: .availabilityMacroDefinitionFile,
600+
message: "child 'endOfFileToken' should not end with 'Token'"
601+
),
602+
ValidationFailure(
603+
node: .accessorBlockFile,
604+
message: "child 'endOfFileToken' should not end with 'Token'"
605+
),
606+
ValidationFailure(
607+
node: .attributeClauseFile,
608+
message: "child 'endOfFileToken' should not end with 'Token'"
609+
),
610+
ValidationFailure(
611+
node: .codeBlockFile,
612+
message: "child 'endOfFileToken' should not end with 'Token'"
613+
),
614+
ValidationFailure(
615+
node: .memberBlockItemListFile,
616+
message: "child 'endOfFileToken' should not end with 'Token'"
617+
),
577618
]
578619
)
579620
}
@@ -758,14 +799,21 @@ class ValidateSyntaxNodes: XCTestCase {
758799
failures.append(
759800
ValidationFailure(
760801
node: node.kind,
761-
message:
762-
"non-collection node should not contain 'List'"
802+
message: "non-collection node should not contain 'List'"
763803
)
764804
)
765805
}
766806
}
767807

768-
assertFailuresMatchXFails(failures, expectedFailures: [])
808+
assertFailuresMatchXFails(
809+
failures,
810+
expectedFailures: [
811+
ValidationFailure(
812+
node: .memberBlockItemListFile,
813+
message: "non-collection node should not contain 'List'"
814+
)
815+
]
816+
)
769817
}
770818

771819
/// Children should always have a plural as their name instead of ending with 'List'.

Sources/SwiftParser/Availability.swift

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -304,20 +304,3 @@ extension Parser {
304304
}
305305
}
306306
}
307-
308-
extension Parser {
309-
/// Parse an argument for '-define-availability' compiler option.
310-
mutating func parseAvailabilityMacroDefinition() -> RawAvailabilityMacroDefinitionSyntax {
311-
let namedAndVersion = self.parsePlatformVersion(allowStarAsVersionNumber: false)
312-
let (unexpectedBetweenPlatformVersionAndColon, colon) = self.expect(.colon)
313-
let specs = self.parseAvailabilitySpecList()
314-
315-
return RawAvailabilityMacroDefinitionSyntax(
316-
platformVersion: namedAndVersion,
317-
unexpectedBetweenPlatformVersionAndColon,
318-
colon: colon,
319-
specs: specs,
320-
arena: self.arena
321-
)
322-
}
323-
}

Sources/SwiftParser/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_swift_syntax_library(SwiftParser
1111
Availability.swift
1212
CharacterInfo.swift
1313
CollectionNodes+Parsable.swift
14+
CompilerFiles.swift
1415
Declarations.swift
1516
Directives.swift
1617
ExpressionInterpretedAsVersionTuple.swift

0 commit comments

Comments
 (0)