Skip to content

Commit 29e2bc2

Browse files
committed
Working on DOM 2
1 parent 2133f72 commit 29e2bc2

File tree

14 files changed

+622
-302
lines changed

14 files changed

+622
-302
lines changed

src/org/jetbrains/java/decompiler/code/cfg/ExceptionRangeCFG.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
22
package org.jetbrains.java.decompiler.code.cfg;
33

4+
import org.jetbrains.annotations.NotNull;
45
import org.jetbrains.java.decompiler.main.DecompilerContext;
56

67
import java.util.ArrayList;
78
import java.util.List;
89
import java.util.stream.Collectors;
910

1011
public class ExceptionRangeCFG {
11-
private final List<BasicBlock> protectedRange; // FIXME: replace with set
12+
private final @NotNull List<BasicBlock> protectedRange; // FIXME: replace with set
1213
private BasicBlock handler;
13-
private List<String> exceptionTypes;
14+
private @NotNull List<String> exceptionTypes;
1415

15-
public ExceptionRangeCFG(List<BasicBlock> protectedRange, BasicBlock handler, List<String> exceptionType) {
16+
public ExceptionRangeCFG(@NotNull List<BasicBlock> protectedRange, BasicBlock handler, @NotNull List<String> exceptionType) {
1617
this.protectedRange = protectedRange;
1718
this.handler = handler;
18-
19-
if (exceptionType != null) {
20-
this.exceptionTypes = new ArrayList<>(exceptionType);
21-
}
19+
this.exceptionTypes = new ArrayList<>(exceptionType);
2220
}
2321

24-
public ExceptionRangeCFG(List<BasicBlock> protectedRange, BasicBlock handler, String exceptionType) {
22+
public ExceptionRangeCFG(@NotNull List<BasicBlock> protectedRange, BasicBlock handler, String exceptionType) {
2523
this.protectedRange = protectedRange;
2624
this.handler = handler;
25+
this.exceptionTypes = new ArrayList<>();
2726

2827
if (exceptionType != null) {
29-
this.exceptionTypes = new ArrayList<>();
3028
this.exceptionTypes.add(exceptionType);
29+
} else {
30+
this.exceptionTypes.add("Ljava/lang/Throwable");
3131
}
3232
}
3333

@@ -71,11 +71,11 @@ public void setHandler(BasicBlock handler) {
7171
this.handler = handler;
7272
}
7373

74-
public List<BasicBlock> getProtectedRange() {
74+
public @NotNull List<BasicBlock> getProtectedRange() {
7575
return protectedRange;
7676
}
7777

78-
public List<String> getExceptionTypes() {
78+
public @NotNull List<String> getExceptionTypes() {
7979
return this.exceptionTypes;
8080
}
8181

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ public static RootStatement codeToJava(StructClass cl, StructMethod mt, MethodDe
164164
}
165165

166166
DotExporter.toDotFile(graph, mt, "cfgParsed", true);
167-
// RootStatement root = DomHelper2.parseGraph(graph, mt, 0);
168-
RootStatement root = DomHelper.parseGraph(graph, mt, 0);
167+
RootStatement root = DomHelper2.parseGraph(graph, mt, 0);
168+
// RootStatement root = DomHelper.parseGraph(graph, mt, 0);
169169

170170
DecompileRecord decompileRecord = new DecompileRecord(mt);
171171
debugCurrentDecompileRecord.set(decompileRecord);
@@ -183,8 +183,8 @@ public static RootStatement codeToJava(StructClass cl, StructMethod mt, MethodDe
183183
decompileRecord.add("ProcessFinallyOld_" + finallyProcessed, root);
184184
DotExporter.toDotFile(graph, mt, "cfgProcessFinally_" + finallyProcessed, true);
185185

186-
// root = DomHelper2.parseGraph(graph, mt, finallyProcessed);
187-
root = DomHelper.parseGraph(graph, mt, finallyProcessed);
186+
root = DomHelper2.parseGraph(graph, mt, finallyProcessed);
187+
// root = DomHelper.parseGraph(graph, mt, finallyProcessed);
188188
root.addComments(oldRoot);
189189

190190
decompileRecord.add("ProcessFinally_" + finallyProcessed, root);

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -832,25 +832,7 @@ public static TextBuffer jmpWrapper(Statement stat, int indent, boolean semicolo
832832

833833
List<StatEdge> lstSuccs = stat.getSuccessorEdges(Statement.STATEDGE_DIRECT_ALL);
834834
if (lstSuccs.size() == 1) {
835-
StatEdge edge = lstSuccs.get(0);
836-
if (edge.getType() != StatEdge.TYPE_REGULAR && edge.explicit && !(edge.getDestination() instanceof DummyExitStatement)) {
837-
buf.appendIndent(indent);
838-
839-
switch (edge.getType()) {
840-
case StatEdge.TYPE_BREAK:
841-
addDeletedGotoInstructionMapping(stat, buf);
842-
buf.append("break");
843-
break;
844-
case StatEdge.TYPE_CONTINUE:
845-
addDeletedGotoInstructionMapping(stat, buf);
846-
buf.append("continue");
847-
}
848-
849-
if (edge.labeled) {
850-
buf.append(" label").append(edge.closure.id);
851-
}
852-
buf.append(";").appendLineSeparator();
853-
}
835+
jmpWrapperEdge(stat, indent, lstSuccs.get(0), buf);
854836
}
855837

856838
if (buf.length() == 0 && semicolon) {
@@ -860,6 +842,32 @@ public static TextBuffer jmpWrapper(Statement stat, int indent, boolean semicolo
860842
return buf;
861843
}
862844

845+
public static void jmpWrapperEdge(Statement stat, int indent, StatEdge edge, TextBuffer buf) {
846+
847+
if (edge.getType() != StatEdge.TYPE_REGULAR && edge.explicit && !(edge.getDestination() instanceof DummyExitStatement)) {
848+
buf.appendIndent(indent);
849+
850+
switch (edge.getType()) {
851+
case StatEdge.TYPE_BREAK:
852+
if (stat != null) {
853+
addDeletedGotoInstructionMapping(stat, buf);
854+
}
855+
buf.append("break");
856+
break;
857+
case StatEdge.TYPE_CONTINUE:
858+
if (stat != null) {
859+
addDeletedGotoInstructionMapping(stat, buf);
860+
}
861+
buf.append("continue");
862+
}
863+
864+
if (edge.labeled) {
865+
buf.append(" label").append(edge.closure.id);
866+
}
867+
buf.append(";").appendLineSeparator();
868+
}
869+
}
870+
863871
public static String buildJavaClassName(String name) {
864872
String res = name.replace('/', '.');
865873

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
import org.jetbrains.java.decompiler.modules.decompiler.sforms.SSAConstructorSparseEx;
2323
import org.jetbrains.java.decompiler.modules.decompiler.sforms.SSAUConstructorSparseEx;
2424
import org.jetbrains.java.decompiler.modules.decompiler.sforms.SimpleSSAReassign;
25-
import org.jetbrains.java.decompiler.modules.decompiler.stats.BasicBlockStatement;
26-
import org.jetbrains.java.decompiler.modules.decompiler.stats.CatchAllStatement;
27-
import org.jetbrains.java.decompiler.modules.decompiler.stats.RootStatement;
28-
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement;
25+
import org.jetbrains.java.decompiler.modules.decompiler.stats.*;
2926
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarProcessor;
3027
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPair;
3128
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionsGraph;

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

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ private static boolean inlineSingleBlocksRec(Statement stat) {
4646
}
4747

4848
private static void inlineBlock(SequenceStatement seq, int index) {
49-
5049
Statement first = seq.getStats().get(index);
5150
Statement pre = seq.getStats().get(index - 1);
5251
pre.removeSuccessor(pre.getFirstSuccessor()); // single regular edge
@@ -75,8 +74,14 @@ private static void inlineBlock(SequenceStatement seq, int index) {
7574

7675
ifparent.getStats().addWithKey(block, block.id);
7776
block.setParent(ifparent);
78-
}
79-
else {
77+
} else if (parent instanceof SwitchStatement switchParent && source == switchParent.getFirst()) {
78+
SequenceStatement block = new SequenceStatement(lst);
79+
block.setAllParent();
80+
81+
StatEdge newEdge = new StatEdge(StatEdge.TYPE_REGULAR, source, block);
82+
source.addSuccessor(newEdge);
83+
switchParent.replaceCaseEdge(edge, newEdge, block);
84+
} else {
8085
lst.add(0, source);
8186

8287
SequenceStatement block = new SequenceStatement(lst);
@@ -143,38 +148,40 @@ private static boolean isInlineable(SequenceStatement seq, int index) {
143148
if (lst.size() == 1) {
144149
StatEdge edge = lst.get(0);
145150

146-
if (sameCatchRanges(edge)) {
147-
if (!edge.canInline) {
148-
return false; //Dirty hack, but lets do it!
149-
}
150-
151-
if (!edge.explicit) {
152-
for (int i = index; i < seq.getStats().size(); i++) {
153-
if (!noExitLabels(seq.getStats().get(i), seq)) {
154-
return false;
155-
}
156-
}
157-
}
151+
if (!sameCatchRanges(edge)) {
152+
return false;
153+
}
158154

159-
if (edge.getSource().getParent() instanceof SwitchStatement) {
160-
SwitchStatement swst = (SwitchStatement) edge.getSource().getParent();
155+
if (!edge.canInline) {
156+
return false; //Dirty hack, but lets do it!
157+
}
161158

162-
// Can't inline into an empty switch statement!
163-
if (swst.getCaseStatements().isEmpty()) {
159+
if (!edge.explicit) {
160+
for (int i = index; i < seq.getStats().size(); i++) {
161+
if (!noExitLabels(seq.getStats().get(i), seq)) {
164162
return false;
165163
}
166164
}
165+
}
167166

168-
boolean noPreSuccessors = !pre.hasAnySuccessor();
167+
if (edge.getSource().getParent() instanceof SwitchStatement) {
168+
SwitchStatement swst = (SwitchStatement) edge.getSource().getParent();
169169

170-
if (noPreSuccessors) {
171-
// No successors so we can't inline (as we don't know where to go!) [TestInlineNoSuccessor]
170+
// Can't inline into an empty switch statement!
171+
if (swst.getCaseStatements().isEmpty()) {
172172
return false;
173173
}
174+
}
175+
176+
boolean noPreSuccessors = !pre.hasAnySuccessor();
174177

175-
// Has at least 1 successor so we can inline
176-
return true;
178+
if (noPreSuccessors) {
179+
// No successors so we can't inline (as we don't know where to go!) [TestInlineNoSuccessor]
180+
return false;
177181
}
182+
183+
// Has at least 1 successor so we can inline
184+
return true;
178185
// FIXME: count labels properly
179186
}
180187

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ private static HashMap<Statement, List<StatEdge>> setExplicitEdges(Statement sta
318318
for (int i = 0; i < last; i++) {
319319
Statement stt = swst.getCaseStatements().get(i);
320320
Statement stnext = swst.getCaseStatements().get(i + 1);
321+
if (stt == null || stnext == null) {
322+
continue;
323+
}
321324

322325
if (stnext.getExprents() != null && stnext.getExprents().isEmpty() && stnext.hasAnySuccessor() && !isPatternSwitch) {
323326
stnext = stnext.getAllSuccessorEdges().get(0).getDestination();
@@ -327,12 +330,14 @@ private static HashMap<Statement, List<StatEdge>> setExplicitEdges(Statement sta
327330

328331
if (last >= 0) { // empty switch possible
329332
Statement stlast = swst.getCaseStatements().get(last);
330-
if (stlast.getExprents() != null && stlast.getExprents().isEmpty() && stlast.hasAnySuccessor()) {
331-
StatEdge edge = stlast.getAllSuccessorEdges().get(0);
332-
mapEdges.put(edge.getDestination(), new ArrayList<>(Collections.singletonList(edge)));
333-
} else {
334-
mapEdges = setExplicitEdges(stlast);
335-
processEdgesWithNext(stlast, mapEdges, null);
333+
if (stlast != null) {
334+
if (stlast.getExprents() != null && stlast.getExprents().isEmpty() && stlast.hasAnySuccessor()) {
335+
StatEdge edge = stlast.getAllSuccessorEdges().get(0);
336+
mapEdges.put(edge.getDestination(), new ArrayList<>(Collections.singletonList(edge)));
337+
} else {
338+
mapEdges = setExplicitEdges(stlast);
339+
processEdgesWithNext(stlast, mapEdges, null);
340+
}
336341
}
337342
}
338343

@@ -467,7 +472,7 @@ public static boolean hideDefaultSwitchEdges(Statement stat) {
467472
SwitchStatement swst = (SwitchStatement)stat;
468473

469474
int last = swst.getCaseStatements().size() - 1;
470-
if (last >= 0) { // empty switch possible
475+
if (last >= 0 && swst.getCaseStatements().get(last) != null) { // empty switch possible
471476
Statement stlast = swst.getCaseStatements().get(last);
472477

473478
if (stlast.getExprents() != null && stlast.getExprents().isEmpty()) {

0 commit comments

Comments
 (0)