[IR] * Implement isSubtypeOfClass and commonSuperclass based on pure Ir
* Implement type strict equality check
* Move type checkers into Ir
* Implement strict Fqn-based classifier equality checker
This commit is contained in:
@@ -11,13 +11,9 @@ 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.symbols.IrClassSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
|
||||
import org.jetbrains.kotlin.ir.types.IrDynamicType
|
||||
import org.jetbrains.kotlin.ir.types.IrSimpleType
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.types.classifierOrNull
|
||||
import org.jetbrains.kotlin.ir.types.*
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.utils.DFS
|
||||
@@ -42,19 +38,11 @@ fun IrType.isNameInPackage(prefix: String, packageFqName: FqName): Boolean {
|
||||
|
||||
}
|
||||
|
||||
|
||||
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 IrType.superTypes() = classifierOrNull?.superTypes() ?: emptyList()
|
||||
|
||||
fun IrType.typeParameterSuperTypes(): List<IrType> {
|
||||
val classifier = classifierOrNull ?: return emptyList()
|
||||
return when(classifier) {
|
||||
return when (classifier) {
|
||||
is IrTypeParameterSymbol -> classifier.owner.superTypes
|
||||
is IrClassSymbol -> emptyList()
|
||||
else -> throw IllegalStateException()
|
||||
@@ -92,4 +80,4 @@ private inline fun IrType.isTypeFromKotlinPackage(namePredicate: (Name) -> Boole
|
||||
} else return false
|
||||
}
|
||||
|
||||
fun IrType.isPrimitiveArray() = isTypeFromKotlinPackage { it in FQ_NAMES.primitiveArrayTypeShortNames }
|
||||
fun IrType.isPrimitiveArray() = isTypeFromKotlinPackage { it in FQ_NAMES.primitiveArrayTypeShortNames }
|
||||
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.ir.symbols
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
|
||||
interface IrClassifierEqualityChecker {
|
||||
fun areEqual(left: IrClassifierSymbol, right: IrClassifierSymbol): Boolean
|
||||
}
|
||||
|
||||
object FqNameEqualityChecker : IrClassifierEqualityChecker {
|
||||
override fun areEqual(left: IrClassifierSymbol, right: IrClassifierSymbol): Boolean {
|
||||
if (left === right) return true
|
||||
if (!left.isBound || !right.isBound) checkViaDescriptors(left.descriptor, right.descriptor)
|
||||
return checkViaDeclarations(left.owner, right.owner)
|
||||
}
|
||||
|
||||
private val IrDeclarationWithName.fqName
|
||||
get(): FqName? {
|
||||
val parentFqName = when (val parent = parent) {
|
||||
is IrPackageFragment -> parent.fqName
|
||||
is IrDeclarationWithName -> parent.fqName
|
||||
else -> return null
|
||||
}
|
||||
return parentFqName?.child(name)
|
||||
}
|
||||
|
||||
private fun checkViaDeclarations(c1: IrSymbolOwner, c2: IrSymbolOwner): Boolean {
|
||||
if (c1 is IrClass && c2 is IrClass) {
|
||||
return c1.fqName == c2.fqName
|
||||
}
|
||||
|
||||
return c1 == c2
|
||||
}
|
||||
|
||||
private fun checkViaDescriptors(c1: ClassifierDescriptor, c2: ClassifierDescriptor) = c1.typeConstructor == c2.typeConstructor
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.ir.types
|
||||
|
||||
import org.jetbrains.kotlin.ir.symbols.*
|
||||
import org.jetbrains.kotlin.ir.util.render
|
||||
import org.jetbrains.kotlin.utils.DFS
|
||||
|
||||
|
||||
fun IrClassifierSymbol.superTypes() = when (this) {
|
||||
is IrClassSymbol -> owner.superTypes
|
||||
is IrTypeParameterSymbol -> owner.superTypes
|
||||
else -> emptyList<IrType>()
|
||||
}
|
||||
|
||||
fun IrClassifierSymbol.isSubtypeOfClass(superClass: IrClassSymbol): Boolean {
|
||||
if (FqNameEqualityChecker.areEqual(this, superClass)) return true
|
||||
return superTypes().any { it.isSubtypeOfClass(superClass) }
|
||||
}
|
||||
|
||||
fun IrType.isSubtypeOfClass(superClass: IrClassSymbol): Boolean {
|
||||
if (this !is IrSimpleType) return false
|
||||
return classifier.isSubtypeOfClass(superClass)
|
||||
}
|
||||
|
||||
fun IrType.isEqualTo(that: IrType): Boolean {
|
||||
if (this is IrDynamicType && that is IrDynamicType) return true
|
||||
if (this is IrErrorType || that is IrErrorType) return false
|
||||
if (this === that) return true
|
||||
if (this is IrSimpleType && that is IrSimpleType) return FqNameEqualityChecker.areEqual(this.classifier, that.classifier) &&
|
||||
this.arguments.zip(that.arguments).all { (ths, tht) ->
|
||||
when (ths) {
|
||||
is IrStarProjection -> tht is IrStarProjection
|
||||
is IrTypeProjection -> tht is IrTypeProjection
|
||||
&& ths.variance == tht.variance
|
||||
&& ths.type.isEqualTo(tht.type)
|
||||
else -> error("Unsupported Type Argument")
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fun Collection<IrClassifierSymbol>.commonSuperclass(): IrClassifierSymbol {
|
||||
var superClassifiers: MutableSet<IrClassifierSymbol>? = null
|
||||
|
||||
require(isNotEmpty())
|
||||
|
||||
val order = fold(emptyList<IrClassifierSymbol>()) { _, classifierSymbol ->
|
||||
val visited = mutableSetOf<IrClassifierSymbol>()
|
||||
DFS.topologicalOrder(
|
||||
listOf(classifierSymbol), { it.superTypes().map { s -> (s as IrSimpleType).classifier } },
|
||||
DFS.VisitedWithSet(visited)
|
||||
).also {
|
||||
if (superClassifiers == null) {
|
||||
superClassifiers = visited
|
||||
} else {
|
||||
superClassifiers!!.apply {
|
||||
retainAll { c -> visited.any { v -> FqNameEqualityChecker.areEqual(c, v) } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
requireNotNull(superClassifiers)
|
||||
|
||||
return order.firstOrNull { o -> superClassifiers!!.any { s -> FqNameEqualityChecker.areEqual(o, s) } }
|
||||
?: error(
|
||||
"No common superType found for non-empty set of classifiers: ${joinToString(
|
||||
prefix = "[",
|
||||
postfix = "]"
|
||||
) { it.owner.render() }}"
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user