All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.ir.interpreter.state.Complex.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.ir.interpreter.state

import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrProperty
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.interpreter.fqName
import org.jetbrains.kotlin.ir.interpreter.stack.CallStack
import org.jetbrains.kotlin.ir.interpreter.stack.Field
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.ir.util.overrides
import org.jetbrains.kotlin.ir.util.resolveFakeOverride

internal interface Complex : State {
    var superWrapperClass: Wrapper?
    var outerClass: Field?

    fun irClassFqName() = irClass.fqName

    private fun getIrFunctionFromGivenClass(irClass: IrClass, owner: IrFunction): IrFunction? {
        val propertyGetters = irClass.declarations.filterIsInstance().mapNotNull { it.getter }
        val propertySetters = irClass.declarations.filterIsInstance().mapNotNull { it.setter }
        val functions = irClass.declarations.filterIsInstance()
        return (propertyGetters + propertySetters + functions).firstOrNull {
            when {
                it is IrSimpleFunction && owner is IrSimpleFunction -> it.overrides(owner) || owner.overrides(it)
                else -> it == owner
            }
        }
    }

    fun getRealFunction(owner: IrSimpleFunction): IrSimpleFunction {
        return owner.resolveFakeOverride() as IrSimpleFunction
    }

    override fun getIrFunctionByIrCall(expression: IrCall): IrFunction? {
        val receiver = expression.superQualifierSymbol?.owner ?: irClass
        val irFunction = getIrFunctionFromGivenClass(receiver, expression.symbol.owner) ?: return null
        return (irFunction as IrSimpleFunction).resolveFakeOverride()
    }

    fun loadOuterClassesInto(callStack: CallStack, receiver: IrValueSymbol? = null) {
        fun  List.takeFromEndWhile(predicate: (T) -> Boolean): List {
            val list = mutableListOf()
            for (i in this.lastIndex downTo 0) {
                if (!predicate(this[i]))
                    break
                list.add(this[i])
            }
            return list
        }

        generateSequence(outerClass) { (it.second as? Complex)?.outerClass }
            .toList()
            .takeFromEndWhile { receiver == null || it.first != receiver } // only state's below receiver must be loaded on stack
            .forEach { (symbol, state) ->
                callStack.storeState(symbol, state)
                (state as? StateWithClosure)?.let { callStack.loadUpValues(it) }
            }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy