Java Code Examples for com.google.javascript.rhino.Node#isSyntheticBlock()

The following examples show how to use com.google.javascript.rhino.Node#isSyntheticBlock() . 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: Closure_105_FoldConstants_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Try removing unneeded block nodes and their useless children
 */
void tryFoldBlock(NodeTraversal t, Node n, Node parent) {
  // Remove any useless children
  for (Node c = n.getFirstChild(); c != null; ) {
    Node next = c.getNext();  // save c.next, since 'c' may be removed
    if (!NodeUtil.mayHaveSideEffects(c)) {
      n.removeChild(c);  // lazy kids
      t.getCompiler().reportCodeChange();
    }
    c = next;
  }

  if (n.isSyntheticBlock() || parent == null) {
    return;
  }

  // Try to remove the block.
  if (NodeUtil.tryMergeBlock(n)) {
    t.getCompiler().reportCodeChange();
  }
}
 
Example 2
Source File: Closure_105_FoldConstants_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Try removing unneeded block nodes and their useless children
 */
void tryFoldBlock(NodeTraversal t, Node n, Node parent) {
  // Remove any useless children
  for (Node c = n.getFirstChild(); c != null; ) {
    Node next = c.getNext();  // save c.next, since 'c' may be removed
    if (!NodeUtil.mayHaveSideEffects(c)) {
      n.removeChild(c);  // lazy kids
      t.getCompiler().reportCodeChange();
    }
    c = next;
  }

  if (n.isSyntheticBlock() || parent == null) {
    return;
  }

  // Try to remove the block.
  if (NodeUtil.tryMergeBlock(n)) {
    t.getCompiler().reportCodeChange();
  }
}
 
Example 3
Source File: AbstractPeepholeOptimization.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Check if the specified node is null or is still in the AST.
 */
@VisibleForTesting
static Node validateResult(Node n) {
  done: {
    if (n != null && !n.isScript()
        && (!n.isBlock() || !n.isSyntheticBlock())) {
      for (Node parent : n.getAncestors()) {
        if (parent.isScript()) {
          break done;
        }
      }
      Preconditions.checkState(false);
    }
  }
  return n;
}
 
Example 4
Source File: 1_ControlFlowAnalysis.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
private void handleStmtList(Node node) {
  Node parent = node.getParent();
  // Special case, don't add a block of empty CATCH block to the graph.
  if (node.isBlock() && parent != null &&
      parent.isTry() &&
      NodeUtil.getCatchBlock(parent) == node &&
      !NodeUtil.hasCatchHandler(node)) {
    return;
  }

  // A block transfer control to its first child if it is not empty.
  Node child = node.getFirstChild();

  // Function declarations are skipped since control doesn't go into that
  // function (unless it is called)
  while (child != null && child.isFunction()) {
    child = child.getNext();
  }

  if (child != null) {
    createEdge(node, Branch.UNCOND, computeFallThrough(child));
  } else {
    createEdge(node, Branch.UNCOND, computeFollowNode(node, this));
  }

  // Synthetic blocks
  if (parent != null) {
    switch (parent.getType()) {
      case Token.DEFAULT_CASE:
      case Token.CASE:
      case Token.TRY:
        break;
      default:
        if (node.isBlock() && node.isSyntheticBlock()) {
          createEdge(node, Branch.SYN_BLOCK, computeFollowNode(node, this));
        }
        break;
    }
  }
}
 
Example 5
Source File: 1_ControlFlowAnalysis.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
private void handleStmtList(Node node) {
  Node parent = node.getParent();
  // Special case, don't add a block of empty CATCH block to the graph.
  if (node.isBlock() && parent != null &&
      parent.isTry() &&
      NodeUtil.getCatchBlock(parent) == node &&
      !NodeUtil.hasCatchHandler(node)) {
    return;
  }

  // A block transfer control to its first child if it is not empty.
  Node child = node.getFirstChild();

  // Function declarations are skipped since control doesn't go into that
  // function (unless it is called)
  while (child != null && child.isFunction()) {
    child = child.getNext();
  }

  if (child != null) {
    createEdge(node, Branch.UNCOND, computeFallThrough(child));
  } else {
    createEdge(node, Branch.UNCOND, computeFollowNode(node, this));
  }

  // Synthetic blocks
  if (parent != null) {
    switch (parent.getType()) {
      case Token.DEFAULT_CASE:
      case Token.CASE:
      case Token.TRY:
        break;
      default:
        if (node.isBlock() && node.isSyntheticBlock()) {
          createEdge(node, Branch.SYN_BLOCK, computeFollowNode(node, this));
        }
        break;
    }
  }
}
 
