[FIR generator] Use TypeVariable instead of TypeArgument for TPs

This is a step towards commonizing the code generator between
FIR and IR: KT-61970
This commit is contained in:
Sergej Jaskiewicz
2023-09-12 20:29:30 +03:00
committed by Space Team
parent c25cde8871
commit 8be455649c
13 changed files with 98 additions and 65 deletions
@@ -5,6 +5,8 @@
package org.jetbrains.kotlin.generators.tree
import org.jetbrains.kotlin.generators.tree.printer.generics
/**
* A common interface representing a FIR or IR tree element.
*/
@@ -17,6 +19,10 @@ interface AbstractElement<Element : AbstractElement<Element, Field>, Field : Abs
val parents: List<Element>
val params: List<TypeVariable>
// TODO: Remove this property as soon as we replace org.jetbrains.kotlin.fir.tree.generator.model.ElementWithArguments with
// a generic ElementRef class
val typeArguments: List<TypeArgument>
val parentsArguments: Map<Element, Map<TypeRef, TypeRef>>
@@ -31,8 +37,3 @@ interface AbstractElement<Element : AbstractElement<Element, Field>, Field : Abs
override fun getTypeWithArguments(notNull: Boolean): String = type + generics
}
val AbstractElement<*, *>.generics: String
get() = typeArguments.takeIf { it.isNotEmpty() }
?.let { it.joinToString(", ", "<", ">") { it.name } }
?: ""
@@ -0,0 +1,54 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.generators.tree.printer
import org.jetbrains.kotlin.generators.tree.AbstractElement
import org.jetbrains.kotlin.generators.tree.ImplementationKind
import org.jetbrains.kotlin.generators.tree.typeWithArguments
/**
* The angle bracket-delimited list of type parameters to print, or empty string if the element has no type parameters.
*
* For type parameters that have a single upper bound, also prints that upper bound. If at least one type parameter has multiple upper
* bounds, doesn't print any upper bounds at all. They are expected to be printed in the `where` clause (see [multipleUpperBoundsList]).
*
* @param end The string to add after the closing angle bracket of the type parameter list
*/
fun AbstractElement<*, *>.typeParameters(end: String = ""): String = params.takeIf { it.isNotEmpty() }
?.joinToString(", ", "<", ">$end") { param ->
param.name + (param.bounds.singleOrNull()?.let { " : ${it.typeWithArguments}" } ?: "")
} ?: ""
/**
* The `where` clause to print after the class or function declaration if at least one of the element's tye parameters has multiple upper
* bounds.
*
* Otherwise, an empty string.
*/
fun AbstractElement<*, *>.multipleUpperBoundsList(): String {
val paramsWithMultipleUpperBounds = params.filter { it.bounds.size > 1 }.takeIf { it.isNotEmpty() } ?: return ""
return buildString {
append(" where ")
paramsWithMultipleUpperBounds.joinTo(this, separator = ", ") { param ->
param.bounds.joinToString(", ") { bound -> "$param : ${bound.typeWithArguments}" }
}
append("")
}
}
/**
* The braces to print in the inheritance clause if this is a class, or empty string if this is an interface.
*/
fun ImplementationKind?.braces(): String = when (this) {
ImplementationKind.Interface, ImplementationKind.SealedInterface -> ""
ImplementationKind.OpenClass, ImplementationKind.AbstractClass, ImplementationKind.SealedClass -> "()"
else -> throw IllegalStateException(this.toString())
}
val AbstractElement<*, *>.generics: String
get() = params.takeIf { it.isNotEmpty() }
?.let { it.joinToString(", ", "<", ">") { it.name } }
?: ""