[FIR/IR generator] Reduce duplication when printing curly-braced blocks
This commit is contained in:
committed by
Space Team
parent
18ed85c26e
commit
fab63e38aa
+3
-6
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.fir.tree.generator.model.*
|
||||
import org.jetbrains.kotlin.generators.tree.ImportCollector
|
||||
import org.jetbrains.kotlin.generators.tree.StandardTypes
|
||||
import org.jetbrains.kotlin.generators.tree.printer.GeneratedFile
|
||||
import org.jetbrains.kotlin.generators.tree.printer.printBlock
|
||||
import org.jetbrains.kotlin.generators.tree.printer.printGeneratedType
|
||||
import org.jetbrains.kotlin.generators.tree.render
|
||||
import org.jetbrains.kotlin.utils.SmartPrinter
|
||||
@@ -50,8 +51,7 @@ private fun SmartPrinter.printBuilder(builder: Builder) {
|
||||
print(builder.parents.joinToString(separator = ", ", prefix = " : ") { it.render() })
|
||||
}
|
||||
var hasRequiredFields = false
|
||||
println(" {")
|
||||
withIndent {
|
||||
printBlock {
|
||||
var needNewLine = false
|
||||
for (field in builder.allFields) {
|
||||
val (newLine, requiredFields) = printFieldInBuilder(field, builder, fieldIsUseless = false)
|
||||
@@ -74,8 +74,7 @@ private fun SmartPrinter.printBuilder(builder: Builder) {
|
||||
}
|
||||
print("fun build(): $buildType")
|
||||
if (builder is LeafBuilder) {
|
||||
println(" {")
|
||||
withIndent {
|
||||
printBlock {
|
||||
println("return ${builder.implementation.render()}(")
|
||||
withIndent {
|
||||
for (field in builder.allFields) {
|
||||
@@ -91,7 +90,6 @@ private fun SmartPrinter.printBuilder(builder: Builder) {
|
||||
}
|
||||
println(")")
|
||||
}
|
||||
println("}")
|
||||
if (hasBackingFields) {
|
||||
println()
|
||||
}
|
||||
@@ -116,7 +114,6 @@ private fun SmartPrinter.printBuilder(builder: Builder) {
|
||||
}
|
||||
}
|
||||
}
|
||||
println("}")
|
||||
if (builder is LeafBuilder) {
|
||||
println()
|
||||
printDslBuildFunction(builder, hasRequiredFields)
|
||||
|
||||
+9
-18
@@ -109,8 +109,7 @@ fun SmartPrinter.printImplementation(implementation: Implementation) {
|
||||
print("${pureAbstractElementType.render()}(), ")
|
||||
}
|
||||
print(allParents.joinToString { "${it.render()}${it.kind.braces()}" })
|
||||
println(" {")
|
||||
withIndent {
|
||||
printBlock {
|
||||
if (isInterface || isAbstract) {
|
||||
allFields.forEach {
|
||||
fieldPrinter.printField(it, override = true, modality = Modality.ABSTRACT.takeIf { isAbstract })
|
||||
@@ -217,15 +216,14 @@ fun SmartPrinter.printImplementation(implementation: Implementation) {
|
||||
|
||||
if (hasTransformChildrenMethod) {
|
||||
printTransformChildrenMethod(
|
||||
this,
|
||||
implementation,
|
||||
firTransformerType,
|
||||
this,
|
||||
implementation,
|
||||
modality = Modality.ABSTRACT.takeIf { isAbstract },
|
||||
override = true,
|
||||
)
|
||||
if (!isInterface && !isAbstract) {
|
||||
println(" {")
|
||||
withIndent {
|
||||
printBlock {
|
||||
for (field in transformableChildren) {
|
||||
when {
|
||||
field.name == "explicitReceiver" -> {
|
||||
@@ -275,24 +273,21 @@ fun SmartPrinter.printImplementation(implementation: Implementation) {
|
||||
}
|
||||
println("return this")
|
||||
}
|
||||
print("}")
|
||||
}
|
||||
println()
|
||||
}
|
||||
|
||||
for (field in allFields) {
|
||||
if (!field.needsSeparateTransform) continue
|
||||
println()
|
||||
transformFunctionDeclaration(field, this, override = true, kind!!)
|
||||
transformFunctionDeclaration(field, implementation, override = true, kind!!)
|
||||
if (isInterface || isAbstract) {
|
||||
println()
|
||||
continue
|
||||
}
|
||||
println(" {")
|
||||
withIndent {
|
||||
printBlock {
|
||||
if (field.isMutable && field.isFirType) {
|
||||
// TODO: replace with smth normal
|
||||
if (this.typeName == "FirWhenExpressionImpl" && field.name == "subject") {
|
||||
if (typeName == "FirWhenExpressionImpl" && field.name == "subject") {
|
||||
println(
|
||||
"""
|
||||
|if (subjectVariable != null) {
|
||||
@@ -309,17 +304,15 @@ fun SmartPrinter.printImplementation(implementation: Implementation) {
|
||||
}
|
||||
println("return this")
|
||||
}
|
||||
println("}")
|
||||
}
|
||||
|
||||
if (element.needTransformOtherChildren) {
|
||||
println()
|
||||
transformOtherChildrenFunctionDeclaration(this, override = true, kind!!)
|
||||
transformOtherChildrenFunctionDeclaration(implementation, override = true, kind!!)
|
||||
if (isInterface || isAbstract) {
|
||||
println()
|
||||
} else {
|
||||
println(" {")
|
||||
withIndent {
|
||||
printBlock {
|
||||
for (field in allFields) {
|
||||
if (!field.isMutable || !field.isFirType || field.name == "subjectVariable") continue
|
||||
if (!field.needsSeparateTransform) {
|
||||
@@ -331,7 +324,6 @@ fun SmartPrinter.printImplementation(implementation: Implementation) {
|
||||
}
|
||||
println("return this")
|
||||
}
|
||||
println("}")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -396,7 +388,6 @@ fun SmartPrinter.printImplementation(implementation: Implementation) {
|
||||
}
|
||||
}
|
||||
}
|
||||
println("}")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-14
@@ -12,13 +12,8 @@ import org.jetbrains.kotlin.fir.tree.generator.firVisitorType
|
||||
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.FunctionParameter
|
||||
import org.jetbrains.kotlin.generators.tree.printer.GeneratedFile
|
||||
import org.jetbrains.kotlin.generators.tree.printer.printFunctionDeclaration
|
||||
import org.jetbrains.kotlin.generators.tree.printer.printGeneratedType
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
import org.jetbrains.kotlin.generators.tree.printer.*
|
||||
import org.jetbrains.kotlin.utils.SmartPrinter
|
||||
import org.jetbrains.kotlin.utils.withIndent
|
||||
import java.io.File
|
||||
|
||||
private class TransformerPrinter(
|
||||
@@ -61,7 +56,7 @@ private class TransformerPrinter(
|
||||
)
|
||||
println()
|
||||
} else {
|
||||
printFunctionDeclaration(
|
||||
printFunctionWithBlockBody(
|
||||
name = "transform" + element.name,
|
||||
parameters = listOf(
|
||||
FunctionParameter(elementParameterName, element),
|
||||
@@ -70,12 +65,9 @@ private class TransformerPrinter(
|
||||
returnType = visitMethodReturnType(element),
|
||||
typeParameters = element.params,
|
||||
modality = Modality.OPEN,
|
||||
)
|
||||
println(" {")
|
||||
withIndent {
|
||||
) {
|
||||
println("return transformElement(", elementParameterName, ", data)")
|
||||
}
|
||||
println("}")
|
||||
}
|
||||
println()
|
||||
printVisitMethodDeclaration(
|
||||
@@ -83,8 +75,7 @@ private class TransformerPrinter(
|
||||
modality = Modality.FINAL,
|
||||
override = true,
|
||||
)
|
||||
println(" {")
|
||||
withIndent {
|
||||
printBlock {
|
||||
println(
|
||||
"return transform",
|
||||
element.name,
|
||||
@@ -94,7 +85,6 @@ private class TransformerPrinter(
|
||||
"data)"
|
||||
)
|
||||
}
|
||||
println("}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.descriptors.ValueClassRepresentation
|
||||
import org.jetbrains.kotlin.generators.tree.*
|
||||
import org.jetbrains.kotlin.generators.tree.printer.FunctionParameter
|
||||
import org.jetbrains.kotlin.generators.tree.printer.printFunctionDeclaration
|
||||
import org.jetbrains.kotlin.generators.tree.printer.printFunctionWithBlockBody
|
||||
import org.jetbrains.kotlin.ir.generator.config.AbstractTreeBuilder
|
||||
import org.jetbrains.kotlin.ir.generator.config.ElementConfig
|
||||
import org.jetbrains.kotlin.ir.generator.config.ElementConfig.Category.*
|
||||
@@ -776,12 +777,9 @@ object IrTree : AbstractTreeBuilder() {
|
||||
vararg statements: String,
|
||||
) {
|
||||
println()
|
||||
printFunctionDeclaration(name, listOf(indexParam) + listOfNotNull(additionalParameter), returnType)
|
||||
println(" {")
|
||||
withIndent {
|
||||
printFunctionWithBlockBody(name, listOf(indexParam) + listOfNotNull(additionalParameter), returnType) {
|
||||
statements.forEach { println(it) }
|
||||
}
|
||||
println("}")
|
||||
}
|
||||
|
||||
printFunction(
|
||||
|
||||
+6
-8
@@ -60,8 +60,7 @@ private class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<Ele
|
||||
)
|
||||
|
||||
if (!element.isRootElement) {
|
||||
println(" {")
|
||||
withIndent {
|
||||
printBlock {
|
||||
for (child in element.walkableChildren) {
|
||||
print(child.name)
|
||||
if (child.nullable) {
|
||||
@@ -79,9 +78,9 @@ private class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<Ele
|
||||
}
|
||||
}
|
||||
}
|
||||
print("}")
|
||||
} else {
|
||||
println()
|
||||
}
|
||||
println()
|
||||
}
|
||||
|
||||
if (element.hasTransformChildrenMethod) {
|
||||
@@ -92,8 +91,7 @@ private class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<Ele
|
||||
override = !element.isRootElement,
|
||||
)
|
||||
if (!element.isRootElement) {
|
||||
println(" {")
|
||||
withIndent {
|
||||
printBlock {
|
||||
for (child in element.transformableChildren) {
|
||||
print(child.name)
|
||||
when (child) {
|
||||
@@ -125,9 +123,9 @@ private class ElementPrinter(printer: SmartPrinter) : AbstractElementPrinter<Ele
|
||||
}
|
||||
}
|
||||
}
|
||||
print("}")
|
||||
} else {
|
||||
println()
|
||||
}
|
||||
println()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
-28
@@ -7,10 +7,7 @@ package org.jetbrains.kotlin.ir.generator.print
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.generators.tree.*
|
||||
import org.jetbrains.kotlin.generators.tree.printer.FunctionParameter
|
||||
import org.jetbrains.kotlin.generators.tree.printer.GeneratedFile
|
||||
import org.jetbrains.kotlin.generators.tree.printer.printFunctionDeclaration
|
||||
import org.jetbrains.kotlin.generators.tree.printer.printGeneratedType
|
||||
import org.jetbrains.kotlin.generators.tree.printer.*
|
||||
import org.jetbrains.kotlin.ir.generator.*
|
||||
import org.jetbrains.kotlin.ir.generator.model.*
|
||||
import org.jetbrains.kotlin.util.capitalizeDecapitalize.capitalizeAsciiOnly
|
||||
@@ -100,12 +97,10 @@ private class TransformerPrinter(
|
||||
override = true,
|
||||
)
|
||||
if (element.transformByChildren) {
|
||||
println(" {")
|
||||
withIndent {
|
||||
printBlock {
|
||||
println(element.visitorParameterName, ".transformChildren(this, data)")
|
||||
println("return ", element.visitorParameterName)
|
||||
}
|
||||
println("}")
|
||||
} else {
|
||||
println(" =")
|
||||
withIndent {
|
||||
@@ -162,7 +157,7 @@ private class TransformerVoidPrinter(
|
||||
override fun SmartPrinter.printAdditionalMethods() {
|
||||
println()
|
||||
val typeParameter = TypeVariable("T", listOf(IrTree.rootElement))
|
||||
printFunctionDeclaration(
|
||||
printFunctionWithBlockBody(
|
||||
name = "transformPostfix",
|
||||
parameters = listOf(FunctionParameter("body", Lambda(receiver = typeParameter, returnType = StandardTypes.unit))),
|
||||
returnType = typeParameter,
|
||||
@@ -170,27 +165,21 @@ private class TransformerVoidPrinter(
|
||||
extensionReceiver = typeParameter,
|
||||
visibility = Visibility.PROTECTED,
|
||||
isInline = true,
|
||||
)
|
||||
println(" {")
|
||||
withIndent {
|
||||
) {
|
||||
println("transformChildrenVoid()")
|
||||
println("this.body()")
|
||||
println("return this")
|
||||
}
|
||||
println("}")
|
||||
println()
|
||||
printFunctionDeclaration(
|
||||
printFunctionWithBlockBody(
|
||||
name = "transformChildrenVoid",
|
||||
parameters = emptyList(),
|
||||
returnType = StandardTypes.unit,
|
||||
extensionReceiver = IrTree.rootElement,
|
||||
visibility = Visibility.PROTECTED,
|
||||
)
|
||||
println(" {")
|
||||
withIndent {
|
||||
) {
|
||||
println("transformChildrenVoid(this@", visitorType.simpleName, ")")
|
||||
}
|
||||
println("}")
|
||||
}
|
||||
|
||||
context(ImportCollector)
|
||||
@@ -201,12 +190,10 @@ private class TransformerVoidPrinter(
|
||||
println()
|
||||
printVisitMethodDeclaration(element, hasDataParameter = false, modality = Modality.OPEN)
|
||||
if (element.transformByChildrenVoid && !element.isPackageFragmentChild) {
|
||||
println(" {")
|
||||
withIndent {
|
||||
printBlock {
|
||||
println(element.visitorParameterName, ".transformChildren(this, null)")
|
||||
println("return ", element.visitorParameterName)
|
||||
}
|
||||
println("}")
|
||||
} else {
|
||||
println(" =")
|
||||
withIndent {
|
||||
@@ -242,17 +229,14 @@ fun printTransformerVoid(generationPath: File, model: Model): GeneratedFile =
|
||||
TransformerVoidPrinter(this, elementTransformerVoidType).printVisitor(model.elements)
|
||||
println()
|
||||
val transformerParameter = FunctionParameter("transformer", elementTransformerVoidType)
|
||||
printFunctionDeclaration(
|
||||
printFunctionWithBlockBody(
|
||||
name = "transformChildrenVoid",
|
||||
parameters = listOf(transformerParameter),
|
||||
returnType = StandardTypes.unit,
|
||||
extensionReceiver = IrTree.rootElement,
|
||||
)
|
||||
println(" {")
|
||||
withIndent {
|
||||
) {
|
||||
println("transformChildren(", transformerParameter.name, ", null)")
|
||||
}
|
||||
println("}")
|
||||
}
|
||||
|
||||
private class TypeTransformerPrinter(
|
||||
@@ -340,8 +324,7 @@ private class TypeTransformerPrinter(
|
||||
}
|
||||
}
|
||||
|
||||
println(" {")
|
||||
withIndent {
|
||||
printBlock {
|
||||
when (element.name) {
|
||||
IrTree.memberAccessExpression.name -> {
|
||||
if (irTypeFields.singleOrNull()?.name != "typeArguments") {
|
||||
@@ -385,7 +368,6 @@ private class TypeTransformerPrinter(
|
||||
", data)"
|
||||
)
|
||||
}
|
||||
println("}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-3
@@ -146,8 +146,7 @@ abstract class AbstractVisitorPrinter<Element : AbstractElement<Element, Field>,
|
||||
print(" : ", it.render(), it.inheritanceClauseParenthesis())
|
||||
}
|
||||
print(visitorTypeParameters.multipleUpperBoundsList())
|
||||
println(" {")
|
||||
withIndent {
|
||||
printBlock {
|
||||
printAdditionalMethods()
|
||||
for (element in elements) {
|
||||
if (element.isRootElement && visitSuperTypeByDefault) continue
|
||||
@@ -155,7 +154,6 @@ abstract class AbstractVisitorPrinter<Element : AbstractElement<Element, Field>,
|
||||
printMethodsForElement(element)
|
||||
}
|
||||
}
|
||||
println("}")
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-5
@@ -6,8 +6,8 @@
|
||||
package org.jetbrains.kotlin.generators.tree
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.generators.tree.printer.printBlock
|
||||
import org.jetbrains.kotlin.utils.SmartPrinter
|
||||
import org.jetbrains.kotlin.utils.withIndent
|
||||
|
||||
abstract class AbstractVisitorVoidPrinter<Element, Field>(
|
||||
printer: SmartPrinter,
|
||||
@@ -50,13 +50,11 @@ abstract class AbstractVisitorVoidPrinter<Element, Field>(
|
||||
)
|
||||
|
||||
fun SmartPrinter.printBody(parentInVisitor: Element?) {
|
||||
println(" {")
|
||||
if (parentInVisitor != null) {
|
||||
withIndent {
|
||||
printBlock {
|
||||
if (parentInVisitor != null) {
|
||||
println(parentInVisitor.visitFunctionName, "(", element.visitorParameterName, ")")
|
||||
}
|
||||
}
|
||||
println("}")
|
||||
}
|
||||
|
||||
printer.run {
|
||||
|
||||
+35
@@ -161,6 +161,41 @@ fun SmartPrinter.printFunctionDeclaration(
|
||||
print(typeParameters.multipleUpperBoundsList())
|
||||
}
|
||||
|
||||
context(ImportCollector)
|
||||
inline fun SmartPrinter.printFunctionWithBlockBody(
|
||||
name: String,
|
||||
parameters: List<FunctionParameter>,
|
||||
returnType: TypeRef,
|
||||
typeParameters: List<TypeVariable> = emptyList(),
|
||||
extensionReceiver: TypeRef? = null,
|
||||
visibility: Visibility = Visibility.PUBLIC,
|
||||
modality: Modality? = null,
|
||||
override: Boolean = false,
|
||||
isInline: Boolean = false,
|
||||
allParametersOnSeparateLines: Boolean = false,
|
||||
blockBody: () -> Unit,
|
||||
) {
|
||||
printFunctionDeclaration(
|
||||
name,
|
||||
parameters,
|
||||
returnType,
|
||||
typeParameters,
|
||||
extensionReceiver,
|
||||
visibility,
|
||||
modality,
|
||||
override,
|
||||
isInline,
|
||||
allParametersOnSeparateLines,
|
||||
)
|
||||
printBlock(blockBody)
|
||||
}
|
||||
|
||||
inline fun SmartPrinter.printBlock(body: () -> Unit) {
|
||||
println(" {")
|
||||
withIndent(body)
|
||||
println("}")
|
||||
}
|
||||
|
||||
private val dataTP = TypeVariable("D")
|
||||
private val dataParameter = FunctionParameter("data", dataTP)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user