[FIR/IR generator] Inherit TypeRef from Importable

This is the first step to replace the usages of `Importable`
in the FIR generator with `TypeRef`.

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 16:39:22 +02:00
committed by Space Team
parent d86161bccb
commit 3a5e69d651
4 changed files with 78 additions and 7 deletions
@@ -10,6 +10,7 @@ import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec import com.squareup.kotlinpoet.TypeSpec
import org.jetbrains.kotlin.generators.tree.* import org.jetbrains.kotlin.generators.tree.*
import org.jetbrains.kotlin.ir.generator.BASE_PACKAGE import org.jetbrains.kotlin.ir.generator.BASE_PACKAGE
import org.jetbrains.kotlin.ir.generator.model.Element
import org.jetbrains.kotlin.ir.generator.util.* import org.jetbrains.kotlin.ir.generator.util.*
class Config( class Config(
@@ -71,6 +72,14 @@ class ElementConfig(
override fun toString() = element.name override fun toString() = element.name
override val packageName: String
get() = category.packageName
override val type: String
get() = Element.elementName2typeName(name)
override fun getTypeWithArguments(notNull: Boolean): String = type
enum class Category(private val packageDir: String, val defaultVisitorParam: String) { enum class Category(private val packageDir: String, val defaultVisitorParam: String) {
Expression("expressions", "expression"), Expression("expressions", "expression"),
Declaration("declarations", "declaration"), Declaration("declarations", "declaration"),
@@ -93,6 +102,14 @@ class ElementConfigRef(
override fun copy(nullable: Boolean) = ElementConfigRef(element, args, nullable) override fun copy(nullable: Boolean) = ElementConfigRef(element, args, nullable)
override fun toString() = element.name override fun toString() = element.name
override val type: String
get() = element.type
override val packageName: String
get() = element.packageName
override fun getTypeWithArguments(notNull: Boolean): String = type + generics
} }
sealed class UseFieldAsParameterInIrFactoryStrategy { sealed class UseFieldAsParameterInIrFactoryStrategy {
@@ -87,6 +87,14 @@ data class ElementRef(
override fun copy(args: Map<NamedTypeParameterRef, TypeRef>) = ElementRef(element, args, nullable) override fun copy(args: Map<NamedTypeParameterRef, TypeRef>) = ElementRef(element, args, nullable)
override fun copy(nullable: Boolean) = ElementRef(element, args, nullable) override fun copy(nullable: Boolean) = ElementRef(element, args, nullable)
override fun toString() = "${element.name}<${args}>" override fun toString() = "${element.name}<${args}>"
override val type: String
get() = element.typeName
override val packageName: String
get() = element.packageName
override fun getTypeWithArguments(notNull: Boolean): String = type + generics
} }
sealed class Field( sealed class Field(
@@ -12,7 +12,16 @@ import org.jetbrains.kotlin.utils.addToStdlib.UnsafeCastFunction
import org.jetbrains.kotlin.utils.addToStdlib.castAll import org.jetbrains.kotlin.utils.addToStdlib.castAll
import org.jetbrains.kotlin.utils.addToStdlib.partitionIsInstance import org.jetbrains.kotlin.utils.addToStdlib.partitionIsInstance
private object InferredOverriddenType : TypeRef private object InferredOverriddenType : TypeRef {
override val type: String
get() = error("not supported")
override val packageName: String?
get() = null
override fun getTypeWithArguments(notNull: Boolean): String {
error("not supported")
}
}
data class Model(val elements: List<Element>, val rootElement: Element) data class Model(val elements: List<Element>, val rootElement: Element)
@@ -9,8 +9,18 @@ import org.jetbrains.kotlin.types.Variance
import java.util.* import java.util.*
import kotlin.reflect.KClass import kotlin.reflect.KClass
interface TypeRef { interface TypeRef : Importable {
object Star : TypeRef object Star : TypeRef {
override val type: String
get() = "*"
override val packageName: String?
get() = null
override fun getTypeWithArguments(notNull: Boolean): String = type
override fun toString(): String = type
}
} }
interface ClassOrElementRef : TypeRefWithNullability interface ClassOrElementRef : TypeRefWithNullability
@@ -43,11 +53,19 @@ class ClassRef<P : TypeParameterRef> private constructor(
names.joinToString(".") names.joinToString(".")
/** Package name, like `"kotlin.collections"` for `Map.Entry`. */ /** Package name, like `"kotlin.collections"` for `Map.Entry`. */
val packageName: String get() = names[0] override val packageName: String get() = names[0]
/** Simple name of this class, like `"Entry"` for `Map.Entry`. */ /** Simple name of this class, like `"Entry"` for `Map.Entry`. */
val simpleName: String get() = names[names.size - 1] val simpleName: String get() = names[names.size - 1]
override val type: String
get() = simpleName
override val fullQualifiedName: String
get() = canonicalName
override fun getTypeWithArguments(notNull: Boolean): String = type + generics
/** /**
* The enclosing classes, outermost first, followed by the simple name. This is `["Map", "Entry"]` * The enclosing classes, outermost first, followed by the simple name. This is `["Map", "Entry"]`
* for `Map.Entry`. * for `Map.Entry`.
@@ -63,13 +81,21 @@ class ClassRef<P : TypeParameterRef> private constructor(
sealed interface TypeParameterRef : TypeRef sealed interface TypeParameterRef : TypeRef
data class PositionTypeParameterRef( data class PositionTypeParameterRef(
val index: Int val index: Int,
) : TypeParameterRef { ) : TypeParameterRef {
override fun toString() = index.toString() override fun toString() = index.toString()
override val type: String
get() = error("Getting type from ${this::class.simpleName} is not supported")
override val packageName: String?
get() = null
override fun getTypeWithArguments(notNull: Boolean): String = type
} }
open class NamedTypeParameterRef( open class NamedTypeParameterRef(
val name: String val name: String,
) : TypeParameterRef { ) : TypeParameterRef {
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
return other is NamedTypeParameterRef && other.name == name return other is NamedTypeParameterRef && other.name == name
@@ -80,6 +106,14 @@ open class NamedTypeParameterRef(
} }
override fun toString() = name override fun toString() = name
override val type: String
get() = name
override val packageName: String?
get() = null
override fun getTypeWithArguments(notNull: Boolean) = name
} }
interface TypeRefWithNullability : TypeRef { interface TypeRefWithNullability : TypeRef {
@@ -94,6 +128,9 @@ interface ParametrizedTypeRef<Self, P : TypeParameterRef> : TypeRef {
fun copy(args: Map<P, TypeRef>): Self fun copy(args: Map<P, TypeRef>): Self
} }
val ParametrizedTypeRef<*, *>.generics: String
get() = if (args.isEmpty()) "" else args.values.joinToString(prefix = "<", postfix = ">") { it.typeWithArguments }
fun <T> ParametrizedTypeRef<T, NamedTypeParameterRef>.withArgs(vararg args: Pair<String, TypeRef>) = fun <T> ParametrizedTypeRef<T, NamedTypeParameterRef>.withArgs(vararg args: Pair<String, TypeRef>) =
copy(args.associate { (k, v) -> NamedTypeParameterRef(k) to v }) copy(args.associate { (k, v) -> NamedTypeParameterRef(k) to v })
@@ -104,7 +141,7 @@ fun <T> ParametrizedTypeRef<T, PositionTypeParameterRef>.withArgs(vararg args: T
class TypeVariable( class TypeVariable(
name: String, name: String,
val bounds: List<TypeRef>, val bounds: List<TypeRef>,
val variance: Variance val variance: Variance,
) : NamedTypeParameterRef(name) ) : NamedTypeParameterRef(name)
fun <P : TypeParameterRef> KClass<*>.asRef(): ClassRef<P> { fun <P : TypeParameterRef> KClass<*>.asRef(): ClassRef<P> {