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();
150 checkFlagExpressionNode(cd, fen);
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);
159 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
161 case Kind.FlagOpNode:
163 FlagOpNode fon=(FlagOpNode)fen;
164 checkFlagExpressionNode(cd, fon.getLeft());
165 if (fon.getRight()!=null)
166 checkFlagExpressionNode(cd, fon.getRight());
171 FlagNode fn=(FlagNode)fen;
172 String name=fn.getFlagName();
173 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
175 throw new Error("Undeclared flag: "+name);
180 throw new Error("Unrecognized FlagExpressionNode");
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());
190 for(int i=0;i<md.numParameters();i++) {
191 TypeDescriptor param_type=md.getParamType(i);
192 checkTypeDescriptor(param_type);
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());
198 if (!md.isStatic()) {
199 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
204 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
205 //System.out.println("Processing method:"+md);
206 BlockNode bn=state.getMethodBody(md);
207 checkBlockNode(md, md.getParameterTable(),bn);
210 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
211 /* Link in the naming environment */
212 bn.getVarTable().setParent(nametable);
213 for(int i=0;i<bn.size();i++) {
214 BlockStatementNode bsn=bn.get(i);
215 checkBlockStatementNode(md, bn.getVarTable(),bsn);
219 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
221 case Kind.BlockExpressionNode:
222 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
225 case Kind.DeclarationNode:
226 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
229 case Kind.TagDeclarationNode:
230 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
233 case Kind.IfStatementNode:
234 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
238 checkLoopNode(md, nametable, (LoopNode)bsn);
241 case Kind.ReturnNode:
242 checkReturnNode(md, nametable, (ReturnNode)bsn);
245 case Kind.TaskExitNode:
246 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
249 case Kind.SubBlockNode:
250 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
256 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
257 checkExpressionNode(md, nametable, ben.getExpression(), null);
260 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
261 VarDescriptor vd=dn.getVarDescriptor();
262 checkTypeDescriptor(vd.getType());
263 Descriptor d=nametable.get(vd.getSymbol());
265 (d instanceof FieldDescriptor)) {
268 throw new Error(vd.getSymbol()+" defined a second time");
269 if (dn.getExpression()!=null)
270 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
273 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
274 TagVarDescriptor vd=dn.getTagVarDescriptor();
275 Descriptor d=nametable.get(vd.getSymbol());
277 (d instanceof FieldDescriptor)) {
280 throw new Error(vd.getSymbol()+" defined a second time");
283 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
284 checkBlockNode(md, nametable, sbn.getBlockNode());
287 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
288 if (d instanceof TaskDescriptor)
289 throw new Error("Illegal return appears in Task: "+d.getSymbol());
290 MethodDescriptor md=(MethodDescriptor)d;
291 if (rn.getReturnExpression()!=null)
292 if (md.getReturnType()==null)
293 throw new Error("Constructor can't return something.");
294 else if (md.getReturnType().isVoid())
295 throw new Error(md+" is void");
297 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
299 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
300 throw new Error("Need to return something for "+md);
303 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
304 if (md instanceof MethodDescriptor)
305 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
306 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
307 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
310 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
311 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
312 checkBlockNode(md, nametable, isn.getTrueBlock());
313 if (isn.getFalseBlock()!=null)
314 checkBlockNode(md, nametable, isn.getFalseBlock());
317 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
319 case Kind.AssignmentNode:
320 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
323 checkCastNode(md,nametable,(CastNode)en,td);
325 case Kind.CreateObjectNode:
326 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
328 case Kind.FieldAccessNode:
329 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
331 case Kind.ArrayAccessNode:
332 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
334 case Kind.LiteralNode:
335 checkLiteralNode(md,nametable,(LiteralNode)en,td);
337 case Kind.MethodInvokeNode:
338 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
341 checkNameNode(md,nametable,(NameNode)en,td);
344 checkOpNode(md,nametable,(OpNode)en,td);
350 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
351 /* Get type descriptor */
352 if (cn.getType()==null) {
353 NameDescriptor typenamed=cn.getTypeName().getName();
354 String typename=typenamed.toString();
355 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
359 /* Check the type descriptor */
360 TypeDescriptor cast_type=cn.getType();
361 checkTypeDescriptor(cast_type);
365 if (!typeutil.isSuperorType(td,cast_type))
366 throw new Error("Cast node returns "+cast_type+", but need "+td);
369 ExpressionNode en=cn.getExpression();
370 checkExpressionNode(md, nametable, en, null);
371 TypeDescriptor etd=en.getType();
372 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
375 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
377 if (typeutil.isCastable(etd, cast_type))
380 /* Different branches */
381 /* TODO: change if add interfaces */
382 throw new Error("Cast will always fail\n"+cn.printNode(0));
385 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
386 ExpressionNode left=fan.getExpression();
387 checkExpressionNode(md,nametable,left,null);
388 TypeDescriptor ltd=left.getType();
389 String fieldname=fan.getFieldName();
391 FieldDescriptor fd=null;
392 if (ltd.isArray()&&fieldname.equals("length"))
393 fd=FieldDescriptor.arrayLength;
395 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
397 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0));
400 if (!typeutil.isSuperorType(td,fan.getType()))
401 throw new Error("Field node returns "+fan.getType()+", but need "+td);
404 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
405 ExpressionNode left=aan.getExpression();
406 checkExpressionNode(md,nametable,left,null);
408 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
409 TypeDescriptor ltd=left.getType();
412 if (!typeutil.isSuperorType(td,aan.getType()))
413 throw new Error("Field node returns "+aan.getType()+", but need "+td);
416 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
417 /* Resolve the type */
418 Object o=ln.getValue();
419 if (ln.getTypeString().equals("null")) {
420 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
421 } else if (o instanceof Integer) {
422 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
423 } else if (o instanceof Long) {
424 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
425 } else if (o instanceof Float) {
426 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
427 } else if (o instanceof Boolean) {
428 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
429 } else if (o instanceof Double) {
430 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
431 } else if (o instanceof Character) {
432 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
433 } else if (o instanceof String) {
434 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
438 if (!typeutil.isSuperorType(td,ln.getType()))
439 throw new Error("Field node returns "+ln.getType()+", but need "+td);
442 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
443 NameDescriptor nd=nn.getName();
444 if (nd.getBase()!=null) {
446 /* Rewrite NameNode */
447 ExpressionNode en=translateNameDescriptorintoExpression(nd);
448 nn.setExpression(en);
449 checkExpressionNode(md,nametable,en,td);
451 String varname=nd.toString();
452 Descriptor d=(Descriptor)nametable.get(varname);
454 throw new Error("Name "+varname+" undefined");
456 if (d instanceof VarDescriptor) {
458 } else if (d instanceof FieldDescriptor) {
459 nn.setField((FieldDescriptor)d);
460 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
461 } else if (d instanceof TagVarDescriptor) {
463 } else throw new Error("Wrong type of descriptor");
465 if (!typeutil.isSuperorType(td,nn.getType()))
466 throw new Error("Field node returns "+nn.getType()+", but need "+td);
470 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
471 boolean postinc=true;
472 if (an.getOperation().getBaseOp()==null||
473 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
474 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
478 checkExpressionNode(md, nametable, an.getSrc() ,td);
479 //TODO: Need check on validity of operation here
480 if (!((an.getDest() instanceof FieldAccessNode)||
481 (an.getDest() instanceof ArrayAccessNode)||
482 (an.getDest() instanceof NameNode)))
483 throw new Error("Bad lside in "+an.printNode(0));
484 checkExpressionNode(md, nametable, an.getDest(), null);
486 /* We want parameter variables to tasks to be immutable */
487 if (md instanceof TaskDescriptor) {
488 if (an.getDest() instanceof NameNode) {
489 NameNode nn=(NameNode)an.getDest();
490 if (nn.getVar()!=null) {
491 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
492 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
497 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
498 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
502 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
503 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
504 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
505 checkBlockNode(md, nametable, ln.getBody());
508 /* Link in the initializer naming environment */
509 BlockNode bn=ln.getInitializer();
510 bn.getVarTable().setParent(nametable);
511 for(int i=0;i<bn.size();i++) {
512 BlockStatementNode bsn=bn.get(i);
513 checkBlockStatementNode(md, bn.getVarTable(),bsn);
515 //check the condition
516 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
517 checkBlockNode(md, bn.getVarTable(), ln.getBody());
518 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
523 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
524 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
525 for(int i=0;i<con.numArgs();i++) {
526 ExpressionNode en=con.getArg(i);
527 checkExpressionNode(md,nametable,en,null);
528 tdarray[i]=en.getType();
531 TypeDescriptor typetolookin=con.getType();
532 checkTypeDescriptor(typetolookin);
534 /* Check flag effects */
535 if (con.getFlagEffects()!=null) {
536 FlagEffects fe=con.getFlagEffects();
537 ClassDescriptor cd=typetolookin.getClassDesc();
539 for(int j=0;j<fe.numEffects();j++) {
540 FlagEffect flag=fe.getEffect(j);
541 String name=flag.getName();
542 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
543 //Make sure the flag is declared
545 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
546 if (flag_d.getExternal())
547 throw new Error("Attempting to modify external flag: "+name);
548 flag.setFlag(flag_d);
550 for(int j=0;j<fe.numTagEffects();j++) {
551 TagEffect tag=fe.getTagEffect(j);
552 String name=tag.getName();
554 Descriptor d=(Descriptor)nametable.get(name);
556 throw new Error("Tag descriptor "+name+" undeclared");
557 else if (!(d instanceof TagVarDescriptor))
558 throw new Error(name+" is not a tag descriptor");
559 tag.setTag((TagVarDescriptor)d);
564 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
565 throw new Error("Can't allocate primitive type:"+con.printNode(0));
567 if (!typetolookin.isArray()) {
568 //Array's don't need constructor calls
569 ClassDescriptor classtolookin=typetolookin.getClassDesc();
570 //System.out.println("Looking for "+typetolookin.getSymbol());
571 //System.out.println(classtolookin.getMethodTable());
573 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
574 MethodDescriptor bestmd=null;
576 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
577 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
578 /* Need correct number of parameters */
579 //System.out.println("Examining: "+currmd);
580 if (con.numArgs()!=currmd.numParameters())
582 for(int i=0;i<con.numArgs();i++) {
583 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
586 /* Method okay so far */
590 if (isMoreSpecific(currmd,bestmd)) {
592 } else if (!isMoreSpecific(bestmd, currmd))
593 throw new Error("No method is most specific");
595 /* Is this more specific than bestmd */
599 throw new Error("No method found for "+con.printNode(0));
600 con.setConstructor(bestmd);
605 /** Check to see if md1 is more specific than md2... Informally
606 if md2 could always be called given the arguments passed into
609 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
610 /* Checks if md1 is more specific than md2 */
611 if (md1.numParameters()!=md2.numParameters())
613 for(int i=0;i<md1.numParameters();i++) {
614 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
617 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
620 if (!typeutil.isSuperorType(md2.getClassDesc(), md1.getClassDesc()))
626 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
627 String id=nd.getIdentifier();
628 NameDescriptor base=nd.getBase();
630 return new NameNode(nd);
632 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
636 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
637 /*Typecheck subexpressions
638 and get types for expressions*/
640 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
641 for(int i=0;i<min.numArgs();i++) {
642 ExpressionNode en=min.getArg(i);
643 checkExpressionNode(md,nametable,en,null);
644 tdarray[i]=en.getType();
646 TypeDescriptor typetolookin=null;
647 if (min.getExpression()!=null) {
648 checkExpressionNode(md,nametable,min.getExpression(),null);
649 typetolookin=min.getExpression().getType();
650 } else if (min.getBaseName()!=null) {
651 String rootname=min.getBaseName().getRoot();
652 if (nametable.get(rootname)!=null) {
653 //we have an expression
654 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
655 checkExpressionNode(md, nametable, min.getExpression(), null);
656 typetolookin=min.getExpression().getType();
659 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
661 throw new Error(min.getBaseName()+" undefined");
662 typetolookin=new TypeDescriptor(cd);
664 } else if (md instanceof MethodDescriptor) {
665 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
667 /* If this a task descriptor we throw an error at this point */
668 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
670 if (!typetolookin.isClass())
671 throw new Error("Error with method call to "+min.getMethodName());
672 ClassDescriptor classtolookin=typetolookin.getClassDesc();
673 //System.out.println("Method name="+min.getMethodName());
675 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
676 MethodDescriptor bestmd=null;
678 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
679 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
680 /* Need correct number of parameters */
681 if (min.numArgs()!=currmd.numParameters())
683 for(int i=0;i<min.numArgs();i++) {
684 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
687 /* Method okay so far */
691 if (isMoreSpecific(currmd,bestmd)) {
693 } else if (!isMoreSpecific(bestmd, currmd))
694 throw new Error("No method is most specific");
696 /* Is this more specific than bestmd */
700 throw new Error("No method found for :"+min.printNode(0));
701 min.setMethod(bestmd);
703 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
704 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
705 /* Check whether we need to set this parameter to implied this */
706 if (!bestmd.isStatic()) {
707 if (min.getExpression()==null) {
708 ExpressionNode en=new NameNode(new NameDescriptor("this"));
709 min.setExpression(en);
710 checkExpressionNode(md, nametable, min.getExpression(), null);
716 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
717 checkExpressionNode(md, nametable, on.getLeft(), null);
718 if (on.getRight()!=null)
719 checkExpressionNode(md, nametable, on.getRight(), null);
720 TypeDescriptor ltd=on.getLeft().getType();
721 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
722 TypeDescriptor lefttype=null;
723 TypeDescriptor righttype=null;
724 Operation op=on.getOp();
727 case Operation.LOGIC_OR:
728 case Operation.LOGIC_AND:
729 if (!(rtd.isBoolean()))
731 on.setRightType(rtd);
732 case Operation.LOGIC_NOT:
733 if (!(ltd.isBoolean()))
738 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
741 case Operation.BIT_OR:
742 case Operation.BIT_XOR:
743 case Operation.BIT_AND:
744 // 5.6.2 Binary Numeric Promotion
745 //TODO unboxing of reference objects
746 if (ltd.isDouble()||rtd.isDouble())
748 else if (ltd.isFloat()||rtd.isFloat())
750 else if (ltd.isLong()||rtd.isLong())
751 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
753 lefttype=new TypeDescriptor(TypeDescriptor.INT);
756 on.setLeftType(lefttype);
757 on.setRightType(righttype);
758 on.setType(lefttype);
761 case Operation.EQUAL:
762 case Operation.NOTEQUAL:
763 // 5.6.2 Binary Numeric Promotion
764 //TODO unboxing of reference objects
765 if (ltd.isBoolean()||rtd.isBoolean()) {
766 if (!(ltd.isBoolean()&&rtd.isBoolean()))
768 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
769 } else if (ltd.isPtr()||ltd.isArray()||rtd.isPtr()||rtd.isArray()) {
770 if (!((ltd.isPtr()||ltd.isArray())&&(rtd.isPtr()||rtd.isArray())))
774 } else if (ltd.isDouble()||rtd.isDouble())
775 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
776 else if (ltd.isFloat()||rtd.isFloat())
777 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
778 else if (ltd.isLong()||rtd.isLong())
779 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
781 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
783 on.setLeftType(lefttype);
784 on.setRightType(righttype);
785 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
794 // 5.6.2 Binary Numeric Promotion
795 //TODO unboxing of reference objects
796 if (!ltd.isNumber()||!rtd.isNumber())
799 if (ltd.isDouble()||rtd.isDouble())
800 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
801 else if (ltd.isFloat()||rtd.isFloat())
802 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
803 else if (ltd.isLong()||rtd.isLong())
804 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
806 lefttype=new TypeDescriptor(TypeDescriptor.INT);
808 on.setLeftType(lefttype);
809 on.setRightType(righttype);
810 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
814 //TODO: Need special case for strings eventually
821 // 5.6.2 Binary Numeric Promotion
822 //TODO unboxing of reference objects
823 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
824 throw new Error("Error in "+on.printNode(0));
826 if (ltd.isDouble()||rtd.isDouble())
827 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
828 else if (ltd.isFloat()||rtd.isFloat())
829 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
830 else if (ltd.isLong()||rtd.isLong())
831 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
833 lefttype=new TypeDescriptor(TypeDescriptor.INT);
835 on.setLeftType(lefttype);
836 on.setRightType(righttype);
837 on.setType(lefttype);
840 case Operation.LEFTSHIFT:
841 case Operation.RIGHTSHIFT:
842 if (!rtd.isIntegerType())
844 //5.6.1 Unary Numeric Promotion
845 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
846 righttype=new TypeDescriptor(TypeDescriptor.INT);
850 on.setRightType(righttype);
851 if (!ltd.isIntegerType())
853 case Operation.UNARYPLUS:
854 case Operation.UNARYMINUS:
855 /* case Operation.POSTINC:
856 case Operation.POSTDEC:
857 case Operation.PREINC:
858 case Operation.PREDEC:*/
861 //5.6.1 Unary Numeric Promotion
862 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
863 lefttype=new TypeDescriptor(TypeDescriptor.INT);
866 on.setLeftType(lefttype);
867 on.setType(lefttype);
870 throw new Error(op.toString());
874 if (!typeutil.isSuperorType(td, on.getType())) {
875 System.out.println(td);
876 System.out.println(on.getType());
877 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));