|
| 1 | +package liquidjava.rj_language.opt; |
| 2 | + |
| 3 | +import static liquidjava.rj_language.opt.VCSimplificationUtils.copyWithRefinement; |
| 4 | +import static liquidjava.rj_language.opt.VCSimplificationUtils.factsOf; |
| 5 | + |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import liquidjava.processor.VCImplication; |
| 10 | +import liquidjava.rj_language.Predicate; |
| 11 | +import liquidjava.rj_language.ast.BinaryExpression; |
| 12 | +import liquidjava.rj_language.ast.Enum; |
| 13 | +import liquidjava.rj_language.ast.Expression; |
| 14 | +import liquidjava.rj_language.ast.LiteralBoolean; |
| 15 | +import liquidjava.rj_language.ast.Var; |
| 16 | +import liquidjava.utils.constants.Formats; |
| 17 | +import spoon.reflect.declaration.CtEnum; |
| 18 | +import spoon.reflect.declaration.CtEnumValue; |
| 19 | + |
| 20 | +/** |
| 21 | + * Decides the comparisons of a variable with an enum constant that a fact about the variable already decides: under |
| 22 | + * {@code x == A}, {@code x == A} is true and {@code x == B} false, and under {@code x != A}, {@code x == A} is false, |
| 23 | + * so the ternaries they test can fold. Only the constants of the enum types of the chain's binders are used, which the |
| 24 | + * solver translates as distinct values, unlike {@code static final} constants that may share a value |
| 25 | + */ |
| 26 | +public class VCKnownEnumValues implements VCSimplificationPass { |
| 27 | + |
| 28 | + /** |
| 29 | + * A comparison {@code variable == constant}, or {@code variable != constant} when not {@code equal} |
| 30 | + */ |
| 31 | + private record EnumComparison(Expression expression, String variable, String constant, boolean equal) { |
| 32 | + } |
| 33 | + |
| 34 | + /** The enum type of each enum constant, by the name the solver translates the constant from */ |
| 35 | + private final Map<String, String> enumTypes = new HashMap<>(); |
| 36 | + |
| 37 | + public VCKnownEnumValues(VCImplication implication) { |
| 38 | + for (VCImplication row = implication; row != null; row = row.getNext()) |
| 39 | + if (row.getType() != null && row.getType().getDeclaration()instanceof CtEnum<?> enumDecl) |
| 40 | + for (CtEnumValue<?> value : enumDecl.getEnumValues()) |
| 41 | + enumTypes.put(String.format(Formats.ENUM, enumDecl.getSimpleName(), value.getSimpleName()), |
| 42 | + enumDecl.getQualifiedName()); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Uses the first fact that decides some comparison to replace those comparisons by their value |
| 47 | + */ |
| 48 | + @Override |
| 49 | + public VCImplication apply(VCImplication implication) { |
| 50 | + for (VCImplication row = implication; row != null; row = row.getNext()) |
| 51 | + for (Expression fact : factsOf(row.getRefinement().getExpression())) { |
| 52 | + EnumComparison known = enumComparison(fact); |
| 53 | + if (known == null) |
| 54 | + continue; |
| 55 | + // the last row may be the conclusion, which the other rows cannot assume |
| 56 | + boolean onlyLastRow = row.getNext() == null && row != implication; |
| 57 | + VCImplication decided = decideRows(implication, known, onlyLastRow); |
| 58 | + if (!decided.equals(implication)) |
| 59 | + return decided; |
| 60 | + } |
| 61 | + return implication; |
| 62 | + } |
| 63 | + |
| 64 | + private VCImplication decideRows(VCImplication implication, EnumComparison known, boolean onlyLastRow) { |
| 65 | + if (implication == null) |
| 66 | + return null; |
| 67 | + Expression expression = implication.getRefinement().getExpression(); |
| 68 | + boolean decides = !onlyLastRow || implication.getNext() == null; |
| 69 | + VCImplication result = copyWithRefinement(implication, |
| 70 | + new Predicate(decides ? decide(expression, known) : expression.clone())); |
| 71 | + result.setNext(decideRows(implication.getNext(), known, onlyLastRow)); |
| 72 | + return result; |
| 73 | + } |
| 74 | + |
| 75 | + // The expression with the comparisons known decides replaced by their value, except for its own fact |
| 76 | + private Expression decide(Expression expression, EnumComparison known) { |
| 77 | + if (expression == known.expression()) |
| 78 | + return expression.clone(); |
| 79 | + EnumComparison compared = enumComparison(expression); |
| 80 | + if (compared != null && compared.variable().equals(known.variable()) |
| 81 | + && enumTypes.get(compared.constant()).equals(enumTypes.get(known.constant()))) { |
| 82 | + boolean sameConstant = compared.constant().equals(known.constant()); |
| 83 | + if (known.equal()) |
| 84 | + return new LiteralBoolean(sameConstant == compared.equal()); |
| 85 | + if (sameConstant) |
| 86 | + return new LiteralBoolean(!compared.equal()); |
| 87 | + } |
| 88 | + Expression result = expression.clone(); |
| 89 | + for (int i = 0; i < expression.getChildren().size(); i++) |
| 90 | + result.setChild(i, decide(expression.getChildren().get(i), known)); |
| 91 | + return result; |
| 92 | + } |
| 93 | + |
| 94 | + // A comparison of a variable with an enum constant, either way round, or null |
| 95 | + private EnumComparison enumComparison(Expression expression) { |
| 96 | + if (!(expression instanceof BinaryExpression binary)) |
| 97 | + return null; |
| 98 | + boolean equal = "==".equals(binary.getOperator()); |
| 99 | + if (!equal && !"!=".equals(binary.getOperator())) |
| 100 | + return null; |
| 101 | + Expression left = binary.getFirstOperand(), right = binary.getSecondOperand(); |
| 102 | + String leftConstant = enumConstant(left), rightConstant = enumConstant(right); |
| 103 | + if (left instanceof Var variable && leftConstant == null && rightConstant != null) |
| 104 | + return new EnumComparison(expression, variable.getName(), rightConstant, equal); |
| 105 | + if (right instanceof Var variable && rightConstant == null && leftConstant != null) |
| 106 | + return new EnumComparison(expression, variable.getName(), leftConstant, equal); |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + // The name of the enum constant an expression is, written in code or in a refinement, or null |
| 111 | + private String enumConstant(Expression expression) { |
| 112 | + String name = null; |
| 113 | + if (expression instanceof Var var) |
| 114 | + name = var.getName(); |
| 115 | + else if (expression instanceof Enum en && en.getResolvedLiteral() == null) |
| 116 | + name = en.toString(); |
| 117 | + return enumTypes.containsKey(name) ? name : null; |
| 118 | + } |
| 119 | +} |
0 commit comments