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

The following examples show how to use com.google.javascript.rhino.Token#AND . 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: Cardumen_0092_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determine if the parent reads the value of a child expression
 * directly.  This is true children used in predicates, RETURN
 * statements and, rhs of variable declarations and assignments.
 *
 * In the case of:
 * if (a) b else c
 *
 * This method returns true for "a", and false for "b" and "c": the
 * IF expression does something special based on "a"'s value.  "b"
 * and "c" are effectivelly outputs.  Same logic applies to FOR,
 * WHILE and DO loop predicates.  AND/OR/HOOK expressions are
 * syntactic sugar for IF statements; therefore this method returns
 * true for the predicate and false otherwise.
 */
private boolean valueConsumedByParent(Node n, Node parent) {
  if (NodeUtil.isAssignmentOp(parent)) {
    return parent.getLastChild() == n;
  }

  switch (parent.getType()) {
    case Token.NAME:
    case Token.RETURN:
      return true;
    case Token.AND:
    case Token.OR:
    case Token.HOOK:
      return parent.getFirstChild() == n;
    case Token.FOR:
      return parent.getFirstChild().getNext() == n;
    case Token.IF:
    case Token.WHILE:
      return parent.getFirstChild() == n;
    case Token.DO:
      return parent.getLastChild() == n;
    default:
      return false;
  }
}
 
Example 2
Source File: NameAnalyzer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determine if the parent reads the value of a child expression
 * directly.  This is true children used in predicates, RETURN
 * statements and, RHS of variable declarations and assignments.
 *
 * In the case of:
 * if (a) b else c
 *
 * This method returns true for "a", and false for "b" and "c": the
 * IF expression does something special based on "a"'s value.  "b"
 * and "c" are effectively outputs.  Same logic applies to FOR,
 * WHILE and DO loop predicates.  AND/OR/HOOK expressions are
 * syntactic sugar for IF statements; therefore this method returns
 * true for the predicate and false otherwise.
 */
private boolean valueConsumedByParent(Node n, Node parent) {
  if (NodeUtil.isAssignmentOp(parent)) {
    return parent.getLastChild() == n;
  }

  switch (parent.getType()) {
    case Token.NAME:
    case Token.RETURN:
      return true;
    case Token.AND:
    case Token.OR:
    case Token.HOOK:
      return parent.getFirstChild() == n;
    case Token.FOR:
      return parent.getFirstChild().getNext() == n;
    case Token.IF:
    case Token.WHILE:
      return parent.getFirstChild() == n;
    case Token.DO:
      return parent.getLastChild() == n;
    default:
      return false;
  }
}
 
Example 3
Source File: Closure_125_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether this node is testing for the existence of a property.
 * If true, we will not emit warnings about a missing property.
 *
 * @param getProp The GETPROP being tested.
 */
private boolean isPropertyTest(Node getProp) {
  Node parent = getProp.getParent();
  switch (parent.getType()) {
    case Token.CALL:
      return parent.getFirstChild() != getProp &&
          compiler.getCodingConvention().isPropertyTestFunction(parent);

    case Token.IF:
    case Token.WHILE:
    case Token.DO:
    case Token.FOR:
      return NodeUtil.getConditionExpression(parent) == getProp;

    case Token.INSTANCEOF:
    case Token.TYPEOF:
      return true;

    case Token.AND:
    case Token.HOOK:
      return parent.getFirstChild() == getProp;

    case Token.NOT:
      return parent.getParent().isOr() &&
          parent.getParent().getFirstChild() == parent;

    case Token.CAST:
      return isPropertyTest(parent);
  }
  return false;
}
 
Example 4
Source File: Cardumen_0014_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if the operator is associative.
 * e.g. (a * b) * c = a * (b * c)
 * Note: "+" is not associative because it is also the concatenation
 * for strings. e.g. "a" + (1 + 2) is not "a" + 1 + 2
 */
static boolean isAssociative(int type) {
  switch (type) {
    case Token.MUL:
    case Token.AND:
    case Token.OR:
    case Token.BITOR:
    case Token.BITXOR:
    case Token.BITAND:
      return true;
    default:
      return false;
  }
}
 
