Package org.codehaus.groovy.classgen.asm
Class CompileStack
java.lang.Object
org.codehaus.groovy.classgen.asm.CompileStack
Manages different aspects of the code of a code block like handling labels,
defining variables, and scopes. After a MethodNode is visited clear should be
called, for initialization the method init should be used.
Some Notes:
- every push method will require a later pop call
- method parameters may define a category 2 variable, so don't ignore the type stored in the variable object
- the index of the variable may not be as assumed when the variable is a parameter of a method because the parameter may be used in a closure, so don't ignore the stored variable index
- the names of temporary variables can be ignored. The names are only used for debugging and do not conflict with each other or normal variables. For accessing, the index of the variable must be used.
- never mix temporary and normal variables by changes to this class. While the name is very important for a normal variable, it is only a helper construct for temporary variables. That means for example a name for a temporary variable can be used multiple times without conflict. So mixing them both may lead to the problem that a normal or temporary variable is hidden or even removed. That must not happen!
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classprotected static class -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddExceptionBlock(org.objectweb.asm.Label start, org.objectweb.asm.Label end, org.objectweb.asm.Label goal, String sig) voidvoidapplyFinallyBlocks(org.objectweb.asm.Label label, boolean isBreakLabel) voidclear()Clears the state of the class.booleancontainsVariable(String name) org.objectweb.asm.LabelcreateLocalLabel(String name) creates a new named labelintdefineTemporaryVariable(String name, boolean store) creates a temporary variable.intdefineTemporaryVariable(String name, ClassNode node, boolean store) creates a temporary variable.intdefineTemporaryVariable(Variable var, boolean store) Creates a temporary variable.defineVariable(Variable v, boolean initFromStack) Defines a new Variable using an AST variable.defineVariable(Variable v, ClassNode variableType, boolean initFromStack) org.objectweb.asm.Labelorg.objectweb.asm.Labelorg.objectweb.asm.LabelReturns the label for the given nameorg.objectweb.asm.LabelgetNamedBreakLabel(String name) Used forbreak fooinside a loop to end the execution of the marked loop.org.objectweb.asm.LabelgetNamedContinueLabel(String name) Used forcontinue fooinside a loop to continue the execution of the marked loop.getScope()getVariable(String variableName) getVariable(String variableName, boolean mustExist) Returns a normal variable.booleanvoidinit(VariableScope scope, Parameter[] parameters) initializes this class for a MethodNode.booleanbooleanbooleanisLHS()voidpop()voidpopBlockRecorderVisit(CompileStack.BlockRecorder finallyBlock) voidvoidpopLHS()voidpushBlockRecorder(CompileStack.BlockRecorder recorder) voidpushBlockRecorderVisit(CompileStack.BlockRecorder finallyBlock) voidDeprecated.voidpushImplicitThis(boolean implicitThis) voidvoidpushLHS(boolean lhs) voidShould be called when descending into a loop that does not define a scope.voidShould be called when descending into a loop that does not define a scope.voidpushLoop(VariableScope scope, String labelName) Should be called when descending into a loop that defines also a scope.voidpushLoop(VariableScope el, List<String> labelNames) Should be called when descending into a loop that defines also a scope.voidorg.objectweb.asm.LabelCreates a new break label and an element for the state stack so pop has to be called latervoidpushVariableScope(VariableScope scope) Causes the state-stack to add an element and sets the given scope as new current variable scope.voidremoveVar(int variableIndex) Indicates that the specified temporary variable is no longer used.voidwriteExceptionTable(CompileStack.BlockRecorder block, org.objectweb.asm.Label goal, String sig)
-
Constructor Details
-
CompileStack
-
-
Method Details
-
getBreakLabel
public org.objectweb.asm.Label getBreakLabel() -
getContinueLabel
public org.objectweb.asm.Label getContinueLabel() -
getScope
-
pushState
public void pushState() -
removeVar
public void removeVar(int variableIndex) Indicates that the specified temporary variable is no longer used. -
pop
public void pop() -
defineTemporaryVariable
Creates a temporary variable.- Parameters:
var- specifies name and typestore- defines if the toplevel argument of the stack should be stored- Returns:
- the index used for this temporary variable
-
getVariable
-
getVariable
Returns a normal variable.If
mustExistis true and the normal variable doesn't exist, then this method will throw a GroovyBugError. It is not the intention of this method to let this happen! And the exception should not be used for flow control - it is just acting as an assertion. If the exception is thrown then it indicates a bug in the class using CompileStack. This method can also not be used to return a temporary variable. Temporary variables are not normal variables.- Parameters:
variableName- name of the variablemustExist- throw exception if variable does not exist- Returns:
- the normal variable or null if not found (and
mustExistnot true)
-
defineTemporaryVariable
creates a temporary variable.- Parameters:
name- defines type and namestore- defines if the top-level argument of the stack should be stored- Returns:
- the index used for this temporary variable
-
defineTemporaryVariable
creates a temporary variable.- Parameters:
name- defines the namenode- defines the nodestore- defines if the top-level argument of the stack should be stored- Returns:
- the index used for this temporary variable
-
clear
public void clear()Clears the state of the class. This method should be called after a MethodNode is visited. Note that a call to init will fail if clear is not called before -
addExceptionBlock
public void addExceptionBlock(org.objectweb.asm.Label start, org.objectweb.asm.Label end, org.objectweb.asm.Label goal, String sig) -
init
initializes this class for a MethodNode. This method will automatically define variables for the method parameters and will create references if needed. The created variables can be accessed by calling getVariable(). -
pushVariableScope
Causes the state-stack to add an element and sets the given scope as new current variable scope. Creates an element for the state stack so pop has to be called later -
pushLoop
Should be called when descending into a loop that defines also a scope. Calls pushVariableScope and prepares labels for a loop structure. Creates a element for the state stack so pop has to be called later, TODO: @Deprecate -
pushLoop
Should be called when descending into a loop that defines also a scope. Calls pushVariableScope and prepares labels for a loop structure. Creates an element for the state stack so pop has to be called later -
pushLoop
Should be called when descending into a loop that does not define a scope. Creates a element for the state stack so pop has to be called later, TODO: @Deprecate -
pushLoop
Should be called when descending into a loop that does not define a scope. Creates an element for the state stack so pop has to be called later -
getNamedBreakLabel
Used forbreak fooinside a loop to end the execution of the marked loop. This method will return the break label of the loop if there is one found for the name. If not, the current break label is returned. -
getNamedContinueLabel
Used forcontinue fooinside a loop to continue the execution of the marked loop. This method will return the break label of the loop if there is one found for the name. If not, getLabel is used. -
pushSwitch
public org.objectweb.asm.Label pushSwitch()Creates a new break label and an element for the state stack so pop has to be called later -
pushBooleanExpression
Deprecated.UsepushState()directly.Because a boolean Expression may not be evaluated completely it is important to keep the registers clean. -
defineVariable
Defines a new Variable using an AST variable.- Parameters:
initFromStack- if true the last element of the stack will be used to initialize the new variable. If false null will be used.
-
defineVariable
-
containsVariable
- Parameters:
name- the name of the variable of interest- Returns:
- true if a variable is already defined
-
getLabel
Returns the label for the given name -
createLocalLabel
creates a new named label -
applyFinallyBlocks
public void applyFinallyBlocks(org.objectweb.asm.Label label, boolean isBreakLabel) -
applyBlockRecorder
public void applyBlockRecorder() -
hasBlockRecorder
public boolean hasBlockRecorder() -
pushBlockRecorder
-
pushBlockRecorderVisit
-
popBlockRecorderVisit
-
writeExceptionTable
public void writeExceptionTable(CompileStack.BlockRecorder block, org.objectweb.asm.Label goal, String sig) -
isLHS
public boolean isLHS() -
pushLHS
public void pushLHS(boolean lhs) -
popLHS
public void popLHS() -
isImplicitThis
public boolean isImplicitThis() -
pushImplicitThis
public void pushImplicitThis(boolean implicitThis) -
popImplicitThis
public void popImplicitThis() -
isInSpecialConstructorCall
public boolean isInSpecialConstructorCall() -
pushInSpecialConstructorCall
public void pushInSpecialConstructorCall()
-
pushState()directly.