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 public MethodDescriptor(Modifiers m, TypeDescriptor rt, String identifier) {
29 this.identifier=identifier;
30 this.safename = "___" + name + "___";
31 this.uniqueid=count++;
33 paramtable=new SymbolTable();
35 isstaticblock = false;
38 public Modifiers getModifiers() {
42 public boolean matches(MethodDescriptor md) {
44 if (!identifier.equals(md.identifier))
46 if (numParameters()!=md.numParameters())
48 for(int i=0; i<numParameters(); i++) {
49 Descriptor d1=getParameter(i);
50 Descriptor d2=md.getParameter(i);
51 TypeDescriptor td1=(d1 instanceof TagVarDescriptor) ? ((TagVarDescriptor)d1).getType() : ((VarDescriptor)d1).getType();
52 TypeDescriptor td2=(d2 instanceof TagVarDescriptor) ? ((TagVarDescriptor)d2).getType() : ((VarDescriptor)d2).getType();
59 public MethodDescriptor(Modifiers m, String identifier) {
60 this(m, identifier, false);
63 public MethodDescriptor(Modifiers m, String identifier, boolean isglobal) {
65 this.isglobal=isglobal;
68 this.identifier=identifier;
69 this.safename = "___" + name + "___";
70 this.uniqueid=count++;
72 paramtable=new SymbolTable();
74 isstaticblock = false;
78 public boolean isGlobal() {
82 public boolean isStaticBlock() {
86 public void setAsStaticBlock() {
90 public boolean isInvokedByStatic() {
91 return this.isinvokedbystatic;
94 public void setIsInvokedByStatic(boolean isinvokedbystatic) {
95 this.isinvokedbystatic = isinvokedbystatic;
98 public void setThis(VarDescriptor vd) {
103 public VarDescriptor getThis() {
107 public String getClassMethodName() {
111 public String getSafeMethodDescriptor() {
113 for(int i=0; i<numParameters(); i++) {
114 st+=getParamType(i).getSafeDescriptor();
115 if ((i+1)<numParameters())
121 public boolean isStatic() {
122 return modifier.isStatic();
125 public boolean isAbstract() {
126 return modifier.isAbstract();
129 public boolean isConstructor() {
130 return (returntype==null);
133 public TypeDescriptor getReturnType() {
137 public void setClassDesc(ClassDescriptor cd) {
141 public ClassDescriptor getClassDesc() {
145 public SymbolTable getParameterTable() {
149 public void addParameter(TypeDescriptor type, String paramname) {
150 if (paramname.equals("this"))
151 throw new Error("Can't have parameter named this");
152 VarDescriptor vd=new VarDescriptor(type, paramname);
155 if (paramtable.getFromSameScope(paramname)!=null) {
156 throw new Error("Parameter "+paramname+" already defined");
161 public void addTagParameter(TypeDescriptor type, String paramname) {
162 if (paramname.equals("this"))
163 throw new Error("Can't have parameter named this");
164 TagVarDescriptor vd=new TagVarDescriptor(null, paramname);
167 if (paramtable.getFromSameScope(paramname)!=null) {
168 throw new Error("Parameter "+paramname+" already defined");
173 public int numParameters() {
174 return params.size();
177 public Descriptor getParameter(int i) {
178 return (Descriptor) params.get(i);
181 public String getParamName(int i) {
182 return ((Descriptor)params.get(i)).getSymbol();
185 public TypeDescriptor getParamType(int i) {
186 Descriptor d=(Descriptor)params.get(i);
187 if (d instanceof VarDescriptor)
188 return ((VarDescriptor)params.get(i)).getType();
189 else if (d instanceof TagVarDescriptor)
190 return new TypeDescriptor(TypeDescriptor.TAG);
191 else throw new Error();
194 public String toString() {
199 if (returntype!=null)
200 st=modifier.toString()+returntype.toString()+" "+type+identifier+"(";
202 st=modifier.toString()+" "+type+identifier+"(";
203 for(int i=0; i<params.size(); i++) {
204 st+=getParamType(i)+" "+getParamName(i);
205 if ((i+1)!=params.size())