Array support
[IRC.git] / Robust / src / IR / Tree / SemanticCheck.java
1 package IR.Tree;
2
3 import java.util.*;
4 import IR.*;
5
6 public class SemanticCheck {
7     State state;
8     TypeUtil typeutil;
9
10     public SemanticCheck(State state, TypeUtil tu) {
11         this.state=state;
12         this.typeutil=tu;
13     }
14
15     public void semanticCheck() {
16         SymbolTable classtable=state.getClassSymbolTable();
17         Iterator it=classtable.getDescriptorsIterator();
18         // Do descriptors first
19         while(it.hasNext()) {
20             ClassDescriptor cd=(ClassDescriptor)it.next();
21             System.out.println("Checking class: "+cd);
22             //Set superclass link up
23             if (cd.getSuper()!=null) {
24                 cd.setSuper(typeutil.getClass(cd.getSuper()));
25                 // Link together Field and Method tables
26                 cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
27                 cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
28             }
29             
30             for(Iterator field_it=cd.getFields();field_it.hasNext();) {
31                 FieldDescriptor fd=(FieldDescriptor)field_it.next();
32                 System.out.println("Checking field: "+fd);
33                 checkField(cd,fd);
34             }
35             for(Iterator method_it=cd.getMethods();method_it.hasNext();) {
36                 MethodDescriptor md=(MethodDescriptor)method_it.next();
37                 checkMethod(cd,md);
38             }
39         }
40
41         it=classtable.getDescriptorsIterator();
42         // Do descriptors first
43         while(it.hasNext()) {
44             ClassDescriptor cd=(ClassDescriptor)it.next();
45             for(Iterator method_it=cd.getMethods();method_it.hasNext();) {
46                 MethodDescriptor md=(MethodDescriptor)method_it.next();
47                 checkMethodBody(cd,md);
48             }
49         }
50     }
51
52     public void checkTypeDescriptor(TypeDescriptor td) {
53         if (td.isPrimitive())
54             return; /* Done */
55         if (td.isClass()) {
56             String name=td.toString();
57             ClassDescriptor field_cd=(ClassDescriptor)state.getClassSymbolTable().get(name);
58             if (field_cd==null)
59                 throw new Error("Undefined class "+name);
60             td.setClassDescriptor(field_cd);
61             return;
62         }
63         throw new Error();
64     }
65
66     public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
67         checkTypeDescriptor(fd.getType());
68     }
69
70     public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
71         /* Check return type */
72         if (!md.isConstructor())
73             if (!md.getReturnType().isVoid())
74                 checkTypeDescriptor(md.getReturnType());
75
76         for(int i=0;i<md.numParameters();i++) {
77             TypeDescriptor param_type=md.getParamType(i);
78             checkTypeDescriptor(param_type);
79         }
80         /* Link the naming environments */
81         if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
82             md.getParameterTable().setParent(cd.getFieldTable());
83         md.setClassDesc(cd);
84         if (!md.isStatic()) {
85             VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
86             md.setThis(thisvd);
87         }
88     }
89
90     public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
91         System.out.println("Processing method:"+md);
92         BlockNode bn=state.getMethodBody(md);
93         checkBlockNode(md, md.getParameterTable(),bn);
94     }
95     
96     public void checkBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn) {
97         /* Link in the naming environment */
98         bn.getVarTable().setParent(nametable);
99         for(int i=0;i<bn.size();i++) {
100             BlockStatementNode bsn=bn.get(i);
101             checkBlockStatementNode(md, bn.getVarTable(),bsn);
102         }
103     }
104     
105     public void checkBlockStatementNode(MethodDescriptor md, SymbolTable nametable, BlockStatementNode bsn) {
106         switch(bsn.kind()) {
107         case Kind.BlockExpressionNode:
108             checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
109             return;
110
111         case Kind.DeclarationNode:
112             checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
113             return;
114             
115         case Kind.IfStatementNode:
116             checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
117             return;
118             
119         case Kind.LoopNode:
120             checkLoopNode(md, nametable, (LoopNode)bsn);
121             return;
122             
123         case Kind.ReturnNode:
124             checkReturnNode(md, nametable, (ReturnNode)bsn);
125             return;
126
127         case Kind.SubBlockNode:
128             checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
129             return;
130         }
131         throw new Error();
132     }
133
134     void checkBlockExpressionNode(MethodDescriptor md, SymbolTable nametable, BlockExpressionNode ben) {
135         checkExpressionNode(md, nametable, ben.getExpression(), null);
136     }
137
138     void checkDeclarationNode(MethodDescriptor md, SymbolTable nametable,  DeclarationNode dn) {
139         VarDescriptor vd=dn.getVarDescriptor();
140         checkTypeDescriptor(vd.getType());
141         Descriptor d=nametable.get(vd.getSymbol());
142         if ((d==null)||
143             (d instanceof FieldDescriptor)) {
144             nametable.add(vd);
145         } else
146             throw new Error(vd.getSymbol()+" defined a second time");
147         if (dn.getExpression()!=null)
148             checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
149     }
150     
151     void checkSubBlockNode(MethodDescriptor md, SymbolTable nametable, SubBlockNode sbn) {
152         checkBlockNode(md, nametable, sbn.getBlockNode());
153     }
154
155     void checkReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn) {
156         if (rn.getReturnExpression()!=null)
157             checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
158         else
159             if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
160                 throw new Error("Need to return something for "+md);
161     }
162
163     void checkIfStatementNode(MethodDescriptor md, SymbolTable nametable, IfStatementNode isn) {
164         checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
165         checkBlockNode(md, nametable, isn.getTrueBlock());
166         if (isn.getFalseBlock()!=null)
167             checkBlockNode(md, nametable, isn.getFalseBlock());
168     }
169     
170     void checkExpressionNode(MethodDescriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
171         switch(en.kind()) {
172         case Kind.AssignmentNode:
173             checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
174             return;
175         case Kind.CastNode:
176             checkCastNode(md,nametable,(CastNode)en,td);
177             return;
178         case Kind.CreateObjectNode:
179             checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
180             return;
181         case Kind.FieldAccessNode:
182             checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
183             return;
184         case Kind.ArrayAccessNode:
185             checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
186             return;
187         case Kind.LiteralNode:
188             checkLiteralNode(md,nametable,(LiteralNode)en,td);
189             return;
190         case Kind.MethodInvokeNode:
191             checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
192             return;
193         case Kind.NameNode:
194             checkNameNode(md,nametable,(NameNode)en,td);
195             return;
196         case Kind.OpNode:
197             checkOpNode(md,nametable,(OpNode)en,td);
198             return;
199         }
200         throw new Error();
201     }
202
203     void checkCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
204         /* Get type descriptor */
205         if (cn.getType()==null) {
206             NameDescriptor typenamed=cn.getTypeName().getName();
207             String typename=typenamed.toString();
208             TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
209             cn.setType(ntd);
210         }
211
212         /* Check the type descriptor */
213         TypeDescriptor cast_type=cn.getType();
214         checkTypeDescriptor(cast_type);
215
216         /* Type check */
217         if (td!=null) {
218             if (!typeutil.isSuperorType(td,cast_type))
219                 throw new Error("Cast node returns "+cast_type+", but need "+td);
220         }
221
222         ExpressionNode en=cn.getExpression();
223         checkExpressionNode(md, nametable, en, null);
224         TypeDescriptor etd=en.getType();
225         if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
226             return;
227
228         if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
229             return;
230
231         /* Different branches */
232         /* TODO: change if add interfaces */
233         throw new Error("Cast will always fail\n"+cn.printNode(0));
234     }
235
236     void checkFieldAccessNode(MethodDescriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
237         ExpressionNode left=fan.getExpression();
238         checkExpressionNode(md,nametable,left,null);
239         TypeDescriptor ltd=left.getType();
240         String fieldname=fan.getFieldName();
241         FieldDescriptor fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
242         if (fd==null)
243             throw new Error("Unknown field "+fieldname);
244         fan.setField(fd);
245         if (td!=null)
246             if (!typeutil.isSuperorType(td,fan.getType()))
247                 throw new Error("Field node returns "+fan.getType()+", but need "+td);
248     }
249
250     void checkArrayAccessNode(MethodDescriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
251         ExpressionNode left=aan.getExpression();
252         checkExpressionNode(md,nametable,left,null);
253
254         checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
255         TypeDescriptor ltd=left.getType();
256
257         if (td!=null)
258             if (!typeutil.isSuperorType(td,aan.getType()))
259                 throw new Error("Field node returns "+aan.getType()+", but need "+td);
260     }
261
262     void checkLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
263         /* Resolve the type */
264         Object o=ln.getValue();
265         if (ln.getTypeString().equals("null")) {
266             ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
267         } else if (o instanceof Integer) {
268             ln.setType(new TypeDescriptor(TypeDescriptor.INT));
269         } else if (o instanceof Long) {
270             ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
271         } else if (o instanceof Float) {
272             ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
273         } else if (o instanceof Boolean) {
274             ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
275         } else if (o instanceof Double) {
276             ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
277         } else if (o instanceof Character) {
278             ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
279         } else if (o instanceof String) {
280             ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
281         }
282
283         if (td!=null)
284             if (!typeutil.isSuperorType(td,ln.getType()))
285                 throw new Error("Field node returns "+ln.getType()+", but need "+td);
286     }
287
288     void checkNameNode(MethodDescriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
289         NameDescriptor nd=nn.getName();
290         if (nd.getBase()!=null) {
291             /* Big hack */
292             /* Rewrite NameNode */
293             ExpressionNode en=translateNameDescriptorintoExpression(nd);
294             nn.setExpression(en);
295             checkExpressionNode(md,nametable,en,td);
296         } else {
297             String varname=nd.toString();
298             Descriptor d=(Descriptor)nametable.get(varname);
299             if (d==null) {
300                 throw new Error("Name "+varname+" undefined");
301             }
302             if (d instanceof VarDescriptor) {
303                 nn.setVar((VarDescriptor)d);
304             } else if (d instanceof FieldDescriptor) {
305                 nn.setField((FieldDescriptor)d);
306                 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
307             }
308             if (td!=null)
309                 if (!typeutil.isSuperorType(td,nn.getType()))
310                     throw new Error("Field node returns "+nn.getType()+", but need "+td);
311         }
312     }
313
314     void checkAssignmentNode(MethodDescriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
315         checkExpressionNode(md, nametable, an.getSrc() ,td);
316         //TODO: Need check on validity of operation here
317         if (!((an.getDest() instanceof FieldAccessNode)||
318               (an.getDest() instanceof ArrayAccessNode)||
319               (an.getDest() instanceof NameNode)))
320             throw new Error("Bad lside in "+an.printNode(0));
321         checkExpressionNode(md, nametable, an.getDest(), null);
322         if (!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
323             throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
324         }
325     }
326
327     void checkLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
328         if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
329             checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
330             checkBlockNode(md, nametable, ln.getBody());
331         } else {
332             //For loop case
333             /* Link in the initializer naming environment */
334             BlockNode bn=ln.getInitializer();
335             bn.getVarTable().setParent(nametable);
336             for(int i=0;i<bn.size();i++) {
337                 BlockStatementNode bsn=bn.get(i);
338                 checkBlockStatementNode(md, bn.getVarTable(),bsn);
339             }
340             //check the condition
341             checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
342             checkBlockNode(md, bn.getVarTable(), ln.getBody());
343             checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
344         }
345     }
346
347
348     void checkCreateObjectNode(MethodDescriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
349         TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
350         for(int i=0;i<con.numArgs();i++) {
351             ExpressionNode en=con.getArg(i);
352             checkExpressionNode(md,nametable,en,null);
353             tdarray[i]=en.getType();
354         }
355
356         TypeDescriptor typetolookin=con.getType();
357         checkTypeDescriptor(typetolookin);
358         if ((!typetolookin.isClass())&&(!typetolookin.isArray())) 
359             throw new Error("Can't allocate primitive type:"+con.printNode(0));
360
361         if (!typetolookin.isArray()) {
362             //Array's don't need constructor calls
363             ClassDescriptor classtolookin=typetolookin.getClassDesc();
364             System.out.println("Looking for "+typetolookin.getSymbol());
365             System.out.println(classtolookin.getMethodTable());
366             
367             Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
368             MethodDescriptor bestmd=null;
369         NextMethod:
370             for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
371                 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
372                 /* Need correct number of parameters */
373                 System.out.println("Examining: "+currmd);
374                 if (con.numArgs()!=currmd.numParameters())
375                     continue;
376                 for(int i=0;i<con.numArgs();i++) {
377                     if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
378                         continue NextMethod;
379                 }
380                 /* Method okay so far */
381                 if (bestmd==null)
382                     bestmd=currmd;
383                 else {
384                     if (isMoreSpecific(currmd,bestmd)) {
385                         bestmd=currmd;
386                     } else if (!isMoreSpecific(bestmd, currmd))
387                         throw new Error("No method is most specific");
388                     
389                     /* Is this more specific than bestmd */
390                 }
391             }
392             if (bestmd==null)
393                 throw new Error("No method found for "+con.printNode(0));
394             con.setConstructor(bestmd);
395         }
396     }
397
398
399     /** Check to see if md1 is more specific than md2...  Informally
400         if md2 could always be called given the arguments passed into
401         md1 */
402
403     boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
404         /* Checks if md1 is more specific than md2 */
405         if (md1.numParameters()!=md2.numParameters())
406             throw new Error();
407         for(int i=0;i<md1.numParameters();i++) {
408             if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
409                 return false;
410         }
411         if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
412                 return false;
413         return true;
414     }
415
416     ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
417         String id=nd.getIdentifier();
418         NameDescriptor base=nd.getBase();
419         if (base==null) 
420             return new NameNode(nd);
421         else 
422             return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
423     }
424
425
426     void checkMethodInvokeNode(MethodDescriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
427         /*Typecheck subexpressions
428           and get types for expressions*/
429
430         TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
431         for(int i=0;i<min.numArgs();i++) {
432             ExpressionNode en=min.getArg(i);
433             checkExpressionNode(md,nametable,en,null);
434             tdarray[i]=en.getType();
435         }
436         TypeDescriptor typetolookin=null;
437         if (min.getExpression()!=null) {
438             checkExpressionNode(md,nametable,min.getExpression(),null);
439             typetolookin=min.getExpression().getType();
440         } else if (min.getBaseName()!=null) {
441             String rootname=min.getBaseName().getRoot();
442             if (nametable.get(rootname)!=null) {
443                 //we have an expression
444                 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
445                 checkExpressionNode(md, nametable, min.getExpression(), null);
446                 typetolookin=min.getExpression().getType();
447             } else {
448                 //we have a type
449                 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
450                 if (cd==null)
451                     throw new Error(min.getBaseName()+" undefined");
452                 typetolookin=new TypeDescriptor(cd);
453             }
454         } else {
455             typetolookin=new TypeDescriptor(md.getClassDesc());
456         }
457         if (!typetolookin.isClass()) 
458             throw new Error();
459         ClassDescriptor classtolookin=typetolookin.getClassDesc();
460         System.out.println("Method name="+min.getMethodName());
461         Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
462         MethodDescriptor bestmd=null;
463         NextMethod:
464         for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
465             MethodDescriptor currmd=(MethodDescriptor)methodit.next();
466             /* Need correct number of parameters */
467             if (min.numArgs()!=currmd.numParameters())
468                 continue;
469             for(int i=0;i<min.numArgs();i++) {
470                 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
471                     continue NextMethod;
472             }
473             /* Method okay so far */
474             if (bestmd==null)
475                 bestmd=currmd;
476             else {
477                 if (isMoreSpecific(currmd,bestmd)) {
478                     bestmd=currmd;
479                 } else if (!isMoreSpecific(bestmd, currmd))
480                     throw new Error("No method is most specific");
481                 
482                 /* Is this more specific than bestmd */
483             }
484         }
485         if (bestmd==null)
486             throw new Error("No method found for :"+min.printNode(0));
487         min.setMethod(bestmd);
488         /* Check whether we need to set this parameter to implied this */
489         if (!bestmd.isStatic()) {
490             if (min.getExpression()==null) {
491                 ExpressionNode en=new NameNode(new NameDescriptor("this"));
492                 min.setExpression(en);
493                 checkExpressionNode(md, nametable, min.getExpression(), null);
494             }
495         }
496
497     }
498
499
500     void checkOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
501         checkExpressionNode(md, nametable, on.getLeft(), null);
502         if (on.getRight()!=null)
503             checkExpressionNode(md, nametable, on.getRight(), null);
504         TypeDescriptor ltd=on.getLeft().getType();
505         TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
506         TypeDescriptor lefttype=null;
507         TypeDescriptor righttype=null;
508         Operation op=on.getOp();
509
510         switch(op.getOp()) {
511         case Operation.LOGIC_OR:
512         case Operation.LOGIC_AND:
513             if (!(ltd.isBoolean()&&rtd.isBoolean()))
514                 throw new Error();
515             //no promotion
516             on.setLeftType(ltd);
517             on.setRightType(rtd);
518             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
519             break;
520
521         case Operation.BIT_OR:
522         case Operation.BIT_XOR:
523         case Operation.BIT_AND:
524             // 5.6.2 Binary Numeric Promotion
525             //TODO unboxing of reference objects
526             if (ltd.isDouble()||rtd.isDouble())
527                 throw new Error();
528             else if (ltd.isFloat()||rtd.isFloat())
529                 throw new Error();
530             else if (ltd.isLong()||rtd.isLong())
531                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
532             else 
533                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
534             righttype=lefttype;
535
536             on.setLeftType(lefttype);
537             on.setRightType(righttype);
538             on.setType(lefttype);
539             break;
540
541         case Operation.EQUAL:
542         case Operation.NOTEQUAL:
543             // 5.6.2 Binary Numeric Promotion
544             //TODO unboxing of reference objects
545             if (ltd.isBoolean()||rtd.isBoolean()) {
546                 if (!(ltd.isBoolean()&&rtd.isBoolean()))
547                     throw new Error();
548                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
549             } else if (ltd.isPtr()||rtd.isPtr()) {
550                 if (!(ltd.isPtr()&&rtd.isPtr()))
551                     throw new Error();
552                 righttype=rtd;
553                 lefttype=ltd;
554             } else if (ltd.isDouble()||rtd.isDouble())
555                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
556             else if (ltd.isFloat()||rtd.isFloat())
557                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
558             else if (ltd.isLong()||rtd.isLong())
559                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
560             else 
561                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
562
563             on.setLeftType(lefttype);
564             on.setRightType(righttype);
565             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
566             break;
567
568
569
570         case Operation.LT:
571         case Operation.GT:
572         case Operation.LTE:
573         case Operation.GTE:
574             // 5.6.2 Binary Numeric Promotion
575             //TODO unboxing of reference objects
576             if (!ltd.isNumber()||!rtd.isNumber())
577                 throw new Error();
578
579             if (ltd.isDouble()||rtd.isDouble())
580                 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
581             else if (ltd.isFloat()||rtd.isFloat())
582                 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
583             else if (ltd.isLong()||rtd.isLong())
584                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
585             else 
586                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
587             righttype=lefttype;
588             on.setLeftType(lefttype);
589             on.setRightType(righttype);
590             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
591             break;
592
593         case Operation.ADD:
594             //TODO: Need special case for strings eventually
595             
596             
597         case Operation.SUB:
598         case Operation.MULT:
599         case Operation.DIV:
600         case Operation.MOD:
601             // 5.6.2 Binary Numeric Promotion
602             //TODO unboxing of reference objects
603             if (!ltd.isNumber()||!rtd.isNumber())
604                 throw new Error("Error in "+on.printNode(0));
605
606             if (ltd.isDouble()||rtd.isDouble())
607                 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
608             else if (ltd.isFloat()||rtd.isFloat())
609                 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
610             else if (ltd.isLong()||rtd.isLong())
611                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
612             else 
613                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
614             righttype=lefttype;
615             on.setLeftType(lefttype);
616             on.setRightType(righttype);
617             on.setType(lefttype);
618             break;
619
620         case Operation.LEFTSHIFT:
621         case Operation.RIGHTSHIFT:
622             if (!rtd.isIntegerType())
623                 throw new Error();
624             //5.6.1 Unary Numeric Promotion
625             if (rtd.isByte()||rtd.isShort()||rtd.isInt())
626                 righttype=new TypeDescriptor(TypeDescriptor.INT);
627             else
628                 righttype=rtd;
629
630             on.setRightType(righttype);
631             if (!ltd.isIntegerType())
632                 throw new Error();
633         case Operation.UNARYPLUS:
634         case Operation.UNARYMINUS:
635         case Operation.POSTINC:
636         case Operation.POSTDEC:
637         case Operation.PREINC:
638         case Operation.PREDEC:
639             if (!ltd.isNumber())
640                 throw new Error();
641             //5.6.1 Unary Numeric Promotion
642             if (ltd.isByte()||ltd.isShort()||ltd.isInt())
643                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
644             else
645                 lefttype=ltd;
646             on.setLeftType(lefttype);
647             on.setType(lefttype);
648             break;
649         default:
650             throw new Error();
651         }
652
653      
654
655         if (td!=null)
656             if (!typeutil.isSuperorType(td, on.getType())) {
657                 System.out.println(td);
658                 System.out.println(on.getType());
659                 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));     
660             }
661     }
662 }