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