/** Null-safe version of toMaybeFunctionType(). */ @SuppressWarnings("AmbiguousMethodReference") public static FunctionType toMaybeFunctionType(JSType type) { return type == null ? null : type.toMaybeFunctionType(); }
/** Returns true if toMaybeFunctionType returns a non-null FunctionType. */ public final boolean isFunctionType() { return toMaybeFunctionType() != null; }
/** * Null-safe version of toMaybeFunctionType(). */ public static FunctionType toMaybeFunctionType(JSType type) { return type == null ? null : type.toMaybeFunctionType(); }
@Override public final FunctionType toMaybeFunctionType() { return referencedType.toMaybeFunctionType(); }
/** Returns true if toMaybeFunctionType returns a non-null FunctionType. */ public final boolean isFunctionType() { return toMaybeFunctionType() != null; }
/** Returns this object cast to FunctionType or throws an exception if it isn't a FunctionType. */ public FunctionType assertFunctionType() { FunctionType result = checkNotNull(toMaybeFunctionType(), "not a FunctionType: %s", this); return result; }
/** * Utility function for getting a function type from a var. */ static FunctionType getFunctionType(@Nullable TypedVar v) { JSType t = v == null ? null : v.getType(); ObjectType o = t == null ? null : t.dereference(); return JSType.toMaybeFunctionType(o); }
/** * Utility function for getting a function type from a var. */ static FunctionType getFunctionType(@Nullable TypedVar v) { JSType t = v == null ? null : v.getType(); ObjectType o = t == null ? null : t.dereference(); return JSType.toMaybeFunctionType(o); }
private boolean isEs6ClassConstructor(JSType type) { return type.isFunctionType() && type.toMaybeFunctionType().getSource() != null && type.toMaybeFunctionType().getSource().isClass(); }
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 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 JSType maybeGetInstanceTypeFromPrototypeRef(Node src) { JSType ownerType = getJSType(src.getFirstChild()); if (ownerType.isConstructor()) { FunctionType functionType = ownerType.toMaybeFunctionType(); 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); }
@Override public JSType caseUnknownType() { FunctionType funcTarget = JSType.toMaybeFunctionType(target); if (funcTarget != null && funcTarget.hasInstanceType()) { return funcTarget.getInstanceType(); } return getNativeType(UNKNOWN_TYPE); }
private JSType getTypeOfThisForConstructor(Node constructor) { checkArgument(constructor.isFunction(), constructor); // If typechecking has run, all function nodes should have a JSType. Nodes that were in a CAST // will also have the TYPE_BEFORE_CAST property, which is null for other nodes. final JSType constructorTypeBeforeCast = constructor.getJSTypeBeforeCast(); final JSType constructorType = constructorTypeBeforeCast != null ? constructorTypeBeforeCast : constructor.getJSType(); if (constructorType == null) { return null; // Type checking passes must not have run. } checkState(constructorType.isFunctionType()); return constructorType.toMaybeFunctionType().getTypeOfThis(); }
@Nullable private JSType getTypeOfThis(Node scopeRoot) { if (scopeRoot.isRoot() || scopeRoot.isScript()) { return castToObject(scopeRoot.getJSType()); } checkArgument(scopeRoot.isFunction(), scopeRoot); JSType nodeType = scopeRoot.getJSType(); if (nodeType != null && nodeType.isFunctionType()) { return nodeType.toMaybeFunctionType().getTypeOfThis(); } else { // Executed when the current scope has not been typechecked. return null; } }
Node createCall(Node callee, Node... args) { Node result = NodeUtil.newCallNode(callee, args); if (isAddingTypes()) { FunctionType calleeType = JSType.toMaybeFunctionType(callee.getJSType()); // TODO(sdh): this does not handle generic functions - we'd need to unify the argument types. // checkState(calleeType == null || !calleeType.hasAnyTemplateTypes(), calleeType); // TODO(bradfordcsmith): Consider throwing an exception if calleeType is null. JSType returnType = calleeType != null ? calleeType.getReturnType() : unknownType; result.setJSType(returnType); } return result; }
private ObjectType getThisTypeForCollectingProperties() { Node rootNode = scope.getRootNode(); if (rootNode.isFromExterns()) return null; JSType type = rootNode.getJSType(); if (type == null || !type.isFunctionType()) return null; FunctionType fnType = type.toMaybeFunctionType(); JSType fnThisType = fnType.getTypeOfThis(); return fnThisType.isUnknownType() ? null : fnThisType.toObjectType(); }
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(); }
/** Creates node that make a call to a context function. */ Node callContextMethod(Node sourceNode, String methodName, Node... args) { Node contextField = getContextField(sourceNode, methodName); Node callNode = IR.call(contextField, args).useSourceInfoFrom(sourceNode); if (shouldAddTypes) { callNode.setJSType( contextField.getJSType().isFunctionType() ? contextField.getJSType().toMaybeFunctionType().getReturnType() : unknownType); } return callNode; }