public class BlockBuilder extends Object
BlockStatement.
Has methods that help ensure that variable names are unique.
| Modifier and Type | Class and Description |
|---|---|
private static class |
BlockBuilder.InlineVariableVisitor
Inline Variable Visitor.
|
private static class |
BlockBuilder.Slot
Holds the number of times a declaration was used.
|
private static class |
BlockBuilder.SubstituteVariableVisitor
Substitute Variable Visitor.
|
private static class |
BlockBuilder.UseCounter
Use counter.
|
| Modifier and Type | Field and Description |
|---|---|
(package private) Map<Expression,DeclarationStatement> |
expressionForReuse
Contains final-fine-to-reuse-declarations.
|
private static Shuttle |
OPTIMIZE_SHUTTLE |
private boolean |
optimizing |
private BlockBuilder |
parent |
(package private) List<Statement> |
statements |
(package private) Set<String> |
variables |
| Constructor and Description |
|---|
BlockBuilder()
Creates a non-optimizing BlockBuilder.
|
BlockBuilder(boolean optimizing)
Creates a BlockBuilder.
|
BlockBuilder(boolean optimizing,
BlockBuilder parent)
Creates a BlockBuilder.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(Expression expression) |
void |
add(Statement statement) |
protected void |
addExpressionForReuse(DeclarationStatement decl) |
private Expression |
append_(String name,
Expression expression,
boolean optimize) |
BlockBuilder |
append(Expression expression) |
Expression |
append(String name,
BlockStatement block)
Appends a block to a list of statements and returns an expression
(possibly a variable) that represents the result of the newly added
block.
|
Expression |
append(String name,
BlockStatement block,
boolean optimize)
Appends an expression to a list of statements, optionally optimizing it
to a variable if it is used more than once.
|
Expression |
append(String name,
Expression expression)
Appends an expression to a list of statements, and returns an expression
(possibly a variable) that represents the result of the newly added
block.
|
Expression |
append(String name,
Expression expression,
boolean optimize)
Appends an expression to a list of statements, optionally optimizing if
the expression is used more than once.
|
Expression |
appendIfNotNull(String name,
Expression expression)
Appends an expression to a list of statements, if it is not null.
|
void |
clear()
Clears this BlockBuilder.
|
protected Shuttle |
createFinishingOptimizeShuttle()
Creates a final optimization shuttle.
|
protected Shuttle |
createOptimizeShuttle()
Creates a shuttle that will be used during block optimization.
|
DeclarationStatement |
getComputedExpression(Expression expr)
Returns the reference to ParameterExpression if given expression was
already computed and stored to local variable
|
boolean |
hasVariable(String name) |
private boolean |
isCostly(DeclarationStatement decl) |
protected boolean |
isSafeForReuse(DeclarationStatement decl) |
protected boolean |
isSimpleExpression(Expression expr)
Checks if expression is simple enough to always inline at zero cost.
|
String |
newName(String suggestion)
Creates a name for a new variable, unique within this block.
|
private String |
newName(String suggestion,
boolean optimize)
Creates a name for a new variable, unique within this block, controlling
whether the variable can be inlined later.
|
private Expression |
normalizeDeclaration(DeclarationStatement decl)
Prepares declaration for inlining: adds cast
|
private boolean |
optimize(Shuttle optimizer,
boolean performInline)
Optimizes the list of statements.
|
BlockStatement |
toBlock()
Returns a block consisting of the current list of statements.
|
final Map<Expression,DeclarationStatement> expressionForReuse
private final boolean optimizing
private final BlockBuilder parent
private static final Shuttle OPTIMIZE_SHUTTLE
public BlockBuilder()
public BlockBuilder(boolean optimizing)
optimizing - Whether to eliminate common sub-expressionspublic BlockBuilder(boolean optimizing,
BlockBuilder parent)
optimizing - Whether to eliminate common sub-expressionspublic void clear()
public Expression append(String name, BlockStatement block)
public Expression append(String name, BlockStatement block, boolean optimize)
name - Suggested variable nameblock - Expressionoptimize - Whether to try to optimize by assigning the expression to
a variable. Do not do this if the expression has
side-effects or a time-dependent value.public Expression append(String name, Expression expression)
public Expression appendIfNotNull(String name, Expression expression)
public Expression append(String name, Expression expression, boolean optimize)
private Expression append_(String name, Expression expression, boolean optimize)
protected boolean isSimpleExpression(Expression expr)
expr - expression to testprotected boolean isSafeForReuse(DeclarationStatement decl)
protected void addExpressionForReuse(DeclarationStatement decl)
private boolean isCostly(DeclarationStatement decl)
private Expression normalizeDeclaration(DeclarationStatement decl)
decl - inlining candidatepublic DeclarationStatement getComputedExpression(Expression expr)
expr - expression to testpublic void add(Statement statement)
public void add(Expression expression)
public BlockStatement toBlock()
private boolean optimize(Shuttle optimizer, boolean performInline)
protected Shuttle createOptimizeShuttle()
protected Shuttle createFinishingOptimizeShuttle()
private String newName(String suggestion, boolean optimize)
public String newName(String suggestion)
public boolean hasVariable(String name)
public BlockBuilder append(Expression expression)
Copyright © 2012–2023 The Apache Software Foundation. All rights reserved.