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

The following examples show how to use com.google.javascript.rhino.Node#isEquivalentTo() . 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 5 votes vote down vote up
private void tryConvertToNumber(Node n) {
  switch (n.getType()) {
    case Token.NUMBER:
      // Nothing to do
      return;
    case Token.AND:
    case Token.OR:
    case Token.COMMA:
      tryConvertToNumber(n.getLastChild());
      return;
    case Token.HOOK:
      tryConvertToNumber(n.getChildAtIndex(1));
      tryConvertToNumber(n.getLastChild());
      return;
    case Token.NAME:
      if (!NodeUtil.isUndefined(n)) {
        return;
      }
      break;
  }

  Double result = NodeUtil.getNumberValue(n);
  if (result == null) {
    return;
  }

  double value = result;

  Node replacement = NodeUtil.numberNode(value, n);
  if (replacement.isEquivalentTo(n)) {
    return;
  }

  n.getParent().replaceChild(n, replacement);
  reportCodeChange();
}
 
Example 2
Source File: Closure_105_FoldConstants_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replaces a node with a number node if the new number node is not equivalent
 * to the current node.
 */
private void maybeReplaceChildWithNumber(NodeTraversal t, Node n, Node parent,
    int num) {
  Node newNode = Node.newNumber(num);
  if(!newNode.isEquivalentTo(n)) {
    parent.replaceChild(n, newNode);
    t.getCompiler().reportCodeChange();
  }
}
 
Example 3
Source File: Closure_105_FoldConstants_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replaces a node with a number node if the new number node is not equivalent
 * to the current node.
 */
private void maybeReplaceChildWithNumber(NodeTraversal t, Node n, Node parent,
    int num) {
  Node newNode = Node.newNumber(num);
  if(!newNode.isEquivalentTo(n)) {
    parent.replaceChild(n, newNode);
    t.getCompiler().reportCodeChange();
  }
}
 
Example 4
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replaces a node with a number node if the new number node is not equivalent
 * to the current node.
 *
 * Returns the replacement for n if it was replaced, otherwise returns n.
 */
private Node maybeReplaceChildWithNumber(Node n, Node parent, int num) {
  Node newNode = IR.number(num);
  if (!newNode.isEquivalentTo(n)) {
    parent.replaceChild(n, newNode);
    reportCodeChange();

    return newNode;
  }

  return n;
}
 
Example 5
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replaces a node with a number node if the new number node is not equivalent
 * to the current node.
 *
 * Returns the replacement for n if it was replaced, otherwise returns n.
 */
private Node maybeReplaceChildWithNumber(Node n, Node parent, int num) {
  Node newNode = IR.number(num);
  if (!newNode.isEquivalentTo(n)) {
    parent.replaceChild(n, newNode);
    reportCodeChange();

    return newNode;
  }

  return n;
}
 
Example 6
Source File: Closure_31_Compiler_t.java    From coming with MIT License 5 votes vote down vote up
@Override
boolean areNodesEqualForInlining(Node n1, Node n2) {
  if (options.ambiguateProperties ||
      options.disambiguateProperties) {
    // The type based optimizations require that type information is preserved
    // during other optimizations.
    return n1.isEquivalentToTyped(n2);
  } else {
    return n1.isEquivalentTo(n2);
  }
}
 
Example 7
Source File: Closure_31_Compiler_s.java    From coming with MIT License 5 votes vote down vote up
@Override
boolean areNodesEqualForInlining(Node n1, Node n2) {
  if (options.ambiguateProperties ||
      options.disambiguateProperties) {
    // The type based optimizations require that type information is preserved
    // during other optimizations.
    return n1.isEquivalentToTyped(n2);
  } else {
    return n1.isEquivalentTo(n2);
  }
}
 
