Ejemplo n.º 1
0
 private boolean briefStatementComparison(List<Statement> firstSeq, List<Statement> secondSeq) {
   if (firstSeq.isEmpty() && secondSeq.isEmpty()) {
     return true;
   }
   if (firstSeq.size() != 1 || secondSeq.size() != 1) {
     return false;
   }
   Statement first = firstSeq.get(0);
   Statement second = secondSeq.get(0);
   if (first instanceof BreakStatement && second instanceof BreakStatement) {
     BreakStatement firstBreak = (BreakStatement) first;
     BreakStatement secondBreak = (BreakStatement) second;
     return firstBreak.getTarget() == secondBreak.getTarget();
   }
   return false;
 }
Ejemplo n.º 2
0
 @Override
 public void visit(VariableExpr expr) {
   int index = expr.getIndex();
   resultExpr = expr;
   if (readFrequencies[index] != 1 || preservedVars[index]) {
     return;
   }
   if (resultSequence.isEmpty()) {
     return;
   }
   Statement last = resultSequence.get(resultSequence.size() - 1);
   if (!(last instanceof AssignmentStatement)) {
     return;
   }
   AssignmentStatement assignment = (AssignmentStatement) last;
   if (assignment.isAsync()) {
     return;
   }
   if (!(assignment.getLeftValue() instanceof VariableExpr)) {
     return;
   }
   VariableExpr var = (VariableExpr) assignment.getLeftValue();
   if (var.getLocation() != null
       && assignment.getLocation() != null
       && !assignment.getLocation().equals(var.getLocation())) {
     return;
   }
   if (var.getIndex() == index) {
     resultSequence.remove(resultSequence.size() - 1);
     assignment.getRightValue().setLocation(assignment.getLocation());
     assignment.getRightValue().acceptVisitor(this);
   }
 }
Ejemplo n.º 3
0
 @Override
 public void visit(ConditionalStatement statement) {
   statement.getCondition().acceptVisitor(this);
   statement.setCondition(resultExpr);
   List<Statement> consequent = processSequence(statement.getConsequent());
   List<Statement> alternative = processSequence(statement.getAlternative());
   if (consequent.isEmpty()) {
     consequent.addAll(alternative);
     alternative.clear();
     statement.setCondition(ExprOptimizer.invert(statement.getCondition()));
   }
   if (consequent.isEmpty()) {
     resultStmt = Statement.empty();
     return;
   }
   statement.getConsequent().clear();
   statement.getConsequent().addAll(consequent);
   statement.getAlternative().clear();
   statement.getAlternative().addAll(alternative);
   resultStmt = statement;
 }
Ejemplo n.º 4
0
 private boolean tryApplyConstructor(InvocationExpr expr) {
   if (!expr.getMethod().getName().equals("<init>")) {
     return false;
   }
   if (resultSequence == null || resultSequence.isEmpty()) {
     return false;
   }
   Statement last = resultSequence.get(resultSequence.size() - 1);
   if (!(last instanceof AssignmentStatement)) {
     return false;
   }
   AssignmentStatement assignment = (AssignmentStatement) last;
   if (!(assignment.getLeftValue() instanceof VariableExpr)) {
     return false;
   }
   VariableExpr var = (VariableExpr) assignment.getLeftValue();
   if (!(expr.getArguments().get(0) instanceof VariableExpr)) {
     return false;
   }
   VariableExpr target = (VariableExpr) expr.getArguments().get(0);
   if (target.getIndex() != var.getIndex()) {
     return false;
   }
   if (!(assignment.getRightValue() instanceof NewExpr)) {
     return false;
   }
   NewExpr constructed = (NewExpr) assignment.getRightValue();
   if (!constructed.getConstructedClass().equals(expr.getMethod().getClassName())) {
     return false;
   }
   Expr[] args = expr.getArguments().toArray(new Expr[0]);
   args = Arrays.copyOfRange(args, 1, args.length);
   InvocationExpr constructrExpr = Expr.constructObject(expr.getMethod(), args);
   constructrExpr.setLocation(expr.getLocation());
   assignment.setRightValue(constructrExpr);
   readFrequencies[var.getIndex()]--;
   return true;
 }