Example 5
Source File: Cardumen_00200_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Returns true if the operator is associative.
 * e.g. (a * b) * c = a * (b * c)
 * Note: "+" is not associative because it is also the concatenation
 * for strings. e.g. "a" + (1 + 2) is not "a" + 1 + 2
 */
static boolean isAssociative(int type) {
  switch (type) {
    case Token.MUL:
    case Token.AND:
    case Token.OR:
    case Token.BITOR:
    case Token.BITXOR:
    case Token.BITAND:
      return true;
    default:
      return false;
  }
}
 
Example 6
Source File: SideEffectsAnalysis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if the number of times the child executes depends on the
 * parent.
 *
 * For example, the guard of an IF is not control dependent on the
 * IF, but its two THEN/ELSE blocks are.
 *
 * Also, the guard of WHILE and DO are control dependent on the parent
 * since the number of times it executes depends on the parent.
 */
private static boolean isControlDependentChild(Node child) {
  Node parent = child.getParent();

  if (parent == null) {
    return false;
  }

  ArrayList<Node> siblings = Lists.newArrayList(parent.children());

  int indexOfChildInParent = siblings.indexOf(child);

  switch(parent.getType()) {
    case Token.IF:
    case Token.HOOK:
      return (indexOfChildInParent == 1 || indexOfChildInParent == 2);
    case Token.WHILE:
    case Token.DO:
      return true;
    case Token.FOR:
      // Only initializer is not control dependent
      return indexOfChildInParent != 0;
    case Token.SWITCH:
        return indexOfChildInParent > 0;
    case Token.AND:
      return true;
    case Token.OR:
      return true;
    case Token.FUNCTION:
      return true;

    default:
      return false;
  }
}
 
Example 7
Source File: Closure_114_NameAnalyzer_t.java    From coming with MIT License 5 votes vote down vote up
private void recordConsumers(NodeTraversal t, Node n, Node recordNode) {
  Node parent = n.getParent();
  switch (parent.getType()) {
    case Token.ASSIGN:
      if (n == parent.getLastChild()) {
        recordAssignment(t, parent, recordNode);
      }
      recordConsumers(t, parent, recordNode);
      break;
    case Token.NAME:
      NameInformation ns = createNameInformation(t, parent);
      recordDepScope(recordNode, ns);
      break;
    case Token.OR:
      recordConsumers(t, parent, recordNode);
      break;
    case Token.AND:
      // In "a && b" only "b" can be meaningfully aliased.
      // "a" must be falsy, which it must be an immutable, non-Object
    case Token.COMMA:
    case Token.HOOK:
      if (n != parent.getFirstChild()) {
        recordConsumers(t, parent, recordNode);
      }
      break;
  }
}
 
Example 8
Source File: PeepholeFoldConstants.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Try to fold a AND/OR node.
 */
private Node tryFoldAndOr(Node n, Node left, Node right) {
  Node parent = n.getParent();

  Node result = null;

  int type = n.getType();

  TernaryValue leftVal = NodeUtil.getImpureBooleanValue(left);

  if (leftVal != TernaryValue.UNKNOWN) {
    boolean lval = leftVal.toBoolean(true);

    // (TRUE || x) => TRUE (also, (3 || x) => 3)
    // (FALSE && x) => FALSE
    if (lval && type == Token.OR ||
        !lval && type == Token.AND) {
      result = left;

    } else if (!mayHaveSideEffects(left)) {
      // (FALSE || x) => x
      // (TRUE && x) => x
      result = right;
    }
  }

  // Note: Right hand side folding is handled by
  // PeepholeSubstituteAlternateSyntax#tryMinimizeCondition

  if (result != null) {
    // Fold it!
    n.removeChild(result);
    parent.replaceChild(n, result);
    reportCodeChange();

    return result;
  } else {
    return n;
  }
}
 
Example 9
Source File: Closure_35_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Gets the outcome scope if we do know the outcome of the entire
 * expression.
 */
FlowScope getOutcomeFlowScope(int nodeType, boolean outcome) {
  if (nodeType == Token.AND && outcome ||
      nodeType == Token.OR && !outcome) {
    // We know that the whole expression must have executed.
    return rightScope;
  } else {
    return getJoinedFlowScope();
  }
}
 
