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