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