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);
77 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
78 checkTypeDescriptor(fd.getType());
81 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
83 return; /* No constraint checks to check */
84 for(int i=0;i<ccs.size();i++) {
85 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
87 for(int j=0;j<cc.numArgs();j++) {
88 ExpressionNode en=cc.getArg(j);
89 checkExpressionNode(td,nametable,en,null);
94 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
96 return; /* No flag effects to check */
97 for(int i=0;i<vfe.size();i++) {
98 FlagEffects fe=(FlagEffects) vfe.get(i);
99 String varname=fe.getName();
100 //Make sure the variable is declared as a parameter to the task
101 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
103 throw new Error("Parameter "+varname+" in Flag Effects not declared");
106 //Make sure it correspods to a class
107 TypeDescriptor type_d=vd.getType();
108 if (!type_d.isClass())
109 throw new Error("Cannot have non-object argument for flag_effect");
111 ClassDescriptor cd=type_d.getClassDesc();
112 for(int j=0;j<fe.numEffects();j++) {
113 FlagEffect flag=fe.getEffect(j);
114 String name=flag.getName();
115 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
116 //Make sure the flag is declared
118 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
119 if (flag_d.getExternal())
120 throw new Error("Attempting to modify external flag: "+name);
121 flag.setFlag(flag_d);
123 for(int j=0;j<fe.numTagEffects();j++) {
124 TagEffect tag=fe.getTagEffect(j);
125 String name=tag.getName();
127 Descriptor d=(Descriptor)nametable.get(name);
129 throw new Error("Tag descriptor "+name+" undeclared");
130 else if (!(d instanceof TagVarDescriptor))
131 throw new Error(name+" is not a tag descriptor");
132 tag.setTag((TagVarDescriptor)d);
137 public void checkTask(TaskDescriptor td) {
138 for(int i=0;i<td.numParameters();i++) {
139 /* Check that parameter is well typed */
140 TypeDescriptor param_type=td.getParamType(i);
141 checkTypeDescriptor(param_type);
143 /* Check the parameter's flag expression is well formed */
144 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
145 if (!param_type.isClass())
146 throw new Error("Cannot have non-object argument to a task");
147 ClassDescriptor cd=param_type.getClassDesc();
148 checkFlagExpressionNode(cd, fen);
151 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
152 /* Check that the task code is valid */
153 BlockNode bn=state.getMethodBody(td);
154 checkBlockNode(td, td.getParameterTable(),bn);
157 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
159 case Kind.FlagOpNode:
161 FlagOpNode fon=(FlagOpNode)fen;
162 checkFlagExpressionNode(cd, fon.getLeft());
163 if (fon.getRight()!=null)
164 checkFlagExpressionNode(cd, fon.getRight());
169 FlagNode fn=(FlagNode)fen;
170 String name=fn.getFlagName();
171 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
173 throw new Error("Undeclared flag: "+name);
178 throw new Error("Unrecognized FlagExpressionNode");
182 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
183 /* Check return type */
184 if (!md.isConstructor())
185 if (!md.getReturnType().isVoid())
186 checkTypeDescriptor(md.getReturnType());
188 for(int i=0;i<md.numParameters();i++) {
189 TypeDescriptor param_type=md.getParamType(i);
190 checkTypeDescriptor(param_type);
192 /* Link the naming environments */
193 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
194 md.getParameterTable().setParent(cd.getFieldTable());
196 if (!md.isStatic()) {
197 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
202 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
203 //System.out.println("Processing method:"+md);
204 BlockNode bn=state.getMethodBody(md);
205 checkBlockNode(md, md.getParameterTable(),bn);
208 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
209 /* Link in the naming environment */
210 bn.getVarTable().setParent(nametable);
211 for(int i=0;i<bn.size();i++) {
212 BlockStatementNode bsn=bn.get(i);
213 checkBlockStatementNode(md, bn.getVarTable(),bsn);
217 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
219 case Kind.BlockExpressionNode:
220 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
223 case Kind.DeclarationNode:
224 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
227 case Kind.TagDeclarationNode:
228 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
231 case Kind.IfStatementNode:
232 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
236 checkLoopNode(md, nametable, (LoopNode)bsn);
239 case Kind.ReturnNode:
240 checkReturnNode(md, nametable, (ReturnNode)bsn);
243 case Kind.TaskExitNode:
244 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
247 case Kind.SubBlockNode:
248 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
254 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
255 checkExpressionNode(md, nametable, ben.getExpression(), null);
258 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
259 VarDescriptor vd=dn.getVarDescriptor();
260 checkTypeDescriptor(vd.getType());
261 Descriptor d=nametable.get(vd.getSymbol());
263 (d instanceof FieldDescriptor)) {
266 throw new Error(vd.getSymbol()+" defined a second time");
267 if (dn.getExpression()!=null)
268 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
271 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
272 TagVarDescriptor vd=dn.getTagVarDescriptor();
273 Descriptor d=nametable.get(vd.getSymbol());
275 (d instanceof FieldDescriptor)) {
278 throw new Error(vd.getSymbol()+" defined a second time");
281 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
282 checkBlockNode(md, nametable, sbn.getBlockNode());
285 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
286 if (d instanceof TaskDescriptor)
287 throw new Error("Illegal return appears in Task: "+d.getSymbol());
288 MethodDescriptor md=(MethodDescriptor)d;
289 if (rn.getReturnExpression()!=null)
290 if (md.getReturnType()==null)
291 throw new Error("Constructor can't return something.");
292 else if (md.getReturnType().isVoid())
293 throw new Error(md+" is void");
295 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
297 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
298 throw new Error("Need to return something for "+md);
301 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
302 if (md instanceof MethodDescriptor)
303 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
304 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
305 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
308 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
309 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
310 checkBlockNode(md, nametable, isn.getTrueBlock());
311 if (isn.getFalseBlock()!=null)
312 checkBlockNode(md, nametable, isn.getFalseBlock());
315 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
317 case Kind.AssignmentNode:
318 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
321 checkCastNode(md,nametable,(CastNode)en,td);
323 case Kind.CreateObjectNode:
324 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
326 case Kind.FieldAccessNode:
327 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
329 case Kind.ArrayAccessNode:
330 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
332 case Kind.LiteralNode:
333 checkLiteralNode(md,nametable,(LiteralNode)en,td);
335 case Kind.MethodInvokeNode:
336 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
339 checkNameNode(md,nametable,(NameNode)en,td);
342 checkOpNode(md,nametable,(OpNode)en,td);
348 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
349 /* Get type descriptor */
350 if (cn.getType()==null) {
351 NameDescriptor typenamed=cn.getTypeName().getName();
352 String typename=typenamed.toString();
353 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
357 /* Check the type descriptor */
358 TypeDescriptor cast_type=cn.getType();
359 checkTypeDescriptor(cast_type);
363 if (!typeutil.isSuperorType(td,cast_type))
364 throw new Error("Cast node returns "+cast_type+", but need "+td);
367 ExpressionNode en=cn.getExpression();
368 checkExpressionNode(md, nametable, en, null);
369 TypeDescriptor etd=en.getType();
370 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
373 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
375 if (typeutil.isCastable(etd, cast_type))
378 /* Different branches */
379 /* TODO: change if add interfaces */
380 throw new Error("Cast will always fail\n"+cn.printNode(0));
383 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
384 ExpressionNode left=fan.getExpression();
385 checkExpressionNode(md,nametable,left,null);
386 TypeDescriptor ltd=left.getType();
387 String fieldname=fan.getFieldName();
389 FieldDescriptor fd=null;
390 if (ltd.isArray()&&fieldname.equals("length"))
391 fd=FieldDescriptor.arrayLength;
393 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
395 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0));
398 if (!typeutil.isSuperorType(td,fan.getType()))
399 throw new Error("Field node returns "+fan.getType()+", but need "+td);
402 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
403 ExpressionNode left=aan.getExpression();
404 checkExpressionNode(md,nametable,left,null);
406 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
407 TypeDescriptor ltd=left.getType();
410 if (!typeutil.isSuperorType(td,aan.getType()))
411 throw new Error("Field node returns "+aan.getType()+", but need "+td);
414 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
415 /* Resolve the type */
416 Object o=ln.getValue();
417 if (ln.getTypeString().equals("null")) {
418 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
419 } else if (o instanceof Integer) {
420 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
421 } else if (o instanceof Long) {
422 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
423 } else if (o instanceof Float) {
424 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
425 } else if (o instanceof Boolean) {
426 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
427 } else if (o instanceof Double) {
428 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
429 } else if (o instanceof Character) {
430 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
431 } else if (o instanceof String) {
432 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
436 if (!typeutil.isSuperorType(td,ln.getType()))
437 throw new Error("Field node returns "+ln.getType()+", but need "+td);
440 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
441 NameDescriptor nd=nn.getName();
442 if (nd.getBase()!=null) {
444 /* Rewrite NameNode */
445 ExpressionNode en=translateNameDescriptorintoExpression(nd);
446 nn.setExpression(en);
447 checkExpressionNode(md,nametable,en,td);
449 String varname=nd.toString();
450 Descriptor d=(Descriptor)nametable.get(varname);
452 throw new Error("Name "+varname+" undefined");
454 if (d instanceof VarDescriptor) {
455 nn.setVar((VarDescriptor)d);
456 } else if (d instanceof FieldDescriptor) {
457 nn.setField((FieldDescriptor)d);
458 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
459 } else throw new Error("Wrong type of descriptor");
461 if (!typeutil.isSuperorType(td,nn.getType()))
462 throw new Error("Field node returns "+nn.getType()+", but need "+td);
466 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
467 boolean postinc=true;
468 if (an.getOperation().getBaseOp()==null||
469 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
470 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
474 checkExpressionNode(md, nametable, an.getSrc() ,td);
475 //TODO: Need check on validity of operation here
476 if (!((an.getDest() instanceof FieldAccessNode)||
477 (an.getDest() instanceof ArrayAccessNode)||
478 (an.getDest() instanceof NameNode)))
479 throw new Error("Bad lside in "+an.printNode(0));
480 checkExpressionNode(md, nametable, an.getDest(), null);
482 /* We want parameter variables to tasks to be immutable */
483 if (md instanceof TaskDescriptor) {
484 if (an.getDest() instanceof NameNode) {
485 NameNode nn=(NameNode)an.getDest();
486 if (nn.getVar()!=null) {
487 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
488 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
493 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
494 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
498 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
499 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
500 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
501 checkBlockNode(md, nametable, ln.getBody());
504 /* Link in the initializer naming environment */
505 BlockNode bn=ln.getInitializer();
506 bn.getVarTable().setParent(nametable);
507 for(int i=0;i<bn.size();i++) {
508 BlockStatementNode bsn=bn.get(i);
509 checkBlockStatementNode(md, bn.getVarTable(),bsn);
511 //check the condition
512 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
513 checkBlockNode(md, bn.getVarTable(), ln.getBody());
514 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
519 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
520 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
521 for(int i=0;i<con.numArgs();i++) {
522 ExpressionNode en=con.getArg(i);
523 checkExpressionNode(md,nametable,en,null);
524 tdarray[i]=en.getType();
527 TypeDescriptor typetolookin=con.getType();
528 checkTypeDescriptor(typetolookin);
530 /* Check flag effects */
531 if (con.getFlagEffects()!=null) {
532 FlagEffects fe=con.getFlagEffects();
533 ClassDescriptor cd=typetolookin.getClassDesc();
535 for(int j=0;j<fe.numEffects();j++) {
536 FlagEffect flag=fe.getEffect(j);
537 String name=flag.getName();
538 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
539 //Make sure the flag is declared
541 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
542 if (flag_d.getExternal())
543 throw new Error("Attempting to modify external flag: "+name);
544 flag.setFlag(flag_d);
546 for(int j=0;j<fe.numTagEffects();j++) {
547 TagEffect tag=fe.getTagEffect(j);
548 String name=tag.getName();
550 Descriptor d=(Descriptor)nametable.get(name);
552 throw new Error("Tag descriptor "+name+" undeclared");
553 else if (!(d instanceof TagVarDescriptor))
554 throw new Error(name+" is not a tag descriptor");
555 tag.setTag((TagVarDescriptor)d);
560 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
561 throw new Error("Can't allocate primitive type:"+con.printNode(0));
563 if (!typetolookin.isArray()) {
564 //Array's don't need constructor calls
565 ClassDescriptor classtolookin=typetolookin.getClassDesc();
566 //System.out.println("Looking for "+typetolookin.getSymbol());
567 //System.out.println(classtolookin.getMethodTable());
569 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
570 MethodDescriptor bestmd=null;
572 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
573 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
574 /* Need correct number of parameters */
575 //System.out.println("Examining: "+currmd);
576 if (con.numArgs()!=currmd.numParameters())
578 for(int i=0;i<con.numArgs();i++) {
579 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
582 /* Method okay so far */
586 if (isMoreSpecific(currmd,bestmd)) {
588 } else if (!isMoreSpecific(bestmd, currmd))
589 throw new Error("No method is most specific");
591 /* Is this more specific than bestmd */
595 throw new Error("No method found for "+con.printNode(0));
596 con.setConstructor(bestmd);
601 /** Check to see if md1 is more specific than md2... Informally
602 if md2 could always be called given the arguments passed into
605 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
606 /* Checks if md1 is more specific than md2 */
607 if (md1.numParameters()!=md2.numParameters())
609 for(int i=0;i<md1.numParameters();i++) {
610 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
613 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
616 if (!typeutil.isSuperorType(md2.getClassDesc(), md1.getClassDesc()))
622 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
623 String id=nd.getIdentifier();
624 NameDescriptor base=nd.getBase();
626 return new NameNode(nd);
628 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
632 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
633 /*Typecheck subexpressions
634 and get types for expressions*/
636 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
637 for(int i=0;i<min.numArgs();i++) {
638 ExpressionNode en=min.getArg(i);
639 checkExpressionNode(md,nametable,en,null);
640 tdarray[i]=en.getType();
642 TypeDescriptor typetolookin=null;
643 if (min.getExpression()!=null) {
644 checkExpressionNode(md,nametable,min.getExpression(),null);
645 typetolookin=min.getExpression().getType();
646 } else if (min.getBaseName()!=null) {
647 String rootname=min.getBaseName().getRoot();
648 if (nametable.get(rootname)!=null) {
649 //we have an expression
650 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
651 checkExpressionNode(md, nametable, min.getExpression(), null);
652 typetolookin=min.getExpression().getType();
655 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
657 throw new Error(min.getBaseName()+" undefined");
658 typetolookin=new TypeDescriptor(cd);
660 } else if (md instanceof MethodDescriptor) {
661 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
663 /* If this a task descriptor we throw an error at this point */
664 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
666 if (!typetolookin.isClass())
667 throw new Error("Error with method call to "+min.getMethodName());
668 ClassDescriptor classtolookin=typetolookin.getClassDesc();
669 //System.out.println("Method name="+min.getMethodName());
671 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
672 MethodDescriptor bestmd=null;
674 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
675 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
676 /* Need correct number of parameters */
677 if (min.numArgs()!=currmd.numParameters())
679 for(int i=0;i<min.numArgs();i++) {
680 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
683 /* Method okay so far */
687 if (isMoreSpecific(currmd,bestmd)) {
689 } else if (!isMoreSpecific(bestmd, currmd))
690 throw new Error("No method is most specific");
692 /* Is this more specific than bestmd */
696 throw new Error("No method found for :"+min.printNode(0));
697 min.setMethod(bestmd);
699 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
700 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
701 /* Check whether we need to set this parameter to implied this */
702 if (!bestmd.isStatic()) {
703 if (min.getExpression()==null) {
704 ExpressionNode en=new NameNode(new NameDescriptor("this"));
705 min.setExpression(en);
706 checkExpressionNode(md, nametable, min.getExpression(), null);
712 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
713 checkExpressionNode(md, nametable, on.getLeft(), null);
714 if (on.getRight()!=null)
715 checkExpressionNode(md, nametable, on.getRight(), null);
716 TypeDescriptor ltd=on.getLeft().getType();
717 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
718 TypeDescriptor lefttype=null;
719 TypeDescriptor righttype=null;
720 Operation op=on.getOp();
723 case Operation.LOGIC_OR:
724 case Operation.LOGIC_AND:
725 if (!(rtd.isBoolean()))
727 on.setRightType(rtd);
728 case Operation.LOGIC_NOT:
729 if (!(ltd.isBoolean()))
734 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
737 case Operation.BIT_OR:
738 case Operation.BIT_XOR:
739 case Operation.BIT_AND:
740 // 5.6.2 Binary Numeric Promotion
741 //TODO unboxing of reference objects
742 if (ltd.isDouble()||rtd.isDouble())
744 else if (ltd.isFloat()||rtd.isFloat())
746 else if (ltd.isLong()||rtd.isLong())
747 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
749 lefttype=new TypeDescriptor(TypeDescriptor.INT);
752 on.setLeftType(lefttype);
753 on.setRightType(righttype);
754 on.setType(lefttype);
757 case Operation.EQUAL:
758 case Operation.NOTEQUAL:
759 // 5.6.2 Binary Numeric Promotion
760 //TODO unboxing of reference objects
761 if (ltd.isBoolean()||rtd.isBoolean()) {
762 if (!(ltd.isBoolean()&&rtd.isBoolean()))
764 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
765 } else if (ltd.isPtr()||ltd.isArray()||rtd.isPtr()||rtd.isArray()) {
766 if (!((ltd.isPtr()||ltd.isArray())&&(rtd.isPtr()||rtd.isArray())))
770 } else if (ltd.isDouble()||rtd.isDouble())
771 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
772 else if (ltd.isFloat()||rtd.isFloat())
773 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
774 else if (ltd.isLong()||rtd.isLong())
775 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
777 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
779 on.setLeftType(lefttype);
780 on.setRightType(righttype);
781 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
790 // 5.6.2 Binary Numeric Promotion
791 //TODO unboxing of reference objects
792 if (!ltd.isNumber()||!rtd.isNumber())
795 if (ltd.isDouble()||rtd.isDouble())
796 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
797 else if (ltd.isFloat()||rtd.isFloat())
798 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
799 else if (ltd.isLong()||rtd.isLong())
800 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
802 lefttype=new TypeDescriptor(TypeDescriptor.INT);
804 on.setLeftType(lefttype);
805 on.setRightType(righttype);
806 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
810 //TODO: Need special case for strings eventually
817 // 5.6.2 Binary Numeric Promotion
818 //TODO unboxing of reference objects
819 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
820 throw new Error("Error in "+on.printNode(0));
822 if (ltd.isDouble()||rtd.isDouble())
823 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
824 else if (ltd.isFloat()||rtd.isFloat())
825 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
826 else if (ltd.isLong()||rtd.isLong())
827 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
829 lefttype=new TypeDescriptor(TypeDescriptor.INT);
831 on.setLeftType(lefttype);
832 on.setRightType(righttype);
833 on.setType(lefttype);
836 case Operation.LEFTSHIFT:
837 case Operation.RIGHTSHIFT:
838 if (!rtd.isIntegerType())
840 //5.6.1 Unary Numeric Promotion
841 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
842 righttype=new TypeDescriptor(TypeDescriptor.INT);
846 on.setRightType(righttype);
847 if (!ltd.isIntegerType())
849 case Operation.UNARYPLUS:
850 case Operation.UNARYMINUS:
851 /* case Operation.POSTINC:
852 case Operation.POSTDEC:
853 case Operation.PREINC:
854 case Operation.PREDEC:*/
857 //5.6.1 Unary Numeric Promotion
858 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
859 lefttype=new TypeDescriptor(TypeDescriptor.INT);
862 on.setLeftType(lefttype);
863 on.setType(lefttype);
866 throw new Error(op.toString());
870 if (!typeutil.isSuperorType(td, on.getType())) {
871 System.out.println(td);
872 System.out.println(on.getType());
873 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));