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 isConstructor() {
126 return (returntype==null);
129 public TypeDescriptor getReturnType() {
133 public void setClassDesc(ClassDescriptor cd) {
137 public ClassDescriptor getClassDesc() {
141 public SymbolTable getParameterTable() {
145 public void addParameter(TypeDescriptor type, String paramname) {
146 if (paramname.equals("this"))
147 throw new Error("Can't have parameter named this");
148 VarDescriptor vd=new VarDescriptor(type, paramname);
151 if (paramtable.getFromSameScope(paramname)!=null) {
152 throw new Error("Parameter "+paramname+" already defined");
157 public void addTagParameter(TypeDescriptor type, String paramname) {
158 if (paramname.equals("this"))
159 throw new Error("Can't have parameter named this");
160 TagVarDescriptor vd=new TagVarDescriptor(null, paramname);
163 if (paramtable.getFromSameScope(paramname)!=null) {
164 throw new Error("Parameter "+paramname+" already defined");
169 public int numParameters() {
170 return params.size();
173 public Descriptor getParameter(int i) {
174 return (Descriptor) params.get(i);
177 public String getParamName(int i) {
178 return ((Descriptor)params.get(i)).getSymbol();
181 public TypeDescriptor getParamType(int i) {
182 Descriptor d=(Descriptor)params.get(i);
183 if (d instanceof VarDescriptor)
184 return ((VarDescriptor)params.get(i)).getType();
185 else if (d instanceof TagVarDescriptor)
186 return new TypeDescriptor(TypeDescriptor.TAG);
187 else throw new Error();
190 public String toString() {
195 if (returntype!=null)
196 st=modifier.toString()+returntype.toString()+" "+type+identifier+"(";
198 st=modifier.toString()+" "+type+identifier+"(";
199 for(int i=0; i<params.size(); i++) {
200 st+=getParamType(i)+" "+getParamName(i);
201 if ((i+1)!=params.size())