more changes
[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 boolean isStatic() {
53         return modifier.isStatic();
54     }
55
56     public boolean isConstructor() {
57         return (returntype==null);
58     }
59
60     public TypeDescriptor getReturnType() {
61         return returntype;
62     }
63
64     public void setClassDesc(ClassDescriptor cd) {
65         this.cd=cd;
66     }
67
68     public ClassDescriptor getClassDesc() {
69         return cd;
70     }
71
72     public SymbolTable getParameterTable() {
73         return paramtable;
74     }
75
76     public void addParameter(TypeDescriptor type, String paramname) {
77         if (paramname.equals("this"))
78             throw new Error("Can't have parameter named this");
79         VarDescriptor vd=new VarDescriptor(type, paramname);
80
81         params.add(vd);
82         if (paramtable.getFromSameScope(paramname)!=null) {
83             throw new Error("Parameter "+paramname+" already defined");
84         }
85         paramtable.add(vd);
86     }
87
88     public int numParameters() {
89         return params.size();
90     }
91
92     public String getParamName(int i) {
93         return ((VarDescriptor)params.get(i)).getName();
94     }
95
96     public TypeDescriptor getParamType(int i) {
97         return ((VarDescriptor)params.get(i)).getType();
98     }
99
100     public String toString() {
101         String st="";
102         if (returntype!=null)
103             st=modifier.toString()+returntype.toString()+" "+identifier+"(";
104         else
105             st=modifier.toString()+" "+identifier+"(";
106         for(int i=0;i<params.size();i++) {
107             st+=getParamType(i)+" "+getParamName(i);
108             if ((i+1)!=params.size())
109                 st+=", ";
110         }
111         st+=")";
112         return st;
113     }
114 }