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

The following examples show how to use com.google.javascript.rhino.Node#addChildrenToBack() . 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: SpecializeModule.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds dummy variable declarations for all the function declarations we've
 * removed to the end of the initial module.
 *
 * We do this to make {@link VarCheck} happy, since it requires variables to
 * be declared before they are used in the whole program AST and doesn't
 * like it when they are declared multiple times.
 *
 * TODO(dcc): Be smarter about whether we need a VAR here or not.
 */
private void addDummyVarDeclarationsToInitialModule(JSModule module) {
  for (Node modifiedFunction :
    functionInfoBySpecializedFunctionNode.keySet()) {
   if (specializationState.getRemovedFunctions().contains(modifiedFunction)) {
     OriginalFunctionInformation originalInfo =
       functionInfoBySpecializedFunctionNode.get(modifiedFunction);

     if (originalInfo.name != null && originalInfo.originalWasDeclaration()) {
       Node block = specializationState.removedFunctionToBlock.get(
           modifiedFunction);

       // Declaring block might be null if no fix-up declarations is needed.
       // For example, InlineFunction can inline an anonymous function call or
       // anything with prototype property requires no dummy declaration
       // fix-ups afterward.
       if (block != null) {
         Node originalRoot = specializedInputRootsByOriginal.get(block);
         block.addChildrenToBack(originalInfo.generateDummyDeclaration());
       }
     }
   }
  }
}
 
Example 2
Source File: jKali_003_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Add a finally block if one does not exist.
 */
static void maybeAddFinally(Node tryNode) {
  Preconditions.checkState(tryNode.isTry());
  if (!NodeUtil.hasFinally(tryNode)) {
    tryNode.addChildrenToBack(IR.block().srcref(tryNode));
  }
}
 
Example 3
Source File: Closure_60_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/** Creates function name(params_0, ..., params_n) { body }. */
public static Node newFunctionNode(String name, List<Node> params,
    Node body, int lineno, int charno) {
  Node parameterParen = new Node(Token.LP, lineno, charno);
  for (Node param : params) {
    parameterParen.addChildToBack(param);
  }
  Node function = new Node(Token.FUNCTION, lineno, charno);
  function.addChildrenToBack(
      Node.newString(Token.NAME, name, lineno, charno));
  function.addChildToBack(parameterParen);
  function.addChildToBack(body);
  return function;
}
 
Example 4
Source File: Closure_61_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/** Creates function name(params_0, ..., params_n) { body }. */
public static Node newFunctionNode(String name, List<Node> params,
    Node body, int lineno, int charno) {
  Node parameterParen = new Node(Token.LP, lineno, charno);
  for (Node param : params) {
    parameterParen.addChildToBack(param);
  }
  Node function = new Node(Token.FUNCTION, lineno, charno);
  function.addChildrenToBack(
      Node.newString(Token.NAME, name, lineno, charno));
  function.addChildToBack(parameterParen);
  function.addChildToBack(body);
  return function;
}
 
Example 5
Source File: Closure_86_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/** Creates function name(params_0, ..., params_n) { body }. */
public static Node newFunctionNode(String name, List<Node> params,
    Node body, int lineno, int charno) {
  Node parameterParen = new Node(Token.LP, lineno, charno);
  for (Node param : params) {
    parameterParen.addChildToBack(param);
  }
  Node function = new Node(Token.FUNCTION, lineno, charno);
  function.addChildrenToBack(
      Node.newString(Token.NAME, name, lineno, charno));
  function.addChildToBack(parameterParen);
  function.addChildToBack(body);
  return function;
}
 
Example 6
Source File: Closure_86_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/** Creates function name(params_0, ..., params_n) { body }. */
public static Node newFunctionNode(String name, List<Node> params,
    Node body, int lineno, int charno) {
  Node parameterParen = new Node(Token.LP, lineno, charno);
  for (Node param : params) {
    parameterParen.addChildToBack(param);
  }
  Node function = new Node(Token.FUNCTION, lineno, charno);
  function.addChildrenToBack(
      Node.newString(Token.NAME, name, lineno, charno));
  function.addChildToBack(parameterParen);
  function.addChildToBack(body);
  return function;
}
 
Example 7
Source File: Closure_60_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/** Creates function name(params_0, ..., params_n) { body }. */
public static Node newFunctionNode(String name, List<Node> params,
    Node body, int lineno, int charno) {
  Node parameterParen = new Node(Token.LP, lineno, charno);
  for (Node param : params) {
    parameterParen.addChildToBack(param);
  }
  Node function = new Node(Token.FUNCTION, lineno, charno);
  function.addChildrenToBack(
      Node.newString(Token.NAME, name, lineno, charno));
  function.addChildToBack(parameterParen);
  function.addChildToBack(body);
  return function;
}
 
Example 8
Source File: Closure_10_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Add a finally block if one does not exist.
 */
