[FIR] builder: provide containingDeclarationSymbol

We cannot use only non-local declarations as anchors due to the same
resolution logic between member declarations of local classes, so we
have to support such cases as well

^KT-63042
This commit is contained in:
Dmitrii Gridin
2023-11-13 14:40:17 +01:00
committed by Space Team
parent be4bc81b1b
commit c5cba4c053
45 changed files with 3767 additions and 1295 deletions
@@ -249,10 +249,9 @@ object LowLevelFirApiFacadeForResolveOnAir {
firResolveSession: LLFirResolvableResolveSession,
): FirAnnotation {
val annotationCall = buildFileFirAnnotation(
session = firFile.moduleData.session,
baseScopeProvider = firFile.moduleData.session.kotlinScopeProvider,
firFile = firFile,
fileAnnotation = annotationEntry,
replacement = replacement
replacement = replacement,
)
val fileAnnotationsContainer = buildFileAnnotationsContainer {
@@ -81,7 +81,7 @@ internal object FirLazyBodiesCalculator {
val builder = PsiRawFirBuilder(session, baseScopeProvider = session.kotlinScopeProvider)
val ktAnnotationEntry = annotationCall.psi as KtAnnotationEntry
builder.context.packageFqName = ktAnnotationEntry.containingKtFile.packageFqName
val newAnnotationCall = builder.buildAnnotationCall(ktAnnotationEntry)
val newAnnotationCall = builder.buildAnnotationCall(ktAnnotationEntry, annotationCall.containingDeclarationSymbol)
return newAnnotationCall.argumentList
}
@@ -1,35 +1,39 @@
/*
* 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.
*/
package org.jetbrains.kotlin.analysis.low.level.api.fir.lazy.resolve
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.builder.PsiRawFirBuilder
import org.jetbrains.kotlin.fir.declarations.FirFile
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
import org.jetbrains.kotlin.fir.scopes.FirScopeProvider
import org.jetbrains.kotlin.fir.scopes.kotlinScopeProvider
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtElement
internal fun buildFileFirAnnotation(
session: FirSession,
baseScopeProvider: FirScopeProvider,
firFile: FirFile,
fileAnnotation: KtAnnotationEntry,
replacement: RawFirReplacement? = null
replacement: RawFirReplacement,
): FirAnnotation {
val replacementApplier = replacement?.Applier()
val session = firFile.moduleData.session
val baseScopeProvider = firFile.moduleData.session.kotlinScopeProvider
val replacementApplier = replacement.Applier()
val builder = object : PsiRawFirBuilder(session, baseScopeProvider) {
inner class VisitorWithReplacement : Visitor() {
override fun convertElement(element: KtElement, original: FirElement?): FirElement? =
super.convertElement(replacementApplier?.tryReplace(element) ?: element, original)
super.convertElement(replacementApplier.tryReplace(element), original)
}
}
builder.context.packageFqName = fileAnnotation.containingKtFile.packageFqName
val result = builder.VisitorWithReplacement().convertElement(fileAnnotation, null) as FirAnnotation
replacementApplier?.ensureApplied()
val visitor = builder.VisitorWithReplacement()
val result = builder.withContainerSymbol(firFile.symbol) {
visitor.convertElement(fileAnnotation, null) as FirAnnotation
}
replacementApplier.ensureApplied()
return result
}
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.name.NameUtils
import org.jetbrains.kotlin.psi
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.hasExpectModifier
import org.jetbrains.kotlin.util.PrivateForInline
import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment
internal class RawFirNonLocalDeclarationBuilder private constructor(
@@ -76,16 +77,17 @@ internal class RawFirNonLocalDeclarationBuilder private constructor(
else -> null
}
return build(session, scopeProvider, designation, rootNonLocalDeclaration, functionsToRebind)
return build(session, scopeProvider, designation, rootNonLocalDeclaration, functionsToRebind, rebindContainingSymbol = true)
}
fun build(
private fun build(
session: FirSession,
scopeProvider: FirScopeProvider,
designation: FirDesignation,
rootNonLocalDeclaration: KtElement,
functionsToRebind: Set<FirFunction>? = null,
replacementApplier: RawFirReplacement.Applier? = null
replacementApplier: RawFirReplacement.Applier? = null,
rebindContainingSymbol: Boolean = false,
): FirDeclaration {
check(rootNonLocalDeclaration is KtDeclaration || rootNonLocalDeclaration is KtCodeFragment)
@@ -98,6 +100,11 @@ internal class RawFirNonLocalDeclarationBuilder private constructor(
replacementApplier = replacementApplier
)
builder.context.packageFqName = rootNonLocalDeclaration.containingKtFile.packageFqName
if (rebindContainingSymbol) {
@OptIn(PrivateForInline::class)
builder.context.forcedContainerSymbol = designation.target.symbol
}
return builder.moveNext(designation.path.iterator(), containingClass = null)
}
}
@@ -299,10 +306,9 @@ internal class RawFirNonLocalDeclarationBuilder private constructor(
if (superTypeListEntry is KtDelegatedSuperTypeEntry) {
val expectedName = NameUtils.delegateFieldName(index)
if (originalDeclaration.name == expectedName) {
return buildFieldForSupertypeDelegate(
superTypeListEntry, superTypeListEntry.typeReference.toFirOrErrorType(), index
)
return buildFieldForSupertypeDelegate(superTypeListEntry, type = null, index)
}
index++
}
}