K2: Simplify JvmMappedScope-related code a bit
JvmMappedScope.Signatures became mostly irrelevant and only "useful" for checking if we really need to have the scope mapped Now, the check is rewritten according to K1 semantics ^KT-57694 Fixed
This commit is contained in:
committed by
Space Team
parent
6b1d9c4d56
commit
6683e229b4
@@ -16,6 +16,8 @@ import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
|
|||||||
import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope
|
import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope
|
||||||
import org.jetbrains.kotlin.fir.scopes.jvm.JvmMappedScope
|
import org.jetbrains.kotlin.fir.scopes.jvm.JvmMappedScope
|
||||||
import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope
|
import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope
|
||||||
|
import org.jetbrains.kotlin.fir.types.isSomeFunctionType
|
||||||
|
import org.jetbrains.kotlin.name.StandardClassIds
|
||||||
|
|
||||||
fun wrapScopeWithJvmMapped(
|
fun wrapScopeWithJvmMapped(
|
||||||
klass: FirClass,
|
klass: FirClass,
|
||||||
@@ -32,23 +34,21 @@ fun wrapScopeWithJvmMapped(
|
|||||||
val symbolProvider = useSiteSession.symbolProvider
|
val symbolProvider = useSiteSession.symbolProvider
|
||||||
val javaClass = symbolProvider.getClassLikeSymbolByClassId(javaClassId)?.fir as? FirRegularClass
|
val javaClass = symbolProvider.getClassLikeSymbolByClassId(javaClassId)?.fir as? FirRegularClass
|
||||||
?: return declaredMemberScope
|
?: return declaredMemberScope
|
||||||
val preparedSignatures = JvmMappedScope.prepareSignatures(javaClass, JavaToKotlinClassMap.isMutable(kotlinUnsafeFqName))
|
|
||||||
return if (preparedSignatures.isNotEmpty()) {
|
// We don't add additional built-in members to function types and kotlin.Any (see JvmBuiltInsCustomizer.getJavaAnalogue)
|
||||||
val javaClassUseSiteScope = javaClass.unsubstitutedScope(
|
if (klass.symbol.toLookupTag().isSomeFunctionType(useSiteSession) || classId == StandardClassIds.Any) return declaredMemberScope
|
||||||
useSiteSession,
|
|
||||||
scopeSession,
|
val javaClassUseSiteScope = javaClass.unsubstitutedScope(
|
||||||
withForcedTypeCalculator = false,
|
useSiteSession,
|
||||||
memberRequiredPhase = memberRequiredPhase,
|
scopeSession,
|
||||||
)
|
withForcedTypeCalculator = false,
|
||||||
JvmMappedScope(
|
memberRequiredPhase = memberRequiredPhase,
|
||||||
useSiteSession,
|
)
|
||||||
klass,
|
return JvmMappedScope(
|
||||||
javaClass,
|
useSiteSession,
|
||||||
declaredMemberScope,
|
klass,
|
||||||
javaClassUseSiteScope,
|
javaClass,
|
||||||
preparedSignatures,
|
declaredMemberScope,
|
||||||
)
|
javaClassUseSiteScope,
|
||||||
} else {
|
)
|
||||||
declaredMemberScope
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,6 @@ class JvmMappedScope(
|
|||||||
private val firJavaClass: FirRegularClass,
|
private val firJavaClass: FirRegularClass,
|
||||||
private val declaredMemberScope: FirContainingNamesAwareScope,
|
private val declaredMemberScope: FirContainingNamesAwareScope,
|
||||||
private val javaMappedClassUseSiteScope: FirTypeScope,
|
private val javaMappedClassUseSiteScope: FirTypeScope,
|
||||||
private val signatures: Signatures,
|
|
||||||
) : FirTypeScope() {
|
) : FirTypeScope() {
|
||||||
private val functionsCache = mutableMapOf<FirNamedFunctionSymbol, FirNamedFunctionSymbol>()
|
private val functionsCache = mutableMapOf<FirNamedFunctionSymbol, FirNamedFunctionSymbol>()
|
||||||
|
|
||||||
@@ -228,7 +227,7 @@ class JvmMappedScope(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun processDeclaredConstructors(processor: (FirConstructorSymbol) -> Unit) {
|
override fun processDeclaredConstructors(processor: (FirConstructorSymbol) -> Unit) {
|
||||||
javaMappedClassUseSiteScope.processDeclaredConstructors { javaCtorSymbol ->
|
javaMappedClassUseSiteScope.processDeclaredConstructors processor@{ javaCtorSymbol ->
|
||||||
|
|
||||||
fun FirConstructor.isShadowedBy(ctorFromKotlin: FirConstructorSymbol): Boolean {
|
fun FirConstructor.isShadowedBy(ctorFromKotlin: FirConstructorSymbol): Boolean {
|
||||||
// assuming already checked for visibility
|
// assuming already checked for visibility
|
||||||
@@ -250,14 +249,15 @@ class JvmMappedScope(
|
|||||||
// Here the logic is generally the same, but simplified for performance by reordering checks and avoiding checking
|
// Here the logic is generally the same, but simplified for performance by reordering checks and avoiding checking
|
||||||
// for the impossible combinations
|
// for the impossible combinations
|
||||||
val javaCtor = javaCtorSymbol.fir
|
val javaCtor = javaCtorSymbol.fir
|
||||||
if (javaCtor.status.visibility.isPublicAPI && !javaCtor.isDeprecated() &&
|
|
||||||
javaCtor.computeJvmDescriptor() !in signatures.hiddenConstructors &&
|
if (!javaCtor.status.visibility.isPublicAPI || javaCtor.isDeprecated()) return@processor
|
||||||
!javaCtor.isTrivialCopyConstructor() &&
|
val signature = SignatureBuildingComponents.signature(firJavaClass.classId, javaCtor.computeJvmDescriptor())
|
||||||
firKotlinClassConstructors.none { javaCtor.isShadowedBy(it) }
|
if (signature in JvmBuiltInsSignatures.HIDDEN_CONSTRUCTOR_SIGNATURES) return@processor
|
||||||
) {
|
if (javaCtor.isTrivialCopyConstructor()) return@processor
|
||||||
val newSymbol = getOrCreateCopy(javaCtorSymbol)
|
if (firKotlinClassConstructors.any { javaCtor.isShadowedBy(it) }) return@processor
|
||||||
processor(newSymbol)
|
|
||||||
}
|
val newSymbol = getOrCreateCopy(javaCtorSymbol)
|
||||||
|
processor(newSymbol)
|
||||||
}
|
}
|
||||||
|
|
||||||
declaredMemberScope.processDeclaredConstructors(processor)
|
declaredMemberScope.processDeclaredConstructors(processor)
|
||||||
@@ -307,33 +307,6 @@ class JvmMappedScope(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
data class Signatures(val visibleMethodSignaturesByName: Map<Name, Set<String>>, val hiddenConstructors: Set<String>) {
|
|
||||||
fun isEmpty() = visibleMethodSignaturesByName.isEmpty() && hiddenConstructors.isEmpty()
|
|
||||||
fun isNotEmpty() = !isEmpty()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun prepareSignatures(klass: FirRegularClass, isMutable: Boolean): Signatures {
|
|
||||||
|
|
||||||
val signaturePrefix = klass.symbol.classId.toString()
|
|
||||||
val visibleMethodsByName = mutableMapOf<Name, MutableSet<String>>()
|
|
||||||
(JvmBuiltInsSignatures.VISIBLE_METHOD_SIGNATURES).filter { signature ->
|
|
||||||
signature in JvmBuiltInsSignatures.MUTABLE_METHOD_SIGNATURES == isMutable &&
|
|
||||||
signature.startsWith(signaturePrefix)
|
|
||||||
}.map { signature ->
|
|
||||||
// +1 to delete dot before function name
|
|
||||||
signature.substring(signaturePrefix.length + 1)
|
|
||||||
}.forEach {
|
|
||||||
visibleMethodsByName.getOrPut(Name.identifier(it.substringBefore("("))) { mutableSetOf() }.add(it)
|
|
||||||
}
|
|
||||||
|
|
||||||
val hiddenConstructors =
|
|
||||||
JvmBuiltInsSignatures.HIDDEN_CONSTRUCTOR_SIGNATURES
|
|
||||||
.filter { it.startsWith(signaturePrefix) }
|
|
||||||
.mapTo(mutableSetOf()) { it.substring(signaturePrefix.length + 1) }
|
|
||||||
|
|
||||||
return Signatures(visibleMethodsByName, hiddenConstructors)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For fromClass=A<T1, T2>, toClass=B<F1, F1> classes
|
* For fromClass=A<T1, T2>, toClass=B<F1, F1> classes
|
||||||
* @returns {T1 -> F1, T2 -> F2} substitution
|
* @returns {T1 -> F1, T2 -> F2} substitution
|
||||||
|
|||||||
Reference in New Issue
Block a user