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