[Analysis API] optimize composite scope

Do not create KtCompositeScope for empty or single scopes
This commit is contained in:
Ilya Kirillov
2023-05-12 15:29:57 +02:00
committed by Space Team
parent 950a32901c
commit ee1fd9f6e9
7 changed files with 28 additions and 7 deletions
@@ -41,6 +41,7 @@ tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>> {
kotlinOptions { kotlinOptions {
freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn" freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
freeCompilerArgs += "-Xcontext-receivers" freeCompilerArgs += "-Xcontext-receivers"
freeCompilerArgs += "-opt-in=org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals"
} }
} }
@@ -181,7 +181,7 @@ internal class KtFe10ScopeProvider(
} }
override fun getCompositeScope(subScopes: List<KtScope>): KtScope { override fun getCompositeScope(subScopes: List<KtScope>): KtScope {
return KtCompositeScope(subScopes, token) return KtCompositeScope.create(subScopes, token)
} }
override fun getTypeScope(type: KtType): KtTypeScope { override fun getTypeScope(type: KtType): KtTypeScope {
@@ -201,12 +201,12 @@ internal class KtFe10ScopeProvider(
val scopeKind = KtScopeKind.LocalScope(0) // TODO val scopeKind = KtScopeKind.LocalScope(0) // TODO
val lexicalScope = positionInFakeFile.getResolutionScope(bindingContext) val lexicalScope = positionInFakeFile.getResolutionScope(bindingContext)
if (lexicalScope != null) { if (lexicalScope != null) {
val compositeScope = KtCompositeScope(listOf(KtFe10ScopeLexical(lexicalScope, analysisContext)), token) val compositeScope = KtCompositeScope.create(listOf(KtFe10ScopeLexical(lexicalScope, analysisContext)), token)
return KtScopeContext(listOf(KtScopeWithKind(compositeScope, scopeKind, token)), collectImplicitReceivers(lexicalScope), token) return KtScopeContext(listOf(KtScopeWithKind(compositeScope, scopeKind, token)), collectImplicitReceivers(lexicalScope), token)
} }
val fileScope = analysisContext.resolveSession.fileScopeProvider.getFileResolutionScope(originalFile) val fileScope = analysisContext.resolveSession.fileScopeProvider.getFileResolutionScope(originalFile)
val compositeScope = KtCompositeScope(listOf(KtFe10ScopeLexical(fileScope, analysisContext)), token) val compositeScope = KtCompositeScope.create(listOf(KtFe10ScopeLexical(fileScope, analysisContext)), token)
return KtScopeContext(listOf(KtScopeWithKind(compositeScope, scopeKind, token)), collectImplicitReceivers(fileScope), token) return KtScopeContext(listOf(KtScopeWithKind(compositeScope, scopeKind, token)), collectImplicitReceivers(fileScope), token)
} }
@@ -95,4 +95,5 @@ compileKotlin.dependsOn(generateCode)
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach { tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions.freeCompilerArgs += "-Xcontext-receivers" kotlinOptions.freeCompilerArgs += "-Xcontext-receivers"
kotlinOptions.freeCompilerArgs += "-opt-in=org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals"
} }
@@ -156,7 +156,7 @@ internal class KtFirScopeProvider(
override fun getCompositeScope(subScopes: List<KtScope>): KtScope { override fun getCompositeScope(subScopes: List<KtScope>): KtScope {
return KtCompositeScope(subScopes, token) return KtCompositeScope.create(subScopes, token)
} }
override fun getTypeScope(type: KtType): KtTypeScope? = getFirTypeScope(type)?.let { convertToKtTypeScope(it) } override fun getTypeScope(type: KtType): KtTypeScope? = getFirTypeScope(type)?.let { convertToKtTypeScope(it) }
@@ -24,7 +24,7 @@ internal class KtFirScopeSubstitution(
override fun getDeclarationScope(scope: KtTypeScope): KtScope { override fun getDeclarationScope(scope: KtTypeScope): KtScope {
return when (scope) { return when (scope) {
is KtFirDelegatingTypeScope -> KtFirDelegatingNamesAwareScope(scope.firScope, analysisSession.firSymbolBuilder) is KtFirDelegatingTypeScope -> KtFirDelegatingNamesAwareScope(scope.firScope, analysisSession.firSymbolBuilder)
is KtCompositeTypeScope -> KtCompositeScope(scope.subScopes.map(::getDeclarationScope), token) is KtCompositeTypeScope -> KtCompositeScope.create(scope.subScopes.map(::getDeclarationScope), token)
else -> unexpectedElementError<KtTypeScope>(scope) else -> unexpectedElementError<KtTypeScope>(scope)
} }
} }
@@ -35,6 +35,7 @@ sourceSets {
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach { tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions.freeCompilerArgs += "-Xcontext-receivers" kotlinOptions.freeCompilerArgs += "-Xcontext-receivers"
kotlinOptions.freeCompilerArgs += "-opt-in=org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals"
} }
testsJar() testsJar()
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.analysis.api.impl.base.scopes package org.jetbrains.kotlin.analysis.api.impl.base.scopes
import org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.scopes.KtScope import org.jetbrains.kotlin.analysis.api.scopes.KtScope
@@ -12,10 +13,18 @@ import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
import org.jetbrains.kotlin.analysis.api.symbols.* import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
class KtCompositeScope( @KtAnalysisApiInternals
class KtCompositeScope private constructor(
private val subScopes: List<KtScope>, private val subScopes: List<KtScope>,
override val token: KtLifetimeToken override val token: KtLifetimeToken,
) : KtScope { ) : KtScope {
init {
require(subScopes.size > 1) {
"Required `subScopes.size > 1` but `subScopes.size = ${subScopes.size}`"
}
}
override fun getAllPossibleNames(): Set<Name> = withValidityAssertion { override fun getAllPossibleNames(): Set<Name> = withValidityAssertion {
buildSet { buildSet {
subScopes.flatMapTo(this) { it.getAllPossibleNames() } subScopes.flatMapTo(this) { it.getAllPossibleNames() }
@@ -81,4 +90,13 @@ class KtCompositeScope(
override fun mayContainName(name: Name): Boolean = withValidityAssertion { override fun mayContainName(name: Name): Boolean = withValidityAssertion {
subScopes.any { it.mayContainName(name) } subScopes.any { it.mayContainName(name) }
} }
companion object {
fun create(subScopes: List<KtScope>, token: KtLifetimeToken): KtScope =
when (subScopes.size) {
0 -> KtEmptyScope(token)
1 -> subScopes.single()
else -> KtCompositeScope(subScopes, token)
}
}
} }