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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#getGreatestSubtype() . 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_7_ChainableReverseAbstractInterpreter_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public JSType caseObjectType(ObjectType type) {
  if (value.equals("function")) {
    JSType ctorType = getNativeType(U2U_CONSTRUCTOR_TYPE);
    if (resultEqualsValue) {
      // Objects are restricted to "Function", subtypes are left
      return ctorType.getGreatestSubtype(type);
    } else {
      // Only filter out subtypes of "function"
      return type.isSubtype(ctorType) ? null : type;
    }
  }
  return matchesExpectation("object") ? type : null;
}
 
Example 2
Source File: Closure_25_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
private FlowScope tightenTypesAfterAssertions(FlowScope scope,
    Node callNode) {
  Node left = callNode.getFirstChild();
  Node firstParam = left.getNext();
  AssertionFunctionSpec assertionFunctionSpec =
      assertionFunctionsMap.get(left.getQualifiedName());
  if (assertionFunctionSpec == null || firstParam == null) {
    return scope;
  }
  Node assertedNode = assertionFunctionSpec.getAssertedParam(firstParam);
  if (assertedNode == null) {
    return scope;
  }
  JSTypeNative assertedType = assertionFunctionSpec.getAssertedType();
  String assertedNodeName = assertedNode.getQualifiedName();

  JSType narrowed;
  // Handle assertions that enforce expressions evaluate to true.
  if (assertedType == null) {
    // Handle arbitrary expressions within the assert.
    scope = reverseInterpreter.getPreciserScopeKnowingConditionOutcome(
        assertedNode, scope, true);
    // Build the result of the assertExpression
    narrowed = getJSType(assertedNode).restrictByNotNullOrUndefined();
  } else {
    // Handle assertions that enforce expressions are of a certain type.
    JSType type = getJSType(assertedNode);
    narrowed = type.getGreatestSubtype(getNativeType(assertedType));
    if (assertedNodeName != null && type.differsFrom(narrowed)) {
      scope = narrowScope(scope, assertedNode, narrowed);
    }
  }

  if (getJSType(callNode).differsFrom(narrowed)) {
    callNode.setJSType(narrowed);
  }
  return scope;
}
 
Example 3
Source File: Closure_25_TypeInference_t.java    From coming with MIT License 5 votes vote down vote up
private FlowScope tightenTypesAfterAssertions(FlowScope scope,
    Node callNode) {
  Node left = callNode.getFirstChild();
  Node firstParam = left.getNext();
  AssertionFunctionSpec assertionFunctionSpec =
      assertionFunctionsMap.get(left.getQualifiedName());
  if (assertionFunctionSpec == null || firstParam == null) {
    return scope;
  }
  Node assertedNode = assertionFunctionSpec.getAssertedParam(firstParam);
  if (assertedNode == null) {
    return scope;
  }
  JSTypeNative assertedType = assertionFunctionSpec.getAssertedType();
  String assertedNodeName = assertedNode.getQualifiedName();

  JSType narrowed;
  // Handle assertions that enforce expressions evaluate to true.
  if (assertedType == null) {
    // Handle arbitrary expressions within the assert.
    scope = reverseInterpreter.getPreciserScopeKnowingConditionOutcome(
        assertedNode, scope, true);
    // Build the result of the assertExpression
    narrowed = getJSType(assertedNode).restrictByNotNullOrUndefined();
  } else {
    // Handle assertions that enforce expressions are of a certain type.
    JSType type = getJSType(assertedNode);
    narrowed = type.getGreatestSubtype(getNativeType(assertedType));
    if (assertedNodeName != null && type.differsFrom(narrowed)) {
      scope = narrowScope(scope, assertedNode, narrowed);
    }
  }

  if (getJSType(callNode).differsFrom(narrowed)) {
    callNode.setJSType(narrowed);
  }
  return scope;
}
 
Example 4
Source File: Closure_112_TypeInference_t.java    From coming with MIT License 5 votes vote down vote up
private FlowScope tightenTypesAfterAssertions(FlowScope scope,
    Node callNode) {
  Node left = callNode.getFirstChild();
  Node firstParam = left.getNext();
  AssertionFunctionSpec assertionFunctionSpec =
      assertionFunctionsMap.get(left.getQualifiedName());
  if (assertionFunctionSpec == null || firstParam == null) {
    return scope;
  }
  Node assertedNode = assertionFunctionSpec.getAssertedParam(firstParam);
  if (assertedNode == null) {
    return scope;
  }
  JSType assertedType = assertionFunctionSpec.getAssertedType(
      callNode, registry);
  String assertedNodeName = assertedNode.getQualifiedName();

  JSType narrowed;
  // Handle assertions that enforce expressions evaluate to true.
  if (assertedType == null) {
    // Handle arbitrary expressions within the assert.
    scope = reverseInterpreter.getPreciserScopeKnowingConditionOutcome(
        assertedNode, scope, true);
    // Build the result of the assertExpression
    narrowed = getJSType(assertedNode).restrictByNotNullOrUndefined();
  } else {
    // Handle assertions that enforce expressions are of a certain type.
    JSType type = getJSType(assertedNode);
    narrowed = type.getGreatestSubtype(assertedType);
    if (assertedNodeName != null && type.differsFrom(narrowed)) {
      scope = narrowScope(scope, assertedNode, narrowed);
    }
  }

  callNode.setJSType(narrowed);
  return scope;
}
 
