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 toanalyze.remove(obj);
67 checkTask((TaskDescriptor)obj);
69 ClassDescriptor cd=(ClassDescriptor)obj;
72 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
73 MethodDescriptor md=(MethodDescriptor)method_it.next();
74 checkMethodBody(cd,md);
80 public void checkTypeDescriptor(TypeDescriptor td) {
83 else if (td.isClass()) {
84 String name=td.toString();
85 ClassDescriptor field_cd=getClass(name);
87 throw new Error("Undefined class "+name);
88 td.setClassDescriptor(field_cd);
90 } else if (td.isTag())
96 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
97 checkTypeDescriptor(fd.getType());
100 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
102 return; /* No constraint checks to check */
103 for(int i=0; i<ccs.size(); i++) {
104 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
106 for(int j=0; j<cc.numArgs(); j++) {
107 ExpressionNode en=cc.getArg(j);
108 checkExpressionNode(td,nametable,en,null);
113 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
115 return; /* No flag effects to check */
116 for(int i=0; i<vfe.size(); i++) {
117 FlagEffects fe=(FlagEffects) vfe.get(i);
118 String varname=fe.getName();
119 //Make sure the variable is declared as a parameter to the task
120 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
122 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
125 //Make sure it correspods to a class
126 TypeDescriptor type_d=vd.getType();
127 if (!type_d.isClass())
128 throw new Error("Cannot have non-object argument for flag_effect");
130 ClassDescriptor cd=type_d.getClassDesc();
131 for(int j=0; j<fe.numEffects(); j++) {
132 FlagEffect flag=fe.getEffect(j);
133 String name=flag.getName();
134 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
135 //Make sure the flag is declared
137 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
138 if (flag_d.getExternal())
139 throw new Error("Attempting to modify external flag: "+name);
140 flag.setFlag(flag_d);
142 for(int j=0; j<fe.numTagEffects(); j++) {
143 TagEffect tag=fe.getTagEffect(j);
144 String name=tag.getName();
146 Descriptor d=(Descriptor)nametable.get(name);
148 throw new Error("Tag descriptor "+name+" undeclared");
149 else if (!(d instanceof TagVarDescriptor))
150 throw new Error(name+" is not a tag descriptor");
151 tag.setTag((TagVarDescriptor)d);
156 public void checkTask(TaskDescriptor td) {
157 for(int i=0; i<td.numParameters(); i++) {
158 /* Check that parameter is well typed */
159 TypeDescriptor param_type=td.getParamType(i);
160 checkTypeDescriptor(param_type);
162 /* Check the parameter's flag expression is well formed */
163 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
164 if (!param_type.isClass())
165 throw new Error("Cannot have non-object argument to a task");
166 ClassDescriptor cd=param_type.getClassDesc();
168 checkFlagExpressionNode(cd, fen);
171 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
172 /* Check that the task code is valid */
173 BlockNode bn=state.getMethodBody(td);
174 checkBlockNode(td, td.getParameterTable(),bn);
177 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
179 case Kind.FlagOpNode:
181 FlagOpNode fon=(FlagOpNode)fen;
182 checkFlagExpressionNode(cd, fon.getLeft());
183 if (fon.getRight()!=null)
184 checkFlagExpressionNode(cd, fon.getRight());
190 FlagNode fn=(FlagNode)fen;
191 String name=fn.getFlagName();
192 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
194 throw new Error("Undeclared flag: "+name);
200 throw new Error("Unrecognized FlagExpressionNode");
204 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
205 /* Check return type */
206 if (!md.isConstructor())
207 if (!md.getReturnType().isVoid())
208 checkTypeDescriptor(md.getReturnType());
210 for(int i=0; i<md.numParameters(); i++) {
211 TypeDescriptor param_type=md.getParamType(i);
212 checkTypeDescriptor(param_type);
214 /* Link the naming environments */
215 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
216 md.getParameterTable().setParent(cd.getFieldTable());
218 if (!md.isStatic()) {
219 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
224 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
225 ClassDescriptor superdesc=cd.getSuperDesc();
226 if (superdesc!=null) {
227 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
228 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext();) {
229 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
230 if (md.matches(matchmd)) {
231 if (matchmd.getModifiers().isFinal()) {
232 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
237 BlockNode bn=state.getMethodBody(md);
238 checkBlockNode(md, md.getParameterTable(),bn);
241 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
242 /* Link in the naming environment */
243 bn.getVarTable().setParent(nametable);
244 for(int i=0; i<bn.size(); i++) {
245 BlockStatementNode bsn=bn.get(i);
246 checkBlockStatementNode(md, bn.getVarTable(),bsn);
250 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
252 case Kind.BlockExpressionNode:
253 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
256 case Kind.DeclarationNode:
257 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
260 case Kind.TagDeclarationNode:
261 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
264 case Kind.IfStatementNode:
265 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
269 checkLoopNode(md, nametable, (LoopNode)bsn);
272 case Kind.ReturnNode:
273 checkReturnNode(md, nametable, (ReturnNode)bsn);
276 case Kind.TaskExitNode:
277 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
280 case Kind.SubBlockNode:
281 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
284 case Kind.AtomicNode:
285 checkAtomicNode(md, nametable, (AtomicNode)bsn);
288 case Kind.ContinueBreakNode:
289 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
293 // do nothing, no semantic check for SESEs
300 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
301 checkExpressionNode(md, nametable, ben.getExpression(), null);
304 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
305 VarDescriptor vd=dn.getVarDescriptor();
306 checkTypeDescriptor(vd.getType());
307 Descriptor d=nametable.get(vd.getSymbol());
309 (d instanceof FieldDescriptor)) {
312 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
313 if (dn.getExpression()!=null)
314 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
317 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
318 TagVarDescriptor vd=dn.getTagVarDescriptor();
319 Descriptor d=nametable.get(vd.getSymbol());
321 (d instanceof FieldDescriptor)) {
324 throw new Error(vd.getSymbol()+" defined a second time");
327 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
328 checkBlockNode(md, nametable, sbn.getBlockNode());
331 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
332 checkBlockNode(md, nametable, sbn.getBlockNode());
335 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
336 if (loopstack.empty())
337 throw new Error("continue/break outside of loop");
338 LoopNode ln=(LoopNode)loopstack.peek();
342 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
343 if (d instanceof TaskDescriptor)
344 throw new Error("Illegal return appears in Task: "+d.getSymbol());
345 MethodDescriptor md=(MethodDescriptor)d;
346 if (rn.getReturnExpression()!=null)
347 if (md.getReturnType()==null)
348 throw new Error("Constructor can't return something.");
349 else if (md.getReturnType().isVoid())
350 throw new Error(md+" is void");
352 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
354 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
355 throw new Error("Need to return something for "+md);
358 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
359 if (md instanceof MethodDescriptor)
360 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
361 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
362 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
365 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
366 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
367 checkBlockNode(md, nametable, isn.getTrueBlock());
368 if (isn.getFalseBlock()!=null)
369 checkBlockNode(md, nametable, isn.getFalseBlock());
372 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
374 case Kind.AssignmentNode:
375 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
379 checkCastNode(md,nametable,(CastNode)en,td);
382 case Kind.CreateObjectNode:
383 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
386 case Kind.FieldAccessNode:
387 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
390 case Kind.ArrayAccessNode:
391 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
394 case Kind.LiteralNode:
395 checkLiteralNode(md,nametable,(LiteralNode)en,td);
398 case Kind.MethodInvokeNode:
399 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
403 checkNameNode(md,nametable,(NameNode)en,td);
407 checkOpNode(md,nametable,(OpNode)en,td);
410 case Kind.OffsetNode:
411 checkOffsetNode(md, nametable, (OffsetNode)en, new TypeDescriptor(TypeDescriptor.OFFSET));
414 case Kind.TertiaryNode:
415 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
418 case Kind.InstanceOfNode:
419 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
422 case Kind.ArrayInitializerNode:
423 checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
429 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
430 /* Get type descriptor */
431 if (cn.getType()==null) {
432 NameDescriptor typenamed=cn.getTypeName().getName();
433 String typename=typenamed.toString();
434 TypeDescriptor ntd=new TypeDescriptor(getClass(typename));
438 /* Check the type descriptor */
439 TypeDescriptor cast_type=cn.getType();
440 checkTypeDescriptor(cast_type);
444 if (!typeutil.isSuperorType(td,cast_type))
445 throw new Error("Cast node returns "+cast_type+", but need "+td);
448 ExpressionNode en=cn.getExpression();
449 checkExpressionNode(md, nametable, en, null);
450 TypeDescriptor etd=en.getType();
451 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
454 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
456 if (typeutil.isCastable(etd, cast_type))
459 /* Different branches */
460 /* TODO: change if add interfaces */
461 throw new Error("Cast will always fail\n"+cn.printNode(0));
464 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
465 ExpressionNode left=fan.getExpression();
466 checkExpressionNode(md,nametable,left,null);
467 TypeDescriptor ltd=left.getType();
468 String fieldname=fan.getFieldName();
470 FieldDescriptor fd=null;
471 if (ltd.isArray()&&fieldname.equals("length"))
472 fd=FieldDescriptor.arrayLength;
474 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
476 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
479 if (!typeutil.isSuperorType(td,fan.getType()))
480 throw new Error("Field node returns "+fan.getType()+", but need "+td);
483 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
484 ExpressionNode left=aan.getExpression();
485 checkExpressionNode(md,nametable,left,null);
487 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
488 TypeDescriptor ltd=left.getType();
491 if (!typeutil.isSuperorType(td,aan.getType()))
492 throw new Error("Field node returns "+aan.getType()+", but need "+td);
495 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
496 /* Resolve the type */
497 Object o=ln.getValue();
498 if (ln.getTypeString().equals("null")) {
499 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
500 } else if (o instanceof Integer) {
501 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
502 } else if (o instanceof Long) {
503 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
504 } else if (o instanceof Float) {
505 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
506 } else if (o instanceof Boolean) {
507 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
508 } else if (o instanceof Double) {
509 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
510 } else if (o instanceof Character) {
511 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
512 } else if (o instanceof String) {
513 ln.setType(new TypeDescriptor(getClass(TypeUtil.StringClass)));
517 if (!typeutil.isSuperorType(td,ln.getType()))
518 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
521 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
522 NameDescriptor nd=nn.getName();
523 if (nd.getBase()!=null) {
525 /* Rewrite NameNode */
526 ExpressionNode en=translateNameDescriptorintoExpression(nd);
527 nn.setExpression(en);
528 checkExpressionNode(md,nametable,en,td);
530 String varname=nd.toString();
531 Descriptor d=(Descriptor)nametable.get(varname);
533 throw new Error("Name "+varname+" undefined in: "+md);
535 if (d instanceof VarDescriptor) {
537 } else if (d instanceof FieldDescriptor) {
538 nn.setField((FieldDescriptor)d);
539 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
540 } else if (d instanceof TagVarDescriptor) {
542 } else throw new Error("Wrong type of descriptor");
544 if (!typeutil.isSuperorType(td,nn.getType()))
545 throw new Error("Field node returns "+nn.getType()+", but need "+td);
549 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
550 TypeDescriptor ltd = ofn.td;
551 //System.out.println("Testing TypeDescriptor ltd = " + ofn.td);
552 String fieldname = ofn.fieldname;
553 //System.out.println("Testing String fieldname = " + ofn.fieldname);
554 Descriptor d = (Descriptor) nameTable.get(fieldname);
555 //System.out.println("Testing Descriptor d = " + d.toString());
557 ClassDescriptor cd = null;
558 checkTypeDescriptor(ltd);
559 cd = ltd.getClassDesc();
560 ofn.setClassDesc(cd);
561 //System.out.println("Testing for ClassDescriptor cd = " + cd.toString());
563 FieldDescriptor fd=null;
564 if (ltd.isArray()&&fieldname.equals("length")) {
565 fd=FieldDescriptor.arrayLength;
567 fd=(FieldDescriptor) cd.getFieldTable().get(fieldname);
569 //System.out.println("Testing for FieldDescriptor fd = " + fd.toString());
572 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
577 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
578 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
579 checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
580 checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
583 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
584 if (td!=null&&!td.isBoolean())
585 throw new Error("Expecting type "+td+"for instanceof expression");
587 checkTypeDescriptor(tn.getExprType());
588 checkExpressionNode(md, nametable, tn.getExpr(), null);
591 void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
592 for( int i = 0; i < ain.numVarInitializers(); ++i ) {
593 checkExpressionNode(md, nametable, ain.getVarInitializer(i), td);
597 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
598 boolean postinc=true;
599 if (an.getOperation().getBaseOp()==null||
600 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
601 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
605 checkExpressionNode(md, nametable, an.getSrc(),td);
606 //TODO: Need check on validity of operation here
607 if (!((an.getDest() instanceof FieldAccessNode)||
608 (an.getDest() instanceof ArrayAccessNode)||
609 (an.getDest() instanceof NameNode)))
610 throw new Error("Bad lside in "+an.printNode(0));
611 checkExpressionNode(md, nametable, an.getDest(), null);
613 /* We want parameter variables to tasks to be immutable */
614 if (md instanceof TaskDescriptor) {
615 if (an.getDest() instanceof NameNode) {
616 NameNode nn=(NameNode)an.getDest();
617 if (nn.getVar()!=null) {
618 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
619 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
624 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
626 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
627 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
628 NameDescriptor nd=new NameDescriptor("String");
629 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
631 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
632 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
633 rightmin.addArgument(an.getSrc());
635 checkExpressionNode(md, nametable, an.getSrc(), null);
639 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
640 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
644 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
646 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
647 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
648 checkBlockNode(md, nametable, ln.getBody());
651 /* Link in the initializer naming environment */
652 BlockNode bn=ln.getInitializer();
653 bn.getVarTable().setParent(nametable);
654 for(int i=0; i<bn.size(); i++) {
655 BlockStatementNode bsn=bn.get(i);
656 checkBlockStatementNode(md, bn.getVarTable(),bsn);
658 //check the condition
659 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
660 checkBlockNode(md, bn.getVarTable(), ln.getBody());
661 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
667 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
668 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
669 for(int i=0; i<con.numArgs(); i++) {
670 ExpressionNode en=con.getArg(i);
671 checkExpressionNode(md,nametable,en,null);
672 tdarray[i]=en.getType();
675 TypeDescriptor typetolookin=con.getType();
676 checkTypeDescriptor(typetolookin);
678 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
679 throw new Error(typetolookin + " isn't a "+td);
681 /* Check flag effects */
682 if (con.getFlagEffects()!=null) {
683 FlagEffects fe=con.getFlagEffects();
684 ClassDescriptor cd=typetolookin.getClassDesc();
686 for(int j=0; j<fe.numEffects(); j++) {
687 FlagEffect flag=fe.getEffect(j);
688 String name=flag.getName();
689 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
690 //Make sure the flag is declared
692 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
693 if (flag_d.getExternal())
694 throw new Error("Attempting to modify external flag: "+name);
695 flag.setFlag(flag_d);
697 for(int j=0; j<fe.numTagEffects(); j++) {
698 TagEffect tag=fe.getTagEffect(j);
699 String name=tag.getName();
701 Descriptor d=(Descriptor)nametable.get(name);
703 throw new Error("Tag descriptor "+name+" undeclared");
704 else if (!(d instanceof TagVarDescriptor))
705 throw new Error(name+" is not a tag descriptor");
706 tag.setTag((TagVarDescriptor)d);
710 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
711 throw new Error("Can't allocate primitive type:"+con.printNode(0));
713 if (!typetolookin.isArray()) {
714 //Array's don't need constructor calls
715 ClassDescriptor classtolookin=typetolookin.getClassDesc();
717 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
718 MethodDescriptor bestmd=null;
720 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
721 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
722 /* Need correct number of parameters */
723 if (con.numArgs()!=currmd.numParameters())
725 for(int i=0; i<con.numArgs(); i++) {
726 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
729 /* Local allocations can't call global allocator */
730 if (!con.isGlobal()&&currmd.isGlobal())
733 /* Method okay so far */
737 if (typeutil.isMoreSpecific(currmd,bestmd)) {
739 } else if (con.isGlobal()&&match(currmd, bestmd)) {
740 if (currmd.isGlobal()&&!bestmd.isGlobal())
742 else if (currmd.isGlobal()&&bestmd.isGlobal())
744 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
745 throw new Error("No method is most specific");
748 /* Is this more specific than bestmd */
752 throw new Error("No method found for "+con.printNode(0)+" in "+md);
753 con.setConstructor(bestmd);
758 /** Check to see if md1 is the same specificity as md2.*/
760 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
761 /* Checks if md1 is more specific than md2 */
762 if (md1.numParameters()!=md2.numParameters())
764 for(int i=0; i<md1.numParameters(); i++) {
765 if (!md2.getParamType(i).equals(md1.getParamType(i)))
768 if (!md2.getReturnType().equals(md1.getReturnType()))
771 if (!md2.getClassDesc().equals(md1.getClassDesc()))
779 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
780 String id=nd.getIdentifier();
781 NameDescriptor base=nd.getBase();
783 return new NameNode(nd);
785 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
789 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
790 /*Typecheck subexpressions
791 and get types for expressions*/
793 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
794 for(int i=0; i<min.numArgs(); i++) {
795 ExpressionNode en=min.getArg(i);
796 checkExpressionNode(md,nametable,en,null);
797 tdarray[i]=en.getType();
799 TypeDescriptor typetolookin=null;
800 if (min.getExpression()!=null) {
801 checkExpressionNode(md,nametable,min.getExpression(),null);
802 typetolookin=min.getExpression().getType();
803 } else if (min.getBaseName()!=null) {
804 String rootname=min.getBaseName().getRoot();
805 if (rootname.equals("super")) {
806 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
807 typetolookin=new TypeDescriptor(supercd);
808 } else if (nametable.get(rootname)!=null) {
809 //we have an expression
810 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
811 checkExpressionNode(md, nametable, min.getExpression(), null);
812 typetolookin=min.getExpression().getType();
816 if (min.getBaseName().getSymbol().equals("System.out"))
817 cd=getClass("System");
819 cd=getClass(min.getBaseName().getSymbol());
821 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
822 typetolookin=new TypeDescriptor(cd);
824 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
825 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
826 min.methodid=supercd.getSymbol();
827 typetolookin=new TypeDescriptor(supercd);
828 } else if (md instanceof MethodDescriptor) {
829 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
831 /* If this a task descriptor we throw an error at this point */
832 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
834 if (!typetolookin.isClass())
835 throw new Error("Error with method call to "+min.getMethodName());
836 ClassDescriptor classtolookin=typetolookin.getClassDesc();
837 //System.out.println("Method name="+min.getMethodName());
839 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
840 MethodDescriptor bestmd=null;
842 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
843 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
844 /* Need correct number of parameters */
845 if (min.numArgs()!=currmd.numParameters())
847 for(int i=0; i<min.numArgs(); i++) {
848 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
851 /* Method okay so far */
855 if (typeutil.isMoreSpecific(currmd,bestmd)) {
857 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
858 throw new Error("No method is most specific");
860 /* Is this more specific than bestmd */
864 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
865 min.setMethod(bestmd);
867 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
868 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
869 /* Check whether we need to set this parameter to implied this */
870 if (!bestmd.isStatic()) {
871 if (min.getExpression()==null) {
872 ExpressionNode en=new NameNode(new NameDescriptor("this"));
873 min.setExpression(en);
874 checkExpressionNode(md, nametable, min.getExpression(), null);
880 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
881 checkExpressionNode(md, nametable, on.getLeft(), null);
882 if (on.getRight()!=null)
883 checkExpressionNode(md, nametable, on.getRight(), null);
884 TypeDescriptor ltd=on.getLeft().getType();
885 TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
886 TypeDescriptor lefttype=null;
887 TypeDescriptor righttype=null;
888 Operation op=on.getOp();
891 case Operation.LOGIC_OR:
892 case Operation.LOGIC_AND:
893 if (!(rtd.isBoolean()))
895 on.setRightType(rtd);
897 case Operation.LOGIC_NOT:
898 if (!(ltd.isBoolean()))
903 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
907 // 5.6.2 Binary Numeric Promotion
908 //TODO unboxing of reference objects
911 else if (ltd.isFloat())
913 else if (ltd.isLong())
914 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
916 lefttype=new TypeDescriptor(TypeDescriptor.INT);
917 on.setLeftType(lefttype);
918 on.setType(lefttype);
921 case Operation.BIT_OR:
922 case Operation.BIT_XOR:
923 case Operation.BIT_AND:
924 // 5.6.2 Binary Numeric Promotion
925 //TODO unboxing of reference objects
926 if (ltd.isDouble()||rtd.isDouble())
928 else if (ltd.isFloat()||rtd.isFloat())
930 else if (ltd.isLong()||rtd.isLong())
931 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
932 // 090205 hack for boolean
933 else if (ltd.isBoolean()||rtd.isBoolean())
934 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
936 lefttype=new TypeDescriptor(TypeDescriptor.INT);
939 on.setLeftType(lefttype);
940 on.setRightType(righttype);
941 on.setType(lefttype);
944 case Operation.ISAVAILABLE:
945 if (!(ltd.isPtr())) {
946 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
949 on.setLeftType(lefttype);
950 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
953 case Operation.EQUAL:
954 case Operation.NOTEQUAL:
955 // 5.6.2 Binary Numeric Promotion
956 //TODO unboxing of reference objects
957 if (ltd.isBoolean()||rtd.isBoolean()) {
958 if (!(ltd.isBoolean()&&rtd.isBoolean()))
960 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
961 } else if (ltd.isPtr()||rtd.isPtr()) {
962 if (!(ltd.isPtr()&&rtd.isPtr()))
966 } else if (ltd.isDouble()||rtd.isDouble())
967 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
968 else if (ltd.isFloat()||rtd.isFloat())
969 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
970 else if (ltd.isLong()||rtd.isLong())
971 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
973 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
975 on.setLeftType(lefttype);
976 on.setRightType(righttype);
977 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
986 // 5.6.2 Binary Numeric Promotion
987 //TODO unboxing of reference objects
988 if (!ltd.isNumber()||!rtd.isNumber())
991 if (ltd.isDouble()||rtd.isDouble())
992 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
993 else if (ltd.isFloat()||rtd.isFloat())
994 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
995 else if (ltd.isLong()||rtd.isLong())
996 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
998 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1000 on.setLeftType(lefttype);
1001 on.setRightType(righttype);
1002 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
1006 if (ltd.isString()||rtd.isString()) {
1007 ClassDescriptor stringcl=getClass(TypeUtil.StringClass);
1008 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
1009 NameDescriptor nd=new NameDescriptor("String");
1010 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
1011 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
1012 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
1013 leftmin.addArgument(on.getLeft());
1015 checkExpressionNode(md, nametable, on.getLeft(), null);
1018 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
1019 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
1020 rightmin.addArgument(on.getRight());
1022 checkExpressionNode(md, nametable, on.getRight(), null);
1025 on.setLeftType(stringtd);
1026 on.setRightType(stringtd);
1027 on.setType(stringtd);
1032 case Operation.MULT:
1035 // 5.6.2 Binary Numeric Promotion
1036 //TODO unboxing of reference objects
1037 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1038 throw new Error("Error in "+on.printNode(0));
1040 if (ltd.isDouble()||rtd.isDouble())
1041 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1042 else if (ltd.isFloat()||rtd.isFloat())
1043 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1044 else if (ltd.isLong()||rtd.isLong())
1045 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1047 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1049 on.setLeftType(lefttype);
1050 on.setRightType(righttype);
1051 on.setType(lefttype);
1054 case Operation.LEFTSHIFT:
1055 case Operation.RIGHTSHIFT:
1056 case Operation.URIGHTSHIFT:
1057 if (!rtd.isIntegerType())
1059 //5.6.1 Unary Numeric Promotion
1060 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1061 righttype=new TypeDescriptor(TypeDescriptor.INT);
1065 on.setRightType(righttype);
1066 if (!ltd.isIntegerType())
1069 case Operation.UNARYPLUS:
1070 case Operation.UNARYMINUS:
1071 /* case Operation.POSTINC:
1072 case Operation.POSTDEC:
1073 case Operation.PREINC:
1074 case Operation.PREDEC:*/
1075 if (!ltd.isNumber())
1077 //5.6.1 Unary Numeric Promotion
1078 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1079 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1082 on.setLeftType(lefttype);
1083 on.setType(lefttype);
1087 throw new Error(op.toString());
1091 if (!typeutil.isSuperorType(td, on.getType())) {
1092 System.out.println(td);
1093 System.out.println(on.getType());
1094 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));