[AA] Refactoring: Simplify JavaClassDeclaredMembersEnhancementScope

- `JavaClassDeclaredMembersEnhancementScope` actually had nothing to do
  with Java enhancement, so it is easily replaceable with a more general
  `FirDeclaredMembersOnlyScope`.

^KT-61800
This commit is contained in:
Marco Pennekamp
2023-09-14 22:35:25 +02:00
committed by Space Team
parent 7a7a923197
commit 8b24baade9
4 changed files with 91 additions and 83 deletions
@@ -130,7 +130,7 @@ internal class KtFirScopeProvider(
}
return scopeSession.getOrBuild(firJavaClass.symbol, cacheKey) {
JavaClassDeclaredMembersEnhancementScope(analysisSession.useSiteSession, firJavaClass, firScope)
FirDeclaredMembersOnlyScope(firScope, firJavaClass)
}
}
@@ -0,0 +1,49 @@
/*
* Copyright 2010-2023 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.analysis.api.fir.scopes
import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope
import org.jetbrains.kotlin.fir.scopes.processAllCallables
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
import org.jetbrains.kotlin.name.Name
internal abstract class FirCallableFilteringScope(private val baseScope: FirContainingNamesAwareScope) : FirContainingNamesAwareScope() {
protected abstract fun isTargetCallable(callable: FirCallableSymbol<*>): Boolean
private val cachedCallableNames by lazy(LazyThreadSafetyMode.NONE) {
buildSet {
baseScope.processAllCallables { callable ->
if (isTargetCallable(callable)) {
add(callable.name)
}
}
}
}
override fun getCallableNames(): Set<Name> = cachedCallableNames
override fun processFunctionsByName(name: Name, processor: (FirNamedFunctionSymbol) -> Unit) {
if (!cachedCallableNames.contains(name)) return
baseScope.processFunctionsByName(name) { function ->
if (isTargetCallable(function)) {
processor(function)
}
}
}
override fun processPropertiesByName(name: Name, processor: (FirVariableSymbol<*>) -> Unit) {
if (!cachedCallableNames.contains(name)) return
baseScope.processPropertiesByName(name) { property ->
if (isTargetCallable(property)) {
processor(property)
}
}
}
}
@@ -0,0 +1,41 @@
/*
* Copyright 2010-2023 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.analysis.api.fir.scopes
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.utils.classId
import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor
import org.jetbrains.kotlin.fir.scopes.*
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
internal class FirDeclaredMembersOnlyScope(
private val delegate: FirContainingNamesAwareScope,
private val owner: FirClass,
) : FirCallableFilteringScope(delegate) {
private fun FirCallableDeclaration.isDeclared(): Boolean =
symbol.callableId.classId == owner.classId
&& origin !is FirDeclarationOrigin.SubstitutionOverride
&& origin != FirDeclarationOrigin.IntersectionOverride
override fun isTargetCallable(callable: FirCallableSymbol<*>): Boolean =
callable.callableId.callableName != SpecialNames.INIT && callable.fir.isDeclared()
override fun processDeclaredConstructors(processor: (FirConstructorSymbol) -> Unit) {
delegate.processDeclaredConstructors(processor)
}
override fun getClassifierNames(): Set<Name> = delegate.getClassifierNames()
override fun processClassifiersByNameWithSubstitution(name: Name, processor: (FirClassifierSymbol<*>, ConeSubstitutor) -> Unit) {
// We do not need to filter classifiers, because for both Kotlin and Java classes, a static base scope cannot contain classifiers
// inherited from supertypes. See `getStaticMemberScope` in `KtScopeProvider` for more information on classifiers in static scopes.
delegate.processClassifiersByNameWithSubstitution(name, processor)
}
override fun toString(): String = "Declared member scope for $delegate with owning class `${owner.classId}`"
}
@@ -1,82 +0,0 @@
/*
* Copyright 2010-2023 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.analysis.api.fir.scopes
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.utils.classId
import org.jetbrains.kotlin.fir.java.declarations.FirJavaClass
import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor
import org.jetbrains.kotlin.fir.scopes.*
import org.jetbrains.kotlin.fir.scopes.impl.nestedClassifierScope
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
internal class JavaClassDeclaredMembersEnhancementScope(
private val useSiteSession: FirSession,
private val owner: FirJavaClass,
private val useSiteMemberEnhancementScope: FirContainingNamesAwareScope
) : FirContainingNamesAwareScope() {
private fun FirCallableDeclaration.isDeclared(): Boolean {
return symbol.callableId.classId == owner.classId
&& origin !is FirDeclarationOrigin.SubstitutionOverride
&& origin != FirDeclarationOrigin.IntersectionOverride
}
private val callableNames = run {
(useSiteMemberEnhancementScope.collectAllProperties() + useSiteMemberEnhancementScope.collectAllFunctions()).filter {
it.fir.isDeclared()
}.map {
it.name
}.toSet()
}
private val nestedClassifierScope: FirContainingNamesAwareScope? =
useSiteSession.nestedClassifierScope(owner)
override fun getCallableNames(): Set<Name> {
return callableNames
}
override fun getClassifierNames(): Set<Name> {
return nestedClassifierScope?.getClassifierNames().orEmpty()
}
override fun processFunctionsByName(name: Name, processor: (FirNamedFunctionSymbol) -> Unit) {
if (name == SpecialNames.INIT) return
useSiteMemberEnhancementScope.processFunctionsByName(name) { symbol ->
if (symbol.fir.isDeclared()) {
processor(symbol)
}
}
}
override fun processClassifiersByNameWithSubstitution(
name: Name,
processor: (FirClassifierSymbol<*>, ConeSubstitutor) -> Unit
) {
nestedClassifierScope?.processClassifiersByNameWithSubstitution(name, processor)
}
override fun processDeclaredConstructors(processor: (FirConstructorSymbol) -> Unit) {
useSiteMemberEnhancementScope.processDeclaredConstructors { symbol ->
processor(symbol)
}
}
override fun processPropertiesByName(name: Name, processor: (FirVariableSymbol<*>) -> Unit) {
useSiteMemberEnhancementScope.processPropertiesByName(name) { symbol ->
if (symbol.fir.isDeclared()) {
processor(symbol)
}
}
}
override fun toString(): String {
return "Java enhancement declared member scope for ${owner.classId}"
}
}