From 23e40693a3f77af4eae0e79689b6164c23c12d77 Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Wed, 22 Mar 2023 13:06:53 +0100 Subject: [PATCH] [LL FIR] add api to FirLazyDeclarationResolver to resolve class with callable members ^KT-56543 --- .../api/fir/LLFirLazyDeclarationResolver.kt | 9 ++++ ...FirDummyCompilerLazyDeclarationResolver.kt | 4 +- .../fir/symbols/FirLazyDeclarationResolver.kt | 45 ++++++++++++++++--- ...azyDeclarationResolverWithPhaseChecking.kt | 7 ++- 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/LLFirLazyDeclarationResolver.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/LLFirLazyDeclarationResolver.kt index ff8b2f80a26..c819d9aef8d 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/LLFirLazyDeclarationResolver.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/LLFirLazyDeclarationResolver.kt @@ -7,9 +7,11 @@ package org.jetbrains.kotlin.analysis.low.level.api.fir import org.jetbrains.kotlin.analysis.low.level.api.fir.sessions.LLFirResolvableModuleSession import org.jetbrains.kotlin.fir.ThreadSafeMutableState +import org.jetbrains.kotlin.fir.declarations.FirRegularClass import org.jetbrains.kotlin.fir.declarations.FirResolvePhase import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.FirLazyDeclarationResolver +import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol @ThreadSafeMutableState internal class LLFirLazyDeclarationResolver : FirLazyDeclarationResolver() { @@ -27,4 +29,11 @@ internal class LLFirLazyDeclarationResolver : FirLazyDeclarationResolver() { toPhase = toPhase, ) } + + override fun lazyResolveToPhaseWithCallableMembers(symbol: FirClassSymbol<*>, toPhase: FirResolvePhase) { + val fir = symbol.fir as? FirRegularClass ?: return + val session = fir.moduleData.session + if (session !is LLFirResolvableModuleSession) return + // TODO: should be implemented in the context of KT-56543 + } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirDummyCompilerLazyDeclarationResolver.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirDummyCompilerLazyDeclarationResolver.kt index a2f1457f00d..76aff0cfa80 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirDummyCompilerLazyDeclarationResolver.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirDummyCompilerLazyDeclarationResolver.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2023 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. */ @@ -8,10 +8,12 @@ package org.jetbrains.kotlin.fir.resolve.transformers import org.jetbrains.kotlin.fir.declarations.FirResolvePhase import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.FirLazyDeclarationResolver +import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol object FirDummyCompilerLazyDeclarationResolver : FirLazyDeclarationResolver() { override fun startResolvingPhase(phase: FirResolvePhase) {} override fun finishResolvingPhase(phase: FirResolvePhase) {} override fun lazyResolveToPhase(symbol: FirBasedSymbol<*>, toPhase: FirResolvePhase) {} + override fun lazyResolveToPhaseWithCallableMembers(symbol: FirClassSymbol<*>, toPhase: FirResolvePhase) {} } diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/FirLazyDeclarationResolver.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/FirLazyDeclarationResolver.kt index 60d06caaab3..0de05d73558 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/FirLazyDeclarationResolver.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/FirLazyDeclarationResolver.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2023 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. */ @@ -7,8 +7,10 @@ package org.jetbrains.kotlin.fir.symbols import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.FirSessionComponent +import org.jetbrains.kotlin.fir.declarations.FirClass import org.jetbrains.kotlin.fir.declarations.FirDeclaration import org.jetbrains.kotlin.fir.declarations.FirResolvePhase +import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol /** * A component to lazy resolve [FirBasedSymbol] to the required phase. @@ -24,7 +26,7 @@ abstract class FirLazyDeclarationResolver : FirSessionComponent { abstract fun finishResolvingPhase(phase: FirResolvePhase) - fun disableLazyResolveContractChecks(){ + fun disableLazyResolveContractChecks() { lazyResolveContractChecksEnabled = false } @@ -39,10 +41,13 @@ abstract class FirLazyDeclarationResolver : FirSessionComponent { } abstract fun lazyResolveToPhase(symbol: FirBasedSymbol<*>, toPhase: FirResolvePhase) + abstract fun lazyResolveToPhaseWithCallableMembers(symbol: FirClassSymbol<*>, toPhase: FirResolvePhase) } val FirSession.lazyDeclarationResolver: FirLazyDeclarationResolver by FirSession.sessionComponentAccessor() +private val FirDeclaration.lazyDeclarationResolver get() = moduleData.session.lazyDeclarationResolver + /** * Lazy resolve [FirBasedSymbol] to [FirResolvePhase]. * @@ -58,9 +63,7 @@ val FirSession.lazyDeclarationResolver: FirLazyDeclarationResolver by FirSession * @param toPhase the minimum phase, the declaration should be resolved to after an execution of the [lazyResolveToPhase] */ fun FirBasedSymbol<*>.lazyResolveToPhase(toPhase: FirResolvePhase) { - val session = fir.moduleData.session - val phaseManager = session.lazyDeclarationResolver - phaseManager.lazyResolveToPhase(this, toPhase) + fir.lazyDeclarationResolver.lazyResolveToPhase(this, toPhase) } /** @@ -71,3 +74,35 @@ fun FirBasedSymbol<*>.lazyResolveToPhase(toPhase: FirResolvePhase) { fun FirDeclaration.lazyResolveToPhase(toPhase: FirResolvePhase) { symbol.lazyResolveToPhase(toPhase) } + +/** + * Lazy resolve [FirClassSymbol] and its callable members to [FirResolvePhase]. + * + * Might resolve additional required declarations. + * + * @receiver [FirClassSymbol] which should be resolved and which callable members should be resolved + * @param toPhase the minimum phase, the declaration and callable members should be resolved + * to after an execution of the [lazyResolveToPhaseWithCallableMembers] + * + * Can be used instead of [lazyResolveToPhase] to avoid extra resolve calls. + * Effectively the same as: + * ``` + * kclass.lazyResolveToPhase(phase) + * kclass.callableDeclarations.forEach { it.lazyResolveToPhase(phase) } + * ``` + * + * @see lazyResolveToPhase + */ + +fun FirClassSymbol<*>.lazyResolveToPhaseWithCallableMembers(toPhase: FirResolvePhase) { + fir.lazyDeclarationResolver.lazyResolveToPhaseWithCallableMembers(this, toPhase) +} + +/** + * Lazy resolve [FirClass] and its callable members to [FirResolvePhase]. + * + * @see lazyResolveToPhaseWithCallableMembers + */ +fun FirClass.lazyResolveToPhaseWithCallableMembers(toPhase: FirResolvePhase) { + symbol.lazyResolveToPhaseWithCallableMembers(toPhase) +} diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/handlers/FirCompilerLazyDeclarationResolverWithPhaseChecking.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/handlers/FirCompilerLazyDeclarationResolverWithPhaseChecking.kt index 2dea3682a88..05d81d5102c 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/handlers/FirCompilerLazyDeclarationResolverWithPhaseChecking.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/fir/handlers/FirCompilerLazyDeclarationResolverWithPhaseChecking.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2023 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. */ @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.test.frontend.fir.handlers import org.jetbrains.kotlin.fir.declarations.FirResolvePhase import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.FirLazyDeclarationResolver +import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol class FirCompilerLazyDeclarationResolverWithPhaseChecking : FirLazyDeclarationResolver() { @@ -22,6 +23,10 @@ class FirCompilerLazyDeclarationResolverWithPhaseChecking : FirLazyDeclarationRe checkIfCanLazyResolveToPhase(symbol, toPhase) } + override fun lazyResolveToPhaseWithCallableMembers(symbol: FirClassSymbol<*>, toPhase: FirResolvePhase) { + checkIfCanLazyResolveToPhase(symbol, toPhase) + } + override fun startResolvingPhase(phase: FirResolvePhase) { check(currentTransformerPhase == null) currentTransformerPhase = phase