Search in sources :

Example 76 with JSType

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

the class NewTypeInference method analyzeInvocationArgumentsFwd.

private TypeEnv analyzeInvocationArgumentsFwd(Node n, Iterable<Node> args, FunctionType funType, List<JSType> argTypesForDeferredCheck, TypeEnv inEnv) {
    checkState(NodeUtil.isCallOrNew(n) || n.isTemplateLit());
    TypeEnv env = inEnv;
    int i = n.isTemplateLit() ? 1 : 0;
    for (Node arg : args) {
        JSType formalType = funType.getFormalType(i);
        checkState(!formalType.isBottom());
        EnvTypePair pair = analyzeExprFwd(arg, env, formalType);
        if (NodeUtil.isUnannotatedCallback(arg) && formalType.getFunType() != null) {
            i++;
            // No deferred check needed.
            argTypesForDeferredCheck.add(null);
            continue;
        }
        JSType argTypeForDeferredCheck = pair.type;
        // Allow passing undefined for an optional argument.
        if (funType.isOptionalArg(i) && pair.type.equals(UNDEFINED)) {
            // No deferred check needed.
            argTypeForDeferredCheck = null;
        } else if (!pair.type.isSubtypeOf(formalType)) {
            warnAboutInvalidArgument(n, arg, i, formalType, pair.type);
            // No deferred check needed.
            argTypeForDeferredCheck = null;
        } else {
            registerImplicitUses(arg, pair.type, formalType);
        }
        argTypesForDeferredCheck.add(argTypeForDeferredCheck);
        env = pair.env;
        i++;
    }
    return env;
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) Node(com.google.javascript.rhino.Node) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) TypeEnv(com.google.javascript.jscomp.newtypes.TypeEnv)

Example 77 with JSType

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

the class NewTypeInference method analyzeLtGtFwd.

private EnvTypePair analyzeLtGtFwd(Node expr, TypeEnv inEnv) {
    Node lhs = expr.getFirstChild();
    Node rhs = expr.getLastChild();
    EnvTypePair lhsPair = analyzeExprFwd(lhs, inEnv);
    EnvTypePair rhsPair = analyzeExprFwd(rhs, lhsPair.env);
    // The type of either side can be specialized based on the other side
    if (lhsPair.type.isScalar() && !rhsPair.type.isScalar()) {
        rhsPair = analyzeExprFwd(rhs, lhsPair.env, lhsPair.type);
    } else if (rhsPair.type.isScalar()) {
        lhsPair = analyzeExprFwd(lhs, inEnv, rhsPair.type);
        rhsPair = analyzeExprFwd(rhs, lhsPair.env, rhsPair.type);
    }
    JSType lhsType = lhsPair.type;
    JSType rhsType = rhsPair.type;
    if (!lhsType.isSubtypeOf(rhsType) && !rhsType.isSubtypeOf(lhsType) && !(lhsType.isBoolean() && rhsType.isBoolean())) {
        warnInvalidOperand(expr, expr.getToken(), "matching types", lhsType + ", " + rhsType);
    }
    rhsPair.type = BOOLEAN;
    return rhsPair;
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) Node(com.google.javascript.rhino.Node) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)

Example 78 with JSType

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

the class NewTypeInference method analyzePropAccessBwd.

private EnvTypePair analyzePropAccessBwd(Node receiver, String pname, TypeEnv outEnv, JSType requiredType) {
    Node propAccessNode = receiver.getParent();
    QualifiedName qname = new QualifiedName(pname);
    JSType reqObjType = pickReqObjType(propAccessNode);
    if (!NodeUtil.isPropertyTest(compiler, propAccessNode)) {
        reqObjType = reqObjType.withProperty(qname, requiredType);
    }
    EnvTypePair pair = analyzeExprBwd(receiver, outEnv, reqObjType);
    JSType receiverType = pair.type;
    JSType propAccessType = receiverType.mayHaveProp(qname) ? receiverType.getProp(qname) : requiredType;
    pair.type = propAccessType;
    return pair;
}
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)

Example 79 with JSType

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

