Socket I/O code and Example
[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             checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
266         else
267             if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
268                 throw new Error("Need to return something for "+md);
269     }
270
271     void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
272         if (md instanceof MethodDescriptor)
273             throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
274         checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects());
275         checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
276     }
277
278     void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
279         checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
280         checkBlockNode(md, nametable, isn.getTrueBlock());
281         if (isn.getFalseBlock()!=null)
282             checkBlockNode(md, nametable, isn.getFalseBlock());
283     }
284     
285     void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
286         switch(en.kind()) {
287         case Kind.AssignmentNode:
288             checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
289             return;
290         case Kind.CastNode:
291             checkCastNode(md,nametable,(CastNode)en,td);
292             return;
293         case Kind.CreateObjectNode:
294             checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
295             return;
296         case Kind.FieldAccessNode:
297             checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
298             return;
299         case Kind.ArrayAccessNode:
300             checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
301             return;
302         case Kind.LiteralNode:
303             checkLiteralNode(md,nametable,(LiteralNode)en,td);
304             return;
305         case Kind.MethodInvokeNode:
306             checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
307             return;
308         case Kind.NameNode:
309             checkNameNode(md,nametable,(NameNode)en,td);
310             return;
311         case Kind.OpNode:
312             checkOpNode(md,nametable,(OpNode)en,td);
313             return;
314         }
315         throw new Error();
316     }
317
318     void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
319         /* Get type descriptor */
320         if (cn.getType()==null) {
321             NameDescriptor typenamed=cn.getTypeName().getName();
322             String typename=typenamed.toString();
323             TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
324             cn.setType(ntd);
325         }
326
327         /* Check the type descriptor */
328         TypeDescriptor cast_type=cn.getType();
329         checkTypeDescriptor(cast_type);
330
331         /* Type check */
332         if (td!=null) {
333             if (!typeutil.isSuperorType(td,cast_type))
334                 throw new Error("Cast node returns "+cast_type+", but need "+td);
335         }
336
337         ExpressionNode en=cn.getExpression();
338         checkExpressionNode(md, nametable, en, null);
339         TypeDescriptor etd=en.getType();
340         if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
341             return;
342
343         if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
344             return;
345
346         /* Different branches */
347         /* TODO: change if add interfaces */
348         throw new Error("Cast will always fail\n"+cn.printNode(0));
349     }
350
351     void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
352         ExpressionNode left=fan.getExpression();
353         checkExpressionNode(md,nametable,left,null);
354         TypeDescriptor ltd=left.getType();
355         String fieldname=fan.getFieldName();
356
357         FieldDescriptor fd=null;
358         if (ltd.isArray()&&fieldname.equals("length"))
359             fd=FieldDescriptor.arrayLength;
360         else
361             fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
362         if (fd==null)
363             throw new Error("Unknown field "+fieldname);
364         fan.setField(fd);
365         if (td!=null)
366             if (!typeutil.isSuperorType(td,fan.getType()))
367                 throw new Error("Field node returns "+fan.getType()+", but need "+td);
368     }
369
370     void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
371         ExpressionNode left=aan.getExpression();
372         checkExpressionNode(md,nametable,left,null);
373
374         checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
375         TypeDescriptor ltd=left.getType();
376
377         if (td!=null)
378             if (!typeutil.isSuperorType(td,aan.getType()))
379                 throw new Error("Field node returns "+aan.getType()+", but need "+td);
380     }
381
382     void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
383         /* Resolve the type */
384         Object o=ln.getValue();
385         if (ln.getTypeString().equals("null")) {
386             ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
387         } else if (o instanceof Integer) {
388             ln.setType(new TypeDescriptor(TypeDescriptor.INT));
389         } else if (o instanceof Long) {
390             ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
391         } else if (o instanceof Float) {
392             ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
393         } else if (o instanceof Boolean) {
394             ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
395         } else if (o instanceof Double) {
396             ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
397         } else if (o instanceof Character) {
398             ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
399         } else if (o instanceof String) {
400             ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
401         }
402
403         if (td!=null)
404             if (!typeutil.isSuperorType(td,ln.getType()))
405                 throw new Error("Field node returns "+ln.getType()+", but need "+td);
406     }
407
408     void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
409         NameDescriptor nd=nn.getName();
410         if (nd.getBase()!=null) {
411             /* Big hack */
412             /* Rewrite NameNode */
413             ExpressionNode en=translateNameDescriptorintoExpression(nd);
414             nn.setExpression(en);
415             checkExpressionNode(md,nametable,en,td);
416         } else {
417             String varname=nd.toString();
418             Descriptor d=(Descriptor)nametable.get(varname);
419             if (d==null) {
420                 throw new Error("Name "+varname+" undefined");
421             }
422             if (d instanceof VarDescriptor) {
423                 nn.setVar((VarDescriptor)d);
424             } else if (d instanceof FieldDescriptor) {
425                 nn.setField((FieldDescriptor)d);
426                 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
427             }
428             if (td!=null)
429                 if (!typeutil.isSuperorType(td,nn.getType()))
430                     throw new Error("Field node returns "+nn.getType()+", but need "+td);
431         }
432     }
433
434     void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
435         boolean postinc=true;
436         if (an.getOperation().getBaseOp()==null||
437             (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
438              an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
439             postinc=false;
440
441         if (!postinc)
442             checkExpressionNode(md, nametable, an.getSrc() ,td);
443         //TODO: Need check on validity of operation here
444         if (!((an.getDest() instanceof FieldAccessNode)||
445               (an.getDest() instanceof ArrayAccessNode)||
446               (an.getDest() instanceof NameNode)))
447             throw new Error("Bad lside in "+an.printNode(0));
448         checkExpressionNode(md, nametable, an.getDest(), null);
449
450         /* We want parameter variables to tasks to be immutable */
451         if (md instanceof TaskDescriptor) {
452             if (an.getDest() instanceof NameNode) {
453                 NameNode nn=(NameNode)an.getDest();
454                 if (nn.getVar()!=null) {
455                     if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
456                         throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
457                 }
458             }
459         }
460         
461         if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
462             throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
463         }
464     }
465
466     void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
467         if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
468             checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
469             checkBlockNode(md, nametable, ln.getBody());
470         } else {
471             //For loop case
472             /* Link in the initializer naming environment */
473             BlockNode bn=ln.getInitializer();
474             bn.getVarTable().setParent(nametable);
475             for(int i=0;i<bn.size();i++) {
476                 BlockStatementNode bsn=bn.get(i);
477                 checkBlockStatementNode(md, bn.getVarTable(),bsn);
478             }
479             //check the condition
480             checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
481             checkBlockNode(md, bn.getVarTable(), ln.getBody());
482             checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
483         }
484     }
485
486
487     void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
488         TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
489         for(int i=0;i<con.numArgs();i++) {
490             ExpressionNode en=con.getArg(i);
491             checkExpressionNode(md,nametable,en,null);
492             tdarray[i]=en.getType();
493         }
494
495         TypeDescriptor typetolookin=con.getType();
496         checkTypeDescriptor(typetolookin);
497
498         /* Check flag effects */
499         if (con.getFlagEffects()!=null) {
500             FlagEffects fe=con.getFlagEffects();
501             ClassDescriptor cd=typetolookin.getClassDesc();
502             
503             for(int j=0;j<fe.numEffects();j++) {
504                 FlagEffect flag=fe.getEffect(j);
505                 String name=flag.getName();
506                 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
507                 //Make sure the flag is declared
508                 if (flag_d==null)
509                     throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
510                 if (flag_d.getExternal())
511                     throw new Error("Attempting to modify external flag: "+name);
512                 flag.setFlag(flag_d);
513             }
514         }
515
516
517         if ((!typetolookin.isClass())&&(!typetolookin.isArray())) 
518             throw new Error("Can't allocate primitive type:"+con.printNode(0));
519
520         if (!typetolookin.isArray()) {
521             //Array's don't need constructor calls
522             ClassDescriptor classtolookin=typetolookin.getClassDesc();
523             System.out.println("Looking for "+typetolookin.getSymbol());
524             System.out.println(classtolookin.getMethodTable());
525             
526             Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
527             MethodDescriptor bestmd=null;
528         NextMethod:
529             for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
530                 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
531                 /* Need correct number of parameters */
532                 System.out.println("Examining: "+currmd);
533                 if (con.numArgs()!=currmd.numParameters())
534                     continue;
535                 for(int i=0;i<con.numArgs();i++) {
536                     if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
537                         continue NextMethod;
538                 }
539                 /* Method okay so far */
540                 if (bestmd==null)
541                     bestmd=currmd;
542                 else {
543                     if (isMoreSpecific(currmd,bestmd)) {
544                         bestmd=currmd;
545                     } else if (!isMoreSpecific(bestmd, currmd))
546                         throw new Error("No method is most specific");
547                     
548                     /* Is this more specific than bestmd */
549                 }
550             }
551             if (bestmd==null)
552                 throw new Error("No method found for "+con.printNode(0));
553             con.setConstructor(bestmd);
554         }
555     }
556
557
558     /** Check to see if md1 is more specific than md2...  Informally
559         if md2 could always be called given the arguments passed into
560         md1 */
561
562     boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
563         /* Checks if md1 is more specific than md2 */
564         if (md1.numParameters()!=md2.numParameters())
565             throw new Error();
566         for(int i=0;i<md1.numParameters();i++) {
567             if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
568                 return false;
569         }
570         if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
571                 return false;
572         return true;
573     }
574
575     ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
576         String id=nd.getIdentifier();
577         NameDescriptor base=nd.getBase();
578         if (base==null) 
579             return new NameNode(nd);
580         else 
581             return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
582     }
583
584
585     void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
586         /*Typecheck subexpressions
587           and get types for expressions*/
588
589         TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
590         for(int i=0;i<min.numArgs();i++) {
591             ExpressionNode en=min.getArg(i);
592             checkExpressionNode(md,nametable,en,null);
593             tdarray[i]=en.getType();
594         }
595         TypeDescriptor typetolookin=null;
596         if (min.getExpression()!=null) {
597             checkExpressionNode(md,nametable,min.getExpression(),null);
598             typetolookin=min.getExpression().getType();
599         } else if (min.getBaseName()!=null) {
600             String rootname=min.getBaseName().getRoot();
601             if (nametable.get(rootname)!=null) {
602                 //we have an expression
603                 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
604                 checkExpressionNode(md, nametable, min.getExpression(), null);
605                 typetolookin=min.getExpression().getType();
606             } else {
607                 //we have a type
608                 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
609                 if (cd==null)
610                     throw new Error(min.getBaseName()+" undefined");
611                 typetolookin=new TypeDescriptor(cd);
612             }
613         } else if (md instanceof MethodDescriptor) {
614             typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
615         } else {
616             /* If this a task descriptor we throw an error at this point */
617             throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
618         }
619         if (!typetolookin.isClass()) 
620             throw new Error();
621         ClassDescriptor classtolookin=typetolookin.getClassDesc();
622         System.out.println("Method name="+min.getMethodName());
623         Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
624         MethodDescriptor bestmd=null;
625         NextMethod:
626         for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
627             MethodDescriptor currmd=(MethodDescriptor)methodit.next();
628             /* Need correct number of parameters */
629             if (min.numArgs()!=currmd.numParameters())
630                 continue;
631             for(int i=0;i<min.numArgs();i++) {
632                 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
633                     continue NextMethod;
634             }
635             /* Method okay so far */
636             if (bestmd==null)
637                 bestmd=currmd;
638             else {
639                 if (isMoreSpecific(currmd,bestmd)) {
640                     bestmd=currmd;
641                 } else if (!isMoreSpecific(bestmd, currmd))
642                     throw new Error("No method is most specific");
643                 
644                 /* Is this more specific than bestmd */
645             }
646         }
647         if (bestmd==null)
648             throw new Error("No method found for :"+min.printNode(0));
649         min.setMethod(bestmd);
650         /* Check whether we need to set this parameter to implied this */
651         if (!bestmd.isStatic()) {
652             if (min.getExpression()==null) {
653                 ExpressionNode en=new NameNode(new NameDescriptor("this"));
654                 min.setExpression(en);
655                 checkExpressionNode(md, nametable, min.getExpression(), null);
656             }
657         }
658
659     }
660
661
662     void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
663         checkExpressionNode(md, nametable, on.getLeft(), null);
664         if (on.getRight()!=null)
665             checkExpressionNode(md, nametable, on.getRight(), null);
666         TypeDescriptor ltd=on.getLeft().getType();
667         TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
668         TypeDescriptor lefttype=null;
669         TypeDescriptor righttype=null;
670         Operation op=on.getOp();
671
672         switch(op.getOp()) {
673         case Operation.LOGIC_OR:
674         case Operation.LOGIC_AND:
675             if (!(ltd.isBoolean()&&rtd.isBoolean()))
676                 throw new Error();
677             //no promotion
678             on.setLeftType(ltd);
679             on.setRightType(rtd);
680             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
681             break;
682
683         case Operation.BIT_OR:
684         case Operation.BIT_XOR:
685         case Operation.BIT_AND:
686             // 5.6.2 Binary Numeric Promotion
687             //TODO unboxing of reference objects
688             if (ltd.isDouble()||rtd.isDouble())
689                 throw new Error();
690             else if (ltd.isFloat()||rtd.isFloat())
691                 throw new Error();
692             else if (ltd.isLong()||rtd.isLong())
693                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
694             else 
695                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
696             righttype=lefttype;
697
698             on.setLeftType(lefttype);
699             on.setRightType(righttype);
700             on.setType(lefttype);
701             break;
702
703         case Operation.EQUAL:
704         case Operation.NOTEQUAL:
705             // 5.6.2 Binary Numeric Promotion
706             //TODO unboxing of reference objects
707             if (ltd.isBoolean()||rtd.isBoolean()) {
708                 if (!(ltd.isBoolean()&&rtd.isBoolean()))
709                     throw new Error();
710                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
711             } else if (ltd.isPtr()||ltd.isArray()||rtd.isPtr()||rtd.isArray()) {
712                 if (!((ltd.isPtr()||ltd.isArray())&&(rtd.isPtr()||rtd.isArray())))
713                     throw new Error();
714                 righttype=rtd;
715                 lefttype=ltd;
716             } else if (ltd.isDouble()||rtd.isDouble())
717                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
718             else if (ltd.isFloat()||rtd.isFloat())
719                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
720             else if (ltd.isLong()||rtd.isLong())
721                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
722             else 
723                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
724
725             on.setLeftType(lefttype);
726             on.setRightType(righttype);
727             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
728             break;
729
730
731
732         case Operation.LT:
733         case Operation.GT:
734         case Operation.LTE:
735         case Operation.GTE:
736             // 5.6.2 Binary Numeric Promotion
737             //TODO unboxing of reference objects
738             if (!ltd.isNumber()||!rtd.isNumber())
739                 throw new Error();
740
741             if (ltd.isDouble()||rtd.isDouble())
742                 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
743             else if (ltd.isFloat()||rtd.isFloat())
744                 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
745             else if (ltd.isLong()||rtd.isLong())
746                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
747             else 
748                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
749             righttype=lefttype;
750             on.setLeftType(lefttype);
751             on.setRightType(righttype);
752             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
753             break;
754
755         case Operation.ADD:
756             //TODO: Need special case for strings eventually
757             
758             
759         case Operation.SUB:
760         case Operation.MULT:
761         case Operation.DIV:
762         case Operation.MOD:
763             // 5.6.2 Binary Numeric Promotion
764             //TODO unboxing of reference objects
765             if (!ltd.isNumber()||!rtd.isNumber())
766                 throw new Error("Error in "+on.printNode(0));
767
768             if (ltd.isDouble()||rtd.isDouble())
769                 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
770             else if (ltd.isFloat()||rtd.isFloat())
771                 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
772             else if (ltd.isLong()||rtd.isLong())
773                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
774             else 
775                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
776             righttype=lefttype;
777             on.setLeftType(lefttype);
778             on.setRightType(righttype);
779             on.setType(lefttype);
780             break;
781
782         case Operation.LEFTSHIFT:
783         case Operation.RIGHTSHIFT:
784             if (!rtd.isIntegerType())
785                 throw new Error();
786             //5.6.1 Unary Numeric Promotion
787             if (rtd.isByte()||rtd.isShort()||rtd.isInt())
788                 righttype=new TypeDescriptor(TypeDescriptor.INT);
789             else
790                 righttype=rtd;
791
792             on.setRightType(righttype);
793             if (!ltd.isIntegerType())
794                 throw new Error();
795         case Operation.UNARYPLUS:
796         case Operation.UNARYMINUS:
797             /*  case Operation.POSTINC:
798                 case Operation.POSTDEC:
799                 case Operation.PREINC:
800                 case Operation.PREDEC:*/
801             if (!ltd.isNumber())
802                 throw new Error();
803             //5.6.1 Unary Numeric Promotion
804             if (ltd.isByte()||ltd.isShort()||ltd.isInt())
805                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
806             else
807                 lefttype=ltd;
808             on.setLeftType(lefttype);
809             on.setType(lefttype);
810             break;
811         default:
812             throw new Error();
813         }
814    
815         if (td!=null)
816             if (!typeutil.isSuperorType(td, on.getType())) {
817                 System.out.println(td);
818                 System.out.println(on.getType());
819                 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));     
820             }
821     }
822 }