Skip to content

Commit 15e1494

Browse files
committed
reorg
1 parent 5ef41e6 commit 15e1494

File tree

10 files changed

+1507
-1467
lines changed

10 files changed

+1507
-1467
lines changed

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

+17-12
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,34 @@ import SyntaxSupport
1616
import Utils
1717

1818
let concreteSyntaxPropertyFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
19-
DeclSyntax("""
20-
extension ConcreteSyntaxProperty {
21-
fileprivate init(index: UInt32) {
22-
self.index = .init(index)
19+
DeclSyntax(
20+
"""
21+
extension ConcreteSyntaxProperty {
22+
fileprivate init(index: UInt32) {
23+
self.index = .init(index)
24+
}
2325
}
24-
}
25-
""")
26+
"""
27+
)
2628

2729
for node in NON_BASE_SYNTAX_NODES {
2830
if let layoutNode = node.layoutNode {
2931
try! ExtensionDeclSyntax(
3032
"\(node.apiAttributes())extension ConcreteSyntaxProperty where Base == \(node.kind.syntaxType)"
3133
) {
3234
for (index, child) in layoutNode.children.enumerated() {
33-
let childType: TypeSyntax = child.kind.isNodeChoicesEmpty
35+
let childType: TypeSyntax =
36+
child.kind.isNodeChoicesEmpty
3437
? child.syntaxNodeKind.syntaxType
3538
: "\(node.kind.syntaxType).\(child.syntaxChoicesType)"
3639
let type = child.isOptional ? TypeSyntax("\(childType)?") : TypeSyntax("\(childType)")
37-
DeclSyntax("""
38-
public static var \(child.varDeclName): ConcreteSyntaxProperty<\(node.kind.syntaxType), \(type)> {
39-
.init(index: \(raw: index))
40-
}
41-
""")
40+
DeclSyntax(
41+
"""
42+
public static var \(child.varDeclName): ConcreteSyntaxProperty<\(node.kind.syntaxType), \(type)> {
43+
.init(index: \(raw: index))
44+
}
45+
"""
46+
)
4247
}
4348
}
4449
}

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

+19-15
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,29 @@ import SyntaxSupport
1616
import Utils
1717

1818
let syntaxLayoutNodesFile = try! SourceFileSyntax(leadingTrivia: copyrightHeader) {
19-
DeclSyntax("""
20-
extension SyntaxLayout {
21-
fileprivate init(kind: SyntaxKind, count: UInt32) {
22-
self.baseKind = kind
23-
self._count = count
19+
DeclSyntax(
20+
"""
21+
extension SyntaxLayout {
22+
fileprivate init(syntaxKind: SyntaxKind, count: UInt32) {
23+
self.syntaxKind = syntaxKind
24+
self._count = count
25+
}
2426
}
25-
}
26-
""")
27-
27+
"""
28+
)
29+
2830
for node in NON_BASE_SYNTAX_NODES {
2931
if let layoutNode = node.layoutNode {
3032
try! ExtensionDeclSyntax(
3133
"extension \(node.kind.syntaxType): _LayoutSyntaxProtocol"
3234
) {
33-
DeclSyntax("""
34-
public static var layout: SyntaxLayout<Self> {
35-
SyntaxLayout<Self>(kind: .\(node.enumCaseCallName), count: \(literal: layoutNode.children.count))
36-
}
37-
""")
35+
DeclSyntax(
36+
"""
37+
public static var layout: SyntaxLayout<Self> {
38+
SyntaxLayout<Self>(syntaxKind: .\(node.enumCaseCallName), count: \(literal: layoutNode.children.count))
39+
}
40+
"""
41+
)
3842
}
3943
}
4044
}
@@ -43,12 +47,12 @@ let syntaxLayoutNodesFile = try! SourceFileSyntax(leadingTrivia: copyrightHeader
4347
try VariableDeclSyntax(
4448
"""
4549
/// Property name if this is a valid property.
46-
/// 'nil' if the `baseKind` is not a layout syntax, or the index is out of range.
50+
/// 'nil' if the `syntaxKind` is not a layout syntax, or the index is out of range.
4751
@_spi(RawSyntax)
4852
public var name: String?
4953
"""
5054
) {
51-
try SwitchExprSyntax("switch (self.baseKind, self.index.value)") {
55+
try SwitchExprSyntax("switch (self.syntaxKind, self.index.value)") {
5256
for node in NON_BASE_SYNTAX_NODES {
5357
if let layoutNode = node.layoutNode {
5458
for (index, child) in layoutNode.children.enumerated() {

Sources/SwiftBasicFormat/BasicFormat.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ open class BasicFormat: SyntaxRewriter {
240240
}
241241
return token.ancestorOrSelf(mapping: {
242242
switch $0.propertyInParent {
243-
case
244-
CatchItemSyntax.layout[.pattern],
243+
case CatchItemSyntax.layout[.pattern],
245244
ConditionElementSyntax.layout[.condition],
246245
ExpressionPatternSyntax.layout[.expression],
247246
ForStmtSyntax.layout[.sequence],

Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ nonisolated(unsafe) private var readabilityHandler: () -> Void = {
263263
)
264264
}
265265

266-
@_expose(wasm, "swift_wasm_macro_v1_pump")
266+
@_expose(wasm,"swift_wasm_macro_v1_pump")
267267
@_cdecl("swift_wasm_macro_v1_pump")
268268
func wasmPump() {
269269
readabilityHandler()

Sources/SwiftIDEUtils/SyntaxClassifier.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ private struct ClassificationVisitor {
202202
for case (let index, let child?) in children.enumerated() {
203203

204204
let classification: (classification: SyntaxClassification, force: Bool)?
205-
let property = SyntaxLayoutProperty(baseKind: child.kind, index: .init(UInt32(index)))
205+
let property = SyntaxLayoutProperty(syntaxKind: child.kind, index: .init(UInt32(index)))
206206
classification = SyntaxClassification.classify(property)
207207

208208
if let classification, classification.force {

Sources/SwiftSyntax/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ add_swift_syntax_library(SwiftSyntax
5858
generated/raw/RawSyntaxNodesTUVWXYZ.swift
5959
generated/raw/RawSyntaxValidation.swift
6060

61-
generated/ChildNameForKeyPath.swift
6261
generated/ConcreteSyntaxProperty.swift
6362
generated/Keyword.swift
6463
generated/RenamedChildrenCompatibility.swift
@@ -68,6 +67,7 @@ add_swift_syntax_library(SwiftSyntax
6867
generated/SyntaxCollections.swift
6968
generated/SyntaxEnum.swift
7069
generated/SyntaxKind.swift
70+
generated/SyntaxLayoutNodes.swift
7171
generated/SyntaxRewriter.swift
7272
generated/SyntaxTraits.swift
7373
generated/SyntaxVisitor.swift

Sources/SwiftSyntax/SyntaxLayout.swift

+65-33
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
/// Represent an index in a layout.
1414
public struct SyntaxLayoutIndex: Equatable, Comparable {
1515
let value: UInt32
16+
17+
@_spi(RawSyntax)
1618
public init(_ value: UInt32) {
1719
self.value = value
1820
}
21+
1922
public static func == (lhs: Self, rhs: Self) -> Bool {
2023
lhs.value == rhs.value
2124
}
25+
2226
public static func < (lhs: Self, rhs: Self) -> Bool {
2327
lhs.value < rhs.value
2428
}
@@ -27,68 +31,80 @@ public struct SyntaxLayoutIndex: Equatable, Comparable {
2731
/// Represent a property in a layout syntax.
2832
public struct SyntaxLayoutProperty: Equatable {
2933
/// 'SyntaxKind' of the parent layout node.
30-
let baseKind: SyntaxKind
34+
let syntaxKind: SyntaxKind
3135
/// Index in the parent node layout.
3236
let index: SyntaxLayoutIndex
3337

34-
public init(baseKind: SyntaxKind, index: SyntaxLayoutIndex) {
35-
self.baseKind = baseKind
38+
@_spi(RawSyntax)
39+
public init(syntaxKind: SyntaxKind, index: SyntaxLayoutIndex) {
40+
self.syntaxKind = syntaxKind
3641
self.index = index
3742
}
3843

3944
public static func == (lhs: Self, rhs: Self) -> Bool {
40-
lhs.baseKind == rhs.baseKind && lhs.index == rhs.index
45+
lhs.syntaxKind == rhs.syntaxKind && lhs.index == rhs.index
4146
}
4247
}
4348

49+
/// Protocol for concrete layout syntax nodes.
50+
public protocol _LayoutSyntaxProtocol: SyntaxProtocol {
51+
static var layout: SyntaxLayout<Self> { get }
52+
}
53+
4454
/// Describes the layout of a `_LayoutSyntaxProtocol` syntax node.
4555
public struct SyntaxLayout<Base: _LayoutSyntaxProtocol> {
46-
let baseKind: SyntaxKind
56+
let syntaxKind: SyntaxKind
4757
let _count: UInt32
4858

4959
// SyntaxLayout can only be constructed by `_LayoutSyntaxProtocol.layout`
5060
private init() { fatalError() }
5161
}
5262

53-
#if false // Not needed at this point.
54-
extension SyntaxLayout: Collection {
55-
public typealias Index = SyntaxLayoutIndex
56-
public typealias Element = SyntaxLayoutProperty
57-
58-
public var count: Int { Int(_count) }
59-
public var startIndex: Index { Index(0) }
60-
public var endIndex: Index { Index(_count) }
61-
public func index(after i: Index) -> Index { Index(i.value + 1) }
62-
63-
public subscript(position: SyntaxLayoutIndex) -> SyntaxLayoutProperty {
64-
return SyntaxLayoutProperty(baseKind: baseKind, index: position)
65-
}
66-
}
67-
#endif
68-
69-
public protocol _LayoutSyntaxProtocol: SyntaxProtocol {
70-
static var layout: SyntaxLayout<Self> { get }
71-
}
72-
73-
public struct ConcreteSyntaxProperty<Base: SyntaxProtocol, Value> {
63+
/// Represent a typed property in a layout syntax.
64+
///
65+
/// Specialized types have static properties to instantiate this type. E.g.:
66+
///
67+
/// let exprProperty: ConcreteSyntaxProperty<ReturnStmtSyntax, ExprSyntax?> = .expression
68+
///
69+
/// The property can be used similar to 'KeyPath'.
70+
///
71+
/// let expr/*: ExprSyntax?*/ = returnStmt[property: exprProperty]
72+
/// let newReturnStmt = returnStmt.with(.expression, expr)
73+
///
74+
public struct ConcreteSyntaxProperty<Base: _LayoutSyntaxProtocol, Value> {
7475
let index: SyntaxLayoutIndex
7576

76-
// ConcreteSyntaxProperty can only be constructed by
77-
// `ConcreteSyntaxProperty<Base, Value>.${name}`
77+
// ConcreteSyntaxProperty can only be constructed by `ConcreteSyntaxProperty<Base, Value>.${name}`
7878
private init() { fatalError() }
7979
}
8080

8181
extension SyntaxLayout {
82-
/// Get `AnySyntaxProperty` from `ConcreteSyntaxProperty` of the `Base` type.
82+
/// Get `AnySyntaxLayoutProperty` from `ConcreteSyntaxProperty` of the `Base` type.
8383
/// This is convenient for comparing '.propertyInParent'. E.g.
8484
///
8585
/// if node.propertyInParent == FunctionDeclSyntax.layout[.name] { ... }
8686
///
8787
public subscript<T>(property: ConcreteSyntaxProperty<Base, T>) -> SyntaxLayoutProperty {
88-
return SyntaxLayoutProperty(baseKind: baseKind, index: property.index)
88+
return SyntaxLayoutProperty(syntaxKind: syntaxKind, index: property.index)
8989
}
9090
}
9191

92+
#if false // Not needed at this point.
93+
extension SyntaxLayout: Collection {
94+
public typealias Index = SyntaxLayoutIndex
95+
public typealias Element = SyntaxLayoutProperty
96+
97+
public var count: Int { Int(_count) }
98+
public var startIndex: Index { Index(0) }
99+
public var endIndex: Index { Index(_count) }
100+
public func index(after i: Index) -> Index { Index(i.value + 1) }
101+
102+
public subscript(position: SyntaxLayoutIndex) -> SyntaxLayoutProperty {
103+
return SyntaxLayoutProperty(syntaxKind: syntaxKind, index: position)
104+
}
105+
}
106+
#endif
107+
92108
extension SyntaxProtocol {
93109
/// Return 'SyntaxLayoutProperty' in the parent node, if this node is a child
94110
/// of a layout node. 'nil' otherwise.
@@ -101,13 +117,29 @@ extension SyntaxProtocol {
101117
return nil
102118
}
103119
return SyntaxLayoutProperty(
104-
baseKind: parent.kind,
120+
syntaxKind: parent.kind,
105121
index: .init(self._syntaxNode.absoluteInfo.indexInParent)
106122
)
107123
}
124+
}
125+
126+
extension SyntaxProtocol {
127+
/// Get a property value.
128+
/// The property must be retrieved from the correct 'SyntaxLayout'
129+
public subscript(property: SyntaxLayoutProperty) -> Syntax? {
130+
precondition(property.syntaxKind == Syntax(self).kind)
131+
return Syntax(self).child(at: Int(property.index.value))
132+
}
133+
134+
/// Get a property value.
135+
public subscript<T: SyntaxProtocol>(property: ConcreteSyntaxProperty<Self, T>) -> T {
136+
return Syntax(self).child(at: Int(property.index.value))!.cast(T.self)
137+
}
138+
public subscript<T: SyntaxProtocol>(property: ConcreteSyntaxProperty<Self, T?>) -> T? {
139+
return Syntax(self).child(at: Int(property.index.value))?.cast(T.self)
140+
}
108141

109-
/// Returns a new syntax node that has the child at `property` replaced by
110-
/// `value`.
142+
/// Returns a new syntax node that has the child at `property` replaced by `value`.
111143
public func with<T: SyntaxProtocol>(_ property: ConcreteSyntaxProperty<Self, T>, _ value: T) -> Self {
112144
Syntax(self)
113145
.replacingChild(at: Int(property.index.value), with: Syntax(value), arena: SyntaxArena())

0 commit comments

Comments
 (0)