Example 5
Source File: Closure_112_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
private FlowScope tightenTypesAfterAssertions(FlowScope scope,
    Node callNode) {
  Node left = callNode.getFirstChild();
  Node firstParam = left.getNext();
  AssertionFunctionSpec assertionFunctionSpec =
      assertionFunctionsMap.get(left.getQualifiedName());
  if (assertionFunctionSpec == null || firstParam == null) {
    return scope;
  }
  Node assertedNode = assertionFunctionSpec.getAssertedParam(firstParam);
  if (assertedNode == null) {
    return scope;
  }
  JSType assertedType = assertionFunctionSpec.getAssertedType(
      callNode, registry);
  String assertedNodeName = assertedNode.getQualifiedName();

  JSType narrowed;
  // Handle assertions that enforce expressions evaluate to true.
  if (assertedType == null) {
    // Handle arbitrary expressions within the assert.
    scope = reverseInterpreter.getPreciserScopeKnowingConditionOutcome(
        assertedNode, scope, true);
    // Build the result of the assertExpression
    narrowed = getJSType(assertedNode).restrictByNotNullOrUndefined();
  } else {
    // Handle assertions that enforce expressions are of a certain type.
    JSType type = getJSType(assertedNode);
    narrowed = type.getGreatestSubtype(assertedType);
    if (assertedNodeName != null && type.differsFrom(narrowed)) {
      scope = narrowScope(scope, assertedNode, narrowed);
    }
  }

  callNode.setJSType(narrowed);
  return scope;
}
 
Example 6
Source File: PureFunctionIdentifier.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return Whether the jstype is something known to be a local value.
 */
private boolean isLocalValueType(JSType jstype, boolean recurse) {
  Preconditions.checkNotNull(jstype);
  JSType subtype =  jstype.getGreatestSubtype(
      compiler.getTypeRegistry().getNativeType(JSTypeNative.OBJECT_TYPE));
  // If the type includes anything related to a object type, don't assume
  // anything about the locality of the value.
  return subtype.isNoType();
}
 
Example 7
Source File: TypeInference.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private FlowScope tightenTypesAfterAssertions(FlowScope scope,
    Node callNode) {
  Node left = callNode.getFirstChild();
  Node firstParam = left.getNext();
  AssertionFunctionSpec assertionFunctionSpec =
      assertionFunctionsMap.get(left.getQualifiedName());
  if (assertionFunctionSpec == null || firstParam == null) {
    return scope;
  }
  Node assertedNode = assertionFunctionSpec.getAssertedParam(firstParam);
  if (assertedNode == null) {
    return scope;
  }
  JSType assertedType = assertionFunctionSpec.getAssertedType(
      callNode, registry);
  String assertedNodeName = assertedNode.getQualifiedName();

  JSType narrowed;
  // Handle assertions that enforce expressions evaluate to true.
  if (assertedType == null) {
    // Handle arbitrary expressions within the assert.
    scope = reverseInterpreter.getPreciserScopeKnowingConditionOutcome(
        assertedNode, scope, true);
    // Build the result of the assertExpression
    narrowed = getJSType(assertedNode).restrictByNotNullOrUndefined();
  } else {
    // Handle assertions that enforce expressions are of a certain type.
    JSType type = getJSType(assertedNode);
    narrowed = type.getGreatestSubtype(assertedType);
    if (assertedNodeName != null && type.differsFrom(narrowed)) {
      scope = narrowScope(scope, assertedNode, narrowed);
    }
  }

  callNode.setJSType(narrowed);
  return scope;
}
 
Example 8
Source File: SemanticReverseAbstractInterpreter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private JSType applyCommonRestriction(JSType type) {
  if (target.isUnknownType()) {
    return type;
  }

  FunctionType funcTarget = target.toMaybeFunctionType();
  if (funcTarget.hasInstanceType()) {
    return type.getGreatestSubtype(funcTarget.getInstanceType());
  }

  return null;
}
 
Example 9
Source File: ChainableReverseAbstractInterpreter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public JSType caseObjectType(ObjectType type) {
  if (value.equals("function")) {
    JSType ctorType = getNativeType(U2U_CONSTRUCTOR_TYPE);
    if (resultEqualsValue) {
      // Objects are restricted to "Function", subtypes are left
      return ctorType.getGreatestSubtype(type);
    } else {
      // Only filter out subtypes of "function"
      return type.isSubtype(ctorType) ? null : type;
    }
  }
  return matchesExpectation("object") ? type : null;
}