class MinimizeExitPoints extends NodeTraversal.AbstractPostOrderCallback implements CompilerPass
Modifier and Type | Field and Description |
---|---|
(package private) AbstractCompiler |
compiler |
Constructor and Description |
---|
MinimizeExitPoints(AbstractCompiler compiler) |
Modifier and Type | Method and Description |
---|---|
private static boolean |
matchingExitNode(Node n,
int type,
java.lang.String labelName)
Determines if n matches the type and name for the following types of
"exits":
- return without values
- continues and breaks with or without names.
|
private static void |
moveAllFollowing(Node start,
Node srcParent,
Node destParent)
Move all the child nodes following start in srcParent to the end of
destParent's child list.
|
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
(package private) void |
tryMinimizeExits(Node n,
int exitType,
java.lang.String labelName)
Attempts to minimize the number of explicit exit points in a control
structure to take advantage of the implied exit at the end of the
structure.
|
private void |
tryMinimizeIfBlockExits(Node srcBlock,
Node destBlock,
Node ifNode,
int exitType,
java.lang.String labelName)
Look for exits (returns, breaks, or continues, depending on the context) at
the end of a block and removes them by moving the if node's siblings,
if any, into the opposite condition block.
|
(package private) void |
tryMinimizeSwitchCaseExits(Node n,
int exitType,
java.lang.String labelName)
Attempt to remove explicit exits from switch cases that also occur implicitly
after the switch.
|
(package private) void |
tryMinimizeSwitchExits(Node n,
int exitType,
java.lang.String labelName) |
void |
visit(NodeTraversal t,
Node n,
Node parent)
Visits a node in postorder (after its children have been visited).
|
shouldTraverse
AbstractCompiler compiler
MinimizeExitPoints(AbstractCompiler compiler)
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
void tryMinimizeExits(Node n, int exitType, @Nullable java.lang.String labelName)
n
- The execution node of a parent to inspect.exitType
- The type of exit to look for.labelName
- If parent is a label the name of the label to look for,
null otherwise. Non-null only for breaks within labels.void tryMinimizeSwitchExits(Node n, int exitType, @Nullable java.lang.String labelName)
void tryMinimizeSwitchCaseExits(Node n, int exitType, @Nullable java.lang.String labelName)
private void tryMinimizeIfBlockExits(Node srcBlock, Node destBlock, Node ifNode, int exitType, @Nullable java.lang.String labelName)
srcBlock
- The block to inspect.destBlock
- The block to move sibling nodes into.ifNode
- The if node to work with.exitType
- The type of exit to look for.labelName
- The name associated with the exit, if any. null for anything excepted for
named-break associated with a label.private static boolean matchingExitNode(Node n, int type, @Nullable java.lang.String labelName)
n
- The node to inspect.type
- The Token type to look for.labelName
- The name that must be associated with the exit type.
non-null only for breaks associated with labels.private static void moveAllFollowing(Node start, Node srcParent, Node destParent)
start
- The start point in the srcParent child list.srcParent
- The parent node of start.destParent
- The destination node.