[SIR, FIR generators] Add KDocs for visitor-related methods of elements
This commit is contained in:
committed by
Space Team
parent
1bed6a063f
commit
cab87eba21
@@ -40,6 +40,11 @@ interface FirElement {
|
|||||||
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
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs the provided [visitor] on the FIR subtree with the root at this node.
|
||||||
|
*
|
||||||
|
* @param visitor The visitor to accept.
|
||||||
|
*/
|
||||||
fun accept(visitor: FirVisitorVoid) = accept(visitor, null)
|
fun accept(visitor: FirVisitorVoid) = accept(visitor, null)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -54,6 +59,15 @@ interface FirElement {
|
|||||||
*/
|
*/
|
||||||
fun <R, D> acceptChildren(visitor: FirVisitor<R, D>, data: D)
|
fun <R, D> acceptChildren(visitor: FirVisitor<R, D>, data: D)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs the provided [visitor] on subtrees with roots in this node's children.
|
||||||
|
*
|
||||||
|
* Basically, calls `accept(visitor)` on each child of this node.
|
||||||
|
*
|
||||||
|
* Does **not** run [visitor] on this node itself.
|
||||||
|
*
|
||||||
|
* @param visitor The visitor for children to accept.
|
||||||
|
*/
|
||||||
fun acceptChildren(visitor: FirVisitorVoid) = acceptChildren(visitor, null)
|
fun acceptChildren(visitor: FirVisitorVoid) = acceptChildren(visitor, null)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+4
-3
@@ -21,14 +21,15 @@ internal class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<El
|
|||||||
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) {
|
||||||
printAcceptMethod(element, firVisitorType, hasImplementation = true, treeName = "FIR")
|
val treeName = "FIR"
|
||||||
|
printAcceptMethod(element, firVisitorType, hasImplementation = true, treeName = treeName)
|
||||||
|
|
||||||
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)),
|
||||||
treeName = "FIR",
|
treeName = treeName,
|
||||||
)
|
)
|
||||||
|
|
||||||
fun Field.replaceDeclaration(override: Boolean, overridenType: TypeRefWithNullability? = null, forceNullable: Boolean = false) {
|
fun Field.replaceDeclaration(override: Boolean, overridenType: TypeRefWithNullability? = null, forceNullable: Boolean = false) {
|
||||||
@@ -66,7 +67,7 @@ internal class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<El
|
|||||||
|
|
||||||
if (element.isRootElement) {
|
if (element.isRootElement) {
|
||||||
println()
|
println()
|
||||||
printAcceptVoidMethod(firVisitorVoidType)
|
printAcceptVoidMethod(firVisitorVoidType, treeName)
|
||||||
printAcceptChildrenMethod(
|
printAcceptChildrenMethod(
|
||||||
element = element,
|
element = element,
|
||||||
visitorClass = firVisitorType,
|
visitorClass = firVisitorType,
|
||||||
|
|||||||
+59
-23
@@ -199,6 +199,32 @@ inline fun SmartPrinter.printBlock(body: () -> Unit) {
|
|||||||
private val dataTP = TypeVariable("D")
|
private val dataTP = TypeVariable("D")
|
||||||
private val dataParameter = FunctionParameter("data", dataTP)
|
private val dataParameter = FunctionParameter("data", dataTP)
|
||||||
|
|
||||||
|
private fun acceptMethodKDoc(
|
||||||
|
visitorParameter: FunctionParameter,
|
||||||
|
dataParameter: FunctionParameter?,
|
||||||
|
returnType: TypeRef,
|
||||||
|
treeName: String,
|
||||||
|
) = buildString {
|
||||||
|
append("Runs the provided [")
|
||||||
|
append(visitorParameter.name)
|
||||||
|
append("] on the ")
|
||||||
|
append(treeName)
|
||||||
|
append(" subtree with the root at this node.\n\n")
|
||||||
|
append("@param ")
|
||||||
|
append(visitorParameter.name)
|
||||||
|
append(" The visitor to accept.")
|
||||||
|
if (dataParameter != null) {
|
||||||
|
append("\n@param ")
|
||||||
|
append(dataParameter.name)
|
||||||
|
append(" An arbitrary context to pass to each invocation of [")
|
||||||
|
append(visitorParameter.name)
|
||||||
|
append("]'s methods.")
|
||||||
|
}
|
||||||
|
if (returnType != StandardTypes.unit) {
|
||||||
|
append("\n@return The value returned by the topmost `visit*` invocation.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
context(ImportCollector)
|
context(ImportCollector)
|
||||||
fun SmartPrinter.printAcceptMethod(
|
fun SmartPrinter.printAcceptMethod(
|
||||||
element: AbstractElement<*, *, *>,
|
element: AbstractElement<*, *, *>,
|
||||||
@@ -211,15 +237,7 @@ fun SmartPrinter.printAcceptMethod(
|
|||||||
val resultTP = TypeVariable("R")
|
val resultTP = TypeVariable("R")
|
||||||
val visitorParameter = FunctionParameter("visitor", visitorClass.withArgs(resultTP, dataTP))
|
val visitorParameter = FunctionParameter("visitor", visitorClass.withArgs(resultTP, dataTP))
|
||||||
if (element.isRootElement) {
|
if (element.isRootElement) {
|
||||||
printKDoc(
|
printKDoc(acceptMethodKDoc(visitorParameter, dataParameter, resultTP, treeName))
|
||||||
"""
|
|
||||||
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",
|
||||||
@@ -291,6 +309,32 @@ fun SmartPrinter.printTransformMethod(
|
|||||||
println()
|
println()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun acceptChildrenKDoc(visitorParameter: FunctionParameter, dataParameter: FunctionParameter?) = buildString {
|
||||||
|
append("Runs the provided [")
|
||||||
|
append(visitorParameter.name)
|
||||||
|
append("] on subtrees with roots in this node's children.\n\n")
|
||||||
|
append("Basically, calls `accept(")
|
||||||
|
append(visitorParameter.name)
|
||||||
|
if (dataParameter != null) {
|
||||||
|
append(", ")
|
||||||
|
append(dataParameter.name)
|
||||||
|
}
|
||||||
|
append(")` on each child of this node.\n\n")
|
||||||
|
append("Does **not** run [")
|
||||||
|
append(visitorParameter.name)
|
||||||
|
append("] on this node itself.\n\n")
|
||||||
|
append("@param ")
|
||||||
|
append(visitorParameter.name)
|
||||||
|
append(" The visitor for children to accept.")
|
||||||
|
if (dataParameter != null) {
|
||||||
|
append("\n@param ")
|
||||||
|
append(dataParameter.name)
|
||||||
|
append(" An arbitrary context to pass to each invocation of [")
|
||||||
|
append(visitorParameter.name)
|
||||||
|
append("]'s methods.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
context(ImportCollector)
|
context(ImportCollector)
|
||||||
fun SmartPrinter.printAcceptChildrenMethod(
|
fun SmartPrinter.printAcceptChildrenMethod(
|
||||||
element: FieldContainer<*>,
|
element: FieldContainer<*>,
|
||||||
@@ -303,18 +347,7 @@ fun SmartPrinter.printAcceptChildrenMethod(
|
|||||||
println()
|
println()
|
||||||
val visitorParameter = FunctionParameter("visitor", visitorClass.withArgs(visitorResultType, dataTP))
|
val visitorParameter = FunctionParameter("visitor", visitorClass.withArgs(visitorResultType, dataTP))
|
||||||
if (!override) {
|
if (!override) {
|
||||||
printKDoc(
|
printKDoc(acceptChildrenKDoc(visitorParameter, dataParameter))
|
||||||
"""
|
|
||||||
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",
|
||||||
@@ -381,15 +414,18 @@ fun SmartPrinter.printTransformChildrenMethod(
|
|||||||
}
|
}
|
||||||
|
|
||||||
context(ImportCollector)
|
context(ImportCollector)
|
||||||
fun SmartPrinter.printAcceptVoidMethod(visitorType: ClassRef<*>) {
|
fun SmartPrinter.printAcceptVoidMethod(visitorType: ClassRef<*>, treeName: String) {
|
||||||
val visitorParameter = FunctionParameter("visitor", visitorType)
|
val visitorParameter = FunctionParameter("visitor", visitorType)
|
||||||
printFunctionDeclaration("accept", listOf(visitorParameter), StandardTypes.unit)
|
val returnType = StandardTypes.unit
|
||||||
|
printKDoc(acceptMethodKDoc(visitorParameter, null, returnType, treeName))
|
||||||
|
printFunctionDeclaration("accept", listOf(visitorParameter), returnType)
|
||||||
println(" = accept(", visitorParameter.name, ", null)")
|
println(" = accept(", visitorParameter.name, ", null)")
|
||||||
}
|
}
|
||||||
|
|
||||||
context(ImportCollector)
|
context(ImportCollector)
|
||||||
fun SmartPrinter.printAcceptChildrenVoidMethod(visitorType: ClassRef<*>) {
|
fun SmartPrinter.printAcceptChildrenVoidMethod(visitorType: ClassRef<*>) {
|
||||||
val visitorParameter = FunctionParameter("visitor", visitorType)
|
val visitorParameter = FunctionParameter("visitor", visitorType)
|
||||||
|
printKDoc(acceptChildrenKDoc(visitorParameter, null))
|
||||||
printFunctionDeclaration("acceptChildren", listOf(visitorParameter), StandardTypes.unit)
|
printFunctionDeclaration("acceptChildren", listOf(visitorParameter), StandardTypes.unit)
|
||||||
println(" = acceptChildren(", visitorParameter.name, ", null)")
|
println(" = acceptChildren(", visitorParameter.name, ", null)")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,11 @@ sealed interface SirElement {
|
|||||||
fun <E : SirElement, D> transform(transformer: SirTransformer<D>, data: D): E =
|
fun <E : SirElement, D> transform(transformer: SirTransformer<D>, data: D): E =
|
||||||
transformer.transformElement(this, data) as E
|
transformer.transformElement(this, data) as E
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs the provided [visitor] on the Swift IR subtree with the root at this node.
|
||||||
|
*
|
||||||
|
* @param visitor The visitor to accept.
|
||||||
|
*/
|
||||||
fun accept(visitor: SirVisitorVoid) = accept(visitor, null)
|
fun accept(visitor: SirVisitorVoid) = accept(visitor, null)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -53,6 +58,15 @@ sealed interface SirElement {
|
|||||||
*/
|
*/
|
||||||
fun <R, D> acceptChildren(visitor: SirVisitor<R, D>, data: D)
|
fun <R, D> acceptChildren(visitor: SirVisitor<R, D>, data: D)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs the provided [visitor] on subtrees with roots in this node's children.
|
||||||
|
*
|
||||||
|
* Basically, calls `accept(visitor)` on each child of this node.
|
||||||
|
*
|
||||||
|
* Does **not** run [visitor] on this node itself.
|
||||||
|
*
|
||||||
|
* @param visitor The visitor for children to accept.
|
||||||
|
*/
|
||||||
fun acceptChildren(visitor: SirVisitorVoid) = acceptChildren(visitor, null)
|
fun acceptChildren(visitor: SirVisitorVoid) = acceptChildren(visitor, null)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+1
-1
@@ -34,7 +34,7 @@ internal class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<El
|
|||||||
|
|
||||||
if (element.isRootElement) {
|
if (element.isRootElement) {
|
||||||
println()
|
println()
|
||||||
printAcceptVoidMethod(elementVisitorVoidType)
|
printAcceptVoidMethod(elementVisitorVoidType, treeName)
|
||||||
printAcceptChildrenMethod(element, elementVisitorType, TypeVariable("R"))
|
printAcceptChildrenMethod(element, elementVisitorType, TypeVariable("R"))
|
||||||
println()
|
println()
|
||||||
println()
|
println()
|
||||||
|
|||||||
Reference in New Issue
Block a user