001: package dk.brics.soot.intermediate.representation;
002:
003: import java.util.*;
004:
005: import dk.brics.soot.intermediate.representation.Variable.Type;
006:
007: /** Superclass of all statements.
008: * <p>
009: * A statements belongs to the body of some method.
010: * It has control flow edges to and from other statements.
011: */
012: public abstract class Statement {
013: private Collection<Statement> succs;
014: private Collection<Statement> preds;
015: /* The method whose body contains this statement. */
016: protected Method method;
017: protected int index;
018:
019: /*
020: * The target variable for assignment statements
021: */
022: protected Variable assignmentTarget;
023:
024: public Statement() {
025: succs = new LinkedList<Statement>();
026: preds = new LinkedList<Statement>();
027: this .assignmentTarget = new Variable(Type.OTHER);
028: }
029:
030: /** Adds a control flow edge from this statement to the given.
031: * @param s the target statement of the edge.
032: */
033: public void addSucc(Statement s) {
034: succs.add(s);
035: s.addPred(this );
036: }
037:
038: void addPred(Statement s) {
039: preds.add(s);
040: }
041:
042: /** Returns all targets of control flow edges
043: * originating from this node.
044: * @return a collection of {@link dk.brics.soot.intermediate.Statement} objects.
045: */
046: public Collection<Statement> getSuccs() {
047: return succs;
048: }
049:
050: /** Returns all origins of control flow edges
051: * going to this node.
052: * @return a collection of {@link dk.brics.soot.intermediate.Statement} objects.
053: */
054: public Collection<Statement> getPreds() {
055: return preds;
056: }
057:
058: /**
059: * Set the method whose body contains this statement.
060: */
061: void setMethod(Method m) {
062: method = m;
063: }
064:
065: /**
066: * Returns the method whose body contains this statement.
067: * @return the method.
068: */
069: public Method getMethod() {
070: return method;
071: }
072:
073: void setIndex(int index) {
074: this .index = index;
075: }
076:
077: /** Returns the index of this statement, indicating the sequence
078: * number in which the statement was added to its method.
079: * @return the index.
080: */
081: public int getIndex() {
082: return index;
083: }
084:
085: /** Returns a string representation of this statement.
086: * This is handled by a {@link dk.brics.soot.intermediate.ToStringVisitor}.
087: * @return the statement as a string.
088: */
089: public String toString() {
090: ToStringVisitor tsv = new ToStringVisitor();
091: return process(tsv);
092: }
093:
094: /**
095: * Visit this statement by the given statement visitor.
096: * This will invoke the corresponding method in the visitor.
097: * @param <T> return type
098: * @param v Entry processor
099: * @return result from processor
100: */
101: abstract public <T> T process(StatementProcessor<T> v);
102:
103: public Variable getAssignmentTarget() {
104: return assignmentTarget;
105: }
106:
107: public void setAssignmentTarget(Variable assignmentTarget) {
108: this.assignmentTarget = assignmentTarget;
109: }
110: }
|