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