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