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