[IR] Support default arguments

Signed-off-by: Evgeniy.Zhelenskiy <Evgeniy.Zhelenskiy@jetbrains.com>

#KT-1179
This commit is contained in:
Evgeniy.Zhelenskiy
2022-10-11 16:57:22 +02:00
committed by Space Team
parent 0c70b60988
commit 51e76aa19a
7 changed files with 305 additions and 41 deletions
@@ -0,0 +1,91 @@
// CHECK_BYTECODE_LISTING
// WITH_STDLIB
// TARGET_BACKEND: JVM_IR
// WORKS_WHEN_VALUE_CLASS
// LANGUAGE: +ValueClasses
@JvmInline
value class DPoint(/*inline */val x: Double/* = 1.0*/, /*inline */val y: Double/* = 2.0*/) {
fun f1(a: Int, b: Int = -1, c: DPoint = DPoint(-2.0, -3.0)) = listOf(this, x, y, a, b, c)
}
@JvmInline
value class DSegment(/*inline */val p1: DPoint/* = DPoint(3.0, 4.0)*/, /*inline */val p2: DPoint/* = DPoint(5.0, 6.0)*/, /*inline */val n: Int/* = 7*/) {
fun f2(a: Int, b: Int = -1, c: DPoint = DPoint(-2.0, -3.0)) = listOf(this, p1, p2, n, a, b, c)
}
data class Wrapper(val segment: DSegment = DSegment(DPoint(8.0, 9.0), DPoint(10.0, 11.0), 7), val n: Int = 12) {
fun f3(a: Int, b: Int = -1, c: DPoint = DPoint(-2.0, -3.0)) = listOf(this, segment, n, a, b, c)
}
fun complexFun(a1: Double, a2: DPoint, a3: Double = a1 * a2.x * a2.y, a4: DPoint = DPoint(a2.x * a1 * a3, a2.y * a1 * a3)) = "$a1, $a2, $a3, $a4"
inline fun complexInlineFun(a1: Double, a2: DPoint, a3: Double = a1 * a2.x * a2.y, a4: DPoint = DPoint(a2.x * a1 * a3, a2.y * a1 * a3)) = "$a1, $a2, $a3, $a4"
fun box(): String {
// comments bellow are because MFVC primary constructors default parameters require support of inline arguments in regular functions
// require(DPoint() == DPoint(1.0, 2.0)) { "${DPoint()} ${DPoint(1.0, 2.0)}" }
// require(DPoint(3.0) == DPoint(3.0, 2.0)) { "${DPoint()} ${DPoint(3.0, 2.0)}" }
// require(DPoint(x = 3.0) == DPoint(3.0, 2.0)) { "${DPoint()} ${DPoint(3.0, 2.0)}" }
// require(DPoint(y = 3.0) == DPoint(1.0, 3.0)) { "${DPoint()} ${DPoint(1.0, 3.0)}" }
// val defaultDPoint = DPoint()
val defaultDPoint = DPoint(1.0, 2.0)
require(defaultDPoint.f1(4) == listOf(DPoint(1.0, 2.0), 1.0, 2.0, 4, -1, DPoint(-2.0, -3.0))) {
defaultDPoint.f1(4).toString()
}
require(defaultDPoint.f1(4, 1, DPoint(2.0, 3.0)) == listOf(DPoint(1.0, 2.0), 1.0, 2.0, 4, 1, DPoint(2.0, 3.0))) {
defaultDPoint.f1(4, 1, DPoint(2.0, 3.0)).toString()
}
require(DPoint(-1.0, -2.0).f1(4) == listOf(DPoint(-1.0, -2.0), -1.0, -2.0, 4, -1, DPoint(-2.0, -3.0))) {
defaultDPoint.f1(4).toString()
}
require(DPoint(-1.0, -2.0).f1(4, 1, DPoint(2.0, 3.0)) == listOf(DPoint(-1.0, -2.0), -1.0, -2.0, 4, 1, DPoint(2.0, 3.0))) {
defaultDPoint.f1(4, 1, DPoint(2.0, 3.0)).toString()
}
// require(DSegment() == DSegment(DPoint(3.0, 4.0), DPoint(5.0, 6.0), 7)) { DSegment().toString() }
// val defaultDSegment = DSegment()
val defaultDSegment = DSegment(DPoint(3.0, 4.0), DPoint(5.0, 6.0), 7)
require(defaultDSegment.f2(100) == listOf(defaultDSegment, DPoint(3.0, 4.0), DPoint(5.0, 6.0), 7, 100, -1, DPoint(-2.0, -3.0))) {
defaultDSegment.f2(100).toString()
}
require(defaultDSegment.f2(100, b = 1) == listOf(defaultDSegment, DPoint(3.0, 4.0), DPoint(5.0, 6.0), 7, 100, 1, DPoint(-2.0, -3.0))) {
defaultDSegment.f2(100, b = 1).toString()
}
require(Wrapper() == Wrapper(DSegment(DPoint(8.0, 9.0), DPoint(10.0, 11.0), 7), 12)) { Wrapper().toString() }
require(Wrapper().f3(100) == listOf(Wrapper(), DSegment(DPoint(8.0, 9.0), DPoint(10.0, 11.0), 7), 12, 100, -1, DPoint(-2.0, -3.0))) {
Wrapper().f3(100).toString()
}
require(Wrapper().f3(100, b = 1) == listOf(Wrapper(), DSegment(DPoint(8.0, 9.0), DPoint(10.0, 11.0), 7), 12, 100, 1, DPoint(-2.0, -3.0))) {
Wrapper().f3(100, b = 1).toString()
}
require(complexFun(2.0, DPoint(3.0, 5.0)) == "2.0, ${DPoint(3.0, 5.0)}, 30.0, ${DPoint(180.0, 300.0)}") {
complexFun(2.0, DPoint(3.0, 5.0))
}
require(complexFun(2.0, DPoint(3.0, 5.0), 7.0) == "2.0, ${DPoint(3.0, 5.0)}, 7.0, ${DPoint(42.0, 70.0)}") {
complexFun(2.0, DPoint(3.0, 5.0), 7.0)
}
require(complexFun(2.0, DPoint(3.0, 5.0), a4 = DPoint(11.0, 13.0)) == "2.0, ${DPoint(3.0, 5.0)}, 30.0, ${DPoint(11.0, 13.0)}") {
complexFun(2.0, DPoint(3.0, 5.0), a4 = DPoint(11.0, 13.0))
}
require(complexFun(2.0, DPoint(3.0, 5.0), 7.0, DPoint(11.0, 13.0)) == "2.0, ${DPoint(3.0, 5.0)}, 7.0, ${DPoint(11.0, 13.0)}") {
complexFun(2.0, DPoint(3.0, 5.0), 7.0, DPoint(11.0, 13.0))
}
require(complexInlineFun(2.0, DPoint(3.0, 5.0)) == "2.0, ${DPoint(3.0, 5.0)}, 30.0, ${DPoint(180.0, 300.0)}") {
complexInlineFun(2.0, DPoint(3.0, 5.0))
}
require(complexInlineFun(2.0, DPoint(3.0, 5.0), 7.0) == "2.0, ${DPoint(3.0, 5.0)}, 7.0, ${DPoint(42.0, 70.0)}") {
complexInlineFun(2.0, DPoint(3.0, 5.0), 7.0)
}
require(complexInlineFun(2.0, DPoint(3.0, 5.0), a4 = DPoint(11.0, 13.0)) == "2.0, ${DPoint(3.0, 5.0)}, 30.0, ${DPoint(11.0, 13.0)}") {
complexInlineFun(2.0, DPoint(3.0, 5.0), a4 = DPoint(11.0, 13.0))
}
require(complexInlineFun(2.0, DPoint(3.0, 5.0), 7.0, DPoint(11.0, 13.0)) == "2.0, ${DPoint(3.0, 5.0)}, 7.0, ${DPoint(11.0, 13.0)}") {
complexInlineFun(2.0, DPoint(3.0, 5.0), 7.0, DPoint(11.0, 13.0))
}
return "OK"
}
@@ -0,0 +1,99 @@
@kotlin.jvm.JvmInline
@kotlin.Metadata
public final class DPoint {
// source: 'defaultParameters.kt'
private final field x: double
private final field y: double
private synthetic method <init>(p0: double, p1: double): void
public synthetic final static method box-impl(p0: double, p1: double): DPoint
public final static method constructor-impl(p0: double, p1: double): void
public method equals(@org.jetbrains.annotations.Nullable p0: java.lang.Object): boolean
public static method equals-impl(p0: double, p1: double, p2: java.lang.Object): boolean
public final static method equals-impl0(p0: double, p1: double, p2: double, p3: double): boolean
public synthetic static method f1-lIoT8es$default(p0: double, p1: double, p2: int, p3: int, p4: double, p5: double, p6: int, p7: java.lang.Object): java.util.List
public final static @org.jetbrains.annotations.NotNull method f1-lIoT8es(p0: double, p1: double, p2: int, p3: int, p4: double, p5: double): java.util.List
public final method getX(): double
public final method getY(): double
public method hashCode(): int
public static method hashCode-impl(p0: double, p1: double): int
public @org.jetbrains.annotations.NotNull method toString(): java.lang.String
public static method toString-impl(p0: double, p1: double): java.lang.String
public synthetic final method unbox-impl-x(): double
public synthetic final method unbox-impl-y(): double
}
@kotlin.jvm.JvmInline
@kotlin.Metadata
public final class DSegment {
// source: 'defaultParameters.kt'
private final field n: int
private final field p1-x: double
private final field p1-y: double
private final field p2-x: double
private final field p2-y: double
private synthetic method <init>(p0: double, p1: double, p2: double, p3: double, p4: int): void
public synthetic final static method box-impl(p0: double, p1: double, p2: double, p3: double, p4: int): DSegment
public final static method constructor-impl(p0: double, p1: double, p2: double, p3: double, p4: int): void
public method equals(@org.jetbrains.annotations.Nullable p0: java.lang.Object): boolean
public static method equals-impl(p0: double, p1: double, p2: double, p3: double, p4: int, p5: java.lang.Object): boolean
public final static method equals-impl0(p0: double, p1: double, p2: double, p3: double, p4: int, p5: double, p6: double, p7: double, p8: double, p9: int): boolean
public synthetic static method f2-lIoT8es$default(p0: double, p1: double, p2: double, p3: double, p4: int, p5: int, p6: int, p7: double, p8: double, p9: int, p10: java.lang.Object): java.util.List
public final static @org.jetbrains.annotations.NotNull method f2-lIoT8es(p0: double, p1: double, p2: double, p3: double, p4: int, p5: int, p6: int, p7: double, p8: double): java.util.List
public final method getN(): int
public final @org.jetbrains.annotations.NotNull method getP1(): DPoint
public final @org.jetbrains.annotations.NotNull method getP2(): DPoint
public method hashCode(): int
public static method hashCode-impl(p0: double, p1: double, p2: double, p3: double, p4: int): int
public @org.jetbrains.annotations.NotNull method toString(): java.lang.String
public static method toString-impl(p0: double, p1: double, p2: double, p3: double, p4: int): java.lang.String
public synthetic final method unbox-impl-n(): int
public synthetic final method unbox-impl-p1(): DPoint
public synthetic final method unbox-impl-p1-x(): double
public synthetic final method unbox-impl-p1-y(): double
public synthetic final method unbox-impl-p2(): DPoint
public synthetic final method unbox-impl-p2-x(): double
public synthetic final method unbox-impl-p2-y(): double
}
@kotlin.Metadata
public final class DefaultParametersKt {
// source: 'defaultParameters.kt'
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
public synthetic static method complexFun-552ch2I$default(p0: double, p1: double, p2: double, p3: double, p4: double, p5: double, p6: int, p7: java.lang.Object): java.lang.String
public final static @org.jetbrains.annotations.NotNull method complexFun-552ch2I(p0: double, p1: double, p2: double, p3: double, p4: double, p5: double): java.lang.String
public synthetic static method complexInlineFun-552ch2I$default(p0: double, p1: double, p2: double, p3: double, p4: double, p5: double, p6: int, p7: java.lang.Object): java.lang.String
public final static @org.jetbrains.annotations.NotNull method complexInlineFun-552ch2I(p0: double, p1: double, p2: double, p3: double, p4: double, p5: double): java.lang.String
public final inner class kotlin/jvm/internal/Ref$DoubleRef
}
@kotlin.Metadata
public final class Wrapper {
// source: 'defaultParameters.kt'
private final field n: int
private final field segment-n: int
private final field segment-p1-x: double
private final field segment-p1-y: double
private final field segment-p2-x: double
private final field segment-p2-y: double
public method <init>(): void
public method <init>(p0: double, p1: double, p2: double, p3: double, p4: int, p5: int): void
public synthetic method <init>(p0: double, p1: double, p2: double, p3: double, p4: int, p5: int, p6: int, p7: kotlin.jvm.internal.DefaultConstructorMarker): void
public final @org.jetbrains.annotations.NotNull method component1(): DSegment
public final method component2(): int
public synthetic static method copy-GPBa7dw$default(p0: Wrapper, p1: double, p2: double, p3: double, p4: double, p5: int, p6: int, p7: int, p8: java.lang.Object): Wrapper
public final @org.jetbrains.annotations.NotNull method copy-GPBa7dw(p0: double, p1: double, p2: double, p3: double, p4: int, p5: int): Wrapper
public method equals(@org.jetbrains.annotations.Nullable p0: java.lang.Object): boolean
public synthetic static method f3-lIoT8es$default(p0: Wrapper, p1: int, p2: int, p3: double, p4: double, p5: int, p6: java.lang.Object): java.util.List
public final @org.jetbrains.annotations.NotNull method f3-lIoT8es(p0: int, p1: int, p2: double, p3: double): java.util.List
public final method getN(): int
public final @org.jetbrains.annotations.NotNull method getSegment(): DSegment
public synthetic final method getSegment-n(): int
public synthetic final method getSegment-p1(): DPoint
public synthetic final method getSegment-p1-x(): double
public synthetic final method getSegment-p1-y(): double
public synthetic final method getSegment-p2(): DPoint
public synthetic final method getSegment-p2-x(): double
public synthetic final method getSegment-p2-y(): double
public method hashCode(): int
public @org.jetbrains.annotations.NotNull method toString(): java.lang.String
}