class FlowSensitiveInlineVariables extends NodeTraversal.AbstractPostOrderCallback implements CompilerPass, NodeTraversal.ScopedCallback
MaybeReachingVariableUse
and MustBeReachingVariableDef
,
this pass attempts to inline a variable by placing the value at the
definition where the variable is used. The basic requirements for inlining
are the following:
Other requirements can be found in FlowSensitiveInlineVariables.Candidate.canInline(com.google.javascript.jscomp.Scope)
. Currently
this pass does not operate on the global scope due to compilation time.
Modifier and Type | Class and Description |
---|---|
private class |
FlowSensitiveInlineVariables.Candidate
Models the connection between a definition and a use of that definition.
|
private class |
FlowSensitiveInlineVariables.GatherCandiates
Gathers a list of possible candidates for inlining based only on
information from
MustBeReachingVariableDef . |
Modifier and Type | Field and Description |
---|---|
private java.util.List<FlowSensitiveInlineVariables.Candidate> |
candidates |
private ControlFlowGraph<Node> |
cfg |
private AbstractCompiler |
compiler
Implementation:
This pass first perform a traversal to gather a list of Candidates that
could be inlined using
FlowSensitiveInlineVariables.GatherCandiates . |
private java.util.Set<Var> |
inlinedNewDependencies |
private MustBeReachingVariableDef |
reachingDef |
private MaybeReachingVariableUse |
reachingUses |
private static com.google.common.base.Predicate<Node> |
SIDE_EFFECT_PREDICATE |
Constructor and Description |
---|
FlowSensitiveInlineVariables(AbstractCompiler compiler) |
Modifier and Type | Method and Description |
---|---|
private static boolean |
checkLeftOf(Node n,
Node expressionRoot,
com.google.common.base.Predicate<Node> predicate)
Given an expression by its root and sub-expression n, return true if there
the predicate is true for some expression on the left of n.
|
private static boolean |
checkRightOf(Node n,
Node expressionRoot,
com.google.common.base.Predicate<Node> predicate)
Given an expression by its root and sub-expression n, return true if there
the predicate is true for some expression on the right of n.
|
void |
enterScope(NodeTraversal t)
Called immediately after entering a new scope.
|
void |
exitScope(NodeTraversal t)
Called immediately before exiting a scope.
|
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
void |
visit(NodeTraversal t,
Node n,
Node parent)
Visits a node in postorder (after its children have been visited).
|
shouldTraverse
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
shouldTraverse
private final AbstractCompiler compiler
FlowSensitiveInlineVariables.GatherCandiates
.
The second step involves verifying that each candidate is actually safe
to inline with FlowSensitiveInlineVariables.Candidate.canInline(Scope)
and finally perform
inlining using FlowSensitiveInlineVariables.Candidate.inlineVariable()
.
The reason for the delayed evaluation of the candidates is because we
need two separate dataflow result.private final java.util.Set<Var> inlinedNewDependencies
private ControlFlowGraph<Node> cfg
private java.util.List<FlowSensitiveInlineVariables.Candidate> candidates
private MustBeReachingVariableDef reachingDef
private MaybeReachingVariableUse reachingUses
private static final com.google.common.base.Predicate<Node> SIDE_EFFECT_PREDICATE
public FlowSensitiveInlineVariables(AbstractCompiler compiler)
public void enterScope(NodeTraversal t)
NodeTraversal.ScopedCallback
enterScope
in interface NodeTraversal.ScopedCallback
public void exitScope(NodeTraversal t)
NodeTraversal.ScopedCallback
exitScope
in interface NodeTraversal.ScopedCallback
public void process(Node externs, Node root)
CompilerPass
process
in interface CompilerPass
externs
- Top of external JS treeroot
- Top of JS treepublic void visit(NodeTraversal t, Node n, Node parent)
NodeTraversal.Callback
Visits a node in postorder (after its children have been visited).
A node is visited only if all its parents should be traversed
(NodeTraversal.Callback.shouldTraverse(NodeTraversal, Node, Node)
).
Implementations can have side effects (e.g. modifying the parse tree).
visit
in interface NodeTraversal.Callback
private static boolean checkRightOf(Node n, Node expressionRoot, com.google.common.base.Predicate<Node> predicate)
private static boolean checkLeftOf(Node n, Node expressionRoot, com.google.common.base.Predicate<Node> predicate)