Example 6
Source File: Closure_14_ControlFlowAnalysis_t.java    From coming with MIT License 5 votes vote down vote up
private void handleStmtList(Node node) {
  Node parent = node.getParent();
  // Special case, don't add a block of empty CATCH block to the graph.
  if (node.isBlock() && parent != null &&
      parent.isTry() &&
      NodeUtil.getCatchBlock(parent) == node &&
      !NodeUtil.hasCatchHandler(node)) {
    return;
  }

  // A block transfer control to its first child if it is not empty.
  Node child = node.getFirstChild();

  // Function declarations are skipped since control doesn't go into that
  // function (unless it is called)
  while (child != null && child.isFunction()) {
    child = child.getNext();
  }

  if (child != null) {
    createEdge(node, Branch.UNCOND, computeFallThrough(child));
  } else {
    createEdge(node, Branch.UNCOND, computeFollowNode(node, this));
  }

  // Synthetic blocks
  if (parent != null) {
    switch (parent.getType()) {
      case Token.DEFAULT_CASE:
      case Token.CASE:
      case Token.TRY:
        break;
      default:
        if (node.isBlock() && node.isSyntheticBlock()) {
          createEdge(node, Branch.SYN_BLOCK, computeFollowNode(node, this));
        }
        break;
    }
  }
}
 
Example 7
Source File: Closure_14_ControlFlowAnalysis_s.java    From coming with MIT License 5 votes vote down vote up
private void handleStmtList(Node node) {
  Node parent = node.getParent();
  // Special case, don't add a block of empty CATCH block to the graph.
  if (node.isBlock() && parent != null &&
      parent.isTry() &&
      NodeUtil.getCatchBlock(parent) == node &&
      !NodeUtil.hasCatchHandler(node)) {
    return;
  }

  // A block transfer control to its first child if it is not empty.
  Node child = node.getFirstChild();

  // Function declarations are skipped since control doesn't go into that
  // function (unless it is called)
  while (child != null && child.isFunction()) {
    child = child.getNext();
  }

  if (child != null) {
    createEdge(node, Branch.UNCOND, computeFallThrough(child));
  } else {
    createEdge(node, Branch.UNCOND, computeFollowNode(node, this));
  }

  // Synthetic blocks
  if (parent != null) {
    switch (parent.getType()) {
      case Token.DEFAULT_CASE:
      case Token.CASE:
      case Token.TRY:
        break;
      default:
        if (node.isBlock() && node.isSyntheticBlock()) {
          createEdge(node, Branch.SYN_BLOCK, computeFollowNode(node, this));
        }
        break;
    }
  }
}
 
Example 8
Source File: PeepholeRemoveDeadCode.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Try removing unneeded block nodes and their useless children
 */
Node tryOptimizeBlock(Node n) {
  // Remove any useless children
  for (Node c = n.getFirstChild(); c != null; ) {
    Node next = c.getNext();  // save c.next, since 'c' may be removed
    if (!isUnremovableNode(c) && !mayHaveSideEffects(c)) {
      // TODO(johnlenz): determine what this is actually removing. Candidates
      //    include: EMPTY nodes, control structures without children
      //    (removing infinite loops), empty try blocks.  What else?
      n.removeChild(c);  // lazy kids
      reportCodeChange();
    } else {
      tryOptimizeConditionalAfterAssign(c);
    }
    c = next;
  }

  if (n.isSyntheticBlock() ||  n.getParent() == null) {
    return n;
  }

  // Try to remove the block.
  if (NodeUtil.tryMergeBlock(n)) {
    reportCodeChange();
    return null;
  }

  return n;
}
 
