[FIR/IR generator] Reduce duplication when printing curly-braced blocks

This commit is contained in:
Sergej Jaskiewicz
2023-11-10 17:15:29 +01:00
committed by Space Team
parent 18ed85c26e
commit fab63e38aa
9 changed files with 73 additions and 86 deletions
@@ -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)
@@ -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("}")
}
}
@@ -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("}")
}
}
}