Add support for 'static' fields in multicore gc version w/o tasks. Now we can have...
[IRC.git] / Robust / src / IR / MethodDescriptor.java
1 package IR;
2 import IR.Tree.Modifiers;
3 import IR.Tree.ExpressionNode;
4 import java.util.Vector;
5
6 /**
7  * Descriptor
8  *
9  * represents a symbol in the language (var name, function name, etc).
10  */
11
12 public class MethodDescriptor extends Descriptor {
13
14   protected Modifiers modifier;
15   protected TypeDescriptor returntype;
16   protected String identifier;
17   protected Vector params;
18   protected SymbolTable paramtable;
19   protected ClassDescriptor cd;
20   protected VarDescriptor thisvd;
21   protected boolean isglobal;
22   protected boolean isstaticblock;
23
24   public MethodDescriptor(Modifiers m, TypeDescriptor rt, String identifier) {
25     super(identifier);
26     this.modifier=m;
27     this.returntype=rt;
28     this.identifier=identifier;
29     this.safename = "___" + name + "___";
30     this.uniqueid=count++;
31     params=new Vector();
32     paramtable=new SymbolTable();
33     thisvd=null;
34     isstaticblock = false;
35   }
36
37   public Modifiers getModifiers() {
38     return modifier;
39   }
40
41   public boolean matches(MethodDescriptor md) {
42     /* Check the name */
43     if (!identifier.equals(md.identifier))
44       return false;
45     if (numParameters()!=md.numParameters())
46       return false;
47     for(int i=0; i<numParameters(); i++) {
48       Descriptor d1=getParameter(i);
49       Descriptor d2=md.getParameter(i);
50       TypeDescriptor td1=(d1 instanceof TagVarDescriptor) ? ((TagVarDescriptor)d1).getType() : ((VarDescriptor)d1).getType();
51       TypeDescriptor td2=(d2 instanceof TagVarDescriptor) ? ((TagVarDescriptor)d2).getType() : ((VarDescriptor)d2).getType();
52       if (!td1.equals(td2))
53         return false;
54     }
55     return true;
56   }
57
58   public MethodDescriptor(Modifiers m, String identifier) {
59     this(m, identifier, false);
60   }
61
62   public MethodDescriptor(Modifiers m, String identifier, boolean isglobal) {
63     super(identifier);
64     this.isglobal=isglobal;
65     this.modifier=m;
66     this.returntype=null;
67     this.identifier=identifier;
68     this.safename = "___" + name + "___";
69     this.uniqueid=count++;
70     params=new Vector();
71     paramtable=new SymbolTable();
72     thisvd=null;
73     isstaticblock = false;
74   }
75
76
77   public boolean isGlobal() {
78     return isglobal;
79   }
80   
81   public boolean isStaticBlock() {
82     return isstaticblock;
83   }
84   
85   public void setAsStaticBlock() {
86     isstaticblock = true;
87   }
88
89   public void setThis(VarDescriptor vd) {
90     thisvd=vd;
91     paramtable.add(vd);
92   }
93
94   public VarDescriptor getThis() {
95     return thisvd;
96   }
97
98   public String getClassMethodName() {
99     return cd+"."+name;
100   }
101
102   public String getSafeMethodDescriptor() {
103     String st="";
104     for(int i=0; i<numParameters(); i++) {
105       st+=getParamType(i).getSafeDescriptor();
106       if ((i+1)<numParameters())
107         st+="_";
108     }
109     return st;
110   }
111
112   public boolean isStatic() {
113     return modifier.isStatic();
114   }
115
116   public boolean isConstructor() {
117     return (returntype==null);
118   }
119
120   public TypeDescriptor getReturnType() {
121     return returntype;
122   }
123
124   public void setClassDesc(ClassDescriptor cd) {
125     this.cd=cd;
126   }
127
128   public ClassDescriptor getClassDesc() {
129     return cd;
130   }
131
132   public SymbolTable getParameterTable() {
133     return paramtable;
134   }
135
136   public void addParameter(TypeDescriptor type, String paramname) {
137     if (paramname.equals("this"))
138       throw new Error("Can't have parameter named this");
139     VarDescriptor vd=new VarDescriptor(type, paramname);
140
141     params.add(vd);
142     if (paramtable.getFromSameScope(paramname)!=null) {
143       throw new Error("Parameter "+paramname+" already defined");
144     }
145     paramtable.add(vd);
146   }
147
148   public void addTagParameter(TypeDescriptor type, String paramname) {
149     if (paramname.equals("this"))
150       throw new Error("Can't have parameter named this");
151     TagVarDescriptor vd=new TagVarDescriptor(null, paramname);
152
153     params.add(vd);
154     if (paramtable.getFromSameScope(paramname)!=null) {
155       throw new Error("Parameter "+paramname+" already defined");
156     }
157     paramtable.add(vd);
158   }
159
160   public int numParameters() {
161     return params.size();
162   }
163
164   public Descriptor getParameter(int i) {
165     return (Descriptor) params.get(i);
166   }
167
168   public String getParamName(int i) {
169     return ((Descriptor)params.get(i)).getSymbol();
170   }
171
172   public TypeDescriptor getParamType(int i) {
173     Descriptor d=(Descriptor)params.get(i);
174     if (d instanceof VarDescriptor)
175       return ((VarDescriptor)params.get(i)).getType();
176     else if (d instanceof TagVarDescriptor)
177       return new TypeDescriptor(TypeDescriptor.TAG);
178     else throw new Error();
179   }
180
181   public String toString() {
182     String st="";
183     String type="";
184     if (cd!=null)
185       type=cd+".";
186     if (returntype!=null)
187       st=modifier.toString()+returntype.toString()+" "+type+identifier+"(";
188     else
189       st=modifier.toString()+" "+type+identifier+"(";
190     for(int i=0; i<params.size(); i++) {
191       st+=getParamType(i)+" "+getParamName(i);
192       if ((i+1)!=params.size())
193         st+=", ";
194     }
195     st+=")";
196     return st;
197   }
198 }