[FIR generator] Write kDocs for accept & transform methods in FirElement
This commit is contained in:
committed by
Space Team
parent
ed28923282
commit
02c12ae26f
@@ -19,18 +19,53 @@ import org.jetbrains.kotlin.fir.visitors.FirVisitorVoid
|
|||||||
interface FirElement {
|
interface FirElement {
|
||||||
val source: KtSourceElement?
|
val source: KtSourceElement?
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs the provided [visitor] on the FIR subtree with the root at this node.
|
||||||
|
*
|
||||||
|
* @param visitor The visitor to accept.
|
||||||
|
* @param data An arbitrary context to pass to each invocation of [visitor]'s methods.
|
||||||
|
* @return The value returned by the topmost `visit*` invocation.
|
||||||
|
*/
|
||||||
fun <R, D> accept(visitor: FirVisitor<R, D>, data: D): R =
|
fun <R, D> accept(visitor: FirVisitor<R, D>, data: D): R =
|
||||||
visitor.visitElement(this, data)
|
visitor.visitElement(this, data)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs the provided [transformer] on the FIR subtree with the root at this node.
|
||||||
|
*
|
||||||
|
* @param transformer The transformer to use.
|
||||||
|
* @param data An arbitrary context to pass to each invocation of [transformer]'s methods.
|
||||||
|
* @return The transformed node.
|
||||||
|
*/
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
fun <E : FirElement, D> transform(transformer: FirTransformer<D>, data: D): E =
|
fun <E : FirElement, D> transform(transformer: FirTransformer<D>, data: D): E =
|
||||||
transformer.transformElement(this, data) as E
|
transformer.transformElement(this, data) as E
|
||||||
|
|
||||||
fun accept(visitor: FirVisitorVoid) = accept(visitor, null)
|
fun accept(visitor: FirVisitorVoid) = accept(visitor, null)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs the provided [visitor] on subtrees with roots in this node's children.
|
||||||
|
*
|
||||||
|
* Basically, calls `accept(visitor, data)` on each child of this node.
|
||||||
|
*
|
||||||
|
* Does **not** run [visitor] on this node itself.
|
||||||
|
*
|
||||||
|
* @param visitor The visitor for children to accept.
|
||||||
|
* @param data An arbitrary context to pass to each invocation of [visitor]'s methods.
|
||||||
|
*/
|
||||||
fun <R, D> acceptChildren(visitor: FirVisitor<R, D>, data: D)
|
fun <R, D> acceptChildren(visitor: FirVisitor<R, D>, data: D)
|
||||||
|
|
||||||
fun acceptChildren(visitor: FirVisitorVoid) = acceptChildren(visitor, null)
|
fun acceptChildren(visitor: FirVisitorVoid) = acceptChildren(visitor, null)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recursively transforms this node's children *in place* using [transformer].
|
||||||
|
*
|
||||||
|
* Basically, executes `this.child = this.child.transform(transformer, data)` for each child of this node.
|
||||||
|
*
|
||||||
|
* Does **not** run [transformer] on this node itself.
|
||||||
|
*
|
||||||
|
* @param transformer The transformer to use for transforming the children.
|
||||||
|
* @param data An arbitrary context to pass to each invocation of [transformer]'s methods.
|
||||||
|
* @return `this`
|
||||||
|
*/
|
||||||
fun <D> transformChildren(transformer: FirTransformer<D>, data: D): FirElement
|
fun <D> transformChildren(transformer: FirTransformer<D>, data: D): FirElement
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-8
@@ -23,16 +23,14 @@ private class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<Ele
|
|||||||
override fun SmartPrinter.printAdditionalMethods(element: Element) {
|
override fun SmartPrinter.printAdditionalMethods(element: Element) {
|
||||||
val kind = element.kind ?: error("Expected non-null element kind")
|
val kind = element.kind ?: error("Expected non-null element kind")
|
||||||
with(element) {
|
with(element) {
|
||||||
// TODO: Add a kDoc for `accept`
|
printAcceptMethod(element, firVisitorType, hasImplementation = true, treeName = "FIR")
|
||||||
printAcceptMethod(element, firVisitorType, hasImplementation = true, kDoc = null)
|
|
||||||
|
|
||||||
// TODO: Add a kDoc for `transform`
|
|
||||||
printTransformMethod(
|
printTransformMethod(
|
||||||
element = element,
|
element = element,
|
||||||
transformerClass = firTransformerType,
|
transformerClass = firTransformerType,
|
||||||
implementation = "transformer.transform${element.name}(this, data)",
|
implementation = "transformer.transform${element.name}(this, data)",
|
||||||
returnType = TypeVariable("E", listOf(AbstractFirTreeBuilder.baseFirElement)),
|
returnType = TypeVariable("E", listOf(AbstractFirTreeBuilder.baseFirElement)),
|
||||||
kDoc = null,
|
treeName = "FIR",
|
||||||
)
|
)
|
||||||
|
|
||||||
fun Field.replaceDeclaration(override: Boolean, overridenType: TypeRefWithNullability? = null, forceNullable: Boolean = false) {
|
fun Field.replaceDeclaration(override: Boolean, overridenType: TypeRefWithNullability? = null, forceNullable: Boolean = false) {
|
||||||
@@ -72,14 +70,20 @@ private class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<Ele
|
|||||||
println()
|
println()
|
||||||
println("fun accept(visitor: ", firVisitorVoidType.render(), ") = accept(visitor, null)")
|
println("fun accept(visitor: ", firVisitorVoidType.render(), ") = accept(visitor, null)")
|
||||||
|
|
||||||
// TODO: Add a kDoc for `acceptChildren`
|
printAcceptChildrenMethod(
|
||||||
printAcceptChildrenMethod(element, firVisitorType, visitorResultType = TypeVariable("R"), kDoc = null)
|
element = element,
|
||||||
|
visitorClass = firVisitorType,
|
||||||
|
visitorResultType = TypeVariable("R"),
|
||||||
|
)
|
||||||
println()
|
println()
|
||||||
println()
|
println()
|
||||||
println("fun acceptChildren(visitor: ", firVisitorVoidType.render(), ") = acceptChildren(visitor, null)")
|
println("fun acceptChildren(visitor: ", firVisitorVoidType.render(), ") = acceptChildren(visitor, null)")
|
||||||
|
|
||||||
// TODO: Add a kDoc for `transformChildren`
|
printTransformChildrenMethod(
|
||||||
printTransformChildrenMethod(element, firTransformerType, returnType = AbstractFirTreeBuilder.baseFirElement, kDoc = null)
|
element = element,
|
||||||
|
transformerClass = firTransformerType,
|
||||||
|
returnType = AbstractFirTreeBuilder.baseFirElement,
|
||||||
|
)
|
||||||
println()
|
println()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-2
@@ -152,7 +152,7 @@ fun SmartPrinter.printImplementation(implementation: Implementation) {
|
|||||||
fun Field.acceptString(): String = "${name}${call()}accept(visitor, data)"
|
fun Field.acceptString(): String = "${name}${call()}accept(visitor, data)"
|
||||||
|
|
||||||
if (hasAcceptChildrenMethod) {
|
if (hasAcceptChildrenMethod) {
|
||||||
printAcceptChildrenMethod(this, firVisitorType, TypeVariable("R"), override = true, kDoc = null)
|
printAcceptChildrenMethod(this, firVisitorType, TypeVariable("R"), override = true)
|
||||||
print(" {")
|
print(" {")
|
||||||
|
|
||||||
val walkableFields = walkableChildren
|
val walkableFields = walkableChildren
|
||||||
@@ -222,7 +222,6 @@ fun SmartPrinter.printImplementation(implementation: Implementation) {
|
|||||||
this,
|
this,
|
||||||
modality = Modality.ABSTRACT.takeIf { isAbstract },
|
modality = Modality.ABSTRACT.takeIf { isAbstract },
|
||||||
override = true,
|
override = true,
|
||||||
kDoc = null,
|
|
||||||
)
|
)
|
||||||
if (!isInterface && !isAbstract) {
|
if (!isInterface && !isAbstract) {
|
||||||
println(" {")
|
println(" {")
|
||||||
|
|||||||
+13
-49
@@ -36,36 +36,20 @@ private class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<Ele
|
|||||||
override fun SmartPrinter.printAdditionalMethods(element: Element) {
|
override fun SmartPrinter.printAdditionalMethods(element: Element) {
|
||||||
element.generationCallback?.invoke(this@ImportCollector, this)
|
element.generationCallback?.invoke(this@ImportCollector, this)
|
||||||
|
|
||||||
if (element.hasAcceptMethod) {
|
printAcceptMethod(
|
||||||
printAcceptMethod(
|
element = element,
|
||||||
element = element,
|
visitorClass = elementVisitorType,
|
||||||
visitorClass = elementVisitorType,
|
hasImplementation = !element.isRootElement,
|
||||||
hasImplementation = !element.isRootElement,
|
treeName = "IR",
|
||||||
kDoc = """
|
)
|
||||||
Runs the provided [visitor] on the IR subtree with the root at this node.
|
|
||||||
|
|
||||||
@param visitor The visitor to accept.
|
|
||||||
@param data An arbitrary context to pass to each invocation of [visitor]'s methods.
|
|
||||||
@return The value returned by the topmost `visit*` invocation.
|
|
||||||
""".trimIndent().takeIf { element.isRootElement }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (element.hasTransformMethod) {
|
printTransformMethod(
|
||||||
printTransformMethod(
|
element = element,
|
||||||
element = element,
|
transformerClass = elementTransformerType,
|
||||||
transformerClass = elementTransformerType,
|
implementation = "accept(transformer, data)".takeIf { !element.isRootElement },
|
||||||
implementation = "accept(transformer, data)".takeIf { !element.isRootElement },
|
returnType = element,
|
||||||
returnType = element,
|
treeName = "IR",
|
||||||
kDoc = """
|
)
|
||||||
Runs the provided [transformer] on the IR subtree with the root at this node.
|
|
||||||
|
|
||||||
@param transformer The transformer to use.
|
|
||||||
@param data An arbitrary context to pass to each invocation of [transformer]'s methods.
|
|
||||||
@return The transformed node.
|
|
||||||
""".trimIndent().takeIf { element.isRootElement }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (element.hasAcceptChildrenMethod) {
|
if (element.hasAcceptChildrenMethod) {
|
||||||
printAcceptChildrenMethod(
|
printAcceptChildrenMethod(
|
||||||
@@ -73,16 +57,6 @@ private class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<Ele
|
|||||||
visitorClass = elementVisitorType,
|
visitorClass = elementVisitorType,
|
||||||
visitorResultType = StandardTypes.unit,
|
visitorResultType = StandardTypes.unit,
|
||||||
override = !element.isRootElement,
|
override = !element.isRootElement,
|
||||||
kDoc = """
|
|
||||||
Runs the provided [visitor] on subtrees with roots in this node's children.
|
|
||||||
|
|
||||||
Basically, calls `accept(visitor, data)` on each child of this node.
|
|
||||||
|
|
||||||
Does **not** run [visitor] on this node itself.
|
|
||||||
|
|
||||||
@param visitor The visitor for children to accept.
|
|
||||||
@param data An arbitrary context to pass to each invocation of [visitor]'s methods.
|
|
||||||
""".trimIndent().takeIf { element.isRootElement }
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!element.isRootElement) {
|
if (!element.isRootElement) {
|
||||||
@@ -116,16 +90,6 @@ private class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<Ele
|
|||||||
transformerClass = elementTransformerType,
|
transformerClass = elementTransformerType,
|
||||||
returnType = StandardTypes.unit,
|
returnType = StandardTypes.unit,
|
||||||
override = !element.isRootElement,
|
override = !element.isRootElement,
|
||||||
kDoc = """
|
|
||||||
Recursively transforms this node's children *in place* using [transformer].
|
|
||||||
|
|
||||||
Basically, executes `this.child = this.child.transform(transformer, data)` for each child of this node.
|
|
||||||
|
|
||||||
Does **not** run [transformer] on this node itself.
|
|
||||||
|
|
||||||
@param transformer The transformer to use for transforming the children.
|
|
||||||
@param data An arbitrary context to pass to each invocation of [transformer]'s methods.
|
|
||||||
""".trimIndent().takeIf { element.isRootElement }
|
|
||||||
)
|
)
|
||||||
if (!element.isRootElement) {
|
if (!element.isRootElement) {
|
||||||
println(" {")
|
println(" {")
|
||||||
|
|||||||
+61
-25
@@ -156,18 +156,26 @@ fun SmartPrinter.printAcceptMethod(
|
|||||||
element: AbstractElement<*, *>,
|
element: AbstractElement<*, *>,
|
||||||
visitorClass: ClassRef<PositionTypeParameterRef>,
|
visitorClass: ClassRef<PositionTypeParameterRef>,
|
||||||
hasImplementation: Boolean,
|
hasImplementation: Boolean,
|
||||||
kDoc: String?,
|
treeName: String,
|
||||||
) {
|
) {
|
||||||
if (!element.hasAcceptMethod) return
|
if (!element.hasAcceptMethod) return
|
||||||
println()
|
println()
|
||||||
printKDoc(kDoc)
|
|
||||||
val resultTP = TypeVariable("R")
|
val resultTP = TypeVariable("R")
|
||||||
|
val visitorParameter = FunctionParameter("visitor", visitorClass.withArgs(resultTP, dataTP))
|
||||||
|
if (element.isRootElement) {
|
||||||
|
printKDoc(
|
||||||
|
"""
|
||||||
|
Runs the provided [${visitorParameter.name}] on the $treeName subtree with the root at this node.
|
||||||
|
|
||||||
|
@param ${visitorParameter.name} The visitor to accept.
|
||||||
|
@param ${dataParameter.name} An arbitrary context to pass to each invocation of [${visitorParameter.name}]'s methods.
|
||||||
|
@return The value returned by the topmost `visit*` invocation.
|
||||||
|
""".trimIndent()
|
||||||
|
)
|
||||||
|
}
|
||||||
printFunctionDeclaration(
|
printFunctionDeclaration(
|
||||||
name = "accept",
|
name = "accept",
|
||||||
parameters = listOf(
|
parameters = listOf(visitorParameter, dataParameter),
|
||||||
FunctionParameter("visitor", visitorClass.withArgs(resultTP, dataTP)),
|
|
||||||
dataParameter,
|
|
||||||
),
|
|
||||||
returnType = resultTP,
|
returnType = resultTP,
|
||||||
typeParameters = listOf(resultTP, dataTP),
|
typeParameters = listOf(resultTP, dataTP),
|
||||||
override = !element.isRootElement,
|
override = !element.isRootElement,
|
||||||
@@ -175,7 +183,7 @@ fun SmartPrinter.printAcceptMethod(
|
|||||||
if (hasImplementation) {
|
if (hasImplementation) {
|
||||||
println(" =")
|
println(" =")
|
||||||
withIndent {
|
withIndent {
|
||||||
print("visitor.", element.visitFunctionName, "(this, ", dataParameter.name, ")")
|
print(visitorParameter.name, ".", element.visitFunctionName, "(this, ", dataParameter.name, ")")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println()
|
println()
|
||||||
@@ -187,20 +195,28 @@ fun SmartPrinter.printTransformMethod(
|
|||||||
transformerClass: ClassRef<PositionTypeParameterRef>,
|
transformerClass: ClassRef<PositionTypeParameterRef>,
|
||||||
implementation: String?,
|
implementation: String?,
|
||||||
returnType: TypeRefWithNullability,
|
returnType: TypeRefWithNullability,
|
||||||
kDoc: String?,
|
treeName: String,
|
||||||
) {
|
) {
|
||||||
if (!element.hasTransformMethod) return
|
if (!element.hasTransformMethod) return
|
||||||
println()
|
println()
|
||||||
printKDoc(kDoc)
|
val transformerParameter = FunctionParameter("transformer", transformerClass.withArgs(dataTP))
|
||||||
|
if (element.isRootElement) {
|
||||||
|
printKDoc(
|
||||||
|
"""
|
||||||
|
Runs the provided [${transformerParameter.name}] on the $treeName subtree with the root at this node.
|
||||||
|
|
||||||
|
@param ${transformerParameter.name} The transformer to use.
|
||||||
|
@param ${dataParameter.name} An arbitrary context to pass to each invocation of [${transformerParameter.name}]'s methods.
|
||||||
|
@return The transformed node.
|
||||||
|
""".trimIndent()
|
||||||
|
)
|
||||||
|
}
|
||||||
if (returnType is TypeParameterRef && implementation != null) {
|
if (returnType is TypeParameterRef && implementation != null) {
|
||||||
println("@Suppress(\"UNCHECKED_CAST\")")
|
println("@Suppress(\"UNCHECKED_CAST\")")
|
||||||
}
|
}
|
||||||
printFunctionDeclaration(
|
printFunctionDeclaration(
|
||||||
name = "transform",
|
name = "transform",
|
||||||
parameters = listOf(
|
parameters = listOf(transformerParameter, dataParameter),
|
||||||
FunctionParameter("transformer", transformerClass.withArgs(dataTP)),
|
|
||||||
dataParameter,
|
|
||||||
),
|
|
||||||
returnType = returnType,
|
returnType = returnType,
|
||||||
typeParameters = listOfNotNull(returnType as? TypeVariable, dataTP),
|
typeParameters = listOfNotNull(returnType as? TypeVariable, dataTP),
|
||||||
override = !element.isRootElement,
|
override = !element.isRootElement,
|
||||||
@@ -221,17 +237,27 @@ fun SmartPrinter.printAcceptChildrenMethod(
|
|||||||
visitorResultType: TypeRef,
|
visitorResultType: TypeRef,
|
||||||
modality: Modality? = null,
|
modality: Modality? = null,
|
||||||
override: Boolean = false,
|
override: Boolean = false,
|
||||||
kDoc: String?,
|
|
||||||
) {
|
) {
|
||||||
if (!element.hasAcceptChildrenMethod) return
|
if (!element.hasAcceptChildrenMethod) return
|
||||||
println()
|
println()
|
||||||
printKDoc(kDoc)
|
val visitorParameter = FunctionParameter("visitor", visitorClass.withArgs(visitorResultType, dataTP))
|
||||||
|
if (!override) {
|
||||||
|
printKDoc(
|
||||||
|
"""
|
||||||
|
Runs the provided [${visitorParameter.name}] on subtrees with roots in this node's children.
|
||||||
|
|
||||||
|
Basically, calls `accept(${visitorParameter.name}, ${dataParameter.name})` on each child of this node.
|
||||||
|
|
||||||
|
Does **not** run [${visitorParameter.name}] on this node itself.
|
||||||
|
|
||||||
|
@param ${visitorParameter.name} The visitor for children to accept.
|
||||||
|
@param ${dataParameter.name} An arbitrary context to pass to each invocation of [${visitorParameter.name}]'s methods.
|
||||||
|
""".trimIndent()
|
||||||
|
)
|
||||||
|
}
|
||||||
printFunctionDeclaration(
|
printFunctionDeclaration(
|
||||||
name = "acceptChildren",
|
name = "acceptChildren",
|
||||||
parameters = listOf(
|
parameters = listOf(visitorParameter, dataParameter),
|
||||||
FunctionParameter("visitor", visitorClass.withArgs(visitorResultType, dataTP)),
|
|
||||||
dataParameter,
|
|
||||||
),
|
|
||||||
returnType = StandardTypes.unit,
|
returnType = StandardTypes.unit,
|
||||||
typeParameters = listOfNotNull(visitorResultType as? TypeVariable, dataTP),
|
typeParameters = listOfNotNull(visitorResultType as? TypeVariable, dataTP),
|
||||||
modality = modality,
|
modality = modality,
|
||||||
@@ -246,17 +272,27 @@ fun SmartPrinter.printTransformChildrenMethod(
|
|||||||
returnType: TypeRef,
|
returnType: TypeRef,
|
||||||
modality: Modality? = null,
|
modality: Modality? = null,
|
||||||
override: Boolean = false,
|
override: Boolean = false,
|
||||||
kDoc: String?,
|
|
||||||
) {
|
) {
|
||||||
if (!element.hasTransformChildrenMethod) return
|
if (!element.hasTransformChildrenMethod) return
|
||||||
println()
|
println()
|
||||||
printKDoc(kDoc)
|
val transformerParameter = FunctionParameter("transformer", transformerClass.withArgs(dataTP))
|
||||||
|
if (!override) {
|
||||||
|
printKDoc(
|
||||||
|
"""
|
||||||
|
Recursively transforms this node's children *in place* using [${transformerParameter.name}].
|
||||||
|
|
||||||
|
Basically, executes `this.child = this.child.transform(${transformerParameter.name}, ${dataParameter.name})` for each child of this node.
|
||||||
|
|
||||||
|
Does **not** run [${transformerParameter.name}] on this node itself.
|
||||||
|
|
||||||
|
@param ${transformerParameter.name} The transformer to use for transforming the children.
|
||||||
|
@param ${dataParameter.name} An arbitrary context to pass to each invocation of [${transformerParameter.name}]'s methods.
|
||||||
|
""".trimIndent() + (if (returnType == StandardTypes.unit) "" else "\n@return `this`")
|
||||||
|
)
|
||||||
|
}
|
||||||
printFunctionDeclaration(
|
printFunctionDeclaration(
|
||||||
name = "transformChildren",
|
name = "transformChildren",
|
||||||
parameters = listOf(
|
parameters = listOf(transformerParameter, dataParameter),
|
||||||
FunctionParameter("transformer", transformerClass.withArgs(dataTP)),
|
|
||||||
dataParameter,
|
|
||||||
),
|
|
||||||
returnType = returnType,
|
returnType = returnType,
|
||||||
typeParameters = listOf(dataTP),
|
typeParameters = listOf(dataTP),
|
||||||
modality = modality,
|
modality = modality,
|
||||||
|
|||||||
Reference in New Issue
Block a user