IR tree gen: allow lists of nullable elements and arrays

This commit is contained in:
mcpiroman
2023-03-04 18:57:59 +01:00
committed by Alexander Udalov
parent 62c72dedcf
commit 9145cf7035
4 changed files with 30 additions and 6 deletions
@@ -36,6 +36,17 @@ fun <T : IrElement, D> MutableList<T>.transformInPlace(transformer: IrElementTra
}
}
fun <T : IrElement, D> Array<T?>.transformInPlace(transformer: IrElementTransformer<D>, data: D) {
for (i in indices) {
// Cast to IrElementBase to avoid casting to interface and invokeinterface, both of which are slow.
val element = get(i) as IrElementBase?
if (element != null) {
@Suppress("UNCHECKED_CAST")
set(i, element.transform(transformer, data) as T)
}
}
}
/**
* Transforms a mutable list in place.
* Each element `it` is replaced with a result of `transformation(it)`,
@@ -116,6 +116,7 @@ class ListFieldConfig(
enum class Mutability {
Immutable,
Var,
List
List,
Array
}
}
@@ -32,10 +32,17 @@ fun config2model(config: Config): Model {
fc.baseGetter
)
is ListFieldConfig -> {
val listType = if (fc.mutability == ListFieldConfig.Mutability.List) type(
"kotlin.collections",
"MutableList"
) else type("kotlin.collections", "List")
val listType = when (fc.mutability) {
ListFieldConfig.Mutability.List -> type(
"kotlin.collections",
"MutableList"
)
ListFieldConfig.Mutability.Array -> type(
"kotlin.",
"Array"
)
else -> type("kotlin.collections", "List")
}
ListField(
fc,
fc.name,
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.ir.generator.elementTransformerType
import org.jetbrains.kotlin.ir.generator.elementVisitorType
import org.jetbrains.kotlin.ir.generator.model.*
import org.jetbrains.kotlin.ir.generator.util.TypeKind
import org.jetbrains.kotlin.ir.generator.util.TypeRefWithNullability
import org.jetbrains.kotlin.ir.generator.util.tryParameterizedBy
import java.io.File
@@ -161,7 +162,11 @@ fun printElements(generationPath: File, model: Model) = sequence {
if (child.nullable) append("?")
when (child) {
is SingleField -> append(".%N(%N, %N)")
is ListField -> append(".forEach { it.%N(%N, %N) }")
is ListField -> {
append(".forEach { it")
if ((child.elementType as? TypeRefWithNullability)?.nullable == true) append("?")
append(".%N(%N, %N) }")
}
}
}, child.name, acceptMethodName, visitorParam, dataParam)
}