Minor, simplify/optimize FunctionTypeConstructor.computeSupertypes
This commit is contained in:
+18
-33
@@ -105,44 +105,29 @@ class FunctionClassDescriptor(
|
|||||||
|
|
||||||
private inner class FunctionTypeConstructor : AbstractClassTypeConstructor(storageManager) {
|
private inner class FunctionTypeConstructor : AbstractClassTypeConstructor(storageManager) {
|
||||||
override fun computeSupertypes(): Collection<KotlinType> {
|
override fun computeSupertypes(): Collection<KotlinType> {
|
||||||
val result = ArrayList<KotlinType>(2)
|
// For K{Suspend}Function{n}, add corresponding numbered {Suspend}Function{n} class, e.g. {Suspend}Function2 for K{Suspend}Function2
|
||||||
|
val supertypes = when (functionKind) {
|
||||||
|
Kind.Function -> // Function$N <: Function
|
||||||
|
listOf(functionClassId)
|
||||||
|
Kind.KFunction -> // KFunction$N <: KFunction
|
||||||
|
listOf(kFunctionClassId, ClassId(BUILT_INS_PACKAGE_FQ_NAME, Kind.Function.numberedClassName(arity)))
|
||||||
|
Kind.SuspendFunction -> // SuspendFunction$N<...> <: Function
|
||||||
|
listOf(functionClassId)
|
||||||
|
Kind.KSuspendFunction -> // KSuspendFunction$N<...> <: KFunction
|
||||||
|
listOf(kFunctionClassId, ClassId(COROUTINES_PACKAGE_FQ_NAME_RELEASE, Kind.SuspendFunction.numberedClassName(arity)))
|
||||||
|
}
|
||||||
|
|
||||||
fun add(id: ClassId) {
|
val moduleDescriptor = containingDeclaration.containingDeclaration
|
||||||
val moduleDescriptor = containingDeclaration.containingDeclaration
|
return supertypes.map { id ->
|
||||||
val descriptor = moduleDescriptor.findClassAcrossModuleDependencies(id) ?: error("Built-in class $id not found")
|
val descriptor = moduleDescriptor.findClassAcrossModuleDependencies(id) ?: error("Built-in class $id not found")
|
||||||
|
|
||||||
val typeConstructor = descriptor.typeConstructor
|
|
||||||
|
|
||||||
// Substitute all type parameters of the super class with our last type parameters
|
// Substitute all type parameters of the super class with our last type parameters
|
||||||
val arguments = parameters.takeLast(typeConstructor.parameters.size).map {
|
val arguments = parameters.takeLast(descriptor.typeConstructor.parameters.size).map {
|
||||||
TypeProjectionImpl(it.defaultType)
|
TypeProjectionImpl(it.defaultType)
|
||||||
}
|
}
|
||||||
|
|
||||||
result.add(KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, descriptor, arguments))
|
KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, descriptor, arguments)
|
||||||
}
|
}.toList()
|
||||||
|
|
||||||
when (functionKind) {
|
|
||||||
Kind.SuspendFunction -> // SuspendFunction$N<...> <: Function
|
|
||||||
add(functionClassId)
|
|
||||||
Kind.KSuspendFunction -> // KSuspendFunction$N<...> <: KFunction
|
|
||||||
add(kFunctionClassId)
|
|
||||||
Kind.Function -> // Function$N <: Function
|
|
||||||
add(functionClassId)
|
|
||||||
Kind.KFunction -> // KFunction$N <: KFunction
|
|
||||||
add(kFunctionClassId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// For K{Suspend}Function{n}, add corresponding numbered {Suspend}Function{n} class, e.g. {Suspend}Function2 for K{Suspend}Function2
|
|
||||||
when (functionKind) {
|
|
||||||
Kind.KFunction ->
|
|
||||||
add(ClassId(BUILT_INS_PACKAGE_FQ_NAME, Kind.Function.numberedClassName(arity)))
|
|
||||||
Kind.KSuspendFunction ->
|
|
||||||
add(ClassId(COROUTINES_PACKAGE_FQ_NAME_RELEASE, Kind.SuspendFunction.numberedClassName(arity)))
|
|
||||||
else -> {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.toList()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getParameters() = this@FunctionClassDescriptor.parameters
|
override fun getParameters() = this@FunctionClassDescriptor.parameters
|
||||||
@@ -159,7 +144,7 @@ class FunctionClassDescriptor(
|
|||||||
override fun toString() = name.asString()
|
override fun toString() = name.asString()
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val functionClassId = ClassId(BUILT_INS_PACKAGE_FQ_NAME, Name.identifier("Function"))
|
private val functionClassId = ClassId(BUILT_INS_PACKAGE_FQ_NAME, Name.identifier("Function"))
|
||||||
val kFunctionClassId = ClassId(KOTLIN_REFLECT_FQ_NAME, Name.identifier("KFunction"))
|
private val kFunctionClassId = ClassId(KOTLIN_REFLECT_FQ_NAME, Name.identifier("KFunction"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user