com.google.javascript.rhino.jstype
Class JSType

java.lang.Object
  extended by com.google.javascript.rhino.jstype.JSType
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
AllType, BooleanType, NullType, NumberType, ObjectType, StringType, UnionType, VoidType

public abstract class JSType
extends Object
implements Serializable

Represents JavaScript value types.

Types are split into two separate families: value types and object types. A special UnknownType exists to represent a wildcard type on which no information can be gathered. In particular, it can assign to everyone, is a subtype of everyone (and everyone is a subtype of it).

If you remove the UnknownType, the set of types in the type system forms a lattice with the isSubtype(com.google.javascript.rhino.jstype.JSType) relation defining the partial order of types. All types are united at the top of the lattice by the AllType and at the bottom by the NoType.

See Also:
Serialized Form

Nested Class Summary
static class JSType.TypePair
           
 
Field Summary
static String EMPTY_TYPE_COMPONENT
           
static int ENUMDECL
           
static String NOT_A_CLASS
           
static String NOT_A_TYPE
           
static int NOT_ENUMDECL
           
static String UNKNOWN_NAME
           
 
Method Summary
 JSType autobox()
          Dereference a type for property access.
 JSType autoboxesTo()
          Gets the type to which this type auto-boxes.
 boolean canAssignTo(JSType that)
          Tests whether values of this type can be safely assigned to values of that type.
 boolean canBeCalled()
          This predicate is used to test whether a given type can be used as the 'function' in a function call.
 boolean canTestForEqualityWith(JSType that)
          Tests whether this and that are meaningfully comparable.
 boolean canTestForShallowEqualityWith(JSType that)
          Tests whether this and that are meaningfully comparable using shallow comparison.
 void clearResolved()
          Clears the resolved field.
 JSType collapseUnion()
          Gets the least supertype of this that's not a union.
 ObjectType dereference()
          Dereference a type for property access.
 boolean differsFrom(JSType that)
          Whether this type is meaningfully different from that type.
 boolean equals(Object jsType)
           
 JSType findPropertyType(String propertyName)
          Coerces this type to an Object type, then gets the type of the property whose name is given.
 JSType forceResolve(ErrorReporter t, StaticScope<JSType> scope)
          Force this type to resolve, even if the registry is in a lazy resolving mode.
 String getDisplayName()
          Returns a user meaningful label for the JSType instance.
 JSType getGreatestSubtype(JSType that)
          Gets the greatest subtype of this and that.
 JSDocInfo getJSDocInfo()
          Gets the docInfo for this type.
 JSType getLeastSupertype(JSType that)
          Gets the least supertype of this and that.
abstract  BooleanLiteralSet getPossibleToBooleanOutcomes()
          Computes the set of possible outcomes of the ToBoolean predicate for this type.
 JSType getRestrictedTypeGivenToBooleanOutcome(boolean outcome)
          Computes the restricted type of this type knowing that the ToBoolean predicate has a specific value.
 JSType.TypePair getTypesUnderEquality(JSType that)
          Computes the subset of this and that types if equality is observed.
 JSType.TypePair getTypesUnderInequality(JSType that)
          Computes the subset of this and that types if inequality is observed.
 JSType.TypePair getTypesUnderShallowEquality(JSType that)
          Computes the subset of this and that types under shallow equality.
 JSType.TypePair getTypesUnderShallowInequality(JSType that)
          Computes the subset of this and that types under shallow inequality.
 boolean hasDisplayName()
           
 int hashCode()
           
 boolean isAllType()
           
 boolean isArrayType()
           
 boolean isBooleanObjectType()
           
 boolean isBooleanValueType()
           
 boolean isCheckedUnknownType()
           
 boolean isConstructor()
          Whether this type is a FunctionType that is a constructor or a named type that points to such a type.
 boolean isDateType()
           
 boolean isEmptyType()
           
 boolean isEnumElementType()
           
 boolean isEnumType()
           
