Java Code Examples for com.google.javascript.rhino.Token#GETELEM

The following examples show how to use com.google.javascript.rhino.Token#GETELEM . 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: ControlFlowAnalysis.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determines if the subtree might throw an exception.
 */
public static boolean mayThrowException(Node n) {
  switch (n.getType()) {
    case Token.CALL:
    case Token.GETPROP:
    case Token.GETELEM:
    case Token.THROW:
    case Token.NEW:
    case Token.ASSIGN:
    case Token.INC:
    case Token.DEC:
    case Token.INSTANCEOF:
      return true;
    case Token.FUNCTION:
      return false;
  }
  for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
    if (!ControlFlowGraph.isEnteringNewCfgNode(c) && mayThrowException(c)) {
      return true;
    }
  }
  return false;
}
 
Example 2
Source File: Reader.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void transformCallExpr(JsonML element, Node parent)
    throws JsonMLException {

  Node node = createNode(Token.CALL, element);
  parent.addChildToBack(node);

  transformAllChildren(element, node);

  // Keep track of of the "this" context of a call.  A call without an
  // explicit "this" is a free call.
  Node first = node.getFirstChild();
  if (first.getType() != Token.GETPROP && first.getType() != Token.GETELEM) {
    node.putBooleanProp(Node.FREE_CALL, true);
  }
}
 
Example 3
Source File: Closure_94_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param knownConstants A set of names known to be constant value at
 * node 'n' (such as locals that are last written before n can execute).
 * @return Whether the tree can be affected by side-effects or
 * has side-effects.
 */
static boolean canBeSideEffected(Node n, Set<String> knownConstants) {
  switch (n.getType()) {
    case Token.CALL:
    case Token.NEW:
      // Function calls or constructor can reference changed values.
      // TODO(johnlenz): Add some mechanism for determining that functions
      // are unaffected by side effects.
      return true;
    case Token.NAME:
      // Non-constant names values may have been changed.
      return !isConstantName(n)
          && !knownConstants.contains(n.getString());

    // Properties on constant NAMEs can still be side-effected.
    case Token.GETPROP:
    case Token.GETELEM:
      return true;

    case Token.FUNCTION:
      // Function expression are not changed by side-effects,
      // and function declarations are not part of expressions.
      Preconditions.checkState(isFunctionExpression(n));
      return false;
  }

  for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
    if (canBeSideEffected(c, knownConstants)) {
      return true;
    }
  }

  return false;
}
 
Example 4
Source File: jKali_003_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param knownConstants A set of names known to be constant value at
 * node 'n' (such as locals that are last written before n can execute).
 * @return Whether the tree can be affected by side-effects or
 * has side-effects.
 */
static boolean canBeSideEffected(Node n, Set<String> knownConstants) {
  switch (n.getType()) {
    case Token.CALL:
    case Token.NEW:
      // Function calls or constructor can reference changed values.
      // TODO(johnlenz): Add some mechanism for determining that functions
      // are unaffected by side effects.
      return true;
    case Token.NAME:
      // Non-constant names values may have been changed.
      return !isConstantName(n)
          && !knownConstants.contains(n.getString());

    // Properties on constant NAMEs can still be side-effected.
    case Token.GETPROP:
    case Token.GETELEM:
      return true;

    case Token.FUNCTION:
      // Function expression are not changed by side-effects,
      // and function declarations are not part of expressions.
      Preconditions.checkState(isFunctionExpression(n));
      return false;
  }

  for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
    if (canBeSideEffected(c, knownConstants)) {
      return true;
    }
  }

  return false;
}
 
Example 5
Source File: Closure_60_NodeUtil_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * @param knownConstants A set of names known to be constant value at
 * node 'n' (such as locals that are last written before n can execute).
 * @return Whether the tree can be affected by side-effects or
 * has side-effects.
 */
