Search in sources :

Example 1 with NamespaceLit

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

the class NTIScope method freezeScope.

void freezeScope() {
    Preconditions.checkNotNull(this.declaredType, "No declared type for scope: %s", this.root);
    unknownTypeNames = ImmutableSet.of();
    // Alternatively, we could move this into NewTypeInference.initEdgeEnvs
    for (Map.Entry<String, Namespace> entry : localNamespaces.entrySet()) {
        String name = entry.getKey();
        Namespace ns = entry.getValue();
        if (ns instanceof NamespaceLit) {
            constVars.add(name);
        }
        JSType t = ns.toJSType();
        if (externs.containsKey(name)) {
            externs.put(name, t);
        } else {
            locals.put(name, LocalVarInfo.makeDeclared(t));
        }
    }
    for (String typedefName : localTypedefs.keySet()) {
        locals.put(typedefName, LocalVarInfo.makeDeclared(this.commonTypes.UNDEFINED));
    }
    copyOuterVarsTransitively(this);
    preservedNamespaces = localNamespaces;
    localNamespaces = ImmutableMap.of();
    isFrozen = true;
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) NamespaceLit(com.google.javascript.jscomp.newtypes.NamespaceLit) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Namespace(com.google.javascript.jscomp.newtypes.Namespace) FunctionNamespace(com.google.javascript.jscomp.newtypes.FunctionNamespace)

Example 2 with NamespaceLit

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

FunctionNamespace (com.google.javascript.jscomp.newtypes.FunctionNamespace)2 JSType (com.google.javascript.jscomp.newtypes.JSType)2 Namespace (com.google.javascript.jscomp.newtypes.Namespace)2 NamespaceLit (com.google.javascript.jscomp.newtypes.NamespaceLit)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 AbstractShallowCallback (com.google.javascript.jscomp.NodeTraversal.AbstractShallowCallback)1 Declaration (com.google.javascript.jscomp.newtypes.Declaration)1 FunctionTypeBuilder (com.google.javascript.jscomp.newtypes.FunctionTypeBuilder)1 RawNominalType (com.google.javascript.jscomp.newtypes.RawNominalType)1 Node (com.google.javascript.rhino.Node)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1