Java Code Examples for com.google.javascript.rhino.IR#assign()

The following examples show how to use com.google.javascript.rhino.IR#assign() . 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_23_PeepholeFoldConstants_s.java    From coming with MIT License 6 votes vote down vote up
private Node tryUnfoldAssignOp(Node n, Node left, Node right) {
  if (late) {
    return n;
  }

  if (!n.hasChildren() ||
      n.getFirstChild().getNext() != n.getLastChild()) {
    return n;
  }

  if (mayHaveSideEffects(left)) {
    return n;
  }

  // Tries to convert x += y -> x = x + y;
  int op = NodeUtil.getOpFromAssignmentOp(n);
  Node replacement = IR.assign(left.detachFromParent(),
      new Node(op, left.cloneTree(), right.detachFromParent())
          .srcref(n));
  n.getParent().replaceChild(n, replacement);
  reportCodeChange();

  return replacement;
}
 
Example 2
Source File: Closure_23_PeepholeFoldConstants_t.java    From coming with MIT License 6 votes vote down vote up
private Node tryUnfoldAssignOp(Node n, Node left, Node right) {
  if (late) {
    return n;
  }

  if (!n.hasChildren() ||
      n.getFirstChild().getNext() != n.getLastChild()) {
    return n;
  }

  if (mayHaveSideEffects(left)) {
    return n;
  }

  // Tries to convert x += y -> x = x + y;
  int op = NodeUtil.getOpFromAssignmentOp(n);
  Node replacement = IR.assign(left.detachFromParent(),
      new Node(op, left.cloneTree(), right.detachFromParent())
          .srcref(n));
  n.getParent().replaceChild(n, replacement);
  reportCodeChange();

  return replacement;
}
 
Example 3
Source File: RescopeGlobalSymbols.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void replaceSymbol(Node node, String name) {
  Node parent = node.getParent();
  Node replacement = IR.getprop(
      IR.name(globalSymbolNamespace).srcref(node),
      IR.string(name).srcref(node));
  replacement.srcref(node);
  if (node.hasChildren()) {
    // var declaration list: var a = 1, b = 2;
    Node assign = IR.assign(replacement,
        node.removeFirstChild());
    parent.replaceChild(node, assign);
  } else {
    parent.replaceChild(node, replacement);
  }
  compiler.reportCodeChange();
}
 
Example 4
Source File: PeepholeFoldConstants.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private Node tryUnfoldAssignOp(Node n, Node left, Node right) {
  if (late) {
    return n;
  }

  if (!n.hasChildren() ||
      n.getFirstChild().getNext() != n.getLastChild()) {
    return n;
  }

  if (mayHaveSideEffects(left)) {
    return n;
  }

  // Tries to convert x += y -> x = x + y;
  int op = NodeUtil.getOpFromAssignmentOp(n);
  Node replacement = IR.assign(left.detachFromParent(),
      new Node(op, left.cloneTree(), right.detachFromParent())
          .srcref(n));
  n.getParent().replaceChild(n, replacement);
  reportCodeChange();

  return replacement;
}
 
Example 5
Source File: SpecializeModule.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generates a definition of the original function that can be added as
 * a fixup in the modules that directly depend on the specialized module.
 *
 * <PRE>
 * The trick here is that even if the original function is declared as:
 *
 * function foo() {
 *   // stuff
 * }
 *
 * the fixup will have to be of the form
 *
 * foo = function() {
 *   // stuff
 * }
 * </PRE>
 *
 */
private Node generateFixupDefinition() {
  Node functionCopy = copiedOriginalFunction();

  Node nameNode;

  if (isAssignFunction) {
    nameNode =
       NodeUtil.newQualifiedNameNode(
           compiler.getCodingConvention(), name, functionCopy, name);
  } else {
    // Grab the name node from the original function and make that
    // function anonymous.
    nameNode = functionCopy.getFirstChild();
    functionCopy.replaceChild(nameNode,
        NodeUtil.newName(compiler.getCodingConvention(), "", nameNode));
  }

  Node assignment = IR.assign(nameNode, functionCopy);
  assignment.copyInformationFrom(functionCopy);

  return NodeUtil.newExpr(assignment);
}
 
Example 6
Source File: FunctionToBlockMutator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a valid statement Node containing an assignment to name of the
 * given expression.
 */
private static Node createAssignStatementNode(String name, Node expression) {
  // Create 'name = result-expression;' statement.
  // EXPR (ASSIGN (NAME, EXPRESSION))
  Node nameNode = IR.name(name);
  Node assign = IR.assign(nameNode, expression);
  return NodeUtil.newExpr(assign);
}
 
