edu.uci.eecs.doc.cascade.controlflow
Class IfThenElse

java.lang.Object
  extended by edu.uci.eecs.doc.cascade.controlflow.Node
      extended by edu.uci.eecs.doc.cascade.controlflow.IfThenElse
All Implemented Interfaces:
Iterable<Node>

public class IfThenElse
extends Node

Represents if-then and if-then-else constructs in control flow graphs and trees.

Author:
Trevor Harmon

Field Summary
protected  Goto gotoNode
           
 
Fields inherited from class edu.uci.eecs.doc.cascade.controlflow.Node
block, nextNode, parent, scope, tree
 
Constructor Summary
IfThenElse(Tree tree, int scope, Node parent, IfThenElseBlock ifThenElseBlock)
          Creates a new if-then-else object.
 
Method Summary
protected  Expression getCondition()
          Returns the conditional expression of the if-then-else construct (e.g., i<10)
 Node getElse()
          Returns a control flow node representing the "else" part of the if-then-else construct.
 Goto getGoto()
          Returns a Goto node representing the goto instruction that jumps over the "else" part in if-then-else constructs.
protected  InstructionHandle getLastInstruction()
           
 List<MethodInvocation> getMethodInvocations()
          Returns the methods that the if's conditional expression invokes in the order in which they are invoked.
 Node getThen()
          Returns a control flow node representing the "then" part of the if-then-else construct.
 boolean hasElse()
          Returns true if this is an if-then-else branch; returns false if this is an if-then branch.
 void setElse(Node elseNode, List<InstructionHandle> instructions)
          Sets the "else" part of the if-then-else construct
 void setThen(Node thenNode)
          Sets the "then" part of the if-then-else construct.
 String toString()
          Converts the if-then-else construct to a string.
 
Methods inherited from class edu.uci.eecs.doc.cascade.controlflow.Node
getBlock, getEnclosingLoop, getFirstInstruction, getInstructions, getMethodInvocations, getNext, getOutermostLoop, getParent, getScope, getSourceCodeLineNumber, getStartAddress, getTree, iterator, lastNode, setNext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

gotoNode

protected Goto gotoNode
Constructor Detail

IfThenElse

public IfThenElse(Tree tree,
                  int scope,
                  Node parent,
                  IfThenElseBlock ifThenElseBlock)
Creates a new if-then-else object.

Parameters:
tree - the control flow tree to which this node belongs
scope - the lexical scope of the node (0 = top-level scope)
parent - the node's parent (null if the node has no parent)
ifThenElseBlock - a JODE object representing the if-then-else construct
Method Detail

hasElse

public boolean hasElse()
Returns true if this is an if-then-else branch; returns false if this is an if-then branch.

Returns:
true if and only if the construct has an "else" part

getMethodInvocations

public List<MethodInvocation> getMethodInvocations()
Returns the methods that the if's conditional expression invokes in the order in which they are invoked. (Methods invoked by the bodies of the if and else branches are not included.) If the same method is invoked more than once, it will appear in the list more than once.

Overrides:
getMethodInvocations in class Node
Returns:
a list of Java method handles, or an empty list if the expression does not invoke any methods

getThen

public Node getThen()
Returns a control flow node representing the "then" part of the if-then-else construct. If the body of the "then" part is empty, this method returns null.

Returns:
a control flow node or null if the "then" part is empty

setThen

public void setThen(Node thenNode)
Sets the "then" part of the if-then-else construct.

Parameters:
thenNode - a node representing a "then" block, or null if the "then" block is empty

getElse

public Node getElse()
Returns a control flow node representing the "else" part of the if-then-else construct. If this is an if-then construct instead of an if-then-else construct, this method returns null.

Returns:
a control flow node or null if hasElse() is false

setElse

public void setElse(Node elseNode,
                    List<InstructionHandle> instructions)
Sets the "else" part of the if-then-else construct

Parameters:
elseNode - a node representing a "then" block, or null if the statement has no "else" branch
instructions - the list of instructions in the node's method (the whole method, not just the if-then part)

getGoto

public Goto getGoto()
Returns a Goto node representing the goto instruction that jumps over the "else" part in if-then-else constructs. If this is an if-then construct instead of an if-then-else construct, this method returns null. This method may also return null if the body of the if statement ends with an if statement which in turn ends with a return statement. In such a case, the compiler will have used the destination of the branch to take the place of a goto instruction. For example:
 if (o == null)
 {
   if (p == null)
   {
     return true;
   }
 }
 else if (o.equals(p))
 {
   return true;
 }
 
In this example, there are no goto instructions anywhere, so it doesn't make sense to have a Goto node.

Returns:
a Goto node or null if hasElse() is false or if the else node has not yet been set

getCondition

protected Expression getCondition()
Returns the conditional expression of the if-then-else construct (e.g., i<10)

Returns:
a JODE object representing an expression

getLastInstruction

protected InstructionHandle getLastInstruction()
Overrides:
getLastInstruction in class Node

toString

public String toString()
Converts the if-then-else construct to a string.

Overrides:
toString in class Object
Returns:
a source-code-like string representing the if-then-else construct