From 8ead85596d80c3d0ff652167876ee4673fec2c16 Mon Sep 17 00:00:00 2001 From: Alexey Stepanov Date: Wed, 17 Aug 2016 19:14:50 +0300 Subject: [PATCH] translator: add support variables declaration --- .../kotlinnative/translator/BlockCodegen.kt | 31 ++++++++++++------- .../kotlin/tests/input/declaration_test_1.txt | 2 ++ .../kotlin/tests/input/null_comparison_1.txt | 2 ++ .../kotlin/tests/kotlin/declaration_test_1.kt | 18 +++++++++++ .../kotlin/tests/kotlin/null_comparison_1.kt | 18 +++++++++++ 5 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 translator/src/test/kotlin/tests/input/declaration_test_1.txt create mode 100644 translator/src/test/kotlin/tests/kotlin/declaration_test_1.kt diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt index 5c9d9295c9f..adc6bcf2369 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt @@ -18,6 +18,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getValueArgumentsInParenthese import org.jetbrains.kotlin.resolve.constants.TypedCompileTimeConstant import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver +import org.jetbrains.kotlin.types.typeUtil.isPrimitiveNumberType import org.kotlinnative.translator.llvm.* import org.kotlinnative.translator.llvm.types.* import java.rmi.UnexpectedException @@ -1019,30 +1020,35 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va val variable = state.bindingContext.get(BindingContext.VARIABLE, element)!! val identifier = variable.name.toString() - val assignExpression = evaluateExpression(element.delegateExpressionOrInitializer, scopeDepth) ?: return null + val assignExpression = evaluateExpression(element.delegateExpressionOrInitializer, scopeDepth) + val expectedExpressionType = LLVMMapStandardType(variable.type, state) + val assignExpressionType = assignExpression?.type when (assignExpression) { + null, is LLVMVariable -> { - if (assignExpression.pointer < 2) { - if (assignExpression.type is LLVMReferenceType) { - throw UnexpectedException(element.text) - } - - val allocVar = variableManager.receiveVariable(identifier, assignExpression.type, LLVMRegisterScope(), pointer = 0) + if (variable.type.isPrimitiveNumberType() || (assignExpression == null)) { + val allocVar = variableManager.receiveVariable(identifier, expectedExpressionType, LLVMRegisterScope(), pointer = + if (variable.type.isPrimitiveNumberType()) 0 else 1) codeBuilder.allocStackVar(allocVar) allocVar.pointer++ allocVar.kotlinName = identifier variableManager.addVariable(identifier, allocVar, scopeDepth) + if (assignExpression != null) { + if (assignExpression.type is LLVMReferenceType) { + throw UnexpectedException(element.text) + } - addPrimitiveBinaryOperation(KtTokens.EQ, null, allocVar, assignExpression) + addPrimitiveBinaryOperation(KtTokens.EQ, null, allocVar, assignExpression) + } } else { - assignExpression.kotlinName = identifier + (assignExpression as LLVMVariable).kotlinName = identifier variableManager.addVariable(identifier, assignExpression, scopeDepth) } } is LLVMConstant -> { - if (assignExpression.type is LLVMNullType) { + if (assignExpressionType!! is LLVMNullType) { val reference = LLVMInstanceOfStandardType(identifier, variable.type, state = state) if (state.classes.containsKey(variable.type.toString().dropLast(1))) { (reference.type as LLVMReferenceType).prefix = "class" @@ -1056,10 +1062,13 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va return null } - val newVar = variableManager.receiveVariable(identifier, assignExpression.type!!, LLVMRegisterScope(), pointer = 1) + val newVar = variableManager.receiveVariable(identifier, assignExpressionType, LLVMRegisterScope(), pointer = 1) codeBuilder.addConstant(newVar, assignExpression) variableManager.addVariable(identifier, newVar, scopeDepth) } + null -> { + val xrptrz = 442 + } else -> { throw UnsupportedOperationException() } diff --git a/translator/src/test/kotlin/tests/input/declaration_test_1.txt b/translator/src/test/kotlin/tests/input/declaration_test_1.txt new file mode 100644 index 00000000000..f083d5e5193 --- /dev/null +++ b/translator/src/test/kotlin/tests/input/declaration_test_1.txt @@ -0,0 +1,2 @@ +declaration_test_1_variable() == 85 +declaration_test_1_class() == 96799 diff --git a/translator/src/test/kotlin/tests/input/null_comparison_1.txt b/translator/src/test/kotlin/tests/input/null_comparison_1.txt index 0be278ae353..697fbb8bfa7 100644 --- a/translator/src/test/kotlin/tests/input/null_comparison_1.txt +++ b/translator/src/test/kotlin/tests/input/null_comparison_1.txt @@ -1 +1,3 @@ null_comparison_1() == 87 +null_comparison_1_reassigned() == 87 +null_comparison_1_declaration() == 87 diff --git a/translator/src/test/kotlin/tests/kotlin/declaration_test_1.kt b/translator/src/test/kotlin/tests/kotlin/declaration_test_1.kt new file mode 100644 index 00000000000..8fe160d884c --- /dev/null +++ b/translator/src/test/kotlin/tests/kotlin/declaration_test_1.kt @@ -0,0 +1,18 @@ +class declaration_test_1_cls() + +fun declaration_test_1_variable(): Int { + val a: Int + a = 85 + return a +} + +fun declaration_test_1_class_slave(): declaration_test_1_cls { + val a: declaration_test_1_cls + a = declaration_test_1_cls() + return a +} + + +fun declaration_test_1_class(): Int { + return if (declaration_test_1_class_slave() == null) 10 else 96799 +} \ No newline at end of file diff --git a/translator/src/test/kotlin/tests/kotlin/null_comparison_1.kt b/translator/src/test/kotlin/tests/kotlin/null_comparison_1.kt index 2002c3e0afb..091ed3a6cbc 100644 --- a/translator/src/test/kotlin/tests/kotlin/null_comparison_1.kt +++ b/translator/src/test/kotlin/tests/kotlin/null_comparison_1.kt @@ -11,4 +11,22 @@ fun null_comparison_1(): Int { return 87 } return 945 +} + +fun null_comparison_1_reassigned(): Int { + var a: null_comparison_1_class? = null_comparison_1_class() + a = null_comparison_1_getClass() + if (a == null) { + return 87 + } + return 945 +} + +fun null_comparison_1_declaration(): Int { + var a: null_comparison_1_class? + a = null_comparison_1_getClass() + if (a == null) { + return 87 + } + return 945 } \ No newline at end of file