Example 10
Source File: Cardumen_00200_t.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 11
Source File: Closure_75_NodeUtil_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Converts an operator's token value (see {@link Token}) to a string
 * representation.
 *
 * @param operator the operator's token value to convert
 * @return the string representation or {@code null} if the token value is
 * not an operator
 */
static String opToStr(int operator) {
  switch (operator) {
    case Token.BITOR: return "|";
    case Token.OR: return "||";
    case Token.BITXOR: return "^";
    case Token.AND: return "&&";
    case Token.BITAND: return "&";
    case Token.SHEQ: return "===";
    case Token.EQ: return "==";
    case Token.NOT: return "!";
    case Token.NE: return "!=";
    case Token.SHNE: return "!==";
    case Token.LSH: return "<<";
    case Token.IN: return "in";
    case Token.LE: return "<=";
    case Token.LT: return "<";
    case Token.URSH: return ">>>";
    case Token.RSH: return ">>";
    case Token.GE: return ">=";
    case Token.GT: return ">";
    case Token.MUL: return "*";
    case Token.DIV: return "/";
    case Token.MOD: return "%";
    case Token.BITNOT: return "~";
    case Token.ADD: return "+";
    case Token.SUB: return "-";
    case Token.POS: return "+";
    case Token.NEG: return "-";
    case Token.ASSIGN: return "=";
    case Token.ASSIGN_BITOR: return "|=";
    case Token.ASSIGN_BITXOR: return "^=";
    case Token.ASSIGN_BITAND: return "&=";
    case Token.ASSIGN_LSH: return "<<=";
    case Token.ASSIGN_RSH: return ">>=";
    case Token.ASSIGN_URSH: return ">>>=";
    case Token.ASSIGN_ADD: return "+=";
    case Token.ASSIGN_SUB: return "-=";
    case Token.ASSIGN_MUL: return "*=";
    case Token.ASSIGN_DIV: return "/=";
    case Token.ASSIGN_MOD: return "%=";
    case Token.VOID: return "void";
    case Token.TYPEOF: return "typeof";
    case Token.INSTANCEOF: return "instanceof";
    default: return null;
  }
}
 
Example 12
Source File: Closure_60_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Converts an operator's token value (see {@link Token}) to a string
 * representation.
 *
 * @param operator the operator's token value to convert
 * @return the string representation or {@code null} if the token value is
 * not an operator
 */
static String opToStr(int operator) {
  switch (operator) {
    case Token.BITOR: return "|";
    case Token.OR: return "||";
    case Token.BITXOR: return "^";
    case Token.AND: return "&&";
    case Token.BITAND: return "&";
    case Token.SHEQ: return "===";
    case Token.EQ: return "==";
    case Token.NOT: return "!";
    case Token.NE: return "!=";
    case Token.SHNE: return "!==";
    case Token.LSH: return "<<";
    case Token.IN: return "in";
    case Token.LE: return "<=";
    case Token.LT: return "<";
    case Token.URSH: return ">>>";
    case Token.RSH: return ">>";
    case Token.GE: return ">=";
    case Token.GT: return ">";
    case Token.MUL: return "*";
    case Token.DIV: return "/";
    case Token.MOD: return "%";
    case Token.BITNOT: return "~";
    case Token.ADD: return "+";
    case Token.SUB: return "-";
    case Token.POS: return "+";
    case Token.NEG: return "-";
    case Token.ASSIGN: return "=";
    case Token.ASSIGN_BITOR: return "|=";
    case Token.ASSIGN_BITXOR: return "^=";
    case Token.ASSIGN_BITAND: return "&=";
    case Token.ASSIGN_LSH: return "<<=";
    case Token.ASSIGN_RSH: return ">>=";
    case Token.ASSIGN_URSH: return ">>>=";
    case Token.ASSIGN_ADD: return "+=";
    case Token.ASSIGN_SUB: return "-=";
    case Token.ASSIGN_MUL: return "*=";
    case Token.ASSIGN_DIV: return "/=";
    case Token.ASSIGN_MOD: return "%=";
    case Token.VOID: return "void";
    case Token.TYPEOF: return "typeof";
    case Token.INSTANCEOF: return "instanceof";
    default: return null;
  }
}
 