Example 8
Source File: Closure_23_PeepholeFoldConstants_t.java    From coming with MIT License 5 votes vote down vote up
private void tryConvertToNumber(Node n) {
  switch (n.getType()) {
    case Token.NUMBER:
      // Nothing to do
      return;
    case Token.AND:
    case Token.OR:
    case Token.COMMA:
      tryConvertToNumber(n.getLastChild());
      return;
    case Token.HOOK:
      tryConvertToNumber(n.getChildAtIndex(1));
      tryConvertToNumber(n.getLastChild());
      return;
    case Token.NAME:
      if (!NodeUtil.isUndefined(n)) {
        return;
      }
      break;
  }

  Double result = NodeUtil.getNumberValue(n);
  if (result == null) {
    return;
  }

  double value = result;

  Node replacement = NodeUtil.numberNode(value, n);
  if (replacement.isEquivalentTo(n)) {
    return;
  }

  n.getParent().replaceChild(n, replacement);
  reportCodeChange();
}
 
Example 9
Source File: OptimizeParameters.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determine which parameters use the same expression.
 * @return Whether any parameter was found that can be updated.
 */
private boolean findFixedParameters(List<Parameter> parameters, Node cur) {
  boolean anyMovable = false;
  int index = 0;
  while ((cur = cur.getNext()) != null) {
    Parameter p;
    if (index >= parameters.size()) {
      p = new Parameter(cur, false);
      parameters.add(p);
    } else {
      p = parameters.get(index);
      if (p.shouldRemove()) {
        Node value = p.getArg();
        if (!cur.isEquivalentTo(value)) {
          p.setShouldRemove(false);
        } else {
          anyMovable = true;
        }
      }
    }

    setParameterSideEffectInfo(p, cur);
    index++;
  }

  for (;index < parameters.size(); index++) {
    parameters.get(index).setShouldRemove(false);
  }

  return anyMovable;
}
 
Example 10
Source File: Closure_87_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replaces a node with a number node if the new number node is not equivalent
 * to the current node.
 *
 * Returns the replacement for n if it was replaced, otherwise returns n.
 */
private Node maybeReplaceChildWithNumber(Node n, Node parent, int num) {
  Node newNode = Node.newNumber(num);
  if (!newNode.isEquivalentTo(n)) {
    parent.replaceChild(n, newNode);
    reportCodeChange();

    return newNode;
  }

  return n;
}
 
Example 11
Source File: Closure_64_Compiler_s.java    From coming with MIT License 5 votes vote down vote up
@Override
boolean areNodesEqualForInlining(Node n1, Node n2) {
  if (options.ambiguateProperties ||
      options.disambiguateProperties) {
    // The type based optimizations require that type information is preserved
    // during other optimizations.
    return n1.isEquivalentToTyped(n2);
  } else {
    return n1.isEquivalentTo(n2);
  }
}
 
Example 12
Source File: ProcessDefines.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void overrideDefines(Map<String, DefineInfo> allDefines) {
  boolean changed = false;
  for (Map.Entry<String, DefineInfo> def : allDefines.entrySet()) {
    String defineName = def.getKey();
    DefineInfo info = def.getValue();
    Node inputValue = dominantReplacements.get(defineName);
    Node finalValue = inputValue != null ?
        inputValue : info.getLastValue();
    if (finalValue != info.initialValue) {
      info.initialValueParent.replaceChild(
          info.initialValue, finalValue.cloneTree());
      compiler.addToDebugLog("Overriding @define variable " + defineName);
      changed = changed ||
          finalValue.getType() != info.initialValue.getType() ||
          !finalValue.isEquivalentTo(info.initialValue);
    }
  }

  if (changed) {
    compiler.reportCodeChange();
  }

  Set<String> unusedReplacements = dominantReplacements.keySet();
  unusedReplacements.removeAll(allDefines.keySet());
  unusedReplacements.removeAll(KNOWN_DEFINES);
  for (String unknownDefine : unusedReplacements) {
    compiler.report(JSError.make(UNKNOWN_DEFINE_WARNING, unknownDefine));
  }
}
 
Example 13
Source File: Closure_87_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Replaces a node with a number node if the new number node is not equivalent
 * to the current node.
 *
 * Returns the replacement for n if it was replaced, otherwise returns n.
 */
private Node maybeReplaceChildWithNumber(Node n, Node parent, int num) {
  Node newNode = Node.newNumber(num);
  if (!newNode.isEquivalentTo(n)) {
    parent.replaceChild(n, newNode);
    reportCodeChange();

    return newNode;
  }

  return n;
}
 
