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
@@ -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() }
}