2 import IR.Tree.Modifiers;
3 import IR.Tree.ExpressionNode;
4 import java.util.Vector;
9 * represents a symbol in the language (var name, function name, etc).
12 public class MethodDescriptor extends Descriptor {
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
25 protected boolean isdefaultconstructor; // flag to indicate if this is a default constructor
27 public MethodDescriptor(Modifiers m, TypeDescriptor rt, String identifier) {
31 this.identifier=identifier;
32 this.safename = "___" + name + "___";
33 this.uniqueid=count++;
35 paramtable=new SymbolTable();
37 isstaticblock = false;
38 this.isinvokedbystatic = false;
39 this.isdefaultconstructor = false;
42 public Modifiers getModifiers() {
46 public boolean matches(MethodDescriptor md) {
48 if (!identifier.equals(md.identifier))
50 if (numParameters()!=md.numParameters())
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();
63 public MethodDescriptor(Modifiers m, String identifier) {
64 this(m, identifier, false);
67 public MethodDescriptor(Modifiers m, String identifier, boolean isglobal) {
69 this.isglobal=isglobal;
72 this.identifier=identifier;
73 this.safename = "___" + name + "___";
74 this.uniqueid=count++;
76 paramtable=new SymbolTable();
78 isstaticblock = false;
82 public boolean isGlobal() {
86 public boolean isStaticBlock() {
90 public void setAsStaticBlock() {
94 public boolean isInvokedByStatic() {
95 return this.isinvokedbystatic;
98 public void setIsInvokedByStatic(boolean isinvokedbystatic) {
99 this.isinvokedbystatic = isinvokedbystatic;
102 public void setThis(VarDescriptor vd) {
107 public VarDescriptor getThis() {
111 public String getClassMethodName() {
115 public String getSafeMethodDescriptor() {
117 for(int i=0; i<numParameters(); i++) {
118 st+=getParamType(i).getSafeDescriptor();
119 if ((i+1)<numParameters())
125 public boolean isStatic() {
126 return modifier.isStatic();
129 public boolean isAbstract() {
130 return modifier.isAbstract();
133 public boolean isConstructor() {
134 return (returntype==null) && !isstaticblock;
137 public TypeDescriptor getReturnType() {
141 public void setClassDesc(ClassDescriptor cd) {
145 public ClassDescriptor getClassDesc() {
149 public SymbolTable getParameterTable() {
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);
159 if (paramtable.getFromSameScope(paramname)!=null) {
160 throw new Error("Parameter "+paramname+" already defined");
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);
171 if (paramtable.getFromSameScope(paramname)!=null) {
172 throw new Error("Parameter "+paramname+" already defined");
177 public int numParameters() {
178 return params.size();
181 public Descriptor getParameter(int i) {
182 return (Descriptor) params.get(i);
185 public String getParamName(int i) {
186 return ((Descriptor)params.get(i)).getSymbol();
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();
198 public String toString() {
203 if (returntype!=null)
204 st=modifier.toString()+returntype.toString()+" "+type+identifier+"(";
206 st=modifier.toString()+" "+type+identifier+"(";
207 for(int i=0; i<params.size(); i++) {
208 st+=getParamType(i)+" "+getParamName(i);
209 if ((i+1)!=params.size())
216 public boolean isDefaultConstructor() {
217 return this.isdefaultconstructor;
220 public void setDefaultConstructor() {
221 this.isdefaultconstructor = true;