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;
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) {
57 this(m, identifier, false);
60 public MethodDescriptor(Modifiers m, String identifier, boolean isglobal) {
62 this.isglobal=isglobal;
65 this.identifier=identifier;
66 this.safename = "___" + name + "___";
67 this.uniqueid=count++;
69 paramtable=new SymbolTable();
74 public boolean isGlobal() {
78 public void setThis(VarDescriptor vd) {
83 public VarDescriptor getThis() {
87 public String getSafeMethodDescriptor() {
89 for(int i=0;i<numParameters();i++) {
90 st+=getParamType(i).getSafeDescriptor();
91 if ((i+1)<numParameters())
97 public boolean isStatic() {
98 return modifier.isStatic();
101 public boolean isConstructor() {
102 return (returntype==null);
105 public TypeDescriptor getReturnType() {
109 public void setClassDesc(ClassDescriptor cd) {
113 public ClassDescriptor getClassDesc() {
117 public SymbolTable getParameterTable() {
121 public void addParameter(TypeDescriptor type, String paramname) {
122 if (paramname.equals("this"))
123 throw new Error("Can't have parameter named this");
124 VarDescriptor vd=new VarDescriptor(type, paramname);
127 if (paramtable.getFromSameScope(paramname)!=null) {
128 throw new Error("Parameter "+paramname+" already defined");
133 public void addTagParameter(TypeDescriptor type, String paramname) {
134 if (paramname.equals("this"))
135 throw new Error("Can't have parameter named this");
136 TagVarDescriptor vd=new TagVarDescriptor(null, paramname);
139 if (paramtable.getFromSameScope(paramname)!=null) {
140 throw new Error("Parameter "+paramname+" already defined");
145 public int numParameters() {
146 return params.size();
149 public Descriptor getParameter(int i) {
150 return (Descriptor) params.get(i);
153 public String getParamName(int i) {
154 return ((Descriptor)params.get(i)).getSymbol();
157 public TypeDescriptor getParamType(int i) {
158 Descriptor d=(Descriptor)params.get(i);
159 if (d instanceof VarDescriptor)
160 return ((VarDescriptor)params.get(i)).getType();
161 else if (d instanceof TagVarDescriptor)
162 return new TypeDescriptor(TypeDescriptor.TAG);
163 else throw new Error();
166 public String toString() {
171 if (returntype!=null)
172 st=modifier.toString()+returntype.toString()+" "+type+identifier+"(";
174 st=modifier.toString()+" "+type+identifier+"(";
175 for(int i=0;i<params.size();i++) {
176 st+=getParamType(i)+" "+getParamName(i);
177 if ((i+1)!=params.size())