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