Java Code Examples for com.google.javascript.jscomp.graph.DiGraph.DiGraphNode#getValue()

The following examples show how to use com.google.javascript.jscomp.graph.DiGraph.DiGraphNode#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InstrumentFunctions.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @returns true if all paths from block must exit with an explicit return.
 */
private boolean allPathsReturn(Node block) {
  // Computes the control flow graph.
  ControlFlowAnalysis cfa = new ControlFlowAnalysis(
      compiler, false, false);
  cfa.process(null, block);
  ControlFlowGraph<Node> cfg = cfa.getCfg();

  Node returnPathsParent = cfg.getImplicitReturn().getValue();
  for (DiGraphNode<Node, Branch> pred :
    cfg.getDirectedPredNodes(returnPathsParent)) {
    Node n = pred.getValue();
    if (!n.isReturn()) {
      return false;
    }
  }
  return true;
}
 
Example 2
Source File: Closure_88_DeadAssignmentsElimination_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Try to remove useless assignments from a control flow graph that has been
 * annotated with liveness information.
 *
 * @param t The node traversal.
 * @param cfg The control flow graph of the program annotated with liveness
 *        information.
 */
private void tryRemoveDeadAssignments(NodeTraversal t,
    ControlFlowGraph<Node> cfg) {
  Iterable<DiGraphNode<Node, Branch>> nodes = cfg.getDirectedGraphNodes();

  for (DiGraphNode<Node, Branch> cfgNode : nodes) {
    FlowState<LiveVariableLattice> state =
        cfgNode.getAnnotation();
    Node n = cfgNode.getValue();
    if (n == null) {
      continue;
    }
    switch (n.getType()) {
      case Token.IF:
      case Token.WHILE:
      case Token.DO:
        tryRemoveAssignment(t, NodeUtil.getConditionExpression(n), state);
        continue;
      case Token.FOR:
        if (!NodeUtil.isForIn(n)) {
          tryRemoveAssignment(
              t, NodeUtil.getConditionExpression(n), state);
        }
        continue;
      case Token.SWITCH:
      case Token.CASE:
      case Token.RETURN:
        if (n.hasChildren()) {
          tryRemoveAssignment(t, n.getFirstChild(), state);
        }
        continue;
      // TODO(user): case Token.VAR: Remove var a=1;a=2;.....
    }

    tryRemoveAssignment(t, n, state);
  }
}
 
Example 3
Source File: Closure_88_DeadAssignmentsElimination_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Try to remove useless assignments from a control flow graph that has been
 * annotated with liveness information.
 *
 * @param t The node traversal.
 * @param cfg The control flow graph of the program annotated with liveness
 *        information.
 */
private void tryRemoveDeadAssignments(NodeTraversal t,
    ControlFlowGraph<Node> cfg) {
  Iterable<DiGraphNode<Node, Branch>> nodes = cfg.getDirectedGraphNodes();

  for (DiGraphNode<Node, Branch> cfgNode : nodes) {
    FlowState<LiveVariableLattice> state =
        cfgNode.getAnnotation();
    Node n = cfgNode.getValue();
    if (n == null) {
      continue;
    }
    switch (n.getType()) {
      case Token.IF:
      case Token.WHILE:
      case Token.DO:
        tryRemoveAssignment(t, NodeUtil.getConditionExpression(n), state);
        continue;
      case Token.FOR:
        if (!NodeUtil.isForIn(n)) {
          tryRemoveAssignment(
              t, NodeUtil.getConditionExpression(n), state);
        }
        continue;
      case Token.SWITCH:
      case Token.CASE:
      case Token.RETURN:
        if (n.hasChildren()) {
          tryRemoveAssignment(t, n.getFirstChild(), state);
        }
        continue;
      // TODO(user): case Token.VAR: Remove var a=1;a=2;.....
    }

    tryRemoveAssignment(t, n, state);
  }
}
 
Example 4
Source File: Closure_76_DeadAssignmentsElimination_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Try to remove useless assignments from a control flow graph that has been
 * annotated with liveness information.
 *
 * @param t The node traversal.
 * @param cfg The control flow graph of the program annotated with liveness
 *        information.
 */
private void tryRemoveDeadAssignments(NodeTraversal t,
    ControlFlowGraph<Node> cfg) {
  Iterable<DiGraphNode<Node, Branch>> nodes = cfg.getDirectedGraphNodes();

  for (DiGraphNode<Node, Branch> cfgNode : nodes) {
    FlowState<LiveVariableLattice> state =
        cfgNode.getAnnotation();
    Node n = cfgNode.getValue();
    if (n == null) {
      continue;
    }
    switch (n.getType()) {
      case Token.IF:
      case Token.WHILE:
      case Token.DO:
        tryRemoveAssignment(t, NodeUtil.getConditionExpression(n), state);
        continue;
      case Token.FOR:
        if (!NodeUtil.isForIn(n)) {
          tryRemoveAssignment(
              t, NodeUtil.getConditionExpression(n), state);
        }
        continue;
      case Token.SWITCH:
      case Token.CASE:
      case Token.RETURN:
        if (n.hasChildren()) {
          tryRemoveAssignment(t, n.getFirstChild(), state);
        }
        continue;
      // TODO(user): case Token.VAR: Remove var a=1;a=2;.....
    }

    tryRemoveAssignment(t, n, state);
  }
}
 
Example 5
Source File: Closure_76_DeadAssignmentsElimination_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Try to remove useless assignments from a control flow graph that has been
 * annotated with liveness information.
 *
 * @param t The node traversal.
 * @param cfg The control flow graph of the program annotated with liveness
 *        information.
 */
private void tryRemoveDeadAssignments(NodeTraversal t,
    ControlFlowGraph<Node> cfg) {
  Iterable<DiGraphNode<Node, Branch>> nodes = cfg.getDirectedGraphNodes();

  for (DiGraphNode<Node, Branch> cfgNode : nodes) {
    FlowState<LiveVariableLattice> state =
        cfgNode.getAnnotation();
    Node n = cfgNode.getValue();
    if (n == null) {
      continue;
    }
    switch (n.getType()) {
      case Token.IF:
      case Token.WHILE:
      case Token.DO:
        tryRemoveAssignment(t, NodeUtil.getConditionExpression(n), state);
        continue;
      case Token.FOR:
        if (!NodeUtil.isForIn(n)) {
          tryRemoveAssignment(
              t, NodeUtil.getConditionExpression(n), state);
        }
        continue;
      case Token.SWITCH:
      case Token.CASE:
      case Token.RETURN:
        if (n.hasChildren()) {
          tryRemoveAssignment(t, n.getFirstChild(), state);
        }
        continue;
      // TODO(user): case Token.VAR: Remove var a=1;a=2;.....
    }

    tryRemoveAssignment(t, n, state);
  }
}
 
Example 6
Source File: DeadAssignmentsElimination.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Try to remove useless assignments from a control flow graph that has been
 * annotated with liveness information.
 *
 * @param t The node traversal.
 * @param cfg The control flow graph of the program annotated with liveness
 *        information.
 */
private void tryRemoveDeadAssignments(NodeTraversal t,
    ControlFlowGraph<Node> cfg) {
  Iterable<DiGraphNode<Node, Branch>> nodes = cfg.getDirectedGraphNodes();

  for (DiGraphNode<Node, Branch> cfgNode : nodes) {
    FlowState<LiveVariableLattice> state =
        cfgNode.getAnnotation();
    Node n = cfgNode.getValue();
    if (n == null) {
      continue;
    }
    switch (n.getType()) {
      case Token.IF:
      case Token.WHILE:
      case Token.DO:
        tryRemoveAssignment(t, NodeUtil.getConditionExpression(n), state);
        continue;
      case Token.FOR:
        if (!NodeUtil.isForIn(n)) {
          tryRemoveAssignment(
              t, NodeUtil.getConditionExpression(n), state);
        }
        continue;
      case Token.SWITCH:
      case Token.CASE:
      case Token.RETURN:
        if (n.hasChildren()) {
          tryRemoveAssignment(t, n.getFirstChild(), state);
        }
        continue;
      // TODO(user): case Token.VAR: Remove var a=1;a=2;.....
    }

    tryRemoveAssignment(t, n, state);
  }
}
 
Example 7
Source File: FixedPointGraphTraversal.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compute a fixed point for the given graph, entering from the given nodes.
 * @param graph The graph to traverse.
 * @param entrySet The nodes to begin traversing from.
 */
public void computeFixedPoint(DiGraph<N, E> graph, Set<N> entrySet) {
  int cycleCount = 0;
  long nodeCount = graph.getNodes().size();

  // Choose a bail-out heuristically in case the computation
  // doesn't converge.
  long maxIterations = Math.max(nodeCount * nodeCount * nodeCount, 100);

  // Use a LinkedHashSet, so that the traversal is deterministic.
  LinkedHashSet<DiGraphNode<N, E>> workSet =
      Sets.newLinkedHashSet();
  for (N n : entrySet) {
    workSet.add(graph.getDirectedGraphNode(n));
  }
  for (; !workSet.isEmpty() && cycleCount < maxIterations; cycleCount++) {
    // For every out edge in the workSet, traverse that edge. If that
    // edge updates the state of the graph, then add the destination
    // node to the resultSet, so that we can update all of its out edges
    // on the next iteration.
    DiGraphNode<N, E> source = workSet.iterator().next();
    N sourceValue = source.getValue();

    workSet.remove(source);

    List<DiGraphEdge<N, E>> outEdges = source.getOutEdges();
    for (DiGraphEdge<N, E> edge : outEdges) {
      N destNode = edge.getDestination().getValue();
      if (callback.traverseEdge(sourceValue, edge.getValue(), destNode)) {
        workSet.add(edge.getDestination());
      }
    }
  }

  Preconditions.checkState(cycleCount != maxIterations,
      NON_HALTING_ERROR_MSG);
}