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