/** * Create an anonymous object type. * @param info Used to mark object literals as structs; can be {@code null} */ public ObjectType createAnonymousObjectType(JSDocInfo info) { PrototypeObjectType type = new PrototypeObjectType( this, null, null, true /* anonymousType */); type.setPrettyPrint(true); type.setJSDocInfo(info); return type; }
/** * Set the implicit prototype if it's possible to do so. * There are a few different reasons why this could be a no-op: 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. */ public void resetImplicitPrototype(JSType type, ObjectType newImplicitProto) { if (type instanceof PrototypeObjectType) { PrototypeObjectType poType = (PrototypeObjectType) type; poType.clearCachedValues(); poType.setImplicitPrototype(newImplicitProto); } }
public void matchRecordTypeConstraint(ObjectType constraintObj) { for (String prop : constraintObj.getOwnPropertyNames()) { JSType propType = constraintObj.getPropertyType(prop); if (!isPropertyTypeDeclared(prop)) { JSType typeToInfer = propType; if (!hasProperty(prop)) { typeToInfer = getNativeType(JSTypeNative.VOID_TYPE) .getLeastSupertype(propType); } defineInferredProperty(prop, typeToInfer, null); } } }
@Override public JSType unboxesTo() { if (isStringObjectType()) { return getNativeType(JSTypeNative.STRING_TYPE); } else if (isBooleanObjectType()) { return getNativeType(JSTypeNative.BOOLEAN_TYPE); } else if (isNumberObjectType()) { return getNativeType(JSTypeNative.NUMBER_TYPE); } else { return super.unboxesTo(); } }
return PrototypeObjectType.isSubtype(this, that.toMaybeRecordType(), implicitImplCache); FunctionType thatCtor = thatObj == null ? null : thatObj.getConstructor(); if (getConstructor() != null && getConstructor().isInterface()) { for (ObjectType thisInterface : getCtorExtendedInterfaces()) { if (thisInterface.isSubtype(that, implicitImplCache)) { return true; Iterable<ObjectType> thisInterfaces = getCtorImplementedInterfaces(); for (ObjectType thisInterface : thisInterfaces) { if (thisInterface.isSubtype(that, implicitImplCache)) { if (isUnknownType() || implicitPrototypeChainIsUnknown()) { return thatObj != null && isImplicitPrototype(thatObj);
@Override JSType resolveInternal(ErrorReporter reporter) { setResolvedTypeInternal(this); ObjectType implicitPrototype = getImplicitPrototype(); if (implicitPrototype != null) { implicitPrototypeFallback = (ObjectType) implicitPrototype.resolve(reporter); FunctionType ctor = getConstructor(); if (ctor != null) { FunctionType superCtor = ctor.getSuperClassConstructor(); if (superCtor != null) { // If the super ctor of this prototype object was not known before resolution, then the // subTypes would not have been set. Update them. superCtor.addSubClassAfterResolution(ctor); } } } for (Property prop : properties.values()) { prop.setType(safeResolve(prop.getType(), reporter)); } return this; }
/** * Create an object type. */ public ObjectType createObjectType(String name, ObjectType implicitPrototype) { return new PrototypeObjectType(this, name, implicitPrototype); }
@Override public void setPropertyJSDocInfo(String propertyName, JSDocInfo info) { if (info != null) { if (properties.getOwnProperty(propertyName) == null) { // If docInfo was attached, but the type of the property // was not defined anywhere, then we consider this an explicit // declaration of the property. defineInferredProperty(propertyName, getPropertyType(propertyName), null); } // The prototype property is not represented as a normal Property. // We probably don't want to attach any JSDoc to it anyway. Property property = properties.getOwnProperty(propertyName); if (property != null) { property.setJSDocInfo(info); } } }
@Override boolean defineProperty(String propertyName, JSType type, boolean inferred, Node propertyNode) { if (isFrozen) { return false; } return super.defineProperty(propertyName, type, inferred, propertyNode); }
@Override public boolean hasProperties() { if (prototypeSlot != null) { return true; } return !super.getOwnPropertyNames().isEmpty(); }
@Override public final void clearCachedValues() { super.clearCachedValues(); if (subTypes != null) { for (FunctionType subType : subTypes) { subType.clearCachedValues(); } } if (!isNativeObjectType()) { if (hasInstanceType()) { getInstanceType().clearCachedValues(); } if (prototypeSlot != null) { ((ObjectType) prototypeSlot.getType()).clearCachedValues(); } } }
private boolean implicitPrototypeChainIsUnknown() { ObjectType p = getImplicitPrototype(); while (p != null) { if (p.isUnknownType()) { return true; } p = p.getImplicitPrototype(); } return false; }
return PrototypeObjectType.isSubtype( this, that.toMaybeRecordType(), implicitImplCache, subtypingMode); FunctionType thatCtor = thatObj == null ? null : thatObj.getConstructor(); if (getConstructor() != null && getConstructor().isInterface()) { for (ObjectType thisInterface : getCtorExtendedInterfaces()) { if (thisInterface.isSubtype(that, implicitImplCache, subtypingMode)) { return true; Iterable<ObjectType> thisInterfaces = getCtorImplementedInterfaces(); for (ObjectType thisInterface : thisInterfaces) { if (thisInterface.isSubtype(that, implicitImplCache, subtypingMode)) { if (isUnknownType()) { return thatObj != null && isImplicitPrototype(thatObj);
@Override public final JSType unboxesTo() { if (isStringObjectType()) { return getNativeType(JSTypeNative.STRING_TYPE); } else if (isBooleanObjectType()) { return getNativeType(JSTypeNative.BOOLEAN_TYPE); } else if (isNumberObjectType()) { return getNativeType(JSTypeNative.NUMBER_TYPE); } else if (isSymbolObjectType()) { return getNativeType(JSTypeNative.SYMBOL_TYPE); } else { return super.unboxesTo(); } }
@Override JSType resolveInternal(ErrorReporter t, StaticTypedScope<JSType> scope) { setResolvedTypeInternal(this); ObjectType implicitPrototype = getImplicitPrototype(); if (implicitPrototype != null) { implicitPrototypeFallback = (ObjectType) implicitPrototype.resolve(t, scope); FunctionType ctor = getConstructor(); if (ctor != null) { FunctionType superCtor = ctor.getSuperClassConstructor(); if (superCtor != null) { // If the super ctor of this prototype object was not known before resolution, then the // subTypes would not have been set. Update them. superCtor.addSubTypeIfNotPresent(ctor); } } } for (Property prop : properties.values()) { prop.setType(safeResolve(prop.getType(), t, scope)); } return this; }
/** * Create an object type. */ public ObjectType createObjectType(String name, ObjectType implicitPrototype) { return new PrototypeObjectType(this, name, implicitPrototype); }
@Override public void setPropertyJSDocInfo(String propertyName, JSDocInfo info) { if (info != null) { if (properties.getOwnProperty(propertyName) == null) { // If docInfo was attached, but the type of the property // was not defined anywhere, then we consider this an explicit // declaration of the property. defineInferredProperty(propertyName, getPropertyType(propertyName), null); } // The prototype property is not represented as a normal Property. // We probably don't want to attach any JSDoc to it anyway. Property property = properties.getOwnProperty(propertyName); if (property != null) { property.setJSDocInfo(info); } } }
@Override boolean defineProperty(String propertyName, JSType type, boolean inferred, Node propertyNode) { if (isFrozen) { return false; } return super.defineProperty(propertyName, type, inferred, propertyNode); }
public final boolean hasProperties() { return !super.getOwnPropertyNames().isEmpty(); }
@Override public void clearCachedValues() { super.clearCachedValues(); if (subTypes != null) { for (FunctionType subType : subTypes) { subType.clearCachedValues(); } } if (!isNativeObjectType()) { if (hasInstanceType()) { getInstanceType().clearCachedValues(); } if (prototypeSlot != null) { ((ObjectType) prototypeSlot.getType()).clearCachedValues(); } } }