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