public class NamedType extends ProxyObjectType
NamedType
is a named reference to some other type. This provides
a convenient mechanism for implementing forward references to types; a
NamedType
can be used as a placeholder until its reference is
resolved. It is also useful for representing type names in JsDoc type
annotations, some of which may never be resolved (as they may refer to
types in host systems not yet supported by JSCompiler, such as the JVM.)
An important distinction: NamedType
is a type name reference,
whereas ObjectType
is a named type object, such as an Enum name.
The Enum itself is typically used only in a dot operator to name one of its
constants, or in a declaration, where its name will appear in a
NamedType.
A NamedType
is not currently a full-fledged typedef, because it
cannot resolve to any JavaScript type. It can only resolve to a named
JSTypeRegistry
type, or to FunctionType
or
EnumType
.
If full typedefs are to be supported, then each method on each type class
needs to be reviewed to make sure that everything works correctly through
typedefs. Alternatively, we would need to walk through the parse tree and
unroll each reference to a NamedType
to its resolved type before
applying the rest of the analysis.
TODO(user): Revisit all of this logic.
The existing typing logic is hacky. Unresolved types should get processed in a more consistent way, but with the Rhino merge coming, there will be much that has to be changed.
Modifier and Type | Class and Description |
---|---|
private static class |
NamedType.PropertyContinuation
Store enough information to define a property at a later time.
|
JSType.EqCache, JSType.ImplCache, JSType.MatchStatus, JSType.TypePair
Modifier and Type | Field and Description |
---|---|
private int |
charno |
private int |
lineno |
private java.util.List<NamedType.PropertyContinuation> |
propertyContinuations
Property-defining continuations.
|
private java.lang.String |
reference |
private static long |
serialVersionUID |
private java.lang.String |
sourceName |
private com.google.common.base.Predicate<JSType> |
validator
Validates the type resolution.
|
ALPHA, registry, templateTypeMap
Constructor and Description |
---|
NamedType(JSTypeRegistry registry,
java.lang.String reference,
java.lang.String sourceName,
int lineno,
int charno)
Create a named type based on the reference.
|
Modifier and Type | Method and Description |
---|---|
private void |
checkEnumElementCycle(ErrorReporter t) |
private void |
checkProtoCycle(ErrorReporter t) |
(package private) boolean |
defineProperty(java.lang.String propertyName,
JSType type,
boolean inferred,
Node propertyNode)
Defines a property.
|
private void |
finishPropertyContinuations() |
JSType |
getReferencedType()
Returns the type to which this refers (which is unknown if unresolved).
|
java.lang.String |
getReferenceName()
Gets the reference name for this object.
|
private JSType |
getTypedefType(ErrorReporter t,
StaticTypedSlot<JSType> slot) |
private void |
handleTypeCycle(ErrorReporter t) |
private void |
handleUnresolvedType(ErrorReporter t,
boolean ignoreForwardReferencedTypes) |
int |
hashCode() |
boolean |
hasReferenceName()
Returns true if the object is named.
|
boolean |
isNominalType()
Whether this type is a nominal type (a named instance object or
a named enum).
|
private JSType |
lookupViaProperties(ErrorReporter reporter,
StaticTypedScope<JSType> enclosing)
Resolves a type by looking up its first component in the scope, and
subsequent components as properties.
|
(package private) JSType |
resolveInternal(ErrorReporter t,
StaticTypedScope<JSType> enclosing)
Resolve the referenced type within the enclosing scope.
|
private void |
resolveViaProperties(ErrorReporter reporter,
StaticTypedScope<JSType> enclosing)
Resolves a named type by looking up its first component in the scope, and
subsequent components as properties.
|
private boolean |
resolveViaRegistry(ErrorReporter reporter)
Resolves a named type by looking it up in the registry.
|
private void |
setReferencedAndResolvedType(JSType type,
ErrorReporter reporter) |
boolean |
setValidator(com.google.common.base.Predicate<JSType> validator)
Certain types have constraints on them at resolution-time.
|
NamedType |
toMaybeNamedType() |
(package private) java.lang.String |
toStringHelper(boolean forAnnotations) |
<T> T |
visit(Visitor<T> visitor)
Visit this type with the given visitor.
|
(package private) void |
warning(ErrorReporter reporter,
java.lang.String message) |
canBeCalled, collapseUnion, findPropertyType, getConstructor, getCtorImplementedInterfaces, getImplicitPrototype, getJSDocInfo, getOwnerFunction, getPropertyMap, getReferencedObjTypeInternal, getReferencedTypeInternal, getTemplateTypeMap, getTemplateTypes, getTypeOfThis, hasAnyTemplateTypesInternal, isAllType, isCheckedUnknownType, isConstructor, isDict, isInstanceType, isInterface, isNativeObjectType, isNoObjectType, isNoResolvedType, isNoType, isNullable, isOrdinaryFunction, isStruct, isStructuralType, isSubtype, isSubtype, isUnknownType, matchConstraint, matchesNumberContext, matchesObjectContext, matchesStringContext, removeProperty, setJSDocInfo, setPropertyJSDocInfo, setReferencedType, testForEquality, toDebugHashCodeString, toMaybeEnumElementType, toMaybeEnumType, toMaybeFunctionType, toMaybeRecordType, toMaybeTemplateType, toMaybeTemplatizedType, toMaybeUnionType, visit, visitReferenceType
cast, checkStructuralEquivalenceHelper, clearCachedValues, collectPropertyNames, createDelegateSuffix, defineDeclaredProperty, defineInferredProperty, defineSynthesizedProperty, detectImplicitPrototypeCycle, detectInheritanceCycle, getCtorExtendedInterfaces, getDisplayName, getNormalizedReferenceName, getOwnPropertyJSDocInfo, getOwnPropertyNames, getOwnSlot, getParentScope, getPossibleToBooleanOutcomes, getPropertiesCount, getPropertyNames, getPropertyNode, getPropertyType, getPropertyTypeMap, getRootNode, getSlot, hasCachedValues, hasOwnDeclaredProperty, hasOwnProperty, hasProperty, isFunctionPrototypeType, isImplicitPrototype, isObject, isPropertyInExterns, isPropertyTypeDeclared, isPropertyTypeInferred, isStructuralSubtype, setOwnerFunction, setPropertyNode
autobox, autoboxesTo, canCastTo, canTestForEqualityWith, canTestForShallowEqualityWith, checkEquivalenceHelper, checkEquivalenceHelper, dereference, differsFrom, equals, extendTemplateTypeMap, filterNoResolvedType, getGreatestSubtype, getGreatestSubtype, getLeastSupertype, getLeastSupertype, getNativeType, getRestrictedTypeGivenToBooleanOutcome, getTypesUnderEquality, getTypesUnderInequality, getTypesUnderShallowEquality, getTypesUnderShallowInequality, hasAnyTemplateTypes, hasDisplayName, isArrayType, isBooleanObjectType, isBooleanValueType, isBottom, isDateType, isEmptyType, isEnumElementType, isEnumType, isEquivalent, isEquivalentTo, isEquivalentTo, isExemptFromTemplateTypeInvariance, isExplicitlyVoidable, isFunctionType, isGlobalThisType, isNamedType, isNominalConstructor, isNullType, isNumber, isNumberObjectType, isNumberValueType, isRecordType, isRegexpType, isResolved, isString, isStringObjectType, isStringValueType, isStructuralInterface, isSubtypeHelper, isSubtypeOf, isSubtypeWithoutStructuralTyping, isTemplateType, isTemplatizedType, isTheObjectType, isUnionType, isVoidable, isVoidType, matchesInt32Context, matchesUint32Context, resolve, restrictByNotNullOrUndefined, safeResolve, setResolvedTypeInternal, testForEqualityHelper, toAnnotationString, toMaybeFunctionType, toMaybeObjectType, toNonNullAnnotationString, toObjectType, toString, unboxesTo
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
isBottom, isEquivalentTo, isFunctionType, isSubtypeOf, restrictByNotNullOrUndefined, toMaybeObjectType
private static final long serialVersionUID
private final java.lang.String reference
private final java.lang.String sourceName
private final int lineno
private final int charno
private com.google.common.base.Predicate<JSType> validator
private java.util.List<NamedType.PropertyContinuation> propertyContinuations
NamedType(JSTypeRegistry registry, java.lang.String reference, java.lang.String sourceName, int lineno, int charno)
boolean defineProperty(java.lang.String propertyName, JSType type, boolean inferred, Node propertyNode)
ObjectType
For clarity, callers should prefer ObjectType.defineDeclaredProperty(java.lang.String, com.google.javascript.rhino.jstype.JSType, com.google.javascript.rhino.Node)
and
ObjectType.defineInferredProperty(java.lang.String, com.google.javascript.rhino.jstype.JSType, com.google.javascript.rhino.Node)
.
defineProperty
in class ProxyObjectType
propertyName
- the property's nametype
- the typeinferred
- true
if this property's type is inferredpropertyNode
- the node that represents the definition of property.
Depending on the actual sub-type the node type might be different.
The general idea is to have an estimate of where in the source code
this property is defined.private void finishPropertyContinuations()
public JSType getReferencedType()
public java.lang.String getReferenceName()
ObjectType
getReferenceName
in class ProxyObjectType
null
if this is an anonymous
objectjava.lang.String toStringHelper(boolean forAnnotations)
toStringHelper
in class ProxyObjectType
forAnnotations
- Whether this is for use in code generator
annotations. Otherwise, it's for warnings.public boolean hasReferenceName()
ObjectType
hasReferenceName
in class ProxyObjectType
public NamedType toMaybeNamedType()
toMaybeNamedType
in class JSType
public boolean isNominalType()
JSType
isNominalType
in class ProxyObjectType
public int hashCode()
hashCode
in class ProxyObjectType
JSType resolveInternal(ErrorReporter t, StaticTypedScope<JSType> enclosing)
private boolean resolveViaRegistry(ErrorReporter reporter)
private void resolveViaProperties(ErrorReporter reporter, StaticTypedScope<JSType> enclosing)
private JSType lookupViaProperties(ErrorReporter reporter, StaticTypedScope<JSType> enclosing)
private void setReferencedAndResolvedType(JSType type, ErrorReporter reporter)
private void handleTypeCycle(ErrorReporter t)
private void checkEnumElementCycle(ErrorReporter t)
private void checkProtoCycle(ErrorReporter t)
private void handleUnresolvedType(ErrorReporter t, boolean ignoreForwardReferencedTypes)
private JSType getTypedefType(ErrorReporter t, StaticTypedSlot<JSType> slot)
public boolean setValidator(com.google.common.base.Predicate<JSType> validator)
JSType
@extends
annotation must be an
object. Clients should inject a validator that emits a warning
if the type does not validate, and return false.setValidator
in class JSType
void warning(ErrorReporter reporter, java.lang.String message)
public <T> T visit(Visitor<T> visitor)
JSType
visit
in class ProxyObjectType
Visitor