switch to spaces only..
[IRC.git] / Robust / src / IR / Flat / FlatNode.java
1 package IR.Flat;
2 import java.util.Vector;
3 import java.util.HashSet;
4 import java.util.Set;
5 import java.util.Iterator;
6
7 public class FlatNode {
8   public Vector next;
9   protected Vector prev;
10   static int idcounter=0;
11   public final int nodeid;
12   public int numLine=-1;
13
14   public FlatNode() {
15     next=new Vector();
16     prev=new Vector();
17     nodeid=(idcounter++);
18   }
19
20   public String toString() {
21     throw new Error(this.getClass().getName() + "does not implement toString!");
22   }
23   public int numNext() {
24     return next.size();
25   }
26   public FlatNode getNext(int i) {
27     return (FlatNode) next.get(i);
28   }
29
30   public int numPrev() {
31     return prev.size();
32   }
33   public FlatNode getPrev(int i) {
34     return (FlatNode) prev.get(i);
35   }
36   public void addNext(FlatNode n) {
37     next.add(n);
38     n.addPrev(this);
39   }
40
41   public void removeNext(FlatNode n) {
42     next.remove(n);
43   }
44   public void removePrev(FlatNode n) {
45     prev.remove(n);
46   }
47
48   /** This function modifies the graph */
49   public void setNext(int i, FlatNode n) {
50     FlatNode old=getNext(i);
51     next.set(i, n);
52     old.prev.remove(this);
53     n.addPrev(this);
54   }
55   /** This function modifies the graph */
56   public void setNewNext(int i, FlatNode n) {
57     if (next.size()<=i)
58       next.setSize(i+1);
59     next.set(i, n);
60     n.addPrev(this);
61   }
62   /** This function modifies the graph */
63   public void setprev(int i, FlatNode n) {
64     prev.set(i, n);
65   }
66   /** This function modifies the graph */
67   public void setnext(int i, FlatNode n) {
68     next.set(i, n);
69   }
70   public void addPrev(FlatNode p) {
71     prev.add(p);
72   }
73   public int kind() {
74     throw new Error();
75   }
76   public TempDescriptor [] readsTemps() {
77     return new TempDescriptor[0];
78   }
79   public TempDescriptor [] writesTemps() {
80     return new TempDescriptor[0];
81   }
82   public FlatNode clone(TempMap t) {
83     throw new Error("no clone method for"+this);
84   }
85
86   public void rewriteUse(TempMap t) {
87     System.out.println(toString());
88     throw new Error();
89   }
90
91   public void rewriteDef(TempMap t) {
92     System.out.println(toString());
93     throw new Error();
94   }
95
96   public Set<FlatNode> getReachableSet(Set<FlatNode> endset) {
97     HashSet<FlatNode> tovisit=new HashSet<FlatNode>();
98     HashSet<FlatNode> visited=new HashSet<FlatNode>();
99     tovisit.add(this);
100     while(!tovisit.isEmpty()) {
101       FlatNode fn=tovisit.iterator().next();
102       tovisit.remove(fn);
103       visited.add(fn);
104       if (endset!=null&&!endset.contains(fn)) {
105         for(int i=0; i<fn.numNext(); i++) {
106           FlatNode nn=fn.getNext(i);
107           if (!visited.contains(nn))
108             tovisit.add(nn);
109         }
110       }
111     }
112     return visited;
113   }
114
115   public void replace(FlatNode fnnew) {
116     fnnew.prev.setSize(prev.size());
117     fnnew.next.setSize(next.size());
118     for(int i=0; i<prev.size(); i++) {
119       FlatNode nprev=(FlatNode)prev.get(i);
120       fnnew.prev.set(i,nprev);
121       for(int j=0; j<nprev.numNext(); j++) {
122         FlatNode n=nprev.getNext(j);
123         if (n==this)
124           nprev.next.set(j, fnnew);
125       }
126     }
127     for(int i=0; i<next.size(); i++) {
128       FlatNode nnext=(FlatNode)next.get(i);
129       fnnew.next.set(i,nnext);
130       for(int j=0; j<nnext.numPrev(); j++) {
131         FlatNode n=nnext.getPrev(j);
132         if (n==this)
133           nnext.prev.set(j, fnnew);
134       }
135     }
136     next=null;
137     prev=null;
138   }
139
140   public void setNumLine(int lineNum) {
141     this.numLine=lineNum;
142   }
143
144   public int getNumLine() {
145     return this.numLine;
146   }
147 }