[SIR generator] Add SirVisitorVoid class

This is convenient for cases when the visitor has no context to pass to
each method
This commit is contained in:
Sergej Jaskiewicz
2023-12-11 18:14:07 +01:00
committed by Space Team
parent 80c9eae409
commit 80847b9ee2
9 changed files with 166 additions and 14 deletions
@@ -10,10 +10,7 @@ import org.jetbrains.kotlin.fir.tree.generator.context.AbstractFirTreeBuilder
import org.jetbrains.kotlin.fir.tree.generator.model.Element
import org.jetbrains.kotlin.fir.tree.generator.model.Field
import org.jetbrains.kotlin.generators.tree.*
import org.jetbrains.kotlin.generators.tree.printer.printAcceptChildrenMethod
import org.jetbrains.kotlin.generators.tree.printer.printAcceptMethod
import org.jetbrains.kotlin.generators.tree.printer.printTransformChildrenMethod
import org.jetbrains.kotlin.generators.tree.printer.printTransformMethod
import org.jetbrains.kotlin.generators.tree.printer.*
import org.jetbrains.kotlin.utils.SmartPrinter
internal class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<Element, Field>(printer) {
@@ -69,8 +66,7 @@ internal class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<El
if (element.isRootElement) {
println()
println("fun accept(visitor: ", firVisitorVoidType.render(), ") = accept(visitor, null)")
printAcceptVoidMethod(firVisitorVoidType)
printAcceptChildrenMethod(
element = element,
visitorClass = firVisitorType,
@@ -78,8 +74,7 @@ internal class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<El
)
println()
println()
println("fun acceptChildren(visitor: ", firVisitorVoidType.render(), ") = acceptChildren(visitor, null)")
printAcceptChildrenVoidMethod(firVisitorVoidType)
printTransformChildrenMethod(
element = element,
transformerClass = firTransformerType,
@@ -60,8 +60,8 @@ abstract class AbstractVisitorVoidPrinter<Element, Field>(
element,
hasDataParameter = false,
modality = when {
element.isRootElement && visitorType.kind == TypeKind.Class -> Modality.ABSTRACT
!element.isRootElement && visitorType.kind == TypeKind.Class -> Modality.OPEN
isAbstractVisitRootElementMethod && visitorType.kind == TypeKind.Class -> Modality.ABSTRACT
!isAbstractVisitRootElementMethod && visitorType.kind == TypeKind.Class -> Modality.OPEN
else -> null
}
)
@@ -348,4 +348,18 @@ fun SmartPrinter.printTransformChildrenMethod(
)
}
context(ImportCollector)
fun SmartPrinter.printAcceptVoidMethod(visitorType: ClassRef<*>) {
val visitorParameter = FunctionParameter("visitor", visitorType)
printFunctionDeclaration("accept", listOf(visitorParameter), StandardTypes.unit)
println(" = accept(", visitorParameter.name, ", null)")
}
context(ImportCollector)
fun SmartPrinter.printAcceptChildrenVoidMethod(visitorType: ClassRef<*>) {
val visitorParameter = FunctionParameter("visitor", visitorType)
printFunctionDeclaration("acceptChildren", listOf(visitorParameter), StandardTypes.unit)
println(" = acceptChildren(", visitorParameter.name, ", null)")
}
fun AbstractField<*>.call(): String = if (nullable) "?." else "."
@@ -10,6 +10,7 @@ package org.jetbrains.kotlin.sir
import org.jetbrains.kotlin.sir.visitors.SirTransformer
import org.jetbrains.kotlin.sir.visitors.SirVisitor
import org.jetbrains.kotlin.sir.visitors.SirVisitorVoid
/**
* The root interface of the Swift IR tree.
@@ -37,6 +38,8 @@ sealed interface SirElement {
fun <E : SirElement, D> transform(transformer: SirTransformer<D>, data: D): E =
transformer.transformElement(this, data) as E
fun accept(visitor: SirVisitorVoid) = accept(visitor, null)
/**
* Runs the provided [visitor] on subtrees with roots in this node's children.
*
@@ -49,6 +52,8 @@ sealed interface SirElement {
*/
fun <R, D> acceptChildren(visitor: SirVisitor<R, D>, data: D)
fun acceptChildren(visitor: SirVisitorVoid) = acceptChildren(visitor, null)
/**
* Recursively transforms this node's children *in place* using [transformer].
*
@@ -0,0 +1,104 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
// This file was generated automatically. See native/swift/sir/tree-generator/Readme.md.
// DO NOT MODIFY IT MANUALLY.
package org.jetbrains.kotlin.sir.visitors
import org.jetbrains.kotlin.sir.*
/**
* Auto-generated by [org.jetbrains.kotlin.sir.tree.generator.printer.VisitorVoidPrinter]
*/
abstract class SirVisitorVoid : SirVisitor<Unit, Nothing?>() {
final override fun visitElement(element: SirElement, data: Nothing?) {
visitElement(element)
}
open fun visitElement(element: SirElement) {
}
final override fun visitModule(module: SirModule, data: Nothing?) {
visitModule(module)
}
open fun visitModule(module: SirModule) {
visitElement(module)
}
final override fun visitDeclarationContainer(declarationContainer: SirDeclarationContainer, data: Nothing?) {
visitDeclarationContainer(declarationContainer)
}
open fun visitDeclarationContainer(declarationContainer: SirDeclarationContainer) {
visitElement(declarationContainer)
}
final override fun visitDeclaration(declaration: SirDeclaration, data: Nothing?) {
visitDeclaration(declaration)
}
open fun visitDeclaration(declaration: SirDeclaration) {
visitElement(declaration)
}
final override fun visitForeignDeclaration(foreignDeclaration: SirForeignDeclaration, data: Nothing?) {
visitForeignDeclaration(foreignDeclaration)
}
open fun visitForeignDeclaration(foreignDeclaration: SirForeignDeclaration) {
visitDeclaration(foreignDeclaration)
}
final override fun visitNamedDeclaration(namedDeclaration: SirNamedDeclaration, data: Nothing?) {
visitNamedDeclaration(namedDeclaration)
}
open fun visitNamedDeclaration(namedDeclaration: SirNamedDeclaration) {
visitDeclaration(namedDeclaration)
}
final override fun visitEnum(enum: SirEnum, data: Nothing?) {
visitEnum(enum)
}
open fun visitEnum(enum: SirEnum) {
visitNamedDeclaration(enum)
}
final override fun visitStruct(struct: SirStruct, data: Nothing?) {
visitStruct(struct)
}
open fun visitStruct(struct: SirStruct) {
visitNamedDeclaration(struct)
}
final override fun visitCallable(callable: SirCallable, data: Nothing?) {
visitCallable(callable)
}
open fun visitCallable(callable: SirCallable) {
visitDeclaration(callable)
}
final override fun visitFunction(function: SirFunction, data: Nothing?) {
visitFunction(function)
}
open fun visitFunction(function: SirFunction) {
visitCallable(function)
}
final override fun visitForeignFunction(foreignFunction: SirForeignFunction, data: Nothing?) {
visitForeignFunction(foreignFunction)
}
open fun visitForeignFunction(foreignFunction: SirForeignFunction) {
visitCallable(foreignFunction)
}
}
@@ -27,6 +27,7 @@ fun main(args: Array<String>) {
::ElementPrinter,
listOf(
elementVisitorType to ::VisitorPrinter,
elementVisitorVoidType to ::VisitorVoidPrinter,
elementTransformerType to ::TransformerPrinter.bind(model.rootElement),
),
ImplementationConfigurator,
@@ -18,6 +18,7 @@ val enumCaseType = type(BASE_PACKAGE, "SirEnumCase", TypeKind.Class)
private const val VISITORS_PACKAGE = "$BASE_PACKAGE.visitors"
val elementVisitorType = type(VISITORS_PACKAGE, "SirVisitor", TypeKind.Class)
val elementVisitorVoidType = type(VISITORS_PACKAGE, "SirVisitorVoid", TypeKind.Class)
val elementTransformerType = type(VISITORS_PACKAGE, "SirTransformer", TypeKind.Class)
val swiftIrImplementationDetailAnnotation = type(BASE_PACKAGE, "SirImplementationDetail", TypeKind.Class)
@@ -7,13 +7,12 @@ package org.jetbrains.kotlin.sir.tree.generator.printer
import org.jetbrains.kotlin.generators.tree.*
import org.jetbrains.kotlin.generators.tree.printer.*
import org.jetbrains.kotlin.sir.tree.generator.BASE_PACKAGE
import org.jetbrains.kotlin.sir.tree.generator.SwiftIrTree
import org.jetbrains.kotlin.sir.tree.generator.model.Element
import org.jetbrains.kotlin.sir.tree.generator.model.Field
import org.jetbrains.kotlin.sir.tree.generator.elementTransformerType
import org.jetbrains.kotlin.sir.tree.generator.elementVisitorType
import org.jetbrains.kotlin.sir.tree.generator.model.SimpleField
import org.jetbrains.kotlin.sir.tree.generator.elementVisitorVoidType
import org.jetbrains.kotlin.sir.tree.generator.model.Element
import org.jetbrains.kotlin.sir.tree.generator.model.Field
import org.jetbrains.kotlin.utils.SmartPrinter
internal class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<Element, Field>(printer) {
@@ -37,8 +36,12 @@ internal class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<El
}
if (element.isRootElement) {
println()
printAcceptVoidMethod(elementVisitorVoidType)
printAcceptChildrenMethod(element, elementVisitorType, TypeVariable("R"))
println()
println()
printAcceptChildrenVoidMethod(elementVisitorVoidType)
printTransformChildrenMethod(element, elementTransformerType, StandardTypes.unit)
println()
}
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.sir.tree.generator.printer
import org.jetbrains.kotlin.generators.tree.AbstractVisitorVoidPrinter
import org.jetbrains.kotlin.generators.tree.ClassRef
import org.jetbrains.kotlin.generators.tree.PositionTypeParameterRef
import org.jetbrains.kotlin.sir.tree.generator.elementVisitorType
import org.jetbrains.kotlin.sir.tree.generator.model.Element
import org.jetbrains.kotlin.sir.tree.generator.model.Field
import org.jetbrains.kotlin.utils.SmartPrinter
internal class VisitorVoidPrinter(
printer: SmartPrinter,
override val visitorType: ClassRef<*>,
) : AbstractVisitorVoidPrinter<Element, Field>(printer) {
override val visitorSuperClass: ClassRef<PositionTypeParameterRef>
get() = elementVisitorType
override val useAbstractMethodForRootElement: Boolean
get() = false
override val overriddenVisitMethodsAreFinal: Boolean
get() = true
}