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 @FirSymbolProviderInternals
override fun getTopLevelCallableSymbolsTo(destination: MutableList<FirCallableSymbol<*>>, packageFqName: FqName, name: Name) { override fun getTopLevelCallableSymbolsTo(destination: MutableList<FirCallableSymbol<*>>, packageFqName: FqName, name: Name) {
destination += (state.functionMap[CallableId(packageFqName, null, name)] ?: emptyList()) destination += (state.functionMap[CallableId(packageFqName, name)] ?: emptyList())
destination += (state.propertyMap[CallableId(packageFqName, null, name)] ?: emptyList()) destination += (state.propertyMap[CallableId(packageFqName, name)] ?: emptyList())
} }
@FirSymbolProviderInternals @FirSymbolProviderInternals
override fun getTopLevelFunctionSymbolsTo(destination: MutableList<FirNamedFunctionSymbol>, packageFqName: FqName, name: Name) { 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 @FirSymbolProviderInternals
override fun getTopLevelPropertySymbolsTo(destination: MutableList<FirPropertySymbol>, packageFqName: FqName, name: Name) { 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? { override fun getPackage(fqName: FqName): FqName? {
@@ -130,7 +130,7 @@ class FirValueParameterSymbol(name: Name) : FirVariableSymbol<FirValueParameter>
class FirErrorPropertySymbol( class FirErrorPropertySymbol(
val diagnostic: ConeDiagnostic val diagnostic: ConeDiagnostic
) : FirVariableSymbol<FirErrorProperty>(CallableId(FqName.ROOT, null, NAME)) { ) : FirVariableSymbol<FirErrorProperty>(CallableId(FqName.ROOT, NAME)) {
companion object { companion object {
val NAME: Name = Name.special("<error property>") val NAME: Name = Name.special("<error property>")
} }
@@ -6,18 +6,24 @@
package org.jetbrains.kotlin.name package org.jetbrains.kotlin.name
// NB: with className == null we are at top level // NB: with className == null we are at top level
data class CallableId( class CallableId private constructor(
val packageName: FqName, val packageName: FqName,
val className: FqName?, val className: FqName?,
val callableName: Name, val callableName: Name,
// Currently, it's only used for debug info val classId: ClassId?,
private val pathToLocal: FqName? = null
) { ) {
companion object { companion object {
private val LOCAL_NAME = SpecialNames.LOCAL private val LOCAL_NAME = SpecialNames.LOCAL
val PACKAGE_FQ_NAME_FOR_LOCAL = FqName.topLevel(LOCAL_NAME) 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 * Return `true` if corresponding declaration is itself local or it is a member of local class
* Otherwise, returns `false` * Otherwise, returns `false`
@@ -25,34 +31,34 @@ data class CallableId(
val isLocal: Boolean val isLocal: Boolean
get() = packageName == PACKAGE_FQ_NAME_FOR_LOCAL || classId?.isLocal == true get() = packageName == PACKAGE_FQ_NAME_FOR_LOCAL || classId?.isLocal == true
var classId: ClassId? = null constructor(
get() { packageName: FqName, className: FqName?, callableName: Name,
if (field == null && className != null) { ) : this(packageName, className, callableName, calculateClassId(packageName, className))
field = ClassId(packageName, className, isLocal = packageName == PACKAGE_FQ_NAME_FOR_LOCAL)
}
return field
}
private set
constructor(classId: ClassId, callableName: Name) : this(classId.packageFqName, classId.relativeClassName, callableName) { constructor(
this.classId = classId 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( constructor(
callableName: Name, callableName: Name,
// Currently, it's only used for debug info // Currently, it's only used for debug info
pathToLocal: FqName? = null pathToLocal: FqName?
) : this( ) : this(PACKAGE_FQ_NAME_FOR_LOCAL, className = null, callableName, classId = null) {
PACKAGE_FQ_NAME_FOR_LOCAL, this.pathToLocal = pathToLocal
className = null, }
callableName,
pathToLocal, constructor(callableName: Name) : this(PACKAGE_FQ_NAME_FOR_LOCAL, className = null, callableName, classId = null)
)
fun asFqNameForDebugInfo(): FqName { fun asFqNameForDebugInfo(): FqName {
if (pathToLocal != null) return pathToLocal.child(callableName) pathToLocal?.child(callableName)?.let { return it }
return asSingleFqName() return asSingleFqName()
} }
@@ -60,6 +66,25 @@ data class CallableId(
return classId?.asSingleFqName()?.child(callableName) ?: packageName.child(callableName) 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 { override fun toString(): String {
return buildString { return buildString {
append(packageName.asString().replace('.', '/')) append(packageName.asString().replace('.', '/'))