diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirTowerResolverSession.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirTowerResolverSession.kt index dc95ddae28c..592578c6b17 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirTowerResolverSession.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/FirTowerResolverSession.kt @@ -19,9 +19,9 @@ import org.jetbrains.kotlin.fir.resolve.calls.* import org.jetbrains.kotlin.fir.resolve.scope import org.jetbrains.kotlin.fir.resolve.transformQualifiedAccessUsingSmartcastInfo import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.firUnsafe +import org.jetbrains.kotlin.fir.scopes.FirCompositeScope import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.fir.scopes.ProcessorAction -import org.jetbrains.kotlin.fir.scopes.impl.FirCompositeScope import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol @@ -456,7 +456,7 @@ class FirTowerResolverSession internal constructor( val scope = when (superTypeRef) { is FirResolvedTypeRef -> superTypeRef.type.scope(session, components.scopeSession) is FirComposedSuperTypeRef -> FirCompositeScope( - superTypeRef.superTypeRefs.mapNotNullTo(mutableListOf()) { it.type.scope(session, components.scopeSession) } + superTypeRef.superTypeRefs.mapNotNull { it.type.scope(session, components.scopeSession) } ) else -> null } ?: return diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirAbstractTreeTransformerWithSuperTypes.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirAbstractTreeTransformerWithSuperTypes.kt index 48e25880975..c8eed4ee89a 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirAbstractTreeTransformerWithSuperTypes.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirAbstractTreeTransformerWithSuperTypes.kt @@ -10,25 +10,28 @@ import org.jetbrains.kotlin.fir.declarations.FirMemberDeclaration import org.jetbrains.kotlin.fir.declarations.FirRegularClass import org.jetbrains.kotlin.fir.declarations.FirResolvePhase import org.jetbrains.kotlin.fir.expressions.FirStatement -import org.jetbrains.kotlin.fir.resolve.providers.getNestedClassifierScope import org.jetbrains.kotlin.fir.resolve.lookupSuperTypes -import org.jetbrains.kotlin.fir.scopes.impl.FirCompositeScope +import org.jetbrains.kotlin.fir.resolve.providers.getNestedClassifierScope +import org.jetbrains.kotlin.fir.scopes.FirCompositeScope +import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.fir.scopes.impl.FirMemberTypeParameterScope import org.jetbrains.kotlin.fir.scopes.impl.nestedClassifierScope import org.jetbrains.kotlin.fir.visitors.CompositeTransformResult abstract class FirAbstractTreeTransformerWithSuperTypes( - phase: FirResolvePhase, - reversedScopePriority: Boolean + phase: FirResolvePhase ) : FirAbstractTreeTransformer(phase) { - protected val towerScope = FirCompositeScope(mutableListOf(), reversedPriority = reversedScopePriority) + protected val scopes = mutableListOf() + protected val towerScope = FirCompositeScope(scopes.asReversed()) protected inline fun withScopeCleanup(crossinline l: () -> T): T { - val sizeBefore = towerScope.scopes.size + val sizeBefore = scopes.size val result = l() - val size = towerScope.scopes.size + val size = scopes.size assert(size >= sizeBefore) - towerScope.dropLastScopes(size - sizeBefore) + repeat(size - sizeBefore) { + scopes.removeAt(scopes.lastIndex) + } return result } @@ -41,18 +44,18 @@ abstract class FirAbstractTreeTransformerWithSuperTypes( val superTypes = lookupSuperTypes(firClass, lookupInterfaces = false, deep = true, useSiteSession = session).asReversed() for (superType in superTypes) { session.getNestedClassifierScope(superType.lookupTag)?.let { - towerScope.addScope(it) + scopes.add(it) } } if (firClass is FirRegularClass) { firClass.addTypeParametersScope() val companionObject = firClass.companionObject if (companionObject != null) { - nestedClassifierScope(companionObject)?.let(towerScope::addScope) + nestedClassifierScope(companionObject)?.let(scopes::add) } } - nestedClassifierScope(firClass)?.let(towerScope::addScope) + nestedClassifierScope(firClass)?.let(scopes::add) transformElement(firClass, data) } @@ -60,7 +63,7 @@ abstract class FirAbstractTreeTransformerWithSuperTypes( protected fun FirMemberDeclaration.addTypeParametersScope() { if (typeParameters.isNotEmpty()) { - towerScope.addScope(FirMemberTypeParameterScope(this)) + scopes.add(FirMemberTypeParameterScope(this)) } } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt index e8aec60ee4d..6856b47288f 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt @@ -16,7 +16,7 @@ import org.jetbrains.kotlin.fir.extensions.supertypeGenerators import org.jetbrains.kotlin.fir.resolve.* import org.jetbrains.kotlin.fir.resolve.providers.getNestedClassifierScope import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.LocalClassesNavigationInfo -import org.jetbrains.kotlin.fir.scopes.FirIterableScope +import org.jetbrains.kotlin.fir.scopes.FirCompositeScope import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.fir.scopes.createImportingScopes import org.jetbrains.kotlin.fir.scopes.impl.FirMemberTypeParameterScope @@ -62,7 +62,7 @@ fun > F.runSupertypeResolvePhaseForLocalClass( val supertypeComputationSession = SupertypeComputationSession() val supertypeResolverVisitor = FirSupertypeResolverVisitor( session, supertypeComputationSession, scopeSession, - FirImmutableCompositeScope(ImmutableList.ofAll(currentScopeList)), + ImmutableList.ofAll(currentScopeList), localClassesNavigationInfo ) @@ -167,26 +167,26 @@ private class FirSupertypeResolverVisitor( private val session: FirSession, private val supertypeComputationSession: SupertypeComputationSession, private val scopeSession: ScopeSession, - private val scopeForLocalClass: FirImmutableCompositeScope? = null, + private val scopeForLocalClass: ImmutableList? = null, private val localClassesNavigationInfo: LocalClassesNavigationInfo? = null ) : FirDefaultVisitorVoid() { private val supertypeGenerationExtensions = session.extensionService.supertypeGenerators override fun visitElement(element: FirElement) {} - private fun prepareFileScope(file: FirFile): FirImmutableCompositeScope { + private fun prepareFileScopes(file: FirFile): ScopeImmutableList { return supertypeComputationSession.getOrPutFileScope(file) { - FirImmutableCompositeScope(ImmutableList.ofAll(createImportingScopes(file, session, scopeSession).asReversed())) + ImmutableList.ofAll(createImportingScopes(file, session, scopeSession).asReversed()) } } - private fun prepareScopeForNestedClasses(klass: FirClass<*>): FirImmutableCompositeScope { + private fun prepareScopeForNestedClasses(klass: FirClass<*>): ScopeImmutableList { return supertypeComputationSession.getOrPutScopeForNestedClasses(klass) { - val scope = prepareScope(klass) + val scopes = prepareScopes(klass) resolveAllSupertypes(klass, klass.superTypeRefs) - scope.childScope(createScopesForNestedClasses(klass, session, supertypeComputationSession)) + scopes.pushAll(createScopesForNestedClasses(klass, session, supertypeComputationSession)) } } @@ -213,33 +213,33 @@ private class FirSupertypeResolverVisitor( else -> emptyList() } - private fun prepareScope(classLikeDeclaration: FirClassLikeDeclaration<*>): FirImmutableCompositeScope { + private fun prepareScopes(classLikeDeclaration: FirClassLikeDeclaration<*>): ImmutableList { val classId = classLikeDeclaration.symbol.classId val result = when { classId.isLocal -> { // Local type aliases are not supported - if (classLikeDeclaration !is FirClass<*>) return FirImmutableCompositeScope.EMPTY + if (classLikeDeclaration !is FirClass<*>) return ImmutableList.empty() // Local classes should be treated specially and supplied with localClassesNavigationInfo, normally // But it seems to be too strict to add an assertion here - val navigationInfo = localClassesNavigationInfo ?: return FirImmutableCompositeScope.EMPTY + val navigationInfo = localClassesNavigationInfo ?: return ImmutableList.empty() val parent = localClassesNavigationInfo.parentForClass[classLikeDeclaration] when { parent != null -> prepareScopeForNestedClasses(parent) - else -> scopeForLocalClass ?: return FirImmutableCompositeScope.EMPTY + else -> scopeForLocalClass ?: return ImmutableList.empty() } } classId.isNestedClass -> { val outerClassFir = classId.outerClassId?.let(session.firProvider::getFirClassifierByFqName) as? FirRegularClass - prepareScopeForNestedClasses(outerClassFir ?: return FirImmutableCompositeScope.EMPTY) + prepareScopeForNestedClasses(outerClassFir ?: return ImmutableList.empty()) } - else -> prepareFileScope(session.firProvider.getFirClassifierContainerFile(classId)) + else -> prepareFileScopes(session.firProvider.getFirClassifierContainerFile(classId)) } - return result.childScope(classLikeDeclaration.typeParametersScope()) + return result.pushIfNotNull(classLikeDeclaration.typeParametersScope()) } private fun resolveSpecificClassLikeSupertypes( @@ -254,10 +254,10 @@ private class FirSupertypeResolverVisitor( } supertypeComputationSession.startComputingSupertypes(classLikeDeclaration) - val scope = prepareScope(classLikeDeclaration) + val scopes = prepareScopes(classLikeDeclaration) val transformer = FirSpecificTypeResolverTransformer(session) - val resolvedTypesRefs = resolveSuperTypeRefs(transformer, scope) + val resolvedTypesRefs = resolveSuperTypeRefs(transformer, FirCompositeScope(scopes)) supertypeComputationSession.storeSupertypes(classLikeDeclaration, resolvedTypesRefs) return resolvedTypesRefs @@ -343,8 +343,8 @@ private fun createErrorTypeRef(fir: FirElement, message: String) = buildErrorTyp } private class SupertypeComputationSession { - private val fileScopesMap = hashMapOf() - private val scopesForNestedClassesMap = hashMapOf, FirImmutableCompositeScope>() + private val fileScopesMap = hashMapOf() + private val scopesForNestedClassesMap = hashMapOf, ScopeImmutableList>() private val supertypeStatusMap = linkedMapOf, SupertypeComputationStatus>() val supertypesSupplier = object : SupertypeSupplier() { @@ -366,10 +366,10 @@ private class SupertypeComputationSession { fun getSupertypesComputationStatus(classLikeDeclaration: FirClassLikeDeclaration<*>): SupertypeComputationStatus = supertypeStatusMap[classLikeDeclaration] ?: SupertypeComputationStatus.NotComputed - fun getOrPutFileScope(file: FirFile, scope: () -> FirImmutableCompositeScope): FirImmutableCompositeScope = + fun getOrPutFileScope(file: FirFile, scope: () -> ScopeImmutableList): ScopeImmutableList = fileScopesMap.getOrPut(file) { scope() } - fun getOrPutScopeForNestedClasses(klass: FirClass<*>, scope: () -> FirImmutableCompositeScope): FirImmutableCompositeScope = + fun getOrPutScopeForNestedClasses(klass: FirClass<*>, scope: () -> ScopeImmutableList): ScopeImmutableList = scopesForNestedClassesMap.getOrPut(klass) { scope() } fun startComputingSupertypes(classLikeDeclaration: FirClassLikeDeclaration<*>) { @@ -453,15 +453,6 @@ sealed class SupertypeComputationStatus { } private typealias ImmutableList = javaslang.collection.List +private typealias ScopeImmutableList = ImmutableList -private class FirImmutableCompositeScope( - override val scopes: ImmutableList -) : FirIterableScope() { - - companion object { - val EMPTY = FirImmutableCompositeScope(ImmutableList.empty()) - } - - fun childScope(newScope: FirScope?) = newScope?.let { FirImmutableCompositeScope(scopes.push(newScope)) } ?: this - fun childScope(newScopes: Collection) = FirImmutableCompositeScope(scopes.pushAll(newScopes)) -} +private fun ScopeImmutableList.pushIfNotNull(scope: FirScope?): ScopeImmutableList = if (scope == null) this else push(scope) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirTypeResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirTypeResolveTransformer.kt index 5db4699ea37..aba7275e782 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirTypeResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirTypeResolveTransformer.kt @@ -37,12 +37,11 @@ class FirTypeResolveTransformer( private val scopeSession: ScopeSession, initialScopes: List = emptyList() ) : FirAbstractTreeTransformerWithSuperTypes( - phase = FirResolvePhase.TYPES, - reversedScopePriority = true + phase = FirResolvePhase.TYPES ) { init { - towerScope.addScopes(initialScopes.asReversed()) + scopes.addAll(initialScopes.asReversed()) } private val typeResolverTransformer: FirSpecificTypeResolverTransformer = FirSpecificTypeResolverTransformer(session) @@ -50,7 +49,7 @@ class FirTypeResolveTransformer( override fun transformFile(file: FirFile, data: Nothing?): CompositeTransformResult { checkSessionConsistency(file) return withScopeCleanup { - towerScope.addScopes(createImportingScopes(file, session, scopeSession)) + scopes.addAll(createImportingScopes(file, session, scopeSession)) super.transformFile(file, data) } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirBodyResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirBodyResolveTransformer.kt index 681c7c57a8a..d9824f02c7a 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirBodyResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirBodyResolveTransformer.kt @@ -15,8 +15,8 @@ import org.jetbrains.kotlin.fir.resolve.dfa.DataFlowAnalyzerContext import org.jetbrains.kotlin.fir.resolve.transformers.ReturnTypeCalculator import org.jetbrains.kotlin.fir.resolve.transformers.ReturnTypeCalculatorForFullBodyResolve import org.jetbrains.kotlin.fir.resolve.transformers.withScopeCleanup +import org.jetbrains.kotlin.fir.scopes.FirCompositeScope import org.jetbrains.kotlin.fir.scopes.createImportingScopes -import org.jetbrains.kotlin.fir.scopes.impl.FirCompositeScope import org.jetbrains.kotlin.fir.scopes.impl.createCurrentScopeList import org.jetbrains.kotlin.fir.types.FirImplicitTypeRef import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/FirAbstractAnnotationResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/FirAbstractAnnotationResolveTransformer.kt index 9e0ee86c16d..01eaea100f4 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/FirAbstractAnnotationResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/plugin/FirAbstractAnnotationResolveTransformer.kt @@ -12,9 +12,9 @@ import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall import org.jetbrains.kotlin.fir.expressions.FirStatement import org.jetbrains.kotlin.fir.resolve.ScopeSession -import org.jetbrains.kotlin.fir.resolve.transformers.withScopeCleanup +import org.jetbrains.kotlin.fir.scopes.FirCompositeScope +import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.fir.scopes.createImportingScopes -import org.jetbrains.kotlin.fir.scopes.impl.FirCompositeScope import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.visitors.CompositeTransformResult import org.jetbrains.kotlin.fir.visitors.FirDefaultTransformer @@ -26,16 +26,14 @@ internal abstract class FirAbstractAnnotationResolveTransformer( ) : FirDefaultTransformer() { abstract override fun transformAnnotationCall(annotationCall: FirAnnotationCall, data: D): CompositeTransformResult - protected val towerScope = FirCompositeScope(mutableListOf()) + protected lateinit var scope: FirScope override fun transformFile(file: FirFile, data: D): CompositeTransformResult { - return withScopeCleanup(towerScope.scopes) { - towerScope.addScopes(createImportingScopes(file, session, scopeSession, useCaching = false)) - val state = beforeChildren(file) - file.transformDeclarations(this, data) - afterChildren(state) - transformAnnotatedDeclaration(file, data) - } + scope = FirCompositeScope(createImportingScopes(file, session, scopeSession, useCaching = false)) + val state = beforeChildren(file) + file.transformDeclarations(this, data) + afterChildren(state) + return transformAnnotatedDeclaration(file, data) } override fun transformProperty(property: FirProperty, data: D): CompositeTransformResult { @@ -119,4 +117,4 @@ internal abstract class FirAbstractAnnotationResolveTransformer( } protected open fun afterChildren(state: S?) {} -} \ No newline at end of file +} diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirCompositeScope.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirCompositeScope.kt deleted file mode 100644 index faba26d9811..00000000000 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirCompositeScope.kt +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2010-2018 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.fir.scopes.impl - -import org.jetbrains.kotlin.fir.scopes.FirIterableScope -import org.jetbrains.kotlin.fir.scopes.FirScope - -class FirCompositeScope( - private val scopeList: MutableList, - private val reversedPriority: Boolean = false -) : FirIterableScope() { - override val scopes get() = if (reversedPriority) scopeList.asReversed() else scopeList - - fun addScopes(scopes: List) { - scopeList += scopes - } - - fun addScope(scope: FirScope) { - scopeList += scope - } - - fun dropLastScopes(number: Int) { - repeat(number) { - scopeList.removeAt(scopeList.size - 1) - } - } - -} diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/scopes/FirIterableScope.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/scopes/FirCompositeScope.kt similarity index 94% rename from compiler/fir/tree/src/org/jetbrains/kotlin/fir/scopes/FirIterableScope.kt rename to compiler/fir/tree/src/org/jetbrains/kotlin/fir/scopes/FirCompositeScope.kt index 30f52214a03..5a59764cc61 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/scopes/FirIterableScope.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/scopes/FirCompositeScope.kt @@ -11,8 +11,7 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol import org.jetbrains.kotlin.name.Name -abstract class FirIterableScope : FirScope() { - protected abstract val scopes: Iterable +class FirCompositeScope(private val scopes: Iterable) : FirScope() { override fun processClassifiersByNameWithSubstitution( name: Name, diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/scopes/FirScopeProvider.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/scopes/FirScopeProvider.kt index cd165890ad6..09d4e6b8747 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/scopes/FirScopeProvider.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/scopes/FirScopeProvider.kt @@ -38,10 +38,8 @@ abstract class FirScopeProvider { return when { nestedClassifierScope != null && callableScope != null -> - FirReadOnlyCompositeScope(listOf(nestedClassifierScope, callableScope)) + FirCompositeScope(listOf(nestedClassifierScope, callableScope)) else -> nestedClassifierScope ?: callableScope } } } - -private class FirReadOnlyCompositeScope(override val scopes: Iterable) : FirIterableScope()