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