6 public class SemanticCheck {
10 public SemanticCheck(State state, TypeUtil tu) {
15 public void semanticCheck() {
16 SymbolTable classtable=state.getClassSymbolTable();
17 Iterator it=classtable.getDescriptorsIterator();
18 // Do descriptors first
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());
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);
39 for(Iterator method_it=cd.getMethods();method_it.hasNext();) {
40 MethodDescriptor md=(MethodDescriptor)method_it.next();
45 it=classtable.getDescriptorsIterator();
46 // Do descriptors first
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);
55 for(Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator();task_it.hasNext();) {
56 TaskDescriptor td=(TaskDescriptor)task_it.next();
63 public void checkTypeDescriptor(TypeDescriptor td) {
66 else if (td.isClass()) {
67 String name=td.toString();
68 ClassDescriptor field_cd=(ClassDescriptor)state.getClassSymbolTable().get(name);
70 throw new Error("Undefined class "+name);
71 td.setClassDescriptor(field_cd);
73 } else if (td.isTag())
79 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
80 checkTypeDescriptor(fd.getType());
83 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
85 return; /* No constraint checks to check */
86 for(int i=0;i<ccs.size();i++) {
87 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
89 for(int j=0;j<cc.numArgs();j++) {
90 ExpressionNode en=cc.getArg(j);
91 checkExpressionNode(td,nametable,en,null);
96 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
98 return; /* No flag effects to check */
99 for(int i=0;i<vfe.size();i++) {
100 FlagEffects fe=(FlagEffects) vfe.get(i);
101 String varname=fe.getName();
102 //Make sure the variable is declared as a parameter to the task
103 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
105 throw new Error("Parameter "+varname+" in Flag Effects not declared");
108 //Make sure it correspods to a class
109 TypeDescriptor type_d=vd.getType();
110 if (!type_d.isClass())
111 throw new Error("Cannot have non-object argument for flag_effect");
113 ClassDescriptor cd=type_d.getClassDesc();
114 for(int j=0;j<fe.numEffects();j++) {
115 FlagEffect flag=fe.getEffect(j);
116 String name=flag.getName();
117 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
118 //Make sure the flag is declared
120 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
121 if (flag_d.getExternal())
122 throw new Error("Attempting to modify external flag: "+name);
123 flag.setFlag(flag_d);
125 for(int j=0;j<fe.numTagEffects();j++) {
126 TagEffect tag=fe.getTagEffect(j);
127 String name=tag.getName();
129 Descriptor d=(Descriptor)nametable.get(name);
131 throw new Error("Tag descriptor "+name+" undeclared");
132 else if (!(d instanceof TagVarDescriptor))
133 throw new Error(name+" is not a tag descriptor");
134 tag.setTag((TagVarDescriptor)d);
139 public void checkTask(TaskDescriptor td) {
140 for(int i=0;i<td.numParameters();i++) {
141 /* Check that parameter is well typed */
142 TypeDescriptor param_type=td.getParamType(i);
143 checkTypeDescriptor(param_type);
145 /* Check the parameter's flag expression is well formed */
146 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
147 if (!param_type.isClass())
148 throw new Error("Cannot have non-object argument to a task");
149 ClassDescriptor cd=param_type.getClassDesc();
151 checkFlagExpressionNode(cd, fen);
154 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
155 /* Check that the task code is valid */
156 BlockNode bn=state.getMethodBody(td);
157 checkBlockNode(td, td.getParameterTable(),bn);
160 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
162 case Kind.FlagOpNode:
164 FlagOpNode fon=(FlagOpNode)fen;
165 checkFlagExpressionNode(cd, fon.getLeft());
166 if (fon.getRight()!=null)
167 checkFlagExpressionNode(cd, fon.getRight());
172 FlagNode fn=(FlagNode)fen;
173 String name=fn.getFlagName();
174 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
176 throw new Error("Undeclared flag: "+name);
181 throw new Error("Unrecognized FlagExpressionNode");
185 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
186 /* Check return type */
187 if (!md.isConstructor())
188 if (!md.getReturnType().isVoid())
189 checkTypeDescriptor(md.getReturnType());
191 for(int i=0;i<md.numParameters();i++) {
192 TypeDescriptor param_type=md.getParamType(i);
193 checkTypeDescriptor(param_type);
195 /* Link the naming environments */
196 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
197 md.getParameterTable().setParent(cd.getFieldTable());
199 if (!md.isStatic()) {
200 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
205 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
206 //System.out.println("Processing method:"+md);
207 BlockNode bn=state.getMethodBody(md);
208 checkBlockNode(md, md.getParameterTable(),bn);
211 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
212 /* Link in the naming environment */
213 bn.getVarTable().setParent(nametable);
214 for(int i=0;i<bn.size();i++) {
215 BlockStatementNode bsn=bn.get(i);
216 checkBlockStatementNode(md, bn.getVarTable(),bsn);
220 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
222 case Kind.BlockExpressionNode:
223 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
226 case Kind.DeclarationNode:
227 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
230 case Kind.TagDeclarationNode:
231 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
234 case Kind.IfStatementNode:
235 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
239 checkLoopNode(md, nametable, (LoopNode)bsn);
242 case Kind.ReturnNode:
243 checkReturnNode(md, nametable, (ReturnNode)bsn);
246 case Kind.TaskExitNode:
247 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
250 case Kind.SubBlockNode:
251 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
254 case Kind.AtomicNode:
255 checkAtomicNode(md, nametable, (AtomicNode)bsn);
261 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
262 checkExpressionNode(md, nametable, ben.getExpression(), null);
265 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
266 VarDescriptor vd=dn.getVarDescriptor();
267 checkTypeDescriptor(vd.getType());
268 Descriptor d=nametable.get(vd.getSymbol());
270 (d instanceof FieldDescriptor)) {
273 throw new Error(vd.getSymbol()+" defined a second time");
274 if (dn.getExpression()!=null)
275 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
278 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
279 TagVarDescriptor vd=dn.getTagVarDescriptor();
280 Descriptor d=nametable.get(vd.getSymbol());
282 (d instanceof FieldDescriptor)) {
285 throw new Error(vd.getSymbol()+" defined a second time");
288 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
289 checkBlockNode(md, nametable, sbn.getBlockNode());
292 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
293 checkBlockNode(md, nametable, sbn.getBlockNode());
296 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
297 if (d instanceof TaskDescriptor)
298 throw new Error("Illegal return appears in Task: "+d.getSymbol());
299 MethodDescriptor md=(MethodDescriptor)d;
300 if (rn.getReturnExpression()!=null)
301 if (md.getReturnType()==null)
302 throw new Error("Constructor can't return something.");
303 else if (md.getReturnType().isVoid())
304 throw new Error(md+" is void");
306 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
308 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
309 throw new Error("Need to return something for "+md);
312 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
313 if (md instanceof MethodDescriptor)
314 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
315 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
316 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
319 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
320 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
321 checkBlockNode(md, nametable, isn.getTrueBlock());
322 if (isn.getFalseBlock()!=null)
323 checkBlockNode(md, nametable, isn.getFalseBlock());
326 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
328 case Kind.AssignmentNode:
329 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
332 checkCastNode(md,nametable,(CastNode)en,td);
334 case Kind.CreateObjectNode:
335 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
337 case Kind.FieldAccessNode:
338 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
340 case Kind.ArrayAccessNode:
341 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
343 case Kind.LiteralNode:
344 checkLiteralNode(md,nametable,(LiteralNode)en,td);
346 case Kind.MethodInvokeNode:
347 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
350 checkNameNode(md,nametable,(NameNode)en,td);
353 checkOpNode(md,nametable,(OpNode)en,td);
359 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
360 /* Get type descriptor */
361 if (cn.getType()==null) {
362 NameDescriptor typenamed=cn.getTypeName().getName();
363 String typename=typenamed.toString();
364 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
368 /* Check the type descriptor */
369 TypeDescriptor cast_type=cn.getType();
370 checkTypeDescriptor(cast_type);
374 if (!typeutil.isSuperorType(td,cast_type))
375 throw new Error("Cast node returns "+cast_type+", but need "+td);
378 ExpressionNode en=cn.getExpression();
379 checkExpressionNode(md, nametable, en, null);
380 TypeDescriptor etd=en.getType();
381 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
384 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
386 if (typeutil.isCastable(etd, cast_type))
389 /* Different branches */
390 /* TODO: change if add interfaces */
391 throw new Error("Cast will always fail\n"+cn.printNode(0));
394 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
395 ExpressionNode left=fan.getExpression();
396 checkExpressionNode(md,nametable,left,null);
397 TypeDescriptor ltd=left.getType();
398 String fieldname=fan.getFieldName();
400 FieldDescriptor fd=null;
401 if (ltd.isArray()&&fieldname.equals("length"))
402 fd=FieldDescriptor.arrayLength;
404 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
406 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0));
409 if (!typeutil.isSuperorType(td,fan.getType()))
410 throw new Error("Field node returns "+fan.getType()+", but need "+td);
413 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
414 ExpressionNode left=aan.getExpression();
415 checkExpressionNode(md,nametable,left,null);
417 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
418 TypeDescriptor ltd=left.getType();
421 if (!typeutil.isSuperorType(td,aan.getType()))
422 throw new Error("Field node returns "+aan.getType()+", but need "+td);
425 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
426 /* Resolve the type */
427 Object o=ln.getValue();
428 if (ln.getTypeString().equals("null")) {
429 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
430 } else if (o instanceof Integer) {
431 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
432 } else if (o instanceof Long) {
433 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
434 } else if (o instanceof Float) {
435 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
436 } else if (o instanceof Boolean) {
437 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
438 } else if (o instanceof Double) {
439 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
440 } else if (o instanceof Character) {
441 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
442 } else if (o instanceof String) {
443 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
447 if (!typeutil.isSuperorType(td,ln.getType()))
448 throw new Error("Field node returns "+ln.getType()+", but need "+td);
451 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
452 NameDescriptor nd=nn.getName();
453 if (nd.getBase()!=null) {
455 /* Rewrite NameNode */
456 ExpressionNode en=translateNameDescriptorintoExpression(nd);
457 nn.setExpression(en);
458 checkExpressionNode(md,nametable,en,td);
460 String varname=nd.toString();
461 Descriptor d=(Descriptor)nametable.get(varname);
463 throw new Error("Name "+varname+" undefined");
465 if (d instanceof VarDescriptor) {
467 } else if (d instanceof FieldDescriptor) {
468 nn.setField((FieldDescriptor)d);
469 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
470 } else if (d instanceof TagVarDescriptor) {
472 } else throw new Error("Wrong type of descriptor");
474 if (!typeutil.isSuperorType(td,nn.getType()))
475 throw new Error("Field node returns "+nn.getType()+", but need "+td);
479 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
480 boolean postinc=true;
481 if (an.getOperation().getBaseOp()==null||
482 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
483 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
487 checkExpressionNode(md, nametable, an.getSrc() ,td);
488 //TODO: Need check on validity of operation here
489 if (!((an.getDest() instanceof FieldAccessNode)||
490 (an.getDest() instanceof ArrayAccessNode)||
491 (an.getDest() instanceof NameNode)))
492 throw new Error("Bad lside in "+an.printNode(0));
493 checkExpressionNode(md, nametable, an.getDest(), null);
495 /* We want parameter variables to tasks to be immutable */
496 if (md instanceof TaskDescriptor) {
497 if (an.getDest() instanceof NameNode) {
498 NameNode nn=(NameNode)an.getDest();
499 if (nn.getVar()!=null) {
500 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
501 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
506 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
507 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
511 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
512 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
513 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
514 checkBlockNode(md, nametable, ln.getBody());
517 /* Link in the initializer naming environment */
518 BlockNode bn=ln.getInitializer();
519 bn.getVarTable().setParent(nametable);
520 for(int i=0;i<bn.size();i++) {
521 BlockStatementNode bsn=bn.get(i);
522 checkBlockStatementNode(md, bn.getVarTable(),bsn);
524 //check the condition
525 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
526 checkBlockNode(md, bn.getVarTable(), ln.getBody());
527 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
532 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
533 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
534 for(int i=0;i<con.numArgs();i++) {
535 ExpressionNode en=con.getArg(i);
536 checkExpressionNode(md,nametable,en,null);
537 tdarray[i]=en.getType();
540 TypeDescriptor typetolookin=con.getType();
541 checkTypeDescriptor(typetolookin);
543 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
544 throw new Error(typetolookin + " isn't a "+td);
548 /* Check flag effects */
549 if (con.getFlagEffects()!=null) {
550 FlagEffects fe=con.getFlagEffects();
551 ClassDescriptor cd=typetolookin.getClassDesc();
553 for(int j=0;j<fe.numEffects();j++) {
554 FlagEffect flag=fe.getEffect(j);
555 String name=flag.getName();
556 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
557 //Make sure the flag is declared
559 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
560 if (flag_d.getExternal())
561 throw new Error("Attempting to modify external flag: "+name);
562 flag.setFlag(flag_d);
564 for(int j=0;j<fe.numTagEffects();j++) {
565 TagEffect tag=fe.getTagEffect(j);
566 String name=tag.getName();
568 Descriptor d=(Descriptor)nametable.get(name);
570 throw new Error("Tag descriptor "+name+" undeclared");
571 else if (!(d instanceof TagVarDescriptor))
572 throw new Error(name+" is not a tag descriptor");
573 tag.setTag((TagVarDescriptor)d);
578 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
579 throw new Error("Can't allocate primitive type:"+con.printNode(0));
581 if (!typetolookin.isArray()) {
582 //Array's don't need constructor calls
583 ClassDescriptor classtolookin=typetolookin.getClassDesc();
584 //System.out.println("Looking for "+typetolookin.getSymbol());
585 //System.out.println(classtolookin.getMethodTable());
587 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
588 MethodDescriptor bestmd=null;
590 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
591 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
592 /* Need correct number of parameters */
593 //System.out.println("Examining: "+currmd);
594 if (con.numArgs()!=currmd.numParameters())
596 for(int i=0;i<con.numArgs();i++) {
597 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
600 /* Method okay so far */
604 if (isMoreSpecific(currmd,bestmd)) {
606 } else if (!isMoreSpecific(bestmd, currmd))
607 throw new Error("No method is most specific");
609 /* Is this more specific than bestmd */
613 throw new Error("No method found for "+con.printNode(0));
614 con.setConstructor(bestmd);
619 /** Check to see if md1 is more specific than md2... Informally
620 if md2 could always be called given the arguments passed into
623 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
624 /* Checks if md1 is more specific than md2 */
625 if (md1.numParameters()!=md2.numParameters())
627 for(int i=0;i<md1.numParameters();i++) {
628 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
631 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
634 if (!typeutil.isSuperorType(md2.getClassDesc(), md1.getClassDesc()))
640 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
641 String id=nd.getIdentifier();
642 NameDescriptor base=nd.getBase();
644 return new NameNode(nd);
646 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
650 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
651 /*Typecheck subexpressions
652 and get types for expressions*/
654 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
655 for(int i=0;i<min.numArgs();i++) {
656 ExpressionNode en=min.getArg(i);
657 checkExpressionNode(md,nametable,en,null);
658 tdarray[i]=en.getType();
660 TypeDescriptor typetolookin=null;
661 if (min.getExpression()!=null) {
662 checkExpressionNode(md,nametable,min.getExpression(),null);
663 typetolookin=min.getExpression().getType();
664 } else if (min.getBaseName()!=null) {
665 String rootname=min.getBaseName().getRoot();
666 if (nametable.get(rootname)!=null) {
667 //we have an expression
668 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
669 checkExpressionNode(md, nametable, min.getExpression(), null);
670 typetolookin=min.getExpression().getType();
673 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
675 throw new Error(min.getBaseName()+" undefined");
676 typetolookin=new TypeDescriptor(cd);
678 } else if (md instanceof MethodDescriptor) {
679 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
681 /* If this a task descriptor we throw an error at this point */
682 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
684 if (!typetolookin.isClass())
685 throw new Error("Error with method call to "+min.getMethodName());
686 ClassDescriptor classtolookin=typetolookin.getClassDesc();
687 //System.out.println("Method name="+min.getMethodName());
689 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
690 MethodDescriptor bestmd=null;
692 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
693 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
694 /* Need correct number of parameters */
695 if (min.numArgs()!=currmd.numParameters())
697 for(int i=0;i<min.numArgs();i++) {
698 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
701 /* Method okay so far */
705 if (isMoreSpecific(currmd,bestmd)) {
707 } else if (!isMoreSpecific(bestmd, currmd))
708 throw new Error("No method is most specific");
710 /* Is this more specific than bestmd */
714 throw new Error("No method found for :"+min.printNode(0));
715 min.setMethod(bestmd);
717 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
718 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
719 /* Check whether we need to set this parameter to implied this */
720 if (!bestmd.isStatic()) {
721 if (min.getExpression()==null) {
722 ExpressionNode en=new NameNode(new NameDescriptor("this"));
723 min.setExpression(en);
724 checkExpressionNode(md, nametable, min.getExpression(), null);
730 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
731 checkExpressionNode(md, nametable, on.getLeft(), null);
732 if (on.getRight()!=null)
733 checkExpressionNode(md, nametable, on.getRight(), null);
734 TypeDescriptor ltd=on.getLeft().getType();
735 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
736 TypeDescriptor lefttype=null;
737 TypeDescriptor righttype=null;
738 Operation op=on.getOp();
741 case Operation.LOGIC_OR:
742 case Operation.LOGIC_AND:
743 if (!(rtd.isBoolean()))
745 on.setRightType(rtd);
746 case Operation.LOGIC_NOT:
747 if (!(ltd.isBoolean()))
752 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
755 case Operation.BIT_OR:
756 case Operation.BIT_XOR:
757 case Operation.BIT_AND:
758 // 5.6.2 Binary Numeric Promotion
759 //TODO unboxing of reference objects
760 if (ltd.isDouble()||rtd.isDouble())
762 else if (ltd.isFloat()||rtd.isFloat())
764 else if (ltd.isLong()||rtd.isLong())
765 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
767 lefttype=new TypeDescriptor(TypeDescriptor.INT);
770 on.setLeftType(lefttype);
771 on.setRightType(righttype);
772 on.setType(lefttype);
775 case Operation.EQUAL:
776 case Operation.NOTEQUAL:
777 // 5.6.2 Binary Numeric Promotion
778 //TODO unboxing of reference objects
779 if (ltd.isBoolean()||rtd.isBoolean()) {
780 if (!(ltd.isBoolean()&&rtd.isBoolean()))
782 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
783 } else if (ltd.isPtr()||ltd.isArray()||rtd.isPtr()||rtd.isArray()) {
784 if (!((ltd.isPtr()||ltd.isArray())&&(rtd.isPtr()||rtd.isArray())))
788 } else if (ltd.isDouble()||rtd.isDouble())
789 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
790 else if (ltd.isFloat()||rtd.isFloat())
791 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
792 else if (ltd.isLong()||rtd.isLong())
793 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
795 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
797 on.setLeftType(lefttype);
798 on.setRightType(righttype);
799 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
808 // 5.6.2 Binary Numeric Promotion
809 //TODO unboxing of reference objects
810 if (!ltd.isNumber()||!rtd.isNumber())
813 if (ltd.isDouble()||rtd.isDouble())
814 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
815 else if (ltd.isFloat()||rtd.isFloat())
816 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
817 else if (ltd.isLong()||rtd.isLong())
818 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
820 lefttype=new TypeDescriptor(TypeDescriptor.INT);
822 on.setLeftType(lefttype);
823 on.setRightType(righttype);
824 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
828 //TODO: Need special case for strings eventually
835 // 5.6.2 Binary Numeric Promotion
836 //TODO unboxing of reference objects
837 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
838 throw new Error("Error in "+on.printNode(0));
840 if (ltd.isDouble()||rtd.isDouble())
841 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
842 else if (ltd.isFloat()||rtd.isFloat())
843 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
844 else if (ltd.isLong()||rtd.isLong())
845 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
847 lefttype=new TypeDescriptor(TypeDescriptor.INT);
849 on.setLeftType(lefttype);
850 on.setRightType(righttype);
851 on.setType(lefttype);
854 case Operation.LEFTSHIFT:
855 case Operation.RIGHTSHIFT:
856 if (!rtd.isIntegerType())
858 //5.6.1 Unary Numeric Promotion
859 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
860 righttype=new TypeDescriptor(TypeDescriptor.INT);
864 on.setRightType(righttype);
865 if (!ltd.isIntegerType())
867 case Operation.UNARYPLUS:
868 case Operation.UNARYMINUS:
869 /* case Operation.POSTINC:
870 case Operation.POSTDEC:
871 case Operation.PREINC:
872 case Operation.PREDEC:*/
875 //5.6.1 Unary Numeric Promotion
876 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
877 lefttype=new TypeDescriptor(TypeDescriptor.INT);
880 on.setLeftType(lefttype);
881 on.setType(lefttype);
884 throw new Error(op.toString());
888 if (!typeutil.isSuperorType(td, on.getType())) {
889 System.out.println(td);
890 System.out.println(on.getType());
891 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));