some bug fix
[IRC.git] / Robust / src / IR / Tree / SemanticCheck.java
1 package IR.Tree;
2
3 import java.util.*;
4
5 import IR.*;
6
7 public class SemanticCheck {
8   State state;
9   TypeUtil typeutil;
10   Stack loopstack;
11   HashSet toanalyze;
12   HashSet completed;
13
14
15   public SemanticCheck(State state, TypeUtil tu) {
16     this.state=state;
17     this.typeutil=tu;
18     this.loopstack=new Stack();
19     this.toanalyze=new HashSet();
20     this.completed=new HashSet();
21   }
22
23   public ClassDescriptor getClass(String classname) {
24     ClassDescriptor cd=typeutil.getClass(classname, toanalyze);
25     checkClass(cd);
26     return cd;
27   }
28
29   private void checkClass(ClassDescriptor cd) {
30     if (!completed.contains(cd)) {
31       completed.add(cd);
32       
33       //Set superclass link up
34       if (cd.getSuper()!=null) {
35         cd.setSuper(getClass(cd.getSuper()));
36         // Link together Field, Method, and Flag tables so classes
37         // inherit these from their superclasses
38         cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
39         cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
40         cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable());
41       }
42       if(state.MGC) {
43         // TODO add version for normal Java later
44         // Link together Field, Method tables do classes inherit these from 
45         // their ancestor interfaces
46         Vector<String> sifv = cd.getSuperInterface();
47         for(int i = 0; i < sifv.size(); i++) {
48           ClassDescriptor superif = getClass(sifv.elementAt(i));
49           cd.addSuperInterfaces(superif);
50           cd.getFieldTable().addParentIF(superif.getFieldTable());
51           cd.getMethodTable().addParentIF(superif.getMethodTable());
52         }
53       }
54       
55       /* Check to see that fields are well typed */
56       for(Iterator field_it=cd.getFields(); field_it.hasNext();) {
57         FieldDescriptor fd=(FieldDescriptor)field_it.next();
58         checkField(cd,fd);
59       }
60       
61       boolean hasConstructor = false;
62       for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
63         MethodDescriptor md=(MethodDescriptor)method_it.next();
64         checkMethod(cd,md);
65     hasConstructor |= md.isConstructor();
66       }
67       if(!hasConstructor) {
68         // add a default constructor for this class
69         MethodDescriptor md = new MethodDescriptor(new Modifiers(Modifiers.PUBLIC),
70             cd.getSymbol(), false);
71         BlockNode bn=new BlockNode();
72         state.addTreeCode(md,bn);
73         cd.addMethod(md);
74         checkMethod(cd,md);
75       }
76     }
77   }
78
79   public void semanticCheck() {
80     SymbolTable classtable=state.getClassSymbolTable();
81     toanalyze.addAll(classtable.getValueSet());
82     toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
83
84     // Do methods next
85     while(!toanalyze.isEmpty()) {
86       Object obj=toanalyze.iterator().next();
87       if (obj instanceof TaskDescriptor) {
88         toanalyze.remove(obj);
89         TaskDescriptor td=(TaskDescriptor)obj;
90         try {
91           checkTask(td);
92         } catch( Error e ) {
93             System.out.println( "Error in "+td );
94             throw e;
95         }
96       } else {
97         ClassDescriptor cd=(ClassDescriptor)obj;
98         toanalyze.remove(cd);
99         //need to initialize typeutil object here...only place we can
100         //get class descriptors without first calling getclass
101         getClass(cd.getSymbol());
102         for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
103           MethodDescriptor md=(MethodDescriptor)method_it.next();
104           try {
105             checkMethodBody(cd,md);
106           } catch( Error e ) {
107             System.out.println( "Error in "+md );
108             throw e;
109           }
110         }
111       }
112     }
113   }
114
115   public void checkTypeDescriptor(TypeDescriptor td) {
116     if (td.isPrimitive())
117       return;       /* Done */
118     else if (td.isClass()) {
119       String name=td.toString();
120       int index = name.lastIndexOf('.');
121       if(index != -1) {
122         name = name.substring(index+1);
123       }
124       ClassDescriptor field_cd=getClass(name);
125       if (field_cd==null)
126         throw new Error("Undefined class "+name);
127       td.setClassDescriptor(field_cd);
128       return;
129     } else if (td.isTag())
130       return;
131     else
132       throw new Error();
133   }
134
135   public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
136     checkTypeDescriptor(fd.getType());
137   }
138
139   public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
140     if (ccs==null)
141       return;       /* No constraint checks to check */
142     for(int i=0; i<ccs.size(); i++) {
143       ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
144
145       for(int j=0; j<cc.numArgs(); j++) {
146         ExpressionNode en=cc.getArg(j);
147         checkExpressionNode(td,nametable,en,null);
148       }
149     }
150   }
151
152   public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
153     if (vfe==null)
154       return;       /* No flag effects to check */
155     for(int i=0; i<vfe.size(); i++) {
156       FlagEffects fe=(FlagEffects) vfe.get(i);
157       String varname=fe.getName();
158       //Make sure the variable is declared as a parameter to the task
159       VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
160       if (vd==null)
161         throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
162       fe.setVar(vd);
163
164       //Make sure it correspods to a class
165       TypeDescriptor type_d=vd.getType();
166       if (!type_d.isClass())
167         throw new Error("Cannot have non-object argument for flag_effect");
168
169       ClassDescriptor cd=type_d.getClassDesc();
170       for(int j=0; j<fe.numEffects(); j++) {
171         FlagEffect flag=fe.getEffect(j);
172         String name=flag.getName();
173         FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
174         //Make sure the flag is declared
175         if (flag_d==null)
176           throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
177         if (flag_d.getExternal())
178           throw new Error("Attempting to modify external flag: "+name);
179         flag.setFlag(flag_d);
180       }
181       for(int j=0; j<fe.numTagEffects(); j++) {
182         TagEffect tag=fe.getTagEffect(j);
183         String name=tag.getName();
184
185         Descriptor d=(Descriptor)nametable.get(name);
186         if (d==null)
187           throw new Error("Tag descriptor "+name+" undeclared");
188         else if (!(d instanceof TagVarDescriptor))
189           throw new Error(name+" is not a tag descriptor");
190         tag.setTag((TagVarDescriptor)d);
191       }
192     }
193   }
194
195   public void checkTask(TaskDescriptor td) {
196     for(int i=0; i<td.numParameters(); i++) {
197       /* Check that parameter is well typed */
198       TypeDescriptor param_type=td.getParamType(i);
199       checkTypeDescriptor(param_type);
200
201       /* Check the parameter's flag expression is well formed */
202       FlagExpressionNode fen=td.getFlag(td.getParameter(i));
203       if (!param_type.isClass())
204         throw new Error("Cannot have non-object argument to a task");
205       ClassDescriptor cd=param_type.getClassDesc();
206       if (fen!=null)
207         checkFlagExpressionNode(cd, fen);
208     }
209
210     checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
211     /* Check that the task code is valid */
212     BlockNode bn=state.getMethodBody(td);
213     checkBlockNode(td, td.getParameterTable(),bn);
214   }
215
216   public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
217     switch(fen.kind()) {
218     case Kind.FlagOpNode:
219     {
220       FlagOpNode fon=(FlagOpNode)fen;
221       checkFlagExpressionNode(cd, fon.getLeft());
222       if (fon.getRight()!=null)
223         checkFlagExpressionNode(cd, fon.getRight());
224       break;
225     }
226
227     case Kind.FlagNode:
228     {
229       FlagNode fn=(FlagNode)fen;
230       String name=fn.getFlagName();
231       FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
232       if (fd==null)
233         throw new Error("Undeclared flag: "+name);
234       fn.setFlag(fd);
235       break;
236     }
237
238     default:
239       throw new Error("Unrecognized FlagExpressionNode");
240     }
241   }
242
243   public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
244     if(state.MGC) {
245       // TODO add version for normal Java later
246       /* Check for abstract methods */
247       if(md.isAbstract()) {
248         if(!cd.isAbstract() && !cd.isInterface()) {
249           throw new Error("Error! The non-abstract Class " + cd.getSymbol() + " contains an abstract method " + md.getSymbol());
250         }
251       }
252     }
253     /* Check return type */
254     if (!md.isConstructor() && !md.isStaticBlock())
255       if (!md.getReturnType().isVoid())
256         checkTypeDescriptor(md.getReturnType());
257
258     for(int i=0; i<md.numParameters(); i++) {
259       TypeDescriptor param_type=md.getParamType(i);
260       checkTypeDescriptor(param_type);
261     }
262     /* Link the naming environments */
263     if (!md.isStatic())     /* Fields aren't accessible directly in a static method, so don't link in this table */
264       md.getParameterTable().setParent(cd.getFieldTable());
265     md.setClassDesc(cd);
266     if (!md.isStatic()) {
267       VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
268       md.setThis(thisvd);
269     }
270   }
271
272   public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
273     ClassDescriptor superdesc=cd.getSuperDesc();
274     if (superdesc!=null) {
275       Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
276       for(Iterator methodit=possiblematches.iterator(); methodit.hasNext();) {
277         MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
278         if (md.matches(matchmd)) {
279           if (matchmd.getModifiers().isFinal()) {
280             throw new Error("Try to override final method in method:"+md+" declared in  "+cd);
281           }
282         }
283       }
284     }
285     BlockNode bn=state.getMethodBody(md);
286     checkBlockNode(md, md.getParameterTable(),bn);
287   }
288
289   public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
290     /* Link in the naming environment */
291     bn.getVarTable().setParent(nametable);
292     for(int i=0; i<bn.size(); i++) {
293       BlockStatementNode bsn=bn.get(i);
294       checkBlockStatementNode(md, bn.getVarTable(),bsn);
295     }
296   }
297
298   public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
299     switch(bsn.kind()) {
300     case Kind.BlockExpressionNode:
301       checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
302       return;
303
304     case Kind.DeclarationNode:
305       checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
306       return;
307
308     case Kind.TagDeclarationNode:
309       checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
310       return;
311
312     case Kind.IfStatementNode:
313       checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
314       return;
315       
316     case Kind.SwitchStatementNode:
317       checkSwitchStatementNode(md, nametable, (SwitchStatementNode)bsn);
318       return;
319
320     case Kind.LoopNode:
321       checkLoopNode(md, nametable, (LoopNode)bsn);
322       return;
323
324     case Kind.ReturnNode:
325       checkReturnNode(md, nametable, (ReturnNode)bsn);
326       return;
327
328     case Kind.TaskExitNode:
329       checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
330       return;
331
332     case Kind.SubBlockNode:
333       checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
334       return;
335
336     case Kind.AtomicNode:
337       checkAtomicNode(md, nametable, (AtomicNode)bsn);
338       return;
339
340     case Kind.SynchronizedNode:
341       checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
342       return;
343
344     case Kind.ContinueBreakNode:
345         checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
346         return;
347
348     case Kind.SESENode:
349     case Kind.GenReachNode:
350       // do nothing, no semantic check for SESEs
351       return;
352     }
353
354     throw new Error();
355   }
356
357   void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
358     checkExpressionNode(md, nametable, ben.getExpression(), null);
359   }
360
361   void checkDeclarationNode(Descriptor md, SymbolTable nametable,  DeclarationNode dn) {
362     VarDescriptor vd=dn.getVarDescriptor();
363     checkTypeDescriptor(vd.getType());
364     Descriptor d=nametable.get(vd.getSymbol());
365     if ((d==null)||
366         (d instanceof FieldDescriptor)) {
367       nametable.add(vd);
368     } else
369       throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
370     if (dn.getExpression()!=null)
371       checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
372   }
373
374   void checkTagDeclarationNode(Descriptor md, SymbolTable nametable,  TagDeclarationNode dn) {
375     TagVarDescriptor vd=dn.getTagVarDescriptor();
376     Descriptor d=nametable.get(vd.getSymbol());
377     if ((d==null)||
378         (d instanceof FieldDescriptor)) {
379       nametable.add(vd);
380     } else
381       throw new Error(vd.getSymbol()+" defined a second time");
382   }
383
384   void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
385     checkBlockNode(md, nametable, sbn.getBlockNode());
386   }
387
388   void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
389     checkBlockNode(md, nametable, sbn.getBlockNode());
390   }
391
392   void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
393     checkBlockNode(md, nametable, sbn.getBlockNode());
394     //todo this could be Object
395     checkExpressionNode(md, nametable, sbn.getExpr(), null);
396   }
397
398   void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
399       if (loopstack.empty())
400           throw new Error("continue/break outside of loop");
401       Object o = loopstack.peek();
402       if(o instanceof LoopNode) {
403         LoopNode ln=(LoopNode)o;
404         cbn.setLoop(ln);
405       }
406   }
407
408   void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
409     if (d instanceof TaskDescriptor)
410       throw new Error("Illegal return appears in Task: "+d.getSymbol());
411     MethodDescriptor md=(MethodDescriptor)d;
412     if (rn.getReturnExpression()!=null)
413       if (md.getReturnType()==null)
414         throw new Error("Constructor can't return something.");
415       else if (md.getReturnType().isVoid())
416         throw new Error(md+" is void");
417       else
418         checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
419     else
420     if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
421       throw new Error("Need to return something for "+md);
422   }
423
424   void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
425     if (md instanceof MethodDescriptor)
426       throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
427     checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
428     checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
429   }
430
431   void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
432     checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
433     checkBlockNode(md, nametable, isn.getTrueBlock());
434     if (isn.getFalseBlock()!=null)
435       checkBlockNode(md, nametable, isn.getFalseBlock());
436   }
437   
438   void checkSwitchStatementNode(Descriptor md, SymbolTable nametable, SwitchStatementNode ssn) {
439     checkExpressionNode(md, nametable, ssn.getCondition(), new TypeDescriptor(TypeDescriptor.INT));
440     
441     BlockNode sbn = ssn.getSwitchBody();
442     boolean hasdefault = false;
443     for(int i = 0; i < sbn.size(); i++) {
444       boolean containdefault = checkSwitchBlockNode(md, nametable, (SwitchBlockNode)sbn.get(i));
445       if(hasdefault && containdefault) {
446         throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
447       }
448       hasdefault = containdefault;
449     }
450   }
451   
452   boolean checkSwitchBlockNode(Descriptor md, SymbolTable nametable, SwitchBlockNode sbn) {
453     Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
454     int defaultb = 0;
455     for(int i = 0; i < slnv.size(); i++) {
456       if(slnv.elementAt(i).isdefault) {
457         defaultb++;
458       } else {
459         checkConstantExpressionNode(md, nametable, slnv.elementAt(i).getCondition(), new TypeDescriptor(TypeDescriptor.INT));
460       }
461     }
462     if(defaultb > 1) {
463       throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
464     } else {
465       loopstack.push(sbn);
466       checkBlockNode(md, nametable, sbn.getSwitchBlockStatement());
467       loopstack.pop();
468       return (defaultb > 0);
469     }
470   }
471   
472   void checkConstantExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
473     switch(en.kind()) {
474     case Kind.FieldAccessNode:
475       checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
476       return;
477      
478     case Kind.LiteralNode:
479       checkLiteralNode(md,nametable,(LiteralNode)en,td);
480       return;
481       
482     case Kind.NameNode:
483       checkNameNode(md,nametable,(NameNode)en,td);
484       return;
485       
486     case Kind.OpNode:
487       checkOpNode(md, nametable, (OpNode)en, td);
488       return;
489     }
490     throw new Error();
491   }
492
493   void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
494     switch(en.kind()) {
495     case Kind.AssignmentNode:
496       checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
497       return;
498
499     case Kind.CastNode:
500       checkCastNode(md,nametable,(CastNode)en,td);
501       return;
502
503     case Kind.CreateObjectNode:
504       checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
505       return;
506
507     case Kind.FieldAccessNode:
508       checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
509       return;
510
511     case Kind.ArrayAccessNode:
512       checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
513       return;
514
515     case Kind.LiteralNode:
516       checkLiteralNode(md,nametable,(LiteralNode)en,td);
517       return;
518
519     case Kind.MethodInvokeNode:
520       checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
521       return;
522
523     case Kind.NameNode:
524       checkNameNode(md,nametable,(NameNode)en,td);
525       return;
526
527     case Kind.OpNode:
528       checkOpNode(md,nametable,(OpNode)en,td);
529       return;
530
531     case Kind.OffsetNode:
532       checkOffsetNode(md, nametable, (OffsetNode)en, td);
533       return;
534
535     case Kind.TertiaryNode:
536       checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
537       return;
538       
539     case Kind.InstanceOfNode:
540       checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
541       return;
542
543     case Kind.ArrayInitializerNode:
544       checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
545       return;
546      
547     case Kind.ClassTypeNode:
548       checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
549       return;
550     }
551     throw new Error();
552   }
553
554   void checkClassTypeNode(Descriptor md, SymbolTable nametable, ClassTypeNode tn, TypeDescriptor td) {
555     checkTypeDescriptor(tn.getType());
556   }
557   
558   void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
559     /* Get type descriptor */
560     if (cn.getType()==null) {
561       NameDescriptor typenamed=cn.getTypeName().getName();
562       String typename=typenamed.toString();
563       TypeDescriptor ntd=new TypeDescriptor(getClass(typename));
564       cn.setType(ntd);
565     }
566
567     /* Check the type descriptor */
568     TypeDescriptor cast_type=cn.getType();
569     checkTypeDescriptor(cast_type);
570
571     /* Type check */
572     if (td!=null) {
573       if (!typeutil.isSuperorType(td,cast_type))
574         throw new Error("Cast node returns "+cast_type+", but need "+td);
575     }
576
577     ExpressionNode en=cn.getExpression();
578     checkExpressionNode(md, nametable, en, null);
579     TypeDescriptor etd=en.getType();
580     if (typeutil.isSuperorType(cast_type,etd))     /* Cast trivially succeeds */
581       return;
582
583     if (typeutil.isSuperorType(etd,cast_type))     /* Cast may succeed */
584       return;
585     if (typeutil.isCastable(etd, cast_type))
586       return;
587
588     /* Different branches */
589     /* TODO: change if add interfaces */
590     throw new Error("Cast will always fail\n"+cn.printNode(0));
591   }
592
593   void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
594     ExpressionNode left=fan.getExpression();
595     checkExpressionNode(md,nametable,left,null);
596     TypeDescriptor ltd=left.getType();
597     String fieldname=fan.getFieldName();
598
599     FieldDescriptor fd=null;
600     if (ltd.isArray()&&fieldname.equals("length"))
601       fd=FieldDescriptor.arrayLength;
602     else
603       fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
604     if(state.MGC) {
605       // TODO add version for normal Java later
606     if(ltd.isStatic()) {
607       // the field access is using a class name directly
608       if(ltd.getClassDesc().isEnum()) {
609         int value = ltd.getClassDesc().getEnumConstant(fieldname);
610         if(-1 == value) {
611           // check if this field is an enum constant
612           throw new Error(fieldname + " is not an enum constant in "+fan.printNode(0)+" in "+md);
613         }
614         fd = new FieldDescriptor(new Modifiers(Modifiers.PUBLIC|Modifiers.FINAL), new TypeDescriptor(TypeDescriptor.INT), fieldname, null, false);
615         fd.setAsEnum();
616         fd.setEnumValue(value);
617       } else if(fd.isStatic()) {
618         // check if this field is a static field
619         if(fd.getExpressionNode() != null) {
620           checkExpressionNode(md,nametable,fd.getExpressionNode(),null);
621         }
622       } else {
623         throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
624       }
625     } 
626     }
627     if (fd==null)
628       throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
629
630     if (fd.getType().iswrapper()) {
631       FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
632       fan2.setField(fd);
633       fan.left=fan2;
634       fan.fieldname="value";
635
636       ExpressionNode leftwr=fan.getExpression();
637       TypeDescriptor ltdwr=leftwr.getType();
638       String fieldnamewr=fan.getFieldName();
639       FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
640       fan.setField(fdwr);
641       if (fdwr==null)
642           throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
643     } else {
644       fan.setField(fd);
645     }
646     if (td!=null) {
647       if (!typeutil.isSuperorType(td,fan.getType()))
648         throw new Error("Field node returns "+fan.getType()+", but need "+td);
649     }
650   }
651
652   void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
653     ExpressionNode left=aan.getExpression();
654     checkExpressionNode(md,nametable,left,null);
655
656     checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
657     TypeDescriptor ltd=left.getType();
658     if (ltd.dereference().iswrapper()) {
659       aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
660     }
661
662     if (td!=null)
663       if (!typeutil.isSuperorType(td,aan.getType()))
664         throw new Error("Field node returns "+aan.getType()+", but need "+td);
665   }
666
667   void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
668     /* Resolve the type */
669     Object o=ln.getValue();
670     if (ln.getTypeString().equals("null")) {
671       ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
672     } else if (o instanceof Integer) {
673       ln.setType(new TypeDescriptor(TypeDescriptor.INT));
674     } else if (o instanceof Long) {
675       ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
676     } else if (o instanceof Float) {
677       ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
678     } else if (o instanceof Boolean) {
679       ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
680     } else if (o instanceof Double) {
681       ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
682     } else if (o instanceof Character) {
683       ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
684     } else if (o instanceof String) {
685       ln.setType(new TypeDescriptor(getClass(TypeUtil.StringClass)));
686     }
687
688     if (td!=null)
689       if (!typeutil.isSuperorType(td,ln.getType())) {
690         Long l = ln.evaluate();
691         if((ln.getType().isByte() || ln.getType().isShort() 
692             || ln.getType().isChar() || ln.getType().isInt()) 
693             && (l != null) 
694             && (td.isByte() || td.isShort() || td.isChar() 
695                 || td.isInt() || td.isLong())) {
696           long lnvalue = l.longValue();
697           if((td.isByte() && ((lnvalue > 127) || (lnvalue < -128))) 
698               || (td.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
699               || (td.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
700               || (td.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
701               || (td.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
702             throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
703           }
704         } else {
705           throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
706         }
707       }
708   }
709
710   void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
711     NameDescriptor nd=nn.getName();
712     if (nd.getBase()!=null) {
713       /* Big hack */
714       /* Rewrite NameNode */
715       ExpressionNode en=translateNameDescriptorintoExpression(nd);
716       nn.setExpression(en);
717       checkExpressionNode(md,nametable,en,td);
718     } else {
719       String varname=nd.toString();
720       if(varname.equals("this")) {
721         // "this"
722         nn.setVar((VarDescriptor)nametable.get("this")); 
723         return;
724       }
725       Descriptor d=(Descriptor)nametable.get(varname);
726       if (d==null) {
727         if(state.MGC) {
728           // TODO add version for normal Java later
729         ClassDescriptor cd = null;
730         if(((MethodDescriptor)md).isStaticBlock()) {
731           // this is a static block, all the accessed fields should be static field
732           cd = ((MethodDescriptor)md).getClassDesc();
733           SymbolTable fieldtbl = cd.getFieldTable();
734           FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
735           if((fd == null) || (!fd.isStatic())){
736             // no such field in the class, check if this is a class
737             if(varname.equals("this")) {
738               throw new Error("Error: access this obj in a static block");
739             }
740             cd=getClass(varname);
741             if(cd != null) {
742               // this is a class name
743               nn.setClassDesc(cd);
744               return;
745             } else {
746               throw new Error("Name "+varname+" should not be used in static block: "+md);
747             }
748           } else {
749             // this is a static field
750             nn.setField(fd);
751             nn.setClassDesc(cd);
752             return;
753           }
754         } else {
755           // check if the var is a static field of the class
756           if(md instanceof MethodDescriptor) {
757             cd = ((MethodDescriptor)md).getClassDesc();
758             FieldDescriptor fd = (FieldDescriptor)cd.getFieldTable().get(varname);
759             if((fd != null) && (fd.isStatic())) {
760               nn.setField(fd);
761               nn.setClassDesc(cd);
762               if (td!=null)
763                 if (!typeutil.isSuperorType(td,nn.getType()))
764                   throw new Error("Field node returns "+nn.getType()+", but need "+td);
765               return;
766             } else if(fd != null) {
767               throw new Error("Name "+varname+" should not be used in " + md);
768             }
769           }
770           cd=getClass(varname);
771           if(cd != null) {
772             // this is a class name
773             nn.setClassDesc(cd);
774             return;
775           } else {
776             throw new Error("Name "+varname+" undefined in: "+md);
777           }
778         }
779         } else {
780           throw new Error("Name "+varname+" undefined in: "+md);
781         }
782       }
783       if (d instanceof VarDescriptor) {
784         nn.setVar(d);
785       } else if (d instanceof FieldDescriptor) {
786         FieldDescriptor fd=(FieldDescriptor)d;
787         if (fd.getType().iswrapper()) {
788           String id=nd.getIdentifier();
789           NameDescriptor base=nd.getBase();
790           NameNode n=new NameNode(nn.getName());
791           n.setField(fd);
792           n.setVar((VarDescriptor)nametable.get("this"));        /* Need a pointer to this */
793           FieldAccessNode fan=new FieldAccessNode(n,"value");
794           FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
795           fan.setField(fdval);
796           nn.setExpression(fan);
797         } else {
798           nn.setField(fd);
799           nn.setVar((VarDescriptor)nametable.get("this"));        /* Need a pointer to this */
800         }
801       } else if (d instanceof TagVarDescriptor) {
802         nn.setVar(d);
803       } else throw new Error("Wrong type of descriptor");
804       if (td!=null)
805         if (!typeutil.isSuperorType(td,nn.getType()))
806           throw new Error("Field node returns "+nn.getType()+", but need "+td);
807     }
808   }
809
810   void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
811     TypeDescriptor ltd=ofn.td;
812     checkTypeDescriptor(ltd);
813     
814     String fieldname = ofn.fieldname;
815     FieldDescriptor fd=null;
816     if (ltd.isArray()&&fieldname.equals("length")) {
817       fd=FieldDescriptor.arrayLength;
818     } else {
819       fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
820     }
821
822     ofn.setField(fd);
823     checkField(ltd.getClassDesc(), fd);
824
825     if (fd==null)
826       throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
827
828     if (td!=null) {
829       if (!typeutil.isSuperorType(td, ofn.getType())) {
830         System.out.println(td);
831         System.out.println(ofn.getType());
832         throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
833       }
834     }
835   }
836
837
838   void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
839     checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
840     checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
841     checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
842   }
843
844   void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
845     if (td!=null&&!td.isBoolean())
846       throw new Error("Expecting type "+td+"for instanceof expression");
847     
848     checkTypeDescriptor(tn.getExprType());
849     checkExpressionNode(md, nametable, tn.getExpr(), null);
850   }
851
852   void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
853     Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
854     for( int i = 0; i < ain.numVarInitializers(); ++i ) {
855       checkExpressionNode(md, nametable, ain.getVarInitializer(i), td==null?td:td.dereference());
856       vec_type.add(ain.getVarInitializer(i).getType());
857     }
858     // descide the type of this variableInitializerNode
859     TypeDescriptor out_type = vec_type.elementAt(0);
860     for(int i = 1; i < vec_type.size(); i++) {
861       TypeDescriptor tmp_type = vec_type.elementAt(i);
862       if(out_type == null) {
863         if(tmp_type != null) {
864           out_type = tmp_type;
865         }
866       } else if(out_type.isNull()) {
867         if(!tmp_type.isNull() ) {
868           if(!tmp_type.isArray()) {
869             throw new Error("Error: mixed type in var initializer list");
870           } else {
871             out_type = tmp_type;
872           }
873         }
874       } else if(out_type.isArray()) {
875         if(tmp_type.isArray()) {
876           if(tmp_type.getArrayCount() > out_type.getArrayCount()) {
877             out_type = tmp_type;
878           }
879         } else if((tmp_type != null) && (!tmp_type.isNull())) {
880           throw new Error("Error: mixed type in var initializer list");
881         }
882       } else if(out_type.isInt()) {
883         if(!tmp_type.isInt()) {
884           throw new Error("Error: mixed type in var initializer list");
885         }
886       } else if(out_type.isString()) {
887         if(!tmp_type.isString()) {
888           throw new Error("Error: mixed type in var initializer list");
889         }
890       }
891     }
892     if(out_type != null) {
893       out_type = out_type.makeArray(state);
894       out_type.setStatic();
895     }
896     ain.setType(out_type);
897   }
898
899   void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
900     boolean postinc=true;
901     if (an.getOperation().getBaseOp()==null||
902         (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
903          an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
904       postinc=false;
905     if (!postinc)      
906       checkExpressionNode(md, nametable, an.getSrc(),td);
907     //TODO: Need check on validity of operation here
908     if (!((an.getDest() instanceof FieldAccessNode)||
909           (an.getDest() instanceof ArrayAccessNode)||
910           (an.getDest() instanceof NameNode)))
911       throw new Error("Bad lside in "+an.printNode(0));
912     checkExpressionNode(md, nametable, an.getDest(), null);
913
914     /* We want parameter variables to tasks to be immutable */
915     if (md instanceof TaskDescriptor) {
916       if (an.getDest() instanceof NameNode) {
917         NameNode nn=(NameNode)an.getDest();
918         if (nn.getVar()!=null) {
919           if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
920             throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
921         }
922       }
923     }
924
925     if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
926       //String add
927       ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
928       TypeDescriptor stringtd=new TypeDescriptor(stringcl);
929       NameDescriptor nd=new NameDescriptor("String");
930       NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
931
932       if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
933         MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
934         rightmin.addArgument(an.getSrc());
935         an.right=rightmin;
936         checkExpressionNode(md, nametable, an.getSrc(), null);
937       }
938     }
939
940     if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
941       TypeDescriptor dt = an.getDest().getType();
942       TypeDescriptor st = an.getSrc().getType();
943       if(an.getSrc().kind() == Kind.ArrayInitializerNode) {
944         if(dt.getArrayCount() != st.getArrayCount()) {
945           throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
946         } else {
947           do {
948             dt = dt.dereference();
949             st = st.dereference();
950           } while(dt.isArray());
951           if((st.isByte() || st.isShort() || st.isChar() || st.isInt()) 
952               && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
953             return;
954           } else {
955             throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
956           }
957         }
958       } else {
959         Long l = an.getSrc().evaluate();
960         if((st.isByte() || st.isShort() || st.isChar() || st.isInt()) 
961             && (l != null) 
962             && (dt.isByte() || dt.isShort() || dt.isChar() || dt.isInt() || dt.isLong())) {
963           long lnvalue = l.longValue();
964           if((dt.isByte() && ((lnvalue > 127) || (lnvalue < -128))) 
965               || (dt.isShort() && ((lnvalue > 32767) || (lnvalue < -32768)))
966               || (dt.isChar() && ((lnvalue > 65535) || (lnvalue < 0)))
967               || (dt.isInt() && ((lnvalue > 2147483647) || (lnvalue < -2147483648)))
968               || (dt.isLong() && ((lnvalue > 9223372036854775807L) || (lnvalue < -9223372036854775808L)))) {
969             throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
970           }
971         } else {
972           throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
973         }
974       }
975     }
976   }
977
978   void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
979       loopstack.push(ln);
980     if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
981       checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
982       checkBlockNode(md, nametable, ln.getBody());
983     } else {
984       //For loop case
985       /* Link in the initializer naming environment */
986       BlockNode bn=ln.getInitializer();
987       bn.getVarTable().setParent(nametable);
988       for(int i=0; i<bn.size(); i++) {
989         BlockStatementNode bsn=bn.get(i);
990         checkBlockStatementNode(md, bn.getVarTable(),bsn);
991       }
992       //check the condition
993       checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
994       checkBlockNode(md, bn.getVarTable(), ln.getBody());
995       checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
996     }
997     loopstack.pop();
998   }
999
1000
1001   void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
1002     TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
1003     for(int i=0; i<con.numArgs(); i++) {
1004       ExpressionNode en=con.getArg(i);
1005       checkExpressionNode(md,nametable,en,null);
1006       tdarray[i]=en.getType();
1007     }
1008
1009     TypeDescriptor typetolookin=con.getType();
1010     checkTypeDescriptor(typetolookin);
1011
1012     if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
1013       throw new Error(typetolookin + " isn't a "+td);
1014     
1015     /* Check Array Initializers */
1016     if(state.MGC && (con.getArrayInitializer() != null)) {
1017       checkArrayInitializerNode(md, nametable, con.getArrayInitializer(), td);
1018     }
1019
1020     /* Check flag effects */
1021     if (con.getFlagEffects()!=null) {
1022       FlagEffects fe=con.getFlagEffects();
1023       ClassDescriptor cd=typetolookin.getClassDesc();
1024
1025       for(int j=0; j<fe.numEffects(); j++) {
1026         FlagEffect flag=fe.getEffect(j);
1027         String name=flag.getName();
1028         FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
1029         //Make sure the flag is declared
1030         if (flag_d==null)
1031           throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
1032         if (flag_d.getExternal())
1033           throw new Error("Attempting to modify external flag: "+name);
1034         flag.setFlag(flag_d);
1035       }
1036       for(int j=0; j<fe.numTagEffects(); j++) {
1037         TagEffect tag=fe.getTagEffect(j);
1038         String name=tag.getName();
1039
1040         Descriptor d=(Descriptor)nametable.get(name);
1041         if (d==null)
1042           throw new Error("Tag descriptor "+name+" undeclared");
1043         else if (!(d instanceof TagVarDescriptor))
1044           throw new Error(name+" is not a tag descriptor");
1045         tag.setTag((TagVarDescriptor)d);
1046       }
1047     }
1048
1049     if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
1050       throw new Error("Can't allocate primitive type:"+con.printNode(0));
1051
1052     if (!typetolookin.isArray()) {
1053       //Array's don't need constructor calls
1054       ClassDescriptor classtolookin=typetolookin.getClassDesc();
1055
1056       Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
1057       MethodDescriptor bestmd=null;
1058 NextMethod:
1059       for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
1060         MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1061         /* Need correct number of parameters */
1062         if (con.numArgs()!=currmd.numParameters())
1063           continue;
1064         for(int i=0; i<con.numArgs(); i++) {
1065           if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1066             continue NextMethod;
1067         }
1068         /* Local allocations can't call global allocator */
1069         if (!con.isGlobal()&&currmd.isGlobal())
1070           continue;
1071
1072         /* Method okay so far */
1073         if (bestmd==null)
1074           bestmd=currmd;
1075         else {
1076           if (typeutil.isMoreSpecific(currmd,bestmd)) {
1077             bestmd=currmd;
1078           } else if (con.isGlobal()&&match(currmd, bestmd)) {
1079             if (currmd.isGlobal()&&!bestmd.isGlobal())
1080               bestmd=currmd;
1081             else if (currmd.isGlobal()&&bestmd.isGlobal())
1082               throw new Error();
1083           } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
1084             throw new Error("No method is most specific");
1085           }
1086
1087           /* Is this more specific than bestmd */
1088         }
1089       }
1090       if (bestmd==null)
1091         throw new Error("No method found for "+con.printNode(0)+" in "+md);
1092       con.setConstructor(bestmd);
1093     }
1094   }
1095
1096
1097   /** Check to see if md1 is the same specificity as md2.*/
1098
1099   boolean match(MethodDescriptor md1, MethodDescriptor md2) {
1100     /* Checks if md1 is more specific than md2 */
1101     if (md1.numParameters()!=md2.numParameters())
1102       throw new Error();
1103     for(int i=0; i<md1.numParameters(); i++) {
1104       if (!md2.getParamType(i).equals(md1.getParamType(i)))
1105         return false;
1106     }
1107     if (!md2.getReturnType().equals(md1.getReturnType()))
1108       return false;
1109
1110     if (!md2.getClassDesc().equals(md1.getClassDesc()))
1111       return false;
1112
1113     return true;
1114   }
1115
1116
1117
1118   ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
1119     String id=nd.getIdentifier();
1120     NameDescriptor base=nd.getBase();
1121     if (base==null)
1122       return new NameNode(nd);
1123     else
1124       return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
1125   }
1126
1127
1128   void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
1129     /*Typecheck subexpressions
1130        and get types for expressions*/
1131
1132     boolean isstatic = false;
1133     if(state.MGC) {
1134       if((md instanceof MethodDescriptor) && ((MethodDescriptor)md).isStatic()) {
1135         isstatic = true;
1136       }
1137     }
1138     TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
1139     for(int i=0; i<min.numArgs(); i++) {
1140       ExpressionNode en=min.getArg(i);
1141       checkExpressionNode(md,nametable,en,null);
1142       tdarray[i]=en.getType();
1143       if(state.MGC && en.getType().isClass() && en.getType().getClassDesc().isEnum()) {
1144         tdarray[i] = new TypeDescriptor(TypeDescriptor.INT);
1145       }
1146     }
1147     TypeDescriptor typetolookin=null;
1148     if (min.getExpression()!=null) {
1149       checkExpressionNode(md,nametable,min.getExpression(),null);
1150       typetolookin=min.getExpression().getType();
1151       //if (typetolookin==null)
1152       //throw new Error(md+" has null return type");
1153
1154     } else if (min.getBaseName()!=null) {
1155       String rootname=min.getBaseName().getRoot();
1156       if (rootname.equals("super")) {
1157         ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1158         typetolookin=new TypeDescriptor(supercd);
1159       } else if (rootname.equals("this")) {
1160         if(isstatic) {
1161           throw new Error("use this object in static method md = "+ md.toString());
1162         }
1163         ClassDescriptor cd=((MethodDescriptor)md).getClassDesc();
1164         typetolookin=new TypeDescriptor(cd);
1165       } else if (nametable.get(rootname)!=null) {
1166         //we have an expression
1167         min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
1168         checkExpressionNode(md, nametable, min.getExpression(), null);
1169         typetolookin=min.getExpression().getType();
1170       } else {
1171         if(state.MGC) {
1172           if(!min.getBaseName().getSymbol().equals("System.out")) {
1173             ExpressionNode nn = translateNameDescriptorintoExpression(min.getBaseName());
1174             checkExpressionNode(md, nametable, nn, null);
1175             typetolookin = nn.getType();
1176             if(!((nn.kind()== Kind.NameNode) && (((NameNode)nn).getField() == null)
1177                 && (((NameNode)nn).getVar() == null) && (((NameNode)nn).getExpression() == null))) {
1178               // this is not a pure class name, need to add to 
1179               min.setExpression(nn);
1180             }
1181           } else {
1182             //we have a type
1183             ClassDescriptor cd = null;
1184             //if (min.getBaseName().getSymbol().equals("System.out"))
1185             cd=getClass("System");
1186             /*else {
1187             cd=getClass(min.getBaseName().getSymbol());
1188           }*/
1189             if (cd==null)
1190               throw new Error("md = "+ md.toString()+ "  "+min.getBaseName()+" undefined");
1191             typetolookin=new TypeDescriptor(cd);
1192           }
1193         } else {
1194           // we have a type
1195           ClassDescriptor cd = null;
1196           if (min.getBaseName().getSymbol().equals("System.out"))
1197             cd=getClass("System");
1198           else {
1199             cd=getClass(min.getBaseName().getSymbol());
1200           }
1201           if (cd==null)
1202             throw new Error("md = "+ md.toString()+ "  "+min.getBaseName()+" undefined");
1203           typetolookin=new TypeDescriptor(cd);
1204         }
1205       }
1206     } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
1207       ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
1208       min.methodid=supercd.getSymbol();
1209       typetolookin=new TypeDescriptor(supercd);
1210     } else if (md instanceof MethodDescriptor) {
1211       typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
1212     } else {
1213       /* If this a task descriptor we throw an error at this point */
1214       throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
1215     }
1216     if (!typetolookin.isClass())
1217       throw new Error("Error with method call to "+min.getMethodName());
1218     ClassDescriptor classtolookin=typetolookin.getClassDesc();
1219     //System.out.println("Method name="+min.getMethodName());
1220
1221     Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
1222     MethodDescriptor bestmd=null;
1223 NextMethod:
1224     for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
1225       MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1226       /* Need correct number of parameters */
1227       if (min.numArgs()!=currmd.numParameters())
1228         continue;
1229       for(int i=0; i<min.numArgs(); i++) {
1230         if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1231           continue NextMethod;
1232       }
1233       /* Method okay so far */
1234       if (bestmd==null)
1235         bestmd=currmd;
1236       else {
1237         if (typeutil.isMoreSpecific(currmd,bestmd)) {
1238           bestmd=currmd;
1239         } else if (!typeutil.isMoreSpecific(bestmd, currmd))
1240           throw new Error("No method is most specific");
1241
1242         /* Is this more specific than bestmd */
1243       }
1244     }
1245     if (bestmd==null)
1246       throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
1247     min.setMethod(bestmd);
1248
1249     if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td,  min.getType()))
1250       throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
1251     /* Check whether we need to set this parameter to implied this */
1252     if (! isstatic && !bestmd.isStatic()) {
1253       if (min.getExpression()==null) {
1254         ExpressionNode en=new NameNode(new NameDescriptor("this"));
1255         min.setExpression(en);
1256         checkExpressionNode(md, nametable, min.getExpression(), null);
1257       }
1258     }
1259   }
1260
1261
1262   void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
1263     checkExpressionNode(md, nametable, on.getLeft(), null);
1264     if (on.getRight()!=null)
1265       checkExpressionNode(md, nametable, on.getRight(), null);
1266     TypeDescriptor ltd=on.getLeft().getType();
1267     TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
1268     TypeDescriptor lefttype=null;
1269     TypeDescriptor righttype=null;
1270     Operation op=on.getOp();
1271
1272     switch(op.getOp()) {
1273     case Operation.LOGIC_OR:
1274     case Operation.LOGIC_AND:
1275       if (!(rtd.isBoolean()))
1276         throw new Error();
1277       on.setRightType(rtd);
1278
1279     case Operation.LOGIC_NOT:
1280       if (!(ltd.isBoolean()))
1281         throw new Error();
1282       //no promotion
1283       on.setLeftType(ltd);
1284
1285       on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1286       break;
1287
1288     case Operation.COMP:
1289       // 5.6.2 Binary Numeric Promotion
1290       //TODO unboxing of reference objects
1291       if (ltd.isDouble())
1292         throw new Error();
1293       else if (ltd.isFloat())
1294         throw new Error();
1295       else if (ltd.isLong())
1296         lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1297       else
1298         lefttype=new TypeDescriptor(TypeDescriptor.INT);
1299       on.setLeftType(lefttype);
1300       on.setType(lefttype);
1301       break;
1302
1303     case Operation.BIT_OR:
1304     case Operation.BIT_XOR:
1305     case Operation.BIT_AND:
1306       // 5.6.2 Binary Numeric Promotion
1307       //TODO unboxing of reference objects
1308       if (ltd.isDouble()||rtd.isDouble())
1309         throw new Error();
1310       else if (ltd.isFloat()||rtd.isFloat())
1311         throw new Error();
1312       else if (ltd.isLong()||rtd.isLong())
1313         lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1314       // 090205 hack for boolean
1315       else if (ltd.isBoolean()||rtd.isBoolean())
1316         lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1317       else
1318         lefttype=new TypeDescriptor(TypeDescriptor.INT);
1319       righttype=lefttype;
1320
1321       on.setLeftType(lefttype);
1322       on.setRightType(righttype);
1323       on.setType(lefttype);
1324       break;
1325
1326     case Operation.ISAVAILABLE:
1327       if (!(ltd.isPtr())) {
1328         throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1329       }
1330       lefttype=ltd;
1331       on.setLeftType(lefttype);
1332       on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1333       break;
1334
1335     case Operation.EQUAL:
1336     case Operation.NOTEQUAL:
1337       // 5.6.2 Binary Numeric Promotion
1338       //TODO unboxing of reference objects
1339       if (ltd.isBoolean()||rtd.isBoolean()) {
1340         if (!(ltd.isBoolean()&&rtd.isBoolean()))
1341           throw new Error();
1342         righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1343       } else if (ltd.isPtr()||rtd.isPtr()) {
1344         if (!(ltd.isPtr()&&rtd.isPtr()))
1345           throw new Error();
1346         righttype=rtd;
1347         lefttype=ltd;
1348       } else if (ltd.isDouble()||rtd.isDouble())
1349         righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1350       else if (ltd.isFloat()||rtd.isFloat())
1351         righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1352       else if (ltd.isLong()||rtd.isLong())
1353         righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1354       else
1355         righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1356
1357       on.setLeftType(lefttype);
1358       on.setRightType(righttype);
1359       on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1360       break;
1361
1362
1363
1364     case Operation.LT:
1365     case Operation.GT:
1366     case Operation.LTE:
1367     case Operation.GTE:
1368       // 5.6.2 Binary Numeric Promotion
1369       //TODO unboxing of reference objects
1370       if (!ltd.isNumber()||!rtd.isNumber()) {
1371         if (!ltd.isNumber())
1372           throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1373         if (!rtd.isNumber())
1374           throw new Error("Rightside is not number"+on.printNode(0));
1375       }
1376
1377       if (ltd.isDouble()||rtd.isDouble())
1378         lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1379       else if (ltd.isFloat()||rtd.isFloat())
1380         lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1381       else if (ltd.isLong()||rtd.isLong())
1382         lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1383       else
1384         lefttype=new TypeDescriptor(TypeDescriptor.INT);
1385       righttype=lefttype;
1386       on.setLeftType(lefttype);
1387       on.setRightType(righttype);
1388       on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1389       break;
1390
1391     case Operation.ADD:
1392       if (ltd.isString()||rtd.isString()) {
1393         ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
1394         TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1395         NameDescriptor nd=new NameDescriptor("String");
1396         NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1397         if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1398           MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1399           leftmin.addArgument(on.getLeft());
1400           on.left=leftmin;
1401           checkExpressionNode(md, nametable, on.getLeft(), null);
1402         }
1403
1404         if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1405           MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1406           rightmin.addArgument(on.getRight());
1407           on.right=rightmin;
1408           checkExpressionNode(md, nametable, on.getRight(), null);
1409         }
1410
1411         on.setLeftType(stringtd);
1412         on.setRightType(stringtd);
1413         on.setType(stringtd);
1414         break;
1415       }
1416
1417     case Operation.SUB:
1418     case Operation.MULT:
1419     case Operation.DIV:
1420     case Operation.MOD:
1421       // 5.6.2 Binary Numeric Promotion
1422       //TODO unboxing of reference objects
1423       if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1424         throw new Error("Error in "+on.printNode(0));
1425
1426       if (ltd.isDouble()||rtd.isDouble())
1427         lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1428       else if (ltd.isFloat()||rtd.isFloat())
1429         lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1430       else if (ltd.isLong()||rtd.isLong())
1431         lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1432       else
1433         lefttype=new TypeDescriptor(TypeDescriptor.INT);
1434       righttype=lefttype;
1435       on.setLeftType(lefttype);
1436       on.setRightType(righttype);
1437       on.setType(lefttype);
1438       break;
1439
1440     case Operation.LEFTSHIFT:
1441     case Operation.RIGHTSHIFT:
1442     case Operation.URIGHTSHIFT:
1443       if (!rtd.isIntegerType())
1444         throw new Error();
1445       //5.6.1 Unary Numeric Promotion
1446       if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1447         righttype=new TypeDescriptor(TypeDescriptor.INT);
1448       else
1449         righttype=rtd;
1450
1451       on.setRightType(righttype);
1452       if (!ltd.isIntegerType())
1453         throw new Error();
1454
1455     case Operation.UNARYPLUS:
1456     case Operation.UNARYMINUS:
1457       /*        case Operation.POSTINC:
1458           case Operation.POSTDEC:
1459           case Operation.PREINC:
1460           case Operation.PREDEC:*/
1461       if (!ltd.isNumber())
1462         throw new Error();
1463       //5.6.1 Unary Numeric Promotion
1464       if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1465         lefttype=new TypeDescriptor(TypeDescriptor.INT);
1466       else
1467         lefttype=ltd;
1468       on.setLeftType(lefttype);
1469       on.setType(lefttype);
1470       break;
1471
1472     default:
1473       throw new Error(op.toString());
1474     }
1475
1476     if (td!=null)
1477       if (!typeutil.isSuperorType(td, on.getType())) {
1478         System.out.println(td);
1479         System.out.println(on.getType());
1480         throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));
1481       }
1482   }
1483 }