AA: introduce new APIs to get containing file (symbol) and JvmClassName

^KTIJ-27686
This commit is contained in:
Jinseong Jeon
2023-11-17 11:08:21 -08:00
committed by Space Team
parent 5b6363b0df
commit 797174ee1f
24 changed files with 891 additions and 37 deletions
@@ -6,14 +6,20 @@
package org.jetbrains.kotlin.analysis.api.components
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtDeclarationSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtFileSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbol
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithKind
import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.resolve.jvm.JvmClassName
public abstract class KtSymbolContainingDeclarationProvider : KtAnalysisSessionComponent() {
public abstract fun getContainingDeclaration(symbol: KtSymbol): KtDeclarationSymbol?
public abstract fun getContainingFileSymbol(symbol: KtSymbol): KtFileSymbol?
public abstract fun getContainingJvmClassName(symbol: KtCallableSymbol): JvmClassName?
public abstract fun getContainingModule(symbol: KtSymbol): KtModule
}
@@ -27,6 +33,25 @@ public interface KtSymbolContainingDeclarationProviderMixIn : KtAnalysisSessionM
public fun KtSymbol.getContainingSymbol(): KtDeclarationSymbol? =
withValidityAssertion { analysisSession.containingDeclarationProvider.getContainingDeclaration(this) }
/**
* Returns containing [KtFile] as [KtFileSymbol]
*
* Caveat: returns null if the given symbol is already [KtFileSymbol], since there is no containing file.
*/
public fun KtSymbol.getContainingFileSymbol(): KtFileSymbol? =
withValidityAssertion { analysisSession.containingDeclarationProvider.getContainingFileSymbol(this) }
/**
* Returns containing class's [JvmClassName] if any for [KtCallableSymbol]
*
* even for deserialized callables! (which is useful to look up the containing facade in [PsiElement])
* for regular, non-local callables from source, it is a mere conversion of [ClassId] inside [CallableId]
*
* Note that this API is applicable for common or JVM modules only.
*/
public fun KtCallableSymbol.getContainingJvmClassName(): JvmClassName? =
withValidityAssertion { analysisSession.containingDeclarationProvider.getContainingJvmClassName(this) }
public fun KtSymbol.getContainingModule(): KtModule =
withValidityAssertion { analysisSession.containingDeclarationProvider.getContainingModule(this) }
}
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.resolve.deprecation.DeprecationInfo
import org.jetbrains.kotlin.types.Variance
@@ -71,9 +72,24 @@ public class DebugSymbolRenderer(
KtSymbolContainingDeclarationProviderMixIn::class
.declaredMemberExtensionFunctions
.filter { it.name == "getContainingModule" }
.filterNot {
// Rendering a containing symbol is prone to stack overflow.
// * function symbol will render its value parameter symbol(s)
// whose containing symbol is that function symbol.
// * property symbol contains accessor symbol(s) and/or backing field symbol
// whose containing symbol is that property symbol.
it.name == "getContainingSymbol"
}
.forEach {
appendLine()
renderFunction(it, renderSymbolsFully = false, this@KtAnalysisSession, symbol)
if (it.name == "getContainingJvmClassName") {
if (symbol is KtCallableSymbol) {
appendLine()
renderFunction(it, renderSymbolsFully = false, this@KtAnalysisSession, symbol)
}
} else {
appendLine()
renderFunction(it, renderSymbolsFully = false, this@KtAnalysisSession, symbol)
}
}
KtSymbolInfoProviderMixIn::class.declaredMemberExtensionProperties
@@ -188,6 +204,7 @@ public class DebugSymbolRenderer(
is KtClassLikeSymbol -> renderId(symbol.classIdIfNonLocal, symbol)
is KtCallableSymbol -> renderId(symbol.callableIdIfNonLocal, symbol)
is KtNamedSymbol -> renderValue(symbol.name, renderSymbolsFully = false)
is KtFileSymbol -> renderValue((symbol.psi as KtFile).name, renderSymbolsFully = false)
else -> error("Unsupported symbol ${symbol::class}")
}
append(")")
@@ -0,0 +1,10 @@
// DO_NOT_CHECK_NON_PSI_SYMBOL_RESTORE_K1
@file:kotlin.jvm.JvmName("DifferentFacadeName")
fun foo() {}
fun String.foo(): Int = 42
val x: Int = 42
val Int.y get() = this
@@ -0,0 +1,8 @@
fun foo()
fun kotlin.String.foo(): kotlin.Int
val x: kotlin.Int
val kotlin.Int.y: kotlin.Int
get()
@@ -0,0 +1,244 @@
KtFunctionSymbol:
annotationsList: []
callableIdIfNonLocal: /foo
contextReceivers: []
contractEffects: []
hasStableParameterNames: true
isActual: false
isBuiltinFunctionInvoke: false
isExpect: false
isExtension: false
isExternal: false
isInfix: false
isInline: false
isOperator: false
isOverride: false
isStatic: false
isSuspend: false
modality: FINAL
name: foo
origin: SOURCE
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Unit
symbolKind: TOP_LEVEL
typeParameters: []
valueParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
KtFunctionSymbol:
annotationsList: []
callableIdIfNonLocal: /foo
contextReceivers: []
contractEffects: []
hasStableParameterNames: true
isActual: false
isBuiltinFunctionInvoke: false
isExpect: false
isExtension: true
isExternal: false
isInfix: false
isInline: false
isOperator: false
isOverride: false
isStatic: false
isSuspend: false
modality: FINAL
name: foo
origin: SOURCE
receiverParameter: KtReceiverParameterSymbol:
annotationsList: []
origin: SOURCE
owningCallableSymbol: KtFunctionSymbol(/foo)
type: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/String
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
symbolKind: TOP_LEVEL
typeParameters: []
valueParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
KtKotlinPropertySymbol:
annotationsList: []
backingFieldSymbol: KtBackingFieldSymbol:
annotationsList: []
callableIdIfNonLocal: null
contextReceivers: []
isExtension: false
name: field
origin: PROPERTY_BACKING_FIELD
owningProperty: KtKotlinPropertySymbol(/x)
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
symbolKind: LOCAL
typeParameters: []
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
callableIdIfNonLocal: /x
contextReceivers: []
getter: KtPropertyGetterSymbol:
annotationsList: []
callableIdIfNonLocal: null
contextReceivers: []
hasBody: false
hasStableParameterNames: true
isDefault: true
isExtension: false
isInline: false
isOverride: false
modality: FINAL
origin: SOURCE
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
symbolKind: ACCESSOR
typeParameters: []
valueParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
hasBackingField: true
hasGetter: true
hasSetter: false
initializer: KtConstantInitializerValue(42)
isActual: false
isConst: false
isDelegatedProperty: false
isExpect: false
isExtension: false
isFromPrimaryConstructor: false
isLateInit: false
isOverride: false
isStatic: false
isVal: true
modality: FINAL
name: x
origin: SOURCE
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
setter: null
symbolKind: TOP_LEVEL
typeParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: getX
javaSetterName: null
setterDeprecationStatus: null
KtKotlinPropertySymbol:
annotationsList: []
backingFieldSymbol: KtBackingFieldSymbol:
annotationsList: []
callableIdIfNonLocal: null
contextReceivers: []
isExtension: false
name: field
origin: PROPERTY_BACKING_FIELD
owningProperty: KtKotlinPropertySymbol(/y)
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
symbolKind: LOCAL
typeParameters: []
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
callableIdIfNonLocal: /y
contextReceivers: []
getter: KtPropertyGetterSymbol:
annotationsList: []
callableIdIfNonLocal: null
contextReceivers: []
hasBody: true
hasStableParameterNames: true
isDefault: false
isExtension: false
isInline: false
isOverride: false
modality: FINAL
origin: SOURCE
receiverParameter: KtReceiverParameterSymbol:
annotationsList: []
origin: SOURCE
owningCallableSymbol: KtKotlinPropertySymbol(/y)
type: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
symbolKind: ACCESSOR
typeParameters: []
valueParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
hasBackingField: false
hasGetter: true
hasSetter: false
initializer: null
isActual: false
isConst: false
isDelegatedProperty: false
isExpect: false
isExtension: true
isFromPrimaryConstructor: false
isLateInit: false
isOverride: false
isStatic: false
isVal: true
modality: FINAL
name: y
origin: SOURCE
receiverParameter: KtReceiverParameterSymbol:
annotationsList: []
origin: SOURCE
owningCallableSymbol: KtKotlinPropertySymbol(/y)
type: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
setter: null
symbolKind: TOP_LEVEL
typeParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: getY
javaSetterName: null
setterDeprecationStatus: null
@@ -0,0 +1,11 @@
// DO_NOT_CHECK_NON_PSI_SYMBOL_RESTORE_K1
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("NotMultiFilePartKt")
fun foo() {}
fun String.foo(): Int = 42
val x: Int = 42
val Int.y get() = this
@@ -0,0 +1,8 @@
fun foo()
fun kotlin.String.foo(): kotlin.Int
val x: kotlin.Int
val kotlin.Int.y: kotlin.Int
get()
@@ -0,0 +1,244 @@
KtFunctionSymbol:
annotationsList: []
callableIdIfNonLocal: /foo
contextReceivers: []
contractEffects: []
hasStableParameterNames: true
isActual: false
isBuiltinFunctionInvoke: false
isExpect: false
isExtension: false
isExternal: false
isInfix: false
isInline: false
isOperator: false
isOverride: false
isStatic: false
isSuspend: false
modality: FINAL
name: foo
origin: SOURCE
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Unit
symbolKind: TOP_LEVEL
typeParameters: []
valueParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
KtFunctionSymbol:
annotationsList: []
callableIdIfNonLocal: /foo
contextReceivers: []
contractEffects: []
hasStableParameterNames: true
isActual: false
isBuiltinFunctionInvoke: false
isExpect: false
isExtension: true
isExternal: false
isInfix: false
isInline: false
isOperator: false
isOverride: false
isStatic: false
isSuspend: false
modality: FINAL
name: foo
origin: SOURCE
receiverParameter: KtReceiverParameterSymbol:
annotationsList: []
origin: SOURCE
owningCallableSymbol: KtFunctionSymbol(/foo)
type: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/String
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
symbolKind: TOP_LEVEL
typeParameters: []
valueParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
KtKotlinPropertySymbol:
annotationsList: []
backingFieldSymbol: KtBackingFieldSymbol:
annotationsList: []
callableIdIfNonLocal: null
contextReceivers: []
isExtension: false
name: field
origin: PROPERTY_BACKING_FIELD
owningProperty: KtKotlinPropertySymbol(/x)
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
symbolKind: LOCAL
typeParameters: []
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
callableIdIfNonLocal: /x
contextReceivers: []
getter: KtPropertyGetterSymbol:
annotationsList: []
callableIdIfNonLocal: null
contextReceivers: []
hasBody: false
hasStableParameterNames: true
isDefault: true
isExtension: false
isInline: false
isOverride: false
modality: FINAL
origin: SOURCE
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
symbolKind: ACCESSOR
typeParameters: []
valueParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
hasBackingField: true
hasGetter: true
hasSetter: false
initializer: KtConstantInitializerValue(42)
isActual: false
isConst: false
isDelegatedProperty: false
isExpect: false
isExtension: false
isFromPrimaryConstructor: false
isLateInit: false
isOverride: false
isStatic: false
isVal: true
modality: FINAL
name: x
origin: SOURCE
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
setter: null
symbolKind: TOP_LEVEL
typeParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: getX
javaSetterName: null
setterDeprecationStatus: null
KtKotlinPropertySymbol:
annotationsList: []
backingFieldSymbol: KtBackingFieldSymbol:
annotationsList: []
callableIdIfNonLocal: null
contextReceivers: []
isExtension: false
name: field
origin: PROPERTY_BACKING_FIELD
owningProperty: KtKotlinPropertySymbol(/y)
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
symbolKind: LOCAL
typeParameters: []
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
callableIdIfNonLocal: /y
contextReceivers: []
getter: KtPropertyGetterSymbol:
annotationsList: []
callableIdIfNonLocal: null
contextReceivers: []
hasBody: true
hasStableParameterNames: true
isDefault: false
isExtension: false
isInline: false
isOverride: false
modality: FINAL
origin: SOURCE
receiverParameter: KtReceiverParameterSymbol:
annotationsList: []
origin: SOURCE
owningCallableSymbol: KtKotlinPropertySymbol(/y)
type: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
symbolKind: ACCESSOR
typeParameters: []
valueParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
hasBackingField: false
hasGetter: true
hasSetter: false
initializer: null
isActual: false
isConst: false
isDelegatedProperty: false
isExpect: false
isExtension: true
isFromPrimaryConstructor: false
isLateInit: false
isOverride: false
isStatic: false
isVal: true
modality: FINAL
name: y
origin: SOURCE
receiverParameter: KtReceiverParameterSymbol:
annotationsList: []
origin: SOURCE
owningCallableSymbol: KtKotlinPropertySymbol(/y)
type: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
setter: null
symbolKind: TOP_LEVEL
typeParameters: []
visibility: Public
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
getterDeprecationStatus: null
javaGetterName: getY
javaSetterName: null
setterDeprecationStatus: null