6 public class SemanticCheck {
10 public SemanticCheck(State state, TypeUtil tu) {
15 public void semanticCheck() {
16 SymbolTable classtable=state.getClassSymbolTable();
17 Iterator it=classtable.getDescriptorsIterator();
18 // Do descriptors first
20 ClassDescriptor cd=(ClassDescriptor)it.next();
21 //System.out.println("Checking class: "+cd);
22 //Set superclass link up
23 if (cd.getSuper()!=null) {
24 cd.setSuper(typeutil.getClass(cd.getSuper()));
25 // Link together Field, Method, and Flag tables so classes
26 // inherit these from their superclasses
27 cd.getFieldTable().setParent(cd.getSuperDesc().getFieldTable());
28 cd.getMethodTable().setParent(cd.getSuperDesc().getMethodTable());
29 cd.getFlagTable().setParent(cd.getSuperDesc().getFlagTable());
32 /* Check to see that fields are well typed */
33 for(Iterator field_it=cd.getFields();field_it.hasNext();) {
34 FieldDescriptor fd=(FieldDescriptor)field_it.next();
35 //System.out.println("Checking field: "+fd);
39 for(Iterator method_it=cd.getMethods();method_it.hasNext();) {
40 MethodDescriptor md=(MethodDescriptor)method_it.next();
45 it=classtable.getDescriptorsIterator();
46 // Do descriptors first
48 ClassDescriptor cd=(ClassDescriptor)it.next();
49 for(Iterator method_it=cd.getMethods();method_it.hasNext();) {
50 MethodDescriptor md=(MethodDescriptor)method_it.next();
51 checkMethodBody(cd,md);
55 for(Iterator task_it=state.getTaskSymbolTable().getDescriptorsIterator();task_it.hasNext();) {
56 TaskDescriptor td=(TaskDescriptor)task_it.next();
62 public void checkTypeDescriptor(TypeDescriptor td) {
65 else if (td.isClass()) {
66 String name=td.toString();
67 ClassDescriptor field_cd=(ClassDescriptor)state.getClassSymbolTable().get(name);
69 throw new Error("Undefined class "+name);
70 td.setClassDescriptor(field_cd);
72 } else if (td.isTag())
78 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
79 checkTypeDescriptor(fd.getType());
82 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
84 return; /* No constraint checks to check */
85 for(int i=0;i<ccs.size();i++) {
86 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
88 for(int j=0;j<cc.numArgs();j++) {
89 ExpressionNode en=cc.getArg(j);
90 checkExpressionNode(td,nametable,en,null);
95 public void checkFlagEffects(TaskDescriptor td, Vector vfe, SymbolTable nametable) {
97 return; /* No flag effects to check */
98 for(int i=0;i<vfe.size();i++) {
99 FlagEffects fe=(FlagEffects) vfe.get(i);
100 String varname=fe.getName();
101 //Make sure the variable is declared as a parameter to the task
102 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
104 throw new Error("Parameter "+varname+" in Flag Effects not declared in "+td);
107 //Make sure it correspods to a class
108 TypeDescriptor type_d=vd.getType();
109 if (!type_d.isClass())
110 throw new Error("Cannot have non-object argument for flag_effect");
112 ClassDescriptor cd=type_d.getClassDesc();
113 for(int j=0;j<fe.numEffects();j++) {
114 FlagEffect flag=fe.getEffect(j);
115 String name=flag.getName();
116 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
117 //Make sure the flag is declared
119 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
120 if (flag_d.getExternal())
121 throw new Error("Attempting to modify external flag: "+name);
122 flag.setFlag(flag_d);
124 for(int j=0;j<fe.numTagEffects();j++) {
125 TagEffect tag=fe.getTagEffect(j);
126 String name=tag.getName();
128 Descriptor d=(Descriptor)nametable.get(name);
130 throw new Error("Tag descriptor "+name+" undeclared");
131 else if (!(d instanceof TagVarDescriptor))
132 throw new Error(name+" is not a tag descriptor");
133 tag.setTag((TagVarDescriptor)d);
138 public void checkTask(TaskDescriptor td) {
139 for(int i=0;i<td.numParameters();i++) {
140 /* Check that parameter is well typed */
141 TypeDescriptor param_type=td.getParamType(i);
142 checkTypeDescriptor(param_type);
144 /* Check the parameter's flag expression is well formed */
145 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
146 if (!param_type.isClass())
147 throw new Error("Cannot have non-object argument to a task");
148 ClassDescriptor cd=param_type.getClassDesc();
150 checkFlagExpressionNode(cd, fen);
153 checkFlagEffects(td, td.getFlagEffects(),td.getParameterTable());
154 /* Check that the task code is valid */
155 BlockNode bn=state.getMethodBody(td);
156 checkBlockNode(td, td.getParameterTable(),bn);
159 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
161 case Kind.FlagOpNode:
163 FlagOpNode fon=(FlagOpNode)fen;
164 checkFlagExpressionNode(cd, fon.getLeft());
165 if (fon.getRight()!=null)
166 checkFlagExpressionNode(cd, fon.getRight());
171 FlagNode fn=(FlagNode)fen;
172 String name=fn.getFlagName();
173 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
175 throw new Error("Undeclared flag: "+name);
180 throw new Error("Unrecognized FlagExpressionNode");
184 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
185 /* Check return type */
186 if (!md.isConstructor())
187 if (!md.getReturnType().isVoid())
188 checkTypeDescriptor(md.getReturnType());
190 for(int i=0;i<md.numParameters();i++) {
191 TypeDescriptor param_type=md.getParamType(i);
192 checkTypeDescriptor(param_type);
194 /* Link the naming environments */
195 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
196 md.getParameterTable().setParent(cd.getFieldTable());
198 if (!md.isStatic()) {
199 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
204 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
205 ClassDescriptor superdesc=cd.getSuperDesc();
206 if (superdesc!=null) {
207 Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
208 for(Iterator methodit=possiblematches.iterator();methodit.hasNext();) {
209 MethodDescriptor matchmd=(MethodDescriptor)methodit.next();
210 if (md.matches(matchmd)) {
211 if (matchmd.getModifiers().isFinal()) {
212 throw new Error("Try to override final method in method:"+md+" declared in "+cd);
217 BlockNode bn=state.getMethodBody(md);
218 checkBlockNode(md, md.getParameterTable(),bn);
221 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
222 /* Link in the naming environment */
223 bn.getVarTable().setParent(nametable);
224 for(int i=0;i<bn.size();i++) {
225 BlockStatementNode bsn=bn.get(i);
226 checkBlockStatementNode(md, bn.getVarTable(),bsn);
230 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
232 case Kind.BlockExpressionNode:
233 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
236 case Kind.DeclarationNode:
237 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
240 case Kind.TagDeclarationNode:
241 checkTagDeclarationNode(md, nametable, (TagDeclarationNode)bsn);
244 case Kind.IfStatementNode:
245 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
249 checkLoopNode(md, nametable, (LoopNode)bsn);
252 case Kind.ReturnNode:
253 checkReturnNode(md, nametable, (ReturnNode)bsn);
256 case Kind.TaskExitNode:
257 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
260 case Kind.SubBlockNode:
261 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
264 case Kind.AtomicNode:
265 checkAtomicNode(md, nametable, (AtomicNode)bsn);
271 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
272 checkExpressionNode(md, nametable, ben.getExpression(), null);
275 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
276 VarDescriptor vd=dn.getVarDescriptor();
277 checkTypeDescriptor(vd.getType());
278 Descriptor d=nametable.get(vd.getSymbol());
280 (d instanceof FieldDescriptor)) {
283 throw new Error(vd.getSymbol()+" in "+md+" defined a second time");
284 if (dn.getExpression()!=null)
285 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
288 void checkTagDeclarationNode(Descriptor md, SymbolTable nametable, TagDeclarationNode dn) {
289 TagVarDescriptor vd=dn.getTagVarDescriptor();
290 Descriptor d=nametable.get(vd.getSymbol());
292 (d instanceof FieldDescriptor)) {
295 throw new Error(vd.getSymbol()+" defined a second time");
298 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
299 checkBlockNode(md, nametable, sbn.getBlockNode());
302 void checkAtomicNode(Descriptor md, SymbolTable nametable, AtomicNode sbn) {
303 checkBlockNode(md, nametable, sbn.getBlockNode());
306 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
307 if (d instanceof TaskDescriptor)
308 throw new Error("Illegal return appears in Task: "+d.getSymbol());
309 MethodDescriptor md=(MethodDescriptor)d;
310 if (rn.getReturnExpression()!=null)
311 if (md.getReturnType()==null)
312 throw new Error("Constructor can't return something.");
313 else if (md.getReturnType().isVoid())
314 throw new Error(md+" is void");
316 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
318 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
319 throw new Error("Need to return something for "+md);
322 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
323 if (md instanceof MethodDescriptor)
324 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
325 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects(),nametable);
326 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
329 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
330 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
331 checkBlockNode(md, nametable, isn.getTrueBlock());
332 if (isn.getFalseBlock()!=null)
333 checkBlockNode(md, nametable, isn.getFalseBlock());
336 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
338 case Kind.AssignmentNode:
339 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
342 checkCastNode(md,nametable,(CastNode)en,td);
344 case Kind.CreateObjectNode:
345 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
347 case Kind.FieldAccessNode:
348 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
350 case Kind.ArrayAccessNode:
351 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
353 case Kind.LiteralNode:
354 checkLiteralNode(md,nametable,(LiteralNode)en,td);
356 case Kind.MethodInvokeNode:
357 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
360 checkNameNode(md,nametable,(NameNode)en,td);
363 checkOpNode(md,nametable,(OpNode)en,td);
369 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
370 /* Get type descriptor */
371 if (cn.getType()==null) {
372 NameDescriptor typenamed=cn.getTypeName().getName();
373 String typename=typenamed.toString();
374 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
378 /* Check the type descriptor */
379 TypeDescriptor cast_type=cn.getType();
380 checkTypeDescriptor(cast_type);
384 if (!typeutil.isSuperorType(td,cast_type))
385 throw new Error("Cast node returns "+cast_type+", but need "+td);
388 ExpressionNode en=cn.getExpression();
389 checkExpressionNode(md, nametable, en, null);
390 TypeDescriptor etd=en.getType();
391 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
394 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
396 if (typeutil.isCastable(etd, cast_type))
399 /* Different branches */
400 /* TODO: change if add interfaces */
401 throw new Error("Cast will always fail\n"+cn.printNode(0));
404 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
405 ExpressionNode left=fan.getExpression();
406 checkExpressionNode(md,nametable,left,null);
407 TypeDescriptor ltd=left.getType();
408 String fieldname=fan.getFieldName();
410 FieldDescriptor fd=null;
411 if (ltd.isArray()&&fieldname.equals("length"))
412 fd=FieldDescriptor.arrayLength;
414 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
416 throw new Error("Unknown field "+fieldname + " in "+fan.printNode(0)+" in "+md);
419 if (!typeutil.isSuperorType(td,fan.getType()))
420 throw new Error("Field node returns "+fan.getType()+", but need "+td);
423 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
424 ExpressionNode left=aan.getExpression();
425 checkExpressionNode(md,nametable,left,null);
427 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
428 TypeDescriptor ltd=left.getType();
431 if (!typeutil.isSuperorType(td,aan.getType()))
432 throw new Error("Field node returns "+aan.getType()+", but need "+td);
435 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
436 /* Resolve the type */
437 Object o=ln.getValue();
438 if (ln.getTypeString().equals("null")) {
439 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
440 } else if (o instanceof Integer) {
441 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
442 } else if (o instanceof Long) {
443 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
444 } else if (o instanceof Float) {
445 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
446 } else if (o instanceof Boolean) {
447 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
448 } else if (o instanceof Double) {
449 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
450 } else if (o instanceof Character) {
451 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
452 } else if (o instanceof String) {
453 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
457 if (!typeutil.isSuperorType(td,ln.getType()))
458 throw new Error("Field node returns "+ln.getType()+", but need "+td);
461 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
462 NameDescriptor nd=nn.getName();
463 if (nd.getBase()!=null) {
465 /* Rewrite NameNode */
466 ExpressionNode en=translateNameDescriptorintoExpression(nd);
467 nn.setExpression(en);
468 checkExpressionNode(md,nametable,en,td);
470 String varname=nd.toString();
471 Descriptor d=(Descriptor)nametable.get(varname);
473 throw new Error("Name "+varname+" undefined in: "+md);
475 if (d instanceof VarDescriptor) {
477 } else if (d instanceof FieldDescriptor) {
478 nn.setField((FieldDescriptor)d);
479 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
480 } else if (d instanceof TagVarDescriptor) {
482 } else throw new Error("Wrong type of descriptor");
484 if (!typeutil.isSuperorType(td,nn.getType()))
485 throw new Error("Field node returns "+nn.getType()+", but need "+td);
489 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
490 boolean postinc=true;
491 if (an.getOperation().getBaseOp()==null||
492 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
493 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
497 checkExpressionNode(md, nametable, an.getSrc() ,td);
498 //TODO: Need check on validity of operation here
499 if (!((an.getDest() instanceof FieldAccessNode)||
500 (an.getDest() instanceof ArrayAccessNode)||
501 (an.getDest() instanceof NameNode)))
502 throw new Error("Bad lside in "+an.printNode(0));
503 checkExpressionNode(md, nametable, an.getDest(), null);
506 /* We want parameter variables to tasks to be immutable */
507 if (md instanceof TaskDescriptor) {
508 if (an.getDest() instanceof NameNode) {
509 NameNode nn=(NameNode)an.getDest();
510 if (nn.getVar()!=null) {
511 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
512 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
517 if (an.getDest().getType().isString()&&an.getOperation().getOp()==AssignOperation.PLUSEQ) {
519 ClassDescriptor stringcl=typeutil.getClass(TypeUtil.StringClass);
520 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
521 NameDescriptor nd=new NameDescriptor("String");
522 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
524 if (!(an.getSrc().getType().isString()&&(an.getSrc() instanceof OpNode))) {
525 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
526 rightmin.addArgument(an.getSrc());
528 checkExpressionNode(md, nametable, an.getSrc(), null);
533 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
534 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
538 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
539 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
540 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
541 checkBlockNode(md, nametable, ln.getBody());
544 /* Link in the initializer naming environment */
545 BlockNode bn=ln.getInitializer();
546 bn.getVarTable().setParent(nametable);
547 for(int i=0;i<bn.size();i++) {
548 BlockStatementNode bsn=bn.get(i);
549 checkBlockStatementNode(md, bn.getVarTable(),bsn);
551 //check the condition
552 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
553 checkBlockNode(md, bn.getVarTable(), ln.getBody());
554 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
559 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
560 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
561 for(int i=0;i<con.numArgs();i++) {
562 ExpressionNode en=con.getArg(i);
563 checkExpressionNode(md,nametable,en,null);
564 tdarray[i]=en.getType();
567 TypeDescriptor typetolookin=con.getType();
568 checkTypeDescriptor(typetolookin);
570 if (td!=null&&!typeutil.isSuperorType(td, typetolookin))
571 throw new Error(typetolookin + " isn't a "+td);
573 /* Check flag effects */
574 if (con.getFlagEffects()!=null) {
575 FlagEffects fe=con.getFlagEffects();
576 ClassDescriptor cd=typetolookin.getClassDesc();
578 for(int j=0;j<fe.numEffects();j++) {
579 FlagEffect flag=fe.getEffect(j);
580 String name=flag.getName();
581 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
582 //Make sure the flag is declared
584 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
585 if (flag_d.getExternal())
586 throw new Error("Attempting to modify external flag: "+name);
587 flag.setFlag(flag_d);
589 for(int j=0;j<fe.numTagEffects();j++) {
590 TagEffect tag=fe.getTagEffect(j);
591 String name=tag.getName();
593 Descriptor d=(Descriptor)nametable.get(name);
595 throw new Error("Tag descriptor "+name+" undeclared");
596 else if (!(d instanceof TagVarDescriptor))
597 throw new Error(name+" is not a tag descriptor");
598 tag.setTag((TagVarDescriptor)d);
602 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
603 throw new Error("Can't allocate primitive type:"+con.printNode(0));
605 if (!typetolookin.isArray()) {
606 //Array's don't need constructor calls
607 ClassDescriptor classtolookin=typetolookin.getClassDesc();
609 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
610 MethodDescriptor bestmd=null;
612 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
613 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
614 /* Need correct number of parameters */
615 if (con.numArgs()!=currmd.numParameters())
617 for(int i=0;i<con.numArgs();i++) {
618 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
621 /* Local allocations can't call global allocator */
622 if (!con.isGlobal()&&currmd.isGlobal())
625 /* Method okay so far */
629 if (typeutil.isMoreSpecific(currmd,bestmd)) {
631 } else if (con.isGlobal()&&match(currmd, bestmd)) {
632 if (currmd.isGlobal()&&!bestmd.isGlobal())
634 else if (currmd.isGlobal()&&bestmd.isGlobal())
636 } else if (!typeutil.isMoreSpecific(bestmd, currmd)) {
637 throw new Error("No method is most specific");
640 /* Is this more specific than bestmd */
644 throw new Error("No method found for "+con.printNode(0)+" in "+md);
645 con.setConstructor(bestmd);
650 /** Check to see if md1 is the same specificity as md2.*/
652 boolean match(MethodDescriptor md1, MethodDescriptor md2) {
653 /* Checks if md1 is more specific than md2 */
654 if (md1.numParameters()!=md2.numParameters())
656 for(int i=0;i<md1.numParameters();i++) {
657 if (!md2.getParamType(i).equals(md1.getParamType(i)))
660 if (!md2.getReturnType().equals(md1.getReturnType()))
663 if (!md2.getClassDesc().equals(md1.getClassDesc()))
671 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
672 String id=nd.getIdentifier();
673 NameDescriptor base=nd.getBase();
675 return new NameNode(nd);
677 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
681 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
682 /*Typecheck subexpressions
683 and get types for expressions*/
685 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
686 for(int i=0;i<min.numArgs();i++) {
687 ExpressionNode en=min.getArg(i);
688 checkExpressionNode(md,nametable,en,null);
689 tdarray[i]=en.getType();
691 TypeDescriptor typetolookin=null;
692 if (min.getExpression()!=null) {
693 checkExpressionNode(md,nametable,min.getExpression(),null);
694 typetolookin=min.getExpression().getType();
695 } else if (min.getBaseName()!=null) {
696 String rootname=min.getBaseName().getRoot();
697 if (rootname.equals("super")) {
698 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
699 typetolookin=new TypeDescriptor(supercd);
700 } else if (nametable.get(rootname)!=null) {
701 //we have an expression
702 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
703 checkExpressionNode(md, nametable, min.getExpression(), null);
704 typetolookin=min.getExpression().getType();
707 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
709 throw new Error(min.getBaseName()+" undefined");
710 typetolookin=new TypeDescriptor(cd);
712 } else if ((md instanceof MethodDescriptor)&&min.getMethodName().equals("super")) {
713 ClassDescriptor supercd=((MethodDescriptor)md).getClassDesc().getSuperDesc();
714 min.methodid=supercd.getSymbol();
715 typetolookin=new TypeDescriptor(supercd);
716 } else if (md instanceof MethodDescriptor) {
717 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
719 /* If this a task descriptor we throw an error at this point */
720 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
722 if (!typetolookin.isClass())
723 throw new Error("Error with method call to "+min.getMethodName());
724 ClassDescriptor classtolookin=typetolookin.getClassDesc();
725 //System.out.println("Method name="+min.getMethodName());
727 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
728 MethodDescriptor bestmd=null;
730 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
731 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
732 /* Need correct number of parameters */
733 if (min.numArgs()!=currmd.numParameters())
735 for(int i=0;i<min.numArgs();i++) {
736 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
739 /* Method okay so far */
743 if (typeutil.isMoreSpecific(currmd,bestmd)) {
745 } else if (!typeutil.isMoreSpecific(bestmd, currmd))
746 throw new Error("No method is most specific");
748 /* Is this more specific than bestmd */
752 throw new Error("No method found for :"+min.printNode(0)+" in class: " + classtolookin+" in "+md);
753 min.setMethod(bestmd);
755 if ((td!=null)&&(min.getType()!=null)&&!typeutil.isSuperorType(td, min.getType()))
756 throw new Error(min.getType()+ " is not equal to or a subclass of "+td);
757 /* Check whether we need to set this parameter to implied this */
758 if (!bestmd.isStatic()) {
759 if (min.getExpression()==null) {
760 ExpressionNode en=new NameNode(new NameDescriptor("this"));
761 min.setExpression(en);
762 checkExpressionNode(md, nametable, min.getExpression(), null);
768 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
769 checkExpressionNode(md, nametable, on.getLeft(), null);
770 if (on.getRight()!=null)
771 checkExpressionNode(md, nametable, on.getRight(), null);
772 TypeDescriptor ltd=on.getLeft().getType();
773 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
774 TypeDescriptor lefttype=null;
775 TypeDescriptor righttype=null;
776 Operation op=on.getOp();
779 case Operation.LOGIC_OR:
780 case Operation.LOGIC_AND:
781 if (!(rtd.isBoolean()))
783 on.setRightType(rtd);
784 case Operation.LOGIC_NOT:
785 if (!(ltd.isBoolean()))
790 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
794 // 5.6.2 Binary Numeric Promotion
795 //TODO unboxing of reference objects
798 else if (ltd.isFloat())
800 else if (ltd.isLong())
801 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
803 lefttype=new TypeDescriptor(TypeDescriptor.INT);
804 on.setLeftType(lefttype);
805 on.setType(lefttype);
808 case Operation.BIT_OR:
809 case Operation.BIT_XOR:
810 case Operation.BIT_AND:
811 // 5.6.2 Binary Numeric Promotion
812 //TODO unboxing of reference objects
813 if (ltd.isDouble()||rtd.isDouble())
815 else if (ltd.isFloat()||rtd.isFloat())
817 else if (ltd.isLong()||rtd.isLong())
818 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
820 lefttype=new TypeDescriptor(TypeDescriptor.INT);
823 on.setLeftType(lefttype);
824 on.setRightType(righttype);
825 on.setType(lefttype);
828 case Operation.ISAVAILABLE:
829 if (!(ltd.isPtr())) {
830 throw new Error("Can't use isavailable on non-pointers/non-parameters.");
833 on.setLeftType(lefttype);
834 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
836 case Operation.EQUAL:
837 case Operation.NOTEQUAL:
838 // 5.6.2 Binary Numeric Promotion
839 //TODO unboxing of reference objects
840 if (ltd.isBoolean()||rtd.isBoolean()) {
841 if (!(ltd.isBoolean()&&rtd.isBoolean()))
843 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
844 } else if (ltd.isPtr()||rtd.isPtr()) {
845 if (!(ltd.isPtr()&&rtd.isPtr()))
849 } else if (ltd.isDouble()||rtd.isDouble())
850 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
851 else if (ltd.isFloat()||rtd.isFloat())
852 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
853 else if (ltd.isLong()||rtd.isLong())
854 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
856 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
858 on.setLeftType(lefttype);
859 on.setRightType(righttype);
860 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
869 // 5.6.2 Binary Numeric Promotion
870 //TODO unboxing of reference objects
871 if (!ltd.isNumber()||!rtd.isNumber())
874 if (ltd.isDouble()||rtd.isDouble())
875 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
876 else if (ltd.isFloat()||rtd.isFloat())
877 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
878 else if (ltd.isLong()||rtd.isLong())
879 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
881 lefttype=new TypeDescriptor(TypeDescriptor.INT);
883 on.setLeftType(lefttype);
884 on.setRightType(righttype);
885 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
889 if (ltd.isString()||rtd.isString()) {
890 ClassDescriptor stringcl=typeutil.getClass(TypeUtil.StringClass);
891 TypeDescriptor stringtd=new TypeDescriptor(stringcl);
892 NameDescriptor nd=new NameDescriptor("String");
893 NameDescriptor valuend=new NameDescriptor(nd, "valueOf");
894 if (!(ltd.isString()&&(on.getLeft() instanceof OpNode))) {
895 MethodInvokeNode leftmin=new MethodInvokeNode(valuend);
896 leftmin.addArgument(on.getLeft());
898 checkExpressionNode(md, nametable, on.getLeft(), null);
901 if (!(rtd.isString()&&(on.getRight() instanceof OpNode))) {
902 MethodInvokeNode rightmin=new MethodInvokeNode(valuend);
903 rightmin.addArgument(on.getRight());
905 checkExpressionNode(md, nametable, on.getRight(), null);
908 on.setLeftType(stringtd);
909 on.setRightType(stringtd);
910 on.setType(stringtd);
918 // 5.6.2 Binary Numeric Promotion
919 //TODO unboxing of reference objects
920 if (ltd.isArray()||rtd.isArray()||!ltd.isNumber()||!rtd.isNumber())
921 throw new Error("Error in "+on.printNode(0));
923 if (ltd.isDouble()||rtd.isDouble())
924 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
925 else if (ltd.isFloat()||rtd.isFloat())
926 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
927 else if (ltd.isLong()||rtd.isLong())
928 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
930 lefttype=new TypeDescriptor(TypeDescriptor.INT);
932 on.setLeftType(lefttype);
933 on.setRightType(righttype);
934 on.setType(lefttype);
937 case Operation.LEFTSHIFT:
938 case Operation.RIGHTSHIFT:
939 case Operation.URIGHTSHIFT:
940 if (!rtd.isIntegerType())
942 //5.6.1 Unary Numeric Promotion
943 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
944 righttype=new TypeDescriptor(TypeDescriptor.INT);
948 on.setRightType(righttype);
949 if (!ltd.isIntegerType())
951 case Operation.UNARYPLUS:
952 case Operation.UNARYMINUS:
953 /* case Operation.POSTINC:
954 case Operation.POSTDEC:
955 case Operation.PREINC:
956 case Operation.PREDEC:*/
959 //5.6.1 Unary Numeric Promotion
960 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
961 lefttype=new TypeDescriptor(TypeDescriptor.INT);
964 on.setLeftType(lefttype);
965 on.setType(lefttype);
968 throw new Error(op.toString());
972 if (!typeutil.isSuperorType(td, on.getType())) {
973 System.out.println(td);
974 System.out.println(on.getType());
975 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));