static boolean canBeSideEffected(Node n, Set<String> knownConstants) {
  switch (n.getType()) {
    case Token.CALL:
    case Token.NEW:
      // Function calls or constructor can reference changed values.
      // TODO(johnlenz): Add some mechanism for determining that functions
      // are unaffected by side effects.
      return true;
    case Token.NAME:
      // Non-constant names values may have been changed.
      return !isConstantName(n)
          && !knownConstants.contains(n.getString());

    // Properties on constant NAMEs can still be side-effected.
    case Token.GETPROP:
    case Token.GETELEM:
      return true;

    case Token.FUNCTION:
      // Function expression are not changed by side-effects,
      // and function declarations are not part of expressions.
      Preconditions.checkState(isFunctionExpression(n));
      return false;
  }

  for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
    if (canBeSideEffected(c, knownConstants)) {
      return true;
    }
  }

  return false;
}
 
Example 6
Source File: RenamePrototypes.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  switch (n.getType()) {
    case Token.GETPROP:
    case Token.GETELEM:
      Node dest = n.getFirstChild().getNext();
      if (dest.isString()) {
        String s = dest.getString();
        if (s.equals("prototype")) {
          processPrototypeParent(parent, t.getInput());
        } else {
          markPropertyAccessCandidate(dest, t.getInput());
        }
      }
      break;
    case Token.OBJECTLIT:
      if (!prototypeObjLits.contains(n)) {
        // Object literals have their property name/value pairs as a flat
        // list as their children. We want every other node in order to get
        // only the property names.
        for (Node child = n.getFirstChild();
             child != null;
             child = child.getNext()) {

          if (TokenStream.isJSIdentifier(child.getString())) {
            markObjLitPropertyCandidate(child, t.getInput());
          }
        }
      }
      break;
  }
}
 
Example 7
Source File: Closure_69_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * This predicate is used to determine if the node represents an expression
 * that is a Reference according to JavaScript definitions.
 *
 * @param n The node being checked.
 * @return true if the sub-tree n is a reference, false otherwise.
 */
private static boolean isReference(Node n) {
  switch (n.getType()) {
    case Token.GETELEM:
    case Token.GETPROP:
    case Token.NAME:
      return true;

    default:
      return false;
  }

}
 
Example 8
Source File: Closure_75_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.ARRAYLIT:
    case Token.CALL:
    case Token.EMPTY:
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.GETELEM:
    case Token.GETPROP:
    case Token.GET_REF:
    case Token.IF:
    case Token.LP:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.STRING:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Node.tokenToName(type) +
                             " (type " + type + ")");
  }
}
 
Example 9
Source File: NodeNameExtractor.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a qualified name of the specified node. Dots and brackets
 * are changed to the delimiter passed in when constructing the
 * NodeNameExtractor object.  We also replace ".prototype" with the
 * delimiter to keep names short, while still differentiating them
 * from static properties.  (Prototype properties will end up
 * looking like "a$b$$c" if this.delimiter = '$'.)
 */
String getName(Node node) {
  switch (node.getType()) {
    case Token.FUNCTION:
      Node functionNameNode = node.getFirstChild();
      return functionNameNode.getString();
    case Token.GETPROP:
      Node lhsOfDot = node.getFirstChild();
      Node rhsOfDot = lhsOfDot.getNext();
      String lhsOfDotName = getName(lhsOfDot);
      String rhsOfDotName = getName(rhsOfDot);
      if ("prototype".equals(rhsOfDotName)) {
        return lhsOfDotName + delimiter;
      } else {
        return lhsOfDotName + delimiter + rhsOfDotName;
      }
    case Token.GETELEM:
      Node outsideBrackets = node.getFirstChild();
      Node insideBrackets = outsideBrackets.getNext();
      String nameOutsideBrackets = getName(outsideBrackets);
      String nameInsideBrackets = getName(insideBrackets);
      if ("prototype".equals(nameInsideBrackets)) {
        return nameOutsideBrackets + delimiter;
      } else {
        return nameOutsideBrackets + delimiter + nameInsideBrackets;
      }
    case Token.NAME:
      return node.getString();
    case Token.STRING:
    case Token.STRING_KEY:
      return TokenStream.isJSIdentifier(node.getString()) ?
          node.getString() : ("__" + nextUniqueInt++);
    case Token.NUMBER:
      return NodeUtil.getStringValue(node);
    case Token.THIS:
      return "this";
    case Token.CALL:
      return getName(node.getFirstChild());
    default:
      StringBuilder sb = new StringBuilder();
      for (Node child = node.getFirstChild(); child != null;
           child = child.getNext()) {
        if (sb.length() > 0) {
          sb.append(delimiter);
        }
        sb.append(getName(child));
      }
      return sb.toString();
  }
}
 
