fixed bug with wrong method being called in inheritance situations
[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
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             TypeDescriptor td1=getParameter(i).getType();
47             TypeDescriptor td2=md.getParameter(i).getType();
48             if (!td1.equals(td2))
49                 return false;
50         }
51         return true;
52     }
53
54     public MethodDescriptor(Modifiers m, String identifier) {
55         super(identifier);
56         this.modifier=m;
57         this.returntype=null;
58         this.identifier=identifier;
59         this.safename = "___" + name + "___";
60         this.uniqueid=count++;
61         params=new Vector();
62         paramtable=new SymbolTable();
63         thisvd=null;
64     }
65
66     public void setThis(VarDescriptor vd) {
67         thisvd=vd;
68         paramtable.add(vd);
69     }
70
71     public VarDescriptor getThis() {
72         return thisvd;
73     }
74
75     public String getSafeMethodDescriptor() {
76         String st="";
77         for(int i=0;i<numParameters();i++) {
78             st+=getParamType(i).getSafeDescriptor();
79             if ((i+1)<numParameters())
80                 st+="_";
81         }
82         return st;
83     }
84
85     public boolean isStatic() {
86         return modifier.isStatic();
87     }
88
89     public boolean isConstructor() {
90         return (returntype==null);
91     }
92
93     public TypeDescriptor getReturnType() {
94         return returntype;
95     }
96
97     public void setClassDesc(ClassDescriptor cd) {
98         this.cd=cd;
99     }
100
101     public ClassDescriptor getClassDesc() {
102         return cd;
103     }
104
105     public SymbolTable getParameterTable() {
106         return paramtable;
107     }
108
109     public void addParameter(TypeDescriptor type, String paramname) {
110         if (paramname.equals("this"))
111             throw new Error("Can't have parameter named this");
112         VarDescriptor vd=new VarDescriptor(type, paramname);
113
114         params.add(vd);
115         if (paramtable.getFromSameScope(paramname)!=null) {
116             throw new Error("Parameter "+paramname+" already defined");
117         }
118         paramtable.add(vd);
119     }
120
121     public int numParameters() {
122         return params.size();
123     }
124
125     public VarDescriptor getParameter(int i) {
126         return (VarDescriptor)params.get(i);
127     }
128
129     public String getParamName(int i) {
130         return ((VarDescriptor)params.get(i)).getName();
131     }
132
133     public TypeDescriptor getParamType(int i) {
134         return ((VarDescriptor)params.get(i)).getType();
135     }
136
137     public String toString() {
138         String st="";
139         String type="";
140         if (cd!=null)
141             type=cd+".";
142         if (returntype!=null)
143             st=modifier.toString()+returntype.toString()+" "+type+identifier+"(";
144         else
145             st=modifier.toString()+" "+type+identifier+"(";
146         for(int i=0;i<params.size();i++) {
147             st+=getParamType(i)+" "+getParamName(i);
148             if ((i+1)!=params.size())
149                 st+=", ";
150         }
151         st+=")";
152         return st;
153     }
154 }