FIR: Refine delegated members introduced to use-site scope
- Do not add hashCode/equals/toString - Do not add privates and ones that are already declared
This commit is contained in:
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.fir.resolve.*
|
||||
import org.jetbrains.kotlin.fir.resolve.calls.SyntheticPropertySymbol
|
||||
import org.jetbrains.kotlin.fir.resolve.providers.FirProvider
|
||||
import org.jetbrains.kotlin.fir.scopes.ProcessorAction
|
||||
import org.jetbrains.kotlin.fir.scopes.impl.delegatedWrapperData
|
||||
import org.jetbrains.kotlin.fir.scopes.processDirectlyOverriddenFunctions
|
||||
import org.jetbrains.kotlin.fir.scopes.processDirectlyOverriddenProperties
|
||||
import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope
|
||||
@@ -239,10 +238,7 @@ internal fun FirSimpleFunction.generateOverriddenFunctionSymbols(
|
||||
return@processDirectlyOverriddenFunctions ProcessorAction.NEXT
|
||||
}
|
||||
|
||||
val unwrapped =
|
||||
it.fir.delegatedWrapperData?.takeIf { it.containingClass == containingClass.symbol.toLookupTag() }?.wrapped?.symbol ?: it
|
||||
|
||||
val overridden = declarationStorage.getIrFunctionSymbol(unwrapped.unwrapSubstitutionOverrides())
|
||||
val overridden = declarationStorage.getIrFunctionSymbol(it.unwrapSubstitutionOverrides())
|
||||
overriddenSet += overridden as IrSimpleFunctionSymbol
|
||||
ProcessorAction.NEXT
|
||||
}
|
||||
|
||||
-4
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.fir.scopes.*
|
||||
import org.jetbrains.kotlin.fir.scopes.impl.delegatedWrapperData
|
||||
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
|
||||
import org.jetbrains.kotlin.fir.symbols.PossiblyFirFakeOverrideSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.StandardClassIds
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.*
|
||||
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
@@ -54,9 +53,6 @@ internal class DelegatedMemberGenerator(
|
||||
if (isJavaDefault(unwrapped)) {
|
||||
return@processAllFunctions
|
||||
}
|
||||
if (firSubClass is FirRegularClass && firSubClass.isData && unwrapped.symbol.callableId.classId == StandardClassIds.Any) {
|
||||
return@processAllFunctions
|
||||
}
|
||||
|
||||
val irSubFunction = generateDelegatedFunction(
|
||||
subClass, firSubClass, irField, member, functionSymbol.fir
|
||||
|
||||
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.fir.declarations.impl.FirDeclarationStatusImpl
|
||||
import org.jetbrains.kotlin.fir.diagnostics.ConeIntermediateDiagnostic
|
||||
import org.jetbrains.kotlin.fir.resolve.calls.FirSyntheticFunctionSymbol
|
||||
import org.jetbrains.kotlin.fir.resolve.substitution.substitutorByMap
|
||||
import org.jetbrains.kotlin.fir.scopes.impl.hasTypeOf
|
||||
import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope
|
||||
import org.jetbrains.kotlin.fir.symbols.CallableId
|
||||
import org.jetbrains.kotlin.fir.symbols.StandardClassIds
|
||||
@@ -29,7 +30,6 @@ import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
|
||||
import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
|
||||
import org.jetbrains.kotlin.fir.types.impl.ConeTypeParameterTypeImpl
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
|
||||
@@ -337,17 +337,6 @@ fun FirSimpleFunction.isPublicInObject(checkOnlyName: Boolean): Boolean {
|
||||
}
|
||||
}
|
||||
|
||||
private fun FirValueParameter.hasTypeOf(classId: ClassId, allowNullable: Boolean): Boolean {
|
||||
val classLike = when (val type = returnTypeRef.coneType) {
|
||||
is ConeClassLikeType -> type
|
||||
is ConeFlexibleType -> type.upperBound as? ConeClassLikeType ?: return false
|
||||
else -> return false
|
||||
}
|
||||
|
||||
if (classLike.isMarkedNullable && !allowNullable) return false
|
||||
return classLike.lookupTag.classId == classId
|
||||
}
|
||||
|
||||
private val PUBLIC_METHOD_NAMES_IN_OBJECT = setOf("equals", "hashCode", "getClass", "wait", "notify", "notifyAll", "toString")
|
||||
|
||||
private fun FirSimpleFunction.getFunctionTypeForAbstractMethod(): ConeLookupTagBasedType {
|
||||
|
||||
@@ -47,7 +47,8 @@ class KotlinScopeProvider(
|
||||
symbol.fir.scopeForSupertype(
|
||||
substitutor(symbol, useSiteSuperType, useSiteSession),
|
||||
useSiteSession, scopeSession, delegateField,
|
||||
subClass = klass
|
||||
subClass = klass,
|
||||
decoratedDeclaredMemberScope
|
||||
).let {
|
||||
it as? FirTypeScope ?: error("$it is expected to be FirOverrideAwareScope")
|
||||
}
|
||||
@@ -122,7 +123,8 @@ private fun FirClass<*>.scopeForSupertype(
|
||||
useSiteSession: FirSession,
|
||||
scopeSession: ScopeSession,
|
||||
delegateField: FirField?,
|
||||
subClass: FirClass<*>
|
||||
subClass: FirClass<*>,
|
||||
declaredMemberScope: FirScope,
|
||||
): FirTypeScope = scopeForClassImpl(
|
||||
substitutor,
|
||||
useSiteSession,
|
||||
@@ -133,7 +135,7 @@ private fun FirClass<*>.scopeForSupertype(
|
||||
).let {
|
||||
if (delegateField != null) {
|
||||
scopeSession.getOrBuild(delegateField, DelegatedMemberScopeKey(delegateField.symbol.callableId)) {
|
||||
FirDelegatedMemberScope(it, useSiteSession, subClass, delegateField)
|
||||
FirDelegatedMemberScope(it, useSiteSession, subClass, delegateField, declaredMemberScope)
|
||||
}
|
||||
} else {
|
||||
it
|
||||
|
||||
+56
-5
@@ -6,13 +6,19 @@
|
||||
package org.jetbrains.kotlin.fir.scopes.impl
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.declarations.*
|
||||
import org.jetbrains.kotlin.fir.resolve.defaultType
|
||||
import org.jetbrains.kotlin.fir.scopes.FirTypeScope
|
||||
import org.jetbrains.kotlin.fir.scopes.ProcessorAction
|
||||
import org.jetbrains.kotlin.fir.scopes.*
|
||||
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
|
||||
import org.jetbrains.kotlin.fir.symbols.StandardClassIds
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.*
|
||||
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
|
||||
import org.jetbrains.kotlin.fir.types.ConeFlexibleType
|
||||
import org.jetbrains.kotlin.fir.types.coneType
|
||||
import org.jetbrains.kotlin.fir.types.isMarkedNullable
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
|
||||
class FirDelegatedMemberScope(
|
||||
@@ -20,22 +26,31 @@ class FirDelegatedMemberScope(
|
||||
private val session: FirSession,
|
||||
private val containingClass: FirClass<*>,
|
||||
private val delegateField: FirField,
|
||||
private val declaredMemberScope: FirScope,
|
||||
) : FirTypeScope() {
|
||||
private val delegatedFunctionCache = mutableMapOf<FirNamedFunctionSymbol, FirNamedFunctionSymbol>()
|
||||
private val delegatedPropertyCache = mutableMapOf<FirPropertySymbol, FirPropertySymbol>()
|
||||
private val dispatchReceiverType = containingClass.defaultType()
|
||||
private val overrideChecker = FirStandardOverrideChecker(session)
|
||||
|
||||
override fun processFunctionsByName(name: Name, processor: (FirFunctionSymbol<*>) -> Unit) {
|
||||
useSiteScope.processFunctionsByName(name) processor@{ functionSymbol ->
|
||||
if (functionSymbol !is FirNamedFunctionSymbol) {
|
||||
if (functionSymbol !is FirNamedFunctionSymbol || functionSymbol.fir.isPublicInAny()) {
|
||||
processor(functionSymbol)
|
||||
return@processor
|
||||
}
|
||||
|
||||
val original = functionSymbol.fir
|
||||
if (original.modality == Modality.FINAL) {
|
||||
if (original.modality == Modality.FINAL || original.visibility == Visibilities.Private) {
|
||||
processor(functionSymbol)
|
||||
return@processor
|
||||
}
|
||||
|
||||
if (declaredMemberScope.getFunctions(name).any { it is FirNamedFunctionSymbol && overrideChecker.isOverriddenFunction(it.fir, original) }) {
|
||||
processor(functionSymbol)
|
||||
return@processor
|
||||
}
|
||||
|
||||
val delegatedSymbol = delegatedFunctionCache.getOrPut(functionSymbol) {
|
||||
val newSymbol = FirNamedFunctionSymbol(
|
||||
functionSymbol.callableId,
|
||||
@@ -61,11 +76,19 @@ class FirDelegatedMemberScope(
|
||||
processor(propertySymbol)
|
||||
return@processor
|
||||
}
|
||||
|
||||
val original = propertySymbol.fir
|
||||
if (original.modality == Modality.FINAL) {
|
||||
|
||||
if (original.modality == Modality.FINAL || original.visibility == Visibilities.Private) {
|
||||
processor(propertySymbol)
|
||||
return@processor
|
||||
}
|
||||
|
||||
if (declaredMemberScope.getProperties(name).any { it is FirPropertySymbol && overrideChecker.isOverriddenProperty(it.fir, original) }) {
|
||||
processor(propertySymbol)
|
||||
return@processor
|
||||
}
|
||||
|
||||
val delegatedSymbol = delegatedPropertyCache.getOrPut(propertySymbol) {
|
||||
FirFakeOverrideGenerator.createCopyForFirProperty(
|
||||
FirPropertySymbol(
|
||||
@@ -133,3 +156,31 @@ class DelegatedWrapperData<D : FirCallableDeclaration<*>>(
|
||||
)
|
||||
var <D : FirCallableDeclaration<*>>
|
||||
D.delegatedWrapperData: DelegatedWrapperData<D>? by FirDeclarationDataRegistry.data(DelegatedWrapperDataKey)
|
||||
|
||||
|
||||
// From the definition of function interfaces in the Java specification (pt. 9.8):
|
||||
// "methods that are members of I that do not have the same signature as any public instance method of the class Object"
|
||||
// It means that if an interface declares `int hashCode()` then the method won't be taken into account when
|
||||
// checking if the interface is SAM.
|
||||
fun FirSimpleFunction.isPublicInAny(): Boolean {
|
||||
if (name.asString() !in PUBLIC_METHOD_NAMES_IN_ANY) return false
|
||||
|
||||
return when (name.asString()) {
|
||||
"hashCode", "toString" -> valueParameters.isEmpty()
|
||||
"equals" -> valueParameters.singleOrNull()?.hasTypeOf(StandardClassIds.Any, allowNullable = true) == true
|
||||
else -> error("Unexpected method name: $name")
|
||||
}
|
||||
}
|
||||
|
||||
fun FirValueParameter.hasTypeOf(classId: ClassId, allowNullable: Boolean): Boolean {
|
||||
val classLike = when (val type = returnTypeRef.coneType) {
|
||||
is ConeClassLikeType -> type
|
||||
is ConeFlexibleType -> type.upperBound as? ConeClassLikeType ?: return false
|
||||
else -> return false
|
||||
}
|
||||
|
||||
if (classLike.isMarkedNullable && !allowNullable) return false
|
||||
return classLike.lookupTag.classId == classId
|
||||
}
|
||||
|
||||
private val PUBLIC_METHOD_NAMES_IN_ANY = setOf("equals", "hashCode", "toString")
|
||||
|
||||
@@ -35,6 +35,14 @@ abstract class FirScope {
|
||||
open fun mayContainName(name: Name) = true
|
||||
}
|
||||
|
||||
fun FirScope.getFunctions(name: Name): List<FirFunctionSymbol<*>> = mutableListOf<FirFunctionSymbol<*>>().apply {
|
||||
processFunctionsByName(name, this::add)
|
||||
}
|
||||
|
||||
fun FirScope.getProperties(name: Name): List<FirVariableSymbol<*>> = mutableListOf<FirVariableSymbol<*>>().apply {
|
||||
processPropertiesByName(name, this::add)
|
||||
}
|
||||
|
||||
fun FirTypeScope.processOverriddenFunctionsAndSelf(
|
||||
functionSymbol: FirFunctionSymbol<*>,
|
||||
processor: (FirFunctionSymbol<*>) -> ProcessorAction
|
||||
|
||||
Reference in New Issue
Block a user