From 3c22d0946b8cb5bd1e48e037ea476189a5d8cdf7 Mon Sep 17 00:00:00 2001 From: e5l Date: Tue, 16 Aug 2016 12:34:16 +0300 Subject: [PATCH] translator: add while block, tests --- .../kotlin/org/kotlinnative/translator/BlockCodegen.kt | 7 +++++-- translator/src/test/kotlin/tests/input/when_block.txt | 3 +++ translator/src/test/kotlin/tests/kotlin/when_block.kt | 9 +++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 translator/src/test/kotlin/tests/input/when_block.txt create mode 100644 translator/src/test/kotlin/tests/kotlin/when_block.kt diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt index e20bb6513a5..18b9dfbe435 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt @@ -856,7 +856,10 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va successExpression = codeBuilder.loadAndGetVariable(successExpression) } - codeBuilder.storeVariable(resultVariable, successExpression ?: return) + if (successExpression != null) { + codeBuilder.storeVariable(resultVariable, successExpression) + } + codeBuilder.addUnconditionalJump(endLabel) codeBuilder.addComment("end last condition item") } @@ -870,7 +873,7 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va val targetExpression = evaluateExpression(whenExpression, scopeDepth + 1)!! val resultVariable = codeBuilder.getNewVariable(expressionType, pointer = 1) - if (expressionType !is LLVMVoidType) { + if (expressionType !is LLVMVoidType && expressionType !is LLVMNullType) { codeBuilder.allocStackPointedVarAsValue(resultVariable) } diff --git a/translator/src/test/kotlin/tests/input/when_block.txt b/translator/src/test/kotlin/tests/input/when_block.txt new file mode 100644 index 00000000000..59450fdc4d8 --- /dev/null +++ b/translator/src/test/kotlin/tests/input/when_block.txt @@ -0,0 +1,3 @@ +when_block_test1_Int(0) == 1 +when_block_test1_Int(1) == 0 +when_block_test1_Int(2) == 0 diff --git a/translator/src/test/kotlin/tests/kotlin/when_block.kt b/translator/src/test/kotlin/tests/kotlin/when_block.kt new file mode 100644 index 00000000000..22a1a996c4a --- /dev/null +++ b/translator/src/test/kotlin/tests/kotlin/when_block.kt @@ -0,0 +1,9 @@ + +fun when_block_test1(i: Int): Boolean { + when (i) { + 0 -> return true + 1 -> return false + } + + return false +}