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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#getLeastSupertype() . 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_19_ChainableReverseAbstractInterpreter_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public JSType caseUnionType(UnionType type) {
  JSType restricted = null;
  for (JSType alternate : type.getAlternates()) {
    JSType restrictedAlternate = alternate.visit(this);
    if (restrictedAlternate != null) {
      if (restricted == null) {
        restricted = restrictedAlternate;
      } else {
        restricted = restrictedAlternate.getLeastSupertype(restricted);
      }
    }
  }
  return restricted;
}
 
Example 2
Source File: ChainableReverseAbstractInterpreter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public JSType caseUnionType(UnionType type) {
  JSType restricted = null;
  for (JSType alternate : type.getAlternates()) {
    JSType restrictedAlternate = alternate.visit(this);
    if (restrictedAlternate != null) {
      if (restricted == null) {
        restricted = restrictedAlternate;
      } else {
        restricted = restrictedAlternate.getLeastSupertype(restricted);
      }
    }
  }
  return restricted;
}
 
Example 3
Source File: TypeInference.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void resolvedTemplateType(
    Map<TemplateType, JSType> map, TemplateType template, JSType resolved) {
  JSType previous = map.get(template);
  if (!resolved.isUnknownType()) {
    if (previous == null) {
      map.put(template, resolved);
    } else {
      JSType join = previous.getLeastSupertype(resolved);
      map.put(template, join);
    }
  }
}
 
Example 4
Source File: Closure_112_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
private static void resolvedTemplateType(
    Map<TemplateType, JSType> map, TemplateType template, JSType resolved) {
  JSType previous = map.get(template);
  if (!resolved.isUnknownType()) {
    if (previous == null) {
      map.put(template, resolved);
    } else {
      JSType join = previous.getLeastSupertype(resolved);
      map.put(template, join);
    }
  }
}
 
Example 5
Source File: Closure_112_TypeInference_t.java    From coming with MIT License 5 votes vote down vote up
private static void resolvedTemplateType(
    Map<TemplateType, JSType> map, TemplateType template, JSType resolved) {
  JSType previous = map.get(template);
  if (!resolved.isUnknownType()) {
    if (previous == null) {
      map.put(template, resolved);
    } else {
      JSType join = previous.getLeastSupertype(resolved);
      map.put(template, join);
    }
  }
}
 
Example 6
Source File: Closure_19_ChainableReverseAbstractInterpreter_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public JSType caseUnionType(UnionType type) {
  JSType restricted = null;
  for (JSType alternate : type.getAlternates()) {
    JSType restrictedAlternate = alternate.visit(this);
    if (restrictedAlternate != null) {
      if (restricted == null) {
        restricted = restrictedAlternate;
      } else {
        restricted = restrictedAlternate.getLeastSupertype(restricted);
      }
    }
  }
  return restricted;
}
 
Example 7
Source File: Cardumen_0024_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public JSType caseUnionType(UnionType type) {
  JSType restricted = null;
  for (JSType alternate : type.getAlternates()) {
    JSType restrictedAlternate = alternate.visit(this);
    if (restrictedAlternate != null) {
      if (restricted == null) {
        restricted = restrictedAlternate;
      } else {
        restricted = restrictedAlternate.getLeastSupertype(restricted);
      }
    }
  }
  return restricted;
}
 
Example 8
Source File: Closure_7_ChainableReverseAbstractInterpreter_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public JSType caseUnionType(UnionType type) {
  JSType restricted = null;
  for (JSType alternate : type.getAlternates()) {
    JSType restrictedAlternate = alternate.visit(this);
    if (restrictedAlternate != null) {
      if (restricted == null) {
        restricted = restrictedAlternate;
      } else {
        restricted = restrictedAlternate.getLeastSupertype(restricted);
      }
    }
  }
  return restricted;
}
 
Example 9
Source File: Closure_7_ChainableReverseAbstractInterpreter_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public JSType caseUnionType(UnionType type) {
  JSType restricted = null;
  for (JSType alternate : type.getAlternates()) {
    JSType restrictedAlternate = alternate.visit(this);
    if (restrictedAlternate != null) {
      if (restricted == null) {
        restricted = restrictedAlternate;
      } else {
        restricted = restrictedAlternate.getLeastSupertype(restricted);
      }
    }
  }
  return restricted;
}
 
Example 10
Source File: Cardumen_0024_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public JSType caseUnionType(UnionType type) {
  JSType restricted = null;
  for (JSType alternate : type.getAlternates()) {
    JSType restrictedAlternate = alternate.visit(this);
    if (restrictedAlternate != null) {
      if (restricted == null) {
        restricted = restrictedAlternate;
      } else {
        restricted = restrictedAlternate.getLeastSupertype(restricted);
      }
    }
  }
  return restricted;
}
 
Example 11
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 12
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 13
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 14
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 15
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 16
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 17
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;
}