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