static boolean isEquivalent(JSType typeA, JSType typeB)
           
 boolean isEquivalentTo(JSType jsType)
          Checks if two types are equivalent.
 boolean isFunctionPrototypeType()
          Whether this is the prototype of a function.
 boolean isFunctionType()
          Returns true if toMaybeFunctionType returns a non-null FunctionType.
 boolean isGlobalThisType()
          Returns true if this is a global this type.
 boolean isInstanceType()
          Whether this type is an Instance object of some constructor.
 boolean isInterface()
          Whether this type is a FunctionType that is an interface or a named type that points to such a type.
 boolean isNominalConstructor()
          Whether this type is the original constructor of a nominal type.
 boolean isNominalType()
          Whether this type is a nominal type (a named instance object or a named enum).
 boolean isNoObjectType()
           
 boolean isNoResolvedType()
           
 boolean isNoType()
           
 boolean isNullable()
          Tests whether this type is nullable.
 boolean isNullType()
           
 boolean isNumber()
          Tests whether the type is a number (value or Object).
 boolean isNumberObjectType()
           
 boolean isNumberValueType()
           
 boolean isObject()
          Tests whether this type is an Object, or any subtype thereof.
 boolean isOrdinaryFunction()
          Whether this type is a FunctionType that is an ordinary function or a named type that points to such a type.
 boolean isRecordType()
           
 boolean isRegexpType()
           
 boolean isResolved()
          Whether the type has been resolved.
 boolean isString()
          Tests whether the type is a string (value or Object).
 boolean isStringObjectType()
           
 boolean isStringValueType()
           
 boolean isSubtype(JSType that)
          Checks whether this is a subtype of that.
 boolean isTemplateType()
           
 boolean isUnionType()
           
 boolean isUnknownType()
           
 boolean isVoidType()
           
 void matchConstraint(ObjectType contraint)
          Modify this type so that it matches the specified type.
 boolean matchesInt32Context()
          This predicate is used to test whether a given type can appear in a 'Int32' context.
 boolean matchesNumberContext()
          This predicate is used to test whether a given type can appear in a numeric context, such as an operand of a multiply operator.
 boolean matchesObjectContext()
          This predicate is used to test whether a given type can appear in an Object context, such as the expression in a with statement.
 boolean matchesStringContext()
          This predicate is used to test whether a given type can appear in a String context, such as an operand of a string concat (+) operator.
 boolean matchesUint32Context()
          This predicate is used to test whether a given type can appear in a 'Uint32' context.
 JSType resolve(ErrorReporter t, StaticScope<JSType> scope)
          Resolve this type in the given scope.
 JSType restrictByNotNullOrUndefined()
          If this is a union type, returns a union type that does not include the null or undefined type.
 boolean setValidator(com.google.common.base.Predicate<JSType> validator)
          Certain types have constraints on them at resolution-time.
 TernaryValue testForEquality(JSType that)
          Compares this and that.
 String toAnnotationString()
          A string representation of this type, suitable for printing in type annotations at code generation time.
 String toDebugHashCodeString()
          A hash code function for diagnosing complicated issues around type-identity.
 EnumElementType toMaybeEnumElementType()
          Downcasts this to an EnumElementType, or returns null if this is not an EnumElementType.
 EnumType toMaybeEnumType()
          Downcasts this to an EnumType, or returns null if this is not an EnumType.
 FunctionType toMaybeFunctionType()
          Downcasts this to a FunctionType, or returns null if this is not a function.
static FunctionType toMaybeFunctionType(JSType type)
          Null-safe version of toMaybeFunctionType().
 UnionType toMaybeUnionType()
          Downcasts this to a UnionType, or returns null if this is not a UnionType.
 ObjectType toObjectType()
          Casts this to an ObjectType, or returns null if this is not an ObjectType.
 String toString()
          A string representation of this type, suitable for printing in warnings.
 JSType unboxesTo()
          Gets the type to which this type unboxes.
abstract
<T> T
visit(Visitor<T> visitor)
          Visit this type with the given visitor.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

UNKNOWN_NAME

public static final String UNKNOWN_NAME
See Also:
Constant Field Values

NOT_A_CLASS

public static final String NOT_A_CLASS
See Also:
Constant Field Values

NOT_A_TYPE

public static final String NOT_A_TYPE
See Also:
Constant Field Values

EMPTY_TYPE_COMPONENT

public static final String EMPTY_TYPE_COMPONENT
See Also:
Constant Field Values

ENUMDECL

public static final int ENUMDECL
See Also:
Constant Field Values

NOT_ENUMDECL

public static final int NOT_ENUMDECL
See Also:
Constant Field Values
Method Detail

getJSDocInfo

public JSDocInfo getJSDocInfo()
Gets the docInfo for this type. By default, documentation cannot be attached to arbitrary types. This must be overridden for programmer-defined types.


getDisplayName

