Skip to content

Commit e139e17

Browse files
committed
Reconfigure Kotlin variable references for more versatility in the many ways they're used
1 parent 36dda49 commit e139e17

File tree

8 files changed

+33
-25
lines changed

8 files changed

+33
-25
lines changed

plugins/kotlin/src/main/java/org/vineflower/kotlin/expr/KVarExprent.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
import java.util.BitSet;
1313

1414
public class KVarExprent extends VarExprent implements KExprent {
15-
private boolean isExceptionType;
15+
public enum DeclarationType {
16+
DEFINITION,
17+
USAGE,
18+
FOR_LOOP_VARIABLE,
19+
EXCEPTION_TYPE,
20+
}
21+
22+
private DeclarationType declarationType;
1623

1724
public KVarExprent(int index, VarType varType, VarProcessor processor, BitSet bytecode) {
1825
super(index, varType, processor, bytecode);
@@ -27,6 +34,11 @@ public KVarExprent(VarExprent ex) {
2734
// this.setLVT(ex.getLVT());
2835
this.setEffectivelyFinal(ex.isEffectivelyFinal());
2936
this.setDefinition(ex.isDefinition());
37+
if (ex instanceof KVarExprent kVarExprent) {
38+
declarationType = kVarExprent.declarationType;
39+
} else {
40+
declarationType = ex.isDefinition() ? DeclarationType.DEFINITION : DeclarationType.USAGE;
41+
}
3042
}
3143

3244
@Override
@@ -46,8 +58,7 @@ public TextBuffer toJava(int indent) {
4658

4759
buffer.addBytecodeMapping(bytecode);
4860

49-
boolean definition = isDefinition();
50-
if (definition && !isExceptionType) {
61+
if (declarationType == DeclarationType.DEFINITION) {
5162
VarProcessor processor = getProcessor();
5263

5364
boolean isFinal = isEffectivelyFinal() ||
@@ -58,16 +69,16 @@ public TextBuffer toJava(int indent) {
5869

5970
buffer.append(getName());
6071

61-
if (definition || isExceptionType) {
72+
if (declarationType == DeclarationType.DEFINITION || declarationType == DeclarationType.EXCEPTION_TYPE) {
6273
buffer.append(": ");
6374
buffer.append(KTypes.getKotlinType(getDefinitionVarType()));
6475
}
6576

6677
return buffer;
6778
}
6879

69-
public void setExceptionType(boolean isExceptionType) {
70-
this.isExceptionType = isExceptionType;
80+
public void setDeclarationType(DeclarationType declarationType) {
81+
this.declarationType = declarationType;
7182
}
7283

7384
@Override

plugins/kotlin/src/main/java/org/vineflower/kotlin/pass/ReplaceExprentsPass.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private static boolean replace(Statement stat) {
3434
for (int i = 0; i < vars.size(); i++) {
3535
VarExprent expr = vars.get(i);
3636
KVarExprent map = new KVarExprent(expr);
37-
map.setExceptionType(true);
37+
map.setDeclarationType(KVarExprent.DeclarationType.EXCEPTION_TYPE);
3838
vars.set(i, map);
3939
}
4040

@@ -60,7 +60,7 @@ private static boolean replace(Statement stat) {
6060
for (int i = 0; i < vars.size(); i++) {
6161
VarExprent expr = vars.get(i);
6262
KVarExprent map = new KVarExprent(expr);
63-
map.setExceptionType(true);
63+
map.setDeclarationType(KVarExprent.DeclarationType.EXCEPTION_TYPE);
6464
vars.set(i, map);
6565
}
6666
}

plugins/kotlin/src/main/java/org/vineflower/kotlin/pass/ReplaceStatsPass.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import org.jetbrains.java.decompiler.api.plugin.pass.Pass;
44
import org.jetbrains.java.decompiler.api.plugin.pass.PassContext;
55
import org.jetbrains.java.decompiler.modules.decompiler.stats.*;
6-
import org.vineflower.kotlin.stat.KDoStatement;
7-
import org.vineflower.kotlin.stat.KIfStatement;
8-
import org.vineflower.kotlin.stat.KSequenceStatement;
9-
import org.vineflower.kotlin.stat.KSwitchStatement;
6+
import org.vineflower.kotlin.stat.*;
107

118
public class ReplaceStatsPass implements Pass {
129
@Override

plugins/kotlin/src/main/java/org/vineflower/kotlin/stat/KDoStatement.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public TextBuffer toJava(int indent) {
100100
}
101101
case FOR_EACH -> {
102102
KVarExprent init = new KVarExprent((VarExprent) getInitExprent());
103-
init.setExceptionType(true);
103+
init.setDeclarationType(KVarExprent.DeclarationType.FOR_LOOP_VARIABLE);
104104

105105
Exprent inc = KUtils.replaceExprent(getIncExprent());
106106
if (inc == null) {
@@ -131,7 +131,7 @@ public TextBuffer toJava(int indent) {
131131
) {
132132
// Turn for loop into range
133133
varExpr = new KVarExprent(varExpr);
134-
((KVarExprent) varExpr).setExceptionType(true);
134+
((KVarExprent) varExpr).setDeclarationType(KVarExprent.DeclarationType.FOR_LOOP_VARIABLE);
135135

136136
Exprent conditionExpr;
137137
if (condition.getLstOperands().get(1) instanceof ConstExprent c) {

plugins/kotlin/testData/results/pkg/TestForRange.dec

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import kotlin.internal.ProgressionUtilKt
44

55
public class TestForRange {
66
public fun testInt() {
7-
for (i: Int in 1..10) {// 5
7+
for (i in 1..10) {// 5
88
System.out.println(i);// 6
99
}
1010
}// 8
1111

1212
public fun testChar() {
13-
for (c: Char in 'a'..'z') {// 11
13+
for (c in 'a'..'z') {// 11
1414
System.out.println(c);// 12
1515
}
1616
}// 14
@@ -50,7 +50,7 @@ public class TestForRange {
5050
}// 26
5151

5252
public fun testIntDownTo() {
53-
for (i: Int in 10 downTo 1) {// 29
53+
for (i in 10 downTo 1) {// 29
5454
System.out.println(i);// 30
5555
}
5656
}// 32
@@ -91,7 +91,7 @@ public class TestForRange {
9191
}// 44
9292

9393
public fun testUntil() {
94-
for (i: Int in 1..9) {// 47
94+
for (i in 1..9) {// 47
9595
System.out.println(i);// 48
9696
}
9797
}// 50

plugins/kotlin/testData/results/pkg/TestFunVarargs.dec

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import java.util.Arrays
44

55
public class TestFunVarargs {
66
public fun printAll(vararg messages: String) {
7-
for (m: java.lang.String in messages) {// 5
7+
for (m in messages) {// 5
88
System.out.println(m);
99
}
1010
}// 6
1111

1212
public fun printAllArray(messages: Array<out String>) {
13-
for (m: java.lang.String in messages) {// 9
13+
for (m in messages) {// 9
1414
System.out.println(m);
1515
}
1616
}// 10

plugins/kotlin/testData/results/pkg/TestLabeledJumps.dec

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package pkg
22

33
public class TestLabeledJumps {
44
public fun testContinue(tester: (Int) -> Boolean) {
5-
label36@ for (i: Int in 1..100) {// 5
6-
for (j: Int in 1..100) {// 6
5+
label36@ for (i in 1..100) {// 5
6+
for (j in 1..100) {// 6
77
if (tester.invoke(j) as java.lang.Boolean) {// 7
88
continue@label36
99
}
@@ -16,8 +16,8 @@ public class TestLabeledJumps {
1616
}// 16
1717

1818
public fun testBreak(tester: (Int) -> Boolean) {
19-
label33@ for (i: Int in 1..100) {// 19
20-
for (j: Int in 1..100) {// 20
19+
label33@ for (i in 1..100) {// 19
20+
for (j in 1..100) {// 20
2121
if (tester.invoke(j) as java.lang.Boolean) {// 21
2222
break@label33
2323
}

plugins/kotlin/testData/results/pkg/TestNonInlineLambda.dec

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public open class TestNonInlineLambda {
3030
}// 26
3131

3232
public fun testCaptureObjectIterationValue(x: Iterable<String>) {
33-
for (i: java.lang.String in x) {// 29
33+
for (i in x) {// 29
3434
this.execute(TestNonInlineLambda::testCaptureObjectIterationValue$lambda$3);// 30
3535
}
3636
}// 34

0 commit comments

Comments
 (0)