[Analysis API FIR] refactoring, extract base part of KtFirDelegatingScope into a separate class

For further reuse
This commit is contained in:
Ilya Kirillov
2023-05-12 13:49:21 +02:00
committed by Space Team
parent 41976a2932
commit 55bbc5eb02
5 changed files with 50 additions and 34 deletions
@@ -88,7 +88,7 @@ internal class KtFirScopeProvider(
)
}?.applyIf(classSymbol is KtEnumEntrySymbol, ::EnumEntryContainingNamesAwareScope)
?: return getEmptyScope()
return KtFirDelegatingScope(firScope, builder)
return KtFirDelegatingNamesAwareScope(firScope, builder)
}
override fun getStaticMemberScope(symbol: KtSymbolWithMembers): KtScope {
@@ -100,13 +100,13 @@ internal class KtFirScopeProvider(
getScopeSession(),
)
} ?: return getEmptyScope()
return KtFirDelegatingScope(firScope, builder)
return KtFirDelegatingNamesAwareScope(firScope, builder)
}
override fun getDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtScope {
val useSiteSession = analysisSession.useSiteSession
if (classSymbol is KtFirScriptSymbol) {
return KtFirDelegatingScope(
return KtFirDelegatingNamesAwareScope(
FirScriptDeclarationsScope(useSiteSession, classSymbol.firSymbol.fir),
builder
)
@@ -117,11 +117,11 @@ internal class KtFirScopeProvider(
else -> useSiteSession.declaredMemberScope(it)
}
} ?: return getEmptyScope()
return KtFirDelegatingScope(firScope, builder)
return KtFirDelegatingNamesAwareScope(firScope, builder)
}
override fun getDelegatedMemberScope(classSymbol: KtSymbolWithMembers): KtScope {
val declaredScope = (getDeclaredMemberScope(classSymbol) as? KtFirDelegatingScope)?.firScope
val declaredScope = (getDeclaredMemberScope(classSymbol) as? KtFirDelegatingNamesAwareScope)?.firScope
?: return getEmptyScope()
val firScope = classSymbol.withFirForScope { fir ->
fir.lazyResolveToPhase(FirResolvePhase.STATUS)
@@ -233,7 +233,7 @@ internal class KtFirScopeProvider(
is FirAbstractSimpleImportingScope -> KtFirNonStarImportingScope(firScope, builder)
is FirAbstractStarImportingScope -> KtFirStarImportingScope(firScope, builder, analysisSession.useSiteScopeDeclarationProvider)
is FirPackageMemberScope -> createPackageScope(firScope.fqName)
is FirContainingNamesAwareScope -> KtFirDelegatingScope(firScope, builder)
is FirContainingNamesAwareScope -> KtFirDelegatingNamesAwareScope(firScope, builder)
else -> TODO(firScope::class.toString())
}
}
@@ -8,7 +8,7 @@ package org.jetbrains.kotlin.analysis.api.fir.components
import org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals
import org.jetbrains.kotlin.analysis.api.components.KtScopeSubstitution
import org.jetbrains.kotlin.analysis.api.fir.KtFirAnalysisSession
import org.jetbrains.kotlin.analysis.api.fir.scopes.KtFirDelegatingScope
import org.jetbrains.kotlin.analysis.api.fir.scopes.KtFirDelegatingNamesAwareScope
import org.jetbrains.kotlin.analysis.api.fir.scopes.KtFirDelegatingTypeScope
import org.jetbrains.kotlin.analysis.api.impl.base.scopes.KtCompositeScope
import org.jetbrains.kotlin.analysis.api.impl.base.scopes.KtCompositeTypeScope
@@ -23,7 +23,7 @@ internal class KtFirScopeSubstitution(
@OptIn(KtAnalysisApiInternals::class)
override fun getDeclarationScope(scope: KtTypeScope): KtScope {
return when (scope) {
is KtFirDelegatingTypeScope -> KtFirDelegatingScope(scope.firScope, analysisSession.firSymbolBuilder)
is KtFirDelegatingTypeScope -> KtFirDelegatingNamesAwareScope(scope.firScope, analysisSession.firSymbolBuilder)
is KtCompositeTypeScope -> KtCompositeScope(scope.subScopes.map(::getDeclarationScope), token)
else -> unexpectedElementError<KtTypeScope>(scope)
}
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.analysis.api.fir.scopes
import org.jetbrains.kotlin.analysis.api.fir.KtSymbolByFirBuilder
import org.jetbrains.kotlin.analysis.api.fir.utils.cached
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.scopes.KtScope
@@ -15,28 +14,14 @@ import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtClassifierSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtConstructorSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtPackageSymbol
import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope
import org.jetbrains.kotlin.fir.scopes.FirScope
import org.jetbrains.kotlin.name.Name
internal open class KtFirDelegatingScope(
val firScope: FirContainingNamesAwareScope,
private val builder: KtSymbolByFirBuilder,
internal abstract class KtFirBasedScope<S : FirScope>(
protected val firScope: S,
protected val builder: KtSymbolByFirBuilder,
) : KtScope {
override val token: KtLifetimeToken get() = builder.token
private val allNamesCached by cached {
getPossibleCallableNames() + getPossibleClassifierNames()
}
override fun getAllPossibleNames(): Set<Name> = withValidityAssertion { allNamesCached }
override fun getPossibleCallableNames(): Set<Name> = withValidityAssertion {
firScope.getCallableNames()
}
override fun getPossibleClassifierNames(): Set<Name> = withValidityAssertion {
firScope.getClassifierNames()
}
final override val token: KtLifetimeToken get() = builder.token
override fun getCallableSymbols(nameFilter: KtScopeNameFilter): Sequence<KtCallableSymbol> = withValidityAssertion {
firScope.getCallableSymbols(getPossibleCallableNames().filter(nameFilter), builder)
@@ -61,10 +46,5 @@ internal open class KtFirDelegatingScope(
override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence<KtPackageSymbol> = withValidityAssertion {
emptySequence()
}
override fun mayContainName(name: Name): Boolean = withValidityAssertion {
name in getAllPossibleNames()
}
}
@@ -16,7 +16,7 @@ import org.jetbrains.kotlin.name.Name
internal class KtFirDelegatedMemberScope(
firScope: FirContainingNamesAwareScope,
builder: KtSymbolByFirBuilder
) : KtFirDelegatingScope(firScope, builder) {
) : KtFirDelegatingNamesAwareScope(firScope, builder) {
override fun getCallableSymbols(nameFilter: KtScopeNameFilter): Sequence<KtCallableSymbol> = withValidityAssertion {
return super.getCallableSymbols(nameFilter).filter { it.origin == KtSymbolOrigin.DELEGATED }
@@ -0,0 +1,36 @@
/*
* 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.analysis.api.fir.scopes
import org.jetbrains.kotlin.analysis.api.fir.KtSymbolByFirBuilder
import org.jetbrains.kotlin.analysis.api.fir.utils.cached
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope
import org.jetbrains.kotlin.name.Name
internal open class KtFirDelegatingNamesAwareScope(
firScope: FirContainingNamesAwareScope,
builder: KtSymbolByFirBuilder,
) : KtFirBasedScope<FirContainingNamesAwareScope>(firScope, builder) {
private val allNamesCached by cached {
getPossibleCallableNames() + getPossibleClassifierNames()
}
override fun getAllPossibleNames(): Set<Name> = withValidityAssertion { allNamesCached }
override fun getPossibleCallableNames(): Set<Name> = withValidityAssertion {
firScope.getCallableNames()
}
override fun getPossibleClassifierNames(): Set<Name> = withValidityAssertion {
firScope.getClassifierNames()
}
override fun mayContainName(name: Name): Boolean = withValidityAssertion {
name in getAllPossibleNames()
}
}