Skip to content

Move gyb files to codegen #1136

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
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public class Node {
return syntaxKind.contains("Token") || collectionElement.contains("Token")
}

public var isVisitable: Bool {
return !isBase
}

init(name: String,
nameForDiagnostics: String?,
description: String? = nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ struct GenerateSwiftSyntax: ParsableCommand {
try generateTemplates(
templates: [
(miscFile, "Misc.swift"),
(syntaxAnyVisitorFile, "SyntaxAnyVisitor.swift"),
(syntaxBaseNodesFile, "SyntaxBaseNodes.swift"),
],
destination: URL(fileURLWithPath: generatedPath),
verbose: verbose
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//===----------------------------------------------------------------------===//
//
// 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 syntaxAnyVisitorFile = SourceFile {
ClassDecl("""
\(generateCopyrightHeader(for: "generate-swiftsyntax"))
/// A `SyntaxVisitor` that can visit the nodes as generic `Syntax` values.
///
/// This subclass of `SyntaxVisitor` is slower than the type-specific visitation
/// of `SyntaxVisitor`. Use `SyntaxAnyVisitor` if the `visitAny(_)` function
/// would be useful to have, otherwise inherit from `SyntaxVisitor`.
///
/// This works by overriding the type-specific visit function that delegate to
/// `visitAny(_)`. A subclass that provides a custom type-specific visit
/// function, should also call `visitAny(_)` in its implementation, if calling
/// `visitAny` is needed:
///
/// struct MyVisitor: SyntaxAnyVisitor {
/// func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
/// <code>
/// }
///
/// func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
/// <code>
/// // Call this to pass tokens to `visitAny(_)` as well if needed
/// visitAny(token)
/// }
open class SyntaxAnyVisitor: SyntaxVisitor
""") {
FunctionDecl("""
open func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
return .visitChildren
}
""")

FunctionDecl("""
/// The function called after visiting the node and its descendents.
/// - node: the node we just finished visiting.
open func visitAnyPost(_ node: Syntax) {}
""")

FunctionDecl("""
// MARK: Override type specific visit methods
override open func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
return visitAny(token._syntaxNode)
}
""")

FunctionDecl("""
override open func visitPost(_ node: TokenSyntax) {
visitAnyPost(node._syntaxNode)
}
""")

for node in SYNTAX_NODES where node.isVisitable {
FunctionDecl("""
override open func visit(_ node: \(raw: node.name)) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}
""")

FunctionDecl("""
override open func visitPost(_ node: \(raw: node.name)) {
visitAnyPost(node._syntaxNode)
}
""")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
//===----------------------------------------------------------------------===//
//
// 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 syntaxBaseNodesFile = SourceFile(leadingTrivia: [.blockComment(generateCopyrightHeader(for: "generate-swiftsyntax"))]) {
for node in SYNTAX_NODES where node.isBase {
ProtocolDecl("""
// MARK: - \(raw: node.name)
/// Protocol to which all `\(raw: node.name)` nodes conform. Extension point to add
/// common methods to all `\(raw: node.name)` nodes.
/// DO NOT CONFORM TO THIS PROTOCOL YOURSELF!
public protocol \(raw: node.name)Protocol: \(raw: node.baseType.baseName)Protocol {}
""")

ExtensionDecl("public extension Syntax") {
FunctionDecl("""
/// Check whether the non-type erased version of this syntax node conforms to
/// \(raw: node.name)Protocol.
/// Note that this will incur an existential conversion.
func isProtocol(_: \(raw: node.name)Protocol.Protocol) -> Bool {
return self.asProtocol(\(raw: node.name)Protocol.self) != nil
}
""")

FunctionDecl("""
/// Return the non-type erased version of this syntax node if it conforms to
/// \(raw: node.name)Protocol. Otherwise return nil.
/// Note that this will incur an existential conversion.
func asProtocol(_: \(raw: node.name)Protocol.Protocol) -> \(raw: node.name)Protocol? {
return self.asProtocol(SyntaxProtocol.self) as? \(raw: node.name)Protocol
}
""")
}

StructDecl("""
\(node.description ?? "")
public struct \(node.name): \(node.name)Protocol, SyntaxHashable
""") {
VariableDecl("public let _syntaxNode: Syntax")

InitializerDecl("""
/// Create a `\(raw: node.name)` node from a specialized syntax node.
public init<S: \(raw: node.name)Protocol>(_ syntax: S) {
// We know this cast is going to succeed. Go through init(_: SyntaxData)
// to do a sanity check and verify the kind matches in debug builds and get
// maximum performance in release builds.
self.init(syntax._syntaxNode.data)
}
""")

InitializerDecl("""
/// Create a `\(raw: node.name)` node from a specialized optional syntax node.
public init?<S: \(raw: node.name)Protocol>(_ syntax: S?) {
guard let syntax = syntax else { return nil }
self.init(syntax)
}
""")

InitializerDecl("""
public init(fromProtocol syntax: \(raw: node.name)Protocol) {
// We know this cast is going to succeed. Go through init(_: SyntaxData)
// to do a sanity check and verify the kind matches in debug builds and get
// maximum performance in release builds.
self.init(syntax._syntaxNode.data)
}
""")

InitializerDecl("""
/// Create a `\(raw: node.name)` node from a specialized optional syntax node.
public init?(fromProtocol syntax: \(raw: node.name)Protocol?) {
guard let syntax = syntax else { return nil }
self.init(fromProtocol: syntax)
}
""")

InitializerDecl("public init?<S: SyntaxProtocol>(_ node: S)") {
SwitchStmt(expression: MemberAccessExpr("node.raw.kind")) {
SwitchCaseList {
SwitchCase(
label: .case(SwitchCaseLabel {
for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind {
CaseItem(
pattern: EnumCasePattern(
period: .period,
caseName: .identifier(childNode.swiftSyntaxKind))
)
}
})) {
Expr("self._syntaxNode = node._syntaxNode")
}

SwitchCase("default:") {
ReturnStmt("return nil")
}
}
}
}

InitializerDecl("""
/// Creates a `\(node.name)` node from the given `SyntaxData`. This assumes
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
/// is undefined.
internal init(_ data: SyntaxData)
""") {
IfConfigDecl(
clauses: IfConfigClauseList {
IfConfigClause(
poundKeyword: .poundIfKeyword(),
condition: Expr("DEBUG"),
elements: IfConfigClauseSyntax.Elements.statements(CodeBlockItemList {
SwitchStmt(
expression: Expr("data.raw.kind")) {
SwitchCase(
label: .case(SwitchCaseLabel {
for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind {
CaseItem(
pattern: EnumCasePatternSyntax(
period: .period,
caseName: .identifier(childNode.swiftSyntaxKind))
)
}
})) {
BreakStmt()
}

SwitchCase("default:") {
FunctionCallExpr("fatalError(\"Unable to create \(raw: node.name) from \\(data.raw.kind)\")")
}
}
})
)
}
)

Expr("self._syntaxNode = Syntax(data)")
}

FunctionDecl("""
public func `is`<S: \(raw: node.name)Protocol>(_ syntaxType: S.Type) -> Bool {
return self.as(syntaxType) != nil
}
""")

FunctionDecl("""
public func `as`<S: \(raw: node.name)Protocol>(_ syntaxType: S.Type) -> S? {
return S.init(self)
}
""")

FunctionDecl("""
public func cast<S: \(raw: node.name)Protocol>(_ syntaxType: S.Type) -> S {
return self.as(S.self)!
}
""")

FunctionDecl("""
/// Syntax nodes always conform to `\(raw: node.name)Protocol`. This API is just
/// added for consistency.
/// Note that this will incur an existential conversion.
@available(*, deprecated, message: "Expression always evaluates to true")
public func isProtocol(_: \(raw: node.name)Protocol.Protocol) -> Bool {
return true
}
""")

FunctionDecl("""
/// Return the non-type erased version of this syntax node.
/// Note that this will incur an existential conversion.
public func asProtocol(_: \(raw: node.name)Protocol.Protocol) -> \(raw: node.name)Protocol {
return Syntax(self).asProtocol(\(raw: node.name)Protocol.self)!
}
""")


VariableDecl(
modifiers: [DeclModifier(name: .public), DeclModifier(name: .static)],
name: IdentifierPattern("structure"),
type: TypeAnnotation(
type: TypeSyntax("SyntaxNodeStructure"))
) {
ReturnStmt(
expression: FunctionCallExpr(
callee: MemberAccessExpr(".choices")) {
TupleExprElement(
expression: ArrayExprSyntax {
for childNode in SYNTAX_NODES where childNode.baseKind == node.syntaxKind {
ArrayElement(
expression: FunctionCallExpr("\n.node(\(raw: childNode.name).self)")
)
}
})
}
)
}

FunctionDecl("""
public func childNameForDiagnostics(_ index: SyntaxChildrenIndex) -> String? {
return Syntax(self).childNameForDiagnostics(index)
}
""")
}

ExtensionDecl("""
extension \(raw: node.name): CustomReflectable {
/// Reconstructs the real syntax type for this type from the node's kind and
/// provides a mirror that reflects this type.
public var customMirror: Mirror {
return Mirror(reflecting: Syntax(self).asProtocol(SyntaxProtocol.self))
}
}
""")

}
}
2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ let package = Package(
"CMakeLists.txt",
"Raw/RawSyntaxNodes.swift.gyb",
"Raw/RawSyntaxValidation.swift.gyb",
"SyntaxAnyVisitor.swift.gyb",
"SyntaxBaseNodes.swift.gyb",
"SyntaxCollections.swift.gyb",
"SyntaxEnum.swift.gyb",
"SyntaxFactory.swift.gyb",
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftSyntax/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ add_swift_host_library(SwiftSyntax
Raw/gyb_generated/RawSyntaxValidation.swift

generated/Misc.swift
gyb_generated/SyntaxAnyVisitor.swift
gyb_generated/SyntaxBaseNodes.swift
generated/SyntaxAnyVisitor.swift
generated/SyntaxBaseNodes.swift
gyb_generated/SyntaxCollections.swift
gyb_generated/SyntaxEnum.swift
gyb_generated/SyntaxFactory.swift
Expand Down
Loading