JVM_IR: Generate args check in existing methods for special bridge methods.

Fix the number of arguments checked (1 for getOrDefault and 2 for
remove).
This commit is contained in:
Mads Ager
2019-11-18 10:56:44 +01:00
committed by Georgy Bronnikov
parent 369d9bfdab
commit 15ed342282
11 changed files with 102 additions and 54 deletions
@@ -17,6 +17,8 @@ import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
data class SpecialMethodWithDefaultInfo(val defaultValueGenerator: (IrSimpleFunction) -> IrExpression, val argumentsToCheck: Int)
class SpecialBridgeMethods(val context: CommonBackendContext) {
private data class SpecialMethodDescription(val kotlinFqClassName: FqName?, val name: Name, val arity: Int)
@@ -48,20 +50,20 @@ class SpecialBridgeMethods(val context: CommonBackendContext) {
private fun getSecondArg(bridge: IrSimpleFunction) =
IrGetValueImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, bridge.valueParameters[1].symbol)
private val SPECIAL_METHODS_WITH_DEFAULTS_MAP = mapOf<SpecialMethodDescription, (IrSimpleFunction) -> IrExpression>(
makeDescription("kotlin.collections.Collection", "contains", 1) to ::constFalse,
makeDescription("kotlin.collections.MutableCollection", "remove", 1) to ::constFalse,
makeDescription("kotlin.collections.Map", "containsKey", 1) to ::constFalse,
makeDescription("kotlin.collections.Map", "containsValue", 1) to ::constFalse,
makeDescription("kotlin.collections.MutableMap", "remove", 2) to ::constFalse,
makeDescription("kotlin.collections.Map", "getOrDefault", 1) to ::getSecondArg,
makeDescription("kotlin.collections.Map", "get", 1) to ::constNull,
makeDescription("kotlin.collections.MutableMap", "remove", 1) to ::constNull,
makeDescription("kotlin.collections.List", "indexOf", 1) to ::constMinusOne,
makeDescription("kotlin.collections.List", "lastIndexOf", 1) to ::constMinusOne
private val SPECIAL_METHODS_WITH_DEFAULTS_MAP = mapOf(
makeDescription("kotlin.collections.Collection", "contains", 1) to SpecialMethodWithDefaultInfo(::constFalse, 1),
makeDescription("kotlin.collections.MutableCollection", "remove", 1) to SpecialMethodWithDefaultInfo(::constFalse, 1),
makeDescription("kotlin.collections.Map", "containsKey", 1) to SpecialMethodWithDefaultInfo(::constFalse, 1),
makeDescription("kotlin.collections.Map", "containsValue", 1) to SpecialMethodWithDefaultInfo(::constFalse, 1),
makeDescription("kotlin.collections.MutableMap", "remove", 2) to SpecialMethodWithDefaultInfo(::constFalse, 2),
makeDescription("kotlin.collections.Map", "getOrDefault", 2) to SpecialMethodWithDefaultInfo(::getSecondArg, 1),
makeDescription("kotlin.collections.Map", "get", 1) to SpecialMethodWithDefaultInfo(::constNull, 1),
makeDescription("kotlin.collections.MutableMap", "remove", 1) to SpecialMethodWithDefaultInfo(::constNull, 1),
makeDescription("kotlin.collections.List", "indexOf", 1) to SpecialMethodWithDefaultInfo(::constMinusOne, 1),
makeDescription("kotlin.collections.List", "lastIndexOf", 1) to SpecialMethodWithDefaultInfo(::constMinusOne, 1)
)
fun findSpecialWithOverride(irFunction: IrSimpleFunction): Pair<IrSimpleFunction, (IrSimpleFunction) -> IrExpression>? {
fun findSpecialWithOverride(irFunction: IrSimpleFunction): Pair<IrSimpleFunction, SpecialMethodWithDefaultInfo>? {
irFunction.allOverridden().forEach { overridden ->
val description = overridden.toDescription()
SPECIAL_METHODS_WITH_DEFAULTS_MAP[description]?.let {