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))
}
@@ -1,51 +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.descriptors.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.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 {
override fun getAllPossibleNames(): Set<Name> = withValidityAssertion {
return emptySet()
}
override fun getPossibleCallableNames(): Set<Name> = withValidityAssertion {
return emptySet()
}
override fun getPossibleClassifierNames(): Set<Name> = withValidityAssertion {
return emptySet()
}
override fun getAllSymbols(): Sequence<KtSymbol> = withValidityAssertion {
return emptySequence()
}
override fun getCallableSymbols(nameFilter: KtScopeNameFilter): Sequence<KtCallableSymbol> = withValidityAssertion {
return emptySequence()
}
override fun getClassifierSymbols(nameFilter: KtScopeNameFilter): Sequence<KtClassifierSymbol> = withValidityAssertion {
return emptySequence()
}
override fun getConstructors(): Sequence<KtConstructorSymbol> = withValidityAssertion {
return emptySequence()
}
override fun mayContainName(name: Name): Boolean = withValidityAssertion {
return false
}
}
@@ -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()
}