Semantic checks for tasks...Made the TaskDescriptor inherit from the MethodDescriptor.
[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(MethodDescriptor 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(MethodDescriptor 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(MethodDescriptor md, SymbolTable nametable, BlockExpressionNode ben) {
223         checkExpressionNode(md, nametable, ben.getExpression(), null);
224     }
225
226     void checkDeclarationNode(MethodDescriptor 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(MethodDescriptor md, SymbolTable nametable, SubBlockNode sbn) {
240         checkBlockNode(md, nametable, sbn.getBlockNode());
241     }
242
243     void checkReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn) {
244         if (md instanceof TaskDescriptor)
245             throw new Error("Illegal return appears in Task: "+md.getSymbol());
246         if (rn.getReturnExpression()!=null)
247             checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
248         else
249             if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
250                 throw new Error("Need to return something for "+md);
251     }
252
253     void checkTaskExitNode(MethodDescriptor md, SymbolTable nametable, TaskExitNode ten) {
254         if (!(md instanceof TaskDescriptor))
255             throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
256         checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects());
257     }
258
259     void checkIfStatementNode(MethodDescriptor md, SymbolTable nametable, IfStatementNode isn) {
260         checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
261         checkBlockNode(md, nametable, isn.getTrueBlock());
262         if (isn.getFalseBlock()!=null)
263             checkBlockNode(md, nametable, isn.getFalseBlock());
264     }
265     
266     void checkExpressionNode(MethodDescriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
267         switch(en.kind()) {
268         case Kind.AssignmentNode:
269             checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
270             return;
271         case Kind.CastNode:
272             checkCastNode(md,nametable,(CastNode)en,td);
273             return;
274         case Kind.CreateObjectNode:
275             checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
276             return;
277         case Kind.FieldAccessNode:
278             checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
279             return;
280         case Kind.ArrayAccessNode:
281             checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
282             return;
283         case Kind.LiteralNode:
284             checkLiteralNode(md,nametable,(LiteralNode)en,td);
285             return;
286         case Kind.MethodInvokeNode:
287             checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
288             return;
289         case Kind.NameNode:
290             checkNameNode(md,nametable,(NameNode)en,td);
291             return;
292         case Kind.OpNode:
293             checkOpNode(md,nametable,(OpNode)en,td);
294             return;
295         }
296         throw new Error();
297     }
298
299     void checkCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
300         /* Get type descriptor */
301         if (cn.getType()==null) {
302             NameDescriptor typenamed=cn.getTypeName().getName();
303             String typename=typenamed.toString();
304             TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
305             cn.setType(ntd);
306         }
307
308         /* Check the type descriptor */
309         TypeDescriptor cast_type=cn.getType();
310         checkTypeDescriptor(cast_type);
311
312         /* Type check */
313         if (td!=null) {
314             if (!typeutil.isSuperorType(td,cast_type))
315                 throw new Error("Cast node returns "+cast_type+", but need "+td);
316         }
317
318         ExpressionNode en=cn.getExpression();
319         checkExpressionNode(md, nametable, en, null);
320         TypeDescriptor etd=en.getType();
321         if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
322             return;
323
324         if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
325             return;
326
327         /* Different branches */
328         /* TODO: change if add interfaces */
329         throw new Error("Cast will always fail\n"+cn.printNode(0));
330     }
331
332     void checkFieldAccessNode(MethodDescriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
333         ExpressionNode left=fan.getExpression();
334         checkExpressionNode(md,nametable,left,null);
335         TypeDescriptor ltd=left.getType();
336         String fieldname=fan.getFieldName();
337
338         FieldDescriptor fd=null;
339         if (ltd.isArray()&&fieldname.equals("length"))
340             fd=FieldDescriptor.arrayLength;
341         else
342             fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
343         if (fd==null)
344             throw new Error("Unknown field "+fieldname);
345         fan.setField(fd);
346         if (td!=null)
347             if (!typeutil.isSuperorType(td,fan.getType()))
348                 throw new Error("Field node returns "+fan.getType()+", but need "+td);
349     }
350
351     void checkArrayAccessNode(MethodDescriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
352         ExpressionNode left=aan.getExpression();
353         checkExpressionNode(md,nametable,left,null);
354
355         checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
356         TypeDescriptor ltd=left.getType();
357
358         if (td!=null)
359             if (!typeutil.isSuperorType(td,aan.getType()))
360                 throw new Error("Field node returns "+aan.getType()+", but need "+td);
361     }
362
363     void checkLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
364         /* Resolve the type */
365         Object o=ln.getValue();
366         if (ln.getTypeString().equals("null")) {
367             ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
368         } else if (o instanceof Integer) {
369             ln.setType(new TypeDescriptor(TypeDescriptor.INT));
370         } else if (o instanceof Long) {
371             ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
372         } else if (o instanceof Float) {
373             ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
374         } else if (o instanceof Boolean) {
375             ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
376         } else if (o instanceof Double) {
377             ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
378         } else if (o instanceof Character) {
379             ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
380         } else if (o instanceof String) {
381             ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
382         }
383
384         if (td!=null)
385             if (!typeutil.isSuperorType(td,ln.getType()))
386                 throw new Error("Field node returns "+ln.getType()+", but need "+td);
387     }
388
389     void checkNameNode(MethodDescriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
390         NameDescriptor nd=nn.getName();
391         if (nd.getBase()!=null) {
392             /* Big hack */
393             /* Rewrite NameNode */
394             ExpressionNode en=translateNameDescriptorintoExpression(nd);
395             nn.setExpression(en);
396             checkExpressionNode(md,nametable,en,td);
397         } else {
398             String varname=nd.toString();
399             Descriptor d=(Descriptor)nametable.get(varname);
400             if (d==null) {
401                 throw new Error("Name "+varname+" undefined");
402             }
403             if (d instanceof VarDescriptor) {
404                 nn.setVar((VarDescriptor)d);
405             } else if (d instanceof FieldDescriptor) {
406                 nn.setField((FieldDescriptor)d);
407                 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
408             }
409             if (td!=null)
410                 if (!typeutil.isSuperorType(td,nn.getType()))
411                     throw new Error("Field node returns "+nn.getType()+", but need "+td);
412         }
413     }
414
415     void checkAssignmentNode(MethodDescriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
416         checkExpressionNode(md, nametable, an.getSrc() ,td);
417         //TODO: Need check on validity of operation here
418         if (!((an.getDest() instanceof FieldAccessNode)||
419               (an.getDest() instanceof ArrayAccessNode)||
420               (an.getDest() instanceof NameNode)))
421             throw new Error("Bad lside in "+an.printNode(0));
422         checkExpressionNode(md, nametable, an.getDest(), null);
423         if (!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
424             throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
425         }
426     }
427
428     void checkLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
429         if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
430             checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
431             checkBlockNode(md, nametable, ln.getBody());
432         } else {
433             //For loop case
434             /* Link in the initializer naming environment */
435             BlockNode bn=ln.getInitializer();
436             bn.getVarTable().setParent(nametable);
437             for(int i=0;i<bn.size();i++) {
438                 BlockStatementNode bsn=bn.get(i);
439                 checkBlockStatementNode(md, bn.getVarTable(),bsn);
440             }
441             //check the condition
442             checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
443             checkBlockNode(md, bn.getVarTable(), ln.getBody());
444             checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
445         }
446     }
447
448
449     void checkCreateObjectNode(MethodDescriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
450         TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
451         for(int i=0;i<con.numArgs();i++) {
452             ExpressionNode en=con.getArg(i);
453             checkExpressionNode(md,nametable,en,null);
454             tdarray[i]=en.getType();
455         }
456
457         TypeDescriptor typetolookin=con.getType();
458         checkTypeDescriptor(typetolookin);
459         if ((!typetolookin.isClass())&&(!typetolookin.isArray())) 
460             throw new Error("Can't allocate primitive type:"+con.printNode(0));
461
462         if (!typetolookin.isArray()) {
463             //Array's don't need constructor calls
464             ClassDescriptor classtolookin=typetolookin.getClassDesc();
465             System.out.println("Looking for "+typetolookin.getSymbol());
466             System.out.println(classtolookin.getMethodTable());
467             
468             Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
469             MethodDescriptor bestmd=null;
470         NextMethod:
471             for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
472                 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
473                 /* Need correct number of parameters */
474                 System.out.println("Examining: "+currmd);
475                 if (con.numArgs()!=currmd.numParameters())
476                     continue;
477                 for(int i=0;i<con.numArgs();i++) {
478                     if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
479                         continue NextMethod;
480                 }
481                 /* Method okay so far */
482                 if (bestmd==null)
483                     bestmd=currmd;
484                 else {
485                     if (isMoreSpecific(currmd,bestmd)) {
486                         bestmd=currmd;
487                     } else if (!isMoreSpecific(bestmd, currmd))
488                         throw new Error("No method is most specific");
489                     
490                     /* Is this more specific than bestmd */
491                 }
492             }
493             if (bestmd==null)
494                 throw new Error("No method found for "+con.printNode(0));
495             con.setConstructor(bestmd);
496         }
497     }
498
499
500     /** Check to see if md1 is more specific than md2...  Informally
501         if md2 could always be called given the arguments passed into
502         md1 */
503
504     boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
505         /* Checks if md1 is more specific than md2 */
506         if (md1.numParameters()!=md2.numParameters())
507             throw new Error();
508         for(int i=0;i<md1.numParameters();i++) {
509             if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
510                 return false;
511         }
512         if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
513                 return false;
514         return true;
515     }
516
517     ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
518         String id=nd.getIdentifier();
519         NameDescriptor base=nd.getBase();
520         if (base==null) 
521             return new NameNode(nd);
522         else 
523             return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
524     }
525
526
527     void checkMethodInvokeNode(MethodDescriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
528         /*Typecheck subexpressions
529           and get types for expressions*/
530
531         TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
532         for(int i=0;i<min.numArgs();i++) {
533             ExpressionNode en=min.getArg(i);
534             checkExpressionNode(md,nametable,en,null);
535             tdarray[i]=en.getType();
536         }
537         TypeDescriptor typetolookin=null;
538         if (min.getExpression()!=null) {
539             checkExpressionNode(md,nametable,min.getExpression(),null);
540             typetolookin=min.getExpression().getType();
541         } else if (min.getBaseName()!=null) {
542             String rootname=min.getBaseName().getRoot();
543             if (nametable.get(rootname)!=null) {
544                 //we have an expression
545                 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
546                 checkExpressionNode(md, nametable, min.getExpression(), null);
547                 typetolookin=min.getExpression().getType();
548             } else {
549                 //we have a type
550                 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
551                 if (cd==null)
552                     throw new Error(min.getBaseName()+" undefined");
553                 typetolookin=new TypeDescriptor(cd);
554             }
555         } else if (!(md instanceof TaskDescriptor)) {
556             typetolookin=new TypeDescriptor(md.getClassDesc());
557         } else {
558             /* If this a task descriptor we throw an error at this point */
559             throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
560         }
561         if (!typetolookin.isClass()) 
562             throw new Error();
563         ClassDescriptor classtolookin=typetolookin.getClassDesc();
564         System.out.println("Method name="+min.getMethodName());
565         Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
566         MethodDescriptor bestmd=null;
567         NextMethod:
568         for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
569             MethodDescriptor currmd=(MethodDescriptor)methodit.next();
570             /* Need correct number of parameters */
571             if (min.numArgs()!=currmd.numParameters())
572                 continue;
573             for(int i=0;i<min.numArgs();i++) {
574                 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
575                     continue NextMethod;
576             }
577             /* Method okay so far */
578             if (bestmd==null)
579                 bestmd=currmd;
580             else {
581                 if (isMoreSpecific(currmd,bestmd)) {
582                     bestmd=currmd;
583                 } else if (!isMoreSpecific(bestmd, currmd))
584                     throw new Error("No method is most specific");
585                 
586                 /* Is this more specific than bestmd */
587             }
588         }
589         if (bestmd==null)
590             throw new Error("No method found for :"+min.printNode(0));
591         min.setMethod(bestmd);
592         /* Check whether we need to set this parameter to implied this */
593         if (!bestmd.isStatic()) {
594             if (min.getExpression()==null) {
595                 ExpressionNode en=new NameNode(new NameDescriptor("this"));
596                 min.setExpression(en);
597                 checkExpressionNode(md, nametable, min.getExpression(), null);
598             }
599         }
600
601     }
602
603
604     void checkOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
605         checkExpressionNode(md, nametable, on.getLeft(), null);
606         if (on.getRight()!=null)
607             checkExpressionNode(md, nametable, on.getRight(), null);
608         TypeDescriptor ltd=on.getLeft().getType();
609         TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
610         TypeDescriptor lefttype=null;
611         TypeDescriptor righttype=null;
612         Operation op=on.getOp();
613
614         switch(op.getOp()) {
615         case Operation.LOGIC_OR:
616         case Operation.LOGIC_AND:
617             if (!(ltd.isBoolean()&&rtd.isBoolean()))
618                 throw new Error();
619             //no promotion
620             on.setLeftType(ltd);
621             on.setRightType(rtd);
622             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
623             break;
624
625         case Operation.BIT_OR:
626         case Operation.BIT_XOR:
627         case Operation.BIT_AND:
628             // 5.6.2 Binary Numeric Promotion
629             //TODO unboxing of reference objects
630             if (ltd.isDouble()||rtd.isDouble())
631                 throw new Error();
632             else if (ltd.isFloat()||rtd.isFloat())
633                 throw new Error();
634             else if (ltd.isLong()||rtd.isLong())
635                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
636             else 
637                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
638             righttype=lefttype;
639
640             on.setLeftType(lefttype);
641             on.setRightType(righttype);
642             on.setType(lefttype);
643             break;
644
645         case Operation.EQUAL:
646         case Operation.NOTEQUAL:
647             // 5.6.2 Binary Numeric Promotion
648             //TODO unboxing of reference objects
649             if (ltd.isBoolean()||rtd.isBoolean()) {
650                 if (!(ltd.isBoolean()&&rtd.isBoolean()))
651                     throw new Error();
652                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
653             } else if (ltd.isPtr()||rtd.isPtr()) {
654                 if (!(ltd.isPtr()&&rtd.isPtr()))
655                     throw new Error();
656                 righttype=rtd;
657                 lefttype=ltd;
658             } else if (ltd.isDouble()||rtd.isDouble())
659                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
660             else if (ltd.isFloat()||rtd.isFloat())
661                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
662             else if (ltd.isLong()||rtd.isLong())
663                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
664             else 
665                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
666
667             on.setLeftType(lefttype);
668             on.setRightType(righttype);
669             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
670             break;
671
672
673
674         case Operation.LT:
675         case Operation.GT:
676         case Operation.LTE:
677         case Operation.GTE:
678             // 5.6.2 Binary Numeric Promotion
679             //TODO unboxing of reference objects
680             if (!ltd.isNumber()||!rtd.isNumber())
681                 throw new Error();
682
683             if (ltd.isDouble()||rtd.isDouble())
684                 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
685             else if (ltd.isFloat()||rtd.isFloat())
686                 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
687             else if (ltd.isLong()||rtd.isLong())
688                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
689             else 
690                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
691             righttype=lefttype;
692             on.setLeftType(lefttype);
693             on.setRightType(righttype);
694             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
695             break;
696
697         case Operation.ADD:
698             //TODO: Need special case for strings eventually
699             
700             
701         case Operation.SUB:
702         case Operation.MULT:
703         case Operation.DIV:
704         case Operation.MOD:
705             // 5.6.2 Binary Numeric Promotion
706             //TODO unboxing of reference objects
707             if (!ltd.isNumber()||!rtd.isNumber())
708                 throw new Error("Error in "+on.printNode(0));
709
710             if (ltd.isDouble()||rtd.isDouble())
711                 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
712             else if (ltd.isFloat()||rtd.isFloat())
713                 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
714             else if (ltd.isLong()||rtd.isLong())
715                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
716             else 
717                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
718             righttype=lefttype;
719             on.setLeftType(lefttype);
720             on.setRightType(righttype);
721             on.setType(lefttype);
722             break;
723
724         case Operation.LEFTSHIFT:
725         case Operation.RIGHTSHIFT:
726             if (!rtd.isIntegerType())
727                 throw new Error();
728             //5.6.1 Unary Numeric Promotion
729             if (rtd.isByte()||rtd.isShort()||rtd.isInt())
730                 righttype=new TypeDescriptor(TypeDescriptor.INT);
731             else
732                 righttype=rtd;
733
734             on.setRightType(righttype);
735             if (!ltd.isIntegerType())
736                 throw new Error();
737         case Operation.UNARYPLUS:
738         case Operation.UNARYMINUS:
739         case Operation.POSTINC:
740         case Operation.POSTDEC:
741         case Operation.PREINC:
742         case Operation.PREDEC:
743             if (!ltd.isNumber())
744                 throw new Error();
745             //5.6.1 Unary Numeric Promotion
746             if (ltd.isByte()||ltd.isShort()||ltd.isInt())
747                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
748             else
749                 lefttype=ltd;
750             on.setLeftType(lefttype);
751             on.setType(lefttype);
752             break;
753         default:
754             throw new Error();
755         }
756
757      
758
759         if (td!=null)
760             if (!typeutil.isSuperorType(td, on.getType())) {
761                 System.out.println(td);
762                 System.out.println(on.getType());
763                 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));     
764             }
765     }
766 }