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();
62 public void checkTypeDescriptor(TypeDescriptor td) {
65 else if (td.isClass()) {
66 String name=td.toString();
67 ClassDescriptor field_cd=(ClassDescriptor)state.getClassSymbolTable().get(name);
69 throw new Error("Undefined class "+name);
70 td.setClassDescriptor(field_cd);
72 } else if (td.isTag())
78 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
79 checkTypeDescriptor(fd.getType());
82 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
84 return; /* No constraint checks to check */
85 for(int i=0;i<ccs.size();i++) {
86 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
88 for(int j=0;j<cc.numArgs();j++) {
89 ExpressionNode en=cc.getArg(j);
90 checkExpressionNode(td,nametable,en,null);
95 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
97 return; /* No flag effects to check */
98 for(int i=0;i<vfe.size();i++) {
99 FlagEffects fe=(FlagEffects) vfe.get(i);
100 String varname=fe.getName();
101 //Make sure the variable is declared as a parameter to the task
102 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
104 throw new Error("Parameter "+varname+" in Flag Effects not declared");
107 //Make sure it correspods to a class
108 TypeDescriptor type_d=vd.getType();
109 if (!type_d.isClass())
110 throw new Error("Cannot have non-object argument for flag_effect");
112 ClassDescriptor cd=type_d.getClassDesc();
113 for(int j=0;j<fe.numEffects();j++) {
114 FlagEffect flag=fe.getEffect(j);
115 String name=flag.getName();
116 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
117 //Make sure the flag is declared
119 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
120 if (flag_d.getExternal())
121 throw new Error("Attempting to modify external flag: "+name);
122 flag.setFlag(flag_d);
124 for(int j=0;j<fe.numTagEffects();j++) {
125 TagEffect tag=fe.getTagEffect(j);
126 String name=tag.getName();
128 Descriptor d=(Descriptor)nametable.get(name);
130 throw new Error("Tag descriptor "+name+" undeclared");
131 else if (!(d instanceof TagVarDescriptor))
132 throw new Error(name+" is not a tag descriptor");
133 tag.setTag((TagVarDescriptor)d);
138 public void checkTask(TaskDescriptor td) {
139 for(int i=0;i<td.numParameters();i++) {
140 /* Check that parameter is well typed */
141 TypeDescriptor param_type=td.getParamType(i);
142 checkTypeDescriptor(param_type);
144 /* Check the parameter's flag expression is well formed */
145 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
146 if (!param_type.isClass())
147 throw new Error("Cannot have non-object argument to a task");
148 ClassDescriptor cd=param_type.getClassDesc();
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 ClassDescriptor superdesc=cd.getSuperDesc();
206 if (superdesc!=null) {
207 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
208 for(Iterator methodit=possiblematches.iterator();methodit.hasNext();) {
209 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
210 if (md.matches(matchmd)) {
211 if (matchmd.getModifiers().isFinal()) {
212 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
217 BlockNode bn=state.getMethodBody(md);
218 checkBlockNode(md, md.getParameterTable(),bn);
221 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
222 /* Link in the naming environment */
223 bn.getVarTable().setParent(nametable);
224 for(int i=0;i<bn.size();i++) {
225 BlockStatementNode bsn=bn.get(i);
226 checkBlockStatementNode(md, bn.getVarTable(),bsn);
230 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
232 case Kind.BlockExpressionNode:
233 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
236 case Kind.DeclarationNode:
237 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
240 case Kind.TagDeclarationNode:
241 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
244 case Kind.IfStatementNode:
245 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
249 checkLoopNode(md, nametable, (LoopNode)bsn);
252 case Kind.ReturnNode:
253 checkReturnNode(md, nametable, (ReturnNode)bsn);
256 case Kind.TaskExitNode:
257 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
260 case Kind.SubBlockNode:
261 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
264 case Kind.AtomicNode:
265 checkAtomicNode(md, nametable, (AtomicNode)bsn);
271 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
272 checkExpressionNode(md, nametable, ben.getExpression(), null);
275 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
276 VarDescriptor vd=dn.getVarDescriptor();
277 checkTypeDescriptor(vd.getType());
278 Descriptor d=nametable.get(vd.getSymbol());
280 (d instanceof FieldDescriptor)) {
283 throw new Error(vd.getSymbol()+" defined a second time");
284 if (dn.getExpression()!=null)
285 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
288 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
289 TagVarDescriptor vd=dn.getTagVarDescriptor();
290 Descriptor d=nametable.get(vd.getSymbol());
292 (d instanceof FieldDescriptor)) {
295 throw new Error(vd.getSymbol()+" defined a second time");
298 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
299 checkBlockNode(md, nametable, sbn.getBlockNode());
302 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
303 checkBlockNode(md, nametable, sbn.getBlockNode());
306 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
307 if (d instanceof TaskDescriptor)
308 throw new Error("Illegal return appears in Task: "+d.getSymbol());
309 MethodDescriptor md=(MethodDescriptor)d;
310 if (rn.getReturnExpression()!=null)
311 if (md.getReturnType()==null)
312 throw new Error("Constructor can't return something.");
313 else if (md.getReturnType().isVoid())
314 throw new Error(md+" is void");
316 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
318 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
319 throw new Error("Need to return something for "+md);
322 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
323 if (md instanceof MethodDescriptor)
324 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
325 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
326 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
329 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
330 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
331 checkBlockNode(md, nametable, isn.getTrueBlock());
332 if (isn.getFalseBlock()!=null)
333 checkBlockNode(md, nametable, isn.getFalseBlock());
336 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
338 case Kind.AssignmentNode:
339 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
342 checkCastNode(md,nametable,(CastNode)en,td);
344 case Kind.CreateObjectNode:
345 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
347 case Kind.FieldAccessNode:
348 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
350 case Kind.ArrayAccessNode:
351 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
353 case Kind.LiteralNode:
354 checkLiteralNode(md,nametable,(LiteralNode)en,td);
356 case Kind.MethodInvokeNode:
357 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
360 checkNameNode(md,nametable,(NameNode)en,td);
363 checkOpNode(md,nametable,(OpNode)en,td);
369 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
370 /* Get type descriptor */
371 if (cn.getType()==null) {
372 NameDescriptor typenamed=cn.getTypeName().getName();
373 String typename=typenamed.toString();
374 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
378 /* Check the type descriptor */
379 TypeDescriptor cast_type=cn.getType();
380 checkTypeDescriptor(cast_type);
384 if (!typeutil.isSuperorType(td,cast_type))
385 throw new Error("Cast node returns "+cast_type+", but need "+td);
388 ExpressionNode en=cn.getExpression();
389 checkExpressionNode(md, nametable, en, null);
390 TypeDescriptor etd=en.getType();
391 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
394 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
396 if (typeutil.isCastable(etd, cast_type))
399 /* Different branches */
400 /* TODO: change if add interfaces */
401 throw new Error("Cast will always fail\n"+cn.printNode(0));
404 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
405 ExpressionNode left=fan.getExpression();
406 checkExpressionNode(md,nametable,left,null);
407 TypeDescriptor ltd=left.getType();
408 String fieldname=fan.getFieldName();
410 FieldDescriptor fd=null;
411 if (ltd.isArray()&&fieldname.equals("length"))
412 fd=FieldDescriptor.arrayLength;
414 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
416 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0));
419 if (!typeutil.isSuperorType(td,fan.getType()))
420 throw new Error("Field node returns "+fan.getType()+", but need "+td);
423 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
424 ExpressionNode left=aan.getExpression();
425 checkExpressionNode(md,nametable,left,null);
427 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
428 TypeDescriptor ltd=left.getType();
431 if (!typeutil.isSuperorType(td,aan.getType()))
432 throw new Error("Field node returns "+aan.getType()+", but need "+td);
435 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
436 /* Resolve the type */
437 Object o=ln.getValue();
438 if (ln.getTypeString().equals("null")) {
439 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
440 } else if (o instanceof Integer) {
441 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
442 } else if (o instanceof Long) {
443 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
444 } else if (o instanceof Float) {
445 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
446 } else if (o instanceof Boolean) {
447 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
448 } else if (o instanceof Double) {
449 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
450 } else if (o instanceof Character) {
451 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
452 } else if (o instanceof String) {
453 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
457 if (!typeutil.isSuperorType(td,ln.getType()))
458 throw new Error("Field node returns "+ln.getType()+", but need "+td);
461 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
462 NameDescriptor nd=nn.getName();
463 if (nd.getBase()!=null) {
465 /* Rewrite NameNode */
466 ExpressionNode en=translateNameDescriptorintoExpression(nd);
467 nn.setExpression(en);
468 checkExpressionNode(md,nametable,en,td);
470 String varname=nd.toString();
471 Descriptor d=(Descriptor)nametable.get(varname);
473 throw new Error("Name "+varname+" undefined in: "+md);
475 if (d instanceof VarDescriptor) {
477 } else if (d instanceof FieldDescriptor) {
478 nn.setField((FieldDescriptor)d);
479 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
480 } else if (d instanceof TagVarDescriptor) {
482 } else throw new Error("Wrong type of descriptor");
484 if (!typeutil.isSuperorType(td,nn.getType()))
485 throw new Error("Field node returns "+nn.getType()+", but need "+td);
489 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
490 boolean postinc=true;
491 if (an.getOperation().getBaseOp()==null||
492 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
493 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
497 checkExpressionNode(md, nametable, an.getSrc() ,td);
498 //TODO: Need check on validity of operation here
499 if (!((an.getDest() instanceof FieldAccessNode)||
500 (an.getDest() instanceof ArrayAccessNode)||
501 (an.getDest() instanceof NameNode)))
502 throw new Error("Bad lside in "+an.printNode(0));
503 checkExpressionNode(md, nametable, an.getDest(), null);
505 /* We want parameter variables to tasks to be immutable */
506 if (md instanceof TaskDescriptor) {
507 if (an.getDest() instanceof NameNode) {
508 NameNode nn=(NameNode)an.getDest();
509 if (nn.getVar()!=null) {
510 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
511 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
516 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
517 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
521 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
522 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
523 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
524 checkBlockNode(md, nametable, ln.getBody());
527 /* Link in the initializer naming environment */
528 BlockNode bn=ln.getInitializer();
529 bn.getVarTable().setParent(nametable);
530 for(int i=0;i<bn.size();i++) {
531 BlockStatementNode bsn=bn.get(i);
532 checkBlockStatementNode(md, bn.getVarTable(),bsn);
534 //check the condition
535 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
536 checkBlockNode(md, bn.getVarTable(), ln.getBody());
537 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
542 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
543 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
544 for(int i=0;i<con.numArgs();i++) {
545 ExpressionNode en=con.getArg(i);
546 checkExpressionNode(md,nametable,en,null);
547 tdarray[i]=en.getType();
550 TypeDescriptor typetolookin=con.getType();
551 checkTypeDescriptor(typetolookin);
553 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
554 throw new Error(typetolookin + " isn't a "+td);
556 /* Check flag effects */
557 if (con.getFlagEffects()!=null) {
558 FlagEffects fe=con.getFlagEffects();
559 ClassDescriptor cd=typetolookin.getClassDesc();
561 for(int j=0;j<fe.numEffects();j++) {
562 FlagEffect flag=fe.getEffect(j);
563 String name=flag.getName();
564 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
565 //Make sure the flag is declared
567 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
568 if (flag_d.getExternal())
569 throw new Error("Attempting to modify external flag: "+name);
570 flag.setFlag(flag_d);
572 for(int j=0;j<fe.numTagEffects();j++) {
573 TagEffect tag=fe.getTagEffect(j);
574 String name=tag.getName();
576 Descriptor d=(Descriptor)nametable.get(name);
578 throw new Error("Tag descriptor "+name+" undeclared");
579 else if (!(d instanceof TagVarDescriptor))
580 throw new Error(name+" is not a tag descriptor");
581 tag.setTag((TagVarDescriptor)d);
585 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
586 throw new Error("Can't allocate primitive type:"+con.printNode(0));
588 if (!typetolookin.isArray()) {
589 //Array's don't need constructor calls
590 ClassDescriptor classtolookin=typetolookin.getClassDesc();
592 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
593 MethodDescriptor bestmd=null;
595 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
596 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
597 /* Need correct number of parameters */
598 if (con.numArgs()!=currmd.numParameters())
600 for(int i=0;i<con.numArgs();i++) {
601 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
604 /* Local allocations can't call global allocator */
605 if (!con.isGlobal()&&currmd.isGlobal())
608 /* Method okay so far */
612 if (typeutil.isMoreSpecific(currmd,bestmd)) {
614 } else if (con.isGlobal()&&match(currmd, bestmd)) {
615 if (currmd.isGlobal()&&!bestmd.isGlobal())
617 else if (currmd.isGlobal()&&bestmd.isGlobal())
619 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
620 throw new Error("No method is most specific");
623 /* Is this more specific than bestmd */
627 throw new Error("No method found for "+con.printNode(0));
628 con.setConstructor(bestmd);
633 /** Check to see if md1 is the same specificity as md2.*/
635 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
636 /* Checks if md1 is more specific than md2 */
637 if (md1.numParameters()!=md2.numParameters())
639 for(int i=0;i<md1.numParameters();i++) {
640 if (!md2.getParamType(i).equals(md1.getParamType(i)))
643 if (!md2.getReturnType().equals(md1.getReturnType()))
646 if (!md2.getClassDesc().equals(md1.getClassDesc()))
654 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
655 String id=nd.getIdentifier();
656 NameDescriptor base=nd.getBase();
658 return new NameNode(nd);
660 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
664 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
665 /*Typecheck subexpressions
666 and get types for expressions*/
668 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
669 for(int i=0;i<min.numArgs();i++) {
670 ExpressionNode en=min.getArg(i);
671 checkExpressionNode(md,nametable,en,null);
672 tdarray[i]=en.getType();
674 TypeDescriptor typetolookin=null;
675 if (min.getExpression()!=null) {
676 checkExpressionNode(md,nametable,min.getExpression(),null);
677 typetolookin=min.getExpression().getType();
678 } else if (min.getBaseName()!=null) {
679 String rootname=min.getBaseName().getRoot();
680 if (rootname.equals("super")) {
681 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
682 typetolookin=new TypeDescriptor(supercd);
683 } else if (nametable.get(rootname)!=null) {
684 //we have an expression
685 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
686 checkExpressionNode(md, nametable, min.getExpression(), null);
687 typetolookin=min.getExpression().getType();
690 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
692 throw new Error(min.getBaseName()+" undefined");
693 typetolookin=new TypeDescriptor(cd);
695 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
696 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
697 min.methodid=supercd.getSymbol();
698 typetolookin=new TypeDescriptor(supercd);
699 } else if (md instanceof MethodDescriptor) {
700 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
702 /* If this a task descriptor we throw an error at this point */
703 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
705 if (!typetolookin.isClass())
706 throw new Error("Error with method call to "+min.getMethodName());
707 ClassDescriptor classtolookin=typetolookin.getClassDesc();
708 //System.out.println("Method name="+min.getMethodName());
710 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
711 MethodDescriptor bestmd=null;
713 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
714 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
715 /* Need correct number of parameters */
716 if (min.numArgs()!=currmd.numParameters())
718 for(int i=0;i<min.numArgs();i++) {
719 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
722 /* Method okay so far */
726 if (typeutil.isMoreSpecific(currmd,bestmd)) {
728 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
729 throw new Error("No method is most specific");
731 /* Is this more specific than bestmd */
735 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin);
736 min.setMethod(bestmd);
738 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
739 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
740 /* Check whether we need to set this parameter to implied this */
741 if (!bestmd.isStatic()) {
742 if (min.getExpression()==null) {
743 ExpressionNode en=new NameNode(new NameDescriptor("this"));
744 min.setExpression(en);
745 checkExpressionNode(md, nametable, min.getExpression(), null);
751 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
752 checkExpressionNode(md, nametable, on.getLeft(), null);
753 if (on.getRight()!=null)
754 checkExpressionNode(md, nametable, on.getRight(), null);
755 TypeDescriptor ltd=on.getLeft().getType();
756 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
757 TypeDescriptor lefttype=null;
758 TypeDescriptor righttype=null;
759 Operation op=on.getOp();
762 case Operation.LOGIC_OR:
763 case Operation.LOGIC_AND:
764 if (!(rtd.isBoolean()))
766 on.setRightType(rtd);
767 case Operation.LOGIC_NOT:
768 if (!(ltd.isBoolean()))
773 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
776 case Operation.BIT_OR:
777 case Operation.BIT_XOR:
778 case Operation.BIT_AND:
779 // 5.6.2 Binary Numeric Promotion
780 //TODO unboxing of reference objects
781 if (ltd.isDouble()||rtd.isDouble())
783 else if (ltd.isFloat()||rtd.isFloat())
785 else if (ltd.isLong()||rtd.isLong())
786 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
788 lefttype=new TypeDescriptor(TypeDescriptor.INT);
791 on.setLeftType(lefttype);
792 on.setRightType(righttype);
793 on.setType(lefttype);
796 case Operation.ISAVAILABLE:
797 if (!(ltd.isPtr())) {
798 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
801 on.setLeftType(lefttype);
802 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
804 case Operation.EQUAL:
805 case Operation.NOTEQUAL:
806 // 5.6.2 Binary Numeric Promotion
807 //TODO unboxing of reference objects
808 if (ltd.isBoolean()||rtd.isBoolean()) {
809 if (!(ltd.isBoolean()&&rtd.isBoolean()))
811 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
812 } else if (ltd.isPtr()||rtd.isPtr()) {
813 if (!(ltd.isPtr()&&rtd.isPtr()))
817 } else if (ltd.isDouble()||rtd.isDouble())
818 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
819 else if (ltd.isFloat()||rtd.isFloat())
820 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
821 else if (ltd.isLong()||rtd.isLong())
822 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
824 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
826 on.setLeftType(lefttype);
827 on.setRightType(righttype);
828 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
837 // 5.6.2 Binary Numeric Promotion
838 //TODO unboxing of reference objects
839 if (!ltd.isNumber()||!rtd.isNumber())
842 if (ltd.isDouble()||rtd.isDouble())
843 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
844 else if (ltd.isFloat()||rtd.isFloat())
845 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
846 else if (ltd.isLong()||rtd.isLong())
847 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
849 lefttype=new TypeDescriptor(TypeDescriptor.INT);
851 on.setLeftType(lefttype);
852 on.setRightType(righttype);
853 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
857 if (ltd.isString()||rtd.isString()) {
858 ClassDescriptor stringcl=typeutil.getClass(TypeUtil.StringClass);
859 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
860 NameDescriptor nd=new NameDescriptor("String");
861 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
862 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
863 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
864 leftmin.addArgument(on.getLeft());
866 checkExpressionNode(md, nametable, on.getLeft(), null);
869 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
870 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
871 rightmin.addArgument(on.getRight());
873 checkExpressionNode(md, nametable, on.getRight(), null);
876 on.setLeftType(stringtd);
877 on.setRightType(stringtd);
878 on.setType(stringtd);
886 // 5.6.2 Binary Numeric Promotion
887 //TODO unboxing of reference objects
888 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
889 throw new Error("Error in "+on.printNode(0));
891 if (ltd.isDouble()||rtd.isDouble())
892 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
893 else if (ltd.isFloat()||rtd.isFloat())
894 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
895 else if (ltd.isLong()||rtd.isLong())
896 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
898 lefttype=new TypeDescriptor(TypeDescriptor.INT);
900 on.setLeftType(lefttype);
901 on.setRightType(righttype);
902 on.setType(lefttype);
905 case Operation.LEFTSHIFT:
906 case Operation.RIGHTSHIFT:
907 if (!rtd.isIntegerType())
909 //5.6.1 Unary Numeric Promotion
910 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
911 righttype=new TypeDescriptor(TypeDescriptor.INT);
915 on.setRightType(righttype);
916 if (!ltd.isIntegerType())
918 case Operation.UNARYPLUS:
919 case Operation.UNARYMINUS:
920 /* case Operation.POSTINC:
921 case Operation.POSTDEC:
922 case Operation.PREINC:
923 case Operation.PREDEC:*/
926 //5.6.1 Unary Numeric Promotion
927 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
928 lefttype=new TypeDescriptor(TypeDescriptor.INT);
931 on.setLeftType(lefttype);
932 on.setType(lefttype);
935 throw new Error(op.toString());
939 if (!typeutil.isSuperorType(td, on.getType())) {
940 System.out.println(td);
941 System.out.println(on.getType());
942 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));