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));
399 case Kind.TertiaryNode:
400 checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
403 case Kind.InstanceOfNode:
404 checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
410 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
411 /* Get type descriptor */
412 if (cn.getType()==null) {
413 NameDescriptor typenamed=cn.getTypeName().getName();
414 String typename=typenamed.toString();
415 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
419 /* Check the type descriptor */
420 TypeDescriptor cast_type=cn.getType();
421 checkTypeDescriptor(cast_type);
425 if (!typeutil.isSuperorType(td,cast_type))
426 throw new Error("Cast node returns "+cast_type+", but need "+td);
429 ExpressionNode en=cn.getExpression();
430 checkExpressionNode(md, nametable, en, null);
431 TypeDescriptor etd=en.getType();
432 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
435 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
437 if (typeutil.isCastable(etd, cast_type))
440 /* Different branches */
441 /* TODO: change if add interfaces */
442 throw new Error("Cast will always fail\n"+cn.printNode(0));
445 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
446 ExpressionNode left=fan.getExpression();
447 checkExpressionNode(md,nametable,left,null);
448 TypeDescriptor ltd=left.getType();
449 String fieldname=fan.getFieldName();
451 FieldDescriptor fd=null;
452 if (ltd.isArray()&&fieldname.equals("length"))
453 fd=FieldDescriptor.arrayLength;
455 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
457 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
460 if (!typeutil.isSuperorType(td,fan.getType()))
461 throw new Error("Field node returns "+fan.getType()+", but need "+td);
464 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
465 ExpressionNode left=aan.getExpression();
466 checkExpressionNode(md,nametable,left,null);
468 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
469 TypeDescriptor ltd=left.getType();
472 if (!typeutil.isSuperorType(td,aan.getType()))
473 throw new Error("Field node returns "+aan.getType()+", but need "+td);
476 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
477 /* Resolve the type */
478 Object o=ln.getValue();
479 if (ln.getTypeString().equals("null")) {
480 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
481 } else if (o instanceof Integer) {
482 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
483 } else if (o instanceof Long) {
484 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
485 } else if (o instanceof Float) {
486 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
487 } else if (o instanceof Boolean) {
488 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
489 } else if (o instanceof Double) {
490 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
491 } else if (o instanceof Character) {
492 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
493 } else if (o instanceof String) {
494 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
498 if (!typeutil.isSuperorType(td,ln.getType()))
499 throw new Error("Field node returns "+ln.getType()+", but need "+td+" in "+md);
502 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
503 NameDescriptor nd=nn.getName();
504 if (nd.getBase()!=null) {
506 /* Rewrite NameNode */
507 ExpressionNode en=translateNameDescriptorintoExpression(nd);
508 nn.setExpression(en);
509 checkExpressionNode(md,nametable,en,td);
511 String varname=nd.toString();
512 Descriptor d=(Descriptor)nametable.get(varname);
514 throw new Error("Name "+varname+" undefined in: "+md);
516 if (d instanceof VarDescriptor) {
518 } else if (d instanceof FieldDescriptor) {
519 nn.setField((FieldDescriptor)d);
520 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
521 } else if (d instanceof TagVarDescriptor) {
523 } else throw new Error("Wrong type of descriptor");
525 if (!typeutil.isSuperorType(td,nn.getType()))
526 throw new Error("Field node returns "+nn.getType()+", but need "+td);
530 void checkOffsetNode(Descriptor md, SymbolTable nameTable, OffsetNode ofn, TypeDescriptor td) {
531 TypeDescriptor ltd = ofn.td;
532 //System.out.println("Testing TypeDescriptor ltd = " + ofn.td);
533 String fieldname = ofn.fieldname;
534 //System.out.println("Testing String fieldname = " + ofn.fieldname);
535 Descriptor d = (Descriptor) nameTable.get(fieldname);
536 //System.out.println("Testing Descriptor d = " + d.toString());
538 ClassDescriptor cd = null;
539 checkTypeDescriptor(ltd);
540 cd = ltd.getClassDesc();
541 ofn.setClassDesc(cd);
542 //System.out.println("Testing for ClassDescriptor cd = " + cd.toString());
544 FieldDescriptor fd=null;
545 if (ltd.isArray()&&fieldname.equals("length")) {
546 fd=FieldDescriptor.arrayLength;
548 fd=(FieldDescriptor) cd.getFieldTable().get(fieldname);
550 //System.out.println("Testing for FieldDescriptor fd = " + fd.toString());
553 throw new Error("Unknown field "+fieldname + " in "+ofn.printNode(1)+" in "+md);
558 void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
559 checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
560 checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
561 checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
564 void checkInstanceOfNode(Descriptor md, SymbolTable nametable, InstanceOfNode tn, TypeDescriptor td) {
565 if (td!=null&&!td.isBoolean())
566 throw new Error("Expecting type "+td+"for instanceof expression");
568 checkTypeDescriptor(tn.getExprType());
569 checkExpressionNode(md, nametable, tn.getExpr(), null);
573 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
574 boolean postinc=true;
575 if (an.getOperation().getBaseOp()==null||
576 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
577 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
581 checkExpressionNode(md, nametable, an.getSrc(),td);
582 //TODO: Need check on validity of operation here
583 if (!((an.getDest() instanceof FieldAccessNode)||
584 (an.getDest() instanceof ArrayAccessNode)||
585 (an.getDest() instanceof NameNode)))
586 throw new Error("Bad lside in "+an.printNode(0));
587 checkExpressionNode(md, nametable, an.getDest(), null);
589 /* We want parameter variables to tasks to be immutable */
590 if (md instanceof TaskDescriptor) {
591 if (an.getDest() instanceof NameNode) {
592 NameNode nn=(NameNode)an.getDest();
593 if (nn.getVar()!=null) {
594 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
595 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
600 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
602 ClassDescriptor stringcl=typeutil.getClass(TypeUtil.StringClass);
603 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
604 NameDescriptor nd=new NameDescriptor("String");
605 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
607 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
608 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
609 rightmin.addArgument(an.getSrc());
611 checkExpressionNode(md, nametable, an.getSrc(), null);
615 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
616 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
620 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
622 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
623 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
624 checkBlockNode(md, nametable, ln.getBody());
627 /* Link in the initializer naming environment */
628 BlockNode bn=ln.getInitializer();
629 bn.getVarTable().setParent(nametable);
630 for(int i=0; i<bn.size(); i++) {
631 BlockStatementNode bsn=bn.get(i);
632 checkBlockStatementNode(md, bn.getVarTable(),bsn);
634 //check the condition
635 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
636 checkBlockNode(md, bn.getVarTable(), ln.getBody());
637 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
643 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
644 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
645 for(int i=0; i<con.numArgs(); i++) {
646 ExpressionNode en=con.getArg(i);
647 checkExpressionNode(md,nametable,en,null);
648 tdarray[i]=en.getType();
651 TypeDescriptor typetolookin=con.getType();
652 checkTypeDescriptor(typetolookin);
654 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
655 throw new Error(typetolookin + " isn't a "+td);
657 /* Check flag effects */
658 if (con.getFlagEffects()!=null) {
659 FlagEffects fe=con.getFlagEffects();
660 ClassDescriptor cd=typetolookin.getClassDesc();
662 for(int j=0; j<fe.numEffects(); j++) {
663 FlagEffect flag=fe.getEffect(j);
664 String name=flag.getName();
665 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
666 //Make sure the flag is declared
668 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
669 if (flag_d.getExternal())
670 throw new Error("Attempting to modify external flag: "+name);
671 flag.setFlag(flag_d);
673 for(int j=0; j<fe.numTagEffects(); j++) {
674 TagEffect tag=fe.getTagEffect(j);
675 String name=tag.getName();
677 Descriptor d=(Descriptor)nametable.get(name);
679 throw new Error("Tag descriptor "+name+" undeclared");
680 else if (!(d instanceof TagVarDescriptor))
681 throw new Error(name+" is not a tag descriptor");
682 tag.setTag((TagVarDescriptor)d);
686 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
687 throw new Error("Can't allocate primitive type:"+con.printNode(0));
689 if (!typetolookin.isArray()) {
690 //Array's don't need constructor calls
691 ClassDescriptor classtolookin=typetolookin.getClassDesc();
693 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
694 MethodDescriptor bestmd=null;
696 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
697 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
698 /* Need correct number of parameters */
699 if (con.numArgs()!=currmd.numParameters())
701 for(int i=0; i<con.numArgs(); i++) {
702 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
705 /* Local allocations can't call global allocator */
706 if (!con.isGlobal()&&currmd.isGlobal())
709 /* Method okay so far */
713 if (typeutil.isMoreSpecific(currmd,bestmd)) {
715 } else if (con.isGlobal()&&match(currmd, bestmd)) {
716 if (currmd.isGlobal()&&!bestmd.isGlobal())
718 else if (currmd.isGlobal()&&bestmd.isGlobal())
720 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
721 throw new Error("No method is most specific");
724 /* Is this more specific than bestmd */
728 throw new Error("No method found for "+con.printNode(0)+" in "+md);
729 con.setConstructor(bestmd);
734 /** Check to see if md1 is the same specificity as md2.*/
736 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
737 /* Checks if md1 is more specific than md2 */
738 if (md1.numParameters()!=md2.numParameters())
740 for(int i=0; i<md1.numParameters(); i++) {
741 if (!md2.getParamType(i).equals(md1.getParamType(i)))
744 if (!md2.getReturnType().equals(md1.getReturnType()))
747 if (!md2.getClassDesc().equals(md1.getClassDesc()))
755 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
756 String id=nd.getIdentifier();
757 NameDescriptor base=nd.getBase();
759 return new NameNode(nd);
761 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
765 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
766 /*Typecheck subexpressions
767 and get types for expressions*/
769 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
770 for(int i=0; i<min.numArgs(); i++) {
771 ExpressionNode en=min.getArg(i);
772 checkExpressionNode(md,nametable,en,null);
773 tdarray[i]=en.getType();
775 TypeDescriptor typetolookin=null;
776 if (min.getExpression()!=null) {
777 checkExpressionNode(md,nametable,min.getExpression(),null);
778 typetolookin=min.getExpression().getType();
779 } else if (min.getBaseName()!=null) {
780 String rootname=min.getBaseName().getRoot();
781 if (rootname.equals("super")) {
782 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
783 typetolookin=new TypeDescriptor(supercd);
784 } else if (nametable.get(rootname)!=null) {
785 //we have an expression
786 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
787 checkExpressionNode(md, nametable, min.getExpression(), null);
788 typetolookin=min.getExpression().getType();
791 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
793 throw new Error("md = "+ md.toString()+ " "+min.getBaseName()+" undefined");
794 typetolookin=new TypeDescriptor(cd);
796 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
797 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
798 min.methodid=supercd.getSymbol();
799 typetolookin=new TypeDescriptor(supercd);
800 } else if (md instanceof MethodDescriptor) {
801 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
803 /* If this a task descriptor we throw an error at this point */
804 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
806 if (!typetolookin.isClass())
807 throw new Error("Error with method call to "+min.getMethodName());
808 ClassDescriptor classtolookin=typetolookin.getClassDesc();
809 //System.out.println("Method name="+min.getMethodName());
811 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
812 MethodDescriptor bestmd=null;
814 for(Iterator methodit=methoddescriptorset.iterator(); methodit.hasNext();) {
815 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
816 /* Need correct number of parameters */
817 if (min.numArgs()!=currmd.numParameters())
819 for(int i=0; i<min.numArgs(); i++) {
820 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
823 /* Method okay so far */
827 if (typeutil.isMoreSpecific(currmd,bestmd)) {
829 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
830 throw new Error("No method is most specific");
832 /* Is this more specific than bestmd */
836 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
837 min.setMethod(bestmd);
839 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
840 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
841 /* Check whether we need to set this parameter to implied this */
842 if (!bestmd.isStatic()) {
843 if (min.getExpression()==null) {
844 ExpressionNode en=new NameNode(new NameDescriptor("this"));
845 min.setExpression(en);
846 checkExpressionNode(md, nametable, min.getExpression(), null);
852 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
853 checkExpressionNode(md, nametable, on.getLeft(), null);
854 if (on.getRight()!=null)
855 checkExpressionNode(md, nametable, on.getRight(), null);
856 TypeDescriptor ltd=on.getLeft().getType();
857 TypeDescriptor rtd=on.getRight()!=null ? on.getRight().getType() : null;
858 TypeDescriptor lefttype=null;
859 TypeDescriptor righttype=null;
860 Operation op=on.getOp();
863 case Operation.LOGIC_OR:
864 case Operation.LOGIC_AND:
865 if (!(rtd.isBoolean()))
867 on.setRightType(rtd);
869 case Operation.LOGIC_NOT:
870 if (!(ltd.isBoolean()))
875 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
879 // 5.6.2 Binary Numeric Promotion
880 //TODO unboxing of reference objects
883 else if (ltd.isFloat())
885 else if (ltd.isLong())
886 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
888 lefttype=new TypeDescriptor(TypeDescriptor.INT);
889 on.setLeftType(lefttype);
890 on.setType(lefttype);
893 case Operation.BIT_OR:
894 case Operation.BIT_XOR:
895 case Operation.BIT_AND:
896 // 5.6.2 Binary Numeric Promotion
897 //TODO unboxing of reference objects
898 if (ltd.isDouble()||rtd.isDouble())
900 else if (ltd.isFloat()||rtd.isFloat())
902 else if (ltd.isLong()||rtd.isLong())
903 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
904 // 090205 hack for boolean
905 else if (ltd.isBoolean()||rtd.isBoolean())
906 lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
908 lefttype=new TypeDescriptor(TypeDescriptor.INT);
911 on.setLeftType(lefttype);
912 on.setRightType(righttype);
913 on.setType(lefttype);
916 case Operation.ISAVAILABLE:
917 if (!(ltd.isPtr())) {
918 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
921 on.setLeftType(lefttype);
922 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
925 case Operation.EQUAL:
926 case Operation.NOTEQUAL:
927 // 5.6.2 Binary Numeric Promotion
928 //TODO unboxing of reference objects
929 if (ltd.isBoolean()||rtd.isBoolean()) {
930 if (!(ltd.isBoolean()&&rtd.isBoolean()))
932 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
933 } else if (ltd.isPtr()||rtd.isPtr()) {
934 if (!(ltd.isPtr()&&rtd.isPtr()))
938 } else if (ltd.isDouble()||rtd.isDouble())
939 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
940 else if (ltd.isFloat()||rtd.isFloat())
941 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
942 else if (ltd.isLong()||rtd.isLong())
943 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
945 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
947 on.setLeftType(lefttype);
948 on.setRightType(righttype);
949 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
958 // 5.6.2 Binary Numeric Promotion
959 //TODO unboxing of reference objects
960 if (!ltd.isNumber()||!rtd.isNumber())
963 if (ltd.isDouble()||rtd.isDouble())
964 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
965 else if (ltd.isFloat()||rtd.isFloat())
966 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
967 else if (ltd.isLong()||rtd.isLong())
968 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
970 lefttype=new TypeDescriptor(TypeDescriptor.INT);
972 on.setLeftType(lefttype);
973 on.setRightType(righttype);
974 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
978 if (ltd.isString()||rtd.isString()) {
979 ClassDescriptor stringcl=typeutil.getClass(TypeUtil.StringClass);
980 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
981 NameDescriptor nd=new NameDescriptor("String");
982 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
983 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
984 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
985 leftmin.addArgument(on.getLeft());
987 checkExpressionNode(md, nametable, on.getLeft(), null);
990 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
991 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
992 rightmin.addArgument(on.getRight());
994 checkExpressionNode(md, nametable, on.getRight(), null);
997 on.setLeftType(stringtd);
998 on.setRightType(stringtd);
999 on.setType(stringtd);
1004 case Operation.MULT:
1007 // 5.6.2 Binary Numeric Promotion
1008 //TODO unboxing of reference objects
1009 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
1010 throw new Error("Error in "+on.printNode(0));
1012 if (ltd.isDouble()||rtd.isDouble())
1013 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
1014 else if (ltd.isFloat()||rtd.isFloat())
1015 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
1016 else if (ltd.isLong()||rtd.isLong())
1017 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
1019 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1021 on.setLeftType(lefttype);
1022 on.setRightType(righttype);
1023 on.setType(lefttype);
1026 case Operation.LEFTSHIFT:
1027 case Operation.RIGHTSHIFT:
1028 case Operation.URIGHTSHIFT:
1029 if (!rtd.isIntegerType())
1031 //5.6.1 Unary Numeric Promotion
1032 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
1033 righttype=new TypeDescriptor(TypeDescriptor.INT);
1037 on.setRightType(righttype);
1038 if (!ltd.isIntegerType())
1041 case Operation.UNARYPLUS:
1042 case Operation.UNARYMINUS:
1043 /* case Operation.POSTINC:
1044 case Operation.POSTDEC:
1045 case Operation.PREINC:
1046 case Operation.PREDEC:*/
1047 if (!ltd.isNumber())
1049 //5.6.1 Unary Numeric Promotion
1050 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
1051 lefttype=new TypeDescriptor(TypeDescriptor.INT);
1054 on.setLeftType(lefttype);
1055 on.setType(lefttype);
1059 throw new Error(op.toString());
1063 if (!typeutil.isSuperorType(td, on.getType())) {
1064 System.out.println(td);
1065 System.out.println(on.getType());
1066 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));