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 Details

    • 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(Property.Key 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 Set<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 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 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(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(List<ObjectType> extendedInterfaces)
    • getPropertyType

      public final JSType getPropertyType(Property.Key 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 @Nullable 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:
    • 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:
      IllegalStateException - if this function is not a constructor (see isConstructor()).
    • hasInstanceType

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

      public final JSType getTypeOfThis()
      Gets the type of this in this function.
      Overrides:
      getTypeOfThis in class ObjectType
    • getSource

      public final Node getSource()
      Gets the source node or null if this is an unknown function.
      Specified by:
      getSource in interface JSType.WithSourceRef
    • getGoogModuleId

      public @Nullable String getGoogModuleId()
      Description copied from interface: JSType.WithSourceRef
      The ID of the goog.module in which this type was declared.
      Specified by:
      getGoogModuleId in interface JSType.WithSourceRef
    • 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
    • isStructuralInterface

      public final boolean isStructuralInterface()
      Overrides:
      isStructuralInterface in class JSType
    • isAbstract

      public final boolean isAbstract()
    • getPropertyTypeMap

      public final Map<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 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(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
    • builder

      public static FunctionType.Builder builder(JSTypeRegistry registry)
    • toBuilder

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

      public final FunctionType getCanonicalRepresentation()