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);
301 case Kind.SwitchStatementNode:
302 checkSwitchStatementNode(md, nametable, (SwitchStatementNode)bsn);
306 checkLoopNode(md, nametable, (LoopNode)bsn);
309 case Kind.ReturnNode:
310 checkReturnNode(md, nametable, (ReturnNode)bsn);
313 case Kind.TaskExitNode:
314 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
317 case Kind.SubBlockNode:
318 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
321 case Kind.AtomicNode:
322 checkAtomicNode(md, nametable, (AtomicNode)bsn);
325 case Kind.SynchronizedNode:
326 checkSynchronizedNode(md, nametable, (SynchronizedNode)bsn);
329 case Kind.ContinueBreakNode:
330 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
334 case Kind.GenReachNode:
335 // do nothing, no semantic check for SESEs
342 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
343 checkExpressionNode(md, nametable, ben.getExpression(), null);
346 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
347 VarDescriptor vd=dn.getVarDescriptor();
348 checkTypeDescriptor(vd.getType());
349 Descriptor d=nametable.get(vd.getSymbol());
351 (d instanceof FieldDescriptor)) {
354 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
355 if (dn.getExpression()!=null)
356 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
359 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
360 TagVarDescriptor vd=dn.getTagVarDescriptor();
361 Descriptor d=nametable.get(vd.getSymbol());
363 (d instanceof FieldDescriptor)) {
366 throw new Error(vd.getSymbol()+" defined a second time");
369 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
370 checkBlockNode(md, nametable, sbn.getBlockNode());
373 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
374 checkBlockNode(md, nametable, sbn.getBlockNode());
377 void checkSynchronizedNode(Descriptor md, SymbolTable nametable, SynchronizedNode sbn) {
378 checkBlockNode(md, nametable, sbn.getBlockNode());
379 //todo this could be Object
380 checkExpressionNode(md, nametable, sbn.getExpr(), null);
383 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
384 if (loopstack.empty())
385 throw new Error("continue/break outside of loop");
386 LoopNode ln=(LoopNode)loopstack.peek();
390 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
391 if (d instanceof TaskDescriptor)
392 throw new Error("Illegal return appears in Task: "+d.getSymbol());
393 MethodDescriptor md=(MethodDescriptor)d;
394 if (rn.getReturnExpression()!=null)
395 if (md.getReturnType()==null)
396 throw new Error("Constructor can't return something.");
397 else if (md.getReturnType().isVoid())
398 throw new Error(md+" is void");
400 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
402 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
403 throw new Error("Need to return something for "+md);
406 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
407 if (md instanceof MethodDescriptor)
408 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
409 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
410 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
413 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
414 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
415 checkBlockNode(md, nametable, isn.getTrueBlock());
416 if (isn.getFalseBlock()!=null)
417 checkBlockNode(md, nametable, isn.getFalseBlock());
420 void checkSwitchStatementNode(Descriptor md, SymbolTable nametable, SwitchStatementNode ssn) {
421 checkExpressionNode(md, nametable, ssn.getCondition(), new TypeDescriptor(TypeDescriptor.INT));
423 BlockNode sbn = ssn.getSwitchBody();
424 boolean hasdefault = false;
425 for(int i = 0; i < sbn.size(); i++) {
426 boolean containdefault = checkSwitchBlockNode(md, nametable, (SwitchBlockNode)sbn.get(i));
427 if(hasdefault && containdefault) {
428 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
430 hasdefault = containdefault;
434 boolean checkSwitchBlockNode(Descriptor md, SymbolTable nametable, SwitchBlockNode sbn) {
435 Vector<SwitchLabelNode> slnv = sbn.getSwitchConditions();
437 for(int i = 0; i < slnv.size(); i++) {
438 if(slnv.elementAt(i).isdefault) {
441 checkConstantExpressionNode(md, nametable, slnv.elementAt(i).getCondition(), new TypeDescriptor(TypeDescriptor.INT));
445 throw new Error("Error: duplicate default branch in switch-case statement in Method: " + md.getSymbol());
447 checkBlockNode(md, nametable, sbn.getSwitchBlockStatement());
448 return (defaultb > 0);
452 void checkConstantExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
454 case Kind.FieldAccessNode:
455 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
458 case Kind.LiteralNode:
459 checkLiteralNode(md,nametable,(LiteralNode)en,td);
465 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
467 case Kind.AssignmentNode:
468 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
472 checkCastNode(md,nametable,(CastNode)en,td);
475 case Kind.CreateObjectNode:
476 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
479 case Kind.FieldAccessNode:
480 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
483 case Kind.ArrayAccessNode:
484 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
487 case Kind.LiteralNode:
488 checkLiteralNode(md,nametable,(LiteralNode)en,td);
491 case Kind.MethodInvokeNode:
492 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
496 checkNameNode(md,nametable,(NameNode)en,td);
500 checkOpNode(md,nametable,(OpNode)en,td);
503 case Kind.OffsetNode:
504 checkOffsetNode(md, nametable, (OffsetNode)en, td);
507 case Kind.TertiaryNode:
508 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
511 case Kind.InstanceOfNode:
512 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
515 case Kind.ArrayInitializerNode:
516 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
522 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
523 /* Get type descriptor */
524 if (cn.getType()==null) {
525 NameDescriptor typenamed=cn.getTypeName().getName();
526 String typename=typenamed.toString();
527 TypeDescriptor ntd=new TypeDescriptor(getClass(typename));
531 /* Check the type descriptor */
532 TypeDescriptor cast_type=cn.getType();
533 checkTypeDescriptor(cast_type);
537 if (!typeutil.isSuperorType(td,cast_type))
538 throw new Error("Cast node returns "+cast_type+", but need "+td);
541 ExpressionNode en=cn.getExpression();
542 checkExpressionNode(md, nametable, en, null);
543 TypeDescriptor etd=en.getType();
544 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
547 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
549 if (typeutil.isCastable(etd, cast_type))
552 /* Different branches */
553 /* TODO: change if add interfaces */
554 throw new Error("Cast will always fail\n"+cn.printNode(0));
557 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
558 ExpressionNode left=fan.getExpression();
559 checkExpressionNode(md,nametable,left,null);
560 TypeDescriptor ltd=left.getType();
561 String fieldname=fan.getFieldName();
563 FieldDescriptor fd=null;
564 if (ltd.isArray()&&fieldname.equals("length"))
565 fd=FieldDescriptor.arrayLength;
567 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
569 // TODO add version for normal Java later
571 // check if this field is a static field
573 throw new Error("Dereference of the non-static field "+ fieldname + " in "+fan.printNode(0)+" in "+md);
578 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
580 if (fd.getType().iswrapper()) {
581 FieldAccessNode fan2=new FieldAccessNode(left, fieldname);
584 fan.fieldname="value";
586 ExpressionNode leftwr=fan.getExpression();
587 TypeDescriptor ltdwr=leftwr.getType();
588 String fieldnamewr=fan.getFieldName();
589 FieldDescriptor fdwr=(FieldDescriptor) ltdwr.getClassDesc().getFieldTable().get(fieldnamewr);
592 throw new Error("Unknown field "+fieldnamewr + " in "+fan.printNode(0)+" in "+md);
597 if (!typeutil.isSuperorType(td,fan.getType()))
598 throw new Error("Field node returns "+fan.getType()+", but need "+td);
602 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
603 ExpressionNode left=aan.getExpression();
604 checkExpressionNode(md,nametable,left,null);
606 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
607 TypeDescriptor ltd=left.getType();
608 if (ltd.dereference().iswrapper()) {
609 aan.wrappertype=((FieldDescriptor)ltd.dereference().getClassDesc().getFieldTable().get("value")).getType();
613 if (!typeutil.isSuperorType(td,aan.getType()))
614 throw new Error("Field node returns "+aan.getType()+", but need "+td);
617 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
618 /* Resolve the type */
619 Object o=ln.getValue();
620 if (ln.getTypeString().equals("null")) {
621 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
622 } else if (o instanceof Integer) {
623 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
624 } else if (o instanceof Long) {
625 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
626 } else if (o instanceof Float) {
627 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
628 } else if (o instanceof Boolean) {
629 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
630 } else if (o instanceof Double) {
631 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
632 } else if (o instanceof Character) {
633 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
634 } else if (o instanceof String) {
635 ln.setType(new TypeDescriptor(getClass(TypeUtil.StringClass)));
639 if (!typeutil.isSuperorType(td,ln.getType()))
640 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
643 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
644 NameDescriptor nd=nn.getName();
645 if (nd.getBase()!=null) {
647 /* Rewrite NameNode */
648 ExpressionNode en=translateNameDescriptorintoExpression(nd);
649 nn.setExpression(en);
650 checkExpressionNode(md,nametable,en,td);
652 String varname=nd.toString();
653 Descriptor d=(Descriptor)nametable.get(varname);
656 // TODO add version for normal Java later
657 ClassDescriptor cd = null;
658 if(((MethodDescriptor)md).isStaticBlock()) {
659 // this is a static block, all the accessed fields should be static field
660 cd = ((MethodDescriptor)md).getClassDesc();
661 SymbolTable fieldtbl = cd.getFieldTable();
662 FieldDescriptor fd=(FieldDescriptor)fieldtbl.get(varname);
663 if((fd == null) || (!fd.isStatic())){
664 // no such field in the class or it is not a static field
665 throw new Error("Name "+varname+" should not be used in static block: "+md);
667 // this is a static field
673 cd=getClass(varname);
675 // this is a class name
679 throw new Error("Name "+varname+" undefined in: "+md);
683 throw new Error("Name "+varname+" undefined in: "+md);
686 if (d instanceof VarDescriptor) {
688 } else if (d instanceof FieldDescriptor) {
689 FieldDescriptor fd=(FieldDescriptor)d;
690 if (fd.getType().iswrapper()) {
691 String id=nd.getIdentifier();
692 NameDescriptor base=nd.getBase();
693 NameNode n=new NameNode(nn.getName());
695 n.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
696 FieldAccessNode fan=new FieldAccessNode(n,"value");
697 FieldDescriptor fdval=(FieldDescriptor) fd.getType().getClassDesc().getFieldTable().get("value");
699 nn.setExpression(fan);
702 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
704 } else if (d instanceof TagVarDescriptor) {
706 } else throw new Error("Wrong type of descriptor");
708 if (!typeutil.isSuperorType(td,nn.getType()))
709 throw new Error("Field node returns "+nn.getType()+", but need "+td);
713 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
714 TypeDescriptor ltd=ofn.td;
715 checkTypeDescriptor(ltd);
717 String fieldname = ofn.fieldname;
718 FieldDescriptor fd=null;
719 if (ltd.isArray()&&fieldname.equals("length")) {
720 fd=FieldDescriptor.arrayLength;
722 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
726 checkField(ltd.getClassDesc(), fd);
729 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
732 if (!typeutil.isSuperorType(td, ofn.getType())) {
733 System.out.println(td);
734 System.out.println(ofn.getType());
735 throw new Error("Type of rside not compatible with type of lside"+ofn.printNode(0));
741 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
742 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
743 checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
744 checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
747 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
748 if (td!=null&&!td.isBoolean())
749 throw new Error("Expecting type "+td+"for instanceof expression");
751 checkTypeDescriptor(tn.getExprType());
752 checkExpressionNode(md, nametable, tn.getExpr(), null);
755 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
756 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
757 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td);
761 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
762 boolean postinc=true;
763 if (an.getOperation().getBaseOp()==null||
764 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
765 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
768 checkExpressionNode(md, nametable, an.getSrc(),td);
769 //TODO: Need check on validity of operation here
770 if (!((an.getDest() instanceof FieldAccessNode)||
771 (an.getDest() instanceof ArrayAccessNode)||
772 (an.getDest() instanceof NameNode)))
773 throw new Error("Bad lside in "+an.printNode(0));
774 checkExpressionNode(md, nametable, an.getDest(), null);
776 /* We want parameter variables to tasks to be immutable */
777 if (md instanceof TaskDescriptor) {
778 if (an.getDest() instanceof NameNode) {
779 NameNode nn=(NameNode)an.getDest();
780 if (nn.getVar()!=null) {
781 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
782 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
787 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
789 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
790 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
791 NameDescriptor nd=new NameDescriptor("String");
792 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
794 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
795 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
796 rightmin.addArgument(an.getSrc());
798 checkExpressionNode(md, nametable, an.getSrc(), null);
802 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
803 throw new Error("Type of rside ("+an.getSrc().getType().toPrettyString()+") not compatible with type of lside ("+an.getDest().getType().toPrettyString()+")"+an.printNode(0));
807 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
809 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
810 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
811 checkBlockNode(md, nametable, ln.getBody());
814 /* Link in the initializer naming environment */
815 BlockNode bn=ln.getInitializer();
816 bn.getVarTable().setParent(nametable);
817 for(int i=0; i<bn.size(); i++) {
818 BlockStatementNode bsn=bn.get(i);
819 checkBlockStatementNode(md, bn.getVarTable(),bsn);
821 //check the condition
822 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
823 checkBlockNode(md, bn.getVarTable(), ln.getBody());
824 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
830 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
831 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
832 for(int i=0; i<con.numArgs(); i++) {
833 ExpressionNode en=con.getArg(i);
834 checkExpressionNode(md,nametable,en,null);
835 tdarray[i]=en.getType();
838 TypeDescriptor typetolookin=con.getType();
839 checkTypeDescriptor(typetolookin);
841 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
842 throw new Error(typetolookin + " isn't a "+td);
844 /* Check flag effects */
845 if (con.getFlagEffects()!=null) {
846 FlagEffects fe=con.getFlagEffects();
847 ClassDescriptor cd=typetolookin.getClassDesc();
849 for(int j=0; j<fe.numEffects(); j++) {
850 FlagEffect flag=fe.getEffect(j);
851 String name=flag.getName();
852 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
853 //Make sure the flag is declared
855 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
856 if (flag_d.getExternal())
857 throw new Error("Attempting to modify external flag: "+name);
858 flag.setFlag(flag_d);
860 for(int j=0; j<fe.numTagEffects(); j++) {
861 TagEffect tag=fe.getTagEffect(j);
862 String name=tag.getName();
864 Descriptor d=(Descriptor)nametable.get(name);
866 throw new Error("Tag descriptor "+name+" undeclared");
867 else if (!(d instanceof TagVarDescriptor))
868 throw new Error(name+" is not a tag descriptor");
869 tag.setTag((TagVarDescriptor)d);
873 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
874 throw new Error("Can't allocate primitive type:"+con.printNode(0));
876 if (!typetolookin.isArray()) {
877 //Array's don't need constructor calls
878 ClassDescriptor classtolookin=typetolookin.getClassDesc();
880 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
881 MethodDescriptor bestmd=null;
883 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
884 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
885 /* Need correct number of parameters */
886 if (con.numArgs()!=currmd.numParameters())
888 for(int i=0; i<con.numArgs(); i++) {
889 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
892 /* Local allocations can't call global allocator */
893 if (!con.isGlobal()&&currmd.isGlobal())
896 /* Method okay so far */
900 if (typeutil.isMoreSpecific(currmd,bestmd)) {
902 } else if (con.isGlobal()&&match(currmd, bestmd)) {
903 if (currmd.isGlobal()&&!bestmd.isGlobal())
905 else if (currmd.isGlobal()&&bestmd.isGlobal())
907 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
908 throw new Error("No method is most specific");
911 /* Is this more specific than bestmd */
915 throw new Error("No method found for "+con.printNode(0)+" in "+md);
916 con.setConstructor(bestmd);
921 /** Check to see if md1 is the same specificity as md2.*/
923 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
924 /* Checks if md1 is more specific than md2 */
925 if (md1.numParameters()!=md2.numParameters())
927 for(int i=0; i<md1.numParameters(); i++) {
928 if (!md2.getParamType(i).equals(md1.getParamType(i)))
931 if (!md2.getReturnType().equals(md1.getReturnType()))
934 if (!md2.getClassDesc().equals(md1.getClassDesc()))
942 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
943 String id=nd.getIdentifier();
944 NameDescriptor base=nd.getBase();
946 return new NameNode(nd);
948 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
952 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
953 /*Typecheck subexpressions
954 and get types for expressions*/
956 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
957 for(int i=0; i<min.numArgs(); i++) {
958 ExpressionNode en=min.getArg(i);
959 checkExpressionNode(md,nametable,en,null);
960 tdarray[i]=en.getType();
962 TypeDescriptor typetolookin=null;
963 if (min.getExpression()!=null) {
964 checkExpressionNode(md,nametable,min.getExpression(),null);
965 typetolookin=min.getExpression().getType();
966 //if (typetolookin==null)
967 //throw new Error(md+" has null return type");
969 } else if (min.getBaseName()!=null) {
970 String rootname=min.getBaseName().getRoot();
971 if (rootname.equals("super")) {
972 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
973 typetolookin=new TypeDescriptor(supercd);
974 } else if (nametable.get(rootname)!=null) {
975 //we have an expression
976 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
977 checkExpressionNode(md, nametable, min.getExpression(), null);
978 typetolookin=min.getExpression().getType();
982 if (min.getBaseName().getSymbol().equals("System.out"))
983 cd=getClass("System");
985 cd=getClass(min.getBaseName().getSymbol());
987 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
988 typetolookin=new TypeDescriptor(cd);
990 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
991 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
992 min.methodid=supercd.getSymbol();
993 typetolookin=new TypeDescriptor(supercd);
994 } else if (md instanceof MethodDescriptor) {
995 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
997 /* If this a task descriptor we throw an error at this point */
998 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
1000 if (!typetolookin.isClass())
1001 throw new Error("Error with method call to "+min.getMethodName());
1002 ClassDescriptor classtolookin=typetolookin.getClassDesc();
1003 //System.out.println("Method name="+min.getMethodName());
1005 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
1006 MethodDescriptor bestmd=null;
1008 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
1009 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
1010 /* Need correct number of parameters */
1011 if (min.numArgs()!=currmd.numParameters())
1013 for(int i=0; i<min.numArgs(); i++) {
1014 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
1015 continue NextMethod;
1017 /* Method okay so far */
1021 if (typeutil.isMoreSpecific(currmd,bestmd)) {
1023 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
1024 throw new Error("No method is most specific");
1026 /* Is this more specific than bestmd */
1030 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
1031 min.setMethod(bestmd);
1033 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
1034 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
1035 /* Check whether we need to set this parameter to implied this */
1036 if (!bestmd.isStatic()) {
1037 if (min.getExpression()==null) {
1038 ExpressionNode en=new NameNode(new NameDescriptor("this"));
1039 min.setExpression(en);
1040 checkExpressionNode(md, nametable, min.getExpression(), null);
1046 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
1047 checkExpressionNode(md, nametable, on.getLeft(), null);
1048 if (on.getRight()!=null)
1049 checkExpressionNode(md, nametable, on.getRight(), null);
1050 TypeDescriptor ltd=on.getLeft().getType();
1051 TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
1052 TypeDescriptor lefttype=null;
1053 TypeDescriptor righttype=null;
1054 Operation op=on.getOp();
1056 switch(op.getOp()) {
1057 case Operation.LOGIC_OR:
1058 case Operation.LOGIC_AND:
1059 if (!(rtd.isBoolean()))
1061 on.setRightType(rtd);
1063 case Operation.LOGIC_NOT:
1064 if (!(ltd.isBoolean()))
1067 on.setLeftType(ltd);
1069 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1072 case Operation.COMP:
1073 // 5.6.2 Binary Numeric Promotion
1074 //TODO unboxing of reference objects
1077 else if (ltd.isFloat())
1079 else if (ltd.isLong())
1080 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1082 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1083 on.setLeftType(lefttype);
1084 on.setType(lefttype);
1087 case Operation.BIT_OR:
1088 case Operation.BIT_XOR:
1089 case Operation.BIT_AND:
1090 // 5.6.2 Binary Numeric Promotion
1091 //TODO unboxing of reference objects
1092 if (ltd.isDouble()||rtd.isDouble())
1094 else if (ltd.isFloat()||rtd.isFloat())
1096 else if (ltd.isLong()||rtd.isLong())
1097 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1098 // 090205 hack for boolean
1099 else if (ltd.isBoolean()||rtd.isBoolean())
1100 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1102 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1105 on.setLeftType(lefttype);
1106 on.setRightType(righttype);
1107 on.setType(lefttype);
1110 case Operation.ISAVAILABLE:
1111 if (!(ltd.isPtr())) {
1112 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
1115 on.setLeftType(lefttype);
1116 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1119 case Operation.EQUAL:
1120 case Operation.NOTEQUAL:
1121 // 5.6.2 Binary Numeric Promotion
1122 //TODO unboxing of reference objects
1123 if (ltd.isBoolean()||rtd.isBoolean()) {
1124 if (!(ltd.isBoolean()&&rtd.isBoolean()))
1126 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
1127 } else if (ltd.isPtr()||rtd.isPtr()) {
1128 if (!(ltd.isPtr()&&rtd.isPtr()))
1132 } else if (ltd.isDouble()||rtd.isDouble())
1133 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1134 else if (ltd.isFloat()||rtd.isFloat())
1135 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1136 else if (ltd.isLong()||rtd.isLong())
1137 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1139 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
1141 on.setLeftType(lefttype);
1142 on.setRightType(righttype);
1143 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1152 // 5.6.2 Binary Numeric Promotion
1153 //TODO unboxing of reference objects
1154 if (!ltd.isNumber()||!rtd.isNumber()) {
1155 if (!ltd.isNumber())
1156 throw new Error("Leftside is not number"+on.printNode(0)+"type="+ltd.toPrettyString());
1157 if (!rtd.isNumber())
1158 throw new Error("Rightside is not number"+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(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1176 if (ltd.isString()||rtd.isString()) {
1177 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
1178 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1179 NameDescriptor nd=new NameDescriptor("String");
1180 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1181 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1182 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1183 leftmin.addArgument(on.getLeft());
1185 checkExpressionNode(md, nametable, on.getLeft(), null);
1188 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1189 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1190 rightmin.addArgument(on.getRight());
1192 checkExpressionNode(md, nametable, on.getRight(), null);
1195 on.setLeftType(stringtd);
1196 on.setRightType(stringtd);
1197 on.setType(stringtd);
1202 case Operation.MULT:
1205 // 5.6.2 Binary Numeric Promotion
1206 //TODO unboxing of reference objects
1207 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1208 throw new Error("Error in "+on.printNode(0));
1210 if (ltd.isDouble()||rtd.isDouble())
1211 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1212 else if (ltd.isFloat()||rtd.isFloat())
1213 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1214 else if (ltd.isLong()||rtd.isLong())
1215 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1217 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1219 on.setLeftType(lefttype);
1220 on.setRightType(righttype);
1221 on.setType(lefttype);
1224 case Operation.LEFTSHIFT:
1225 case Operation.RIGHTSHIFT:
1226 case Operation.URIGHTSHIFT:
1227 if (!rtd.isIntegerType())
1229 //5.6.1 Unary Numeric Promotion
1230 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1231 righttype=new TypeDescriptor(TypeDescriptor.INT);
1235 on.setRightType(righttype);
1236 if (!ltd.isIntegerType())
1239 case Operation.UNARYPLUS:
1240 case Operation.UNARYMINUS:
1241 /* case Operation.POSTINC:
1242 case Operation.POSTDEC:
1243 case Operation.PREINC:
1244 case Operation.PREDEC:*/
1245 if (!ltd.isNumber())
1247 //5.6.1 Unary Numeric Promotion
1248 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1249 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1252 on.setLeftType(lefttype);
1253 on.setType(lefttype);
1257 throw new Error(op.toString());
1261 if (!typeutil.isSuperorType(td, on.getType())) {
1262 System.out.println(td);
1263 System.out.println(on.getType());
1264 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));