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