org.jetbrains.kotlin.fir.backend.utils.ScopeUtils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
/*
* 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.fir.backend.utils
import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.backend.Fir2IrComponents
import org.jetbrains.kotlin.fir.declarations.FirClass
import org.jetbrains.kotlin.fir.declarations.FirConstructor
import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction
import org.jetbrains.kotlin.fir.resolve.isRealOwnerOf
import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope
import org.jetbrains.kotlin.fir.scopes.FirTypeScope
import org.jetbrains.kotlin.fir.scopes.ProcessorAction
import org.jetbrains.kotlin.fir.scopes.impl.declaredMemberScope
import org.jetbrains.kotlin.fir.scopes.impl.originalConstructorIfTypeAlias
import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
import org.jetbrains.kotlin.fir.types.ConeIntersectionType
internal tailrec fun FirCallableSymbol<*>.unwrapCallRepresentative(
c: Fir2IrComponents,
owner: ConeClassLikeLookupTag? = containingClassLookupTag()
): FirCallableSymbol<*> {
val fir = fir
if (fir is FirConstructor) {
val originalForTypeAlias = fir.originalConstructorIfTypeAlias
if (originalForTypeAlias != null) {
return originalForTypeAlias.symbol.unwrapCallRepresentative(c, owner)
}
}
if (fir.isIntersectionOverride) {
// We've got IR declarations (fake overrides) for intersection overrides in classes, but not for intersection types
// interface A { fun foo() }
// interface B { fun foo() }
// interface C : A, B // for C.foo we've got an IR fake override
// for {A & B} we don't have such an IR declaration, so we're unwrapping it
if (fir.dispatchReceiverType is ConeIntersectionType) {
return fir.baseForIntersectionOverride!!.symbol.unwrapCallRepresentative(c, owner)
}
return this
}
val originalForOverride = fir.originalForSubstitutionOverride
if (originalForOverride != null && originalForOverride.containingClassLookupTag() == owner) {
return originalForOverride.symbol.unwrapCallRepresentative(c, owner)
}
return this
}
internal fun FirSimpleFunction.processOverriddenFunctionSymbols(
containingClass: FirClass,
c: Fir2IrComponents,
processor: (FirNamedFunctionSymbol) -> Unit
) {
val scope = containingClass.unsubstitutedScope(c)
scope.processFunctionsByName(name) {}
scope.processOverriddenFunctionsFromSuperClasses(symbol, containingClass) { overriddenSymbol ->
if (!c.session.visibilityChecker.isVisibleForOverriding(
candidateInDerivedClass = symbol.fir, candidateInBaseClass = overriddenSymbol.fir
)
) {
return@processOverriddenFunctionsFromSuperClasses ProcessorAction.NEXT
}
processor(overriddenSymbol)
ProcessorAction.NEXT
}
}
fun FirTypeScope.processOverriddenFunctionsFromSuperClasses(
functionSymbol: FirNamedFunctionSymbol,
containingClass: FirClass,
processor: (FirNamedFunctionSymbol) -> ProcessorAction
): ProcessorAction {
val ownerTag = containingClass.symbol.toLookupTag()
return processDirectOverriddenFunctionsWithBaseScope(functionSymbol) { overridden, _ ->
val unwrapped = if (overridden.fir.isSubstitutionOverride && ownerTag.isRealOwnerOf(overridden))
overridden.originalForSubstitutionOverride!!
else
overridden
processor(unwrapped)
}
}
fun FirTypeScope.processOverriddenPropertiesFromSuperClasses(
propertySymbol: FirPropertySymbol,
containingClass: FirClass,
processor: (FirPropertySymbol) -> ProcessorAction
): ProcessorAction {
val ownerTag = containingClass.symbol.toLookupTag()
return processDirectOverriddenPropertiesWithBaseScope(propertySymbol) { overridden, _ ->
val unwrapped = if (overridden.fir.isSubstitutionOverride && ownerTag.isRealOwnerOf(overridden))
overridden.originalForSubstitutionOverride!!
else
overridden
processor(unwrapped)
}
}
internal fun FirProperty.processOverriddenPropertySymbols(
containingClass: FirClass,
c: Fir2IrComponents,
processor: (FirPropertySymbol) -> Unit
) {
val scope = containingClass.unsubstitutedScope(c)
scope.processPropertiesByName(name) {}
scope.processOverriddenPropertiesFromSuperClasses(symbol, containingClass) { overriddenSymbol ->
if (!c.session.visibilityChecker.isVisibleForOverriding(
candidateInDerivedClass = symbol.fir, candidateInBaseClass = overriddenSymbol.fir
)
) {
return@processOverriddenPropertiesFromSuperClasses ProcessorAction.NEXT
}
processor(overriddenSymbol)
ProcessorAction.NEXT
}
}
internal fun FirClassSymbol<*>.unsubstitutedScope(c: Fir2IrComponents): FirTypeScope {
return this.unsubstitutedScope(c.session, c.scopeSession, withForcedTypeCalculator = true, memberRequiredPhase = null)
}
internal fun FirClass.unsubstitutedScope(c: Fir2IrComponents): FirTypeScope {
return symbol.unsubstitutedScope(c)
}
internal fun FirClassSymbol<*>.declaredScope(c: Fir2IrComponents): FirContainingNamesAwareScope {
return this.declaredMemberScope(c.session, memberRequiredPhase = null)
}