Wrote callgraph.
[IRC.git] / Robust / src / IR / Tree / SemanticCheck.java
1 package IR.Tree;
2
3 import java.util.*;
4 import IR.*;
5
6 public class SemanticCheck {
7     State state;
8     TypeUtil typeutil;
9
10     public SemanticCheck(State state, TypeUtil tu) {
11         this.state=state;
12         this.typeutil=tu;
13     }
14
15     public void semanticCheck() {
16         SymbolTable classtable=state.getClassSymbolTable();
17         Iterator it=classtable.getDescriptorsIterator();
18         // Do descriptors first
19         while(it.hasNext()) {
20             ClassDescriptor cd=(ClassDescriptor)it.next();
21             System.out.println("Checking class: "+cd);
22             //Set superclass link up
23             if (cd.getSuper()!=null) {
24                 cd.setSuper(typeutil.getClass(cd.getSuper()));
25                 // Link together Field, Method, and Flag tables so classes
26                 // inherit these from their superclasses
27                 cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
28                 cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
29                 cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable());
30             }
31             
32             /* Check to see that fields are well typed */
33             for(Iterator field_it=cd.getFields();field_it.hasNext();) {
34                 FieldDescriptor fd=(FieldDescriptor)field_it.next();
35                 System.out.println("Checking field: "+fd);
36                 checkField(cd,fd);
37             }
38
39             for(Iterator method_it=cd.getMethods();method_it.hasNext();) {
40                 MethodDescriptor md=(MethodDescriptor)method_it.next();
41                 checkMethod(cd,md);
42             }
43         }
44
45         it=classtable.getDescriptorsIterator();
46         // Do descriptors first
47         while(it.hasNext()) {
48             ClassDescriptor cd=(ClassDescriptor)it.next();
49             for(Iterator method_it=cd.getMethods();method_it.hasNext();) {
50                 MethodDescriptor md=(MethodDescriptor)method_it.next();
51                 checkMethodBody(cd,md);
52             }
53         }
54
55         for(Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator();task_it.hasNext();) {
56             TaskDescriptor td=(TaskDescriptor)task_it.next();
57             checkTask(td);
58             
59         }
60
61     }
62
63     public void checkTypeDescriptor(TypeDescriptor td) {
64         if (td.isPrimitive())
65             return; /* Done */
66         if (td.isClass()) {
67             String name=td.toString();
68             ClassDescriptor field_cd=(ClassDescriptor)state.getClassSymbolTable().get(name);
69             if (field_cd==null)
70                 throw new Error("Undefined class "+name);
71             td.setClassDescriptor(field_cd);
72             return;
73         }
74         throw new Error();
75     }
76
77     public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
78         checkTypeDescriptor(fd.getType());
79     }
80
81     public void checkFlagEffects(TaskDescriptor td, Vector vfe) {
82         for(int i=0;i<vfe.size();i++) {
83             FlagEffects fe=(FlagEffects) vfe.get(i);
84             String varname=fe.getName();
85             //Make sure the variable is declared as a parameter to the task
86             VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
87             if (vd==null)
88                 throw new Error("Parameter "+varname+" in Flag Effects not declared");
89             fe.setVar(vd);
90
91             //Make sure it correspods to a class
92             TypeDescriptor type_d=vd.getType();
93             if (!type_d.isClass())
94                 throw new Error("Cannot have non-object argument for flag_effect");
95
96             ClassDescriptor cd=type_d.getClassDesc();
97             for(int j=0;j<fe.numEffects();j++) {
98                 FlagEffect flag=fe.getEffect(j);
99                 String name=flag.getName();
100                 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
101                 //Make sure the flag is declared
102                 if (flag_d==null)
103                     throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
104                 flag.setFlag(flag_d);
105             }
106         }
107     }
108
109     public void checkTask(TaskDescriptor td) {
110         for(int i=0;i<td.numParameters();i++) {
111             /* Check that parameter is well typed */
112             TypeDescriptor param_type=td.getParamType(i);
113             checkTypeDescriptor(param_type);
114
115             /* Check the parameter's flag expression is well formed */
116             FlagExpressionNode fen=td.getFlag(td.getParameter(i));
117             if (!param_type.isClass())
118                 throw new Error("Cannot have non-object argument to a task");
119             ClassDescriptor cd=param_type.getClassDesc();
120             checkFlagExpressionNode(cd, fen);
121             checkFlagEffects(td, td.getFlagEffects());
122
123             /* Check that the task code is valid */
124             BlockNode bn=state.getMethodBody(td);
125             checkBlockNode(td, td.getParameterTable(),bn);
126         }
127     }
128
129     public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
130         switch(fen.kind()) {
131         case Kind.FlagOpNode: 
132             {
133                 FlagOpNode fon=(FlagOpNode)fen;
134                 checkFlagExpressionNode(cd, fon.getLeft());
135                 if (fon.getRight()!=null)
136                     checkFlagExpressionNode(cd, fon.getRight());
137                 break;
138             }
139         case Kind.FlagNode:
140             {
141                 FlagNode fn=(FlagNode)fen;
142                 String name=fn.getFlagName();
143                 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
144                 if (fd==null)
145                     throw new Error("Undeclared flag: "+name);
146                 fn.setFlag(fd);
147                 break;
148             }
149         default:
150             throw new Error("Unrecognized FlagExpressionNode");
151         }
152     }
153
154     public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
155         /* Check return type */
156         if (!md.isConstructor())
157             if (!md.getReturnType().isVoid())
158                 checkTypeDescriptor(md.getReturnType());
159
160         for(int i=0;i<md.numParameters();i++) {
161             TypeDescriptor param_type=md.getParamType(i);
162             checkTypeDescriptor(param_type);
163         }
164         /* Link the naming environments */
165         if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
166             md.getParameterTable().setParent(cd.getFieldTable());
167         md.setClassDesc(cd);
168         if (!md.isStatic()) {
169             VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
170             md.setThis(thisvd);
171         }
172     }
173
174     public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
175         System.out.println("Processing method:"+md);
176         BlockNode bn=state.getMethodBody(md);
177         checkBlockNode(md, md.getParameterTable(),bn);
178     }
179     
180     public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
181         /* Link in the naming environment */
182         bn.getVarTable().setParent(nametable);
183         for(int i=0;i<bn.size();i++) {
184             BlockStatementNode bsn=bn.get(i);
185             checkBlockStatementNode(md, bn.getVarTable(),bsn);
186         }
187     }
188     
189     public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
190         switch(bsn.kind()) {
191         case Kind.BlockExpressionNode:
192             checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
193             return;
194
195         case Kind.DeclarationNode:
196             checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
197             return;
198             
199         case Kind.IfStatementNode:
200             checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
201             return;
202             
203         case Kind.LoopNode:
204             checkLoopNode(md, nametable, (LoopNode)bsn);
205             return;
206             
207         case Kind.ReturnNode:
208             checkReturnNode(md, nametable, (ReturnNode)bsn);
209             return;
210
211         case Kind.TaskExitNode:
212             checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
213             return;
214
215         case Kind.SubBlockNode:
216             checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
217             return;
218         }
219         throw new Error();
220     }
221
222     void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
223         checkExpressionNode(md, nametable, ben.getExpression(), null);
224     }
225
226     void checkDeclarationNode(Descriptor md, SymbolTable nametable,  DeclarationNode dn) {
227         VarDescriptor vd=dn.getVarDescriptor();
228         checkTypeDescriptor(vd.getType());
229         Descriptor d=nametable.get(vd.getSymbol());
230         if ((d==null)||
231             (d instanceof FieldDescriptor)) {
232             nametable.add(vd);
233         } else
234             throw new Error(vd.getSymbol()+" defined a second time");
235         if (dn.getExpression()!=null)
236             checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
237     }
238     
239     void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
240         checkBlockNode(md, nametable, sbn.getBlockNode());
241     }
242
243     void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
244         if (d instanceof TaskDescriptor)
245             throw new Error("Illegal return appears in Task: "+d.getSymbol());
246         MethodDescriptor md=(MethodDescriptor)d;
247         if (rn.getReturnExpression()!=null)
248             checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
249         else
250             if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
251                 throw new Error("Need to return something for "+md);
252     }
253
254     void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
255         if (md instanceof MethodDescriptor)
256             throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
257         checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects());
258     }
259
260     void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
261         checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
262         checkBlockNode(md, nametable, isn.getTrueBlock());
263         if (isn.getFalseBlock()!=null)
264             checkBlockNode(md, nametable, isn.getFalseBlock());
265     }
266     
267     void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
268         switch(en.kind()) {
269         case Kind.AssignmentNode:
270             checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
271             return;
272         case Kind.CastNode:
273             checkCastNode(md,nametable,(CastNode)en,td);
274             return;
275         case Kind.CreateObjectNode:
276             checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
277             return;
278         case Kind.FieldAccessNode:
279             checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
280             return;
281         case Kind.ArrayAccessNode:
282             checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
283             return;
284         case Kind.LiteralNode:
285             checkLiteralNode(md,nametable,(LiteralNode)en,td);
286             return;
287         case Kind.MethodInvokeNode:
288             checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
289             return;
290         case Kind.NameNode:
291             checkNameNode(md,nametable,(NameNode)en,td);
292             return;
293         case Kind.OpNode:
294             checkOpNode(md,nametable,(OpNode)en,td);
295             return;
296         }
297         throw new Error();
298     }
299
300     void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
301         /* Get type descriptor */
302         if (cn.getType()==null) {
303             NameDescriptor typenamed=cn.getTypeName().getName();
304             String typename=typenamed.toString();
305             TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
306             cn.setType(ntd);
307         }
308
309         /* Check the type descriptor */
310         TypeDescriptor cast_type=cn.getType();
311         checkTypeDescriptor(cast_type);
312
313         /* Type check */
314         if (td!=null) {
315             if (!typeutil.isSuperorType(td,cast_type))
316                 throw new Error("Cast node returns "+cast_type+", but need "+td);
317         }
318
319         ExpressionNode en=cn.getExpression();
320         checkExpressionNode(md, nametable, en, null);
321         TypeDescriptor etd=en.getType();
322         if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
323             return;
324
325         if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
326             return;
327
328         /* Different branches */
329         /* TODO: change if add interfaces */
330         throw new Error("Cast will always fail\n"+cn.printNode(0));
331     }
332
333     void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
334         ExpressionNode left=fan.getExpression();
335         checkExpressionNode(md,nametable,left,null);
336         TypeDescriptor ltd=left.getType();
337         String fieldname=fan.getFieldName();
338
339         FieldDescriptor fd=null;
340         if (ltd.isArray()&&fieldname.equals("length"))
341             fd=FieldDescriptor.arrayLength;
342         else
343             fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
344         if (fd==null)
345             throw new Error("Unknown field "+fieldname);
346         fan.setField(fd);
347         if (td!=null)
348             if (!typeutil.isSuperorType(td,fan.getType()))
349                 throw new Error("Field node returns "+fan.getType()+", but need "+td);
350     }
351
352     void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
353         ExpressionNode left=aan.getExpression();
354         checkExpressionNode(md,nametable,left,null);
355
356         checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
357         TypeDescriptor ltd=left.getType();
358
359         if (td!=null)
360             if (!typeutil.isSuperorType(td,aan.getType()))
361                 throw new Error("Field node returns "+aan.getType()+", but need "+td);
362     }
363
364     void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
365         /* Resolve the type */
366         Object o=ln.getValue();
367         if (ln.getTypeString().equals("null")) {
368             ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
369         } else if (o instanceof Integer) {
370             ln.setType(new TypeDescriptor(TypeDescriptor.INT));
371         } else if (o instanceof Long) {
372             ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
373         } else if (o instanceof Float) {
374             ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
375         } else if (o instanceof Boolean) {
376             ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
377         } else if (o instanceof Double) {
378             ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
379         } else if (o instanceof Character) {
380             ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
381         } else if (o instanceof String) {
382             ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
383         }
384
385         if (td!=null)
386             if (!typeutil.isSuperorType(td,ln.getType()))
387                 throw new Error("Field node returns "+ln.getType()+", but need "+td);
388     }
389
390     void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
391         NameDescriptor nd=nn.getName();
392         if (nd.getBase()!=null) {
393             /* Big hack */
394             /* Rewrite NameNode */
395             ExpressionNode en=translateNameDescriptorintoExpression(nd);
396             nn.setExpression(en);
397             checkExpressionNode(md,nametable,en,td);
398         } else {
399             String varname=nd.toString();
400             Descriptor d=(Descriptor)nametable.get(varname);
401             if (d==null) {
402                 throw new Error("Name "+varname+" undefined");
403             }
404             if (d instanceof VarDescriptor) {
405                 nn.setVar((VarDescriptor)d);
406             } else if (d instanceof FieldDescriptor) {
407                 nn.setField((FieldDescriptor)d);
408                 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
409             }
410             if (td!=null)
411                 if (!typeutil.isSuperorType(td,nn.getType()))
412                     throw new Error("Field node returns "+nn.getType()+", but need "+td);
413         }
414     }
415
416     void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
417         checkExpressionNode(md, nametable, an.getSrc() ,td);
418         //TODO: Need check on validity of operation here
419         if (!((an.getDest() instanceof FieldAccessNode)||
420               (an.getDest() instanceof ArrayAccessNode)||
421               (an.getDest() instanceof NameNode)))
422             throw new Error("Bad lside in "+an.printNode(0));
423         checkExpressionNode(md, nametable, an.getDest(), null);
424
425         /* We want parameter variables to tasks to be immutable */
426         if (md instanceof TaskDescriptor) {
427             if (an.getDest() instanceof NameNode) {
428                 NameNode nn=(NameNode)an.getDest();
429                 if (nn.getVar()!=null) {
430                     if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
431                         throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
432                 }
433             }
434         }
435         
436         if (!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
437             throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
438         }
439     }
440
441     void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
442         if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
443             checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
444             checkBlockNode(md, nametable, ln.getBody());
445         } else {
446             //For loop case
447             /* Link in the initializer naming environment */
448             BlockNode bn=ln.getInitializer();
449             bn.getVarTable().setParent(nametable);
450             for(int i=0;i<bn.size();i++) {
451                 BlockStatementNode bsn=bn.get(i);
452                 checkBlockStatementNode(md, bn.getVarTable(),bsn);
453             }
454             //check the condition
455             checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
456             checkBlockNode(md, bn.getVarTable(), ln.getBody());
457             checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
458         }
459     }
460
461
462     void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
463         TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
464         for(int i=0;i<con.numArgs();i++) {
465             ExpressionNode en=con.getArg(i);
466             checkExpressionNode(md,nametable,en,null);
467             tdarray[i]=en.getType();
468         }
469
470         TypeDescriptor typetolookin=con.getType();
471         checkTypeDescriptor(typetolookin);
472
473         /* Check flag effects */
474         if (con.getFlagEffects()!=null) {
475             FlagEffects fe=con.getFlagEffects();
476             ClassDescriptor cd=typetolookin.getClassDesc();
477             
478             for(int j=0;j<fe.numEffects();j++) {
479                 FlagEffect flag=fe.getEffect(j);
480                 String name=flag.getName();
481                 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
482                 //Make sure the flag is declared
483                 if (flag_d==null)
484                     throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
485                 flag.setFlag(flag_d);
486             }
487         }
488
489
490         if ((!typetolookin.isClass())&&(!typetolookin.isArray())) 
491             throw new Error("Can't allocate primitive type:"+con.printNode(0));
492
493         if (!typetolookin.isArray()) {
494             //Array's don't need constructor calls
495             ClassDescriptor classtolookin=typetolookin.getClassDesc();
496             System.out.println("Looking for "+typetolookin.getSymbol());
497             System.out.println(classtolookin.getMethodTable());
498             
499             Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
500             MethodDescriptor bestmd=null;
501         NextMethod:
502             for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
503                 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
504                 /* Need correct number of parameters */
505                 System.out.println("Examining: "+currmd);
506                 if (con.numArgs()!=currmd.numParameters())
507                     continue;
508                 for(int i=0;i<con.numArgs();i++) {
509                     if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
510                         continue NextMethod;
511                 }
512                 /* Method okay so far */
513                 if (bestmd==null)
514                     bestmd=currmd;
515                 else {
516                     if (isMoreSpecific(currmd,bestmd)) {
517                         bestmd=currmd;
518                     } else if (!isMoreSpecific(bestmd, currmd))
519                         throw new Error("No method is most specific");
520                     
521                     /* Is this more specific than bestmd */
522                 }
523             }
524             if (bestmd==null)
525                 throw new Error("No method found for "+con.printNode(0));
526             con.setConstructor(bestmd);
527         }
528     }
529
530
531     /** Check to see if md1 is more specific than md2...  Informally
532         if md2 could always be called given the arguments passed into
533         md1 */
534
535     boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
536         /* Checks if md1 is more specific than md2 */
537         if (md1.numParameters()!=md2.numParameters())
538             throw new Error();
539         for(int i=0;i<md1.numParameters();i++) {
540             if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
541                 return false;
542         }
543         if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
544                 return false;
545         return true;
546     }
547
548     ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
549         String id=nd.getIdentifier();
550         NameDescriptor base=nd.getBase();
551         if (base==null) 
552             return new NameNode(nd);
553         else 
554             return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
555     }
556
557
558     void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
559         /*Typecheck subexpressions
560           and get types for expressions*/
561
562         TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
563         for(int i=0;i<min.numArgs();i++) {
564             ExpressionNode en=min.getArg(i);
565             checkExpressionNode(md,nametable,en,null);
566             tdarray[i]=en.getType();
567         }
568         TypeDescriptor typetolookin=null;
569         if (min.getExpression()!=null) {
570             checkExpressionNode(md,nametable,min.getExpression(),null);
571             typetolookin=min.getExpression().getType();
572         } else if (min.getBaseName()!=null) {
573             String rootname=min.getBaseName().getRoot();
574             if (nametable.get(rootname)!=null) {
575                 //we have an expression
576                 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
577                 checkExpressionNode(md, nametable, min.getExpression(), null);
578                 typetolookin=min.getExpression().getType();
579             } else {
580                 //we have a type
581                 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
582                 if (cd==null)
583                     throw new Error(min.getBaseName()+" undefined");
584                 typetolookin=new TypeDescriptor(cd);
585             }
586         } else if (md instanceof MethodDescriptor) {
587             typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
588         } else {
589             /* If this a task descriptor we throw an error at this point */
590             throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
591         }
592         if (!typetolookin.isClass()) 
593             throw new Error();
594         ClassDescriptor classtolookin=typetolookin.getClassDesc();
595         System.out.println("Method name="+min.getMethodName());
596         Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
597         MethodDescriptor bestmd=null;
598         NextMethod:
599         for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
600             MethodDescriptor currmd=(MethodDescriptor)methodit.next();
601             /* Need correct number of parameters */
602             if (min.numArgs()!=currmd.numParameters())
603                 continue;
604             for(int i=0;i<min.numArgs();i++) {
605                 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
606                     continue NextMethod;
607             }
608             /* Method okay so far */
609             if (bestmd==null)
610                 bestmd=currmd;
611             else {
612                 if (isMoreSpecific(currmd,bestmd)) {
613                     bestmd=currmd;
614                 } else if (!isMoreSpecific(bestmd, currmd))
615                     throw new Error("No method is most specific");
616                 
617                 /* Is this more specific than bestmd */
618             }
619         }
620         if (bestmd==null)
621             throw new Error("No method found for :"+min.printNode(0));
622         min.setMethod(bestmd);
623         /* Check whether we need to set this parameter to implied this */
624         if (!bestmd.isStatic()) {
625             if (min.getExpression()==null) {
626                 ExpressionNode en=new NameNode(new NameDescriptor("this"));
627                 min.setExpression(en);
628                 checkExpressionNode(md, nametable, min.getExpression(), null);
629             }
630         }
631
632     }
633
634
635     void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
636         checkExpressionNode(md, nametable, on.getLeft(), null);
637         if (on.getRight()!=null)
638             checkExpressionNode(md, nametable, on.getRight(), null);
639         TypeDescriptor ltd=on.getLeft().getType();
640         TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
641         TypeDescriptor lefttype=null;
642         TypeDescriptor righttype=null;
643         Operation op=on.getOp();
644
645         switch(op.getOp()) {
646         case Operation.LOGIC_OR:
647         case Operation.LOGIC_AND:
648             if (!(ltd.isBoolean()&&rtd.isBoolean()))
649                 throw new Error();
650             //no promotion
651             on.setLeftType(ltd);
652             on.setRightType(rtd);
653             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
654             break;
655
656         case Operation.BIT_OR:
657         case Operation.BIT_XOR:
658         case Operation.BIT_AND:
659             // 5.6.2 Binary Numeric Promotion
660             //TODO unboxing of reference objects
661             if (ltd.isDouble()||rtd.isDouble())
662                 throw new Error();
663             else if (ltd.isFloat()||rtd.isFloat())
664                 throw new Error();
665             else if (ltd.isLong()||rtd.isLong())
666                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
667             else 
668                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
669             righttype=lefttype;
670
671             on.setLeftType(lefttype);
672             on.setRightType(righttype);
673             on.setType(lefttype);
674             break;
675
676         case Operation.EQUAL:
677         case Operation.NOTEQUAL:
678             // 5.6.2 Binary Numeric Promotion
679             //TODO unboxing of reference objects
680             if (ltd.isBoolean()||rtd.isBoolean()) {
681                 if (!(ltd.isBoolean()&&rtd.isBoolean()))
682                     throw new Error();
683                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
684             } else if (ltd.isPtr()||rtd.isPtr()) {
685                 if (!(ltd.isPtr()&&rtd.isPtr()))
686                     throw new Error();
687                 righttype=rtd;
688                 lefttype=ltd;
689             } else if (ltd.isDouble()||rtd.isDouble())
690                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
691             else if (ltd.isFloat()||rtd.isFloat())
692                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
693             else if (ltd.isLong()||rtd.isLong())
694                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
695             else 
696                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
697
698             on.setLeftType(lefttype);
699             on.setRightType(righttype);
700             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
701             break;
702
703
704
705         case Operation.LT:
706         case Operation.GT:
707         case Operation.LTE:
708         case Operation.GTE:
709             // 5.6.2 Binary Numeric Promotion
710             //TODO unboxing of reference objects
711             if (!ltd.isNumber()||!rtd.isNumber())
712                 throw new Error();
713
714             if (ltd.isDouble()||rtd.isDouble())
715                 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
716             else if (ltd.isFloat()||rtd.isFloat())
717                 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
718             else if (ltd.isLong()||rtd.isLong())
719                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
720             else 
721                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
722             righttype=lefttype;
723             on.setLeftType(lefttype);
724             on.setRightType(righttype);
725             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
726             break;
727
728         case Operation.ADD:
729             //TODO: Need special case for strings eventually
730             
731             
732         case Operation.SUB:
733         case Operation.MULT:
734         case Operation.DIV:
735         case Operation.MOD:
736             // 5.6.2 Binary Numeric Promotion
737             //TODO unboxing of reference objects
738             if (!ltd.isNumber()||!rtd.isNumber())
739                 throw new Error("Error in "+on.printNode(0));
740
741             if (ltd.isDouble()||rtd.isDouble())
742                 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
743             else if (ltd.isFloat()||rtd.isFloat())
744                 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
745             else if (ltd.isLong()||rtd.isLong())
746                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
747             else 
748                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
749             righttype=lefttype;
750             on.setLeftType(lefttype);
751             on.setRightType(righttype);
752             on.setType(lefttype);
753             break;
754
755         case Operation.LEFTSHIFT:
756         case Operation.RIGHTSHIFT:
757             if (!rtd.isIntegerType())
758                 throw new Error();
759             //5.6.1 Unary Numeric Promotion
760             if (rtd.isByte()||rtd.isShort()||rtd.isInt())
761                 righttype=new TypeDescriptor(TypeDescriptor.INT);
762             else
763                 righttype=rtd;
764
765             on.setRightType(righttype);
766             if (!ltd.isIntegerType())
767                 throw new Error();
768         case Operation.UNARYPLUS:
769         case Operation.UNARYMINUS:
770         case Operation.POSTINC:
771         case Operation.POSTDEC:
772         case Operation.PREINC:
773         case Operation.PREDEC:
774             if (!ltd.isNumber())
775                 throw new Error();
776             //5.6.1 Unary Numeric Promotion
777             if (ltd.isByte()||ltd.isShort()||ltd.isInt())
778                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
779             else
780                 lefttype=ltd;
781             on.setLeftType(lefttype);
782             on.setType(lefttype);
783             break;
784         default:
785             throw new Error();
786         }
787    
788         if (td!=null)
789             if (!typeutil.isSuperorType(td, on.getType())) {
790                 System.out.println(td);
791                 System.out.println(on.getType());
792                 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));     
793             }
794     }
795 }