translator: add numeric literals, fix constant type resolving

This commit is contained in:
Alexey Stepanov
2016-08-08 13:55:30 +03:00
parent c26ea6b5ae
commit 445824f72e
4 changed files with 23 additions and 10 deletions
@@ -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? {
@@ -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)
@@ -0,0 +1,3 @@
numeric_literals_1_Int(0x20) == 32
numeric_literals_1_LL() == 320
numeric_literals_1_Binary() == 127
@@ -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
}