public abstract class JSType extends java.lang.Object implements TypeI, java.io.Serializable
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
.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
JSType.EqCache
cache used by equivalence check logic
|
(package private) static class |
JSType.ImplCache
cache used by check sub-type logic
|
private static class |
JSType.MatchCache
base cache data structure
|
(package private) static class |
JSType.MatchStatus
describe the status of checking that a function
implicitly implements an interface.
|
static class |
JSType.TypePair
a data structure that represents a pair of types
|
Modifier and Type | Field and Description |
---|---|
(package private) static java.util.Comparator<JSType> |
ALPHA
Total ordering on types based on their textual representation.
|
private static CanCastToVisitor |
CAN_CAST_TO_VISITOR |
private static com.google.common.collect.ImmutableList<java.lang.String> |
COVARIANT_TYPES |
private boolean |
inTemplatedCheckVisit |
(package private) JSTypeRegistry |
registry |
private boolean |
resolved |
private JSType |
resolveResult |
private static long |
serialVersionUID |
protected TemplateTypeMap |
templateTypeMap |
Constructor and Description |
---|
JSType(JSTypeRegistry registry) |
JSType(JSTypeRegistry registry,
TemplateTypeMap templateTypeMap) |
Modifier and Type | Method and Description |
---|---|
JSType |
autobox()
Dereference a type for property access.
|
JSType |
autoboxesTo()
Turn a scalar type to the corresponding object type.
|
boolean |
canBeCalled()
This predicate is used to test whether a given type can be used as the
'function' in a function call.
|
boolean |
canCastTo(JSType that)
Tests whether values of
this type can be safely assigned
to values of that type. |
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. |
(package private) boolean |
checkEquivalenceHelper(JSType that,
EquivalenceMethod eqMethod)
An equivalence visitor.
|
(package private) boolean |
checkEquivalenceHelper(JSType that,
EquivalenceMethod eqMethod,
JSType.EqCache eqCache) |
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 for
the purposes of data flow analysis. |
boolean |
equals(java.lang.Object jsType) |
void |
extendTemplateTypeMap(TemplateTypeMap otherMap)
Extends the template type map associated with this type, merging in the
keys and values of the specified map.
|
(package private) static JSType |
filterNoResolvedType(JSType type)
When computing infima, we may get a situation like
inf(Type1, Type2)
where both types are unresolved, so they're technically
subtypes of one another.
|
JSType |
findPropertyType(java.lang.String propertyName)
Coerces this type to an Object type, then gets the type of the property
whose name is given.
|
private java.lang.String |
getConcreteNominalTypeName(ObjectType objType) |
java.lang.String |
getDisplayName()
Returns a user meaningful label for the JSType instance.
|
JSType |
getGreatestSubtype(JSType that)
Gets the greatest subtype of
this and that . |
(package private) static JSType |
getGreatestSubtype(JSType thisType,
JSType thatType)
A generic implementation meant to be used as a helper for common
getGreatestSubtype implementations.
|
JSDocInfo |
getJSDocInfo()
Gets the docInfo for this type.
|
JSType |
getLeastSupertype(JSType that)
Gets the least supertype of
this and that . |
(package private) static JSType |
getLeastSupertype(JSType thisType,
JSType thatType)
A generic implementation meant to be used as a helper for common
getLeastSupertype implementations.
|
(package private) JSType |
getNativeType(JSTypeNative typeId)
Utility method for less verbose code.
|
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. |
TemplateTypeMap |
getTemplateTypeMap()
Returns the template type map associated with this type.
|
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 |
hasAnyTemplateTypes() |
(package private) boolean |
hasAnyTemplateTypesInternal() |
boolean |
hasDisplayName() |
int |
hashCode() |
boolean |
hasProperty(java.lang.String pname)
Checks whether the property is present on the object.
|
boolean |
isAllType() |
boolean |
isArrayType() |
boolean |
isBooleanObjectType() |
boolean |
isBooleanValueType() |
boolean |
isBottom() |
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 |
isDict()
Returns true iff
this can be a dict . |
boolean |
isEmptyType() |
boolean |
isEnumElementType() |
boolean |
isEnumType() |
static boolean |
isEquivalent(JSType typeA,
JSType typeB) |
boolean |
isEquivalentTo(TypeI that)
Checks if two types are equivalent.
|
boolean |
isEquivalentTo(TypeI that,
boolean isStructural) |
(package private) static boolean |
isExemptFromTemplateTypeInvariance(JSType type)
Determines if the specified type is exempt from standard invariant
templatized typing rules.
|
boolean |
isExplicitlyVoidable()
Tests whether this type explicitly allows undefined, as opposed to ? or *.
|
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 |
isNamedType() |
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 |
isStruct()
Returns true iff
this can be a struct . |
boolean |
isStructuralInterface() |
boolean |
isStructuralType() |
boolean |
isSubtype(JSType that)
Checks whether
this is a subtype of that . |
protected boolean |
isSubtype(JSType that,
JSType.ImplCache implicitImplCache)
checking isSubtype with structural interface matching
|
(package private) static boolean |
isSubtypeHelper(JSType thisType,
JSType thatType,
JSType.ImplCache implicitImplCache)
if implicitImplCache is null, then there will
be no structural interface matching
|
boolean |
isSubtypeOf(TypeI other) |
boolean |
isSubtypeWithoutStructuralTyping(JSType that)
the logic of this method is similar to isSubtype,
except that it does not perform structural interface matching
This function is added for disambiguate properties,
and is deprecated for the other use cases.
|
boolean |
isTemplateType() |
boolean |
isTemplatizedType() |
(package private) boolean |
isTheObjectType() |
boolean |
isUnionType() |
boolean |
isUnknownType() |
boolean |
isVoidable()
Tests whether this type is voidable.
|
boolean |
isVoidType() |
void |
matchConstraint(JSType constraint)
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,
StaticTypedScope<JSType> scope)
Resolve this type in the given scope.
|
(package private) abstract JSType |
resolveInternal(ErrorReporter t,
StaticTypedScope<JSType> scope) |
JSType |
restrictByNotNullOrUndefined()
If this is a union type, returns a union type that does not include
the null or undefined type.
|
(package private) static JSType |
safeResolve(JSType type,
ErrorReporter t,
StaticTypedScope<JSType> scope)
A null-safe resolve.
|
(package private) void |
setResolvedTypeInternal(JSType 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 . |
(package private) TernaryValue |
testForEqualityHelper(JSType aType,
JSType bType) |
java.lang.String |
toAnnotationString()
A string representation of this type, suitable for printing
in type annotations at code generation time.
|
java.lang.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().
|
NamedType |
toMaybeNamedType() |
ObjectType |
toMaybeObjectType() |
RecordType |
toMaybeRecordType()
Downcasts this to a RecordType, or returns null if this is not
a RecordType.
|
TemplateType |
toMaybeTemplateType()
Downcasts this to a TemplateType, or returns null if this is not
a function.
|
TemplatizedType |
toMaybeTemplatizedType()
Downcasts this to a TemplatizedType, or returns null if this is not
a function.
|
UnionType |
toMaybeUnionType()
Downcasts this to a UnionType, or returns null if this is not a UnionType.
|
java.lang.String |
toNonNullAnnotationString() |
ObjectType |
toObjectType()
Casts this to an ObjectType, or returns null if this is not an ObjectType.
|
java.lang.String |
toString()
A string representation of this type, suitable for printing
in warnings.
|
(package private) abstract java.lang.String |
toStringHelper(boolean forAnnotations) |
JSType |
unboxesTo()
Turn an object type to its corresponding scalar type.
|
(package private) abstract <T> T |
visit(RelationshipVisitor<T> visitor,
JSType that)
Visit the types with the given visitor.
|
abstract <T> T |
visit(Visitor<T> visitor)
Visit this type with the given visitor.
|
private static final long serialVersionUID
private boolean resolved
private JSType resolveResult
protected TemplateTypeMap templateTypeMap
private boolean inTemplatedCheckVisit
private static final CanCastToVisitor CAN_CAST_TO_VISITOR
private static final com.google.common.collect.ImmutableList<java.lang.String> COVARIANT_TYPES
static final java.util.Comparator<JSType> ALPHA
final JSTypeRegistry registry
JSType(JSTypeRegistry registry)
JSType(JSTypeRegistry registry, TemplateTypeMap templateTypeMap)
JSType getNativeType(JSTypeNative typeId)
public JSDocInfo getJSDocInfo()
public java.lang.String getDisplayName()
public boolean hasDisplayName()
public boolean hasProperty(java.lang.String pname)
pname
- The property name.public boolean isNoType()
public boolean isNoResolvedType()
public boolean isNoObjectType()
public final boolean isEmptyType()
public boolean isNumberObjectType()
public boolean isNumberValueType()
public boolean isFunctionPrototypeType()
public boolean isStringObjectType()
boolean isTheObjectType()
public boolean isStringValueType()
public final boolean isString()
this <: (String, string)
public final boolean isNumber()
this <: (Number, number)
public boolean isArrayType()
public boolean isBooleanObjectType()
public boolean isBooleanValueType()
public boolean isRegexpType()
public boolean isDateType()
public boolean isNullType()
public boolean isVoidType()
public boolean isAllType()
public boolean isUnknownType()
isUnknownType
in interface TypeI
public boolean isCheckedUnknownType()
public final boolean isUnionType()
public boolean isStruct()
this
can be a struct
.
UnionType overrides the method, assume this
is not a union here.public boolean isDict()
this
can be a dict
.
UnionType overrides the method, assume this
is not a union here.public UnionType toMaybeUnionType()
public final boolean isGlobalThisType()
public final boolean isFunctionType()
isFunctionType
in interface TypeI
public FunctionType toMaybeFunctionType()
toMaybeFunctionType
in interface TypeI
public static FunctionType toMaybeFunctionType(JSType type)
public final boolean isEnumElementType()
public EnumElementType toMaybeEnumElementType()
public boolean isEnumType()
public EnumType toMaybeEnumType()
public boolean isNamedType()
public NamedType toMaybeNamedType()
public boolean isRecordType()
public boolean isStructuralInterface()
public boolean isStructuralType()
public RecordType toMaybeRecordType()
public final boolean isTemplatizedType()
public TemplatizedType toMaybeTemplatizedType()
public final boolean isTemplateType()
public TemplateType toMaybeTemplateType()
public boolean hasAnyTemplateTypes()
boolean hasAnyTemplateTypesInternal()
public TemplateTypeMap getTemplateTypeMap()
public void extendTemplateTypeMap(TemplateTypeMap otherMap)
public boolean isObject()
Object
, or any subtype thereof.this <: Object
public boolean isConstructor()
FunctionType
that is a constructor or a
named type that points to such a type.isConstructor
in interface TypeI
public boolean isNominalType()
public final boolean isNominalConstructor()
public boolean isInstanceType()
InstanceObjectType
.public boolean isInterface()
FunctionType
that is an interface or a named
type that points to such a type.isInterface
in interface TypeI
public boolean isOrdinaryFunction()
FunctionType
that is an ordinary function or
a named type that points to such a type.public final boolean isEquivalentTo(TypeI that)
isEquivalentTo
in interface TypeI
public final boolean isEquivalentTo(TypeI that, boolean isStructural)
public final boolean differsFrom(JSType that)
that
type for
the purposes of data flow analysis.
This is a trickier check than pure equality, because it has to properly
handle unknown types. See EquivalenceMethod
for more info.boolean checkEquivalenceHelper(JSType that, EquivalenceMethod eqMethod)
boolean checkEquivalenceHelper(JSType that, EquivalenceMethod eqMethod, JSType.EqCache eqCache)
private java.lang.String getConcreteNominalTypeName(ObjectType objType)
public boolean equals(java.lang.Object jsType)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public final boolean matchesInt32Context()
Number
.public final boolean matchesUint32Context()
public boolean matchesNumberContext()
public boolean matchesStringContext()
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
.public boolean matchesObjectContext()
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.public JSType findPropertyType(java.lang.String propertyName)
ObjectType.getPropertyType(java.lang.String)
, returns null if the property
is not found.null
if the current type cannot
have properties, or if the type is not found.public boolean canBeCalled()
true
if this type might be callable.public boolean canCastTo(JSType that)
this
type can be safely assigned
to values of that
type.
The default implementation verifies that this
is a subtype
of that
.
public JSType autoboxesTo()
null
if this type is not a scalar.public JSType unboxesTo()
null
if this type does not unbox.public ObjectType toObjectType()
public JSType autobox()
public final ObjectType dereference()
public final boolean canTestForEqualityWith(JSType that)
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.public TernaryValue testForEquality(JSType that)
this
and that
.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 valuesTernaryValue testForEqualityHelper(JSType aType, JSType bType)
public final boolean canTestForShallowEqualityWith(JSType that)
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.public boolean isNullable()
public boolean isVoidable()
public boolean isExplicitlyVoidable()
public JSType collapseUnion()
public JSType getLeastSupertype(JSType that)
this
and that
.
The least supertype is the join (∨) or supremum of both types in the
type lattice.Examples:
number ∨ *
= *
number ∨ Object
= (number, Object)
Number ∨ Object
= Object
this ∨ that
static JSType getLeastSupertype(JSType thisType, JSType thatType)
public JSType getGreatestSubtype(JSType that)
this
and that
.
The greatest subtype is the meet (∧) or infimum of both types in the
type lattice.Examples
Number ∧ Any
= Any
number ∧ Object
= Any
Number ∧ Object
= Number
this ∨ that
static JSType getGreatestSubtype(JSType thisType, JSType thatType)
static JSType filterNoResolvedType(JSType type)
public JSType getRestrictedTypeGivenToBooleanOutcome(boolean outcome)
ToBoolean
predicate has a specific value. For more information
about the ToBoolean
predicate, see
getPossibleToBooleanOutcomes()
.outcome
- the value of the ToBoolean
predicatepublic abstract BooleanLiteralSet getPossibleToBooleanOutcomes()
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:
type | result |
---|---|
undefined | {false} |
null | {false} |
boolean | {true, false} |
number | {true, false} |
string | {true, false} |
Object | {true} |
public JSType.TypePair getTypesUnderEquality(JSType that)
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
.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
public JSType.TypePair getTypesUnderInequality(JSType that)
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.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
public JSType.TypePair getTypesUnderShallowEquality(JSType that)
this
and that
types under shallow
equality.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
.public JSType.TypePair getTypesUnderShallowInequality(JSType that)
this
and that
types under
shallow inequality.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
public JSType restrictByNotNullOrUndefined()
restrictByNotNullOrUndefined
in interface TypeI
public boolean isSubtypeWithoutStructuralTyping(JSType that)
public boolean isSubtype(JSType that)
this
is a subtype of that
.Note this function also returns true if this type structurally matches the protocol define by that type (if that type is an interface function type) Subtyping rules:
(T1, …, Tn) <: U
if and only
Tk <: U
for all k ∈ 1..n
.U <: (T1, …, Tn)
if and only
if U <: Tk
for some index k
.O1
is a subtype
of an object O2
if it has more properties
than O2
and all common properties are
pairwise subtypes.this <: that
protected boolean isSubtype(JSType that, JSType.ImplCache implicitImplCache)
implicitImplCache
- a cache that records the checked
or currently checking type pairs, for example, if previous
checking found that constructor C is a subtype of interface I,
then in the cache, table key <I,C>
maps to IMPLEMENT status.that
- static boolean isSubtypeHelper(JSType thisType, JSType thatType, JSType.ImplCache implicitImplCache)
static boolean isExemptFromTemplateTypeInvariance(JSType type)
public abstract <T> T visit(Visitor<T> visitor)
Visitor
abstract <T> T visit(RelationshipVisitor<T> visitor, JSType that)
RelationshipVisitor
public final JSType resolve(ErrorReporter t, StaticTypedScope<JSType> scope)
this
, as defined by
isEquivalentTo(com.google.javascript.rhino.TypeI)
. 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.abstract JSType resolveInternal(ErrorReporter t, StaticTypedScope<JSType> scope)
void setResolvedTypeInternal(JSType type)
public final boolean isResolved()
static final JSType safeResolve(JSType type, ErrorReporter t, StaticTypedScope<JSType> scope)
public boolean setValidator(com.google.common.base.Predicate<JSType> validator)
@extends
annotation must be an
object. Clients should inject a validator that emits a warning
if the type does not validate, and return false.public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toDebugHashCodeString()
public final java.lang.String toAnnotationString()
public final java.lang.String toNonNullAnnotationString()
abstract java.lang.String toStringHelper(boolean forAnnotations)
forAnnotations
- Whether this is for use in code generator
annotations. Otherwise, it's for warnings.public void matchConstraint(JSType constraint)
constraint
- public boolean isSubtypeOf(TypeI other)
isSubtypeOf
in interface TypeI
public ObjectType toMaybeObjectType()
toMaybeObjectType
in interface TypeI