static void maybeAddFinally(Node tryNode) {
  Preconditions.checkState(tryNode.isTry());
  if (!NodeUtil.hasFinally(tryNode)) {
    tryNode.addChildrenToBack(IR.block().srcref(tryNode));
  }
}
 
Example 9
Source File: Closure_94_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/** Creates function name(params_0, ..., params_n) { body }. */
public static Node newFunctionNode(String name, List<Node> params,
    Node body, int lineno, int charno) {
  Node parameterParen = new Node(Token.LP, lineno, charno);
  for (Node param : params) {
    parameterParen.addChildToBack(param);
  }
  Node function = new Node(Token.FUNCTION, lineno, charno);
  function.addChildrenToBack(
      Node.newString(Token.NAME, name, lineno, charno));
  function.addChildToBack(parameterParen);
  function.addChildToBack(body);
  return function;
}
 
Example 10
Source File: CombinedCompilerPassTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a Node tree with the post-order traversal a b c d e f g h i j k l m
 * and the in-order traversal m d a b c h e f g l i j k:
 *
 *                                   m
 *                         ,---------|---------.
 *                         d         h         l
 *                      ,--|--.   ,--|--.   ,--|--.
 *                      a  b  c   e  f  g   i  j  k
 *
 */
private static Node createPostOrderAlphabet() {
  Node a = Node.newString("a");
  Node b = Node.newString("b");
  Node c = Node.newString("c");
  Node d = Node.newString("d");
  Node e = Node.newString("e");
  Node f = Node.newString("f");
  Node g = Node.newString("g");
  Node h = Node.newString("h");
  Node i = Node.newString("i");
  Node j = Node.newString("j");
  Node k = Node.newString("k");
  Node l = Node.newString("l");
  Node m = Node.newString("m");

  d.addChildToBack(a);
  d.addChildToBack(b);
  d.addChildToBack(c);

  h.addChildrenToBack(e);
  h.addChildrenToBack(f);
  h.addChildrenToBack(g);

  l.addChildToBack(i);
  l.addChildToBack(j);
  l.addChildToBack(k);

  m.addChildToBack(d);
  m.addChildToBack(h);
  m.addChildToBack(l);

  return m;
}
 
Example 11
Source File: Cardumen_0087_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Add a finally block if one does not exist.
 */
static void maybeAddFinally(Node tryNode) {
  Preconditions.checkState(tryNode.isTry());
  if (!NodeUtil.hasFinally(tryNode)) {
    tryNode.addChildrenToBack(IR.block().srcref(tryNode));
  }
}
 
Example 12
Source File: Closure_94_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/** Creates function name(params_0, ..., params_n) { body }. */
public static Node newFunctionNode(String name, List<Node> params,
    Node body, int lineno, int charno) {
  Node parameterParen = new Node(Token.LP, lineno, charno);
  for (Node param : params) {
    parameterParen.addChildToBack(param);
  }
  Node function = new Node(Token.FUNCTION, lineno, charno);
  function.addChildrenToBack(
      Node.newString(Token.NAME, name, lineno, charno));
  function.addChildToBack(parameterParen);
  function.addChildToBack(body);
  return function;
}
 
Example 13
Source File: Cardumen_0014_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Add a finally block if one does not exist.
 */
static void maybeAddFinally(Node tryNode) {
  Preconditions.checkState(tryNode.isTry());
  if (!NodeUtil.hasFinally(tryNode)) {
    tryNode.addChildrenToBack(IR.block().srcref(tryNode));
  }
}
 
Example 14
Source File: Cardumen_00149_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Add a finally block if one does not exist.
 */
static void maybeAddFinally(Node tryNode) {
  Preconditions.checkState(tryNode.isTry());
  if (!NodeUtil.hasFinally(tryNode)) {
    tryNode.addChildrenToBack(IR.block().srcref(tryNode));
  }
}
 
Example 15
Source File: Cardumen_00149_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Add a finally block if one does not exist.
 */
static void maybeAddFinally(Node tryNode) {
  Preconditions.checkState(tryNode.isTry());
  if (!NodeUtil.hasFinally(tryNode)) {
    tryNode.addChildrenToBack(IR.block().srcref(tryNode));
  }
}
 
Example 16
Source File: Cardumen_00200_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Add a finally block if one does not exist.
 */
static void maybeAddFinally(Node tryNode) {
  Preconditions.checkState(tryNode.isTry());
  if (!NodeUtil.hasFinally(tryNode)) {
    tryNode.addChildrenToBack(IR.block().srcref(tryNode));
  }
}
 
Example 17
Source File: Closure_75_NodeUtil_t.java    From coming with MIT License 5 votes vote down vote up
/** Creates function name(params_0, ..., params_n) { body }. */
public static Node newFunctionNode(String name, List<Node> params,
    Node body, int lineno, int charno) {
  Node parameterParen = new Node(Token.LP, lineno, charno);
  for (Node param : params) {
    parameterParen.addChildToBack(param);
  }
  Node function = new Node(Token.FUNCTION, lineno, charno);
  function.addChildrenToBack(
      Node.newString(Token.NAME, name, lineno, charno));
  function.addChildToBack(parameterParen);
  function.addChildToBack(body);
  return function;
}
 
