6 public class SemanticCheck {
14 public SemanticCheck(State state, TypeUtil tu) {
17 this.loopstack=new Stack();
18 this.toanalyze=new HashSet();
19 this.completed=new HashSet();
22 public ClassDescriptor getClass(String classname) {
23 ClassDescriptor cd=typeutil.getClass(classname, toanalyze);
28 private void checkClass(ClassDescriptor cd) {
29 if (!completed.contains(cd)) {
32 //System.out.println("Checking class: "+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();
46 //System.out.println("Checking field: "+fd);
50 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
51 MethodDescriptor md=(MethodDescriptor)method_it.next();
57 public void semanticCheck() {
58 SymbolTable classtable=state.getClassSymbolTable();
59 toanalyze.addAll(classtable.getValueSet());
60 toanalyze.addAll(state.getTaskSymbolTable().getValueSet());
63 while(!toanalyze.isEmpty()) {
64 Object obj=toanalyze.iterator().next();
65 if (obj instanceof TaskDescriptor) {
66 checkTask((TaskDescriptor)obj);
68 ClassDescriptor cd=(ClassDescriptor)obj;
71 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
72 MethodDescriptor md=(MethodDescriptor)method_it.next();
73 checkMethodBody(cd,md);
79 public void checkTypeDescriptor(TypeDescriptor td) {
82 else if (td.isClass()) {
83 String name=td.toString();
84 ClassDescriptor field_cd=getClass(name);
86 throw new Error("Undefined class "+name);
87 td.setClassDescriptor(field_cd);
89 } else if (td.isTag())
95 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
96 checkTypeDescriptor(fd.getType());
99 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
101 return; /* No constraint checks to check */
102 for(int i=0; i<ccs.size(); i++) {
103 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
105 for(int j=0; j<cc.numArgs(); j++) {
106 ExpressionNode en=cc.getArg(j);
107 checkExpressionNode(td,nametable,en,null);
112 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
114 return; /* No flag effects to check */
115 for(int i=0; i<vfe.size(); i++) {
116 FlagEffects fe=(FlagEffects) vfe.get(i);
117 String varname=fe.getName();
118 //Make sure the variable is declared as a parameter to the task
119 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
121 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
124 //Make sure it correspods to a class
125 TypeDescriptor type_d=vd.getType();
126 if (!type_d.isClass())
127 throw new Error("Cannot have non-object argument for flag_effect");
129 ClassDescriptor cd=type_d.getClassDesc();
130 for(int j=0; j<fe.numEffects(); j++) {
131 FlagEffect flag=fe.getEffect(j);
132 String name=flag.getName();
133 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
134 //Make sure the flag is declared
136 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
137 if (flag_d.getExternal())
138 throw new Error("Attempting to modify external flag: "+name);
139 flag.setFlag(flag_d);
141 for(int j=0; j<fe.numTagEffects(); j++) {
142 TagEffect tag=fe.getTagEffect(j);
143 String name=tag.getName();
145 Descriptor d=(Descriptor)nametable.get(name);
147 throw new Error("Tag descriptor "+name+" undeclared");
148 else if (!(d instanceof TagVarDescriptor))
149 throw new Error(name+" is not a tag descriptor");
150 tag.setTag((TagVarDescriptor)d);
155 public void checkTask(TaskDescriptor td) {
156 for(int i=0; i<td.numParameters(); i++) {
157 /* Check that parameter is well typed */
158 TypeDescriptor param_type=td.getParamType(i);
159 checkTypeDescriptor(param_type);
161 /* Check the parameter's flag expression is well formed */
162 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
163 if (!param_type.isClass())
164 throw new Error("Cannot have non-object argument to a task");
165 ClassDescriptor cd=param_type.getClassDesc();
167 checkFlagExpressionNode(cd, fen);
170 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
171 /* Check that the task code is valid */
172 BlockNode bn=state.getMethodBody(td);
173 checkBlockNode(td, td.getParameterTable(),bn);
176 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
178 case Kind.FlagOpNode:
180 FlagOpNode fon=(FlagOpNode)fen;
181 checkFlagExpressionNode(cd, fon.getLeft());
182 if (fon.getRight()!=null)
183 checkFlagExpressionNode(cd, fon.getRight());
189 FlagNode fn=(FlagNode)fen;
190 String name=fn.getFlagName();
191 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
193 throw new Error("Undeclared flag: "+name);
199 throw new Error("Unrecognized FlagExpressionNode");
203 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
204 /* Check return type */
205 if (!md.isConstructor())
206 if (!md.getReturnType().isVoid())
207 checkTypeDescriptor(md.getReturnType());
209 for(int i=0; i<md.numParameters(); i++) {
210 TypeDescriptor param_type=md.getParamType(i);
211 checkTypeDescriptor(param_type);
213 /* Link the naming environments */
214 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
215 md.getParameterTable().setParent(cd.getFieldTable());
217 if (!md.isStatic()) {
218 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
223 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
224 ClassDescriptor superdesc=cd.getSuperDesc();
225 if (superdesc!=null) {
226 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
227 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext();) {
228 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
229 if (md.matches(matchmd)) {
230 if (matchmd.getModifiers().isFinal()) {
231 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
236 BlockNode bn=state.getMethodBody(md);
237 checkBlockNode(md, md.getParameterTable(),bn);
240 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
241 /* Link in the naming environment */
242 bn.getVarTable().setParent(nametable);
243 for(int i=0; i<bn.size(); i++) {
244 BlockStatementNode bsn=bn.get(i);
245 checkBlockStatementNode(md, bn.getVarTable(),bsn);
249 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
251 case Kind.BlockExpressionNode:
252 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
255 case Kind.DeclarationNode:
256 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
259 case Kind.TagDeclarationNode:
260 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
263 case Kind.IfStatementNode:
264 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
268 checkLoopNode(md, nametable, (LoopNode)bsn);
271 case Kind.ReturnNode:
272 checkReturnNode(md, nametable, (ReturnNode)bsn);
275 case Kind.TaskExitNode:
276 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
279 case Kind.SubBlockNode:
280 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
283 case Kind.AtomicNode:
284 checkAtomicNode(md, nametable, (AtomicNode)bsn);
287 case Kind.ContinueBreakNode:
288 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
292 // do nothing, no semantic check for SESEs
299 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
300 checkExpressionNode(md, nametable, ben.getExpression(), null);
303 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
304 VarDescriptor vd=dn.getVarDescriptor();
305 checkTypeDescriptor(vd.getType());
306 Descriptor d=nametable.get(vd.getSymbol());
308 (d instanceof FieldDescriptor)) {
311 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
312 if (dn.getExpression()!=null)
313 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
316 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
317 TagVarDescriptor vd=dn.getTagVarDescriptor();
318 Descriptor d=nametable.get(vd.getSymbol());
320 (d instanceof FieldDescriptor)) {
323 throw new Error(vd.getSymbol()+" defined a second time");
326 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
327 checkBlockNode(md, nametable, sbn.getBlockNode());
330 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
331 checkBlockNode(md, nametable, sbn.getBlockNode());
334 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
335 if (loopstack.empty())
336 throw new Error("continue/break outside of loop");
337 LoopNode ln=(LoopNode)loopstack.peek();
341 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
342 if (d instanceof TaskDescriptor)
343 throw new Error("Illegal return appears in Task: "+d.getSymbol());
344 MethodDescriptor md=(MethodDescriptor)d;
345 if (rn.getReturnExpression()!=null)
346 if (md.getReturnType()==null)
347 throw new Error("Constructor can't return something.");
348 else if (md.getReturnType().isVoid())
349 throw new Error(md+" is void");
351 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
353 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
354 throw new Error("Need to return something for "+md);
357 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
358 if (md instanceof MethodDescriptor)
359 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
360 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
361 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
364 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
365 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
366 checkBlockNode(md, nametable, isn.getTrueBlock());
367 if (isn.getFalseBlock()!=null)
368 checkBlockNode(md, nametable, isn.getFalseBlock());
371 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
373 case Kind.AssignmentNode:
374 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
378 checkCastNode(md,nametable,(CastNode)en,td);
381 case Kind.CreateObjectNode:
382 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
385 case Kind.FieldAccessNode:
386 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
389 case Kind.ArrayAccessNode:
390 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
393 case Kind.LiteralNode:
394 checkLiteralNode(md,nametable,(LiteralNode)en,td);
397 case Kind.MethodInvokeNode:
398 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
402 checkNameNode(md,nametable,(NameNode)en,td);
406 checkOpNode(md,nametable,(OpNode)en,td);
409 case Kind.OffsetNode:
410 checkOffsetNode(md, nametable, (OffsetNode)en, new TypeDescriptor(TypeDescriptor.OFFSET));
413 case Kind.TertiaryNode:
414 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
417 case Kind.InstanceOfNode:
418 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
424 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
425 /* Get type descriptor */
426 if (cn.getType()==null) {
427 NameDescriptor typenamed=cn.getTypeName().getName();
428 String typename=typenamed.toString();
429 TypeDescriptor ntd=new TypeDescriptor(getClass(typename));
433 /* Check the type descriptor */
434 TypeDescriptor cast_type=cn.getType();
435 checkTypeDescriptor(cast_type);
439 if (!typeutil.isSuperorType(td,cast_type))
440 throw new Error("Cast node returns "+cast_type+", but need "+td);
443 ExpressionNode en=cn.getExpression();
444 checkExpressionNode(md, nametable, en, null);
445 TypeDescriptor etd=en.getType();
446 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
449 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
451 if (typeutil.isCastable(etd, cast_type))
454 /* Different branches */
455 /* TODO: change if add interfaces */
456 throw new Error("Cast will always fail\n"+cn.printNode(0));
459 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
460 ExpressionNode left=fan.getExpression();
461 checkExpressionNode(md,nametable,left,null);
462 TypeDescriptor ltd=left.getType();
463 String fieldname=fan.getFieldName();
465 FieldDescriptor fd=null;
466 if (ltd.isArray()&&fieldname.equals("length"))
467 fd=FieldDescriptor.arrayLength;
469 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
471 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
474 if (!typeutil.isSuperorType(td,fan.getType()))
475 throw new Error("Field node returns "+fan.getType()+", but need "+td);
478 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
479 ExpressionNode left=aan.getExpression();
480 checkExpressionNode(md,nametable,left,null);
482 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
483 TypeDescriptor ltd=left.getType();
486 if (!typeutil.isSuperorType(td,aan.getType()))
487 throw new Error("Field node returns "+aan.getType()+", but need "+td);
490 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
491 /* Resolve the type */
492 Object o=ln.getValue();
493 if (ln.getTypeString().equals("null")) {
494 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
495 } else if (o instanceof Integer) {
496 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
497 } else if (o instanceof Long) {
498 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
499 } else if (o instanceof Float) {
500 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
501 } else if (o instanceof Boolean) {
502 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
503 } else if (o instanceof Double) {
504 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
505 } else if (o instanceof Character) {
506 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
507 } else if (o instanceof String) {
508 ln.setType(new TypeDescriptor(getClass(TypeUtil.StringClass)));
512 if (!typeutil.isSuperorType(td,ln.getType()))
513 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
516 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
517 NameDescriptor nd=nn.getName();
518 if (nd.getBase()!=null) {
520 /* Rewrite NameNode */
521 ExpressionNode en=translateNameDescriptorintoExpression(nd);
522 nn.setExpression(en);
523 checkExpressionNode(md,nametable,en,td);
525 String varname=nd.toString();
526 Descriptor d=(Descriptor)nametable.get(varname);
528 throw new Error("Name "+varname+" undefined in: "+md);
530 if (d instanceof VarDescriptor) {
532 } else if (d instanceof FieldDescriptor) {
533 nn.setField((FieldDescriptor)d);
534 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
535 } else if (d instanceof TagVarDescriptor) {
537 } else throw new Error("Wrong type of descriptor");
539 if (!typeutil.isSuperorType(td,nn.getType()))
540 throw new Error("Field node returns "+nn.getType()+", but need "+td);
544 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
545 TypeDescriptor ltd = ofn.td;
546 //System.out.println("Testing TypeDescriptor ltd = " + ofn.td);
547 String fieldname = ofn.fieldname;
548 //System.out.println("Testing String fieldname = " + ofn.fieldname);
549 Descriptor d = (Descriptor) nameTable.get(fieldname);
550 //System.out.println("Testing Descriptor d = " + d.toString());
552 ClassDescriptor cd = null;
553 checkTypeDescriptor(ltd);
554 cd = ltd.getClassDesc();
555 ofn.setClassDesc(cd);
556 //System.out.println("Testing for ClassDescriptor cd = " + cd.toString());
558 FieldDescriptor fd=null;
559 if (ltd.isArray()&&fieldname.equals("length")) {
560 fd=FieldDescriptor.arrayLength;
562 fd=(FieldDescriptor) cd.getFieldTable().get(fieldname);
564 //System.out.println("Testing for FieldDescriptor fd = " + fd.toString());
567 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
572 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
573 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
574 checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
575 checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
578 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
579 if (td!=null&&!td.isBoolean())
580 throw new Error("Expecting type "+td+"for instanceof expression");
582 checkTypeDescriptor(tn.getExprType());
583 checkExpressionNode(md, nametable, tn.getExpr(), null);
587 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
588 boolean postinc=true;
589 if (an.getOperation().getBaseOp()==null||
590 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
591 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
595 checkExpressionNode(md, nametable, an.getSrc(),td);
596 //TODO: Need check on validity of operation here
597 if (!((an.getDest() instanceof FieldAccessNode)||
598 (an.getDest() instanceof ArrayAccessNode)||
599 (an.getDest() instanceof NameNode)))
600 throw new Error("Bad lside in "+an.printNode(0));
601 checkExpressionNode(md, nametable, an.getDest(), null);
603 /* We want parameter variables to tasks to be immutable */
604 if (md instanceof TaskDescriptor) {
605 if (an.getDest() instanceof NameNode) {
606 NameNode nn=(NameNode)an.getDest();
607 if (nn.getVar()!=null) {
608 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
609 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
614 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
616 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
617 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
618 NameDescriptor nd=new NameDescriptor("String");
619 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
621 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
622 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
623 rightmin.addArgument(an.getSrc());
625 checkExpressionNode(md, nametable, an.getSrc(), null);
629 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
630 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
634 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
636 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
637 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
638 checkBlockNode(md, nametable, ln.getBody());
641 /* Link in the initializer naming environment */
642 BlockNode bn=ln.getInitializer();
643 bn.getVarTable().setParent(nametable);
644 for(int i=0; i<bn.size(); i++) {
645 BlockStatementNode bsn=bn.get(i);
646 checkBlockStatementNode(md, bn.getVarTable(),bsn);
648 //check the condition
649 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
650 checkBlockNode(md, bn.getVarTable(), ln.getBody());
651 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
657 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
658 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
659 for(int i=0; i<con.numArgs(); i++) {
660 ExpressionNode en=con.getArg(i);
661 checkExpressionNode(md,nametable,en,null);
662 tdarray[i]=en.getType();
665 TypeDescriptor typetolookin=con.getType();
666 checkTypeDescriptor(typetolookin);
668 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
669 throw new Error(typetolookin + " isn't a "+td);
671 /* Check flag effects */
672 if (con.getFlagEffects()!=null) {
673 FlagEffects fe=con.getFlagEffects();
674 ClassDescriptor cd=typetolookin.getClassDesc();
676 for(int j=0; j<fe.numEffects(); j++) {
677 FlagEffect flag=fe.getEffect(j);
678 String name=flag.getName();
679 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
680 //Make sure the flag is declared
682 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
683 if (flag_d.getExternal())
684 throw new Error("Attempting to modify external flag: "+name);
685 flag.setFlag(flag_d);
687 for(int j=0; j<fe.numTagEffects(); j++) {
688 TagEffect tag=fe.getTagEffect(j);
689 String name=tag.getName();
691 Descriptor d=(Descriptor)nametable.get(name);
693 throw new Error("Tag descriptor "+name+" undeclared");
694 else if (!(d instanceof TagVarDescriptor))
695 throw new Error(name+" is not a tag descriptor");
696 tag.setTag((TagVarDescriptor)d);
700 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
701 throw new Error("Can't allocate primitive type:"+con.printNode(0));
703 if (!typetolookin.isArray()) {
704 //Array's don't need constructor calls
705 ClassDescriptor classtolookin=typetolookin.getClassDesc();
707 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
708 MethodDescriptor bestmd=null;
710 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
711 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
712 /* Need correct number of parameters */
713 if (con.numArgs()!=currmd.numParameters())
715 for(int i=0; i<con.numArgs(); i++) {
716 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
719 /* Local allocations can't call global allocator */
720 if (!con.isGlobal()&&currmd.isGlobal())
723 /* Method okay so far */
727 if (typeutil.isMoreSpecific(currmd,bestmd)) {
729 } else if (con.isGlobal()&&match(currmd, bestmd)) {
730 if (currmd.isGlobal()&&!bestmd.isGlobal())
732 else if (currmd.isGlobal()&&bestmd.isGlobal())
734 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
735 throw new Error("No method is most specific");
738 /* Is this more specific than bestmd */
742 throw new Error("No method found for "+con.printNode(0)+" in "+md);
743 con.setConstructor(bestmd);
748 /** Check to see if md1 is the same specificity as md2.*/
750 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
751 /* Checks if md1 is more specific than md2 */
752 if (md1.numParameters()!=md2.numParameters())
754 for(int i=0; i<md1.numParameters(); i++) {
755 if (!md2.getParamType(i).equals(md1.getParamType(i)))
758 if (!md2.getReturnType().equals(md1.getReturnType()))
761 if (!md2.getClassDesc().equals(md1.getClassDesc()))
769 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
770 String id=nd.getIdentifier();
771 NameDescriptor base=nd.getBase();
773 return new NameNode(nd);
775 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
779 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
780 /*Typecheck subexpressions
781 and get types for expressions*/
783 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
784 for(int i=0; i<min.numArgs(); i++) {
785 ExpressionNode en=min.getArg(i);
786 checkExpressionNode(md,nametable,en,null);
787 tdarray[i]=en.getType();
789 TypeDescriptor typetolookin=null;
790 if (min.getExpression()!=null) {
791 checkExpressionNode(md,nametable,min.getExpression(),null);
792 typetolookin=min.getExpression().getType();
793 } else if (min.getBaseName()!=null) {
794 String rootname=min.getBaseName().getRoot();
795 if (rootname.equals("super")) {
796 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
797 typetolookin=new TypeDescriptor(supercd);
798 } else if (nametable.get(rootname)!=null) {
799 //we have an expression
800 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
801 checkExpressionNode(md, nametable, min.getExpression(), null);
802 typetolookin=min.getExpression().getType();
805 ClassDescriptor cd=getClass(min.getBaseName().getSymbol());
807 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
808 typetolookin=new TypeDescriptor(cd);
810 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
811 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
812 min.methodid=supercd.getSymbol();
813 typetolookin=new TypeDescriptor(supercd);
814 } else if (md instanceof MethodDescriptor) {
815 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
817 /* If this a task descriptor we throw an error at this point */
818 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
820 if (!typetolookin.isClass())
821 throw new Error("Error with method call to "+min.getMethodName());
822 ClassDescriptor classtolookin=typetolookin.getClassDesc();
823 //System.out.println("Method name="+min.getMethodName());
825 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
826 MethodDescriptor bestmd=null;
828 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
829 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
830 /* Need correct number of parameters */
831 if (min.numArgs()!=currmd.numParameters())
833 for(int i=0; i<min.numArgs(); i++) {
834 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
837 /* Method okay so far */
841 if (typeutil.isMoreSpecific(currmd,bestmd)) {
843 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
844 throw new Error("No method is most specific");
846 /* Is this more specific than bestmd */
850 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
851 min.setMethod(bestmd);
853 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
854 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
855 /* Check whether we need to set this parameter to implied this */
856 if (!bestmd.isStatic()) {
857 if (min.getExpression()==null) {
858 ExpressionNode en=new NameNode(new NameDescriptor("this"));
859 min.setExpression(en);
860 checkExpressionNode(md, nametable, min.getExpression(), null);
866 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
867 checkExpressionNode(md, nametable, on.getLeft(), null);
868 if (on.getRight()!=null)
869 checkExpressionNode(md, nametable, on.getRight(), null);
870 TypeDescriptor ltd=on.getLeft().getType();
871 TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
872 TypeDescriptor lefttype=null;
873 TypeDescriptor righttype=null;
874 Operation op=on.getOp();
877 case Operation.LOGIC_OR:
878 case Operation.LOGIC_AND:
879 if (!(rtd.isBoolean()))
881 on.setRightType(rtd);
883 case Operation.LOGIC_NOT:
884 if (!(ltd.isBoolean()))
889 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
893 // 5.6.2 Binary Numeric Promotion
894 //TODO unboxing of reference objects
897 else if (ltd.isFloat())
899 else if (ltd.isLong())
900 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
902 lefttype=new TypeDescriptor(TypeDescriptor.INT);
903 on.setLeftType(lefttype);
904 on.setType(lefttype);
907 case Operation.BIT_OR:
908 case Operation.BIT_XOR:
909 case Operation.BIT_AND:
910 // 5.6.2 Binary Numeric Promotion
911 //TODO unboxing of reference objects
912 if (ltd.isDouble()||rtd.isDouble())
914 else if (ltd.isFloat()||rtd.isFloat())
916 else if (ltd.isLong()||rtd.isLong())
917 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
918 // 090205 hack for boolean
919 else if (ltd.isBoolean()||rtd.isBoolean())
920 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
922 lefttype=new TypeDescriptor(TypeDescriptor.INT);
925 on.setLeftType(lefttype);
926 on.setRightType(righttype);
927 on.setType(lefttype);
930 case Operation.ISAVAILABLE:
931 if (!(ltd.isPtr())) {
932 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
935 on.setLeftType(lefttype);
936 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
939 case Operation.EQUAL:
940 case Operation.NOTEQUAL:
941 // 5.6.2 Binary Numeric Promotion
942 //TODO unboxing of reference objects
943 if (ltd.isBoolean()||rtd.isBoolean()) {
944 if (!(ltd.isBoolean()&&rtd.isBoolean()))
946 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
947 } else if (ltd.isPtr()||rtd.isPtr()) {
948 if (!(ltd.isPtr()&&rtd.isPtr()))
952 } else if (ltd.isDouble()||rtd.isDouble())
953 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
954 else if (ltd.isFloat()||rtd.isFloat())
955 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
956 else if (ltd.isLong()||rtd.isLong())
957 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
959 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
961 on.setLeftType(lefttype);
962 on.setRightType(righttype);
963 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
972 // 5.6.2 Binary Numeric Promotion
973 //TODO unboxing of reference objects
974 if (!ltd.isNumber()||!rtd.isNumber())
977 if (ltd.isDouble()||rtd.isDouble())
978 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
979 else if (ltd.isFloat()||rtd.isFloat())
980 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
981 else if (ltd.isLong()||rtd.isLong())
982 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
984 lefttype=new TypeDescriptor(TypeDescriptor.INT);
986 on.setLeftType(lefttype);
987 on.setRightType(righttype);
988 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
992 if (ltd.isString()||rtd.isString()) {
993 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
994 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
995 NameDescriptor nd=new NameDescriptor("String");
996 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
997 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
998 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
999 leftmin.addArgument(on.getLeft());
1001 checkExpressionNode(md, nametable, on.getLeft(), null);
1004 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1005 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1006 rightmin.addArgument(on.getRight());
1008 checkExpressionNode(md, nametable, on.getRight(), null);
1011 on.setLeftType(stringtd);
1012 on.setRightType(stringtd);
1013 on.setType(stringtd);
1018 case Operation.MULT:
1021 // 5.6.2 Binary Numeric Promotion
1022 //TODO unboxing of reference objects
1023 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1024 throw new Error("Error in "+on.printNode(0));
1026 if (ltd.isDouble()||rtd.isDouble())
1027 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1028 else if (ltd.isFloat()||rtd.isFloat())
1029 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1030 else if (ltd.isLong()||rtd.isLong())
1031 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1033 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1035 on.setLeftType(lefttype);
1036 on.setRightType(righttype);
1037 on.setType(lefttype);
1040 case Operation.LEFTSHIFT:
1041 case Operation.RIGHTSHIFT:
1042 case Operation.URIGHTSHIFT:
1043 if (!rtd.isIntegerType())
1045 //5.6.1 Unary Numeric Promotion
1046 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1047 righttype=new TypeDescriptor(TypeDescriptor.INT);
1051 on.setRightType(righttype);
1052 if (!ltd.isIntegerType())
1055 case Operation.UNARYPLUS:
1056 case Operation.UNARYMINUS:
1057 /* case Operation.POSTINC:
1058 case Operation.POSTDEC:
1059 case Operation.PREINC:
1060 case Operation.PREDEC:*/
1061 if (!ltd.isNumber())
1063 //5.6.1 Unary Numeric Promotion
1064 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1065 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1068 on.setLeftType(lefttype);
1069 on.setType(lefttype);
1073 throw new Error(op.toString());
1077 if (!typeutil.isSuperorType(td, on.getType())) {
1078 System.out.println(td);
1079 System.out.println(on.getType());
1080 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));