Added switch (enum-only when) lowering

Added enum ordinal to serialization
This commit is contained in:
Sergey Bogolepov
2018-05-10 19:03:09 +07:00
committed by Sergey Bogolepov
parent 1afc5e5e90
commit 5ad749d56b
12 changed files with 293 additions and 23 deletions
+11
View File
@@ -763,6 +763,11 @@ task enum_loop(type: RunKonanTest) {
source = "codegen/enum/loop.kt"
}
task switchLowering(type: RunKonanTest) {
goldValue = "EnumA.A\nok\nok\nok\nok\nok\n"
source = "codegen/enum/switchLowering.kt"
}
task mangling(type: LinkKonanTest) {
goldValue =
"Int direct [1, 2, 3, 4]\n" +
@@ -2420,6 +2425,12 @@ task serialized_no_typemap(type: RunStandaloneKonanTest) {
goldValue = "OK\n"
}
task serialized_enum_ordinal(type: LinkKonanTest) {
source = "serialization/enum_ordinal/main.kt"
lib = "serialization/enum_ordinal/library.kt"
goldValue = "0\n1\n2\nb\n"
}
task testing_annotations(type: RunStandaloneKonanTest) {
source = "testing/annotations.kt"
flags = ['-tr']
@@ -0,0 +1,86 @@
package codegen.enum.switchLowering
import kotlin.test.*
enum class EnumA {
A, B, C
}
enum class EnumB {
A, B
}
enum class E {
ONE, TWO, THREE
}
fun produceEntry() = EnumA.A
// Check that we fail on comparison of different enum types.
fun differentEnums() {
println(when (produceEntry()) {
EnumB.A -> "EnumB.A"
EnumA.A -> "EnumA.A"
EnumA.B -> "EnumA.B"
else -> "nah"
})
}
// Nullable subject shouldn't be lowered.
fun nullable() {
val x: EnumA? = null
when(x) {
EnumA.A -> println("fail")
else -> println("ok")
}
}
// Operator overloading won't trick us!
fun operatorOverloading() {
operator fun E.contains(other: E): Boolean = false
val y = E.ONE
when(y) {
in E.ONE -> println("Should not reach here")
else -> println("ok")
}
}
fun smoke1() {
when (produceEntry()) {
EnumA.B -> println("error")
EnumA.A -> println("ok")
EnumA.C -> println("error")
}
}
fun smoke2() {
when (produceEntry()) {
EnumA.B -> println("error")
else -> println("ok")
}
}
fun eA() = EnumA.A
fun eB() = EnumA.B
fun nestedWhen() {
println(when (eA()) {
EnumA.A, EnumA.C -> when (eB()) {
EnumA.B -> "ok"
else -> "nope"
}
else -> "nope"
})
}
@Test fun runTest() {
differentEnums()
nullable()
operatorOverloading()
smoke1()
smoke2()
nestedWhen()
}
@@ -0,0 +1,9 @@
enum class Color {
RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW
}
fun determineColor(code: Int): Color = when (code) {
0 -> Color.BLUE
1 -> Color.MAGENTA
else -> Color.CYAN
}
@@ -0,0 +1,14 @@
fun main(args: Array<String>) {
println(Color.RED.ordinal)
println(Color.GREEN.ordinal)
println(Color.BLUE.ordinal)
val color = when (determineColor(args.size)) {
Color.RED -> println("r")
Color.GREEN -> println("g")
Color.BLUE -> println("b")
Color.CYAN -> println("c")
Color.MAGENTA -> println("m")
Color.YELLOW -> println("y")
}
}