class ReferenceCollectingCallback extends java.lang.Object implements NodeTraversal.ScopedCallback, HotSwapCompilerPass, StaticSymbolTable<Var,ReferenceCollectingCallback.Reference>
ReferenceCollectingCallback.Behavior
and then
calling process(Node, Node)
.Modifier and Type | Class and Description |
---|---|
(package private) static class |
ReferenceCollectingCallback.BasicBlock
Represents a section of code that is uninterrupted by control structures
(conditional or iterative logic).
|
(package private) static interface |
ReferenceCollectingCallback.Behavior
Way for callers to add specific behavior during traversal that
utilizes the built-up reference information.
|
(package private) static class |
ReferenceCollectingCallback.Reference
Represents a single declaration or reference to a variable.
|
(package private) static class |
ReferenceCollectingCallback.ReferenceCollection
A collection of references.
|
(package private) static interface |
ReferenceCollectingCallback.ReferenceMap |
private static class |
ReferenceCollectingCallback.ReferenceMapWrapper |
Modifier and Type | Field and Description |
---|---|
private ReferenceCollectingCallback.Behavior |
behavior
Source of behavior at various points in the traversal.
|
private java.util.List<ReferenceCollectingCallback.BasicBlock> |
blockStack
The stack of basic blocks and scopes the current traversal is in.
|
private AbstractCompiler |
compiler
JavaScript compiler to use in traversing.
|
(package private) static ReferenceCollectingCallback.Behavior |
DO_NOTHING_BEHAVIOR |
private java.util.Set<Var> |
finishedFunctionTraverse |
private Scope |
narrowScope |
private java.util.Map<Var,ReferenceCollectingCallback.ReferenceCollection> |
referenceMap
Maps a given variable to a collection of references to that name.
|
private java.util.Set<Var> |
startedFunctionTraverse
Traverse hoisted functions where they're referenced, not
where they're declared.
|
private com.google.common.base.Predicate<Var> |
varFilter
Only collect references for filtered variables.
|
Constructor and Description |
---|
ReferenceCollectingCallback(AbstractCompiler compiler,
ReferenceCollectingCallback.Behavior behavior)
Constructor initializes block stack.
|
ReferenceCollectingCallback(AbstractCompiler compiler,
ReferenceCollectingCallback.Behavior behavior,
com.google.common.base.Predicate<Var> varFilter)
Constructor only collects references that match the given variable.
|
Modifier and Type | Method and Description |
---|---|
private void |
addReference(Var v,
ReferenceCollectingCallback.Reference reference) |
void |
enterScope(NodeTraversal t)
Updates block stack and invokes any additional behavior.
|
void |
exitScope(NodeTraversal t)
Updates block stack and invokes any additional behavior.
|
java.lang.Iterable<Var> |
getAllSymbols()
Gets the variables that were referenced in this callback.
|
ReferenceCollectingCallback.ReferenceCollection |
getReferences(Var v)
Gets the reference collection for the given variable.
|
Scope |
getScope(Var var)
Returns the scope for a given symbol.
|
void |
hotSwapScript(Node scriptRoot,
Node originalRoot)
Same as process but only runs on a part of AST associated to one script.
|
private static boolean |
isBlockBoundary(Node n,
Node parent) |
private void |
outOfBandTraversal(Var v) |
private static <T> T |
peek(java.util.List<T> list) |
private static <T> T |
pop(java.util.List<T> list) |
void |
process(Node externs,
Node root)
Convenience method for running this pass over a tree with this
class as a callback.
|
(package private) void |
processScope(Scope scope)
Targets reference collection to a particular scope.
|
boolean |
shouldTraverse(NodeTraversal nodeTraversal,
Node n,
Node parent)
Updates block stack.
|
void |
visit(NodeTraversal t,
Node n,
Node parent)
For each node, update the block stack and reference collection
as appropriate.
|
private final java.util.Map<Var,ReferenceCollectingCallback.ReferenceCollection> referenceMap
private java.util.List<ReferenceCollectingCallback.BasicBlock> blockStack
private final ReferenceCollectingCallback.Behavior behavior
private final AbstractCompiler compiler
private final com.google.common.base.Predicate<Var> varFilter
private final java.util.Set<Var> startedFunctionTraverse
private final java.util.Set<Var> finishedFunctionTraverse
private Scope narrowScope
static final ReferenceCollectingCallback.Behavior DO_NOTHING_BEHAVIOR
ReferenceCollectingCallback(AbstractCompiler compiler, ReferenceCollectingCallback.Behavior behavior)
ReferenceCollectingCallback(AbstractCompiler compiler, ReferenceCollectingCallback.Behavior behavior, com.google.common.base.Predicate<Var> varFilter)
public void process(Node externs, Node root)
process
in interface CompilerPass
externs
- Top of external JS treeroot
- Top of JS treevoid processScope(Scope scope)
public void hotSwapScript(Node scriptRoot, Node originalRoot)
hotSwapScript
in interface HotSwapCompilerPass
scriptRoot
- Root node corresponding to the file that is modified,
should be of type Token.SCRIPT
.originalRoot
- Root node corresponding to the original version of the
file that is modified. Should be of type token.SCRIPT
.public java.lang.Iterable<Var> getAllSymbols()
getAllSymbols
in interface StaticSymbolTable<Var,ReferenceCollectingCallback.Reference>
public Scope getScope(Var var)
StaticSymbolTable
getScope
in interface StaticSymbolTable<Var,ReferenceCollectingCallback.Reference>
public ReferenceCollectingCallback.ReferenceCollection getReferences(Var v)
getReferences
in interface StaticSymbolTable<Var,ReferenceCollectingCallback.Reference>
public void visit(NodeTraversal t, Node n, Node parent)
visit
in interface NodeTraversal.Callback
private void outOfBandTraversal(Var v)
public void enterScope(NodeTraversal t)
enterScope
in interface NodeTraversal.ScopedCallback
public void exitScope(NodeTraversal t)
exitScope
in interface NodeTraversal.ScopedCallback
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node n, Node parent)
shouldTraverse
in interface NodeTraversal.Callback
private static <T> T pop(java.util.List<T> list)
private static <T> T peek(java.util.List<T> list)
private static boolean isBlockBoundary(Node n, Node parent)
private void addReference(Var v, ReferenceCollectingCallback.Reference reference)