Updates
[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.LiteralNode:
185             checkLiteralNode(md,nametable,(LiteralNode)en,td);
186             return;
187         case Kind.MethodInvokeNode:
188             checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
189             return;
190         case Kind.NameNode:
191             checkNameNode(md,nametable,(NameNode)en,td);
192             return;
193         case Kind.OpNode:
194             checkOpNode(md,nametable,(OpNode)en,td);
195             return;
196         }
197         throw new Error();
198     }
199
200     void checkCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
201         /* Get type descriptor */
202         if (cn.getType()==null) {
203             NameDescriptor typenamed=cn.getTypeName().getName();
204             String typename=typenamed.toString();
205             TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
206             cn.setType(ntd);
207         }
208
209         /* Check the type descriptor */
210         TypeDescriptor cast_type=cn.getType();
211         checkTypeDescriptor(cast_type);
212
213         /* Type check */
214         if (td!=null) {
215             if (!typeutil.isSuperorType(td,cast_type))
216                 throw new Error("Cast node returns "+cast_type+", but need "+td);
217         }
218
219         ExpressionNode en=cn.getExpression();
220         checkExpressionNode(md, nametable, en, null);
221         TypeDescriptor etd=en.getType();
222         if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
223             return;
224
225         if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
226             return;
227
228         /* Different branches */
229         /* TODO: change if add interfaces */
230         throw new Error("Cast will always fail\n"+cn.printNode(0));
231     }
232
233     void checkFieldAccessNode(MethodDescriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
234         ExpressionNode left=fan.getExpression();
235         checkExpressionNode(md,nametable,left,null);
236         TypeDescriptor ltd=left.getType();
237         String fieldname=fan.getFieldName();
238         FieldDescriptor fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
239         if (fd==null)
240             throw new Error("Unknown field "+fieldname);
241         fan.setField(fd);
242         if (td!=null)
243             if (!typeutil.isSuperorType(td,fan.getType()))
244                 throw new Error("Field node returns "+fan.getType()+", but need "+td);
245     }
246
247     void checkLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
248         /* Resolve the type */
249         Object o=ln.getValue();
250         if (ln.getTypeString().equals("null")) {
251             ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
252         } else if (o instanceof Integer) {
253             ln.setType(new TypeDescriptor(TypeDescriptor.INT));
254         } else if (o instanceof Long) {
255             ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
256         } else if (o instanceof Float) {
257             ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
258         } else if (o instanceof Boolean) {
259             ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
260         } else if (o instanceof Double) {
261             ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
262         } else if (o instanceof Character) {
263             ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
264         } else if (o instanceof String) {
265             ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
266         }
267
268         if (td!=null)
269             if (!typeutil.isSuperorType(td,ln.getType()))
270                 throw new Error("Field node returns "+ln.getType()+", but need "+td);
271     }
272
273     void checkNameNode(MethodDescriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
274         NameDescriptor nd=nn.getName();
275         if (nd.getBase()!=null) {
276             /* Big hack */
277             /* Rewrite NameNode */
278             ExpressionNode en=translateNameDescriptorintoExpression(nd);
279             nn.setExpression(en);
280             checkExpressionNode(md,nametable,en,td);
281         } else {
282             String varname=nd.toString();
283             Descriptor d=(Descriptor)nametable.get(varname);
284             if (d==null) {
285                 throw new Error("Name "+varname+" undefined");
286             }
287             if (d instanceof VarDescriptor) {
288                 nn.setVar((VarDescriptor)d);
289             } else if (d instanceof FieldDescriptor) {
290                 nn.setField((FieldDescriptor)d);
291                 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
292             }
293             if (td!=null)
294                 if (!typeutil.isSuperorType(td,nn.getType()))
295                     throw new Error("Field node returns "+nn.getType()+", but need "+td);
296         }
297     }
298
299     void checkAssignmentNode(MethodDescriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
300         checkExpressionNode(md, nametable, an.getSrc() ,td);
301         //TODO: Need check on validity of operation here
302         if (!((an.getDest() instanceof FieldAccessNode)||
303               (an.getDest() instanceof NameNode)))
304             throw new Error("Bad lside in "+an.printNode(0));
305         checkExpressionNode(md, nametable, an.getDest(), null);
306         if (!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
307             throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
308         }
309     }
310
311     void checkLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln) {
312         if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
313             checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
314             checkBlockNode(md, nametable, ln.getBody());
315         } else {
316             //For loop case
317             /* Link in the initializer naming environment */
318             BlockNode bn=ln.getInitializer();
319             bn.getVarTable().setParent(nametable);
320             for(int i=0;i<bn.size();i++) {
321                 BlockStatementNode bsn=bn.get(i);
322                 checkBlockStatementNode(md, bn.getVarTable(),bsn);
323             }
324             //check the condition
325             checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
326             checkBlockNode(md, bn.getVarTable(), ln.getBody());
327             checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
328         }
329     }
330
331
332     void checkCreateObjectNode(MethodDescriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
333         TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
334         for(int i=0;i<con.numArgs();i++) {
335             ExpressionNode en=con.getArg(i);
336             checkExpressionNode(md,nametable,en,null);
337             tdarray[i]=en.getType();
338         }
339
340         TypeDescriptor typetolookin=con.getType();
341         checkTypeDescriptor(typetolookin);
342         if (!typetolookin.isClass()) 
343             throw new Error();
344
345         ClassDescriptor classtolookin=typetolookin.getClassDesc();
346         System.out.println("Looking for "+typetolookin.getSymbol());
347         System.out.println(classtolookin.getMethodTable());
348
349         Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
350         MethodDescriptor bestmd=null;
351         NextMethod:
352         for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
353             MethodDescriptor currmd=(MethodDescriptor)methodit.next();
354             /* Need correct number of parameters */
355             System.out.println("Examining: "+currmd);
356             if (con.numArgs()!=currmd.numParameters())
357                 continue;
358             for(int i=0;i<con.numArgs();i++) {
359                 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
360                     continue NextMethod;
361             }
362             /* Method okay so far */
363             if (bestmd==null)
364                 bestmd=currmd;
365             else {
366                 if (isMoreSpecific(currmd,bestmd)) {
367                     bestmd=currmd;
368                 } else if (!isMoreSpecific(bestmd, currmd))
369                     throw new Error("No method is most specific");
370                 
371                 /* Is this more specific than bestmd */
372             }
373         }
374         if (bestmd==null)
375             throw new Error("No method found for "+con.printNode(0));
376         con.setConstructor(bestmd);
377
378         
379     }
380
381
382     /** Check to see if md1 is more specific than md2...  Informally
383         if md2 could always be called given the arguments passed into
384         md1 */
385
386     boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
387         /* Checks if md1 is more specific than md2 */
388         if (md1.numParameters()!=md2.numParameters())
389             throw new Error();
390         for(int i=0;i<md1.numParameters();i++) {
391             if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
392                 return false;
393         }
394         if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
395                 return false;
396         return true;
397     }
398
399     ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
400         String id=nd.getIdentifier();
401         NameDescriptor base=nd.getBase();
402         if (base==null) 
403             return new NameNode(nd);
404         else 
405             return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
406     }
407
408
409     void checkMethodInvokeNode(MethodDescriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
410         /*Typecheck subexpressions
411           and get types for expressions*/
412
413         TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
414         for(int i=0;i<min.numArgs();i++) {
415             ExpressionNode en=min.getArg(i);
416             checkExpressionNode(md,nametable,en,null);
417             tdarray[i]=en.getType();
418         }
419         TypeDescriptor typetolookin=null;
420         if (min.getExpression()!=null) {
421             checkExpressionNode(md,nametable,min.getExpression(),null);
422             typetolookin=min.getExpression().getType();
423         } else if (min.getBaseName()!=null) {
424             String rootname=min.getBaseName().getRoot();
425             if (nametable.get(rootname)!=null) {
426                 //we have an expression
427                 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
428                 checkExpressionNode(md, nametable, min.getExpression(), null);
429                 typetolookin=min.getExpression().getType();
430             } else {
431                 //we have a type
432                 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
433                 if (cd==null)
434                     throw new Error(min.getBaseName()+" undefined");
435                 typetolookin=new TypeDescriptor(cd);
436             }
437         } else {
438             typetolookin=new TypeDescriptor(md.getClassDesc());
439         }
440         if (!typetolookin.isClass()) 
441             throw new Error();
442         ClassDescriptor classtolookin=typetolookin.getClassDesc();
443         System.out.println("Method name="+min.getMethodName());
444         Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
445         MethodDescriptor bestmd=null;
446         NextMethod:
447         for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
448             MethodDescriptor currmd=(MethodDescriptor)methodit.next();
449             /* Need correct number of parameters */
450             if (min.numArgs()!=currmd.numParameters())
451                 continue;
452             for(int i=0;i<min.numArgs();i++) {
453                 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
454                     continue NextMethod;
455             }
456             /* Method okay so far */
457             if (bestmd==null)
458                 bestmd=currmd;
459             else {
460                 if (isMoreSpecific(currmd,bestmd)) {
461                     bestmd=currmd;
462                 } else if (!isMoreSpecific(bestmd, currmd))
463                     throw new Error("No method is most specific");
464                 
465                 /* Is this more specific than bestmd */
466             }
467         }
468         if (bestmd==null)
469             throw new Error("No method found for :"+min.printNode(0));
470         min.setMethod(bestmd);
471         /* Check whether we need to set this parameter to implied this */
472         if (!bestmd.isStatic()) {
473             if (min.getExpression()==null) {
474                 ExpressionNode en=new NameNode(new NameDescriptor("this"));
475                 min.setExpression(en);
476                 checkExpressionNode(md, nametable, min.getExpression(), null);
477             }
478         }
479
480     }
481
482
483     void checkOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
484         checkExpressionNode(md, nametable, on.getLeft(), null);
485         if (on.getRight()!=null)
486             checkExpressionNode(md, nametable, on.getRight(), null);
487         TypeDescriptor ltd=on.getLeft().getType();
488         TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
489         TypeDescriptor lefttype=null;
490         TypeDescriptor righttype=null;
491         Operation op=on.getOp();
492
493         switch(op.getOp()) {
494         case Operation.LOGIC_OR:
495         case Operation.LOGIC_AND:
496             if (!(ltd.isBoolean()&&rtd.isBoolean()))
497                 throw new Error();
498             //no promotion
499             on.setLeftType(ltd);
500             on.setRightType(rtd);
501             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
502             break;
503
504         case Operation.BIT_OR:
505         case Operation.BIT_XOR:
506         case Operation.BIT_AND:
507             // 5.6.2 Binary Numeric Promotion
508             //TODO unboxing of reference objects
509             if (ltd.isDouble()||rtd.isDouble())
510                 throw new Error();
511             else if (ltd.isFloat()||rtd.isFloat())
512                 throw new Error();
513             else if (ltd.isLong()||rtd.isLong())
514                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
515             else 
516                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
517             righttype=lefttype;
518
519             on.setLeftType(lefttype);
520             on.setRightType(righttype);
521             on.setType(lefttype);
522             break;
523
524         case Operation.EQUAL:
525         case Operation.NOTEQUAL:
526             // 5.6.2 Binary Numeric Promotion
527             //TODO unboxing of reference objects
528             if (ltd.isBoolean()||rtd.isBoolean()) {
529                 if (!(ltd.isBoolean()&&rtd.isBoolean()))
530                     throw new Error();
531                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
532             } else if (ltd.isPtr()||rtd.isPtr()) {
533                 if (!(ltd.isPtr()&&rtd.isPtr()))
534                     throw new Error();
535                 righttype=rtd;
536                 lefttype=ltd;
537             } else if (ltd.isDouble()||rtd.isDouble())
538                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
539             else if (ltd.isFloat()||rtd.isFloat())
540                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
541             else if (ltd.isLong()||rtd.isLong())
542                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
543             else 
544                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
545
546             on.setLeftType(lefttype);
547             on.setRightType(righttype);
548             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
549             break;
550
551
552
553         case Operation.LT:
554         case Operation.GT:
555         case Operation.LTE:
556         case Operation.GTE:
557             // 5.6.2 Binary Numeric Promotion
558             //TODO unboxing of reference objects
559             if (!ltd.isNumber()||!rtd.isNumber())
560                 throw new Error();
561
562             if (ltd.isDouble()||rtd.isDouble())
563                 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
564             else if (ltd.isFloat()||rtd.isFloat())
565                 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
566             else if (ltd.isLong()||rtd.isLong())
567                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
568             else 
569                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
570             righttype=lefttype;
571             on.setLeftType(lefttype);
572             on.setRightType(righttype);
573             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
574             break;
575
576         case Operation.ADD:
577             //TODO: Need special case for strings eventually
578             
579             
580         case Operation.SUB:
581         case Operation.MULT:
582         case Operation.DIV:
583         case Operation.MOD:
584             // 5.6.2 Binary Numeric Promotion
585             //TODO unboxing of reference objects
586             if (!ltd.isNumber()||!rtd.isNumber())
587                 throw new Error("Error in "+on.printNode(0));
588
589             if (ltd.isDouble()||rtd.isDouble())
590                 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
591             else if (ltd.isFloat()||rtd.isFloat())
592                 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
593             else if (ltd.isLong()||rtd.isLong())
594                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
595             else 
596                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
597             righttype=lefttype;
598             on.setLeftType(lefttype);
599             on.setRightType(righttype);
600             on.setType(lefttype);
601             break;
602
603         case Operation.LEFTSHIFT:
604         case Operation.RIGHTSHIFT:
605             if (!rtd.isIntegerType())
606                 throw new Error();
607             //5.6.1 Unary Numeric Promotion
608             if (rtd.isByte()||rtd.isShort()||rtd.isInt())
609                 righttype=new TypeDescriptor(TypeDescriptor.INT);
610             else
611                 righttype=rtd;
612
613             on.setRightType(righttype);
614             if (!ltd.isIntegerType())
615                 throw new Error();
616         case Operation.UNARYPLUS:
617         case Operation.UNARYMINUS:
618         case Operation.POSTINC:
619         case Operation.POSTDEC:
620         case Operation.PREINC:
621         case Operation.PREDEC:
622             if (!ltd.isNumber())
623                 throw new Error();
624             //5.6.1 Unary Numeric Promotion
625             if (ltd.isByte()||ltd.isShort()||ltd.isInt())
626                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
627             else
628                 lefttype=ltd;
629             on.setLeftType(lefttype);
630             on.setType(lefttype);
631             break;
632         default:
633             throw new Error();
634         }
635
636      
637
638         if (td!=null)
639             if (!typeutil.isSuperorType(td, on.getType())) {
640                 System.out.println(td);
641                 System.out.println(on.getType());
642                 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));     
643             }
644     }
645 }