Add support for volatile keyword in mgc version. For Tilera, as we execute a process...
[IRC.git] / Robust / src / IR / Tree / SemanticCheck.java
1 package IR.Tree;
2
3 import java.util.*;
4
5 import IR.*;
6
7 public class SemanticCheck {
8   State state;
9   TypeUtil typeutil;
10   Stack loopstack;
11   HashSet toanalyze;
12   HashSet completed;
13
14
15   public SemanticCheck(State state, TypeUtil tu) {
16     this.state=state;
17     this.typeutil=tu;
18     this.loopstack=new Stack();
19     this.toanalyze=new HashSet();
20     this.completed=new HashSet();
21   }
22
23   public ClassDescriptor getClass(String classname) {
24     ClassDescriptor cd=typeutil.getClass(classname, toanalyze);
25     checkClass(cd);
26     return cd;
27   }
28
29   private void checkClass(ClassDescriptor cd) {
30     if (!completed.contains(cd)) {
31       completed.add(cd);
32       
33       //Set superclass link up
34       if (cd.getSuper()!=null) {
35         cd.setSuper(getClass(cd.getSuper()));
36         // Link together Field, Method, and Flag tables so classes
37         // inherit these from their superclasses
38         cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
39         cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
40         cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable());
41     if(state.MGC) {
42       // TODO add version for normal Java later
43       // Link together Field, Method tables do classes inherit these from 
44       // their ancestor interfaces
45       Vector<String> sifv = cd.getSuperInterface();
46       for(int i = 0; i < sifv.size(); i++) {
47         ClassDescriptor superif = getClass(sifv.elementAt(i));
48         cd.addSuperInterfaces(superif);
49         cd.getFieldTable().addParentIF(superif.getFieldTable());
50         cd.getMethodTable().addParentIF(superif.getMethodTable());
51       }
52     }
53       }
54       
55       /* Check to see that fields are well typed */
56       for(Iterator field_it=cd.getFields(); field_it.hasNext();) {
57         FieldDescriptor fd=(FieldDescriptor)field_it.next();
58         checkField(cd,fd);
59       }
60       
61       for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
62         MethodDescriptor md=(MethodDescriptor)method_it.next();
63         checkMethod(cd,md);
64       }
65     }
66   }
67
68   public void semanticCheck() {
69     SymbolTable classtable=state.getClassSymbolTable();
70     toanalyze.addAll(classtable.getValueSet());
71     toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
72
73     // Do methods next
74     while(!toanalyze.isEmpty()) {
75       Object obj=toanalyze.iterator().next();
76       if (obj instanceof TaskDescriptor) {
77         toanalyze.remove(obj);
78         TaskDescriptor td=(TaskDescriptor)obj;
79         try {
80           checkTask(td);
81         } catch( Error e ) {
82             System.out.println( "Error in "+td );
83             throw e;
84         }
85       } else {
86         ClassDescriptor cd=(ClassDescriptor)obj;
87         toanalyze.remove(cd);
88         //need to initialize typeutil object here...only place we can
89         //get class descriptors without first calling getclass
90         getClass(cd.getSymbol());
91         for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
92           MethodDescriptor md=(MethodDescriptor)method_it.next();
93           try {
94             checkMethodBody(cd,md);
95           } catch( Error e ) {
96             System.out.println( "Error in "+md );
97             throw e;
98           }
99         }
100       }
101     }
102   }
103
104   public void checkTypeDescriptor(TypeDescriptor td) {
105     if (td.isPrimitive())
106       return;       /* Done */
107     else if (td.isClass()) {
108       String name=td.toString();
109       ClassDescriptor field_cd=getClass(name);
110       if (field_cd==null)
111         throw new Error("Undefined class "+name);
112       td.setClassDescriptor(field_cd);
113       return;
114     } else if (td.isTag())
115       return;
116     else
117       throw new Error();
118   }
119
120   public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
121     checkTypeDescriptor(fd.getType());
122   }
123
124   public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
125     if (ccs==null)
126       return;       /* No constraint checks to check */
127     for(int i=0; i<ccs.size(); i++) {
128       ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
129
130       for(int j=0; j<cc.numArgs(); j++) {
131         ExpressionNode en=cc.getArg(j);
132         checkExpressionNode(td,nametable,en,null);
133       }
134     }
135   }
136
137   public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
138     if (vfe==null)
139       return;       /* No flag effects to check */
140     for(int i=0; i<vfe.size(); i++) {
141       FlagEffects fe=(FlagEffects) vfe.get(i);
142       String varname=fe.getName();
143       //Make sure the variable is declared as a parameter to the task
144       VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
145       if (vd==null)
146         throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
147       fe.setVar(vd);
148
149       //Make sure it correspods to a class
150       TypeDescriptor type_d=vd.getType();
151       if (!type_d.isClass())
152         throw new Error("Cannot have non-object argument for flag_effect");
153
154       ClassDescriptor cd=type_d.getClassDesc();
155       for(int j=0; j<fe.numEffects(); j++) {
156         FlagEffect flag=fe.getEffect(j);
157         String name=flag.getName();
158         FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
159         //Make sure the flag is declared
160         if (flag_d==null)
161           throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
162         if (flag_d.getExternal())
163           throw new Error("Attempting to modify external flag: "+name);
164         flag.setFlag(flag_d);
165       }
166       for(int j=0; j<fe.numTagEffects(); j++) {
167         TagEffect tag=fe.getTagEffect(j);
168         String name=tag.getName();
169
170         Descriptor d=(Descriptor)nametable.get(name);
171         if (d==null)
172           throw new Error("Tag descriptor "+name+" undeclared");
173         else if (!(d instanceof TagVarDescriptor))
174           throw new Error(name+" is not a tag descriptor");
175         tag.setTag((TagVarDescriptor)d);
176       }
177     }
178   }
179
180   public void checkTask(TaskDescriptor td) {
181     for(int i=0; i<td.numParameters(); i++) {
182       /* Check that parameter is well typed */
183       TypeDescriptor param_type=td.getParamType(i);
184       checkTypeDescriptor(param_type);
185
186       /* Check the parameter's flag expression is well formed */
187       FlagExpressionNode fen=td.getFlag(td.getParameter(i));
188       if (!param_type.isClass())
189         throw new Error("Cannot have non-object argument to a task");
190       ClassDescriptor cd=param_type.getClassDesc();
191       if (fen!=null)
192         checkFlagExpressionNode(cd, fen);
193     }
194
195     checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
196     /* Check that the task code is valid */
197     BlockNode bn=state.getMethodBody(td);
198     checkBlockNode(td, td.getParameterTable(),bn);
199   }
200
201   public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
202     switch(fen.kind()) {
203     case Kind.FlagOpNode:
204     {
205       FlagOpNode fon=(FlagOpNode)fen;
206       checkFlagExpressionNode(cd, fon.getLeft());
207       if (fon.getRight()!=null)
208         checkFlagExpressionNode(cd, fon.getRight());
209       break;
210     }
211
212     case Kind.FlagNode:
213     {
214       FlagNode fn=(FlagNode)fen;
215       String name=fn.getFlagName();
216       FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
217       if (fd==null)
218         throw new Error("Undeclared flag: "+name);
219       fn.setFlag(fd);
220       break;
221     }
222
223     default:
224       throw new Error("Unrecognized FlagExpressionNode");
225     }
226   }
227
228   public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
229     if(state.MGC) {
230       // TODO add version for normal Java later
231       /* Check for abstract methods */
232       if(md.isAbstract()) {
233         if(!cd.isAbstract() && !cd.isInterface()) {
234           throw new Error("Error! The non-abstract Class " + cd.getSymbol() + " contains an abstract method " + md.getSymbol());
235         }
236       }
237     }
238     /* Check return type */
239     if (!md.isConstructor())
240       if (!md.getReturnType().isVoid())
241         checkTypeDescriptor(md.getReturnType());
242
243     for(int i=0; i<md.numParameters(); i++) {
244       TypeDescriptor param_type=md.getParamType(i);
245       checkTypeDescriptor(param_type);
246     }
247     /* Link the naming environments */
248     if (!md.isStatic())     /* Fields aren't accessible directly in a static method, so don't link in this table */
249       md.getParameterTable().setParent(cd.getFieldTable());
250     md.setClassDesc(cd);
251     if (!md.isStatic()) {
252       VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
253       md.setThis(thisvd);
254     }
255   }
256
257   public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
258     ClassDescriptor superdesc=cd.getSuperDesc();
259     if (superdesc!=null) {
260       Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
261       for(Iterator methodit=possiblematches.iterator(); methodit.hasNext();) {
262         MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
263         if (md.matches(matchmd)) {
264           if (matchmd.getModifiers().isFinal()) {
265             throw new Error("Try to override final method in method:"+md+" declared in  "+cd);
266           }
267         }
268       }
269     }
270     BlockNode bn=state.getMethodBody(md);
271     checkBlockNode(md, md.getParameterTable(),bn);
272   }
273
274   public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
275     /* Link in the naming environment */
276     bn.getVarTable().setParent(nametable);
277     for(int i=0; i<bn.size(); i++) {
278       BlockStatementNode bsn=bn.get(i);
279       checkBlockStatementNode(md, bn.getVarTable(),bsn);
280     }
281   }
282
283   public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
284     switch(bsn.kind()) {
285     case Kind.BlockExpressionNode:
286       checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
287       return;
288
289     case Kind.DeclarationNode:
290       checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
291       return;
292
293     case Kind.TagDeclarationNode:
294       checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
295       return;
296
297     case Kind.IfStatementNode:
298       checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
299       return;
300
301     case Kind.LoopNode:
302       checkLoopNode(md, nametable, (LoopNode)bsn);
303       return;
304
305     case Kind.ReturnNode:
306       checkReturnNode(md, nametable, (ReturnNode)bsn);
307       return;
308
309     case Kind.TaskExitNode:
310       checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
311       return;
312
313     case Kind.SubBlockNode:
314       checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
315       return;
316
317     case Kind.AtomicNode:
318       checkAtomicNode(md, nametable, (AtomicNode)bsn);
319       return;
320
321     case Kind.SynchronizedNode:
322       checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
323       return;
324
325     case Kind.ContinueBreakNode:
326         checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
327         return;
328
329     case Kind.SESENode:
330     case Kind.GenReachNode:
331       // do nothing, no semantic check for SESEs
332       return;
333     }
334
335     throw new Error();
336   }
337
338   void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
339     checkExpressionNode(md, nametable, ben.getExpression(), null);
340   }
341
342   void checkDeclarationNode(Descriptor md, SymbolTable nametable,  DeclarationNode dn) {
343     VarDescriptor vd=dn.getVarDescriptor();
344     checkTypeDescriptor(vd.getType());
345     Descriptor d=nametable.get(vd.getSymbol());
346     if ((d==null)||
347         (d instanceof FieldDescriptor)) {
348       nametable.add(vd);
349     } else
350       throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
351     if (dn.getExpression()!=null)
352       checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
353   }
354
355   void checkTagDeclarationNode(Descriptor md, SymbolTable nametable,  TagDeclarationNode dn) {
356     TagVarDescriptor vd=dn.getTagVarDescriptor();
357     Descriptor d=nametable.get(vd.getSymbol());
358     if ((d==null)||
359         (d instanceof FieldDescriptor)) {
360       nametable.add(vd);
361     } else
362       throw new Error(vd.getSymbol()+" defined a second time");
363   }
364
365   void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
366     checkBlockNode(md, nametable, sbn.getBlockNode());
367   }
368
369   void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
370     checkBlockNode(md, nametable, sbn.getBlockNode());
371   }
372
373   void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
374     checkBlockNode(md, nametable, sbn.getBlockNode());
375     //todo this could be Object
376     checkExpressionNode(md, nametable, sbn.getExpr(), null);
377   }
378
379   void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
380       if (loopstack.empty())
381           throw new Error("continue/break outside of loop");
382       LoopNode ln=(LoopNode)loopstack.peek();
383       cbn.setLoop(ln);
384   }
385
386   void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
387     if (d instanceof TaskDescriptor)
388       throw new Error("Illegal return appears in Task: "+d.getSymbol());
389     MethodDescriptor md=(MethodDescriptor)d;
390     if (rn.getReturnExpression()!=null)
391       if (md.getReturnType()==null)
392         throw new Error("Constructor can't return something.");
393       else if (md.getReturnType().isVoid())
394         throw new Error(md+" is void");
395       else
396         checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
397     else
398     if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
399       throw new Error("Need to return something for "+md);
400   }
401
402   void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
403     if (md instanceof MethodDescriptor)
404       throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
405     checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
406     checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
407   }
408
409   void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
410     checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
411     checkBlockNode(md, nametable, isn.getTrueBlock());
412     if (isn.getFalseBlock()!=null)
413       checkBlockNode(md, nametable, isn.getFalseBlock());
414   }
415
416   void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
417     switch(en.kind()) {
418     case Kind.AssignmentNode:
419       checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
420       return;
421
422     case Kind.CastNode:
423       checkCastNode(md,nametable,(CastNode)en,td);
424       return;
425
426     case Kind.CreateObjectNode:
427       checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
428       return;
429
430     case Kind.FieldAccessNode:
431       checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
432       return;
433
434     case Kind.ArrayAccessNode:
435       checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
436       return;
437
438     case Kind.LiteralNode:
439       checkLiteralNode(md,nametable,(LiteralNode)en,td);
440       return;
441
442     case Kind.MethodInvokeNode:
443       checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
444       return;
445
446     case Kind.NameNode:
447       checkNameNode(md,nametable,(NameNode)en,td);
448       return;
449
450     case Kind.OpNode:
451       checkOpNode(md,nametable,(OpNode)en,td);
452       return;
453
454     case Kind.OffsetNode:
455       checkOffsetNode(md, nametable, (OffsetNode)en, td);
456       return;
457
458     case Kind.TertiaryNode:
459       checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
460       return;
461       
462     case Kind.InstanceOfNode:
463       checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
464       return;
465
466     case Kind.ArrayInitializerNode:
467       checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
468       return;
469     }
470     throw new Error();
471   }
472
473   void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
474     /* Get type descriptor */
475     if (cn.getType()==null) {
476       NameDescriptor typenamed=cn.getTypeName().getName();
477       String typename=typenamed.toString();
478       TypeDescriptor ntd=new TypeDescriptor(getClass(typename));
479       cn.setType(ntd);
480     }
481
482     /* Check the type descriptor */
483     TypeDescriptor cast_type=cn.getType();
484     checkTypeDescriptor(cast_type);
485
486     /* Type check */
487     if (td!=null) {
488       if (!typeutil.isSuperorType(td,cast_type))
489         throw new Error("Cast node returns "+cast_type+", but need "+td);
490     }
491
492     ExpressionNode en=cn.getExpression();
493     checkExpressionNode(md, nametable, en, null);
494     TypeDescriptor etd=en.getType();
495     if (typeutil.isSuperorType(cast_type,etd))     /* Cast trivially succeeds */
496       return;
497
498     if (typeutil.isSuperorType(etd,cast_type))     /* Cast may succeed */
499       return;
500     if (typeutil.isCastable(etd, cast_type))
501       return;
502
503     /* Different branches */
504     /* TODO: change if add interfaces */
505     throw new Error("Cast will always fail\n"+cn.printNode(0));
506   }
507
508   void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
509     ExpressionNode left=fan.getExpression();
510     checkExpressionNode(md,nametable,left,null);
511     TypeDescriptor ltd=left.getType();
512     String fieldname=fan.getFieldName();
513
514     FieldDescriptor fd=null;
515     if (ltd.isArray()&&fieldname.equals("length"))
516       fd=FieldDescriptor.arrayLength;
517     else 
518       fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
519     if(state.MGC) {
520       // TODO add version for normal Java later
521     if(ltd.isStatic()) {
522       // check if this field is a static field
523       if(!fd.isStatic()) {
524         throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
525       }
526     } 
527     }
528     if (fd==null)
529       throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
530
531     if (fd.getType().iswrapper()) {
532       FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
533       fan2.setField(fd);
534       fan.left=fan2;
535       fan.fieldname="value";
536
537       ExpressionNode leftwr=fan.getExpression();
538       TypeDescriptor ltdwr=leftwr.getType();
539       String fieldnamewr=fan.getFieldName();
540       FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
541       fan.setField(fdwr);
542       if (fdwr==null)
543           throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
544     } else {
545       fan.setField(fd);
546     }
547     if (td!=null) {
548       if (!typeutil.isSuperorType(td,fan.getType()))
549         throw new Error("Field node returns "+fan.getType()+", but need "+td);
550     }
551   }
552
553   void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
554     ExpressionNode left=aan.getExpression();
555     checkExpressionNode(md,nametable,left,null);
556
557     checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
558     TypeDescriptor ltd=left.getType();
559     if (ltd.dereference().iswrapper()) {
560       aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
561     }
562
563     if (td!=null)
564       if (!typeutil.isSuperorType(td,aan.getType()))
565         throw new Error("Field node returns "+aan.getType()+", but need "+td);
566   }
567
568   void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
569     /* Resolve the type */
570     Object o=ln.getValue();
571     if (ln.getTypeString().equals("null")) {
572       ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
573     } else if (o instanceof Integer) {
574       ln.setType(new TypeDescriptor(TypeDescriptor.INT));
575     } else if (o instanceof Long) {
576       ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
577     } else if (o instanceof Float) {
578       ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
579     } else if (o instanceof Boolean) {
580       ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
581     } else if (o instanceof Double) {
582       ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
583     } else if (o instanceof Character) {
584       ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
585     } else if (o instanceof String) {
586       ln.setType(new TypeDescriptor(getClass(TypeUtil.StringClass)));
587     }
588
589     if (td!=null)
590       if (!typeutil.isSuperorType(td,ln.getType()))
591         throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
592   }
593
594   void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
595     NameDescriptor nd=nn.getName();
596     if (nd.getBase()!=null) {
597       /* Big hack */
598       /* Rewrite NameNode */
599       ExpressionNode en=translateNameDescriptorintoExpression(nd);
600       nn.setExpression(en);
601       checkExpressionNode(md,nametable,en,td);
602     } else {
603       String varname=nd.toString();
604       Descriptor d=(Descriptor)nametable.get(varname);
605       if (d==null) {
606         if(state.MGC) {
607           // TODO add version for normal Java later
608         ClassDescriptor cd = null;
609         if(((MethodDescriptor)md).isStaticBlock()) {
610           // this is a static block, all the accessed fields should be static field
611           cd = ((MethodDescriptor)md).getClassDesc();
612           SymbolTable fieldtbl = cd.getFieldTable();
613           FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
614           if((fd == null) || (!fd.isStatic()) || (!fd.isVolatile())){
615             // no such field in the class or it is not a static field
616             throw new Error("Name "+varname+" should not be used in static block: "+md);
617           } else {
618             // this is a static field
619             nn.setField(fd);
620             nn.setClassDesc(cd);
621             return;
622           }
623         } else {
624           cd=getClass(varname);
625           if(cd != null) {
626             // this is a class name
627             nn.setClassDesc(cd);
628             return;
629           } else {
630             throw new Error("Name "+varname+" undefined in: "+md);
631           }
632         }
633         } else {
634           throw new Error("Name "+varname+" undefined in: "+md);
635         }
636       }
637       if (d instanceof VarDescriptor) {
638         nn.setVar(d);
639       } else if (d instanceof FieldDescriptor) {
640         FieldDescriptor fd=(FieldDescriptor)d;
641         if (fd.getType().iswrapper()) {
642           String id=nd.getIdentifier();
643           NameDescriptor base=nd.getBase();
644           NameNode n=new NameNode(nn.getName());
645           n.setField(fd);
646           n.setVar((VarDescriptor)nametable.get("this"));        /* Need a pointer to this */
647           FieldAccessNode fan=new FieldAccessNode(n,"value");
648           FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
649           fan.setField(fdval);
650           nn.setExpression(fan);
651         } else {
652           nn.setField(fd);
653           nn.setVar((VarDescriptor)nametable.get("this"));        /* Need a pointer to this */
654         }
655       } else if (d instanceof TagVarDescriptor) {
656         nn.setVar(d);
657       } else throw new Error("Wrong type of descriptor");
658       if (td!=null)
659         if (!typeutil.isSuperorType(td,nn.getType()))
660           throw new Error("Field node returns "+nn.getType()+", but need "+td);
661     }
662   }
663
664   void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
665     TypeDescriptor ltd=ofn.td;
666     checkTypeDescriptor(ltd);
667     
668     String fieldname = ofn.fieldname;
669     FieldDescriptor fd=null;
670     if (ltd.isArray()&&fieldname.equals("length")) {
671       fd=FieldDescriptor.arrayLength;
672     } else {
673       fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
674     }
675
676     ofn.setField(fd);
677     checkField(ltd.getClassDesc(), fd);
678
679     if (fd==null)
680       throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
681
682     if (td!=null) {
683       if (!typeutil.isSuperorType(td, ofn.getType())) {
684         System.out.println(td);
685         System.out.println(ofn.getType());
686         throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
687       }
688     }
689   }
690
691
692   void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
693     checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
694     checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
695     checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
696   }
697
698   void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
699     if (td!=null&&!td.isBoolean())
700       throw new Error("Expecting type "+td+"for instanceof expression");
701     
702     checkTypeDescriptor(tn.getExprType());
703     checkExpressionNode(md, nametable, tn.getExpr(), null);
704   }
705
706   void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
707     for( int i = 0; i < ain.numVarInitializers(); ++i ) {
708       checkExpressionNode(md, nametable, ain.getVarInitializer(i), td); 
709     }
710   }
711
712   void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
713     boolean postinc=true;
714     if (an.getOperation().getBaseOp()==null||
715         (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
716          an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
717       postinc=false;
718     if (!postinc)      
719       checkExpressionNode(md, nametable, an.getSrc(),td);
720     //TODO: Need check on validity of operation here
721     if (!((an.getDest() instanceof FieldAccessNode)||
722           (an.getDest() instanceof ArrayAccessNode)||
723           (an.getDest() instanceof NameNode)))
724       throw new Error("Bad lside in "+an.printNode(0));
725     checkExpressionNode(md, nametable, an.getDest(), null);
726
727     /* We want parameter variables to tasks to be immutable */
728     if (md instanceof TaskDescriptor) {
729       if (an.getDest() instanceof NameNode) {
730         NameNode nn=(NameNode)an.getDest();
731         if (nn.getVar()!=null) {
732           if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
733             throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
734         }
735       }
736     }
737
738     if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
739       //String add
740       ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
741       TypeDescriptor stringtd=new TypeDescriptor(stringcl);
742       NameDescriptor nd=new NameDescriptor("String");
743       NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
744
745       if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
746         MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
747         rightmin.addArgument(an.getSrc());
748         an.right=rightmin;
749         checkExpressionNode(md, nametable, an.getSrc(), null);
750       }
751     }
752
753     if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
754       throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
755     }
756   }
757
758   void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
759       loopstack.push(ln);
760     if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
761       checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
762       checkBlockNode(md, nametable, ln.getBody());
763     } else {
764       //For loop case
765       /* Link in the initializer naming environment */
766       BlockNode bn=ln.getInitializer();
767       bn.getVarTable().setParent(nametable);
768       for(int i=0; i<bn.size(); i++) {
769         BlockStatementNode bsn=bn.get(i);
770         checkBlockStatementNode(md, bn.getVarTable(),bsn);
771       }
772       //check the condition
773       checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
774       checkBlockNode(md, bn.getVarTable(), ln.getBody());
775       checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
776     }
777     loopstack.pop();
778   }
779
780
781   void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
782     TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
783     for(int i=0; i<con.numArgs(); i++) {
784       ExpressionNode en=con.getArg(i);
785       checkExpressionNode(md,nametable,en,null);
786       tdarray[i]=en.getType();
787     }
788
789     TypeDescriptor typetolookin=con.getType();
790     checkTypeDescriptor(typetolookin);
791
792     if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
793       throw new Error(typetolookin + " isn't a "+td);
794
795     /* Check flag effects */
796     if (con.getFlagEffects()!=null) {
797       FlagEffects fe=con.getFlagEffects();
798       ClassDescriptor cd=typetolookin.getClassDesc();
799
800       for(int j=0; j<fe.numEffects(); j++) {
801         FlagEffect flag=fe.getEffect(j);
802         String name=flag.getName();
803         FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
804         //Make sure the flag is declared
805         if (flag_d==null)
806           throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
807         if (flag_d.getExternal())
808           throw new Error("Attempting to modify external flag: "+name);
809         flag.setFlag(flag_d);
810       }
811       for(int j=0; j<fe.numTagEffects(); j++) {
812         TagEffect tag=fe.getTagEffect(j);
813         String name=tag.getName();
814
815         Descriptor d=(Descriptor)nametable.get(name);
816         if (d==null)
817           throw new Error("Tag descriptor "+name+" undeclared");
818         else if (!(d instanceof TagVarDescriptor))
819           throw new Error(name+" is not a tag descriptor");
820         tag.setTag((TagVarDescriptor)d);
821       }
822     }
823
824     if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
825       throw new Error("Can't allocate primitive type:"+con.printNode(0));
826
827     if (!typetolookin.isArray()) {
828       //Array's don't need constructor calls
829       ClassDescriptor classtolookin=typetolookin.getClassDesc();
830
831       Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
832       MethodDescriptor bestmd=null;
833 NextMethod:
834       for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
835         MethodDescriptor currmd=(MethodDescriptor)methodit.next();
836         /* Need correct number of parameters */
837         if (con.numArgs()!=currmd.numParameters())
838           continue;
839         for(int i=0; i<con.numArgs(); i++) {
840           if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
841             continue NextMethod;
842         }
843         /* Local allocations can't call global allocator */
844         if (!con.isGlobal()&&currmd.isGlobal())
845           continue;
846
847         /* Method okay so far */
848         if (bestmd==null)
849           bestmd=currmd;
850         else {
851           if (typeutil.isMoreSpecific(currmd,bestmd)) {
852             bestmd=currmd;
853           } else if (con.isGlobal()&&match(currmd, bestmd)) {
854             if (currmd.isGlobal()&&!bestmd.isGlobal())
855               bestmd=currmd;
856             else if (currmd.isGlobal()&&bestmd.isGlobal())
857               throw new Error();
858           } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
859             throw new Error("No method is most specific");
860           }
861
862           /* Is this more specific than bestmd */
863         }
864       }
865       if (bestmd==null)
866         throw new Error("No method found for "+con.printNode(0)+" in "+md);
867       con.setConstructor(bestmd);
868     }
869   }
870
871
872   /** Check to see if md1 is the same specificity as md2.*/
873
874   boolean match(MethodDescriptor md1, MethodDescriptor md2) {
875     /* Checks if md1 is more specific than md2 */
876     if (md1.numParameters()!=md2.numParameters())
877       throw new Error();
878     for(int i=0; i<md1.numParameters(); i++) {
879       if (!md2.getParamType(i).equals(md1.getParamType(i)))
880         return false;
881     }
882     if (!md2.getReturnType().equals(md1.getReturnType()))
883       return false;
884
885     if (!md2.getClassDesc().equals(md1.getClassDesc()))
886       return false;
887
888     return true;
889   }
890
891
892
893   ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
894     String id=nd.getIdentifier();
895     NameDescriptor base=nd.getBase();
896     if (base==null)
897       return new NameNode(nd);
898     else
899       return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
900   }
901
902
903   void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
904     /*Typecheck subexpressions
905        and get types for expressions*/
906
907     TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
908     for(int i=0; i<min.numArgs(); i++) {
909       ExpressionNode en=min.getArg(i);
910       checkExpressionNode(md,nametable,en,null);
911       tdarray[i]=en.getType();
912     }
913     TypeDescriptor typetolookin=null;
914     if (min.getExpression()!=null) {
915       checkExpressionNode(md,nametable,min.getExpression(),null);
916       typetolookin=min.getExpression().getType();
917       //if (typetolookin==null)
918       //throw new Error(md+" has null return type");
919
920     } else if (min.getBaseName()!=null) {
921       String rootname=min.getBaseName().getRoot();
922       if (rootname.equals("super")) {
923         ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
924         typetolookin=new TypeDescriptor(supercd);
925       } else if (nametable.get(rootname)!=null) {
926         //we have an expression
927         min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
928         checkExpressionNode(md, nametable, min.getExpression(), null);
929         typetolookin=min.getExpression().getType();
930       } else {
931         //we have a type
932         ClassDescriptor cd;
933         if (min.getBaseName().getSymbol().equals("System.out"))
934           cd=getClass("System");
935         else
936           cd=getClass(min.getBaseName().getSymbol());
937         if (cd==null)
938           throw new Error("md = "+ md.toString()+ "  "+min.getBaseName()+" undefined");
939         typetolookin=new TypeDescriptor(cd);
940       }
941     } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
942       ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
943       min.methodid=supercd.getSymbol();
944       typetolookin=new TypeDescriptor(supercd);
945     } else if (md instanceof MethodDescriptor) {
946       typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
947     } else {
948       /* If this a task descriptor we throw an error at this point */
949       throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
950     }
951     if (!typetolookin.isClass())
952       throw new Error("Error with method call to "+min.getMethodName());
953     ClassDescriptor classtolookin=typetolookin.getClassDesc();
954     //System.out.println("Method name="+min.getMethodName());
955
956     Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
957     MethodDescriptor bestmd=null;
958 NextMethod:
959     for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
960       MethodDescriptor currmd=(MethodDescriptor)methodit.next();
961       /* Need correct number of parameters */
962       if (min.numArgs()!=currmd.numParameters())
963         continue;
964       for(int i=0; i<min.numArgs(); i++) {
965         if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
966           continue NextMethod;
967       }
968       /* Method okay so far */
969       if (bestmd==null)
970         bestmd=currmd;
971       else {
972         if (typeutil.isMoreSpecific(currmd,bestmd)) {
973           bestmd=currmd;
974         } else if (!typeutil.isMoreSpecific(bestmd, currmd))
975           throw new Error("No method is most specific");
976
977         /* Is this more specific than bestmd */
978       }
979     }
980     if (bestmd==null)
981       throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
982     min.setMethod(bestmd);
983
984     if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td,  min.getType()))
985       throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
986     /* Check whether we need to set this parameter to implied this */
987     if (!bestmd.isStatic()) {
988       if (min.getExpression()==null) {
989         ExpressionNode en=new NameNode(new NameDescriptor("this"));
990         min.setExpression(en);
991         checkExpressionNode(md, nametable, min.getExpression(), null);
992       }
993     }
994   }
995
996
997   void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
998     checkExpressionNode(md, nametable, on.getLeft(), null);
999     if (on.getRight()!=null)
1000       checkExpressionNode(md, nametable, on.getRight(), null);
1001     TypeDescriptor ltd=on.getLeft().getType();
1002     TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
1003     TypeDescriptor lefttype=null;
1004     TypeDescriptor righttype=null;
1005     Operation op=on.getOp();
1006
1007     switch(op.getOp()) {
1008     case Operation.LOGIC_OR:
1009     case Operation.LOGIC_AND:
1010       if (!(rtd.isBoolean()))
1011         throw new Error();
1012       on.setRightType(rtd);
1013
1014     case Operation.LOGIC_NOT:
1015       if (!(ltd.isBoolean()))
1016         throw new Error();
1017       //no promotion
1018       on.setLeftType(ltd);
1019
1020       on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1021       break;
1022
1023     case Operation.COMP:
1024       // 5.6.2 Binary Numeric Promotion
1025       //TODO unboxing of reference objects
1026       if (ltd.isDouble())
1027         throw new Error();
1028       else if (ltd.isFloat())
1029         throw new Error();
1030       else if (ltd.isLong())
1031         lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1032       else
1033         lefttype=new TypeDescriptor(TypeDescriptor.INT);
1034       on.setLeftType(lefttype);
1035       on.setType(lefttype);
1036       break;
1037
1038     case Operation.BIT_OR:
1039     case Operation.BIT_XOR:
1040     case Operation.BIT_AND:
1041       // 5.6.2 Binary Numeric Promotion
1042       //TODO unboxing of reference objects
1043       if (ltd.isDouble()||rtd.isDouble())
1044         throw new Error();
1045       else if (ltd.isFloat()||rtd.isFloat())
1046         throw new Error();
1047       else if (ltd.isLong()||rtd.isLong())
1048         lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1049       // 090205 hack for boolean
1050       else if (ltd.isBoolean()||rtd.isBoolean())
1051         lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1052       else
1053         lefttype=new TypeDescriptor(TypeDescriptor.INT);
1054       righttype=lefttype;
1055
1056       on.setLeftType(lefttype);
1057       on.setRightType(righttype);
1058       on.setType(lefttype);
1059       break;
1060
1061     case Operation.ISAVAILABLE:
1062       if (!(ltd.isPtr())) {
1063         throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1064       }
1065       lefttype=ltd;
1066       on.setLeftType(lefttype);
1067       on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1068       break;
1069
1070     case Operation.EQUAL:
1071     case Operation.NOTEQUAL:
1072       // 5.6.2 Binary Numeric Promotion
1073       //TODO unboxing of reference objects
1074       if (ltd.isBoolean()||rtd.isBoolean()) {
1075         if (!(ltd.isBoolean()&&rtd.isBoolean()))
1076           throw new Error();
1077         righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1078       } else if (ltd.isPtr()||rtd.isPtr()) {
1079         if (!(ltd.isPtr()&&rtd.isPtr()))
1080           throw new Error();
1081         righttype=rtd;
1082         lefttype=ltd;
1083       } else if (ltd.isDouble()||rtd.isDouble())
1084         righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1085       else if (ltd.isFloat()||rtd.isFloat())
1086         righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1087       else if (ltd.isLong()||rtd.isLong())
1088         righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1089       else
1090         righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1091
1092       on.setLeftType(lefttype);
1093       on.setRightType(righttype);
1094       on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1095       break;
1096
1097
1098
1099     case Operation.LT:
1100     case Operation.GT:
1101     case Operation.LTE:
1102     case Operation.GTE:
1103       // 5.6.2 Binary Numeric Promotion
1104       //TODO unboxing of reference objects
1105       if (!ltd.isNumber()||!rtd.isNumber()) {
1106         if (!ltd.isNumber())
1107           throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1108         if (!rtd.isNumber())
1109           throw new Error("Rightside is not number"+on.printNode(0));
1110       }
1111
1112       if (ltd.isDouble()||rtd.isDouble())
1113         lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1114       else if (ltd.isFloat()||rtd.isFloat())
1115         lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1116       else if (ltd.isLong()||rtd.isLong())
1117         lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1118       else
1119         lefttype=new TypeDescriptor(TypeDescriptor.INT);
1120       righttype=lefttype;
1121       on.setLeftType(lefttype);
1122       on.setRightType(righttype);
1123       on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1124       break;
1125
1126     case Operation.ADD:
1127       if (ltd.isString()||rtd.isString()) {
1128         ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
1129         TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1130         NameDescriptor nd=new NameDescriptor("String");
1131         NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1132         if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1133           MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1134           leftmin.addArgument(on.getLeft());
1135           on.left=leftmin;
1136           checkExpressionNode(md, nametable, on.getLeft(), null);
1137         }
1138
1139         if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1140           MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1141           rightmin.addArgument(on.getRight());
1142           on.right=rightmin;
1143           checkExpressionNode(md, nametable, on.getRight(), null);
1144         }
1145
1146         on.setLeftType(stringtd);
1147         on.setRightType(stringtd);
1148         on.setType(stringtd);
1149         break;
1150       }
1151
1152     case Operation.SUB:
1153     case Operation.MULT:
1154     case Operation.DIV:
1155     case Operation.MOD:
1156       // 5.6.2 Binary Numeric Promotion
1157       //TODO unboxing of reference objects
1158       if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1159         throw new Error("Error in "+on.printNode(0));
1160
1161       if (ltd.isDouble()||rtd.isDouble())
1162         lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1163       else if (ltd.isFloat()||rtd.isFloat())
1164         lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1165       else if (ltd.isLong()||rtd.isLong())
1166         lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1167       else
1168         lefttype=new TypeDescriptor(TypeDescriptor.INT);
1169       righttype=lefttype;
1170       on.setLeftType(lefttype);
1171       on.setRightType(righttype);
1172       on.setType(lefttype);
1173       break;
1174
1175     case Operation.LEFTSHIFT:
1176     case Operation.RIGHTSHIFT:
1177     case Operation.URIGHTSHIFT:
1178       if (!rtd.isIntegerType())
1179         throw new Error();
1180       //5.6.1 Unary Numeric Promotion
1181       if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1182         righttype=new TypeDescriptor(TypeDescriptor.INT);
1183       else
1184         righttype=rtd;
1185
1186       on.setRightType(righttype);
1187       if (!ltd.isIntegerType())
1188         throw new Error();
1189
1190     case Operation.UNARYPLUS:
1191     case Operation.UNARYMINUS:
1192       /*        case Operation.POSTINC:
1193           case Operation.POSTDEC:
1194           case Operation.PREINC:
1195           case Operation.PREDEC:*/
1196       if (!ltd.isNumber())
1197         throw new Error();
1198       //5.6.1 Unary Numeric Promotion
1199       if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1200         lefttype=new TypeDescriptor(TypeDescriptor.INT);
1201       else
1202         lefttype=ltd;
1203       on.setLeftType(lefttype);
1204       on.setType(lefttype);
1205       break;
1206
1207     default:
1208       throw new Error(op.toString());
1209     }
1210
1211     if (td!=null)
1212       if (!typeutil.isSuperorType(td, on.getType())) {
1213         System.out.println(td);
1214         System.out.println(on.getType());
1215         throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));
1216       }
1217   }
1218 }