public String getDisplayName()
Returns a user meaningful label for the JSType instance. For example, Functions and Enums will return their declaration name (if they have one). Some types will not have a meaningful display name. Calls to hasDisplayName() will return true IFF getDisplayName() will return null or a zero length string.

Returns:
the display name of the type, or null if one is not available

hasDisplayName

public boolean hasDisplayName()
Returns:
true if the JSType has a user meaningful label.

isNoType

public boolean isNoType()

isNoResolvedType

public boolean isNoResolvedType()

isNoObjectType

public boolean isNoObjectType()

isEmptyType

public final boolean isEmptyType()

isNumberObjectType

public boolean isNumberObjectType()

isNumberValueType

public boolean isNumberValueType()

isFunctionPrototypeType

public boolean isFunctionPrototypeType()
Whether this is the prototype of a function.


isStringObjectType

public boolean isStringObjectType()

isStringValueType

public boolean isStringValueType()

isString

public final boolean isString()
Tests whether the type is a string (value or Object).

Returns:
this &lt;: (String, string)

isNumber

public final boolean isNumber()
Tests whether the type is a number (value or Object).

Returns:
this &lt;: (Number, number)

isArrayType

public boolean isArrayType()

isBooleanObjectType

public boolean isBooleanObjectType()

isBooleanValueType

public boolean isBooleanValueType()

isRegexpType

public boolean isRegexpType()

isDateType

public boolean isDateType()

isNullType

public boolean isNullType()

isVoidType

public boolean isVoidType()

isAllType

public boolean isAllType()

isUnknownType

public boolean isUnknownType()

isCheckedUnknownType

public boolean isCheckedUnknownType()

isUnionType

public final boolean isUnionType()

toMaybeUnionType

public UnionType toMaybeUnionType()
Downcasts this to a UnionType, or returns null if this is not a UnionType. Named in honor of Haskell's Maybe type constructor.


isGlobalThisType

public final boolean isGlobalThisType()
Returns true if this is a global this type.


isFunctionType

public final boolean isFunctionType()
Returns true if toMaybeFunctionType returns a non-null FunctionType.


toMaybeFunctionType

public FunctionType toMaybeFunctionType()
Downcasts this to a FunctionType, or returns null if this is not a function. For the purposes of this function, we define a MaybeFunctionType as any type in the sub-lattice { x | LEAST_FUNCTION_TYPE <= x <= GREATEST_FUNCTION_TYPE } This definition excludes bottom types like NoType and NoObjectType. This definition is somewhat arbitrary and axiomatic, but this is the definition that makes the most sense for the most callers.


toMaybeFunctionType

public static FunctionType toMaybeFunctionType(JSType type)
Null-safe version of toMaybeFunctionType().


isEnumElementType

public final boolean isEnumElementType()

toMaybeEnumElementType

public EnumElementType toMaybeEnumElementType()
Downcasts this to an EnumElementType, or returns null if this is not an EnumElementType.


isEnumType

public boolean isEnumType()

toMaybeEnumType

public EnumType toMaybeEnumType()
Downcasts this to an EnumType, or returns null if this is not an EnumType.


isRecordType

public boolean isRecordType()

isTemplateType

public boolean isTemplateType()

isObject

public boolean isObject()
Tests whether this type is an Object, or any subtype thereof.

Returns:
this &lt;: Object

isConstructor

public boolean isConstructor()
Whether this type is a FunctionType that is a constructor or a named type that points to such a type.


isNominalType

public boolean isNominalType()
Whether this type is a nominal type (a named instance object or a named enum).


isNominalConstructor

public final boolean isNominalConstructor()
Whether this type is the original constructor of a nominal type. Does not include structural constructors.


isInstanceType

public boolean isInstanceType()
Whether this type is an Instance object of some constructor. Does not necessarily mean this is an InstanceObjectType.


isInterface

public boolean isInterface()
Whether this type is a FunctionType that is an interface or a named type that points to such a type.


isOrdinaryFunction

public boolean isOrdinaryFunction()
Whether this type is a FunctionType that is an ordinary function or a named type that points to such a type.


isEquivalentTo

public boolean isEquivalentTo(JSType jsType)
Checks if two types are equivalent.


isEquivalent

public static boolean isEquivalent(JSType typeA,
                                   JSType typeB)

equals

public boolean equals(Object jsType)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

matchesInt32Context

public final boolean matchesInt32Context()
This predicate is used to test whether a given type can appear in a 'Int32' context. This context includes, for example, the operands of a bitwise or operator. Since we do not currently support integer types, this is a synonym for Number.


