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