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