Example 13
Source File: NodeUtil.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts an operator's token value (see {@link Token}) to a string
 * representation.
 *
 * @param operator the operator's token value to convert
 * @return the string representation or {@code null} if the token value is
 * not an operator
 */
static String opToStr(int operator) {
  switch (operator) {
    case Token.BITOR: return "|";
    case Token.OR: return "||";
    case Token.BITXOR: return "^";
    case Token.AND: return "&&";
    case Token.BITAND: return "&";
    case Token.SHEQ: return "===";
    case Token.EQ: return "==";
    case Token.NOT: return "!";
    case Token.NE: return "!=";
    case Token.SHNE: return "!==";
    case Token.LSH: return "<<";
    case Token.IN: return "in";
    case Token.LE: return "<=";
    case Token.LT: return "<";
    case Token.URSH: return ">>>";
    case Token.RSH: return ">>";
    case Token.GE: return ">=";
    case Token.GT: return ">";
    case Token.MUL: return "*";
    case Token.DIV: return "/";
    case Token.MOD: return "%";
    case Token.BITNOT: return "~";
    case Token.ADD: return "+";
    case Token.SUB: return "-";
    case Token.POS: return "+";
    case Token.NEG: return "-";
    case Token.ASSIGN: return "=";
    case Token.ASSIGN_BITOR: return "|=";
    case Token.ASSIGN_BITXOR: return "^=";
    case Token.ASSIGN_BITAND: return "&=";
    case Token.ASSIGN_LSH: return "<<=";
    case Token.ASSIGN_RSH: return ">>=";
    case Token.ASSIGN_URSH: return ">>>=";
    case Token.ASSIGN_ADD: return "+=";
    case Token.ASSIGN_SUB: return "-=";
    case Token.ASSIGN_MUL: return "*=";
    case Token.ASSIGN_DIV: return "/=";
    case Token.ASSIGN_MOD: return "%=";
    case Token.VOID: return "void";
    case Token.TYPEOF: return "typeof";
    case Token.INSTANCEOF: return "instanceof";
    default: return null;
  }
}
 
Example 14
Source File: Cardumen_00200_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 15
Source File: Cardumen_0014_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 16
Source File: Closure_76_DeadAssignmentsElimination_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a variable, node n in the tree and a sub-tree denoted by exprRoot as
 * the root, this function returns true if there exists a read of that
 * variable before a write to that variable that is on the right side of n.
 *
 * For example, suppose the node is x = 1:
 *
 * y = 1, x = 1; // false, there is no reads at all.
 * y = 1, x = 1, print(x) // true, there is a read right of n.
 * y = 1, x = 1, x = 2, print(x) // false, there is a read right of n but
 *                               // it is after a write.
 *
 * @param n The current node we should look at.
 * @param exprRoot The node
 */
private boolean isVariableStillLiveWithinExpression(
    Node n, Node exprRoot, String variable) {
  while (n != exprRoot) {
    VariableLiveness state = VariableLiveness.MAYBE_LIVE;
    switch (n.getParent().getType()) {
      case Token.OR:
      case Token.AND:
        // If the currently node is the first child of
        // AND/OR, be conservative only consider the READs
        // of the second operand.
        if (n.getNext() != null) {
          state = isVariableReadBeforeKill(
              n.getNext(), variable);
          if (state == VariableLiveness.KILL) {
            state = VariableLiveness.MAYBE_LIVE;
          }
        }
        break;

      case Token.HOOK:
        // If current node is the condition, check each following
        // branch, otherwise it is a conditional branch and the
        // other branch can be ignored.
        if (n.getNext() != null && n.getNext().getNext() != null) {
          state = checkHookBranchReadBeforeKill(
              n.getNext(), n.getNext().getNext(), variable);
        }
        break;

      default:
        for(Node sibling = n.getNext(); sibling != null;
            sibling = sibling.getNext()) {
          state = isVariableReadBeforeKill(sibling, variable);
          if (state != VariableLiveness.MAYBE_LIVE) {
            break;
          }
        }
    }

    // If we see a READ or KILL there is no need to continue.
    if (state == VariableLiveness.READ) {
      return true;
    } else if (state == VariableLiveness.KILL) {
      return false;
    }
    n = n.getParent();
  }
  return false;
}
 
