/** * Gets the type of instance of this function. * * @throws IllegalStateException if this function is not a constructor (see {@link * #isConstructor()}). */ public final ObjectType getInstanceType() { Preconditions.checkState(hasInstanceType(), "Expected a constructor; got %s", this); return typeOfThis.toObjectType(); }
/** * Gets the type of instance of this function. * @throws IllegalStateException if this function is not a constructor * (see {@link #isConstructor()}). */ @Override public ObjectType getInstanceType() { Preconditions.checkState(hasInstanceType()); return typeOfThis.toObjectType(); }
/** * Adds the instance of the given constructor, its implicit prototype and all * its related types to the given bit set. */ private void addRelatedInstance(FunctionType constructor, JSTypeBitSet related) { checkArgument(constructor.hasInstanceType(), "Constructor %s without instance type.", constructor); ObjectType instanceType = constructor.getInstanceType(); addRelatedType(instanceType, related); }
@Override public JSType caseUnknownType() { FunctionType funcTarget = JSType.toMaybeFunctionType(target); if (funcTarget != null && funcTarget.hasInstanceType()) { return funcTarget.getInstanceType(); } return getNativeType(UNKNOWN_TYPE); }
@Override public JSType caseUnknownType() { FunctionType funcTarget = JSType.toMaybeFunctionType(target); if (funcTarget != null && funcTarget.hasInstanceType()) { return funcTarget.getInstanceType(); } return getNativeType(UNKNOWN_TYPE); }
/** * Returns the corresponding instance if `maybePrototype` is a prototype of a constructor, * otherwise null */ @Nullable private static JSType getInstanceIfPrototype(JSType maybePrototype) { if (maybePrototype.isFunctionPrototypeType()) { FunctionType constructor = maybePrototype.toObjectType().getOwnerFunction(); if (constructor != null) { if (!constructor.hasInstanceType()) { // this can happen when adding to the prototype of a non-constructor function return null; } return constructor.getInstanceType(); } } return null; }
/** * When a class B inherits from A and A is annotated as a struct, then B automatically gets the * annotation, even if B's constructor is not explicitly annotated. */ public final boolean makesStructs() { if (!hasInstanceType()) { return false; } if (propAccess == PropAccess.STRUCT) { return true; } FunctionType superc = getSuperClassConstructor(); if (superc != null && superc.makesStructs()) { setStruct(); return true; } return false; }
private JSType applyCommonRestriction(JSType type) { if (target.isUnknownType()) { return type; } FunctionType funcTarget = target.toMaybeFunctionType(); if (funcTarget.hasInstanceType()) { return type.getGreatestSubtype(funcTarget.getInstanceType()); } return null; } }
@Override public JSType caseUnionType(UnionType type) { if (target.isUnknownType()) { return type; } FunctionType funcTarget = target.toMaybeFunctionType(); if (funcTarget.hasInstanceType()) { return type.getRestrictedUnion(funcTarget.getInstanceType()); } return null; }
@Override public JSType caseObjectType(ObjectType type) { if (target.isUnknownType()) { return type; } FunctionType funcTarget = target.toMaybeFunctionType(); if (funcTarget.hasInstanceType()) { if (type.isSubtypeOf(funcTarget.getInstanceType())) { return null; } return type; } return null; }
/** * When a class B inherits from A and A is annotated as a struct, then B * automatically gets the annotation, even if B's constructor is not * explicitly annotated. */ public boolean makesStructs() { if (!hasInstanceType()) { return false; } if (propAccess == PropAccess.STRUCT) { return true; } FunctionType superc = getSuperClassConstructor(); if (superc != null && superc.makesStructs()) { setStruct(); return true; } return false; }
private JSType applyCommonRestriction(JSType type) { if (target.isUnknownType()) { return type; } FunctionType funcTarget = target.toMaybeFunctionType(); if (funcTarget.hasInstanceType()) { return type.getGreatestSubtype(funcTarget.getInstanceType()); } return null; } }
@Override public JSType caseUnionType(UnionType type) { if (target.isUnknownType()) { return type; } FunctionType funcTarget = target.toMaybeFunctionType(); if (funcTarget.hasInstanceType()) { return type.getRestrictedUnion(funcTarget.getInstanceType()); } return null; }
@Override public JSType caseObjectType(ObjectType type) { if (target.isUnknownType()) { return type; } FunctionType funcTarget = target.toMaybeFunctionType(); if (funcTarget.hasInstanceType()) { if (type.isSubtype(funcTarget.getInstanceType())) { return null; } return type; } return null; }
/** * Adds the instance of the given constructor, its implicit prototype and all * its related types to the given bit set. */ private void addRelatedInstance( FunctionType constructor, JSTypeBitSet related) { // TODO(user): A constructor which doesn't have an instance type // (e.g. it's missing the @constructor annotation) should be an invalidating // type which doesn't reach this code path. if (constructor.hasInstanceType()) { ObjectType instanceType = constructor.getInstanceType(); related.set(getIntForType(instanceType.getImplicitPrototype())); computeRelatedTypes(instanceType); related.or(relatedBitsets.get(instanceType)); } }
@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(); } } }
@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(); } } }
if (prototypeSlot != null && hasInstanceType() && baseType.equals(getInstanceType())) {
/** * Visits a NEW node. */ private void visitNew(NodeTraversal t, Node n) { Node constructor = n.getFirstChild(); JSType type = getJSType(constructor).restrictByNotNullOrUndefined(); if (type.isConstructor() || type.isEmptyType() || type.isUnknownType()) { FunctionType fnType = type.toMaybeFunctionType(); if (fnType != null && fnType.hasInstanceType()) { visitParameterList(t, n, fnType); ensureTyped(t, n, fnType.getInstanceType()); } else { ensureTyped(t, n); } } else { report(t, n, NOT_A_CONSTRUCTOR); ensureTyped(t, n); } }
/** * Visits a NEW node. */ private void visitNew(NodeTraversal t, Node n) { Node constructor = n.getFirstChild(); JSType type = getJSType(constructor).restrictByNotNullOrUndefined(); if (!couldBeAConstructor(type) || type.isEquivalentTo(typeRegistry.getNativeType(SYMBOL_OBJECT_FUNCTION_TYPE))) { report(t, n, NOT_A_CONSTRUCTOR); ensureTyped(n); return; } FunctionType fnType = type.toMaybeFunctionType(); if (fnType != null && fnType.hasInstanceType()) { FunctionType ctorType = fnType.getInstanceType().getConstructor(); if (ctorType != null && ctorType.isAbstract()) { report(t, n, INSTANTIATE_ABSTRACT_CLASS); } visitArgumentList(t, n, fnType); ensureTyped(n, fnType.getInstanceType()); } else { ensureTyped(n); } }