Example 14
Source File: PeepholeFoldConstants.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void tryConvertToNumber(Node n) {
  switch (n.getType()) {
    case Token.NUMBER:
      // Nothing to do
      return;
    case Token.AND:
    case Token.OR:
    case Token.COMMA:
      tryConvertToNumber(n.getLastChild());
      return;
    case Token.HOOK:
      tryConvertToNumber(n.getChildAtIndex(1));
      tryConvertToNumber(n.getLastChild());
      return;
    case Token.NAME:
      if (!NodeUtil.isUndefined(n)) {
        return;
      }
      break;
  }

  Double result = NodeUtil.getNumberValue(n);
  if (result == null) {
    return;
  }

  double value = result;

  Node replacement = NodeUtil.numberNode(value, n);
  if (replacement.isEquivalentTo(n)) {
    return;
  }

  n.getParent().replaceChild(n, replacement);
  reportCodeChange();
}
 
Example 15
Source File: Closure_18_Compiler_t.java    From coming with MIT License 5 votes vote down vote up
@Override
boolean areNodesEqualForInlining(Node n1, Node n2) {
  if (options.ambiguateProperties ||
      options.disambiguateProperties) {
    // The type based optimizations require that type information is preserved
    // during other optimizations.
    return n1.isEquivalentToTyped(n2);
  } else {
    return n1.isEquivalentTo(n2);
  }
}
 
Example 16
Source File: Closure_59_Compiler_t.java    From coming with MIT License 5 votes vote down vote up
@Override
boolean areNodesEqualForInlining(Node n1, Node n2) {
  if (options.ambiguateProperties ||
      options.disambiguateProperties) {
    // The type based optimizations require that type information is preserved
    // during other optimizations.
    return n1.isEquivalentToTyped(n2);
  } else {
    return n1.isEquivalentTo(n2);
  }
}
 
Example 17
Source File: Closure_132_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 2 votes vote down vote up
/**
 * Check whether one exit can be replaced with another. Verify:
 * 1) They are identical expressions
 * 2) If an exception is possible that the statements, the original
 * and the potential replacement are in the same exception handler.
 */
boolean areMatchingExits(Node nodeThis, Node nodeThat) {
  return nodeThis.isEquivalentTo(nodeThat)
      && (!isExceptionPossible(nodeThis)
          || getExceptionHandler(nodeThis) == getExceptionHandler(nodeThat));
}
 
Example 18
Source File: PeepholeSubstituteAlternateSyntax.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Check whether one exit can be replaced with another. Verify:
 * 1) They are identical expressions
 * 2) If an exception is possible that the statements, the original
 * and the potential replacement are in the same exception handler.
 */
boolean areMatchingExits(Node nodeThis, Node nodeThat) {
  return nodeThis.isEquivalentTo(nodeThat)
      && (!isExceptionPossible(nodeThis)
          || getExceptionHandler(nodeThis) == getExceptionHandler(nodeThat));
}
 
Example 19
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_s.java    From coming with MIT License 2 votes vote down vote up
/**
 * Check whether one exit can be replaced with another. Verify:
 * 1) They are identical expressions
 * 2) If an exception is possible that the statements, the original
 * and the potential replacement are in the same exception handler.
 */
boolean areMatchingExits(Node nodeThis, Node nodeThat) {
  return nodeThis.isEquivalentTo(nodeThat)
      && (!isExceptionPossible(nodeThis)
          || getExceptionHandler(nodeThis) == getExceptionHandler(nodeThat));
}
 
Example 20
Source File: Closure_20_PeepholeSubstituteAlternateSyntax_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Check whether one exit can be replaced with another. Verify:
 * 1) They are identical expressions
 * 2) If an exception is possible that the statements, the original
 * and the potential replacement are in the same exception handler.
 */
boolean areMatchingExits(Node nodeThis, Node nodeThat) {
  return nodeThis.isEquivalentTo(nodeThat)
      && (!isExceptionPossible(nodeThis)
          || getExceptionHandler(nodeThis) == getExceptionHandler(nodeThat));
}