Skip to content

Commit

Permalink
Improve statement ordering and conditionals with pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskarth committed Aug 24, 2024
1 parent dca0799 commit d75fd5f
Show file tree
Hide file tree
Showing 13 changed files with 3,377 additions and 3,464 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -581,12 +581,6 @@ private static boolean reorderIf(IfStatement ifstat) {
return false;
}

// Cannot reorder pattern matches, causes semantic issues!
// TODO: proper pattern match reorder analysis
if (ifstat.isPatternMatched()) {
return false;
}

boolean ifdirect, elsedirect;
boolean noifstat = false, noelsestat;
boolean ifdirectpath = false, elsedirectpath = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,16 @@ private static boolean isSuperSimple(Exprent exprent) {
return exprent.type == Exprent.Type.VAR || exprent.type == Exprent.Type.CONST;
}

private static boolean hasPattern(Exprent exprent) {
for (Exprent ex : exprent.getAllExprents(false, true)) {
if (ex instanceof PatternExprent || ex instanceof FunctionExprent func && func.getFuncType() == FunctionType.INSTANCEOF && func.getLstOperands().size() > 2) {
return true;
}
}

return false;
}

public static Exprent propagateBoolNot(Exprent exprent) {

if (exprent instanceof FunctionExprent) {
Expand All @@ -581,10 +591,13 @@ public static Exprent propagateBoolNot(Exprent exprent) {

Exprent param = fexpr.getLstOperands().get(0);

if (param instanceof FunctionExprent) {
FunctionExprent fparam = (FunctionExprent) param;
if (param instanceof FunctionExprent fparam) {

FunctionType ftype = fparam.getFuncType();
// Can't change any patterns, except for nested '!' which we can peek through
if (hasPattern(fparam) && ftype != FunctionType.BOOL_NOT) {
return null;
}
boolean canSimplify = false;
switch (ftype) {
case BOOL_NOT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,11 @@ public static void validateDGraph(DirectGraph graph, RootStatement root) {
// ensure all exprents are unique
Map<ID<Exprent>, DirectNode> allExprents = new HashMap<>();

for (var node : graph.nodes) {
for (var exprent : node.exprents) {
for (var subExprent : exprent.getAllExprents(true, true)) {
for (DirectNode node : graph.nodes) {
for (Exprent exprent : node.exprents) {
for (Exprent subExprent : exprent.getAllExprents(true, true)) {
ID<Exprent> key = new ID<>(subExprent);
if (allExprents.containsKey(key)) {
if (allExprents.containsKey(key) && !(subExprent instanceof VarExprent)) {
throw new IllegalStateException(
"Duplicated exprent: " + subExprent + " (Sub exprent of: " + exprent + ") in dgraph. " +
"Appears in both node " + node.id + " and node " + allExprents.get(key).id + "!"
Expand Down
1 change: 1 addition & 0 deletions src/org/jetbrains/java/decompiler/struct/ContextUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public void save(final Function<String, StructClass> loader) throws IOException
try {
decompiledData.processClass(classCtx.cl);
} catch (final Throwable thr) {
DecompilerContext.getLogger().writeMessage("Class " + classCtx.cl.qualifiedName + " couldn't be fully decompiled.", thr);
classCtx.onError(thr);
} finally {
DecompilerContext.setCurrentContext(null);
Expand Down
2 changes: 2 additions & 0 deletions testData/results/pkg/TestCaseClasses.dec
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class 'pkg/Option1' {
23 35
24 35
25 35
2e 35
32 35
33 35
34 35
Expand Down Expand Up @@ -695,6 +696,7 @@ class 'pkg/Option2' {
3c 41
3d 41
3e 41
47 41
4b 41
4c 41
4d 41
Expand Down
Loading

0 comments on commit d75fd5f

Please sign in to comment.