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