Class FunctionType

  • All Implemented Interfaces:
    JSType.WithSourceRef
    Direct Known Subclasses:
    NoObjectType

    public class FunctionType
    extends PrototypeObjectType
    implements JSType.WithSourceRef
    This derived type provides extended information about a function, including its return type and argument types.

    Note: the parameters list is the PARAM_LIST node that is the parent of the actual NAME node containing the parsed argument list (annotated with JSDOC_TYPE_PROP's for the compile-time type of each argument.

    • Method Detail

      • getConstructor

        public FunctionType getConstructor()
        Description copied from class: ObjectType
        Gets this object's constructor.
        Overrides:
        getConstructor in class PrototypeObjectType
        Returns:
        this object's constructor or null if it is a native object (constructed natively v.s. by instantiation of a function)
      • isInstanceType

        public final boolean isInstanceType()
        Description copied from class: JSType
        Whether this type is an Instance object of some constructor. Does not necessarily mean this is an InstanceObjectType.
        Overrides:
        isInstanceType in class JSType
      • isConstructor

        public final boolean isConstructor()
        Description copied from class: JSType
        Whether this type is a FunctionType that is a constructor or a named type that points to such a type.
        Overrides:
        isConstructor in class JSType
      • isInterface

        public final boolean isInterface()
        Description copied from class: JSType
        Whether this type is a FunctionType that is an interface or a named type that points to such a type.
        Overrides:
        isInterface in class JSType
      • isOrdinaryFunction

        public final boolean isOrdinaryFunction()
        Description copied from class: JSType
        Whether this type is a FunctionType that is an ordinary function (i.e. not a constructor, nominal interface, or record interface), or a named type that points to such a type.
        Overrides:
        isOrdinaryFunction in class JSType
      • makesStructs

        public final boolean makesStructs()
        When a class B inherits from A and A is annotated as a struct, then B automatically gets the annotation, if B's constructor is not explicitly annotated.
      • makesDicts

        public final boolean makesDicts()
        When a class B inherits from A and A is annotated as a dict, then B automatically gets the annotation, if B's constructor is not explicitly annotated.
      • setStruct

        public final void setStruct()
      • setDict

        public final void setDict()
      • setExplicitUnrestricted

        public final void setExplicitUnrestricted()
      • toMaybeFunctionType

        public FunctionType toMaybeFunctionType()
        Description copied from class: JSType
        Downcasts this to a FunctionType, or returns null if this is not a function.

        For the purposes of this function, we define a MaybeFunctionType as any type in the sub-lattice { x | LEAST_FUNCTION_TYPE <= x <= GREATEST_FUNCTION_TYPE } This definition excludes bottom types like NoType and NoObjectType.

        This definition is somewhat arbitrary and axiomatic, but this is the definition that makes the most sense for the most callers.

        Overrides:
        toMaybeFunctionType in class JSType
      • canBeCalled

        public final boolean canBeCalled()
        Description copied from class: JSType
        This predicate is used to test whether a given type can be used as the 'function' in a function call.
        Overrides:
        canBeCalled in class JSType
        Returns:
        true if this type might be callable.
      • hasImplementedInterfaces

        public final boolean hasImplementedInterfaces()
      • getParameters

        public final com.google.common.collect.ImmutableList<FunctionType.Parameter> getParameters()
      • getMinArity

        public final int getMinArity()
        Gets the minimum number of arguments that this function requires.
      • getMaxArity

        public final int getMaxArity()
        Gets the maximum number of arguments that this function requires, or Integer.MAX_VALUE if this is a variable argument function.
      • getReturnType

        public final JSType getReturnType()
      • isReturnTypeInferred

        public final boolean isReturnTypeInferred()
      • getSlot

        public final Property getSlot​(java.lang.String name)
        Description copied from class: ObjectType
        Default getSlot implementation. This gets overridden by FunctionType for lazily-resolved prototypes.
        Overrides:
        getSlot in class ObjectType
      • getOwnPropertyNames

        public final java.util.Set<java.lang.String> getOwnPropertyNames()
        Includes the prototype iff someone has created it. We do not want to expose the prototype for ordinary functions.
        Overrides:
        getOwnPropertyNames in class ObjectType
      • getPrototypeProperty

        public final ObjectType getPrototypeProperty()
      • getPrototype

        public final ObjectType getPrototype()
        Gets the prototype property of this function type. This is equivalent to (ObjectType) getPropertyType("prototype").
      • setPrototypeBasedOn

        public final void setPrototypeBasedOn​(ObjectType baseType)
        Sets the prototype, creating the prototype object from the given base type.
        Parameters:
        baseType - The base type.
      • getAllImplementedInterfaces

        public final java.lang.Iterable<ObjectType> getAllImplementedInterfaces()
        Returns all interfaces implemented by a class or its superclass and any superclasses for any of those interfaces. If this is called before all types are resolved, it may return an incomplete set.
      • getAncestorInterfaces

        public final java.util.Collection<ObjectType> getAncestorInterfaces()
      • getImplementedInterfaces

        public final com.google.common.collect.ImmutableList<ObjectType> getImplementedInterfaces()
        Returns interfaces implemented directly by a class or its superclass.
      • getOwnImplementedInterfaces

        public final com.google.common.collect.ImmutableList<ObjectType> getOwnImplementedInterfaces()
        Returns interfaces directly implemented by the class.
      • setImplementedInterfaces

        public final void setImplementedInterfaces​(java.util.List<ObjectType> implementedInterfaces)
      • getExtendedInterfaces

        public final com.google.common.collect.ImmutableList<ObjectType> getExtendedInterfaces()
        Returns interfaces directly extended by an interface
      • getExtendedInterfacesCount

        public final int getExtendedInterfacesCount()
        Returns the number of interfaces directly extended by an interface
      • setExtendedInterfaces

        public final void setExtendedInterfaces​(java.util.List<ObjectType> extendedInterfaces)
      • getPropertyType

        public final JSType getPropertyType​(java.lang.String name)
        Description copied from class: ObjectType
        Gets the property type of the property whose name is given. If the underlying object does not have this property, the Unknown type is returned to indicate that no information is available on this property. This gets overridden by FunctionType for lazily-resolved call() and bind() functions.
        Overrides:
        getPropertyType in class ObjectType
        Returns:
        the property's type or UnknownType. This method never returns null.
      • getBindReturnType

        public final FunctionType getBindReturnType​(int argsToBind)
        Get the return value of calling "bind" on this function with the specified number of arguments.

        If -1 is passed, then we will return a result that accepts any parameters.

      • getSuperClassConstructor

        public final FunctionType getSuperClassConstructor()
        Given a constructor or an interface type, get its superclass constructor or null if none exists.
        Overrides:
        getSuperClassConstructor in class ObjectType
      • hasEqualCallType

        public final boolean hasEqualCallType​(FunctionType that)
      • visit

        public <T> T visit​(Visitor<T> visitor)
        Description copied from class: JSType
        Visit this type with the given visitor.
        Overrides:
        visit in class ObjectType
        Returns:
        the value returned by the visitor
        See Also:
        Visitor
      • getInstanceType

        public final ObjectType getInstanceType()
        Gets the type of instance of this function. May return null if the `this` type can not be converted to "ObjectType" (see JSType#toObjectType).
        Throws:
        java.lang.IllegalStateException - if this function is not a constructor (see isConstructor()).
      • hasInstanceType

        public final boolean hasInstanceType()
        Returns whether this function type has an instance type.
      • setSource

        public final void setSource​(Node source)
        Sets the source node.
      • clearCachedValues

        public final void clearCachedValues()
        Description copied from class: ObjectType
        Clear cached values. Should be called before making changes to a prototype that may have been changed since creation.
        Overrides:
        clearCachedValues in class ObjectType
      • hasCachedValues

        public final boolean hasCachedValues()
        Description copied from class: ObjectType
        Returns true if any cached values have been set for this type. If true, then the prototype chain should not be changed, as it might invalidate the cached values.
        Overrides:
        hasCachedValues in class ObjectType
      • hasProperties

        public final boolean hasProperties()
      • setImplicitMatch

        public final void setImplicitMatch​(boolean flag)
        sets the current interface type to support structural interface matching (abbr. SMI)
        Parameters:
        flag - indicates whether or not it should support SMI
      • isAbstract

        public final boolean isAbstract()
      • getPropertyTypeMap

        public final java.util.Map<java.lang.String,​JSType> getPropertyTypeMap()
        get the map of properties to types covered in a function type
        Overrides:
        getPropertyTypeMap in class ObjectType
        Returns:
        a Map that maps the property's name to the property's type
      • checkExtendsLoop

        public final java.util.List<FunctionType> checkExtendsLoop()
        check if there is a loop in the type extends chain
        Returns:
        an array of all functions in the loop chain if a loop exists, otherwise returns null
      • acceptsArguments

        public final boolean acceptsArguments​(java.util.List<? extends JSType> argumentTypes)
      • forgetParameterAndReturnTypes

        public final FunctionType forgetParameterAndReturnTypes()
        Create a new constructor with the parameters and return type stripped.
      • getConstructorOnlyTemplateParameters

        public final com.google.common.collect.ImmutableList<TemplateType> getConstructorOnlyTemplateParameters()
        Returns a list of template types present on the constructor but not on the instance.
      • isAmbiguousConstructor

        public final boolean isAmbiguousConstructor()
        Returns true if the constructor does not come from a literal class or function in the AST, or if it extends such an ambiguous constructor
      • getClosurePrimitive

        public final ClosurePrimitive getClosurePrimitive()
        Returns the @closurePrimitive identifier associated with this function
      • toBuilder

        public FunctionType.Builder toBuilder()
        Copies all the information from another function type.
      • getCanonicalRepresentation

        public final FunctionType getCanonicalRepresentation()