Analysis API: simplify scope hierarchy

This commit is contained in:
Ilya Kirillov
2021-11-04 21:57:34 +01:00
parent 0dca176e28
commit 6453f2bdbf
18 changed files with 133 additions and 202 deletions
@@ -20,12 +20,12 @@ import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.bas
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.psiBased.base.KtFe10PsiSymbol
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.psiBased.base.getResolutionScope
import org.jetbrains.kotlin.analysis.api.descriptors.types.base.KtFe10Type
import org.jetbrains.kotlin.analysis.api.impl.base.scopes.SimpleKtCompositeScope
import org.jetbrains.kotlin.analysis.api.impl.base.scopes.KtEmptyScope
import org.jetbrains.kotlin.analysis.api.impl.base.scopes.KtCompositeScope
import org.jetbrains.kotlin.analysis.api.scopes.*
import org.jetbrains.kotlin.analysis.api.symbols.KtFileSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtPackageSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbol
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithDeclarations
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithMembers
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.api.types.KtType
@@ -48,58 +48,47 @@ internal class KtFe10ScopeProvider(override val analysisSession: KtFe10AnalysisS
override val token: ValidityToken
get() = analysisSession.token
override fun getMemberScope(classSymbol: KtSymbolWithMembers): KtMemberScope = withValidityAssertion {
override fun getMemberScope(classSymbol: KtSymbolWithMembers): KtScope = withValidityAssertion {
val descriptor = getDescriptor<ClassDescriptor>(classSymbol)
?: return object : KtFe10EmptyScope(token), KtMemberScope {
override val owner get() = classSymbol
}
?: return getEmptyScope()
// TODO either this or declared scope should return a different set of members
return object : KtFe10ScopeMember(descriptor.unsubstitutedMemberScope, analysisSession), KtMemberScope {
override val owner get() = classSymbol
}
return KtFe10ScopeMember(descriptor.unsubstitutedMemberScope, analysisSession)
}
override fun getDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtDeclaredMemberScope = withValidityAssertion {
override fun getDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtScope = withValidityAssertion {
val descriptor = getDescriptor<ClassDescriptor>(classSymbol)
?: return object : KtFe10EmptyScope(token), KtDeclaredMemberScope {
override val owner get() = classSymbol
}
?: return getEmptyScope()
// TODO: need to return declared members only
return object : KtFe10ScopeMember(descriptor.unsubstitutedMemberScope, analysisSession), KtDeclaredMemberScope {
override val owner get() = classSymbol
}
return KtFe10ScopeMember(descriptor.unsubstitutedMemberScope, analysisSession)
}
override fun getDelegatedMemberScope(classSymbol: KtSymbolWithMembers): KtDelegatedMemberScope = withValidityAssertion {
override fun getDelegatedMemberScope(classSymbol: KtSymbolWithMembers): KtScope = withValidityAssertion {
val descriptor = getDescriptor<ClassDescriptor>(classSymbol)
?: return object : KtFe10EmptyScope(token), KtDelegatedMemberScope {
override val owner get() = classSymbol
}
?: return getEmptyScope()
// TODO: need to return delegated members only
return object : KtFe10ScopeMember(descriptor.unsubstitutedMemberScope, analysisSession), KtDelegatedMemberScope {
override val owner get() = classSymbol
}
return KtFe10ScopeMember(descriptor.unsubstitutedMemberScope, analysisSession)
}
override fun getStaticMemberScope(symbol: KtSymbolWithMembers): KtScope = withValidityAssertion {
val descriptor = getDescriptor<ClassDescriptor>(symbol) ?: return KtFe10EmptyScope(token)
val descriptor = getDescriptor<ClassDescriptor>(symbol) ?: return getEmptyScope()
return KtFe10ScopeMember(descriptor.staticScope, analysisSession)
}
override fun getFileScope(fileSymbol: KtFileSymbol): KtDeclarationScope<KtSymbolWithDeclarations> = withValidityAssertion {
override fun getEmptyScope(): KtScope = withValidityAssertion {
KtEmptyScope(token)
}
override fun getFileScope(fileSymbol: KtFileSymbol): KtScope = withValidityAssertion {
require(fileSymbol is KtFe10FileSymbol)
val scope = analysisSession.resolveSession.fileScopeProvider.getFileResolutionScope(fileSymbol.psi)
return object : KtFe10ScopeLexical(scope, analysisSession), KtDeclarationScope<KtSymbolWithDeclarations> {
override val owner: KtSymbolWithDeclarations
get() = withValidityAssertion { fileSymbol }
}
return KtFe10ScopeLexical(scope, analysisSession)
}
override fun getPackageScope(packageSymbol: KtPackageSymbol): KtPackageScope = withValidityAssertion {
override fun getPackageScope(packageSymbol: KtPackageSymbol): KtScope = withValidityAssertion {
require(packageSymbol is KtFe10PackageSymbol)
val packageFragments = analysisSession.resolveSession.packageFragmentProvider.packageFragments(packageSymbol.fqName)
val scopeDescription = "Compound scope for package \"${packageSymbol.fqName}\""
@@ -107,8 +96,8 @@ internal class KtFe10ScopeProvider(override val analysisSession: KtFe10AnalysisS
return KtFe10PackageScope(chainedScope, packageSymbol, analysisSession)
}
override fun getCompositeScope(subScopes: List<KtScope>): KtCompositeScope = withValidityAssertion {
return SimpleKtCompositeScope(subScopes, token)
override fun getCompositeScope(subScopes: List<KtScope>): KtScope = withValidityAssertion {
return KtCompositeScope(subScopes, token)
}
override fun getTypeScope(type: KtType): KtScope = withValidityAssertion {
@@ -122,12 +111,12 @@ internal class KtFe10ScopeProvider(override val analysisSession: KtFe10AnalysisS
val lexicalScope = positionInFakeFile.getResolutionScope(bindingContext)
if (lexicalScope != null) {
val compositeScope = SimpleKtCompositeScope(listOf(KtFe10ScopeLexical(lexicalScope, analysisSession)), token)
val compositeScope = KtCompositeScope(listOf(KtFe10ScopeLexical(lexicalScope, analysisSession)), token)
return KtScopeContext(compositeScope, collectImplicitReceivers(lexicalScope))
}
val fileScope = analysisSession.resolveSession.fileScopeProvider.getFileResolutionScope(originalFile)
val compositeScope = SimpleKtCompositeScope(listOf(KtFe10ScopeLexical(fileScope, analysisSession)), token)
val compositeScope = KtCompositeScope(listOf(KtFe10ScopeLexical(fileScope, analysisSession)), token)
return KtScopeContext(compositeScope, collectImplicitReceivers(fileScope))
}
@@ -7,22 +7,17 @@ package org.jetbrains.kotlin.analysis.api.descriptors.scopes
import org.jetbrains.kotlin.analysis.api.descriptors.KtFe10AnalysisSession
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.KtFe10PackageSymbol
import org.jetbrains.kotlin.analysis.api.scopes.KtPackageScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
import org.jetbrains.kotlin.analysis.api.symbols.KtConstructorSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtPackageSymbol
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.scopes.MemberScope
internal class KtFe10PackageScope(
scope: MemberScope,
private val owner: KtPackageSymbol,
analysisSession: KtFe10AnalysisSession
) : KtFe10ScopeMember(scope, analysisSession), KtPackageScope {
override val fqName: FqName
get() = withValidityAssertion { owner.fqName }
) : KtFe10ScopeMember(scope, analysisSession) {
override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence<KtPackageSymbol> = withValidityAssertion {
val packageFragmentProvider = analysisSession.resolveSession.packageFragmentProvider
return packageFragmentProvider.getSubPackagesOf(owner.fqName, nameFilter)
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
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.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
@@ -51,11 +52,15 @@ internal abstract class KtFe10ScopeResolution : KtScope, ValidityTokenOwner {
.map { it.toKtConstructorSymbol(analysisSession) }
}
override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence<KtPackageSymbol> = withValidityAssertion {
emptySequence()
}
override val token: ValidityToken
get() = analysisSession.token
}
internal open class KtFe10ScopeLexical(
internal class KtFe10ScopeLexical(
override val scope: LexicalScope,
override val analysisSession: KtFe10AnalysisSession
) : KtFe10ScopeResolution(), ValidityTokenOwner {
@@ -71,7 +76,8 @@ internal open class KtFe10ScopeLexical(
internal open class KtFe10ScopeMember(
override val scope: MemberScope,
override val analysisSession: KtFe10AnalysisSession
) : KtFe10ScopeResolution(), ValidityTokenOwner {
) : KtFe10ScopeResolution() {
override fun getPossibleCallableNames(): Set<Name> = withValidityAssertion {
return scope.getFunctionNames() + scope.getVariableNames()
}
@@ -29,11 +29,11 @@ import org.jetbrains.kotlin.analysis.api.fir.scopes.*
import org.jetbrains.kotlin.analysis.api.fir.symbols.*
import org.jetbrains.kotlin.analysis.api.fir.types.KtFirType
import org.jetbrains.kotlin.analysis.api.fir.utils.weakRef
import org.jetbrains.kotlin.analysis.api.impl.base.scopes.SimpleKtCompositeScope
import org.jetbrains.kotlin.analysis.api.impl.base.scopes.KtEmptyScope
import org.jetbrains.kotlin.analysis.api.impl.base.scopes.KtCompositeScope
import org.jetbrains.kotlin.analysis.api.scopes.*
import org.jetbrains.kotlin.analysis.api.symbols.KtFileSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtPackageSymbol
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithDeclarations
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithMembers
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.api.types.KtType
@@ -54,11 +54,11 @@ internal class KtFirScopeProvider(
private val builder by weakRef(builder)
private val firResolveState by weakRef(firResolveState)
private val memberScopeCache = IdentityHashMap<KtSymbolWithMembers, KtMemberScope>()
private val declaredMemberScopeCache = IdentityHashMap<KtSymbolWithMembers, KtDeclaredMemberScope>()
private val delegatedMemberScopeCache = IdentityHashMap<KtSymbolWithMembers, KtDelegatedMemberScope>()
private val fileScopeCache = IdentityHashMap<KtFileSymbol, KtDeclarationScope<KtSymbolWithDeclarations>>()
private val packageMemberScopeCache = IdentityHashMap<KtPackageSymbol, KtPackageScope>()
private val memberScopeCache = IdentityHashMap<KtSymbolWithMembers, KtScope>()
private val declaredMemberScopeCache = IdentityHashMap<KtSymbolWithMembers, KtScope>()
private val delegatedMemberScopeCache = IdentityHashMap<KtSymbolWithMembers, KtScope>()
private val fileScopeCache = IdentityHashMap<KtFileSymbol, KtScope>()
private val packageMemberScopeCache = IdentityHashMap<KtPackageSymbol, KtScope>()
private inline fun <T> KtSymbolWithMembers.withFirForScope(crossinline body: (FirClass) -> T): T? = when (this) {
is KtFirNamedClassOrObjectSymbol -> firRef.withFir(FirResolvePhase.TYPES, body)
@@ -71,9 +71,8 @@ internal class KtFirScopeProvider(
else -> error { "Unknown KtSymbolWithDeclarations implementation ${this::class.qualifiedName}" }
}
override fun getMemberScope(classSymbol: KtSymbolWithMembers): KtMemberScope = withValidityAssertion {
override fun getMemberScope(classSymbol: KtSymbolWithMembers): KtScope = withValidityAssertion {
memberScopeCache.getOrPut(classSymbol) {
val firScope = classSymbol.withFirForScope { fir ->
val firSession = analysisSession.rootModuleSession
fir.unsubstitutedScope(
@@ -81,32 +80,32 @@ internal class KtFirScopeProvider(
ScopeSession(),
withForcedTypeCalculator = false
)
} ?: return@getOrPut KtFirEmptyMemberScope(classSymbol)
} ?: return@getOrPut getEmptyScope()
KtFirMemberScope(classSymbol, firScope, token, builder)
KtFirMemberScope(firScope, token, builder)
}
}
override fun getStaticMemberScope(symbol: KtSymbolWithMembers): KtScope {
val firScope = symbol.withFirForScope { fir ->
fir.scopeProvider.getStaticScope(fir, analysisSession.rootModuleSession, ScopeSession())
} ?: return KtFirEmptyMemberScope(symbol)
} ?: return getEmptyScope()
return KtFirDelegatingScopeImpl(firScope, builder, token)
}
override fun getDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtDeclaredMemberScope = withValidityAssertion {
override fun getDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtScope = withValidityAssertion {
declaredMemberScopeCache.getOrPut(classSymbol) {
val firScope = classSymbol.withFirForScope {
analysisSession.rootModuleSession.declaredMemberScope(it)
} ?: return@getOrPut KtFirEmptyMemberScope(classSymbol)
} ?: return@getOrPut getEmptyScope()
KtFirDeclaredMemberScope(classSymbol, firScope, token, builder)
KtFirDeclaredMemberScope(firScope, token, builder)
}
}
override fun getDelegatedMemberScope(classSymbol: KtSymbolWithMembers): KtDelegatedMemberScope = withValidityAssertion {
override fun getDelegatedMemberScope(classSymbol: KtSymbolWithMembers): KtScope = withValidityAssertion {
val declaredScope = (getDeclaredMemberScope(classSymbol) as? KtFirDeclaredMemberScope)?.firScope
?: return delegatedMemberScopeCache.getOrPut(classSymbol) { KtFirEmptyMemberScope(classSymbol) }
?: return delegatedMemberScopeCache.getOrPut(classSymbol) { getEmptyScope() }
delegatedMemberScopeCache.getOrPut(classSymbol) {
val firScope = classSymbol.withFirForScope { fir ->
val delegateFields = fir.delegateFields
@@ -119,20 +118,24 @@ internal class KtFirScopeProvider(
delegateFields
)
} else null
} ?: return@getOrPut KtFirEmptyMemberScope(classSymbol)
} ?: return@getOrPut getEmptyScope()
KtFirDelegatedMemberScope(classSymbol, firScope, token, builder)
KtFirDelegatedMemberScope(firScope, token, builder)
}
}
override fun getFileScope(fileSymbol: KtFileSymbol): KtDeclarationScope<KtSymbolWithDeclarations> = withValidityAssertion {
override fun getFileScope(fileSymbol: KtFileSymbol): KtScope = withValidityAssertion {
fileScopeCache.getOrPut(fileSymbol) {
check(fileSymbol is KtFirFileSymbol) { "KtFirScopeProvider can only work with KtFirFileSymbol, but ${fileSymbol::class} was provided" }
KtFirFileScope(fileSymbol, token, builder)
}
}
override fun getPackageScope(packageSymbol: KtPackageSymbol): KtPackageScope = withValidityAssertion {
override fun getEmptyScope(): KtScope = withValidityAssertion {
KtEmptyScope(token)
}
override fun getPackageScope(packageSymbol: KtPackageSymbol): KtScope = withValidityAssertion {
packageMemberScopeCache.getOrPut(packageSymbol) {
KtFirPackageScope(
packageSymbol.fqName,
@@ -146,8 +149,8 @@ internal class KtFirScopeProvider(
}
override fun getCompositeScope(subScopes: List<KtScope>): KtCompositeScope = withValidityAssertion {
SimpleKtCompositeScope(subScopes, token)
override fun getCompositeScope(subScopes: List<KtScope>): KtScope = withValidityAssertion {
KtCompositeScope(subScopes, token)
}
override fun getTypeScope(type: KtType): KtScope? {
@@ -5,18 +5,12 @@
package org.jetbrains.kotlin.analysis.api.fir.scopes
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.api.ValidityTokenOwner
import org.jetbrains.kotlin.analysis.api.fir.KtSymbolByFirBuilder
import org.jetbrains.kotlin.analysis.api.scopes.KtDeclaredMemberScope
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithMembers
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope
internal class KtFirDeclaredMemberScope(
override val owner: KtSymbolWithMembers,
override val firScope: FirContainingNamesAwareScope,
token: ValidityToken,
builder: KtSymbolByFirBuilder
) : KtFirDelegatingScope<FirContainingNamesAwareScope>(builder, token),
KtDeclaredMemberScope,
ValidityTokenOwner
) : KtFirDelegatingScope<FirContainingNamesAwareScope>(builder, token)
@@ -5,24 +5,18 @@
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.scopes.KtDelegatedMemberScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbolOrigin
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithMembers
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope
internal class KtFirDelegatedMemberScope(
override val owner: KtSymbolWithMembers,
override val firScope: FirContainingNamesAwareScope,
token: ValidityToken,
builder: KtSymbolByFirBuilder
) : KtFirDelegatingScope<FirContainingNamesAwareScope>(builder, token),
KtDelegatedMemberScope,
ValidityTokenOwner {
) : KtFirDelegatingScope<FirContainingNamesAwareScope>(builder, token) {
override fun getCallableSymbols(nameFilter: KtScopeNameFilter): Sequence<KtCallableSymbol> {
return super.getCallableSymbols(nameFilter).filter { it.origin == KtSymbolOrigin.DELEGATED }
@@ -18,6 +18,7 @@ import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
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.analysis.api.withValidityAssertion
import org.jetbrains.kotlin.name.Name
@@ -54,6 +55,10 @@ internal abstract class KtFirDelegatingScope<S : FirContainingNamesAwareScope>(
firScope.getConstructors(builder)
}
override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence<KtPackageSymbol> = withValidityAssertion {
emptySequence()
}
override fun mayContainName(name: Name): Boolean = withValidityAssertion {
name in getAllPossibleNames()
}
@@ -1,40 +0,0 @@
/*
* Copyright 2010-2021 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.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.api.ValidityTokenOwner
import org.jetbrains.kotlin.analysis.api.scopes.KtDeclaredMemberScope
import org.jetbrains.kotlin.analysis.api.scopes.KtDelegatedMemberScope
import org.jetbrains.kotlin.analysis.api.scopes.KtMemberScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
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.markers.KtSymbolWithMembers
import org.jetbrains.kotlin.name.Name
internal class KtFirEmptyMemberScope(
override val owner: KtSymbolWithMembers
) : KtMemberScope, KtDeclaredMemberScope, KtDelegatedMemberScope, ValidityTokenOwner {
override fun getPossibleCallableNames(): Set<Name> = emptySet()
override fun getPossibleClassifierNames(): Set<Name> = emptySet()
override fun getCallableSymbols(nameFilter: KtScopeNameFilter): Sequence<KtCallableSymbol> =
emptySequence()
override fun getClassifierSymbols(nameFilter: KtScopeNameFilter): Sequence<KtClassifierSymbol> =
emptySequence()
override fun getConstructors(): Sequence<KtConstructorSymbol> =
emptySequence()
override fun mayContainName(name: Name): Boolean = false
override val token: ValidityToken
get() = owner.token
}
@@ -9,25 +9,23 @@ import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.api.ValidityTokenOwner
import org.jetbrains.kotlin.analysis.api.fir.KtSymbolByFirBuilder
import org.jetbrains.kotlin.analysis.api.fir.symbols.KtFirFileSymbol
import org.jetbrains.kotlin.analysis.api.fir.utils.cached
import org.jetbrains.kotlin.analysis.api.scopes.KtDeclarationScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
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.markers.KtSymbolWithDeclarations
import org.jetbrains.kotlin.analysis.api.symbols.KtPackageSymbol
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
import org.jetbrains.kotlin.name.Name
internal class KtFirFileScope(
override val owner: KtFirFileSymbol,
private val owner: KtFirFileSymbol,
override val token: ValidityToken,
private val builder: KtSymbolByFirBuilder
) : KtDeclarationScope<KtSymbolWithDeclarations>,
ValidityTokenOwner {
) : KtScope {
private val allNamesCached by cached {
_callableNames + _classifierNames
@@ -95,4 +93,8 @@ internal class KtFirFileScope(
}
override fun getConstructors(): Sequence<KtConstructorSymbol> = emptySequence()
override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence<KtPackageSymbol> = withValidityAssertion {
emptySequence()
}
}
@@ -5,17 +5,13 @@
package org.jetbrains.kotlin.analysis.api.fir.scopes
import org.jetbrains.kotlin.fir.scopes.FirTypeScope
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.api.ValidityTokenOwner
import org.jetbrains.kotlin.analysis.api.fir.KtSymbolByFirBuilder
import org.jetbrains.kotlin.analysis.api.fir.utils.weakRef
import org.jetbrains.kotlin.analysis.api.scopes.KtMemberScope
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithMembers
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.fir.scopes.FirTypeScope
internal class KtFirMemberScope(
override val owner: KtSymbolWithMembers,
override val firScope: FirTypeScope,
token: ValidityToken,
builder: KtSymbolByFirBuilder
) : KtFirDelegatingScope<FirTypeScope>(builder, token), KtMemberScope, ValidityTokenOwner
) : KtFirDelegatingScope<FirTypeScope>(builder, token), ValidityTokenOwner
@@ -5,7 +5,6 @@
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.fir.utils.cached
import org.jetbrains.kotlin.analysis.api.scopes.KtNonStarImportingScope
@@ -26,7 +25,7 @@ internal class KtFirNonStarImportingScope(
private val firScope: FirAbstractSimpleImportingScope,
private val builder: KtSymbolByFirBuilder,
override val token: ValidityToken,
) : KtNonStarImportingScope, ValidityTokenOwner {
) : KtNonStarImportingScope {
@OptIn(ExperimentalStdlibApi::class)
override val imports: List<NonStarImport> by cached {
@@ -9,10 +9,11 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.analysis.api.fir.KtSymbolByFirBuilder
import org.jetbrains.kotlin.analysis.api.scopes.KtPackageScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
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.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
@@ -25,13 +26,13 @@ import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.platform.jvm.isJvm
internal class KtFirPackageScope(
override val fqName: FqName,
private val fqName: FqName,
private val project: Project,
private val builder: KtSymbolByFirBuilder,
override val token: ValidityToken,
private val searchScope: GlobalSearchScope,
private val targetPlatform: TargetPlatform,
) : KtPackageScope {
) : KtScope {
private val declarationsProvider = project.createDeclarationProvider(searchScope)
private val packageProvider = project.createPackageProvider(searchScope)
@@ -66,6 +67,10 @@ internal class KtFirPackageScope(
firScope.getClassifierSymbols(getPossibleClassifierNames().filter(nameFilter), builder)
}
override fun getConstructors(): Sequence<KtConstructorSymbol> = withValidityAssertion {
emptySequence()
}
override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence<KtPackageSymbol> = withValidityAssertion {
sequence {
if (targetPlatform.isJvm()) {
@@ -5,23 +5,17 @@
package org.jetbrains.kotlin.analysis.api.impl.base.scopes
import org.jetbrains.kotlin.analysis.api.ValidityTokenOwner
import org.jetbrains.kotlin.analysis.api.scopes.KtCompositeScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
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.KtSymbol
import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
import org.jetbrains.kotlin.name.Name
@OptIn(ExperimentalStdlibApi::class)
class SimpleKtCompositeScope(
override val subScopes: List<KtScope>,
class KtCompositeScope(
private val subScopes: List<KtScope>,
override val token: ValidityToken
) : KtCompositeScope, ValidityTokenOwner {
) : KtScope {
override fun getAllPossibleNames(): Set<Name> = withValidityAssertion {
buildSet {
subScopes.flatMapTo(this) { it.getAllPossibleNames() }
@@ -64,6 +58,12 @@ class SimpleKtCompositeScope(
}
}
override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence<KtPackageSymbol> = withValidityAssertion {
sequence {
subScopes.forEach { yieldAll(it.getPackageSymbols(nameFilter)) }
}
}
override fun mayContainName(name: Name): Boolean = withValidityAssertion {
subScopes.any { it.mayContainName(name) }
}
@@ -3,20 +3,16 @@
* 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.descriptors.scopes
package org.jetbrains.kotlin.analysis.api.impl.base.scopes
import org.jetbrains.kotlin.analysis.api.ValidityTokenOwner
import org.jetbrains.kotlin.analysis.api.scopes.KtScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
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.KtSymbol
import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
import org.jetbrains.kotlin.name.Name
internal open class KtFe10EmptyScope(override val token: ValidityToken) : KtScope, ValidityTokenOwner {
class KtEmptyScope(override val token: ValidityToken) : KtScope {
override fun getAllPossibleNames(): Set<Name> = withValidityAssertion {
return emptySet()
}
@@ -45,6 +41,10 @@ internal open class KtFe10EmptyScope(override val token: ValidityToken) : KtScop
return emptySequence()
}
override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence<KtPackageSymbol> = withValidityAssertion {
emptySequence()
}
override fun mayContainName(name: Name): Boolean = withValidityAssertion {
return false
}
@@ -18,19 +18,21 @@ import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
public abstract class KtScopeProvider : KtAnalysisSessionComponent() {
public abstract fun getMemberScope(classSymbol: KtSymbolWithMembers): KtMemberScope
public abstract fun getMemberScope(classSymbol: KtSymbolWithMembers): KtScope
public abstract fun getDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtDeclaredMemberScope
public abstract fun getDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtScope
public abstract fun getDelegatedMemberScope(classSymbol: KtSymbolWithMembers): KtDelegatedMemberScope
public abstract fun getDelegatedMemberScope(classSymbol: KtSymbolWithMembers): KtScope
public abstract fun getStaticMemberScope(symbol: KtSymbolWithMembers): KtScope
public abstract fun getFileScope(fileSymbol: KtFileSymbol): KtDeclarationScope<KtSymbolWithDeclarations>
public abstract fun getEmptyScope(): KtScope
public abstract fun getPackageScope(packageSymbol: KtPackageSymbol): KtPackageScope
public abstract fun getFileScope(fileSymbol: KtFileSymbol): KtScope
public abstract fun getCompositeScope(subScopes: List<KtScope>): KtCompositeScope
public abstract fun getPackageScope(packageSymbol: KtPackageSymbol): KtScope
public abstract fun getCompositeScope(subScopes: List<KtScope>): KtScope
public abstract fun getTypeScope(type: KtType): KtScope?
@@ -41,25 +43,25 @@ public abstract class KtScopeProvider : KtAnalysisSessionComponent() {
}
public interface KtScopeProviderMixIn : KtAnalysisSessionMixIn {
public fun KtSymbolWithMembers.getMemberScope(): KtMemberScope =
public fun KtSymbolWithMembers.getMemberScope(): KtScope =
analysisSession.scopeProvider.getMemberScope(this)
public fun KtSymbolWithMembers.getDeclaredMemberScope(): KtDeclaredMemberScope =
public fun KtSymbolWithMembers.getDeclaredMemberScope(): KtScope =
analysisSession.scopeProvider.getDeclaredMemberScope(this)
public fun KtSymbolWithMembers.getDelegatedMemberScope(): KtDelegatedMemberScope =
public fun KtSymbolWithMembers.getDelegatedMemberScope(): KtScope =
analysisSession.scopeProvider.getDelegatedMemberScope(this)
public fun KtSymbolWithMembers.getStaticMemberScope(): KtScope =
analysisSession.scopeProvider.getStaticMemberScope(this)
public fun KtFileSymbol.getFileScope(): KtDeclarationScope<KtSymbolWithDeclarations> =
public fun KtFileSymbol.getFileScope(): KtScope =
analysisSession.scopeProvider.getFileScope(this)
public fun KtPackageSymbol.getPackageScope(): KtPackageScope =
public fun KtPackageSymbol.getPackageScope(): KtScope =
analysisSession.scopeProvider.getPackageScope(this)
public fun List<KtScope>.asCompositeScope(): KtCompositeScope =
public fun List<KtScope>.asCompositeScope(): KtScope =
analysisSession.scopeProvider.getCompositeScope(this)
public fun KtType.getTypeScope(): KtScope? =
@@ -72,7 +74,7 @@ public interface KtScopeProviderMixIn : KtAnalysisSessionMixIn {
analysisSession.scopeProvider.getScopeContextForPosition(this, this)
}
public data class KtScopeContext(val scopes: KtCompositeScope, val implicitReceivers: List<KtImplicitReceiver>)
public data class KtScopeContext(val scopes: KtScope, val implicitReceivers: List<KtImplicitReceiver>)
public class KtImplicitReceiver(
override val token: ValidityToken,
@@ -5,6 +5,8 @@
package org.jetbrains.kotlin.analysis.api.scopes
import org.jetbrains.kotlin.analysis.api.symbols.KtPackageSymbol
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
@@ -12,6 +14,10 @@ import org.jetbrains.kotlin.name.Name
public interface KtImportingScope : KtScope {
public val imports: List<Import>
public val isDefaultImportingScope: Boolean
override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence<KtPackageSymbol> = withValidityAssertion {
emptySequence()
}
}
public interface KtStarImportingScope : KtImportingScope {
@@ -62,6 +62,13 @@ public interface KtScope : ValidityTokenOwner {
*/
public fun getConstructors(): Sequence<KtConstructorSymbol>
/**
* Return a sequence of [KtPackageSymbol] nested in current scope contain if package name matches [nameFilter]
*/
public fun getPackageSymbols(nameFilter: KtScopeNameFilter = { true }): Sequence<KtPackageSymbol>
/**
* return true if the scope may contain name, false otherwise.
*
@@ -73,35 +80,3 @@ public interface KtScope : ValidityTokenOwner {
}
public typealias KtScopeNameFilter = (Name) -> Boolean
public interface KtCompositeScope : KtScope {
public val subScopes: List<KtScope>
}
public interface KtMemberScope : KtDeclarationScope<KtSymbolWithMembers> {
override val owner: KtSymbolWithMembers
}
public interface KtDeclaredMemberScope : KtDeclarationScope<KtSymbolWithMembers> {
override val owner: KtSymbolWithMembers
}
public interface KtDelegatedMemberScope : KtDeclarationScope<KtSymbolWithMembers> {
override val owner: KtSymbolWithMembers
}
public interface KtDeclarationScope<out T : KtSymbolWithDeclarations> : KtScope {
public val owner: T
}
public interface KtPackageScope : KtScope {
public val fqName: FqName
public fun getPackageSymbols(nameFilter: KtScopeNameFilter = { true }): Sequence<KtPackageSymbol>
override fun getAllSymbols(): Sequence<KtSymbol> = withValidityAssertion {
super.getAllSymbols() + getPackageSymbols()
}
override fun getConstructors(): Sequence<KtConstructorSymbol> = withValidityAssertion { emptySequence() }
}
@@ -18,7 +18,7 @@ import org.jetbrains.kotlin.asJava.elements.KtLightMethod
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.fileClasses.javaFileFacadeFqName
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.analysis.api.scopes.KtDeclarationScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScope
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtFunctionSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtKotlinPropertySymbol
@@ -109,7 +109,7 @@ class FirLightClassForFacade(
}
private fun loadFieldsFromFile(
fileScope: KtDeclarationScope<KtSymbolWithDeclarations>,
fileScope: KtScope,
nameGenerator: FirLightField.FieldNameGenerator,
result: MutableList<KtLightField>
) {