public final ObjectType getPrototypeProperty() { return getPrototype(); }
@Override public final Property getSlot(String name) { if ("prototype".equals(name)) { // Lazy initialization of the prototype field. getPrototype(); return prototypeSlot; } else { return super.getSlot(name); } }
@Override public ObjectType getImplicitPrototype() { return getConstructor().getPrototype(); }
@Override public Property getSlot(String name) { if ("prototype".equals(name)) { // Lazy initialization of the prototype field. getPrototype(); return prototypeSlot; } else { return super.getSlot(name); } }
@Override public ObjectType getImplicitPrototype() { return getConstructor().getPrototype(); }
private ObjectType getSuperType(FunctionType type) { ObjectType proto = type.getPrototype(); if (proto == null) return null; ObjectType implicitProto = proto.getImplicitPrototype(); if (implicitProto == null) return null; return "Object".equals(implicitProto.getDisplayName()) ? null : implicitProto; }
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()); }
/** * Given a constructor or an interface type, get its superclass constructor * or {@code null} if none exists. */ public FunctionType getSuperClassConstructor() { Preconditions.checkArgument(isConstructor() || isInterface()); ObjectType maybeSuperInstanceType = getPrototype().getImplicitPrototype(); if (maybeSuperInstanceType == null) { return null; } return maybeSuperInstanceType.getConstructor(); }
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()); }
/** * Given a constructor or an interface type, get its superclass constructor or {@code null} if * none exists. */ @Override public final FunctionType getSuperClassConstructor() { checkArgument(isConstructor() || isInterface()); ObjectType maybeSuperInstanceType = getPrototype().getImplicitPrototype(); if (maybeSuperInstanceType == null) { return null; } return maybeSuperInstanceType.getConstructor(); }
/** Gets the symbol for the prototype of the given constructor or interface. */ public Symbol getSymbolForInstancesOf(FunctionType fn) { checkState(fn.isConstructor() || fn.isInterface()); ObjectType pType = fn.getPrototype(); return getSymbolForName(fn.getSource(), pType.getReferenceName()); }
/** * Gets the symbol for the prototype of the given constructor or interface. */ public Symbol getSymbolForInstancesOf(FunctionType fn) { Preconditions.checkState(fn.isConstructor() || fn.isInterface()); ObjectType pType = fn.getPrototype(); return getSymbolForName(fn.getSource(), pType.getReferenceName()); }
private void aggregateFieldsFromClass(Set<String> fields, ObjectType superType) { // visit instance properties. for (String field : superType.getOwnPropertyNames()) { if (!superType.getPropertyType(field).isFunctionType()) { fields.add(field); } } // visit prototype properties. if (superType.getConstructor() != null && superType.getConstructor().getPrototype() != null && superType.getConstructor().getPrototype().getOwnPropertyNames() != null) { for (String field : superType.getConstructor().getPrototype().getOwnPropertyNames()) { // getPropertyType works with non-owned property names, i.e. names from the prototype // chain. if (!superType.getPropertyType(field).isFunctionType()) { fields.add(field); } } } }
/** * Returns the owner type for a class member function, getter, or setter. * * <p>For a member on class C, this is either `C` for a static member or `C.prototype` for a * nonstatic member. */ private ObjectType determineOwnerTypeForClassMember(Node member) { // MEMBER_FUNCTION_DEF -> CLASS_MEMBERS -> CLASS or // GETTER_DEF -> CLASS_MEMBERS -> CLASS Node ownerNode = member.getGrandparent(); checkState(ownerNode.isClass()); ObjectType ownerType = ownerNode.getJSType().toMaybeFunctionType(); if (!member.isStaticMember()) { ownerType = ((FunctionType) ownerType).getPrototype(); } return ownerType; }
/** * Closure's goog.inherits adds a {@code superClass_} property to the * subclass, and a {@code constructor} property. */ @Override public void applySubclassRelationship(FunctionType parentCtor, FunctionType childCtor, SubclassType type) { super.applySubclassRelationship(parentCtor, childCtor, type); if (type == SubclassType.INHERITS) { childCtor.defineDeclaredProperty("superClass_", parentCtor.getPrototype(), childCtor.getSource()); childCtor.getPrototype().defineDeclaredProperty("constructor", // Notice that constructor functions do not need to be covariant // on the superclass. // So if G extends F, new G() and new F() can accept completely // different argument types, but G.prototype.constructor needs // to be covariant on F.prototype.constructor. // To get around this, we just turn off type-checking on arguments // and return types of G.prototype.constructor. childCtor.forgetParameterAndReturnTypes(), childCtor.getSource()); } }
/** * Given a constructor or an interface type and a property, finds the top-most superclass that has * the property defined (including this constructor). */ public final ObjectType getTopMostDefiningType(String propertyName) { checkState(isConstructor() || isInterface()); checkArgument(getInstanceType().hasProperty(propertyName)); FunctionType ctor = this; if (isInterface()) { return getInstanceType().getTopDefiningInterface(propertyName); } ObjectType topInstanceType = null; do { topInstanceType = ctor.getInstanceType(); ctor = ctor.getSuperClassConstructor(); } while (ctor != null && ctor.getPrototype().hasProperty(propertyName)); return topInstanceType; }
/** * Look for the super class implementation up the tree. */ private void recordSuperClassPrototypePropUse( FunctionType classType, String prop, Reference ref) { FunctionType superClass = classType.getSuperClassConstructor(); while (superClass != null) { if (superClass.getPrototype().hasOwnProperty(prop)) { graph.connect(getNamedContainingFunction(), ref, graph.defineNameIfNotExists( superClass.getReferenceName() + ".prototype." + prop, false)); return; } else { superClass = superClass.getSuperClassConstructor(); } } }
/** * Given a constructor or an interface type and a property, finds the * top-most superclass that has the property defined (including this * constructor). */ public ObjectType getTopMostDefiningType(String propertyName) { Preconditions.checkState(isConstructor() || isInterface()); Preconditions.checkArgument(getInstanceType().hasProperty(propertyName)); FunctionType ctor = this; if (isInterface()) { return getTopDefiningInterface(getInstanceType(), propertyName); } ObjectType topInstanceType = null; do { topInstanceType = ctor.getInstanceType(); ctor = ctor.getSuperClassConstructor(); } while (ctor != null && ctor.getPrototype().hasProperty(propertyName)); return topInstanceType; }
/** * Conservatively assumes that all subclass implementation of this property * might be called. */ private void recordSubclassPrototypePropUse( FunctionType classType, String prop, Reference ref) { if (classType.getPrototype().hasOwnProperty(prop)) { graph.connect(getNamedContainingFunction(), ref, graph.defineNameIfNotExists( classType.getReferenceName() + ".prototype." + prop, false)); } if (classType.getSubTypes() != null) { for (FunctionType subclass : classType.getSubTypes()) { recordSubclassPrototypePropUse(subclass, prop, ref); } } }
/** * @param constructor A constructor function defined by a call, which may be a mixin application. * The constructor implements at least one interface. If the constructor is missing some * properties of the inherited interfaces, this method declares these properties. */ private static void addMissingInterfaceProperties(JSType constructor) { if (constructor != null && constructor.isConstructor()) { FunctionType f = constructor.toMaybeFunctionType(); ObjectType proto = f.getPrototype(); for (ObjectType interf : f.getImplementedInterfaces()) { for (String pname : interf.getPropertyNames()) { if (!proto.hasProperty(pname)) { proto.defineDeclaredProperty(pname, interf.getPropertyType(pname), null); } } } } }