diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt index 0038803110a..455634e6ec9 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt @@ -584,17 +584,12 @@ abstract class BlockCodegen(open val state: TranslationState, open val variableM } private fun evaluateConstantExpression(expr: KtConstantExpression): LLVMConstant { - val node = expr.node + val expressionKotlinType = state.bindingContext.get(BindingContext.EXPRESSION_TYPE_INFO, expr)!!.type!! + val expressionValue = state.bindingContext.get(BindingContext.COMPILE_TIME_VALUE, expr)?.getValue(expressionKotlinType) - val type = when (node.elementType) { - KtNodeTypes.BOOLEAN_CONSTANT -> LLVMBooleanType() - KtNodeTypes.INTEGER_CONSTANT -> LLVMIntType() - KtNodeTypes.FLOAT_CONSTANT -> LLVMDoubleType() - KtNodeTypes.CHARACTER_CONSTANT -> LLVMCharType() - KtNodeTypes.NULL -> LLVMNullType() - else -> throw IllegalArgumentException("Unknown type") - } - return LLVMConstant(node.firstChildNode.text, type, pointer = 0) + val type = LLVMMapStandardType(expressionKotlinType) + + return LLVMConstant(expressionValue?.toString() ?: "", type, pointer = 0) } private fun evaluatePsiElement(element: PsiElement, scopeDepth: Int): LLVMSingleValue? { diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/generators.kt b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/generators.kt index b4e22e09719..25d3a301896 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/generators.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/generators.kt @@ -1,6 +1,7 @@ package org.kotlinnative.translator.llvm import org.jetbrains.kotlin.builtins.isFunctionTypeOrSubtype +import org.jetbrains.kotlin.js.descriptorUtils.nameIfStandardType import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.typeUtil.isUnit import org.kotlinnative.translator.llvm.types.* @@ -22,6 +23,7 @@ fun LLVMInstanceOfStandardType(name: String, type: KotlinType, scope: LLVMScope type.toString() == "Long" -> LLVMVariable(name, LLVMLongType(), name, scope) type.toString() == "Float" -> LLVMVariable(name, LLVMFloatType(), name, scope) type.toString() == "Double" -> LLVMVariable(name, LLVMDoubleType(), name, scope) + type.nameIfStandardType.toString() == "Nothing" -> LLVMVariable(name, LLVMNullType(), name, scope) type.isUnit() -> LLVMVariable("", LLVMVoidType(), name, scope) type.isMarkedNullable -> LLVMVariable(name, LLVMReferenceType(type.toString().dropLast(1)), name, scope, pointer = 1) else -> LLVMVariable(name, LLVMReferenceType(type.toString()), name, scope, pointer = 1) diff --git a/translator/src/test/kotlin/tests/input/numeric_literals_1.txt b/translator/src/test/kotlin/tests/input/numeric_literals_1.txt new file mode 100644 index 00000000000..067b988201b --- /dev/null +++ b/translator/src/test/kotlin/tests/input/numeric_literals_1.txt @@ -0,0 +1,3 @@ +numeric_literals_1_Int(0x20) == 32 +numeric_literals_1_LL() == 320 +numeric_literals_1_Binary() == 127 diff --git a/translator/src/test/kotlin/tests/kotlin/numeric_literals_1.kt b/translator/src/test/kotlin/tests/kotlin/numeric_literals_1.kt new file mode 100644 index 00000000000..8fb70fbdfcf --- /dev/null +++ b/translator/src/test/kotlin/tests/kotlin/numeric_literals_1.kt @@ -0,0 +1,13 @@ +fun numeric_literals_1(x: Int): Int { + return x +} + +fun numeric_literals_1_LL(): Long { + val k = 320L + return k +} + +fun numeric_literals_1_Binary(): Int{ + val k = 0b01111111 + return k +}