[JS IR BE] Add intrinsics for hashCode on primitive number types

This commit is contained in:
Zalim Bashorov
2020-01-13 21:05:22 +03:00
parent adfb296e45
commit 9e59d93ad5
2 changed files with 17 additions and 5 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@@ -15,20 +15,17 @@ import org.jetbrains.kotlin.ir.declarations.IrProperty
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrExternalPackageFragmentSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrExternalPackageFragmentSymbolImpl
import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.defaultType import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.types.isLong import org.jetbrains.kotlin.ir.types.isLong
import org.jetbrains.kotlin.ir.util.companionObject
import org.jetbrains.kotlin.ir.util.constructors import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.findDeclaration import org.jetbrains.kotlin.ir.util.findDeclaration
import org.jetbrains.kotlin.ir.util.kotlinPackageFqn import org.jetbrains.kotlin.ir.util.kotlinPackageFqn
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi2ir.findSingleFunction import org.jetbrains.kotlin.psi2ir.findSingleFunction
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
import java.util.* import java.util.*
@@ -146,6 +143,7 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
val jsObjectCreate = defineObjectCreateIntrinsic() // Object.create val jsObjectCreate = defineObjectCreateIntrinsic() // Object.create
val jsCode = getInternalFunction("js") // js("<code>") val jsCode = getInternalFunction("js") // js("<code>")
val jsHashCode = getInternalFunction("hashCode") val jsHashCode = getInternalFunction("hashCode")
val jsGetNumberHashCode = getInternalFunction("getNumberHashCode")
val jsGetObjectHashCode = getInternalFunction("getObjectHashCode") val jsGetObjectHashCode = getInternalFunction("getObjectHashCode")
val jsToString = getInternalFunction("toString") val jsToString = getInternalFunction("toString")
val jsAnyToString = getInternalFunction("anyToString") val jsAnyToString = getInternalFunction("anyToString")
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@@ -56,6 +56,7 @@ class NumberOperatorCallsTransformer(context: JsIrBackendContext) : CallsTransfo
for (type in primitiveNumbers) { for (type in primitiveNumbers) {
add(type, Name.identifier("rangeTo"), ::transformRangeTo) add(type, Name.identifier("rangeTo"), ::transformRangeTo)
add(type, Name.identifier("hashCode"), ::transformHashCode)
} }
for (type in primitiveNumbers) { for (type in primitiveNumbers) {
@@ -97,6 +98,19 @@ class NumberOperatorCallsTransformer(context: JsIrBackendContext) : CallsTransfo
} }
} }
private fun transformHashCode(call: IrFunctionAccessExpression): IrExpression {
return with(call.symbol.owner.dispatchReceiverParameter!!.type) {
when {
isByte() || isShort() || isInt() ->
call.dispatchReceiver!!
isFloat() || isDouble() ->
// TODO introduce doubleToHashCode?
irCall(call, intrinsics.jsGetNumberHashCode, receiversAsArguments = true)
else -> call
}
}
}
private fun irBinaryOp( private fun irBinaryOp(
call: IrFunctionAccessExpression, call: IrFunctionAccessExpression,
intrinsic: IrFunctionSymbol, intrinsic: IrFunctionSymbol,