final class FunctionTypeBuilder
extends java.lang.Object
this
for ease of use.
Right now, this mostly uses JSDocInfo to infer type information about
functions. In the long term, developers should extend it to use other
signals by overloading the various "inferXXX" methods. For example, we
might want to use goog.inherits
calls as a signal for inheritance, or
return
statements as a signal for return type.
NOTE(nicksantos): Organizationally, this feels like it should be in Rhino.
But it depends on some coding convention stuff that's really part
of JSCompiler.Modifier and Type | Class and Description |
---|---|
(package private) static class |
FunctionTypeBuilder.AstFunctionContents |
private class |
FunctionTypeBuilder.ExtendedTypeValidator |
(package private) static interface |
FunctionTypeBuilder.FunctionContents
Holds data dynamically inferred about functions.
|
private class |
FunctionTypeBuilder.ImplementedTypeValidator |
(package private) static class |
FunctionTypeBuilder.UnknownFunctionContents |
Constructor and Description |
---|
FunctionTypeBuilder(java.lang.String fnName,
AbstractCompiler compiler,
Node errorRoot,
TypedScope scope) |
Modifier and Type | Method and Description |
---|---|
private boolean |
addParameter(FunctionParamBuilder builder,
JSType paramType,
boolean warnedAboutArgList,
boolean isOptional,
boolean isVarArgs)
Add a parameter to the param list.
|
(package private) FunctionType |
buildAndRegister()
Builds the function type, and puts it in the registry.
|
(package private) java.lang.String |
formatFnName()
Format the function name for use in warnings.
|
private FunctionType |
getOrCreateConstructor()
Returns a constructor function either by returning it from the
registry if it exists or creating and registering a new type.
|
private TypedScope |
getScopeDeclaredIn()
The scope that we should declare this function in, if it needs
to be declared in a scope.
|
private static boolean |
hasMoreTagsToResolve(ObjectType objectType)
Check whether a type is resolvable in the future
If this has a supertype that hasn't been resolved yet, then we can assume
this type will be OK once the super type resolves.
|
(package private) FunctionTypeBuilder |
inferFromOverriddenFunction(FunctionType oldType,
Node paramsParent)
Infer the parameter and return types of a function from
the parameter and return types of the function it is overriding.
|
(package private) FunctionTypeBuilder |
inferInheritance(JSDocInfo info)
Infer the role of the function (whether it's a constructor or interface)
and what it inherits from in JSDocInfo.
|
(package private) FunctionTypeBuilder |
inferParameterTypes(JSDocInfo info)
Infer the parameter types from the doc info alone.
|
(package private) FunctionTypeBuilder |
inferParameterTypes(Node argsParent,
JSDocInfo info)
Infer the parameter types from the list of argument names and
the doc info.
|
(package private) FunctionTypeBuilder |
inferReturnType(JSDocInfo info,
boolean fromInlineDoc)
Infer the return type from JSDocInfo.
|
(package private) FunctionTypeBuilder |
inferTemplateTypeName(JSDocInfo info,
JSType ownerType)
Infer the template type from the doc info.
|
(package private) FunctionTypeBuilder |
inferThisType(JSDocInfo info)
Infers the type of
this . |
(package private) FunctionTypeBuilder |
inferThisType(JSDocInfo info,
JSType type)
Infers the type of
this . |
(package private) static boolean |
isFunctionTypeDeclaration(JSDocInfo info)
Determines whether the given JsDoc info declares a function type.
|
private boolean |
isOptionalParameter(Node param,
JSDocInfo info) |
private boolean |
isVarArgsParameter(Node param,
JSDocInfo info)
Determine whether this is a var args parameter.
|
private void |
maybeSetBaseType(FunctionType fnType) |
private void |
reportError(DiagnosticType error,
java.lang.String... args) |
private void |
reportWarning(DiagnosticType warning,
java.lang.String... args) |
(package private) FunctionTypeBuilder |
setContents(FunctionTypeBuilder.FunctionContents contents)
Sets the contents of this function.
|
private final java.lang.String fnName
private final AbstractCompiler compiler
private final CodingConvention codingConvention
private final JSTypeRegistry typeRegistry
private final Node errorRoot
private final TypedScope scope
private FunctionTypeBuilder.FunctionContents contents
private JSType returnType
private boolean returnTypeInferred
private java.util.List<ObjectType> implementedInterfaces
private java.util.List<ObjectType> extendedInterfaces
private ObjectType baseType
private JSType thisType
private boolean isConstructor
private boolean makesStructs
private boolean makesDicts
private boolean isInterface
private Node parametersNode
private com.google.common.collect.ImmutableList<TemplateType> templateTypeNames
private com.google.common.collect.ImmutableList<TemplateType> classTemplateTypeNames
static final DiagnosticType EXTENDS_WITHOUT_TYPEDEF
static final DiagnosticType EXTENDS_NON_OBJECT
static final DiagnosticType RESOLVED_TAG_EMPTY
static final DiagnosticType IMPLEMENTS_WITHOUT_CONSTRUCTOR
static final DiagnosticType CONSTRUCTOR_REQUIRED
static final DiagnosticType VAR_ARGS_MUST_BE_LAST
static final DiagnosticType OPTIONAL_ARG_AT_END
static final DiagnosticType INEXISTENT_PARAM
static final DiagnosticType TYPE_REDEFINITION
static final DiagnosticType TEMPLATE_TYPE_DUPLICATED
static final DiagnosticType TEMPLATE_TYPE_EXPECTED
static final DiagnosticType THIS_TYPE_NON_OBJECT
static final DiagnosticType SAME_INTERFACE_MULTIPLE_IMPLEMENTS
FunctionTypeBuilder(java.lang.String fnName, AbstractCompiler compiler, Node errorRoot, TypedScope scope)
fnName
- The function name.compiler
- The compiler.errorRoot
- The node to associate with any warning generated by
this builder.scope
- The syntactic scope.java.lang.String formatFnName()
FunctionTypeBuilder setContents(@Nullable FunctionTypeBuilder.FunctionContents contents)
FunctionTypeBuilder inferFromOverriddenFunction(@Nullable FunctionType oldType, @Nullable Node paramsParent)
oldType
- The function being overridden. Does nothing if this is null.paramsParent
- The LP node of the function that we're assigning to.
If null, that just means we're not initializing this to a function
literal.FunctionTypeBuilder inferReturnType(@Nullable JSDocInfo info, boolean fromInlineDoc)
fromInlineDoc
- Indicates whether return type is inferred from inline
doc attached to function nameFunctionTypeBuilder inferInheritance(@Nullable JSDocInfo info)
FunctionTypeBuilder inferThisType(JSDocInfo info, JSType type)
this
.type
- The type of this if the info is missing.FunctionTypeBuilder inferThisType(JSDocInfo info)
this
.info
- The JSDocInfo for this function.FunctionTypeBuilder inferParameterTypes(JSDocInfo info)
FunctionTypeBuilder inferParameterTypes(@Nullable Node argsParent, @Nullable JSDocInfo info)
private boolean isOptionalParameter(Node param, @Nullable JSDocInfo info)
private boolean isVarArgsParameter(Node param, @Nullable JSDocInfo info)
FunctionTypeBuilder inferTemplateTypeName(@Nullable JSDocInfo info, JSType ownerType)
private boolean addParameter(FunctionParamBuilder builder, JSType paramType, boolean warnedAboutArgList, boolean isOptional, boolean isVarArgs)
builder
- A builder.paramType
- The parameter type.warnedAboutArgList
- Whether we've already warned about arg ordering
issues (like if optional args appeared before required ones).isOptional
- Is this an optional parameter?isVarArgs
- Is this a var args parameter?FunctionType buildAndRegister()
private void maybeSetBaseType(FunctionType fnType)
private FunctionType getOrCreateConstructor()
JSTypeRegistry
. We a) want to
make sure that the type information specified in the externs file
matches what is in the registry and b) annotate the externs with
the JSType
from the registry so that there are not two
separate JSType objects for one type.private void reportWarning(DiagnosticType warning, java.lang.String... args)
private void reportError(DiagnosticType error, java.lang.String... args)
static boolean isFunctionTypeDeclaration(JSDocInfo info)
private TypedScope getScopeDeclaredIn()
private static boolean hasMoreTagsToResolve(ObjectType objectType)
objectType
-