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

org.jetbrains.kotlin.ir.util.IrFakeOverrideUtils.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2010-2024 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.util

import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.symbols.IrSymbol

val IrDeclaration.isReal: Boolean get() = !isFakeOverride

val IrDeclaration.isFakeOverride: Boolean
    get() = when (this) {
        is IrSimpleFunction -> isFakeOverride
        is IrProperty -> isFakeOverride
        else -> false
    }

val IrSimpleFunction.target: IrSimpleFunction
    get() = if (modality == Modality.ABSTRACT) this else resolveFakeOverrideOrFail()

val IrFunction.target: IrFunction
    get() = when (this) {
        is IrSimpleFunction -> this.target
        is IrConstructor -> this
    }

fun > T.collectRealOverrides(
    toSkip: (T) -> Boolean = { false },
    filter: (T) -> Boolean = { false },
): Set {
    if (isReal && !toSkip(this)) return setOf(this)

    @Suppress("UNCHECKED_CAST")
    return this.overriddenSymbols
        .map { it.owner as T }
        .collectAndFilterRealOverrides(toSkip, filter)
}

fun > Collection.collectAndFilterRealOverrides(
    toSkip: (T) -> Boolean = { false },
    filter: (T) -> Boolean = { false }
): Set {
    val visited = mutableSetOf()
    val realOverrides = mutableMapOf()

    // Due to IR copying in performByIrFile, overrides should only be distinguished up to their signatures.
    fun T.toKey(): Any = symbol.signature ?: this

    fun collectRealOverrides(member: T) {
        if (!visited.add(member) || filter(member)) return

        if (member.isReal && !toSkip(member)) {
            realOverrides[member.toKey()] = member
        } else {
            @Suppress("UNCHECKED_CAST")
            for (overridden in member.overriddenSymbols) {
                collectRealOverrides(overridden.owner as T)
            }
        }
    }

    for (declaration in this) {
        collectRealOverrides(declaration)
    }

    fun excludeRepeated(member: T) {
        if (!visited.add(member)) return

        for (overridden in member.overriddenSymbols) {
            @Suppress("UNCHECKED_CAST")
            val owner = overridden.owner as T
            realOverrides.remove(owner.toKey())
            excludeRepeated(owner)
        }
    }

    visited.clear()
    for ((_, realOverride) in realOverrides.toList()) {
        excludeRepeated(realOverride)
    }

    return realOverrides.values.toSet()
}

@Suppress("UNCHECKED_CAST")
fun Collection.collectAndFilterRealOverrides(): Set = when {
    all { it is IrSimpleFunction } -> (this as Collection).collectAndFilterRealOverrides()
    all { it is IrProperty } -> (this as Collection).collectAndFilterRealOverrides()
    else -> error("all members should be of the same kind, got ${map { it.render() }}")
}

fun > T.resolveFakeOverrideMaybeAbstractOrFail(): T =
    resolveFakeOverrideMaybeAbstract() ?: error("No real overrides for ${this.render()}")

fun > T.resolveFakeOverrideMaybeAbstract(
    toSkip: (T) -> Boolean = { false },
): T? {
    if (!isFakeOverride && !toSkip(this)) return this
    return collectRealOverrides(toSkip).firstOrNull()
}

fun > T.resolveFakeOverrideOrFail(): T =
    resolveFakeOverride() ?: error("No real overrides for ${this.render()}")

// TODO: use this implementation instead of any other
fun > T.resolveFakeOverride(
    toSkip: (T) -> Boolean = { false },
): T? {
    if (!isFakeOverride && !toSkip(this)) return this
    return collectRealOverrides(toSkip) { it.modality == Modality.ABSTRACT }
        .let { realOverrides ->
            // Kotlin forbids conflicts between overrides, but they may trickle down from Java.
            realOverrides.singleOrNull { (it.parent as? IrClass)?.isInterface != true }
            // TODO: We take firstOrNull instead of singleOrNull here because of KT-36188.
                ?: realOverrides.firstOrNull()
        }
}