Implement stub methods generation for Kotlin Immutable Collection classes.

This change is to fill the gap between Kotlin Collection
classes(immutable) and Java Collection classes(mutable), to avoid
calling an unsupported operation like remove() on an immutable class in
jvm.
This commit is contained in:
Jiaxiang Chen
2019-04-02 14:02:15 -07:00
committed by Georgy Bronnikov
parent 8c3cef97bd
commit afcbd76c9e
22 changed files with 259 additions and 19 deletions
@@ -155,6 +155,22 @@ abstract class Symbols<out T : CommonBackendContext>(val context: T, private val
val arrays = primitiveArrays.values + unsignedArrays.values + array
val collection = symbolTable.referenceClass(builtIns.collection)
val set = symbolTable.referenceClass(builtIns.set)
val list = symbolTable.referenceClass(builtIns.list)
val map = symbolTable.referenceClass(builtIns.map)
val mapEntry = symbolTable.referenceClass(builtIns.mapEntry)
val iterable = symbolTable.referenceClass(builtIns.iterable)
val listIterator = symbolTable.referenceClass(builtIns.listIterator)
val mutableCollection = symbolTable.referenceClass(builtIns.mutableCollection)
val mutableSet = symbolTable.referenceClass(builtIns.mutableSet)
val mutableList = symbolTable.referenceClass(builtIns.mutableList)
val mutableMap = symbolTable.referenceClass(builtIns.mutableMap)
val mutableMapEntry = symbolTable.referenceClass(builtIns.mutableMapEntry)
val mutableIterable = symbolTable.referenceClass(builtIns.mutableIterable)
val mutableIterator = symbolTable.referenceClass(builtIns.mutableIterator)
val mutableListIterator = symbolTable.referenceClass(builtIns.mutableListIterator)
abstract val copyRangeTo: Map<ClassDescriptor, IrSimpleFunctionSymbol>
abstract val coroutineImpl: IrClassSymbol
@@ -119,3 +119,28 @@ fun IrType.substitute(substitutionMap: Map<IrTypeParameterSymbol, IrType>): IrTy
newAnnotations
)
}
private fun getImmediateSupertypes(irClass: IrClass): List<IrSimpleType> {
val originalSupertypes = irClass.superTypes
val args = irClass.defaultType.arguments.mapNotNull { (it as? IrTypeProjection)?.type }
return originalSupertypes
.filter { it.classOrNull != null }
.map { superType ->
superType.substitute(superType.classOrNull!!.owner.typeParameters, args) as IrSimpleType
}
}
private fun collectAllSupertypes(irClass: IrClass, result: MutableSet<IrSimpleType>) {
val immediateSupertypes = getImmediateSupertypes(irClass)
result.addAll(immediateSupertypes)
for (supertype in immediateSupertypes) {
supertype.classOrNull.let { collectAllSupertypes(it!!.owner, result) }
}
}
fun getAllSupertypes(irClass: IrClass): MutableSet<IrSimpleType> {
val result = HashSet<IrSimpleType>()
collectAllSupertypes(irClass, result)
return result
}