6 public class SemanticCheck {
12 public SemanticCheck(State state, TypeUtil tu) {
15 this.loopstack=new Stack();
18 public void semanticCheck() {
19 SymbolTable classtable=state.getClassSymbolTable();
20 Iterator it=classtable.getDescriptorsIterator();
21 // Do descriptors first
23 ClassDescriptor cd=(ClassDescriptor)it.next();
24 //System.out.println("Checking class: "+cd);
25 //Set superclass link up
26 if (cd.getSuper()!=null) {
27 cd.setSuper(typeutil.getClass(cd.getSuper()));
28 // Link together Field, Method, and Flag tables so classes
29 // inherit these from their superclasses
30 cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
31 cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
32 cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable());
35 /* Check to see that fields are well typed */
36 for(Iterator field_it=cd.getFields(); field_it.hasNext();) {
37 FieldDescriptor fd=(FieldDescriptor)field_it.next();
38 //System.out.println("Checking field: "+fd);
42 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
43 MethodDescriptor md=(MethodDescriptor)method_it.next();
48 it=classtable.getDescriptorsIterator();
49 // Do descriptors first
51 ClassDescriptor cd=(ClassDescriptor)it.next();
52 for(Iterator method_it=cd.getMethods(); method_it.hasNext();) {
53 MethodDescriptor md=(MethodDescriptor)method_it.next();
54 checkMethodBody(cd,md);
58 for(Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator(); task_it.hasNext();) {
59 TaskDescriptor td=(TaskDescriptor)task_it.next();
65 public void checkTypeDescriptor(TypeDescriptor td) {
68 else if (td.isClass()) {
69 String name=td.toString();
70 ClassDescriptor field_cd=(ClassDescriptor)state.getClassSymbolTable().get(name);
72 throw new Error("Undefined class "+name);
73 td.setClassDescriptor(field_cd);
75 } else if (td.isTag())
81 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
82 checkTypeDescriptor(fd.getType());
85 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
87 return; /* No constraint checks to check */
88 for(int i=0; i<ccs.size(); i++) {
89 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
91 for(int j=0; j<cc.numArgs(); j++) {
92 ExpressionNode en=cc.getArg(j);
93 checkExpressionNode(td,nametable,en,null);
98 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
100 return; /* No flag effects to check */
101 for(int i=0; i<vfe.size(); i++) {
102 FlagEffects fe=(FlagEffects) vfe.get(i);
103 String varname=fe.getName();
104 //Make sure the variable is declared as a parameter to the task
105 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
107 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
110 //Make sure it correspods to a class
111 TypeDescriptor type_d=vd.getType();
112 if (!type_d.isClass())
113 throw new Error("Cannot have non-object argument for flag_effect");
115 ClassDescriptor cd=type_d.getClassDesc();
116 for(int j=0; j<fe.numEffects(); j++) {
117 FlagEffect flag=fe.getEffect(j);
118 String name=flag.getName();
119 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
120 //Make sure the flag is declared
122 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
123 if (flag_d.getExternal())
124 throw new Error("Attempting to modify external flag: "+name);
125 flag.setFlag(flag_d);
127 for(int j=0; j<fe.numTagEffects(); j++) {
128 TagEffect tag=fe.getTagEffect(j);
129 String name=tag.getName();
131 Descriptor d=(Descriptor)nametable.get(name);
133 throw new Error("Tag descriptor "+name+" undeclared");
134 else if (!(d instanceof TagVarDescriptor))
135 throw new Error(name+" is not a tag descriptor");
136 tag.setTag((TagVarDescriptor)d);
141 public void checkTask(TaskDescriptor td) {
142 for(int i=0; i<td.numParameters(); i++) {
143 /* Check that parameter is well typed */
144 TypeDescriptor param_type=td.getParamType(i);
145 checkTypeDescriptor(param_type);
147 /* Check the parameter's flag expression is well formed */
148 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
149 if (!param_type.isClass())
150 throw new Error("Cannot have non-object argument to a task");
151 ClassDescriptor cd=param_type.getClassDesc();
153 checkFlagExpressionNode(cd, fen);
156 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
157 /* Check that the task code is valid */
158 BlockNode bn=state.getMethodBody(td);
159 checkBlockNode(td, td.getParameterTable(),bn);
162 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
164 case Kind.FlagOpNode:
166 FlagOpNode fon=(FlagOpNode)fen;
167 checkFlagExpressionNode(cd, fon.getLeft());
168 if (fon.getRight()!=null)
169 checkFlagExpressionNode(cd, fon.getRight());
175 FlagNode fn=(FlagNode)fen;
176 String name=fn.getFlagName();
177 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
179 throw new Error("Undeclared flag: "+name);
185 throw new Error("Unrecognized FlagExpressionNode");
189 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
190 /* Check return type */
191 if (!md.isConstructor())
192 if (!md.getReturnType().isVoid())
193 checkTypeDescriptor(md.getReturnType());
195 for(int i=0; i<md.numParameters(); i++) {
196 TypeDescriptor param_type=md.getParamType(i);
197 checkTypeDescriptor(param_type);
199 /* Link the naming environments */
200 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
201 md.getParameterTable().setParent(cd.getFieldTable());
203 if (!md.isStatic()) {
204 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
209 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
210 ClassDescriptor superdesc=cd.getSuperDesc();
211 if (superdesc!=null) {
212 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
213 for(Iterator methodit=possiblematches.iterator(); methodit.hasNext();) {
214 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
215 if (md.matches(matchmd)) {
216 if (matchmd.getModifiers().isFinal()) {
217 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
222 BlockNode bn=state.getMethodBody(md);
223 checkBlockNode(md, md.getParameterTable(),bn);
226 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
227 /* Link in the naming environment */
228 bn.getVarTable().setParent(nametable);
229 for(int i=0; i<bn.size(); i++) {
230 BlockStatementNode bsn=bn.get(i);
231 checkBlockStatementNode(md, bn.getVarTable(),bsn);
235 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
237 case Kind.BlockExpressionNode:
238 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
241 case Kind.DeclarationNode:
242 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
245 case Kind.TagDeclarationNode:
246 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
249 case Kind.IfStatementNode:
250 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
254 checkLoopNode(md, nametable, (LoopNode)bsn);
257 case Kind.ReturnNode:
258 checkReturnNode(md, nametable, (ReturnNode)bsn);
261 case Kind.TaskExitNode:
262 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
265 case Kind.SubBlockNode:
266 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
269 case Kind.AtomicNode:
270 checkAtomicNode(md, nametable, (AtomicNode)bsn);
273 case Kind.ContinueBreakNode:
274 checkContinueBreakNode(md, nametable, (ContinueBreakNode) bsn);
278 // do nothing, no semantic check for SESEs
285 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
286 checkExpressionNode(md, nametable, ben.getExpression(), null);
289 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
290 VarDescriptor vd=dn.getVarDescriptor();
291 checkTypeDescriptor(vd.getType());
292 Descriptor d=nametable.get(vd.getSymbol());
294 (d instanceof FieldDescriptor)) {
297 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
298 if (dn.getExpression()!=null)
299 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
302 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
303 TagVarDescriptor vd=dn.getTagVarDescriptor();
304 Descriptor d=nametable.get(vd.getSymbol());
306 (d instanceof FieldDescriptor)) {
309 throw new Error(vd.getSymbol()+" defined a second time");
312 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
313 checkBlockNode(md, nametable, sbn.getBlockNode());
316 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
317 checkBlockNode(md, nametable, sbn.getBlockNode());
320 void checkContinueBreakNode(Descriptor md, SymbolTable nametable, ContinueBreakNode cbn) {
321 if (loopstack.empty())
322 throw new Error("continue/break outside of loop");
323 LoopNode ln=(LoopNode)loopstack.peek();
327 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
328 if (d instanceof TaskDescriptor)
329 throw new Error("Illegal return appears in Task: "+d.getSymbol());
330 MethodDescriptor md=(MethodDescriptor)d;
331 if (rn.getReturnExpression()!=null)
332 if (md.getReturnType()==null)
333 throw new Error("Constructor can't return something.");
334 else if (md.getReturnType().isVoid())
335 throw new Error(md+" is void");
337 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
339 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
340 throw new Error("Need to return something for "+md);
343 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
344 if (md instanceof MethodDescriptor)
345 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
346 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
347 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
350 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
351 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
352 checkBlockNode(md, nametable, isn.getTrueBlock());
353 if (isn.getFalseBlock()!=null)
354 checkBlockNode(md, nametable, isn.getFalseBlock());
357 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
359 case Kind.AssignmentNode:
360 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
364 checkCastNode(md,nametable,(CastNode)en,td);
367 case Kind.CreateObjectNode:
368 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
371 case Kind.FieldAccessNode:
372 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
375 case Kind.ArrayAccessNode:
376 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
379 case Kind.LiteralNode:
380 checkLiteralNode(md,nametable,(LiteralNode)en,td);
383 case Kind.MethodInvokeNode:
384 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
388 checkNameNode(md,nametable,(NameNode)en,td);
392 checkOpNode(md,nametable,(OpNode)en,td);
395 case Kind.OffsetNode:
396 checkOffsetNode(md, nametable, (OffsetNode)en, new TypeDescriptor(TypeDescriptor.OFFSET));
402 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
403 /* Get type descriptor */
404 if (cn.getType()==null) {
405 NameDescriptor typenamed=cn.getTypeName().getName();
406 String typename=typenamed.toString();
407 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
411 /* Check the type descriptor */
412 TypeDescriptor cast_type=cn.getType();
413 checkTypeDescriptor(cast_type);
417 if (!typeutil.isSuperorType(td,cast_type))
418 throw new Error("Cast node returns "+cast_type+", but need "+td);
421 ExpressionNode en=cn.getExpression();
422 checkExpressionNode(md, nametable, en, null);
423 TypeDescriptor etd=en.getType();
424 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
427 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
429 if (typeutil.isCastable(etd, cast_type))
432 /* Different branches */
433 /* TODO: change if add interfaces */
434 throw new Error("Cast will always fail\n"+cn.printNode(0));
437 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
438 ExpressionNode left=fan.getExpression();
439 checkExpressionNode(md,nametable,left,null);
440 TypeDescriptor ltd=left.getType();
441 String fieldname=fan.getFieldName();
443 FieldDescriptor fd=null;
444 if (ltd.isArray()&&fieldname.equals("length"))
445 fd=FieldDescriptor.arrayLength;
447 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
449 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
452 if (!typeutil.isSuperorType(td,fan.getType()))
453 throw new Error("Field node returns "+fan.getType()+", but need "+td);
456 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
457 ExpressionNode left=aan.getExpression();
458 checkExpressionNode(md,nametable,left,null);
460 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
461 TypeDescriptor ltd=left.getType();
464 if (!typeutil.isSuperorType(td,aan.getType()))
465 throw new Error("Field node returns "+aan.getType()+", but need "+td);
468 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
469 /* Resolve the type */
470 Object o=ln.getValue();
471 if (ln.getTypeString().equals("null")) {
472 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
473 } else if (o instanceof Integer) {
474 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
475 } else if (o instanceof Long) {
476 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
477 } else if (o instanceof Float) {
478 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
479 } else if (o instanceof Boolean) {
480 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
481 } else if (o instanceof Double) {
482 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
483 } else if (o instanceof Character) {
484 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
485 } else if (o instanceof String) {
486 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
490 if (!typeutil.isSuperorType(td,ln.getType()))
491 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
494 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
495 NameDescriptor nd=nn.getName();
496 if (nd.getBase()!=null) {
498 /* Rewrite NameNode */
499 ExpressionNode en=translateNameDescriptorintoExpression(nd);
500 nn.setExpression(en);
501 checkExpressionNode(md,nametable,en,td);
503 String varname=nd.toString();
504 Descriptor d=(Descriptor)nametable.get(varname);
506 throw new Error("Name "+varname+" undefined in: "+md);
508 if (d instanceof VarDescriptor) {
510 } else if (d instanceof FieldDescriptor) {
511 nn.setField((FieldDescriptor)d);
512 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
513 } else if (d instanceof TagVarDescriptor) {
515 } else throw new Error("Wrong type of descriptor");
517 if (!typeutil.isSuperorType(td,nn.getType()))
518 throw new Error("Field node returns "+nn.getType()+", but need "+td);
522 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
523 TypeDescriptor ltd = ofn.td;
524 //System.out.println("Testing TypeDescriptor ltd = " + ofn.td);
525 String fieldname = ofn.fieldname;
526 //System.out.println("Testing String fieldname = " + ofn.fieldname);
527 Descriptor d = (Descriptor) nameTable.get(fieldname);
528 //System.out.println("Testing Descriptor d = " + d.toString());
530 ClassDescriptor cd = null;
531 checkTypeDescriptor(ltd);
532 cd = ltd.getClassDesc();
533 ofn.setClassDesc(cd);
534 //System.out.println("Testing for ClassDescriptor cd = " + cd.toString());
536 FieldDescriptor fd=null;
537 if (ltd.isArray()&&fieldname.equals("length")) {
538 fd=FieldDescriptor.arrayLength;
540 fd=(FieldDescriptor) cd.getFieldTable().get(fieldname);
542 //System.out.println("Testing for FieldDescriptor fd = " + fd.toString());
545 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
549 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
550 boolean postinc=true;
551 if (an.getOperation().getBaseOp()==null||
552 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
553 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
557 checkExpressionNode(md, nametable, an.getSrc(),td);
558 //TODO: Need check on validity of operation here
559 if (!((an.getDest() instanceof FieldAccessNode)||
560 (an.getDest() instanceof ArrayAccessNode)||
561 (an.getDest() instanceof NameNode)))
562 throw new Error("Bad lside in "+an.printNode(0));
563 checkExpressionNode(md, nametable, an.getDest(), null);
565 /* We want parameter variables to tasks to be immutable */
566 if (md instanceof TaskDescriptor) {
567 if (an.getDest() instanceof NameNode) {
568 NameNode nn=(NameNode)an.getDest();
569 if (nn.getVar()!=null) {
570 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
571 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
576 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
578 ClassDescriptor stringcl=typeutil.getClass(TypeUtil.StringClass);
579 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
580 NameDescriptor nd=new NameDescriptor("String");
581 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
583 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
584 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
585 rightmin.addArgument(an.getSrc());
587 checkExpressionNode(md, nametable, an.getSrc(), null);
591 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
592 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
596 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
598 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
599 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
600 checkBlockNode(md, nametable, ln.getBody());
603 /* Link in the initializer naming environment */
604 BlockNode bn=ln.getInitializer();
605 bn.getVarTable().setParent(nametable);
606 for(int i=0; i<bn.size(); i++) {
607 BlockStatementNode bsn=bn.get(i);
608 checkBlockStatementNode(md, bn.getVarTable(),bsn);
610 //check the condition
611 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
612 checkBlockNode(md, bn.getVarTable(), ln.getBody());
613 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
619 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
620 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
621 for(int i=0; i<con.numArgs(); i++) {
622 ExpressionNode en=con.getArg(i);
623 checkExpressionNode(md,nametable,en,null);
624 tdarray[i]=en.getType();
627 TypeDescriptor typetolookin=con.getType();
628 checkTypeDescriptor(typetolookin);
630 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
631 throw new Error(typetolookin + " isn't a "+td);
633 /* Check flag effects */
634 if (con.getFlagEffects()!=null) {
635 FlagEffects fe=con.getFlagEffects();
636 ClassDescriptor cd=typetolookin.getClassDesc();
638 for(int j=0; j<fe.numEffects(); j++) {
639 FlagEffect flag=fe.getEffect(j);
640 String name=flag.getName();
641 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
642 //Make sure the flag is declared
644 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
645 if (flag_d.getExternal())
646 throw new Error("Attempting to modify external flag: "+name);
647 flag.setFlag(flag_d);
649 for(int j=0; j<fe.numTagEffects(); j++) {
650 TagEffect tag=fe.getTagEffect(j);
651 String name=tag.getName();
653 Descriptor d=(Descriptor)nametable.get(name);
655 throw new Error("Tag descriptor "+name+" undeclared");
656 else if (!(d instanceof TagVarDescriptor))
657 throw new Error(name+" is not a tag descriptor");
658 tag.setTag((TagVarDescriptor)d);
662 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
663 throw new Error("Can't allocate primitive type:"+con.printNode(0));
665 if (!typetolookin.isArray()) {
666 //Array's don't need constructor calls
667 ClassDescriptor classtolookin=typetolookin.getClassDesc();
669 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
670 MethodDescriptor bestmd=null;
672 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
673 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
674 /* Need correct number of parameters */
675 if (con.numArgs()!=currmd.numParameters())
677 for(int i=0; i<con.numArgs(); i++) {
678 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
681 /* Local allocations can't call global allocator */
682 if (!con.isGlobal()&&currmd.isGlobal())
685 /* Method okay so far */
689 if (typeutil.isMoreSpecific(currmd,bestmd)) {
691 } else if (con.isGlobal()&&match(currmd, bestmd)) {
692 if (currmd.isGlobal()&&!bestmd.isGlobal())
694 else if (currmd.isGlobal()&&bestmd.isGlobal())
696 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
697 throw new Error("No method is most specific");
700 /* Is this more specific than bestmd */
704 throw new Error("No method found for "+con.printNode(0)+" in "+md);
705 con.setConstructor(bestmd);
710 /** Check to see if md1 is the same specificity as md2.*/
712 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
713 /* Checks if md1 is more specific than md2 */
714 if (md1.numParameters()!=md2.numParameters())
716 for(int i=0; i<md1.numParameters(); i++) {
717 if (!md2.getParamType(i).equals(md1.getParamType(i)))
720 if (!md2.getReturnType().equals(md1.getReturnType()))
723 if (!md2.getClassDesc().equals(md1.getClassDesc()))
731 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
732 String id=nd.getIdentifier();
733 NameDescriptor base=nd.getBase();
735 return new NameNode(nd);
737 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
741 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
742 /*Typecheck subexpressions
743 and get types for expressions*/
745 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
746 for(int i=0; i<min.numArgs(); i++) {
747 ExpressionNode en=min.getArg(i);
748 checkExpressionNode(md,nametable,en,null);
749 tdarray[i]=en.getType();
751 TypeDescriptor typetolookin=null;
752 if (min.getExpression()!=null) {
753 checkExpressionNode(md,nametable,min.getExpression(),null);
754 typetolookin=min.getExpression().getType();
755 } else if (min.getBaseName()!=null) {
756 String rootname=min.getBaseName().getRoot();
757 if (rootname.equals("super")) {
758 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
759 typetolookin=new TypeDescriptor(supercd);
760 } else if (nametable.get(rootname)!=null) {
761 //we have an expression
762 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
763 checkExpressionNode(md, nametable, min.getExpression(), null);
764 typetolookin=min.getExpression().getType();
767 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
769 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
770 typetolookin=new TypeDescriptor(cd);
772 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
773 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
774 min.methodid=supercd.getSymbol();
775 typetolookin=new TypeDescriptor(supercd);
776 } else if (md instanceof MethodDescriptor) {
777 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
779 /* If this a task descriptor we throw an error at this point */
780 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
782 if (!typetolookin.isClass())
783 throw new Error("Error with method call to "+min.getMethodName());
784 ClassDescriptor classtolookin=typetolookin.getClassDesc();
785 //System.out.println("Method name="+min.getMethodName());
787 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
788 MethodDescriptor bestmd=null;
790 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
791 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
792 /* Need correct number of parameters */
793 if (min.numArgs()!=currmd.numParameters())
795 for(int i=0; i<min.numArgs(); i++) {
796 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
799 /* Method okay so far */
803 if (typeutil.isMoreSpecific(currmd,bestmd)) {
805 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
806 throw new Error("No method is most specific");
808 /* Is this more specific than bestmd */
812 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
813 min.setMethod(bestmd);
815 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
816 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
817 /* Check whether we need to set this parameter to implied this */
818 if (!bestmd.isStatic()) {
819 if (min.getExpression()==null) {
820 ExpressionNode en=new NameNode(new NameDescriptor("this"));
821 min.setExpression(en);
822 checkExpressionNode(md, nametable, min.getExpression(), null);
828 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
829 checkExpressionNode(md, nametable, on.getLeft(), null);
830 if (on.getRight()!=null)
831 checkExpressionNode(md, nametable, on.getRight(), null);
832 TypeDescriptor ltd=on.getLeft().getType();
833 TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
834 TypeDescriptor lefttype=null;
835 TypeDescriptor righttype=null;
836 Operation op=on.getOp();
839 case Operation.LOGIC_OR:
840 case Operation.LOGIC_AND:
841 if (!(rtd.isBoolean()))
843 on.setRightType(rtd);
845 case Operation.LOGIC_NOT:
846 if (!(ltd.isBoolean()))
851 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
855 // 5.6.2 Binary Numeric Promotion
856 //TODO unboxing of reference objects
859 else if (ltd.isFloat())
861 else if (ltd.isLong())
862 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
864 lefttype=new TypeDescriptor(TypeDescriptor.INT);
865 on.setLeftType(lefttype);
866 on.setType(lefttype);
869 case Operation.BIT_OR:
870 case Operation.BIT_XOR:
871 case Operation.BIT_AND:
872 // 5.6.2 Binary Numeric Promotion
873 //TODO unboxing of reference objects
874 if (ltd.isDouble()||rtd.isDouble())
876 else if (ltd.isFloat()||rtd.isFloat())
878 else if (ltd.isLong()||rtd.isLong())
879 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
880 // 090205 hack for boolean
881 else if (ltd.isBoolean()||rtd.isBoolean())
882 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
884 lefttype=new TypeDescriptor(TypeDescriptor.INT);
887 on.setLeftType(lefttype);
888 on.setRightType(righttype);
889 on.setType(lefttype);
892 case Operation.ISAVAILABLE:
893 if (!(ltd.isPtr())) {
894 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
897 on.setLeftType(lefttype);
898 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
901 case Operation.EQUAL:
902 case Operation.NOTEQUAL:
903 // 5.6.2 Binary Numeric Promotion
904 //TODO unboxing of reference objects
905 if (ltd.isBoolean()||rtd.isBoolean()) {
906 if (!(ltd.isBoolean()&&rtd.isBoolean()))
908 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
909 } else if (ltd.isPtr()||rtd.isPtr()) {
910 if (!(ltd.isPtr()&&rtd.isPtr()))
914 } else if (ltd.isDouble()||rtd.isDouble())
915 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
916 else if (ltd.isFloat()||rtd.isFloat())
917 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
918 else if (ltd.isLong()||rtd.isLong())
919 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
921 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
923 on.setLeftType(lefttype);
924 on.setRightType(righttype);
925 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
934 // 5.6.2 Binary Numeric Promotion
935 //TODO unboxing of reference objects
936 if (!ltd.isNumber()||!rtd.isNumber())
939 if (ltd.isDouble()||rtd.isDouble())
940 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
941 else if (ltd.isFloat()||rtd.isFloat())
942 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
943 else if (ltd.isLong()||rtd.isLong())
944 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
946 lefttype=new TypeDescriptor(TypeDescriptor.INT);
948 on.setLeftType(lefttype);
949 on.setRightType(righttype);
950 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
954 if (ltd.isString()||rtd.isString()) {
955 ClassDescriptor stringcl=typeutil.getClass(TypeUtil.StringClass);
956 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
957 NameDescriptor nd=new NameDescriptor("String");
958 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
959 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
960 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
961 leftmin.addArgument(on.getLeft());
963 checkExpressionNode(md, nametable, on.getLeft(), null);
966 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
967 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
968 rightmin.addArgument(on.getRight());
970 checkExpressionNode(md, nametable, on.getRight(), null);
973 on.setLeftType(stringtd);
974 on.setRightType(stringtd);
975 on.setType(stringtd);
983 // 5.6.2 Binary Numeric Promotion
984 //TODO unboxing of reference objects
985 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
986 throw new Error("Error in "+on.printNode(0));
988 if (ltd.isDouble()||rtd.isDouble())
989 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
990 else if (ltd.isFloat()||rtd.isFloat())
991 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
992 else if (ltd.isLong()||rtd.isLong())
993 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
995 lefttype=new TypeDescriptor(TypeDescriptor.INT);
997 on.setLeftType(lefttype);
998 on.setRightType(righttype);
999 on.setType(lefttype);
1002 case Operation.LEFTSHIFT:
1003 case Operation.RIGHTSHIFT:
1004 case Operation.URIGHTSHIFT:
1005 if (!rtd.isIntegerType())
1007 //5.6.1 Unary Numeric Promotion
1008 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1009 righttype=new TypeDescriptor(TypeDescriptor.INT);
1013 on.setRightType(righttype);
1014 if (!ltd.isIntegerType())
1017 case Operation.UNARYPLUS:
1018 case Operation.UNARYMINUS:
1019 /* case Operation.POSTINC:
1020 case Operation.POSTDEC:
1021 case Operation.PREINC:
1022 case Operation.PREDEC:*/
1023 if (!ltd.isNumber())
1025 //5.6.1 Unary Numeric Promotion
1026 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1027 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1030 on.setLeftType(lefttype);
1031 on.setType(lefttype);
1035 throw new Error(op.toString());
1039 if (!typeutil.isSuperorType(td, on.getType())) {
1040 System.out.println(td);
1041 System.out.println(on.getType());
1042 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));