[PSI2IR] Generate IR for functions and calls with context receivers

This commit is contained in:
Anastasiya Shadrina
2021-02-17 04:53:15 +07:00
committed by TeamCityServer
parent f4ddf66ac4
commit 307f318c9e
29 changed files with 378 additions and 98 deletions
@@ -0,0 +1,18 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
class View {
val coefficient = 42
}
context(View) val Int.dp get() = coefficient * this
fun box(): String {
with(View()) {
if (listOf(1, 2, 10).map { it.dp } == listOf(42, 84, 420)) {
return "OK"
}
return "fail"
}
}
@@ -0,0 +1,51 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
interface NumberOperations {
operator fun Number.plus(other: Number): Number
}
object DoubleOperations : NumberOperations {
override operator fun Number.plus(other: Number) = this.toDouble() + other.toDouble()
}
data class Matrix(val rows: Int, val columns: Int, val data: Array<out Number>) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Matrix
if (rows != other.rows) return false
if (columns != other.columns) return false
if (!data.contentEquals(other.data)) return false
return true
}
override fun hashCode(): Int {
var result = rows
result = 31 * result + columns
result = 31 * result + data.contentHashCode()
return result
}
}
fun matrixOf(rows: Int, columns: Int, vararg data: Number): Matrix {
assert(rows * columns == data.size) { "Wrong dimentions" }
return Matrix(rows, columns, data)
}
context(NumberOperations) operator fun Matrix.plus(other: Matrix): Matrix {
assert(rows == other.rows && columns == other.columns) { "Matrices should have the same dimentions" }
return matrixOf(rows, columns, *data.mapIndexed { i, element -> element + other.data[i] }.toTypedArray())
}
fun box(): String {
val m1 = matrixOf(2, 2, 1, 2, 3, 4)
val m2 = matrixOf(2, 2, .4, .3, .2, .1)
with(DoubleOperations) {
return if (m1 + m2 == matrixOf(2, 2, 1.4, 2.3, 3.2, 4.1)) "OK" else "fail"
}
}
@@ -0,0 +1,21 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
class A {
val o = "O"
}
class B {
val k = "K"
}
context(B) fun A.f(a: Any, b: Any) = o + k
fun B.g(a: A): String {
with (a) {
return f(1, "2")
}
}
fun box(): String {
return B().g(A())
}