Java Code Examples for com.google.javascript.rhino.jstype.JSType#getRestrictedTypeGivenToBooleanOutcome()

The following examples show how to use com.google.javascript.rhino.jstype.JSType#getRestrictedTypeGivenToBooleanOutcome() . 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_25_TypeInference_s.java    From coming with MIT License 4 votes vote down vote up
private BooleanOutcomePair traverseShortCircuitingBinOp(
    Node n, FlowScope scope, boolean condition) {
  Node left = n.getFirstChild();
  Node right = n.getLastChild();

  // type the left node
  BooleanOutcomePair leftLiterals =
      traverseWithinShortCircuitingBinOp(left,
          scope.createChildFlowScope());
  JSType leftType = left.getJSType();

  // reverse abstract interpret the left node to produce the correct
  // scope in which to verify the right node
  FlowScope rightScope = reverseInterpreter.
      getPreciserScopeKnowingConditionOutcome(
          left, leftLiterals.getOutcomeFlowScope(left.getType(), condition),
          condition);

  // type the right node
  BooleanOutcomePair rightLiterals =
      traverseWithinShortCircuitingBinOp(
          right, rightScope.createChildFlowScope());
  JSType rightType = right.getJSType();

  JSType type;
  BooleanOutcomePair literals;
  if (leftType != null && rightType != null) {
    leftType = leftType.getRestrictedTypeGivenToBooleanOutcome(!condition);
    if (leftLiterals.toBooleanOutcomes ==
        BooleanLiteralSet.get(!condition)) {
      // Use the restricted left type, since the right side never gets
      // evaluated.
      type = leftType;
      literals = leftLiterals;
    } else {
      // Use the join of the restricted left type knowing the outcome of the
      // ToBoolean predicate and of the right type.
      type = leftType.getLeastSupertype(rightType);
      literals =
          getBooleanOutcomePair(leftLiterals, rightLiterals, condition);
    }

    // Exclude the boolean type if the literal set is empty because a boolean
    // can never actually be returned.
    if (literals.booleanValues == BooleanLiteralSet.EMPTY &&
        getNativeType(BOOLEAN_TYPE).isSubtype(type)) {
      // Exclusion only make sense for a union type.
      if (type.isUnionType()) {
        type = type.toMaybeUnionType().getRestrictedUnion(
            getNativeType(BOOLEAN_TYPE));
      }
    }
  } else {
    type = null;
    literals = new BooleanOutcomePair(
        BooleanLiteralSet.BOTH, BooleanLiteralSet.BOTH,
        leftLiterals.getJoinedFlowScope(),
        rightLiterals.getJoinedFlowScope());
  }
  n.setJSType(type);

  return literals;
}
 
Example 2
Source File: Closure_25_TypeInference_t.java    From coming with MIT License 4 votes vote down vote up
private BooleanOutcomePair traverseShortCircuitingBinOp(
    Node n, FlowScope scope, boolean condition) {
  Node left = n.getFirstChild();
  Node right = n.getLastChild();

  // type the left node
  BooleanOutcomePair leftLiterals =
      traverseWithinShortCircuitingBinOp(left,
          scope.createChildFlowScope());
  JSType leftType = left.getJSType();

  // reverse abstract interpret the left node to produce the correct
  // scope in which to verify the right node
  FlowScope rightScope = reverseInterpreter.
      getPreciserScopeKnowingConditionOutcome(
          left, leftLiterals.getOutcomeFlowScope(left.getType(), condition),
          condition);

  // type the right node
  BooleanOutcomePair rightLiterals =
      traverseWithinShortCircuitingBinOp(
          right, rightScope.createChildFlowScope());
  JSType rightType = right.getJSType();

  JSType type;
  BooleanOutcomePair literals;
  if (leftType != null && rightType != null) {
    leftType = leftType.getRestrictedTypeGivenToBooleanOutcome(!condition);
    if (leftLiterals.toBooleanOutcomes ==
        BooleanLiteralSet.get(!condition)) {
      // Use the restricted left type, since the right side never gets
      // evaluated.
      type = leftType;
      literals = leftLiterals;
    } else {
      // Use the join of the restricted left type knowing the outcome of the
      // ToBoolean predicate and of the right type.
      type = leftType.getLeastSupertype(rightType);
      literals =
          getBooleanOutcomePair(leftLiterals, rightLiterals, condition);
    }

    // Exclude the boolean type if the literal set is empty because a boolean
    // can never actually be returned.
    if (literals.booleanValues == BooleanLiteralSet.EMPTY &&
        getNativeType(BOOLEAN_TYPE).isSubtype(type)) {
      // Exclusion only make sense for a union type.
      if (type.isUnionType()) {
        type = type.toMaybeUnionType().getRestrictedUnion(
            getNativeType(BOOLEAN_TYPE));
      }
    }
  } else {
    type = null;
    literals = new BooleanOutcomePair(
        BooleanLiteralSet.BOTH, BooleanLiteralSet.BOTH,
        leftLiterals.getJoinedFlowScope(),
        rightLiterals.getJoinedFlowScope());
  }
  n.setJSType(type);

  return literals;
}
 
