[LL FIR] adapt LLFirTargetResolver for new FirDesignation

We should maintain `containingDeclarations` to be consistent with
the new ` FirDesignation `

^KT-65345 Fixed
This commit is contained in:
Dmitrii Gridin
2024-01-31 20:02:31 +01:00
committed by Space Team
parent 9b60e5ca06
commit 9589043a05
9 changed files with 91 additions and 54 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 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.
*/
@@ -43,22 +43,24 @@ internal abstract class LLFirAbstractBodyTargetResolver(
}
}
override fun withScript(firScript: FirScript, action: () -> Unit) {
transformer.declarationsTransformer?.withScript(firScript) {
action()
firScript
}
}
override fun withFile(firFile: FirFile, action: () -> Unit) {
@Deprecated("Should never be called directly, only for override purposes, please use withFile", level = DeprecationLevel.ERROR)
override fun withContainingFile(firFile: FirFile, action: () -> Unit) {
transformer.declarationsTransformer?.withFile(firFile) {
action()
firFile
}
}
@Deprecated("Should never be called directly, only for override purposes, please use withScript", level = DeprecationLevel.ERROR)
override fun withContainingScript(firScript: FirScript, action: () -> Unit) {
transformer.declarationsTransformer?.withScript(firScript) {
action()
firScript
}
}
@Deprecated("Should never be called directly, only for override purposes, please use withRegularClass", level = DeprecationLevel.ERROR)
override fun withRegularClassImpl(firClass: FirRegularClass, action: () -> Unit) {
override fun withContainingRegularClass(firClass: FirRegularClass, action: () -> Unit) {
transformer.declarationsTransformer?.context?.withContainingClass(firClass) {
transformer.declarationsTransformer?.withRegularClass(firClass) {
action()
@@ -68,7 +70,7 @@ internal abstract class LLFirAbstractBodyTargetResolver(
}
protected fun <T : FirElementWithResolveState> resolve(target: T, keeper: StateKeeper<T, FirDesignation>) {
val firDesignation = FirDesignation(nestedClassesStack, target)
val firDesignation = FirDesignation(containingDeclarations, target)
resolveWithKeeper(target, firDesignation, keeper, { FirLazyBodiesCalculator.calculateBodies(firDesignation) }) {
rawResolve(target)
}
@@ -175,7 +175,7 @@ private class LLFirAnnotationArgumentsTargetResolver(
}
@Deprecated("Should never be called directly, only for override purposes, please use withRegularClass", level = DeprecationLevel.ERROR)
override fun withRegularClassImpl(firClass: FirRegularClass, action: () -> Unit) {
override fun withContainingRegularClass(firClass: FirRegularClass, action: () -> Unit) {
transformer.declarationsTransformer.withRegularClass(firClass) {
action()
firClass
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 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.
*/
@@ -90,12 +90,13 @@ private class LLFirCompilerRequiredAnnotationsTargetResolver(
private val llFirComputationSession: LLFirCompilerRequiredAnnotationsComputationSession
get() = transformer.annotationTransformer.computationSession as LLFirCompilerRequiredAnnotationsComputationSession
override fun withFile(firFile: FirFile, action: () -> Unit) {
@Deprecated("Should never be called directly, only for override purposes, please use withFile", level = DeprecationLevel.ERROR)
override fun withContainingFile(firFile: FirFile, action: () -> Unit) {
transformer.annotationTransformer.withFileAndFileScopes(firFile, action)
}
@Deprecated("Should never be called directly, only for override purposes, please use withRegularClass", level = DeprecationLevel.ERROR)
override fun withRegularClassImpl(firClass: FirRegularClass, action: () -> Unit) {
override fun withContainingRegularClass(firClass: FirRegularClass, action: () -> Unit) {
transformer.annotationTransformer.withRegularClass(firClass, action)
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 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.
*/
@@ -116,30 +116,34 @@ private class LLFirContractsTargetResolver(
}
}
override fun withScript(firScript: FirScript, action: () -> Unit) {
val actionWithCollector = actionWithContextCollector(action) { collector, context ->
collector.addDeclarationContext(firScript, context)
}
super.withScript(firScript, actionWithCollector)
}
override fun withFile(firFile: FirFile, action: () -> Unit) {
@Deprecated("Should never be called directly, only for override purposes, please use withFile", level = DeprecationLevel.ERROR)
override fun withContainingFile(firFile: FirFile, action: () -> Unit) {
val actionWithCollector = actionWithContextCollector(action) { collector, context ->
collector.addFileContext(firFile, context.towerDataContext)
}
super.withFile(firFile, actionWithCollector)
@Suppress("DEPRECATION_ERROR")
super.withContainingFile(firFile, actionWithCollector)
}
@Deprecated("Should never be called directly, only for override purposes, please use withScript", level = DeprecationLevel.ERROR)
override fun withContainingScript(firScript: FirScript, action: () -> Unit) {
val actionWithCollector = actionWithContextCollector(action) { collector, context ->
collector.addDeclarationContext(firScript, context)
}
@Suppress("DEPRECATION_ERROR")
super.withContainingScript(firScript, actionWithCollector)
}
@Deprecated("Should never be called directly, only for override purposes, please use withRegularClass", level = DeprecationLevel.ERROR)
override fun withRegularClassImpl(firClass: FirRegularClass, action: () -> Unit) {
override fun withContainingRegularClass(firClass: FirRegularClass, action: () -> Unit) {
val actionWithCollector = actionWithContextCollector(action) { collector, context ->
collector.addDeclarationContext(firClass, context)
}
@Suppress("DEPRECATION_ERROR")
super.withRegularClassImpl(firClass, actionWithCollector)
super.withContainingRegularClass(firClass, actionWithCollector)
}
private fun collectTowerDataContext(target: FirElementWithResolveState) {
@@ -48,7 +48,7 @@ private class LLFirExpectActualMatchingTargetResolver(
private val enabled = resolveTargetSession.languageVersionSettings.supportsFeature(LanguageFeature.MultiPlatformProjects)
@Deprecated("Should never be called directly, only for override purposes, please use withRegularClass", level = DeprecationLevel.ERROR)
override fun withRegularClassImpl(firClass: FirRegularClass, action: () -> Unit) {
override fun withContainingRegularClass(firClass: FirRegularClass, action: () -> Unit) {
if (enabled) {
// Resolve outer classes before resolving inner declarations. It's the requirement of FirExpectActualResolver
firClass.lazyResolveToPhase(resolverPhase.previous)
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 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.
*/
@@ -116,7 +116,7 @@ private class LLFirStatusTargetResolver(
private val transformer = Transformer(resolveTargetSession, scopeSession)
@Deprecated("Should never be called directly, only for override purposes, please use withRegularClass", level = DeprecationLevel.ERROR)
override fun withRegularClassImpl(firClass: FirRegularClass, action: () -> Unit) {
override fun withContainingRegularClass(firClass: FirRegularClass, action: () -> Unit) {
doResolveWithoutLock(firClass)
transformer.storeClass(firClass) {
action()
@@ -213,7 +213,7 @@ private class LLFirStatusTargetResolver(
if (resolveMode.resolveSupertypes) {
transformer.storeClass(firClass) {
withClassInStack(firClass) {
withContainingDeclaration(firClass) {
resolveCallableMembers(firClass)
}
@@ -91,7 +91,7 @@ private class LLFirSuperTypeTargetResolver(
}
@Deprecated("Should never be called directly, only for override purposes, please use withRegularClass", level = DeprecationLevel.ERROR)
override fun withRegularClassImpl(firClass: FirRegularClass, action: () -> Unit) {
override fun withContainingRegularClass(firClass: FirRegularClass, action: () -> Unit) {
supertypeResolver.withClass(firClass) {
doResolveWithoutLock(firClass)
action()
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.fir.FirElementWithResolveState
import org.jetbrains.kotlin.fir.FirFileAnnotationsContainer
import org.jetbrains.kotlin.fir.declarations.FirCallableDeclaration
import org.jetbrains.kotlin.fir.declarations.FirConstructor
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin
import org.jetbrains.kotlin.fir.declarations.FirField
import org.jetbrains.kotlin.fir.declarations.FirFile
@@ -42,26 +43,38 @@ internal abstract class LLFirTargetResolver(
) : LLFirResolveTargetVisitor {
val resolveTargetSession: LLFirSession get() = resolveTarget.session
private val _nestedClassesStack = mutableListOf<FirRegularClass>()
private val _containingDeclarations = mutableListOf<FirDeclaration>()
val nestedClassesStack: List<FirRegularClass> get() = _nestedClassesStack
val containingDeclarations: List<FirDeclaration> get() = _containingDeclarations
/**
* @param context used as a context in the case of exception
* @return the last class from [nestedClassesStack]
* @return the last class from [containingDeclarations]
*/
fun containingClass(context: FirElement): FirRegularClass = nestedClassesStack.lastOrNull()
?: errorWithAttachment("Containing class is not found") {
fun containingClass(context: FirElement): FirRegularClass {
val containingDeclaration = containingDeclarations.lastOrNull() ?: errorWithAttachment("Containing declaration is not found") {
withFirEntry("context", context)
withEntry("originalTarget", resolveTarget.toString())
}
protected inline fun withClassInStack(clazz: FirRegularClass, action: () -> Unit) {
_nestedClassesStack += clazz
action()
val removed = _nestedClassesStack.removeLast()
checkWithAttachment(removed === clazz, { "Unexpected state"}) {
withFirEntry("expected", clazz)
withFirEntry("actual", removed)
requireWithAttachment(
containingDeclaration is FirRegularClass,
{ "${FirRegularClass::class.simpleName} expected, but ${containingDeclaration::class.simpleName} found" },
)
return containingDeclaration
}
protected inline fun withContainingDeclaration(declaration: FirDeclaration, action: () -> Unit) {
_containingDeclarations += declaration
try {
action()
} finally {
val removed = _containingDeclarations.removeLast()
checkWithAttachment(removed === declaration, { "Unexpected state" }) {
withFirEntry("expected", declaration)
withFirEntry("actual", removed)
}
}
}
@@ -129,23 +142,39 @@ internal abstract class LLFirTargetResolver(
}
}
override fun withFile(firFile: FirFile, action: () -> Unit) {
final override fun withFile(firFile: FirFile, action: () -> Unit) {
withContainingDeclaration(firFile) {
@Suppress("DEPRECATION_ERROR")
withContainingFile(firFile, action)
}
}
@Deprecated("Should never be called directly, only for override purposes, please use withFile", level = DeprecationLevel.ERROR)
protected open fun withContainingFile(firFile: FirFile, action: () -> Unit) {
action()
}
override fun withScript(firScript: FirScript, action: () -> Unit) {
final override fun withScript(firScript: FirScript, action: () -> Unit) {
withContainingDeclaration(firScript) {
@Suppress("DEPRECATION_ERROR")
withContainingScript(firScript, action)
}
}
@Deprecated("Should never be called directly, only for override purposes, please use withScript", level = DeprecationLevel.ERROR)
protected open fun withContainingScript(firScript: FirScript, action: () -> Unit) {
action()
}
@Deprecated("Should never be called directly, only for override purposes, please use withRegularClass", level = DeprecationLevel.ERROR)
protected open fun withRegularClassImpl(firClass: FirRegularClass, action: () -> Unit) {
protected open fun withContainingRegularClass(firClass: FirRegularClass, action: () -> Unit) {
action()
}
@Suppress("DEPRECATION_ERROR")
final override fun withRegularClass(firClass: FirRegularClass, action: () -> Unit) {
withClassInStack(firClass) {
withRegularClassImpl(firClass, action)
withContainingDeclaration(firClass) {
@Suppress("DEPRECATION_ERROR")
withContainingRegularClass(firClass, action)
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 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.
*/
@@ -81,12 +81,13 @@ private class LLFirTypeTargetResolver(
}
}
override fun withFile(firFile: FirFile, action: () -> Unit) {
@Deprecated("Should never be called directly, only for override purposes, please use withFile", level = DeprecationLevel.ERROR)
override fun withContainingFile(firFile: FirFile, action: () -> Unit) {
transformer.withFileScope(firFile, action)
}
@Deprecated("Should never be called directly, only for override purposes, please use withRegularClass", level = DeprecationLevel.ERROR)
override fun withRegularClassImpl(firClass: FirRegularClass, action: () -> Unit) {
override fun withContainingRegularClass(firClass: FirRegularClass, action: () -> Unit) {
firClass.lazyResolveToPhase(resolverPhase.previous)
transformer.withClassDeclarationCleanup(firClass) {
performCustomResolveUnderLock(firClass) {