Skip to content

Commit 2cf8978

Browse files
committed
Move switch simplifying to main loop
1 parent d5306ab commit 2cf8978

6 files changed

Lines changed: 164 additions & 179 deletions

File tree

src/org/jetbrains/java/decompiler/main/rels/MethodProcessor.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,14 @@ public static RootStatement codeToJava(StructClass cl, StructMethod mt, MethodDe
375375
decompileRecord.add("CondenseDo", root);
376376
continue;
377377
}
378-
378+
379+
if (root.hasSwitch() && SwitchHelper.simplifySwitches(root, mt, root)) {
380+
SequenceHelper.condenseSequences(root); // remove empty blocks
381+
decompileRecord.add("SimplifySwitches", root);
382+
383+
continue;
384+
}
385+
379386
// Apply main loop plugin passes
380387
if (pluginContext.runPasses(JavaPassLocation.MAIN_LOOP, pctx)) {
381388
root = pctx.getRoot();
@@ -401,26 +408,6 @@ public static RootStatement codeToJava(StructClass cl, StructMethod mt, MethodDe
401408
decompileRecord.resetMainLoop();
402409
decompileRecord.add("MainLoopEnd", root);
403410

404-
// this has to be done after all inlining is done so the case values do not get reverted
405-
if (root.hasSwitch() && SwitchHelper.simplifySwitches(root, mt, root)) {
406-
SequenceHelper.condenseSequences(root); // remove empty blocks
407-
decompileRecord.add("SimplifySwitches", root);
408-
409-
// If we have simplified switches, try to make switch expressions
410-
if (SwitchExpressionHelper.hasSwitchExpressions(root)) {
411-
if (SwitchExpressionHelper.processSwitchExpressions(root)) {
412-
decompileRecord.add("ProcessSwitchExpr_SS", root);
413-
414-
// Simplify stack vars to integrate and inline switch expressions
415-
StackVarsProcessor.simplifyStackVars(root, mt, cl);
416-
decompileRecord.add("SimplifyStackVars_SS", root);
417-
418-
varProc.setVarVersions(root);
419-
decompileRecord.add("SetVarVersions_SS", root);
420-
}
421-
}
422-
}
423-
424411
// Makes constant returns the same type as the method descriptor
425412
if (ExitHelper.adjustReturnType(root, md)) {
426413
decompileRecord.add("AdjustReturnType", root);

src/org/jetbrains/java/decompiler/modules/decompiler/SwitchHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,9 @@ private static boolean simplifySwitchOnEnumJ21(SwitchStatement switchSt, RootSta
685685
if (list.size() == 1 && list.get(0) == null) { // default by itself
686686
Statement st = switchSt.getCaseStatements().get(i);
687687
if (IfPatternMatchProcessor.isStatementMatchThrow(st)) {
688-
// Replace it with an empty block
688+
// Replace it with an empty block and remove it
689689
st.replaceWithEmpty();
690+
switchSt.removeCase(i);
690691
}
691692
}
692693
}

src/org/jetbrains/java/decompiler/modules/decompiler/exps/SwitchHeadExprent.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
package org.jetbrains.java.decompiler.modules.decompiler.exps;
55

6+
import org.jetbrains.java.decompiler.modules.decompiler.ValidationHelper;
67
import org.jetbrains.java.decompiler.modules.decompiler.vars.CheckTypesResult;
78
import org.jetbrains.java.decompiler.struct.gen.VarType;
89
import org.jetbrains.java.decompiler.util.InterpreterUtil;
@@ -46,10 +47,6 @@ public VarType getExprType() {
4647
public CheckTypesResult checkExprTypeBounds() {
4748
CheckTypesResult result = new CheckTypesResult();
4849

49-
// TODO: this surely can't be right with switch on enum and string?
50-
result.addExprLowerBound(value, VarType.VARTYPE_BYTECHAR);
51-
result.addExprUpperBound(value, VarType.VARTYPE_INT);
52-
5350
VarType valType = value.getExprType();
5451
for (List<Exprent> lst : caseValues) {
5552
for (Exprent expr : lst) {
@@ -60,7 +57,10 @@ public CheckTypesResult checkExprTypeBounds() {
6057
: expr.getExprType();
6158
if (!caseType.equals(valType)) {
6259
if (valType == null) {
63-
throw new IllegalStateException("Invalid switch case set: " + caseValues + " for selector of type " + value.getExprType());
60+
ValidationHelper.validateTrue(false, "Couldn't generate a valid merged switch set!");
61+
// TODO: should this add a bound of unknown to value?
62+
return result;
63+
// throw new IllegalStateException("Invalid switch case set: " + caseValues + " for selector of type " + value.getExprType());
6464
}
6565
// allow coercion of primitive -> boxes [see TestSwitchPatternMatching18]
6666
// e.g. `switch(o) { case 40 -> ...; case Integer i -> ...; }`

testData/results/TestEclipseSwitchString.dec

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ public class TestEclipseSwitchString {
77
return 1;// 10
88
case "2":
99
return 2;// 12
10+
default:
11+
System.out.println("Test");// 7
12+
return 0;// 15
1013
}
11-
12-
System.out.println("Test");// 7
13-
return 0;// 15
1414
}
1515

1616
public int test1(String var1) {
@@ -20,10 +20,10 @@ public class TestEclipseSwitchString {
2020
case "2":
2121
case "3":
2222
return 2;// 27
23+
default:
24+
System.out.println("Test");// 21
25+
return 0;// 30
2326
}
24-
25-
System.out.println("Test");// 21
26-
return 0;// 30
2727
}
2828

2929
public int test2(String var1) {
@@ -67,10 +67,10 @@ public class TestEclipseSwitchString {
6767
case "BB":
6868
case "Aa":
6969
return 2;// 68
70+
default:
71+
System.out.println("Test");// 64
72+
return 0;// 70
7073
}
71-
72-
System.out.println("Test");// 64
73-
return 0;// 70
7474
}
7575

7676
public int testHashcodeCollision2(String var1) {
@@ -128,20 +128,20 @@ class 'pkg/TestEclipseSwitchString' {
128128
22 5
129129
2d 7
130130
2e 7
131-
35 11
132-
36 11
133-
37 11
134-
38 11
135-
39 11
136-
3a 11
137-
3b 11
138-
3c 11
131+
35 10
132+
36 10
133+
37 10
134+
38 10
135+
39 10
136+
3a 10
137+
3b 10
138+
3c 10
139139
40 6
140140
41 6
141141
42 8
142142
43 8
143-
44 12
144-
45 12
143+
44 11
144+
45 11
145145
}
146146

147147
method 'test1 (Ljava/lang/String;)I' {
@@ -186,20 +186,20 @@ class 'pkg/TestEclipseSwitchString' {
186186
36 19
187187
41 20
188188
42 20
189-
49 24
190-
4a 24
191-
4b 24
192-
4c 24
193-
4d 24
194-
4e 24
195-
4f 24
196-
50 24
189+
49 23
190+
4a 23
191+
4b 23
192+
4c 23
193+
4d 23
194+
4e 23
195+
4f 23
196+
50 23
197197
54 18
198198
55 18
199199
56 21
200200
57 21
201-
58 25
202-
59 25
201+
58 24
202+
59 24
203203
}
204204

205205
method 'test2 (Ljava/lang/String;)I' {
@@ -355,18 +355,18 @@ class 'pkg/TestEclipseSwitchString' {
355355
1a 66
356356
22 67
357357
23 67
358-
2a 71
359-
2b 71
360-
2c 71
361-
2d 71
362-
2e 71
363-
2f 71
364-
30 71
365-
31 71
358+
2a 70
359+
2b 70
360+
2c 70
361+
2d 70
362+
2e 70
363+
2f 70
364+
30 70
365+
31 70
366366
35 68
367367
36 68
368-
37 72
369-
38 72
368+
37 71
369+
38 71
370370
}
371371

372372
method 'testHashcodeCollision2 (Ljava/lang/String;)I' {
@@ -436,15 +436,15 @@ class 'pkg/TestEclipseSwitchString' {
436436

437437
Lines mapping:
438438
5 <-> 5
439-
7 <-> 12
439+
7 <-> 11
440440
10 <-> 7
441441
12 <-> 9
442-
15 <-> 13
442+
15 <-> 12
443443
19 <-> 17
444-
21 <-> 25
444+
21 <-> 24
445445
24 <-> 19
446446
27 <-> 22
447-
30 <-> 26
447+
30 <-> 25
448448
34 <-> 30
449449
36 <-> 49
450450
39 <-> 34
@@ -456,9 +456,9 @@ Lines mapping:
456456
56 <-> 58
457457
58 <-> 61
458458
62 <-> 66
459-
64 <-> 72
459+
64 <-> 71
460460
68 <-> 69
461-
70 <-> 73
461+
70 <-> 72
462462
74 <-> 77
463463
76 <-> 93
464464
79 <-> 86
@@ -470,4 +470,4 @@ Not mapped:
470470
37
471471
52
472472
65
473-
77
473+
77

0 commit comments

Comments
 (0)