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