Skip to content

Commit

Permalink
[Kotlin] Infer val in method bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
sschr15 committed Aug 15, 2024
1 parent 78728ec commit a0b6136
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent;
import org.jetbrains.java.decompiler.modules.decompiler.exps.VarExprent;
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarProcessor;
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarTypeProcessor;
import org.jetbrains.java.decompiler.struct.gen.VarType;
import org.jetbrains.java.decompiler.util.TextBuffer;
import org.vineflower.kotlin.KotlinWriter;
Expand Down Expand Up @@ -47,8 +48,12 @@ public TextBuffer toJava(int indent) {

boolean definition = isDefinition();
if (definition && !isExceptionType) {
// TODO: inference of var/val
buffer.append("var ");
VarProcessor processor = getProcessor();

boolean isFinal = isEffectivelyFinal() ||
(processor != null && processor.getVarFinal(getVarVersionPair()) != VarTypeProcessor.FinalType.NON_FINAL);

buffer.append(isFinal ? "val " : "var ");
}

buffer.append(getName());
Expand Down
4 changes: 2 additions & 2 deletions plugins/kotlin/testData/results/pkg/TestClassDec.dec
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class TestClassDec {

public fun test() {
new TestClassDec.EmptyDec();
var vec: TestClassDec.Vec2iVal = new TestClassDec.Vec2iVal(1, 2);// 16
var vec1: TestClassDec.Vec2iVal = new TestClassDec.Vec2iVal(2, 4);// 17
val vec: TestClassDec.Vec2iVal = new TestClassDec.Vec2iVal(1, 2);// 16
val vec1: TestClassDec.Vec2iVal = new TestClassDec.Vec2iVal(2, 4);// 17
System.out.println(vec.getX());// 19
System.out.println(this.dot(vec, vec1));// 20
}// 21
Expand Down
6 changes: 3 additions & 3 deletions plugins/kotlin/testData/results/pkg/TestContracts.dec
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public class TestContracts {
callsInPlace(f)
}

var var3: java.lang.Iterable = (new IntRange(0, i)) as java.lang.Iterable;
val var3: java.lang.Iterable = (new IntRange(0, i)) as java.lang.Iterable;
var var4: Int = 0;
var var5: java.util.Iterator = var3.iterator();
val var5: java.util.Iterator = var3.iterator();

while (var5.hasNext()) {
var var6: Int = (var5 as IntIterator).nextInt();
val var6: Int = (var5 as IntIterator).nextInt();
var4 += (f.invoke() as java.lang.Number).intValue();
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/kotlin/testData/results/pkg/TestDataClass.dec
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public data class TestDataClass(dataClassVal: Regex, variableWithVeryLongName: I
} else if (other !is TestDataClass) {
return false;
} else {
var var2: TestDataClass = other as TestDataClass;
val var2: TestDataClass = other as TestDataClass;
if (!(this.dataClassVal == (other as TestDataClass).dataClassVal)) {
return false;
} else if (this.variableWithVeryLongName != var2.variableWithVeryLongName) {
Expand Down
8 changes: 4 additions & 4 deletions plugins/kotlin/testData/results/pkg/TestDestructors.dec
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class TestDestructors {

public fun destructDataClassesSpecial(x: Pair<Int, String>, y: Triple<List<Int>, Nothing?, Unit>) {
System.out.println("${(x.component1() as java.lang.Number).intValue()} ${x.component2() as java.lang.String}");// 19 20
var c: java.util.List = y.component1() as java.util.List;// 22
var d: Void = y.component2() as Void;
val c: java.util.List = y.component1() as java.util.List;// 22
val d: Void = y.component2() as Void;
y.component3();
System.out.println("$c $d ${Unit.INSTANCE}");// 23
}// 24
Expand All @@ -24,7 +24,7 @@ public class TestDestructors {
}// 35

public fun destructorImpossible(x: Pair<String, Nothing>): String {
var a: java.lang.String = x.component1() as java.lang.String;// 38
val a: java.lang.String = x.component1() as java.lang.String;// 38
x.component2();
throw new KotlinNothingValueException();
}
Expand All @@ -43,7 +43,7 @@ public class TestDestructors {
}// 51

public fun destructLambdaInline(x: Int) {
var var2: Function0 = TestDestructors::destructLambdaInline$lambda$0;
val var2: Function0 = TestDestructors::destructLambdaInline$lambda$0;
System.out
.println(
"${this.component1((var2.invoke() as java.lang.Number).intValue())}${this.component2((var2.invoke() as java.lang.Number).intValue())}${this.component3(// 55 75 76 77
Expand Down
26 changes: 13 additions & 13 deletions plugins/kotlin/testData/results/pkg/TestForRange.dec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TestForRange {

public fun testIntStep() {
var i: Int = 1;
var var2: Int = ProgressionUtilKt.getProgressionLastElement(1, 10, 2);
val var2: Int = ProgressionUtilKt.getProgressionLastElement(1, 10, 2);
if (1 <= var2) {
while (true) {
System.out.println(i);// 18
Expand All @@ -35,7 +35,7 @@ public class TestForRange {
throw new IllegalArgumentException("Step must be positive, was: $x.");
} else {
var i: Int = 1;
var var3: Int = ProgressionUtilKt.getProgressionLastElement(1, 100, x);
val var3: Int = ProgressionUtilKt.getProgressionLastElement(1, 100, x);
if (1 <= var3) {
while (true) {
System.out.println(i);// 24
Expand All @@ -57,7 +57,7 @@ public class TestForRange {

public fun testIntDownToStep() {
var i: Int = 10;
var var2: Int = ProgressionUtilKt.getProgressionLastElement(10, 1, -2);
val var2: Int = ProgressionUtilKt.getProgressionLastElement(10, 1, -2);
if (var2 <= 10) {
while (true) {
System.out.println(i);// 36
Expand All @@ -74,9 +74,9 @@ public class TestForRange {
if (x <= 0) {
throw new IllegalArgumentException("Step must be positive, was: $x.");
} else {
var var2: Int = -x;
val var2: Int = -x;
var i: Int = 100;
var var4: Int = ProgressionUtilKt.getProgressionLastElement(100, 1, var2);
val var4: Int = ProgressionUtilKt.getProgressionLastElement(100, 1, var2);
if (var4 <= 100) {
while (true) {
System.out.println(i);// 42
Expand All @@ -97,10 +97,10 @@ public class TestForRange {
}// 50

public fun testUntilStep() {
var var1: IntProgression = RangesKt.step(RangesKt.until(1, 100) as IntProgression, 2);
val var1: IntProgression = RangesKt.step(RangesKt.until(1, 100) as IntProgression, 2);
var i: Int = var1.getFirst();
var var3: Int = var1.getLast();
var var4: Int = var1.getStep();
val var3: Int = var1.getLast();
val var4: Int = var1.getStep();
if (var4 > 0 && i <= var3 || var4 < 0 && var3 <= i) {
while (true) {
System.out.println(i);// 54
Expand All @@ -114,10 +114,10 @@ public class TestForRange {
}// 56

public fun testUntilStepX(x: Int) {
var var2: IntProgression = RangesKt.step(RangesKt.until(1, 100) as IntProgression, x);
val var2: IntProgression = RangesKt.step(RangesKt.until(1, 100) as IntProgression, x);
var i: Int = var2.getFirst();
var var4: Int = var2.getLast();
var var5: Int = var2.getStep();
val var4: Int = var2.getLast();
val var5: Int = var2.getStep();
if (var5 > 0 && i <= var4 || var5 < 0 && var4 <= i) {
while (true) {
System.out.println(i);// 60
Expand Down Expand Up @@ -146,7 +146,7 @@ public class TestForRange {

public fun testIntYStep(x: Int, y: Int) {
var i: Int = x;
var var4: Int = ProgressionUtilKt.getProgressionLastElement(x, y, 2);
val var4: Int = ProgressionUtilKt.getProgressionLastElement(x, y, 2);
if (x <= var4) {
while (true) {
System.out.println(i);// 72
Expand All @@ -164,7 +164,7 @@ public class TestForRange {
throw new IllegalArgumentException("Step must be positive, was: $z.");
} else {
var i: Int = x;
var var5: Int = ProgressionUtilKt.getProgressionLastElement(x, y, z);
val var5: Int = ProgressionUtilKt.getProgressionLastElement(x, y, z);
if (x <= var5) {
while (true) {
System.out.println(i);// 78
Expand Down
18 changes: 9 additions & 9 deletions plugins/kotlin/testData/results/pkg/TestNonInlineLambda.dec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public open class TestNonInlineLambda {
}// 18

public fun testCaptureIntIterationValue(x: Iterable<Int>) {
var var2: java.util.Iterator = x.iterator();// 21
val var2: java.util.Iterator = x.iterator();// 21

while (var2.hasNext()) {
this.execute(TestNonInlineLambda::testCaptureIntIterationValue$lambda$2);// 22
Expand All @@ -35,10 +35,10 @@ public open class TestNonInlineLambda {
}// 34

public fun testCaptureMutableInt(x: Int) {
var y: IntRef = new IntRef();// 37
val y: IntRef = new IntRef();// 37
y.element = x;
this.execute(TestNonInlineLambda::testCaptureMutableInt$lambda$4);// 38
var var3: Int = y.element++;// 41
val var3: Int = y.element++;// 41
this.execute(TestNonInlineLambda::testCaptureMutableInt$lambda$5);// 42
y.element *= 500;// 45
this.execute(TestNonInlineLambda::testCaptureMutableInt$lambda$6);// 46
Expand All @@ -49,7 +49,7 @@ public open class TestNonInlineLambda {
}// 57

public fun testCaptureMutableObject(x: String) {
var y: ObjectRef = new ObjectRef();// 60
val y: ObjectRef = new ObjectRef();// 60
y.element = x;
this.execute(TestNonInlineLambda::testCaptureMutableObject$lambda$9);// 61
y.element = "${y.element}!!";// 64
Expand All @@ -63,14 +63,14 @@ public open class TestNonInlineLambda {
}// 80

public fun testCaptureAndMutateInt(x: Int) {
var y: IntRef = new IntRef();// 83
val y: IntRef = new IntRef();// 83
this.execute(TestNonInlineLambda::testCaptureAndMutateInt$lambda$14);// 84
y.element = 5 + x;// 89
this.execute(TestNonInlineLambda::testCaptureAndMutateInt$lambda$15);// 90
}// 95

public fun testCaptureAndMutateString(x: String) {
var y: ObjectRef = new ObjectRef();// 98
val y: ObjectRef = new ObjectRef();// 98
y.element = "";
this.execute(TestNonInlineLambda::testCaptureAndMutateString$lambda$16);// 99
y.element = "Hello: $x";// 105
Expand Down Expand Up @@ -192,7 +192,7 @@ public open class TestNonInlineLambda {
@JvmStatic
fun `testCaptureAndMutateInt$lambda$15`(`$y`: IntRef): Unit {
while ($y.element > 0) {// 91
var var1: Int = `$y`.element;// 92
val var1: Int = `$y`.element;// 92
`$y`.element += -1;
System.out.println(var1);
}
Expand Down Expand Up @@ -222,7 +222,7 @@ public open class TestNonInlineLambda {

@JvmStatic
fun `testCapturePublicMutableIntField$lambda$18`(`this$0`: TestNonInlineLambda): Unit {
var var1: Int = `this$0`.intField++;// 117
val var1: Int = `this$0`.intField++;// 117
return Unit.INSTANCE;
}

Expand All @@ -234,7 +234,7 @@ public open class TestNonInlineLambda {

@JvmStatic
fun `testCapturePrivateMutableIntField$lambda$20`(`this$0`: TestNonInlineLambda): Unit {
var var1: Int = `this$0`.privateIntField++;// 129
val var1: Int = `this$0`.privateIntField++;// 129
return Unit.INSTANCE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class TestNullableOperator {

public fun test6(x: Int?): Int {
if (x != null) {// 33
var y: Int = x;
val y: Int = x;
System.out.println(y);// 35
return y;// 37
} else {
Expand Down
2 changes: 1 addition & 1 deletion plugins/kotlin/testData/results/pkg/TestObject.dec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public object TestObject private constructor() {

@JvmStatic
public fun objectJvmStaticFun() {
var var0: Int = objectVar++;// 16
val var0: Int = objectVar++;// 16
}// 17
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/kotlin/testData/results/pkg/TestReflection.dec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TestReflection {
}// 20

public fun testFunctionReference() {
var f: KFunction = <unknownclass>.INSTANCE as KFunction;// 23
val f: KFunction = <unknownclass>.INSTANCE as KFunction;// 23
System.out.println(<unknownclass>.INSTANCE as KFunction);// 24
(f as Function1).invoke(new TestReflection());// 25
}// 26
Expand Down
12 changes: 6 additions & 6 deletions plugins/kotlin/testData/results/pkg/TestSafeCasts.dec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package pkg

public class TestSafeCasts {
public fun test(obj: Any): Boolean {
var t: Int = obj as? Integer;// 5
val t: Int = obj as? Integer;// 5
if ((obj as? Integer) != null) {// 7
if (t == 1) {
return true;
Expand All @@ -16,7 +16,7 @@ public class TestSafeCasts {
if (obj !is Integer) {// 11
return null;// 12
} else {
var t: Int = obj as? Integer;// 15
val t: Int = obj as? Integer;// 15
if ((obj as? Integer) != null) {// 17
if (t == 1) {
return true;
Expand All @@ -36,7 +36,7 @@ public class TestSafeCasts {
}

public fun testCastNonNullToNullable(obj: Any): Boolean {
var t: Int = obj as? Integer;// 37
val t: Int = obj as? Integer;// 37
if ((obj as? Integer) != null) {// 39
if (t == 1) {
return true;
Expand All @@ -50,7 +50,7 @@ public class TestSafeCasts {
if (obj !is Integer) {// 43
return null;// 44
} else {
var t: Int = obj as? Integer;// 47
val t: Int = obj as? Integer;// 47
if ((obj as? Integer) != null) {// 49
if (t == 1) {
return true;
Expand All @@ -62,7 +62,7 @@ public class TestSafeCasts {
}

public fun testCastNullableToNullable(obj: Any?): Boolean {
var t: Int = obj as? Integer;// 53
val t: Int = obj as? Integer;// 53
if ((obj as? Integer) != null) {// 55
if (t == 1) {
return true;
Expand All @@ -76,7 +76,7 @@ public class TestSafeCasts {
if (obj != null && obj !is Integer) {// 59
return null;// 60
} else {
var t: Int = obj as? Integer;// 63
val t: Int = obj as? Integer;// 63
if ((obj as? Integer) != null) {// 65
if (t == 1) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion plugins/kotlin/testData/results/pkg/TestShadowParam.dec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package pkg

public class TestShadowParam {
public fun test(x: Int) {
var xx: Int = x - 1;// 5 6
val xx: Int = x - 1;// 5 6
System.out.println(x - 1);// 7
if (xx < 0) {// 8
System.out.println(xx);// 9
Expand Down
16 changes: 8 additions & 8 deletions plugins/kotlin/testData/results/pkg/TestTailrecFunctions.dec
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public class TestTailrecFunctions {
var var5: TestTailrecFunctions = this;// 4

while (x != 0L) {// 5
var var7: Long = x - 1L;
var var9: Long = sum + x;
val var7: Long = x - 1L;
val var9: Long = sum + x;
var5 = var5;// 6
x = var7;
sum = var9;
Expand Down Expand Up @@ -71,15 +71,15 @@ public class TestTailrecFunctions {

while (n != 0L) {// 44
if (n % 2 == 0L) {// 45
var var9: Long = x * x;
var var11: Long = n / 2;
val var9: Long = x * x;
val var11: Long = n / 2;
var7 = var7;
x = var9;
n = var11;
acc = acc;
} else {
var var15: Long = n - 1L;
var var13: Long = acc * x;
val var15: Long = n - 1L;
val var13: Long = acc * x;
var7 = var7;// 46
x = x;
n = var15;
Expand Down Expand Up @@ -115,8 +115,8 @@ public class TestTailrecFunctions {
break;
}

var var7: Long = x * x;
var var9: Long = n / 2;
val var7: Long = x * x;
val var9: Long = n / 2;
var5 = var5;
x = var7;
n = var9;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class TestTryCatchExpressions {
var4 = var10001;
}

var var7: java.lang.String = var4;// 24
val var7: java.lang.String = var4;// 24
var6 = var10000;

var var13: java.lang.String;
Expand Down Expand Up @@ -89,7 +89,7 @@ public class TestTryCatchExpressions {
var4 = var15;// 51
}

var var8: java.lang.String = var4;// 52
val var8: java.lang.String = var4;// 52
var7 = var10000;

var var10001: java.lang.String;
Expand Down
Loading

0 comments on commit a0b6136

Please sign in to comment.