[Analysis API] add KtScope.getCallableSymbols/getClassifier symbols overloads which accept a candidate name set

This is needed for the cases when a candidate name set is known
 to avoid retrieving a set with all possible names when processing the scope.

^KT-58653 fixed
This commit is contained in:
Ilya Kirillov
2023-05-12 11:05:15 +02:00
committed by Space Team
parent aacdfb67c6
commit 29be88e3c9
12 changed files with 154 additions and 9 deletions
@@ -8,14 +8,14 @@ package org.jetbrains.kotlin.analysis.api.descriptors.scopes
import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisContext
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.toKtCallableSymbol
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.toKtClassifierSymbol
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
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.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.name.Name
@@ -51,6 +51,12 @@ internal class KtFe10FileScope(
}
}
override fun getCallableSymbols(names: Collection<Name>): Sequence<KtCallableSymbol> {
if (names.isEmpty()) return emptySequence()
val namesSet = names.toSet()
return getCallableSymbols { it in namesSet }
}
override fun getClassifierSymbols(nameFilter: KtScopeNameFilter): Sequence<KtClassifierSymbol> = withValidityAssertion {
val context = analysisContext.analyze(ktFile)
@@ -65,6 +71,12 @@ internal class KtFe10FileScope(
}
}
override fun getClassifierSymbols(names: Collection<Name>): Sequence<KtClassifierSymbol> {
if (names.isEmpty()) return emptySequence()
val namesSet = names.toSet()
return getClassifierSymbols { it in namesSet }
}
override fun getConstructors(): Sequence<KtConstructorSymbol> = withValidityAssertion {
emptySequence()
}
@@ -34,6 +34,12 @@ internal abstract class KtFe10ScopeResolution : KtScope, KtLifetimeOwner {
.mapNotNull { it.toKtSymbol(analysisContext) as? KtCallableSymbol }
}
override fun getCallableSymbols(names: Collection<Name>): Sequence<KtCallableSymbol> {
if (names.isEmpty()) return emptySequence()
val namesSet = names.toSet()
return getCallableSymbols { it in namesSet }
}
override fun getClassifierSymbols(nameFilter: KtScopeNameFilter): Sequence<KtClassifierSymbol> = withValidityAssertion {
return scope
.getContributedDescriptors(kindFilter = DescriptorKindFilter.CLASSIFIERS, nameFilter)
@@ -42,6 +48,12 @@ internal abstract class KtFe10ScopeResolution : KtScope, KtLifetimeOwner {
.mapNotNull { it.toKtSymbol(analysisContext) as? KtClassifierSymbol }
}
override fun getClassifierSymbols(names: Collection<Name>): Sequence<KtClassifierSymbol> {
if (names.isEmpty()) return emptySequence()
val namesSet = names.toSet()
return getClassifierSymbols { it in namesSet }
}
override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence<KtPackageSymbol> = withValidityAssertion {
emptySequence()
}
@@ -46,10 +46,22 @@ private class KtFirResolveExtensionScope(
gelTopLevelDeclarations(nameFilter) { it.getTopLevelCallables() }
}
override fun getCallableSymbols(names: Collection<Name>): Sequence<KtCallableSymbol> = withValidityAssertion {
if (names.isEmpty()) return emptySequence()
val namesSet = names.toSet()
return getCallableSymbols { it in namesSet }
}
override fun getClassifierSymbols(nameFilter: KtScopeNameFilter): Sequence<KtClassifierSymbol> = withValidityAssertion {
gelTopLevelDeclarations(nameFilter) { it.getTopLevelClassifiers() }
}
override fun getClassifierSymbols(names: Collection<Name>): Sequence<KtClassifierSymbol> {
if (names.isEmpty()) return emptySequence()
val namesSet = names.toSet()
return getClassifierSymbols { it in namesSet }
}
private inline fun <D : KtNamedDeclaration, reified S : KtDeclaration> gelTopLevelDeclarations(
crossinline nameFilter: KtScopeNameFilter,
crossinline getDeclarationsByProvider: (LLFirResolveExtensionToolDeclarationProvider) -> Sequence<D>,
@@ -11,6 +11,7 @@ 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.fir.scopes.FirContainingNamesAwareScope
import org.jetbrains.kotlin.name.Name
internal class KtFirDelegatedMemberScope(
firScope: FirContainingNamesAwareScope,
@@ -20,4 +21,8 @@ internal class KtFirDelegatedMemberScope(
override fun getCallableSymbols(nameFilter: KtScopeNameFilter): Sequence<KtCallableSymbol> = withValidityAssertion {
return super.getCallableSymbols(nameFilter).filter { it.origin == KtSymbolOrigin.DELEGATED }
}
override fun getCallableSymbols(names: Collection<Name>): Sequence<KtCallableSymbol> {
return super.getCallableSymbols(names).filter { it.origin == KtSymbolOrigin.DELEGATED }
}
}
@@ -42,10 +42,18 @@ internal open class KtFirDelegatingScope(
firScope.getCallableSymbols(getPossibleCallableNames().filter(nameFilter), builder)
}
override fun getCallableSymbols(names: Collection<Name>): Sequence<KtCallableSymbol> = withValidityAssertion {
firScope.getCallableSymbols(names, builder)
}
override fun getClassifierSymbols(nameFilter: KtScopeNameFilter): Sequence<KtClassifierSymbol> = withValidityAssertion {
firScope.getClassifierSymbols(getPossibleClassifierNames().filter(nameFilter), builder)
}
override fun getClassifierSymbols(names: Collection<Name>): Sequence<KtClassifierSymbol> = withValidityAssertion {
firScope.getClassifierSymbols(names, builder)
}
override fun getConstructors(): Sequence<KtConstructorSymbol> = withValidityAssertion {
firScope.getConstructors(builder)
}
@@ -78,6 +78,12 @@ internal class KtFirFileScope(
}
}
override fun getCallableSymbols(names: Collection<Name>): Sequence<KtCallableSymbol> = withValidityAssertion {
if (names.isEmpty()) return emptySequence()
val namesSet = names.toSet()
return getCallableSymbols { it in namesSet }
}
override fun getClassifierSymbols(nameFilter: KtScopeNameFilter): Sequence<KtClassifierSymbol> = withValidityAssertion {
sequence {
owner.firSymbol.fir.declarations.forEach { firDeclaration ->
@@ -93,6 +99,12 @@ internal class KtFirFileScope(
}
}
override fun getClassifierSymbols(names: Collection<Name>): Sequence<KtClassifierSymbol> {
if (names.isEmpty()) return emptySequence()
val namesSet = names.toSet()
return getClassifierSymbols { it in namesSet }
}
override fun getConstructors(): Sequence<KtConstructorSymbol> = withValidityAssertion { emptySequence() }
override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence<KtPackageSymbol> = withValidityAssertion {
@@ -51,10 +51,18 @@ internal class KtFirNonStarImportingScope(
firScope.getCallableSymbols(getPossibleCallableNames().filter(nameFilter), builder)
}
override fun getCallableSymbols(names: Collection<Name>): Sequence<KtCallableSymbol> = withValidityAssertion {
firScope.getCallableSymbols(names, builder)
}
override fun getClassifierSymbols(nameFilter: KtScopeNameFilter): Sequence<KtClassifierSymbol> = withValidityAssertion {
firScope.getClassifierSymbols(getPossibleClassifierNames().filter(nameFilter), builder)
}
override fun getClassifierSymbols(names: Collection<Name>): Sequence<KtClassifierSymbol> = withValidityAssertion {
firScope.getClassifierSymbols(names, builder)
}
override fun getPackageSymbols(nameFilter: KtScopeNameFilter): Sequence<KtPackageSymbol> = withValidityAssertion {
emptySequence()
}
@@ -89,10 +89,18 @@ internal class KtFirPackageScope(
firScope.getCallableSymbols(getPossibleCallableNames().filter(nameFilter), analysisSession.firSymbolBuilder)
}
override fun getCallableSymbols(names: Collection<Name>): Sequence<KtCallableSymbol> = withValidityAssertion {
firScope.getCallableSymbols(names, analysisSession.firSymbolBuilder)
}
override fun getClassifierSymbols(nameFilter: KtScopeNameFilter): Sequence<KtClassifierSymbol> = withValidityAssertion {
firScope.getClassifierSymbols(getPossibleClassifierNames().filter(nameFilter), analysisSession.firSymbolBuilder)
}
override fun getClassifierSymbols(names: Collection<Name>): Sequence<KtClassifierSymbol> = withValidityAssertion {
firScope.getClassifierSymbols(names, analysisSession.firSymbolBuilder)
}
override fun getConstructors(): Sequence<KtConstructorSymbol> = withValidityAssertion {
emptySequence()
}
@@ -40,10 +40,18 @@ internal class KtFirStarImportingScope(
firScope.getCallableSymbols(getPossibleCallableNames().filter(nameFilter), builder)
}
override fun getCallableSymbols(names: Collection<Name>): Sequence<KtCallableSymbol> = withValidityAssertion {
firScope.getCallableSymbols(names, builder)
}
override fun getClassifierSymbols(nameFilter: KtScopeNameFilter): Sequence<KtClassifierSymbol> = withValidityAssertion {
firScope.getClassifierSymbols(getPossibleClassifierNames().filter(nameFilter), builder)
}
override fun getClassifierSymbols(names: Collection<Name>): Sequence<KtClassifierSymbol> = withValidityAssertion {
firScope.getClassifierSymbols(names, builder)
}
override fun getConstructors(): Sequence<KtConstructorSymbol> = withValidityAssertion { emptySequence() }
// todo cache?
@@ -5,11 +5,11 @@
package org.jetbrains.kotlin.analysis.api.impl.base.scopes
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.scopes.KtScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.name.Name
class KtCompositeScope(
@@ -46,12 +46,26 @@ class KtCompositeScope(
}
}
override fun getCallableSymbols(names: Collection<Name>): Sequence<KtCallableSymbol> = withValidityAssertion {
if (names.isEmpty()) return emptySequence()
sequence {
subScopes.forEach { yieldAll(it.getCallableSymbols(names)) }
}
}
override fun getClassifierSymbols(nameFilter: KtScopeNameFilter): Sequence<KtClassifierSymbol> = withValidityAssertion {
sequence {
subScopes.forEach { yieldAll(it.getClassifierSymbols(nameFilter)) }
}
}
override fun getClassifierSymbols(names: Collection<Name>): Sequence<KtClassifierSymbol> = withValidityAssertion {
if (names.isEmpty()) return emptySequence()
sequence {
subScopes.forEach { yieldAll(it.getClassifierSymbols(names)) }
}
}
override fun getConstructors(): Sequence<KtConstructorSymbol> = withValidityAssertion {
sequence {
subScopes.forEach { yieldAll(it.getConstructors()) }
@@ -5,11 +5,11 @@
package org.jetbrains.kotlin.analysis.api.impl.base.scopes
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.scopes.KtScope
import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.name.Name
class KtEmptyScope(override val token: KtLifetimeToken) : KtScope {
@@ -33,10 +33,18 @@ class KtEmptyScope(override val token: KtLifetimeToken) : KtScope {
return emptySequence()
}
override fun getCallableSymbols(names: Collection<Name>): Sequence<KtCallableSymbol> = withValidityAssertion {
return emptySequence()
}
override fun getClassifierSymbols(nameFilter: KtScopeNameFilter): Sequence<KtClassifierSymbol> = withValidityAssertion {
return emptySequence()
}
override fun getClassifierSymbols(names: Collection<Name>): Sequence<KtClassifierSymbol> = withValidityAssertion {
return emptySequence()
}
override fun getConstructors(): Sequence<KtConstructorSymbol> = withValidityAssertion {
return emptySequence()
}
@@ -5,9 +5,8 @@
package org.jetbrains.kotlin.analysis.api.scopes
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeOwner
import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.name.Name
@@ -24,16 +23,55 @@ public interface KtScope : KtScopeLike {
}
/**
* Return a sequence of [KtCallableSymbol] which current scope contain if declaration name matches [nameFilter]
* Return a sequence of [KtCallableSymbol] which current scope contain if declaration name matches [nameFilter].
*
* This function needs to retrieve a set of all possible names before processing the scope.
* The overload with `names: Collection<Name>` should be used when the candidate name set is known.
*/
public fun getCallableSymbols(nameFilter: KtScopeNameFilter = { true }): Sequence<KtCallableSymbol>
/**
* Return a sequence of [KtCallableSymbol] which current scope contain, if declaration name present in [names]
*
* This implementation is more optimal than the one with `nameFilter` and should be used when the candidate name set is known.
*/
public fun getCallableSymbols(names: Collection<Name>): Sequence<KtCallableSymbol>
/**
* Return a sequence of [KtCallableSymbol] which current scope contain, if declaration name present in [names]
*
* @see getCallableSymbols
*/
public fun getCallableSymbols(vararg names: Name): Sequence<KtCallableSymbol> =
getCallableSymbols(names.toList())
/**
* Return a sequence of [KtClassifierSymbol] which current scope contain if classifier name matches [nameFilter]. The sequence includes:
* nested classes, inner classes, nested type aliases for the class scope, and top-level classes and top-level type aliases for file scope.
*
* This function needs to retrieve a set of all possible names before processing the scope.
* The overload with `names: Collection<Name>` should be used when the candidate name set is known.
*/
public fun getClassifierSymbols(nameFilter: KtScopeNameFilter = { true }): Sequence<KtClassifierSymbol>
/**
* Return a sequence of [KtClassifierSymbol] which current scope contains, if classifier name present in [names].
*
* The sequence includes: nested classes, inner classes, nested type aliases for the class scope,
* and top-level classes and top-level type aliases for file scope.
*
* This implementation is more optimal than the one with `nameFilter` and should be used when the candidate name set is known.
*/
public fun getClassifierSymbols(names: Collection<Name>): Sequence<KtClassifierSymbol>
/**
* Return a sequence of [KtClassifierSymbol] which current scope contains, if classifier name present in [names].
*
* @see getClassifierSymbols
*/
public fun getClassifierSymbols(vararg names: Name): Sequence<KtClassifierSymbol> =
getClassifierSymbols(names.toList())
/**
* Return a sequence of [KtConstructorSymbol] which current scope contain
*/