[LL API] Remove redundant 'LLFirSessionInvalidator'

This commit is contained in:
Yan Zhulanow
2023-03-07 19:02:14 +09:00
committed by Space Team
parent 708e7dd11a
commit ae2dd8c71a
8 changed files with 24 additions and 64 deletions
@@ -15,17 +15,14 @@ import org.jetbrains.kotlin.analysis.low.level.api.fir.file.builder.ModuleFileCa
import org.jetbrains.kotlin.analysis.low.level.api.fir.file.structure.FileStructureCache
import org.jetbrains.kotlin.analysis.low.level.api.fir.lazy.resolve.LLFirModuleLazyDeclarationResolver
import org.jetbrains.kotlin.analysis.low.level.api.fir.sessions.LLFirResolvableModuleSession
import org.jetbrains.kotlin.analysis.low.level.api.fir.sessions.LLFirSessionInvalidator
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.LLFirScopeSessionProvider
import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.fir.scopes.FirScopeProvider
@Suppress("CanBeParameter")
internal class LLFirModuleResolveComponents(
val module: KtModule,
val globalResolveComponents: LLFirGlobalResolveComponents,
val scopeProvider: FirScopeProvider,
val sessionInvalidator: LLFirSessionInvalidator,
val scopeProvider: FirScopeProvider
) {
val cache: ModuleFileCache = ModuleFileCacheImpl(this)
val firFileBuilder: LLFirFileBuilder = LLFirFileBuilder(this)
@@ -15,9 +15,9 @@ import org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostics.FileStructure
import org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostics.SingleNonLocalDeclarationDiagnosticRetriever
import org.jetbrains.kotlin.analysis.low.level.api.fir.lazy.resolve.RawFirNonLocalDeclarationBuilder
import org.jetbrains.kotlin.analysis.low.level.api.fir.lazy.resolve.declarationCanBeLazilyResolved
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.withInvalidationOnException
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.expressions.FirVariableAssignment
import org.jetbrains.kotlin.fir.psi
import org.jetbrains.kotlin.fir.scopes.kotlinScopeProvider
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
@@ -119,7 +119,7 @@ internal class ReanalyzableFunctionStructureElement(
return moduleComponents.globalResolveComponents.lockProvider.withLock(firFile) {
val upgradedPhase = minOf(originalFunction.resolvePhase, FirResolvePhase.DECLARATIONS)
moduleComponents.sessionInvalidator.withInvalidationOnException(moduleComponents.session) {
withInvalidationOnException(moduleComponents.session) {
with(originalFunction) {
replaceBody(temporaryFunction.body)
replaceContractDescription(temporaryFunction.contractDescription)
@@ -168,7 +168,7 @@ internal class ReanalyzablePropertyStructureElement(
val setterPhase = originalProperty.setter?.resolvePhase ?: originalProperty.resolvePhase
val upgradedPhase = minOf(originalProperty.resolvePhase, getterPhase, setterPhase, FirResolvePhase.DECLARATIONS)
moduleComponents.sessionInvalidator.withInvalidationOnException(moduleComponents.session) {
withInvalidationOnException(moduleComponents.session) {
with(originalProperty) {
getter?.replaceBody(temporaryProperty.getter?.body)
setter?.replaceBody(temporaryProperty.setter?.body)
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.analysis.low.level.api.fir.api.FirDesignationWithFil
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.throwUnexpectedFirElementError
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.tryCollectDesignationWithFile
import org.jetbrains.kotlin.analysis.low.level.api.fir.project.structure.llFirModuleData
import org.jetbrains.kotlin.analysis.low.level.api.fir.sessions.LLFirSessionInvalidator
import org.jetbrains.kotlin.analysis.low.level.api.fir.sessions.llFirSession
import org.jetbrains.kotlin.analysis.low.level.api.fir.transformers.LLFirLazyTransformerExecutor
import org.jetbrains.kotlin.analysis.low.level.api.fir.transformers.withOnAirDesignation
@@ -96,7 +95,7 @@ internal class LLFirModuleLazyDeclarationResolver(val moduleComponents: LLFirMod
try {
doLazyResolve(target, scopeSession, toPhase)
} catch (e: Exception) {
handleExceptionFromResolve(e, moduleComponents.sessionInvalidator, target, fromPhase, toPhase)
handleExceptionFromResolve(e, target, fromPhase, toPhase)
}
}
@@ -204,12 +203,11 @@ internal class LLFirModuleLazyDeclarationResolver(val moduleComponents: LLFirMod
private fun handleExceptionFromResolve(
exception: Exception,
sessionInvalidator: LLFirSessionInvalidator,
firDeclarationToResolve: FirElementWithResolvePhase,
fromPhase: FirResolvePhase,
toPhase: FirResolvePhase?
): Nothing {
sessionInvalidator.invalidate(firDeclarationToResolve.llFirSession)
firDeclarationToResolve.llFirSession.invalidate()
rethrowExceptionWithDetails(
buildString {
val moduleData = firDeclarationToResolve.llFirModuleData
@@ -48,7 +48,6 @@ internal object LLFirSessionFactory {
project: Project,
module: KtSourceModule,
globalResolveComponents: LLFirGlobalResolveComponents,
sessionInvalidator: LLFirSessionInvalidator,
sessionsCache: MutableMap<KtModule, LLFirSession>
): LLFirSourcesSession {
sessionsCache[module]?.let { return it as LLFirSourcesSession }
@@ -60,7 +59,7 @@ internal object LLFirSessionFactory {
val scopeProvider = FirKotlinScopeProvider(::wrapScopeWithJvmMapped)
val components = LLFirModuleResolveComponents(module, globalResolveComponents, scopeProvider, sessionInvalidator)
val components = LLFirModuleResolveComponents(module, globalResolveComponents, scopeProvider)
val contentScope = module.contentScope
val session = LLFirSourcesSession(
@@ -103,12 +102,7 @@ internal object LLFirSessionFactory {
}
val dependencyProvider = LLFirDependentModuleProvidersBySessions(this) {
processSourceDependencies(
module,
sessionsCache,
globalResolveComponents,
sessionInvalidator
)
processSourceDependencies(module, sessionsCache, globalResolveComponents)
add(builtinsSession)
}
@@ -145,7 +139,6 @@ internal object LLFirSessionFactory {
project: Project,
module: KtModule,
globalComponents: LLFirGlobalResolveComponents,
sessionInvalidator: LLFirSessionInvalidator,
builtinSession: LLFirBuiltinsAndCloneableSession,
sessionsCache: MutableMap<KtModule, LLFirSession>,
languageVersionSettings: LanguageVersionSettings = LanguageVersionSettingsImpl.DEFAULT
@@ -161,7 +154,7 @@ internal object LLFirSessionFactory {
}
val scopeProvider = FirKotlinScopeProvider()
val components = LLFirModuleResolveComponents(module, globalComponents, scopeProvider, sessionInvalidator)
val components = LLFirModuleResolveComponents(module, globalComponents, scopeProvider)
val contentScope = module.contentScope
val session = LLFirLibraryOrLibrarySourceResolvableModuleSession(module, components, builtinSession.builtinTypes)
@@ -240,7 +233,6 @@ internal object LLFirSessionFactory {
fun createScriptSession(
project: Project,
module: KtScriptModule,
sessionInvalidator: LLFirSessionInvalidator,
sessionsCache: MutableMap<KtModule, LLFirSession>
): LLFirScriptSession {
sessionsCache[module]?.let { return it as LLFirScriptSession }
@@ -252,7 +244,7 @@ internal object LLFirSessionFactory {
val scopeProvider = FirKotlinScopeProvider(::wrapScopeWithJvmMapped)
val globalResolveComponents = LLFirGlobalResolveComponents(project)
val components = LLFirModuleResolveComponents(module, globalResolveComponents, scopeProvider, sessionInvalidator)
val components = LLFirModuleResolveComponents(module, globalResolveComponents, scopeProvider)
val contentScope = module.contentScope
val session = LLFirScriptSession(module, components, builtinsSession.builtinTypes)
@@ -286,8 +278,7 @@ internal object LLFirSessionFactory {
processSourceDependencies(
module,
sessionsCache,
globalResolveComponents,
sessionInvalidator
globalResolveComponents
)
add(builtinsSession)
@@ -320,7 +311,6 @@ internal object LLFirSessionFactory {
fun createNotUnderContentRootResolvableSession(
project: Project,
module: KtNotUnderContentRootModule,
sessionInvalidator: LLFirSessionInvalidator,
sessionsCache: MutableMap<KtModule, LLFirSession>
): LLFirNonUnderContentRootResolvableModuleSession {
sessionsCache[module]?.let { return it as LLFirNonUnderContentRootResolvableModuleSession }
@@ -330,7 +320,7 @@ internal object LLFirSessionFactory {
val languageVersionSettings = LanguageVersionSettingsImpl.DEFAULT
val scopeProvider = FirKotlinScopeProvider(::wrapScopeWithJvmMapped)
val globalResolveComponents = LLFirGlobalResolveComponents(project)
val components = LLFirModuleResolveComponents(module, globalResolveComponents, scopeProvider, sessionInvalidator)
val components = LLFirModuleResolveComponents(module, globalResolveComponents, scopeProvider)
val contentScope = module.contentScope
val session = LLFirNonUnderContentRootResolvableModuleSession(module, components, builtinsSession.builtinTypes)
@@ -407,8 +397,7 @@ internal object LLFirSessionFactory {
private fun MutableList<LLFirSession>.processSourceDependencies(
module: KtModule,
sessionsCache: MutableMap<KtModule, LLFirSession>,
globalResolveComponents: LLFirGlobalResolveComponents,
sessionInvalidator: LLFirSessionInvalidator
globalResolveComponents: LLFirGlobalResolveComponents
) {
val project = module.project
@@ -427,7 +416,6 @@ internal object LLFirSessionFactory {
project,
dependency,
globalResolveComponents,
sessionInvalidator,
sessionsCache
)
}
@@ -1,24 +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.low.level.api.fir.sessions
import org.jetbrains.kotlin.fir.FirSession
internal class LLFirSessionInvalidator(private val invalidateSourcesSession: (LLFirResolvableModuleSession) -> Unit) {
fun invalidate(session: FirSession) {
require(session is LLFirResolvableModuleSession)
invalidateSourcesSession(session)
}
internal inline fun <R : Any> withInvalidationOnException(session: LLFirResolvableModuleSession, action: () -> R): R {
try {
return action()
} catch (e: Throwable) {
invalidate(session)
throw e
}
}
}
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.analysis.low.level.api.fir.sessions
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.analysis.low.level.api.fir.LLFirGlobalResolveComponents
import org.jetbrains.kotlin.analysis.low.level.api.fir.project.structure.LLFirBuiltinsSessionFactory
import org.jetbrains.kotlin.analysis.low.level.api.fir.project.structure.llFirModuleData
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.addValueFor
import org.jetbrains.kotlin.analysis.project.structure.*
import java.lang.ref.SoftReference
@@ -49,7 +48,6 @@ class LLFirSessionProviderStorage(val project: Project) {
project,
useSiteKtModule,
globalComponents,
sourceAsUseSiteSessionCache.sessionInvalidator,
sessions
)
sessions to session
@@ -65,7 +63,6 @@ class LLFirSessionProviderStorage(val project: Project) {
project,
useSiteKtModule,
globalComponents,
libraryAsUseSiteSessionCache.sessionInvalidator,
builtInsSessionFactory.getBuiltinsSession(useSiteKtModule.platform),
sessions
)
@@ -80,7 +77,6 @@ class LLFirSessionProviderStorage(val project: Project) {
val session = LLFirSessionFactory.createScriptSession(
project,
useSiteKtModule,
sourceAsUseSiteSessionCache.sessionInvalidator,
sessions
)
sessions to session
@@ -94,7 +90,6 @@ class LLFirSessionProviderStorage(val project: Project) {
val session = LLFirSessionFactory.createNotUnderContentRootResolvableSession(
project,
useSiteKtModule,
notUnderContentRootSessionCache.sessionInvalidator,
sessions
)
sessions to session
@@ -107,10 +102,6 @@ private class LLFirSessionsCache {
@Volatile
private var mappings: Map<KtModule, SoftReference<LLFirSession>> = emptyMap()
val sessionInvalidator: LLFirSessionInvalidator = LLFirSessionInvalidator { session ->
mappings[session.llFirModuleData.ktModule]?.get()?.invalidate()
}
inline fun <R> withMappings(
action: (Map<KtModule, LLFirSession>) -> Pair<Map<KtModule, LLFirSession>, R>
): Pair<Map<KtModule, LLFirSession>, R> {
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.analysis.low.level.api.fir.transformers.LLFirLazyTra
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.checkCanceled
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.checkPhase
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.checkTypeRefIsResolved
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.withInvalidationOnException
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.FirElementWithResolvePhase
import org.jetbrains.kotlin.fir.FirSession
@@ -146,7 +147,7 @@ internal class LLFirDesignatedSupertypeResolverTransformer(
lockProvider.withLock(designationsPerFile.key) {
val session = designationsPerFile.key.llFirResolvableSession
?: error("When FirFile exists for the declaration, the session should be resolvevablable")
session.moduleComponents.sessionInvalidator.withInvalidationOnException(session) {
withInvalidationOnException(session) {
applyToFileSymbols(designationsPerFile.value)
}
}
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.analysis.low.level.api.fir.util
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.util.ModificationTracker
import org.jetbrains.kotlin.analysis.low.level.api.fir.sessions.LLFirResolvableModuleSession
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
import org.jetbrains.kotlin.fir.diagnostics.FirDiagnosticHolder
@@ -19,6 +20,14 @@ import org.jetbrains.kotlin.psi.psiUtil.isObjectLiteral
import java.util.concurrent.TimeUnit
import java.util.concurrent.locks.Lock
internal fun <R : Any> withInvalidationOnException(session: LLFirResolvableModuleSession, action: () -> R): R {
try {
return action()
} catch (e: Throwable) {
session.invalidate()
throw e
}
}
internal inline fun <T> Lock.lockWithPCECheck(lockingIntervalMs: Long, action: () -> T): T {
while (true) {