Analysis API: simplify fir scope hierarchy

This commit is contained in:
Ilya Kirillov
2021-11-04 22:04:17 +01:00
parent 6453f2bdbf
commit d9fcea9451
5 changed files with 11 additions and 59 deletions
@@ -82,7 +82,7 @@ internal class KtFirScopeProvider(
) )
} ?: return@getOrPut getEmptyScope() } ?: return@getOrPut getEmptyScope()
KtFirMemberScope(firScope, token, builder) KtFirDelegatingScope(firScope, builder, token)
} }
} }
@@ -90,7 +90,7 @@ internal class KtFirScopeProvider(
val firScope = symbol.withFirForScope { fir -> val firScope = symbol.withFirForScope { fir ->
fir.scopeProvider.getStaticScope(fir, analysisSession.rootModuleSession, ScopeSession()) fir.scopeProvider.getStaticScope(fir, analysisSession.rootModuleSession, ScopeSession())
} ?: return getEmptyScope() } ?: return getEmptyScope()
return KtFirDelegatingScopeImpl(firScope, builder, token) return KtFirDelegatingScope(firScope, builder, token)
} }
override fun getDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtScope = withValidityAssertion { override fun getDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtScope = withValidityAssertion {
@@ -99,12 +99,12 @@ internal class KtFirScopeProvider(
analysisSession.rootModuleSession.declaredMemberScope(it) analysisSession.rootModuleSession.declaredMemberScope(it)
} ?: return@getOrPut getEmptyScope() } ?: return@getOrPut getEmptyScope()
KtFirDeclaredMemberScope(firScope, token, builder) KtFirDelegatingScope(firScope, builder, token)
} }
} }
override fun getDelegatedMemberScope(classSymbol: KtSymbolWithMembers): KtScope = withValidityAssertion { override fun getDelegatedMemberScope(classSymbol: KtSymbolWithMembers): KtScope = withValidityAssertion {
val declaredScope = (getDeclaredMemberScope(classSymbol) as? KtFirDeclaredMemberScope)?.firScope val declaredScope = (getDeclaredMemberScope(classSymbol) as? KtFirDelegatingScope)?.firScope
?: return delegatedMemberScopeCache.getOrPut(classSymbol) { getEmptyScope() } ?: return delegatedMemberScopeCache.getOrPut(classSymbol) { getEmptyScope() }
delegatedMemberScopeCache.getOrPut(classSymbol) { delegatedMemberScopeCache.getOrPut(classSymbol) {
val firScope = classSymbol.withFirForScope { fir -> val firScope = classSymbol.withFirForScope { fir ->
@@ -219,15 +219,9 @@ internal class KtFirScopeProvider(
GlobalSearchScope.allScope(project), // todo GlobalSearchScope.allScope(project), // todo
analysisSession.targetPlatform analysisSession.targetPlatform
) )
is FirContainingNamesAwareScope -> KtFirDelegatingScopeImpl(firScope, builder, token) is FirContainingNamesAwareScope -> KtFirDelegatingScope(firScope, builder, token)
is FirMemberTypeParameterScope -> KtFirDelegatingScopeImpl(firScope, builder, token) is FirMemberTypeParameterScope -> KtFirDelegatingScope(firScope, builder, token)
else -> TODO(firScope::class.toString()) else -> TODO(firScope::class.toString())
} }
} }
} }
private class KtFirDelegatingScopeImpl<S : FirContainingNamesAwareScope>(
override val firScope: S,
builder: KtSymbolByFirBuilder,
token: ValidityToken
) : KtFirDelegatingScope<S>(builder, token), ValidityTokenOwner
@@ -1,16 +0,0 @@
/*
* 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.tokens.ValidityToken
import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope
internal class KtFirDeclaredMemberScope(
override val firScope: FirContainingNamesAwareScope,
token: ValidityToken,
builder: KtSymbolByFirBuilder
) : KtFirDelegatingScope<FirContainingNamesAwareScope>(builder, token)
@@ -13,10 +13,10 @@ import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope
internal class KtFirDelegatedMemberScope( internal class KtFirDelegatedMemberScope(
override val firScope: FirContainingNamesAwareScope, firScope: FirContainingNamesAwareScope,
token: ValidityToken, token: ValidityToken,
builder: KtSymbolByFirBuilder builder: KtSymbolByFirBuilder
) : KtFirDelegatingScope<FirContainingNamesAwareScope>(builder, token) { ) : KtFirDelegatingScope(firScope, builder, token) {
override fun getCallableSymbols(nameFilter: KtScopeNameFilter): Sequence<KtCallableSymbol> { override fun getCallableSymbols(nameFilter: KtScopeNameFilter): Sequence<KtCallableSymbol> {
return super.getCallableSymbols(nameFilter).filter { it.origin == KtSymbolOrigin.DELEGATED } return super.getCallableSymbols(nameFilter).filter { it.origin == KtSymbolOrigin.DELEGATED }
@@ -22,13 +22,11 @@ import org.jetbrains.kotlin.analysis.api.symbols.KtPackageSymbol
import org.jetbrains.kotlin.analysis.api.withValidityAssertion import org.jetbrains.kotlin.analysis.api.withValidityAssertion
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
internal abstract class KtFirDelegatingScope<S : FirContainingNamesAwareScope>( internal open class KtFirDelegatingScope(
val firScope: FirContainingNamesAwareScope,
private val builder: KtSymbolByFirBuilder, private val builder: KtSymbolByFirBuilder,
final override val token: ValidityToken final override val token: ValidityToken
) : KtScope { ) : KtScope {
abstract val firScope: S
private val allNamesCached by cached { private val allNamesCached by cached {
getPossibleCallableNames() + getPossibleClassifierNames() getPossibleCallableNames() + getPossibleClassifierNames()
} }
@@ -84,13 +82,6 @@ internal fun FirScope.getCallableSymbols(callableNames: Collection<Name>, builde
} }
} }
internal class KtFirDelegatingScopeImpl<S : FirContainingNamesAwareScope>(
override val firScope: S,
builder: KtSymbolByFirBuilder,
token: ValidityToken
) : KtFirDelegatingScope<S>(builder, token)
internal fun FirScope.getClassifierSymbols(classLikeNames: Collection<Name>, builder: KtSymbolByFirBuilder): Sequence<KtClassifierSymbol> = internal fun FirScope.getClassifierSymbols(classLikeNames: Collection<Name>, builder: KtSymbolByFirBuilder): Sequence<KtClassifierSymbol> =
sequence { sequence {
classLikeNames.forEach { name -> classLikeNames.forEach { name ->
@@ -1,17 +0,0 @@
/*
* 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.ValidityTokenOwner
import org.jetbrains.kotlin.analysis.api.fir.KtSymbolByFirBuilder
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.fir.scopes.FirTypeScope
internal class KtFirMemberScope(
override val firScope: FirTypeScope,
token: ValidityToken,
builder: KtSymbolByFirBuilder
) : KtFirDelegatingScope<FirTypeScope>(builder, token), ValidityTokenOwner