Example 9
Source File: ControlFlowAnalysis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void handleStmtList(Node node) {
  Node parent = node.getParent();
  // Special case, don't add a block of empty CATCH block to the graph.
  if (node.isBlock() && parent != null &&
      parent.isTry() &&
      NodeUtil.getCatchBlock(parent) == node &&
      !NodeUtil.hasCatchHandler(node)) {
    return;
  }

  // A block transfer control to its first child if it is not empty.
  Node child = node.getFirstChild();

  // Function declarations are skipped since control doesn't go into that
  // function (unless it is called)
  while (child != null && child.isFunction()) {
    child = child.getNext();
  }

  if (child != null) {
    createEdge(node, Branch.UNCOND, computeFallThrough(child));
  } else {
    createEdge(node, Branch.UNCOND, computeFollowNode(node, this));
  }

  // Synthetic blocks
  if (parent != null) {
    switch (parent.getType()) {
      case Token.DEFAULT_CASE:
      case Token.CASE:
      case Token.TRY:
        break;
      default:
        if (node.isBlock() && node.isSyntheticBlock()) {
          createEdge(node, Branch.SYN_BLOCK, computeFollowNode(node, this));
        }
        break;
    }
  }
}
 
Example 10
Source File: Closure_103_ControlFlowAnalysis_s.java    From coming with MIT License 4 votes vote down vote up
private void handleStmtList(Node node) {
  Node parent = node.getParent();
  // Special case, don't add a block of empty CATCH block to the graph.
  if (node.getType() == Token.BLOCK && parent != null &&
      parent.getType() == Token.TRY &&
      NodeUtil.getCatchBlock(parent) == node &&
      !NodeUtil.hasCatchHandler(node)) {
    return;
  }

  // A block transfer control to its first child if it is not empty.
  Node child = node.getFirstChild();

  // Function declarations are skipped since control doesn't go into that
  // function (unless it is called)
  while (child != null && child.getType() == Token.FUNCTION) {
    child = child.getNext();
  }

  if (child != null) {
    createEdge(node, Branch.UNCOND, computeFallThrough(child));
  } else {
    createEdge(node, Branch.UNCOND, computeFollowNode(node));
  }

  // Synthetic blocks
  if (parent != null) {
    switch (parent.getType()) {
      case Token.DEFAULT:
      case Token.CASE:
      case Token.TRY:
        break;
      default:
        if (node.getType() == Token.BLOCK && node.isSyntheticBlock()) {
          Node next = node.getLastChild();
          if (next != null) {
            createEdge(node, Branch.SYN_BLOCK, computeFallThrough(next));
          }
        }
        break;
    }
  }
}
 
Example 11
Source File: Closure_103_ControlFlowAnalysis_t.java    From coming with MIT License 4 votes vote down vote up
private void handleStmtList(Node node) {
  Node parent = node.getParent();
  // Special case, don't add a block of empty CATCH block to the graph.
  if (node.getType() == Token.BLOCK && parent != null &&
      parent.getType() == Token.TRY &&
      NodeUtil.getCatchBlock(parent) == node &&
      !NodeUtil.hasCatchHandler(node)) {
    return;
  }

  // A block transfer control to its first child if it is not empty.
  Node child = node.getFirstChild();

  // Function declarations are skipped since control doesn't go into that
  // function (unless it is called)
  while (child != null && child.getType() == Token.FUNCTION) {
    child = child.getNext();
  }

  if (child != null) {
    createEdge(node, Branch.UNCOND, computeFallThrough(child));
  } else {
    createEdge(node, Branch.UNCOND, computeFollowNode(node));
  }

  // Synthetic blocks
  if (parent != null) {
    switch (parent.getType()) {
      case Token.DEFAULT:
      case Token.CASE:
      case Token.TRY:
        break;
      default:
        if (node.getType() == Token.BLOCK && node.isSyntheticBlock()) {
          Node next = node.getLastChild();
          if (next != null) {
            createEdge(node, Branch.SYN_BLOCK, computeFallThrough(next));
          }
        }
        break;
    }
  }
}
 
Example 12
Source File: PeepholeRemoveDeadCode.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Some nodes unremovable node don't have side-effects.
 */
private boolean isUnremovableNode(Node n) {
  return (n.isBlock() && n.isSyntheticBlock()) || n.isScript();
}