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