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