Search in sources :

Example 6 with Declaration

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

the class NTIScope method getDeclaration.

public Declaration getDeclaration(String name, boolean includeTypes) {
    checkArgument(!name.contains("."));
    Declaration decl = getLocalDeclaration(name, includeTypes);
    if (decl != null) {
        return decl;
    }
    return parent == null ? null : parent.getDeclaration(name, includeTypes);
}
Also used : Declaration(com.google.javascript.jscomp.newtypes.Declaration)

Example 7 with Declaration

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

the class NTIScope method getNamespace.

Namespace getNamespace(String name) {
    checkArgument(!name.contains("."));
    Declaration decl = getDeclaration(name, false);
    return decl == null ? null : decl.getNamespace();
}
Also used : Declaration(com.google.javascript.jscomp.newtypes.Declaration)

Example 8 with Declaration

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

the class SimpleInference method inferPropAccess.

private JSType inferPropAccess(Node recv, String pname, NTIScope scope) {
    if (recv.isGetProp() && recv.getLastChild().getString().equals("prototype")) {
        return inferPrototypeProperty(recv.getFirstChild(), pname, scope);
    }
    QualifiedName propQname = new QualifiedName(pname);
    JSType recvType = null;
    if (recv.isQualifiedName()) {
        QualifiedName recvQname = QualifiedName.fromNode(recv);
        Declaration decl = scope.getDeclaration(recvQname, false);
        if (decl != null) {
            EnumType et = decl.getEnum();
            if (et != null && et.enumLiteralHasKey(pname)) {
                return et.getPropType();
            }
            Namespace ns = decl.getNamespace();
            if (ns != null) {
                return inferDeclaration(ns.getDeclaration(propQname));
            }
            recvType = decl.getTypeOfSimpleDecl();
        }
    }
    if (recvType == null) {
        recvType = inferExprRecur(recv, scope);
    }
    if (recvType == null) {
        return null;
    }
    if (recvType.isScalar()) {
        recvType = recvType.autobox();
    }
    FunctionType ft = recvType.getFunTypeIfSingletonObj();
    if (ft != null && pname.equals("call")) {
        return this.commonTypes.fromFunctionType(ft.transformByCallProperty());
    } else if (ft != null && pname.equals("apply")) {
        return this.commonTypes.fromFunctionType(ft.transformByApplyProperty());
    }
    if (recvType.mayHaveProp(propQname)) {
        return recvType.getProp(propQname);
    }
    return null;
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) EnumType(com.google.javascript.jscomp.newtypes.EnumType) QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName) FunctionType(com.google.javascript.jscomp.newtypes.FunctionType) DeclaredFunctionType(com.google.javascript.jscomp.newtypes.DeclaredFunctionType) Declaration(com.google.javascript.jscomp.newtypes.Declaration) FunctionNamespace(com.google.javascript.jscomp.newtypes.FunctionNamespace) Namespace(com.google.javascript.jscomp.newtypes.Namespace)

Example 9 with Declaration

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

the class SimpleInference method inferInstantiatedCallee.

FunctionType inferInstantiatedCallee(Node call, FunctionType calleeType, boolean bailForUntypedArguments, NTIScope scope) {
    Node callee = call.getFirstChild();
    Preconditions.checkArgument(calleeType.isGeneric(), "Expected generic type for %s but found %s", callee, calleeType);
    // The receiver type is useful for inference when calleeType has a @this annotation
    // that includes a type variable.
    JSType recvType = null;
    if (callee.isGetProp() && callee.getFirstChild().isQualifiedName()) {
        Node recv = callee.getFirstChild();
        QualifiedName recvQname = QualifiedName.fromNode(recv);
        Declaration decl = scope.getDeclaration(recvQname, false);
        if (decl != null) {
            recvType = decl.getTypeOfSimpleDecl();
        }
    }
    ImmutableList.Builder<JSType> argTypes = ImmutableList.builder();
    for (Node argNode = call.getSecondChild(); argNode != null; argNode = argNode.getNext()) {
        JSType t = inferExprRecur(argNode, scope);
        if (t == null) {
            if (bailForUntypedArguments && !argNode.isFunction()) {
                // Used for @const inference, where we want to be strict.
                return null;
            } else {
                // Used when inferring a signature for unannotated callbacks passed to generic
                // functions. Whatever type variable we can't infer will become unknown.
                t = this.commonTypes.BOTTOM;
            }
        }
        argTypes.add(t);
    }
    return calleeType.instantiateGenericsFromArgumentTypes(recvType, argTypes.build());
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) ImmutableList(com.google.common.collect.ImmutableList) Node(com.google.javascript.rhino.Node) QualifiedName(com.google.javascript.jscomp.newtypes.QualifiedName) Declaration(com.google.javascript.jscomp.newtypes.Declaration)

