implemented PCLOC annotation.
[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;  // flag to indicate if this is a static block
23   protected boolean isinvokedbystatic;  // flag to indicate if this method is invoked by some static block
24
25   protected boolean isdefaultconstructor; // flag to indicate if this is a default constructor
26
27   public MethodDescriptor(Modifiers m, TypeDescriptor rt, String identifier) {
28     super(identifier);
29     this.modifier=m;
30     this.returntype=rt;
31     this.identifier=identifier;
32     this.safename = "___" + name + "___";
33     this.uniqueid=count++;
34     params=new Vector();
35     paramtable=new SymbolTable();
36     thisvd=null;
37     isstaticblock = false;
38     this.isinvokedbystatic = false;
39     this.isdefaultconstructor = false;
40   }
41
42   public Modifiers getModifiers() {
43     return modifier;
44   }
45
46   public boolean matches(MethodDescriptor md) {
47     /* Check the name */
48     if (!identifier.equals(md.identifier))
49       return false;
50     if (numParameters()!=md.numParameters())
51       return false;
52     for(int i=0; i<numParameters(); i++) {
53       Descriptor d1=getParameter(i);
54       Descriptor d2=md.getParameter(i);
55       TypeDescriptor td1=(d1 instanceof TagVarDescriptor)?((TagVarDescriptor)d1).getType():((VarDescriptor)d1).getType();
56       TypeDescriptor td2=(d2 instanceof TagVarDescriptor)?((TagVarDescriptor)d2).getType():((VarDescriptor)d2).getType();
57       if (!td1.equals(td2))
58         return false;
59     }
60     return true;
61   }
62
63   public MethodDescriptor(Modifiers m, String identifier) {
64     this(m, identifier, false);
65   }
66
67   public MethodDescriptor(Modifiers m, String identifier, boolean isglobal) {
68     super(identifier);
69     this.isglobal=isglobal;
70     this.modifier=m;
71     this.returntype=null;
72     this.identifier=identifier;
73     this.safename = "___" + name + "___";
74     this.uniqueid=count++;
75     params=new Vector();
76     paramtable=new SymbolTable();
77     thisvd=null;
78     isstaticblock = false;
79   }
80
81
82   public boolean isGlobal() {
83     return isglobal;
84   }
85
86   public boolean isStaticBlock() {
87     return isstaticblock;
88   }
89
90   public void setAsStaticBlock() {
91     isstaticblock = true;
92   }
93
94   public boolean isInvokedByStatic() {
95     return this.isinvokedbystatic;
96   }
97
98   public void setIsInvokedByStatic(boolean isinvokedbystatic) {
99     this.isinvokedbystatic = isinvokedbystatic;
100   }
101
102   public void setThis(VarDescriptor vd) {
103     thisvd=vd;
104     paramtable.add(vd);
105   }
106
107   public VarDescriptor getThis() {
108     return thisvd;
109   }
110
111   public String getClassMethodName() {
112     return cd+"."+name;
113   }
114
115   public String getSafeMethodDescriptor() {
116     String st="";
117     for(int i=0; i<numParameters(); i++) {
118       st+=getParamType(i).getSafeDescriptor();
119       if ((i+1)<numParameters())
120         st+="_";
121     }
122     return st;
123   }
124
125   public boolean isStatic() {
126     return modifier.isStatic();
127   }
128
129   public boolean isAbstract() {
130     return modifier.isAbstract();
131   }
132
133   public boolean isConstructor() {
134     return (returntype==null) && !isstaticblock;
135   }
136
137   public TypeDescriptor getReturnType() {
138     return returntype;
139   }
140
141   public void setClassDesc(ClassDescriptor cd) {
142     this.cd=cd;
143   }
144
145   public ClassDescriptor getClassDesc() {
146     return cd;
147   }
148
149   public SymbolTable getParameterTable() {
150     return paramtable;
151   }
152
153   public void addParameter(TypeDescriptor type, String paramname) {
154     if (paramname.equals("this"))
155       throw new Error("Can't have parameter named this");
156     VarDescriptor vd=new VarDescriptor(type, paramname);
157
158     params.add(vd);
159     if (paramtable.getFromSameScope(paramname)!=null) {
160       throw new Error("Parameter "+paramname+" already defined");
161     }
162     paramtable.add(vd);
163   }
164
165   public void addTagParameter(TypeDescriptor type, String paramname) {
166     if (paramname.equals("this"))
167       throw new Error("Can't have parameter named this");
168     TagVarDescriptor vd=new TagVarDescriptor(null, paramname);
169
170     params.add(vd);
171     if (paramtable.getFromSameScope(paramname)!=null) {
172       throw new Error("Parameter "+paramname+" already defined");
173     }
174     paramtable.add(vd);
175   }
176
177   public int numParameters() {
178     return params.size();
179   }
180
181   public Descriptor getParameter(int i) {
182     return (Descriptor) params.get(i);
183   }
184
185   public String getParamName(int i) {
186     return ((Descriptor)params.get(i)).getSymbol();
187   }
188
189   public TypeDescriptor getParamType(int i) {
190     Descriptor d=(Descriptor)params.get(i);
191     if (d instanceof VarDescriptor)
192       return ((VarDescriptor)params.get(i)).getType();
193     else if (d instanceof TagVarDescriptor)
194       return new TypeDescriptor(TypeDescriptor.TAG);
195     else throw new Error();
196   }
197
198   public String getSafeSymbol() {
199     return safename.replace(".","___________").replace("$","___DOLLAR___");
200   }
201
202   public String toString() {
203     String st="";
204     String type="";
205     if (cd!=null)
206       type=cd+".";
207     if (returntype!=null)
208       st=modifier.toString()+returntype.toString()+" "+type+identifier+"(";
209     else
210       st=modifier.toString()+" "+type+identifier+"(";
211     for(int i=0; i<params.size(); i++) {
212       st+=getParamType(i)+" "+getParamName(i);
213       if ((i+1)!=params.size())
214         st+=", ";
215     }
216     st+=")";
217     return st;
218   }
219
220   public boolean isDefaultConstructor() {
221     return this.isdefaultconstructor;
222   }
223
224   public void setDefaultConstructor() {
225     this.isdefaultconstructor = true;
226   }
227 }