Example 10
Source File: jMutRepair_003_t.java    From coming with MIT License 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.CALL:
    case Token.GETELEM:
    case Token.GETPROP:
    // Data values
    case Token.ARRAYLIT:
    case Token.EMPTY:  // TODO(johnlenz): remove this.
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.STRING:
    case Token.STRING_KEY:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Token.name(type) +
                             " (type " + type + ")");
  }
}
 
Example 11
Source File: Closure_94_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.ARRAYLIT:
    case Token.CALL:
    case Token.EMPTY:
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.GETELEM:
    case Token.GETPROP:
    case Token.GET_REF:
    case Token.IF:
    case Token.LP:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.RETURN:
    case Token.STRING:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Node.tokenToName(type) +
                             " (type " + type + ")");
  }
}
 
Example 12
Source File: Closure_80_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.ARRAYLIT:
    case Token.CALL:
    case Token.EMPTY:
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.GETELEM:
    case Token.GETPROP:
    case Token.GET_REF:
    case Token.IF:
    case Token.LP:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.RETURN:
    case Token.STRING:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Node.tokenToName(type) +
                             " (type " + type + ")");
  }
}
 
Example 13
Source File: Closure_80_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Is this a GETPROP or GETELEM node?
 */
static boolean isGet(Node n) {
  return n.getType() == Token.GETPROP
      || n.getType() == Token.GETELEM;
}
 
Example 14
Source File: Cardumen_00200_t.java    From coming with MIT License 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.CALL:
    case Token.GETELEM:
    case Token.GETPROP:
    // Data values
    case Token.ARRAYLIT:
    case Token.EMPTY:  // TODO(johnlenz): remove this.
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.STRING:
    case Token.STRING_KEY:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Token.name(type) +
                             " (type " + type + ")");
  }
}
 
Example 15
Source File: StripCode.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Removes a reference if it is a reference to a removed variable.
 *
 * @param t The traversal
 * @param n A NAME node
 * @param parent {@code n}'s parent
 */
void maybeRemoveReferenceToRemovedVariable(NodeTraversal t, Node n,
                                           Node parent) {
  switch (parent.getType()) {
    case Token.VAR:
      // This is a variable declaration, not a reference.
      break;

    case Token.GETPROP:
      // GETPROP
      //   NAME
      //   STRING (property name)
    case Token.GETELEM:
      // GETELEM
      //   NAME
      //   NUMBER|STRING|NAME|...
      if (parent.getFirstChild() == n && isReferenceToRemovedVar(t, n)) {
        replaceHighestNestedCallWithNull(parent, parent.getParent());
      }
      break;

    case Token.ASSIGN:
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
      if (isReferenceToRemovedVar(t, n)) {
        if (parent.getFirstChild() == n) {
          Node gramps = parent.getParent();
          if (gramps.isExprResult()) {
            // Remove the assignment.
            Node greatGramps = gramps.getParent();
            replaceWithEmpty(gramps, greatGramps);
            compiler.reportCodeChange();
          } else {
            // Substitute the r-value for the assignment.
            Node rvalue = n.getNext();
            parent.removeChild(rvalue);
            gramps.replaceChild(parent, rvalue);
            compiler.reportCodeChange();
          }
        } else {
          // The var reference is the r-value. Replace it with null.
          replaceWithNull(n, parent);
          compiler.reportCodeChange();
        }
      }
      break;

    default:
      if (isReferenceToRemovedVar(t, n)) {
        replaceWithNull(n, parent);
        compiler.reportCodeChange();
      }
      break;
  }
}
 
Example 16
Source File: Closure_86_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Is this a GETPROP or GETELEM node?
 */
static boolean isGet(Node n) {
  return n.getType() == Token.GETPROP
      || n.getType() == Token.GETELEM;
}
 
