FIR IDE: introduce containingDeclarationProvider for symbols

This commit is contained in:
Ilya Kirillov
2020-08-12 20:38:45 +03:00
parent b5a4e4c409
commit cadf99ca1e
22 changed files with 247 additions and 66 deletions
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.psi.*
abstract class KtAnalysisSession(override val token: ValidityToken) : ValidityTokenOwner {
abstract val symbolProvider: KtSymbolProvider
abstract val scopeProvider: KtScopeProvider
abstract val containingDeclarationProvider: KtSymbolContainingDeclarationProvider
abstract fun getSmartCastedToTypes(expression: KtExpression): Collection<KtType>?
@@ -46,8 +46,8 @@ object DebugSymbolRenderer {
is KtType -> value.asStringForDebugging()
is KtSymbol -> {
val symbolTag = when (value) {
is KtClassLikeSymbol -> renderValue(value.classId)
is KtFunctionSymbol -> renderValue(value.fqName)
is KtClassLikeSymbol -> renderValue(value.classIdIfNonLocal ?: "<local>/${value.name}")
is KtFunctionSymbol -> renderValue(value.fqNameIfNonLocal ?: "<local>/${value.name}")
is KtConstructorSymbol -> "<constructor>"
is KtNamedSymbol -> renderValue(value.name)
else -> TODO(value::class.toString())
@@ -5,21 +5,33 @@
package org.jetbrains.kotlin.idea.frontend.api.symbols
import org.jetbrains.kotlin.idea.frontend.api.symbols.markers.KtSymbolKind
import org.jetbrains.kotlin.idea.frontend.api.symbols.markers.KtSymbolWithKind
import org.jetbrains.kotlin.idea.frontend.api.symbols.pointers.KtSymbolPointer
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
abstract class KtClassLikeSymbol : KtSymbol, KtNamedSymbol, KtSymbolWithKind {
abstract val classId: ClassId
abstract val classIdIfNonLocal: ClassId?
abstract override fun createPointer(): KtSymbolPointer<KtClassLikeSymbol>
}
abstract class KtTypeAliasSymbol : KtClassLikeSymbol() {
abstract override val classIdIfNonLocal: ClassId
final override val symbolKind: KtSymbolKind get() = KtSymbolKind.TOP_LEVEL
final override val containingNonLocalClassIdIfMember: ClassId? get() = null
abstract override val containingPackageFqNameIfTopLevel: FqName
abstract override fun createPointer(): KtSymbolPointer<KtTypeAliasSymbol>
}
abstract class KtClassOrObjectSymbol : KtClassLikeSymbol(), KtSymbolWithTypeParameters, KtSymbolWithModality<KtSymbolModality> {
abstract class KtClassOrObjectSymbol : KtClassLikeSymbol(),
KtSymbolWithTypeParameters,
KtSymbolWithModality<KtSymbolModality> {
abstract val classKind: KtClassKind
abstract override fun createPointer(): KtSymbolPointer<KtClassOrObjectSymbol>
@@ -5,6 +5,8 @@
package org.jetbrains.kotlin.idea.frontend.api.symbols
import org.jetbrains.kotlin.idea.frontend.api.symbols.markers.KtSymbolKind
import org.jetbrains.kotlin.idea.frontend.api.symbols.markers.KtSymbolWithKind
import org.jetbrains.kotlin.idea.frontend.api.symbols.pointers.KtSymbolPointer
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
@@ -17,6 +19,8 @@ sealed class KtFunctionLikeSymbol : KtCallableSymbol(), KtTypedSymbol, KtSymbolW
abstract class KtAnonymousFunctionSymbol : KtFunctionLikeSymbol() {
final override val symbolKind: KtSymbolKind get() = KtSymbolKind.LOCAL
final override val containingNonLocalClassIdIfMember: ClassId? get() = null
final override val containingPackageFqNameIfTopLevel: FqName? get() = null
abstract override fun createPointer(): KtSymbolPointer<KtAnonymousFunctionSymbol>
}
@@ -26,10 +30,10 @@ abstract class KtFunctionSymbol : KtFunctionLikeSymbol(),
KtPossibleExtensionSymbol,
KtSymbolWithTypeParameters,
KtSymbolWithModality<KtCommonSymbolModality> {
abstract val fqNameIfNonLocal: FqName?
abstract val isSuspend: Boolean
abstract val isOperator: Boolean
abstract val fqName: FqName?
abstract override val valueParameters: List<KtFunctionParameterSymbol>
@@ -37,10 +41,12 @@ abstract class KtFunctionSymbol : KtFunctionLikeSymbol(),
}
abstract class KtConstructorSymbol : KtFunctionLikeSymbol() {
abstract override val valueParameters: List<KtConstructorParameterSymbol>
abstract val isPrimary: Boolean
abstract val owner: KtClassOrObjectSymbol
abstract val ownerClassId: ClassId
abstract override val valueParameters: List<KtConstructorParameterSymbol>
final override val symbolKind: KtSymbolKind get() = KtSymbolKind.MEMBER
final override val containingPackageFqNameIfTopLevel: FqName? get() = null
abstract override fun createPointer(): KtSymbolPointer<KtConstructorSymbol>
}
@@ -0,0 +1,18 @@
/*
* Copyright 2010-2020 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.idea.frontend.api.symbols
import org.jetbrains.kotlin.idea.frontend.api.symbols.markers.KtSymbolWithKind
abstract class KtSymbolContainingDeclarationProvider {
/**
* Returns containing declaration for symbol:
* for top-level declarations returns null
* for class members returns containing class
* for local declaration returns declaration it was declared it
*/
abstract fun getContainingDeclaration(symbol: KtSymbolWithKind): KtSymbolWithKind?
}
@@ -5,6 +5,8 @@
package org.jetbrains.kotlin.idea.frontend.api.symbols
import org.jetbrains.kotlin.idea.frontend.api.symbols.markers.KtSymbolKind
import org.jetbrains.kotlin.idea.frontend.api.symbols.markers.KtSymbolWithKind
import org.jetbrains.kotlin.idea.frontend.api.symbols.pointers.KtSymbolPointer
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
@@ -13,13 +15,16 @@ abstract class KtVariableLikeSymbol : KtCallableSymbol(), KtTypedSymbol, KtNamed
abstract override fun createPointer(): KtSymbolPointer<KtVariableLikeSymbol>
}
abstract class KtEnumEntrySymbol : KtVariableLikeSymbol() {
abstract class KtEnumEntrySymbol : KtVariableLikeSymbol(), KtSymbolWithKind {
final override val symbolKind: KtSymbolKind get() = KtSymbolKind.MEMBER
abstract val enumClassId: ClassId
final override val containingPackageFqNameIfTopLevel: FqName? get() = null
abstract override fun createPointer(): KtSymbolPointer<KtEnumEntrySymbol>
}
abstract class KtParameterSymbol : KtVariableLikeSymbol() {
final override val containingPackageFqNameIfTopLevel: FqName? get() = null
abstract val hasDefaultValue: Boolean
abstract override fun createPointer(): KtSymbolPointer<KtParameterSymbol>
@@ -30,28 +35,41 @@ abstract class KtVariableSymbol : KtVariableLikeSymbol() {
abstract override fun createPointer(): KtSymbolPointer<KtVariableSymbol>
}
abstract class KtJavaFieldSymbol : KtVariableSymbol(), KtSymbolWithModality<KtCommonSymbolModality> {
abstract class KtJavaFieldSymbol : KtVariableSymbol(), KtSymbolWithModality<KtCommonSymbolModality>, KtSymbolWithKind {
final override val symbolKind: KtSymbolKind get() = KtSymbolKind.MEMBER
final override val containingPackageFqNameIfTopLevel: FqName? get() = null
abstract override fun createPointer(): KtSymbolPointer<KtJavaFieldSymbol>
}
// TODO getters & setters
abstract class KtPropertySymbol : KtVariableSymbol(), KtPossibleExtensionSymbol, KtSymbolWithModality<KtCommonSymbolModality> {
abstract val fqName: FqName
abstract class KtPropertySymbol : KtVariableSymbol(),
KtPossibleExtensionSymbol,
KtSymbolWithModality<KtCommonSymbolModality>,
KtSymbolWithKind {
abstract override fun createPointer(): KtSymbolPointer<KtPropertySymbol>
}
abstract class KtLocalVariableSymbol : KtVariableSymbol() {
abstract class KtLocalVariableSymbol : KtVariableSymbol(), KtSymbolWithKind {
final override val containingPackageFqNameIfTopLevel: FqName? get() = null
final override val containingNonLocalClassIdIfMember: ClassId? get() = null
abstract override fun createPointer(): KtSymbolPointer<KtLocalVariableSymbol>
}
abstract class KtFunctionParameterSymbol : KtParameterSymbol() {
abstract class KtFunctionParameterSymbol : KtParameterSymbol(), KtSymbolWithKind {
abstract val isVararg: Boolean
override val symbolKind: KtSymbolKind get() = KtSymbolKind.NON_PROPERTY_PARAMETER
override val containingNonLocalClassIdIfMember: ClassId? get() = null
abstract override fun createPointer(): KtSymbolPointer<KtFunctionParameterSymbol>
}
abstract class KtConstructorParameterSymbol : KtParameterSymbol(), KtNamedSymbol {
abstract class KtConstructorParameterSymbol : KtParameterSymbol(), KtNamedSymbol, KtSymbolWithKind {
abstract val constructorParameterKind: KtConstructorParameterSymbolKind
abstract override fun createPointer(): KtSymbolPointer<KtConstructorParameterSymbol>
}
@@ -12,14 +12,6 @@ interface KtNamedSymbol : KtSymbol {
val name: Name
}
interface KtSymbolWithKind : KtSymbol {
val symbolKind: KtSymbolKind
}
enum class KtSymbolKind {
TOP_LEVEL, MEMBER, LOCAL
}
interface KtTypedSymbol : KtSymbol {
val type: KtType
}
@@ -0,0 +1,33 @@
/*
* Copyright 2010-2020 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.idea.frontend.api.symbols.markers
import org.jetbrains.kotlin.idea.frontend.api.symbols.KtSymbol
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
/**
* [KtSymbol] which can possibly be class member, top level or local declaration
* Contracts:
* [containingNonLocalClassIdIfMember] != null iff [symbolKind] == [KtSymbolKind.MEMBER]
* [containingPackageFqNameIfTopLevel] != null iff [symbolKind] == [KtSymbolKind.TOP_LEVEL]
*/
interface KtSymbolWithKind : KtSymbol {
val symbolKind: KtSymbolKind
val containingNonLocalClassIdIfMember: ClassId?
val containingPackageFqNameIfTopLevel: FqName?
}
enum class KtSymbolKind {
TOP_LEVEL, MEMBER, LOCAL, NON_PROPERTY_PARAMETER
}
val KtSymbol.containingClassIdIfMember: ClassId?
get() = (this as? KtSymbolWithKind)?.containingClassIdIfMember
val KtSymbol.containingPackageFqNameIfTopLevel: FqName?
get() = (this as? KtSymbolWithKind)?.containingPackageFqNameIfTopLevel