public class JSTypeRegistry extends Object implements TypeIRegistry
This class is not thread-safe.
| Modifier and Type | Field and Description |
|---|---|
static String |
OBJECT_ELEMENT_TEMPLATE
Deprecated.
|
| Constructor and Description |
|---|
JSTypeRegistry(ErrorReporter reporter) |
JSTypeRegistry(ErrorReporter reporter,
Set<String> forwardDeclaredTypes)
Constructs a new type registry populated with the built-in types.
|
| Modifier and Type | Method and Description |
|---|---|
JSType |
buildRecordTypeFromObject(ObjectTypeI obj) |
boolean |
canPropertyBeDefined(JSType type,
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(String name,
Node source,
Node parameters,
JSType returnType,
com.google.common.collect.ImmutableList<TemplateType> templateKeys,
boolean isAbstract)
Creates a constructor function type.
|
JSType |
createDefaultObjectUnion(JSType type)
Creates a type representing nullable values of the given type.
|
EnumType |
createEnumType(String name,
Node source,
JSType elementsType)
Creates an enum type.
|
FunctionType |
createFunctionType(JSType returnType,
JSType... parameterTypes)
Creates a function type.
|
FunctionType |
createFunctionType(JSType returnType,
Node parameters) |
JSType |
createFunctionTypeWithInstanceType(ObjectType instanceType,
JSType returnType,
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.
|
String |
createGetterPropName(String originalPropName)
For NTI, this method returns an obfuscated name that represents the getter.
|
FunctionType |
createInterfaceType(String name,
Node source,
com.google.common.collect.ImmutableList<TemplateType> templateKeys,
boolean struct)
Creates an interface function type.
|
NamedType |
createNamedType(String reference,
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(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.
|
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.
|
JSType |
createRecordType(Map<String,? extends TypeI> props)
Creates an anonymous structural type with the given properties.
|
String |
createSetterPropName(String originalPropName)
For NTI, this method returns an obfuscated name that represents the setter.
|
TemplateType |
createTemplateType(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(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,
Map<TemplateType,JSType> templatizedTypes)
Creates a templatized instance of the specified type.
|
JSType |
createTypeFromCommentNode(Node n) |
JSType |
createTypeFromCommentNode(Node n,
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.
|
JSType |
createUnionType(List<? extends TypeI> variants) |
boolean |
declareType(String name,
JSType t)
Records declared global type names.
|
JSType |
evaluateTypeExpression(JSTypeExpression expr,
TypeIEnv<TypeI> scope) |
JSType |
evaluateTypeExpressionInGlobalScope(JSTypeExpression expr) |
Collection<FunctionTypeI> |
getDirectImplementors(ObjectType interfaceInstance)
Returns a collection of types that directly implement
interfaceInstance. |
Iterable<ObjectType> |
getEachReferenceTypeWithProperty(String propertyName)
Returns each reference type that has a property
propertyName
defined on it. |
ErrorReporter |
getErrorReporter() |
JSType |
getGreatestSubtypeWithProperty(JSType type,
String propertyName)
Gets the greatest subtype of the
type that has a property
propertyName defined on it. |
FunctionType |
getNativeFunctionType(JSTypeNative typeId) |
ObjectType |
getNativeObjectType(JSTypeNative typeId) |
JSType |
getNativeType(JSTypeNative typeId) |
TemplateType |
getObjectElementKey() |
TemplateType |
getObjectIndexKey() |
String |
getReadableTypeName(Node n)
The nice API for this method is a single argument; dereference is a detail.
|
String |
getReadableTypeNameNoDeref(Node n) |
com.google.common.collect.ImmutableList<TemplateType> |
getTemplateTypesOfBuiltin(String fnName) |
JSType |
getType(StaticTypedScope<JSType> scope,
String jsTypeName,
String sourceName,
int lineno,
int charno)
Looks up a type by name.
|
JSType |
getType(String jsTypeName)
Looks up a native type by name.
|
void |
identifyNonNullableName(String name)
Identifies the name of a typedef or enum before we actually declare it.
|
ObjectTypeI |
instantiateGenericsWithUnknown(ObjectType obj) |
TypeI |
instantiateGenericType(ObjectTypeI genericType,
com.google.common.collect.ImmutableList<? extends TypeI> typeArgs)
Instantiates genericType using typeArgs.
|
boolean |
isForwardDeclaredType(String name)
Whether this is a forward-declared type name.
|
boolean |
isTemplatedBuiltin(String fnName,
JSDocInfo info)
Check if a function declaration is one of the templated builitin contructor/interfaces,
namely one of IObject, IArrayLike, or Array
|
void |
overwriteDeclaredType(String name,
JSType t)
Overrides a declared global type name.
|
void |
registerPropertyOnType(String propertyName,
JSType type)
Tells the type system that
owner may have a property named
propertyName. |
void |
removeType(String jsTypeName)
Removes a type by name.
|
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 |
restoreContents(ObjectInputStream in)
Restores the derived state.
|
void |
saveContents(ObjectOutputStream out)
Saves the derived state.
|
void |
setOptimizePropertyIndex_TRANSITIONAL_METHOD(boolean optimizePropIndex) |
void |
setTemplateTypeNames(List<TemplateType> keys)
Sets the template type name.
|
void |
unregisterPropertyOnType(String propertyName,
JSType type)
Removes the index's reference to a property on the given type (if it is
currently registered).
|
@Deprecated public static final String OBJECT_ELEMENT_TEMPLATE
public JSTypeRegistry(ErrorReporter reporter)
public JSTypeRegistry(ErrorReporter reporter, Set<String> forwardDeclaredTypes)
public void setOptimizePropertyIndex_TRANSITIONAL_METHOD(boolean optimizePropIndex)
public TemplateType getObjectElementKey()
public TemplateType getObjectIndexKey()
public boolean isTemplatedBuiltin(String fnName, JSDocInfo info)
fnName - the function's nameinfo - the JSDoc from the function declarationpublic com.google.common.collect.ImmutableList<TemplateType> getTemplateTypesOfBuiltin(String fnName)
public ErrorReporter getErrorReporter()
public void resetForTypeCheck()
public void registerPropertyOnType(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").public void unregisterPropertyOnType(String propertyName, JSType type)
propertyName - the name of the property to unregistertype - the type to unregister the property on.public JSType getGreatestSubtypeWithProperty(JSType type, String propertyName)
type that has a property
propertyName defined on it.public boolean canPropertyBeDefined(JSType type, String propertyName)
public Iterable<ObjectType> getEachReferenceTypeWithProperty(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].public Collection<FunctionTypeI> 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(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(String name, JSType t)
public boolean isForwardDeclaredType(String name)
public String getReadableTypeName(Node n)
getReadableTypeName in interface TypeIRegistrypublic String createGetterPropName(String originalPropName)
TypeIRegistrycreateGetterPropName in interface TypeIRegistrypublic String createSetterPropName(String originalPropName)
TypeIRegistrycreateSetterPropName in interface TypeIRegistrypublic void removeType(String jsTypeName)
jsTypeName - The name string.public JSType getType(String jsTypeName)
getType in interface TypeIRegistryjsTypeName - The name string.null it cannot be foundpublic JSType getNativeType(JSTypeNative typeId)
getNativeType in interface TypeIRegistrypublic ObjectType getNativeObjectType(JSTypeNative typeId)
getNativeObjectType in interface TypeIRegistrypublic FunctionType getNativeFunctionType(JSTypeNative typeId)
getNativeFunctionType in interface TypeIRegistrypublic JSType getType(StaticTypedScope<JSType> scope, String jsTypeName, 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 evaluateTypeExpressionInGlobalScope(JSTypeExpression expr)
evaluateTypeExpressionInGlobalScope in interface TypeIRegistrypublic 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(List<? extends TypeI> variants)
createUnionType in interface TypeIRegistrypublic JSType createUnionType(JSTypeNative... variants)
public EnumType createEnumType(String name, Node source, JSType elementsType)
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' typespublic JSType createFunctionTypeWithInstanceType(ObjectType instanceType, JSType returnType, List<JSType> parameterTypes)
this refers to an object instance.instanceType - the type of thisreturnType - 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)
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.public JSType buildRecordTypeFromObject(ObjectTypeI obj)
buildRecordTypeFromObject in interface TypeIRegistrypublic JSType createRecordType(Map<String,? extends TypeI> props)
TypeIRegistrycreateRecordType in interface TypeIRegistrypublic ObjectType createObjectType(String name, ObjectType implicitPrototype)
public ObjectType createAnonymousObjectType(JSDocInfo info)
info - Used to mark object literals as structs; can be nullpublic boolean resetImplicitPrototype(JSType type, ObjectType newImplicitProto)
public FunctionType createConstructorType(String name, Node source, Node parameters, JSType returnType, com.google.common.collect.ImmutableList<TemplateType> templateKeys, boolean isAbstract)
name - the function's name or null to indicate that the function is anonymous.source - the node defining this function. Its type (Node.getToken() ()}) 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.isAbstract - whether the function type represents an abstract classpublic FunctionType createInterfaceType(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.getToken()) must be
Token.FUNCTION.templateKeys - the templatized types for the interface.public TemplateType createTemplateType(String name)
public TemplateType createTemplateTypeWithTransformation(String name, Node expr)
public TemplateTypeMap createTemplateTypeMap(com.google.common.collect.ImmutableList<TemplateType> templateKeys, com.google.common.collect.ImmutableList<JSType> templateValues)
public ObjectTypeI instantiateGenericsWithUnknown(ObjectType obj)
public TypeI instantiateGenericType(ObjectTypeI genericType, com.google.common.collect.ImmutableList<? extends TypeI> typeArgs)
TypeIRegistryinstantiateGenericType in interface TypeIRegistrypublic 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, 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(String reference, String sourceName, int lineno, int charno)
public void identifyNonNullableName(String name)
public JSType evaluateTypeExpression(JSTypeExpression expr, TypeIEnv<TypeI> scope)
evaluateTypeExpression in interface TypeIRegistrypublic JSType createTypeFromCommentNode(Node n)
createTypeFromCommentNode in interface TypeIRegistrypublic JSType createTypeFromCommentNode(Node n, String sourceName, StaticTypedScope<? extends TypeI> scope)
n - The node with type info.sourceName - The source file name.scope - A scope for doing type name lookups.public void setTemplateTypeNames(List<TemplateType> keys)
public void clearTemplateTypeNames()
@GwtIncompatible(value="ObjectOutputStream") public void saveContents(ObjectOutputStream out) throws IOException
IOException@GwtIncompatible(value="ObjectInputStream") public void restoreContents(ObjectInputStream in) throws IOException, ClassNotFoundException
IOExceptionClassNotFoundExceptionCopyright © 2009-2017 Google. All Rights Reserved.