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());
42 // TODO add version for normal Java later
43 // Link together Field, Method tables do classes inherit these from
44 // their ancestor interfaces
45 Vector<String> sifv = cd.getSuperInterface();
46 for(int i = 0; i < sifv.size(); i++) {
47 ClassDescriptor superif = getClass(sifv.elementAt(i));
48 cd.addSuperInterfaces(superif);
49 cd.getFieldTable().addParentIF(superif.getFieldTable());
50 cd.getMethodTable().addParentIF(superif.getMethodTable());
55 /* Check to see that fields are well typed */
56 for(Iterator field_it=cd.getFields(); field_it.hasNext();) {
57 FieldDescriptor fd=(FieldDescriptor)field_it.next();
61 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
62 MethodDescriptor md=(MethodDescriptor)method_it.next();
68 public void semanticCheck() {
69 SymbolTable classtable=state.getClassSymbolTable();
70 toanalyze.addAll(classtable.getValueSet());
71 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
74 while(!toanalyze.isEmpty()) {
75 Object obj=toanalyze.iterator().next();
76 if (obj instanceof TaskDescriptor) {
77 toanalyze.remove(obj);
78 TaskDescriptor td=(TaskDescriptor)obj;
82 System.out.println( "Error in "+td );
86 ClassDescriptor cd=(ClassDescriptor)obj;
88 //need to initialize typeutil object here...only place we can
89 //get class descriptors without first calling getclass
90 getClass(cd.getSymbol());
91 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
92 MethodDescriptor md=(MethodDescriptor)method_it.next();
94 checkMethodBody(cd,md);
96 System.out.println( "Error in "+md );
104 public void checkTypeDescriptor(TypeDescriptor td) {
105 if (td.isPrimitive())
107 else if (td.isClass()) {
108 String name=td.toString();
109 ClassDescriptor field_cd=getClass(name);
111 throw new Error("Undefined class "+name);
112 td.setClassDescriptor(field_cd);
114 } else if (td.isTag())
120 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
121 checkTypeDescriptor(fd.getType());
124 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
126 return; /* No constraint checks to check */
127 for(int i=0; i<ccs.size(); i++) {
128 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
130 for(int j=0; j<cc.numArgs(); j++) {
131 ExpressionNode en=cc.getArg(j);
132 checkExpressionNode(td,nametable,en,null);
137 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
139 return; /* No flag effects to check */
140 for(int i=0; i<vfe.size(); i++) {
141 FlagEffects fe=(FlagEffects) vfe.get(i);
142 String varname=fe.getName();
143 //Make sure the variable is declared as a parameter to the task
144 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
146 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
149 //Make sure it correspods to a class
150 TypeDescriptor type_d=vd.getType();
151 if (!type_d.isClass())
152 throw new Error("Cannot have non-object argument for flag_effect");
154 ClassDescriptor cd=type_d.getClassDesc();
155 for(int j=0; j<fe.numEffects(); j++) {
156 FlagEffect flag=fe.getEffect(j);
157 String name=flag.getName();
158 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
159 //Make sure the flag is declared
161 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
162 if (flag_d.getExternal())
163 throw new Error("Attempting to modify external flag: "+name);
164 flag.setFlag(flag_d);
166 for(int j=0; j<fe.numTagEffects(); j++) {
167 TagEffect tag=fe.getTagEffect(j);
168 String name=tag.getName();
170 Descriptor d=(Descriptor)nametable.get(name);
172 throw new Error("Tag descriptor "+name+" undeclared");
173 else if (!(d instanceof TagVarDescriptor))
174 throw new Error(name+" is not a tag descriptor");
175 tag.setTag((TagVarDescriptor)d);
180 public void checkTask(TaskDescriptor td) {
181 for(int i=0; i<td.numParameters(); i++) {
182 /* Check that parameter is well typed */
183 TypeDescriptor param_type=td.getParamType(i);
184 checkTypeDescriptor(param_type);
186 /* Check the parameter's flag expression is well formed */
187 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
188 if (!param_type.isClass())
189 throw new Error("Cannot have non-object argument to a task");
190 ClassDescriptor cd=param_type.getClassDesc();
192 checkFlagExpressionNode(cd, fen);
195 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
196 /* Check that the task code is valid */
197 BlockNode bn=state.getMethodBody(td);
198 checkBlockNode(td, td.getParameterTable(),bn);
201 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
203 case Kind.FlagOpNode:
205 FlagOpNode fon=(FlagOpNode)fen;
206 checkFlagExpressionNode(cd, fon.getLeft());
207 if (fon.getRight()!=null)
208 checkFlagExpressionNode(cd, fon.getRight());
214 FlagNode fn=(FlagNode)fen;
215 String name=fn.getFlagName();
216 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
218 throw new Error("Undeclared flag: "+name);
224 throw new Error("Unrecognized FlagExpressionNode");
228 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
230 // TODO add version for normal Java later
231 /* Check for abstract methods */
232 if(md.isAbstract()) {
233 if(!cd.isAbstract() && !cd.isInterface()) {
234 throw new Error("Error! The non-abstract Class " + cd.getSymbol() + " contains an abstract method " + md.getSymbol());
238 /* Check return type */
239 if (!md.isConstructor())
240 if (!md.getReturnType().isVoid())
241 checkTypeDescriptor(md.getReturnType());
243 for(int i=0; i<md.numParameters(); i++) {
244 TypeDescriptor param_type=md.getParamType(i);
245 checkTypeDescriptor(param_type);
247 /* Link the naming environments */
248 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
249 md.getParameterTable().setParent(cd.getFieldTable());
251 if (!md.isStatic()) {
252 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
257 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
258 ClassDescriptor superdesc=cd.getSuperDesc();
259 if (superdesc!=null) {
260 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
261 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext();) {
262 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
263 if (md.matches(matchmd)) {
264 if (matchmd.getModifiers().isFinal()) {
265 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
270 BlockNode bn=state.getMethodBody(md);
271 checkBlockNode(md, md.getParameterTable(),bn);
274 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
275 /* Link in the naming environment */
276 bn.getVarTable().setParent(nametable);
277 for(int i=0; i<bn.size(); i++) {
278 BlockStatementNode bsn=bn.get(i);
279 checkBlockStatementNode(md, bn.getVarTable(),bsn);
283 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
285 case Kind.BlockExpressionNode:
286 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
289 case Kind.DeclarationNode:
290 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
293 case Kind.TagDeclarationNode:
294 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
297 case Kind.IfStatementNode:
298 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
302 checkLoopNode(md, nametable, (LoopNode)bsn);
305 case Kind.ReturnNode:
306 checkReturnNode(md, nametable, (ReturnNode)bsn);
309 case Kind.TaskExitNode:
310 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
313 case Kind.SubBlockNode:
314 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
317 case Kind.AtomicNode:
318 checkAtomicNode(md, nametable, (AtomicNode)bsn);
321 case Kind.SynchronizedNode:
322 checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
325 case Kind.ContinueBreakNode:
326 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
330 case Kind.GenReachNode:
331 // do nothing, no semantic check for SESEs
338 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
339 checkExpressionNode(md, nametable, ben.getExpression(), null);
342 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
343 VarDescriptor vd=dn.getVarDescriptor();
344 checkTypeDescriptor(vd.getType());
345 Descriptor d=nametable.get(vd.getSymbol());
347 (d instanceof FieldDescriptor)) {
350 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
351 if (dn.getExpression()!=null)
352 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
355 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
356 TagVarDescriptor vd=dn.getTagVarDescriptor();
357 Descriptor d=nametable.get(vd.getSymbol());
359 (d instanceof FieldDescriptor)) {
362 throw new Error(vd.getSymbol()+" defined a second time");
365 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
366 checkBlockNode(md, nametable, sbn.getBlockNode());
369 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
370 checkBlockNode(md, nametable, sbn.getBlockNode());
373 void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
374 checkBlockNode(md, nametable, sbn.getBlockNode());
375 //todo this could be Object
376 checkExpressionNode(md, nametable, sbn.getExpr(), null);
379 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
380 if (loopstack.empty())
381 throw new Error("continue/break outside of loop");
382 LoopNode ln=(LoopNode)loopstack.peek();
386 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
387 if (d instanceof TaskDescriptor)
388 throw new Error("Illegal return appears in Task: "+d.getSymbol());
389 MethodDescriptor md=(MethodDescriptor)d;
390 if (rn.getReturnExpression()!=null)
391 if (md.getReturnType()==null)
392 throw new Error("Constructor can't return something.");
393 else if (md.getReturnType().isVoid())
394 throw new Error(md+" is void");
396 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
398 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
399 throw new Error("Need to return something for "+md);
402 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
403 if (md instanceof MethodDescriptor)
404 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
405 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
406 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
409 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
410 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
411 checkBlockNode(md, nametable, isn.getTrueBlock());
412 if (isn.getFalseBlock()!=null)
413 checkBlockNode(md, nametable, isn.getFalseBlock());
416 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
418 case Kind.AssignmentNode:
419 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
423 checkCastNode(md,nametable,(CastNode)en,td);
426 case Kind.CreateObjectNode:
427 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
430 case Kind.FieldAccessNode:
431 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
434 case Kind.ArrayAccessNode:
435 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
438 case Kind.LiteralNode:
439 checkLiteralNode(md,nametable,(LiteralNode)en,td);
442 case Kind.MethodInvokeNode:
443 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
447 checkNameNode(md,nametable,(NameNode)en,td);
451 checkOpNode(md,nametable,(OpNode)en,td);
454 case Kind.OffsetNode:
455 checkOffsetNode(md, nametable, (OffsetNode)en, td);
458 case Kind.TertiaryNode:
459 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
462 case Kind.InstanceOfNode:
463 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
466 case Kind.ArrayInitializerNode:
467 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
473 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
474 /* Get type descriptor */
475 if (cn.getType()==null) {
476 NameDescriptor typenamed=cn.getTypeName().getName();
477 String typename=typenamed.toString();
478 TypeDescriptor ntd=new TypeDescriptor(getClass(typename));
482 /* Check the type descriptor */
483 TypeDescriptor cast_type=cn.getType();
484 checkTypeDescriptor(cast_type);
488 if (!typeutil.isSuperorType(td,cast_type))
489 throw new Error("Cast node returns "+cast_type+", but need "+td);
492 ExpressionNode en=cn.getExpression();
493 checkExpressionNode(md, nametable, en, null);
494 TypeDescriptor etd=en.getType();
495 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
498 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
500 if (typeutil.isCastable(etd, cast_type))
503 /* Different branches */
504 /* TODO: change if add interfaces */
505 throw new Error("Cast will always fail\n"+cn.printNode(0));
508 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
509 ExpressionNode left=fan.getExpression();
510 checkExpressionNode(md,nametable,left,null);
511 TypeDescriptor ltd=left.getType();
512 String fieldname=fan.getFieldName();
514 FieldDescriptor fd=null;
515 if (ltd.isArray()&&fieldname.equals("length"))
516 fd=FieldDescriptor.arrayLength;
518 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
520 // TODO add version for normal Java later
522 // check if this field is a static field
524 throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
529 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
531 if (fd.getType().iswrapper()) {
532 FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
535 fan.fieldname="value";
537 ExpressionNode leftwr=fan.getExpression();
538 TypeDescriptor ltdwr=leftwr.getType();
539 String fieldnamewr=fan.getFieldName();
540 FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
543 throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
548 if (!typeutil.isSuperorType(td,fan.getType()))
549 throw new Error("Field node returns "+fan.getType()+", but need "+td);
553 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
554 ExpressionNode left=aan.getExpression();
555 checkExpressionNode(md,nametable,left,null);
557 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
558 TypeDescriptor ltd=left.getType();
559 if (ltd.dereference().iswrapper()) {
560 aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
564 if (!typeutil.isSuperorType(td,aan.getType()))
565 throw new Error("Field node returns "+aan.getType()+", but need "+td);
568 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
569 /* Resolve the type */
570 Object o=ln.getValue();
571 if (ln.getTypeString().equals("null")) {
572 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
573 } else if (o instanceof Integer) {
574 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
575 } else if (o instanceof Long) {
576 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
577 } else if (o instanceof Float) {
578 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
579 } else if (o instanceof Boolean) {
580 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
581 } else if (o instanceof Double) {
582 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
583 } else if (o instanceof Character) {
584 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
585 } else if (o instanceof String) {
586 ln.setType(new TypeDescriptor(getClass(TypeUtil.StringClass)));
590 if (!typeutil.isSuperorType(td,ln.getType()))
591 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
594 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
595 NameDescriptor nd=nn.getName();
596 if (nd.getBase()!=null) {
598 /* Rewrite NameNode */
599 ExpressionNode en=translateNameDescriptorintoExpression(nd);
600 nn.setExpression(en);
601 checkExpressionNode(md,nametable,en,td);
603 String varname=nd.toString();
604 Descriptor d=(Descriptor)nametable.get(varname);
607 // TODO add version for normal Java later
608 ClassDescriptor cd = null;
609 if(((MethodDescriptor)md).isStaticBlock()) {
610 // this is a static block, all the accessed fields should be static field
611 cd = ((MethodDescriptor)md).getClassDesc();
612 SymbolTable fieldtbl = cd.getFieldTable();
613 FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
614 if((fd == null) || (!fd.isStatic())){
615 // no such field in the class or it is not a static field
616 throw new Error("Name "+varname+" should not be used in static block: "+md);
618 // this is a static field
624 cd=getClass(varname);
626 // this is a class name
630 throw new Error("Name "+varname+" undefined in: "+md);
634 throw new Error("Name "+varname+" undefined in: "+md);
637 if (d instanceof VarDescriptor) {
639 } else if (d instanceof FieldDescriptor) {
640 FieldDescriptor fd=(FieldDescriptor)d;
641 if (fd.getType().iswrapper()) {
642 String id=nd.getIdentifier();
643 NameDescriptor base=nd.getBase();
644 NameNode n=new NameNode(nn.getName());
646 n.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
647 FieldAccessNode fan=new FieldAccessNode(n,"value");
648 FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
650 nn.setExpression(fan);
653 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
655 } else if (d instanceof TagVarDescriptor) {
657 } else throw new Error("Wrong type of descriptor");
659 if (!typeutil.isSuperorType(td,nn.getType()))
660 throw new Error("Field node returns "+nn.getType()+", but need "+td);
664 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
665 TypeDescriptor ltd=ofn.td;
666 checkTypeDescriptor(ltd);
668 String fieldname = ofn.fieldname;
669 FieldDescriptor fd=null;
670 if (ltd.isArray()&&fieldname.equals("length")) {
671 fd=FieldDescriptor.arrayLength;
673 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
677 checkField(ltd.getClassDesc(), fd);
680 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
683 if (!typeutil.isSuperorType(td, ofn.getType())) {
684 System.out.println(td);
685 System.out.println(ofn.getType());
686 throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
692 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
693 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
694 checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
695 checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
698 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
699 if (td!=null&&!td.isBoolean())
700 throw new Error("Expecting type "+td+"for instanceof expression");
702 checkTypeDescriptor(tn.getExprType());
703 checkExpressionNode(md, nametable, tn.getExpr(), null);
706 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
707 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
708 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td);
712 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
713 boolean postinc=true;
714 if (an.getOperation().getBaseOp()==null||
715 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
716 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
719 checkExpressionNode(md, nametable, an.getSrc(),td);
720 //TODO: Need check on validity of operation here
721 if (!((an.getDest() instanceof FieldAccessNode)||
722 (an.getDest() instanceof ArrayAccessNode)||
723 (an.getDest() instanceof NameNode)))
724 throw new Error("Bad lside in "+an.printNode(0));
725 checkExpressionNode(md, nametable, an.getDest(), null);
727 /* We want parameter variables to tasks to be immutable */
728 if (md instanceof TaskDescriptor) {
729 if (an.getDest() instanceof NameNode) {
730 NameNode nn=(NameNode)an.getDest();
731 if (nn.getVar()!=null) {
732 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
733 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
738 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
740 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
741 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
742 NameDescriptor nd=new NameDescriptor("String");
743 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
745 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
746 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
747 rightmin.addArgument(an.getSrc());
749 checkExpressionNode(md, nametable, an.getSrc(), null);
753 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
754 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
758 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
760 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
761 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
762 checkBlockNode(md, nametable, ln.getBody());
765 /* Link in the initializer naming environment */
766 BlockNode bn=ln.getInitializer();
767 bn.getVarTable().setParent(nametable);
768 for(int i=0; i<bn.size(); i++) {
769 BlockStatementNode bsn=bn.get(i);
770 checkBlockStatementNode(md, bn.getVarTable(),bsn);
772 //check the condition
773 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
774 checkBlockNode(md, bn.getVarTable(), ln.getBody());
775 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
781 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
782 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
783 for(int i=0; i<con.numArgs(); i++) {
784 ExpressionNode en=con.getArg(i);
785 checkExpressionNode(md,nametable,en,null);
786 tdarray[i]=en.getType();
789 TypeDescriptor typetolookin=con.getType();
790 checkTypeDescriptor(typetolookin);
792 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
793 throw new Error(typetolookin + " isn't a "+td);
795 /* Check flag effects */
796 if (con.getFlagEffects()!=null) {
797 FlagEffects fe=con.getFlagEffects();
798 ClassDescriptor cd=typetolookin.getClassDesc();
800 for(int j=0; j<fe.numEffects(); j++) {
801 FlagEffect flag=fe.getEffect(j);
802 String name=flag.getName();
803 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
804 //Make sure the flag is declared
806 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
807 if (flag_d.getExternal())
808 throw new Error("Attempting to modify external flag: "+name);
809 flag.setFlag(flag_d);
811 for(int j=0; j<fe.numTagEffects(); j++) {
812 TagEffect tag=fe.getTagEffect(j);
813 String name=tag.getName();
815 Descriptor d=(Descriptor)nametable.get(name);
817 throw new Error("Tag descriptor "+name+" undeclared");
818 else if (!(d instanceof TagVarDescriptor))
819 throw new Error(name+" is not a tag descriptor");
820 tag.setTag((TagVarDescriptor)d);
824 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
825 throw new Error("Can't allocate primitive type:"+con.printNode(0));
827 if (!typetolookin.isArray()) {
828 //Array's don't need constructor calls
829 ClassDescriptor classtolookin=typetolookin.getClassDesc();
831 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
832 MethodDescriptor bestmd=null;
834 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
835 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
836 /* Need correct number of parameters */
837 if (con.numArgs()!=currmd.numParameters())
839 for(int i=0; i<con.numArgs(); i++) {
840 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
843 /* Local allocations can't call global allocator */
844 if (!con.isGlobal()&&currmd.isGlobal())
847 /* Method okay so far */
851 if (typeutil.isMoreSpecific(currmd,bestmd)) {
853 } else if (con.isGlobal()&&match(currmd, bestmd)) {
854 if (currmd.isGlobal()&&!bestmd.isGlobal())
856 else if (currmd.isGlobal()&&bestmd.isGlobal())
858 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
859 throw new Error("No method is most specific");
862 /* Is this more specific than bestmd */
866 throw new Error("No method found for "+con.printNode(0)+" in "+md);
867 con.setConstructor(bestmd);
872 /** Check to see if md1 is the same specificity as md2.*/
874 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
875 /* Checks if md1 is more specific than md2 */
876 if (md1.numParameters()!=md2.numParameters())
878 for(int i=0; i<md1.numParameters(); i++) {
879 if (!md2.getParamType(i).equals(md1.getParamType(i)))
882 if (!md2.getReturnType().equals(md1.getReturnType()))
885 if (!md2.getClassDesc().equals(md1.getClassDesc()))
893 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
894 String id=nd.getIdentifier();
895 NameDescriptor base=nd.getBase();
897 return new NameNode(nd);
899 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
903 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
904 /*Typecheck subexpressions
905 and get types for expressions*/
907 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
908 for(int i=0; i<min.numArgs(); i++) {
909 ExpressionNode en=min.getArg(i);
910 checkExpressionNode(md,nametable,en,null);
911 tdarray[i]=en.getType();
913 TypeDescriptor typetolookin=null;
914 if (min.getExpression()!=null) {
915 checkExpressionNode(md,nametable,min.getExpression(),null);
916 typetolookin=min.getExpression().getType();
917 //if (typetolookin==null)
918 //throw new Error(md+" has null return type");
920 } else if (min.getBaseName()!=null) {
921 String rootname=min.getBaseName().getRoot();
922 if (rootname.equals("super")) {
923 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
924 typetolookin=new TypeDescriptor(supercd);
925 } else if (nametable.get(rootname)!=null) {
926 //we have an expression
927 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
928 checkExpressionNode(md, nametable, min.getExpression(), null);
929 typetolookin=min.getExpression().getType();
933 if (min.getBaseName().getSymbol().equals("System.out"))
934 cd=getClass("System");
936 cd=getClass(min.getBaseName().getSymbol());
938 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
939 typetolookin=new TypeDescriptor(cd);
941 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
942 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
943 min.methodid=supercd.getSymbol();
944 typetolookin=new TypeDescriptor(supercd);
945 } else if (md instanceof MethodDescriptor) {
946 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
948 /* If this a task descriptor we throw an error at this point */
949 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
951 if (!typetolookin.isClass())
952 throw new Error("Error with method call to "+min.getMethodName());
953 ClassDescriptor classtolookin=typetolookin.getClassDesc();
954 //System.out.println("Method name="+min.getMethodName());
956 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
957 MethodDescriptor bestmd=null;
959 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
960 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
961 /* Need correct number of parameters */
962 if (min.numArgs()!=currmd.numParameters())
964 for(int i=0; i<min.numArgs(); i++) {
965 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
968 /* Method okay so far */
972 if (typeutil.isMoreSpecific(currmd,bestmd)) {
974 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
975 throw new Error("No method is most specific");
977 /* Is this more specific than bestmd */
981 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
982 min.setMethod(bestmd);
984 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
985 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
986 /* Check whether we need to set this parameter to implied this */
987 if (!bestmd.isStatic()) {
988 if (min.getExpression()==null) {
989 ExpressionNode en=new NameNode(new NameDescriptor("this"));
990 min.setExpression(en);
991 checkExpressionNode(md, nametable, min.getExpression(), null);
997 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
998 checkExpressionNode(md, nametable, on.getLeft(), null);
999 if (on.getRight()!=null)
1000 checkExpressionNode(md, nametable, on.getRight(), null);
1001 TypeDescriptor ltd=on.getLeft().getType();
1002 TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
1003 TypeDescriptor lefttype=null;
1004 TypeDescriptor righttype=null;
1005 Operation op=on.getOp();
1007 switch(op.getOp()) {
1008 case Operation.LOGIC_OR:
1009 case Operation.LOGIC_AND:
1010 if (!(rtd.isBoolean()))
1012 on.setRightType(rtd);
1014 case Operation.LOGIC_NOT:
1015 if (!(ltd.isBoolean()))
1018 on.setLeftType(ltd);
1020 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1023 case Operation.COMP:
1024 // 5.6.2 Binary Numeric Promotion
1025 //TODO unboxing of reference objects
1028 else if (ltd.isFloat())
1030 else if (ltd.isLong())
1031 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1033 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1034 on.setLeftType(lefttype);
1035 on.setType(lefttype);
1038 case Operation.BIT_OR:
1039 case Operation.BIT_XOR:
1040 case Operation.BIT_AND:
1041 // 5.6.2 Binary Numeric Promotion
1042 //TODO unboxing of reference objects
1043 if (ltd.isDouble()||rtd.isDouble())
1045 else if (ltd.isFloat()||rtd.isFloat())
1047 else if (ltd.isLong()||rtd.isLong())
1048 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1049 // 090205 hack for boolean
1050 else if (ltd.isBoolean()||rtd.isBoolean())
1051 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1053 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1056 on.setLeftType(lefttype);
1057 on.setRightType(righttype);
1058 on.setType(lefttype);
1061 case Operation.ISAVAILABLE:
1062 if (!(ltd.isPtr())) {
1063 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1066 on.setLeftType(lefttype);
1067 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1070 case Operation.EQUAL:
1071 case Operation.NOTEQUAL:
1072 // 5.6.2 Binary Numeric Promotion
1073 //TODO unboxing of reference objects
1074 if (ltd.isBoolean()||rtd.isBoolean()) {
1075 if (!(ltd.isBoolean()&&rtd.isBoolean()))
1077 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1078 } else if (ltd.isPtr()||rtd.isPtr()) {
1079 if (!(ltd.isPtr()&&rtd.isPtr()))
1083 } else if (ltd.isDouble()||rtd.isDouble())
1084 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1085 else if (ltd.isFloat()||rtd.isFloat())
1086 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1087 else if (ltd.isLong()||rtd.isLong())
1088 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1090 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1092 on.setLeftType(lefttype);
1093 on.setRightType(righttype);
1094 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1103 // 5.6.2 Binary Numeric Promotion
1104 //TODO unboxing of reference objects
1105 if (!ltd.isNumber()||!rtd.isNumber()) {
1106 if (!ltd.isNumber())
1107 throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1108 if (!rtd.isNumber())
1109 throw new Error("Rightside is not number"+on.printNode(0));
1112 if (ltd.isDouble()||rtd.isDouble())
1113 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1114 else if (ltd.isFloat()||rtd.isFloat())
1115 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1116 else if (ltd.isLong()||rtd.isLong())
1117 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1119 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1121 on.setLeftType(lefttype);
1122 on.setRightType(righttype);
1123 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1127 if (ltd.isString()||rtd.isString()) {
1128 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
1129 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1130 NameDescriptor nd=new NameDescriptor("String");
1131 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1132 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1133 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1134 leftmin.addArgument(on.getLeft());
1136 checkExpressionNode(md, nametable, on.getLeft(), null);
1139 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1140 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1141 rightmin.addArgument(on.getRight());
1143 checkExpressionNode(md, nametable, on.getRight(), null);
1146 on.setLeftType(stringtd);
1147 on.setRightType(stringtd);
1148 on.setType(stringtd);
1153 case Operation.MULT:
1156 // 5.6.2 Binary Numeric Promotion
1157 //TODO unboxing of reference objects
1158 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1159 throw new Error("Error in "+on.printNode(0));
1161 if (ltd.isDouble()||rtd.isDouble())
1162 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1163 else if (ltd.isFloat()||rtd.isFloat())
1164 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1165 else if (ltd.isLong()||rtd.isLong())
1166 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1168 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1170 on.setLeftType(lefttype);
1171 on.setRightType(righttype);
1172 on.setType(lefttype);
1175 case Operation.LEFTSHIFT:
1176 case Operation.RIGHTSHIFT:
1177 case Operation.URIGHTSHIFT:
1178 if (!rtd.isIntegerType())
1180 //5.6.1 Unary Numeric Promotion
1181 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1182 righttype=new TypeDescriptor(TypeDescriptor.INT);
1186 on.setRightType(righttype);
1187 if (!ltd.isIntegerType())
1190 case Operation.UNARYPLUS:
1191 case Operation.UNARYMINUS:
1192 /* case Operation.POSTINC:
1193 case Operation.POSTDEC:
1194 case Operation.PREINC:
1195 case Operation.PREDEC:*/
1196 if (!ltd.isNumber())
1198 //5.6.1 Unary Numeric Promotion
1199 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1200 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1203 on.setLeftType(lefttype);
1204 on.setType(lefttype);
1208 throw new Error(op.toString());
1212 if (!typeutil.isSuperorType(td, on.getType())) {
1213 System.out.println(td);
1214 System.out.println(on.getType());
1215 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));