Example 3
Source File: Closure_35_TypeInference_s.java    From coming with MIT License 4 votes vote down vote up
private BooleanOutcomePair traverseShortCircuitingBinOp(
    Node n, FlowScope scope, boolean condition) {
  Node left = n.getFirstChild();
  Node right = n.getLastChild();

  // type the left node
  BooleanOutcomePair leftLiterals =
      traverseWithinShortCircuitingBinOp(left,
          scope.createChildFlowScope());
  JSType leftType = left.getJSType();

  // reverse abstract interpret the left node to produce the correct
  // scope in which to verify the right node
  FlowScope rightScope = reverseInterpreter.
      getPreciserScopeKnowingConditionOutcome(
          left, leftLiterals.getOutcomeFlowScope(left.getType(), condition),
          condition);

  // type the right node
  BooleanOutcomePair rightLiterals =
      traverseWithinShortCircuitingBinOp(
          right, rightScope.createChildFlowScope());
  JSType rightType = right.getJSType();

  JSType type;
  BooleanOutcomePair literals;
  if (leftType != null && rightType != null) {
    leftType = leftType.getRestrictedTypeGivenToBooleanOutcome(!condition);
    if (leftLiterals.toBooleanOutcomes ==
        BooleanLiteralSet.get(!condition)) {
      // Use the restricted left type, since the right side never gets
      // evaluated.
      type = leftType;
      literals = leftLiterals;
    } else {
      // Use the join of the restricted left type knowing the outcome of the
      // ToBoolean predicate and of the right type.
      type = leftType.getLeastSupertype(rightType);
      literals =
          getBooleanOutcomePair(leftLiterals, rightLiterals, condition);
    }

    // Exclude the boolean type if the literal set is empty because a boolean
    // can never actually be returned.
    if (literals.booleanValues == BooleanLiteralSet.EMPTY &&
        getNativeType(BOOLEAN_TYPE).isSubtype(type)) {
      // Exclusion only make sense for a union type.
      if (type.isUnionType()) {
        type = type.toMaybeUnionType().getRestrictedUnion(
            getNativeType(BOOLEAN_TYPE));
      }
    }
  } else {
    type = null;
    literals = new BooleanOutcomePair(
        BooleanLiteralSet.BOTH, BooleanLiteralSet.BOTH,
        leftLiterals.getJoinedFlowScope(),
        rightLiterals.getJoinedFlowScope());
  }
  n.setJSType(type);

  return literals;
}
 
Example 4
Source File: Closure_35_TypeInference_t.java    From coming with MIT License 4 votes vote down vote up
private BooleanOutcomePair traverseShortCircuitingBinOp(
    Node n, FlowScope scope, boolean condition) {
  Node left = n.getFirstChild();
  Node right = n.getLastChild();

  // type the left node
  BooleanOutcomePair leftLiterals =
      traverseWithinShortCircuitingBinOp(left,
          scope.createChildFlowScope());
  JSType leftType = left.getJSType();

  // reverse abstract interpret the left node to produce the correct
  // scope in which to verify the right node
  FlowScope rightScope = reverseInterpreter.
      getPreciserScopeKnowingConditionOutcome(
          left, leftLiterals.getOutcomeFlowScope(left.getType(), condition),
          condition);

  // type the right node
  BooleanOutcomePair rightLiterals =
      traverseWithinShortCircuitingBinOp(
          right, rightScope.createChildFlowScope());
  JSType rightType = right.getJSType();

  JSType type;
  BooleanOutcomePair literals;
  if (leftType != null && rightType != null) {
    leftType = leftType.getRestrictedTypeGivenToBooleanOutcome(!condition);
    if (leftLiterals.toBooleanOutcomes ==
        BooleanLiteralSet.get(!condition)) {
      // Use the restricted left type, since the right side never gets
      // evaluated.
      type = leftType;
      literals = leftLiterals;
    } else {
      // Use the join of the restricted left type knowing the outcome of the
      // ToBoolean predicate and of the right type.
      type = leftType.getLeastSupertype(rightType);
      literals =
          getBooleanOutcomePair(leftLiterals, rightLiterals, condition);
    }

    // Exclude the boolean type if the literal set is empty because a boolean
    // can never actually be returned.
    if (literals.booleanValues == BooleanLiteralSet.EMPTY &&
        getNativeType(BOOLEAN_TYPE).isSubtype(type)) {
      // Exclusion only make sense for a union type.
      if (type.isUnionType()) {
        type = type.toMaybeUnionType().getRestrictedUnion(
            getNativeType(BOOLEAN_TYPE));
      }
    }
  } else {
    type = null;
    literals = new BooleanOutcomePair(
        BooleanLiteralSet.BOTH, BooleanLiteralSet.BOTH,
        leftLiterals.getJoinedFlowScope(),
        rightLiterals.getJoinedFlowScope());
  }
  n.setJSType(type);

  return literals;
}
 
