-
Notifications
You must be signed in to change notification settings - Fork 441
Move raw syntax nodes from gyb to code gen #1326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kimdv
merged 2 commits into
swiftlang:main
from
kimdv:kimdv/move-raw-syntax-nodes-from-gyb-to-code-gen
Feb 9, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
250 changes: 250 additions & 0 deletions
250
CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/RawSyntaxNodesFile.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SyntaxSupport | ||
import Utils | ||
|
||
let rawSyntaxNodesFile = SourceFileSyntax(leadingTrivia: "\(generateCopyrightHeader(for: "generate-swiftsyntax"))" + .newline) { | ||
for node in SYNTAX_NODES where node.isBase { | ||
DeclSyntax( | ||
""" | ||
@_spi(RawSyntax) | ||
public protocol Raw\(raw: node.name)NodeProtocol: Raw\(raw: node.baseType.syntaxKind)NodeProtocol {} | ||
""" | ||
) | ||
} | ||
|
||
for node in SYNTAX_NODES { | ||
try! StructDeclSyntax( | ||
""" | ||
@_spi(RawSyntax) | ||
public struct Raw\(raw: node.name): Raw\(raw: (node.isBase ? node.name : node.baseType.syntaxBaseName))NodeProtocol | ||
""" | ||
) { | ||
let enums: [(String, [(swiftName: String, typeName: String)])] = | ||
node.children.compactMap { child -> (String, [(String, String)])? in | ||
switch child.kind { | ||
case .nodeChoices(let choices): | ||
return (child.name, choices.map { ($0.swiftName, $0.typeName) }) | ||
default: | ||
return nil | ||
} | ||
} | ||
+ (node.collectionElementChoices?.isEmpty == false | ||
? [("Element", node.collectionElementChoices!.map { choice -> (String, String) in (SYNTAX_NODE_MAP[choice]!.swiftSyntaxKind, SYNTAX_NODE_MAP[choice]!.name) })] | ||
: []) | ||
|
||
for (name, choices) in enums { | ||
try EnumDeclSyntax( | ||
""" | ||
@frozen // FIXME: Not actually stable, works around a miscompile | ||
public enum \(raw: name): RawSyntaxNodeProtocol | ||
""" | ||
) { | ||
for (swiftName, typeName) in choices { | ||
DeclSyntax("case `\(raw: swiftName)`(Raw\(raw: typeName))") | ||
} | ||
|
||
DeclSyntax( | ||
""" | ||
public static func isKindOf(_ raw: RawSyntax) -> Bool { | ||
return \(raw: choices.map { "Raw\($0.typeName).isKindOf(raw)" }.joined(separator: " || ")) | ||
} | ||
""" | ||
) | ||
|
||
try VariableDeclSyntax("public var raw: RawSyntax") { | ||
try SwitchExprSyntax("switch self") { | ||
for (swiftName, _) in choices { | ||
SwitchCaseSyntax("case .\(raw: swiftName)(let node): return node.raw") | ||
} | ||
} | ||
} | ||
|
||
try InitializerDeclSyntax("public init?<T>(_ other: T) where T : RawSyntaxNodeProtocol") { | ||
for (swiftName, typeName) in choices { | ||
StmtSyntax( | ||
""" | ||
if let node = Raw\(raw: typeName)(other) { | ||
self = .\(raw: swiftName)(node) | ||
return | ||
} | ||
""" | ||
) | ||
} | ||
|
||
StmtSyntax("return nil") | ||
} | ||
} | ||
} | ||
|
||
DeclSyntax( | ||
""" | ||
@_spi(RawSyntax) | ||
public var layoutView: RawSyntaxLayoutView { | ||
return raw.layoutView! | ||
} | ||
""" | ||
) | ||
|
||
try FunctionDeclSyntax("public static func isKindOf(_ raw: RawSyntax) -> Bool") { | ||
if node.isBase { | ||
|
||
let cases = CaseItemListSyntax { | ||
for n in SYNTAX_NODES where n.baseKind == node.syntaxKind { | ||
CaseItemSyntax( | ||
pattern: ExpressionPatternSyntax( | ||
expression: ExprSyntax(".\(raw: n.swiftSyntaxKind)") | ||
) | ||
) | ||
} | ||
} | ||
|
||
ExprSyntax( | ||
""" | ||
switch raw.kind { | ||
case \(cases): return true | ||
default: return false | ||
} | ||
""" | ||
) | ||
} else { | ||
StmtSyntax("return raw.kind == .\(raw: node.swiftSyntaxKind)") | ||
} | ||
} | ||
|
||
DeclSyntax("public var raw: RawSyntax") | ||
|
||
DeclSyntax( | ||
""" | ||
init(raw: RawSyntax) { | ||
assert(Self.isKindOf(raw)) | ||
self.raw = raw | ||
} | ||
""" | ||
) | ||
|
||
DeclSyntax( | ||
""" | ||
public init?<Node: RawSyntaxNodeProtocol>(_ other: Node) { | ||
guard Self.isKindOf(other.raw) else { return nil } | ||
self.init(raw: other.raw) | ||
} | ||
""" | ||
) | ||
|
||
if node.isBase { | ||
DeclSyntax( | ||
""" | ||
public init<Node: Raw\(raw: node.name)NodeProtocol>(_ other: Node) { | ||
self.init(raw: other.raw) | ||
} | ||
""" | ||
) | ||
} | ||
|
||
if node.isSyntaxCollection { | ||
let element = node.collectionElementChoices?.isEmpty == false ? "Element" : "Raw\(node.collectionElementType.syntaxBaseName)" | ||
DeclSyntax( | ||
""" | ||
public init(elements: [\(raw: element)], arena: __shared SyntaxArena) { | ||
let raw = RawSyntax.makeLayout( | ||
kind: .\(raw: node.swiftSyntaxKind), uninitializedCount: elements.count, arena: arena) { layout in | ||
guard var ptr = layout.baseAddress else { return } | ||
for elem in elements { | ||
ptr.initialize(to: elem.raw) | ||
ptr += 1 | ||
} | ||
} | ||
self.init(raw: raw) | ||
} | ||
""" | ||
) | ||
|
||
DeclSyntax( | ||
""" | ||
public var elements: [Raw\(raw: node.collectionElementType.syntaxBaseName)] { | ||
layoutView.children.map { Raw\(raw: node.collectionElementType.syntaxBaseName)(raw: $0!) } | ||
} | ||
""" | ||
) | ||
} | ||
|
||
if node.isBuildable || node.isMissing { | ||
let params = FunctionParameterListSyntax { | ||
for child in node.children { | ||
FunctionParameterSyntax( | ||
firstName: child.isUnexpectedNodes ? .wildcardToken(trailingTrivia: .space) : nil, | ||
secondName: .identifier(child.swiftName), | ||
colon: .colonToken(), | ||
type: child.rawParameterType, | ||
defaultArgument: child.isUnexpectedNodes ? child.defaultInitialization.map { InitializerClauseSyntax(value: $0) } : nil | ||
) | ||
} | ||
|
||
FunctionParameterSyntax("arena: __shared SyntaxArena", for: .functionParameters) | ||
} | ||
try InitializerDeclSyntax("public init(\(params))") { | ||
if !node.children.isEmpty { | ||
let list = ExprListSyntax { | ||
ExprSyntax("layout.initialize(repeating: nil)") | ||
for (index, child) in node.children.enumerated() { | ||
let optionalMark = child.isOptional ? "?" : "" | ||
ExprSyntax("layout[\(raw: index)] = \(raw: child.swiftName)\(raw: optionalMark).raw") | ||
.with(\.leadingTrivia, .newline) | ||
} | ||
} | ||
|
||
DeclSyntax( | ||
""" | ||
let raw = RawSyntax.makeLayout( | ||
kind: .\(raw: node.swiftSyntaxKind), uninitializedCount: \(raw: node.children.count), arena: arena) { layout in | ||
\(list) | ||
} | ||
""" | ||
) | ||
} else { | ||
DeclSyntax("let raw = RawSyntax.makeEmptyLayout(kind: .\(raw: node.swiftSyntaxKind), arena: arena)") | ||
} | ||
ExprSyntax("self.init(raw: raw)") | ||
} | ||
|
||
for (index, child) in node.children.enumerated() { | ||
try VariableDeclSyntax("public var \(raw: child.swiftName): Raw\(raw: child.type.buildable)") { | ||
let iuoMark = child.isOptional ? "" : "!" | ||
|
||
if child.typeName == "Syntax" { | ||
ExprSyntax("layoutView.children[\(raw: index)]\(raw: iuoMark)") | ||
} else { | ||
ExprSyntax("layoutView.children[\(raw: index)].map(Raw\(raw: child.typeName).init(raw:))\(raw: iuoMark)") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fileprivate extension Child { | ||
var rawParameterType: TypeSyntax { | ||
let paramType: String | ||
if case ChildKind.nodeChoices = kind { | ||
paramType = name | ||
} else { | ||
paramType = "Raw\(typeName)" | ||
} | ||
|
||
return type.optionalWrapped(type: SimpleTypeIdentifierSyntax(name: .identifier(paramType))) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if we had something like
ChildKind
forNode
as well so we don’t need to force-unwrap here. But I think that’s something we can more easily do once we’ve gotten rid of all thegyb
. In general, I feel like there’s a lot we can clean up in the SyntaxSupport module once we’re no longer tied to the Python design.