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;
23 public MethodDescriptor(Modifiers m, TypeDescriptor rt, String identifier) {
27 this.identifier=identifier;
28 this.safename = "___" + name + "___";
29 this.uniqueid=count++;
31 paramtable=new SymbolTable();
35 public Modifiers getModifiers() {
39 public boolean matches(MethodDescriptor md) {
41 if (!identifier.equals(md.identifier))
43 if (numParameters()!=md.numParameters())
45 for(int i=0;i<numParameters();i++) {
46 Descriptor d1=getParameter(i);
47 Descriptor d2=md.getParameter(i);
48 TypeDescriptor td1=(d1 instanceof TagVarDescriptor)?((TagVarDescriptor)d1).getType():((VarDescriptor)d1).getType();
49 TypeDescriptor td2=(d2 instanceof TagVarDescriptor)?((TagVarDescriptor)d2).getType():((VarDescriptor)d2).getType();
56 public MethodDescriptor(Modifiers m, String identifier) {
60 this.identifier=identifier;
61 this.safename = "___" + name + "___";
62 this.uniqueid=count++;
64 paramtable=new SymbolTable();
68 public void setThis(VarDescriptor vd) {
73 public VarDescriptor getThis() {
77 public String getSafeMethodDescriptor() {
79 for(int i=0;i<numParameters();i++) {
80 st+=getParamType(i).getSafeDescriptor();
81 if ((i+1)<numParameters())
87 public boolean isStatic() {
88 return modifier.isStatic();
91 public boolean isConstructor() {
92 return (returntype==null);
95 public TypeDescriptor getReturnType() {
99 public void setClassDesc(ClassDescriptor cd) {
103 public ClassDescriptor getClassDesc() {
107 public SymbolTable getParameterTable() {
111 public void addParameter(TypeDescriptor type, String paramname) {
112 if (paramname.equals("this"))
113 throw new Error("Can't have parameter named this");
114 VarDescriptor vd=new VarDescriptor(type, paramname);
117 if (paramtable.getFromSameScope(paramname)!=null) {
118 throw new Error("Parameter "+paramname+" already defined");
123 public void addTagParameter(TypeDescriptor type, String paramname) {
124 if (paramname.equals("this"))
125 throw new Error("Can't have parameter named this");
126 TagVarDescriptor vd=new TagVarDescriptor(null, paramname);
129 if (paramtable.getFromSameScope(paramname)!=null) {
130 throw new Error("Parameter "+paramname+" already defined");
135 public int numParameters() {
136 return params.size();
139 public Descriptor getParameter(int i) {
140 return (Descriptor) params.get(i);
143 public String getParamName(int i) {
144 return ((Descriptor)params.get(i)).getSymbol();
147 public TypeDescriptor getParamType(int i) {
148 Descriptor d=(Descriptor)params.get(i);
149 if (d instanceof VarDescriptor)
150 return ((VarDescriptor)params.get(i)).getType();
151 else if (d instanceof TagVarDescriptor)
152 return new TypeDescriptor(TypeDescriptor.TAG);
153 else throw new Error();
156 public String toString() {
161 if (returntype!=null)
162 st=modifier.toString()+returntype.toString()+" "+type+identifier+"(";
164 st=modifier.toString()+" "+type+identifier+"(";
165 for(int i=0;i<params.size();i++) {
166 st+=getParamType(i)+" "+getParamName(i);
167 if ((i+1)!=params.size())