[FIR] Support constant unary operators for integer literal operators

^KT-38895
This commit is contained in:
Dmitriy Novozhilov
2022-02-04 11:06:27 +03:00
committed by teamcity
parent 52b72a7dac
commit 1591518cf6
14 changed files with 116 additions and 20 deletions
@@ -5,15 +5,19 @@
package org.jetbrains.kotlin.fir.scopes.impl
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.StandardClassIds
object ConvertibleIntegerOperators {
val operatorsNames: Set<Name> = listOf(
val binaryOperatorsNames: Set<Name> = listOf(
"plus", "minus", "times", "div", "rem",
"and", "or", "xor",
"shl", "shr", "ushr"
).mapTo(mutableSetOf()) { Name.identifier(it) }
).toNameSet()
// Constant conversion for those unary operators works only for signed integers
val unaryOperatorNames: Set<Name> = listOf(
"inv", "unaryPlus", "unaryMinus"
).toNameSet()
private fun List<String>.toNameSet(): Set<Name> = mapTo(mutableSetOf()) { Name.identifier(it) }
}
@@ -40,12 +40,18 @@ class FirIntegerConstantOperatorScope(
private val mappedFunctions = mutableMapOf<Name, FirNamedFunctionSymbol>()
override fun processFunctionsByName(name: Name, processor: (FirNamedFunctionSymbol) -> Unit) {
if (name !in ConvertibleIntegerOperators.operatorsNames) {
// Constant conversion for those unary operators works only for signed integers
val isUnaryOperator = !isUnsigned && (name in ConvertibleIntegerOperators.unaryOperatorNames)
val isBinaryOperator = name in ConvertibleIntegerOperators.binaryOperatorsNames
if (!isUnaryOperator && !isBinaryOperator) {
return baseScope.processFunctionsByName(name, processor)
}
val wrappedSymbol = mappedFunctions.getOrPut(name) {
val allFunctions = baseScope.getFunctions(name)
val functionSymbol = allFunctions.first {
// unary operators have only one overload
if (isUnaryOperator) return@first true
val coneType = it.fir.valueParameters.first().returnTypeRef.coneType
if (isUnsigned) {
coneType.isUInt