Example 18
Source File: 1_NodeUtil.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/** Creates function name(params_0, ..., params_n) { body }. */
public static Node newFunctionNode(String name, List<Node> params,
    Node body, int lineno, int charno) {
  Node parameterParen = new Node(Token.LP, lineno, charno);
  for (Node param : params) {
    parameterParen.addChildToBack(param);
  }
  Node function = new Node(Token.FUNCTION, lineno, charno);
  function.addChildrenToBack(
      Node.newString(Token.NAME, name, lineno, charno));
  function.addChildToBack(parameterParen);
  function.addChildToBack(body);
  return function;
}
 
Example 19
Source File: FunctionToBlockMutator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 *  Convert returns to assignments and breaks, as needed.
 *  For example, with a labelName of 'foo':
 *    {
 *      return a;
 *    }
 *  becomes:
 *    foo: {
 *      a;
 *      break foo;
 *    }
 *  or
 *    foo: {
 *      resultName = a;
 *      break foo;
 *    }
 *
 * @param resultMustBeSet Whether the result must always be set to a value.
 * @return The node containing the transformed block, this may be different
 *     than the passed in node 'block'.
 */
private static Node replaceReturns(
    Node block, String resultName, String labelName,
    boolean resultMustBeSet) {
  Preconditions.checkNotNull(block);
  Preconditions.checkNotNull(labelName);

  Node root = block;

  boolean hasReturnAtExit = false;
  int returnCount = NodeUtil.getNodeTypeReferenceCount(
      block, Token.RETURN, new NodeUtil.MatchShallowStatement());
  if (returnCount > 0) {
    hasReturnAtExit = hasReturnAtExit(block);
    // TODO(johnlenz): Simpler not to special case this,
    // and let it be optimized later.
    if (hasReturnAtExit) {
      convertLastReturnToStatement(block, resultName);
      returnCount--;
    }

    if (returnCount > 0) {
      // A label and breaks are needed.

      // Add the breaks
      replaceReturnWithBreak(block, null, resultName, labelName);

      // Add label
      Node name = IR.labelName(labelName).srcref(block);
      Node label = IR.label(name, block).srcref(block);

      Node newRoot = IR.block().srcref(block);
      newRoot.addChildrenToBack(label);


      // The label is now the root.
      root = newRoot;
    }
  }

  // If there wasn't an return at the end of the function block, and we need
  // a result, add one to the block.
  if (resultMustBeSet && !hasReturnAtExit && resultName != null) {
    addDummyAssignment(block, resultName);
  }

  return root;
}
 
Example 20
Source File: Closure_72_FunctionToBlockMutator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 *  Convert returns to assignments and breaks, as needed.
 *  For example, with a lableName of 'foo':
 *    {
 *      return a;
 *    }
 *  becomes:
 *    foo: {
 *      a;
 *      break foo;
 *    }
 *  or
 *    foo: {
 *      resultName = a;
 *      break foo;
 *    }
 *
 * @param resultMustBeSet Whether the result must always be set to a value.
 * @return The node containing the transformed block, this may be different
 *     than the passed in node 'block'.
 */
private static Node replaceReturns(
    Node block, String resultName, String labelName,
    boolean resultMustBeSet) {
  Preconditions.checkNotNull(block);
  Preconditions.checkNotNull(labelName);

  Node root = block;

  boolean hasReturnAtExit = false;
  int returnCount = NodeUtil.getNodeTypeReferenceCount(
      block, Token.RETURN, new NodeUtil.MatchShallowStatement());
  if (returnCount > 0) {
    hasReturnAtExit = hasReturnAtExit(block);
    // TODO(johnlenz): Simpler not to special case this,
    // and let it be optimized later.
    if (hasReturnAtExit) {
      convertLastReturnToStatement(block, resultName);
      returnCount--;
    }

    if (returnCount > 0) {
      // A label and breaks are needed.

      // Add the breaks
      replaceReturnWithBreak(block, null, resultName, labelName);

      // Add label
      Node label = new Node(Token.LABEL).copyInformationFrom(block);
      Node name = Node.newString(Token.LABEL_NAME, labelName)
          .copyInformationFrom(block);
      label.addChildToFront(name);
      label.addChildToBack(block);

      Node newRoot = new Node(Token.BLOCK).copyInformationFrom(block);
      newRoot.addChildrenToBack(label);


      // The label is now the root.
      root = newRoot;
    }
  }

  // If there wasn't an return at the end of the function block, and we need
  // a result, add one to the block.
  if (resultMustBeSet && !hasReturnAtExit && resultName != null) {
    addDummyAssignment(block, resultName);
  }

  return root;
}