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