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;
24 public MethodDescriptor(Modifiers m, TypeDescriptor rt, String identifier) {
28 this.identifier=identifier;
29 this.safename = "___" + name + "___";
30 this.uniqueid=count++;
32 paramtable=new SymbolTable();
34 isstaticblock = false;
37 public Modifiers getModifiers() {
41 public boolean matches(MethodDescriptor md) {
43 if (!identifier.equals(md.identifier))
45 if (numParameters()!=md.numParameters())
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();
58 public MethodDescriptor(Modifiers m, String identifier) {
59 this(m, identifier, false);
62 public MethodDescriptor(Modifiers m, String identifier, boolean isglobal) {
64 this.isglobal=isglobal;
67 this.identifier=identifier;
68 this.safename = "___" + name + "___";
69 this.uniqueid=count++;
71 paramtable=new SymbolTable();
73 isstaticblock = false;
77 public boolean isGlobal() {
81 public boolean isStaticBlock() {
85 public void setAsStaticBlock() {
89 public void setThis(VarDescriptor vd) {
94 public VarDescriptor getThis() {
98 public String getClassMethodName() {
102 public String getSafeMethodDescriptor() {
104 for(int i=0; i<numParameters(); i++) {
105 st+=getParamType(i).getSafeDescriptor();
106 if ((i+1)<numParameters())
112 public boolean isStatic() {
113 return modifier.isStatic();
116 public boolean isConstructor() {
117 return (returntype==null);
120 public TypeDescriptor getReturnType() {
124 public void setClassDesc(ClassDescriptor cd) {
128 public ClassDescriptor getClassDesc() {
132 public SymbolTable getParameterTable() {
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);
142 if (paramtable.getFromSameScope(paramname)!=null) {
143 throw new Error("Parameter "+paramname+" already defined");
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);
154 if (paramtable.getFromSameScope(paramname)!=null) {
155 throw new Error("Parameter "+paramname+" already defined");
160 public int numParameters() {
161 return params.size();
164 public Descriptor getParameter(int i) {
165 return (Descriptor) params.get(i);
168 public String getParamName(int i) {
169 return ((Descriptor)params.get(i)).getSymbol();
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();
181 public String toString() {
186 if (returntype!=null)
187 st=modifier.toString()+returntype.toString()+" "+type+identifier+"(";
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())