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