public class JSTypeRegistry extends java.lang.Object implements TypeIRegistry, java.io.Serializable
This class is not thread-safe.
Modifier and Type | Field and Description |
---|---|
private TemplateType |
arrayElementTemplateKey
The template variable in
Array<T> . |
private java.util.Map<java.lang.String,java.util.Map<java.lang.String,ObjectType>> |
eachRefTypeIndexedByProperty |
private TemplateTypeMap |
emptyTemplateTypeMap |
private java.util.Set<java.lang.String> |
forwardDeclaredTypes |
private java.util.Map<java.lang.String,JSType> |
greatestSubtypeByProperty |
private static java.lang.String |
I_OBJECT_ELEMENT_TEMPLATE |
private com.google.common.collect.Multimap<java.lang.String,FunctionType> |
interfaceToImplementors |
private TemplateType |
iObjectElementTemplateKey
The template variable corresponding to the VALUE type in
IObject<KEY, VALUE> (plus the builtin Javascript Object). |
private TemplateType |
iObjectIndexTemplateKey
The template variable corresponding to the KEY type in
IObject<KEY, VALUE> (plus the builtin Javascript Object). |
private boolean |
lastGeneration |
private java.util.Map<java.lang.String,JSType> |
namesToTypes |
private JSType[] |
nativeTypes |
private java.util.Set<java.lang.String> |
nonNullableTypeNames |
static java.lang.String |
OBJECT_ELEMENT_TEMPLATE
Deprecated.
|
private static int |
PROPERTY_CHECKING_UNION_SIZE
The UnionTypeBuilder caps the maximum number of alternate types it
remembers and then defaults to "?" (unknown type).
|
private ErrorReporter |
reporter |
private com.google.common.collect.Multimap<StaticTypedScope<JSType>,NamedType> |
resolvedNamedTypes |
private static long |
serialVersionUID |
private java.util.Map<java.lang.String,TemplateType> |
templateTypes |
private java.util.Map<java.lang.String,UnionTypeBuilder> |
typesIndexedByProperty |
private com.google.common.collect.Multimap<StaticTypedScope<JSType>,NamedType> |
unresolvedNamedTypes |
Constructor and Description |
---|
JSTypeRegistry(ErrorReporter reporter)
Constructs a new type registry populated with the built-in types.
|
Modifier and Type | Method and Description |
---|---|
private void |
addReferenceTypeIndexedByProperty(java.lang.String propertyName,
JSType type) |
boolean |
canPropertyBeDefined(JSType type,
java.lang.String propertyName)
Returns whether the given property can possibly be set on the given type.
|
void |
clearNamedTypes()
Flushes out the current resolved and unresolved Named Types from
the type registry.
|
void |
clearTemplateTypeNames()
Clears the template type name.
|
ObjectType |
createAnonymousObjectType(JSDocInfo info)
Create an anonymous object type.
|
(package private) ArrowType |
createArrowType(Node parametersNode)
Creates an arrow type with an unknown return type.
|
(package private) ArrowType |
createArrowType(Node parametersNode,
JSType returnType)
Creates an arrow type, an abstract representation of the parameters
and return value of a function.
|
FunctionType |
createConstructorType(java.lang.String name,
Node source,
Node parameters,
JSType returnType,
com.google.common.collect.ImmutableList<TemplateType> templateKeys)
Creates a constructor function type.
|
JSType |
createDefaultObjectUnion(JSType type)
Creates a type representing nullable values of the given type.
|
EnumType |
createEnumType(java.lang.String name,
Node source,
JSType elementsType)
Creates an enum type.
|
private JSType |
createFromTypeNodesInternal(Node n,
java.lang.String sourceName,
StaticTypedScope<JSType> scope) |
FunctionType |
createFunctionType(JSType returnType,
JSType... parameterTypes)
Creates a function type.
|
FunctionType |
createFunctionType(JSType returnType,
Node parameters) |
JSType |
createFunctionTypeWithInstanceType(ObjectType instanceType,
JSType returnType,
java.util.List<JSType> parameterTypes)
Creates a function type in which
this refers to an object instance. |
FunctionType |
createFunctionTypeWithNewReturnType(FunctionType existingFunctionType,
JSType returnType)
Creates a new function type based on an existing function type but
with a new return type.
|
FunctionType |
createFunctionTypeWithVarArgs(JSType returnType,
JSType... parameterTypes)
Creates a function type.
|
FunctionType |
createInterfaceType(java.lang.String name,
Node source,
com.google.common.collect.ImmutableList<TemplateType> templateKeys,
boolean struct)
Creates an interface function type.
|
NamedType |
createNamedType(java.lang.String reference,
java.lang.String sourceName,
int lineno,
int charno)
Creates a named type.
|
private FunctionType |
createNativeFunctionType(JSType returnType,
Node parameters) |
private FunctionType |
createNativeFunctionTypeWithVarArgs(JSType returnType,
JSType... parameterTypes)
Creates a function type.
|
JSType |
createNullableType(JSType type)
Creates a type representing nullable values of the given type.
|
ObjectType |
createObjectType(java.lang.String name,
ObjectType implicitPrototype)
Create an object type.
|
JSType |
createOptionalNullableType(JSType type)
Creates a nullable and undefine-able value of the given type.
|
Node |
createOptionalParameters(JSType... parameterTypes)
Creates a tree hierarchy representing a typed parameter list in which
every parameter is optional.
|
JSType |
createOptionalType(JSType type)
Creates a type representing optional values of the given type.
|
private Node |
createParameters(boolean lastVarArgs,
JSType... parameterTypes)
Creates a tree hierarchy representing a typed argument list.
|
Node |
createParameters(JSType... parameterTypes)
Creates a tree hierarchy representing a typed argument list.
|
Node |
createParametersWithVarArgs(JSType... parameterTypes)
Creates a tree hierarchy representing a typed argument list.
|
RecordType |
createRecordType(java.util.Map<java.lang.String,RecordTypeBuilder.RecordProperty> properties)
Creates a record type.
|
private JSType |
createRecordTypeFromNodes(Node n,
java.lang.String sourceName,
StaticTypedScope<JSType> scope)
Creates a RecordType from the nodes representing said record type.
|
TemplateType |
createTemplateType(java.lang.String name) |
TemplateTypeMap |
createTemplateTypeMap(com.google.common.collect.ImmutableList<TemplateType> templateKeys,
com.google.common.collect.ImmutableList<JSType> templateValues)
Creates a template type map from the specified list of template keys and
template value types.
|
TemplateType |
createTemplateTypeWithTransformation(java.lang.String name,
Node expr) |
TemplatizedType |
createTemplatizedType(ObjectType baseType,
com.google.common.collect.ImmutableList<JSType> templatizedTypes)
Creates a templatized instance of the specified type.
|
TemplatizedType |
createTemplatizedType(ObjectType baseType,
JSType... templatizedTypes)
Creates a templatized instance of the specified type.
|
TemplatizedType |
createTemplatizedType(ObjectType baseType,
java.util.Map<TemplateType,JSType> templatizedTypes)
Creates a templatized instance of the specified type.
|
JSType |
createTypeFromCommentNode(Node n,
java.lang.String sourceName,
StaticTypedScope<? extends TypeI> scope)
Creates a JSType from the nodes representing a type.
|
JSType |
createUnionType(JSType... variants)
Creates a union type whose variants are the arguments.
|
JSType |
createUnionType(JSTypeNative... variants)
Creates a union type whose variants are the built-in types specified
by the arguments.
|
boolean |
declareType(java.lang.String name,
JSType t)
Records declared global type names.
|
(package private) ObjectType |
findCommonSuperObject(ObjectType a,
ObjectType b)
Finds the common supertype of the two given object types.
|
void |
forwardDeclareType(java.lang.String name)
Records a forward-declared type name.
|
java.util.Collection<FunctionType> |
getDirectImplementors(ObjectType interfaceInstance)
Returns a collection of types that directly implement
interfaceInstance . |
java.lang.Iterable<ObjectType> |
getEachReferenceTypeWithProperty(java.lang.String propertyName)
Returns each reference type that has a property
propertyName
defined on it. |
ErrorReporter |
getErrorReporter() |
JSType |
getGreatestSubtypeWithProperty(JSType type,
java.lang.String propertyName)
Gets the greatest subtype of the
type that has a property
propertyName defined on it. |
private JSType |
getJSTypeOrUnknown(Node n) |
FunctionType |
getNativeFunctionType(JSTypeNative typeId) |
ObjectType |
getNativeObjectType(JSTypeNative typeId) |
JSType |
getNativeType(JSTypeNative typeId) |
TemplateType |
getObjectElementKey() |
TemplateType |
getObjectIndexKey() |
private java.lang.String |
getReadableJSTypeName(Node n,
boolean dereference)
Given a node, get a human-readable name for the type of that node so
that will be easy for the programmer to find the original declaration.
|
java.lang.String |
getReadableTypeName(Node n)
The nice API for this method is a single argument; dereference is a detail.
|
java.lang.String |
getReadableTypeNameNoDeref(Node n) |
private static java.util.List<ObjectType> |
getSuperStack(ObjectType a) |
com.google.common.collect.ImmutableList<TemplateType> |
getTemplateTypesOfBuiltin(java.lang.String fnName) |
JSType |
getType(StaticTypedScope<JSType> scope,
java.lang.String jsTypeName,
java.lang.String sourceName,
int lineno,
int charno)
Looks up a type by name.
|
JSType |
getType(java.lang.String jsTypeName)
Looks up a type by name.
|
void |
identifyNonNullableName(java.lang.String name)
Identifies the name of a typedef or enum before we actually declare it.
|
private void |
initializeBuiltInTypes() |
private void |
initializeRegistry() |
boolean |
isForwardDeclaredType(java.lang.String name)
Whether this is a forward-declared type name.
|
(package private) boolean |
isLastGeneration() |
private boolean |
isNonNullable(JSType type) |
boolean |
isTemplatedBuiltin(java.lang.String fnName,
JSDocInfo info)
Check if a function declaration is one of the templated builitin contructor/interfaces,
namely one of IObject, IArrayLike, or Array
|
boolean |
isTemplatizable(JSType type)
Checks whether the input type can be templatized.
|
void |
overwriteDeclaredType(java.lang.String name,
JSType t)
Overrides a declared global type name.
|
private void |
register(JSType type) |
private void |
register(JSType type,
java.lang.String name) |
private void |
registerNativeType(JSTypeNative typeId,
JSType type) |
void |
registerPropertyOnType(java.lang.String propertyName,
JSType type)
Tells the type system that
owner may have a property named
propertyName . |
(package private) void |
registerTypeImplementingInterface(FunctionType type,
ObjectType interfaceInstance)
Tells the type system that
type implements interface interfaceInstance . |
void |
resetForTypeCheck()
Reset to run the TypeCheck pass.
|
boolean |
resetImplicitPrototype(JSType type,
ObjectType newImplicitProto)
Set the implicit prototype if it's possible to do so.
|
void |
resolveTypesInScope(StaticTypedScope<JSType> scope)
Resolve all the unresolved types in the given scope.
|
void |
setTemplateTypeNames(java.util.List<TemplateType> keys)
Sets the template type name.
|
void |
unregisterPropertyOnType(java.lang.String propertyName,
JSType type)
Removes the index's reference to a property on the given type (if it is
currently registered).
|
private static final long serialVersionUID
private TemplateType iObjectIndexTemplateKey
IObject<KEY, VALUE>
(plus the builtin Javascript Object).private TemplateType iObjectElementTemplateKey
IObject<KEY, VALUE>
(plus the builtin Javascript Object).private static final java.lang.String I_OBJECT_ELEMENT_TEMPLATE
private TemplateType arrayElementTemplateKey
Array<T>
.@Deprecated public static final java.lang.String OBJECT_ELEMENT_TEMPLATE
private static final int PROPERTY_CHECKING_UNION_SIZE
private final transient ErrorReporter reporter
private final JSType[] nativeTypes
private final java.util.Map<java.lang.String,JSType> namesToTypes
private final java.util.Set<java.lang.String> nonNullableTypeNames
private final java.util.Set<java.lang.String> forwardDeclaredTypes
private final java.util.Map<java.lang.String,UnionTypeBuilder> typesIndexedByProperty
private final java.util.Map<java.lang.String,java.util.Map<java.lang.String,ObjectType>> eachRefTypeIndexedByProperty
private final java.util.Map<java.lang.String,JSType> greatestSubtypeByProperty
private final com.google.common.collect.Multimap<java.lang.String,FunctionType> interfaceToImplementors
private final com.google.common.collect.Multimap<StaticTypedScope<JSType>,NamedType> unresolvedNamedTypes
private final com.google.common.collect.Multimap<StaticTypedScope<JSType>,NamedType> resolvedNamedTypes
private boolean lastGeneration
private final java.util.Map<java.lang.String,TemplateType> templateTypes
private final TemplateTypeMap emptyTemplateTypeMap
public JSTypeRegistry(ErrorReporter reporter)
public TemplateType getObjectElementKey()
public TemplateType getObjectIndexKey()
public boolean isTemplatedBuiltin(java.lang.String fnName, JSDocInfo info)
fnName
- the function's nameinfo
- the JSDoc from the function declarationpublic com.google.common.collect.ImmutableList<TemplateType> getTemplateTypesOfBuiltin(java.lang.String fnName)
public ErrorReporter getErrorReporter()
public void resetForTypeCheck()
private void initializeBuiltInTypes()
private void initializeRegistry()
private void register(JSType type)
private void register(JSType type, java.lang.String name)
private void registerNativeType(JSTypeNative typeId, JSType type)
public void registerPropertyOnType(java.lang.String propertyName, JSType type)
owner
may have a property named
propertyName
. This allows the registry to keep track of what
types a property is defined upon.
This is NOT the same as saying that owner
must have a property
named type. ObjectType#hasProperty attempts to minimize false positives
("if we're not sure, then don't type check this property"). The type
registry, on the other hand, should attempt to minimize false negatives
("if this property is assigned anywhere in the program, it must
show up in the type registry").private void addReferenceTypeIndexedByProperty(java.lang.String propertyName, JSType type)
public void unregisterPropertyOnType(java.lang.String propertyName, JSType type)
propertyName
- the name of the property to unregistertype
- the type to unregister the property on.public JSType getGreatestSubtypeWithProperty(JSType type, java.lang.String propertyName)
type
that has a property
propertyName
defined on it.public boolean canPropertyBeDefined(JSType type, java.lang.String propertyName)
public java.lang.Iterable<ObjectType> getEachReferenceTypeWithProperty(java.lang.String propertyName)
propertyName
defined on it.
Unlike most types in our type system, the collection of types returned
will not be collapsed. This means that if a type is defined on
Object
and on Array
, this method must return
[Object, Array]
. It would not be correct to collapse them to
[Object]
.ObjectType findCommonSuperObject(ObjectType a, ObjectType b)
private static java.util.List<ObjectType> getSuperStack(ObjectType a)
boolean isLastGeneration()
void registerTypeImplementingInterface(FunctionType type, ObjectType interfaceInstance)
type
implements interface interfaceInstance
.
inter
must be an ObjectType for the instance of the interface as it
could be a named type and not yet have the constructor.public java.util.Collection<FunctionType> getDirectImplementors(ObjectType interfaceInstance)
interfaceInstance
. Subtypes of implementing types are not guaranteed to
be returned. interfaceInstance
must be an ObjectType for the
instance of the interface.public boolean declareType(java.lang.String name, JSType t)
name
- The name of the type to be recorded.t
- The actual type being associated with the name.public void overwriteDeclaredType(java.lang.String name, JSType t)
public void forwardDeclareType(java.lang.String name)
public boolean isForwardDeclaredType(java.lang.String name)
public java.lang.String getReadableTypeName(Node n)
getReadableTypeName
in interface TypeIRegistry
public java.lang.String getReadableTypeNameNoDeref(Node n)
private java.lang.String getReadableJSTypeName(Node n, boolean dereference)
n
- The node.dereference
- If true, the type of the node will be dereferenced
to an Object type, if possible.public JSType getType(java.lang.String jsTypeName)
getType
in interface TypeIRegistry
jsTypeName
- The name string.null
it cannot be foundpublic JSType getNativeType(JSTypeNative typeId)
getNativeType
in interface TypeIRegistry
public ObjectType getNativeObjectType(JSTypeNative typeId)
getNativeObjectType
in interface TypeIRegistry
public FunctionType getNativeFunctionType(JSTypeNative typeId)
getNativeFunctionType
in interface TypeIRegistry
public JSType getType(StaticTypedScope<JSType> scope, java.lang.String jsTypeName, java.lang.String sourceName, int lineno, int charno)
scope
- A scope for doing type name resolution.jsTypeName
- The name string.sourceName
- The name of the source file where this reference appears.lineno
- The line number of the reference.public void clearNamedTypes()
public void resolveTypesInScope(StaticTypedScope<JSType> scope)
public JSType createOptionalType(JSType type)
public JSType createDefaultObjectUnion(JSType type)
public JSType createNullableType(JSType type)
public JSType createOptionalNullableType(JSType type)
public JSType createUnionType(JSType... variants)
public JSType createUnionType(JSTypeNative... variants)
public EnumType createEnumType(java.lang.String name, Node source, JSType elementsType)
ArrowType createArrowType(Node parametersNode, JSType returnType)
parametersNode
- the parameters' types, formatted as a Node with
param names and optionality info.returnType
- the function's return typeArrowType createArrowType(Node parametersNode)
parametersNode
- the parameters' types, formatted as a Node with
param names and optionality info.public FunctionType createFunctionType(JSType returnType, JSType... parameterTypes)
returnType
- the function's return typeparameterTypes
- the parameters' typespublic FunctionType createFunctionTypeWithVarArgs(JSType returnType, JSType... parameterTypes)
returnType
- the function's return typeparameterTypes
- the parameters' typesprivate FunctionType createNativeFunctionTypeWithVarArgs(JSType returnType, JSType... parameterTypes)
returnType
- the function's return typeparameterTypes
- the parameters' typespublic JSType createFunctionTypeWithInstanceType(ObjectType instanceType, JSType returnType, java.util.List<JSType> parameterTypes)
this
refers to an object instance.instanceType
- the type of this
returnType
- the function's return typeparameterTypes
- the parameters' typespublic Node createParameters(JSType... parameterTypes)
parameterTypes
- the parameter types.public Node createParametersWithVarArgs(JSType... parameterTypes)
parameterTypes
- the parameter types. The last element of this array
is considered a variable length argument.public Node createOptionalParameters(JSType... parameterTypes)
private Node createParameters(boolean lastVarArgs, JSType... parameterTypes)
lastVarArgs
- whether the last type should considered as a variable
length argument.parameterTypes
- the parameter types. The last element of this array
is considered a variable length argument is lastVarArgs
is
true
.public FunctionType createFunctionTypeWithNewReturnType(FunctionType existingFunctionType, JSType returnType)
existingFunctionType
- the existing function type.returnType
- the new return type.public FunctionType createFunctionType(JSType returnType, Node parameters)
parameters
- the function's parameters or null
to indicate that the parameter types are unknown.returnType
- the function's return type or null
to indicate
that the return type is unknown.private FunctionType createNativeFunctionType(JSType returnType, Node parameters)
public RecordType createRecordType(java.util.Map<java.lang.String,RecordTypeBuilder.RecordProperty> properties)
public ObjectType createObjectType(java.lang.String name, ObjectType implicitPrototype)
public ObjectType createAnonymousObjectType(JSDocInfo info)
info
- Used to mark object literals as structs; can be null
public boolean resetImplicitPrototype(JSType type, ObjectType newImplicitProto)
public FunctionType createConstructorType(java.lang.String name, Node source, Node parameters, JSType returnType, com.google.common.collect.ImmutableList<TemplateType> templateKeys)
name
- the function's name or null
to indicate that the
function is anonymous.source
- the node defining this function. Its type
(Node.getType()
) must be Token.FUNCTION
.parameters
- the function's parameters or null
to indicate that the parameter types are unknown.returnType
- the function's return type or null
to indicate
that the return type is unknown.templateKeys
- the templatized types for the class.public FunctionType createInterfaceType(java.lang.String name, Node source, com.google.common.collect.ImmutableList<TemplateType> templateKeys, boolean struct)
name
- the function's namesource
- the node defining this function. Its type
(Node.getType()
) must be Token.FUNCTION
.templateKeys
- the templatized types for the interface.public TemplateType createTemplateType(java.lang.String name)
public TemplateType createTemplateTypeWithTransformation(java.lang.String name, Node expr)
public TemplateTypeMap createTemplateTypeMap(com.google.common.collect.ImmutableList<TemplateType> templateKeys, com.google.common.collect.ImmutableList<JSType> templateValues)
public TemplatizedType createTemplatizedType(ObjectType baseType, com.google.common.collect.ImmutableList<JSType> templatizedTypes)
baseType
- the type to be templatized.templatizedTypes
- a list of the template JSTypes. Will be matched by
list order to the template keys on the base type.public TemplatizedType createTemplatizedType(ObjectType baseType, java.util.Map<TemplateType,JSType> templatizedTypes)
baseType
- the type to be templatized.templatizedTypes
- a map from TemplateType to corresponding JSType
value. Any unfilled TemplateTypes on the baseType that are *not*
contained in this map will have UNKNOWN_TYPE used as their value.public TemplatizedType createTemplatizedType(ObjectType baseType, JSType... templatizedTypes)
baseType
- the type to be templatized.templatizedTypes
- a list of the template JSTypes. Will be matched by
list order to the template keys on the base type.public NamedType createNamedType(java.lang.String reference, java.lang.String sourceName, int lineno, int charno)
public void identifyNonNullableName(java.lang.String name)
public JSType createTypeFromCommentNode(Node n, java.lang.String sourceName, StaticTypedScope<? extends TypeI> scope)
createTypeFromCommentNode
in interface TypeIRegistry
n
- The node with type info.sourceName
- The source file name.scope
- A scope for doing type name lookups.private JSType createFromTypeNodesInternal(Node n, java.lang.String sourceName, StaticTypedScope<JSType> scope)
private JSType createRecordTypeFromNodes(Node n, java.lang.String sourceName, StaticTypedScope<JSType> scope)
n
- The node with type info.sourceName
- The source file name.scope
- A scope for doing type name lookups.public void setTemplateTypeNames(java.util.List<TemplateType> keys)
public void clearTemplateTypeNames()
private boolean isNonNullable(JSType type)
public boolean isTemplatizable(JSType type)
Object
type which is not a NamespaceType
and is not a
non-nullable type.