This commit was manufactured by cvs2svn to create tag 'buildscript'.
[IRC.git] /
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 in "+td);
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()+" in "+md+" 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)+" in "+md);
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
506         /* We want parameter variables to tasks to be immutable */
507         if (md instanceof TaskDescriptor) {
508             if (an.getDest() instanceof NameNode) {
509                 NameNode nn=(NameNode)an.getDest();
510                 if (nn.getVar()!=null) {
511                     if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
512                         throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
513                 }
514             }
515         }
516         
517         if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
518             //String add
519             ClassDescriptor stringcl=typeutil.getClass(TypeUtil.StringClass);
520             TypeDescriptor stringtd=new TypeDescriptor(stringcl);
521             NameDescriptor nd=new NameDescriptor("String");
522             NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
523             
524             if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
525                 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
526                 rightmin.addArgument(an.getSrc());
527                 an.right=rightmin;
528                 checkExpressionNode(md, nametable, an.getSrc(), null);
529             }
530         }
531
532         
533         if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
534             throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
535         }
536     }
537
538     void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
539         if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
540             checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
541             checkBlockNode(md, nametable, ln.getBody());
542         } else {
543             //For loop case
544             /* Link in the initializer naming environment */
545             BlockNode bn=ln.getInitializer();
546             bn.getVarTable().setParent(nametable);
547             for(int i=0;i<bn.size();i++) {
548                 BlockStatementNode bsn=bn.get(i);
549                 checkBlockStatementNode(md, bn.getVarTable(),bsn);
550             }
551             //check the condition
552             checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
553             checkBlockNode(md, bn.getVarTable(), ln.getBody());
554             checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
555         }
556     }
557
558
559     void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
560         TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
561         for(int i=0;i<con.numArgs();i++) {
562             ExpressionNode en=con.getArg(i);
563             checkExpressionNode(md,nametable,en,null);
564             tdarray[i]=en.getType();
565         }
566
567         TypeDescriptor typetolookin=con.getType();
568         checkTypeDescriptor(typetolookin);
569
570         if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
571             throw new Error(typetolookin + " isn't a "+td);
572
573         /* Check flag effects */
574         if (con.getFlagEffects()!=null) {
575             FlagEffects fe=con.getFlagEffects();
576             ClassDescriptor cd=typetolookin.getClassDesc();
577             
578             for(int j=0;j<fe.numEffects();j++) {
579                 FlagEffect flag=fe.getEffect(j);
580                 String name=flag.getName();
581                 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
582                 //Make sure the flag is declared
583                 if (flag_d==null)
584                     throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
585                 if (flag_d.getExternal())
586                     throw new Error("Attempting to modify external flag: "+name);
587                 flag.setFlag(flag_d);
588             }
589             for(int j=0;j<fe.numTagEffects();j++) {
590                 TagEffect tag=fe.getTagEffect(j);
591                 String name=tag.getName();
592                 
593                 Descriptor d=(Descriptor)nametable.get(name);
594                 if (d==null)
595                     throw new Error("Tag descriptor "+name+" undeclared");
596                 else if (!(d instanceof TagVarDescriptor))
597                     throw new Error(name+" is not a tag descriptor");
598                 tag.setTag((TagVarDescriptor)d);
599             }
600         }
601
602         if ((!typetolookin.isClass())&&(!typetolookin.isArray())) 
603             throw new Error("Can't allocate primitive type:"+con.printNode(0));
604
605         if (!typetolookin.isArray()) {
606             //Array's don't need constructor calls
607             ClassDescriptor classtolookin=typetolookin.getClassDesc();
608
609             Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
610             MethodDescriptor bestmd=null;
611         NextMethod:
612             for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
613                 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
614                 /* Need correct number of parameters */
615                 if (con.numArgs()!=currmd.numParameters())
616                     continue;
617                 for(int i=0;i<con.numArgs();i++) {
618                     if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
619                         continue NextMethod;
620                 }
621                 /* Local allocations can't call global allocator */
622                 if (!con.isGlobal()&&currmd.isGlobal())
623                     continue;
624
625                 /* Method okay so far */
626                 if (bestmd==null)
627                     bestmd=currmd;
628                 else {
629                     if (typeutil.isMoreSpecific(currmd,bestmd)) {
630                         bestmd=currmd;
631                     } else if (con.isGlobal()&&match(currmd, bestmd)) {
632                         if (currmd.isGlobal()&&!bestmd.isGlobal())
633                             bestmd=currmd;
634                         else if (currmd.isGlobal()&&bestmd.isGlobal())
635                             throw new Error();
636                     } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
637                         throw new Error("No method is most specific");
638                     }
639                     
640                     /* Is this more specific than bestmd */
641                 }
642             }
643             if (bestmd==null)
644                 throw new Error("No method found for "+con.printNode(0)+" in "+md);
645             con.setConstructor(bestmd);
646         }
647     }
648
649
650     /** Check to see if md1 is the same specificity as md2.*/
651
652     boolean match(MethodDescriptor md1, MethodDescriptor md2) {
653         /* Checks if md1 is more specific than md2 */
654         if (md1.numParameters()!=md2.numParameters())
655             throw new Error();
656         for(int i=0;i<md1.numParameters();i++) {
657             if (!md2.getParamType(i).equals(md1.getParamType(i)))
658                 return false;
659         }
660         if (!md2.getReturnType().equals(md1.getReturnType()))
661                 return false;
662
663         if (!md2.getClassDesc().equals(md1.getClassDesc()))
664                 return false;
665
666         return true;
667     }
668
669
670
671     ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
672         String id=nd.getIdentifier();
673         NameDescriptor base=nd.getBase();
674         if (base==null) 
675             return new NameNode(nd);
676         else 
677             return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
678     }
679
680
681     void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
682         /*Typecheck subexpressions
683           and get types for expressions*/
684
685         TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
686         for(int i=0;i<min.numArgs();i++) {
687             ExpressionNode en=min.getArg(i);
688             checkExpressionNode(md,nametable,en,null);
689             tdarray[i]=en.getType();
690         }
691         TypeDescriptor typetolookin=null;
692         if (min.getExpression()!=null) {
693             checkExpressionNode(md,nametable,min.getExpression(),null);
694             typetolookin=min.getExpression().getType();
695         } else if (min.getBaseName()!=null) {
696             String rootname=min.getBaseName().getRoot();
697             if (rootname.equals("super")) {
698                 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
699                 typetolookin=new TypeDescriptor(supercd);
700             } else if (nametable.get(rootname)!=null) {
701                 //we have an expression
702                 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
703                 checkExpressionNode(md, nametable, min.getExpression(), null);
704                 typetolookin=min.getExpression().getType();
705             } else {
706                 //we have a type
707                 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
708                 if (cd==null)
709                     throw new Error("md = "+ md.toString()+ "  "+min.getBaseName()+" undefined");
710                 typetolookin=new TypeDescriptor(cd);
711             }
712         } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
713             ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
714             min.methodid=supercd.getSymbol();
715             typetolookin=new TypeDescriptor(supercd);
716         } else if (md instanceof MethodDescriptor) {
717             typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
718         } else {
719             /* If this a task descriptor we throw an error at this point */
720             throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
721         }
722         if (!typetolookin.isClass()) 
723             throw new Error("Error with method call to "+min.getMethodName());
724         ClassDescriptor classtolookin=typetolookin.getClassDesc();
725         //System.out.println("Method name="+min.getMethodName());
726
727         Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
728         MethodDescriptor bestmd=null;
729         NextMethod:
730         for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
731             MethodDescriptor currmd=(MethodDescriptor)methodit.next();
732             /* Need correct number of parameters */
733             if (min.numArgs()!=currmd.numParameters())
734                 continue;
735             for(int i=0;i<min.numArgs();i++) {
736                 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
737                     continue NextMethod;
738             }
739             /* Method okay so far */
740             if (bestmd==null)
741                 bestmd=currmd;
742             else {
743                 if (typeutil.isMoreSpecific(currmd,bestmd)) {
744                     bestmd=currmd;
745                 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
746                     throw new Error("No method is most specific");
747                 
748                 /* Is this more specific than bestmd */
749             }
750         }
751         if (bestmd==null)
752             throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
753         min.setMethod(bestmd);
754
755         if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td,  min.getType()))
756             throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
757         /* Check whether we need to set this parameter to implied this */
758         if (!bestmd.isStatic()) {
759             if (min.getExpression()==null) {
760                 ExpressionNode en=new NameNode(new NameDescriptor("this"));
761                 min.setExpression(en);
762                 checkExpressionNode(md, nametable, min.getExpression(), null);
763             }
764         }
765     }
766
767
768     void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
769         checkExpressionNode(md, nametable, on.getLeft(), null);
770         if (on.getRight()!=null)
771             checkExpressionNode(md, nametable, on.getRight(), null);
772         TypeDescriptor ltd=on.getLeft().getType();
773         TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
774         TypeDescriptor lefttype=null;
775         TypeDescriptor righttype=null;
776         Operation op=on.getOp();
777
778         switch(op.getOp()) {
779         case Operation.LOGIC_OR:
780         case Operation.LOGIC_AND:
781             if (!(rtd.isBoolean()))
782                 throw new Error();
783             on.setRightType(rtd);
784         case Operation.LOGIC_NOT:
785             if (!(ltd.isBoolean()))
786                 throw new Error();
787             //no promotion
788             on.setLeftType(ltd);
789
790             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
791             break;
792
793         case Operation.COMP:
794             // 5.6.2 Binary Numeric Promotion
795             //TODO unboxing of reference objects
796             if (ltd.isDouble())
797                 throw new Error();
798             else if (ltd.isFloat())
799                 throw new Error();
800             else if (ltd.isLong())
801                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
802             else 
803                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
804             on.setLeftType(lefttype);
805             on.setType(lefttype);
806             break;
807
808         case Operation.BIT_OR:
809         case Operation.BIT_XOR:
810         case Operation.BIT_AND:
811             // 5.6.2 Binary Numeric Promotion
812             //TODO unboxing of reference objects
813             if (ltd.isDouble()||rtd.isDouble())
814                 throw new Error();
815             else if (ltd.isFloat()||rtd.isFloat())
816                 throw new Error();
817             else if (ltd.isLong()||rtd.isLong())
818                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
819             else 
820                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
821             righttype=lefttype;
822
823             on.setLeftType(lefttype);
824             on.setRightType(righttype);
825             on.setType(lefttype);
826             break;
827
828         case Operation.ISAVAILABLE:
829             if (!(ltd.isPtr())) {
830                 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
831             }
832             lefttype=ltd;
833             on.setLeftType(lefttype);
834             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
835             break;
836         case Operation.EQUAL:
837         case Operation.NOTEQUAL:
838             // 5.6.2 Binary Numeric Promotion
839             //TODO unboxing of reference objects
840             if (ltd.isBoolean()||rtd.isBoolean()) {
841                 if (!(ltd.isBoolean()&&rtd.isBoolean()))
842                     throw new Error();
843                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
844             } else if (ltd.isPtr()||rtd.isPtr()) {
845                 if (!(ltd.isPtr()&&rtd.isPtr()))
846                     throw new Error();
847                 righttype=rtd;
848                 lefttype=ltd;
849             } else if (ltd.isDouble()||rtd.isDouble())
850                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
851             else if (ltd.isFloat()||rtd.isFloat())
852                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
853             else if (ltd.isLong()||rtd.isLong())
854                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
855             else 
856                 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
857
858             on.setLeftType(lefttype);
859             on.setRightType(righttype);
860             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
861             break;
862
863
864
865         case Operation.LT:
866         case Operation.GT:
867         case Operation.LTE:
868         case Operation.GTE:
869             // 5.6.2 Binary Numeric Promotion
870             //TODO unboxing of reference objects
871             if (!ltd.isNumber()||!rtd.isNumber())
872                 throw new Error();
873
874             if (ltd.isDouble()||rtd.isDouble())
875                 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
876             else if (ltd.isFloat()||rtd.isFloat())
877                 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
878             else if (ltd.isLong()||rtd.isLong())
879                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
880             else 
881                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
882             righttype=lefttype;
883             on.setLeftType(lefttype);
884             on.setRightType(righttype);
885             on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
886             break;
887
888         case Operation.ADD:
889             if (ltd.isString()||rtd.isString()) {
890                 ClassDescriptor stringcl=typeutil.getClass(TypeUtil.StringClass);
891                 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
892                 NameDescriptor nd=new NameDescriptor("String");
893                 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
894                 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
895                     MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
896                     leftmin.addArgument(on.getLeft());
897                     on.left=leftmin;
898                     checkExpressionNode(md, nametable, on.getLeft(), null);
899                 }
900
901                 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
902                     MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
903                     rightmin.addArgument(on.getRight());
904                     on.right=rightmin;
905                     checkExpressionNode(md, nametable, on.getRight(), null);
906                 }
907
908                 on.setLeftType(stringtd);
909                 on.setRightType(stringtd);
910                 on.setType(stringtd);
911                 break;
912             }
913             
914         case Operation.SUB:
915         case Operation.MULT:
916         case Operation.DIV:
917         case Operation.MOD:
918             // 5.6.2 Binary Numeric Promotion
919             //TODO unboxing of reference objects
920             if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
921                 throw new Error("Error in "+on.printNode(0));
922
923             if (ltd.isDouble()||rtd.isDouble())
924                 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
925             else if (ltd.isFloat()||rtd.isFloat())
926                 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
927             else if (ltd.isLong()||rtd.isLong())
928                 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
929             else 
930                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
931             righttype=lefttype;
932             on.setLeftType(lefttype);
933             on.setRightType(righttype);
934             on.setType(lefttype);
935             break;
936
937         case Operation.LEFTSHIFT:
938         case Operation.RIGHTSHIFT:
939         case Operation.URIGHTSHIFT:
940             if (!rtd.isIntegerType())
941                 throw new Error();
942             //5.6.1 Unary Numeric Promotion
943             if (rtd.isByte()||rtd.isShort()||rtd.isInt())
944                 righttype=new TypeDescriptor(TypeDescriptor.INT);
945             else
946                 righttype=rtd;
947
948             on.setRightType(righttype);
949             if (!ltd.isIntegerType())
950                 throw new Error();
951         case Operation.UNARYPLUS:
952         case Operation.UNARYMINUS:
953             /*  case Operation.POSTINC:
954                 case Operation.POSTDEC:
955                 case Operation.PREINC:
956                 case Operation.PREDEC:*/
957             if (!ltd.isNumber())
958                 throw new Error();
959             //5.6.1 Unary Numeric Promotion
960             if (ltd.isByte()||ltd.isShort()||ltd.isInt())
961                 lefttype=new TypeDescriptor(TypeDescriptor.INT);
962             else
963                 lefttype=ltd;
964             on.setLeftType(lefttype);
965             on.setType(lefttype);
966             break;
967         default:
968             throw new Error(op.toString());
969         }
970    
971         if (td!=null)
972             if (!typeutil.isSuperorType(td, on.getType())) {
973                 System.out.println(td);
974                 System.out.println(on.getType());
975                 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));     
976             }
977     }
978 }