[FIR] Avoid instanceof List in transformInplace

This commit is contained in:
Simon Ogorodnik
2019-06-05 18:09:05 +03:00
committed by Mikhail Glukhikh
parent 024636f704
commit 5a06027c53
2 changed files with 28 additions and 14 deletions
@@ -148,7 +148,7 @@ abstract class AbstractRawFirBuilderTestCase : KtParsingTestCase(
} else {
element.transformChildren(this, Unit)
}
return CompositeTransformResult(element)
return CompositeTransformResult.single(element)
}
}
@@ -7,28 +7,42 @@ package org.jetbrains.kotlin.fir.visitors
import org.jetbrains.kotlin.fir.FirElement
class CompositeTransformResult<out T : Any>(val a: Any) {
sealed class CompositeTransformResult<out T : Any>() {
class Single<out T : Any>(val _single: T) : CompositeTransformResult<T>()
class Multiple<out T : Any>(val _list: List<T>) : CompositeTransformResult<T>()
companion object {
fun <T : Any> empty() = CompositeTransformResult<T>(emptyList<T>())
fun <T : Any> single(t: T) = CompositeTransformResult<T>(t)
fun <T : Any> many(l: List<T>) = CompositeTransformResult<T>(l)
fun <T : Any> empty() = CompositeTransformResult.Multiple<T>(emptyList<T>())
fun <T : Any> single(t: T) = CompositeTransformResult.Single(t)
fun <T : Any> many(l: List<T>) = CompositeTransformResult.Multiple(l)
}
val isSingle get() = a !is List<*>
val isEmpty get() = a is List<*> && a.isEmpty()
val list: List<T>
get() = when (this) {
is CompositeTransformResult.Multiple<*> -> _list as List<T>
else -> error("!")
}
val single: T
get() {
assert(isSingle)
return a as T
get() = when (this) {
is CompositeTransformResult.Single<*> -> _single as T
else -> error("!")
}
val list: List<T>
get() {
return a as List<T>
}
val isSingle
get() = this is CompositeTransformResult.Single<*>
val isEmpty
get() = this is CompositeTransformResult.Multiple<*> && this.list.isEmpty()
}
@Suppress("NOTHING_TO_INLINE")