Example 17
Source File: PeepholeFoldConstants.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private Node tryFoldBinaryOperator(Node subtree) {
  Node left = subtree.getFirstChild();

  if (left == null) {
    return subtree;
  }

  Node right = left.getNext();

  if (right == null) {
    return subtree;
  }

  // If we've reached here, node is truly a binary operator.
  switch(subtree.getType()) {
    case Token.GETPROP:
      return tryFoldGetProp(subtree, left, right);

    case Token.GETELEM:
      return tryFoldGetElem(subtree, left, right);

    case Token.INSTANCEOF:
      return tryFoldInstanceof(subtree, left, right);

    case Token.AND:
    case Token.OR:
      return tryFoldAndOr(subtree, left, right);

    case Token.LSH:
    case Token.RSH:
    case Token.URSH:
      return tryFoldShift(subtree, left, right);

    case Token.ASSIGN:
      return tryFoldAssign(subtree, left, right);

    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
      return tryUnfoldAssignOp(subtree, left, right);

    case Token.ADD:
      return tryFoldAdd(subtree, left, right);

    case Token.SUB:
    case Token.DIV:
    case Token.MOD:
      return tryFoldArithmeticOp(subtree, left, right);

    case Token.MUL:
    case Token.BITAND:
    case Token.BITOR:
    case Token.BITXOR:
      Node result = tryFoldArithmeticOp(subtree, left, right);
      if (result != subtree) {
        return result;
      }
      return tryFoldLeftChildOp(subtree, left, right);

    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:
      return tryFoldComparison(subtree, left, right);

    default:
      return subtree;
  }
}
 
Example 18
Source File: Cardumen_00149_s.java    From coming with MIT License 4 votes vote down vote up
static int precedence(int type) {
  switch (type) {
    case Token.COMMA:  return 0;
    case Token.ASSIGN_BITOR:
    case Token.ASSIGN_BITXOR:
    case Token.ASSIGN_BITAND:
    case Token.ASSIGN_LSH:
    case Token.ASSIGN_RSH:
    case Token.ASSIGN_URSH:
    case Token.ASSIGN_ADD:
    case Token.ASSIGN_SUB:
    case Token.ASSIGN_MUL:
    case Token.ASSIGN_DIV:
    case Token.ASSIGN_MOD:
    case Token.ASSIGN: return 1;
    case Token.HOOK:   return 2;  // ?: operator
    case Token.OR:     return 3;
    case Token.AND:    return 4;
    case Token.BITOR:  return 5;
    case Token.BITXOR: return 6;
    case Token.BITAND: return 7;
    case Token.EQ:
    case Token.NE:
    case Token.SHEQ:
    case Token.SHNE:   return 8;
    case Token.LT:
    case Token.GT:
    case Token.LE:
    case Token.GE:
    case Token.INSTANCEOF:
    case Token.IN:     return 9;
    case Token.LSH:
    case Token.RSH:
    case Token.URSH:   return 10;
    case Token.SUB:
    case Token.ADD:    return 11;
    case Token.MUL:
    case Token.MOD:
    case Token.DIV:    return 12;
    case Token.INC:
    case Token.DEC:
    case Token.NEW:
    case Token.DELPROP:
    case Token.TYPEOF:
    case Token.VOID:
    case Token.NOT:
    case Token.BITNOT:
    case Token.POS:
    case Token.NEG:    return 13;

    case Token.CALL:
    case Token.GETELEM:
    case Token.GETPROP:
    // Data values
    case Token.ARRAYLIT:
    case Token.EMPTY:  // TODO(johnlenz): remove this.
    case Token.FALSE:
    case Token.FUNCTION:
    case Token.NAME:
    case Token.NULL:
    case Token.NUMBER:
    case Token.OBJECTLIT:
    case Token.REGEXP:
    case Token.STRING:
    case Token.STRING_KEY:
    case Token.THIS:
    case Token.TRUE:
      return 15;

    default: throw new Error("Unknown precedence for " +
                             Token.name(type) +
                             " (type " + type + ")");
  }
}
 
Example 19
Source File: jKali_003_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * @param locals A predicate to apply to unknown local values.
 * @return Whether the node is known to be a value that is not a reference
 *     outside the expression scope.
 */
