[IR generator] Use Element#typeName i/o Element#name where needed

This slightly reduces repetition.
This commit is contained in:
Sergej Jaskiewicz
2023-11-03 10:37:24 +01:00
committed by Space Team
parent 6ac4cd5973
commit 8ba42a7ff2
3 changed files with 12 additions and 15 deletions
@@ -16,8 +16,9 @@ import org.jetbrains.kotlin.generators.tree.printer.printFunctionWithBlockBody
import org.jetbrains.kotlin.ir.generator.config.AbstractTreeBuilder import org.jetbrains.kotlin.ir.generator.config.AbstractTreeBuilder
import org.jetbrains.kotlin.ir.generator.model.Element import org.jetbrains.kotlin.ir.generator.model.Element
import org.jetbrains.kotlin.ir.generator.model.Element.Category.* import org.jetbrains.kotlin.ir.generator.model.Element.Category.*
import org.jetbrains.kotlin.ir.generator.model.Element.Companion.elementName2typeName
import org.jetbrains.kotlin.ir.generator.model.ListField.Mutability.* import org.jetbrains.kotlin.ir.generator.model.ListField.Mutability.*
import org.jetbrains.kotlin.ir.generator.model.ListField.Mutability.Array
import org.jetbrains.kotlin.ir.generator.model.ListField.Mutability.List
import org.jetbrains.kotlin.ir.generator.model.SingleField import org.jetbrains.kotlin.ir.generator.model.SingleField
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
@@ -141,11 +142,11 @@ object IrTree : AbstractTreeBuilder() {
kDoc = """ kDoc = """
The arbitrary metadata associated with this IR node. The arbitrary metadata associated with this IR node.
@see ${elementName2typeName(this@element.name)} @see $typeName
""".trimIndent() """.trimIndent()
} }
kDoc = """ kDoc = """
An [${elementName2typeName(rootElement.name)}] capable of holding something which backends can use to write An [${rootElement.typeName}] capable of holding something which backends can use to write
as the metadata for the declaration. as the metadata for the declaration.
Technically, it can even be ± an array of bytes, but right now it's usually the frontend representation of the declaration, Technically, it can even be ± an array of bytes, but right now it's usually the frontend representation of the declaration,
@@ -224,7 +225,7 @@ object IrTree : AbstractTreeBuilder() {
fun foo(defined: Int, ${'$'}extra: String) { /* ... */ } fun foo(defined: Int, ${'$'}extra: String) { /* ... */ }
``` ```
If a compiler plugin adds parameters to an [${elementName2typeName(function.name)}], If a compiler plugin adds parameters to an [${function.typeName}],
the representations of the function in the frontend and in the backend may diverge, potentially causing signature mismatch and the representations of the function in the frontend and in the backend may diverge, potentially causing signature mismatch and
linkage errors (see [KT-40980](https://youtrack.jetbrains.com/issue/KT-40980)). linkage errors (see [KT-40980](https://youtrack.jetbrains.com/issue/KT-40980)).
We wouldn't want IR plugins to affect the frontend representation, since in an IDE you'd want to be able to see those We wouldn't want IR plugins to affect the frontend representation, since in an IDE you'd want to be able to see those
@@ -299,7 +300,7 @@ object IrTree : AbstractTreeBuilder() {
If this is a sealed class or interface, this list contains symbols of all its immediate subclasses. If this is a sealed class or interface, this list contains symbols of all its immediate subclasses.
Otherwise, this is an empty list. Otherwise, this is an empty list.
NOTE: If this [${elementName2typeName(this@element.name)}] was deserialized from a klib, this list will always be empty! NOTE: If this [$typeName] was deserialized from a klib, this list will always be empty!
See [KT-54028](https://youtrack.jetbrains.com/issue/KT-54028). See [KT-54028](https://youtrack.jetbrains.com/issue/KT-54028).
""".trimIndent() """.trimIndent()
} }
@@ -68,7 +68,7 @@ class Element(
override var kind: ImplementationKind? = null override var kind: ImplementationKind? = null
override val typeName override val typeName
get() = elementName2typeName(name) get() = "Ir" + name.replaceFirstChar(Char::uppercaseChar)
/** /**
* Whether this element is semantically a leaf element in the hierarchy. * Whether this element is semantically a leaf element in the hierarchy.
@@ -113,10 +113,6 @@ class Element(
override fun toString() = name override fun toString() = name
companion object {
fun elementName2typeName(name: String) = "Ir" + name.replaceFirstChar(Char::uppercaseChar)
}
fun elementParentsRecursively(): List<ElementRef> { fun elementParentsRecursively(): List<ElementRef> {
val linkedSet = buildSet { val linkedSet = buildSet {
fun recurse(element: Element) { fun recurse(element: Element) {
@@ -133,7 +133,7 @@ private class TransformerVoidPrinter(
// IrPackageFragment is treated as transformByChildren in IrElementTransformerVoid for historical reasons. // IrPackageFragment is treated as transformByChildren in IrElementTransformerVoid for historical reasons.
private val Element.isPackageFragment: Boolean private val Element.isPackageFragment: Boolean
get() = name == IrTree.packageFragment.name get() = this == IrTree.packageFragment
// Despite IrFile and IrExternalPackageFragment being transformByChildren, we treat them differently in IrElementTransformerVoid // Despite IrFile and IrExternalPackageFragment being transformByChildren, we treat them differently in IrElementTransformerVoid
// than in IrElementTransformer for historical reasons. We want to preserve the historical semantics here. // than in IrElementTransformer for historical reasons. We want to preserve the historical semantics here.
@@ -325,11 +325,11 @@ private class TypeTransformerPrinter(
} }
printBlock { printBlock {
when (element.name) { when (element) {
IrTree.memberAccessExpression.name -> { IrTree.memberAccessExpression -> {
if (irTypeFields.singleOrNull()?.name != "typeArguments") { if (irTypeFields.singleOrNull()?.name != "typeArguments") {
error( error(
"""`Ir${IrTree.memberAccessExpression.name.capitalizeAsciiOnly()}` has unexpected fields with `IrType` type. """`${IrTree.memberAccessExpression.typeName}` has unexpected fields with `IrType` type.
|Please adjust logic of `${visitorType.simpleName}`'s generation.""".trimMargin() |Please adjust logic of `${visitorType.simpleName}`'s generation.""".trimMargin()
) )
} }
@@ -348,7 +348,7 @@ private class TypeTransformerPrinter(
} }
println("}") println("}")
} }
IrTree.`class`.name -> { IrTree.`class` -> {
println(visitorParam, ".valueClassRepresentation?.mapUnderlyingType {") println(visitorParam, ".valueClassRepresentation?.mapUnderlyingType {")
withIndent { withIndent {
println("transformType(", visitorParam, ", it, data)") println("transformType(", visitorParam, ", it, data)")