From 5d7a5245fff7301c693cd5a0ded6458908f238bd Mon Sep 17 00:00:00 2001 From: Wojciech Litewka Date: Wed, 14 Feb 2024 10:41:01 +0100 Subject: [PATCH] [tree generator] Extract printPropertyDeclaration util analogous to printFunctionDeclaration. This decouples the printing logic from handling IR elements' fields, and makes it easier to directly print arbitrary fields. --- .../generators/tree/AbstractFieldPrinter.kt | 98 ++++++------------- .../generators/tree/printer/printUtils.kt | 74 ++++++++++++++ 2 files changed, 105 insertions(+), 67 deletions(-) diff --git a/generators/tree-generator-common/src/org/jetbrains/kotlin/generators/tree/AbstractFieldPrinter.kt b/generators/tree-generator-common/src/org/jetbrains/kotlin/generators/tree/AbstractFieldPrinter.kt index 401f9a3aad8..93239eb0913 100644 --- a/generators/tree-generator-common/src/org/jetbrains/kotlin/generators/tree/AbstractFieldPrinter.kt +++ b/generators/tree-generator-common/src/org/jetbrains/kotlin/generators/tree/AbstractFieldPrinter.kt @@ -6,10 +6,14 @@ package org.jetbrains.kotlin.generators.tree import org.jetbrains.kotlin.descriptors.Modality +import org.jetbrains.kotlin.generators.tree.printer.VariableKind +import org.jetbrains.kotlin.generators.tree.printer.printBlock import org.jetbrains.kotlin.generators.tree.printer.printKDoc +import org.jetbrains.kotlin.generators.tree.printer.printPropertyDeclaration import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly import org.jetbrains.kotlin.utils.SmartPrinter import org.jetbrains.kotlin.utils.withIndent +import java.lang.reflect.Modifier.isVolatile abstract class AbstractFieldPrinter>( private val printer: SmartPrinter, @@ -34,78 +38,38 @@ abstract class AbstractFieldPrinter>( modality: Modality? = null, ) { printer.run { - if (!field.fromParent) { - printKDoc(field.kDoc) - } - - field.deprecation?.let { - println("@Deprecated(") - withIndent { - println("message = \"", it.message, "\",") - println("replaceWith = ReplaceWith(\"", it.replaceWith.expression, "\"),") - println("level = DeprecationLevel.", it.level.name, ",") - } - println(")") - } - - if (field.isVolatile) { - println("@", type().render()) - } - val defaultValue = field.defaultValueInImplementation + printPropertyDeclaration( + name = field.name, + type = actualTypeOfField(field), + kind = if (forceMutable(field) || field.isFinal && field.isMutable) VariableKind.VAR else VariableKind.VAL, + inConstructor = inConstructor, + visibility = field.visibility, + modality = modality, + override = override, + isLateinit = field.isLateinit, + isVolatile = field.isVolatile, + optInAnnotation = field.optInAnnotation, + printOptInWrapped = defaultValue != null, + deprecation = field.deprecation, + kDoc = field.kDoc, + initializer = defaultValue.takeUnless { field.withGetter } + ) + println() - field.optInAnnotation?.let { - val rendered = it.render() - when { - defaultValue != null -> println("@OptIn(", rendered, "::class)") - inConstructor -> println("@property:", rendered) - else -> println("@", rendered) - } - } - - if (field.visibility != Visibility.PUBLIC) { - print(field.visibility.name.toLowerCaseAsciiOnly(), " ") - } - - modality?.let { - print(it.name.toLowerCaseAsciiOnly(), " ") - } - - if (override) { - print("override ") - } - if (field.isLateinit) { - print("lateinit ") - } - if (forceMutable(field) || field.isFinal && field.isMutable) { - print("var ") - } else { - print("val ") - } - print(field.name, ": ", actualTypeOfField(field).render()) - if (inConstructor) { - print(",") - } - if (defaultValue == null) { - println() - return - } - - if (field.withGetter) { - println() - pushIndent() - print("get()") - } - println(" = $defaultValue") - field.customSetter?.let { - println("set(value) {") + if (defaultValue != null && field.withGetter) { withIndent { - println(it) + println("get() = $defaultValue") } - println("}") } - if (field.withGetter) { - popIndent() + + field.customSetter?.let { + withIndent { + print("set(value)") + printBlock { + println(it) + } + } } } } diff --git a/generators/tree-generator-common/src/org/jetbrains/kotlin/generators/tree/printer/printUtils.kt b/generators/tree-generator-common/src/org/jetbrains/kotlin/generators/tree/printer/printUtils.kt index ab2ee266306..d491dca8feb 100644 --- a/generators/tree-generator-common/src/org/jetbrains/kotlin/generators/tree/printer/printUtils.kt +++ b/generators/tree-generator-common/src/org/jetbrains/kotlin/generators/tree/printer/printUtils.kt @@ -190,6 +190,80 @@ inline fun SmartPrinter.printFunctionWithBlockBody( printBlock(blockBody) } +context(ImportCollector) +fun SmartPrinter.printPropertyDeclaration( + name: String, + type: TypeRef, + kind: VariableKind, + inConstructor: Boolean = false, + visibility: Visibility = Visibility.PUBLIC, + modality: Modality? = null, + override: Boolean = false, + isLateinit: Boolean = false, + isVolatile: Boolean = false, + kDoc: String? = null, + optInAnnotation: ClassRef<*>? = null, + printOptInWrapped: Boolean = false, + deprecation: Deprecated? = null, + initializer: String? = null, +) { + printKDoc(kDoc) + + deprecation?.let { + println("@Deprecated(") + withIndent { + println("message = \"", it.message, "\",") + println("replaceWith = ReplaceWith(\"", it.replaceWith.expression, "\"),") + println("level = DeprecationLevel.", it.level.name, ",") + } + println(")") + } + + if (isVolatile) { + println("@", type().render()) + } + + optInAnnotation?.let { + val rendered = it.render() + when { + printOptInWrapped -> println("@OptIn(", rendered, "::class)") + inConstructor -> println("@property:", rendered) + else -> println("@", rendered) + } + } + + if (visibility != Visibility.PUBLIC) { + print(visibility.name.toLowerCaseAsciiOnly(), " ") + } + + modality?.let { + print(it.name.toLowerCaseAsciiOnly(), " ") + } + + if (override) { + print("override ") + } + if (isLateinit) { + print("lateinit ") + } + when (kind) { + VariableKind.PARAMETER -> {} + VariableKind.VAL -> print("val ") + VariableKind.VAR -> print("var ") + } + print(name, ": ", type.render()) + + if (initializer != null) { + print(" = $initializer") + } + + if (inConstructor) { + print(",") + } +} + +enum class VariableKind { VAL, VAR, PARAMETER } + inline fun SmartPrinter.printBlock(body: () -> Unit) { println(" {") withIndent(body)