Implement a more sorphisticated execution strategy for static blocks. Tested with...
[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   public ClassDescriptor(String classname) {
24     this("", classname);
25   }
26
27   public ClassDescriptor(String packagename, String classname) {
28     super(classname);
29     superclass=null;
30     flags=new SymbolTable();
31     fields=new SymbolTable();
32     fieldvec=new Vector();
33     methods=new SymbolTable();
34     classid=UIDCount++;
35     this.packagename=packagename;
36   }
37
38   public int getId() {
39     return classid;
40   }
41
42   public Iterator getMethods() {
43     return methods.getDescriptorsIterator();
44   }
45
46   public Iterator getFields() {
47     return fields.getDescriptorsIterator();
48   }
49
50   public Iterator getFlags() {
51     return flags.getDescriptorsIterator();
52   }
53
54   public SymbolTable getFieldTable() {
55     return fields;
56   }
57
58   public Vector getFieldVec() {
59     return fieldvec;
60   }
61
62   public SymbolTable getFlagTable() {
63     return flags;
64   }
65
66   public SymbolTable getMethodTable() {
67     return methods;
68   }
69
70   public String getSafeDescriptor() {
71     return "L"+safename.replace('.','/');
72   }
73
74   public String printTree(State state) {
75     int indent;
76     String st=modifiers.toString()+"class "+getSymbol();
77     if (superclass!=null)
78       st+="extends "+superclass.toString();
79     st+=" {\n";
80     indent=TreeNode.INDENT;
81     boolean printcr=false;
82
83     for(Iterator it=getFlags(); it.hasNext();) {
84       FlagDescriptor fd=(FlagDescriptor)it.next();
85       st+=TreeNode.printSpace(indent)+fd.toString()+"\n";
86       printcr=true;
87     }
88     if (printcr)
89       st+="\n";
90
91     printcr=false;
92
93     for(Iterator it=getFields(); it.hasNext();) {
94       FieldDescriptor fd=(FieldDescriptor)it.next();
95       st+=TreeNode.printSpace(indent)+fd.toString()+"\n";
96       printcr=true;
97     }
98     if (printcr)
99       st+="\n";
100
101     for(Iterator it=getMethods(); it.hasNext();) {
102       MethodDescriptor md=(MethodDescriptor)it.next();
103       st+=TreeNode.printSpace(indent)+md.toString()+" ";
104       BlockNode bn=state.getMethodBody(md);
105       st+=bn.printNode(indent)+"\n\n";
106     }
107     st+="}\n";
108     return st;
109   }
110
111   public void addFlag(FlagDescriptor fd) {
112     if (flags.contains(fd.getSymbol()))
113       throw new Error(fd.getSymbol()+" already defined");
114     hasFlags=true;
115     flags.add(fd);
116   }
117
118   public boolean hasFlags() {
119     return hasFlags||getSuperDesc()!=null&&getSuperDesc().hasFlags();
120   }
121
122   public void addField(FieldDescriptor fd) {
123     if (fields.contains(fd.getSymbol()))
124       throw new Error(fd.getSymbol()+" already defined");
125     fields.add(fd);
126     fieldvec.add(fd);
127     if(fd.isStatic()) {
128       this.incStaticFields();
129     }
130   }
131
132   public void addMethod(MethodDescriptor md) {
133     methods.add(md);
134   }
135
136   public void setModifiers(Modifiers modifiers) {
137     this.modifiers=modifiers;
138   }
139
140   public void setSuper(String superclass) {
141     this.superclass=superclass;
142   }
143
144   public ClassDescriptor getSuperDesc() {
145     return superdesc;
146   }
147
148   public void setSuper(ClassDescriptor scd) {
149     this.superdesc=scd;
150   }
151
152   public String getSuper() {
153     return superclass;
154   }
155   
156   public void incStaticBlocks() {
157     this.numstaticblocks++;
158   }
159   
160   public int getNumStaticBlocks() {
161     return this.numstaticblocks;
162   }
163   
164   public void incStaticFields() {
165     this.numstaticfields++;
166   }
167   
168   public int getNumStaticFields() {
169     return this.numstaticfields;
170   }
171 }