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