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 in "+td);
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());
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);
182 throw new Error("Unrecognized FlagExpressionNode");
186 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
187 /* Check return type */
188 if (!md.isConstructor())
189 if (!md.getReturnType().isVoid())
190 checkTypeDescriptor(md.getReturnType());
192 for(int i=0; i<md.numParameters(); i++) {
193 TypeDescriptor param_type=md.getParamType(i);
194 checkTypeDescriptor(param_type);
196 /* Link the naming environments */
197 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
198 md.getParameterTable().setParent(cd.getFieldTable());
200 if (!md.isStatic()) {
201 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
206 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
207 ClassDescriptor superdesc=cd.getSuperDesc();
208 if (superdesc!=null) {
209 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
210 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext();) {
211 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
212 if (md.matches(matchmd)) {
213 if (matchmd.getModifiers().isFinal()) {
214 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
219 BlockNode bn=state.getMethodBody(md);
220 checkBlockNode(md, md.getParameterTable(),bn);
223 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
224 /* Link in the naming environment */
225 bn.getVarTable().setParent(nametable);
226 for(int i=0; i<bn.size(); i++) {
227 BlockStatementNode bsn=bn.get(i);
228 checkBlockStatementNode(md, bn.getVarTable(),bsn);
232 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
234 case Kind.BlockExpressionNode:
235 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
238 case Kind.DeclarationNode:
239 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
242 case Kind.TagDeclarationNode:
243 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
246 case Kind.IfStatementNode:
247 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
251 checkLoopNode(md, nametable, (LoopNode)bsn);
254 case Kind.ReturnNode:
255 checkReturnNode(md, nametable, (ReturnNode)bsn);
258 case Kind.TaskExitNode:
259 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
262 case Kind.SubBlockNode:
263 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
266 case Kind.AtomicNode:
267 checkAtomicNode(md, nametable, (AtomicNode)bsn);
271 // do nothing, no semantic check for SESEs
278 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
279 checkExpressionNode(md, nametable, ben.getExpression(), null);
282 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
283 VarDescriptor vd=dn.getVarDescriptor();
284 checkTypeDescriptor(vd.getType());
285 Descriptor d=nametable.get(vd.getSymbol());
287 (d instanceof FieldDescriptor)) {
290 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
291 if (dn.getExpression()!=null)
292 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
295 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
296 TagVarDescriptor vd=dn.getTagVarDescriptor();
297 Descriptor d=nametable.get(vd.getSymbol());
299 (d instanceof FieldDescriptor)) {
302 throw new Error(vd.getSymbol()+" defined a second time");
305 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
306 checkBlockNode(md, nametable, sbn.getBlockNode());
309 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
310 checkBlockNode(md, nametable, sbn.getBlockNode());
313 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
314 if (d instanceof TaskDescriptor)
315 throw new Error("Illegal return appears in Task: "+d.getSymbol());
316 MethodDescriptor md=(MethodDescriptor)d;
317 if (rn.getReturnExpression()!=null)
318 if (md.getReturnType()==null)
319 throw new Error("Constructor can't return something.");
320 else if (md.getReturnType().isVoid())
321 throw new Error(md+" is void");
323 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
325 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
326 throw new Error("Need to return something for "+md);
329 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
330 if (md instanceof MethodDescriptor)
331 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
332 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
333 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
336 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
337 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
338 checkBlockNode(md, nametable, isn.getTrueBlock());
339 if (isn.getFalseBlock()!=null)
340 checkBlockNode(md, nametable, isn.getFalseBlock());
343 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
345 case Kind.AssignmentNode:
346 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
350 checkCastNode(md,nametable,(CastNode)en,td);
353 case Kind.CreateObjectNode:
354 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
357 case Kind.FieldAccessNode:
358 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
361 case Kind.ArrayAccessNode:
362 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
365 case Kind.LiteralNode:
366 checkLiteralNode(md,nametable,(LiteralNode)en,td);
369 case Kind.MethodInvokeNode:
370 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
374 checkNameNode(md,nametable,(NameNode)en,td);
378 checkOpNode(md,nametable,(OpNode)en,td);
381 case Kind.OffsetNode:
382 checkOffsetNode(md, nametable, (OffsetNode)en, new TypeDescriptor(TypeDescriptor.OFFSET));
388 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
389 /* Get type descriptor */
390 if (cn.getType()==null) {
391 NameDescriptor typenamed=cn.getTypeName().getName();
392 String typename=typenamed.toString();
393 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
397 /* Check the type descriptor */
398 TypeDescriptor cast_type=cn.getType();
399 checkTypeDescriptor(cast_type);
403 if (!typeutil.isSuperorType(td,cast_type))
404 throw new Error("Cast node returns "+cast_type+", but need "+td);
407 ExpressionNode en=cn.getExpression();
408 checkExpressionNode(md, nametable, en, null);
409 TypeDescriptor etd=en.getType();
410 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
413 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
415 if (typeutil.isCastable(etd, cast_type))
418 /* Different branches */
419 /* TODO: change if add interfaces */
420 throw new Error("Cast will always fail\n"+cn.printNode(0));
423 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
424 ExpressionNode left=fan.getExpression();
425 checkExpressionNode(md,nametable,left,null);
426 TypeDescriptor ltd=left.getType();
427 String fieldname=fan.getFieldName();
429 FieldDescriptor fd=null;
430 if (ltd.isArray()&&fieldname.equals("length"))
431 fd=FieldDescriptor.arrayLength;
433 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
435 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
438 if (!typeutil.isSuperorType(td,fan.getType()))
439 throw new Error("Field node returns "+fan.getType()+", but need "+td);
442 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
443 ExpressionNode left=aan.getExpression();
444 checkExpressionNode(md,nametable,left,null);
446 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
447 TypeDescriptor ltd=left.getType();
450 if (!typeutil.isSuperorType(td,aan.getType()))
451 throw new Error("Field node returns "+aan.getType()+", but need "+td);
454 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
455 /* Resolve the type */
456 Object o=ln.getValue();
457 if (ln.getTypeString().equals("null")) {
458 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
459 } else if (o instanceof Integer) {
460 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
461 } else if (o instanceof Long) {
462 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
463 } else if (o instanceof Float) {
464 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
465 } else if (o instanceof Boolean) {
466 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
467 } else if (o instanceof Double) {
468 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
469 } else if (o instanceof Character) {
470 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
471 } else if (o instanceof String) {
472 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
476 if (!typeutil.isSuperorType(td,ln.getType()))
477 throw new Error("Field node returns "+ln.getType()+", but need "+td);
480 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
481 NameDescriptor nd=nn.getName();
482 if (nd.getBase()!=null) {
484 /* Rewrite NameNode */
485 ExpressionNode en=translateNameDescriptorintoExpression(nd);
486 nn.setExpression(en);
487 checkExpressionNode(md,nametable,en,td);
489 String varname=nd.toString();
490 Descriptor d=(Descriptor)nametable.get(varname);
492 throw new Error("Name "+varname+" undefined in: "+md);
494 if (d instanceof VarDescriptor) {
496 } else if (d instanceof FieldDescriptor) {
497 nn.setField((FieldDescriptor)d);
498 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
499 } else if (d instanceof TagVarDescriptor) {
501 } else throw new Error("Wrong type of descriptor");
503 if (!typeutil.isSuperorType(td,nn.getType()))
504 throw new Error("Field node returns "+nn.getType()+", but need "+td);
508 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
509 TypeDescriptor ltd = ofn.td;
510 //System.out.println("Testing TypeDescriptor ltd = " + ofn.td);
511 String fieldname = ofn.fieldname;
512 //System.out.println("Testing String fieldname = " + ofn.fieldname);
513 Descriptor d = (Descriptor) nameTable.get(fieldname);
514 //System.out.println("Testing Descriptor d = " + d.toString());
516 ClassDescriptor cd = null;
517 checkTypeDescriptor(ltd);
518 cd = ltd.getClassDesc();
519 ofn.setClassDesc(cd);
520 //System.out.println("Testing for ClassDescriptor cd = " + cd.toString());
522 FieldDescriptor fd=null;
523 if (ltd.isArray()&&fieldname.equals("length")) {
524 fd=FieldDescriptor.arrayLength;
526 fd=(FieldDescriptor) cd.getFieldTable().get(fieldname);
528 //System.out.println("Testing for FieldDescriptor fd = " + fd.toString());
531 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
535 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
536 boolean postinc=true;
537 if (an.getOperation().getBaseOp()==null||
538 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
539 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
543 checkExpressionNode(md, nametable, an.getSrc(),td);
544 //TODO: Need check on validity of operation here
545 if (!((an.getDest() instanceof FieldAccessNode)||
546 (an.getDest() instanceof ArrayAccessNode)||
547 (an.getDest() instanceof NameNode)))
548 throw new Error("Bad lside in "+an.printNode(0));
549 checkExpressionNode(md, nametable, an.getDest(), null);
551 /* We want parameter variables to tasks to be immutable */
552 if (md instanceof TaskDescriptor) {
553 if (an.getDest() instanceof NameNode) {
554 NameNode nn=(NameNode)an.getDest();
555 if (nn.getVar()!=null) {
556 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
557 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
562 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
564 ClassDescriptor stringcl=typeutil.getClass(TypeUtil.StringClass);
565 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
566 NameDescriptor nd=new NameDescriptor("String");
567 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
569 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
570 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
571 rightmin.addArgument(an.getSrc());
573 checkExpressionNode(md, nametable, an.getSrc(), null);
577 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
578 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
582 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
583 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
584 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
585 checkBlockNode(md, nametable, ln.getBody());
588 /* Link in the initializer naming environment */
589 BlockNode bn=ln.getInitializer();
590 bn.getVarTable().setParent(nametable);
591 for(int i=0; i<bn.size(); i++) {
592 BlockStatementNode bsn=bn.get(i);
593 checkBlockStatementNode(md, bn.getVarTable(),bsn);
595 //check the condition
596 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
597 checkBlockNode(md, bn.getVarTable(), ln.getBody());
598 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
603 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
604 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
605 for(int i=0; i<con.numArgs(); i++) {
606 ExpressionNode en=con.getArg(i);
607 checkExpressionNode(md,nametable,en,null);
608 tdarray[i]=en.getType();
611 TypeDescriptor typetolookin=con.getType();
612 checkTypeDescriptor(typetolookin);
614 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
615 throw new Error(typetolookin + " isn't a "+td);
617 /* Check flag effects */
618 if (con.getFlagEffects()!=null) {
619 FlagEffects fe=con.getFlagEffects();
620 ClassDescriptor cd=typetolookin.getClassDesc();
622 for(int j=0; j<fe.numEffects(); j++) {
623 FlagEffect flag=fe.getEffect(j);
624 String name=flag.getName();
625 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
626 //Make sure the flag is declared
628 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
629 if (flag_d.getExternal())
630 throw new Error("Attempting to modify external flag: "+name);
631 flag.setFlag(flag_d);
633 for(int j=0; j<fe.numTagEffects(); j++) {
634 TagEffect tag=fe.getTagEffect(j);
635 String name=tag.getName();
637 Descriptor d=(Descriptor)nametable.get(name);
639 throw new Error("Tag descriptor "+name+" undeclared");
640 else if (!(d instanceof TagVarDescriptor))
641 throw new Error(name+" is not a tag descriptor");
642 tag.setTag((TagVarDescriptor)d);
646 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
647 throw new Error("Can't allocate primitive type:"+con.printNode(0));
649 if (!typetolookin.isArray()) {
650 //Array's don't need constructor calls
651 ClassDescriptor classtolookin=typetolookin.getClassDesc();
653 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
654 MethodDescriptor bestmd=null;
656 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
657 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
658 /* Need correct number of parameters */
659 if (con.numArgs()!=currmd.numParameters())
661 for(int i=0; i<con.numArgs(); i++) {
662 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
665 /* Local allocations can't call global allocator */
666 if (!con.isGlobal()&&currmd.isGlobal())
669 /* Method okay so far */
673 if (typeutil.isMoreSpecific(currmd,bestmd)) {
675 } else if (con.isGlobal()&&match(currmd, bestmd)) {
676 if (currmd.isGlobal()&&!bestmd.isGlobal())
678 else if (currmd.isGlobal()&&bestmd.isGlobal())
680 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
681 throw new Error("No method is most specific");
684 /* Is this more specific than bestmd */
688 throw new Error("No method found for "+con.printNode(0)+" in "+md);
689 con.setConstructor(bestmd);
694 /** Check to see if md1 is the same specificity as md2.*/
696 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
697 /* Checks if md1 is more specific than md2 */
698 if (md1.numParameters()!=md2.numParameters())
700 for(int i=0; i<md1.numParameters(); i++) {
701 if (!md2.getParamType(i).equals(md1.getParamType(i)))
704 if (!md2.getReturnType().equals(md1.getReturnType()))
707 if (!md2.getClassDesc().equals(md1.getClassDesc()))
715 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
716 String id=nd.getIdentifier();
717 NameDescriptor base=nd.getBase();
719 return new NameNode(nd);
721 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
725 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
726 /*Typecheck subexpressions
727 and get types for expressions*/
729 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
730 for(int i=0; i<min.numArgs(); i++) {
731 ExpressionNode en=min.getArg(i);
732 checkExpressionNode(md,nametable,en,null);
733 tdarray[i]=en.getType();
735 TypeDescriptor typetolookin=null;
736 if (min.getExpression()!=null) {
737 checkExpressionNode(md,nametable,min.getExpression(),null);
738 typetolookin=min.getExpression().getType();
739 } else if (min.getBaseName()!=null) {
740 String rootname=min.getBaseName().getRoot();
741 if (rootname.equals("super")) {
742 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
743 typetolookin=new TypeDescriptor(supercd);
744 } else if (nametable.get(rootname)!=null) {
745 //we have an expression
746 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
747 checkExpressionNode(md, nametable, min.getExpression(), null);
748 typetolookin=min.getExpression().getType();
751 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
753 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
754 typetolookin=new TypeDescriptor(cd);
756 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
757 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
758 min.methodid=supercd.getSymbol();
759 typetolookin=new TypeDescriptor(supercd);
760 } else if (md instanceof MethodDescriptor) {
761 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
763 /* If this a task descriptor we throw an error at this point */
764 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
766 if (!typetolookin.isClass())
767 throw new Error("Error with method call to "+min.getMethodName());
768 ClassDescriptor classtolookin=typetolookin.getClassDesc();
769 //System.out.println("Method name="+min.getMethodName());
771 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
772 MethodDescriptor bestmd=null;
774 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
775 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
776 /* Need correct number of parameters */
777 if (min.numArgs()!=currmd.numParameters())
779 for(int i=0; i<min.numArgs(); i++) {
780 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
783 /* Method okay so far */
787 if (typeutil.isMoreSpecific(currmd,bestmd)) {
789 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
790 throw new Error("No method is most specific");
792 /* Is this more specific than bestmd */
796 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
797 min.setMethod(bestmd);
799 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
800 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
801 /* Check whether we need to set this parameter to implied this */
802 if (!bestmd.isStatic()) {
803 if (min.getExpression()==null) {
804 ExpressionNode en=new NameNode(new NameDescriptor("this"));
805 min.setExpression(en);
806 checkExpressionNode(md, nametable, min.getExpression(), null);
812 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
813 checkExpressionNode(md, nametable, on.getLeft(), null);
814 if (on.getRight()!=null)
815 checkExpressionNode(md, nametable, on.getRight(), null);
816 TypeDescriptor ltd=on.getLeft().getType();
817 TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
818 TypeDescriptor lefttype=null;
819 TypeDescriptor righttype=null;
820 Operation op=on.getOp();
823 case Operation.LOGIC_OR:
824 case Operation.LOGIC_AND:
825 if (!(rtd.isBoolean()))
827 on.setRightType(rtd);
829 case Operation.LOGIC_NOT:
830 if (!(ltd.isBoolean()))
835 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
839 // 5.6.2 Binary Numeric Promotion
840 //TODO unboxing of reference objects
843 else if (ltd.isFloat())
845 else if (ltd.isLong())
846 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
848 lefttype=new TypeDescriptor(TypeDescriptor.INT);
849 on.setLeftType(lefttype);
850 on.setType(lefttype);
853 case Operation.BIT_OR:
854 case Operation.BIT_XOR:
855 case Operation.BIT_AND:
856 // 5.6.2 Binary Numeric Promotion
857 //TODO unboxing of reference objects
858 if (ltd.isDouble()||rtd.isDouble())
860 else if (ltd.isFloat()||rtd.isFloat())
862 else if (ltd.isLong()||rtd.isLong())
863 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
864 // 090205 hack for boolean
865 else if (ltd.isBoolean()||rtd.isBoolean())
866 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
868 lefttype=new TypeDescriptor(TypeDescriptor.INT);
871 on.setLeftType(lefttype);
872 on.setRightType(righttype);
873 on.setType(lefttype);
876 case Operation.ISAVAILABLE:
877 if (!(ltd.isPtr())) {
878 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
881 on.setLeftType(lefttype);
882 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
885 case Operation.EQUAL:
886 case Operation.NOTEQUAL:
887 // 5.6.2 Binary Numeric Promotion
888 //TODO unboxing of reference objects
889 if (ltd.isBoolean()||rtd.isBoolean()) {
890 if (!(ltd.isBoolean()&&rtd.isBoolean()))
892 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
893 } else if (ltd.isPtr()||rtd.isPtr()) {
894 if (!(ltd.isPtr()&&rtd.isPtr()))
898 } else if (ltd.isDouble()||rtd.isDouble())
899 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
900 else if (ltd.isFloat()||rtd.isFloat())
901 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
902 else if (ltd.isLong()||rtd.isLong())
903 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
905 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
907 on.setLeftType(lefttype);
908 on.setRightType(righttype);
909 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
918 // 5.6.2 Binary Numeric Promotion
919 //TODO unboxing of reference objects
920 if (!ltd.isNumber()||!rtd.isNumber())
923 if (ltd.isDouble()||rtd.isDouble())
924 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
925 else if (ltd.isFloat()||rtd.isFloat())
926 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
927 else if (ltd.isLong()||rtd.isLong())
928 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
930 lefttype=new TypeDescriptor(TypeDescriptor.INT);
932 on.setLeftType(lefttype);
933 on.setRightType(righttype);
934 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
938 if (ltd.isString()||rtd.isString()) {
939 ClassDescriptor stringcl=typeutil.getClass(TypeUtil.StringClass);
940 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
941 NameDescriptor nd=new NameDescriptor("String");
942 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
943 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
944 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
945 leftmin.addArgument(on.getLeft());
947 checkExpressionNode(md, nametable, on.getLeft(), null);
950 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
951 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
952 rightmin.addArgument(on.getRight());
954 checkExpressionNode(md, nametable, on.getRight(), null);
957 on.setLeftType(stringtd);
958 on.setRightType(stringtd);
959 on.setType(stringtd);
967 // 5.6.2 Binary Numeric Promotion
968 //TODO unboxing of reference objects
969 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
970 throw new Error("Error in "+on.printNode(0));
972 if (ltd.isDouble()||rtd.isDouble())
973 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
974 else if (ltd.isFloat()||rtd.isFloat())
975 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
976 else if (ltd.isLong()||rtd.isLong())
977 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
979 lefttype=new TypeDescriptor(TypeDescriptor.INT);
981 on.setLeftType(lefttype);
982 on.setRightType(righttype);
983 on.setType(lefttype);
986 case Operation.LEFTSHIFT:
987 case Operation.RIGHTSHIFT:
988 case Operation.URIGHTSHIFT:
989 if (!rtd.isIntegerType())
991 //5.6.1 Unary Numeric Promotion
992 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
993 righttype=new TypeDescriptor(TypeDescriptor.INT);
997 on.setRightType(righttype);
998 if (!ltd.isIntegerType())
1001 case Operation.UNARYPLUS:
1002 case Operation.UNARYMINUS:
1003 /* case Operation.POSTINC:
1004 case Operation.POSTDEC:
1005 case Operation.PREINC:
1006 case Operation.PREDEC:*/
1007 if (!ltd.isNumber())
1009 //5.6.1 Unary Numeric Promotion
1010 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1011 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1014 on.setLeftType(lefttype);
1015 on.setType(lefttype);
1019 throw new Error(op.toString());
1023 if (!typeutil.isSuperorType(td, on.getType())) {
1024 System.out.println(td);
1025 System.out.println(on.getType());
1026 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));