7 public class SemanticCheck {
15 public SemanticCheck(State state, TypeUtil tu) {
18 this.loopstack=new Stack();
19 this.toanalyze=new HashSet();
20 this.completed=new HashSet();
23 public ClassDescriptor getClass(String classname) {
24 ClassDescriptor cd=typeutil.getClass(classname, toanalyze);
29 private void checkClass(ClassDescriptor cd) {
30 if (!completed.contains(cd)) {
33 //Set superclass link up
34 if (cd.getSuper()!=null) {
35 cd.setSuper(getClass(cd.getSuper()));
36 // Link together Field, Method, and Flag tables so classes
37 // inherit these from their superclasses
38 cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
39 cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
40 cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable());
43 /* Check to see that fields are well typed */
44 for(Iterator field_it=cd.getFields(); field_it.hasNext();) {
45 FieldDescriptor fd=(FieldDescriptor)field_it.next();
49 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
50 MethodDescriptor md=(MethodDescriptor)method_it.next();
56 public void semanticCheck() {
57 SymbolTable classtable=state.getClassSymbolTable();
58 toanalyze.addAll(classtable.getValueSet());
59 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
62 while(!toanalyze.isEmpty()) {
63 Object obj=toanalyze.iterator().next();
64 if (obj instanceof TaskDescriptor) {
65 toanalyze.remove(obj);
66 TaskDescriptor td=(TaskDescriptor)obj;
70 System.out.println( "Error in "+td );
74 ClassDescriptor cd=(ClassDescriptor)obj;
76 //need to initialize typeutil object here...only place we can
77 //get class descriptors without first calling getclass
78 getClass(cd.getSymbol());
79 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
80 MethodDescriptor md=(MethodDescriptor)method_it.next();
82 checkMethodBody(cd,md);
84 System.out.println( "Error in "+md );
92 public void checkTypeDescriptor(TypeDescriptor td) {
95 else if (td.isClass()) {
96 String name=td.toString();
97 ClassDescriptor field_cd=getClass(name);
99 throw new Error("Undefined class "+name);
100 td.setClassDescriptor(field_cd);
102 } else if (td.isTag())
108 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
109 checkTypeDescriptor(fd.getType());
112 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
114 return; /* No constraint checks to check */
115 for(int i=0; i<ccs.size(); i++) {
116 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
118 for(int j=0; j<cc.numArgs(); j++) {
119 ExpressionNode en=cc.getArg(j);
120 checkExpressionNode(td,nametable,en,null);
125 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
127 return; /* No flag effects to check */
128 for(int i=0; i<vfe.size(); i++) {
129 FlagEffects fe=(FlagEffects) vfe.get(i);
130 String varname=fe.getName();
131 //Make sure the variable is declared as a parameter to the task
132 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
134 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
137 //Make sure it correspods to a class
138 TypeDescriptor type_d=vd.getType();
139 if (!type_d.isClass())
140 throw new Error("Cannot have non-object argument for flag_effect");
142 ClassDescriptor cd=type_d.getClassDesc();
143 for(int j=0; j<fe.numEffects(); j++) {
144 FlagEffect flag=fe.getEffect(j);
145 String name=flag.getName();
146 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
147 //Make sure the flag is declared
149 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
150 if (flag_d.getExternal())
151 throw new Error("Attempting to modify external flag: "+name);
152 flag.setFlag(flag_d);
154 for(int j=0; j<fe.numTagEffects(); j++) {
155 TagEffect tag=fe.getTagEffect(j);
156 String name=tag.getName();
158 Descriptor d=(Descriptor)nametable.get(name);
160 throw new Error("Tag descriptor "+name+" undeclared");
161 else if (!(d instanceof TagVarDescriptor))
162 throw new Error(name+" is not a tag descriptor");
163 tag.setTag((TagVarDescriptor)d);
168 public void checkTask(TaskDescriptor td) {
169 for(int i=0; i<td.numParameters(); i++) {
170 /* Check that parameter is well typed */
171 TypeDescriptor param_type=td.getParamType(i);
172 checkTypeDescriptor(param_type);
174 /* Check the parameter's flag expression is well formed */
175 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
176 if (!param_type.isClass())
177 throw new Error("Cannot have non-object argument to a task");
178 ClassDescriptor cd=param_type.getClassDesc();
180 checkFlagExpressionNode(cd, fen);
183 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
184 /* Check that the task code is valid */
185 BlockNode bn=state.getMethodBody(td);
186 checkBlockNode(td, td.getParameterTable(),bn);
189 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
191 case Kind.FlagOpNode:
193 FlagOpNode fon=(FlagOpNode)fen;
194 checkFlagExpressionNode(cd, fon.getLeft());
195 if (fon.getRight()!=null)
196 checkFlagExpressionNode(cd, fon.getRight());
202 FlagNode fn=(FlagNode)fen;
203 String name=fn.getFlagName();
204 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
206 throw new Error("Undeclared flag: "+name);
212 throw new Error("Unrecognized FlagExpressionNode");
216 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
218 // TODO add version for normal Java later
219 /* Check for abstract methods */
220 if(md.isAbstract()) {
221 if(!cd.isAbstract()) {
222 throw new Error("Error! The non-abstract Class " + cd.getSymbol() + "contains an abstract method " + md.getSymbol());
226 /* Check return type */
227 if (!md.isConstructor())
228 if (!md.getReturnType().isVoid())
229 checkTypeDescriptor(md.getReturnType());
231 for(int i=0; i<md.numParameters(); i++) {
232 TypeDescriptor param_type=md.getParamType(i);
233 checkTypeDescriptor(param_type);
235 /* Link the naming environments */
236 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
237 md.getParameterTable().setParent(cd.getFieldTable());
239 if (!md.isStatic()) {
240 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
245 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
246 ClassDescriptor superdesc=cd.getSuperDesc();
247 if (superdesc!=null) {
248 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
249 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext();) {
250 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
251 if (md.matches(matchmd)) {
252 if (matchmd.getModifiers().isFinal()) {
253 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
258 BlockNode bn=state.getMethodBody(md);
259 checkBlockNode(md, md.getParameterTable(),bn);
262 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
263 /* Link in the naming environment */
264 bn.getVarTable().setParent(nametable);
265 for(int i=0; i<bn.size(); i++) {
266 BlockStatementNode bsn=bn.get(i);
267 checkBlockStatementNode(md, bn.getVarTable(),bsn);
271 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
273 case Kind.BlockExpressionNode:
274 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
277 case Kind.DeclarationNode:
278 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
281 case Kind.TagDeclarationNode:
282 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
285 case Kind.IfStatementNode:
286 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
290 checkLoopNode(md, nametable, (LoopNode)bsn);
293 case Kind.ReturnNode:
294 checkReturnNode(md, nametable, (ReturnNode)bsn);
297 case Kind.TaskExitNode:
298 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
301 case Kind.SubBlockNode:
302 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
305 case Kind.AtomicNode:
306 checkAtomicNode(md, nametable, (AtomicNode)bsn);
309 case Kind.SynchronizedNode:
310 checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
313 case Kind.ContinueBreakNode:
314 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
318 case Kind.GenReachNode:
319 // do nothing, no semantic check for SESEs
326 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
327 checkExpressionNode(md, nametable, ben.getExpression(), null);
330 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
331 VarDescriptor vd=dn.getVarDescriptor();
332 checkTypeDescriptor(vd.getType());
333 Descriptor d=nametable.get(vd.getSymbol());
335 (d instanceof FieldDescriptor)) {
338 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
339 if (dn.getExpression()!=null)
340 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
343 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
344 TagVarDescriptor vd=dn.getTagVarDescriptor();
345 Descriptor d=nametable.get(vd.getSymbol());
347 (d instanceof FieldDescriptor)) {
350 throw new Error(vd.getSymbol()+" defined a second time");
353 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
354 checkBlockNode(md, nametable, sbn.getBlockNode());
357 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
358 checkBlockNode(md, nametable, sbn.getBlockNode());
361 void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
362 checkBlockNode(md, nametable, sbn.getBlockNode());
363 //todo this could be Object
364 checkExpressionNode(md, nametable, sbn.getExpr(), null);
367 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
368 if (loopstack.empty())
369 throw new Error("continue/break outside of loop");
370 LoopNode ln=(LoopNode)loopstack.peek();
374 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
375 if (d instanceof TaskDescriptor)
376 throw new Error("Illegal return appears in Task: "+d.getSymbol());
377 MethodDescriptor md=(MethodDescriptor)d;
378 if (rn.getReturnExpression()!=null)
379 if (md.getReturnType()==null)
380 throw new Error("Constructor can't return something.");
381 else if (md.getReturnType().isVoid())
382 throw new Error(md+" is void");
384 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
386 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
387 throw new Error("Need to return something for "+md);
390 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
391 if (md instanceof MethodDescriptor)
392 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
393 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
394 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
397 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
398 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
399 checkBlockNode(md, nametable, isn.getTrueBlock());
400 if (isn.getFalseBlock()!=null)
401 checkBlockNode(md, nametable, isn.getFalseBlock());
404 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
406 case Kind.AssignmentNode:
407 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
411 checkCastNode(md,nametable,(CastNode)en,td);
414 case Kind.CreateObjectNode:
415 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
418 case Kind.FieldAccessNode:
419 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
422 case Kind.ArrayAccessNode:
423 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
426 case Kind.LiteralNode:
427 checkLiteralNode(md,nametable,(LiteralNode)en,td);
430 case Kind.MethodInvokeNode:
431 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
435 checkNameNode(md,nametable,(NameNode)en,td);
439 checkOpNode(md,nametable,(OpNode)en,td);
442 case Kind.OffsetNode:
443 checkOffsetNode(md, nametable, (OffsetNode)en, td);
446 case Kind.TertiaryNode:
447 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
450 case Kind.InstanceOfNode:
451 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
454 case Kind.ArrayInitializerNode:
455 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
461 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
462 /* Get type descriptor */
463 if (cn.getType()==null) {
464 NameDescriptor typenamed=cn.getTypeName().getName();
465 String typename=typenamed.toString();
466 TypeDescriptor ntd=new TypeDescriptor(getClass(typename));
470 /* Check the type descriptor */
471 TypeDescriptor cast_type=cn.getType();
472 checkTypeDescriptor(cast_type);
476 if (!typeutil.isSuperorType(td,cast_type))
477 throw new Error("Cast node returns "+cast_type+", but need "+td);
480 ExpressionNode en=cn.getExpression();
481 checkExpressionNode(md, nametable, en, null);
482 TypeDescriptor etd=en.getType();
483 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
486 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
488 if (typeutil.isCastable(etd, cast_type))
491 /* Different branches */
492 /* TODO: change if add interfaces */
493 throw new Error("Cast will always fail\n"+cn.printNode(0));
496 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
497 ExpressionNode left=fan.getExpression();
498 checkExpressionNode(md,nametable,left,null);
499 TypeDescriptor ltd=left.getType();
500 String fieldname=fan.getFieldName();
502 FieldDescriptor fd=null;
503 if (ltd.isArray()&&fieldname.equals("length"))
504 fd=FieldDescriptor.arrayLength;
506 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
508 // TODO add version for normal Java later
510 // check if this field is a static field
512 throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
517 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
519 if (fd.getType().iswrapper()) {
520 FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
523 fan.fieldname="value";
525 ExpressionNode leftwr=fan.getExpression();
526 TypeDescriptor ltdwr=leftwr.getType();
527 String fieldnamewr=fan.getFieldName();
528 FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
531 throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
536 if (!typeutil.isSuperorType(td,fan.getType()))
537 throw new Error("Field node returns "+fan.getType()+", but need "+td);
541 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
542 ExpressionNode left=aan.getExpression();
543 checkExpressionNode(md,nametable,left,null);
545 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
546 TypeDescriptor ltd=left.getType();
547 if (ltd.dereference().iswrapper()) {
548 aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
552 if (!typeutil.isSuperorType(td,aan.getType()))
553 throw new Error("Field node returns "+aan.getType()+", but need "+td);
556 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
557 /* Resolve the type */
558 Object o=ln.getValue();
559 if (ln.getTypeString().equals("null")) {
560 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
561 } else if (o instanceof Integer) {
562 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
563 } else if (o instanceof Long) {
564 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
565 } else if (o instanceof Float) {
566 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
567 } else if (o instanceof Boolean) {
568 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
569 } else if (o instanceof Double) {
570 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
571 } else if (o instanceof Character) {
572 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
573 } else if (o instanceof String) {
574 ln.setType(new TypeDescriptor(getClass(TypeUtil.StringClass)));
578 if (!typeutil.isSuperorType(td,ln.getType()))
579 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
582 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
583 NameDescriptor nd=nn.getName();
584 if (nd.getBase()!=null) {
586 /* Rewrite NameNode */
587 ExpressionNode en=translateNameDescriptorintoExpression(nd);
588 nn.setExpression(en);
589 checkExpressionNode(md,nametable,en,td);
591 String varname=nd.toString();
592 Descriptor d=(Descriptor)nametable.get(varname);
595 // TODO add version for normal Java later
596 ClassDescriptor cd = null;
597 if(((MethodDescriptor)md).isStaticBlock()) {
598 // this is a static block, all the accessed fields should be static field
599 cd = ((MethodDescriptor)md).getClassDesc();
600 SymbolTable fieldtbl = cd.getFieldTable();
601 FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
602 if((fd == null) || (!fd.isStatic())){
603 // no such field in the class or it is not a static field
604 throw new Error("Name "+varname+" should not be used in static block: "+md);
606 // this is a static field
612 cd=getClass(varname);
614 // this is a class name
618 throw new Error("Name "+varname+" undefined in: "+md);
622 throw new Error("Name "+varname+" undefined in: "+md);
625 if (d instanceof VarDescriptor) {
627 } else if (d instanceof FieldDescriptor) {
628 FieldDescriptor fd=(FieldDescriptor)d;
629 if (fd.getType().iswrapper()) {
630 String id=nd.getIdentifier();
631 NameDescriptor base=nd.getBase();
632 NameNode n=new NameNode(nn.getName());
634 n.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
635 FieldAccessNode fan=new FieldAccessNode(n,"value");
636 FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
638 nn.setExpression(fan);
641 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
643 } else if (d instanceof TagVarDescriptor) {
645 } else throw new Error("Wrong type of descriptor");
647 if (!typeutil.isSuperorType(td,nn.getType()))
648 throw new Error("Field node returns "+nn.getType()+", but need "+td);
652 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
653 TypeDescriptor ltd=ofn.td;
654 checkTypeDescriptor(ltd);
656 String fieldname = ofn.fieldname;
657 FieldDescriptor fd=null;
658 if (ltd.isArray()&&fieldname.equals("length")) {
659 fd=FieldDescriptor.arrayLength;
661 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
665 checkField(ltd.getClassDesc(), fd);
668 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
671 if (!typeutil.isSuperorType(td, ofn.getType())) {
672 System.out.println(td);
673 System.out.println(ofn.getType());
674 throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
680 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
681 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
682 checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
683 checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
686 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
687 if (td!=null&&!td.isBoolean())
688 throw new Error("Expecting type "+td+"for instanceof expression");
690 checkTypeDescriptor(tn.getExprType());
691 checkExpressionNode(md, nametable, tn.getExpr(), null);
694 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
695 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
696 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td);
700 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
701 boolean postinc=true;
702 if (an.getOperation().getBaseOp()==null||
703 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
704 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
707 checkExpressionNode(md, nametable, an.getSrc(),td);
708 //TODO: Need check on validity of operation here
709 if (!((an.getDest() instanceof FieldAccessNode)||
710 (an.getDest() instanceof ArrayAccessNode)||
711 (an.getDest() instanceof NameNode)))
712 throw new Error("Bad lside in "+an.printNode(0));
713 checkExpressionNode(md, nametable, an.getDest(), null);
715 /* We want parameter variables to tasks to be immutable */
716 if (md instanceof TaskDescriptor) {
717 if (an.getDest() instanceof NameNode) {
718 NameNode nn=(NameNode)an.getDest();
719 if (nn.getVar()!=null) {
720 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
721 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
726 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
728 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
729 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
730 NameDescriptor nd=new NameDescriptor("String");
731 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
733 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
734 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
735 rightmin.addArgument(an.getSrc());
737 checkExpressionNode(md, nametable, an.getSrc(), null);
741 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
742 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
746 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
748 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
749 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
750 checkBlockNode(md, nametable, ln.getBody());
753 /* Link in the initializer naming environment */
754 BlockNode bn=ln.getInitializer();
755 bn.getVarTable().setParent(nametable);
756 for(int i=0; i<bn.size(); i++) {
757 BlockStatementNode bsn=bn.get(i);
758 checkBlockStatementNode(md, bn.getVarTable(),bsn);
760 //check the condition
761 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
762 checkBlockNode(md, bn.getVarTable(), ln.getBody());
763 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
769 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
770 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
771 for(int i=0; i<con.numArgs(); i++) {
772 ExpressionNode en=con.getArg(i);
773 checkExpressionNode(md,nametable,en,null);
774 tdarray[i]=en.getType();
777 TypeDescriptor typetolookin=con.getType();
778 checkTypeDescriptor(typetolookin);
780 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
781 throw new Error(typetolookin + " isn't a "+td);
783 /* Check flag effects */
784 if (con.getFlagEffects()!=null) {
785 FlagEffects fe=con.getFlagEffects();
786 ClassDescriptor cd=typetolookin.getClassDesc();
788 for(int j=0; j<fe.numEffects(); j++) {
789 FlagEffect flag=fe.getEffect(j);
790 String name=flag.getName();
791 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
792 //Make sure the flag is declared
794 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
795 if (flag_d.getExternal())
796 throw new Error("Attempting to modify external flag: "+name);
797 flag.setFlag(flag_d);
799 for(int j=0; j<fe.numTagEffects(); j++) {
800 TagEffect tag=fe.getTagEffect(j);
801 String name=tag.getName();
803 Descriptor d=(Descriptor)nametable.get(name);
805 throw new Error("Tag descriptor "+name+" undeclared");
806 else if (!(d instanceof TagVarDescriptor))
807 throw new Error(name+" is not a tag descriptor");
808 tag.setTag((TagVarDescriptor)d);
812 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
813 throw new Error("Can't allocate primitive type:"+con.printNode(0));
815 if (!typetolookin.isArray()) {
816 //Array's don't need constructor calls
817 ClassDescriptor classtolookin=typetolookin.getClassDesc();
819 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
820 MethodDescriptor bestmd=null;
822 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
823 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
824 /* Need correct number of parameters */
825 if (con.numArgs()!=currmd.numParameters())
827 for(int i=0; i<con.numArgs(); i++) {
828 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
831 /* Local allocations can't call global allocator */
832 if (!con.isGlobal()&&currmd.isGlobal())
835 /* Method okay so far */
839 if (typeutil.isMoreSpecific(currmd,bestmd)) {
841 } else if (con.isGlobal()&&match(currmd, bestmd)) {
842 if (currmd.isGlobal()&&!bestmd.isGlobal())
844 else if (currmd.isGlobal()&&bestmd.isGlobal())
846 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
847 throw new Error("No method is most specific");
850 /* Is this more specific than bestmd */
854 throw new Error("No method found for "+con.printNode(0)+" in "+md);
855 con.setConstructor(bestmd);
860 /** Check to see if md1 is the same specificity as md2.*/
862 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
863 /* Checks if md1 is more specific than md2 */
864 if (md1.numParameters()!=md2.numParameters())
866 for(int i=0; i<md1.numParameters(); i++) {
867 if (!md2.getParamType(i).equals(md1.getParamType(i)))
870 if (!md2.getReturnType().equals(md1.getReturnType()))
873 if (!md2.getClassDesc().equals(md1.getClassDesc()))
881 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
882 String id=nd.getIdentifier();
883 NameDescriptor base=nd.getBase();
885 return new NameNode(nd);
887 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
891 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
892 /*Typecheck subexpressions
893 and get types for expressions*/
895 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
896 for(int i=0; i<min.numArgs(); i++) {
897 ExpressionNode en=min.getArg(i);
898 checkExpressionNode(md,nametable,en,null);
899 tdarray[i]=en.getType();
901 TypeDescriptor typetolookin=null;
902 if (min.getExpression()!=null) {
903 checkExpressionNode(md,nametable,min.getExpression(),null);
904 typetolookin=min.getExpression().getType();
905 //if (typetolookin==null)
906 //throw new Error(md+" has null return type");
908 } else if (min.getBaseName()!=null) {
909 String rootname=min.getBaseName().getRoot();
910 if (rootname.equals("super")) {
911 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
912 typetolookin=new TypeDescriptor(supercd);
913 } else if (nametable.get(rootname)!=null) {
914 //we have an expression
915 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
916 checkExpressionNode(md, nametable, min.getExpression(), null);
917 typetolookin=min.getExpression().getType();
921 if (min.getBaseName().getSymbol().equals("System.out"))
922 cd=getClass("System");
924 cd=getClass(min.getBaseName().getSymbol());
926 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
927 typetolookin=new TypeDescriptor(cd);
929 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
930 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
931 min.methodid=supercd.getSymbol();
932 typetolookin=new TypeDescriptor(supercd);
933 } else if (md instanceof MethodDescriptor) {
934 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
936 /* If this a task descriptor we throw an error at this point */
937 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
939 if (!typetolookin.isClass())
940 throw new Error("Error with method call to "+min.getMethodName());
941 ClassDescriptor classtolookin=typetolookin.getClassDesc();
942 //System.out.println("Method name="+min.getMethodName());
944 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
945 MethodDescriptor bestmd=null;
947 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
948 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
949 /* Need correct number of parameters */
950 if (min.numArgs()!=currmd.numParameters())
952 for(int i=0; i<min.numArgs(); i++) {
953 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
956 /* Method okay so far */
960 if (typeutil.isMoreSpecific(currmd,bestmd)) {
962 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
963 throw new Error("No method is most specific");
965 /* Is this more specific than bestmd */
969 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
970 min.setMethod(bestmd);
972 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
973 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
974 /* Check whether we need to set this parameter to implied this */
975 if (!bestmd.isStatic()) {
976 if (min.getExpression()==null) {
977 ExpressionNode en=new NameNode(new NameDescriptor("this"));
978 min.setExpression(en);
979 checkExpressionNode(md, nametable, min.getExpression(), null);
985 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
986 checkExpressionNode(md, nametable, on.getLeft(), null);
987 if (on.getRight()!=null)
988 checkExpressionNode(md, nametable, on.getRight(), null);
989 TypeDescriptor ltd=on.getLeft().getType();
990 TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
991 TypeDescriptor lefttype=null;
992 TypeDescriptor righttype=null;
993 Operation op=on.getOp();
996 case Operation.LOGIC_OR:
997 case Operation.LOGIC_AND:
998 if (!(rtd.isBoolean()))
1000 on.setRightType(rtd);
1002 case Operation.LOGIC_NOT:
1003 if (!(ltd.isBoolean()))
1006 on.setLeftType(ltd);
1008 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1011 case Operation.COMP:
1012 // 5.6.2 Binary Numeric Promotion
1013 //TODO unboxing of reference objects
1016 else if (ltd.isFloat())
1018 else if (ltd.isLong())
1019 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1021 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1022 on.setLeftType(lefttype);
1023 on.setType(lefttype);
1026 case Operation.BIT_OR:
1027 case Operation.BIT_XOR:
1028 case Operation.BIT_AND:
1029 // 5.6.2 Binary Numeric Promotion
1030 //TODO unboxing of reference objects
1031 if (ltd.isDouble()||rtd.isDouble())
1033 else if (ltd.isFloat()||rtd.isFloat())
1035 else if (ltd.isLong()||rtd.isLong())
1036 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1037 // 090205 hack for boolean
1038 else if (ltd.isBoolean()||rtd.isBoolean())
1039 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1041 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1044 on.setLeftType(lefttype);
1045 on.setRightType(righttype);
1046 on.setType(lefttype);
1049 case Operation.ISAVAILABLE:
1050 if (!(ltd.isPtr())) {
1051 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1054 on.setLeftType(lefttype);
1055 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1058 case Operation.EQUAL:
1059 case Operation.NOTEQUAL:
1060 // 5.6.2 Binary Numeric Promotion
1061 //TODO unboxing of reference objects
1062 if (ltd.isBoolean()||rtd.isBoolean()) {
1063 if (!(ltd.isBoolean()&&rtd.isBoolean()))
1065 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1066 } else if (ltd.isPtr()||rtd.isPtr()) {
1067 if (!(ltd.isPtr()&&rtd.isPtr()))
1071 } else if (ltd.isDouble()||rtd.isDouble())
1072 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1073 else if (ltd.isFloat()||rtd.isFloat())
1074 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1075 else if (ltd.isLong()||rtd.isLong())
1076 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1078 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1080 on.setLeftType(lefttype);
1081 on.setRightType(righttype);
1082 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1091 // 5.6.2 Binary Numeric Promotion
1092 //TODO unboxing of reference objects
1093 if (!ltd.isNumber()||!rtd.isNumber()) {
1094 if (!ltd.isNumber())
1095 throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1096 if (!rtd.isNumber())
1097 throw new Error("Rightside is not number"+on.printNode(0));
1100 if (ltd.isDouble()||rtd.isDouble())
1101 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1102 else if (ltd.isFloat()||rtd.isFloat())
1103 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1104 else if (ltd.isLong()||rtd.isLong())
1105 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1107 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1109 on.setLeftType(lefttype);
1110 on.setRightType(righttype);
1111 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1115 if (ltd.isString()||rtd.isString()) {
1116 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
1117 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1118 NameDescriptor nd=new NameDescriptor("String");
1119 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1120 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1121 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1122 leftmin.addArgument(on.getLeft());
1124 checkExpressionNode(md, nametable, on.getLeft(), null);
1127 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1128 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1129 rightmin.addArgument(on.getRight());
1131 checkExpressionNode(md, nametable, on.getRight(), null);
1134 on.setLeftType(stringtd);
1135 on.setRightType(stringtd);
1136 on.setType(stringtd);
1141 case Operation.MULT:
1144 // 5.6.2 Binary Numeric Promotion
1145 //TODO unboxing of reference objects
1146 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1147 throw new Error("Error in "+on.printNode(0));
1149 if (ltd.isDouble()||rtd.isDouble())
1150 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1151 else if (ltd.isFloat()||rtd.isFloat())
1152 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1153 else if (ltd.isLong()||rtd.isLong())
1154 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1156 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1158 on.setLeftType(lefttype);
1159 on.setRightType(righttype);
1160 on.setType(lefttype);
1163 case Operation.LEFTSHIFT:
1164 case Operation.RIGHTSHIFT:
1165 case Operation.URIGHTSHIFT:
1166 if (!rtd.isIntegerType())
1168 //5.6.1 Unary Numeric Promotion
1169 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1170 righttype=new TypeDescriptor(TypeDescriptor.INT);
1174 on.setRightType(righttype);
1175 if (!ltd.isIntegerType())
1178 case Operation.UNARYPLUS:
1179 case Operation.UNARYMINUS:
1180 /* case Operation.POSTINC:
1181 case Operation.POSTDEC:
1182 case Operation.PREINC:
1183 case Operation.PREDEC:*/
1184 if (!ltd.isNumber())
1186 //5.6.1 Unary Numeric Promotion
1187 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1188 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1191 on.setLeftType(lefttype);
1192 on.setType(lefttype);
1196 throw new Error(op.toString());
1200 if (!typeutil.isSuperorType(td, on.getType())) {
1201 System.out.println(td);
1202 System.out.println(on.getType());
1203 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));