matchesUint32Context

public final boolean matchesUint32Context()
This predicate is used to test whether a given type can appear in a 'Uint32' context. This context includes the right-hand operand of a shift operator.


matchesNumberContext

public boolean matchesNumberContext()
This predicate is used to test whether a given type can appear in a numeric context, such as an operand of a multiply operator.


matchesStringContext

public boolean matchesStringContext()
This predicate is used to test whether a given type can appear in a String context, such as an operand of a string concat (+) operator. All types have at least the potential for converting to String. When we add externally defined types, such as a browser OM, we may choose to add types that do not automatically convert to String.


matchesObjectContext

public boolean matchesObjectContext()
This predicate is used to test whether a given type can appear in an Object context, such as the expression in a with statement. Most types we will encounter, except notably null, have at least the potential for converting to Object. Host defined objects can get peculiar.


findPropertyType

public JSType findPropertyType(String propertyName)
Coerces this type to an Object type, then gets the type of the property whose name is given. Unlike ObjectType.getPropertyType(java.lang.String), returns null if the property is not found.

Returns:
The property's type. null if the current type cannot have properties, or if the type is not found.

canBeCalled

public boolean canBeCalled()
This predicate is used to test whether a given type can be used as the 'function' in a function call.

Returns:
true if this type might be callable.

canAssignTo

public boolean canAssignTo(JSType that)
Tests whether values of this type can be safely assigned to values of that type.

The default implementation verifies that this is a subtype of that.


autoboxesTo

public JSType autoboxesTo()
Gets the type to which this type auto-boxes.

Returns:
the auto-boxed type or null if this type does not auto-box

unboxesTo

public JSType unboxesTo()
Gets the type to which this type unboxes.

Returns:
the unboxed type or null if this type does not unbox.

toObjectType

public ObjectType toObjectType()
Casts this to an ObjectType, or returns null if this is not an ObjectType. Does not change the underlying JS type. If you want to simulate JS autoboxing or dereferencing, you should use autoboxesTo() or dereference(). Those methods may change the underlying JS type.


autobox

public JSType autobox()
Dereference a type for property access. Autoboxes the type, and filters null/undefined, and returns the result.


dereference

public final ObjectType dereference()
Dereference a type for property access. Autoboxes the type, filters null/undefined, and returns the result iff it's an object.


canTestForEqualityWith

public final boolean canTestForEqualityWith(JSType that)
Tests whether this and that are meaningfully comparable. By meaningfully, we mean compatible types that do not lead to step 22 of the definition of the Abstract Equality Comparison Algorithm (11.9.3, page 55–56) of the ECMA-262 specification.


testForEquality

public TernaryValue testForEquality(JSType that)
Compares this and that.

Returns:
  • TernaryValue.TRUE if the comparison of values of this type and that always succeed (such as undefined compared to null)
  • TernaryValue.FALSE if the comparison of values of this type and that always fails (such as undefined compared to number)
  • TernaryValue.UNKNOWN if the comparison can succeed or fail depending on the concrete values

canTestForShallowEqualityWith

public final boolean canTestForShallowEqualityWith(JSType that)
Tests whether this and that are meaningfully comparable using shallow comparison. By meaningfully, we mean compatible types that are not rejected by step 1 of the definition of the Strict Equality Comparison Algorithm (11.9.6, page 56–57) of the ECMA-262 specification.


isNullable

public boolean isNullable()
Tests whether this type is nullable.


collapseUnion

public JSType collapseUnion()
Gets the least supertype of this that's not a union.


getLeastSupertype

public JSType getLeastSupertype(JSType that)
Gets the least supertype of this and that. The least supertype is the join (∨) or supremum of both types in the type lattice.

Examples:

Returns:
this &#8744; that

getGreatestSubtype

public JSType getGreatestSubtype(JSType that)
Gets the greatest subtype of this and that. The greatest subtype is the meet (∧) or infimum of both types in the type lattice.

Examples

Returns:
this &#8744; that

getRestrictedTypeGivenToBooleanOutcome

public JSType getRestrictedTypeGivenToBooleanOutcome(boolean outcome)
Computes the restricted type of this type knowing that the ToBoolean predicate has a specific value. For more information about the ToBoolean predicate, see getPossibleToBooleanOutcomes().