Example 17
Source File: jMutRepair_003_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * @param n The expression to check.
 * @return Whether the expression is unconditionally executed only once in the
 *     containing execution scope.
 */
static boolean isExecutedExactlyOnce(Node n) {
  inspect: do {
    Node parent = n.getParent();
    switch (parent.getType()) {
      case Token.IF:
      case Token.HOOK:
      case Token.AND:
      case Token.OR:
        if (parent.getFirstChild() != n) {
          return false;
        }
        // other ancestors may be conditional
        continue inspect;
      case Token.FOR:
        if (NodeUtil.isForIn(parent)) {
          if (parent.getChildAtIndex(1) != n) {
            return false;
          }
        } else {
          if (parent.getFirstChild() != n) {
            return false;
          }
        }
        // other ancestors may be conditional
        continue inspect;
      case Token.WHILE:
      case Token.DO:
        return false;
      case Token.TRY:
        // Consider all code under a try/catch to be conditionally executed.
        if (!hasFinally(parent) || parent.getLastChild() != n) {
          return false;
        }
        continue inspect;
      case Token.CASE:
      case Token.DEFAULT_CASE:
        return false;
      case Token.SCRIPT:
      case Token.FUNCTION:
        // Done, we've reached the scope root.
        break inspect;
    }
  } while ((n = n.getParent()) != null);
  return true;
}
 
Example 18
Source File: Cardumen_00149_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * @param n The expression to check.
 * @return Whether the expression is unconditionally executed only once in the
 *     containing execution scope.
 */
static boolean isExecutedExactlyOnce(Node n) {
  inspect: do {
    Node parent = n.getParent();
    switch (parent.getType()) {
      case Token.IF:
      case Token.HOOK:
      case Token.AND:
      case Token.OR:
        if (parent.getFirstChild() != n) {
          return false;
        }
        // other ancestors may be conditional
        continue inspect;
      case Token.FOR:
        if (NodeUtil.isForIn(parent)) {
          if (parent.getChildAtIndex(1) != n) {
            return false;
          }
        } else {
          if (parent.getFirstChild() != n) {
            return false;
          }
        }
        // other ancestors may be conditional
        continue inspect;
      case Token.WHILE:
      case Token.DO:
        return false;
      case Token.TRY:
        // Consider all code under a try/catch to be conditionally executed.
        if (!hasFinally(parent) || parent.getLastChild() != n) {
          return false;
        }
        continue inspect;
      case Token.CASE:
      case Token.DEFAULT_CASE:
        return false;
      case Token.SCRIPT:
      case Token.FUNCTION:
        // Done, we've reached the scope root.
        break inspect;
    }
  } while ((n = n.getParent()) != null);
  return true;
}
 
Example 19
Source File: Closure_10_NodeUtil_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * @param n The expression to check.
 * @return Whether the expression is unconditionally executed only once in the
 *     containing execution scope.
 */
static boolean isExecutedExactlyOnce(Node n) {
  inspect: do {
    Node parent = n.getParent();
    switch (parent.getType()) {
      case Token.IF:
      case Token.HOOK:
      case Token.AND:
      case Token.OR:
        if (parent.getFirstChild() != n) {
          return false;
        }
        // other ancestors may be conditional
        continue inspect;
      case Token.FOR:
        if (NodeUtil.isForIn(parent)) {
          if (parent.getChildAtIndex(1) != n) {
            return false;
          }
        } else {
          if (parent.getFirstChild() != n) {
            return false;
          }
        }
        // other ancestors may be conditional
        continue inspect;
      case Token.WHILE:
      case Token.DO:
        return false;
      case Token.TRY:
        // Consider all code under a try/catch to be conditionally executed.
        if (!hasFinally(parent) || parent.getLastChild() != n) {
          return false;
        }
        continue inspect;
      case Token.CASE:
      case Token.DEFAULT_CASE:
        return false;
      case Token.SCRIPT:
      case Token.FUNCTION:
        // Done, we've reached the scope root.
        break inspect;
    }
  } while ((n = n.getParent()) != null);
  return true;
}
 
Example 20
Source File: Closure_60_NodeUtil_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());
  }
}