add more comments
[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     public void setThis(VarDescriptor vd) {
74         thisvd=vd;
75         paramtable.add(vd);
76     }
77
78     public VarDescriptor getThis() {
79         return thisvd;
80     }
81
82     public String getSafeMethodDescriptor() {
83         String st="";
84         for(int i=0;i<numParameters();i++) {
85             st+=getParamType(i).getSafeDescriptor();
86             if ((i+1)<numParameters())
87                 st+="_";
88         }
89         return st;
90     }
91
92     public boolean isStatic() {
93         return modifier.isStatic();
94     }
95
96     public boolean isConstructor() {
97         return (returntype==null);
98     }
99
100     public TypeDescriptor getReturnType() {
101         return returntype;
102     }
103
104     public void setClassDesc(ClassDescriptor cd) {
105         this.cd=cd;
106     }
107
108     public ClassDescriptor getClassDesc() {
109         return cd;
110     }
111
112     public SymbolTable getParameterTable() {
113         return paramtable;
114     }
115
116     public void addParameter(TypeDescriptor type, String paramname) {
117         if (paramname.equals("this"))
118             throw new Error("Can't have parameter named this");
119         VarDescriptor vd=new VarDescriptor(type, paramname);
120
121         params.add(vd);
122         if (paramtable.getFromSameScope(paramname)!=null) {
123             throw new Error("Parameter "+paramname+" already defined");
124         }
125         paramtable.add(vd);
126     }
127
128     public void addTagParameter(TypeDescriptor type, String paramname) {
129         if (paramname.equals("this"))
130             throw new Error("Can't have parameter named this");
131         TagVarDescriptor vd=new TagVarDescriptor(null, paramname);
132
133         params.add(vd);
134         if (paramtable.getFromSameScope(paramname)!=null) {
135             throw new Error("Parameter "+paramname+" already defined");
136         }
137         paramtable.add(vd);
138     }
139
140     public int numParameters() {
141         return params.size();
142     }
143
144     public Descriptor getParameter(int i) {
145         return (Descriptor) params.get(i);
146     }
147
148     public String getParamName(int i) {
149         return ((Descriptor)params.get(i)).getSymbol();
150     }
151
152     public TypeDescriptor getParamType(int i) {
153         Descriptor d=(Descriptor)params.get(i);
154         if (d instanceof VarDescriptor)
155             return ((VarDescriptor)params.get(i)).getType();
156         else if (d instanceof TagVarDescriptor)
157             return new TypeDescriptor(TypeDescriptor.TAG);
158         else throw new Error();
159     }
160
161     public String toString() {
162         String st="";
163         String type="";
164         if (cd!=null)
165             type=cd+".";
166         if (returntype!=null)
167             st=modifier.toString()+returntype.toString()+" "+type+identifier+"(";
168         else
169             st=modifier.toString()+" "+type+identifier+"(";
170         for(int i=0;i<params.size();i++) {
171             st+=getParamType(i)+" "+getParamName(i);
172             if ((i+1)!=params.size())
173                 st+=", ";
174         }
175         st+=")";
176         return st;
177     }
178 }