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. * Transforms a mutable list in place.
* Each element `it` is replaced with a result of `transformation(it)`, * Each element `it` is replaced with a result of `transformation(it)`,
@@ -116,6 +116,7 @@ class ListFieldConfig(
enum class Mutability { enum class Mutability {
Immutable, Immutable,
Var, Var,
List List,
Array
} }
} }
@@ -32,10 +32,17 @@ fun config2model(config: Config): Model {
fc.baseGetter fc.baseGetter
) )
is ListFieldConfig -> { is ListFieldConfig -> {
val listType = if (fc.mutability == ListFieldConfig.Mutability.List) type( val listType = when (fc.mutability) {
"kotlin.collections", ListFieldConfig.Mutability.List -> type(
"MutableList" "kotlin.collections",
) else type("kotlin.collections", "List") "MutableList"
)
ListFieldConfig.Mutability.Array -> type(
"kotlin.",
"Array"
)
else -> type("kotlin.collections", "List")
}
ListField( ListField(
fc, fc,
fc.name, 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.elementVisitorType
import org.jetbrains.kotlin.ir.generator.model.* import org.jetbrains.kotlin.ir.generator.model.*
import org.jetbrains.kotlin.ir.generator.util.TypeKind 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 org.jetbrains.kotlin.ir.generator.util.tryParameterizedBy
import java.io.File import java.io.File
@@ -161,7 +162,11 @@ fun printElements(generationPath: File, model: Model) = sequence {
if (child.nullable) append("?") if (child.nullable) append("?")
when (child) { when (child) {
is SingleField -> append(".%N(%N, %N)") 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) }, child.name, acceptMethodName, visitorParam, dataParam)
} }