static boolean evaluatesToLocalValue(Node value, Predicate<Node> locals) {
  switch (value.getType()) {
    case Token.ASSIGN:
      // A result that is aliased by a non-local name, is the effectively the
      // same as returning a non-local name, but this doesn't matter if the
      // value is immutable.
      return NodeUtil.isImmutableValue(value.getLastChild())
          || (locals.apply(value)
              && evaluatesToLocalValue(value.getLastChild(), locals));
    case Token.COMMA:
      return evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.AND:
    case Token.OR:
      return evaluatesToLocalValue(value.getFirstChild(), locals)
         && evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.HOOK:
      return evaluatesToLocalValue(value.getFirstChild().getNext(), locals)
         && evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.INC:
    case Token.DEC:
      if (value.getBooleanProp(Node.INCRDECR_PROP)) {
        return evaluatesToLocalValue(value.getFirstChild(), locals);
      } else {
        return true;
      }
    case Token.THIS:
      return locals.apply(value);
    case Token.NAME:
      return isImmutableValue(value) || locals.apply(value);
    case Token.GETELEM:
    case Token.GETPROP:
      // There is no information about the locality of object properties.
      return locals.apply(value);
    case Token.CALL:
      return callHasLocalResult(value)
          || isToStringMethodCall(value)
          || locals.apply(value);
    case Token.NEW:
      return newHasLocalResult(value)
             || locals.apply(value);
    case Token.FUNCTION:
    case Token.REGEXP:
    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      // Literals objects with non-literal children are allowed.
      return true;
    case Token.DELPROP:
    case Token.IN:
      // TODO(johnlenz): should IN operator be included in #isSimpleOperator?
      return true;
    default:
      // Other op force a local value:
      //  x = '' + g (x is now an local string)
      //  x -= g (x is now an local number)
      if (isAssignmentOp(value)
          || isSimpleOperator(value)
          || isImmutableValue(value)) {
        return true;
      }

      throw new IllegalStateException(
          "Unexpected expression node" + value +
          "\n parent:" + value.getParent());
  }
}
 
Example 20
Source File: jMutRepair_003_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * @param locals A predicate to apply to unknown local values.
 * @return Whether the node is known to be a value that is not a reference
 *     outside the expression scope.
 */
static boolean evaluatesToLocalValue(Node value, Predicate<Node> locals) {
  switch (value.getType()) {
    case Token.ASSIGN:
      // A result that is aliased by a non-local name, is the effectively the
      // same as returning a non-local name, but this doesn't matter if the
      // value is immutable.
      return NodeUtil.isImmutableValue(value.getLastChild())
          || (locals.apply(value)
              && evaluatesToLocalValue(value.getLastChild(), locals));
    case Token.COMMA:
      return evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.AND:
    case Token.OR:
      return evaluatesToLocalValue(value.getFirstChild(), locals)
         && evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.HOOK:
      return evaluatesToLocalValue(value.getFirstChild().getNext(), locals)
         && evaluatesToLocalValue(value.getLastChild(), locals);
    case Token.INC:
    case Token.DEC:
      if (value.getBooleanProp(Node.INCRDECR_PROP)) {
        return evaluatesToLocalValue(value.getFirstChild(), locals);
      } else {
        return true;
      }
    case Token.THIS:
      return locals.apply(value);
    case Token.NAME:
      return isImmutableValue(value) || locals.apply(value);
    case Token.GETELEM:
    case Token.GETPROP:
      // There is no information about the locality of object properties.
      return locals.apply(value);
    case Token.CALL:
      return callHasLocalResult(value)
          || isToStringMethodCall(value)
          || locals.apply(value);
    case Token.NEW:
      return newHasLocalResult(value)
             || locals.apply(value);
    case Token.FUNCTION:
    case Token.REGEXP:
    case Token.ARRAYLIT:
    case Token.OBJECTLIT:
      // Literals objects with non-literal children are allowed.
      return true;
    case Token.DELPROP:
    case Token.IN:
      // TODO(johnlenz): should IN operator be included in #isSimpleOperator?
      return true;
    default:
      // Other op force a local value:
      //  x = '' + g (x is now an local string)
      //  x -= g (x is now an local number)
      if (isAssignmentOp(value)
          || isSimpleOperator(value)
          || isImmutableValue(value)) {
        return true;
      }

      throw new IllegalStateException(
          "Unexpected expression node" + value +
          "\n parent:" + value.getParent());
  }
}