Parameters:
outcome - the value of the ToBoolean predicate
Returns:
the restricted type, or the Any Type if the underlying type could not have yielded this ToBoolean value TODO(user): Move this method to the SemanticRAI and use the visit method of types to get the restricted type.

getPossibleToBooleanOutcomes

public abstract BooleanLiteralSet getPossibleToBooleanOutcomes()
Computes the set of possible outcomes of the ToBoolean predicate for this type. The ToBoolean predicate is defined by the ECMA-262 standard, 3rd edition. Its behavior for simple types can be summarized by the following table:
typeresult
undefined{false}
null{false}
boolean{true, false}
number{true, false}
string{true, false}
Object{true}

Returns:
the set of boolean literals for this type

getTypesUnderEquality

public JSType.TypePair getTypesUnderEquality(JSType that)
Computes the subset of this and that types if equality is observed. If a value v1 of type null is equal to a value v2 of type (undefined,number), we can infer that the type of v1 is null and the type of v2 is undefined.

Returns:
a pair containing the restricted type of this as the first component and the restricted type of that as the second element. The returned pair is never null even though its components may be null

getTypesUnderInequality

public JSType.TypePair getTypesUnderInequality(JSType that)
Computes the subset of this and that types if inequality is observed. If a value v1 of type number is not equal to a value v2 of type (undefined,number), we can infer that the type of v1 is number and the type of v2 is number as well.

Returns:
a pair containing the restricted type of this as the first component and the restricted type of that as the second element. The returned pair is never null even though its components may be null

getTypesUnderShallowEquality

public JSType.TypePair getTypesUnderShallowEquality(JSType that)
Computes the subset of this and that types under shallow equality.

Returns:
a pair containing the restricted type of this as the first component and the restricted type of that as the second element. The returned pair is never null even though its components may be null.

getTypesUnderShallowInequality

public JSType.TypePair getTypesUnderShallowInequality(JSType that)
Computes the subset of this and that types under shallow inequality.

Returns:
A pair containing the restricted type of this as the first component and the restricted type of that as the second element. The returned pair is never null even though its components may be null

restrictByNotNullOrUndefined

public JSType restrictByNotNullOrUndefined()
If this is a union type, returns a union type that does not include the null or undefined type.


isSubtype

public boolean isSubtype(JSType that)
Checks whether this is a subtype of that.

Subtyping rules:

Returns:
this &lt;: that

differsFrom

public boolean differsFrom(JSType that)
Whether this type is meaningfully different from that type. This is a trickier check than pure equality, because it has to properly handle unknown types.

See Also:
Unknown unknowns

visit

public abstract <T> T visit(Visitor<T> visitor)
Visit this type with the given visitor.

Returns:
the value returned by the visitor
See Also:
Visitor

forceResolve

public final JSType forceResolve(ErrorReporter t,
                                 StaticScope<JSType> scope)
Force this type to resolve, even if the registry is in a lazy resolving mode.

See Also:
resolve(com.google.javascript.rhino.ErrorReporter, com.google.javascript.rhino.jstype.StaticScope)

resolve

public final JSType resolve(ErrorReporter t,
                            StaticScope<JSType> scope)
Resolve this type in the given scope. The returned value must be equal to this, as defined by isEquivalentTo(com.google.javascript.rhino.jstype.JSType). It may or may not be the same object. This method may modify the internal state of this, as long as it does so in a way that preserves Object equality. For efficiency, we should only resolve a type once per compilation job. For incremental compilations, one compilation job may need the artifacts from a previous generation, so we will eventually need a generational flag instead of a boolean one.


isResolved

public final boolean isResolved()
Whether the type has been resolved.


clearResolved

public final void clearResolved()
Clears the resolved field.


setValidator

public boolean setValidator(com.google.common.base.Predicate<JSType> validator)
Certain types have constraints on them at resolution-time. For example, a type in an @extends annotation must be an object. Clients should inject a validator that emits a warning if the type does not validate, and return false.


toString

public String toString()
A string representation of this type, suitable for printing in warnings.

Overrides:
toString in class Object

toDebugHashCodeString

public String toDebugHashCodeString()
A hash code function for diagnosing complicated issues around type-identity.


toAnnotationString

public final String toAnnotationString()
A string representation of this type, suitable for printing in type annotations at code generation time.


matchConstraint

public void matchConstraint(ObjectType contraint)
Modify this type so that it matches the specified type. This is useful for reverse type-inference, where we want to infer that an object literal matches its contraint (much like how the java compiler does reverse-inference to figure out generics).