/** * Regenerates the top scope from scratch. * * @param compiler The compiler for which the global scope is regenerated. * @param root The root of the AST. */ void regenerateGlobalTypedScope(AbstractCompiler compiler, Node root) { internalScopeCreator = new TypedScopeCreator(compiler); typedScopeCreator = new MemoizedScopeCreator(internalScopeCreator); topScope = typedScopeCreator.createScope(root, null); }
/** * Traverses *just* the contents of provided scope nodes (and optionally scopes nested within * them) but will fall back on traversing the entire AST from root if a null scope nodes list is * provided. Also allows for a callback to notify when starting on one of the provided scope * nodes. * @param root If scopeNodes is null, this method will just traverse 'root' instead. If scopeNodes * is not null, this parameter is ignored. */ public static void traverseScopeRoots( AbstractCompiler compiler, @Nullable Node root, @Nullable List<Node> scopeNodes, final Callback cb, @Nullable final ChangeScopeRootCallback changeCallback, final boolean traverseNested) { if (scopeNodes == null) { NodeTraversal.traverse(compiler, root, cb); } else { MemoizedScopeCreator scopeCreator = new MemoizedScopeCreator(new Es6SyntacticScopeCreator(compiler)); for (final Node scopeNode : scopeNodes) { traverseSingleScopeRoot( compiler, cb, changeCallback, traverseNested, scopeCreator, scopeNode); } } }
@Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { ScopeCreator creator = compiler.getTypedScopeCreator(); if (creator instanceof MemoizedScopeCreator) { MemoizedScopeCreator scopeCreator = (MemoizedScopeCreator) creator; String newSrc = scriptRoot.getSourceFileName(); for (TypedVar var : scopeCreator.getAllSymbols()) { TypeI type = var.getType(); if (type != null) { FunctionTypeI fnType = type.toMaybeFunctionType(); if (fnType != null && newSrc.equals(NodeUtil.getSourceName(fnType.getSource()))) { fnType.setSource(null); } } } scopeCreator.removeScopesForScript(originalRoot.getSourceFileName()); } }
.traverseWithScope(node, topScope); for (TypedScope s : scopeCreator.getAllMemoizedScopes()) { s.resolveTypes();
@Override public void process(Node externs, Node root) { ScopeCreator scopeCreator = new MemoizedScopeCreator(new TypedScopeCreator(compiler)); NodeTraversal externsTraversal = new NodeTraversal(compiler, new Traversal(true), scopeCreator); NodeTraversal codeTraversal = new NodeTraversal(compiler, new Traversal(false), scopeCreator); Scope topScope = compiler.getTopScope(); if (topScope != null) { externsTraversal.traverseWithScope(externs, topScope); codeTraversal.traverseWithScope(root, topScope); } else { externsTraversal.traverse(externs); codeTraversal.traverse(root); } connectUnknowns(); }
symbolTable.addScopes(typedScopeCreator.getAllMemoizedScopes()); symbolTable.addSymbolsFrom(typedScopeCreator); } else {
/** Main entry point of this phase for testing code. */ public TypedScope processForTesting(Node externsRoot, Node jsRoot) { Preconditions.checkState(scopeCreator == null); Preconditions.checkState(topScope == null); Preconditions.checkState(jsRoot.getParent() != null); Node externsAndJsRoot = jsRoot.getParent(); scopeCreator = new MemoizedScopeCreator(new TypedScopeCreator(compiler)); topScope = scopeCreator.createScope(externsAndJsRoot, null); TypeInferencePass inference = new TypeInferencePass(compiler, reverseInterpreter, topScope, scopeCreator); inference.process(externsRoot, jsRoot); process(externsRoot, jsRoot); return topScope; }