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