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