From 34877130c7e77c4367c5e6f9c56b94a5c420804e Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 12 Jan 2024 13:39:56 +0100 Subject: [PATCH] Refactor callableId around pathToLocal and classId properties #KT-58739 Fixed --- .../resolve/providers/impl/FirProviderImpl.kt | 8 +-- .../fir/symbols/impl/FirVariableSymbol.kt | 2 +- .../org/jetbrains/kotlin/name/CallableId.kt | 69 +++++++++++++------ 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirProviderImpl.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirProviderImpl.kt index 0ba3ec3b5c4..781443775dd 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirProviderImpl.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirProviderImpl.kt @@ -63,18 +63,18 @@ class FirProviderImpl(val session: FirSession, val kotlinScopeProvider: FirKotli @FirSymbolProviderInternals override fun getTopLevelCallableSymbolsTo(destination: MutableList>, 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, 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, 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? { diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/impl/FirVariableSymbol.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/impl/FirVariableSymbol.kt index 5fe73ea2ddd..ba1064a484e 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/impl/FirVariableSymbol.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/impl/FirVariableSymbol.kt @@ -130,7 +130,7 @@ class FirValueParameterSymbol(name: Name) : FirVariableSymbol class FirErrorPropertySymbol( val diagnostic: ConeDiagnostic -) : FirVariableSymbol(CallableId(FqName.ROOT, null, NAME)) { +) : FirVariableSymbol(CallableId(FqName.ROOT, NAME)) { companion object { val NAME: Name = Name.special("") } diff --git a/core/compiler.common/src/org/jetbrains/kotlin/name/CallableId.kt b/core/compiler.common/src/org/jetbrains/kotlin/name/CallableId.kt index 7fbc7bd0e0f..3140baae24e 100644 --- a/core/compiler.common/src/org/jetbrains/kotlin/name/CallableId.kt +++ b/core/compiler.common/src/org/jetbrains/kotlin/name/CallableId.kt @@ -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('.', '/'))