the class NewTypeInference method getTypeEnvFromDeclaredTypes.

private TypeEnv getTypeEnvFromDeclaredTypes() {
    TypeEnv env = new TypeEnv();
    Set<String> varNames = this.currentScope.getOuterVars();
    Set<String> locals = this.currentScope.getLocals();
    varNames.addAll(locals);
    varNames.addAll(this.currentScope.getExterns());
    if (this.currentScope.hasThis()) {
        varNames.add(THIS_ID);
    }
    if (this.currentScope.isFunction()) {
        Node fn = this.currentScope.getRoot();
        if (!this.currentScope.hasThis() && // a function.
        NodeUtil.containsType(fn.getLastChild(), Token.SUPER, NodeUtil.MATCH_NOT_FUNCTION)) {
            // This function is a static method on some class. To do lookups of the
            // class name, we add the root of the qualified name to the environment.
            Node funNameNode = NodeUtil.getBestLValue(fn);
            Node qnameRoot = NodeUtil.getRootOfQualifiedName(funNameNode);
            checkState(qnameRoot.isName());
            varNames.add(qnameRoot.getString());
        }
        if (this.currentScope.getName() != null) {
            varNames.add(this.currentScope.getName());
        }
        varNames.addAll(this.currentScope.getFormals());
        // In the rare case when there is a local variable named "arguments",
        // this entry will be overwritten in the foreach loop below.
        JSType argumentsType;
        DeclaredFunctionType dft = this.currentScope.getDeclaredTypeForOwnBody();
        if (dft.getOptionalArity() == 0 && dft.hasRestFormals()) {
            argumentsType = dft.getRestFormalsType();
        } else {
            argumentsType = UNKNOWN;
        }
        env = envPutType(env, "arguments", commonTypes.getArgumentsArrayType(argumentsType));
    }
    for (String varName : varNames) {
        if (!this.currentScope.isLocalFunDef(varName)) {
            JSType declType = this.currentScope.getDeclaredTypeOf(varName);
            if (declType == null) {
                declType = UNKNOWN;
            } else if (areTypeVariablesUnknown) {
                declType = declType.substituteGenericsWithUnknown();
            }
            env = envPutType(env, varName, declType);
        }
    }
    for (String fnName : this.currentScope.getLocalFunDefs()) {
        env = envPutType(env, fnName, getSummaryOfLocalFunDef(fnName));
    }
    return env;
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) DeclaredFunctionType(com.google.javascript.jscomp.newtypes.DeclaredFunctionType) Node(com.google.javascript.rhino.Node) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) TypeEnv(com.google.javascript.jscomp.newtypes.TypeEnv)

Example 80 with JSType

use of com.google.javascript.jscomp.newtypes.JSType 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)

Aggregations

JSType (com.google.javascript.jscomp.newtypes.JSType)86 Node (com.google.javascript.rhino.Node)60 DiGraphNode (com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)54 TypeEnv (com.google.javascript.jscomp.newtypes.TypeEnv)28 DeclaredFunctionType (com.google.javascript.jscomp.newtypes.DeclaredFunctionType)20 QualifiedName (com.google.javascript.jscomp.newtypes.QualifiedName)18 FunctionType (com.google.javascript.jscomp.newtypes.FunctionType)16 Declaration (com.google.javascript.jscomp.newtypes.Declaration)5 FunctionTypeBuilder (com.google.javascript.jscomp.newtypes.FunctionTypeBuilder)5 FunctionNamespace (com.google.javascript.jscomp.newtypes.FunctionNamespace)4 Namespace (com.google.javascript.jscomp.newtypes.Namespace)4 ArrayList (java.util.ArrayList)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 TypeI (com.google.javascript.rhino.TypeI)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 NamespaceLit (com.google.javascript.jscomp.newtypes.NamespaceLit)2 NominalType (com.google.javascript.jscomp.newtypes.NominalType)2 RawNominalType (com.google.javascript.jscomp.newtypes.RawNominalType)2 JSDocInfo (com.google.javascript.rhino.JSDocInfo)2