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