Refactoring: move getRepresentativeUpperBound method to core

Plus prettify it a bit
This commit is contained in:
Mikhail Zarechenskiy
2019-03-05 12:34:11 +03:00
parent 7c357c0ec0
commit 82c8289666
4 changed files with 19 additions and 21 deletions
@@ -6,11 +6,11 @@
package org.jetbrains.kotlin.codegen.state package org.jetbrains.kotlin.codegen.state
import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.load.kotlin.getRepresentativeUpperBound
import org.jetbrains.kotlin.resolve.InlineClassDescriptorResolver import org.jetbrains.kotlin.resolve.InlineClassDescriptorResolver
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.resolve.jvm.* import org.jetbrains.kotlin.resolve.jvm.*
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.typeUtil.representativeUpperBound
import java.security.MessageDigest import java.security.MessageDigest
import java.util.* import java.util.*
@@ -44,7 +44,7 @@ private fun getSignatureElementForMangling(type: KotlinType): String = buildStri
} }
is TypeParameterDescriptor -> { is TypeParameterDescriptor -> {
append(getSignatureElementForMangling(getRepresentativeUpperBound(descriptor))) append(getSignatureElementForMangling(descriptor.representativeUpperBound))
} }
} }
} }
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.resolve.unsubstitutedUnderlyingType
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.typeUtil.makeNullable import org.jetbrains.kotlin.types.typeUtil.makeNullable
import org.jetbrains.kotlin.types.typeUtil.replaceArgumentsWithStarProjections import org.jetbrains.kotlin.types.typeUtil.replaceArgumentsWithStarProjections
import org.jetbrains.kotlin.types.typeUtil.representativeUpperBound
import org.jetbrains.kotlin.utils.DO_NOTHING_3 import org.jetbrains.kotlin.utils.DO_NOTHING_3
interface JvmTypeFactory<T : Any> { interface JvmTypeFactory<T : Any> {
@@ -181,7 +182,7 @@ fun <T : Any> mapType(
descriptor is TypeParameterDescriptor -> { descriptor is TypeParameterDescriptor -> {
val type = mapType( val type = mapType(
getRepresentativeUpperBound(descriptor), descriptor.representativeUpperBound,
factory, factory,
mode, mode,
typeMappingConfiguration, typeMappingConfiguration,
@@ -255,7 +256,7 @@ internal fun computeUnderlyingType(inlineClassType: KotlinType): KotlinType? {
val descriptor = inlineClassType.unsubstitutedUnderlyingType()?.constructor?.declarationDescriptor ?: return null val descriptor = inlineClassType.unsubstitutedUnderlyingType()?.constructor?.declarationDescriptor ?: return null
return if (descriptor is TypeParameterDescriptor) return if (descriptor is TypeParameterDescriptor)
getRepresentativeUpperBound(descriptor) descriptor.representativeUpperBound
else else
inlineClassType.substitutedUnderlyingType() inlineClassType.substitutedUnderlyingType()
} }
@@ -270,7 +271,7 @@ internal fun computeExpandedTypeInner(kotlinType: KotlinType, visitedClassifiers
return when { return when {
classifier is TypeParameterDescriptor -> classifier is TypeParameterDescriptor ->
computeExpandedTypeInner(getRepresentativeUpperBound(classifier), visitedClassifiers) computeExpandedTypeInner(classifier.representativeUpperBound, visitedClassifiers)
?.let { expandedUpperBound -> ?.let { expandedUpperBound ->
if (expandedUpperBound.isNullable() || !kotlinType.isMarkedNullable) if (expandedUpperBound.isNullable() || !kotlinType.isMarkedNullable)
expandedUpperBound expandedUpperBound
@@ -338,16 +339,6 @@ fun computeInternalName(
private fun getContainer(container: DeclarationDescriptor?): DeclarationDescriptor? = private fun getContainer(container: DeclarationDescriptor?): DeclarationDescriptor? =
container as? ClassDescriptor ?: container as? PackageFragmentDescriptor ?: container?.let { getContainer(it.containingDeclaration) } container as? ClassDescriptor ?: container as? PackageFragmentDescriptor ?: container?.let { getContainer(it.containingDeclaration) }
fun getRepresentativeUpperBound(descriptor: TypeParameterDescriptor): KotlinType {
val upperBounds = descriptor.upperBounds
assert(!upperBounds.isEmpty()) { "Upper bounds should not be empty: $descriptor" }
return upperBounds.firstOrNull {
val classDescriptor = it.constructor.declarationDescriptor as? ClassDescriptor ?: return@firstOrNull false
classDescriptor.kind != ClassKind.INTERFACE && classDescriptor.kind != ClassKind.ANNOTATION_CLASS
} ?: upperBounds.first()
}
open class JvmDescriptorTypeWriter<T : Any>(private val jvmTypeFactory: JvmTypeFactory<T>) { open class JvmDescriptorTypeWriter<T : Any>(private val jvmTypeFactory: JvmTypeFactory<T>) {
private var jvmCurrentTypeArrayLevel: Int = 0 private var jvmCurrentTypeArrayLevel: Int = 0
protected var jvmCurrentType: T? = null protected var jvmCurrentType: T? = null
@@ -6,15 +6,11 @@
package org.jetbrains.kotlin.resolve.jvm package org.jetbrains.kotlin.resolve.jvm
import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.load.kotlin.getRepresentativeUpperBound
import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.resolve.isInlineClass import org.jetbrains.kotlin.resolve.isInlineClass
import org.jetbrains.kotlin.resolve.isInlineClassType
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import java.security.MessageDigest import org.jetbrains.kotlin.types.typeUtil.representativeUpperBound
import java.util.*
fun shouldHideConstructorDueToInlineClassTypeValueParameters(descriptor: CallableMemberDescriptor): Boolean { fun shouldHideConstructorDueToInlineClassTypeValueParameters(descriptor: CallableMemberDescriptor): Boolean {
val constructorDescriptor = descriptor as? ClassConstructorDescriptor ?: return false val constructorDescriptor = descriptor as? ClassConstructorDescriptor ?: return false
@@ -45,5 +41,5 @@ private fun isDontMangleClass(classDescriptor: ClassDescriptor) =
private fun KotlinType.isTypeParameterWithUpperBoundThatRequiresMangling(): Boolean { private fun KotlinType.isTypeParameterWithUpperBoundThatRequiresMangling(): Boolean {
val descriptor = constructor.declarationDescriptor as? TypeParameterDescriptor ?: return false val descriptor = constructor.declarationDescriptor as? TypeParameterDescriptor ?: return false
return getRepresentativeUpperBound(descriptor).requiresFunctionNameMangling() return descriptor.representativeUpperBound.requiresFunctionNameMangling()
} }
@@ -228,3 +228,14 @@ fun UnwrappedType.canHaveUndefinedNullability(): Boolean =
constructor is NewTypeVariableConstructor || constructor is NewTypeVariableConstructor ||
constructor.declarationDescriptor is TypeParameterDescriptor || constructor.declarationDescriptor is TypeParameterDescriptor ||
this is NewCapturedType this is NewCapturedType
val TypeParameterDescriptor.representativeUpperBound: KotlinType
get() {
assert(upperBounds.isNotEmpty()) { "Upper bounds should not be empty: $this" }
return upperBounds.firstOrNull {
val classDescriptor = it.constructor.declarationDescriptor as? ClassDescriptor ?: return@firstOrNull false
classDescriptor.kind != ClassKind.INTERFACE && classDescriptor.kind != ClassKind.ANNOTATION_CLASS
} ?: upperBounds.first()
}