Class JSTypeRegistry
- java.lang.Object
-
- com.google.javascript.rhino.jstype.JSTypeRegistry
-
- All Implemented Interfaces:
java.io.Serializable
public class JSTypeRegistry extends java.lang.Object implements java.io.Serializable
The type registry is used to resolve named types.This class is not thread-safe.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JSTypeRegistry.ResolveMode
The type registry has three modes, which control how type ASTs are converted to types increateFromTypeNodes(com.google.javascript.rhino.Node, java.lang.String, com.google.javascript.rhino.jstype.StaticScope<com.google.javascript.rhino.jstype.JSType>)
.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
OBJECT_ELEMENT_TEMPLATE
The name associated with the template variable corresponding to the property value type for Javascript Objects and Arrays.static java.lang.String
OBJECT_INDEX_TEMPLATE
The name associated with the template variable corresponding to the property key type of the built-in Javascript object.
-
Constructor Summary
Constructors Constructor Description JSTypeRegistry(ErrorReporter reporter)
Constructs a new type registry populated with the built-in types.JSTypeRegistry(ErrorReporter reporter, boolean tolerateUndefinedValues)
Constructs a new type registry populated with the built-in types.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.FunctionType
createConstructorType(JSType returnType, boolean lastVarArgs, JSType... parameterTypes)
Creates a function type which can act as a constructor.FunctionType
createConstructorType(JSType returnType, JSType... parameterTypes)
Creates a function type which can act as a constructor.FunctionType
createConstructorType(java.lang.String name, Node source, Node parameters, JSType returnType, com.google.common.collect.ImmutableList<java.lang.String> templateKeyNames)
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.JSType
createFromTypeNodes(Node n, java.lang.String sourceName, StaticScope<JSType> scope)
Creates a JSType from the nodes representing a type.FunctionType
createFunctionType(JSType returnType, boolean lastVarArgs, JSType... parameterTypes)
Creates a function type.FunctionType
createFunctionType(JSType returnType, JSType... parameterTypes)
Creates a function type.FunctionType
createFunctionType(JSType returnType, Node parameters)
FunctionType
createFunctionType(JSType returnType, java.util.List<JSType> parameterTypes)
Creates a function type.JSType
createFunctionType(ObjectType instanceType, JSType returnType, java.util.List<JSType> parameterTypes)
Creates a function type in whichthis
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
createFunctionTypeWithNewThisType(FunctionType existingFunctionType, ObjectType thisType)
Creates a new function type based on an existing function type but with a newthis
type.FunctionType
createFunctionTypeWithVarArgs(JSType returnType, JSType... parameterTypes)
Creates a function type.FunctionType
createFunctionTypeWithVarArgs(JSType returnType, java.util.List<JSType> parameterTypes)
Creates a function type.JSType
createFunctionTypeWithVarArgs(ObjectType instanceType, JSType returnType, java.util.List<JSType> parameterTypes)
Creates a function type in whichthis
refers to an object instance.FunctionType
createInterfaceType(java.lang.String name, Node source)
Creates an interface function type.JSType
createNamedType(java.lang.String reference, java.lang.String sourceName, int lineno, int charno)
Creates a named type.JSType
createNullableType(JSType type)
Creates a type representing nullable values of the given type.ObjectType
createObjectType(ObjectType implicitPrototype)
Create an object type.ObjectType
createObjectType(java.lang.String name, Node n, 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.Node
createParameters(JSType... parameterTypes)
Creates a tree hierarchy representing a typed argument list.Node
createParameters(java.util.List<JSType> parameterTypes)
Creates a tree hierarchy representing a typed argument list.Node
createParametersWithVarArgs(JSType... parameterTypes)
Creates a tree hierarchy representing a typed argument list.Node
createParametersWithVarArgs(java.util.List<JSType> parameterTypes)
Creates a tree hierarchy representing a typed argument list.com.google.javascript.rhino.jstype.RecordType
createRecordType(java.util.Map<java.lang.String,com.google.javascript.rhino.jstype.RecordTypeBuilder.RecordProperty> properties)
Creates a 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.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.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.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 implementinterfaceInstance
.java.lang.Iterable<ObjectType>
getEachReferenceTypeWithProperty(java.lang.String propertyName)
Returns each reference type that has a propertypropertyName
defined on it.ErrorReporter
getErrorReporter()
JSType
getGreatestSubtypeWithProperty(JSType type, java.lang.String propertyName)
Gets the greatest subtype of thetype
that has a propertypropertyName
defined on it.FunctionType
getNativeFunctionType(JSTypeNative typeId)
ObjectType
getNativeObjectType(JSTypeNative typeId)
JSType
getNativeType(JSTypeNative typeId)
TemplateType
getObjectElementKey()
TemplateType
getObjectIndexKey()
JSType
getType(StaticScope<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.java.lang.Iterable<JSType>
getTypesWithProperty(java.lang.String propertyName)
Returns each type that has a propertypropertyName
defined on it.boolean
hasNamespace(java.lang.String name)
Determines whether the given JS package exists.void
identifyNonNullableName(java.lang.String name)
Identifies the name of a typedef or enum before we actually declare it.void
incrementGeneration()
Increments the current generation.boolean
isForwardDeclaredType(java.lang.String name)
Whether this is a forward-declared type name.void
overwriteDeclaredType(java.lang.String name, JSType t)
Overrides a declared global type name.void
registerPropertyOnType(java.lang.String propertyName, JSType type)
Tells the type system thatowner
may have a property namedpropertyName
.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(StaticScope<JSType> scope)
Resolve all the unresolved types in the given scope.void
setLastGeneration(boolean lastGeneration)
Sets whether this is the last generation.void
setResolveMode(JSTypeRegistry.ResolveMode mode)
Set the current resolving mode of the type registry.void
setTemplateTypeNames(java.util.List<TemplateType> keys)
Sets the template type name.boolean
shouldTolerateUndefinedValues()
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).
-
-
-
Field Detail
-
OBJECT_INDEX_TEMPLATE
public static final java.lang.String OBJECT_INDEX_TEMPLATE
The name associated with the template variable corresponding to the property key type of the built-in Javascript object.- See Also:
- Constant Field Values
-
OBJECT_ELEMENT_TEMPLATE
public static final java.lang.String OBJECT_ELEMENT_TEMPLATE
The name associated with the template variable corresponding to the property value type for Javascript Objects and Arrays.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
JSTypeRegistry
public JSTypeRegistry(ErrorReporter reporter)
Constructs a new type registry populated with the built-in types.
-
JSTypeRegistry
public JSTypeRegistry(ErrorReporter reporter, boolean tolerateUndefinedValues)
Constructs a new type registry populated with the built-in types.
-
-
Method Detail
-
getObjectElementKey
public TemplateType getObjectElementKey()
- Returns:
- The template variable corresponding to the property value type for Javascript Objects and Arrays.
-
getObjectIndexKey
public TemplateType getObjectIndexKey()
- Returns:
- The template variable corresponding to the property key type of the built-in Javascript object.
-
setResolveMode
public void setResolveMode(JSTypeRegistry.ResolveMode mode)
Set the current resolving mode of the type registry.- See Also:
JSTypeRegistry.ResolveMode
-
getErrorReporter
public ErrorReporter getErrorReporter()
-
shouldTolerateUndefinedValues
public boolean shouldTolerateUndefinedValues()
-
resetForTypeCheck
public void resetForTypeCheck()
Reset to run the TypeCheck pass.
-
registerPropertyOnType
public void registerPropertyOnType(java.lang.String propertyName, JSType type)
Tells the type system thatowner
may have a property namedpropertyName
. This allows the registry to keep track of what types a property is defined upon. This is NOT the same as saying thatowner
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").
-
unregisterPropertyOnType
public 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). If the property is not registered on the type yet, this method will not change internal state.- Parameters:
propertyName
- the name of the property to unregistertype
- the type to unregister the property on.
-
getGreatestSubtypeWithProperty
public JSType getGreatestSubtypeWithProperty(JSType type, java.lang.String propertyName)
Gets the greatest subtype of thetype
that has a propertypropertyName
defined on it.
-
canPropertyBeDefined
public boolean canPropertyBeDefined(JSType type, java.lang.String propertyName)
Returns whether the given property can possibly be set on the given type.
-
getTypesWithProperty
public java.lang.Iterable<JSType> getTypesWithProperty(java.lang.String propertyName)
Returns each type that has a propertypropertyName
defined on it. Like most types in our type system, the collection of types returned will be collapsed. This means that if a type is defined onObject
and onArray
, it would be reasonable for this method to return either[Object, Array]
or just[Object]
.
-
getEachReferenceTypeWithProperty
public java.lang.Iterable<ObjectType> getEachReferenceTypeWithProperty(java.lang.String propertyName)
Returns each reference type that has a propertypropertyName
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 onObject
and onArray
, this method must return[Object, Array]
. It would not be correct to collapse them to[Object]
.
-
incrementGeneration
public void incrementGeneration()
Increments the current generation. Clients must call this in order to move to the next generation of type resolution, allowing types to attempt resolution again.
-
setLastGeneration
public void setLastGeneration(boolean lastGeneration)
Sets whether this is the last generation. In the last generation,NamedType
warns about unresolved types.
-
getDirectImplementors
public java.util.Collection<FunctionType> getDirectImplementors(ObjectType interfaceInstance)
Returns a collection of types that directly implementinterfaceInstance
. Subtypes of implementing types are not guaranteed to be returned.interfaceInstance
must be an ObjectType for the instance of the interface.
-
declareType
public boolean declareType(java.lang.String name, JSType t)
Records declared global type names. This makes resolution faster and more robust in the common case.- Parameters:
name
- The name of the type to be recorded.t
- The actual type being associated with the name.- Returns:
- True if this name is not already defined, false otherwise.
-
overwriteDeclaredType
public void overwriteDeclaredType(java.lang.String name, JSType t)
Overrides a declared global type name. Throws an exception if this type name hasn't been declared yet.
-
forwardDeclareType
public void forwardDeclareType(java.lang.String name)
Records a forward-declared type name. We will not emit errors if this type name never resolves to anything.
-
isForwardDeclaredType
public boolean isForwardDeclaredType(java.lang.String name)
Whether this is a forward-declared type name.
-
hasNamespace
public boolean hasNamespace(java.lang.String name)
Determines whether the given JS package exists.
-
getType
public JSType getType(java.lang.String jsTypeName)
Looks up a type by name.- Parameters:
jsTypeName
- The name string.- Returns:
- the corresponding JSType object or
null
it cannot be found
-
getNativeType
public JSType getNativeType(JSTypeNative typeId)
-
getNativeObjectType
public ObjectType getNativeObjectType(JSTypeNative typeId)
-
getNativeFunctionType
public FunctionType getNativeFunctionType(JSTypeNative typeId)
-
getType
public JSType getType(StaticScope<JSType> scope, java.lang.String jsTypeName, java.lang.String sourceName, int lineno, int charno)
Looks up a type by name. To allow for forward references to types, an unrecognized string has to be bound to a NamedType object that will be resolved later.- Parameters:
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.- Returns:
- a NamedType if the string argument is not one of the known types, otherwise the corresponding JSType object.
-
clearNamedTypes
public void clearNamedTypes()
Flushes out the current resolved and unresolved Named Types from the type registry. This is intended to be used ONLY before a compile is run.
-
resolveTypesInScope
public void resolveTypesInScope(StaticScope<JSType> scope)
Resolve all the unresolved types in the given scope.
-
createOptionalType
public JSType createOptionalType(JSType type)
Creates a type representing optional values of the given type.- Returns:
- the union of the type and the void type
-
createDefaultObjectUnion
public JSType createDefaultObjectUnion(JSType type)
Creates a type representing nullable values of the given type.- Returns:
- the union of the type and the Null type
-
createNullableType
public JSType createNullableType(JSType type)
Creates a type representing nullable values of the given type.- Returns:
- the union of the type and the Null type
-
createOptionalNullableType
public JSType createOptionalNullableType(JSType type)
Creates a nullable and undefine-able value of the given type.- Returns:
- The union of the type and null and undefined.
-
createUnionType
public JSType createUnionType(JSType... variants)
Creates a union type whose variants are the arguments.
-
createUnionType
public JSType createUnionType(JSTypeNative... variants)
Creates a union type whose variants are the built-in types specified by the arguments.
-
createEnumType
public EnumType createEnumType(java.lang.String name, Node source, JSType elementsType)
Creates an enum type.
-
createFunctionType
public FunctionType createFunctionType(JSType returnType, JSType... parameterTypes)
Creates a function type.- Parameters:
returnType
- the function's return typeparameterTypes
- the parameters' types
-
createFunctionTypeWithVarArgs
public FunctionType createFunctionTypeWithVarArgs(JSType returnType, java.util.List<JSType> parameterTypes)
Creates a function type. The last parameter type of the function is considered a variable length argument.- Parameters:
returnType
- the function's return typeparameterTypes
- the parameters' types
-
createFunctionType
public FunctionType createFunctionType(JSType returnType, java.util.List<JSType> parameterTypes)
Creates a function type.- Parameters:
returnType
- the function's return typeparameterTypes
- the parameters' types
-
createFunctionTypeWithVarArgs
public FunctionType createFunctionTypeWithVarArgs(JSType returnType, JSType... parameterTypes)
Creates a function type. The last parameter type of the function is considered a variable length argument.- Parameters:
returnType
- the function's return typeparameterTypes
- the parameters' types
-
createConstructorType
public FunctionType createConstructorType(JSType returnType, JSType... parameterTypes)
Creates a function type which can act as a constructor.- Parameters:
returnType
- the function's return typeparameterTypes
- the parameters' types
-
createFunctionType
public JSType createFunctionType(ObjectType instanceType, JSType returnType, java.util.List<JSType> parameterTypes)
Creates a function type in whichthis
refers to an object instance.- Parameters:
instanceType
- the type ofthis
returnType
- the function's return typeparameterTypes
- the parameters' types
-
createFunctionTypeWithVarArgs
public JSType createFunctionTypeWithVarArgs(ObjectType instanceType, JSType returnType, java.util.List<JSType> parameterTypes)
Creates a function type in whichthis
refers to an object instance. The last parameter type of the function is considered a variable length argument.- Parameters:
instanceType
- the type ofthis
returnType
- the function's return typeparameterTypes
- the parameters' types
-
createParameters
public Node createParameters(java.util.List<JSType> parameterTypes)
Creates a tree hierarchy representing a typed argument list.- Parameters:
parameterTypes
- the parameter types.- Returns:
- a tree hierarchy representing a typed argument list.
-
createParametersWithVarArgs
public Node createParametersWithVarArgs(java.util.List<JSType> parameterTypes)
Creates a tree hierarchy representing a typed argument list. The last parameter type is considered a variable length argument.- Parameters:
parameterTypes
- the parameter types. The last element of this array is considered a variable length argument.- Returns:
- a tree hierarchy representing a typed argument list.
-
createParameters
public Node createParameters(JSType... parameterTypes)
Creates a tree hierarchy representing a typed argument list.- Parameters:
parameterTypes
- the parameter types.- Returns:
- a tree hierarchy representing a typed argument list.
-
createParametersWithVarArgs
public Node createParametersWithVarArgs(JSType... parameterTypes)
Creates a tree hierarchy representing a typed argument list. The last parameter type is considered a variable length argument.- Parameters:
parameterTypes
- the parameter types. The last element of this array is considered a variable length argument.- Returns:
- a tree hierarchy representing a typed argument list.
-
createOptionalParameters
public Node createOptionalParameters(JSType... parameterTypes)
Creates a tree hierarchy representing a typed parameter list in which every parameter is optional.
-
createFunctionType
public FunctionType createFunctionType(JSType returnType, boolean lastVarArgs, JSType... parameterTypes)
Creates a function type.- Parameters:
returnType
- the function's return typelastVarArgs
- whether the last parameter type should be considered as an extensible var_args parameterparameterTypes
- the parameters' types
-
createFunctionTypeWithNewReturnType
public FunctionType createFunctionTypeWithNewReturnType(FunctionType existingFunctionType, JSType returnType)
Creates a new function type based on an existing function type but with a new return type.- Parameters:
existingFunctionType
- the existing function type.returnType
- the new return type.
-
createFunctionTypeWithNewThisType
public FunctionType createFunctionTypeWithNewThisType(FunctionType existingFunctionType, ObjectType thisType)
Creates a new function type based on an existing function type but with a newthis
type.- Parameters:
existingFunctionType
- the existing function type.thisType
- the new this type.
-
createFunctionType
public FunctionType createFunctionType(JSType returnType, Node parameters)
- Parameters:
parameters
- the function's parameters ornull
to indicate that the parameter types are unknown.returnType
- the function's return type ornull
to indicate that the return type is unknown.
-
createConstructorType
public FunctionType createConstructorType(JSType returnType, boolean lastVarArgs, JSType... parameterTypes)
Creates a function type which can act as a constructor.- Parameters:
returnType
- the function's return typelastVarArgs
- whether the last parameter type should be considered as an extensible var_args parameterparameterTypes
- the parameters' types
-
createObjectType
public ObjectType createObjectType(ObjectType implicitPrototype)
Create an object type.
-
createRecordType
public com.google.javascript.rhino.jstype.RecordType createRecordType(java.util.Map<java.lang.String,com.google.javascript.rhino.jstype.RecordTypeBuilder.RecordProperty> properties)
Creates a record type.
-
createObjectType
public ObjectType createObjectType(java.lang.String name, Node n, ObjectType implicitPrototype)
Create an object type.
-
createAnonymousObjectType
public ObjectType createAnonymousObjectType(JSDocInfo info)
Create an anonymous object type.- Parameters:
info
- Used to mark object literals as structs; can benull
-
resetImplicitPrototype
public boolean resetImplicitPrototype(JSType type, ObjectType newImplicitProto)
Set the implicit prototype if it's possible to do so.- Returns:
- True if we were able to set the implicit prototype successfully, false if it was not possible to do so for some reason. There are a few different reasons why this could fail: for example, numbers can't be implicit prototypes, and we don't want to change the implicit prototype if other classes have already subclassed this one.
-
createConstructorType
public FunctionType createConstructorType(java.lang.String name, Node source, Node parameters, JSType returnType, com.google.common.collect.ImmutableList<java.lang.String> templateKeyNames)
Creates a constructor function type.- Parameters:
name
- the function's name ornull
to indicate that the function is anonymous.source
- the node defining this function. Its type (Node.getType()
) must beToken.FUNCTION
.parameters
- the function's parameters ornull
to indicate that the parameter types are unknown.returnType
- the function's return type ornull
to indicate that the return type is unknown.templateKeyNames
- the templatized type keys for the class.
-
createInterfaceType
public FunctionType createInterfaceType(java.lang.String name, Node source)
Creates an interface function type.- Parameters:
name
- the function's namesource
- the node defining this function. Its type (Node.getType()
) must beToken.FUNCTION
.
-
createTemplateType
public TemplateType createTemplateType(java.lang.String name)
-
createTemplateTypeMap
public 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.
-
createTemplatizedType
public TemplatizedType createTemplatizedType(ObjectType baseType, com.google.common.collect.ImmutableList<JSType> templatizedTypes)
Creates a templatized instance of the specified type. Only ObjectTypes can currently be templatized; extend the logic in this function when more types can be templatized.- Parameters:
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.
-
createTemplatizedType
public TemplatizedType createTemplatizedType(ObjectType baseType, JSType... templatizedTypes)
Creates a templatized instance of the specified type. Only ObjectTypes can currently be templatized; extend the logic in this function when more types can be templatized.- Parameters:
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.
-
createNamedType
public JSType createNamedType(java.lang.String reference, java.lang.String sourceName, int lineno, int charno)
Creates a named type.
-
identifyNonNullableName
public void identifyNonNullableName(java.lang.String name)
Identifies the name of a typedef or enum before we actually declare it.
-
createFromTypeNodes
public JSType createFromTypeNodes(Node n, java.lang.String sourceName, StaticScope<JSType> scope)
Creates a JSType from the nodes representing a type.- Parameters:
n
- The node with type info.sourceName
- The source file name.scope
- A scope for doing type name lookups.
-
setTemplateTypeNames
public void setTemplateTypeNames(java.util.List<TemplateType> keys)
Sets the template type name.
-
clearTemplateTypeNames
public void clearTemplateTypeNames()
Clears the template type name.
-
-