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