コード例 #1
0
  public void visitIfStatement(GrIfStatement ifStatement) {
    InstructionImpl ifInstruction = startNode(ifStatement);

    final GrCondition condition = ifStatement.getCondition();
    final GrStatement thenBranch = ifStatement.getThenBranch();
    final GrStatement elseBranch = ifStatement.getElseBranch();

    InstructionImpl conditionEnd = null;
    InstructionImpl thenEnd = null;
    InstructionImpl elseEnd = null;

    if (condition != null) {
      condition.accept(this);
      conditionEnd = myHead;
    }

    List<GotoInstruction> negations = collectAndRemoveAllPendingNegations(ifStatement);

    if (thenBranch != null) {
      thenBranch.accept(this);
      handlePossibleReturn(thenBranch);
      thenEnd = myHead;
      interruptFlow();
      readdPendingEdge(ifStatement);
    }

    myHead = reduceAllNegationsIntoInstruction(ifStatement, negations);
    if (myHead == null && conditionEnd != null) {
      myHead = conditionEnd;
    }
    if (elseBranch != null) {
      elseBranch.accept(this);
      handlePossibleReturn(elseBranch);
      elseEnd = myHead;
      interruptFlow();
    }

    if (thenBranch != null || elseBranch != null) {
      if (thenEnd != null || elseEnd != null || elseBranch == null) {
        final InstructionImpl end = new IfEndInstruction(ifStatement);
        addNode(end);

        if (thenEnd != null) {
          addEdge(thenEnd, end);
        }

        if (elseEnd != null) {
          addEdge(elseEnd, end);
        } else if (elseBranch == null) {
          addEdge(conditionEnd != null ? conditionEnd : ifInstruction, end);
        }
      }
    }

    finishNode(ifInstruction);
  }
コード例 #2
0
 private static boolean ifStatementsAreEquivalent(
     @NotNull GrIfStatement statement1, @NotNull GrIfStatement statement2) {
   final GrExpression condition1 = statement1.getCondition();
   final GrExpression condition2 = statement2.getCondition();
   final GrStatement thenBranch1 = statement1.getThenBranch();
   final GrStatement thenBranch2 = statement2.getThenBranch();
   final GrStatement elseBranch1 = statement1.getElseBranch();
   final GrStatement elseBranch2 = statement2.getElseBranch();
   return expressionsAreEquivalent(condition1, condition2)
       && statementsAreEquivalent(thenBranch1, thenBranch2)
       && statementsAreEquivalent(elseBranch1, elseBranch2);
 }