Example 5
Source File: Closure_112_TypeInference_t.java    From coming with MIT License 4 votes vote down vote up
private BooleanOutcomePair traverseShortCircuitingBinOp(
    Node n, FlowScope scope, boolean condition) {
  Node left = n.getFirstChild();
  Node right = n.getLastChild();

  // type the left node
  BooleanOutcomePair leftLiterals =
      traverseWithinShortCircuitingBinOp(left,
          scope.createChildFlowScope());
  JSType leftType = left.getJSType();

  // reverse abstract interpret the left node to produce the correct
  // scope in which to verify the right node
  FlowScope rightScope = reverseInterpreter.
      getPreciserScopeKnowingConditionOutcome(
          left, leftLiterals.getOutcomeFlowScope(left.getType(), condition),
          condition);

  // type the right node
  BooleanOutcomePair rightLiterals =
      traverseWithinShortCircuitingBinOp(
          right, rightScope.createChildFlowScope());
  JSType rightType = right.getJSType();

  JSType type;
  BooleanOutcomePair literals;
  if (leftType != null && rightType != null) {
    leftType = leftType.getRestrictedTypeGivenToBooleanOutcome(!condition);
    if (leftLiterals.toBooleanOutcomes ==
        BooleanLiteralSet.get(!condition)) {
      // Use the restricted left type, since the right side never gets
      // evaluated.
      type = leftType;
      literals = leftLiterals;
    } else {
      // Use the join of the restricted left type knowing the outcome of the
      // ToBoolean predicate and of the right type.
      type = leftType.getLeastSupertype(rightType);
      literals =
          getBooleanOutcomePair(leftLiterals, rightLiterals, condition);
    }

    // Exclude the boolean type if the literal set is empty because a boolean
    // can never actually be returned.
    if (literals.booleanValues == BooleanLiteralSet.EMPTY &&
        getNativeType(BOOLEAN_TYPE).isSubtype(type)) {
      // Exclusion only make sense for a union type.
      if (type.isUnionType()) {
        type = type.toMaybeUnionType().getRestrictedUnion(
            getNativeType(BOOLEAN_TYPE));
      }
    }
  } else {
    type = null;
    literals = new BooleanOutcomePair(
        BooleanLiteralSet.BOTH, BooleanLiteralSet.BOTH,
        leftLiterals.getJoinedFlowScope(),
        rightLiterals.getJoinedFlowScope());
  }
  n.setJSType(type);

  return literals;
}
 
Example 6
Source File: Closure_112_TypeInference_s.java    From coming with MIT License 4 votes vote down vote up
private BooleanOutcomePair traverseShortCircuitingBinOp(
    Node n, FlowScope scope, boolean condition) {
  Node left = n.getFirstChild();
  Node right = n.getLastChild();

  // type the left node
  BooleanOutcomePair leftLiterals =
      traverseWithinShortCircuitingBinOp(left,
          scope.createChildFlowScope());
  JSType leftType = left.getJSType();

  // reverse abstract interpret the left node to produce the correct
  // scope in which to verify the right node
  FlowScope rightScope = reverseInterpreter.
      getPreciserScopeKnowingConditionOutcome(
          left, leftLiterals.getOutcomeFlowScope(left.getType(), condition),
          condition);

  // type the right node
  BooleanOutcomePair rightLiterals =
      traverseWithinShortCircuitingBinOp(
          right, rightScope.createChildFlowScope());
  JSType rightType = right.getJSType();

  JSType type;
  BooleanOutcomePair literals;
  if (leftType != null && rightType != null) {
    leftType = leftType.getRestrictedTypeGivenToBooleanOutcome(!condition);
    if (leftLiterals.toBooleanOutcomes ==
        BooleanLiteralSet.get(!condition)) {
      // Use the restricted left type, since the right side never gets
      // evaluated.
      type = leftType;
      literals = leftLiterals;
    } else {
      // Use the join of the restricted left type knowing the outcome of the
      // ToBoolean predicate and of the right type.
      type = leftType.getLeastSupertype(rightType);
      literals =
          getBooleanOutcomePair(leftLiterals, rightLiterals, condition);
    }

    // Exclude the boolean type if the literal set is empty because a boolean
    // can never actually be returned.
    if (literals.booleanValues == BooleanLiteralSet.EMPTY &&
        getNativeType(BOOLEAN_TYPE).isSubtype(type)) {
      // Exclusion only make sense for a union type.
      if (type.isUnionType()) {
        type = type.toMaybeUnionType().getRestrictedUnion(
            getNativeType(BOOLEAN_TYPE));
      }
    }
  } else {
    type = null;
    literals = new BooleanOutcomePair(
        BooleanLiteralSet.BOTH, BooleanLiteralSet.BOTH,
        leftLiterals.getJoinedFlowScope(),
        rightLiterals.getJoinedFlowScope());
  }
  n.setJSType(type);

  return literals;
}
 
