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