Ejemplo n.º 5
0
 private void eliminateRedundantBreaks(List<Statement> statements, IdentifiedStatement exit) {
   if (statements.isEmpty()) {
     return;
   }
   Statement last = statements.get(statements.size() - 1);
   if (last instanceof BreakStatement && exit != null) {
     IdentifiedStatement target = ((BreakStatement) last).getTarget();
     if (exit == target) {
       statements.remove(statements.size() - 1);
     }
   }
   if (statements.isEmpty()) {
     return;
   }
   for (int i = 0; i < statements.size(); ++i) {
     Statement stmt = statements.get(i);
     if (stmt instanceof ConditionalStatement) {
       ConditionalStatement cond = (ConditionalStatement) stmt;
       check_conditional:
       {
         last =
             cond.getConsequent().isEmpty()
                 ? null
                 : cond.getConsequent().get(cond.getConsequent().size() - 1);
         if (last instanceof BreakStatement) {
           BreakStatement breakStmt = (BreakStatement) last;
           if (exit != null && exit == breakStmt.getTarget()) {
             cond.getConsequent().remove(cond.getConsequent().size() - 1);
             List<Statement> remaining = statements.subList(i + 1, statements.size());
             cond.getAlternative().addAll(remaining);
             remaining.clear();
             break check_conditional;
           }
         }
         last =
             cond.getAlternative().isEmpty()
                 ? null
                 : cond.getAlternative().get(cond.getAlternative().size() - 1);
         if (last instanceof BreakStatement) {
           BreakStatement breakStmt = (BreakStatement) last;
           if (exit != null && exit == breakStmt.getTarget()) {
             cond.getAlternative().remove(cond.getAlternative().size() - 1);
             List<Statement> remaining = statements.subList(i + 1, statements.size());
             cond.getConsequent().addAll(remaining);
             remaining.clear();
           }
         }
       }
       if (i == statements.size() - 1) {
         eliminateRedundantBreaks(cond.getConsequent(), exit);
         eliminateRedundantBreaks(cond.getAlternative(), exit);
       }
       normalizeConditional(cond);
       if (cond.getConsequent().size() == 1
           && cond.getConsequent().get(0) instanceof ConditionalStatement) {
         ConditionalStatement innerCond = (ConditionalStatement) cond.getConsequent().get(0);
         if (innerCond.getAlternative().isEmpty()) {
           if (cond.getAlternative().isEmpty()) {
             cond.getConsequent().clear();
             cond.getConsequent().addAll(innerCond.getConsequent());
             cond.setCondition(
                 Expr.binary(
                     BinaryOperation.AND,
                     cond.getCondition(),
                     innerCond.getCondition(),
                     cond.getCondition().getLocation()));
             --i;
           } else if (cond.getAlternative().size() != 1
               || !(cond.getAlternative().get(0) instanceof ConditionalStatement)) {
             cond.setCondition(ExprOptimizer.invert(cond.getCondition()));
             cond.getConsequent().clear();
             cond.getConsequent().addAll(cond.getAlternative());
             cond.getAlternative().clear();
             cond.getAlternative().add(innerCond);
             --i;
           }
         }
       }
     } else if (stmt instanceof BlockStatement) {
       BlockStatement nestedBlock = (BlockStatement) stmt;
       eliminateRedundantBreaks(nestedBlock.getBody(), nestedBlock);
     } else if (stmt instanceof WhileStatement) {
       WhileStatement whileStmt = (WhileStatement) stmt;
       eliminateRedundantBreaks(whileStmt.getBody(), null);
     } else if (stmt instanceof SwitchStatement) {
       SwitchStatement switchStmt = (SwitchStatement) stmt;
       for (SwitchClause clause : switchStmt.getClauses()) {
         eliminateRedundantBreaks(clause.getBody(), null);
       }
       eliminateRedundantBreaks(switchStmt.getDefaultClause(), null);
     }
   }
 }