Skip to content

Commit

Permalink
Remove magic typeFamily, replace with enum
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskarth committed Nov 13, 2023
1 parent ba6bbd5 commit 8f37d17
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.java.decompiler.modules.decompiler.exps.FieldExprent;
import org.jetbrains.java.decompiler.modules.decompiler.exps.FunctionExprent;
import org.jetbrains.java.decompiler.modules.decompiler.vars.CheckTypesResult;
import org.jetbrains.java.decompiler.struct.gen.TypeFamily;
import org.jetbrains.java.decompiler.struct.gen.VarType;
import org.jetbrains.java.decompiler.util.TextBuffer;
import org.jetbrains.java.decompiler.util.Typed;
Expand Down Expand Up @@ -255,7 +256,7 @@ private void optimizeType() {
Exprent l = getLstOperands().get(0);
Exprent r = getLstOperands().get(1);

if (l.getExprType().typeFamily != CodeConstants.TYPE_FAMILY_OBJECT || r.getExprType().typeFamily != CodeConstants.TYPE_FAMILY_OBJECT) {
if (l.getExprType().typeFamily != TypeFamily.OBJECT || r.getExprType().typeFamily != TypeFamily.OBJECT) {
setFuncType(FunctionType.EQ);
}
}
Expand Down
12 changes: 0 additions & 12 deletions src/org/jetbrains/java/decompiler/code/CodeConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@ public interface CodeConstants {
int TYPE_UNKNOWN = 17;
int TYPE_GENVAR = 18;

// ----------------------------------------------------------------------
// VARIABLE TYPE FAMILIES
// ----------------------------------------------------------------------

int TYPE_FAMILY_UNKNOWN = 0;
int TYPE_FAMILY_BOOLEAN = 1;
int TYPE_FAMILY_INTEGER = 2;
int TYPE_FAMILY_FLOAT = 3;
int TYPE_FAMILY_LONG = 4;
int TYPE_FAMILY_DOUBLE = 5;
int TYPE_FAMILY_OBJECT = 6;

// ----------------------------------------------------------------------
// ACCESS FLAGS
// ----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jetbrains.java.decompiler.struct.StructClass;

import java.util.*;
import java.util.regex.Pattern;

public class PluginContext {
private final List<Plugin> plugins = new ArrayList<>();
Expand Down Expand Up @@ -50,8 +51,9 @@ public void initialize() {

JavaPassRegistrar registrar = new JavaPassRegistrar();
for (Plugin plugin : plugins) {
if (!ids.add(plugin.id())) {
throw new IllegalStateException("Duplicate plugin " + plugin.getClass().getName() + " with id " + plugin.id());
String id = plugin.id();
if (!ids.add(id)) {
throw new IllegalStateException("Duplicate plugin " + plugin.getClass().getName() + " with id " + id);
}

plugin.registerJavaPasses(registrar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jetbrains.java.decompiler.modules.decompiler.stats.*;
import org.jetbrains.java.decompiler.struct.StructField;
import org.jetbrains.java.decompiler.struct.StructMethod;
import org.jetbrains.java.decompiler.struct.gen.TypeFamily;
import org.jetbrains.java.decompiler.struct.gen.VarType;
import org.jetbrains.java.decompiler.util.DotExporter;
import org.jetbrains.java.decompiler.util.Pair;
Expand Down Expand Up @@ -136,7 +137,7 @@ private static boolean simplify(SwitchStatement switchStatement, StructMethod mt
if (realConst == null) {
if (exprent instanceof ConstExprent) {
ConstExprent constLabel = (ConstExprent) exprent;
if (constLabel.getConstType().typeFamily == CodeConstants.TYPE_FAMILY_INTEGER) {
if (constLabel.getConstType().typeFamily == TypeFamily.INTEGER) {
int intLabel = constLabel.getIntValue();
// check for -1, used by nullable switches for the null branch
if (intLabel == -1) {
Expand All @@ -148,7 +149,7 @@ private static boolean simplify(SwitchStatement switchStatement, StructMethod mt
// TODO: more tests
for (Exprent key : mapping.keySet()) {
if (key instanceof ConstExprent
&& ((ConstExprent) key).getConstType().typeFamily == CodeConstants.TYPE_FAMILY_INTEGER
&& ((ConstExprent) key).getConstType().typeFamily == TypeFamily.INTEGER
&& ((ConstExprent) key).getIntValue() > intLabel) {
values.add(key.copy());
continue cases;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.jetbrains.java.decompiler.struct.consts.PooledConstant;
import org.jetbrains.java.decompiler.struct.consts.PrimitiveConstant;
import org.jetbrains.java.decompiler.struct.gen.FieldDescriptor;
import org.jetbrains.java.decompiler.struct.gen.TypeFamily;
import org.jetbrains.java.decompiler.struct.gen.VarType;
import org.jetbrains.java.decompiler.util.Pair;

Expand Down Expand Up @@ -88,7 +89,7 @@ private static boolean processStatement(SwitchStatement stat, Statement root) {
Pair<Statement, Exprent> initialUse = references.get(0);
if (initialUse.b instanceof AssignmentExprent && ((AssignmentExprent) initialUse.b).getRight() instanceof ConstExprent) {
ConstExprent constExprent = (ConstExprent) ((AssignmentExprent) initialUse.b).getRight();
if (constExprent.getConstType().typeFamily == CodeConstants.TYPE_FAMILY_INTEGER && constExprent.getIntValue() == 0) {
if (constExprent.getConstType().typeFamily == TypeFamily.INTEGER && constExprent.getIntValue() == 0) {
references.remove(0);
} else {
return false;
Expand Down Expand Up @@ -233,7 +234,7 @@ private static boolean processStatement(SwitchStatement stat, Statement root) {
// make sure we replace the right constant, null can be shared with anything
stat.getCaseValues().get(replaceIndex).replaceAll(u ->
u instanceof ConstExprent
&& u.getExprType().typeFamily == CodeConstants.TYPE_FAMILY_INTEGER
&& u.getExprType().typeFamily == TypeFamily.INTEGER
&& ((ConstExprent) u).getIntValue() == ix
? nvx : u);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ public CheckTypesResult checkExprTypeBounds() {
VarType typeLeft = left.getExprType();
VarType typeRight = right.getExprType();

if (typeLeft.typeFamily > typeRight.typeFamily) {
if (typeLeft.typeFamily.isGreater(typeRight.typeFamily)) {
result.addMinTypeExprent(right, VarType.getMinTypeInFamily(typeLeft.typeFamily));
}
else if (typeLeft.typeFamily < typeRight.typeFamily) {
else if (typeLeft.typeFamily.isGreater(typeRight.typeFamily)) {
result.addMinTypeExprent(left, typeRight);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
import org.jetbrains.java.decompiler.struct.gen.FieldDescriptor;
import org.jetbrains.java.decompiler.struct.gen.TypeFamily;
import org.jetbrains.java.decompiler.struct.gen.VarType;
import org.jetbrains.java.decompiler.struct.gen.generics.GenericType;
import org.jetbrains.java.decompiler.struct.match.MatchEngine;
Expand Down Expand Up @@ -616,7 +617,7 @@ public void adjustConstType(VarType expectedType) {
}
// BYTE, BYTECHAR, SHORTCHAR, SHORT, CHAR => INT in the INT context
else if ((expectedType.equals(VarType.VARTYPE_INT) || expectedType.equals(VarType.VARTYPE_INTEGER)) &&
constType.typeFamily == CodeConstants.TYPE_FAMILY_INTEGER) {
constType.typeFamily == TypeFamily.INTEGER) {
setConstType(VarType.VARTYPE_INT);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jetbrains.java.decompiler.struct.consts.PooledConstant;
import org.jetbrains.java.decompiler.struct.consts.PrimitiveConstant;
import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor;
import org.jetbrains.java.decompiler.struct.gen.TypeFamily;
import org.jetbrains.java.decompiler.struct.gen.VarType;
import org.jetbrains.java.decompiler.struct.gen.generics.GenericMethodDescriptor;
import org.jetbrains.java.decompiler.struct.gen.generics.GenericType;
Expand Down Expand Up @@ -224,7 +225,7 @@ public VarType getInferredExprType(VarType upperBound) {
// Don't keep the cast in that case.
VarType unboxed = VarType.UNBOXING_TYPES.get(inferred);
if (unboxed == null || !unboxed.equals(upperBound)) {
if (inferred.typeFamily == CodeConstants.TYPE_FAMILY_OBJECT || inferred.isGeneric()) {
if (inferred.typeFamily == TypeFamily.OBJECT || inferred.isGeneric()) {
boxing.keepCast = true;
}
}
Expand Down Expand Up @@ -280,7 +281,7 @@ else if (!bounds.containsKey(from)) {

// Don't capture as a real upper bound unless the upperbound is an object or a likewise generic.
// FunctionExprent needs to inform its children about the type that it has, so it'll cause conflict here.
if (r.type == CodeConstants.TYPE_GENVAR && (upperBound.typeFamily == CodeConstants.TYPE_FAMILY_OBJECT || upperBound.isGeneric())) {
if (r.type == CodeConstants.TYPE_GENVAR && (upperBound.typeFamily == TypeFamily.OBJECT || upperBound.isGeneric())) {
upperBoundsMap.put(r.resizeArrayDim(0), upperBound.resizeArrayDim(upperBound.arrayDim - r.arrayDim));
}
else {
Expand Down Expand Up @@ -984,7 +985,7 @@ public TextBuffer appendParamList(int indent) {
Exprent value = inv.lstParameters.get(0);
types[i] = value.getExprType(); //Infer?
//Unboxing in this case is lossy, so we need to explicitly set the type
if (types[i].typeFamily == CodeConstants.TYPE_FAMILY_INTEGER) {
if (types[i].typeFamily == TypeFamily.INTEGER) {
types[i] =
"java/lang/Short".equals(inv.classname) ? VarType.VARTYPE_SHORT :
"java/lang/Byte".equals(inv.classname) ? VarType.VARTYPE_BYTE :
Expand Down Expand Up @@ -1191,7 +1192,7 @@ public boolean isBoxingCall() {
// 'Integer.valueOf(1)' has '1' type detected as TYPE_BYTECHAR
// 'Integer.valueOf(40_000)' has '40_000' type detected as TYPE_CHAR
// so we check the type family instead
if (lstParameters.get(0).getExprType().typeFamily == CodeConstants.TYPE_FAMILY_INTEGER) {
if (lstParameters.get(0).getExprType().typeFamily == TypeFamily.INTEGER) {
if (classname.equals("java/lang/Integer")) {
return true;
}
Expand Down Expand Up @@ -1487,11 +1488,11 @@ private boolean isSuperset(MethodDescriptor md, int i, Exprent exp) {
// See also: TestVarIndex
private boolean shouldBeAmbiguous(VarType param, Exprent exp) {
if (exp instanceof VarExprent) {
if (exp.getExprType().typeFamily == CodeConstants.TYPE_FAMILY_INTEGER && param.typeFamily == CodeConstants.TYPE_FAMILY_INTEGER) {
if (exp.getExprType().typeFamily == TypeFamily.INTEGER && param.typeFamily == TypeFamily.INTEGER) {
return !param.equals(exp.getExprType());
}

if (param.equals(VarType.VARTYPE_OBJECT) && param.arrayDim == 0 && exp.getExprType().typeFamily == CodeConstants.TYPE_FAMILY_OBJECT) {
if (param.equals(VarType.VARTYPE_OBJECT) && param.arrayDim == 0 && exp.getExprType().typeFamily == TypeFamily.OBJECT) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jetbrains.java.decompiler.struct.attr.StructLocalVariableTableAttribute.LocalVariable;
import org.jetbrains.java.decompiler.struct.attr.StructMethodParametersAttribute;
import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor;
import org.jetbrains.java.decompiler.struct.gen.TypeFamily;
import org.jetbrains.java.decompiler.struct.gen.VarType;
import org.jetbrains.java.decompiler.struct.gen.generics.GenericType;
import org.jetbrains.java.decompiler.util.ArrayHelper;
Expand Down Expand Up @@ -928,7 +929,7 @@ else if (expr instanceof VarExprent) {
}

private static boolean canConstTypeMerge(VarType type) {
if (type.typeFamily == CodeConstants.TYPE_FAMILY_OBJECT) {
if (type.typeFamily == TypeFamily.OBJECT) {
return type == VarType.VARTYPE_STRING || type == VarType.VARTYPE_CLASS || type == VarType.VARTYPE_NULL;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jetbrains.java.decompiler.struct.StructClass;
import org.jetbrains.java.decompiler.struct.StructMethod;
import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor;
import org.jetbrains.java.decompiler.struct.gen.TypeFamily;
import org.jetbrains.java.decompiler.struct.gen.VarType;

import java.util.HashMap;
Expand Down Expand Up @@ -99,7 +100,7 @@ private static void resetExprentTypes(DirectGraph graph) {
}
else if (expr instanceof ConstExprent) {
ConstExprent constExpr = (ConstExprent)expr;
if (constExpr.getConstType().typeFamily == CodeConstants.TYPE_FAMILY_INTEGER) {
if (constExpr.getConstType().typeFamily == TypeFamily.INTEGER) {
constExpr.setConstType(new ConstExprent(constExpr.getIntValue(), constExpr.isBoolPermitted(), null).getConstType());
}
}
Expand Down Expand Up @@ -127,7 +128,7 @@ private boolean checkTypeExprent(Exprent exprent) {
private boolean checkTypeExpr(Exprent exprent) {
if (exprent instanceof ConstExprent) {
ConstExprent constExpr = (ConstExprent) exprent;
if (constExpr.getConstType().typeFamily <= CodeConstants.TYPE_FAMILY_INTEGER) { // boolean or integer
if (constExpr.getConstType().typeFamily.isLesserOrEqual(TypeFamily.INTEGER)) { // boolean or integer
VarVersionPair pair = new VarVersionPair(constExpr.id, -1);
if (!mapExprentMinTypes.containsKey(pair)) {
mapExprentMinTypes.put(pair, constExpr.getConstType());
Expand All @@ -140,7 +141,7 @@ private boolean checkTypeExpr(Exprent exprent) {
boolean res = true;
if (result != null) {
for (CheckTypesResult.ExprentTypePair entry : result.getLstMaxTypeExprents()) {
if (entry.type.typeFamily != CodeConstants.TYPE_FAMILY_OBJECT) {
if (entry.type.typeFamily != TypeFamily.OBJECT) {
changeExprentType(entry.exprent, entry.type, 1);
}
}
Expand All @@ -162,10 +163,10 @@ private boolean changeExprentType(Exprent exprent, VarType newType, int minMax)
ConstExprent constExpr = (ConstExprent)exprent;
VarType constType = constExpr.getConstType();

if (newType.typeFamily > CodeConstants.TYPE_FAMILY_INTEGER || constType.typeFamily > CodeConstants.TYPE_FAMILY_INTEGER) {
if (newType.typeFamily.isGreater(TypeFamily.INTEGER) || constType.typeFamily.isGreater(TypeFamily.INTEGER)) {
return true;
}
else if (newType.typeFamily == CodeConstants.TYPE_FAMILY_INTEGER) {
else if (newType.typeFamily == TypeFamily.INTEGER) {
VarType minInteger = new ConstExprent((Integer)constExpr.getValue(), false, null).getConstType();
if (minInteger.isStrictSuperset(newType)) {
newType = minInteger;
Expand All @@ -189,9 +190,9 @@ private boolean changeVarExprentType(Exprent exprent, VarType newType, int minMa
if (minMax == 0) { // min
VarType currentMinType = mapExprentMinTypes.get(pair);
VarType newMinType;
if (currentMinType == null || newType.typeFamily > currentMinType.typeFamily) {
if (currentMinType == null || newType.typeFamily.isGreater(currentMinType.typeFamily)) {
newMinType = newType;
} else if (newType.typeFamily < currentMinType.typeFamily) {
} else if (newType.typeFamily.isLesser(currentMinType.typeFamily)) {
return true;
} else {
newMinType = VarType.getCommonSupertype(currentMinType, newType);
Expand All @@ -202,15 +203,15 @@ private boolean changeVarExprentType(Exprent exprent, VarType newType, int minMa
((ConstExprent) exprent).setConstType(newMinType);
}

if (currentMinType != null && (newMinType.typeFamily > currentMinType.typeFamily || newMinType.isStrictSuperset(currentMinType))) {
if (currentMinType != null && (newMinType.typeFamily.isGreater(currentMinType.typeFamily) || newMinType.isStrictSuperset(currentMinType))) {
return false;
}
} else { // max
VarType currentMaxType = mapExprentMaxTypes.get(pair);
VarType newMaxType;
if (currentMaxType == null || newType.typeFamily < currentMaxType.typeFamily) {
if (currentMaxType == null || newType.typeFamily.isLesser(currentMaxType.typeFamily)) {
newMaxType = newType;
} else if (newType.typeFamily > currentMaxType.typeFamily) {
} else if (newType.typeFamily.isGreater(currentMaxType.typeFamily)) {
return true;
} else {
newMaxType = VarType.getCommonMinType(currentMaxType, newType);
Expand Down
27 changes: 27 additions & 0 deletions src/org/jetbrains/java/decompiler/struct/gen/TypeFamily.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jetbrains.java.decompiler.struct.gen;

import org.jetbrains.annotations.NotNull;

public enum TypeFamily {
UNKNOWN,
BOOLEAN,
INTEGER,
FLOAT,
LONG,
DOUBLE,
OBJECT;

// TODO: document what these mean, and try to remove! Doesn't make sense to have these

public boolean isGreater(@NotNull TypeFamily other) {
return ordinal() > other.ordinal();
}

public boolean isLesser(@NotNull TypeFamily other) {
return ordinal() < other.ordinal();
}

public boolean isLesserOrEqual(@NotNull TypeFamily other) {
return ordinal() <= other.ordinal();
}
}
Loading

0 comments on commit 8f37d17

Please sign in to comment.