helpful progress reporting
[IRC.git] / Robust / src / IR / MethodDescriptor.java
1 package IR;
2 import IR.Tree.Modifiers;
3 import IR.Tree.ExpressionNode;
4 import java.util.Vector;
5
6 /**
7  * Descriptor
8  *
9  * represents a symbol in the language (var name, function name, etc).
10  */
11
12 public class MethodDescriptor extends Descriptor {
13
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
23   public MethodDescriptor(Modifiers m, TypeDescriptor rt, String identifier) {
24     super(identifier);
25     this.modifier=m;
26     this.returntype=rt;
27     this.identifier=identifier;
28     this.safename = "___" + name + "___";
29     this.uniqueid=count++;
30     params=new Vector();
31     paramtable=new SymbolTable();
32     thisvd=null;
33   }
34
35   public Modifiers getModifiers() {
36     return modifier;
37   }
38
39   public boolean matches(MethodDescriptor md) {
40     /* Check the name */
41     if (!identifier.equals(md.identifier))
42       return false;
43     if (numParameters()!=md.numParameters())
44       return false;
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();
50       if (!td1.equals(td2))
51         return false;
52     }
53     return true;
54   }
55
56   public MethodDescriptor(Modifiers m, String identifier) {
57     this(m, identifier, false);
58   }
59
60   public MethodDescriptor(Modifiers m, String identifier, boolean isglobal) {
61     super(identifier);
62     this.isglobal=isglobal;
63     this.modifier=m;
64     this.returntype=null;
65     this.identifier=identifier;
66     this.safename = "___" + name + "___";
67     this.uniqueid=count++;
68     params=new Vector();
69     paramtable=new SymbolTable();
70     thisvd=null;
71   }
72
73
74   public boolean isGlobal() {
75     return isglobal;
76   }
77
78   public void setThis(VarDescriptor vd) {
79     thisvd=vd;
80     paramtable.add(vd);
81   }
82
83   public VarDescriptor getThis() {
84     return thisvd;
85   }
86
87   public String getClassMethodName() {
88     return cd+"."+name;
89   }
90
91   public String getSafeMethodDescriptor() {
92     String st="";
93     for(int i=0; i<numParameters(); i++) {
94       st+=getParamType(i).getSafeDescriptor();
95       if ((i+1)<numParameters())
96         st+="_";
97     }
98     return st;
99   }
100
101   public boolean isStatic() {
102     return modifier.isStatic();
103   }
104
105   public boolean isConstructor() {
106     return (returntype==null);
107   }
108
109   public TypeDescriptor getReturnType() {
110     return returntype;
111   }
112
113   public void setClassDesc(ClassDescriptor cd) {
114     this.cd=cd;
115   }
116
117   public ClassDescriptor getClassDesc() {
118     return cd;
119   }
120
121   public SymbolTable getParameterTable() {
122     return paramtable;
123   }
124
125   public void addParameter(TypeDescriptor type, String paramname) {
126     if (paramname.equals("this"))
127       throw new Error("Can't have parameter named this");
128     VarDescriptor vd=new VarDescriptor(type, paramname);
129
130     params.add(vd);
131     if (paramtable.getFromSameScope(paramname)!=null) {
132       throw new Error("Parameter "+paramname+" already defined");
133     }
134     paramtable.add(vd);
135   }
136
137   public void addTagParameter(TypeDescriptor type, String paramname) {
138     if (paramname.equals("this"))
139       throw new Error("Can't have parameter named this");
140     TagVarDescriptor vd=new TagVarDescriptor(null, paramname);
141
142     params.add(vd);
143     if (paramtable.getFromSameScope(paramname)!=null) {
144       throw new Error("Parameter "+paramname+" already defined");
145     }
146     paramtable.add(vd);
147   }
148
149   public int numParameters() {
150     return params.size();
151   }
152
153   public Descriptor getParameter(int i) {
154     return (Descriptor) params.get(i);
155   }
156
157   public String getParamName(int i) {
158     return ((Descriptor)params.get(i)).getSymbol();
159   }
160
161   public TypeDescriptor getParamType(int i) {
162     Descriptor d=(Descriptor)params.get(i);
163     if (d instanceof VarDescriptor)
164       return ((VarDescriptor)params.get(i)).getType();
165     else if (d instanceof TagVarDescriptor)
166       return new TypeDescriptor(TypeDescriptor.TAG);
167     else throw new Error();
168   }
169
170   public String toString() {
171     String st="";
172     String type="";
173     if (cd!=null)
174       type=cd+".";
175     if (returntype!=null)
176       st=modifier.toString()+returntype.toString()+" "+type+identifier+"(";
177     else
178       st=modifier.toString()+" "+type+identifier+"(";
179     for(int i=0; i<params.size(); i++) {
180       st+=getParamType(i)+" "+getParamName(i);
181       if ((i+1)!=params.size())
182         st+=", ";
183     }
184     st+=")";
185     return st;
186   }
187 }