Groovy 1.7.0

org.codehaus.groovy.classgen
Class CompileStack

java.lang.Object
  org.codehaus.groovy.classgen.CompileStack
All Implemented Interfaces:
org.objectweb.asm.Opcodes

class CompileStack
extends Object

This class is a helper for AsmClassGenerator. It 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:

see:
AsmClassGenerator
author:
Jochen Theodorou


Nested Class Summary
class CompileStack.StateStackElement

 
Constructor Summary
CompileStack()

 
Method Summary
void applyFinallyBlocks(Label label, boolean isBreakLabel)

void applyFinallyBlocks()

void clear()

Clears the state of the class.

boolean containsVariable(String name)

Label createLocalLabel(String name)

creates a new named label

int defineTemporaryVariable(def var, boolean store)

creates a temporary variable.

int defineTemporaryVariable(String name, boolean store)

creates a temporary variable.

int defineTemporaryVariable(String name, ClassNode node, boolean store)

creates a temporary variable.

Variable defineVariable(def v, boolean initFromStack)

Defines a new Variable using an AST variable.

Label getBreakLabel()

Label getContinueLabel()

Label getLabel(String name)

Returns the label for the given name

protected Label getNamedBreakLabel(String name)

Used for break foo inside a loop to end the execution of the marked loop.

protected Label getNamedContinueLabel(String name)

Used for continue foo inside a loop to continue the execution of the marked loop.

VariableScope getScope()

Variable getVariable(String variableName)

Variable getVariable(String variableName, boolean mustExist)

Returns a normal variable.

boolean hasFinallyBlocks()

protected void init(VariableScope el, Parameter[] parameters, MethodVisitor mv, ClassNode cn)

initializes this class for a MethodNode.

void pop()

void popFinallyBlock()

void popFinallyBlockVisit(Runnable block)

protected void pushBooleanExpression()

because a boolean Expression may not be evaluated completly it is important to keep the registers clean

void pushFinallyBlock(Runnable block)

void pushFinallyBlockVisit(Runnable block)

protected void pushLoop(VariableScope el, String labelName)

Should be called when decending into a loop that defines also a scope.

protected void pushLoop(String labelName)

Should be called when decending into a loop that does not define a scope.

protected void pushState()

protected Label pushSwitch()

Creates a new break label and a element for the state stack so pop has to be called later

protected void pushVariableScope(VariableScope el)

Causes the statestack to add an element and sets the given scope as new current variable scope.

void removeVar(int tempIndex)

 
Methods inherited from class Object
wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll
 

Constructor Detail

CompileStack

CompileStack()


 
Method Detail

applyFinallyBlocks

public void applyFinallyBlocks(Label label, boolean isBreakLabel)


applyFinallyBlocks

public void applyFinallyBlocks()


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


containsVariable

public boolean containsVariable(String name)
param:
name the name of the variable of interest
return:
true if a variable is already defined


createLocalLabel

public Label createLocalLabel(String name)
creates a new named label


defineTemporaryVariable

public int defineTemporaryVariable(def var, boolean store)
creates a temporary variable.
param:
var defines type and name
param:
store defines if the toplevel argument of the stack should be stored
return:
the index used for this temporary variable


defineTemporaryVariable

public int defineTemporaryVariable(String name, boolean store)
creates a temporary variable.
param:
name defines type and name
param:
store defines if the toplevel argument of the stack should be stored
return:
the index used for this temporary variable


defineTemporaryVariable

public int defineTemporaryVariable(String name, ClassNode node, boolean store)
creates a temporary variable.
param:
name defines the name
param:
node defines the node
param:
store defines if the toplevel argument of the stack should be stored
return:
the index used for this temporary variable


defineVariable

public Variable defineVariable(def v, boolean initFromStack)
Defines a new Variable using an AST variable.
param:
initFromStack if true the last element of the stack will be used to initilize the new variable. If false null will be used.


getBreakLabel

public Label getBreakLabel()


getContinueLabel

public Label getContinueLabel()


getLabel

public Label getLabel(String name)
Returns the label for the given name


getNamedBreakLabel

protected Label getNamedBreakLabel(String name)
Used for break foo inside 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

protected Label getNamedContinueLabel(String name)
Used for continue foo inside 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.


getScope

public VariableScope getScope()


getVariable

public Variable getVariable(String variableName)


getVariable

public Variable getVariable(String variableName, boolean mustExist)
Returns a normal variable.

If mustExist is 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.

param:
variableName name of the variable
param:
mustExist throw exception if variable does not exist
return:
the normal variable or null if not found (and mustExist not true)


hasFinallyBlocks

public boolean hasFinallyBlocks()


init

protected void init(VariableScope el, Parameter[] parameters, MethodVisitor mv, ClassNode cn)
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().


pop

public void pop()


popFinallyBlock

public void popFinallyBlock()


popFinallyBlockVisit

public void popFinallyBlockVisit(Runnable block)


pushBooleanExpression

protected void pushBooleanExpression()
because a boolean Expression may not be evaluated completly it is important to keep the registers clean


pushFinallyBlock

public void pushFinallyBlock(Runnable block)


pushFinallyBlockVisit

public void pushFinallyBlockVisit(Runnable block)


pushLoop

protected void pushLoop(VariableScope el, String labelName)
Should be called when decending 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


pushLoop

protected void pushLoop(String labelName)
Should be called when decending into a loop that does not define a scope. Creates a element for the state stack so pop has to be called later


pushState

protected void pushState()


pushSwitch

protected Label pushSwitch()
Creates a new break label and a element for the state stack so pop has to be called later


pushVariableScope

protected void pushVariableScope(VariableScope el)
Causes the statestack to add an element and sets the given scope as new current variable scope. Creates a element for the state stack so pop has to be called later


removeVar

public void removeVar(int tempIndex)


 

Copyright © 2003-2009 The Codehaus. All rights reserved.