[FIR] Extract common code for converting unaryMinus call on integer literal

This commit is contained in:
Dmitriy Novozhilov
2022-02-03 14:48:02 +03:00
committed by teamcity
parent 563e649ac3
commit cc86ca2a0f
3 changed files with 28 additions and 28 deletions
@@ -408,17 +408,7 @@ class ExpressionsConverter(
) { getAsFirExpression(this) }
}
val receiver = getAsFirExpression<FirExpression>(argument, "No operand")
if (operationToken == PLUS || operationToken == MINUS) {
if (receiver is FirConstExpression<*> && receiver.kind == ConstantValueKind.IntegerLiteral) {
val value = receiver.value as Long
val convertedValue = when (operationToken) {
MINUS -> -value
PLUS -> value
else -> error("Should not be here")
}
return buildConstExpression(unaryExpression.toFirSourceElement(), ConstantValueKind.IntegerLiteral, convertedValue)
}
}
convertUnaryPlusMinusCallOnIntegerLiteralIfNecessary(unaryExpression, receiver, operationToken)?.let { return it }
buildFunctionCall {
source = unaryExpression.toFirSourceElement()
calleeReference = buildSimpleNamedReference {
@@ -2239,21 +2239,9 @@ open class RawFirBuilder(
}
val receiver = argument.toFirExpression("No operand")
if (operationToken == PLUS || operationToken == MINUS) {
if (receiver is FirConstExpression<*> && receiver.kind == ConstantValueKind.IntegerLiteral) {
val value = receiver.value as Long
val convertedValue = when (operationToken) {
MINUS -> -value
PLUS -> value
else -> error("Should not be here")
}
return buildConstExpression(
expression.toKtPsiSourceElement(),
ConstantValueKind.IntegerLiteral,
convertedValue
)
}
}
convertUnaryPlusMinusCallOnIntegerLiteralIfNecessary(expression, receiver, operationToken)?.let { return it }
buildFunctionCall {
source = expression.toFirSourceElement()
calleeReference = buildSimpleNamedReference {
@@ -32,8 +32,7 @@ import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
import org.jetbrains.kotlin.fir.types.impl.ConeTypeParameterTypeImpl
import org.jetbrains.kotlin.lexer.KtTokens.CLOSING_QUOTE
import org.jetbrains.kotlin.lexer.KtTokens.OPEN_QUOTE
import org.jetbrains.kotlin.lexer.KtTokens.*
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
@@ -409,6 +408,29 @@ abstract class BaseFirBuilder<T>(val baseSession: FirSession, val context: Conte
}
}
fun convertUnaryPlusMinusCallOnIntegerLiteralIfNecessary(
source: T,
receiver: FirExpression,
operationToken: IElementType
): FirExpression? {
if (receiver !is FirConstExpression<*>) return null
if (receiver.kind != ConstantValueKind.IntegerLiteral) return null
if (operationToken != PLUS && operationToken != MINUS) return null
val value = receiver.value as Long
val convertedValue = when (operationToken) {
MINUS -> -value
PLUS -> value
else -> error("Should not be here")
}
return buildConstExpression(
source.toFirSourceElement(),
ConstantValueKind.IntegerLiteral,
convertedValue
)
}
fun Array<out T?>.toInterpolatingCall(
base: T,
getElementType: (T) -> IElementType = { it.elementType },