class ExpressionDecomposer
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
private static class |
ExpressionDecomposer.DecompositionState
A simple class to track two things:
- whether side effects have been seen.
|
(package private) static class |
ExpressionDecomposer.DecompositionType |
Modifier and Type | Field and Description |
---|---|
private AbstractCompiler |
compiler |
private java.util.Set<java.lang.String> |
knownConstants |
private static int |
MAX_INTERATIONS |
private java.lang.String |
resultNamePrefix |
private com.google.common.base.Supplier<java.lang.String> |
safeNameIdSupplier |
private Scope |
scope |
private java.lang.String |
tempNamePrefix |
Constructor and Description |
---|
ExpressionDecomposer(AbstractCompiler compiler,
com.google.common.base.Supplier<java.lang.String> safeNameIdSupplier,
java.util.Set<java.lang.String> constNames,
Scope scope) |
Modifier and Type | Method and Description |
---|---|
private static boolean |
allowObjectCallDecomposing() |
private static Node |
buildResultExpression(Node expr,
boolean needResult,
java.lang.String tempName)
Create an expression tree for an expression.
|
(package private) ExpressionDecomposer.DecompositionType |
canExposeExpression(Node subExpression)
Determine whether a expression is movable, or can be be made movable be
decomposing the containing expression.
|
private void |
decomposeObjectLiteralKeys(Node key,
Node stopNode,
ExpressionDecomposer.DecompositionState state)
Decompose an object literal.
|
private void |
decomposeSubExpressions(Node n,
Node stopNode,
ExpressionDecomposer.DecompositionState state) |
(package private) void |
exposeExpression(Node expression)
Perform any rewriting necessary so that the specified expression
is movable.
|
private void |
exposeExpression(Node expressionRoot,
Node subExpression)
Rewrite the expression such that the sub-expression is in a movable
expression statement while maintaining evaluation order.
|
private Node |
extractConditional(Node expr,
Node injectionPoint,
boolean needResult) |
private Node |
extractExpression(Node expr,
Node injectionPoint) |
(package private) static Node |
findExpressionRoot(Node subExpression) |
(package private) static Node |
findInjectionPoint(Node subExpression) |
private static Node |
findNonconditionalParent(Node subExpression,
Node expressionRoot) |
private java.lang.String |
getResultValueName()
Create a unique name for call results.
|
private java.lang.String |
getTempConstantValueName()
Create a constant unique temp name.
|
private java.lang.String |
getTempValueName()
Create a unique temp name.
|
private static boolean |
isConditionalOp(Node n) |
private boolean |
isConstantNameNode(Node n,
java.util.Set<java.lang.String> knownConstants) |
private boolean |
isExpressionTreeUnsafe(Node n,
boolean followingSideEffectsExist) |
private boolean |
isSafeAssign(Node n,
boolean seenSideEffects)
It is always safe to inline "foo()" for expressions such as
"a = b = c = foo();"
As the assignment is unaffected by side effect of "foo()"
and the names assigned-to can not influence the state before
the call to foo.
|
private ExpressionDecomposer.DecompositionType |
isSubexpressionMovable(Node expressionRoot,
Node subExpression)
Walk the AST from the call site to the expression root and verify that
the portions of the expression that are evaluated before the call are:
1) Unaffected by the the side-effects, if any, of the call.
|
(package private) void |
maybeExposeExpression(Node expression)
If required, rewrite the statement containing the expression.
|
private static boolean |
maybeExternMethod(Node node) |
(package private) void |
moveExpression(Node expression)
Extract the specified expression from its parent expression.
|
private Node |
rewriteCallExpression(Node call,
ExpressionDecomposer.DecompositionState state)
Rewrite the call so "this" is preserved.
|
void |
setResultNamePrefix(java.lang.String prefix)
Allow the temp name to be overridden to make tests more readable.
|
void |
setTempNamePrefix(java.lang.String prefix)
Allow the temp name to be overridden to make tests more readable.
|
private final AbstractCompiler compiler
private final com.google.common.base.Supplier<java.lang.String> safeNameIdSupplier
private final java.util.Set<java.lang.String> knownConstants
private final Scope scope
private static final int MAX_INTERATIONS
private java.lang.String tempNamePrefix
private java.lang.String resultNamePrefix
ExpressionDecomposer(AbstractCompiler compiler, com.google.common.base.Supplier<java.lang.String> safeNameIdSupplier, java.util.Set<java.lang.String> constNames, Scope scope)
void maybeExposeExpression(Node expression)
expression
- The expression to be exposed.canExposeExpression(com.google.javascript.rhino.Node)
void exposeExpression(Node expression)
void moveExpression(Node expression)
private void exposeExpression(Node expressionRoot, Node subExpression)
private static boolean allowObjectCallDecomposing()
private static boolean maybeExternMethod(Node node)
private static Node findNonconditionalParent(Node subExpression, Node expressionRoot)
private void decomposeObjectLiteralKeys(Node key, Node stopNode, ExpressionDecomposer.DecompositionState state)
key
- The object literal key.stopNode
- A node after which to stop iterating.private void decomposeSubExpressions(Node n, Node stopNode, ExpressionDecomposer.DecompositionState state)
n
- The node with which to start iterating.stopNode
- A node after which to stop iterating.private Node extractConditional(Node expr, Node injectionPoint, boolean needResult)
expr
- The conditional expression to extract.injectionPoint
- The before which extracted expression, would be
injected.needResult
- Whether the result of the expression is required.private static Node buildResultExpression(Node expr, boolean needResult, java.lang.String tempName)
private boolean isConstantNameNode(Node n, java.util.Set<java.lang.String> knownConstants)
private Node extractExpression(Node expr, Node injectionPoint)
expr
- The expression to extract.injectionPoint
- The node before which to added the extracted
expression.private Node rewriteCallExpression(Node call, ExpressionDecomposer.DecompositionState state)
public void setTempNamePrefix(java.lang.String prefix)
private java.lang.String getTempValueName()
public void setResultNamePrefix(java.lang.String prefix)
private java.lang.String getResultValueName()
private java.lang.String getTempConstantValueName()
static Node findInjectionPoint(Node subExpression)
private static boolean isConditionalOp(Node n)
static Node findExpressionRoot(Node subExpression)
ExpressionDecomposer.DecompositionType canExposeExpression(Node subExpression)
private ExpressionDecomposer.DecompositionType isSubexpressionMovable(Node expressionRoot, Node subExpression)
private boolean isSafeAssign(Node n, boolean seenSideEffects)
seenSideEffects
- If true, check to see if node-tree maybe affected by
side-effects, otherwise if the tree has side-effects. @see
isExpressionTreeUnsafeprivate boolean isExpressionTreeUnsafe(Node n, boolean followingSideEffectsExist)