Add support for volatile keyword in mgc version. For Tilera, as we execute a process...
[IRC.git] / Robust / src / IR / ClassDescriptor.java
1 package IR;
2 import java.util.*;
3 import IR.Tree.*;
4
5 public class ClassDescriptor extends Descriptor {
6   private static int UIDCount=1; // start from 1 instead of 0 for multicore gc
7   private final int classid;
8   String superclass;
9   ClassDescriptor superdesc;
10   boolean hasFlags=false;
11   String packagename;
12
13   Modifiers modifiers;
14
15   SymbolTable fields;
16   Vector fieldvec;
17   SymbolTable flags;
18   SymbolTable methods;
19   
20   int numstaticblocks = 0;
21   int numstaticfields = 0;
22   
23   // for interfaces
24   Vector<String> superinterfaces;
25   SymbolTable superIFdesc;
26   
27   // for inner classes
28   boolean isInnerClass=false;
29   
30   // inner classes/enum can have these
31   String surroundingclass=null;
32   ClassDescriptor surroudingdesc=null;
33   SymbolTable innerdescs;
34   
35   // for enum type
36   boolean isEnum = false;
37   SymbolTable enumdescs;
38   HashMap<String, Integer> enumConstantTbl;
39   int enumconstantid = 0;
40
41   public ClassDescriptor(String classname, boolean isInterface) {
42     this("", classname, isInterface);
43   }
44
45   public ClassDescriptor(String packagename, String classname, boolean isInterface) {
46     super(classname);
47     superclass=null;
48     flags=new SymbolTable();
49     fields=new SymbolTable();
50     fieldvec=new Vector();
51     methods=new SymbolTable();
52     if(isInterface) {
53       classid = -2;
54     } else {
55       classid=UIDCount++;
56     }
57     this.packagename=packagename;
58     superinterfaces = new Vector<String>();
59     superIFdesc = new SymbolTable();
60     this.innerdescs = new SymbolTable();
61     this.enumdescs = new SymbolTable();
62   }
63
64   public int getId() {
65     return classid;
66   }
67
68   public Iterator getMethods() {
69     return methods.getDescriptorsIterator();
70   }
71
72   public Iterator getFields() {
73     return fields.getDescriptorsIterator();
74   }
75
76   public Iterator getFlags() {
77     return flags.getDescriptorsIterator();
78   }
79   
80   public Iterator getSuperInterfaces() {
81     return this.superIFdesc.getDescriptorsIterator();
82   }
83
84   public SymbolTable getFieldTable() {
85     return fields;
86   }
87
88   public Vector getFieldVec() {
89     return fieldvec;
90   }
91
92   public SymbolTable getFlagTable() {
93     return flags;
94   }
95
96   public SymbolTable getMethodTable() {
97     return methods;
98   }
99   
100   public SymbolTable getSuperInterfaceTable() {
101     return this.superIFdesc;
102   }
103
104   public String getSafeDescriptor() {
105     return "L"+safename.replace('.','/');
106   }
107
108   public String printTree(State state) {
109     int indent;
110     String st=modifiers.toString()+"class "+getSymbol();
111     if (superclass!=null)
112       st+="extends "+superclass.toString();
113     if(this.superinterfaces != null) {
114       st += "implements ";
115       boolean needcomma = false;
116       for(int i = 0; i < this.superinterfaces.size(); i++) {
117         if(needcomma) {
118           st += ", ";
119         }
120         st += this.superinterfaces.elementAt(i);
121         needcomma = true;
122       }
123     }
124     st+=" {\n";
125     indent=TreeNode.INDENT;
126     boolean printcr=false;
127
128     for(Iterator it=getFlags(); it.hasNext();) {
129       FlagDescriptor fd=(FlagDescriptor)it.next();
130       st+=TreeNode.printSpace(indent)+fd.toString()+"\n";
131       printcr=true;
132     }
133     if (printcr)
134       st+="\n";
135
136     printcr=false;
137
138     for(Iterator it=getFields(); it.hasNext();) {
139       FieldDescriptor fd=(FieldDescriptor)it.next();
140       st+=TreeNode.printSpace(indent)+fd.toString()+"\n";
141       printcr=true;
142     }
143     if (printcr)
144       st+="\n";
145     
146     for(Iterator it=this.getInnerClasses(); it.hasNext();) {
147       ClassDescriptor icd=(ClassDescriptor)it.next();
148       st+=icd.printTree(state)+"\n";
149       printcr=true;
150     }
151     if (printcr)
152       st+="\n";
153     
154     for(Iterator it=this.getEnum(); it.hasNext();) {
155       ClassDescriptor icd = (ClassDescriptor)it.next();
156       st += icd.getModifier().toString() + " enum " + icd.getSymbol() + " {\n  ";
157       Set keys = icd.getEnumConstantTbl().keySet();
158       String[] econstants = new String[keys.size()];
159       Iterator it_keys = keys.iterator();
160       while(it_keys.hasNext()) {
161         String key = (String)it_keys.next();
162         econstants[icd.getEnumConstant(key)] = key;
163       }
164       for(int i = 0; i < econstants.length; i++) {
165         st += econstants[i];
166         if(i < econstants.length-1) {
167           st += ", ";
168         }
169       }
170       st+="\n}\n";
171       printcr=true;
172     }
173     if (printcr)
174       st+="\n";
175
176     for(Iterator it=getMethods(); it.hasNext();) {
177       MethodDescriptor md=(MethodDescriptor)it.next();
178       st+=TreeNode.printSpace(indent)+md.toString()+" ";
179       BlockNode bn=state.getMethodBody(md);
180       st+=bn.printNode(indent)+"\n\n";
181     }
182     st+="}\n";
183     return st;
184   }
185
186   public void addFlag(FlagDescriptor fd) {
187     if (flags.contains(fd.getSymbol()))
188       throw new Error(fd.getSymbol()+" already defined");
189     hasFlags=true;
190     flags.add(fd);
191   }
192
193   public boolean hasFlags() {
194     return hasFlags||getSuperDesc()!=null&&getSuperDesc().hasFlags();
195   }
196
197   public void addField(FieldDescriptor fd) {
198     if (fields.contains(fd.getSymbol()))
199       throw new Error(fd.getSymbol()+" already defined");
200     fields.add(fd);
201     fieldvec.add(fd);
202     if((fd.isStatic()) || (fd.isVolatile())) {
203       this.incStaticFields();
204     }
205   }
206
207   public void addMethod(MethodDescriptor md) {
208     methods.add(md);
209   }
210
211   public void setModifiers(Modifiers modifiers) {
212     this.modifiers=modifiers;
213   }
214
215   public void setSuper(String superclass) {
216     this.superclass=superclass;
217   }
218
219   public ClassDescriptor getSuperDesc() {
220     return superdesc;
221   }
222
223   public void setSuper(ClassDescriptor scd) {
224     this.superdesc=scd;
225   }
226
227   public String getSuper() {
228     return superclass;
229   }
230   
231   public void addSuperInterface(String superif) {
232     this.superinterfaces.addElement(superif);
233   }
234   
235   public Vector<String> getSuperInterface() {
236     return this.superinterfaces;
237   }
238   
239   public void addSuperInterfaces(ClassDescriptor sif) {
240     this.superIFdesc.add(sif);
241   }
242   
243   public void incStaticBlocks() {
244     this.numstaticblocks++;
245   }
246   
247   public int getNumStaticBlocks() {
248     return this.numstaticblocks;
249   }
250   
251   public void incStaticFields() {
252     this.numstaticfields++;
253   }
254   
255   public int getNumStaticFields() {
256     return this.numstaticfields;
257   }
258   
259   public boolean isAbstract() {
260     return this.modifiers.isAbstract();
261   }
262   
263   public boolean isInterface() {
264     return this.classid == -2;
265   }
266   
267   public boolean isStatic() {
268     return this.modifiers.isStatic();
269   }
270   
271   public void setAsInnerClass() {
272     this.isInnerClass = true;
273   }
274   
275   public boolean isInnerClass() {
276     return this.isInnerClass;
277   }
278   
279   public void setSurroundingClass(String sclass) {
280     this.surroundingclass=sclass;
281   }
282
283   public String getSurrounding() {
284     return this.surroundingclass;
285   }
286
287   public ClassDescriptor getSurroundingDesc() {
288     return this.surroudingdesc;
289   }
290
291   public void setSurrounding(ClassDescriptor scd) {
292     this.surroudingdesc=scd;
293   }
294   
295   public void addInnerClass(ClassDescriptor icd) {
296     this.innerdescs.add(icd);
297   }
298   
299   public Iterator getInnerClasses() {
300     return this.innerdescs.getDescriptorsIterator();
301   }
302
303   public SymbolTable getInnerClassTable() {
304     return this.innerdescs;
305   }
306   
307   public void setAsEnum() {
308     this.isEnum = true;
309   }
310   
311   public boolean isEnum() {
312     return this.isEnum;
313   }
314   
315   public void addEnum(ClassDescriptor icd) {
316     this.enumdescs.add(icd);
317   }
318   
319   public Iterator getEnum() {
320     return this.enumdescs.getDescriptorsIterator();
321   }
322
323   public SymbolTable getEnumTable() {
324     return this.enumdescs;
325   }
326   
327   public void addEnumConstant(String econstant) {
328     if(this.enumConstantTbl == null) {
329       this.enumConstantTbl = new HashMap<String, Integer>();
330     }
331     if(this.enumConstantTbl.containsKey(econstant)) {
332       return;
333     } else {
334       this.enumConstantTbl.put(econstant, this.enumconstantid++);
335     }
336     return;
337   }
338   
339   public int getEnumConstant(String econstant) {
340     if(this.enumConstantTbl.containsKey(econstant)) {
341       return this.enumConstantTbl.get(econstant).intValue();
342     } else {
343       return -1;
344     }
345   }
346   
347   public HashMap<String, Integer> getEnumConstantTbl() {
348     return this.enumConstantTbl;
349   }
350   
351   public Modifiers getModifier() {
352     return this.modifiers;
353   }
354 }