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