Search in sources :

Example 16 with FunctionType

use of com.google.javascript.jscomp.newtypes.FunctionType in project closure-compiler by google.

the class NewTypeInference method mayAdjustObjLitType.

/**
 * If the object literal is lended, or assigned to a prototype, find a better
 * type for it than the object-literal type.
 */
private JSType mayAdjustObjLitType(Node objLit, JSDocInfo jsdoc, TypeEnv env, JSType originalType) {
    Node parent = objLit.getParent();
    QualifiedName classqname = null;
    if (parent.isAssign() && NodeUtil.isPrototypeAssignment(parent.getFirstChild())) {
        classqname = QualifiedName.fromNode(parent.getFirstFirstChild());
    } else if (jsdoc != null && jsdoc.getLendsName() != null) {
        QualifiedName lendsQname = QualifiedName.fromQualifiedString(jsdoc.getLendsName());
        if (lendsQname.getRightmostName().equals("prototype")) {
            classqname = lendsQname.getAllButRightmost();
        }
    } else if (parent.isCall() && this.convention.getObjectLiteralCast(parent) != null) {
        ObjectLiteralCast cast = this.convention.getObjectLiteralCast(parent);
        if (cast.typeName != null) {
            classqname = QualifiedName.fromQualifiedString(cast.typeName);
        }
    }
    if (classqname != null) {
        JSType classType = envGetTypeOfQname(env, classqname);
        if (classType != null) {
            FunctionType clazz = classType.getFunTypeIfSingletonObj();
            JSType instance = clazz == null ? null : clazz.getInstanceTypeOfCtor();
            if (instance != null) {
                return instance.getPrototypeObject();
            }
        }
    }
    return originalType;
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) Node(com.google.javascript.rhino.Node) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName) FunctionType(com.google.javascript.jscomp.newtypes.FunctionType) DeclaredFunctionType(com.google.javascript.jscomp.newtypes.DeclaredFunctionType) ObjectLiteralCast(com.google.javascript.jscomp.CodingConvention.ObjectLiteralCast)

Example 17 with FunctionType

use of com.google.javascript.jscomp.newtypes.FunctionType in project closure-compiler by google.

the class NewTypeInference method analyzeInvocationBwd.

private EnvTypePair analyzeInvocationBwd(Node expr, TypeEnv outEnv, JSType requiredType) {
    checkArgument(expr.isNew() || expr.isCall() || expr.isTaggedTemplateLit());
    Node callee = expr.getFirstChild();
    EnvTypePair pair = analyzeExprBwd(callee, outEnv, commonTypes.topFunction());
    TypeEnv envAfterCallee = pair.env;
    FunctionType funType = pair.type.getFunType();
    if (funType == null) {
        return analyzeInvocationArgumentsBwd(expr, expr.getFirstChild(), envAfterCallee);
    } else if (funType.isLoose()) {
        return analyzeLooseCallNodeBwd(expr, envAfterCallee, requiredType);
    } else if ((expr.isCall() && funType.isSomeConstructorOrInterface()) || (expr.isNew() && !funType.isSomeConstructorOrInterface())) {
        return analyzeInvocationArgumentsBwd(expr, expr.getFirstChild(), envAfterCallee);
    } else if (funType.isTopFunction()) {
        return analyzeInvocationArgumentsBwd(expr, expr.getFirstChild(), envAfterCallee);
    }
    if (callee.isName() && !funType.isGeneric() && (expr.isCall() || expr.isTaggedTemplateLit())) {
        createDeferredCheckBwd(expr, requiredType);
    }
    int numArgs = NodeUtil.getInvocationArgsCount(expr);
    if (numArgs < funType.getMinArity() || numArgs > funType.getMaxArity()) {
        if (expr.isTaggedTemplateLit()) {
            return analyzeInvocationArgumentsBwd(expr.getLastChild(), null, envAfterCallee);
        } else {
            return analyzeInvocationArgumentsBwd(expr, expr.getFirstChild(), envAfterCallee);
        }
    }
    if (funType.isGeneric()) {
        Map<String, JSType> typeMap = calcTypeInstantiationBwd(expr, funType, envAfterCallee);
        funType = funType.instantiateGenerics(typeMap);
    }
    TypeEnv tmpEnv = envAfterCallee;
    // In bwd direction, analyze arguments in reverse
    Node target = expr.isTaggedTemplateLit() ? null : expr.getFirstChild();
    Node start = expr.isTaggedTemplateLit() ? expr.getLastChild().getLastChild() : expr.getLastChild();
    int i = numArgs;
    for (Node arg = start; arg != target; arg = arg.getPrevious()) {
        if (expr.isTaggedTemplateLit() && !arg.isTemplateLitSub()) {
            // with the formal types of the tag function, i needs to stay unchanged here.
            continue;
        }
        i--;
        JSType formalType = funType.getFormalType(i);
        // The type of a formal can be BOTTOM as the result of a join.
        // Don't use this as a requiredType.
        formalType = firstNonBottom(formalType, UNKNOWN);
        tmpEnv = analyzeExprBwd(arg, tmpEnv, formalType).env;
    // We don't need deferred checks for args in BWD
    }
    JSType retType = expr.isNew() ? funType.getThisType() : funType.getReturnType();
    return new EnvTypePair(tmpEnv, retType);
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) Node(com.google.javascript.rhino.Node) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) FunctionType(com.google.javascript.jscomp.newtypes.FunctionType) DeclaredFunctionType(com.google.javascript.jscomp.newtypes.DeclaredFunctionType) TypeEnv(com.google.javascript.jscomp.newtypes.TypeEnv)

Aggregations

DeclaredFunctionType (com.google.javascript.jscomp.newtypes.DeclaredFunctionType)17 FunctionType (com.google.javascript.jscomp.newtypes.FunctionType)17 JSType (com.google.javascript.jscomp.newtypes.JSType)16 Node (com.google.javascript.rhino.Node)13 DiGraphNode (com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)11 TypeEnv (com.google.javascript.jscomp.newtypes.TypeEnv)7 QualifiedName (com.google.javascript.jscomp.newtypes.QualifiedName)5 FunctionTypeBuilder (com.google.javascript.jscomp.newtypes.FunctionTypeBuilder)2 NominalType (com.google.javascript.jscomp.newtypes.NominalType)2 JSDocInfo (com.google.javascript.rhino.JSDocInfo)2 ArrayList (java.util.ArrayList)2 AssertionFunctionSpec (com.google.javascript.jscomp.CodingConvention.AssertionFunctionSpec)1 Bind (com.google.javascript.jscomp.CodingConvention.Bind)1 ObjectLiteralCast (com.google.javascript.jscomp.CodingConvention.ObjectLiteralCast)1 Declaration (com.google.javascript.jscomp.newtypes.Declaration)1 EnumType (com.google.javascript.jscomp.newtypes.EnumType)1 FunctionNamespace (com.google.javascript.jscomp.newtypes.FunctionNamespace)1 Namespace (com.google.javascript.jscomp.newtypes.Namespace)1 RawNominalType (com.google.javascript.jscomp.newtypes.RawNominalType)1