Refactor callableId around pathToLocal and classId properties

#KT-58739 Fixed
This commit is contained in:
Mikhail Glukhikh
2024-01-12 13:39:56 +01:00
committed by Space Team
parent 71f2a2df8e
commit 34877130c7
3 changed files with 52 additions and 27 deletions
@@ -63,18 +63,18 @@ class FirProviderImpl(val session: FirSession, val kotlinScopeProvider: FirKotli
@FirSymbolProviderInternals
override fun getTopLevelCallableSymbolsTo(destination: MutableList<FirCallableSymbol<*>>, packageFqName: FqName, name: Name) {
destination += (state.functionMap[CallableId(packageFqName, null, name)] ?: emptyList())
destination += (state.propertyMap[CallableId(packageFqName, null, name)] ?: emptyList())
destination += (state.functionMap[CallableId(packageFqName, name)] ?: emptyList())
destination += (state.propertyMap[CallableId(packageFqName, name)] ?: emptyList())
}
@FirSymbolProviderInternals
override fun getTopLevelFunctionSymbolsTo(destination: MutableList<FirNamedFunctionSymbol>, packageFqName: FqName, name: Name) {
destination += (state.functionMap[CallableId(packageFqName, null, name)] ?: emptyList())
destination += (state.functionMap[CallableId(packageFqName, name)] ?: emptyList())
}
@FirSymbolProviderInternals
override fun getTopLevelPropertySymbolsTo(destination: MutableList<FirPropertySymbol>, packageFqName: FqName, name: Name) {
destination += (state.propertyMap[CallableId(packageFqName, null, name)] ?: emptyList())
destination += (state.propertyMap[CallableId(packageFqName, name)] ?: emptyList())
}
override fun getPackage(fqName: FqName): FqName? {
@@ -130,7 +130,7 @@ class FirValueParameterSymbol(name: Name) : FirVariableSymbol<FirValueParameter>
class FirErrorPropertySymbol(
val diagnostic: ConeDiagnostic
) : FirVariableSymbol<FirErrorProperty>(CallableId(FqName.ROOT, null, NAME)) {
) : FirVariableSymbol<FirErrorProperty>(CallableId(FqName.ROOT, NAME)) {
companion object {
val NAME: Name = Name.special("<error property>")
}
@@ -6,18 +6,24 @@
package org.jetbrains.kotlin.name
// NB: with className == null we are at top level
data class CallableId(
class CallableId private constructor(
val packageName: FqName,
val className: FqName?,
val callableName: Name,
// Currently, it's only used for debug info
private val pathToLocal: FqName? = null
val classId: ClassId?,
) {
companion object {
private val LOCAL_NAME = SpecialNames.LOCAL
val PACKAGE_FQ_NAME_FOR_LOCAL = FqName.topLevel(LOCAL_NAME)
private fun calculateClassId(packageName: FqName, className: FqName?): ClassId? =
className?.let { ClassId(packageName, it, isLocal = packageName == PACKAGE_FQ_NAME_FOR_LOCAL) }
}
// Effectively val (changed in constructors only)
// Used only for debug info
private var pathToLocal: FqName? = null
/**
* Return `true` if corresponding declaration is itself local or it is a member of local class
* Otherwise, returns `false`
@@ -25,34 +31,34 @@ data class CallableId(
val isLocal: Boolean
get() = packageName == PACKAGE_FQ_NAME_FOR_LOCAL || classId?.isLocal == true
var classId: ClassId? = null
get() {
if (field == null && className != null) {
field = ClassId(packageName, className, isLocal = packageName == PACKAGE_FQ_NAME_FOR_LOCAL)
}
return field
}
private set
constructor(
packageName: FqName, className: FqName?, callableName: Name,
) : this(packageName, className, callableName, calculateClassId(packageName, className))
constructor(classId: ClassId, callableName: Name) : this(classId.packageFqName, classId.relativeClassName, callableName) {
this.classId = classId
constructor(
packageName: FqName, className: FqName?, callableName: Name,
// Currently, it's only used for debug info
pathToLocal: FqName?
) : this(packageName, className, callableName, calculateClassId(packageName, className)) {
this.pathToLocal = pathToLocal
}
constructor(packageName: FqName, callableName: Name) : this(packageName, null, callableName)
constructor(classId: ClassId, callableName: Name) : this(classId.packageFqName, classId.relativeClassName, callableName, classId)
constructor(packageName: FqName, callableName: Name) : this(packageName, className = null, callableName, classId = null)
constructor(
callableName: Name,
// Currently, it's only used for debug info
pathToLocal: FqName? = null
) : this(
PACKAGE_FQ_NAME_FOR_LOCAL,
className = null,
callableName,
pathToLocal,
)
pathToLocal: FqName?
) : this(PACKAGE_FQ_NAME_FOR_LOCAL, className = null, callableName, classId = null) {
this.pathToLocal = pathToLocal
}
constructor(callableName: Name) : this(PACKAGE_FQ_NAME_FOR_LOCAL, className = null, callableName, classId = null)
fun asFqNameForDebugInfo(): FqName {
if (pathToLocal != null) return pathToLocal.child(callableName)
pathToLocal?.child(callableName)?.let { return it }
return asSingleFqName()
}
@@ -60,6 +66,25 @@ data class CallableId(
return classId?.asSingleFqName()?.child(callableName) ?: packageName.child(callableName)
}
fun copy(callableName: Name): CallableId = CallableId(packageName, className, callableName, classId)
override fun equals(other: Any?): Boolean {
return when {
this === other -> true
other !is CallableId -> false
// classId isn't needed
else -> packageName == other.packageName && className == other.className && callableName == other.callableName
}
}
override fun hashCode(): Int {
var result = 17
result = result * 31 + packageName.hashCode()
result = result * 31 + className.hashCode()
result = result * 31 + callableName.hashCode()
return result
}
override fun toString(): String {
return buildString {
append(packageName.asString().replace('.', '/'))