class MinimizedCondition
extends java.lang.Object
Depending on the context, a leading NOT node in front of the conditional may or may not be counted as a cost, so this class provides ways to access minimized versions of both of those abstract syntax trees (ASTs).
Modifier and Type | Class and Description |
---|---|
(package private) static class |
MinimizedCondition.MeasuredNode
An AST-node along with some additional metadata.
|
(package private) static class |
MinimizedCondition.MinimizationStyle
Definitions of the style of minimization preferred.
|
Modifier and Type | Field and Description |
---|---|
private static java.util.Comparator<MinimizedCondition.MeasuredNode> |
AST_LENGTH_COMPARATOR |
private MinimizedCondition.MeasuredNode |
negative
A representation equivalent to the negation of the original condition.
|
private Node |
placeholder
A placeholder at the same AST location as the original condition
|
private MinimizedCondition.MeasuredNode |
positive
A representation equivalent to the original condition.
|
Modifier | Constructor and Description |
---|---|
private |
MinimizedCondition(MinimizedCondition.MeasuredNode p,
MinimizedCondition.MeasuredNode n) |
Modifier and Type | Method and Description |
---|---|
private static MinimizedCondition |
computeMinimizedCondition(Node n)
Minimize the condition at the given node.
|
(package private) static MinimizedCondition |
fromConditionNode(Node n)
Remove the passed condition node from the AST, and then return a
MinimizedCondition that represents the condition node after
minimization.
|
(package private) MinimizedCondition.MeasuredNode |
getMinimized(MinimizedCondition.MinimizationStyle style)
Return the shorter representation of the original condition node.
|
(package private) Node |
getPlaceholder() |
(package private) MinimizedCondition |
setPlaceholder(Node placeholder) |
private static Node |
swapWithPlaceholderNode(Node n)
Remove the given node from the AST, and replace it with a placeholder
SCRIPT node.
|
(package private) static MinimizedCondition |
unoptimized(Node n)
Return a MeasuredNode of the given condition node, without minimizing
the result.
|
private final MinimizedCondition.MeasuredNode positive
private final MinimizedCondition.MeasuredNode negative
private Node placeholder
private static final java.util.Comparator<MinimizedCondition.MeasuredNode> AST_LENGTH_COMPARATOR
private MinimizedCondition(MinimizedCondition.MeasuredNode p, MinimizedCondition.MeasuredNode n)
Node getPlaceholder()
MinimizedCondition setPlaceholder(Node placeholder)
static MinimizedCondition fromConditionNode(Node n)
MinimizedCondition.MeasuredNode getMinimized(MinimizedCondition.MinimizationStyle style)
Depending on the context, this may require to either penalize or not the existence of a leading NOT node.
style
is PREFER_UNNEGATED
, simply try to
minimize the total length of the conditional.style
is ALLOW_LEADING_NOT
, prefer the right side
in cases such as:
!x || !y || z ==> !(x && y && !z)
fromConditionNode(com.google.javascript.rhino.Node)
.static MinimizedCondition unoptimized(Node n)
Since a MinimizedCondition necessarily must contain two trees, this
method sets the negative side to a Token.SCRIPT
node (never valid
inside an expression) with an unreasonably high length so that it will
never be chosen by getMinimized(com.google.javascript.jscomp.MinimizedCondition.MinimizationStyle)
.
n
- the conditional expression tree to minimize.
This must be connected to the AST, and will be swapped
with a placeholder node during minimization.private static Node swapWithPlaceholderNode(Node n)
private static MinimizedCondition computeMinimizedCondition(Node n)
n
- the conditional expression tree to minimize.
This must be connected to the AST, and will be swapped
with a placeholder node during minimization.