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();
63 public void checkTypeDescriptor(TypeDescriptor td) {
67 String name=td.toString();
68 ClassDescriptor field_cd=(ClassDescriptor)state.getClassSymbolTable().get(name);
70 throw new Error("Undefined class "+name);
71 td.setClassDescriptor(field_cd);
77 public void checkField(ClassDescriptor cd, FieldDescriptor fd) {
78 checkTypeDescriptor(fd.getType());
81 public void checkConstraintCheck(TaskDescriptor td, SymbolTable nametable, Vector ccs) {
83 return; /* No constraint checks to check */
84 for(int i=0;i<ccs.size();i++) {
85 ConstraintCheck cc=(ConstraintCheck) ccs.get(i);
87 for(int j=0;j<cc.numArgs();j++) {
88 ExpressionNode en=cc.getArg(j);
89 checkExpressionNode(td,nametable,en,null);
94 public void checkFlagEffects(TaskDescriptor td, Vector vfe) {
96 return; /* No flag effects to check */
97 for(int i=0;i<vfe.size();i++) {
98 FlagEffects fe=(FlagEffects) vfe.get(i);
99 String varname=fe.getName();
100 //Make sure the variable is declared as a parameter to the task
101 VarDescriptor vd=(VarDescriptor)td.getParameterTable().get(varname);
103 throw new Error("Parameter "+varname+" in Flag Effects not declared");
106 //Make sure it correspods to a class
107 TypeDescriptor type_d=vd.getType();
108 if (!type_d.isClass())
109 throw new Error("Cannot have non-object argument for flag_effect");
111 ClassDescriptor cd=type_d.getClassDesc();
112 for(int j=0;j<fe.numEffects();j++) {
113 FlagEffect flag=fe.getEffect(j);
114 String name=flag.getName();
115 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
116 //Make sure the flag is declared
118 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
119 if (flag_d.getExternal())
120 throw new Error("Attempting to modify external flag: "+name);
121 flag.setFlag(flag_d);
126 public void checkTask(TaskDescriptor td) {
127 for(int i=0;i<td.numParameters();i++) {
128 /* Check that parameter is well typed */
129 TypeDescriptor param_type=td.getParamType(i);
130 checkTypeDescriptor(param_type);
132 /* Check the parameter's flag expression is well formed */
133 FlagExpressionNode fen=td.getFlag(td.getParameter(i));
134 if (!param_type.isClass())
135 throw new Error("Cannot have non-object argument to a task");
136 ClassDescriptor cd=param_type.getClassDesc();
137 checkFlagExpressionNode(cd, fen);
138 checkFlagEffects(td, td.getFlagEffects());
140 /* Check that the task code is valid */
141 BlockNode bn=state.getMethodBody(td);
142 checkBlockNode(td, td.getParameterTable(),bn);
146 public void checkFlagExpressionNode(ClassDescriptor cd, FlagExpressionNode fen) {
148 case Kind.FlagOpNode:
150 FlagOpNode fon=(FlagOpNode)fen;
151 checkFlagExpressionNode(cd, fon.getLeft());
152 if (fon.getRight()!=null)
153 checkFlagExpressionNode(cd, fon.getRight());
158 FlagNode fn=(FlagNode)fen;
159 String name=fn.getFlagName();
160 FlagDescriptor fd=(FlagDescriptor)cd.getFlagTable().get(name);
162 throw new Error("Undeclared flag: "+name);
167 throw new Error("Unrecognized FlagExpressionNode");
171 public void checkMethod(ClassDescriptor cd, MethodDescriptor md) {
172 /* Check return type */
173 if (!md.isConstructor())
174 if (!md.getReturnType().isVoid())
175 checkTypeDescriptor(md.getReturnType());
177 for(int i=0;i<md.numParameters();i++) {
178 TypeDescriptor param_type=md.getParamType(i);
179 checkTypeDescriptor(param_type);
181 /* Link the naming environments */
182 if (!md.isStatic()) /* Fields aren't accessible directly in a static method, so don't link in this table */
183 md.getParameterTable().setParent(cd.getFieldTable());
185 if (!md.isStatic()) {
186 VarDescriptor thisvd=new VarDescriptor(new TypeDescriptor(cd),"this");
191 public void checkMethodBody(ClassDescriptor cd, MethodDescriptor md) {
192 System.out.println("Processing method:"+md);
193 BlockNode bn=state.getMethodBody(md);
194 checkBlockNode(md, md.getParameterTable(),bn);
197 public void checkBlockNode(Descriptor md, SymbolTable nametable, BlockNode bn) {
198 /* Link in the naming environment */
199 bn.getVarTable().setParent(nametable);
200 for(int i=0;i<bn.size();i++) {
201 BlockStatementNode bsn=bn.get(i);
202 checkBlockStatementNode(md, bn.getVarTable(),bsn);
206 public void checkBlockStatementNode(Descriptor md, SymbolTable nametable, BlockStatementNode bsn) {
208 case Kind.BlockExpressionNode:
209 checkBlockExpressionNode(md, nametable,(BlockExpressionNode)bsn);
212 case Kind.DeclarationNode:
213 checkDeclarationNode(md, nametable, (DeclarationNode)bsn);
216 case Kind.IfStatementNode:
217 checkIfStatementNode(md, nametable, (IfStatementNode)bsn);
221 checkLoopNode(md, nametable, (LoopNode)bsn);
224 case Kind.ReturnNode:
225 checkReturnNode(md, nametable, (ReturnNode)bsn);
228 case Kind.TaskExitNode:
229 checkTaskExitNode(md, nametable, (TaskExitNode)bsn);
232 case Kind.SubBlockNode:
233 checkSubBlockNode(md, nametable, (SubBlockNode)bsn);
239 void checkBlockExpressionNode(Descriptor md, SymbolTable nametable, BlockExpressionNode ben) {
240 checkExpressionNode(md, nametable, ben.getExpression(), null);
243 void checkDeclarationNode(Descriptor md, SymbolTable nametable, DeclarationNode dn) {
244 VarDescriptor vd=dn.getVarDescriptor();
245 checkTypeDescriptor(vd.getType());
246 Descriptor d=nametable.get(vd.getSymbol());
248 (d instanceof FieldDescriptor)) {
251 throw new Error(vd.getSymbol()+" defined a second time");
252 if (dn.getExpression()!=null)
253 checkExpressionNode(md, nametable, dn.getExpression(), vd.getType());
256 void checkSubBlockNode(Descriptor md, SymbolTable nametable, SubBlockNode sbn) {
257 checkBlockNode(md, nametable, sbn.getBlockNode());
260 void checkReturnNode(Descriptor d, SymbolTable nametable, ReturnNode rn) {
261 if (d instanceof TaskDescriptor)
262 throw new Error("Illegal return appears in Task: "+d.getSymbol());
263 MethodDescriptor md=(MethodDescriptor)d;
264 if (rn.getReturnExpression()!=null)
265 if (md.getReturnType().isVoid())
266 throw new Error(md+" is void");
268 checkExpressionNode(md, nametable, rn.getReturnExpression(), md.getReturnType());
270 if (md.getReturnType()!=null&&!md.getReturnType().isVoid())
271 throw new Error("Need to return something for "+md);
274 void checkTaskExitNode(Descriptor md, SymbolTable nametable, TaskExitNode ten) {
275 if (md instanceof MethodDescriptor)
276 throw new Error("Illegal taskexit appears in Method: "+md.getSymbol());
277 checkFlagEffects((TaskDescriptor)md, ten.getFlagEffects());
278 checkConstraintCheck((TaskDescriptor) md, nametable, ten.getChecks());
281 void checkIfStatementNode(Descriptor md, SymbolTable nametable, IfStatementNode isn) {
282 checkExpressionNode(md, nametable, isn.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
283 checkBlockNode(md, nametable, isn.getTrueBlock());
284 if (isn.getFalseBlock()!=null)
285 checkBlockNode(md, nametable, isn.getFalseBlock());
288 void checkExpressionNode(Descriptor md, SymbolTable nametable, ExpressionNode en, TypeDescriptor td) {
290 case Kind.AssignmentNode:
291 checkAssignmentNode(md,nametable,(AssignmentNode)en,td);
294 checkCastNode(md,nametable,(CastNode)en,td);
296 case Kind.CreateObjectNode:
297 checkCreateObjectNode(md,nametable,(CreateObjectNode)en,td);
299 case Kind.FieldAccessNode:
300 checkFieldAccessNode(md,nametable,(FieldAccessNode)en,td);
302 case Kind.ArrayAccessNode:
303 checkArrayAccessNode(md,nametable,(ArrayAccessNode)en,td);
305 case Kind.LiteralNode:
306 checkLiteralNode(md,nametable,(LiteralNode)en,td);
308 case Kind.MethodInvokeNode:
309 checkMethodInvokeNode(md,nametable,(MethodInvokeNode)en,td);
312 checkNameNode(md,nametable,(NameNode)en,td);
315 checkOpNode(md,nametable,(OpNode)en,td);
321 void checkCastNode(Descriptor md, SymbolTable nametable, CastNode cn, TypeDescriptor td) {
322 /* Get type descriptor */
323 if (cn.getType()==null) {
324 NameDescriptor typenamed=cn.getTypeName().getName();
325 String typename=typenamed.toString();
326 TypeDescriptor ntd=new TypeDescriptor(typeutil.getClass(typename));
330 /* Check the type descriptor */
331 TypeDescriptor cast_type=cn.getType();
332 checkTypeDescriptor(cast_type);
336 if (!typeutil.isSuperorType(td,cast_type))
337 throw new Error("Cast node returns "+cast_type+", but need "+td);
340 ExpressionNode en=cn.getExpression();
341 checkExpressionNode(md, nametable, en, null);
342 TypeDescriptor etd=en.getType();
343 if (typeutil.isSuperorType(cast_type,etd)) /* Cast trivially succeeds */
346 if (typeutil.isSuperorType(etd,cast_type)) /* Cast may succeed */
348 if (typeutil.isCastable(etd, cast_type))
351 /* Different branches */
352 /* TODO: change if add interfaces */
353 throw new Error("Cast will always fail\n"+cn.printNode(0));
356 void checkFieldAccessNode(Descriptor md, SymbolTable nametable, FieldAccessNode fan, TypeDescriptor td) {
357 ExpressionNode left=fan.getExpression();
358 checkExpressionNode(md,nametable,left,null);
359 TypeDescriptor ltd=left.getType();
360 String fieldname=fan.getFieldName();
362 FieldDescriptor fd=null;
363 if (ltd.isArray()&&fieldname.equals("length"))
364 fd=FieldDescriptor.arrayLength;
366 fd=(FieldDescriptor) ltd.getClassDesc().getFieldTable().get(fieldname);
368 throw new Error("Unknown field "+fieldname);
371 if (!typeutil.isSuperorType(td,fan.getType()))
372 throw new Error("Field node returns "+fan.getType()+", but need "+td);
375 void checkArrayAccessNode(Descriptor md, SymbolTable nametable, ArrayAccessNode aan, TypeDescriptor td) {
376 ExpressionNode left=aan.getExpression();
377 checkExpressionNode(md,nametable,left,null);
379 checkExpressionNode(md,nametable,aan.getIndex(),new TypeDescriptor(TypeDescriptor.INT));
380 TypeDescriptor ltd=left.getType();
383 if (!typeutil.isSuperorType(td,aan.getType()))
384 throw new Error("Field node returns "+aan.getType()+", but need "+td);
387 void checkLiteralNode(Descriptor md, SymbolTable nametable, LiteralNode ln, TypeDescriptor td) {
388 /* Resolve the type */
389 Object o=ln.getValue();
390 if (ln.getTypeString().equals("null")) {
391 ln.setType(new TypeDescriptor(TypeDescriptor.NULL));
392 } else if (o instanceof Integer) {
393 ln.setType(new TypeDescriptor(TypeDescriptor.INT));
394 } else if (o instanceof Long) {
395 ln.setType(new TypeDescriptor(TypeDescriptor.LONG));
396 } else if (o instanceof Float) {
397 ln.setType(new TypeDescriptor(TypeDescriptor.FLOAT));
398 } else if (o instanceof Boolean) {
399 ln.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
400 } else if (o instanceof Double) {
401 ln.setType(new TypeDescriptor(TypeDescriptor.DOUBLE));
402 } else if (o instanceof Character) {
403 ln.setType(new TypeDescriptor(TypeDescriptor.CHAR));
404 } else if (o instanceof String) {
405 ln.setType(new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass)));
409 if (!typeutil.isSuperorType(td,ln.getType()))
410 throw new Error("Field node returns "+ln.getType()+", but need "+td);
413 void checkNameNode(Descriptor md, SymbolTable nametable, NameNode nn, TypeDescriptor td) {
414 NameDescriptor nd=nn.getName();
415 if (nd.getBase()!=null) {
417 /* Rewrite NameNode */
418 ExpressionNode en=translateNameDescriptorintoExpression(nd);
419 nn.setExpression(en);
420 checkExpressionNode(md,nametable,en,td);
422 String varname=nd.toString();
423 Descriptor d=(Descriptor)nametable.get(varname);
425 throw new Error("Name "+varname+" undefined");
427 if (d instanceof VarDescriptor) {
428 nn.setVar((VarDescriptor)d);
429 } else if (d instanceof FieldDescriptor) {
430 nn.setField((FieldDescriptor)d);
431 nn.setVar((VarDescriptor)nametable.get("this")); /* Need a pointer to this */
434 if (!typeutil.isSuperorType(td,nn.getType()))
435 throw new Error("Field node returns "+nn.getType()+", but need "+td);
439 void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
440 boolean postinc=true;
441 if (an.getOperation().getBaseOp()==null||
442 (an.getOperation().getBaseOp().getOp()!=Operation.POSTINC&&
443 an.getOperation().getBaseOp().getOp()!=Operation.POSTDEC))
447 checkExpressionNode(md, nametable, an.getSrc() ,td);
448 //TODO: Need check on validity of operation here
449 if (!((an.getDest() instanceof FieldAccessNode)||
450 (an.getDest() instanceof ArrayAccessNode)||
451 (an.getDest() instanceof NameNode)))
452 throw new Error("Bad lside in "+an.printNode(0));
453 checkExpressionNode(md, nametable, an.getDest(), null);
455 /* We want parameter variables to tasks to be immutable */
456 if (md instanceof TaskDescriptor) {
457 if (an.getDest() instanceof NameNode) {
458 NameNode nn=(NameNode)an.getDest();
459 if (nn.getVar()!=null) {
460 if (((TaskDescriptor)md).getParameterTable().contains(nn.getVar().getSymbol()))
461 throw new Error("Can't modify parameter "+nn.getVar()+ " to task "+td.getSymbol());
466 if (!postinc&&!typeutil.isSuperorType(an.getDest().getType(),an.getSrc().getType())) {
467 throw new Error("Type of rside ("+an.getSrc().getType()+") not compatible with type of lside ("+an.getDest().getType()+")"+an.printNode(0));
471 void checkLoopNode(Descriptor md, SymbolTable nametable, LoopNode ln) {
472 if (ln.getType()==LoopNode.WHILELOOP||ln.getType()==LoopNode.DOWHILELOOP) {
473 checkExpressionNode(md, nametable, ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
474 checkBlockNode(md, nametable, ln.getBody());
477 /* Link in the initializer naming environment */
478 BlockNode bn=ln.getInitializer();
479 bn.getVarTable().setParent(nametable);
480 for(int i=0;i<bn.size();i++) {
481 BlockStatementNode bsn=bn.get(i);
482 checkBlockStatementNode(md, bn.getVarTable(),bsn);
484 //check the condition
485 checkExpressionNode(md, bn.getVarTable(), ln.getCondition(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
486 checkBlockNode(md, bn.getVarTable(), ln.getBody());
487 checkBlockNode(md, bn.getVarTable(), ln.getUpdate());
492 void checkCreateObjectNode(Descriptor md, SymbolTable nametable, CreateObjectNode con, TypeDescriptor td) {
493 TypeDescriptor[] tdarray=new TypeDescriptor[con.numArgs()];
494 for(int i=0;i<con.numArgs();i++) {
495 ExpressionNode en=con.getArg(i);
496 checkExpressionNode(md,nametable,en,null);
497 tdarray[i]=en.getType();
500 TypeDescriptor typetolookin=con.getType();
501 checkTypeDescriptor(typetolookin);
503 /* Check flag effects */
504 if (con.getFlagEffects()!=null) {
505 FlagEffects fe=con.getFlagEffects();
506 ClassDescriptor cd=typetolookin.getClassDesc();
508 for(int j=0;j<fe.numEffects();j++) {
509 FlagEffect flag=fe.getEffect(j);
510 String name=flag.getName();
511 FlagDescriptor flag_d=(FlagDescriptor)cd.getFlagTable().get(name);
512 //Make sure the flag is declared
514 throw new Error("Flag descriptor "+name+" undefined in class: "+cd.getSymbol());
515 if (flag_d.getExternal())
516 throw new Error("Attempting to modify external flag: "+name);
517 flag.setFlag(flag_d);
522 if ((!typetolookin.isClass())&&(!typetolookin.isArray()))
523 throw new Error("Can't allocate primitive type:"+con.printNode(0));
525 if (!typetolookin.isArray()) {
526 //Array's don't need constructor calls
527 ClassDescriptor classtolookin=typetolookin.getClassDesc();
528 System.out.println("Looking for "+typetolookin.getSymbol());
529 System.out.println(classtolookin.getMethodTable());
531 Set methoddescriptorset=classtolookin.getMethodTable().getSet(typetolookin.getSymbol());
532 MethodDescriptor bestmd=null;
534 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
535 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
536 /* Need correct number of parameters */
537 System.out.println("Examining: "+currmd);
538 if (con.numArgs()!=currmd.numParameters())
540 for(int i=0;i<con.numArgs();i++) {
541 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
544 /* Method okay so far */
548 if (isMoreSpecific(currmd,bestmd)) {
550 } else if (!isMoreSpecific(bestmd, currmd))
551 throw new Error("No method is most specific");
553 /* Is this more specific than bestmd */
557 throw new Error("No method found for "+con.printNode(0));
558 con.setConstructor(bestmd);
563 /** Check to see if md1 is more specific than md2... Informally
564 if md2 could always be called given the arguments passed into
567 boolean isMoreSpecific(MethodDescriptor md1, MethodDescriptor md2) {
568 /* Checks if md1 is more specific than md2 */
569 if (md1.numParameters()!=md2.numParameters())
571 for(int i=0;i<md1.numParameters();i++) {
572 if (!typeutil.isSuperorType(md2.getParamType(i), md1.getParamType(i)))
575 if (!typeutil.isSuperorType(md2.getReturnType(), md1.getReturnType()))
580 ExpressionNode translateNameDescriptorintoExpression(NameDescriptor nd) {
581 String id=nd.getIdentifier();
582 NameDescriptor base=nd.getBase();
584 return new NameNode(nd);
586 return new FieldAccessNode(translateNameDescriptorintoExpression(base),id);
590 void checkMethodInvokeNode(Descriptor md, SymbolTable nametable, MethodInvokeNode min, TypeDescriptor td) {
591 /*Typecheck subexpressions
592 and get types for expressions*/
594 TypeDescriptor[] tdarray=new TypeDescriptor[min.numArgs()];
595 for(int i=0;i<min.numArgs();i++) {
596 ExpressionNode en=min.getArg(i);
597 checkExpressionNode(md,nametable,en,null);
598 tdarray[i]=en.getType();
600 TypeDescriptor typetolookin=null;
601 if (min.getExpression()!=null) {
602 checkExpressionNode(md,nametable,min.getExpression(),null);
603 typetolookin=min.getExpression().getType();
604 } else if (min.getBaseName()!=null) {
605 String rootname=min.getBaseName().getRoot();
606 if (nametable.get(rootname)!=null) {
607 //we have an expression
608 min.setExpression(translateNameDescriptorintoExpression(min.getBaseName()));
609 checkExpressionNode(md, nametable, min.getExpression(), null);
610 typetolookin=min.getExpression().getType();
613 ClassDescriptor cd=typeutil.getClass(min.getBaseName().getSymbol());
615 throw new Error(min.getBaseName()+" undefined");
616 typetolookin=new TypeDescriptor(cd);
618 } else if (md instanceof MethodDescriptor) {
619 typetolookin=new TypeDescriptor(((MethodDescriptor)md).getClassDesc());
621 /* If this a task descriptor we throw an error at this point */
622 throw new Error("Unknown method call to "+min.getMethodName()+"in task"+md.getSymbol());
624 if (!typetolookin.isClass())
626 ClassDescriptor classtolookin=typetolookin.getClassDesc();
627 System.out.println("Method name="+min.getMethodName());
628 Set methoddescriptorset=classtolookin.getMethodTable().getSet(min.getMethodName());
629 MethodDescriptor bestmd=null;
631 for(Iterator methodit=methoddescriptorset.iterator();methodit.hasNext();) {
632 MethodDescriptor currmd=(MethodDescriptor)methodit.next();
633 /* Need correct number of parameters */
634 if (min.numArgs()!=currmd.numParameters())
636 for(int i=0;i<min.numArgs();i++) {
637 if (!typeutil.isSuperorType(currmd.getParamType(i),tdarray[i]))
640 /* Method okay so far */
644 if (isMoreSpecific(currmd,bestmd)) {
646 } else if (!isMoreSpecific(bestmd, currmd))
647 throw new Error("No method is most specific");
649 /* Is this more specific than bestmd */
653 throw new Error("No method found for :"+min.printNode(0));
654 min.setMethod(bestmd);
655 /* Check whether we need to set this parameter to implied this */
656 if (!bestmd.isStatic()) {
657 if (min.getExpression()==null) {
658 ExpressionNode en=new NameNode(new NameDescriptor("this"));
659 min.setExpression(en);
660 checkExpressionNode(md, nametable, min.getExpression(), null);
667 void checkOpNode(Descriptor md, SymbolTable nametable, OpNode on, TypeDescriptor td) {
668 checkExpressionNode(md, nametable, on.getLeft(), null);
669 if (on.getRight()!=null)
670 checkExpressionNode(md, nametable, on.getRight(), null);
671 TypeDescriptor ltd=on.getLeft().getType();
672 TypeDescriptor rtd=on.getRight()!=null?on.getRight().getType():null;
673 TypeDescriptor lefttype=null;
674 TypeDescriptor righttype=null;
675 Operation op=on.getOp();
678 case Operation.LOGIC_OR:
679 case Operation.LOGIC_AND:
680 if (!(ltd.isBoolean()&&rtd.isBoolean()))
684 on.setRightType(rtd);
685 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
688 case Operation.BIT_OR:
689 case Operation.BIT_XOR:
690 case Operation.BIT_AND:
691 // 5.6.2 Binary Numeric Promotion
692 //TODO unboxing of reference objects
693 if (ltd.isDouble()||rtd.isDouble())
695 else if (ltd.isFloat()||rtd.isFloat())
697 else if (ltd.isLong()||rtd.isLong())
698 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
700 lefttype=new TypeDescriptor(TypeDescriptor.INT);
703 on.setLeftType(lefttype);
704 on.setRightType(righttype);
705 on.setType(lefttype);
708 case Operation.EQUAL:
709 case Operation.NOTEQUAL:
710 // 5.6.2 Binary Numeric Promotion
711 //TODO unboxing of reference objects
712 if (ltd.isBoolean()||rtd.isBoolean()) {
713 if (!(ltd.isBoolean()&&rtd.isBoolean()))
715 righttype=lefttype=new TypeDescriptor(TypeDescriptor.BOOLEAN);
716 } else if (ltd.isPtr()||ltd.isArray()||rtd.isPtr()||rtd.isArray()) {
717 if (!((ltd.isPtr()||ltd.isArray())&&(rtd.isPtr()||rtd.isArray())))
721 } else if (ltd.isDouble()||rtd.isDouble())
722 righttype=lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
723 else if (ltd.isFloat()||rtd.isFloat())
724 righttype=lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
725 else if (ltd.isLong()||rtd.isLong())
726 righttype=lefttype=new TypeDescriptor(TypeDescriptor.LONG);
728 righttype=lefttype=new TypeDescriptor(TypeDescriptor.INT);
730 on.setLeftType(lefttype);
731 on.setRightType(righttype);
732 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
741 // 5.6.2 Binary Numeric Promotion
742 //TODO unboxing of reference objects
743 if (!ltd.isNumber()||!rtd.isNumber())
746 if (ltd.isDouble()||rtd.isDouble())
747 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
748 else if (ltd.isFloat()||rtd.isFloat())
749 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
750 else if (ltd.isLong()||rtd.isLong())
751 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
753 lefttype=new TypeDescriptor(TypeDescriptor.INT);
755 on.setLeftType(lefttype);
756 on.setRightType(righttype);
757 on.setType(new TypeDescriptor(TypeDescriptor.BOOLEAN));
761 //TODO: Need special case for strings eventually
768 // 5.6.2 Binary Numeric Promotion
769 //TODO unboxing of reference objects
770 if (!ltd.isNumber()||!rtd.isNumber())
771 throw new Error("Error in "+on.printNode(0));
773 if (ltd.isDouble()||rtd.isDouble())
774 lefttype=new TypeDescriptor(TypeDescriptor.DOUBLE);
775 else if (ltd.isFloat()||rtd.isFloat())
776 lefttype=new TypeDescriptor(TypeDescriptor.FLOAT);
777 else if (ltd.isLong()||rtd.isLong())
778 lefttype=new TypeDescriptor(TypeDescriptor.LONG);
780 lefttype=new TypeDescriptor(TypeDescriptor.INT);
782 on.setLeftType(lefttype);
783 on.setRightType(righttype);
784 on.setType(lefttype);
787 case Operation.LEFTSHIFT:
788 case Operation.RIGHTSHIFT:
789 if (!rtd.isIntegerType())
791 //5.6.1 Unary Numeric Promotion
792 if (rtd.isByte()||rtd.isShort()||rtd.isInt())
793 righttype=new TypeDescriptor(TypeDescriptor.INT);
797 on.setRightType(righttype);
798 if (!ltd.isIntegerType())
800 case Operation.UNARYPLUS:
801 case Operation.UNARYMINUS:
802 /* case Operation.POSTINC:
803 case Operation.POSTDEC:
804 case Operation.PREINC:
805 case Operation.PREDEC:*/
808 //5.6.1 Unary Numeric Promotion
809 if (ltd.isByte()||ltd.isShort()||ltd.isInt())
810 lefttype=new TypeDescriptor(TypeDescriptor.INT);
813 on.setLeftType(lefttype);
814 on.setType(lefttype);
821 if (!typeutil.isSuperorType(td, on.getType())) {
822 System.out.println(td);
823 System.out.println(on.getType());
824 throw new Error("Type of rside not compatible with type of lside"+on.printNode(0));