FIR IDE: reuse scope session between different resolve calls for the same file

This commit is contained in:
Ilya Kirillov
2021-04-06 15:52:07 +02:00
parent a9fc3dd1ea
commit 60ea64143c
4 changed files with 21 additions and 7 deletions
@@ -10,6 +10,7 @@ import org.jetbrains.annotations.TestOnly
import org.jetbrains.kotlin.fir.FirElement import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.declarations.FirFile import org.jetbrains.kotlin.fir.declarations.FirFile
import org.jetbrains.kotlin.fir.declarations.FirResolvePhase import org.jetbrains.kotlin.fir.declarations.FirResolvePhase
import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.idea.fir.low.level.api.annotations.ThreadSafe import org.jetbrains.kotlin.idea.fir.low.level.api.annotations.ThreadSafe
import org.jetbrains.kotlin.idea.fir.low.level.api.file.builder.FirFileBuilder import org.jetbrains.kotlin.idea.fir.low.level.api.file.builder.FirFileBuilder
import org.jetbrains.kotlin.idea.fir.low.level.api.file.builder.ModuleFileCache import org.jetbrains.kotlin.idea.fir.low.level.api.file.builder.ModuleFileCache
@@ -60,6 +61,7 @@ internal class FirElementBuilder {
ktFile, ktFile,
moduleFileCache, moduleFileCache,
FirResolvePhase.BODY_RESOLVE, FirResolvePhase.BODY_RESOLVE,
scopeSession = ScopeSession(),
checkPCE = true checkPCE = true
) )
@@ -50,6 +50,7 @@ internal class FirFileBuilder(
ktFile: KtFile, ktFile: KtFile,
cache: ModuleFileCache, cache: ModuleFileCache,
@Suppress("SameParameterValue") toPhase: FirResolvePhase, @Suppress("SameParameterValue") toPhase: FirResolvePhase,
scopeSession: ScopeSession,
checkPCE: Boolean checkPCE: Boolean
): FirFile { ): FirFile {
val needResolve = toPhase > FirResolvePhase.RAW_FIR val needResolve = toPhase > FirResolvePhase.RAW_FIR
@@ -57,7 +58,13 @@ internal class FirFileBuilder(
if (needResolve) { if (needResolve) {
cache.firFileLockProvider.withWriteLock(firFile) { cache.firFileLockProvider.withWriteLock(firFile) {
if (firFile.resolvePhase >= toPhase) return@withWriteLock if (firFile.resolvePhase >= toPhase) return@withWriteLock
runResolveWithoutLock(firFile, fromPhase = firFile.resolvePhase, toPhase = toPhase, checkPCE = checkPCE) runResolveWithoutLock(
firFile,
fromPhase = firFile.resolvePhase,
toPhase = toPhase,
scopeSession = scopeSession,
checkPCE = checkPCE,
)
} }
} }
return firFile return firFile
@@ -76,13 +83,13 @@ internal class FirFileBuilder(
firFile: FirFile, firFile: FirFile,
fromPhase: FirResolvePhase, fromPhase: FirResolvePhase,
toPhase: FirResolvePhase, toPhase: FirResolvePhase,
checkPCE: Boolean scopeSession: ScopeSession,
checkPCE: Boolean,
) { ) {
assert(fromPhase <= toPhase) { assert(fromPhase <= toPhase) {
"Trying to resolve file ${firFile.name} from $fromPhase to $toPhase" "Trying to resolve file ${firFile.name} from $fromPhase to $toPhase"
} }
var currentPhase = fromPhase var currentPhase = fromPhase
val scopeSession = ScopeSession()
while (currentPhase < toPhase) { while (currentPhase < toPhase) {
if (checkPCE) checkCanceled() if (checkPCE) checkCanceled()
currentPhase = currentPhase.next currentPhase = currentPhase.next
@@ -9,6 +9,7 @@ import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirPsiDiagnostic import org.jetbrains.kotlin.fir.analysis.diagnostics.FirPsiDiagnostic
import org.jetbrains.kotlin.fir.declarations.FirFile import org.jetbrains.kotlin.fir.declarations.FirFile
import org.jetbrains.kotlin.fir.declarations.FirResolvePhase import org.jetbrains.kotlin.fir.declarations.FirResolvePhase
import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.idea.fir.low.level.api.api.DiagnosticCheckerFilter import org.jetbrains.kotlin.idea.fir.low.level.api.api.DiagnosticCheckerFilter
import org.jetbrains.kotlin.idea.fir.low.level.api.element.builder.FirTowerDataContextCollector import org.jetbrains.kotlin.idea.fir.low.level.api.element.builder.FirTowerDataContextCollector
import org.jetbrains.kotlin.idea.fir.low.level.api.element.builder.getNonLocalContainingOrThisDeclaration import org.jetbrains.kotlin.idea.fir.low.level.api.element.builder.getNonLocalContainingOrThisDeclaration
@@ -120,13 +121,15 @@ internal class FileStructure(
private fun createStructureElement(container: KtAnnotated): FileStructureElement = when (container) { private fun createStructureElement(container: KtAnnotated): FileStructureElement = when (container) {
is KtFile -> { is KtFile -> {
val scopeSession = ScopeSession()
val firFile = firFileBuilder.getFirFileResolvedToPhaseWithCaching( val firFile = firFileBuilder.getFirFileResolvedToPhaseWithCaching(
container, container,
moduleFileCache, moduleFileCache,
FirResolvePhase.IMPORTS, FirResolvePhase.IMPORTS,
scopeSession,
checkPCE = true checkPCE = true
) )
firLazyDeclarationResolver.resolveFileAnnotations(firFile, moduleFileCache) firLazyDeclarationResolver.resolveFileAnnotations(firFile, moduleFileCache, scopeSession)
RootStructureElement( RootStructureElement(
firFile, firFile,
container, container,
@@ -31,7 +31,7 @@ internal class FirLazyDeclarationResolver(
fun resolveFileAnnotations( fun resolveFileAnnotations(
firFile: FirFile, firFile: FirFile,
moduleFileCache: ModuleFileCache, moduleFileCache: ModuleFileCache,
scopeSession: ScopeSession = ScopeSession() scopeSession: ScopeSession,
) { ) {
firFileBuilder.runCustomResolveUnderLock(firFile, moduleFileCache) { firFileBuilder.runCustomResolveUnderLock(firFile, moduleFileCache) {
val transformer = FirFileAnnotationsResolveTransformer(firFile.session, scopeSession) val transformer = FirFileAnnotationsResolveTransformer(firFile.session, scopeSession)
@@ -118,16 +118,19 @@ internal class FirLazyDeclarationResolver(
) { ) {
if (fromPhase >= toPhase) return if (fromPhase >= toPhase) return
val nonLazyPhase = minOf(toPhase, LAST_NON_LAZY_PHASE) val nonLazyPhase = minOf(toPhase, LAST_NON_LAZY_PHASE)
val scopeSession = ScopeSession()
if (fromPhase < nonLazyPhase) { if (fromPhase < nonLazyPhase) {
firFileBuilder.runResolveWithoutLock( firFileBuilder.runResolveWithoutLock(
containerFirFile, containerFirFile,
fromPhase = fromPhase, fromPhase = fromPhase,
toPhase = nonLazyPhase, toPhase = nonLazyPhase,
scopeSession = scopeSession,
checkPCE = checkPCE checkPCE = checkPCE
) )
} }
if (toPhase <= nonLazyPhase) return if (toPhase <= nonLazyPhase) return
resolveFileAnnotations(containerFirFile, moduleFileCache) resolveFileAnnotations(containerFirFile, moduleFileCache, scopeSession)
val nonLocalDeclarationToResolve = firDeclarationToResolve.getNonLocalDeclarationToResolve(provider, moduleFileCache) val nonLocalDeclarationToResolve = firDeclarationToResolve.getNonLocalDeclarationToResolve(provider, moduleFileCache)
val designation = nonLocalDeclarationToResolve.getDesignation(containerFirFile, provider, moduleFileCache) val designation = nonLocalDeclarationToResolve.getDesignation(containerFirFile, provider, moduleFileCache)
@@ -137,7 +140,6 @@ internal class FirLazyDeclarationResolver(
} }
var currentPhase = nonLazyPhase var currentPhase = nonLazyPhase
val scopeSession = ScopeSession()
while (currentPhase < toPhase) { while (currentPhase < toPhase) {
currentPhase = currentPhase.next currentPhase = currentPhase.next