[AA K2] implement symbol pointers for enum entry members without psi

^KT-54311
This commit is contained in:
Dmitrii Gridin
2022-11-04 13:25:06 +01:00
committed by Space Team
parent 1dfca7aca9
commit 897a5b085d
11 changed files with 55 additions and 16 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2022 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. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@@ -83,12 +83,11 @@ internal class KtFirSymbolContainingDeclarationProvider(
withSymbolAttachment("symbolForContainingPsi", symbol, analysisSession) withSymbolAttachment("symbolForContainingPsi", symbol, analysisSession)
} }
KtFakeSourceElementKind.ImplicitConstructor -> KtFakeSourceElementKind.ImplicitConstructor -> return source.psi as KtDeclaration
return source.psi as KtDeclaration
KtFakeSourceElementKind.PropertyFromParameter -> return source.psi?.parentOfType<KtPrimaryConstructor>()!! KtFakeSourceElementKind.PropertyFromParameter -> return source.psi?.parentOfType<KtPrimaryConstructor>()!!
KtFakeSourceElementKind.DefaultAccessor -> return source.psi as KtProperty KtFakeSourceElementKind.DefaultAccessor -> return source.psi as KtProperty
KtFakeSourceElementKind.ItLambdaParameter -> return source.psi as KtFunctionLiteral KtFakeSourceElementKind.ItLambdaParameter -> return source.psi as KtFunctionLiteral
KtFakeSourceElementKind.EnumInitializer -> return source.psi as KtEnumEntry
KtRealSourceElementKind -> source.psi!! KtRealSourceElementKind -> source.psi!!
else -> else ->
buildErrorWithAttachment("errorWithAttachment FirSourceElement: kind=${source.kind} element=${source.psi!!::class.simpleName}") { buildErrorWithAttachment("errorWithAttachment FirSourceElement: kind=${source.kind} element=${source.psi!!::class.simpleName}") {
@@ -6,9 +6,12 @@
package org.jetbrains.kotlin.analysis.api.fir.symbols package org.jetbrains.kotlin.analysis.api.fir.symbols
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.KtFakeSourceElementKind
import org.jetbrains.kotlin.analysis.api.fir.KtSymbolByFirBuilder import org.jetbrains.kotlin.analysis.api.fir.KtSymbolByFirBuilder
import org.jetbrains.kotlin.analysis.api.fir.annotations.KtFirAnnotationListForDeclaration import org.jetbrains.kotlin.analysis.api.fir.annotations.KtFirAnnotationListForDeclaration
import org.jetbrains.kotlin.analysis.api.fir.findPsi import org.jetbrains.kotlin.analysis.api.fir.findPsi
import org.jetbrains.kotlin.analysis.api.fir.symbols.pointers.KtFirEnumEntryInitializerSymbolPointer
import org.jetbrains.kotlin.analysis.api.fir.symbols.pointers.requireOwnerPointer
import org.jetbrains.kotlin.analysis.api.fir.utils.cached import org.jetbrains.kotlin.analysis.api.fir.utils.cached
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
@@ -35,8 +38,12 @@ internal class KtFirAnonymousObjectSymbol(
override val superTypes: List<KtType> by cached { firSymbol.superTypesList(builder) } override val superTypes: List<KtType> by cached { firSymbol.superTypesList(builder) }
override fun createPointer(): KtSymbolPointer<KtAnonymousObjectSymbol> = withValidityAssertion { override fun createPointer(): KtSymbolPointer<KtAnonymousObjectSymbol> = withValidityAssertion {
KtPsiBasedSymbolPointer.createForSymbolFromSource(this) KtPsiBasedSymbolPointer.createForSymbolFromSource(this)?.let { return it }
?: throw CanNotCreateSymbolPointerForLocalLibraryDeclarationException("Cannot create pointer for KtFirAnonymousObjectSymbol") if (firSymbol.source?.kind == KtFakeSourceElementKind.EnumInitializer) {
return KtFirEnumEntryInitializerSymbolPointer(requireOwnerPointer())
}
throw CanNotCreateSymbolPointerForLocalLibraryDeclarationException("Cannot create pointer for KtFirAnonymousObjectSymbol")
} }
override fun equals(other: Any?): Boolean = symbolEquals(other) override fun equals(other: Any?): Boolean = symbolEquals(other)
@@ -135,7 +135,7 @@ internal class KtFirKotlinPropertySymbol(
KtFirMemberPropertySymbolPointer( KtFirMemberPropertySymbolPointer(
requireOwnerPointer(), requireOwnerPointer(),
firSymbol.name, firSymbol.name,
firSymbol.createSignature() firSymbol.createSignature(),
) )
else -> throw UnsupportedSymbolKind(this::class, kind) else -> throw UnsupportedSymbolKind(this::class, kind)
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2022 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.api.fir.symbols.pointers
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.fir.KtFirAnalysisSession
import org.jetbrains.kotlin.analysis.api.fir.utils.firSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtAnonymousObjectSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtEnumEntrySymbol
import org.jetbrains.kotlin.analysis.api.symbols.pointers.KtSymbolPointer
import org.jetbrains.kotlin.fir.expressions.FirAnonymousObjectExpression
internal class KtFirEnumEntryInitializerSymbolPointer(
private val ownerPointer: KtSymbolPointer<KtEnumEntrySymbol>,
) : KtSymbolPointer<KtAnonymousObjectSymbol>() {
@Deprecated("Consider using org.jetbrains.kotlin.analysis.api.KtAnalysisSession.restoreSymbol")
override fun restoreSymbol(analysisSession: KtAnalysisSession): KtAnonymousObjectSymbol? {
require(analysisSession is KtFirAnalysisSession)
val owner = with(analysisSession) {
ownerPointer.restoreSymbol()
}
val initializer = owner?.firSymbol?.fir?.initializer as? FirAnonymousObjectExpression ?: return null
return analysisSession.firSymbolBuilder.classifierBuilder.buildAnonymousObjectSymbol(initializer.anonymousObject.symbol)
}
}
@@ -24,8 +24,10 @@ internal class KtFirMemberPropertySymbolPointer(
candidates: FirScope, candidates: FirScope,
firSession: FirSession firSession: FirSession
): KtKotlinPropertySymbol? { ): KtKotlinPropertySymbol? {
val firProperty = candidates.findDeclarationWithSignature<FirProperty>(signature, firSession) { processPropertiesByName(name, it) } val firProperty = candidates.findDeclarationWithSignature<FirProperty>(signature, firSession) {
?: return null processPropertiesByName(name, it)
} ?: return null
return firSymbolBuilder.variableLikeBuilder.buildVariableSymbol(firProperty.symbol) as? KtKotlinPropertySymbol return firSymbolBuilder.variableLikeBuilder.buildVariableSymbol(firProperty.symbol) as? KtKotlinPropertySymbol
} }
} }
@@ -29,10 +29,11 @@ internal inline fun <reified D : FirDeclaration> FirScope.findDeclarationWithSig
var foundSymbol: D? = null var foundSymbol: D? = null
processor { symbol -> processor { symbol ->
val declaration = symbol.fir val declaration = symbol.fir
if (declaration is D && signatureComposer.composeSignature(declaration) == signature) { if (declaration is D && signatureComposer.composeSignature(declaration, allowLocalClasses = true) == signature) {
foundSymbol = declaration foundSymbol = declaration
} }
} }
return foundSymbol return foundSymbol
} }
@@ -66,7 +67,7 @@ internal fun FirBasedSymbol<*>.createSignature(): IdSignature =
internal fun FirDeclaration.createSignature(): IdSignature { internal fun FirDeclaration.createSignature(): IdSignature {
val signatureComposer = moduleData.session.ideSessionComponents.signatureComposer val signatureComposer = moduleData.session.ideSessionComponents.signatureComposer
return signatureComposer.composeSignature(this) return signatureComposer.composeSignature(this, allowLocalClasses = true)
?: error("Could not compose signature for ${this.renderWithType()}, looks like it is private or local") ?: error("Could not compose signature for ${this.renderWithType()}, looks like it is private or local")
} }
@@ -1,4 +1,4 @@
// DO_NOT_CHECK_NON_PSI_SYMBOL_RESTORE // DO_NOT_CHECK_NON_PSI_SYMBOL_RESTORE_K1
package test package test
enum class E { enum class E {
@@ -1,4 +1,4 @@
// DO_NOT_CHECK_NON_PSI_SYMBOL_RESTORE // DO_NOT_CHECK_NON_PSI_SYMBOL_RESTORE_K1
enum class MyEnumClass { enum class MyEnumClass {
FirstEntry { FirstEntry {
@@ -1,4 +1,4 @@
// DO_NOT_CHECK_NON_PSI_SYMBOL_RESTORE // DO_NOT_CHECK_NON_PSI_SYMBOL_RESTORE_K1
enum class MyEnumClass { enum class MyEnumClass {
FirstEntry { FirstEntry {
@@ -1,4 +1,4 @@
// DO_NOT_CHECK_NON_PSI_SYMBOL_RESTORE // DO_NOT_CHECK_NON_PSI_SYMBOL_RESTORE_K1
enum class MyEnumClass { enum class MyEnumClass {
FirstEntry { FirstEntry {
@@ -1,4 +1,5 @@
// DO_NOT_CHECK_NON_PSI_SYMBOL_RESTORE // DO_NOT_CHECK_NON_PSI_SYMBOL_RESTORE_K1
enum class Style(val value: String) { enum class Style(val value: String) {
SHEET("foo") { SHEET("foo") {
override val exitAnimation: String override val exitAnimation: String