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);
257 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
258 checkExpressionNode(md, nametable, ben.getExpression(), null);
261 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
262 VarDescriptor vd=dn.getVarDescriptor();
263 checkTypeDescriptor(vd.getType());
264 Descriptor d=nametable.get(vd.getSymbol());
266 (d instanceof FieldDescriptor)) {
269 throw new Error(vd.getSymbol()+" defined a second time");
270 if (dn.getExpression()!=null)
271 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
274 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
275 TagVarDescriptor vd=dn.getTagVarDescriptor();
276 Descriptor d=nametable.get(vd.getSymbol());
278 (d instanceof FieldDescriptor)) {
281 throw new Error(vd.getSymbol()+" defined a second time");
284 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
285 checkBlockNode(md, nametable, sbn.getBlockNode());
288 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
289 if (d instanceof TaskDescriptor)
290 throw new Error("Illegal return appears in Task: "+d.getSymbol());
291 MethodDescriptor md=(MethodDescriptor)d;
292 if (rn.getReturnExpression()!=null)
293 if (md.getReturnType()==null)
294 throw new Error("Constructor can't return something.");
295 else if (md.getReturnType().isVoid())
296 throw new Error(md+" is void");
298 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
300 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
301 throw new Error("Need to return something for "+md);
304 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
305 if (md instanceof MethodDescriptor)
306 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
307 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
308 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
311 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
312 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
313 checkBlockNode(md, nametable, isn.getTrueBlock());
314 if (isn.getFalseBlock()!=null)
315 checkBlockNode(md, nametable, isn.getFalseBlock());
318 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
320 case Kind.AssignmentNode:
321 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
324 checkCastNode(md,nametable,(CastNode)en,td);
326 case Kind.CreateObjectNode:
327 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
329 case Kind.FieldAccessNode:
330 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
332 case Kind.ArrayAccessNode:
333 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
335 case Kind.LiteralNode:
336 checkLiteralNode(md,nametable,(LiteralNode)en,td);
338 case Kind.MethodInvokeNode:
339 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
342 checkNameNode(md,nametable,(NameNode)en,td);
345 checkOpNode(md,nametable,(OpNode)en,td);
351 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
352 /* Get type descriptor */
353 if (cn.getType()==null) {
354 NameDescriptor typenamed=cn.getTypeName().getName();
355 String typename=typenamed.toString();
356 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
360 /* Check the type descriptor */
361 TypeDescriptor cast_type=cn.getType();
362 checkTypeDescriptor(cast_type);
366 if (!typeutil.isSuperorType(td,cast_type))
367 throw new Error("Cast node returns "+cast_type+", but need "+td);
370 ExpressionNode en=cn.getExpression();
371 checkExpressionNode(md, nametable, en, null);
372 TypeDescriptor etd=en.getType();
373 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
376 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
378 if (typeutil.isCastable(etd, cast_type))
381 /* Different branches */
382 /* TODO: change if add interfaces */
383 throw new Error("Cast will always fail\n"+cn.printNode(0));
386 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
387 ExpressionNode left=fan.getExpression();
388 checkExpressionNode(md,nametable,left,null);
389 TypeDescriptor ltd=left.getType();
390 String fieldname=fan.getFieldName();
392 FieldDescriptor fd=null;
393 if (ltd.isArray()&&fieldname.equals("length"))
394 fd=FieldDescriptor.arrayLength;
396 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
398 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0));
401 if (!typeutil.isSuperorType(td,fan.getType()))
402 throw new Error("Field node returns "+fan.getType()+", but need "+td);
405 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
406 ExpressionNode left=aan.getExpression();
407 checkExpressionNode(md,nametable,left,null);
409 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
410 TypeDescriptor ltd=left.getType();
413 if (!typeutil.isSuperorType(td,aan.getType()))
414 throw new Error("Field node returns "+aan.getType()+", but need "+td);
417 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
418 /* Resolve the type */
419 Object o=ln.getValue();
420 if (ln.getTypeString().equals("null")) {
421 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
422 } else if (o instanceof Integer) {
423 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
424 } else if (o instanceof Long) {
425 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
426 } else if (o instanceof Float) {
427 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
428 } else if (o instanceof Boolean) {
429 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
430 } else if (o instanceof Double) {
431 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
432 } else if (o instanceof Character) {
433 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
434 } else if (o instanceof String) {
435 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
439 if (!typeutil.isSuperorType(td,ln.getType()))
440 throw new Error("Field node returns "+ln.getType()+", but need "+td);
443 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
444 NameDescriptor nd=nn.getName();
445 if (nd.getBase()!=null) {
447 /* Rewrite NameNode */
448 ExpressionNode en=translateNameDescriptorintoExpression(nd);
449 nn.setExpression(en);
450 checkExpressionNode(md,nametable,en,td);
452 String varname=nd.toString();
453 Descriptor d=(Descriptor)nametable.get(varname);
455 throw new Error("Name "+varname+" undefined");
457 if (d instanceof VarDescriptor) {
459 } else if (d instanceof FieldDescriptor) {
460 nn.setField((FieldDescriptor)d);
461 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
462 } else if (d instanceof TagVarDescriptor) {
464 } else throw new Error("Wrong type of descriptor");
466 if (!typeutil.isSuperorType(td,nn.getType()))
467 throw new Error("Field node returns "+nn.getType()+", but need "+td);
471 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
472 boolean postinc=true;
473 if (an.getOperation().getBaseOp()==null||
474 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
475 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
479 checkExpressionNode(md, nametable, an.getSrc() ,td);
480 //TODO: Need check on validity of operation here
481 if (!((an.getDest() instanceof FieldAccessNode)||
482 (an.getDest() instanceof ArrayAccessNode)||
483 (an.getDest() instanceof NameNode)))
484 throw new Error("Bad lside in "+an.printNode(0));
485 checkExpressionNode(md, nametable, an.getDest(), null);
487 /* We want parameter variables to tasks to be immutable */
488 if (md instanceof TaskDescriptor) {
489 if (an.getDest() instanceof NameNode) {
490 NameNode nn=(NameNode)an.getDest();
491 if (nn.getVar()!=null) {
492 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
493 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
498 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
499 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
503 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
504 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
505 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
506 checkBlockNode(md, nametable, ln.getBody());
509 /* Link in the initializer naming environment */
510 BlockNode bn=ln.getInitializer();
511 bn.getVarTable().setParent(nametable);
512 for(int i=0;i<bn.size();i++) {
513 BlockStatementNode bsn=bn.get(i);
514 checkBlockStatementNode(md, bn.getVarTable(),bsn);
516 //check the condition
517 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
518 checkBlockNode(md, bn.getVarTable(), ln.getBody());
519 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
524 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
525 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
526 for(int i=0;i<con.numArgs();i++) {
527 ExpressionNode en=con.getArg(i);
528 checkExpressionNode(md,nametable,en,null);
529 tdarray[i]=en.getType();
532 TypeDescriptor typetolookin=con.getType();
533 checkTypeDescriptor(typetolookin);
535 /* Check flag effects */
536 if (con.getFlagEffects()!=null) {
537 FlagEffects fe=con.getFlagEffects();
538 ClassDescriptor cd=typetolookin.getClassDesc();
540 for(int j=0;j<fe.numEffects();j++) {
541 FlagEffect flag=fe.getEffect(j);
542 String name=flag.getName();
543 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
544 //Make sure the flag is declared
546 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
547 if (flag_d.getExternal())
548 throw new Error("Attempting to modify external flag: "+name);
549 flag.setFlag(flag_d);
551 for(int j=0;j<fe.numTagEffects();j++) {
552 TagEffect tag=fe.getTagEffect(j);
553 String name=tag.getName();
555 Descriptor d=(Descriptor)nametable.get(name);
557 throw new Error("Tag descriptor "+name+" undeclared");
558 else if (!(d instanceof TagVarDescriptor))
559 throw new Error(name+" is not a tag descriptor");
560 tag.setTag((TagVarDescriptor)d);
565 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
566 throw new Error("Can't allocate primitive type:"+con.printNode(0));
568 if (!typetolookin.isArray()) {
569 //Array's don't need constructor calls
570 ClassDescriptor classtolookin=typetolookin.getClassDesc();
571 //System.out.println("Looking for "+typetolookin.getSymbol());
572 //System.out.println(classtolookin.getMethodTable());
574 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
575 MethodDescriptor bestmd=null;
577 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
578 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
579 /* Need correct number of parameters */
580 //System.out.println("Examining: "+currmd);
581 if (con.numArgs()!=currmd.numParameters())
583 for(int i=0;i<con.numArgs();i++) {
584 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
587 /* Method okay so far */
591 if (isMoreSpecific(currmd,bestmd)) {
593 } else if (!isMoreSpecific(bestmd, currmd))
594 throw new Error("No method is most specific");
596 /* Is this more specific than bestmd */
600 throw new Error("No method found for "+con.printNode(0));
601 con.setConstructor(bestmd);
606 /** Check to see if md1 is more specific than md2... Informally
607 if md2 could always be called given the arguments passed into
610 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
611 /* Checks if md1 is more specific than md2 */
612 if (md1.numParameters()!=md2.numParameters())
614 for(int i=0;i<md1.numParameters();i++) {
615 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
618 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
621 if (!typeutil.isSuperorType(md2.getClassDesc(), md1.getClassDesc()))
627 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
628 String id=nd.getIdentifier();
629 NameDescriptor base=nd.getBase();
631 return new NameNode(nd);
633 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
637 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
638 /*Typecheck subexpressions
639 and get types for expressions*/
641 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
642 for(int i=0;i<min.numArgs();i++) {
643 ExpressionNode en=min.getArg(i);
644 checkExpressionNode(md,nametable,en,null);
645 tdarray[i]=en.getType();
647 TypeDescriptor typetolookin=null;
648 if (min.getExpression()!=null) {
649 checkExpressionNode(md,nametable,min.getExpression(),null);
650 typetolookin=min.getExpression().getType();
651 } else if (min.getBaseName()!=null) {
652 String rootname=min.getBaseName().getRoot();
653 if (nametable.get(rootname)!=null) {
654 //we have an expression
655 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
656 checkExpressionNode(md, nametable, min.getExpression(), null);
657 typetolookin=min.getExpression().getType();
660 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
662 throw new Error(min.getBaseName()+" undefined");
663 typetolookin=new TypeDescriptor(cd);
665 } else if (md instanceof MethodDescriptor) {
666 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
668 /* If this a task descriptor we throw an error at this point */
669 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
671 if (!typetolookin.isClass())
672 throw new Error("Error with method call to "+min.getMethodName());
673 ClassDescriptor classtolookin=typetolookin.getClassDesc();
674 //System.out.println("Method name="+min.getMethodName());
676 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
677 MethodDescriptor bestmd=null;
679 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
680 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
681 /* Need correct number of parameters */
682 if (min.numArgs()!=currmd.numParameters())
684 for(int i=0;i<min.numArgs();i++) {
685 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
688 /* Method okay so far */
692 if (isMoreSpecific(currmd,bestmd)) {
694 } else if (!isMoreSpecific(bestmd, currmd))
695 throw new Error("No method is most specific");
697 /* Is this more specific than bestmd */
701 throw new Error("No method found for :"+min.printNode(0));
702 min.setMethod(bestmd);
704 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
705 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
706 /* Check whether we need to set this parameter to implied this */
707 if (!bestmd.isStatic()) {
708 if (min.getExpression()==null) {
709 ExpressionNode en=new NameNode(new NameDescriptor("this"));
710 min.setExpression(en);
711 checkExpressionNode(md, nametable, min.getExpression(), null);
717 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
718 checkExpressionNode(md, nametable, on.getLeft(), null);
719 if (on.getRight()!=null)
720 checkExpressionNode(md, nametable, on.getRight(), null);
721 TypeDescriptor ltd=on.getLeft().getType();
722 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
723 TypeDescriptor lefttype=null;
724 TypeDescriptor righttype=null;
725 Operation op=on.getOp();
728 case Operation.LOGIC_OR:
729 case Operation.LOGIC_AND:
730 if (!(rtd.isBoolean()))
732 on.setRightType(rtd);
733 case Operation.LOGIC_NOT:
734 if (!(ltd.isBoolean()))
739 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
742 case Operation.BIT_OR:
743 case Operation.BIT_XOR:
744 case Operation.BIT_AND:
745 // 5.6.2 Binary Numeric Promotion
746 //TODO unboxing of reference objects
747 if (ltd.isDouble()||rtd.isDouble())
749 else if (ltd.isFloat()||rtd.isFloat())
751 else if (ltd.isLong()||rtd.isLong())
752 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
754 lefttype=new TypeDescriptor(TypeDescriptor.INT);
757 on.setLeftType(lefttype);
758 on.setRightType(righttype);
759 on.setType(lefttype);
762 case Operation.EQUAL:
763 case Operation.NOTEQUAL:
764 // 5.6.2 Binary Numeric Promotion
765 //TODO unboxing of reference objects
766 if (ltd.isBoolean()||rtd.isBoolean()) {
767 if (!(ltd.isBoolean()&&rtd.isBoolean()))
769 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
770 } else if (ltd.isPtr()||ltd.isArray()||rtd.isPtr()||rtd.isArray()) {
771 if (!((ltd.isPtr()||ltd.isArray())&&(rtd.isPtr()||rtd.isArray())))
775 } else if (ltd.isDouble()||rtd.isDouble())
776 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
777 else if (ltd.isFloat()||rtd.isFloat())
778 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
779 else if (ltd.isLong()||rtd.isLong())
780 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
782 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
784 on.setLeftType(lefttype);
785 on.setRightType(righttype);
786 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
795 // 5.6.2 Binary Numeric Promotion
796 //TODO unboxing of reference objects
797 if (!ltd.isNumber()||!rtd.isNumber())
800 if (ltd.isDouble()||rtd.isDouble())
801 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
802 else if (ltd.isFloat()||rtd.isFloat())
803 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
804 else if (ltd.isLong()||rtd.isLong())
805 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
807 lefttype=new TypeDescriptor(TypeDescriptor.INT);
809 on.setLeftType(lefttype);
810 on.setRightType(righttype);
811 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
815 //TODO: Need special case for strings eventually
822 // 5.6.2 Binary Numeric Promotion
823 //TODO unboxing of reference objects
824 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
825 throw new Error("Error in "+on.printNode(0));
827 if (ltd.isDouble()||rtd.isDouble())
828 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
829 else if (ltd.isFloat()||rtd.isFloat())
830 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
831 else if (ltd.isLong()||rtd.isLong())
832 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
834 lefttype=new TypeDescriptor(TypeDescriptor.INT);
836 on.setLeftType(lefttype);
837 on.setRightType(righttype);
838 on.setType(lefttype);
841 case Operation.LEFTSHIFT:
842 case Operation.RIGHTSHIFT:
843 if (!rtd.isIntegerType())
845 //5.6.1 Unary Numeric Promotion
846 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
847 righttype=new TypeDescriptor(TypeDescriptor.INT);
851 on.setRightType(righttype);
852 if (!ltd.isIntegerType())
854 case Operation.UNARYPLUS:
855 case Operation.UNARYMINUS:
856 /* case Operation.POSTINC:
857 case Operation.POSTDEC:
858 case Operation.PREINC:
859 case Operation.PREDEC:*/
862 //5.6.1 Unary Numeric Promotion
863 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
864 lefttype=new TypeDescriptor(TypeDescriptor.INT);
867 on.setLeftType(lefttype);
868 on.setType(lefttype);
871 throw new Error(op.toString());
875 if (!typeutil.isSuperorType(td, on.getType())) {
876 System.out.println(td);
877 System.out.println(on.getType());
878 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));