diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ImplicitReceiverStack.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ImplicitReceiverStack.kt index 35e8b27e7fc..76d3c32494e 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ImplicitReceiverStack.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/ImplicitReceiverStack.kt @@ -5,15 +5,8 @@ package org.jetbrains.kotlin.fir.resolve -import kotlinx.collections.immutable.PersistentList -import kotlinx.collections.immutable.PersistentMap -import kotlinx.collections.immutable.persistentListOf -import kotlinx.collections.immutable.persistentMapOf import org.jetbrains.kotlin.fir.resolve.calls.ImplicitDispatchReceiverValue import org.jetbrains.kotlin.fir.resolve.calls.ImplicitReceiverValue -import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol -import org.jetbrains.kotlin.fir.types.ConeKotlinType -import org.jetbrains.kotlin.name.Name abstract class ImplicitReceiverStack : Iterable> { abstract operator fun get(name: String?): ImplicitReceiverValue<*>? @@ -22,88 +15,3 @@ abstract class ImplicitReceiverStack : Iterable> { abstract fun lastDispatchReceiver(lookupCondition: (ImplicitReceiverValue<*>) -> Boolean): ImplicitDispatchReceiverValue? abstract fun receiversAsReversed(): List> } - -abstract class MutableImplicitReceiverStack : ImplicitReceiverStack() { - abstract fun add(name: Name?, value: ImplicitReceiverValue<*>) - abstract fun pop(name: Name?) - - abstract fun snapshot(): MutableImplicitReceiverStack -} - -class ImplicitReceiverStackImpl private constructor( - private var stack: PersistentList>, - // This multi-map holds indexes of the stack ^ - private var originalTypes: PersistentList, - private var indexesPerLabel: PersistentSetMultimap, - private var indexesPerSymbol: PersistentMap, Int> -) : MutableImplicitReceiverStack() { - val size: Int get() = stack.size - - constructor() : this( - persistentListOf(), - persistentListOf(), - PersistentSetMultimap(), - persistentMapOf() - ) - - override fun add(name: Name?, value: ImplicitReceiverValue<*>) { - stack = stack.add(value) - originalTypes = originalTypes.add(value.type) - val index = stack.size - 1 - if (name != null) { - indexesPerLabel = indexesPerLabel.put(name, index) - } - indexesPerSymbol = indexesPerSymbol.put(value.boundSymbol, index) - } - - override fun pop(name: Name?) { - val index = stack.size - 1 - if (name != null) { - indexesPerLabel = indexesPerLabel.remove(name, index) - } - originalTypes = originalTypes.removeAt(index) - val value = stack.get(index) - stack = stack.removeAt(index) - indexesPerSymbol = indexesPerSymbol.remove(value.boundSymbol) - } - - override operator fun get(name: String?): ImplicitReceiverValue<*>? { - if (name == null) { - return stack.lastOrNull { - it !is ImplicitDispatchReceiverValue || !it.inDelegated - } - } - return indexesPerLabel[Name.identifier(name)].lastOrNull()?.let { stack[it] }?.takeIf { - it !is ImplicitDispatchReceiverValue || !it.inDelegated - } - } - - override fun lastDispatchReceiver(): ImplicitDispatchReceiverValue? { - return stack.filterIsInstance().lastOrNull() - } - - override fun lastDispatchReceiver(lookupCondition: (ImplicitReceiverValue<*>) -> Boolean): ImplicitDispatchReceiverValue? { - return stack.filterIsInstance().lastOrNull(lookupCondition) - } - - override fun receiversAsReversed(): List> = stack.asReversed() - - fun getReceiverIndex(symbol: FirBasedSymbol<*>): Int? = indexesPerSymbol[symbol] - - fun getOriginalType(index: Int): ConeKotlinType { - return originalTypes[index] - } - - fun replaceReceiverType(index: Int, type: ConeKotlinType) { - assert(index >= 0 && index < stack.size) - stack[index].replaceType(type) - } - - override operator fun iterator(): Iterator> { - return stack.iterator() - } - - override fun snapshot(): ImplicitReceiverStackImpl { - return ImplicitReceiverStackImpl(stack, originalTypes, indexesPerLabel, indexesPerSymbol) - } -}