IR
[repair.git] / Repair / RepairCompiler / MCC / IR / SymbolTable.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class SymbolTable {
6
7     private Hashtable table;
8     private SymbolTable parent;
9   
10     public SymbolTable() {
11         table = new Hashtable();
12         this.parent = null;
13     }
14
15     public SymbolTable(SymbolTable parent) {
16         table = new Hashtable();
17         this.parent = parent;
18     }
19
20     //public void add(String name, Descriptor d) {
21         //table.put(name, d);
22     //}
23
24     public void add(Descriptor d) {
25         table.put(d.getSymbol(), d);
26     }
27     
28     public void add(String name, Descriptor d) {
29         table.put(name, d);
30         
31     }
32
33     public void dump() {
34         Enumeration e = getDescriptors();
35         while (e.hasMoreElements()) {
36             Descriptor d = (Descriptor) e.nextElement();
37             System.out.println(d.getSymbol());
38         }
39         if (parent != null) {
40             System.out.println("parent:");
41             parent.dump();
42         }
43     }
44     
45     public Descriptor get(String name) {
46         Descriptor d = (Descriptor) table.get(name);
47         if (d == null && parent != null) {
48             return parent.get(name);
49         } else {
50             return d;
51         }
52     }
53
54     public Descriptor getFromSameScope(String name) {
55         return (Descriptor)table.get(name);
56     }
57     
58     public Enumeration getNames() {
59         return table.keys();
60     }
61
62     public Enumeration getDescriptors() {
63         return table.elements();
64     }
65
66     public Iterator descriptors() {
67         return table.values().iterator();
68     }
69
70     public Vector getAllDescriptors() {
71         Vector d;
72         if (parent == null) {
73             d = new Vector();
74         } else {
75             d = parent.getAllDescriptors();
76         }
77
78         Enumeration e = getDescriptors();
79         while(e.hasMoreElements()) {
80             d.addElement(e.nextElement());
81         }
82
83         return d;
84     }
85
86     public boolean contains(String name) {
87         return (get(name) != null);
88     }
89             
90         
91     public int size() {
92         return table.size();
93     }
94
95     public int sizeAll() {
96         if (parent != null) {
97             return parent.sizeAll() + table.size();
98         } else {
99             return table.size();
100         }
101     }
102
103     public SymbolTable getParent() {
104         return parent;
105     }
106     
107     public void setParent(SymbolTable parent) {
108         this.parent = parent;
109     }
110
111     /**
112      * Adds contents of st2.table to this.table and returns a
113      * Vector of shared names, unless there are no shared names,
114      * in which case returns null.
115      */
116     public Vector merge(SymbolTable st2) {
117         Vector v = new Vector();
118         Enumeration names = st2.table.keys();
119
120         while (names.hasMoreElements()) {
121             Object o = names.nextElement();
122
123             if (table.containsKey(o)) {
124                 v.addElement(o);
125             } else {
126                 table.put(o, st2.table.get(o));
127             }
128         }
129
130         if (v.size() == 0) {
131             return null;
132         } else {
133             return v;
134         }
135     }
136
137     public String toString() {
138         return "ST: " + table.toString();               
139     }
140 }