Example 7
Source File: Normalize.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove the parent VAR. There are three cases that need to be handled:
 *   1) "var a = b;" which is replaced with "a = b"
 *   2) "label:var a;" which is replaced with "label:;". Ideally, the
 *      label itself would be removed but that is not possible in the
 *      context in which "onRedeclaration" is called.
 *   3) "for (var a in b) ..." which is replaced with "for (a in b)..."
 *      Cases we don't need to handle are VARs with multiple children,
 *      which have already been split into separate declarations, so there
 *      is no need to handle that here, and "for (var a;;);", which has
 *      been moved out of the loop.
 *      The result of this is that in each case the parent node is replaced
 *      which is generally dangerous in a traversal but is fine here with
 *      the scope creator, as the next node of interest is the parent's
 *      next sibling.
 */
private void replaceVarWithAssignment(Node n, Node parent, Node gramps) {
  if (n.hasChildren()) {
    // The  *  is being initialize, preserve the new value.
    parent.removeChild(n);
    // Convert "var name = value" to "name = value"
    Node value = n.getFirstChild();
    n.removeChild(value);
    Node replacement = IR.assign(n, value);
    replacement.copyInformationFrom(parent);
    gramps.replaceChild(parent, NodeUtil.newExpr(replacement));
  } else {
    // It is an empty reference remove it.
    if (NodeUtil.isStatementBlock(gramps)) {
      gramps.removeChild(parent);
    } else if (gramps.isFor()) {
      // This is the "for (var a in b)..." case.  We don't need to worry
      // about initializers in "for (var a;;)..." as those are moved out
      // as part of the other normalizations.
      parent.removeChild(n);
      gramps.replaceChild(parent, n);
    } else {
      Preconditions.checkState(gramps.isLabel());
      // We should never get here. LABELs with a single VAR statement should
      // already have been normalized to have a BLOCK.
      throw new IllegalStateException("Unexpected LABEL");
    }
  }
  reportCodeChange("Duplicate VAR declaration");
}
 
Example 8
Source File: GroupVariableDeclarations.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Attempts to collapse groupVar. This can only happen if groupVar has at most
 * one variable initialization (it may have multiple variable declarations).
 * If successful, then detaches groupVar's children and appends them to
 * firstVar
 *
 * @param firstVar The first VAR {@code Node} in that scope. This is the node
 *                 that we want to collapse groupVar into
 * @param groupVar The VAR {@code Node} that we want to try collapsing
 *                 into the first VAR node of that scope
 */
private void applyGroupingToVar(Node firstVar, Node groupVar) {
  Node child = groupVar.getFirstChild();
  // if some variable is initialized, then the corresponding NAME node will be
  // stored here
  Node initializedName = null;
  while (child != null) {
    if (child.hasChildren()) {
      // check that no more than one var is initialized
      if (initializedName != null) {
        return;
      }
      initializedName = child;
    }
    child = child.getNext();
  }

  // we will be modifying the groupVar subtree so get its parent
  Node groupVarParent = groupVar.getParent();


  if (initializedName != null) {
    if (NodeUtil.isForIn(groupVarParent)) {
      // The target of the for-in expression must be an assignable expression.
      return;
    }

    // we have an initialized var in the VAR node. We will replace the
    // VAR node with an assignment.

    // first create a detached childless clone of initializedName.
    Node clone = initializedName.cloneNode();
    // replace
    groupVar.replaceChild(initializedName, clone);
    // add the assignment now.
    Node initializedVal = initializedName.removeFirstChild();
    Node assignmentNode = IR.assign(initializedName, initializedVal);
    if (groupVarParent.isFor()) {
      // Handle For and For-In Loops specially. For these, we do not need
      // to construct an EXPR_RESULT node.
      groupVarParent.replaceChild(groupVar, assignmentNode);
    } else {
      Node exprNode = NodeUtil.newExpr(assignmentNode);
      groupVarParent.replaceChild(groupVar, exprNode);
    }
  } else {
    // There is no initialized var. But we need to handle FOR and
    // FOR-IN loops specially
    if (groupVarParent.isFor()) {
      if (NodeUtil.isForIn(groupVarParent)) {
        // In For-In loop, we replace the VAR node with a NAME node
        Node nameNodeClone = groupVar.getFirstChild().cloneNode();
        groupVarParent.replaceChild(groupVar, nameNodeClone);
      } else {
        // In For loop, we replace the VAR node with an EMPTY node
        Node emptyNode = IR.empty();
        groupVarParent.replaceChild(groupVar, emptyNode);
      }
    } else {
      // we can safely remove the VAR node
      groupVarParent.removeChild(groupVar);
    }
  }

  Node children = groupVar.removeChildren();
  firstVar.addChildrenToBack(children);

  compiler.reportCodeChange();
}