Partially reimplement KotlinType based util using IrType
This commit is contained in:
-15
@@ -21,24 +21,12 @@ import org.jetbrains.kotlin.types.typeUtil.isTypeParameter
|
||||
|
||||
// TODO: implement pure Ir-based function (see IrTypeUtils.kt)
|
||||
|
||||
@Deprecated("Use pure Ir helper")
|
||||
fun IrType.isNullable() = toKotlinType().isNullable()
|
||||
|
||||
@Deprecated("Use pure Ir helper")
|
||||
fun IrType.isInterface() = toKotlinType().isInterface()
|
||||
|
||||
@Deprecated("Use pure Ir helper")
|
||||
fun IrType.isPrimitiveArray() = KotlinBuiltIns.isPrimitiveArray(toKotlinType())
|
||||
|
||||
@Deprecated("Use pure Ir helper")
|
||||
fun IrType.getPrimitiveArrayElementType() = KotlinBuiltIns.getPrimitiveArrayElementType(toKotlinType())
|
||||
|
||||
@Deprecated("Use pure Ir helper")
|
||||
fun IrType.isTypeParameter() = toKotlinType().isTypeParameter()
|
||||
|
||||
@Deprecated("Use pure Ir helper")
|
||||
fun IrType.isFunctionOrKFunction() = toKotlinType().isFunctionOrKFunctionType
|
||||
|
||||
@Deprecated("Use pure Ir helper")
|
||||
fun List<IrType>.commonSupertype() = CommonSupertypes.commonSupertype(map(IrType::toKotlinType)).toIrType()!!
|
||||
|
||||
@@ -47,6 +35,3 @@ fun IrType.isSubtypeOf(superType: IrType) = toKotlinType().isSubtypeOf(superType
|
||||
|
||||
@Deprecated("Use pure Ir helper")
|
||||
fun IrType.isSubtypeOfClass(superClass: IrClassSymbol) = DescriptorUtils.isSubtypeOfClass(toKotlinType(), superClass.descriptor)
|
||||
|
||||
@Deprecated("Use pure Ir helper")
|
||||
fun IrType.isBuiltinFunctionalTypeOrSubtype() = toKotlinType().isBuiltinFunctionalTypeOrSubtype
|
||||
@@ -5,37 +5,72 @@
|
||||
|
||||
package org.jetbrains.kotlin.ir.util
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||
import org.jetbrains.kotlin.ir.declarations.IrPackageFragment
|
||||
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.types.classifierOrNull
|
||||
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
|
||||
import org.jetbrains.kotlin.ir.types.*
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.utils.DFS
|
||||
|
||||
val kotlinPackageFqn = FqName.fromSegments(listOf("kotlin"))
|
||||
val kotlinReflectionPackageFqn = kotlinPackageFqn.child(Name.identifier("reflection"))
|
||||
|
||||
fun IrType.isFunctionTypeOrSubtype(): Boolean {
|
||||
fun IrType.isFunction(): Boolean {
|
||||
val classifier = classifierOrNull ?: return false
|
||||
val name = classifier.descriptor.name.asString()
|
||||
if (!name.startsWith("Function")) return false
|
||||
val declaration = classifier.owner as IrDeclaration
|
||||
val parent = declaration.parent as? IrPackageFragment ?: return false
|
||||
|
||||
val kotlinPackageFqn = FqName.fromSegments(listOf("kotlin"))
|
||||
fun checkType(irType: IrType): Boolean {
|
||||
val classifier = irType.classifierOrNull ?: return false
|
||||
val name = classifier.descriptor.name.asString()
|
||||
if (!name.startsWith("Function")) return false
|
||||
val declaration = classifier.owner as IrDeclaration
|
||||
val parent = declaration.parent as? IrPackageFragment ?: return false
|
||||
return parent.fqName == kotlinPackageFqn
|
||||
}
|
||||
|
||||
return parent.fqName == kotlinPackageFqn
|
||||
|
||||
fun IrType.isKFunction(): Boolean {
|
||||
val classifier = classifierOrNull ?: return false
|
||||
val name = classifier.descriptor.name.asString()
|
||||
if (!name.startsWith("KFunction")) return false
|
||||
val declaration = classifier.owner as IrDeclaration
|
||||
val parent = declaration.parent as? IrPackageFragment ?: return false
|
||||
|
||||
return parent.fqName == kotlinReflectionPackageFqn
|
||||
}
|
||||
|
||||
|
||||
fun IrType.superTypes(): List<IrType> {
|
||||
val classifier = classifierOrNull?.owner ?: return emptyList()
|
||||
return when(classifier) {
|
||||
is IrClass -> classifier.superTypes
|
||||
is IrTypeParameter -> classifier.superTypes
|
||||
else -> throw IllegalStateException()
|
||||
}
|
||||
}
|
||||
|
||||
fun superTypes(irType: IrType): List<IrType> {
|
||||
val classifier = irType.classifierOrNull?.owner ?: return emptyList()
|
||||
return when(classifier) {
|
||||
is IrClass -> classifier.superTypes
|
||||
is IrTypeParameter -> classifier.superTypes
|
||||
else -> throw IllegalStateException()
|
||||
}
|
||||
fun IrType.typeParameterSuperTypes(): List<IrType> {
|
||||
val classifier = classifierOrNull ?: return emptyList()
|
||||
return when(classifier) {
|
||||
is IrTypeParameterSymbol -> classifier.owner.superTypes
|
||||
is IrClassSymbol -> emptyList()
|
||||
else -> throw IllegalStateException()
|
||||
}
|
||||
}
|
||||
|
||||
return DFS.ifAny(listOf(this), ::superTypes, ::checkType)
|
||||
}
|
||||
fun IrType.isFunctionTypeOrSubtype(): Boolean = DFS.ifAny(listOf(this), { it.superTypes() }, { it.isFunction() })
|
||||
|
||||
fun IrType.isTypeParameter() = classifierOrNull is IrTypeParameterSymbol
|
||||
|
||||
fun IrType.isInterface() = (classifierOrNull?.owner as? IrClass)?.kind == ClassKind.INTERFACE
|
||||
|
||||
fun IrType.isFunctionOrKFunction() = isFunction() || isKFunction()
|
||||
|
||||
fun IrType.isNullable(): Boolean = DFS.ifAny(listOf(this), { it.typeParameterSuperTypes() }, {
|
||||
when (it) {
|
||||
is IrSimpleType -> it.hasQuestionMark
|
||||
else -> it is IrDynamicType
|
||||
}
|
||||
})
|
||||
-1
@@ -6,7 +6,6 @@
|
||||
package org.jetbrains.kotlin.ir.backend.js.lower
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.FileLoweringPass
|
||||
import org.jetbrains.kotlin.backend.common.utils.isNullable
|
||||
import org.jetbrains.kotlin.backend.common.utils.isSubtypeOf
|
||||
import org.jetbrains.kotlin.backend.common.utils.isSubtypeOfClass
|
||||
import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor
|
||||
|
||||
+4
@@ -22,6 +22,10 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
|
||||
import org.jetbrains.kotlin.ir.types.*
|
||||
import org.jetbrains.kotlin.ir.util.isFunctionOrKFunction
|
||||
import org.jetbrains.kotlin.ir.util.isInterface
|
||||
import org.jetbrains.kotlin.ir.util.isNullable
|
||||
import org.jetbrains.kotlin.ir.util.isTypeParameter
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
|
||||
|
||||
class TypeOperatorLowering(val context: JsIrBackendContext) : FileLoweringPass {
|
||||
|
||||
Reference in New Issue
Block a user