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