From e5dbadcb83877898c81982403c1ad26b85395d3e Mon Sep 17 00:00:00 2001 From: Alexey Stepanov Date: Tue, 23 Aug 2016 11:03:13 +0300 Subject: [PATCH] translator: fix comparison in nullable primitive types, tests for n.p.t. --- .../translator/llvm/types/LLVMBooleanType.kt | 4 +-- .../translator/llvm/types/LLVMByteType.kt | 4 +-- .../translator/llvm/types/LLVMCharType.kt | 4 +-- .../translator/llvm/types/LLVMFloatType.kt | 4 +-- .../translator/llvm/types/LLVMIntType.kt | 4 +-- .../translator/llvm/types/LLVMLongType.kt | 4 +-- .../translator/llvm/types/LLVMShortType.kt | 4 +-- .../input/nullable_primitive_types_1.txt | 4 +++ .../kotlin/nullable_primitive_types_1.kt | 32 +++++++++++++++++++ 9 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 translator/src/test/kotlin/tests/input/nullable_primitive_types_1.txt create mode 100644 translator/src/test/kotlin/tests/kotlin/nullable_primitive_types_1.kt diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMBooleanType.kt b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMBooleanType.kt index 5673b7aad4f..4fffe2116a4 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMBooleanType.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMBooleanType.kt @@ -31,10 +31,10 @@ class LLVMBooleanType() : LLVMType() { LLVMExpression(LLVMBooleanType(), "icmp sge i1 $firstOp, $secondOp") override fun operatorEq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = - LLVMExpression(LLVMBooleanType(), "icmp eq i1 $firstOp, $secondOp") + LLVMExpression(LLVMBooleanType(), "icmp eq i1" + (if ((firstOp.pointer > 0) || (secondOp.pointer > 0)) "*" else "") + " $firstOp, $secondOp") override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = - LLVMExpression(LLVMBooleanType(), "icmp ne i1 $firstOp, $secondOp") + LLVMExpression(LLVMBooleanType(), "icmp ne i1" + (if ((firstOp.pointer > 0) || (secondOp.pointer > 0)) "*" else "") + " $firstOp, $secondOp") override fun operatorOr(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = LLVMExpression(LLVMBooleanType(), "or i1 $firstOp, $secondOp") diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMByteType.kt b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMByteType.kt index f228db83e8e..0da474a927e 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMByteType.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMByteType.kt @@ -17,10 +17,10 @@ class LLVMByteType() : LLVMType() { LLVMExpression(LLVMBooleanType(), "icmp sge i8 $firstOp, $secondOp") override fun operatorEq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = - LLVMExpression(LLVMBooleanType(), "icmp eq i8 $firstOp, $secondOp") + LLVMExpression(LLVMBooleanType(), "icmp eq i8" + (if ((firstOp.pointer > 0) || (secondOp.pointer > 0)) "*" else "") + " $firstOp, $secondOp") override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = - LLVMExpression(LLVMBooleanType(), "icmp ne i8 $firstOp, $secondOp") + LLVMExpression(LLVMBooleanType(), "icmp ne i8" + (if ((firstOp.pointer > 0) || (secondOp.pointer > 0)) "*" else "") + " $firstOp, $secondOp") override fun operatorMod(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = LLVMExpression(LLVMByteType(), "srem i8 $firstOp, $secondOp") diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMCharType.kt b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMCharType.kt index c05adb92efb..e67012c1742 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMCharType.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMCharType.kt @@ -17,10 +17,10 @@ class LLVMCharType() : LLVMType() { LLVMExpression(LLVMBooleanType(), "icmp sge i8 $firstOp, $secondOp") override fun operatorEq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = - LLVMExpression(LLVMBooleanType(), "icmp eq i8 $firstOp, $secondOp") + LLVMExpression(LLVMBooleanType(), "icmp eq i8" + (if ((firstOp.pointer > 0) || (secondOp.pointer > 0)) "*" else "") + " $firstOp, $secondOp") override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = - LLVMExpression(LLVMBooleanType(), "icmp ne i8 $firstOp, $secondOp") + LLVMExpression(LLVMBooleanType(), "icmp ne i8" + (if ((firstOp.pointer > 0) || (secondOp.pointer > 0)) "*" else "") + " $firstOp, $secondOp") override fun equals(other: Any?): Boolean { return other is LLVMCharType diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMFloatType.kt b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMFloatType.kt index 167753c1b2e..5a42b6c2e20 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMFloatType.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMFloatType.kt @@ -37,10 +37,10 @@ class LLVMFloatType() : LLVMType() { LLVMExpression(LLVMBooleanType(), "fcmp oge float i32 $firstOp, $secondOp") override fun operatorEq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = - LLVMExpression(LLVMBooleanType(), "fcmp oeq float $firstOp, $secondOp") + LLVMExpression(LLVMBooleanType(), "fcmp oeq float" + (if ((firstOp.pointer > 0) || (secondOp.pointer > 0)) "*" else "") + " $firstOp, $secondOp") override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = - LLVMExpression(LLVMBooleanType(), "fcmp one float $firstOp, $secondOp") + LLVMExpression(LLVMBooleanType(), "fcmp one float" + (if ((firstOp.pointer > 0) || (secondOp.pointer > 0)) "*" else "") + " $firstOp, $secondOp") override fun operatorMod(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = LLVMExpression(LLVMFloatType(), "frem float $firstOp, $secondOp") diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMIntType.kt b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMIntType.kt index da8064736dc..b0f2fe4a887 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMIntType.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMIntType.kt @@ -56,10 +56,10 @@ class LLVMIntType() : LLVMType() { LLVMExpression(LLVMBooleanType(), "icmp sge i32 $firstOp, $secondOp") override fun operatorEq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = - LLVMExpression(LLVMBooleanType(), "icmp eq i32 $firstOp, $secondOp") + LLVMExpression(LLVMBooleanType(), "icmp eq i32" + (if ((firstOp.pointer > 0) || (secondOp.pointer > 0)) "*" else "") + " $firstOp, $secondOp") override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = - LLVMExpression(LLVMBooleanType(), "icmp ne i32 $firstOp, $secondOp") + LLVMExpression(LLVMBooleanType(), "icmp ne i32" + (if ((firstOp.pointer > 0) || (secondOp.pointer > 0)) "*" else "") + " $firstOp, $secondOp") override fun operatorMod(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = LLVMExpression(LLVMIntType(), "srem i32 $firstOp, $secondOp") diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMLongType.kt b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMLongType.kt index ef12bc79d08..2250ff0476b 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMLongType.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMLongType.kt @@ -50,10 +50,10 @@ class LLVMLongType() : LLVMType() { LLVMExpression(LLVMBooleanType(), "icmp sge i64 $firstOp, $secondOp") override fun operatorEq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = - LLVMExpression(LLVMBooleanType(), "icmp eq i64 $firstOp, $secondOp") + LLVMExpression(LLVMBooleanType(), "icmp eq i64" + (if ((firstOp.pointer > 0) || (secondOp.pointer > 0)) "*" else "") + " $firstOp, $secondOp") override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = - LLVMExpression(LLVMBooleanType(), "icmp ne i64 $firstOp, $secondOp") + LLVMExpression(LLVMBooleanType(), "icmp ne i64" + (if ((firstOp.pointer > 0) || (secondOp.pointer > 0)) "*" else "") + " $firstOp, $secondOp") override fun operatorMod(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = LLVMExpression(LLVMLongType(), "srem i64 $firstOp, $secondOp") diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMShortType.kt b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMShortType.kt index fb3efd3b5aa..616f0fbe4a2 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMShortType.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMShortType.kt @@ -17,10 +17,10 @@ class LLVMShortType() : LLVMType() { LLVMExpression(LLVMBooleanType(), "icmp sge i16 $firstOp, $secondOp") override fun operatorEq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = - LLVMExpression(LLVMBooleanType(), "icmp eq i16 $firstOp, $secondOp") + LLVMExpression(LLVMBooleanType(), "icmp eq i16" + (if ((firstOp.pointer > 0) || (secondOp.pointer > 0)) "*" else "") + " $firstOp, $secondOp") override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = - LLVMExpression(LLVMBooleanType(), "icmp ne i16 $firstOp, $secondOp") + LLVMExpression(LLVMBooleanType(), "icmp ne i16" + (if ((firstOp.pointer > 0) || (secondOp.pointer > 0)) "*" else "") + " $firstOp, $secondOp") override fun operatorMod(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = LLVMExpression(LLVMShortType(), "srem i16 $firstOp, $secondOp") diff --git a/translator/src/test/kotlin/tests/input/nullable_primitive_types_1.txt b/translator/src/test/kotlin/tests/input/nullable_primitive_types_1.txt new file mode 100644 index 00000000000..aee55bbdca5 --- /dev/null +++ b/translator/src/test/kotlin/tests/input/nullable_primitive_types_1.txt @@ -0,0 +1,4 @@ +nullable_primitive_types_1_small() == 29 +nullable_primitive_types_1_extern_Int(11) == 19 +nullable_primitive_types_1_if_null() == 11 +nullable_primitive_types_1_if_not_null() == 2245 diff --git a/translator/src/test/kotlin/tests/kotlin/nullable_primitive_types_1.kt b/translator/src/test/kotlin/tests/kotlin/nullable_primitive_types_1.kt new file mode 100644 index 00000000000..c9d103a7fc6 --- /dev/null +++ b/translator/src/test/kotlin/tests/kotlin/nullable_primitive_types_1.kt @@ -0,0 +1,32 @@ +fun nullable_primitive_types_1_small(): Int { + var variable: Int? = null + variable = 29 + val art: Int? = variable + return art!! +} + +fun nullable_primitive_types_1_extern(x: Int): Int { + val variable: Int? = x + val art: Int? = variable!! + 8 + return art!! +} + +fun nullable_primitive_types_1_if_null(): Int { + val variable: Int? = null + if (variable == null){ + return 11 + } + else{ + return 2245 + } +} + +fun nullable_primitive_types_1_if_not_null(): Int { + val variable: Int? = null + if (variable != null){ + return 11 + } + else{ + return 2245 + } +} \ No newline at end of file