Example 7
Source File: TypeInference.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private BooleanOutcomePair traverseShortCircuitingBinOp(
    Node n, FlowScope scope, boolean condition) {
  Node left = n.getFirstChild();
  Node right = n.getLastChild();

  // type the left node
  BooleanOutcomePair leftLiterals =
      traverseWithinShortCircuitingBinOp(left,
          scope.createChildFlowScope());
  JSType leftType = left.getJSType();

  // reverse abstract interpret the left node to produce the correct
  // scope in which to verify the right node
  FlowScope rightScope = reverseInterpreter.
      getPreciserScopeKnowingConditionOutcome(
          left, leftLiterals.getOutcomeFlowScope(left.getType(), condition),
          condition);

  // type the right node
  BooleanOutcomePair rightLiterals =
      traverseWithinShortCircuitingBinOp(
          right, rightScope.createChildFlowScope());
  JSType rightType = right.getJSType();

  JSType type;
  BooleanOutcomePair literals;
  if (leftType != null && rightType != null) {
    leftType = leftType.getRestrictedTypeGivenToBooleanOutcome(!condition);
    if (leftLiterals.toBooleanOutcomes ==
        BooleanLiteralSet.get(!condition)) {
      // Use the restricted left type, since the right side never gets
      // evaluated.
      type = leftType;
      literals = leftLiterals;
    } else {
      // Use the join of the restricted left type knowing the outcome of the
      // ToBoolean predicate and of the right type.
      type = leftType.getLeastSupertype(rightType);
      literals =
          getBooleanOutcomePair(leftLiterals, rightLiterals, condition);
    }

    // Exclude the boolean type if the literal set is empty because a boolean
    // can never actually be returned.
    if (literals.booleanValues == BooleanLiteralSet.EMPTY &&
        getNativeType(BOOLEAN_TYPE).isSubtype(type)) {
      // Exclusion only make sense for a union type.
      if (type.isUnionType()) {
        type = type.toMaybeUnionType().getRestrictedUnion(
            getNativeType(BOOLEAN_TYPE));
      }
    }
  } else {
    type = null;
    literals = new BooleanOutcomePair(
        BooleanLiteralSet.BOTH, BooleanLiteralSet.BOTH,
        leftLiterals.getJoinedFlowScope(),
        rightLiterals.getJoinedFlowScope());
  }
  n.setJSType(type);

  return literals;
}
 
Example 8
Source File: SemanticReverseAbstractInterpreter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private FlowScope caseAndOrNotShortCircuiting(Node left, Node right,
      FlowScope blindScope, boolean condition) {
  // left type
  JSType leftType = getTypeIfRefinable(left, blindScope);
  boolean leftIsRefineable;
  if (leftType != null) {
    leftIsRefineable = true;
  } else {
    leftIsRefineable = false;
    leftType = left.getJSType();
    blindScope = firstPreciserScopeKnowingConditionOutcome(
        left, blindScope, condition);
  }

  // restricting left type
  JSType restrictedLeftType = (leftType == null) ? null :
      leftType.getRestrictedTypeGivenToBooleanOutcome(condition);
  if (restrictedLeftType == null) {
    return firstPreciserScopeKnowingConditionOutcome(
        right, blindScope, condition);
  }

  // right type
  JSType rightType = getTypeIfRefinable(right, blindScope);
  boolean rightIsRefineable;
  if (rightType != null) {
    rightIsRefineable = true;
  } else {
    rightIsRefineable = false;
    rightType = right.getJSType();
    blindScope = firstPreciserScopeKnowingConditionOutcome(
        right, blindScope, condition);
  }

  if (condition) {
    JSType restrictedRightType = (rightType == null) ? null :
        rightType.getRestrictedTypeGivenToBooleanOutcome(condition);

    // creating new scope
    return maybeRestrictTwoNames(
        blindScope,
        left, leftType, leftIsRefineable ? restrictedLeftType : null,
        right, rightType, rightIsRefineable ? restrictedRightType : null);
  }
  return blindScope;
}