Analysis API: get rid of weak refs scheme for working with FIR internals for KtScope
This commit is contained in:
committed by
TeamCityServer
parent
d348c48e4f
commit
d723916d67
+3
-28
@@ -50,7 +50,6 @@ internal class KtFirScopeProvider(
|
|||||||
override val analysisSession: KtFirAnalysisSession by weakRef(analysisSession)
|
override val analysisSession: KtFirAnalysisSession by weakRef(analysisSession)
|
||||||
private val builder by weakRef(builder)
|
private val builder by weakRef(builder)
|
||||||
private val firResolveState by weakRef(firResolveState)
|
private val firResolveState by weakRef(firResolveState)
|
||||||
private val firScopeStorage = FirScopeRegistry()
|
|
||||||
|
|
||||||
private val memberScopeCache = IdentityHashMap<KtSymbolWithMembers, KtMemberScope>()
|
private val memberScopeCache = IdentityHashMap<KtSymbolWithMembers, KtMemberScope>()
|
||||||
private val declaredMemberScopeCache = IdentityHashMap<KtSymbolWithMembers, KtDeclaredMemberScope>()
|
private val declaredMemberScopeCache = IdentityHashMap<KtSymbolWithMembers, KtDeclaredMemberScope>()
|
||||||
@@ -80,7 +79,6 @@ internal class KtFirScopeProvider(
|
|||||||
)
|
)
|
||||||
} ?: return@getOrPut KtFirEmptyMemberScope(classSymbol)
|
} ?: return@getOrPut KtFirEmptyMemberScope(classSymbol)
|
||||||
|
|
||||||
firScopeStorage.register(firScope)
|
|
||||||
KtFirMemberScope(classSymbol, firScope, token, builder)
|
KtFirMemberScope(classSymbol, firScope, token, builder)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,7 +87,6 @@ internal class KtFirScopeProvider(
|
|||||||
val firScope = symbol.withFirForScope { fir ->
|
val firScope = symbol.withFirForScope { fir ->
|
||||||
fir.scopeProvider.getStaticScope(fir, analysisSession.rootModuleSession, ScopeSession())
|
fir.scopeProvider.getStaticScope(fir, analysisSession.rootModuleSession, ScopeSession())
|
||||||
} ?: return KtFirEmptyMemberScope(symbol)
|
} ?: return KtFirEmptyMemberScope(symbol)
|
||||||
firScopeStorage.register(firScope)
|
|
||||||
check(firScope is FirContainingNamesAwareScope)
|
check(firScope is FirContainingNamesAwareScope)
|
||||||
return KtFirDelegatingScopeImpl(firScope, builder, token)
|
return KtFirDelegatingScopeImpl(firScope, builder, token)
|
||||||
}
|
}
|
||||||
@@ -100,8 +97,6 @@ internal class KtFirScopeProvider(
|
|||||||
analysisSession.rootModuleSession.declaredMemberScope(it)
|
analysisSession.rootModuleSession.declaredMemberScope(it)
|
||||||
} ?: return@getOrPut KtFirEmptyMemberScope(classSymbol)
|
} ?: return@getOrPut KtFirEmptyMemberScope(classSymbol)
|
||||||
|
|
||||||
firScopeStorage.register(firScope)
|
|
||||||
|
|
||||||
KtFirDeclaredMemberScope(classSymbol, firScope, token, builder)
|
KtFirDeclaredMemberScope(classSymbol, firScope, token, builder)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,7 +114,6 @@ internal class KtFirScopeProvider(
|
|||||||
packageSymbol.fqName,
|
packageSymbol.fqName,
|
||||||
project,
|
project,
|
||||||
builder,
|
builder,
|
||||||
this,
|
|
||||||
token,
|
token,
|
||||||
analysisSession.searchScope,
|
analysisSession.searchScope,
|
||||||
analysisSession.targetPlatform
|
analysisSession.targetPlatform
|
||||||
@@ -127,9 +121,6 @@ internal class KtFirScopeProvider(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun registerScope(scope: FirScope) {
|
|
||||||
firScopeStorage.register(scope)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getCompositeScope(subScopes: List<KtScope>): KtCompositeScope = withValidityAssertion {
|
override fun getCompositeScope(subScopes: List<KtScope>): KtCompositeScope = withValidityAssertion {
|
||||||
KtFirCompositeScope(subScopes, token)
|
KtFirCompositeScope(subScopes, token)
|
||||||
@@ -190,7 +181,6 @@ internal class KtFirScopeProvider(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun convertToKtScope(firScope: FirScope): KtScope {
|
private fun convertToKtScope(firScope: FirScope): KtScope {
|
||||||
firScopeStorage.register(firScope)
|
|
||||||
return when (firScope) {
|
return when (firScope) {
|
||||||
is FirAbstractSimpleImportingScope -> KtFirNonStarImportingScope(firScope, builder, token)
|
is FirAbstractSimpleImportingScope -> KtFirNonStarImportingScope(firScope, builder, token)
|
||||||
is FirAbstractStarImportingScope -> KtFirStarImportingScope(firScope, builder, project, token)
|
is FirAbstractStarImportingScope -> KtFirStarImportingScope(firScope, builder, project, token)
|
||||||
@@ -198,7 +188,6 @@ internal class KtFirScopeProvider(
|
|||||||
firScope.fqName,
|
firScope.fqName,
|
||||||
project,
|
project,
|
||||||
builder,
|
builder,
|
||||||
this,
|
|
||||||
token,
|
token,
|
||||||
analysisSession.searchScope,
|
analysisSession.searchScope,
|
||||||
analysisSession.targetPlatform
|
analysisSession.targetPlatform
|
||||||
@@ -211,21 +200,7 @@ internal class KtFirScopeProvider(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class KtFirDelegatingScopeImpl<S>(
|
private class KtFirDelegatingScopeImpl<S>(
|
||||||
firScope: S, builder: KtSymbolByFirBuilder,
|
override val firScope: S,
|
||||||
|
builder: KtSymbolByFirBuilder,
|
||||||
token: ValidityToken
|
token: ValidityToken
|
||||||
) : KtFirDelegatingScope<S>(builder, token), ValidityTokenOwner where S : FirContainingNamesAwareScope, S : FirScope {
|
) : KtFirDelegatingScope<S>(builder, token), ValidityTokenOwner where S : FirContainingNamesAwareScope, S : FirScope
|
||||||
override val firScope: S by weakRef(firScope)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stores strong references to all instances of [FirScope] used
|
|
||||||
* Needed as the only entity which may have a strong references to FIR internals is [KtFirAnalysisSession] & [KtAnalysisSessionComponent]
|
|
||||||
* Entities which needs storing [FirScope] instances will store them as weak references via [org.jetbrains.kotlin.analysis.api.fir.utils.weakRef]
|
|
||||||
*/
|
|
||||||
internal class FirScopeRegistry {
|
|
||||||
private val scopes = mutableListOf<FirScope>()
|
|
||||||
|
|
||||||
fun register(scope: FirScope) {
|
|
||||||
scopes += scope
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+2
-5
@@ -15,12 +15,9 @@ import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithMembers
|
|||||||
|
|
||||||
internal class KtFirDeclaredMemberScope(
|
internal class KtFirDeclaredMemberScope(
|
||||||
override val owner: KtSymbolWithMembers,
|
override val owner: KtSymbolWithMembers,
|
||||||
firScope: FirClassDeclaredMemberScope,
|
override val firScope: FirClassDeclaredMemberScope,
|
||||||
token: ValidityToken,
|
token: ValidityToken,
|
||||||
builder: KtSymbolByFirBuilder
|
builder: KtSymbolByFirBuilder
|
||||||
) : KtFirDelegatingScope<FirClassDeclaredMemberScope>(builder, token),
|
) : KtFirDelegatingScope<FirClassDeclaredMemberScope>(builder, token),
|
||||||
KtDeclaredMemberScope,
|
KtDeclaredMemberScope,
|
||||||
ValidityTokenOwner {
|
ValidityTokenOwner
|
||||||
override val firScope: FirClassDeclaredMemberScope by weakRef(firScope)
|
|
||||||
}
|
|
||||||
|
|
||||||
+2
-5
@@ -15,10 +15,7 @@ import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithMembers
|
|||||||
|
|
||||||
internal class KtFirMemberScope(
|
internal class KtFirMemberScope(
|
||||||
override val owner: KtSymbolWithMembers,
|
override val owner: KtSymbolWithMembers,
|
||||||
firScope: FirTypeScope,
|
override val firScope: FirTypeScope,
|
||||||
token: ValidityToken,
|
token: ValidityToken,
|
||||||
builder: KtSymbolByFirBuilder
|
builder: KtSymbolByFirBuilder
|
||||||
) : KtFirDelegatingScope<FirTypeScope>(builder, token), KtMemberScope, ValidityTokenOwner {
|
) : KtFirDelegatingScope<FirTypeScope>(builder, token), KtMemberScope, ValidityTokenOwner
|
||||||
override val firScope: FirTypeScope by weakRef(firScope)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
+1
-2
@@ -22,11 +22,10 @@ import org.jetbrains.kotlin.analysis.api.withValidityAssertion
|
|||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
|
|
||||||
internal class KtFirNonStarImportingScope(
|
internal class KtFirNonStarImportingScope(
|
||||||
firScope: FirAbstractSimpleImportingScope,
|
private val firScope : FirAbstractSimpleImportingScope,
|
||||||
private val builder: KtSymbolByFirBuilder,
|
private val builder: KtSymbolByFirBuilder,
|
||||||
override val token: ValidityToken
|
override val token: ValidityToken
|
||||||
) : KtNonStarImportingScope, ValidityTokenOwner {
|
) : KtNonStarImportingScope, ValidityTokenOwner {
|
||||||
private val firScope: FirAbstractSimpleImportingScope by weakRef(firScope)
|
|
||||||
|
|
||||||
@OptIn(ExperimentalStdlibApi::class)
|
@OptIn(ExperimentalStdlibApi::class)
|
||||||
override val imports: List<NonStarImport> by cached {
|
override val imports: List<NonStarImport> by cached {
|
||||||
|
|||||||
+2
-8
@@ -14,8 +14,6 @@ import org.jetbrains.kotlin.fir.scopes.impl.FirPackageMemberScope
|
|||||||
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
|
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
|
||||||
import org.jetbrains.kotlin.analysis.api.fir.KtSymbolByFirBuilder
|
import org.jetbrains.kotlin.analysis.api.fir.KtSymbolByFirBuilder
|
||||||
import org.jetbrains.kotlin.analysis.api.fir.components.KtFirScopeProvider
|
import org.jetbrains.kotlin.analysis.api.fir.components.KtFirScopeProvider
|
||||||
import org.jetbrains.kotlin.analysis.api.fir.utils.lazyThreadUnsafeWeakRef
|
|
||||||
import org.jetbrains.kotlin.analysis.api.fir.utils.weakRef
|
|
||||||
import org.jetbrains.kotlin.analysis.api.scopes.KtPackageScope
|
import org.jetbrains.kotlin.analysis.api.scopes.KtPackageScope
|
||||||
import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
|
import org.jetbrains.kotlin.analysis.api.scopes.KtScopeNameFilter
|
||||||
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
|
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
|
||||||
@@ -31,19 +29,15 @@ internal class KtFirPackageScope(
|
|||||||
override val fqName: FqName,
|
override val fqName: FqName,
|
||||||
private val project: Project,
|
private val project: Project,
|
||||||
private val builder: KtSymbolByFirBuilder,
|
private val builder: KtSymbolByFirBuilder,
|
||||||
_scopeProvider: KtFirScopeProvider,
|
|
||||||
override val token: ValidityToken,
|
override val token: ValidityToken,
|
||||||
private val searchScope: GlobalSearchScope,
|
private val searchScope: GlobalSearchScope,
|
||||||
private val targetPlatform: TargetPlatform,
|
private val targetPlatform: TargetPlatform,
|
||||||
) : KtPackageScope {
|
) : KtPackageScope {
|
||||||
private val scopeProvider by weakRef(_scopeProvider)
|
|
||||||
private val declarationsProvider = project.createDeclarationProvider(searchScope)
|
private val declarationsProvider = project.createDeclarationProvider(searchScope)
|
||||||
private val packageProvider = project.createPackageProvider(searchScope)
|
private val packageProvider = project.createPackageProvider(searchScope)
|
||||||
|
|
||||||
private val firScope: FirPackageMemberScope by lazyThreadUnsafeWeakRef {
|
private val firScope: FirPackageMemberScope by lazy(LazyThreadSafetyMode.PUBLICATION) {
|
||||||
val scope = FirPackageMemberScope(fqName, builder.rootSession)
|
FirPackageMemberScope(fqName, builder.rootSession)
|
||||||
scopeProvider.registerScope(scope)
|
|
||||||
scope
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getPossibleCallableNames() = withValidityAssertion {
|
override fun getPossibleCallableNames() = withValidityAssertion {
|
||||||
|
|||||||
+1
-2
@@ -28,12 +28,11 @@ import org.jetbrains.kotlin.analysis.api.withValidityAssertion
|
|||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
|
|
||||||
internal class KtFirStarImportingScope(
|
internal class KtFirStarImportingScope(
|
||||||
firScope: FirAbstractStarImportingScope,
|
private val firScope: FirAbstractStarImportingScope,
|
||||||
private val builder: KtSymbolByFirBuilder,
|
private val builder: KtSymbolByFirBuilder,
|
||||||
project: Project,
|
project: Project,
|
||||||
override val token: ValidityToken,
|
override val token: ValidityToken,
|
||||||
) : KtStarImportingScope, ValidityTokenOwner {
|
) : KtStarImportingScope, ValidityTokenOwner {
|
||||||
private val firScope: FirAbstractStarImportingScope by weakRef(firScope)
|
|
||||||
override val isDefaultImportingScope: Boolean = withValidityAssertion { firScope is FirDefaultStarImportingScope }
|
override val isDefaultImportingScope: Boolean = withValidityAssertion { firScope is FirDefaultStarImportingScope }
|
||||||
|
|
||||||
//todo use more concrete scope
|
//todo use more concrete scope
|
||||||
|
|||||||
-32
@@ -1,32 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2020 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.fir.utils
|
|
||||||
|
|
||||||
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
|
|
||||||
import org.jetbrains.kotlin.analysis.api.ValidityTokenOwner
|
|
||||||
import java.lang.ref.WeakReference
|
|
||||||
import kotlin.reflect.KProperty
|
|
||||||
|
|
||||||
internal class LazyThreadUnsafeWeakRef<V : Any>
|
|
||||||
constructor(_createValue: () -> V, val token: ValidityToken) {
|
|
||||||
private var createValue: (() -> V)? = _createValue
|
|
||||||
private var weakRef: WeakReference<V>? = null
|
|
||||||
|
|
||||||
@Suppress("NOTHING_TO_INLINE")
|
|
||||||
inline operator fun getValue(thisRef: Any, property: KProperty<*>): V {
|
|
||||||
if (weakRef == null) {
|
|
||||||
weakRef = WeakReference(createValue!!())
|
|
||||||
createValue = null
|
|
||||||
}
|
|
||||||
return weakRef!!.get() ?: if (token.isValid()) {
|
|
||||||
throw EntityWasGarbageCollectedException(property.toString())
|
|
||||||
} else {
|
|
||||||
error("Accessing the invalid value of $property")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun <V : Any> ValidityTokenOwner.lazyThreadUnsafeWeakRef(createValue: () -> V) = LazyThreadUnsafeWeakRef(createValue, token)
|
|
||||||
Reference in New Issue
Block a user