Support secondary constructors in JVM backend

There is a lot of changes about closures calculating and generating.

1. As classes can have more than one constructor each of them should
have closure arguments.

2. Captured variables set is the same for all of them.

3. Within constructors bodies/delegating calls closure parameters
should be accessed through method arguments because fields may be
not initialized yet.
This commit is contained in:
Denis Zharkov
2015-02-24 19:47:16 +03:00
parent 6779e95767
commit e65382e6ec
35 changed files with 1290 additions and 39 deletions
@@ -0,0 +1,24 @@
fun <R> run(block: () -> R) = block()
inline fun <R> inlineRun(block: () -> R) = block()
class Outer(val outerProp: String) {
fun foo(arg: String): String {
class Local {
val work1 = run { outerProp + arg }
val work2 = inlineRun { outerProp + arg }
val obj = object : Any() {
override fun toString() = outerProp + arg
}
override fun toString() = "${work1}#${work2}#${obj.toString()}"
}
return Local().toString()
}
}
fun box(): String {
val res = Outer("O").foo("K")
if (res != "OK#OK#OK") return "fail: $res"
return "OK"
}
@@ -0,0 +1,35 @@
var sideEffects: String = ""
class A {
var prop: String = ""
init {
sideEffects += prop + "first"
}
constructor(x: String) {
prop = x
sideEffects += "#third"
}
init {
sideEffects += prop + "#second"
}
constructor(x: Int) {
prop += "$x#int"
sideEffects += "#fourth"
}
}
fun box(): String {
val a1 = A("abc")
if (a1.prop != "abc") return "fail1: ${a1.prop}"
if (sideEffects != "first#second#third") return "fail1-sideEffects: ${sideEffects}"
sideEffects = ""
val a2 = A(123)
if (a2.prop != "123#int") return "fail2: ${a2.prop}"
if (sideEffects != "first#second#fourth") return "fail2-sideEffects: ${sideEffects}"
return "OK"
}
@@ -0,0 +1,41 @@
var sideEffects: String = ""
class A {
var prop: String = ""
init {
sideEffects += prop + "first"
}
constructor() {}
constructor(x: String): this() {
prop = x
sideEffects += "#third"
}
init {
sideEffects += prop + "#second"
}
constructor(x: Int): this(x.toString()) {
prop += "#int"
sideEffects += "#fourth"
}
}
fun box(): String {
val a1 = A("abc")
if (a1.prop != "abc") return "fail1: ${a1.prop}"
if (sideEffects != "first#second#third") return "fail1-sideEffects: ${sideEffects}"
sideEffects = ""
val a2 = A(123)
if (a2.prop != "123#int") return "fail2: ${a2.prop}"
if (sideEffects != "first#second#third#fourth") return "fail2-sideEffects: ${sideEffects}"
sideEffects = ""
val a3 = A()
if (a3.prop != "") return "fail2: ${a3.prop}"
if (sideEffects != "first#second") return "fail3-sideEffects: ${sideEffects}"
return "OK"
}
@@ -0,0 +1,39 @@
var sideEffects: String = ""
class A() {
var prop: String = ""
init {
sideEffects += prop + "first"
}
constructor(x: String): this() {
prop = x
sideEffects += "#third"
}
init {
sideEffects += prop + "#second"
}
constructor(x: Int): this(x.toString()) {
prop += "#int"
sideEffects += "#fourth"
}
}
fun box(): String {
val a1 = A("abc")
if (a1.prop != "abc") return "fail1: ${a1.prop}"
if (sideEffects != "first#second#third") return "fail1-sideEffects: ${sideEffects}"
sideEffects = ""
val a2 = A(123)
if (a2.prop != "123#int") return "fail2: ${a2.prop}"
if (sideEffects != "first#second#third#fourth") return "fail2-sideEffects: ${sideEffects}"
sideEffects = ""
val a3 = A()
if (a3.prop != "") return "fail2: ${a3.prop}"
if (sideEffects != "first#second") return "fail3-sideEffects: ${sideEffects}"
return "OK"
}
@@ -0,0 +1,53 @@
data class A1(val prop1: String) {
val prop2: String = "const2"
var prop3: String = ""
constructor(): this("default") {
prop3 = "empty"
}
constructor(x: Int): this(x.toString()) {
prop3 = "int"
}
fun f(): String = "$prop1#$prop2#$prop3"
}
data class A2 private () {
var prop1: String = ""
var prop2: String = "const2"
var prop3: String = ""
constructor(arg: String): this() {
prop1 = arg
}
constructor(x: Double): this() {
prop1 = "default"
prop3 = "empty"
}
constructor(x: Int): this(x.toString()) {
prop3 = "int"
}
fun f(): String = "$prop1#$prop2#$prop3"
}
fun box(): String {
val a1x = A1("asd")
if (a1x.f() != "asd#const2#") return "fail1: ${a1x.f()}"
if (a1x.toString() != "A1(prop1=asd)") return "fail1s: ${a1x.toString()}"
val a1y = A1()
if (a1y.f() != "default#const2#empty") return "fail2: ${a1y.f()}"
if (a1y.toString() != "A1(prop1=default)") return "fail2s: ${a1y.toString()}"
val a1z = A1(5)
if (a1z.f() != "5#const2#int") return "fail3: ${a1z.f()}"
if (a1z.toString() != "A1(prop1=5)") return "fail3s: ${a1z.toString()}"
val a2x = A2("asd")
if (a2x.f() != "asd#const2#") return "fail4: ${a2x.f()}"
val a2y = A2(123.0)
if (a2y.f() != "default#const2#empty") return "fail5: ${a2y.f()}"
val a2z = A2(5)
if (a2z.f() != "5#const2#int") return "fail6: ${a2z.f()}"
return "OK"
}
@@ -0,0 +1,34 @@
val global = "OK"
class A {
val prop: String
constructor(arg1: String = global) {
prop = arg1
}
constructor(arg1: String = global, arg2: Long) {
prop = "$arg1#$arg2"
}
constructor(arg1: String = global, argDouble: Double, arg3: Long = 1L) {
prop = "$arg1#$argDouble#$arg3"
}
}
fun box(): String {
val a1 = A()
if (a1.prop != "OK") return "fail1: ${a1.prop}"
val a2 = A("A")
if (a2.prop != "A") return "fail2: ${a2.prop}"
val a3 = A(arg2=123)
if (a3.prop != "OK#123") return "fail3: ${a3.prop}"
val a4 = A("A", arg2=123)
if (a4.prop != "A#123") return "fail4: ${a4.prop}"
val a5 = A(argDouble=23.0)
if (a5.prop != "OK#23.0#1") return "fail5: ${a5.prop}"
val a6 = A("A", argDouble=23.0)
if (a6.prop != "A#23.0#1") return "fail6: ${a6.prop}"
val a7 = A("A", arg3=2L, argDouble=23.0)
if (a7.prop != "A#23.0#2") return "fail7: ${a7.prop}"
return "OK"
}
@@ -0,0 +1,62 @@
enum class A1(val prop1: String) {
X: A1("asd")
Y: A1() {
override fun f() = super.f() + "#Y"
}
Z: A1(5)
val prop2: String = "const2"
var prop3: String = ""
constructor(): this("default") {
prop3 = "empty"
}
constructor(x: Int): this(x.toString()) {
prop3 = "int"
}
open fun f(): String = "$prop1#$prop2#$prop3"
}
enum class A2 {
val prop1: String
X: A2("asd")
Y: A2() {
override fun f() = super.f() + "#Y"
}
Z: A2(5)
val prop2: String = "const2"
var prop3: String = ""
constructor(arg: String) {
prop1 = arg
}
constructor() {
prop1 = "default"
prop3 = "empty"
}
constructor(x: Int): this(x.toString()) {
prop3 = "int"
}
open fun f(): String = "$prop1#$prop2#$prop3"
}
fun box(): String {
val a1x = A1.X
if (a1x.f() != "asd#const2#") return "fail1: ${a1x.f()}"
val a1y = A1.Y
if (a1y.f() != "default#const2#empty#Y") return "fail2: ${a1y.f()}"
val a1z = A1.Z
if (a1z.f() != "5#const2#int") return "fail3: ${a1z.f()}"
val a2x = A2.X
if (a2x.f() != "asd#const2#") return "fail4: ${a2x.f()}"
val a2y = A2.Y
if (a2y.f() != "default#const2#empty#Y") return "fail5: ${a2y.f()}"
val a2z = A2.Z
if (a2z.f() != "5#const2#int") return "fail6: ${a2z.f()}"
return "OK"
}
@@ -0,0 +1,23 @@
open class B<T>(val x: T, val y: T) {
constructor(x: T): this(x, x) {}
override fun toString() = "$x#$y"
}
class A : B<String> {
constructor(): super("default") {}
constructor(x: String): super(x, "default") {}
}
fun box(): String {
val b1 = B("1", "2").toString()
if (b1 != "1#2") return "fail1: $b1"
val b2 = B("abc").toString()
if (b2 != "abc#abc") return "fail2: $b2"
val a1 = A().toString()
if (a1 != "default#default") return "fail3: $a1"
val a2 = A("xyz").toString()
if (a2 != "xyz#default") return "fail4: $a2"
return "OK"
}
@@ -0,0 +1,79 @@
class Outer {
val outerProp: String
constructor(x: String) {
outerProp = x
}
var sideEffects = ""
inner class A1() {
var prop: String = ""
init {
sideEffects += outerProp + "#" + prop + "first"
}
constructor(x: String): this() {
prop = x + "#${outerProp}"
sideEffects += "#third"
}
init {
sideEffects += prop + "#second"
}
constructor(x: Int): this(x.toString() + "#" + outerProp) {
prop += "#int"
sideEffects += "#fourth"
}
}
inner class A2 {
var prop: String = ""
init {
sideEffects += outerProp + "#" + prop + "first"
}
constructor(x: String) {
prop = x + "#$outerProp"
sideEffects += "#third"
}
init {
sideEffects += prop + "#second"
}
constructor(x: Int) {
prop += "$x#$outerProp#int"
sideEffects += "#fourth"
}
}
}
fun box(): String {
val outer1 = Outer("propValue1")
val a1 = outer1.A1("abc")
if (a1.prop != "abc#propValue1") return "fail1: ${a1.prop}"
if (outer1.sideEffects != "propValue1#first#second#third") return "fail1-sideEffects: ${outer1.sideEffects}"
val outer2 = Outer("propValue2")
val a2 = outer2.A1(123)
if (a2.prop != "123#propValue2#propValue2#int") return "fail2: ${a2.prop}"
if (outer2.sideEffects != "propValue2#first#second#third#fourth") return "fail2-sideEffects: ${outer2.sideEffects}"
val outer3 = Outer("propValue3")
val a3 = outer3.A1()
if (a3.prop != "") return "fail2: ${a3.prop}"
if (outer3.sideEffects != "propValue3#first#second") return "fail3-sideEffects: ${outer3.sideEffects}"
val outer4 = Outer("propValue4")
val a4 = outer4.A2("abc")
if (a4.prop != "abc#propValue4") return "fail4: ${a4.prop}"
if (outer4.sideEffects != "propValue4#first#second#third") return "fail4-sideEffects: ${outer4.sideEffects}"
val outer5 = Outer("propValue5")
val a5 = outer5.A2(123)
if (a5.prop != "123#propValue5#int") return "fail5: ${a5.prop}"
if (outer5.sideEffects != "propValue5#first#second#fourth") return "fail5-sideEffects: ${outer5.sideEffects}"
return "OK"
}
@@ -0,0 +1,66 @@
class Outer {
val outerProp: String
constructor(x: String) {
outerProp = x
}
var sideEffects = ""
abstract inner class A1 {
var parentProp: String = ""
init {
sideEffects += outerProp + "#" + parentProp + "first"
}
protected constructor(x: String) {
parentProp = x + "#${outerProp}"
sideEffects += "#second#"
}
init {
sideEffects += parentProp + "#third"
}
protected constructor(x: Int): this(x.toString() + "#" + outerProp) {
parentProp += "#int"
sideEffects += "fourth#"
}
}
inner class A2 : A1 {
var prop: String = ""
init {
sideEffects += outerProp + "#" + prop + "fifth"
}
constructor(x: String): super(x + "#" + outerProp) {
prop = x + "#$outerProp"
sideEffects += "#sixth"
}
init {
sideEffects += prop + "#seventh"
}
constructor(x: Int): super(x + 1) {
prop += "$x#$outerProp#int"
sideEffects += "#eighth"
}
}
}
fun box(): String {
val outer1 = Outer("propValue1")
val a1 = outer1.A2("abc")
if (a1.parentProp != "abc#propValue1#propValue1") return "fail1: ${a1.parentProp}"
if (a1.prop != "abc#propValue1") return "fail2: ${a1.prop}"
if (outer1.sideEffects != "propValue1#first#third#second#propValue1#fifth#seventh#sixth") return "fail1-sideEffects: ${outer1.sideEffects}"
val outer2 = Outer("propValue2")
val a2 = outer2.A2(123)
if (a2.parentProp != "124#propValue2#propValue2#int") return "fail3: ${a2.parentProp}"
if (a2.prop != "123#propValue2#int") return "fail4: ${a2.prop}"
if (outer2.sideEffects != "propValue2#first#third#second#fourth#propValue2#fifth#seventh#eighth") return "fail2-sideEffects: ${outer2.sideEffects}"
return "OK"
}
@@ -0,0 +1,77 @@
open class C(val grandParentProp: String)
fun box(): String {
var sideEffects: String = ""
var parentSideEffects: String = ""
val justForUsageInClosure = 7
val justForUsageInParentClosure = "parentCaptured"
abstract class B : C {
val parentProp: String
init {
sideEffects += "minus-one#"
parentSideEffects += "1"
}
protected constructor(arg: Int): super(justForUsageInParentClosure) {
parentProp = (arg).toString()
sideEffects += "0.5#"
parentSideEffects += "#" + justForUsageInParentClosure
}
protected constructor(arg1: Int, arg2: Int): super(justForUsageInParentClosure) {
parentProp = (arg1 + arg2).toString()
sideEffects += "0.7#"
parentSideEffects += "#3"
}
init {
sideEffects += "zero#"
parentSideEffects += "#4"
}
}
class A : B {
var prop: String = ""
init {
sideEffects += prop + "first"
}
constructor(x1: Int, x2: Int): super(x1, x2) {
prop = x1.toString()
sideEffects += "#third"
}
init {
sideEffects += prop + "#second"
}
constructor(x: Int): super(justForUsageInClosure + x) {
prop += "${x}#int"
sideEffects += "#fourth"
}
constructor(): this(justForUsageInClosure) {
sideEffects += "#fifth"
}
override fun toString() = "$prop#$parentProp#$grandParentProp"
}
val a1 = A(5, 10).toString()
if (a1 != "5#15#parentCaptured") return "fail1: $a1"
if (sideEffects != "minus-one#zero#0.7#first#second#third") return "fail2: ${sideEffects}"
if (parentSideEffects != "1#4#3") return "fail3: ${parentSideEffects}"
sideEffects = ""
parentSideEffects = ""
val a2 = A(123).toString()
if (a2 != "123#int#130#parentCaptured") return "fail1: $a2"
if (sideEffects != "minus-one#zero#0.5#first#second#fourth") return "fail4: ${sideEffects}"
if (parentSideEffects != "1#4#parentCaptured") return "fail5: ${parentSideEffects}"
sideEffects = ""
parentSideEffects = ""
val a3 = A().toString()
if (a3 != "7#int#14#parentCaptured") return "fail6: $a3"
if (sideEffects != "minus-one#zero#0.5#first#second#fourth#fifth") return "fail7: ${sideEffects}"
if (parentSideEffects != "1#4#parentCaptured") return "fail8: ${parentSideEffects}"
return "OK"
}
@@ -0,0 +1,53 @@
var sideEffects: String = ""
abstract class B protected(val arg: Int) {
val parentProp: String
init {
sideEffects += "zero#"
parentProp = arg.toString()
}
}
class A(x: Boolean) : B(if (x) 1 else 2) {
var prop: String = ""
init {
sideEffects += prop + "first"
}
constructor(x: String): this(x == "abc") {
prop = x
sideEffects += "#third"
}
init {
sideEffects += prop + "#second"
}
constructor(x: Int): this(x < 0) {
prop += "${x}#int"
sideEffects += "#fourth"
}
}
fun box(): String {
val a1 = A("abc")
if (a1.prop != "abc") return "fail0: ${a1.prop}"
if (a1.parentProp != "1") return "fail1: ${a1.parentProp}"
if (a1.arg != 1) return "fail1': ${a1.arg}"
if (sideEffects != "zero#first#second#third") return "fail2: ${sideEffects}"
sideEffects = ""
val a2 = A(123)
if (a2.prop != "123#int") return "fail3: ${a2.prop}"
if (a2.parentProp != "2") return "fail4: ${a2.parentProp}"
if (a2.arg != 2) return "fail5': ${a2.arg}"
if (sideEffects != "zero#first#second#fourth") return "fail6: ${sideEffects}"
sideEffects = ""
val a3 = A(false)
if (a3.prop != "") return "fail7: ${a3.prop}"
if (a3.parentProp != "2") return "fail8: ${a3.parentProp}"
if (a3.arg != 2) return "fail9': ${a3.arg}"
if (sideEffects != "zero#first#second") return "fail10: ${sideEffects}"
return "OK"
}
@@ -0,0 +1,64 @@
var sideEffects: String = ""
abstract class B {
val parentProp: String
init {
sideEffects += "minus-one#"
}
protected constructor(arg: Int) {
parentProp = (arg).toString()
sideEffects += "0.5#"
}
protected constructor(arg1: Int, arg2: Int) {
parentProp = (arg1 + arg2).toString()
sideEffects += "0.7#"
}
init {
sideEffects += "zero#"
}
}
class A : B {
var prop: String = ""
init {
sideEffects += prop + "first"
}
constructor(x1: Int, x2: Int): super(x1, x2) {
prop = x1.toString()
sideEffects += "#third"
}
init {
sideEffects += prop + "#second"
}
constructor(x: Int): super(3 + x) {
prop += "${x}#int"
sideEffects += "#fourth"
}
constructor(): this(7) {
sideEffects += "#fifth"
}
}
fun box(): String {
val a1 = A(5, 10)
if (a1.prop != "5") return "fail0: ${a1.prop}"
if (a1.parentProp != "15") return "fail1: ${a1.parentProp}"
if (sideEffects != "minus-one#zero#0.7#first#second#third") return "fail2: ${sideEffects}"
sideEffects = ""
val a2 = A(123)
if (a2.prop != "123#int") return "fail3: ${a2.prop}"
if (a2.parentProp != "126") return "fail4: ${a2.parentProp}"
if (sideEffects != "minus-one#zero#0.5#first#second#fourth") return "fail5: ${sideEffects}"
sideEffects = ""
val a3 = A()
if (a3.prop != "7#int") return "fail6: ${a3.prop}"
if (a3.parentProp != "10") return "fail7: ${a3.parentProp}"
if (sideEffects != "minus-one#zero#0.5#first#second#fourth#fifth") return "fail8: ${sideEffects}"
return "OK"
}
@@ -0,0 +1,31 @@
fun join(x: Array<out String>): String {
var result = ""
for (i in x) {
result += i
result += "#"
}
return result
}
open class B {
val parentProp: String
constructor(vararg x: String) {
parentProp = join(x)
}
}
class A : B {
val prop: String
constructor(vararg x: String): super("0", *x, "4") {
prop = join(x)
}
}
fun box(): String {
val a1 = A("1", "2", "3")
if (a1.prop != "1#2#3#") return "fail1: ${a1.prop}"
if (a1.parentProp != "0#1#2#3#4#") return "fail2: ${a1.parentProp}"
return "OK"
}
@@ -0,0 +1,21 @@
inline fun run(block: () -> Unit) = block()
class A {
val prop: Int
constructor(arg: Boolean) {
if (arg) {
prop = 1
run { return }
throw RuntimeException("fail 0")
}
prop = 2
}
}
fun box(): String {
val a1 = A(true)
if (a1.prop != 1) return "fail1: ${a1.prop}"
val a2 = A(false)
if (a2.prop != 2) return "fail2: ${a2.prop}"
return "OK"
}
@@ -0,0 +1,18 @@
class A {
val prop: Int
constructor(arg: Boolean) {
if (arg) {
prop = 1
return
}
prop = 2
}
}
fun box(): String {
val a1 = A(true)
if (a1.prop != 1) return "fail1: ${a1.prop}"
val a2 = A(false)
if (a2.prop != 2) return "fail2: ${a2.prop}"
return "OK"
}
@@ -0,0 +1,18 @@
class A {
val prop: Int
constructor(arg: Boolean) {
if (arg) {
prop = 1
return Unit
}
prop = 2
}
}
fun box(): String {
val a1 = A(true)
if (a1.prop != 1) return "fail1: ${a1.prop}"
val a2 = A(false)
if (a2.prop != 2) return "fail2: ${a2.prop}"
return "OK"
}