/** * Return an object with the same nominal type as obj, * but without any possible extra properties that exist on obj. */ static ObjectType getCanonicalInstance(ObjectType obj) { FunctionType ctor = obj.getConstructor(); return ctor == null ? obj : ctor.getInstanceType(); }
/** * Utility function for getting an instance type from a var pointing * to the constructor. */ static ObjectType getInstanceOfCtor(@Nullable TypedVar v) { FunctionType ctor = getFunctionType(v); if (ctor != null && ctor.isConstructor()) { return ctor.getInstanceType(); } 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) { checkArgument(constructor.hasInstanceType(), "Constructor %s without instance type.", constructor); ObjectType instanceType = constructor.getInstanceType(); addRelatedType(instanceType, related); }
/** Create a new constructor with the parameters and return type stripped. */ public FunctionType forgetParameterAndReturnTypes() { FunctionType result = new FunctionType( registry, getReferenceName(), source, registry.createArrowType(null, null), getInstanceType(), null, true, false); result.setPrototypeBasedOn(getInstanceType()); return result; }
@Override public NominalTypeBuilder superClass() { FunctionType ctor = instance.getSuperClassConstructor(); if (ctor == null) { return null; } return new NominalTypeBuilderOti(ctor, ctor.getInstanceType()); }
private JSType getInstanceTypeForClassNode(Node classNode) { checkArgument(classNode.isClass(), classNode); final JSType constructorType = classNode.getJSType(); final JSType result; if (constructorType != null) { checkArgument(constructorType.isConstructor(), classNode); result = JSType.toMaybeFunctionType(constructorType).getInstanceType(); } else { result = null; } return result; }
private static boolean typesAreRelated(JSType expected, JSType actual) { if (actual.isConstructor() && expected.isConstructor()) { // All constructors can cast to one another, even if they're incompatible. // If both types are constructors then compare the instance types instead. actual = actual.toMaybeFunctionType().getInstanceType(); expected = expected.toMaybeFunctionType().getInstanceType(); } actual = firstNonNull(actual.autobox(), actual); return actual.canCastTo(expected); }
private JSType maybeGetInstanceTypeFromPrototypeRef(Node src) { JSType ownerType = getJSType(src.getFirstChild()); if (ownerType.isFunctionType() && ownerType.isConstructor()) { FunctionType functionType = ((FunctionType) ownerType); return functionType.getInstanceType(); } return null; }
@Override public JSType caseUnknownType() { FunctionType funcTarget = JSType.toMaybeFunctionType(target); if (funcTarget != null && funcTarget.hasInstanceType()) { return funcTarget.getInstanceType(); } return getNativeType(UNKNOWN_TYPE); }
private void checkPropertyInheritanceOnClassMember( NodeTraversal t, Node key, String propertyName, FunctionType ctorType) { if (key.isStaticMember()) { // TODO(sdh): Handle static members later - will probably need to add an extra boolean // parameter to checkDeclaredPropertyInheritance, as well as an extra case to getprop // assigns to check if the owner's type is an ES6 constructor. Put it off for now // because it's a change in behavior from transpiled code and it may break things. return; } ObjectType owner = key.isStaticMember() ? ctorType : ctorType.getInstanceType(); checkPropertyInheritance(t, key, propertyName, ctorType, owner); }
private JSType maybeGetInstanceTypeFromPrototypeRef(Node src) { JSType ownerType = getJSType(src.getFirstChild()); if (ownerType.isConstructor()) { FunctionType functionType = ownerType.toMaybeFunctionType(); return functionType.getInstanceType(); } return null; }
/** Gets the symbol for the given constructor or interface. */ public Symbol getSymbolDeclaredBy(FunctionType fn) { checkState(fn.isConstructor() || fn.isInterface()); ObjectType instanceType = fn.getInstanceType(); return getSymbolForName(fn.getSource(), instanceType.getReferenceName()); }
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; }
/** * Gets the symbol for the given constructor or interface. */ public Symbol getSymbolDeclaredBy(FunctionType fn) { Preconditions.checkState(fn.isConstructor() || fn.isInterface()); ObjectType instanceType = fn.getInstanceType(); return getSymbolForName(fn.getSource(), instanceType.getReferenceName()); }
/** Create a new constructor with the parameters and return type stripped. */ public final FunctionType forgetParameterAndReturnTypes() { FunctionType result = new FunctionBuilder(registry) .withName(getReferenceName()) .withSourceNode(source) .withTypeOfThis(getInstanceType()) .withKind(kind) .build(); result.setPrototypeBasedOn(getInstanceType()); return result; }
@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; }
private void declareNativeFunctionType(TypedScope scope, JSTypeNative tId) { FunctionType t = typeRegistry.getNativeFunctionType(tId); declareNativeType(scope, t.getInstanceType().getReferenceName(), t); declareNativeType( scope, t.getPrototype().getReferenceName(), t.getPrototype()); }
private void declareNativeFunctionType(TypedScope scope, JSTypeNative tId) { FunctionType t = typeRegistry.getNativeFunctionType(tId); declareNativeType(scope, t.getInstanceType().getReferenceName(), t); declareNativeType( scope, t.getPrototype().getReferenceName(), t.getPrototype()); }
private JSType evalInstanceOf(Node ttlAst, NameResolver nameResolver) { JSType type = evalInternal(getCallArgument(ttlAst, 0), nameResolver); if (type.isUnknownType() || !type.isConstructor()) { reportWarning(ttlAst, INVALID_CTOR, type.getDisplayName()); return getUnknownType(); } return type.toMaybeFunctionType().getInstanceType(); }