Example 10 with Declaration

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

the class GlobalTypeInfoCollector method process.

@Override
public void process(Node externs, Node root) {
    checkNotNull(warnings, "Cannot rerun GlobalTypeInfoCollector.process");
    checkArgument(externs == null || externs.isRoot());
    checkArgument(root.isRoot(), "Root must be ROOT, but is %s", root.getToken());
    this.compiler.setMostRecentTypechecker(MostRecentTypechecker.NTI);
    NTIScope globalScope = new NTIScope(root, null, ImmutableList.<String>of(), getCommonTypes());
    globalScope.addUnknownTypeNames(this.globalTypeInfo.getUnknownTypeNames());
    this.globalTypeInfo.setGlobalScope(globalScope);
    this.scopes.add(globalScope);
    // Processing of a scope is split into many separate phases, and it's not
    // straightforward to remember which phase does what.
    // (1) Find names of classes, interfaces, typedefs, enums, and namespaces
    // defined in the global scope.
    CollectNamedTypes rootCnt = new CollectNamedTypes(globalScope);
    NodeTraversal.traverseEs6(this.compiler, externs, this.orderedExterns);
    rootCnt.collectNamedTypesInExterns();
    defineObjectAndFunctionIfMissing();
    NodeTraversal.traverseEs6(compiler, root, rootCnt);
    // (2) Determine the type represented by each typedef and each enum
    globalScope.resolveTypedefs(getTypeParser());
    globalScope.resolveEnums(getTypeParser());
    // (3) Repeat steps 1-2 for all the other scopes (outer-to-inner)
    for (int i = 1; i < this.scopes.size(); i++) {
        NTIScope s = this.scopes.get(i);
        CollectNamedTypes cnt = new CollectNamedTypes(s);
        NodeTraversal.traverseEs6(compiler, s.getBody(), cnt);
        s.resolveTypedefs(getTypeParser());
        s.resolveEnums(getTypeParser());
        if (NewTypeInference.measureMem) {
            NewTypeInference.updatePeakMem();
        }
    }
    // (4) The bulk of the global-scope processing happens here:
    // - Create scopes for functions
    // - Declare properties on types
    ProcessScope rootPs = new ProcessScope(globalScope);
    if (externs != null) {
        NodeTraversal.traverseEs6(compiler, externs, rootPs);
    }
    NodeTraversal.traverseEs6(compiler, root, rootPs);
    // (5) Things that must happen after the traversal of the scope
    rootPs.finishProcessingScope();
    // (6) Repeat steps 4-5 for all the other scopes (outer-to-inner)
    for (int i = 1; i < this.scopes.size(); i++) {
        NTIScope s = this.scopes.get(i);
        ProcessScope ps = new ProcessScope(s);
        NodeTraversal.traverseEs6(compiler, s.getBody(), ps);
        ps.finishProcessingScope();
        if (NewTypeInference.measureMem) {
            NewTypeInference.updatePeakMem();
        }
    }
    // (7) Adjust types of properties based on inheritance information.
    // Report errors in the inheritance chain. Do Window last.
    Collection<RawNominalType> windows = new ArrayList<>();
    for (Map.Entry<Node, RawNominalType> entry : nominaltypesByNode.entrySet()) {
        RawNominalType rawType = entry.getValue();
        if (this.window != null && rawType.hasAncestorClass(this.window)) {
            windows.add(rawType);
            continue;
        }
        checkAndFreezeNominalType(rawType);
    }
    JSType globalThisType = null;
    if (this.window != null) {
        // Copy properties from window to Window.prototype, because sometimes
        // people pass window around rather than using it directly.
        Namespace winNs = globalScope.getNamespace(WINDOW_INSTANCE);
        if (winNs != null) {
            winNs.copyWindowProperties(getCommonTypes(), this.window);
        }
        for (RawNominalType rawType : windows) {
            checkAndFreezeNominalType(rawType);
        }
        if (winNs != null) {
            ((NamespaceLit) winNs).setWindowType(this.window.getAsNominalType());
            // Type the global THIS as window
            globalThisType = winNs.toJSType();
        }
    }
    if (globalThisType == null) {
        // Type the global THIS as a loose object
        globalThisType = getCommonTypes().getTopObject().withLoose();
    }
    getCommonTypes().setGlobalThis(globalThisType);
    globalScope.setDeclaredType((new FunctionTypeBuilder(getCommonTypes())).addReceiverType(globalThisType).buildDeclaration());
    this.globalTypeInfo.setRawNominalTypes(nominaltypesByNode.values());
    nominaltypesByNode = null;
    propertyDefs = null;
    for (NTIScope s : this.scopes) {
        s.freezeScope();
    }
    this.simpleInference.setScopesAreFrozen();
    // Traverse the externs and annotate them with types.
    // Only works for the top level, not inside function bodies.
    NodeTraversal.traverseEs6(this.compiler, externs, new NodeTraversal.AbstractShallowCallback() {

        @Override
        public void visit(NodeTraversal t, Node n, Node parent) {
            if (n.isQualifiedName()) {
                Declaration d = getGlobalScope().getDeclaration(QualifiedName.fromNode(n), false);
                JSType type = simpleInferDeclaration(d);
                if (type == null) {
                    type = simpleInferExpr(n, getGlobalScope());
                }
                // Type-based passes expect the externs to be annotated, so use ? when type is null.
                n.setTypeI(type != null ? type : getCommonTypes().UNKNOWN);
            }
        }
    });
    Map<Node, String> unknownTypes = getTypeParser().getUnknownTypesMap();
    for (Map.Entry<Node, String> unknownTypeEntry : unknownTypes.entrySet()) {
        this.warnings.add(JSError.make(unknownTypeEntry.getKey(), UNRECOGNIZED_TYPE_NAME, unknownTypeEntry.getValue()));
    }
    // package, so we collect its warnings here.
    for (JSError warning : getTypeParser().getWarnings()) {
        this.warnings.add(warning);
    }
    this.warnings = null;
    this.funNameGen = null;
    reorderScopesForNTI();
    this.compiler.setExternProperties(ImmutableSet.copyOf(getExternPropertyNames()));
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) Node(com.google.javascript.rhino.Node) ArrayList(java.util.ArrayList) NamespaceLit(com.google.javascript.jscomp.newtypes.NamespaceLit) AbstractShallowCallback(com.google.javascript.jscomp.NodeTraversal.AbstractShallowCallback) Namespace(com.google.javascript.jscomp.newtypes.Namespace) FunctionNamespace(com.google.javascript.jscomp.newtypes.FunctionNamespace) RawNominalType(com.google.javascript.jscomp.newtypes.RawNominalType) Declaration(com.google.javascript.jscomp.newtypes.Declaration) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FunctionTypeBuilder(com.google.javascript.jscomp.newtypes.FunctionTypeBuilder)

Aggregations

Declaration (com.google.javascript.jscomp.newtypes.Declaration)13 FunctionNamespace (com.google.javascript.jscomp.newtypes.FunctionNamespace)6 Namespace (com.google.javascript.jscomp.newtypes.Namespace)6 JSType (com.google.javascript.jscomp.newtypes.JSType)5 QualifiedName (com.google.javascript.jscomp.newtypes.QualifiedName)3 DeclaredFunctionType (com.google.javascript.jscomp.newtypes.DeclaredFunctionType)2 RawNominalType (com.google.javascript.jscomp.newtypes.RawNominalType)2 Node (com.google.javascript.rhino.Node)2 ImmutableList (com.google.common.collect.ImmutableList)1 AbstractShallowCallback (com.google.javascript.jscomp.NodeTraversal.AbstractShallowCallback)1 EnumType (com.google.javascript.jscomp.newtypes.EnumType)1 FunctionType (com.google.javascript.jscomp.newtypes.FunctionType)1 FunctionTypeBuilder (com.google.javascript.jscomp.newtypes.FunctionTypeBuilder)1 NamespaceLit (com.google.javascript.jscomp.newtypes.NamespaceLit)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1