fix type checking. We now complain about wrong type declarations
[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         if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
536             throw new Error(typetolookin + " isn't a "+td);
537
538
539
540         /* Check flag effects */
541         if (con.getFlagEffects()!=null) {
542             FlagEffects fe=con.getFlagEffects();
543             ClassDescriptor cd=typetolookin.getClassDesc();
544             
545             for(int j=0;j<fe.numEffects();j++) {
546                 FlagEffect flag=fe.getEffect(j);
547                 String name=flag.getName();
548                 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
549                 //Make sure the flag is declared
550                 if (flag_d==null)
551                     throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
552                 if (flag_d.getExternal())
553                     throw new Error("Attempting to modify external flag: "+name);
554                 flag.setFlag(flag_d);
555             }
556             for(int j=0;j<fe.numTagEffects();j++) {
557                 TagEffect tag=fe.getTagEffect(j);
558                 String name=tag.getName();
559                 
560                 Descriptor d=(Descriptor)nametable.get(name);
561                 if (d==null)
562                     throw new Error("Tag descriptor "+name+" undeclared");
563                 else if (!(d instanceof TagVarDescriptor))
564                     throw new Error(name+" is not a tag descriptor");
565                 tag.setTag((TagVarDescriptor)d);
566             }
567         }
568
569
570         if ((!typetolookin.isClass())&&(!typetolookin.isArray())) 
571             throw new Error("Can't allocate primitive type:"+con.printNode(0));
572
573         if (!typetolookin.isArray()) {
574             //Array's don't need constructor calls
575             ClassDescriptor classtolookin=typetolookin.getClassDesc();
576             //System.out.println("Looking for "+typetolookin.getSymbol());
577             //System.out.println(classtolookin.getMethodTable());
578             
579             Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
580             MethodDescriptor bestmd=null;
581         NextMethod:
582             for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
583                 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
584                 /* Need correct number of parameters */
585                 //System.out.println("Examining: "+currmd);
586                 if (con.numArgs()!=currmd.numParameters())
587                     continue;
588                 for(int i=0;i<con.numArgs();i++) {
589                     if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
590                         continue NextMethod;
591                 }
592                 /* Method okay so far */
593                 if (bestmd==null)
594                     bestmd=currmd;
595                 else {
596                     if (isMoreSpecific(currmd,bestmd)) {
597                         bestmd=currmd;
598                     } else if (!isMoreSpecific(bestmd, currmd))
599                         throw new Error("No method is most specific");
600                     
601                     /* Is this more specific than bestmd */
602                 }
603             }
604             if (bestmd==null)
605                 throw new Error("No method found for "+con.printNode(0));
606             con.setConstructor(bestmd);
607         }
608     }
609
610
611     /** Check to see if md1 is more specific than md2...  Informally
612         if md2 could always be called given the arguments passed into
613         md1 */
614
615     boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
616         /* Checks if md1 is more specific than md2 */
617         if (md1.numParameters()!=md2.numParameters())
618             throw new Error();
619         for(int i=0;i<md1.numParameters();i++) {
620             if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
621                 return false;
622         }
623         if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
624                 return false;
625
626         if (!typeutil.isSuperorType(md2.getClassDesc(), md1.getClassDesc()))
627                 return false;
628
629         return true;
630     }
631
632     ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
633         String id=nd.getIdentifier();
634         NameDescriptor base=nd.getBase();
635         if (base==null) 
636             return new NameNode(nd);
637         else 
638             return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
639     }
640
641
642     void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
643         /*Typecheck subexpressions
644           and get types for expressions*/
645
646         TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
647         for(int i=0;i<min.numArgs();i++) {
648             ExpressionNode en=min.getArg(i);
649             checkExpressionNode(md,nametable,en,null);
650             tdarray[i]=en.getType();
651         }
652         TypeDescriptor typetolookin=null;
653         if (min.getExpression()!=null) {
654             checkExpressionNode(md,nametable,min.getExpression(),null);
655             typetolookin=min.getExpression().getType();
656         } else if (min.getBaseName()!=null) {
657             String rootname=min.getBaseName().getRoot();
658             if (nametable.get(rootname)!=null) {
659                 //we have an expression
660                 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
661                 checkExpressionNode(md, nametable, min.getExpression(), null);
662                 typetolookin=min.getExpression().getType();
663             } else {
664                 //we have a type
665                 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
666                 if (cd==null)
667                     throw new Error(min.getBaseName()+" undefined");
668                 typetolookin=new TypeDescriptor(cd);
669             }
670         } else if (md instanceof MethodDescriptor) {
671             typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
672         } else {
673             /* If this a task descriptor we throw an error at this point */
674             throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
675         }
676         if (!typetolookin.isClass()) 
677             throw new Error("Error with method call to "+min.getMethodName());
678         ClassDescriptor classtolookin=typetolookin.getClassDesc();
679         //System.out.println("Method name="+min.getMethodName());
680
681         Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
682         MethodDescriptor bestmd=null;
683         NextMethod:
684         for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
685             MethodDescriptor currmd=(MethodDescriptor)methodit.next();
686             /* Need correct number of parameters */
687             if (min.numArgs()!=currmd.numParameters())
688                 continue;
689             for(int i=0;i<min.numArgs();i++) {
690                 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
691                     continue NextMethod;
692             }
693             /* Method okay so far */
694             if (bestmd==null)
695                 bestmd=currmd;
696             else {
697                 if (isMoreSpecific(currmd,bestmd)) {
698                     bestmd=currmd;
699                 } else if (!isMoreSpecific(bestmd, currmd))
700                     throw new Error("No method is most specific");
701                 
702                 /* Is this more specific than bestmd */
703             }
704         }
705         if (bestmd==null)
706             throw new Error("No method found for :"+min.printNode(0));
707         min.setMethod(bestmd);
708
709         if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td,  min.getType()))
710             throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
711         /* Check whether we need to set this parameter to implied this */
712         if (!bestmd.isStatic()) {
713             if (min.getExpression()==null) {
714                 ExpressionNode en=new NameNode(new NameDescriptor("this"));
715                 min.setExpression(en);
716                 checkExpressionNode(md, nametable, min.getExpression(), null);
717             }
718         }
719     }
720
721
722     void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
723         checkExpressionNode(md, nametable, on.getLeft(), null);
724         if (on.getRight()!=null)
725             checkExpressionNode(md, nametable, on.getRight(), null);
726         TypeDescriptor ltd=on.getLeft().getType();
727         TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
728         TypeDescriptor lefttype=null;
729         TypeDescriptor righttype=null;
730         Operation op=on.getOp();
731
732         switch(op.getOp()) {
733         case Operation.LOGIC_OR:
734         case Operation.LOGIC_AND:
735             if (!(rtd.isBoolean()))
736                 throw new Error();
737             on.setRightType(rtd);
738         case Operation.LOGIC_NOT:
739             if (!(ltd.isBoolean()))
740                 throw new Error();
741             //no promotion
742             on.setLeftType(ltd);
743
744             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
745             break;
746
747         case Operation.BIT_OR:
748         case Operation.BIT_XOR:
749         case Operation.BIT_AND:
750             // 5.6.2 Binary Numeric Promotion
751             //TODO unboxing of reference objects
752             if (ltd.isDouble()||rtd.isDouble())
753                 throw new Error();
754             else if (ltd.isFloat()||rtd.isFloat())
755                 throw new Error();
756             else if (ltd.isLong()||rtd.isLong())
757                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
758             else 
759                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
760             righttype=lefttype;
761
762             on.setLeftType(lefttype);
763             on.setRightType(righttype);
764             on.setType(lefttype);
765             break;
766
767         case Operation.EQUAL:
768         case Operation.NOTEQUAL:
769             // 5.6.2 Binary Numeric Promotion
770             //TODO unboxing of reference objects
771             if (ltd.isBoolean()||rtd.isBoolean()) {
772                 if (!(ltd.isBoolean()&&rtd.isBoolean()))
773                     throw new Error();
774                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
775             } else if (ltd.isPtr()||ltd.isArray()||rtd.isPtr()||rtd.isArray()) {
776                 if (!((ltd.isPtr()||ltd.isArray())&&(rtd.isPtr()||rtd.isArray())))
777                     throw new Error();
778                 righttype=rtd;
779                 lefttype=ltd;
780             } else if (ltd.isDouble()||rtd.isDouble())
781                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
782             else if (ltd.isFloat()||rtd.isFloat())
783                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
784             else if (ltd.isLong()||rtd.isLong())
785                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
786             else 
787                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
788
789             on.setLeftType(lefttype);
790             on.setRightType(righttype);
791             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
792             break;
793
794
795
796         case Operation.LT:
797         case Operation.GT:
798         case Operation.LTE:
799         case Operation.GTE:
800             // 5.6.2 Binary Numeric Promotion
801             //TODO unboxing of reference objects
802             if (!ltd.isNumber()||!rtd.isNumber())
803                 throw new Error();
804
805             if (ltd.isDouble()||rtd.isDouble())
806                 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
807             else if (ltd.isFloat()||rtd.isFloat())
808                 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
809             else if (ltd.isLong()||rtd.isLong())
810                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
811             else 
812                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
813             righttype=lefttype;
814             on.setLeftType(lefttype);
815             on.setRightType(righttype);
816             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
817             break;
818
819         case Operation.ADD:
820             //TODO: Need special case for strings eventually
821             
822             
823         case Operation.SUB:
824         case Operation.MULT:
825         case Operation.DIV:
826         case Operation.MOD:
827             // 5.6.2 Binary Numeric Promotion
828             //TODO unboxing of reference objects
829             if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
830                 throw new Error("Error in "+on.printNode(0));
831
832             if (ltd.isDouble()||rtd.isDouble())
833                 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
834             else if (ltd.isFloat()||rtd.isFloat())
835                 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
836             else if (ltd.isLong()||rtd.isLong())
837                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
838             else 
839                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
840             righttype=lefttype;
841             on.setLeftType(lefttype);
842             on.setRightType(righttype);
843             on.setType(lefttype);
844             break;
845
846         case Operation.LEFTSHIFT:
847         case Operation.RIGHTSHIFT:
848             if (!rtd.isIntegerType())
849                 throw new Error();
850             //5.6.1 Unary Numeric Promotion
851             if (rtd.isByte()||rtd.isShort()||rtd.isInt())
852                 righttype=new TypeDescriptor(TypeDescriptor.INT);
853             else
854                 righttype=rtd;
855
856             on.setRightType(righttype);
857             if (!ltd.isIntegerType())
858                 throw new Error();
859         case Operation.UNARYPLUS:
860         case Operation.UNARYMINUS:
861             /*  case Operation.POSTINC:
862                 case Operation.POSTDEC:
863                 case Operation.PREINC:
864                 case Operation.PREDEC:*/
865             if (!ltd.isNumber())
866                 throw new Error();
867             //5.6.1 Unary Numeric Promotion
868             if (ltd.isByte()||ltd.isShort()||ltd.isInt())
869                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
870             else
871                 lefttype=ltd;
872             on.setLeftType(lefttype);
873             on.setType(lefttype);
874             break;
875         default:
876             throw new Error(op.toString());
877         }
878    
879         if (td!=null)
880             if (!typeutil.isSuperorType(td, on.getType())) {
881                 System.out.println(td);
882                 System.out.println(on.getType());
883                 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));     
884             }
885     }
886 }